blob: ef2cee5de62b2b1d8da91e7ef5719c7a7f4103e6 [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>
hayeswange9746042014-07-11 16:25:58 +080030#include <linux/ipv6.h>
31#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
françois romieubca03d52011-01-03 15:07:31 +000040#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000042#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080044#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080045#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
46#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080047#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080048#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080049#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080050#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080051#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000052#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000053#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000054#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
françois romieubca03d52011-01-03 15:07:31 +000055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#ifdef RTL8169_DEBUG
57#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020058 if (!(expr)) { \
59 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070060 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020061 }
Joe Perches06fa7352007-10-18 21:15:00 +020062#define dprintk(fmt, args...) \
63 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#else
65#define assert(expr) do {} while (0)
66#define dprintk(fmt, args...) do {} while (0)
67#endif /* RTL8169_DEBUG */
68
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020069#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070070 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020071
Julien Ducourthial477206a2012-05-09 00:00:06 +020072#define TX_SLOTS_AVAIL(tp) \
73 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
74
75/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
76#define TX_FRAGS_READY_FOR(tp,nr_frags) \
77 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
80 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050081static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Francois Romieu9c14cea2008-07-05 00:21:15 +020083#define MAX_READ_REQUEST_SHIFT 12
Michal Schmidtaee77e42012-09-09 13:55:26 +000084#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
86
87#define R8169_REGS_SIZE 256
88#define R8169_NAPI_WEIGHT 64
89#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000090#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
92#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
93
94#define RTL8169_TX_TIMEOUT (6*HZ)
95#define RTL8169_PHY_TIMEOUT (10*HZ)
96
97/* write/read MMIO register */
98#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
99#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
100#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
101#define RTL_R8(reg) readb (ioaddr + (reg))
102#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +0000103#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200106 RTL_GIGA_MAC_VER_01 = 0,
107 RTL_GIGA_MAC_VER_02,
108 RTL_GIGA_MAC_VER_03,
109 RTL_GIGA_MAC_VER_04,
110 RTL_GIGA_MAC_VER_05,
111 RTL_GIGA_MAC_VER_06,
112 RTL_GIGA_MAC_VER_07,
113 RTL_GIGA_MAC_VER_08,
114 RTL_GIGA_MAC_VER_09,
115 RTL_GIGA_MAC_VER_10,
116 RTL_GIGA_MAC_VER_11,
117 RTL_GIGA_MAC_VER_12,
118 RTL_GIGA_MAC_VER_13,
119 RTL_GIGA_MAC_VER_14,
120 RTL_GIGA_MAC_VER_15,
121 RTL_GIGA_MAC_VER_16,
122 RTL_GIGA_MAC_VER_17,
123 RTL_GIGA_MAC_VER_18,
124 RTL_GIGA_MAC_VER_19,
125 RTL_GIGA_MAC_VER_20,
126 RTL_GIGA_MAC_VER_21,
127 RTL_GIGA_MAC_VER_22,
128 RTL_GIGA_MAC_VER_23,
129 RTL_GIGA_MAC_VER_24,
130 RTL_GIGA_MAC_VER_25,
131 RTL_GIGA_MAC_VER_26,
132 RTL_GIGA_MAC_VER_27,
133 RTL_GIGA_MAC_VER_28,
134 RTL_GIGA_MAC_VER_29,
135 RTL_GIGA_MAC_VER_30,
136 RTL_GIGA_MAC_VER_31,
137 RTL_GIGA_MAC_VER_32,
138 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800139 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800140 RTL_GIGA_MAC_VER_35,
141 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800142 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800143 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800144 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800145 RTL_GIGA_MAC_VER_40,
146 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000147 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000148 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800149 RTL_GIGA_MAC_VER_44,
Francois Romieu85bffe62011-04-27 08:22:39 +0200150 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Francois Romieu2b7b4312011-04-18 22:53:24 -0700153enum rtl_tx_desc_version {
154 RTL_TD_0 = 0,
155 RTL_TD_1 = 1,
156};
157
Francois Romieud58d46b2011-05-03 16:38:29 +0200158#define JUMBO_1K ETH_DATA_LEN
159#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
160#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
161#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
162#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
163
164#define _R(NAME,TD,FW,SZ,B) { \
165 .name = NAME, \
166 .txd_version = TD, \
167 .fw_name = FW, \
168 .jumbo_max = SZ, \
169 .jumbo_tx_csum = B \
170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800172static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700174 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200175 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200176 u16 jumbo_max;
177 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200178} rtl_chip_infos[] = {
179 /* PCI devices. */
180 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200181 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200182 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200183 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200184 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200185 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200186 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200187 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200188 [RTL_GIGA_MAC_VER_05] =
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 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200191 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200192 /* PCI-E devices. */
193 [RTL_GIGA_MAC_VER_07] =
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_08] =
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_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200198 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200199 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200200 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200201 [RTL_GIGA_MAC_VER_11] =
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_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200204 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200205 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200206 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200207 [RTL_GIGA_MAC_VER_14] =
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_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200210 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200211 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200212 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200213 [RTL_GIGA_MAC_VER_17] =
hayeswangf75761b2014-03-11 15:11:59 +0800214 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200215 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200216 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200217 [RTL_GIGA_MAC_VER_19] =
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_20] =
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_21] =
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_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200224 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200225 [RTL_GIGA_MAC_VER_23] =
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_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200228 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200229 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200230 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
231 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200232 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200233 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
234 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200235 [RTL_GIGA_MAC_VER_27] =
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_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200238 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200239 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200240 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
241 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200242 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200243 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
244 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200245 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200246 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200247 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200248 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
249 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200250 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200251 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
252 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800253 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200254 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
255 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800256 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200257 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
258 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800259 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200260 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
261 JUMBO_9K, false),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800262 [RTL_GIGA_MAC_VER_37] =
263 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
264 JUMBO_1K, true),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800265 [RTL_GIGA_MAC_VER_38] =
266 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
267 JUMBO_9K, false),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800268 [RTL_GIGA_MAC_VER_39] =
269 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
270 JUMBO_1K, true),
Hayes Wangc5583862012-07-02 17:23:22 +0800271 [RTL_GIGA_MAC_VER_40] =
hayeswangbeb330a2013-04-01 22:23:39 +0000272 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
Hayes Wangc5583862012-07-02 17:23:22 +0800273 JUMBO_9K, false),
274 [RTL_GIGA_MAC_VER_41] =
275 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
hayeswang57538c42013-04-01 22:23:40 +0000276 [RTL_GIGA_MAC_VER_42] =
277 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3,
278 JUMBO_9K, false),
hayeswang58152cd2013-04-01 22:23:42 +0000279 [RTL_GIGA_MAC_VER_43] =
280 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2,
281 JUMBO_1K, true),
hayeswang45dd95c2013-07-08 17:09:01 +0800282 [RTL_GIGA_MAC_VER_44] =
283 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2,
284 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285};
286#undef _R
287
Francois Romieubcf0bf92006-07-26 23:14:13 +0200288enum cfg_version {
289 RTL_CFG_0 = 0x00,
290 RTL_CFG_1,
291 RTL_CFG_2
292};
293
Benoit Taine9baa3c32014-08-08 15:56:03 +0200294static const struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200295 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200296 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200297 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100298 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200299 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200300 { PCI_VENDOR_ID_DLINK, 0x4300,
301 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200302 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000303 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200304 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200305 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
306 { PCI_VENDOR_ID_LINKSYS, 0x1032,
307 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100308 { 0x0001, 0x8168,
309 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 {0,},
311};
312
313MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
314
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000315static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700316static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200317static struct {
318 u32 msg_enable;
319} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Francois Romieu07d3f512007-02-21 22:40:46 +0100321enum rtl_registers {
322 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100323 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100324 MAR0 = 8, /* Multicast filter. */
325 CounterAddrLow = 0x10,
326 CounterAddrHigh = 0x14,
327 TxDescStartAddrLow = 0x20,
328 TxDescStartAddrHigh = 0x24,
329 TxHDescStartAddrLow = 0x28,
330 TxHDescStartAddrHigh = 0x2c,
331 FLASH = 0x30,
332 ERSR = 0x36,
333 ChipCmd = 0x37,
334 TxPoll = 0x38,
335 IntrMask = 0x3c,
336 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700337
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800338 TxConfig = 0x40,
339#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
340#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
341
342 RxConfig = 0x44,
343#define RX128_INT_EN (1 << 15) /* 8111c and later */
344#define RX_MULTI_EN (1 << 14) /* 8111c only */
345#define RXCFG_FIFO_SHIFT 13
346 /* No threshold before first PCI xfer */
347#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000348#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800349#define RXCFG_DMA_SHIFT 8
350 /* Unlimited maximum PCI burst. */
351#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700352
Francois Romieu07d3f512007-02-21 22:40:46 +0100353 RxMissed = 0x4c,
354 Cfg9346 = 0x50,
355 Config0 = 0x51,
356 Config1 = 0x52,
357 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200358#define PME_SIGNAL (1 << 5) /* 8168c and later */
359
Francois Romieu07d3f512007-02-21 22:40:46 +0100360 Config3 = 0x54,
361 Config4 = 0x55,
362 Config5 = 0x56,
363 MultiIntr = 0x5c,
364 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100365 PHYstatus = 0x6c,
366 RxMaxSize = 0xda,
367 CPlusCmd = 0xe0,
368 IntrMitigate = 0xe2,
369 RxDescAddrLow = 0xe4,
370 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000371 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
372
373#define NoEarlyTx 0x3f /* Max value : no early transmit. */
374
375 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
376
377#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800378#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000379
Francois Romieu07d3f512007-02-21 22:40:46 +0100380 FuncEvent = 0xf0,
381 FuncEventMask = 0xf4,
382 FuncPresetState = 0xf8,
383 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384};
385
Francois Romieuf162a5d2008-06-01 22:37:49 +0200386enum rtl8110_registers {
387 TBICSR = 0x64,
388 TBI_ANAR = 0x68,
389 TBI_LPAR = 0x6a,
390};
391
392enum rtl8168_8101_registers {
393 CSIDR = 0x64,
394 CSIAR = 0x68,
395#define CSIAR_FLAG 0x80000000
396#define CSIAR_WRITE_CMD 0x80000000
397#define CSIAR_BYTE_ENABLE 0x0f
398#define CSIAR_BYTE_ENABLE_SHIFT 12
399#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800400#define CSIAR_FUNC_CARD 0x00000000
401#define CSIAR_FUNC_SDIO 0x00010000
402#define CSIAR_FUNC_NIC 0x00020000
hayeswang45dd95c2013-07-08 17:09:01 +0800403#define CSIAR_FUNC_NIC2 0x00010000
françois romieu065c27c2011-01-03 15:08:12 +0000404 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200405 EPHYAR = 0x80,
406#define EPHYAR_FLAG 0x80000000
407#define EPHYAR_WRITE_CMD 0x80000000
408#define EPHYAR_REG_MASK 0x1f
409#define EPHYAR_REG_SHIFT 16
410#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800411 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800412#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200413 DBG_REG = 0xd1,
414#define FIX_NAK_1 (1 << 4)
415#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800416 TWSI = 0xd2,
417 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800418#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800419#define TX_EMPTY (1 << 5)
420#define RX_EMPTY (1 << 4)
421#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800422#define EN_NDP (1 << 3)
423#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800424#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000425 EFUSEAR = 0xdc,
426#define EFUSEAR_FLAG 0x80000000
427#define EFUSEAR_WRITE_CMD 0x80000000
428#define EFUSEAR_READ_CMD 0x00000000
429#define EFUSEAR_REG_MASK 0x03ff
430#define EFUSEAR_REG_SHIFT 8
431#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200432};
433
françois romieuc0e45c12011-01-03 15:08:04 +0000434enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800435 LED_FREQ = 0x1a,
436 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000437 ERIDR = 0x70,
438 ERIAR = 0x74,
439#define ERIAR_FLAG 0x80000000
440#define ERIAR_WRITE_CMD 0x80000000
441#define ERIAR_READ_CMD 0x00000000
442#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000443#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800444#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
445#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
446#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
447#define ERIAR_MASK_SHIFT 12
448#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
449#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800450#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800451#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000452 EPHY_RXER_NUM = 0x7c,
453 OCPDR = 0xb0, /* OCP GPHY access */
454#define OCPDR_WRITE_CMD 0x80000000
455#define OCPDR_READ_CMD 0x00000000
456#define OCPDR_REG_MASK 0x7f
457#define OCPDR_GPHY_REG_SHIFT 16
458#define OCPDR_DATA_MASK 0xffff
459 OCPAR = 0xb4,
460#define OCPAR_FLAG 0x80000000
461#define OCPAR_GPHY_WRITE_CMD 0x8000f060
462#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800463 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000464 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
465 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200466#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800467#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800468#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800469#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800470#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000471};
472
Francois Romieu07d3f512007-02-21 22:40:46 +0100473enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100475 SYSErr = 0x8000,
476 PCSTimeout = 0x4000,
477 SWInt = 0x0100,
478 TxDescUnavail = 0x0080,
479 RxFIFOOver = 0x0040,
480 LinkChg = 0x0020,
481 RxOverflow = 0x0010,
482 TxErr = 0x0008,
483 TxOK = 0x0004,
484 RxErr = 0x0002,
485 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400488 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200489 RxFOVF = (1 << 23),
490 RxRWT = (1 << 22),
491 RxRES = (1 << 21),
492 RxRUNT = (1 << 20),
493 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800496 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100497 CmdReset = 0x10,
498 CmdRxEnb = 0x08,
499 CmdTxEnb = 0x04,
500 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Francois Romieu275391a2007-02-23 23:50:28 +0100502 /* TXPoll register p.5 */
503 HPQ = 0x80, /* Poll cmd on the high prio queue */
504 NPQ = 0x40, /* Poll cmd on the low prio queue */
505 FSWInt = 0x01, /* Forced software interrupt */
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100508 Cfg9346_Lock = 0x00,
509 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100512 AcceptErr = 0x20,
513 AcceptRunt = 0x10,
514 AcceptBroadcast = 0x08,
515 AcceptMulticast = 0x04,
516 AcceptMyPhys = 0x02,
517 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200518#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* TxConfigBits */
521 TxInterFrameGapShift = 24,
522 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
523
Francois Romieu5d06a992006-02-23 00:47:58 +0100524 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200525 LEDS1 = (1 << 7),
526 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200527 Speed_down = (1 << 4),
528 MEMMAP = (1 << 3),
529 IOMAP = (1 << 2),
530 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100531 PMEnable = (1 << 0), /* Power Management Enable */
532
Francois Romieu6dccd162007-02-13 23:38:05 +0100533 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000534 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000535 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100536 PCI_Clock_66MHz = 0x01,
537 PCI_Clock_33MHz = 0x00,
538
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100539 /* Config3 register p.25 */
540 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
541 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200542 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800543 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200544 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100545
Francois Romieud58d46b2011-05-03 16:38:29 +0200546 /* Config4 register */
547 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
548
Francois Romieu5d06a992006-02-23 00:47:58 +0100549 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100550 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
551 MWF = (1 << 5), /* Accept Multicast wakeup frame */
552 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200553 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100554 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100555 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000556 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /* TBICSR p.28 */
559 TBIReset = 0x80000000,
560 TBILoopback = 0x40000000,
561 TBINwEnable = 0x20000000,
562 TBINwRestart = 0x10000000,
563 TBILinkOk = 0x02000000,
564 TBINwComplete = 0x01000000,
565
566 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200567 EnableBist = (1 << 15), // 8168 8101
568 Mac_dbgo_oe = (1 << 14), // 8168 8101
569 Normal_mode = (1 << 13), // unused
570 Force_half_dup = (1 << 12), // 8168 8101
571 Force_rxflow_en = (1 << 11), // 8168 8101
572 Force_txflow_en = (1 << 10), // 8168 8101
573 Cxpl_dbg_sel = (1 << 9), // 8168 8101
574 ASF = (1 << 8), // 8168 8101
575 PktCntrDisable = (1 << 7), // 8168 8101
576 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 RxVlan = (1 << 6),
578 RxChkSum = (1 << 5),
579 PCIDAC = (1 << 4),
580 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100581 INTT_0 = 0x0000, // 8168
582 INTT_1 = 0x0001, // 8168
583 INTT_2 = 0x0002, // 8168
584 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100587 TBI_Enable = 0x80,
588 TxFlowCtrl = 0x40,
589 RxFlowCtrl = 0x20,
590 _1000bpsF = 0x10,
591 _100bps = 0x08,
592 _10bps = 0x04,
593 LinkStatus = 0x02,
594 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100597 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200598
599 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100600 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601};
602
Francois Romieu2b7b4312011-04-18 22:53:24 -0700603enum rtl_desc_bit {
604 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
606 RingEnd = (1 << 30), /* End of descriptor ring */
607 FirstFrag = (1 << 29), /* First segment of a packet */
608 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700609};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Francois Romieu2b7b4312011-04-18 22:53:24 -0700611/* Generic case. */
612enum rtl_tx_desc_bit {
613 /* First doubleword. */
614 TD_LSO = (1 << 27), /* Large Send Offload */
615#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Francois Romieu2b7b4312011-04-18 22:53:24 -0700617 /* Second doubleword. */
618 TxVlanTag = (1 << 17), /* Add VLAN tag */
619};
620
621/* 8169, 8168b and 810x except 8102e. */
622enum rtl_tx_desc_bit_0 {
623 /* First doubleword. */
624#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
625 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
626 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
627 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
628};
629
630/* 8102e, 8168c and beyond. */
631enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800632 /* First doubleword. */
633 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800634 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800635#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800636#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800637
Francois Romieu2b7b4312011-04-18 22:53:24 -0700638 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800639#define TCPHO_SHIFT 18
640#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700641#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800642 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
643 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700644 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
645 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
646};
647
Francois Romieu2b7b4312011-04-18 22:53:24 -0700648enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /* Rx private */
650 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
651 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
652
653#define RxProtoUDP (PID1)
654#define RxProtoTCP (PID0)
655#define RxProtoIP (PID1 | PID0)
656#define RxProtoMask RxProtoIP
657
658 IPFail = (1 << 16), /* IP checksum failed */
659 UDPFail = (1 << 15), /* UDP/IP checksum failed */
660 TCPFail = (1 << 14), /* TCP/IP checksum failed */
661 RxVlanTag = (1 << 16), /* VLAN tag available */
662};
663
664#define RsvdMask 0x3fffc000
665
666struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200667 __le32 opts1;
668 __le32 opts2;
669 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670};
671
672struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200673 __le32 opts1;
674 __le32 opts2;
675 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676};
677
678struct ring_info {
679 struct sk_buff *skb;
680 u32 len;
681 u8 __pad[sizeof(void *) - sizeof(u32)];
682};
683
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200684enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200685 RTL_FEATURE_WOL = (1 << 0),
686 RTL_FEATURE_MSI = (1 << 1),
687 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200688};
689
Ivan Vecera355423d2009-02-06 21:49:57 -0800690struct rtl8169_counters {
691 __le64 tx_packets;
692 __le64 rx_packets;
693 __le64 tx_errors;
694 __le32 rx_errors;
695 __le16 rx_missed;
696 __le16 align_errors;
697 __le32 tx_one_collision;
698 __le32 tx_multi_collision;
699 __le64 rx_unicast;
700 __le64 rx_broadcast;
701 __le32 rx_multicast;
702 __le16 tx_aborted;
703 __le16 tx_underun;
704};
705
Francois Romieuda78dbf2012-01-26 14:18:23 +0100706enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100707 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100708 RTL_FLAG_TASK_SLOW_PENDING,
709 RTL_FLAG_TASK_RESET_PENDING,
710 RTL_FLAG_TASK_PHY_PENDING,
711 RTL_FLAG_MAX
712};
713
Junchang Wang8027aa22012-03-04 23:30:32 +0100714struct rtl8169_stats {
715 u64 packets;
716 u64 bytes;
717 struct u64_stats_sync syncp;
718};
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720struct rtl8169_private {
721 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200722 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000723 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700724 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200725 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700726 u16 txd_version;
727 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
729 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100731 struct rtl8169_stats rx_stats;
732 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
734 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
735 dma_addr_t TxPhyAddr;
736 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000737 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 struct timer_list timer;
740 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100741
742 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000743
744 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200745 void (*write)(struct rtl8169_private *, int, int);
746 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000747 } mdio_ops;
748
françois romieu065c27c2011-01-03 15:08:12 +0000749 struct pll_power_ops {
750 void (*down)(struct rtl8169_private *);
751 void (*up)(struct rtl8169_private *);
752 } pll_power_ops;
753
Francois Romieud58d46b2011-05-03 16:38:29 +0200754 struct jumbo_ops {
755 void (*enable)(struct rtl8169_private *);
756 void (*disable)(struct rtl8169_private *);
757 } jumbo_ops;
758
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800759 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200760 void (*write)(struct rtl8169_private *, int, int);
761 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800762 } csi_ops;
763
Oliver Neukum54405cd2011-01-06 21:55:13 +0100764 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200765 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000766 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100767 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000768 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800770 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800771 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100772
773 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100774 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
775 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100776 struct work_struct work;
777 } wk;
778
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200779 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200780
781 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800782 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000783 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400784 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000785
Francois Romieub6ffd972011-06-17 17:00:05 +0200786 struct rtl_fw {
787 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200788
789#define RTL_VER_SIZE 32
790
791 char version[RTL_VER_SIZE];
792
793 struct rtl_fw_phy_action {
794 __le32 *code;
795 size_t size;
796 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200797 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300798#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800799
800 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801};
802
Ralf Baechle979b6c12005-06-13 14:30:40 -0700803MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700806MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200807module_param_named(debug, debug.msg_enable, int, 0);
808MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809MODULE_LICENSE("GPL");
810MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000811MODULE_FIRMWARE(FIRMWARE_8168D_1);
812MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000813MODULE_FIRMWARE(FIRMWARE_8168E_1);
814MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400815MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800816MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800817MODULE_FIRMWARE(FIRMWARE_8168F_1);
818MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800819MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800820MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800821MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800822MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000823MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000824MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000825MODULE_FIRMWARE(FIRMWARE_8168G_3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Francois Romieuda78dbf2012-01-26 14:18:23 +0100827static void rtl_lock_work(struct rtl8169_private *tp)
828{
829 mutex_lock(&tp->wk.mutex);
830}
831
832static void rtl_unlock_work(struct rtl8169_private *tp)
833{
834 mutex_unlock(&tp->wk.mutex);
835}
836
Francois Romieud58d46b2011-05-03 16:38:29 +0200837static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
838{
Jiang Liu7d7903b2012-07-24 17:20:16 +0800839 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
840 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200841}
842
Francois Romieuffc46952012-07-06 14:19:23 +0200843struct rtl_cond {
844 bool (*check)(struct rtl8169_private *);
845 const char *msg;
846};
847
848static void rtl_udelay(unsigned int d)
849{
850 udelay(d);
851}
852
853static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
854 void (*delay)(unsigned int), unsigned int d, int n,
855 bool high)
856{
857 int i;
858
859 for (i = 0; i < n; i++) {
860 delay(d);
861 if (c->check(tp) == high)
862 return true;
863 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200864 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
865 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200866 return false;
867}
868
869static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
870 const struct rtl_cond *c,
871 unsigned int d, int n)
872{
873 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
874}
875
876static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
877 const struct rtl_cond *c,
878 unsigned int d, int n)
879{
880 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
881}
882
883static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
884 const struct rtl_cond *c,
885 unsigned int d, int n)
886{
887 return rtl_loop_wait(tp, c, msleep, d, n, true);
888}
889
890static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
891 const struct rtl_cond *c,
892 unsigned int d, int n)
893{
894 return rtl_loop_wait(tp, c, msleep, d, n, false);
895}
896
897#define DECLARE_RTL_COND(name) \
898static bool name ## _check(struct rtl8169_private *); \
899 \
900static const struct rtl_cond name = { \
901 .check = name ## _check, \
902 .msg = #name \
903}; \
904 \
905static bool name ## _check(struct rtl8169_private *tp)
906
907DECLARE_RTL_COND(rtl_ocpar_cond)
908{
909 void __iomem *ioaddr = tp->mmio_addr;
910
911 return RTL_R32(OCPAR) & OCPAR_FLAG;
912}
913
françois romieub646d902011-01-03 15:08:21 +0000914static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
915{
916 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000917
918 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Francois Romieuffc46952012-07-06 14:19:23 +0200919
920 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
921 RTL_R32(OCPDR) : ~0;
françois romieub646d902011-01-03 15:08:21 +0000922}
923
924static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
925{
926 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000927
928 RTL_W32(OCPDR, data);
929 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Francois Romieuffc46952012-07-06 14:19:23 +0200930
931 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
932}
933
934DECLARE_RTL_COND(rtl_eriar_cond)
935{
936 void __iomem *ioaddr = tp->mmio_addr;
937
938 return RTL_R32(ERIAR) & ERIAR_FLAG;
françois romieub646d902011-01-03 15:08:21 +0000939}
940
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800941static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000942{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800943 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000944
945 RTL_W8(ERIDR, cmd);
946 RTL_W32(ERIAR, 0x800010e8);
947 msleep(2);
Francois Romieuffc46952012-07-06 14:19:23 +0200948
949 if (!rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 5))
950 return;
françois romieub646d902011-01-03 15:08:21 +0000951
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800952 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000953}
954
955#define OOB_CMD_RESET 0x00
956#define OOB_CMD_DRIVER_START 0x05
957#define OOB_CMD_DRIVER_STOP 0x06
958
Francois Romieucecb5fd2011-04-01 10:21:07 +0200959static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
960{
961 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
962}
963
Francois Romieuffc46952012-07-06 14:19:23 +0200964DECLARE_RTL_COND(rtl_ocp_read_cond)
françois romieub646d902011-01-03 15:08:21 +0000965{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200966 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000967
Francois Romieucecb5fd2011-04-01 10:21:07 +0200968 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000969
Francois Romieuffc46952012-07-06 14:19:23 +0200970 return ocp_read(tp, 0x0f, reg) & 0x00000800;
971}
972
973static void rtl8168_driver_start(struct rtl8169_private *tp)
974{
975 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
976
977 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
françois romieub646d902011-01-03 15:08:21 +0000978}
979
980static void rtl8168_driver_stop(struct rtl8169_private *tp)
981{
françois romieub646d902011-01-03 15:08:21 +0000982 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
983
Francois Romieuffc46952012-07-06 14:19:23 +0200984 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
françois romieub646d902011-01-03 15:08:21 +0000985}
986
hayeswang4804b3b2011-03-21 01:50:29 +0000987static int r8168dp_check_dash(struct rtl8169_private *tp)
988{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200989 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000990
Francois Romieucecb5fd2011-04-01 10:21:07 +0200991 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000992}
françois romieub646d902011-01-03 15:08:21 +0000993
Hayes Wangc5583862012-07-02 17:23:22 +0800994static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
995{
996 if (reg & 0xffff0001) {
997 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
998 return true;
999 }
1000 return false;
1001}
1002
1003DECLARE_RTL_COND(rtl_ocp_gphy_cond)
1004{
1005 void __iomem *ioaddr = tp->mmio_addr;
1006
1007 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
1008}
1009
1010static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1011{
1012 void __iomem *ioaddr = tp->mmio_addr;
1013
1014 if (rtl_ocp_reg_failure(tp, reg))
1015 return;
1016
1017 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
1018
1019 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
1020}
1021
1022static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
1023{
1024 void __iomem *ioaddr = tp->mmio_addr;
1025
1026 if (rtl_ocp_reg_failure(tp, reg))
1027 return 0;
1028
1029 RTL_W32(GPHY_OCP, reg << 15);
1030
1031 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1032 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
1033}
1034
Hayes Wangc5583862012-07-02 17:23:22 +08001035static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1036{
1037 void __iomem *ioaddr = tp->mmio_addr;
1038
1039 if (rtl_ocp_reg_failure(tp, reg))
1040 return;
1041
1042 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +08001043}
1044
1045static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1046{
1047 void __iomem *ioaddr = tp->mmio_addr;
1048
1049 if (rtl_ocp_reg_failure(tp, reg))
1050 return 0;
1051
1052 RTL_W32(OCPDR, reg << 15);
1053
Hayes Wang3a83ad12012-07-11 20:31:56 +08001054 return RTL_R32(OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +08001055}
1056
1057#define OCP_STD_PHY_BASE 0xa400
1058
1059static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1060{
1061 if (reg == 0x1f) {
1062 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1063 return;
1064 }
1065
1066 if (tp->ocp_base != OCP_STD_PHY_BASE)
1067 reg -= 0x10;
1068
1069 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1070}
1071
1072static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1073{
1074 if (tp->ocp_base != OCP_STD_PHY_BASE)
1075 reg -= 0x10;
1076
1077 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1078}
1079
hayeswangeee37862013-04-01 22:23:38 +00001080static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1081{
1082 if (reg == 0x1f) {
1083 tp->ocp_base = value << 4;
1084 return;
1085 }
1086
1087 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1088}
1089
1090static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1091{
1092 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1093}
1094
Francois Romieuffc46952012-07-06 14:19:23 +02001095DECLARE_RTL_COND(rtl_phyar_cond)
1096{
1097 void __iomem *ioaddr = tp->mmio_addr;
1098
1099 return RTL_R32(PHYAR) & 0x80000000;
1100}
1101
Francois Romieu24192212012-07-06 20:19:42 +02001102static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Francois Romieu24192212012-07-06 20:19:42 +02001104 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Francois Romieu24192212012-07-06 20:19:42 +02001106 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Francois Romieuffc46952012-07-06 14:19:23 +02001108 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001109 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001110 * According to hardware specs a 20us delay is required after write
1111 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001112 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001113 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
Francois Romieu24192212012-07-06 20:19:42 +02001116static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Francois Romieu24192212012-07-06 20:19:42 +02001118 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieuffc46952012-07-06 14:19:23 +02001119 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Francois Romieu24192212012-07-06 20:19:42 +02001121 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Francois Romieuffc46952012-07-06 14:19:23 +02001123 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1124 RTL_R32(PHYAR) & 0xffff : ~0;
1125
Timo Teräs81a95f02010-06-09 17:31:48 -07001126 /*
1127 * According to hardware specs a 20us delay is required after read
1128 * complete indication, but before sending next command.
1129 */
1130 udelay(20);
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return value;
1133}
1134
Francois Romieu24192212012-07-06 20:19:42 +02001135static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001136{
Francois Romieu24192212012-07-06 20:19:42 +02001137 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001138
Francois Romieu24192212012-07-06 20:19:42 +02001139 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
françois romieuc0e45c12011-01-03 15:08:04 +00001140 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1141 RTL_W32(EPHY_RXER_NUM, 0);
1142
Francois Romieuffc46952012-07-06 14:19:23 +02001143 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001144}
1145
Francois Romieu24192212012-07-06 20:19:42 +02001146static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001147{
Francois Romieu24192212012-07-06 20:19:42 +02001148 r8168dp_1_mdio_access(tp, reg,
1149 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001150}
1151
Francois Romieu24192212012-07-06 20:19:42 +02001152static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001153{
Francois Romieu24192212012-07-06 20:19:42 +02001154 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001155
Francois Romieu24192212012-07-06 20:19:42 +02001156 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001157
1158 mdelay(1);
1159 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1160 RTL_W32(EPHY_RXER_NUM, 0);
1161
Francois Romieuffc46952012-07-06 14:19:23 +02001162 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1163 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001164}
1165
françois romieue6de30d2011-01-03 15:08:37 +00001166#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1167
1168static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1169{
1170 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1171}
1172
1173static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1174{
1175 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1176}
1177
Francois Romieu24192212012-07-06 20:19:42 +02001178static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001179{
Francois Romieu24192212012-07-06 20:19:42 +02001180 void __iomem *ioaddr = tp->mmio_addr;
1181
françois romieue6de30d2011-01-03 15:08:37 +00001182 r8168dp_2_mdio_start(ioaddr);
1183
Francois Romieu24192212012-07-06 20:19:42 +02001184 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001185
1186 r8168dp_2_mdio_stop(ioaddr);
1187}
1188
Francois Romieu24192212012-07-06 20:19:42 +02001189static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001190{
Francois Romieu24192212012-07-06 20:19:42 +02001191 void __iomem *ioaddr = tp->mmio_addr;
françois romieue6de30d2011-01-03 15:08:37 +00001192 int value;
1193
1194 r8168dp_2_mdio_start(ioaddr);
1195
Francois Romieu24192212012-07-06 20:19:42 +02001196 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001197
1198 r8168dp_2_mdio_stop(ioaddr);
1199
1200 return value;
1201}
1202
françois romieu4da19632011-01-03 15:07:55 +00001203static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001204{
Francois Romieu24192212012-07-06 20:19:42 +02001205 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001206}
1207
françois romieu4da19632011-01-03 15:07:55 +00001208static int rtl_readphy(struct rtl8169_private *tp, int location)
1209{
Francois Romieu24192212012-07-06 20:19:42 +02001210 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001211}
1212
1213static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1214{
1215 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1216}
1217
1218static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001219{
1220 int val;
1221
françois romieu4da19632011-01-03 15:07:55 +00001222 val = rtl_readphy(tp, reg_addr);
1223 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001224}
1225
Francois Romieuccdffb92008-07-26 14:26:06 +02001226static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1227 int val)
1228{
1229 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001230
françois romieu4da19632011-01-03 15:07:55 +00001231 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001232}
1233
1234static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1235{
1236 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001237
françois romieu4da19632011-01-03 15:07:55 +00001238 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001239}
1240
Francois Romieuffc46952012-07-06 14:19:23 +02001241DECLARE_RTL_COND(rtl_ephyar_cond)
1242{
1243 void __iomem *ioaddr = tp->mmio_addr;
1244
1245 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1246}
1247
Francois Romieufdf6fc02012-07-06 22:40:38 +02001248static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001249{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001250 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001251
1252 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1253 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1254
Francois Romieuffc46952012-07-06 14:19:23 +02001255 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1256
1257 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001258}
1259
Francois Romieufdf6fc02012-07-06 22:40:38 +02001260static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001261{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001262 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001263
1264 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1265
Francois Romieuffc46952012-07-06 14:19:23 +02001266 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1267 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001268}
1269
Francois Romieufdf6fc02012-07-06 22:40:38 +02001270static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1271 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001272{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001273 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001274
1275 BUG_ON((addr & 3) || (mask == 0));
1276 RTL_W32(ERIDR, val);
1277 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1278
Francois Romieuffc46952012-07-06 14:19:23 +02001279 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001280}
1281
Francois Romieufdf6fc02012-07-06 22:40:38 +02001282static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001283{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001284 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001285
1286 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1287
Francois Romieuffc46952012-07-06 14:19:23 +02001288 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1289 RTL_R32(ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001290}
1291
Francois Romieufdf6fc02012-07-06 22:40:38 +02001292static void rtl_w1w0_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1293 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001294{
1295 u32 val;
1296
Francois Romieufdf6fc02012-07-06 22:40:38 +02001297 val = rtl_eri_read(tp, addr, type);
1298 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001299}
1300
françois romieuc28aa382011-08-02 03:53:43 +00001301struct exgmac_reg {
1302 u16 addr;
1303 u16 mask;
1304 u32 val;
1305};
1306
Francois Romieufdf6fc02012-07-06 22:40:38 +02001307static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001308 const struct exgmac_reg *r, int len)
1309{
1310 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001311 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001312 r++;
1313 }
1314}
1315
Francois Romieuffc46952012-07-06 14:19:23 +02001316DECLARE_RTL_COND(rtl_efusear_cond)
1317{
1318 void __iomem *ioaddr = tp->mmio_addr;
1319
1320 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1321}
1322
Francois Romieufdf6fc02012-07-06 22:40:38 +02001323static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001324{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001325 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00001326
1327 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1328
Francois Romieuffc46952012-07-06 14:19:23 +02001329 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1330 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001331}
1332
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001333static u16 rtl_get_events(struct rtl8169_private *tp)
1334{
1335 void __iomem *ioaddr = tp->mmio_addr;
1336
1337 return RTL_R16(IntrStatus);
1338}
1339
1340static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1341{
1342 void __iomem *ioaddr = tp->mmio_addr;
1343
1344 RTL_W16(IntrStatus, bits);
1345 mmiowb();
1346}
1347
1348static void rtl_irq_disable(struct rtl8169_private *tp)
1349{
1350 void __iomem *ioaddr = tp->mmio_addr;
1351
1352 RTL_W16(IntrMask, 0);
1353 mmiowb();
1354}
1355
Francois Romieu3e990ff2012-01-26 12:50:01 +01001356static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1357{
1358 void __iomem *ioaddr = tp->mmio_addr;
1359
1360 RTL_W16(IntrMask, bits);
1361}
1362
Francois Romieuda78dbf2012-01-26 14:18:23 +01001363#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1364#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1365#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1366
1367static void rtl_irq_enable_all(struct rtl8169_private *tp)
1368{
1369 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1370}
1371
françois romieu811fd302011-12-04 20:30:45 +00001372static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
françois romieu811fd302011-12-04 20:30:45 +00001374 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001376 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001377 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001378 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
françois romieu4da19632011-01-03 15:07:55 +00001381static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
françois romieu4da19632011-01-03 15:07:55 +00001383 void __iomem *ioaddr = tp->mmio_addr;
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return RTL_R32(TBICSR) & TBIReset;
1386}
1387
françois romieu4da19632011-01-03 15:07:55 +00001388static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
françois romieu4da19632011-01-03 15:07:55 +00001390 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
1393static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1394{
1395 return RTL_R32(TBICSR) & TBILinkOk;
1396}
1397
1398static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1399{
1400 return RTL_R8(PHYstatus) & LinkStatus;
1401}
1402
françois romieu4da19632011-01-03 15:07:55 +00001403static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
françois romieu4da19632011-01-03 15:07:55 +00001405 void __iomem *ioaddr = tp->mmio_addr;
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1408}
1409
françois romieu4da19632011-01-03 15:07:55 +00001410static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412 unsigned int val;
1413
françois romieu4da19632011-01-03 15:07:55 +00001414 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1415 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
Hayes Wang70090422011-07-06 15:58:06 +08001418static void rtl_link_chg_patch(struct rtl8169_private *tp)
1419{
1420 void __iomem *ioaddr = tp->mmio_addr;
1421 struct net_device *dev = tp->dev;
1422
1423 if (!netif_running(dev))
1424 return;
1425
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001426 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1427 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001428 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001429 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1430 ERIAR_EXGMAC);
1431 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1432 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001433 } else if (RTL_R8(PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001434 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1435 ERIAR_EXGMAC);
1436 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1437 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001438 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001439 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1440 ERIAR_EXGMAC);
1441 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1442 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001443 }
1444 /* Reset packet filter */
Francois Romieufdf6fc02012-07-06 22:40:38 +02001445 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001446 ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02001447 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001448 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001449 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1450 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1451 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001452 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1453 ERIAR_EXGMAC);
1454 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1455 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001456 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001457 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1458 ERIAR_EXGMAC);
1459 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1460 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001461 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001462 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1463 if (RTL_R8(PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001464 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1465 ERIAR_EXGMAC);
1466 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1467 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001468 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001469 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1470 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001471 }
Hayes Wang70090422011-07-06 15:58:06 +08001472 }
1473}
1474
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001475static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001476 struct rtl8169_private *tp,
1477 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001480 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001481 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001482 if (pm)
1483 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001485 if (net_ratelimit())
1486 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001487 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001489 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001490 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001491 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001495static void rtl8169_check_link_status(struct net_device *dev,
1496 struct rtl8169_private *tp,
1497 void __iomem *ioaddr)
1498{
1499 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1500}
1501
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001502#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1503
1504static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1505{
1506 void __iomem *ioaddr = tp->mmio_addr;
1507 u8 options;
1508 u32 wolopts = 0;
1509
1510 options = RTL_R8(Config1);
1511 if (!(options & PMEnable))
1512 return 0;
1513
1514 options = RTL_R8(Config3);
1515 if (options & LinkUp)
1516 wolopts |= WAKE_PHY;
1517 if (options & MagicPacket)
1518 wolopts |= WAKE_MAGIC;
1519
1520 options = RTL_R8(Config5);
1521 if (options & UWF)
1522 wolopts |= WAKE_UCAST;
1523 if (options & BWF)
1524 wolopts |= WAKE_BCAST;
1525 if (options & MWF)
1526 wolopts |= WAKE_MCAST;
1527
1528 return wolopts;
1529}
1530
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001531static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1532{
1533 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001534
Francois Romieuda78dbf2012-01-26 14:18:23 +01001535 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001536
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001537 wol->supported = WAKE_ANY;
1538 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001539
Francois Romieuda78dbf2012-01-26 14:18:23 +01001540 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001541}
1542
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001543static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001544{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001545 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001546 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001547 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001548 u32 opt;
1549 u16 reg;
1550 u8 mask;
1551 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001552 { WAKE_PHY, Config3, LinkUp },
1553 { WAKE_MAGIC, Config3, MagicPacket },
1554 { WAKE_UCAST, Config5, UWF },
1555 { WAKE_BCAST, Config5, BWF },
1556 { WAKE_MCAST, Config5, MWF },
1557 { WAKE_ANY, Config5, LanWake }
1558 };
Francois Romieu851e6022012-04-17 11:10:11 +02001559 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001560
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001561 RTL_W8(Cfg9346, Cfg9346_Unlock);
1562
1563 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001564 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001565 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001566 options |= cfg[i].mask;
1567 RTL_W8(cfg[i].reg, options);
1568 }
1569
Francois Romieu851e6022012-04-17 11:10:11 +02001570 switch (tp->mac_version) {
1571 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1572 options = RTL_R8(Config1) & ~PMEnable;
1573 if (wolopts)
1574 options |= PMEnable;
1575 RTL_W8(Config1, options);
1576 break;
1577 default:
Francois Romieud387b422012-04-17 11:12:01 +02001578 options = RTL_R8(Config2) & ~PME_SIGNAL;
1579 if (wolopts)
1580 options |= PME_SIGNAL;
1581 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001582 break;
1583 }
1584
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001585 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001586}
1587
1588static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1589{
1590 struct rtl8169_private *tp = netdev_priv(dev);
1591
Francois Romieuda78dbf2012-01-26 14:18:23 +01001592 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001593
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001594 if (wol->wolopts)
1595 tp->features |= RTL_FEATURE_WOL;
1596 else
1597 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001598 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001599
1600 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001601
françois romieuea809072010-11-08 13:23:58 +00001602 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1603
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001604 return 0;
1605}
1606
Francois Romieu31bd2042011-04-26 18:58:59 +02001607static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1608{
Francois Romieu85bffe62011-04-27 08:22:39 +02001609 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001610}
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612static void rtl8169_get_drvinfo(struct net_device *dev,
1613 struct ethtool_drvinfo *info)
1614{
1615 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001616 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Rick Jones68aad782011-11-07 13:29:27 +00001618 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1619 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1620 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001621 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001622 if (!IS_ERR_OR_NULL(rtl_fw))
1623 strlcpy(info->fw_version, rtl_fw->version,
1624 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
1627static int rtl8169_get_regs_len(struct net_device *dev)
1628{
1629 return R8169_REGS_SIZE;
1630}
1631
1632static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001633 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 struct rtl8169_private *tp = netdev_priv(dev);
1636 void __iomem *ioaddr = tp->mmio_addr;
1637 int ret = 0;
1638 u32 reg;
1639
1640 reg = RTL_R32(TBICSR);
1641 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1642 (duplex == DUPLEX_FULL)) {
1643 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1644 } else if (autoneg == AUTONEG_ENABLE)
1645 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1646 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001647 netif_warn(tp, link, dev,
1648 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 ret = -EOPNOTSUPP;
1650 }
1651
1652 return ret;
1653}
1654
1655static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001656 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001659 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001660 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Hayes Wang716b50a2011-02-22 17:26:18 +08001662 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001665 int auto_nego;
1666
françois romieu4da19632011-01-03 15:07:55 +00001667 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001668 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1669 ADVERTISE_100HALF | ADVERTISE_100FULL);
1670
1671 if (adv & ADVERTISED_10baseT_Half)
1672 auto_nego |= ADVERTISE_10HALF;
1673 if (adv & ADVERTISED_10baseT_Full)
1674 auto_nego |= ADVERTISE_10FULL;
1675 if (adv & ADVERTISED_100baseT_Half)
1676 auto_nego |= ADVERTISE_100HALF;
1677 if (adv & ADVERTISED_100baseT_Full)
1678 auto_nego |= ADVERTISE_100FULL;
1679
françois romieu3577aa12009-05-19 10:46:48 +00001680 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1681
françois romieu4da19632011-01-03 15:07:55 +00001682 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001683 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1684
1685 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001686 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001687 if (adv & ADVERTISED_1000baseT_Half)
1688 giga_ctrl |= ADVERTISE_1000HALF;
1689 if (adv & ADVERTISED_1000baseT_Full)
1690 giga_ctrl |= ADVERTISE_1000FULL;
1691 } else if (adv & (ADVERTISED_1000baseT_Half |
1692 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001693 netif_info(tp, link, dev,
1694 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001695 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
françois romieu3577aa12009-05-19 10:46:48 +00001698 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001699
françois romieu4da19632011-01-03 15:07:55 +00001700 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1701 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001702 } else {
1703 giga_ctrl = 0;
1704
1705 if (speed == SPEED_10)
1706 bmcr = 0;
1707 else if (speed == SPEED_100)
1708 bmcr = BMCR_SPEED100;
1709 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001710 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001711
1712 if (duplex == DUPLEX_FULL)
1713 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001714 }
1715
françois romieu4da19632011-01-03 15:07:55 +00001716 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001717
Francois Romieucecb5fd2011-04-01 10:21:07 +02001718 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1719 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001720 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001721 rtl_writephy(tp, 0x17, 0x2138);
1722 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001723 } else {
françois romieu4da19632011-01-03 15:07:55 +00001724 rtl_writephy(tp, 0x17, 0x2108);
1725 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001726 }
1727 }
1728
Oliver Neukum54405cd2011-01-06 21:55:13 +01001729 rc = 0;
1730out:
1731 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733
1734static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001735 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
1737 struct rtl8169_private *tp = netdev_priv(dev);
1738 int ret;
1739
Oliver Neukum54405cd2011-01-06 21:55:13 +01001740 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001741 if (ret < 0)
1742 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Francois Romieu4876cc12011-03-11 21:07:11 +01001744 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1745 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001747 }
1748out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 return ret;
1750}
1751
1752static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1753{
1754 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 int ret;
1756
Francois Romieu4876cc12011-03-11 21:07:11 +01001757 del_timer_sync(&tp->timer);
1758
Francois Romieuda78dbf2012-01-26 14:18:23 +01001759 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001760 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001761 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001762 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return ret;
1765}
1766
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001767static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1768 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
Francois Romieud58d46b2011-05-03 16:38:29 +02001770 struct rtl8169_private *tp = netdev_priv(dev);
1771
Francois Romieu2b7b4312011-04-18 22:53:24 -07001772 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001773 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Francois Romieud58d46b2011-05-03 16:38:29 +02001775 if (dev->mtu > JUMBO_1K &&
1776 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1777 features &= ~NETIF_F_IP_CSUM;
1778
Michał Mirosław350fb322011-04-08 06:35:56 +00001779 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
Francois Romieuda78dbf2012-01-26 14:18:23 +01001782static void __rtl8169_set_features(struct net_device *dev,
1783 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
1785 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001786 void __iomem *ioaddr = tp->mmio_addr;
hayeswang929a0312014-09-16 11:40:47 +08001787 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
hayeswang929a0312014-09-16 11:40:47 +08001789 rx_config = RTL_R32(RxConfig);
1790 if (features & NETIF_F_RXALL)
1791 rx_config |= (AcceptErr | AcceptRunt);
1792 else
1793 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
hayeswang929a0312014-09-16 11:40:47 +08001795 RTL_W32(RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001796
hayeswang929a0312014-09-16 11:40:47 +08001797 if (features & NETIF_F_RXCSUM)
1798 tp->cp_cmd |= RxChkSum;
1799 else
1800 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001801
hayeswang929a0312014-09-16 11:40:47 +08001802 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1803 tp->cp_cmd |= RxVlan;
1804 else
1805 tp->cp_cmd &= ~RxVlan;
1806
1807 tp->cp_cmd |= RTL_R16(CPlusCmd) & ~(RxVlan | RxChkSum);
1808
1809 RTL_W16(CPlusCmd, tp->cp_cmd);
1810 RTL_R16(CPlusCmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001811}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Francois Romieuda78dbf2012-01-26 14:18:23 +01001813static int rtl8169_set_features(struct net_device *dev,
1814 netdev_features_t features)
1815{
1816 struct rtl8169_private *tp = netdev_priv(dev);
1817
hayeswang929a0312014-09-16 11:40:47 +08001818 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
1819
Francois Romieuda78dbf2012-01-26 14:18:23 +01001820 rtl_lock_work(tp);
hayeswang929a0312014-09-16 11:40:47 +08001821 if (features ^ dev->features);
1822 __rtl8169_set_features(dev, features);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001823 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 return 0;
1826}
1827
Francois Romieuda78dbf2012-01-26 14:18:23 +01001828
Kirill Smelkov810f4892012-11-10 21:11:02 +04001829static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Jesse Grosseab6d182010-10-20 13:56:03 +00001831 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1833}
1834
Francois Romieu7a8fc772011-03-01 17:18:33 +01001835static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
1837 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Francois Romieu7a8fc772011-03-01 17:18:33 +01001839 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001840 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
Francois Romieuccdffb92008-07-26 14:26:06 +02001843static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
1845 struct rtl8169_private *tp = netdev_priv(dev);
1846 void __iomem *ioaddr = tp->mmio_addr;
1847 u32 status;
1848
1849 cmd->supported =
1850 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1851 cmd->port = PORT_FIBRE;
1852 cmd->transceiver = XCVR_INTERNAL;
1853
1854 status = RTL_R32(TBICSR);
1855 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1856 cmd->autoneg = !!(status & TBINwEnable);
1857
David Decotigny70739492011-04-27 18:32:40 +00001858 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001860
1861 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
Francois Romieuccdffb92008-07-26 14:26:06 +02001864static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Francois Romieuccdffb92008-07-26 14:26:06 +02001868 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870
1871static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1872{
1873 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001874 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Francois Romieuda78dbf2012-01-26 14:18:23 +01001876 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001877 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001878 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Francois Romieuccdffb92008-07-26 14:26:06 +02001880 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
1883static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1884 void *p)
1885{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001886 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02001887 u32 __iomem *data = tp->mmio_addr;
1888 u32 *dw = p;
1889 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Francois Romieuda78dbf2012-01-26 14:18:23 +01001891 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02001892 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1893 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001894 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895}
1896
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001897static u32 rtl8169_get_msglevel(struct net_device *dev)
1898{
1899 struct rtl8169_private *tp = netdev_priv(dev);
1900
1901 return tp->msg_enable;
1902}
1903
1904static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1905{
1906 struct rtl8169_private *tp = netdev_priv(dev);
1907
1908 tp->msg_enable = value;
1909}
1910
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001911static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1912 "tx_packets",
1913 "rx_packets",
1914 "tx_errors",
1915 "rx_errors",
1916 "rx_missed",
1917 "align_errors",
1918 "tx_single_collisions",
1919 "tx_multi_collisions",
1920 "unicast",
1921 "broadcast",
1922 "multicast",
1923 "tx_aborted",
1924 "tx_underrun",
1925};
1926
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001927static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001928{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001929 switch (sset) {
1930 case ETH_SS_STATS:
1931 return ARRAY_SIZE(rtl8169_gstrings);
1932 default:
1933 return -EOPNOTSUPP;
1934 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001935}
1936
Francois Romieuffc46952012-07-06 14:19:23 +02001937DECLARE_RTL_COND(rtl_counters_cond)
1938{
1939 void __iomem *ioaddr = tp->mmio_addr;
1940
1941 return RTL_R32(CounterAddrLow) & CounterDump;
1942}
1943
Ivan Vecera355423d2009-02-06 21:49:57 -08001944static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001945{
1946 struct rtl8169_private *tp = netdev_priv(dev);
1947 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001948 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001949 struct rtl8169_counters *counters;
1950 dma_addr_t paddr;
1951 u32 cmd;
1952
Ivan Vecera355423d2009-02-06 21:49:57 -08001953 /*
1954 * Some chips are unable to dump tally counters when the receiver
1955 * is disabled.
1956 */
1957 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1958 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001959
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001960 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001961 if (!counters)
1962 return;
1963
1964 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001965 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001966 RTL_W32(CounterAddrLow, cmd);
1967 RTL_W32(CounterAddrLow, cmd | CounterDump);
1968
Francois Romieuffc46952012-07-06 14:19:23 +02001969 if (rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000))
1970 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001971
1972 RTL_W32(CounterAddrLow, 0);
1973 RTL_W32(CounterAddrHigh, 0);
1974
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001975 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001976}
1977
Ivan Vecera355423d2009-02-06 21:49:57 -08001978static void rtl8169_get_ethtool_stats(struct net_device *dev,
1979 struct ethtool_stats *stats, u64 *data)
1980{
1981 struct rtl8169_private *tp = netdev_priv(dev);
1982
1983 ASSERT_RTNL();
1984
1985 rtl8169_update_counters(dev);
1986
1987 data[0] = le64_to_cpu(tp->counters.tx_packets);
1988 data[1] = le64_to_cpu(tp->counters.rx_packets);
1989 data[2] = le64_to_cpu(tp->counters.tx_errors);
1990 data[3] = le32_to_cpu(tp->counters.rx_errors);
1991 data[4] = le16_to_cpu(tp->counters.rx_missed);
1992 data[5] = le16_to_cpu(tp->counters.align_errors);
1993 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1994 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1995 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1996 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1997 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1998 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1999 data[12] = le16_to_cpu(tp->counters.tx_underun);
2000}
2001
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002002static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2003{
2004 switch(stringset) {
2005 case ETH_SS_STATS:
2006 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2007 break;
2008 }
2009}
2010
Jeff Garzik7282d492006-09-13 14:30:00 -04002011static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 .get_drvinfo = rtl8169_get_drvinfo,
2013 .get_regs_len = rtl8169_get_regs_len,
2014 .get_link = ethtool_op_get_link,
2015 .get_settings = rtl8169_get_settings,
2016 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002017 .get_msglevel = rtl8169_get_msglevel,
2018 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002020 .get_wol = rtl8169_get_wol,
2021 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002022 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002023 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002024 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002025 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026};
2027
Francois Romieu07d3f512007-02-21 22:40:46 +01002028static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02002029 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
Francois Romieu5d320a22011-05-08 17:47:36 +02002031 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01002032 /*
2033 * The driver currently handles the 8168Bf and the 8168Be identically
2034 * but they can be identified more specifically through the test below
2035 * if needed:
2036 *
2037 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002038 *
2039 * Same thing for the 8101Eb and the 8101Ec:
2040 *
2041 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002042 */
Francois Romieu37441002011-06-17 22:58:54 +02002043 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002045 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 int mac_version;
2047 } mac_info[] = {
Hayes Wangc5583862012-07-02 17:23:22 +08002048 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002049 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002050 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002051 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2052 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2053
Hayes Wangc2218922011-09-06 16:55:18 +08002054 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002055 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002056 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2057 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2058
hayeswang01dc7fe2011-03-21 01:50:28 +00002059 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002060 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002061 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2062 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2063 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2064
Francois Romieu5b538df2008-07-20 16:22:45 +02002065 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002066 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2067 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002068 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002069
françois romieue6de30d2011-01-03 15:08:37 +00002070 /* 8168DP family. */
2071 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2072 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002073 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002074
Francois Romieuef808d52008-06-29 13:10:54 +02002075 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07002076 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02002077 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002078 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002079 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002080 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2081 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002082 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02002083 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02002084 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002085
2086 /* 8168B family. */
2087 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2088 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2089 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2090 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2091
2092 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002093 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2094 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002095 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00002096 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002097 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2098 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2099 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002100 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2101 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2102 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2103 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2104 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2105 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002106 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002107 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002108 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002109 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2110 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002111 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2112 /* FIXME: where did these entries come from ? -- FR */
2113 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2114 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2115
2116 /* 8110 family. */
2117 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2118 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2119 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2120 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2121 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2122 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2123
Jean Delvaref21b75e2009-05-26 20:54:48 -07002124 /* Catch-all */
2125 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002126 };
2127 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 u32 reg;
2129
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002130 reg = RTL_R32(TxConfig);
2131 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 p++;
2133 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002134
2135 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2136 netif_notice(tp, probe, dev,
2137 "unknown MAC, using family default\n");
2138 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002139 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2140 tp->mac_version = tp->mii.supports_gmii ?
2141 RTL_GIGA_MAC_VER_42 :
2142 RTL_GIGA_MAC_VER_43;
Francois Romieu5d320a22011-05-08 17:47:36 +02002143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
2146static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2147{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002148 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
Francois Romieu867763c2007-08-17 18:21:58 +02002151struct phy_reg {
2152 u16 reg;
2153 u16 val;
2154};
2155
françois romieu4da19632011-01-03 15:07:55 +00002156static void rtl_writephy_batch(struct rtl8169_private *tp,
2157 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002158{
2159 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002160 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002161 regs++;
2162 }
2163}
2164
françois romieubca03d52011-01-03 15:07:31 +00002165#define PHY_READ 0x00000000
2166#define PHY_DATA_OR 0x10000000
2167#define PHY_DATA_AND 0x20000000
2168#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002169#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002170#define PHY_CLEAR_READCOUNT 0x70000000
2171#define PHY_WRITE 0x80000000
2172#define PHY_READCOUNT_EQ_SKIP 0x90000000
2173#define PHY_COMP_EQ_SKIPN 0xa0000000
2174#define PHY_COMP_NEQ_SKIPN 0xb0000000
2175#define PHY_WRITE_PREVIOUS 0xc0000000
2176#define PHY_SKIPN 0xd0000000
2177#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002178
Hayes Wang960aee62011-06-18 11:37:48 +02002179struct fw_info {
2180 u32 magic;
2181 char version[RTL_VER_SIZE];
2182 __le32 fw_start;
2183 __le32 fw_len;
2184 u8 chksum;
2185} __packed;
2186
Francois Romieu1c361ef2011-06-17 17:16:24 +02002187#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2188
2189static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002190{
Francois Romieub6ffd972011-06-17 17:00:05 +02002191 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002192 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002193 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2194 char *version = rtl_fw->version;
2195 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002196
Francois Romieu1c361ef2011-06-17 17:16:24 +02002197 if (fw->size < FW_OPCODE_SIZE)
2198 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002199
2200 if (!fw_info->magic) {
2201 size_t i, size, start;
2202 u8 checksum = 0;
2203
2204 if (fw->size < sizeof(*fw_info))
2205 goto out;
2206
2207 for (i = 0; i < fw->size; i++)
2208 checksum += fw->data[i];
2209 if (checksum != 0)
2210 goto out;
2211
2212 start = le32_to_cpu(fw_info->fw_start);
2213 if (start > fw->size)
2214 goto out;
2215
2216 size = le32_to_cpu(fw_info->fw_len);
2217 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2218 goto out;
2219
2220 memcpy(version, fw_info->version, RTL_VER_SIZE);
2221
2222 pa->code = (__le32 *)(fw->data + start);
2223 pa->size = size;
2224 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002225 if (fw->size % FW_OPCODE_SIZE)
2226 goto out;
2227
2228 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2229
2230 pa->code = (__le32 *)fw->data;
2231 pa->size = fw->size / FW_OPCODE_SIZE;
2232 }
2233 version[RTL_VER_SIZE - 1] = 0;
2234
2235 rc = true;
2236out:
2237 return rc;
2238}
2239
Francois Romieufd112f22011-06-18 00:10:29 +02002240static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2241 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002242{
Francois Romieufd112f22011-06-18 00:10:29 +02002243 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002244 size_t index;
2245
Francois Romieu1c361ef2011-06-17 17:16:24 +02002246 for (index = 0; index < pa->size; index++) {
2247 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002248 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002249
hayeswang42b82dc2011-01-10 02:07:25 +00002250 switch(action & 0xf0000000) {
2251 case PHY_READ:
2252 case PHY_DATA_OR:
2253 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002254 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002255 case PHY_CLEAR_READCOUNT:
2256 case PHY_WRITE:
2257 case PHY_WRITE_PREVIOUS:
2258 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002259 break;
2260
hayeswang42b82dc2011-01-10 02:07:25 +00002261 case PHY_BJMPN:
2262 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002263 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002264 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002265 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002266 }
2267 break;
2268 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002269 if (index + 2 >= 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 case PHY_COMP_EQ_SKIPN:
2276 case PHY_COMP_NEQ_SKIPN:
2277 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002278 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002279 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002280 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002281 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002282 }
2283 break;
2284
hayeswang42b82dc2011-01-10 02:07:25 +00002285 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002286 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002287 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002288 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002289 }
2290 }
Francois Romieufd112f22011-06-18 00:10:29 +02002291 rc = true;
2292out:
2293 return rc;
2294}
françois romieubca03d52011-01-03 15:07:31 +00002295
Francois Romieufd112f22011-06-18 00:10:29 +02002296static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2297{
2298 struct net_device *dev = tp->dev;
2299 int rc = -EINVAL;
2300
2301 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2302 netif_err(tp, ifup, dev, "invalid firwmare\n");
2303 goto out;
2304 }
2305
2306 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2307 rc = 0;
2308out:
2309 return rc;
2310}
2311
2312static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2313{
2314 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002315 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002316 u32 predata, count;
2317 size_t index;
2318
2319 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002320 org.write = ops->write;
2321 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002322
Francois Romieu1c361ef2011-06-17 17:16:24 +02002323 for (index = 0; index < pa->size; ) {
2324 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002325 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002326 u32 regno = (action & 0x0fff0000) >> 16;
2327
2328 if (!action)
2329 break;
françois romieubca03d52011-01-03 15:07:31 +00002330
2331 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002332 case PHY_READ:
2333 predata = rtl_readphy(tp, regno);
2334 count++;
2335 index++;
françois romieubca03d52011-01-03 15:07:31 +00002336 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002337 case PHY_DATA_OR:
2338 predata |= data;
2339 index++;
2340 break;
2341 case PHY_DATA_AND:
2342 predata &= data;
2343 index++;
2344 break;
2345 case PHY_BJMPN:
2346 index -= regno;
2347 break;
hayeswangeee37862013-04-01 22:23:38 +00002348 case PHY_MDIO_CHG:
2349 if (data == 0) {
2350 ops->write = org.write;
2351 ops->read = org.read;
2352 } else if (data == 1) {
2353 ops->write = mac_mcu_write;
2354 ops->read = mac_mcu_read;
2355 }
2356
hayeswang42b82dc2011-01-10 02:07:25 +00002357 index++;
2358 break;
2359 case PHY_CLEAR_READCOUNT:
2360 count = 0;
2361 index++;
2362 break;
2363 case PHY_WRITE:
2364 rtl_writephy(tp, regno, data);
2365 index++;
2366 break;
2367 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002368 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002369 break;
2370 case PHY_COMP_EQ_SKIPN:
2371 if (predata == data)
2372 index += regno;
2373 index++;
2374 break;
2375 case PHY_COMP_NEQ_SKIPN:
2376 if (predata != data)
2377 index += regno;
2378 index++;
2379 break;
2380 case PHY_WRITE_PREVIOUS:
2381 rtl_writephy(tp, regno, predata);
2382 index++;
2383 break;
2384 case PHY_SKIPN:
2385 index += regno + 1;
2386 break;
2387 case PHY_DELAY_MS:
2388 mdelay(data);
2389 index++;
2390 break;
2391
françois romieubca03d52011-01-03 15:07:31 +00002392 default:
2393 BUG();
2394 }
2395 }
hayeswangeee37862013-04-01 22:23:38 +00002396
2397 ops->write = org.write;
2398 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002399}
2400
françois romieuf1e02ed2011-01-13 13:07:53 +00002401static void rtl_release_firmware(struct rtl8169_private *tp)
2402{
Francois Romieub6ffd972011-06-17 17:00:05 +02002403 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2404 release_firmware(tp->rtl_fw->fw);
2405 kfree(tp->rtl_fw);
2406 }
2407 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002408}
2409
François Romieu953a12c2011-04-24 17:38:48 +02002410static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002411{
Francois Romieub6ffd972011-06-17 17:00:05 +02002412 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002413
2414 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002415 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002416 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002417}
2418
2419static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2420{
2421 if (rtl_readphy(tp, reg) != val)
2422 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2423 else
2424 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002425}
2426
françois romieu4da19632011-01-03 15:07:55 +00002427static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002429 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002430 { 0x1f, 0x0001 },
2431 { 0x06, 0x006e },
2432 { 0x08, 0x0708 },
2433 { 0x15, 0x4000 },
2434 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
françois romieu0b9b5712009-08-10 19:44:56 +00002436 { 0x1f, 0x0001 },
2437 { 0x03, 0x00a1 },
2438 { 0x02, 0x0008 },
2439 { 0x01, 0x0120 },
2440 { 0x00, 0x1000 },
2441 { 0x04, 0x0800 },
2442 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
françois romieu0b9b5712009-08-10 19:44:56 +00002444 { 0x03, 0xff41 },
2445 { 0x02, 0xdf60 },
2446 { 0x01, 0x0140 },
2447 { 0x00, 0x0077 },
2448 { 0x04, 0x7800 },
2449 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
françois romieu0b9b5712009-08-10 19:44:56 +00002451 { 0x03, 0x802f },
2452 { 0x02, 0x4f02 },
2453 { 0x01, 0x0409 },
2454 { 0x00, 0xf0f9 },
2455 { 0x04, 0x9800 },
2456 { 0x04, 0x9000 },
2457
2458 { 0x03, 0xdf01 },
2459 { 0x02, 0xdf20 },
2460 { 0x01, 0xff95 },
2461 { 0x00, 0xba00 },
2462 { 0x04, 0xa800 },
2463 { 0x04, 0xa000 },
2464
2465 { 0x03, 0xff41 },
2466 { 0x02, 0xdf20 },
2467 { 0x01, 0x0140 },
2468 { 0x00, 0x00bb },
2469 { 0x04, 0xb800 },
2470 { 0x04, 0xb000 },
2471
2472 { 0x03, 0xdf41 },
2473 { 0x02, 0xdc60 },
2474 { 0x01, 0x6340 },
2475 { 0x00, 0x007d },
2476 { 0x04, 0xd800 },
2477 { 0x04, 0xd000 },
2478
2479 { 0x03, 0xdf01 },
2480 { 0x02, 0xdf20 },
2481 { 0x01, 0x100a },
2482 { 0x00, 0xa0ff },
2483 { 0x04, 0xf800 },
2484 { 0x04, 0xf000 },
2485
2486 { 0x1f, 0x0000 },
2487 { 0x0b, 0x0000 },
2488 { 0x00, 0x9200 }
2489 };
2490
françois romieu4da19632011-01-03 15:07:55 +00002491 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
françois romieu4da19632011-01-03 15:07:55 +00002494static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002495{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002496 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002497 { 0x1f, 0x0002 },
2498 { 0x01, 0x90d0 },
2499 { 0x1f, 0x0000 }
2500 };
2501
françois romieu4da19632011-01-03 15:07:55 +00002502 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002503}
2504
françois romieu4da19632011-01-03 15:07:55 +00002505static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002506{
2507 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002508
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002509 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2510 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002511 return;
2512
françois romieu4da19632011-01-03 15:07:55 +00002513 rtl_writephy(tp, 0x1f, 0x0001);
2514 rtl_writephy(tp, 0x10, 0xf01b);
2515 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002516}
2517
françois romieu4da19632011-01-03 15:07:55 +00002518static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002519{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002520 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002521 { 0x1f, 0x0001 },
2522 { 0x04, 0x0000 },
2523 { 0x03, 0x00a1 },
2524 { 0x02, 0x0008 },
2525 { 0x01, 0x0120 },
2526 { 0x00, 0x1000 },
2527 { 0x04, 0x0800 },
2528 { 0x04, 0x9000 },
2529 { 0x03, 0x802f },
2530 { 0x02, 0x4f02 },
2531 { 0x01, 0x0409 },
2532 { 0x00, 0xf099 },
2533 { 0x04, 0x9800 },
2534 { 0x04, 0xa000 },
2535 { 0x03, 0xdf01 },
2536 { 0x02, 0xdf20 },
2537 { 0x01, 0xff95 },
2538 { 0x00, 0xba00 },
2539 { 0x04, 0xa800 },
2540 { 0x04, 0xf000 },
2541 { 0x03, 0xdf01 },
2542 { 0x02, 0xdf20 },
2543 { 0x01, 0x101a },
2544 { 0x00, 0xa0ff },
2545 { 0x04, 0xf800 },
2546 { 0x04, 0x0000 },
2547 { 0x1f, 0x0000 },
2548
2549 { 0x1f, 0x0001 },
2550 { 0x10, 0xf41b },
2551 { 0x14, 0xfb54 },
2552 { 0x18, 0xf5c7 },
2553 { 0x1f, 0x0000 },
2554
2555 { 0x1f, 0x0001 },
2556 { 0x17, 0x0cc0 },
2557 { 0x1f, 0x0000 }
2558 };
2559
françois romieu4da19632011-01-03 15:07:55 +00002560 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002561
françois romieu4da19632011-01-03 15:07:55 +00002562 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002563}
2564
françois romieu4da19632011-01-03 15:07:55 +00002565static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002566{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002567 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002568 { 0x1f, 0x0001 },
2569 { 0x04, 0x0000 },
2570 { 0x03, 0x00a1 },
2571 { 0x02, 0x0008 },
2572 { 0x01, 0x0120 },
2573 { 0x00, 0x1000 },
2574 { 0x04, 0x0800 },
2575 { 0x04, 0x9000 },
2576 { 0x03, 0x802f },
2577 { 0x02, 0x4f02 },
2578 { 0x01, 0x0409 },
2579 { 0x00, 0xf099 },
2580 { 0x04, 0x9800 },
2581 { 0x04, 0xa000 },
2582 { 0x03, 0xdf01 },
2583 { 0x02, 0xdf20 },
2584 { 0x01, 0xff95 },
2585 { 0x00, 0xba00 },
2586 { 0x04, 0xa800 },
2587 { 0x04, 0xf000 },
2588 { 0x03, 0xdf01 },
2589 { 0x02, 0xdf20 },
2590 { 0x01, 0x101a },
2591 { 0x00, 0xa0ff },
2592 { 0x04, 0xf800 },
2593 { 0x04, 0x0000 },
2594 { 0x1f, 0x0000 },
2595
2596 { 0x1f, 0x0001 },
2597 { 0x0b, 0x8480 },
2598 { 0x1f, 0x0000 },
2599
2600 { 0x1f, 0x0001 },
2601 { 0x18, 0x67c7 },
2602 { 0x04, 0x2000 },
2603 { 0x03, 0x002f },
2604 { 0x02, 0x4360 },
2605 { 0x01, 0x0109 },
2606 { 0x00, 0x3022 },
2607 { 0x04, 0x2800 },
2608 { 0x1f, 0x0000 },
2609
2610 { 0x1f, 0x0001 },
2611 { 0x17, 0x0cc0 },
2612 { 0x1f, 0x0000 }
2613 };
2614
françois romieu4da19632011-01-03 15:07:55 +00002615 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002616}
2617
françois romieu4da19632011-01-03 15:07:55 +00002618static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002619{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002620 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002621 { 0x10, 0xf41b },
2622 { 0x1f, 0x0000 }
2623 };
2624
françois romieu4da19632011-01-03 15:07:55 +00002625 rtl_writephy(tp, 0x1f, 0x0001);
2626 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002627
françois romieu4da19632011-01-03 15:07:55 +00002628 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002629}
2630
françois romieu4da19632011-01-03 15:07:55 +00002631static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002632{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002633 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002634 { 0x1f, 0x0001 },
2635 { 0x10, 0xf41b },
2636 { 0x1f, 0x0000 }
2637 };
2638
françois romieu4da19632011-01-03 15:07:55 +00002639 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002640}
2641
françois romieu4da19632011-01-03 15:07:55 +00002642static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002643{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002644 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002645 { 0x1f, 0x0000 },
2646 { 0x1d, 0x0f00 },
2647 { 0x1f, 0x0002 },
2648 { 0x0c, 0x1ec8 },
2649 { 0x1f, 0x0000 }
2650 };
2651
françois romieu4da19632011-01-03 15:07:55 +00002652 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002653}
2654
françois romieu4da19632011-01-03 15:07:55 +00002655static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002656{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002657 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002658 { 0x1f, 0x0001 },
2659 { 0x1d, 0x3d98 },
2660 { 0x1f, 0x0000 }
2661 };
2662
françois romieu4da19632011-01-03 15:07:55 +00002663 rtl_writephy(tp, 0x1f, 0x0000);
2664 rtl_patchphy(tp, 0x14, 1 << 5);
2665 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002666
françois romieu4da19632011-01-03 15:07:55 +00002667 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002668}
2669
françois romieu4da19632011-01-03 15:07:55 +00002670static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002671{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002672 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002673 { 0x1f, 0x0001 },
2674 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002675 { 0x1f, 0x0002 },
2676 { 0x00, 0x88d4 },
2677 { 0x01, 0x82b1 },
2678 { 0x03, 0x7002 },
2679 { 0x08, 0x9e30 },
2680 { 0x09, 0x01f0 },
2681 { 0x0a, 0x5500 },
2682 { 0x0c, 0x00c8 },
2683 { 0x1f, 0x0003 },
2684 { 0x12, 0xc096 },
2685 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002686 { 0x1f, 0x0000 },
2687 { 0x1f, 0x0000 },
2688 { 0x09, 0x2000 },
2689 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002690 };
2691
françois romieu4da19632011-01-03 15:07:55 +00002692 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002693
françois romieu4da19632011-01-03 15:07:55 +00002694 rtl_patchphy(tp, 0x14, 1 << 5);
2695 rtl_patchphy(tp, 0x0d, 1 << 5);
2696 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002697}
2698
françois romieu4da19632011-01-03 15:07:55 +00002699static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002700{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002701 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002702 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002703 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002704 { 0x03, 0x802f },
2705 { 0x02, 0x4f02 },
2706 { 0x01, 0x0409 },
2707 { 0x00, 0xf099 },
2708 { 0x04, 0x9800 },
2709 { 0x04, 0x9000 },
2710 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002711 { 0x1f, 0x0002 },
2712 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002713 { 0x06, 0x0761 },
2714 { 0x1f, 0x0003 },
2715 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002716 { 0x1f, 0x0000 }
2717 };
2718
françois romieu4da19632011-01-03 15:07:55 +00002719 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002720
françois romieu4da19632011-01-03 15:07:55 +00002721 rtl_patchphy(tp, 0x16, 1 << 0);
2722 rtl_patchphy(tp, 0x14, 1 << 5);
2723 rtl_patchphy(tp, 0x0d, 1 << 5);
2724 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002725}
2726
françois romieu4da19632011-01-03 15:07:55 +00002727static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002728{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002729 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002730 { 0x1f, 0x0001 },
2731 { 0x12, 0x2300 },
2732 { 0x1d, 0x3d98 },
2733 { 0x1f, 0x0002 },
2734 { 0x0c, 0x7eb8 },
2735 { 0x06, 0x5461 },
2736 { 0x1f, 0x0003 },
2737 { 0x16, 0x0f0a },
2738 { 0x1f, 0x0000 }
2739 };
2740
françois romieu4da19632011-01-03 15:07:55 +00002741 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002742
françois romieu4da19632011-01-03 15:07:55 +00002743 rtl_patchphy(tp, 0x16, 1 << 0);
2744 rtl_patchphy(tp, 0x14, 1 << 5);
2745 rtl_patchphy(tp, 0x0d, 1 << 5);
2746 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002747}
2748
françois romieu4da19632011-01-03 15:07:55 +00002749static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002750{
françois romieu4da19632011-01-03 15:07:55 +00002751 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002752}
2753
françois romieubca03d52011-01-03 15:07:31 +00002754static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002755{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002756 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002757 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002758 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002759 { 0x06, 0x4064 },
2760 { 0x07, 0x2863 },
2761 { 0x08, 0x059c },
2762 { 0x09, 0x26b4 },
2763 { 0x0a, 0x6a19 },
2764 { 0x0b, 0xdcc8 },
2765 { 0x10, 0xf06d },
2766 { 0x14, 0x7f68 },
2767 { 0x18, 0x7fd9 },
2768 { 0x1c, 0xf0ff },
2769 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002770 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002771 { 0x12, 0xf49f },
2772 { 0x13, 0x070b },
2773 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002774 { 0x14, 0x94c0 },
2775
2776 /*
2777 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002778 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002779 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002780 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002781 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002782 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002783 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002784 { 0x06, 0x5561 },
2785
2786 /*
2787 * Can not link to 1Gbps with bad cable
2788 * Decrease SNR threshold form 21.07dB to 19.04dB
2789 */
2790 { 0x1f, 0x0001 },
2791 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002792
2793 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002794 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002795 };
2796
françois romieu4da19632011-01-03 15:07:55 +00002797 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002798
françois romieubca03d52011-01-03 15:07:31 +00002799 /*
2800 * Rx Error Issue
2801 * Fine Tune Switching regulator parameter
2802 */
françois romieu4da19632011-01-03 15:07:55 +00002803 rtl_writephy(tp, 0x1f, 0x0002);
2804 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2805 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002806
Francois Romieufdf6fc02012-07-06 22:40:38 +02002807 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002808 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002809 { 0x1f, 0x0002 },
2810 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002811 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002812 { 0x05, 0x8330 },
2813 { 0x06, 0x669a },
2814 { 0x1f, 0x0002 }
2815 };
2816 int val;
2817
françois romieu4da19632011-01-03 15:07:55 +00002818 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002819
françois romieu4da19632011-01-03 15:07:55 +00002820 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002821
2822 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002823 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002824 0x0065, 0x0066, 0x0067, 0x0068,
2825 0x0069, 0x006a, 0x006b, 0x006c
2826 };
2827 int i;
2828
françois romieu4da19632011-01-03 15:07:55 +00002829 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002830
2831 val &= 0xff00;
2832 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002833 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002834 }
2835 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002836 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002837 { 0x1f, 0x0002 },
2838 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002839 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002840 { 0x05, 0x8330 },
2841 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002842 };
2843
françois romieu4da19632011-01-03 15:07:55 +00002844 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002845 }
2846
françois romieubca03d52011-01-03 15:07:31 +00002847 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002848 rtl_writephy(tp, 0x1f, 0x0002);
2849 rtl_patchphy(tp, 0x0d, 0x0300);
2850 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002851
françois romieubca03d52011-01-03 15:07:31 +00002852 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002853 rtl_writephy(tp, 0x1f, 0x0002);
2854 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2855 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002856
françois romieu4da19632011-01-03 15:07:55 +00002857 rtl_writephy(tp, 0x1f, 0x0005);
2858 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002859
2860 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002861
françois romieu4da19632011-01-03 15:07:55 +00002862 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002863}
2864
françois romieubca03d52011-01-03 15:07:31 +00002865static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002866{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002867 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002868 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002869 { 0x1f, 0x0001 },
2870 { 0x06, 0x4064 },
2871 { 0x07, 0x2863 },
2872 { 0x08, 0x059c },
2873 { 0x09, 0x26b4 },
2874 { 0x0a, 0x6a19 },
2875 { 0x0b, 0xdcc8 },
2876 { 0x10, 0xf06d },
2877 { 0x14, 0x7f68 },
2878 { 0x18, 0x7fd9 },
2879 { 0x1c, 0xf0ff },
2880 { 0x1d, 0x3d9c },
2881 { 0x1f, 0x0003 },
2882 { 0x12, 0xf49f },
2883 { 0x13, 0x070b },
2884 { 0x1a, 0x05ad },
2885 { 0x14, 0x94c0 },
2886
françois romieubca03d52011-01-03 15:07:31 +00002887 /*
2888 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002889 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002890 */
françois romieudaf9df62009-10-07 12:44:20 +00002891 { 0x1f, 0x0002 },
2892 { 0x06, 0x5561 },
2893 { 0x1f, 0x0005 },
2894 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002895 { 0x06, 0x5561 },
2896
2897 /*
2898 * Can not link to 1Gbps with bad cable
2899 * Decrease SNR threshold form 21.07dB to 19.04dB
2900 */
2901 { 0x1f, 0x0001 },
2902 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002903
2904 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002905 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002906 };
2907
françois romieu4da19632011-01-03 15:07:55 +00002908 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002909
Francois Romieufdf6fc02012-07-06 22:40:38 +02002910 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002911 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002912 { 0x1f, 0x0002 },
2913 { 0x05, 0x669a },
2914 { 0x1f, 0x0005 },
2915 { 0x05, 0x8330 },
2916 { 0x06, 0x669a },
2917
2918 { 0x1f, 0x0002 }
2919 };
2920 int val;
2921
françois romieu4da19632011-01-03 15:07:55 +00002922 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002923
françois romieu4da19632011-01-03 15:07:55 +00002924 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002925 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002926 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002927 0x0065, 0x0066, 0x0067, 0x0068,
2928 0x0069, 0x006a, 0x006b, 0x006c
2929 };
2930 int i;
2931
françois romieu4da19632011-01-03 15:07:55 +00002932 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002933
2934 val &= 0xff00;
2935 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002936 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002937 }
2938 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002939 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002940 { 0x1f, 0x0002 },
2941 { 0x05, 0x2642 },
2942 { 0x1f, 0x0005 },
2943 { 0x05, 0x8330 },
2944 { 0x06, 0x2642 }
2945 };
2946
françois romieu4da19632011-01-03 15:07:55 +00002947 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002948 }
2949
françois romieubca03d52011-01-03 15:07:31 +00002950 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002951 rtl_writephy(tp, 0x1f, 0x0002);
2952 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2953 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002954
françois romieubca03d52011-01-03 15:07:31 +00002955 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002956 rtl_writephy(tp, 0x1f, 0x0002);
2957 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002958
françois romieu4da19632011-01-03 15:07:55 +00002959 rtl_writephy(tp, 0x1f, 0x0005);
2960 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002961
2962 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002963
françois romieu4da19632011-01-03 15:07:55 +00002964 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002965}
2966
françois romieu4da19632011-01-03 15:07:55 +00002967static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002968{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002969 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002970 { 0x1f, 0x0002 },
2971 { 0x10, 0x0008 },
2972 { 0x0d, 0x006c },
2973
2974 { 0x1f, 0x0000 },
2975 { 0x0d, 0xf880 },
2976
2977 { 0x1f, 0x0001 },
2978 { 0x17, 0x0cc0 },
2979
2980 { 0x1f, 0x0001 },
2981 { 0x0b, 0xa4d8 },
2982 { 0x09, 0x281c },
2983 { 0x07, 0x2883 },
2984 { 0x0a, 0x6b35 },
2985 { 0x1d, 0x3da4 },
2986 { 0x1c, 0xeffd },
2987 { 0x14, 0x7f52 },
2988 { 0x18, 0x7fc6 },
2989 { 0x08, 0x0601 },
2990 { 0x06, 0x4063 },
2991 { 0x10, 0xf074 },
2992 { 0x1f, 0x0003 },
2993 { 0x13, 0x0789 },
2994 { 0x12, 0xf4bd },
2995 { 0x1a, 0x04fd },
2996 { 0x14, 0x84b0 },
2997 { 0x1f, 0x0000 },
2998 { 0x00, 0x9200 },
2999
3000 { 0x1f, 0x0005 },
3001 { 0x01, 0x0340 },
3002 { 0x1f, 0x0001 },
3003 { 0x04, 0x4000 },
3004 { 0x03, 0x1d21 },
3005 { 0x02, 0x0c32 },
3006 { 0x01, 0x0200 },
3007 { 0x00, 0x5554 },
3008 { 0x04, 0x4800 },
3009 { 0x04, 0x4000 },
3010 { 0x04, 0xf000 },
3011 { 0x03, 0xdf01 },
3012 { 0x02, 0xdf20 },
3013 { 0x01, 0x101a },
3014 { 0x00, 0xa0ff },
3015 { 0x04, 0xf800 },
3016 { 0x04, 0xf000 },
3017 { 0x1f, 0x0000 },
3018
3019 { 0x1f, 0x0007 },
3020 { 0x1e, 0x0023 },
3021 { 0x16, 0x0000 },
3022 { 0x1f, 0x0000 }
3023 };
3024
françois romieu4da19632011-01-03 15:07:55 +00003025 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003026}
3027
françois romieue6de30d2011-01-03 15:08:37 +00003028static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3029{
3030 static const struct phy_reg phy_reg_init[] = {
3031 { 0x1f, 0x0001 },
3032 { 0x17, 0x0cc0 },
3033
3034 { 0x1f, 0x0007 },
3035 { 0x1e, 0x002d },
3036 { 0x18, 0x0040 },
3037 { 0x1f, 0x0000 }
3038 };
3039
3040 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3041 rtl_patchphy(tp, 0x0d, 1 << 5);
3042}
3043
Hayes Wang70090422011-07-06 15:58:06 +08003044static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003045{
3046 static const struct phy_reg phy_reg_init[] = {
3047 /* Enable Delay cap */
3048 { 0x1f, 0x0005 },
3049 { 0x05, 0x8b80 },
3050 { 0x06, 0xc896 },
3051 { 0x1f, 0x0000 },
3052
3053 /* Channel estimation fine tune */
3054 { 0x1f, 0x0001 },
3055 { 0x0b, 0x6c20 },
3056 { 0x07, 0x2872 },
3057 { 0x1c, 0xefff },
3058 { 0x1f, 0x0003 },
3059 { 0x14, 0x6420 },
3060 { 0x1f, 0x0000 },
3061
3062 /* Update PFM & 10M TX idle timer */
3063 { 0x1f, 0x0007 },
3064 { 0x1e, 0x002f },
3065 { 0x15, 0x1919 },
3066 { 0x1f, 0x0000 },
3067
3068 { 0x1f, 0x0007 },
3069 { 0x1e, 0x00ac },
3070 { 0x18, 0x0006 },
3071 { 0x1f, 0x0000 }
3072 };
3073
Francois Romieu15ecd032011-04-27 13:52:22 -07003074 rtl_apply_firmware(tp);
3075
hayeswang01dc7fe2011-03-21 01:50:28 +00003076 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3077
3078 /* DCO enable for 10M IDLE Power */
3079 rtl_writephy(tp, 0x1f, 0x0007);
3080 rtl_writephy(tp, 0x1e, 0x0023);
3081 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3082 rtl_writephy(tp, 0x1f, 0x0000);
3083
3084 /* For impedance matching */
3085 rtl_writephy(tp, 0x1f, 0x0002);
3086 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003087 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003088
3089 /* PHY auto speed down */
3090 rtl_writephy(tp, 0x1f, 0x0007);
3091 rtl_writephy(tp, 0x1e, 0x002d);
3092 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
3093 rtl_writephy(tp, 0x1f, 0x0000);
3094 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3095
3096 rtl_writephy(tp, 0x1f, 0x0005);
3097 rtl_writephy(tp, 0x05, 0x8b86);
3098 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3099 rtl_writephy(tp, 0x1f, 0x0000);
3100
3101 rtl_writephy(tp, 0x1f, 0x0005);
3102 rtl_writephy(tp, 0x05, 0x8b85);
3103 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3104 rtl_writephy(tp, 0x1f, 0x0007);
3105 rtl_writephy(tp, 0x1e, 0x0020);
3106 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
3107 rtl_writephy(tp, 0x1f, 0x0006);
3108 rtl_writephy(tp, 0x00, 0x5a00);
3109 rtl_writephy(tp, 0x1f, 0x0000);
3110 rtl_writephy(tp, 0x0d, 0x0007);
3111 rtl_writephy(tp, 0x0e, 0x003c);
3112 rtl_writephy(tp, 0x0d, 0x4007);
3113 rtl_writephy(tp, 0x0e, 0x0000);
3114 rtl_writephy(tp, 0x0d, 0x0000);
3115}
3116
françois romieu9ecb9aa2012-12-07 11:20:21 +00003117static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3118{
3119 const u16 w[] = {
3120 addr[0] | (addr[1] << 8),
3121 addr[2] | (addr[3] << 8),
3122 addr[4] | (addr[5] << 8)
3123 };
3124 const struct exgmac_reg e[] = {
3125 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3126 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3127 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3128 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3129 };
3130
3131 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3132}
3133
Hayes Wang70090422011-07-06 15:58:06 +08003134static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3135{
3136 static const struct phy_reg phy_reg_init[] = {
3137 /* Enable Delay cap */
3138 { 0x1f, 0x0004 },
3139 { 0x1f, 0x0007 },
3140 { 0x1e, 0x00ac },
3141 { 0x18, 0x0006 },
3142 { 0x1f, 0x0002 },
3143 { 0x1f, 0x0000 },
3144 { 0x1f, 0x0000 },
3145
3146 /* Channel estimation fine tune */
3147 { 0x1f, 0x0003 },
3148 { 0x09, 0xa20f },
3149 { 0x1f, 0x0000 },
3150 { 0x1f, 0x0000 },
3151
3152 /* Green Setting */
3153 { 0x1f, 0x0005 },
3154 { 0x05, 0x8b5b },
3155 { 0x06, 0x9222 },
3156 { 0x05, 0x8b6d },
3157 { 0x06, 0x8000 },
3158 { 0x05, 0x8b76 },
3159 { 0x06, 0x8000 },
3160 { 0x1f, 0x0000 }
3161 };
3162
3163 rtl_apply_firmware(tp);
3164
3165 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3166
3167 /* For 4-corner performance improve */
3168 rtl_writephy(tp, 0x1f, 0x0005);
3169 rtl_writephy(tp, 0x05, 0x8b80);
3170 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3171 rtl_writephy(tp, 0x1f, 0x0000);
3172
3173 /* PHY auto speed down */
3174 rtl_writephy(tp, 0x1f, 0x0004);
3175 rtl_writephy(tp, 0x1f, 0x0007);
3176 rtl_writephy(tp, 0x1e, 0x002d);
3177 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3178 rtl_writephy(tp, 0x1f, 0x0002);
3179 rtl_writephy(tp, 0x1f, 0x0000);
3180 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3181
3182 /* improve 10M EEE waveform */
3183 rtl_writephy(tp, 0x1f, 0x0005);
3184 rtl_writephy(tp, 0x05, 0x8b86);
3185 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3186 rtl_writephy(tp, 0x1f, 0x0000);
3187
3188 /* Improve 2-pair detection performance */
3189 rtl_writephy(tp, 0x1f, 0x0005);
3190 rtl_writephy(tp, 0x05, 0x8b85);
3191 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3192 rtl_writephy(tp, 0x1f, 0x0000);
3193
3194 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003195 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003196 rtl_writephy(tp, 0x1f, 0x0005);
3197 rtl_writephy(tp, 0x05, 0x8b85);
3198 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3199 rtl_writephy(tp, 0x1f, 0x0004);
3200 rtl_writephy(tp, 0x1f, 0x0007);
3201 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003202 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003203 rtl_writephy(tp, 0x1f, 0x0002);
3204 rtl_writephy(tp, 0x1f, 0x0000);
3205 rtl_writephy(tp, 0x0d, 0x0007);
3206 rtl_writephy(tp, 0x0e, 0x003c);
3207 rtl_writephy(tp, 0x0d, 0x4007);
3208 rtl_writephy(tp, 0x0e, 0x0000);
3209 rtl_writephy(tp, 0x0d, 0x0000);
3210
3211 /* Green feature */
3212 rtl_writephy(tp, 0x1f, 0x0003);
3213 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3214 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3215 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003216
françois romieu9ecb9aa2012-12-07 11:20:21 +00003217 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3218 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003219}
3220
Hayes Wang5f886e02012-03-30 14:33:03 +08003221static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3222{
3223 /* For 4-corner performance improve */
3224 rtl_writephy(tp, 0x1f, 0x0005);
3225 rtl_writephy(tp, 0x05, 0x8b80);
3226 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3227 rtl_writephy(tp, 0x1f, 0x0000);
3228
3229 /* PHY auto speed down */
3230 rtl_writephy(tp, 0x1f, 0x0007);
3231 rtl_writephy(tp, 0x1e, 0x002d);
3232 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3233 rtl_writephy(tp, 0x1f, 0x0000);
3234 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3235
3236 /* Improve 10M EEE waveform */
3237 rtl_writephy(tp, 0x1f, 0x0005);
3238 rtl_writephy(tp, 0x05, 0x8b86);
3239 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3240 rtl_writephy(tp, 0x1f, 0x0000);
3241}
3242
Hayes Wangc2218922011-09-06 16:55:18 +08003243static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3244{
3245 static const struct phy_reg phy_reg_init[] = {
3246 /* Channel estimation fine tune */
3247 { 0x1f, 0x0003 },
3248 { 0x09, 0xa20f },
3249 { 0x1f, 0x0000 },
3250
3251 /* Modify green table for giga & fnet */
3252 { 0x1f, 0x0005 },
3253 { 0x05, 0x8b55 },
3254 { 0x06, 0x0000 },
3255 { 0x05, 0x8b5e },
3256 { 0x06, 0x0000 },
3257 { 0x05, 0x8b67 },
3258 { 0x06, 0x0000 },
3259 { 0x05, 0x8b70 },
3260 { 0x06, 0x0000 },
3261 { 0x1f, 0x0000 },
3262 { 0x1f, 0x0007 },
3263 { 0x1e, 0x0078 },
3264 { 0x17, 0x0000 },
3265 { 0x19, 0x00fb },
3266 { 0x1f, 0x0000 },
3267
3268 /* Modify green table for 10M */
3269 { 0x1f, 0x0005 },
3270 { 0x05, 0x8b79 },
3271 { 0x06, 0xaa00 },
3272 { 0x1f, 0x0000 },
3273
3274 /* Disable hiimpedance detection (RTCT) */
3275 { 0x1f, 0x0003 },
3276 { 0x01, 0x328a },
3277 { 0x1f, 0x0000 }
3278 };
3279
3280 rtl_apply_firmware(tp);
3281
3282 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3283
Hayes Wang5f886e02012-03-30 14:33:03 +08003284 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003285
3286 /* Improve 2-pair detection performance */
3287 rtl_writephy(tp, 0x1f, 0x0005);
3288 rtl_writephy(tp, 0x05, 0x8b85);
3289 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3290 rtl_writephy(tp, 0x1f, 0x0000);
3291}
3292
3293static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3294{
3295 rtl_apply_firmware(tp);
3296
Hayes Wang5f886e02012-03-30 14:33:03 +08003297 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003298}
3299
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003300static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3301{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003302 static const struct phy_reg phy_reg_init[] = {
3303 /* Channel estimation fine tune */
3304 { 0x1f, 0x0003 },
3305 { 0x09, 0xa20f },
3306 { 0x1f, 0x0000 },
3307
3308 /* Modify green table for giga & fnet */
3309 { 0x1f, 0x0005 },
3310 { 0x05, 0x8b55 },
3311 { 0x06, 0x0000 },
3312 { 0x05, 0x8b5e },
3313 { 0x06, 0x0000 },
3314 { 0x05, 0x8b67 },
3315 { 0x06, 0x0000 },
3316 { 0x05, 0x8b70 },
3317 { 0x06, 0x0000 },
3318 { 0x1f, 0x0000 },
3319 { 0x1f, 0x0007 },
3320 { 0x1e, 0x0078 },
3321 { 0x17, 0x0000 },
3322 { 0x19, 0x00aa },
3323 { 0x1f, 0x0000 },
3324
3325 /* Modify green table for 10M */
3326 { 0x1f, 0x0005 },
3327 { 0x05, 0x8b79 },
3328 { 0x06, 0xaa00 },
3329 { 0x1f, 0x0000 },
3330
3331 /* Disable hiimpedance detection (RTCT) */
3332 { 0x1f, 0x0003 },
3333 { 0x01, 0x328a },
3334 { 0x1f, 0x0000 }
3335 };
3336
3337
3338 rtl_apply_firmware(tp);
3339
3340 rtl8168f_hw_phy_config(tp);
3341
3342 /* Improve 2-pair detection performance */
3343 rtl_writephy(tp, 0x1f, 0x0005);
3344 rtl_writephy(tp, 0x05, 0x8b85);
3345 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3346 rtl_writephy(tp, 0x1f, 0x0000);
3347
3348 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3349
3350 /* Modify green table for giga */
3351 rtl_writephy(tp, 0x1f, 0x0005);
3352 rtl_writephy(tp, 0x05, 0x8b54);
3353 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3354 rtl_writephy(tp, 0x05, 0x8b5d);
3355 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3356 rtl_writephy(tp, 0x05, 0x8a7c);
3357 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3358 rtl_writephy(tp, 0x05, 0x8a7f);
3359 rtl_w1w0_phy(tp, 0x06, 0x0100, 0x0000);
3360 rtl_writephy(tp, 0x05, 0x8a82);
3361 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3362 rtl_writephy(tp, 0x05, 0x8a85);
3363 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3364 rtl_writephy(tp, 0x05, 0x8a88);
3365 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3366 rtl_writephy(tp, 0x1f, 0x0000);
3367
3368 /* uc same-seed solution */
3369 rtl_writephy(tp, 0x1f, 0x0005);
3370 rtl_writephy(tp, 0x05, 0x8b85);
3371 rtl_w1w0_phy(tp, 0x06, 0x8000, 0x0000);
3372 rtl_writephy(tp, 0x1f, 0x0000);
3373
3374 /* eee setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003375 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003376 rtl_writephy(tp, 0x1f, 0x0005);
3377 rtl_writephy(tp, 0x05, 0x8b85);
3378 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3379 rtl_writephy(tp, 0x1f, 0x0004);
3380 rtl_writephy(tp, 0x1f, 0x0007);
3381 rtl_writephy(tp, 0x1e, 0x0020);
3382 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3383 rtl_writephy(tp, 0x1f, 0x0000);
3384 rtl_writephy(tp, 0x0d, 0x0007);
3385 rtl_writephy(tp, 0x0e, 0x003c);
3386 rtl_writephy(tp, 0x0d, 0x4007);
3387 rtl_writephy(tp, 0x0e, 0x0000);
3388 rtl_writephy(tp, 0x0d, 0x0000);
3389
3390 /* Green feature */
3391 rtl_writephy(tp, 0x1f, 0x0003);
3392 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3393 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3394 rtl_writephy(tp, 0x1f, 0x0000);
3395}
3396
Hayes Wangc5583862012-07-02 17:23:22 +08003397static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3398{
Hayes Wangc5583862012-07-02 17:23:22 +08003399 rtl_apply_firmware(tp);
3400
hayeswang41f44d12013-04-01 22:23:36 +00003401 rtl_writephy(tp, 0x1f, 0x0a46);
3402 if (rtl_readphy(tp, 0x10) & 0x0100) {
3403 rtl_writephy(tp, 0x1f, 0x0bcc);
3404 rtl_w1w0_phy(tp, 0x12, 0x0000, 0x8000);
3405 } else {
3406 rtl_writephy(tp, 0x1f, 0x0bcc);
3407 rtl_w1w0_phy(tp, 0x12, 0x8000, 0x0000);
3408 }
Hayes Wangc5583862012-07-02 17:23:22 +08003409
hayeswang41f44d12013-04-01 22:23:36 +00003410 rtl_writephy(tp, 0x1f, 0x0a46);
3411 if (rtl_readphy(tp, 0x13) & 0x0100) {
3412 rtl_writephy(tp, 0x1f, 0x0c41);
3413 rtl_w1w0_phy(tp, 0x15, 0x0002, 0x0000);
3414 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003415 rtl_writephy(tp, 0x1f, 0x0c41);
3416 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003417 }
Hayes Wangc5583862012-07-02 17:23:22 +08003418
hayeswang41f44d12013-04-01 22:23:36 +00003419 /* Enable PHY auto speed down */
3420 rtl_writephy(tp, 0x1f, 0x0a44);
3421 rtl_w1w0_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003422
hayeswangfe7524c2013-04-01 22:23:37 +00003423 rtl_writephy(tp, 0x1f, 0x0bcc);
3424 rtl_w1w0_phy(tp, 0x14, 0x0100, 0x0000);
3425 rtl_writephy(tp, 0x1f, 0x0a44);
3426 rtl_w1w0_phy(tp, 0x11, 0x00c0, 0x0000);
3427 rtl_writephy(tp, 0x1f, 0x0a43);
3428 rtl_writephy(tp, 0x13, 0x8084);
3429 rtl_w1w0_phy(tp, 0x14, 0x0000, 0x6000);
3430 rtl_w1w0_phy(tp, 0x10, 0x1003, 0x0000);
3431
hayeswang41f44d12013-04-01 22:23:36 +00003432 /* EEE auto-fallback function */
3433 rtl_writephy(tp, 0x1f, 0x0a4b);
3434 rtl_w1w0_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003435
hayeswang41f44d12013-04-01 22:23:36 +00003436 /* Enable UC LPF tune function */
3437 rtl_writephy(tp, 0x1f, 0x0a43);
3438 rtl_writephy(tp, 0x13, 0x8012);
3439 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3440
3441 rtl_writephy(tp, 0x1f, 0x0c42);
3442 rtl_w1w0_phy(tp, 0x11, 0x4000, 0x2000);
3443
hayeswangfe7524c2013-04-01 22:23:37 +00003444 /* Improve SWR Efficiency */
3445 rtl_writephy(tp, 0x1f, 0x0bcd);
3446 rtl_writephy(tp, 0x14, 0x5065);
3447 rtl_writephy(tp, 0x14, 0xd065);
3448 rtl_writephy(tp, 0x1f, 0x0bc8);
3449 rtl_writephy(tp, 0x11, 0x5655);
3450 rtl_writephy(tp, 0x1f, 0x0bcd);
3451 rtl_writephy(tp, 0x14, 0x1065);
3452 rtl_writephy(tp, 0x14, 0x9065);
3453 rtl_writephy(tp, 0x14, 0x1065);
3454
David Chang1bac1072013-11-27 15:48:36 +08003455 /* Check ALDPS bit, disable it if enabled */
3456 rtl_writephy(tp, 0x1f, 0x0a43);
3457 if (rtl_readphy(tp, 0x10) & 0x0004)
3458 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0004);
3459
hayeswang41f44d12013-04-01 22:23:36 +00003460 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003461}
3462
hayeswang57538c42013-04-01 22:23:40 +00003463static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3464{
3465 rtl_apply_firmware(tp);
3466}
3467
françois romieu4da19632011-01-03 15:07:55 +00003468static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003469{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003470 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003471 { 0x1f, 0x0003 },
3472 { 0x08, 0x441d },
3473 { 0x01, 0x9100 },
3474 { 0x1f, 0x0000 }
3475 };
3476
françois romieu4da19632011-01-03 15:07:55 +00003477 rtl_writephy(tp, 0x1f, 0x0000);
3478 rtl_patchphy(tp, 0x11, 1 << 12);
3479 rtl_patchphy(tp, 0x19, 1 << 13);
3480 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003481
françois romieu4da19632011-01-03 15:07:55 +00003482 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003483}
3484
Hayes Wang5a5e4442011-02-22 17:26:21 +08003485static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3486{
3487 static const struct phy_reg phy_reg_init[] = {
3488 { 0x1f, 0x0005 },
3489 { 0x1a, 0x0000 },
3490 { 0x1f, 0x0000 },
3491
3492 { 0x1f, 0x0004 },
3493 { 0x1c, 0x0000 },
3494 { 0x1f, 0x0000 },
3495
3496 { 0x1f, 0x0001 },
3497 { 0x15, 0x7701 },
3498 { 0x1f, 0x0000 }
3499 };
3500
3501 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003502 rtl_writephy(tp, 0x1f, 0x0000);
3503 rtl_writephy(tp, 0x18, 0x0310);
3504 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003505
François Romieu953a12c2011-04-24 17:38:48 +02003506 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003507
3508 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3509}
3510
Hayes Wang7e18dca2012-03-30 14:33:02 +08003511static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3512{
Hayes Wang7e18dca2012-03-30 14:33:02 +08003513 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01003514 rtl_writephy(tp, 0x1f, 0x0000);
3515 rtl_writephy(tp, 0x18, 0x0310);
3516 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003517
3518 rtl_apply_firmware(tp);
3519
3520 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003521 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003522 rtl_writephy(tp, 0x1f, 0x0004);
3523 rtl_writephy(tp, 0x10, 0x401f);
3524 rtl_writephy(tp, 0x19, 0x7030);
3525 rtl_writephy(tp, 0x1f, 0x0000);
3526}
3527
Hayes Wang5598bfe2012-07-02 17:23:21 +08003528static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3529{
Hayes Wang5598bfe2012-07-02 17:23:21 +08003530 static const struct phy_reg phy_reg_init[] = {
3531 { 0x1f, 0x0004 },
3532 { 0x10, 0xc07f },
3533 { 0x19, 0x7030 },
3534 { 0x1f, 0x0000 }
3535 };
3536
3537 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003538 rtl_writephy(tp, 0x1f, 0x0000);
3539 rtl_writephy(tp, 0x18, 0x0310);
3540 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003541
3542 rtl_apply_firmware(tp);
3543
Francois Romieufdf6fc02012-07-06 22:40:38 +02003544 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003545 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3546
Francois Romieufdf6fc02012-07-06 22:40:38 +02003547 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003548}
3549
Francois Romieu5615d9f2007-08-17 17:50:46 +02003550static void rtl_hw_phy_config(struct net_device *dev)
3551{
3552 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003553
3554 rtl8169_print_mac_version(tp);
3555
3556 switch (tp->mac_version) {
3557 case RTL_GIGA_MAC_VER_01:
3558 break;
3559 case RTL_GIGA_MAC_VER_02:
3560 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003561 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003562 break;
3563 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003564 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003565 break;
françois romieu2e9558562009-08-10 19:44:19 +00003566 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003567 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003568 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003569 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003570 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003571 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003572 case RTL_GIGA_MAC_VER_07:
3573 case RTL_GIGA_MAC_VER_08:
3574 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003575 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003576 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003577 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003578 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003579 break;
3580 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003581 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003582 break;
3583 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003584 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003585 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003586 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003587 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003588 break;
3589 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003590 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003591 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003592 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003593 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003594 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003595 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003596 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003597 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003598 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003599 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003600 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003601 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003602 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003603 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003604 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003605 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003606 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003607 break;
3608 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003609 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003610 break;
3611 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003612 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003613 break;
françois romieue6de30d2011-01-03 15:08:37 +00003614 case RTL_GIGA_MAC_VER_28:
3615 rtl8168d_4_hw_phy_config(tp);
3616 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003617 case RTL_GIGA_MAC_VER_29:
3618 case RTL_GIGA_MAC_VER_30:
3619 rtl8105e_hw_phy_config(tp);
3620 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003621 case RTL_GIGA_MAC_VER_31:
3622 /* None. */
3623 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003624 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003625 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003626 rtl8168e_1_hw_phy_config(tp);
3627 break;
3628 case RTL_GIGA_MAC_VER_34:
3629 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003630 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003631 case RTL_GIGA_MAC_VER_35:
3632 rtl8168f_1_hw_phy_config(tp);
3633 break;
3634 case RTL_GIGA_MAC_VER_36:
3635 rtl8168f_2_hw_phy_config(tp);
3636 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003637
Hayes Wang7e18dca2012-03-30 14:33:02 +08003638 case RTL_GIGA_MAC_VER_37:
3639 rtl8402_hw_phy_config(tp);
3640 break;
3641
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003642 case RTL_GIGA_MAC_VER_38:
3643 rtl8411_hw_phy_config(tp);
3644 break;
3645
Hayes Wang5598bfe2012-07-02 17:23:21 +08003646 case RTL_GIGA_MAC_VER_39:
3647 rtl8106e_hw_phy_config(tp);
3648 break;
3649
Hayes Wangc5583862012-07-02 17:23:22 +08003650 case RTL_GIGA_MAC_VER_40:
3651 rtl8168g_1_hw_phy_config(tp);
3652 break;
hayeswang57538c42013-04-01 22:23:40 +00003653 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00003654 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08003655 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00003656 rtl8168g_2_hw_phy_config(tp);
3657 break;
Hayes Wangc5583862012-07-02 17:23:22 +08003658
3659 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02003660 default:
3661 break;
3662 }
3663}
3664
Francois Romieuda78dbf2012-01-26 14:18:23 +01003665static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 struct timer_list *timer = &tp->timer;
3668 void __iomem *ioaddr = tp->mmio_addr;
3669 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3670
Francois Romieubcf0bf92006-07-26 23:14:13 +02003671 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
françois romieu4da19632011-01-03 15:07:55 +00003673 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003674 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 * A busy loop could burn quite a few cycles on nowadays CPU.
3676 * Let's delay the execution of the timer for a few ticks.
3677 */
3678 timeout = HZ/10;
3679 goto out_mod_timer;
3680 }
3681
3682 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003683 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02003685 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686
françois romieu4da19632011-01-03 15:07:55 +00003687 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
3689out_mod_timer:
3690 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003691}
3692
3693static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3694{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003695 if (!test_and_set_bit(flag, tp->wk.flags))
3696 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003697}
3698
3699static void rtl8169_phy_timer(unsigned long __opaque)
3700{
3701 struct net_device *dev = (struct net_device *)__opaque;
3702 struct rtl8169_private *tp = netdev_priv(dev);
3703
Francois Romieu98ddf982012-01-31 10:47:34 +01003704 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705}
3706
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3708 void __iomem *ioaddr)
3709{
3710 iounmap(ioaddr);
3711 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003712 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 pci_disable_device(pdev);
3714 free_netdev(dev);
3715}
3716
Francois Romieuffc46952012-07-06 14:19:23 +02003717DECLARE_RTL_COND(rtl_phy_reset_cond)
3718{
3719 return tp->phy_reset_pending(tp);
3720}
3721
Francois Romieubf793292006-11-01 00:53:05 +01003722static void rtl8169_phy_reset(struct net_device *dev,
3723 struct rtl8169_private *tp)
3724{
françois romieu4da19632011-01-03 15:07:55 +00003725 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02003726 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01003727}
3728
David S. Miller8decf862011-09-22 03:23:13 -04003729static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3730{
3731 void __iomem *ioaddr = tp->mmio_addr;
3732
3733 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3734 (RTL_R8(PHYstatus) & TBI_Enable);
3735}
3736
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003737static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003739 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003740
Francois Romieu5615d9f2007-08-17 17:50:46 +02003741 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003742
Marcus Sundberg773328942008-07-10 21:28:08 +02003743 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3744 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3745 RTL_W8(0x82, 0x01);
3746 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003747
Francois Romieu6dccd162007-02-13 23:38:05 +01003748 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3749
3750 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3751 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003752
Francois Romieubcf0bf92006-07-26 23:14:13 +02003753 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003754 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3755 RTL_W8(0x82, 0x01);
3756 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003757 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003758 }
3759
Francois Romieubf793292006-11-01 00:53:05 +01003760 rtl8169_phy_reset(dev, tp);
3761
Oliver Neukum54405cd2011-01-06 21:55:13 +01003762 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003763 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3764 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3765 (tp->mii.supports_gmii ?
3766 ADVERTISED_1000baseT_Half |
3767 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003768
David S. Miller8decf862011-09-22 03:23:13 -04003769 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003770 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003771}
3772
Francois Romieu773d2022007-01-31 23:47:43 +01003773static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3774{
3775 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu773d2022007-01-31 23:47:43 +01003776
Francois Romieuda78dbf2012-01-26 14:18:23 +01003777 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003778
3779 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003780
françois romieu9ecb9aa2012-12-07 11:20:21 +00003781 RTL_W32(MAC4, addr[4] | addr[5] << 8);
françois romieu908ba2b2010-04-26 11:42:58 +00003782 RTL_R32(MAC4);
3783
françois romieu9ecb9aa2012-12-07 11:20:21 +00003784 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
françois romieu908ba2b2010-04-26 11:42:58 +00003785 RTL_R32(MAC0);
3786
françois romieu9ecb9aa2012-12-07 11:20:21 +00003787 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
3788 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00003789
Francois Romieu773d2022007-01-31 23:47:43 +01003790 RTL_W8(Cfg9346, Cfg9346_Lock);
3791
Francois Romieuda78dbf2012-01-26 14:18:23 +01003792 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003793}
3794
3795static int rtl_set_mac_address(struct net_device *dev, void *p)
3796{
3797 struct rtl8169_private *tp = netdev_priv(dev);
3798 struct sockaddr *addr = p;
3799
3800 if (!is_valid_ether_addr(addr->sa_data))
3801 return -EADDRNOTAVAIL;
3802
3803 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3804
3805 rtl_rar_set(tp, dev->dev_addr);
3806
3807 return 0;
3808}
3809
Francois Romieu5f787a12006-08-17 13:02:36 +02003810static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3811{
3812 struct rtl8169_private *tp = netdev_priv(dev);
3813 struct mii_ioctl_data *data = if_mii(ifr);
3814
Francois Romieu8b4ab282008-11-19 22:05:25 -08003815 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3816}
Francois Romieu5f787a12006-08-17 13:02:36 +02003817
Francois Romieucecb5fd2011-04-01 10:21:07 +02003818static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3819 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003820{
Francois Romieu5f787a12006-08-17 13:02:36 +02003821 switch (cmd) {
3822 case SIOCGMIIPHY:
3823 data->phy_id = 32; /* Internal PHY */
3824 return 0;
3825
3826 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003827 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003828 return 0;
3829
3830 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003831 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003832 return 0;
3833 }
3834 return -EOPNOTSUPP;
3835}
3836
Francois Romieu8b4ab282008-11-19 22:05:25 -08003837static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3838{
3839 return -EOPNOTSUPP;
3840}
3841
Francois Romieufbac58f2007-10-04 22:51:38 +02003842static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3843{
3844 if (tp->features & RTL_FEATURE_MSI) {
3845 pci_disable_msi(pdev);
3846 tp->features &= ~RTL_FEATURE_MSI;
3847 }
3848}
3849
Bill Pembertonbaf63292012-12-03 09:23:28 -05003850static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00003851{
3852 struct mdio_ops *ops = &tp->mdio_ops;
3853
3854 switch (tp->mac_version) {
3855 case RTL_GIGA_MAC_VER_27:
3856 ops->write = r8168dp_1_mdio_write;
3857 ops->read = r8168dp_1_mdio_read;
3858 break;
françois romieue6de30d2011-01-03 15:08:37 +00003859 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003860 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003861 ops->write = r8168dp_2_mdio_write;
3862 ops->read = r8168dp_2_mdio_read;
3863 break;
Hayes Wangc5583862012-07-02 17:23:22 +08003864 case RTL_GIGA_MAC_VER_40:
3865 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00003866 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00003867 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08003868 case RTL_GIGA_MAC_VER_44:
Hayes Wangc5583862012-07-02 17:23:22 +08003869 ops->write = r8168g_mdio_write;
3870 ops->read = r8168g_mdio_read;
3871 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003872 default:
3873 ops->write = r8169_mdio_write;
3874 ops->read = r8169_mdio_read;
3875 break;
3876 }
3877}
3878
hayeswange2409d82013-03-31 17:02:04 +00003879static void rtl_speed_down(struct rtl8169_private *tp)
3880{
3881 u32 adv;
3882 int lpa;
3883
3884 rtl_writephy(tp, 0x1f, 0x0000);
3885 lpa = rtl_readphy(tp, MII_LPA);
3886
3887 if (lpa & (LPA_10HALF | LPA_10FULL))
3888 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
3889 else if (lpa & (LPA_100HALF | LPA_100FULL))
3890 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3891 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
3892 else
3893 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3894 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3895 (tp->mii.supports_gmii ?
3896 ADVERTISED_1000baseT_Half |
3897 ADVERTISED_1000baseT_Full : 0);
3898
3899 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
3900 adv);
3901}
3902
David S. Miller1805b2f2011-10-24 18:18:09 -04003903static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3904{
3905 void __iomem *ioaddr = tp->mmio_addr;
3906
3907 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00003908 case RTL_GIGA_MAC_VER_25:
3909 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04003910 case RTL_GIGA_MAC_VER_29:
3911 case RTL_GIGA_MAC_VER_30:
3912 case RTL_GIGA_MAC_VER_32:
3913 case RTL_GIGA_MAC_VER_33:
3914 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08003915 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003916 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08003917 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08003918 case RTL_GIGA_MAC_VER_40:
3919 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00003920 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00003921 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08003922 case RTL_GIGA_MAC_VER_44:
David S. Miller1805b2f2011-10-24 18:18:09 -04003923 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3924 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3925 break;
3926 default:
3927 break;
3928 }
3929}
3930
3931static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3932{
3933 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3934 return false;
3935
hayeswange2409d82013-03-31 17:02:04 +00003936 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04003937 rtl_wol_suspend_quirk(tp);
3938
3939 return true;
3940}
3941
françois romieu065c27c2011-01-03 15:08:12 +00003942static void r810x_phy_power_down(struct rtl8169_private *tp)
3943{
3944 rtl_writephy(tp, 0x1f, 0x0000);
3945 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3946}
3947
3948static void r810x_phy_power_up(struct rtl8169_private *tp)
3949{
3950 rtl_writephy(tp, 0x1f, 0x0000);
3951 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3952}
3953
3954static void r810x_pll_power_down(struct rtl8169_private *tp)
3955{
Hayes Wang00042992012-03-30 14:33:00 +08003956 void __iomem *ioaddr = tp->mmio_addr;
3957
David S. Miller1805b2f2011-10-24 18:18:09 -04003958 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003959 return;
françois romieu065c27c2011-01-03 15:08:12 +00003960
3961 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003962
3963 switch (tp->mac_version) {
3964 case RTL_GIGA_MAC_VER_07:
3965 case RTL_GIGA_MAC_VER_08:
3966 case RTL_GIGA_MAC_VER_09:
3967 case RTL_GIGA_MAC_VER_10:
3968 case RTL_GIGA_MAC_VER_13:
3969 case RTL_GIGA_MAC_VER_16:
3970 break;
3971 default:
3972 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3973 break;
3974 }
françois romieu065c27c2011-01-03 15:08:12 +00003975}
3976
3977static void r810x_pll_power_up(struct rtl8169_private *tp)
3978{
Hayes Wang00042992012-03-30 14:33:00 +08003979 void __iomem *ioaddr = tp->mmio_addr;
3980
françois romieu065c27c2011-01-03 15:08:12 +00003981 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003982
3983 switch (tp->mac_version) {
3984 case RTL_GIGA_MAC_VER_07:
3985 case RTL_GIGA_MAC_VER_08:
3986 case RTL_GIGA_MAC_VER_09:
3987 case RTL_GIGA_MAC_VER_10:
3988 case RTL_GIGA_MAC_VER_13:
3989 case RTL_GIGA_MAC_VER_16:
3990 break;
3991 default:
3992 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3993 break;
3994 }
françois romieu065c27c2011-01-03 15:08:12 +00003995}
3996
3997static void r8168_phy_power_up(struct rtl8169_private *tp)
3998{
3999 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004000 switch (tp->mac_version) {
4001 case RTL_GIGA_MAC_VER_11:
4002 case RTL_GIGA_MAC_VER_12:
4003 case RTL_GIGA_MAC_VER_17:
4004 case RTL_GIGA_MAC_VER_18:
4005 case RTL_GIGA_MAC_VER_19:
4006 case RTL_GIGA_MAC_VER_20:
4007 case RTL_GIGA_MAC_VER_21:
4008 case RTL_GIGA_MAC_VER_22:
4009 case RTL_GIGA_MAC_VER_23:
4010 case RTL_GIGA_MAC_VER_24:
4011 case RTL_GIGA_MAC_VER_25:
4012 case RTL_GIGA_MAC_VER_26:
4013 case RTL_GIGA_MAC_VER_27:
4014 case RTL_GIGA_MAC_VER_28:
4015 case RTL_GIGA_MAC_VER_31:
4016 rtl_writephy(tp, 0x0e, 0x0000);
4017 break;
4018 default:
4019 break;
4020 }
françois romieu065c27c2011-01-03 15:08:12 +00004021 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4022}
4023
4024static void r8168_phy_power_down(struct rtl8169_private *tp)
4025{
4026 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004027 switch (tp->mac_version) {
4028 case RTL_GIGA_MAC_VER_32:
4029 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004030 case RTL_GIGA_MAC_VER_40:
4031 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004032 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4033 break;
4034
4035 case RTL_GIGA_MAC_VER_11:
4036 case RTL_GIGA_MAC_VER_12:
4037 case RTL_GIGA_MAC_VER_17:
4038 case RTL_GIGA_MAC_VER_18:
4039 case RTL_GIGA_MAC_VER_19:
4040 case RTL_GIGA_MAC_VER_20:
4041 case RTL_GIGA_MAC_VER_21:
4042 case RTL_GIGA_MAC_VER_22:
4043 case RTL_GIGA_MAC_VER_23:
4044 case RTL_GIGA_MAC_VER_24:
4045 case RTL_GIGA_MAC_VER_25:
4046 case RTL_GIGA_MAC_VER_26:
4047 case RTL_GIGA_MAC_VER_27:
4048 case RTL_GIGA_MAC_VER_28:
4049 case RTL_GIGA_MAC_VER_31:
4050 rtl_writephy(tp, 0x0e, 0x0200);
4051 default:
4052 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4053 break;
4054 }
françois romieu065c27c2011-01-03 15:08:12 +00004055}
4056
4057static void r8168_pll_power_down(struct rtl8169_private *tp)
4058{
4059 void __iomem *ioaddr = tp->mmio_addr;
4060
Francois Romieucecb5fd2011-04-01 10:21:07 +02004061 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4062 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4063 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00004064 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00004065 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08004066 }
françois romieu065c27c2011-01-03 15:08:12 +00004067
Francois Romieucecb5fd2011-04-01 10:21:07 +02004068 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4069 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00004070 (RTL_R16(CPlusCmd) & ASF)) {
4071 return;
4072 }
4073
hayeswang01dc7fe2011-03-21 01:50:28 +00004074 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4075 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004076 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004077
David S. Miller1805b2f2011-10-24 18:18:09 -04004078 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004079 return;
françois romieu065c27c2011-01-03 15:08:12 +00004080
4081 r8168_phy_power_down(tp);
4082
4083 switch (tp->mac_version) {
4084 case RTL_GIGA_MAC_VER_25:
4085 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004086 case RTL_GIGA_MAC_VER_27:
4087 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004088 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004089 case RTL_GIGA_MAC_VER_32:
4090 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00004091 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4092 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004093 case RTL_GIGA_MAC_VER_40:
4094 case RTL_GIGA_MAC_VER_41:
4095 rtl_w1w0_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
4096 0xfc000000, ERIAR_EXGMAC);
4097 break;
françois romieu065c27c2011-01-03 15:08:12 +00004098 }
4099}
4100
4101static void r8168_pll_power_up(struct rtl8169_private *tp)
4102{
4103 void __iomem *ioaddr = tp->mmio_addr;
4104
françois romieu065c27c2011-01-03 15:08:12 +00004105 switch (tp->mac_version) {
4106 case RTL_GIGA_MAC_VER_25:
4107 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004108 case RTL_GIGA_MAC_VER_27:
4109 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004110 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004111 case RTL_GIGA_MAC_VER_32:
4112 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00004113 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4114 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004115 case RTL_GIGA_MAC_VER_40:
4116 case RTL_GIGA_MAC_VER_41:
4117 rtl_w1w0_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
4118 0x00000000, ERIAR_EXGMAC);
4119 break;
françois romieu065c27c2011-01-03 15:08:12 +00004120 }
4121
4122 r8168_phy_power_up(tp);
4123}
4124
Francois Romieud58d46b2011-05-03 16:38:29 +02004125static void rtl_generic_op(struct rtl8169_private *tp,
4126 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00004127{
4128 if (op)
4129 op(tp);
4130}
4131
4132static void rtl_pll_power_down(struct rtl8169_private *tp)
4133{
Francois Romieud58d46b2011-05-03 16:38:29 +02004134 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00004135}
4136
4137static void rtl_pll_power_up(struct rtl8169_private *tp)
4138{
Francois Romieud58d46b2011-05-03 16:38:29 +02004139 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00004140}
4141
Bill Pembertonbaf63292012-12-03 09:23:28 -05004142static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00004143{
4144 struct pll_power_ops *ops = &tp->pll_power_ops;
4145
4146 switch (tp->mac_version) {
4147 case RTL_GIGA_MAC_VER_07:
4148 case RTL_GIGA_MAC_VER_08:
4149 case RTL_GIGA_MAC_VER_09:
4150 case RTL_GIGA_MAC_VER_10:
4151 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08004152 case RTL_GIGA_MAC_VER_29:
4153 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004154 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004155 case RTL_GIGA_MAC_VER_39:
hayeswang58152cd2013-04-01 22:23:42 +00004156 case RTL_GIGA_MAC_VER_43:
françois romieu065c27c2011-01-03 15:08:12 +00004157 ops->down = r810x_pll_power_down;
4158 ops->up = r810x_pll_power_up;
4159 break;
4160
4161 case RTL_GIGA_MAC_VER_11:
4162 case RTL_GIGA_MAC_VER_12:
4163 case RTL_GIGA_MAC_VER_17:
4164 case RTL_GIGA_MAC_VER_18:
4165 case RTL_GIGA_MAC_VER_19:
4166 case RTL_GIGA_MAC_VER_20:
4167 case RTL_GIGA_MAC_VER_21:
4168 case RTL_GIGA_MAC_VER_22:
4169 case RTL_GIGA_MAC_VER_23:
4170 case RTL_GIGA_MAC_VER_24:
4171 case RTL_GIGA_MAC_VER_25:
4172 case RTL_GIGA_MAC_VER_26:
4173 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00004174 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004175 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004176 case RTL_GIGA_MAC_VER_32:
4177 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004178 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08004179 case RTL_GIGA_MAC_VER_35:
4180 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004181 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08004182 case RTL_GIGA_MAC_VER_40:
4183 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004184 case RTL_GIGA_MAC_VER_42:
hayeswang45dd95c2013-07-08 17:09:01 +08004185 case RTL_GIGA_MAC_VER_44:
françois romieu065c27c2011-01-03 15:08:12 +00004186 ops->down = r8168_pll_power_down;
4187 ops->up = r8168_pll_power_up;
4188 break;
4189
4190 default:
4191 ops->down = NULL;
4192 ops->up = NULL;
4193 break;
4194 }
4195}
4196
Hayes Wange542a222011-07-06 15:58:04 +08004197static void rtl_init_rxcfg(struct rtl8169_private *tp)
4198{
4199 void __iomem *ioaddr = tp->mmio_addr;
4200
4201 switch (tp->mac_version) {
4202 case RTL_GIGA_MAC_VER_01:
4203 case RTL_GIGA_MAC_VER_02:
4204 case RTL_GIGA_MAC_VER_03:
4205 case RTL_GIGA_MAC_VER_04:
4206 case RTL_GIGA_MAC_VER_05:
4207 case RTL_GIGA_MAC_VER_06:
4208 case RTL_GIGA_MAC_VER_10:
4209 case RTL_GIGA_MAC_VER_11:
4210 case RTL_GIGA_MAC_VER_12:
4211 case RTL_GIGA_MAC_VER_13:
4212 case RTL_GIGA_MAC_VER_14:
4213 case RTL_GIGA_MAC_VER_15:
4214 case RTL_GIGA_MAC_VER_16:
4215 case RTL_GIGA_MAC_VER_17:
4216 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4217 break;
4218 case RTL_GIGA_MAC_VER_18:
4219 case RTL_GIGA_MAC_VER_19:
4220 case RTL_GIGA_MAC_VER_20:
4221 case RTL_GIGA_MAC_VER_21:
4222 case RTL_GIGA_MAC_VER_22:
4223 case RTL_GIGA_MAC_VER_23:
4224 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004225 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02004226 case RTL_GIGA_MAC_VER_35:
Hayes Wange542a222011-07-06 15:58:04 +08004227 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4228 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004229 case RTL_GIGA_MAC_VER_40:
Michel Dänzer7a9810e2014-07-17 12:55:40 +09004230 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
4231 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004232 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004233 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004234 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004235 case RTL_GIGA_MAC_VER_44:
hayeswangbeb330a2013-04-01 22:23:39 +00004236 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST | RX_EARLY_OFF);
4237 break;
Hayes Wange542a222011-07-06 15:58:04 +08004238 default:
4239 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
4240 break;
4241 }
4242}
4243
Hayes Wang92fc43b2011-07-06 15:58:03 +08004244static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4245{
Timo Teräs9fba0812013-01-15 21:01:24 +00004246 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004247}
4248
Francois Romieud58d46b2011-05-03 16:38:29 +02004249static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4250{
françois romieu9c5028e2012-03-02 04:43:14 +00004251 void __iomem *ioaddr = tp->mmio_addr;
4252
4253 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004254 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00004255 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004256}
4257
4258static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4259{
françois romieu9c5028e2012-03-02 04:43:14 +00004260 void __iomem *ioaddr = tp->mmio_addr;
4261
4262 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004263 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00004264 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004265}
4266
4267static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4268{
4269 void __iomem *ioaddr = tp->mmio_addr;
4270
4271 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4272 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
4273 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
4274}
4275
4276static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4277{
4278 void __iomem *ioaddr = tp->mmio_addr;
4279
4280 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4281 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
4282 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
4283}
4284
4285static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4286{
4287 void __iomem *ioaddr = tp->mmio_addr;
4288
4289 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4290}
4291
4292static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4293{
4294 void __iomem *ioaddr = tp->mmio_addr;
4295
4296 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4297}
4298
4299static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4300{
4301 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004302
4303 RTL_W8(MaxTxPacketSize, 0x3f);
4304 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4305 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004306 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004307}
4308
4309static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4310{
4311 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004312
4313 RTL_W8(MaxTxPacketSize, 0x0c);
4314 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4315 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004316 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004317}
4318
4319static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4320{
4321 rtl_tx_performance_tweak(tp->pci_dev,
4322 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4323}
4324
4325static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4326{
4327 rtl_tx_performance_tweak(tp->pci_dev,
4328 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4329}
4330
4331static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4332{
4333 void __iomem *ioaddr = tp->mmio_addr;
4334
4335 r8168b_0_hw_jumbo_enable(tp);
4336
4337 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
4338}
4339
4340static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4341{
4342 void __iomem *ioaddr = tp->mmio_addr;
4343
4344 r8168b_0_hw_jumbo_disable(tp);
4345
4346 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4347}
4348
Bill Pembertonbaf63292012-12-03 09:23:28 -05004349static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02004350{
4351 struct jumbo_ops *ops = &tp->jumbo_ops;
4352
4353 switch (tp->mac_version) {
4354 case RTL_GIGA_MAC_VER_11:
4355 ops->disable = r8168b_0_hw_jumbo_disable;
4356 ops->enable = r8168b_0_hw_jumbo_enable;
4357 break;
4358 case RTL_GIGA_MAC_VER_12:
4359 case RTL_GIGA_MAC_VER_17:
4360 ops->disable = r8168b_1_hw_jumbo_disable;
4361 ops->enable = r8168b_1_hw_jumbo_enable;
4362 break;
4363 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4364 case RTL_GIGA_MAC_VER_19:
4365 case RTL_GIGA_MAC_VER_20:
4366 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4367 case RTL_GIGA_MAC_VER_22:
4368 case RTL_GIGA_MAC_VER_23:
4369 case RTL_GIGA_MAC_VER_24:
4370 case RTL_GIGA_MAC_VER_25:
4371 case RTL_GIGA_MAC_VER_26:
4372 ops->disable = r8168c_hw_jumbo_disable;
4373 ops->enable = r8168c_hw_jumbo_enable;
4374 break;
4375 case RTL_GIGA_MAC_VER_27:
4376 case RTL_GIGA_MAC_VER_28:
4377 ops->disable = r8168dp_hw_jumbo_disable;
4378 ops->enable = r8168dp_hw_jumbo_enable;
4379 break;
4380 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4381 case RTL_GIGA_MAC_VER_32:
4382 case RTL_GIGA_MAC_VER_33:
4383 case RTL_GIGA_MAC_VER_34:
4384 ops->disable = r8168e_hw_jumbo_disable;
4385 ops->enable = r8168e_hw_jumbo_enable;
4386 break;
4387
4388 /*
4389 * No action needed for jumbo frames with 8169.
4390 * No jumbo for 810x at all.
4391 */
Hayes Wangc5583862012-07-02 17:23:22 +08004392 case RTL_GIGA_MAC_VER_40:
4393 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004394 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004395 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004396 case RTL_GIGA_MAC_VER_44:
Francois Romieud58d46b2011-05-03 16:38:29 +02004397 default:
4398 ops->disable = NULL;
4399 ops->enable = NULL;
4400 break;
4401 }
4402}
4403
Francois Romieuffc46952012-07-06 14:19:23 +02004404DECLARE_RTL_COND(rtl_chipcmd_cond)
4405{
4406 void __iomem *ioaddr = tp->mmio_addr;
4407
4408 return RTL_R8(ChipCmd) & CmdReset;
4409}
4410
Francois Romieu6f43adc2011-04-29 15:05:51 +02004411static void rtl_hw_reset(struct rtl8169_private *tp)
4412{
4413 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu6f43adc2011-04-29 15:05:51 +02004414
Francois Romieu6f43adc2011-04-29 15:05:51 +02004415 RTL_W8(ChipCmd, CmdReset);
4416
Francois Romieuffc46952012-07-06 14:19:23 +02004417 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004418}
4419
Francois Romieub6ffd972011-06-17 17:00:05 +02004420static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4421{
4422 struct rtl_fw *rtl_fw;
4423 const char *name;
4424 int rc = -ENOMEM;
4425
4426 name = rtl_lookup_firmware_name(tp);
4427 if (!name)
4428 goto out_no_firmware;
4429
4430 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4431 if (!rtl_fw)
4432 goto err_warn;
4433
4434 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4435 if (rc < 0)
4436 goto err_free;
4437
Francois Romieufd112f22011-06-18 00:10:29 +02004438 rc = rtl_check_firmware(tp, rtl_fw);
4439 if (rc < 0)
4440 goto err_release_firmware;
4441
Francois Romieub6ffd972011-06-17 17:00:05 +02004442 tp->rtl_fw = rtl_fw;
4443out:
4444 return;
4445
Francois Romieufd112f22011-06-18 00:10:29 +02004446err_release_firmware:
4447 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004448err_free:
4449 kfree(rtl_fw);
4450err_warn:
4451 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4452 name, rc);
4453out_no_firmware:
4454 tp->rtl_fw = NULL;
4455 goto out;
4456}
4457
François Romieu953a12c2011-04-24 17:38:48 +02004458static void rtl_request_firmware(struct rtl8169_private *tp)
4459{
Francois Romieub6ffd972011-06-17 17:00:05 +02004460 if (IS_ERR(tp->rtl_fw))
4461 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004462}
4463
Hayes Wang92fc43b2011-07-06 15:58:03 +08004464static void rtl_rx_close(struct rtl8169_private *tp)
4465{
4466 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004467
Francois Romieu1687b562011-07-19 17:21:29 +02004468 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004469}
4470
Francois Romieuffc46952012-07-06 14:19:23 +02004471DECLARE_RTL_COND(rtl_npq_cond)
4472{
4473 void __iomem *ioaddr = tp->mmio_addr;
4474
4475 return RTL_R8(TxPoll) & NPQ;
4476}
4477
4478DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4479{
4480 void __iomem *ioaddr = tp->mmio_addr;
4481
4482 return RTL_R32(TxConfig) & TXCFG_EMPTY;
4483}
4484
françois romieue6de30d2011-01-03 15:08:37 +00004485static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486{
françois romieue6de30d2011-01-03 15:08:37 +00004487 void __iomem *ioaddr = tp->mmio_addr;
4488
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004490 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
Hayes Wang92fc43b2011-07-06 15:58:03 +08004492 rtl_rx_close(tp);
4493
Hayes Wang5d2e1952011-02-22 17:26:22 +08004494 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004495 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4496 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02004497 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08004498 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4499 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
Hayes Wang7e18dca2012-03-30 14:33:02 +08004500 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004501 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
Hayes Wangc5583862012-07-02 17:23:22 +08004502 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
4503 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
hayeswang57538c42013-04-01 22:23:40 +00004504 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
hayeswang58152cd2013-04-01 22:23:42 +00004505 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
hayeswang45dd95c2013-07-08 17:09:01 +08004506 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004507 tp->mac_version == RTL_GIGA_MAC_VER_38) {
David S. Miller8decf862011-09-22 03:23:13 -04004508 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02004509 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004510 } else {
4511 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4512 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004513 }
4514
Hayes Wang92fc43b2011-07-06 15:58:03 +08004515 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516}
4517
Francois Romieu7f796d832007-06-11 23:04:41 +02004518static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004519{
4520 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004521
4522 /* Set DMA burst size and Interframe Gap Time */
4523 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4524 (InterFrameGap << TxInterFrameGapShift));
4525}
4526
Francois Romieu07ce4062007-02-23 23:36:39 +01004527static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528{
4529 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
Francois Romieu07ce4062007-02-23 23:36:39 +01004531 tp->hw_start(dev);
4532
Francois Romieuda78dbf2012-01-26 14:18:23 +01004533 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004534}
4535
Francois Romieu7f796d832007-06-11 23:04:41 +02004536static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4537 void __iomem *ioaddr)
4538{
4539 /*
4540 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4541 * register to be written before TxDescAddrLow to work.
4542 * Switching from MMIO to I/O access fixes the issue as well.
4543 */
4544 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004545 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004546 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004547 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004548}
4549
4550static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4551{
4552 u16 cmd;
4553
4554 cmd = RTL_R16(CPlusCmd);
4555 RTL_W16(CPlusCmd, cmd);
4556 return cmd;
4557}
4558
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004559static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004560{
4561 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004562 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004563}
4564
Francois Romieu6dccd162007-02-13 23:38:05 +01004565static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4566{
Francois Romieu37441002011-06-17 22:58:54 +02004567 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004568 u32 mac_version;
4569 u32 clk;
4570 u32 val;
4571 } cfg2_info [] = {
4572 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4573 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4574 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4575 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004576 };
4577 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004578 unsigned int i;
4579 u32 clk;
4580
4581 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004582 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004583 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4584 RTL_W32(0x7c, p->val);
4585 break;
4586 }
4587 }
4588}
4589
Francois Romieue6b763e2012-03-08 09:35:39 +01004590static void rtl_set_rx_mode(struct net_device *dev)
4591{
4592 struct rtl8169_private *tp = netdev_priv(dev);
4593 void __iomem *ioaddr = tp->mmio_addr;
4594 u32 mc_filter[2]; /* Multicast hash filter */
4595 int rx_mode;
4596 u32 tmp = 0;
4597
4598 if (dev->flags & IFF_PROMISC) {
4599 /* Unconditionally log net taps. */
4600 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4601 rx_mode =
4602 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4603 AcceptAllPhys;
4604 mc_filter[1] = mc_filter[0] = 0xffffffff;
4605 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4606 (dev->flags & IFF_ALLMULTI)) {
4607 /* Too many to filter perfectly -- accept all multicasts. */
4608 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4609 mc_filter[1] = mc_filter[0] = 0xffffffff;
4610 } else {
4611 struct netdev_hw_addr *ha;
4612
4613 rx_mode = AcceptBroadcast | AcceptMyPhys;
4614 mc_filter[1] = mc_filter[0] = 0;
4615 netdev_for_each_mc_addr(ha, dev) {
4616 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4617 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4618 rx_mode |= AcceptMulticast;
4619 }
4620 }
4621
4622 if (dev->features & NETIF_F_RXALL)
4623 rx_mode |= (AcceptErr | AcceptRunt);
4624
4625 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4626
4627 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4628 u32 data = mc_filter[0];
4629
4630 mc_filter[0] = swab32(mc_filter[1]);
4631 mc_filter[1] = swab32(data);
4632 }
4633
Nathan Walp04817762012-11-01 12:08:47 +00004634 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4635 mc_filter[1] = mc_filter[0] = 0xffffffff;
4636
Francois Romieue6b763e2012-03-08 09:35:39 +01004637 RTL_W32(MAR0 + 4, mc_filter[1]);
4638 RTL_W32(MAR0 + 0, mc_filter[0]);
4639
4640 RTL_W32(RxConfig, tmp);
4641}
4642
Francois Romieu07ce4062007-02-23 23:36:39 +01004643static void rtl_hw_start_8169(struct net_device *dev)
4644{
4645 struct rtl8169_private *tp = netdev_priv(dev);
4646 void __iomem *ioaddr = tp->mmio_addr;
4647 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004648
Francois Romieu9cb427b2006-11-02 00:10:16 +01004649 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4650 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4651 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4652 }
4653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004655 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4656 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4657 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4658 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004659 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4660
Hayes Wange542a222011-07-06 15:58:04 +08004661 rtl_init_rxcfg(tp);
4662
françois romieuf0298f82011-01-03 15:07:42 +00004663 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004665 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
Francois Romieucecb5fd2011-04-01 10:21:07 +02004667 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4668 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4669 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4670 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004671 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
Francois Romieu7f796d832007-06-11 23:04:41 +02004673 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004674
Francois Romieucecb5fd2011-04-01 10:21:07 +02004675 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4676 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004677 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004679 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 }
4681
Francois Romieubcf0bf92006-07-26 23:14:13 +02004682 RTL_W16(CPlusCmd, tp->cp_cmd);
4683
Francois Romieu6dccd162007-02-13 23:38:05 +01004684 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 /*
4687 * Undocumented corner. Supposedly:
4688 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4689 */
4690 RTL_W16(IntrMitigate, 0x0000);
4691
Francois Romieu7f796d832007-06-11 23:04:41 +02004692 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004693
Francois Romieucecb5fd2011-04-01 10:21:07 +02004694 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4695 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4696 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4697 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004698 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4699 rtl_set_rx_tx_config_registers(tp);
4700 }
4701
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004703
4704 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4705 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
4707 RTL_W32(RxMissed, 0);
4708
Francois Romieu07ce4062007-02-23 23:36:39 +01004709 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
4711 /* no early-rx interrupts */
4712 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004713}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004715static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4716{
4717 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02004718 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004719}
4720
4721static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4722{
Francois Romieu52989f02012-07-06 13:37:00 +02004723 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004724}
4725
4726static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004727{
4728 u32 csi;
4729
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004730 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4731 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00004732}
4733
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004734static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004735{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004736 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00004737}
4738
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004739static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00004740{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004741 rtl_csi_access_enable(tp, 0x27000000);
4742}
4743
Francois Romieuffc46952012-07-06 14:19:23 +02004744DECLARE_RTL_COND(rtl_csiar_cond)
4745{
4746 void __iomem *ioaddr = tp->mmio_addr;
4747
4748 return RTL_R32(CSIAR) & CSIAR_FLAG;
4749}
4750
Francois Romieu52989f02012-07-06 13:37:00 +02004751static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
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(CSIDR, value);
4756 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4757 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4758
Francois Romieuffc46952012-07-06 14:19:23 +02004759 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004760}
4761
Francois Romieu52989f02012-07-06 13:37:00 +02004762static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004763{
Francois Romieu52989f02012-07-06 13:37:00 +02004764 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004765
4766 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
4767 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4768
Francois Romieuffc46952012-07-06 14:19:23 +02004769 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4770 RTL_R32(CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004771}
4772
Francois Romieu52989f02012-07-06 13:37:00 +02004773static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004774{
Francois Romieu52989f02012-07-06 13:37:00 +02004775 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004776
4777 RTL_W32(CSIDR, value);
4778 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4779 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4780 CSIAR_FUNC_NIC);
4781
Francois Romieuffc46952012-07-06 14:19:23 +02004782 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004783}
4784
Francois Romieu52989f02012-07-06 13:37:00 +02004785static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004786{
Francois Romieu52989f02012-07-06 13:37:00 +02004787 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004788
4789 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
4790 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4791
Francois Romieuffc46952012-07-06 14:19:23 +02004792 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4793 RTL_R32(CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004794}
4795
hayeswang45dd95c2013-07-08 17:09:01 +08004796static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
4797{
4798 void __iomem *ioaddr = tp->mmio_addr;
4799
4800 RTL_W32(CSIDR, value);
4801 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4802 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4803 CSIAR_FUNC_NIC2);
4804
4805 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
4806}
4807
4808static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
4809{
4810 void __iomem *ioaddr = tp->mmio_addr;
4811
4812 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
4813 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4814
4815 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4816 RTL_R32(CSIDR) : ~0;
4817}
4818
Bill Pembertonbaf63292012-12-03 09:23:28 -05004819static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004820{
4821 struct csi_ops *ops = &tp->csi_ops;
4822
4823 switch (tp->mac_version) {
4824 case RTL_GIGA_MAC_VER_01:
4825 case RTL_GIGA_MAC_VER_02:
4826 case RTL_GIGA_MAC_VER_03:
4827 case RTL_GIGA_MAC_VER_04:
4828 case RTL_GIGA_MAC_VER_05:
4829 case RTL_GIGA_MAC_VER_06:
4830 case RTL_GIGA_MAC_VER_10:
4831 case RTL_GIGA_MAC_VER_11:
4832 case RTL_GIGA_MAC_VER_12:
4833 case RTL_GIGA_MAC_VER_13:
4834 case RTL_GIGA_MAC_VER_14:
4835 case RTL_GIGA_MAC_VER_15:
4836 case RTL_GIGA_MAC_VER_16:
4837 case RTL_GIGA_MAC_VER_17:
4838 ops->write = NULL;
4839 ops->read = NULL;
4840 break;
4841
Hayes Wang7e18dca2012-03-30 14:33:02 +08004842 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004843 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004844 ops->write = r8402_csi_write;
4845 ops->read = r8402_csi_read;
4846 break;
4847
hayeswang45dd95c2013-07-08 17:09:01 +08004848 case RTL_GIGA_MAC_VER_44:
4849 ops->write = r8411_csi_write;
4850 ops->read = r8411_csi_read;
4851 break;
4852
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004853 default:
4854 ops->write = r8169_csi_write;
4855 ops->read = r8169_csi_read;
4856 break;
4857 }
Francois Romieudacf8152008-08-02 20:44:13 +02004858}
4859
4860struct ephy_info {
4861 unsigned int offset;
4862 u16 mask;
4863 u16 bits;
4864};
4865
Francois Romieufdf6fc02012-07-06 22:40:38 +02004866static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
4867 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004868{
4869 u16 w;
4870
4871 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02004872 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4873 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02004874 e++;
4875 }
4876}
4877
Francois Romieub726e492008-06-28 12:22:59 +02004878static void rtl_disable_clock_request(struct pci_dev *pdev)
4879{
Jiang Liu7d7903b2012-07-24 17:20:16 +08004880 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
4881 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02004882}
4883
françois romieue6de30d2011-01-03 15:08:37 +00004884static void rtl_enable_clock_request(struct pci_dev *pdev)
4885{
Jiang Liu7d7903b2012-07-24 17:20:16 +08004886 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
4887 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00004888}
4889
hayeswangb51ecea2014-07-09 14:52:51 +08004890static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
4891{
4892 void __iomem *ioaddr = tp->mmio_addr;
4893 u8 data;
4894
4895 data = RTL_R8(Config3);
4896
4897 if (enable)
4898 data |= Rdy_to_L23;
4899 else
4900 data &= ~Rdy_to_L23;
4901
4902 RTL_W8(Config3, data);
4903}
4904
Francois Romieub726e492008-06-28 12:22:59 +02004905#define R8168_CPCMD_QUIRK_MASK (\
4906 EnableBist | \
4907 Mac_dbgo_oe | \
4908 Force_half_dup | \
4909 Force_rxflow_en | \
4910 Force_txflow_en | \
4911 Cxpl_dbg_sel | \
4912 ASF | \
4913 PktCntrDisable | \
4914 Mac_dbgo_sel)
4915
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004916static void rtl_hw_start_8168bb(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(Config3, RTL_R8(Config3) & ~Beacon_en);
4922
4923 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
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) |
4927 PCI_EXP_DEVCTL_NOSNOOP_EN);
4928 }
Francois Romieu219a1e92008-06-28 11:58:39 +02004929}
4930
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004931static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004932{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004933 void __iomem *ioaddr = tp->mmio_addr;
4934
4935 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004936
françois romieuf0298f82011-01-03 15:07:42 +00004937 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004938
4939 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004940}
4941
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004942static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004943{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004944 void __iomem *ioaddr = tp->mmio_addr;
4945 struct pci_dev *pdev = tp->pci_dev;
4946
Francois Romieub726e492008-06-28 12:22:59 +02004947 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4948
4949 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4950
françois romieufaf1e782013-02-27 13:01:57 +00004951 if (tp->dev->mtu <= ETH_DATA_LEN)
4952 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004953
4954 rtl_disable_clock_request(pdev);
4955
4956 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004957}
4958
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004959static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004960{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004961 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004962 { 0x01, 0, 0x0001 },
4963 { 0x02, 0x0800, 0x1000 },
4964 { 0x03, 0, 0x0042 },
4965 { 0x06, 0x0080, 0x0000 },
4966 { 0x07, 0, 0x2000 }
4967 };
4968
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004969 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004970
Francois Romieufdf6fc02012-07-06 22:40:38 +02004971 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02004972
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004973 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004974}
4975
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004976static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004977{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004978 void __iomem *ioaddr = tp->mmio_addr;
4979 struct pci_dev *pdev = tp->pci_dev;
4980
4981 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004982
4983 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4984
françois romieufaf1e782013-02-27 13:01:57 +00004985 if (tp->dev->mtu <= ETH_DATA_LEN)
4986 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieuef3386f2008-06-29 12:24:30 +02004987
4988 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4989}
4990
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004991static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004992{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004993 void __iomem *ioaddr = tp->mmio_addr;
4994 struct pci_dev *pdev = tp->pci_dev;
4995
4996 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004997
4998 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4999
5000 /* Magic. */
5001 RTL_W8(DBG_REG, 0x20);
5002
françois romieuf0298f82011-01-03 15:07:42 +00005003 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005004
françois romieufaf1e782013-02-27 13:01:57 +00005005 if (tp->dev->mtu <= ETH_DATA_LEN)
5006 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005007
5008 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5009}
5010
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005011static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005012{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005013 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005014 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005015 { 0x02, 0x0800, 0x1000 },
5016 { 0x03, 0, 0x0002 },
5017 { 0x06, 0x0080, 0x0000 }
5018 };
5019
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005020 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005021
5022 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
5023
Francois Romieufdf6fc02012-07-06 22:40:38 +02005024 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005025
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005026 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005027}
5028
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005029static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005030{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005031 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005032 { 0x01, 0, 0x0001 },
5033 { 0x03, 0x0400, 0x0220 }
5034 };
5035
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005036 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005037
Francois Romieufdf6fc02012-07-06 22:40:38 +02005038 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005039
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005040 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005041}
5042
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005043static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005044{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005045 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005046}
5047
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005048static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005049{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005050 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005051
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005052 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005053}
5054
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005055static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005056{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005057 void __iomem *ioaddr = tp->mmio_addr;
5058 struct pci_dev *pdev = tp->pci_dev;
5059
5060 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005061
5062 rtl_disable_clock_request(pdev);
5063
françois romieuf0298f82011-01-03 15:07:42 +00005064 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005065
françois romieufaf1e782013-02-27 13:01:57 +00005066 if (tp->dev->mtu <= ETH_DATA_LEN)
5067 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu5b538df2008-07-20 16:22:45 +02005068
5069 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5070}
5071
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005072static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005073{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005074 void __iomem *ioaddr = tp->mmio_addr;
5075 struct pci_dev *pdev = tp->pci_dev;
5076
5077 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005078
françois romieufaf1e782013-02-27 13:01:57 +00005079 if (tp->dev->mtu <= ETH_DATA_LEN)
5080 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang4804b3b2011-03-21 01:50:29 +00005081
5082 RTL_W8(MaxTxPacketSize, TxPacketMax);
5083
5084 rtl_disable_clock_request(pdev);
5085}
5086
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005087static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005088{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005089 void __iomem *ioaddr = tp->mmio_addr;
5090 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00005091 static const struct ephy_info e_info_8168d_4[] = {
5092 { 0x0b, ~0, 0x48 },
5093 { 0x19, 0x20, 0x50 },
5094 { 0x0c, ~0, 0x20 }
5095 };
5096 int i;
5097
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005098 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005099
5100 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5101
5102 RTL_W8(MaxTxPacketSize, TxPacketMax);
5103
5104 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
5105 const struct ephy_info *e = e_info_8168d_4 + i;
5106 u16 w;
5107
Francois Romieufdf6fc02012-07-06 22:40:38 +02005108 w = rtl_ephy_read(tp, e->offset);
5109 rtl_ephy_write(tp, 0x03, (w & e->mask) | e->bits);
françois romieue6de30d2011-01-03 15:08:37 +00005110 }
5111
5112 rtl_enable_clock_request(pdev);
5113}
5114
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005115static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005116{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005117 void __iomem *ioaddr = tp->mmio_addr;
5118 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005119 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005120 { 0x00, 0x0200, 0x0100 },
5121 { 0x00, 0x0000, 0x0004 },
5122 { 0x06, 0x0002, 0x0001 },
5123 { 0x06, 0x0000, 0x0030 },
5124 { 0x07, 0x0000, 0x2000 },
5125 { 0x00, 0x0000, 0x0020 },
5126 { 0x03, 0x5800, 0x2000 },
5127 { 0x03, 0x0000, 0x0001 },
5128 { 0x01, 0x0800, 0x1000 },
5129 { 0x07, 0x0000, 0x4000 },
5130 { 0x1e, 0x0000, 0x2000 },
5131 { 0x19, 0xffff, 0xfe6c },
5132 { 0x0a, 0x0000, 0x0040 }
5133 };
5134
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005135 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005136
Francois Romieufdf6fc02012-07-06 22:40:38 +02005137 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005138
françois romieufaf1e782013-02-27 13:01:57 +00005139 if (tp->dev->mtu <= ETH_DATA_LEN)
5140 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang01dc7fe2011-03-21 01:50:28 +00005141
5142 RTL_W8(MaxTxPacketSize, TxPacketMax);
5143
5144 rtl_disable_clock_request(pdev);
5145
5146 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02005147 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
5148 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005149
Francois Romieucecb5fd2011-04-01 10:21:07 +02005150 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005151}
5152
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005153static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005154{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005155 void __iomem *ioaddr = tp->mmio_addr;
5156 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005157 static const struct ephy_info e_info_8168e_2[] = {
5158 { 0x09, 0x0000, 0x0080 },
5159 { 0x19, 0x0000, 0x0224 }
5160 };
5161
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005162 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005163
Francois Romieufdf6fc02012-07-06 22:40:38 +02005164 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005165
françois romieufaf1e782013-02-27 13:01:57 +00005166 if (tp->dev->mtu <= ETH_DATA_LEN)
5167 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Hayes Wang70090422011-07-06 15:58:06 +08005168
Francois Romieufdf6fc02012-07-06 22:40:38 +02005169 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5170 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5171 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5172 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5173 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5174 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
5175 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5176 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005177
Hayes Wang3090bd92011-09-06 16:55:15 +08005178 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005179
Francois Romieu4521e1a92012-11-01 16:46:28 +00005180 rtl_disable_clock_request(pdev);
5181
Hayes Wang70090422011-07-06 15:58:06 +08005182 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5183 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5184
5185 /* Adjust EEE LED frequency */
5186 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5187
5188 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5189 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005190 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08005191}
5192
Hayes Wang5f886e02012-03-30 14:33:03 +08005193static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005194{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005195 void __iomem *ioaddr = tp->mmio_addr;
5196 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08005197
Hayes Wang5f886e02012-03-30 14:33:03 +08005198 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005199
5200 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5201
Francois Romieufdf6fc02012-07-06 22:40:38 +02005202 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5203 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5204 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5205 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5206 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5207 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5208 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5209 rtl_w1w0_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5210 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5211 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005212
5213 RTL_W8(MaxTxPacketSize, EarlySize);
5214
Francois Romieu4521e1a92012-11-01 16:46:28 +00005215 rtl_disable_clock_request(pdev);
5216
Hayes Wangc2218922011-09-06 16:55:18 +08005217 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5218 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08005219 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005220 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5221 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005222}
5223
Hayes Wang5f886e02012-03-30 14:33:03 +08005224static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5225{
5226 void __iomem *ioaddr = tp->mmio_addr;
5227 static const struct ephy_info e_info_8168f_1[] = {
5228 { 0x06, 0x00c0, 0x0020 },
5229 { 0x08, 0x0001, 0x0002 },
5230 { 0x09, 0x0000, 0x0080 },
5231 { 0x19, 0x0000, 0x0224 }
5232 };
5233
5234 rtl_hw_start_8168f(tp);
5235
Francois Romieufdf6fc02012-07-06 22:40:38 +02005236 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005237
Francois Romieufdf6fc02012-07-06 22:40:38 +02005238 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005239
5240 /* Adjust EEE LED frequency */
5241 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5242}
5243
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005244static void rtl_hw_start_8411(struct rtl8169_private *tp)
5245{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005246 static const struct ephy_info e_info_8168f_1[] = {
5247 { 0x06, 0x00c0, 0x0020 },
5248 { 0x0f, 0xffff, 0x5200 },
5249 { 0x1e, 0x0000, 0x4000 },
5250 { 0x19, 0x0000, 0x0224 }
5251 };
5252
5253 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08005254 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005255
Francois Romieufdf6fc02012-07-06 22:40:38 +02005256 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005257
Francois Romieufdf6fc02012-07-06 22:40:38 +02005258 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005259}
5260
Hayes Wangc5583862012-07-02 17:23:22 +08005261static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5262{
5263 void __iomem *ioaddr = tp->mmio_addr;
5264 struct pci_dev *pdev = tp->pci_dev;
5265
hayeswangbeb330a2013-04-01 22:23:39 +00005266 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5267
Hayes Wangc5583862012-07-02 17:23:22 +08005268 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5269 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5270 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5271 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5272
5273 rtl_csi_access_enable_1(tp);
5274
5275 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5276
5277 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5278 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00005279 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005280
5281 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005282 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08005283 RTL_W8(MaxTxPacketSize, EarlySize);
5284
5285 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5286 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5287
5288 /* Adjust EEE LED frequency */
5289 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5290
hayeswangbeb330a2013-04-01 22:23:39 +00005291 rtl_w1w0_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5292 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005293
5294 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08005295}
5296
hayeswang57538c42013-04-01 22:23:40 +00005297static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5298{
5299 void __iomem *ioaddr = tp->mmio_addr;
5300 static const struct ephy_info e_info_8168g_2[] = {
5301 { 0x00, 0x0000, 0x0008 },
5302 { 0x0c, 0x3df0, 0x0200 },
5303 { 0x19, 0xffff, 0xfc00 },
5304 { 0x1e, 0xffff, 0x20eb }
5305 };
5306
5307 rtl_hw_start_8168g_1(tp);
5308
5309 /* disable aspm and clock request before access ephy */
5310 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
5311 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
5312 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5313}
5314
hayeswang45dd95c2013-07-08 17:09:01 +08005315static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5316{
5317 void __iomem *ioaddr = tp->mmio_addr;
5318 static const struct ephy_info e_info_8411_2[] = {
5319 { 0x00, 0x0000, 0x0008 },
5320 { 0x0c, 0x3df0, 0x0200 },
5321 { 0x0f, 0xffff, 0x5200 },
5322 { 0x19, 0x0020, 0x0000 },
5323 { 0x1e, 0x0000, 0x2000 }
5324 };
5325
5326 rtl_hw_start_8168g_1(tp);
5327
5328 /* disable aspm and clock request before access ephy */
5329 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
5330 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
5331 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
5332}
5333
Francois Romieu07ce4062007-02-23 23:36:39 +01005334static void rtl_hw_start_8168(struct net_device *dev)
5335{
Francois Romieu2dd99532007-06-11 23:22:52 +02005336 struct rtl8169_private *tp = netdev_priv(dev);
5337 void __iomem *ioaddr = tp->mmio_addr;
5338
5339 RTL_W8(Cfg9346, Cfg9346_Unlock);
5340
françois romieuf0298f82011-01-03 15:07:42 +00005341 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02005342
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005343 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02005344
Francois Romieu0e485152007-02-20 00:00:26 +01005345 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02005346
5347 RTL_W16(CPlusCmd, tp->cp_cmd);
5348
Francois Romieu0e485152007-02-20 00:00:26 +01005349 RTL_W16(IntrMitigate, 0x5151);
5350
5351 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00005352 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005353 tp->event_slow |= RxFIFOOver | PCSTimeout;
5354 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01005355 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005356
5357 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5358
hayeswang1a964642013-04-01 22:23:41 +00005359 rtl_set_rx_tx_config_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02005360
5361 RTL_R8(IntrMask);
5362
Francois Romieu219a1e92008-06-28 11:58:39 +02005363 switch (tp->mac_version) {
5364 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005365 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005366 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005367
5368 case RTL_GIGA_MAC_VER_12:
5369 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005370 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005371 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005372
5373 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005374 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005375 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005376
5377 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005378 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005379 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005380
5381 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005382 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005383 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005384
Francois Romieu197ff762008-06-28 13:16:02 +02005385 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005386 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005387 break;
Francois Romieu197ff762008-06-28 13:16:02 +02005388
Francois Romieu6fb07052008-06-29 11:54:28 +02005389 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005390 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005391 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02005392
Francois Romieuef3386f2008-06-29 12:24:30 +02005393 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005394 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005395 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02005396
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005397 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005398 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005399 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005400
Francois Romieu5b538df2008-07-20 16:22:45 +02005401 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00005402 case RTL_GIGA_MAC_VER_26:
5403 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005404 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005405 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02005406
françois romieue6de30d2011-01-03 15:08:37 +00005407 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005408 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005409 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005410
hayeswang4804b3b2011-03-21 01:50:29 +00005411 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005412 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005413 break;
5414
hayeswang01dc7fe2011-03-21 01:50:28 +00005415 case RTL_GIGA_MAC_VER_32:
5416 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005417 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005418 break;
5419 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005420 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005421 break;
françois romieue6de30d2011-01-03 15:08:37 +00005422
Hayes Wangc2218922011-09-06 16:55:18 +08005423 case RTL_GIGA_MAC_VER_35:
5424 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005425 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005426 break;
5427
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005428 case RTL_GIGA_MAC_VER_38:
5429 rtl_hw_start_8411(tp);
5430 break;
5431
Hayes Wangc5583862012-07-02 17:23:22 +08005432 case RTL_GIGA_MAC_VER_40:
5433 case RTL_GIGA_MAC_VER_41:
5434 rtl_hw_start_8168g_1(tp);
5435 break;
hayeswang57538c42013-04-01 22:23:40 +00005436 case RTL_GIGA_MAC_VER_42:
5437 rtl_hw_start_8168g_2(tp);
5438 break;
Hayes Wangc5583862012-07-02 17:23:22 +08005439
hayeswang45dd95c2013-07-08 17:09:01 +08005440 case RTL_GIGA_MAC_VER_44:
5441 rtl_hw_start_8411_2(tp);
5442 break;
5443
Francois Romieu219a1e92008-06-28 11:58:39 +02005444 default:
5445 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5446 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00005447 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005448 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005449
hayeswang1a964642013-04-01 22:23:41 +00005450 RTL_W8(Cfg9346, Cfg9346_Lock);
5451
Francois Romieu0e485152007-02-20 00:00:26 +01005452 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5453
hayeswang1a964642013-04-01 22:23:41 +00005454 rtl_set_rx_mode(dev);
Francois Romieub8363902008-06-01 12:31:57 +02005455
Francois Romieu2dd99532007-06-11 23:22:52 +02005456 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005457}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
Francois Romieu2857ffb2008-08-02 21:08:49 +02005459#define R810X_CPCMD_QUIRK_MASK (\
5460 EnableBist | \
5461 Mac_dbgo_oe | \
5462 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00005463 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02005464 Force_txflow_en | \
5465 Cxpl_dbg_sel | \
5466 ASF | \
5467 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005468 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005469
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005470static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005471{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005472 void __iomem *ioaddr = tp->mmio_addr;
5473 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005474 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005475 { 0x01, 0, 0x6e65 },
5476 { 0x02, 0, 0x091f },
5477 { 0x03, 0, 0xc2f9 },
5478 { 0x06, 0, 0xafb5 },
5479 { 0x07, 0, 0x0e00 },
5480 { 0x19, 0, 0xec80 },
5481 { 0x01, 0, 0x2e65 },
5482 { 0x01, 0, 0x6e65 }
5483 };
5484 u8 cfg1;
5485
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005486 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005487
5488 RTL_W8(DBG_REG, FIX_NAK_1);
5489
5490 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5491
5492 RTL_W8(Config1,
5493 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5494 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5495
5496 cfg1 = RTL_R8(Config1);
5497 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5498 RTL_W8(Config1, cfg1 & ~LEDS0);
5499
Francois Romieufdf6fc02012-07-06 22:40:38 +02005500 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02005501}
5502
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005503static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005504{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005505 void __iomem *ioaddr = tp->mmio_addr;
5506 struct pci_dev *pdev = tp->pci_dev;
5507
5508 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005509
5510 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5511
5512 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5513 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005514}
5515
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005516static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005517{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005518 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005519
Francois Romieufdf6fc02012-07-06 22:40:38 +02005520 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005521}
5522
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005523static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005524{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005525 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005526 static const struct ephy_info e_info_8105e_1[] = {
5527 { 0x07, 0, 0x4000 },
5528 { 0x19, 0, 0x0200 },
5529 { 0x19, 0, 0x0020 },
5530 { 0x1e, 0, 0x2000 },
5531 { 0x03, 0, 0x0001 },
5532 { 0x19, 0, 0x0100 },
5533 { 0x19, 0, 0x0004 },
5534 { 0x0a, 0, 0x0020 }
5535 };
5536
Francois Romieucecb5fd2011-04-01 10:21:07 +02005537 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005538 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5539
Francois Romieucecb5fd2011-04-01 10:21:07 +02005540 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005541 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5542
5543 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08005544 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005545
Francois Romieufdf6fc02012-07-06 22:40:38 +02005546 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08005547
5548 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005549}
5550
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005551static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005552{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005553 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005554 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005555}
5556
Hayes Wang7e18dca2012-03-30 14:33:02 +08005557static void rtl_hw_start_8402(struct rtl8169_private *tp)
5558{
5559 void __iomem *ioaddr = tp->mmio_addr;
5560 static const struct ephy_info e_info_8402[] = {
5561 { 0x19, 0xffff, 0xff64 },
5562 { 0x1e, 0, 0x4000 }
5563 };
5564
5565 rtl_csi_access_enable_2(tp);
5566
5567 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5568 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5569
5570 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5571 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5572
Francois Romieufdf6fc02012-07-06 22:40:38 +02005573 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08005574
5575 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5576
Francois Romieufdf6fc02012-07-06 22:40:38 +02005577 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5578 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5579 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5580 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5581 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5582 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5583 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005584
5585 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005586}
5587
Hayes Wang5598bfe2012-07-02 17:23:21 +08005588static void rtl_hw_start_8106(struct rtl8169_private *tp)
5589{
5590 void __iomem *ioaddr = tp->mmio_addr;
5591
5592 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5593 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5594
Francois Romieu4521e1a92012-11-01 16:46:28 +00005595 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005596 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5597 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08005598
5599 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005600}
5601
Francois Romieu07ce4062007-02-23 23:36:39 +01005602static void rtl_hw_start_8101(struct net_device *dev)
5603{
Francois Romieucdf1a602007-06-11 23:29:50 +02005604 struct rtl8169_private *tp = netdev_priv(dev);
5605 void __iomem *ioaddr = tp->mmio_addr;
5606 struct pci_dev *pdev = tp->pci_dev;
5607
Francois Romieuda78dbf2012-01-26 14:18:23 +01005608 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5609 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00005610
Francois Romieucecb5fd2011-04-01 10:21:07 +02005611 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08005612 tp->mac_version == RTL_GIGA_MAC_VER_16)
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06005613 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
5614 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02005615
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005616 RTL_W8(Cfg9346, Cfg9346_Unlock);
5617
hayeswang1a964642013-04-01 22:23:41 +00005618 RTL_W8(MaxTxPacketSize, TxPacketMax);
5619
5620 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
5621
5622 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
5623 RTL_W16(CPlusCmd, tp->cp_cmd);
5624
5625 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5626
5627 rtl_set_rx_tx_config_registers(tp);
5628
Francois Romieu2857ffb2008-08-02 21:08:49 +02005629 switch (tp->mac_version) {
5630 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005631 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005632 break;
5633
5634 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005635 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005636 break;
5637
5638 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005639 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005640 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005641
5642 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005643 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005644 break;
5645 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005646 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005647 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005648
5649 case RTL_GIGA_MAC_VER_37:
5650 rtl_hw_start_8402(tp);
5651 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08005652
5653 case RTL_GIGA_MAC_VER_39:
5654 rtl_hw_start_8106(tp);
5655 break;
hayeswang58152cd2013-04-01 22:23:42 +00005656 case RTL_GIGA_MAC_VER_43:
5657 rtl_hw_start_8168g_2(tp);
5658 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005659 }
5660
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005661 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005662
Francois Romieucdf1a602007-06-11 23:29:50 +02005663 RTL_W16(IntrMitigate, 0x0000);
5664
Francois Romieucdf1a602007-06-11 23:29:50 +02005665 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieucdf1a602007-06-11 23:29:50 +02005666
Francois Romieucdf1a602007-06-11 23:29:50 +02005667 rtl_set_rx_mode(dev);
5668
hayeswang1a964642013-04-01 22:23:41 +00005669 RTL_R8(IntrMask);
5670
Francois Romieucdf1a602007-06-11 23:29:50 +02005671 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672}
5673
5674static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5675{
Francois Romieud58d46b2011-05-03 16:38:29 +02005676 struct rtl8169_private *tp = netdev_priv(dev);
5677
5678 if (new_mtu < ETH_ZLEN ||
5679 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 return -EINVAL;
5681
Francois Romieud58d46b2011-05-03 16:38:29 +02005682 if (new_mtu > ETH_DATA_LEN)
5683 rtl_hw_jumbo_enable(tp);
5684 else
5685 rtl_hw_jumbo_disable(tp);
5686
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005688 netdev_update_features(dev);
5689
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005690 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691}
5692
5693static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5694{
Al Viro95e09182007-12-22 18:55:39 +00005695 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5697}
5698
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005699static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5700 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005702 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005703 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005704
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005705 kfree(*data_buff);
5706 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 rtl8169_make_unusable_by_asic(desc);
5708}
5709
5710static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5711{
5712 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5713
5714 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5715}
5716
5717static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5718 u32 rx_buf_sz)
5719{
5720 desc->addr = cpu_to_le64(mapping);
5721 wmb();
5722 rtl8169_mark_to_asic(desc, rx_buf_sz);
5723}
5724
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005725static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005727 return (void *)ALIGN((long)data, 16);
5728}
5729
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005730static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5731 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005732{
5733 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005735 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005736 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005737 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005739 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5740 if (!data)
5741 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005742
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005743 if (rtl8169_align(data) != data) {
5744 kfree(data);
5745 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5746 if (!data)
5747 return NULL;
5748 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005749
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005750 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005751 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005752 if (unlikely(dma_mapping_error(d, mapping))) {
5753 if (net_ratelimit())
5754 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005755 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757
5758 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005759 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005760
5761err_out:
5762 kfree(data);
5763 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764}
5765
5766static void rtl8169_rx_clear(struct rtl8169_private *tp)
5767{
Francois Romieu07d3f512007-02-21 22:40:46 +01005768 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769
5770 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005771 if (tp->Rx_databuff[i]) {
5772 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 tp->RxDescArray + i);
5774 }
5775 }
5776}
5777
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005778static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005780 desc->opts1 |= cpu_to_le32(RingEnd);
5781}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005782
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005783static int rtl8169_rx_fill(struct rtl8169_private *tp)
5784{
5785 unsigned int i;
5786
5787 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005788 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005789
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005790 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005792
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005793 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005794 if (!data) {
5795 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005796 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005797 }
5798 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005801 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5802 return 0;
5803
5804err_out:
5805 rtl8169_rx_clear(tp);
5806 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807}
5808
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809static int rtl8169_init_ring(struct net_device *dev)
5810{
5811 struct rtl8169_private *tp = netdev_priv(dev);
5812
5813 rtl8169_init_ring_indexes(tp);
5814
5815 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005816 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005818 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819}
5820
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005821static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 struct TxDesc *desc)
5823{
5824 unsigned int len = tx_skb->len;
5825
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005826 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5827
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 desc->opts1 = 0x00;
5829 desc->opts2 = 0x00;
5830 desc->addr = 0x00;
5831 tx_skb->len = 0;
5832}
5833
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005834static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5835 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836{
5837 unsigned int i;
5838
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005839 for (i = 0; i < n; i++) {
5840 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 struct ring_info *tx_skb = tp->tx_skb + entry;
5842 unsigned int len = tx_skb->len;
5843
5844 if (len) {
5845 struct sk_buff *skb = tx_skb->skb;
5846
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005847 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 tp->TxDescArray + entry);
5849 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005850 tp->dev->stats.tx_dropped++;
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07005851 dev_kfree_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 tx_skb->skb = NULL;
5853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 }
5855 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005856}
5857
5858static void rtl8169_tx_clear(struct rtl8169_private *tp)
5859{
5860 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 tp->cur_tx = tp->dirty_tx = 0;
5862}
5863
Francois Romieu4422bcd2012-01-26 11:23:32 +01005864static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865{
David Howellsc4028952006-11-22 14:57:56 +00005866 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005867 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868
Francois Romieuda78dbf2012-01-26 14:18:23 +01005869 napi_disable(&tp->napi);
5870 netif_stop_queue(dev);
5871 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872
françois romieuc7c2c392011-12-04 20:30:52 +00005873 rtl8169_hw_reset(tp);
5874
Francois Romieu56de4142011-03-15 17:29:31 +01005875 for (i = 0; i < NUM_RX_DESC; i++)
5876 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5877
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005879 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880
Francois Romieuda78dbf2012-01-26 14:18:23 +01005881 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005882 rtl_hw_start(dev);
5883 netif_wake_queue(dev);
5884 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885}
5886
5887static void rtl8169_tx_timeout(struct net_device *dev)
5888{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005889 struct rtl8169_private *tp = netdev_priv(dev);
5890
5891 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892}
5893
5894static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005895 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896{
5897 struct skb_shared_info *info = skb_shinfo(skb);
5898 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005899 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005900 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901
5902 entry = tp->cur_tx;
5903 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005904 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 dma_addr_t mapping;
5906 u32 status, len;
5907 void *addr;
5908
5909 entry = (entry + 1) % NUM_TX_DESC;
5910
5911 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005912 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005913 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005914 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005915 if (unlikely(dma_mapping_error(d, mapping))) {
5916 if (net_ratelimit())
5917 netif_err(tp, drv, tp->dev,
5918 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005919 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921
Francois Romieucecb5fd2011-04-01 10:21:07 +02005922 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005923 status = opts[0] | len |
5924 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925
5926 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005927 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 txd->addr = cpu_to_le64(mapping);
5929
5930 tp->tx_skb[entry].len = len;
5931 }
5932
5933 if (cur_frag) {
5934 tp->tx_skb[entry].skb = skb;
5935 txd->opts1 |= cpu_to_le32(LastFrag);
5936 }
5937
5938 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005939
5940err_out:
5941 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5942 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943}
5944
françois romieub423e9a2013-05-18 01:24:46 +00005945static bool rtl_skb_pad(struct sk_buff *skb)
5946{
5947 if (skb_padto(skb, ETH_ZLEN))
5948 return false;
5949 skb_put(skb, ETH_ZLEN - skb->len);
5950 return true;
5951}
5952
5953static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
5954{
5955 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
5956}
5957
hayeswange9746042014-07-11 16:25:58 +08005958static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5959 struct net_device *dev);
5960/* r8169_csum_workaround()
5961 * The hw limites the value the transport offset. When the offset is out of the
5962 * range, calculate the checksum by sw.
5963 */
5964static void r8169_csum_workaround(struct rtl8169_private *tp,
5965 struct sk_buff *skb)
5966{
5967 if (skb_shinfo(skb)->gso_size) {
5968 netdev_features_t features = tp->dev->features;
5969 struct sk_buff *segs, *nskb;
5970
5971 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
5972 segs = skb_gso_segment(skb, features);
5973 if (IS_ERR(segs) || !segs)
5974 goto drop;
5975
5976 do {
5977 nskb = segs;
5978 segs = segs->next;
5979 nskb->next = NULL;
5980 rtl8169_start_xmit(nskb, tp->dev);
5981 } while (segs);
5982
5983 dev_kfree_skb(skb);
5984 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5985 if (skb_checksum_help(skb) < 0)
5986 goto drop;
5987
5988 rtl8169_start_xmit(skb, tp->dev);
5989 } else {
5990 struct net_device_stats *stats;
5991
5992drop:
5993 stats = &tp->dev->stats;
5994 stats->tx_dropped++;
5995 dev_kfree_skb(skb);
5996 }
5997}
5998
5999/* msdn_giant_send_check()
6000 * According to the document of microsoft, the TCP Pseudo Header excludes the
6001 * packet length for IPv6 TCP large packets.
6002 */
6003static int msdn_giant_send_check(struct sk_buff *skb)
6004{
6005 const struct ipv6hdr *ipv6h;
6006 struct tcphdr *th;
6007 int ret;
6008
6009 ret = skb_cow_head(skb, 0);
6010 if (ret)
6011 return ret;
6012
6013 ipv6h = ipv6_hdr(skb);
6014 th = tcp_hdr(skb);
6015
6016 th->check = 0;
6017 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6018
6019 return ret;
6020}
6021
6022static inline __be16 get_protocol(struct sk_buff *skb)
6023{
6024 __be16 protocol;
6025
6026 if (skb->protocol == htons(ETH_P_8021Q))
6027 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
6028 else
6029 protocol = skb->protocol;
6030
6031 return protocol;
6032}
6033
hayeswang5888d3f2014-07-11 16:25:56 +08006034static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6035 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036{
Michał Mirosław350fb322011-04-08 06:35:56 +00006037 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038
Francois Romieu2b7b4312011-04-18 22:53:24 -07006039 if (mss) {
6040 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08006041 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6042 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6043 const struct iphdr *ip = ip_hdr(skb);
6044
6045 if (ip->protocol == IPPROTO_TCP)
6046 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6047 else if (ip->protocol == IPPROTO_UDP)
6048 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6049 else
6050 WARN_ON_ONCE(1);
6051 }
6052
6053 return true;
6054}
6055
6056static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6057 struct sk_buff *skb, u32 *opts)
6058{
hayeswangbdfa4ed2014-07-11 16:25:57 +08006059 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08006060 u32 mss = skb_shinfo(skb)->gso_size;
6061
6062 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08006063 if (transport_offset > GTTCPHO_MAX) {
6064 netif_warn(tp, tx_err, tp->dev,
6065 "Invalid transport offset 0x%x for TSO\n",
6066 transport_offset);
6067 return false;
6068 }
6069
6070 switch (get_protocol(skb)) {
6071 case htons(ETH_P_IP):
6072 opts[0] |= TD1_GTSENV4;
6073 break;
6074
6075 case htons(ETH_P_IPV6):
6076 if (msdn_giant_send_check(skb))
6077 return false;
6078
6079 opts[0] |= TD1_GTSENV6;
6080 break;
6081
6082 default:
6083 WARN_ON_ONCE(1);
6084 break;
6085 }
6086
hayeswangbdfa4ed2014-07-11 16:25:57 +08006087 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08006088 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006089 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08006090 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091
françois romieub423e9a2013-05-18 01:24:46 +00006092 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
6093 return skb_checksum_help(skb) == 0 && rtl_skb_pad(skb);
6094
hayeswange9746042014-07-11 16:25:58 +08006095 if (transport_offset > TCPHO_MAX) {
6096 netif_warn(tp, tx_err, tp->dev,
6097 "Invalid transport offset 0x%x\n",
6098 transport_offset);
6099 return false;
6100 }
6101
6102 switch (get_protocol(skb)) {
6103 case htons(ETH_P_IP):
6104 opts[1] |= TD1_IPv4_CS;
6105 ip_protocol = ip_hdr(skb)->protocol;
6106 break;
6107
6108 case htons(ETH_P_IPV6):
6109 opts[1] |= TD1_IPv6_CS;
6110 ip_protocol = ipv6_hdr(skb)->nexthdr;
6111 break;
6112
6113 default:
6114 ip_protocol = IPPROTO_RAW;
6115 break;
6116 }
6117
6118 if (ip_protocol == IPPROTO_TCP)
6119 opts[1] |= TD1_TCP_CS;
6120 else if (ip_protocol == IPPROTO_UDP)
6121 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006122 else
6123 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08006124
6125 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00006126 } else {
6127 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
6128 return rtl_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 }
hayeswang5888d3f2014-07-11 16:25:56 +08006130
françois romieub423e9a2013-05-18 01:24:46 +00006131 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132}
6133
Stephen Hemminger613573252009-08-31 19:50:58 +00006134static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6135 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136{
6137 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006138 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139 struct TxDesc *txd = tp->TxDescArray + entry;
6140 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006141 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 dma_addr_t mapping;
6143 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006144 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006145 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02006146
Julien Ducourthial477206a2012-05-09 00:00:06 +02006147 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006148 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006149 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 }
6151
6152 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006153 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154
françois romieub423e9a2013-05-18 01:24:46 +00006155 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6156 opts[0] = DescOwn;
6157
hayeswange9746042014-07-11 16:25:58 +08006158 if (!tp->tso_csum(tp, skb, opts)) {
6159 r8169_csum_workaround(tp, skb);
6160 return NETDEV_TX_OK;
6161 }
françois romieub423e9a2013-05-18 01:24:46 +00006162
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006163 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006164 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006165 if (unlikely(dma_mapping_error(d, mapping))) {
6166 if (net_ratelimit())
6167 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006168 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170
6171 tp->tx_skb[entry].len = len;
6172 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173
Francois Romieu2b7b4312011-04-18 22:53:24 -07006174 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006175 if (frags < 0)
6176 goto err_dma_1;
6177 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07006178 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006179 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07006180 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006181 tp->tx_skb[entry].skb = skb;
6182 }
6183
Francois Romieu2b7b4312011-04-18 22:53:24 -07006184 txd->opts2 = cpu_to_le32(opts[1]);
6185
Richard Cochran5047fb52012-03-10 07:29:42 +00006186 skb_tx_timestamp(skb);
6187
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 wmb();
6189
Francois Romieucecb5fd2011-04-01 10:21:07 +02006190 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006191 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 txd->opts1 = cpu_to_le32(status);
6193
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 tp->cur_tx += frags + 1;
6195
David Dillow4c020a92010-03-03 16:33:10 +00006196 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197
Francois Romieucecb5fd2011-04-01 10:21:07 +02006198 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199
Francois Romieuda78dbf2012-01-26 14:18:23 +01006200 mmiowb();
6201
Julien Ducourthial477206a2012-05-09 00:00:06 +02006202 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01006203 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6204 * not miss a ring update when it notices a stopped queue.
6205 */
6206 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01006208 /* Sync with rtl_tx:
6209 * - publish queue status and cur_tx ring index (write barrier)
6210 * - refresh dirty_tx ring index (read barrier).
6211 * May the current thread have a pessimistic view of the ring
6212 * status and forget to wake up queue, a racing rtl_tx thread
6213 * can't.
6214 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006215 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02006216 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 netif_wake_queue(dev);
6218 }
6219
Stephen Hemminger613573252009-08-31 19:50:58 +00006220 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006222err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006223 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006224err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006225 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006226 dev->stats.tx_dropped++;
6227 return NETDEV_TX_OK;
6228
6229err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006231 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00006232 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233}
6234
6235static void rtl8169_pcierr_interrupt(struct net_device *dev)
6236{
6237 struct rtl8169_private *tp = netdev_priv(dev);
6238 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 u16 pci_status, pci_cmd;
6240
6241 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6242 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6243
Joe Perchesbf82c182010-02-09 11:49:50 +00006244 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6245 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246
6247 /*
6248 * The recovery sequence below admits a very elaborated explanation:
6249 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01006250 * - I did not see what else could be done;
6251 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 *
6253 * Feel free to adjust to your needs.
6254 */
Francois Romieua27993f2006-12-18 00:04:19 +01006255 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01006256 pci_cmd &= ~PCI_COMMAND_PARITY;
6257 else
6258 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6259
6260 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261
6262 pci_write_config_word(pdev, PCI_STATUS,
6263 pci_status & (PCI_STATUS_DETECTED_PARITY |
6264 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6265 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6266
6267 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00006268 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00006269 void __iomem *ioaddr = tp->mmio_addr;
6270
Joe Perchesbf82c182010-02-09 11:49:50 +00006271 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 tp->cp_cmd &= ~PCIDAC;
6273 RTL_W16(CPlusCmd, tp->cp_cmd);
6274 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 }
6276
françois romieue6de30d2011-01-03 15:08:37 +00006277 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01006278
Francois Romieu98ddf982012-01-31 10:47:34 +01006279 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280}
6281
Francois Romieuda78dbf2012-01-26 14:18:23 +01006282static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283{
6284 unsigned int dirty_tx, tx_left;
6285
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 dirty_tx = tp->dirty_tx;
6287 smp_rmb();
6288 tx_left = tp->cur_tx - dirty_tx;
6289
6290 while (tx_left > 0) {
6291 unsigned int entry = dirty_tx % NUM_TX_DESC;
6292 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 u32 status;
6294
6295 rmb();
6296 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6297 if (status & DescOwn)
6298 break;
6299
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006300 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
6301 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 if (status & LastFrag) {
Francois Romieu17bcb682012-07-23 22:55:55 +02006303 u64_stats_update_begin(&tp->tx_stats.syncp);
6304 tp->tx_stats.packets++;
6305 tp->tx_stats.bytes += tx_skb->skb->len;
6306 u64_stats_update_end(&tp->tx_stats.syncp);
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006307 dev_kfree_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 tx_skb->skb = NULL;
6309 }
6310 dirty_tx++;
6311 tx_left--;
6312 }
6313
6314 if (tp->dirty_tx != dirty_tx) {
6315 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01006316 /* Sync with rtl8169_start_xmit:
6317 * - publish dirty_tx ring index (write barrier)
6318 * - refresh cur_tx ring index and queue status (read barrier)
6319 * May the current thread miss the stopped queue condition,
6320 * a racing xmit thread can only have a right view of the
6321 * ring status.
6322 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006323 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02006325 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 netif_wake_queue(dev);
6327 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02006328 /*
6329 * 8168 hack: TxPoll requests are lost when the Tx packets are
6330 * too close. Let's kick an extra TxPoll request when a burst
6331 * of start_xmit activity is detected (if it is not detected,
6332 * it is slow enough). -- FR
6333 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006334 if (tp->cur_tx != dirty_tx) {
6335 void __iomem *ioaddr = tp->mmio_addr;
6336
Francois Romieud78ae2d2007-08-26 20:08:19 +02006337 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 }
6340}
6341
Francois Romieu126fa4b2005-05-12 20:09:17 -04006342static inline int rtl8169_fragmented_frame(u32 status)
6343{
6344 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6345}
6346
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006347static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 u32 status = opts1 & RxProtoMask;
6350
6351 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00006352 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 skb->ip_summed = CHECKSUM_UNNECESSARY;
6354 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006355 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356}
6357
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006358static struct sk_buff *rtl8169_try_rx_copy(void *data,
6359 struct rtl8169_private *tp,
6360 int pkt_size,
6361 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02006363 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006364 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006366 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006367 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006368 prefetch(data);
6369 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
6370 if (skb)
6371 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006372 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6373
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006374 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375}
6376
Francois Romieuda78dbf2012-01-26 14:18:23 +01006377static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378{
6379 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006380 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383
Timo Teräs9fba0812013-01-15 21:01:24 +00006384 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006386 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 u32 status;
6388
6389 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04006390 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391
6392 if (status & DescOwn)
6393 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02006394 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006395 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6396 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006397 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02006399 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02006401 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02006402 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006403 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006404 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02006405 }
Ben Greear6bbe0212012-02-10 15:04:33 +00006406 if ((status & (RxRUNT | RxCRC)) &&
6407 !(status & (RxRWT | RxFOVF)) &&
6408 (dev->features & NETIF_F_RXALL))
6409 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006411 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00006412 dma_addr_t addr;
6413 int pkt_size;
6414
6415process_pkt:
6416 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00006417 if (likely(!(dev->features & NETIF_F_RXFCS)))
6418 pkt_size = (status & 0x00003fff) - 4;
6419 else
6420 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421
Francois Romieu126fa4b2005-05-12 20:09:17 -04006422 /*
6423 * The driver does not support incoming fragmented
6424 * frames. They are seen as a symptom of over-mtu
6425 * sized frames.
6426 */
6427 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02006428 dev->stats.rx_dropped++;
6429 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00006430 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006431 }
6432
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006433 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6434 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006435 if (!skb) {
6436 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00006437 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438 }
6439
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006440 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 skb_put(skb, pkt_size);
6442 skb->protocol = eth_type_trans(skb, dev);
6443
Francois Romieu7a8fc772011-03-01 17:18:33 +01006444 rtl8169_rx_vlan_tag(desc, skb);
6445
Francois Romieu56de4142011-03-15 17:29:31 +01006446 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447
Junchang Wang8027aa22012-03-04 23:30:32 +01006448 u64_stats_update_begin(&tp->rx_stats.syncp);
6449 tp->rx_stats.packets++;
6450 tp->rx_stats.bytes += pkt_size;
6451 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 }
françois romieuce11ff52013-01-24 13:30:06 +00006453release_descriptor:
6454 desc->opts2 = 0;
6455 wmb();
6456 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 }
6458
6459 count = cur_rx - tp->cur_rx;
6460 tp->cur_rx = cur_rx;
6461
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462 return count;
6463}
6464
Francois Romieu07d3f512007-02-21 22:40:46 +01006465static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466{
Francois Romieu07d3f512007-02-21 22:40:46 +01006467 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006470 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006472 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006473 if (status && status != 0xffff) {
6474 status &= RTL_EVENT_NAPI | tp->event_slow;
6475 if (status) {
6476 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00006477
Francois Romieuda78dbf2012-01-26 14:18:23 +01006478 rtl_irq_disable(tp);
6479 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 return IRQ_RETVAL(handled);
6483}
6484
Francois Romieuda78dbf2012-01-26 14:18:23 +01006485/*
6486 * Workqueue context.
6487 */
6488static void rtl_slow_event_work(struct rtl8169_private *tp)
6489{
6490 struct net_device *dev = tp->dev;
6491 u16 status;
6492
6493 status = rtl_get_events(tp) & tp->event_slow;
6494 rtl_ack_events(tp, status);
6495
6496 if (unlikely(status & RxFIFOOver)) {
6497 switch (tp->mac_version) {
6498 /* Work around for rx fifo overflow */
6499 case RTL_GIGA_MAC_VER_11:
6500 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01006501 /* XXX - Hack alert. See rtl_task(). */
6502 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006503 default:
6504 break;
6505 }
6506 }
6507
6508 if (unlikely(status & SYSErr))
6509 rtl8169_pcierr_interrupt(dev);
6510
6511 if (status & LinkChg)
6512 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
6513
françois romieu7dbb4912012-06-09 10:53:16 +00006514 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006515}
6516
Francois Romieu4422bcd2012-01-26 11:23:32 +01006517static void rtl_task(struct work_struct *work)
6518{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006519 static const struct {
6520 int bitnr;
6521 void (*action)(struct rtl8169_private *);
6522 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01006523 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006524 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
6525 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
6526 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
6527 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01006528 struct rtl8169_private *tp =
6529 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006530 struct net_device *dev = tp->dev;
6531 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01006532
Francois Romieuda78dbf2012-01-26 14:18:23 +01006533 rtl_lock_work(tp);
6534
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006535 if (!netif_running(dev) ||
6536 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01006537 goto out_unlock;
6538
6539 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6540 bool pending;
6541
Francois Romieuda78dbf2012-01-26 14:18:23 +01006542 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006543 if (pending)
6544 rtl_work[i].action(tp);
6545 }
6546
6547out_unlock:
6548 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01006549}
6550
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006551static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006553 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6554 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006555 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
6556 int work_done= 0;
6557 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558
Francois Romieuda78dbf2012-01-26 14:18:23 +01006559 status = rtl_get_events(tp);
6560 rtl_ack_events(tp, status & ~tp->event_slow);
6561
6562 if (status & RTL_EVENT_NAPI_RX)
6563 work_done = rtl_rx(dev, tp, (u32) budget);
6564
6565 if (status & RTL_EVENT_NAPI_TX)
6566 rtl_tx(dev, tp);
6567
6568 if (status & tp->event_slow) {
6569 enable_mask &= ~tp->event_slow;
6570
6571 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006574 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006575 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00006576
Francois Romieuda78dbf2012-01-26 14:18:23 +01006577 rtl_irq_enable(tp, enable_mask);
6578 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 }
6580
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006581 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583
Francois Romieu523a6092008-09-10 22:28:56 +02006584static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
6585{
6586 struct rtl8169_private *tp = netdev_priv(dev);
6587
6588 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6589 return;
6590
6591 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
6592 RTL_W32(RxMissed, 0);
6593}
6594
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595static void rtl8169_down(struct net_device *dev)
6596{
6597 struct rtl8169_private *tp = netdev_priv(dev);
6598 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599
Francois Romieu4876cc12011-03-11 21:07:11 +01006600 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006602 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006603 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604
Hayes Wang92fc43b2011-07-06 15:58:03 +08006605 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006606 /*
6607 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006608 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6609 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006610 */
Francois Romieu523a6092008-09-10 22:28:56 +02006611 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006614 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 rtl8169_tx_clear(tp);
6617
6618 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006619
6620 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621}
6622
6623static int rtl8169_close(struct net_device *dev)
6624{
6625 struct rtl8169_private *tp = netdev_priv(dev);
6626 struct pci_dev *pdev = tp->pci_dev;
6627
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006628 pm_runtime_get_sync(&pdev->dev);
6629
Francois Romieucecb5fd2011-04-01 10:21:07 +02006630 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08006631 rtl8169_update_counters(dev);
6632
Francois Romieuda78dbf2012-01-26 14:18:23 +01006633 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006634 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006635
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006637 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638
Lekensteyn4ea72442013-07-22 09:53:30 +02006639 cancel_work_sync(&tp->wk.work);
6640
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006641 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006643 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6644 tp->RxPhyAddr);
6645 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6646 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647 tp->TxDescArray = NULL;
6648 tp->RxDescArray = NULL;
6649
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006650 pm_runtime_put_sync(&pdev->dev);
6651
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652 return 0;
6653}
6654
Francois Romieudc1c00c2012-03-08 10:06:18 +01006655#ifdef CONFIG_NET_POLL_CONTROLLER
6656static void rtl8169_netpoll(struct net_device *dev)
6657{
6658 struct rtl8169_private *tp = netdev_priv(dev);
6659
6660 rtl8169_interrupt(tp->pci_dev->irq, dev);
6661}
6662#endif
6663
Francois Romieudf43ac72012-03-08 09:48:40 +01006664static int rtl_open(struct net_device *dev)
6665{
6666 struct rtl8169_private *tp = netdev_priv(dev);
6667 void __iomem *ioaddr = tp->mmio_addr;
6668 struct pci_dev *pdev = tp->pci_dev;
6669 int retval = -ENOMEM;
6670
6671 pm_runtime_get_sync(&pdev->dev);
6672
6673 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02006674 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01006675 * dma_alloc_coherent provides more.
6676 */
6677 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6678 &tp->TxPhyAddr, GFP_KERNEL);
6679 if (!tp->TxDescArray)
6680 goto err_pm_runtime_put;
6681
6682 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6683 &tp->RxPhyAddr, GFP_KERNEL);
6684 if (!tp->RxDescArray)
6685 goto err_free_tx_0;
6686
6687 retval = rtl8169_init_ring(dev);
6688 if (retval < 0)
6689 goto err_free_rx_1;
6690
6691 INIT_WORK(&tp->wk.work, rtl_task);
6692
6693 smp_mb();
6694
6695 rtl_request_firmware(tp);
6696
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006697 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01006698 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
6699 dev->name, dev);
6700 if (retval < 0)
6701 goto err_release_fw_2;
6702
6703 rtl_lock_work(tp);
6704
6705 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6706
6707 napi_enable(&tp->napi);
6708
6709 rtl8169_init_phy(dev, tp);
6710
hayeswang929a0312014-09-16 11:40:47 +08006711 __rtl8169_set_features(dev, dev->features);
Francois Romieudf43ac72012-03-08 09:48:40 +01006712
6713 rtl_pll_power_up(tp);
6714
6715 rtl_hw_start(dev);
6716
6717 netif_start_queue(dev);
6718
6719 rtl_unlock_work(tp);
6720
6721 tp->saved_wolopts = 0;
6722 pm_runtime_put_noidle(&pdev->dev);
6723
6724 rtl8169_check_link_status(dev, tp, ioaddr);
6725out:
6726 return retval;
6727
6728err_release_fw_2:
6729 rtl_release_firmware(tp);
6730 rtl8169_rx_clear(tp);
6731err_free_rx_1:
6732 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6733 tp->RxPhyAddr);
6734 tp->RxDescArray = NULL;
6735err_free_tx_0:
6736 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6737 tp->TxPhyAddr);
6738 tp->TxDescArray = NULL;
6739err_pm_runtime_put:
6740 pm_runtime_put_noidle(&pdev->dev);
6741 goto out;
6742}
6743
Junchang Wang8027aa22012-03-04 23:30:32 +01006744static struct rtnl_link_stats64 *
6745rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746{
6747 struct rtl8169_private *tp = netdev_priv(dev);
6748 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01006749 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750
Francois Romieuda78dbf2012-01-26 14:18:23 +01006751 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02006752 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006753
Junchang Wang8027aa22012-03-04 23:30:32 +01006754 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006755 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006756 stats->rx_packets = tp->rx_stats.packets;
6757 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006758 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006759
6760
6761 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006762 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006763 stats->tx_packets = tp->tx_stats.packets;
6764 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006765 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006766
6767 stats->rx_dropped = dev->stats.rx_dropped;
6768 stats->tx_dropped = dev->stats.tx_dropped;
6769 stats->rx_length_errors = dev->stats.rx_length_errors;
6770 stats->rx_errors = dev->stats.rx_errors;
6771 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6772 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6773 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6774
6775 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776}
6777
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006778static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006779{
françois romieu065c27c2011-01-03 15:08:12 +00006780 struct rtl8169_private *tp = netdev_priv(dev);
6781
Francois Romieu5d06a992006-02-23 00:47:58 +01006782 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006783 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006784
6785 netif_device_detach(dev);
6786 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006787
6788 rtl_lock_work(tp);
6789 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006790 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006791 rtl_unlock_work(tp);
6792
6793 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006794}
Francois Romieu5d06a992006-02-23 00:47:58 +01006795
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006796#ifdef CONFIG_PM
6797
6798static int rtl8169_suspend(struct device *device)
6799{
6800 struct pci_dev *pdev = to_pci_dev(device);
6801 struct net_device *dev = pci_get_drvdata(pdev);
6802
6803 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006804
Francois Romieu5d06a992006-02-23 00:47:58 +01006805 return 0;
6806}
6807
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006808static void __rtl8169_resume(struct net_device *dev)
6809{
françois romieu065c27c2011-01-03 15:08:12 +00006810 struct rtl8169_private *tp = netdev_priv(dev);
6811
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006812 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006813
6814 rtl_pll_power_up(tp);
6815
Artem Savkovcff4c162012-04-03 10:29:11 +00006816 rtl_lock_work(tp);
6817 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006818 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00006819 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006820
Francois Romieu98ddf982012-01-31 10:47:34 +01006821 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006822}
6823
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006824static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006825{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006826 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006827 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006828 struct rtl8169_private *tp = netdev_priv(dev);
6829
6830 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006831
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006832 if (netif_running(dev))
6833 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006834
Francois Romieu5d06a992006-02-23 00:47:58 +01006835 return 0;
6836}
6837
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006838static int rtl8169_runtime_suspend(struct device *device)
6839{
6840 struct pci_dev *pdev = to_pci_dev(device);
6841 struct net_device *dev = pci_get_drvdata(pdev);
6842 struct rtl8169_private *tp = netdev_priv(dev);
6843
6844 if (!tp->TxDescArray)
6845 return 0;
6846
Francois Romieuda78dbf2012-01-26 14:18:23 +01006847 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006848 tp->saved_wolopts = __rtl8169_get_wol(tp);
6849 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006850 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006851
6852 rtl8169_net_suspend(dev);
6853
6854 return 0;
6855}
6856
6857static int rtl8169_runtime_resume(struct device *device)
6858{
6859 struct pci_dev *pdev = to_pci_dev(device);
6860 struct net_device *dev = pci_get_drvdata(pdev);
6861 struct rtl8169_private *tp = netdev_priv(dev);
6862
6863 if (!tp->TxDescArray)
6864 return 0;
6865
Francois Romieuda78dbf2012-01-26 14:18:23 +01006866 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006867 __rtl8169_set_wol(tp, tp->saved_wolopts);
6868 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006869 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006870
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006871 rtl8169_init_phy(dev, tp);
6872
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006873 __rtl8169_resume(dev);
6874
6875 return 0;
6876}
6877
6878static int rtl8169_runtime_idle(struct device *device)
6879{
6880 struct pci_dev *pdev = to_pci_dev(device);
6881 struct net_device *dev = pci_get_drvdata(pdev);
6882 struct rtl8169_private *tp = netdev_priv(dev);
6883
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006884 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006885}
6886
Alexey Dobriyan47145212009-12-14 18:00:08 -08006887static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006888 .suspend = rtl8169_suspend,
6889 .resume = rtl8169_resume,
6890 .freeze = rtl8169_suspend,
6891 .thaw = rtl8169_resume,
6892 .poweroff = rtl8169_suspend,
6893 .restore = rtl8169_resume,
6894 .runtime_suspend = rtl8169_runtime_suspend,
6895 .runtime_resume = rtl8169_runtime_resume,
6896 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006897};
6898
6899#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6900
6901#else /* !CONFIG_PM */
6902
6903#define RTL8169_PM_OPS NULL
6904
6905#endif /* !CONFIG_PM */
6906
David S. Miller1805b2f2011-10-24 18:18:09 -04006907static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6908{
6909 void __iomem *ioaddr = tp->mmio_addr;
6910
6911 /* WoL fails with 8168b when the receiver is disabled. */
6912 switch (tp->mac_version) {
6913 case RTL_GIGA_MAC_VER_11:
6914 case RTL_GIGA_MAC_VER_12:
6915 case RTL_GIGA_MAC_VER_17:
6916 pci_clear_master(tp->pci_dev);
6917
6918 RTL_W8(ChipCmd, CmdRxEnb);
6919 /* PCI commit */
6920 RTL_R8(ChipCmd);
6921 break;
6922 default:
6923 break;
6924 }
6925}
6926
Francois Romieu1765f952008-09-13 17:21:40 +02006927static void rtl_shutdown(struct pci_dev *pdev)
6928{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006929 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006930 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00006931 struct device *d = &pdev->dev;
6932
6933 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02006934
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006935 rtl8169_net_suspend(dev);
6936
Francois Romieucecb5fd2011-04-01 10:21:07 +02006937 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006938 rtl_rar_set(tp, dev->perm_addr);
6939
Hayes Wang92fc43b2011-07-06 15:58:03 +08006940 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006941
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006942 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006943 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6944 rtl_wol_suspend_quirk(tp);
6945 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006946 }
6947
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006948 pci_wake_from_d3(pdev, true);
6949 pci_set_power_state(pdev, PCI_D3hot);
6950 }
françois romieu2a15cd22012-03-06 01:14:12 +00006951
6952 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006953}
Francois Romieu5d06a992006-02-23 00:47:58 +01006954
Bill Pembertonbaf63292012-12-03 09:23:28 -05006955static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01006956{
6957 struct net_device *dev = pci_get_drvdata(pdev);
6958 struct rtl8169_private *tp = netdev_priv(dev);
6959
6960 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6961 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6962 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6963 rtl8168_driver_stop(tp);
6964 }
6965
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006966 netif_napi_del(&tp->napi);
6967
Francois Romieue27566e2012-03-08 09:54:01 +01006968 unregister_netdev(dev);
6969
6970 rtl_release_firmware(tp);
6971
6972 if (pci_dev_run_wake(pdev))
6973 pm_runtime_get_noresume(&pdev->dev);
6974
6975 /* restore original MAC address */
6976 rtl_rar_set(tp, dev->perm_addr);
6977
6978 rtl_disable_msi(pdev, tp);
6979 rtl8169_release_board(pdev, dev, tp->mmio_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01006980}
6981
Francois Romieufa9c3852012-03-08 10:01:50 +01006982static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006983 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006984 .ndo_stop = rtl8169_close,
6985 .ndo_get_stats64 = rtl8169_get_stats64,
6986 .ndo_start_xmit = rtl8169_start_xmit,
6987 .ndo_tx_timeout = rtl8169_tx_timeout,
6988 .ndo_validate_addr = eth_validate_addr,
6989 .ndo_change_mtu = rtl8169_change_mtu,
6990 .ndo_fix_features = rtl8169_fix_features,
6991 .ndo_set_features = rtl8169_set_features,
6992 .ndo_set_mac_address = rtl_set_mac_address,
6993 .ndo_do_ioctl = rtl8169_ioctl,
6994 .ndo_set_rx_mode = rtl_set_rx_mode,
6995#ifdef CONFIG_NET_POLL_CONTROLLER
6996 .ndo_poll_controller = rtl8169_netpoll,
6997#endif
6998
6999};
7000
Francois Romieu31fa8b12012-03-08 10:09:40 +01007001static const struct rtl_cfg_info {
7002 void (*hw_start)(struct net_device *);
7003 unsigned int region;
7004 unsigned int align;
7005 u16 event_slow;
7006 unsigned features;
7007 u8 default_ver;
7008} rtl_cfg_infos [] = {
7009 [RTL_CFG_0] = {
7010 .hw_start = rtl_hw_start_8169,
7011 .region = 1,
7012 .align = 0,
7013 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
7014 .features = RTL_FEATURE_GMII,
7015 .default_ver = RTL_GIGA_MAC_VER_01,
7016 },
7017 [RTL_CFG_1] = {
7018 .hw_start = rtl_hw_start_8168,
7019 .region = 2,
7020 .align = 8,
7021 .event_slow = SYSErr | LinkChg | RxOverflow,
7022 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
7023 .default_ver = RTL_GIGA_MAC_VER_11,
7024 },
7025 [RTL_CFG_2] = {
7026 .hw_start = rtl_hw_start_8101,
7027 .region = 2,
7028 .align = 8,
7029 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
7030 PCSTimeout,
7031 .features = RTL_FEATURE_MSI,
7032 .default_ver = RTL_GIGA_MAC_VER_13,
7033 }
7034};
7035
7036/* Cfg9346_Unlock assumed. */
7037static unsigned rtl_try_msi(struct rtl8169_private *tp,
7038 const struct rtl_cfg_info *cfg)
7039{
7040 void __iomem *ioaddr = tp->mmio_addr;
7041 unsigned msi = 0;
7042 u8 cfg2;
7043
7044 cfg2 = RTL_R8(Config2) & ~MSIEnable;
7045 if (cfg->features & RTL_FEATURE_MSI) {
7046 if (pci_enable_msi(tp->pci_dev)) {
7047 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
7048 } else {
7049 cfg2 |= MSIEnable;
7050 msi = RTL_FEATURE_MSI;
7051 }
7052 }
7053 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
7054 RTL_W8(Config2, cfg2);
7055 return msi;
7056}
7057
Hayes Wangc5583862012-07-02 17:23:22 +08007058DECLARE_RTL_COND(rtl_link_list_ready_cond)
7059{
7060 void __iomem *ioaddr = tp->mmio_addr;
7061
7062 return RTL_R8(MCU) & LINK_LIST_RDY;
7063}
7064
7065DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7066{
7067 void __iomem *ioaddr = tp->mmio_addr;
7068
7069 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
7070}
7071
Bill Pembertonbaf63292012-12-03 09:23:28 -05007072static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007073{
7074 void __iomem *ioaddr = tp->mmio_addr;
7075 u32 data;
7076
7077 tp->ocp_base = OCP_STD_PHY_BASE;
7078
7079 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
7080
7081 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7082 return;
7083
7084 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7085 return;
7086
7087 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
7088 msleep(1);
7089 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
7090
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007091 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007092 data &= ~(1 << 14);
7093 r8168_mac_ocp_write(tp, 0xe8de, data);
7094
7095 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7096 return;
7097
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007098 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007099 data |= (1 << 15);
7100 r8168_mac_ocp_write(tp, 0xe8de, data);
7101
7102 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7103 return;
7104}
7105
Bill Pembertonbaf63292012-12-03 09:23:28 -05007106static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007107{
7108 switch (tp->mac_version) {
7109 case RTL_GIGA_MAC_VER_40:
7110 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00007111 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00007112 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08007113 case RTL_GIGA_MAC_VER_44:
Hayes Wangc5583862012-07-02 17:23:22 +08007114 rtl_hw_init_8168g(tp);
7115 break;
7116
7117 default:
7118 break;
7119 }
7120}
7121
hayeswang929a0312014-09-16 11:40:47 +08007122static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01007123{
7124 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
7125 const unsigned int region = cfg->region;
7126 struct rtl8169_private *tp;
7127 struct mii_if_info *mii;
7128 struct net_device *dev;
7129 void __iomem *ioaddr;
7130 int chipset, i;
7131 int rc;
7132
7133 if (netif_msg_drv(&debug)) {
7134 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
7135 MODULENAME, RTL8169_VERSION);
7136 }
7137
7138 dev = alloc_etherdev(sizeof (*tp));
7139 if (!dev) {
7140 rc = -ENOMEM;
7141 goto out;
7142 }
7143
7144 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01007145 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007146 tp = netdev_priv(dev);
7147 tp->dev = dev;
7148 tp->pci_dev = pdev;
7149 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
7150
7151 mii = &tp->mii;
7152 mii->dev = dev;
7153 mii->mdio_read = rtl_mdio_read;
7154 mii->mdio_write = rtl_mdio_write;
7155 mii->phy_id_mask = 0x1f;
7156 mii->reg_num_mask = 0x1f;
7157 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
7158
7159 /* disable ASPM completely as that cause random device stop working
7160 * problems as well as full system hangs for some PCIe devices users */
7161 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
7162 PCIE_LINK_STATE_CLKPM);
7163
7164 /* enable device (incl. PCI PM wakeup and hotplug setup) */
7165 rc = pci_enable_device(pdev);
7166 if (rc < 0) {
7167 netif_err(tp, probe, dev, "enable failure\n");
7168 goto err_out_free_dev_1;
7169 }
7170
7171 if (pci_set_mwi(pdev) < 0)
7172 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
7173
7174 /* make sure PCI base addr 1 is MMIO */
7175 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
7176 netif_err(tp, probe, dev,
7177 "region #%d not an MMIO resource, aborting\n",
7178 region);
7179 rc = -ENODEV;
7180 goto err_out_mwi_2;
7181 }
7182
7183 /* check for weird/broken PCI region reporting */
7184 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
7185 netif_err(tp, probe, dev,
7186 "Invalid PCI region size(s), aborting\n");
7187 rc = -ENODEV;
7188 goto err_out_mwi_2;
7189 }
7190
7191 rc = pci_request_regions(pdev, MODULENAME);
7192 if (rc < 0) {
7193 netif_err(tp, probe, dev, "could not request regions\n");
7194 goto err_out_mwi_2;
7195 }
7196
hayeswang929a0312014-09-16 11:40:47 +08007197 tp->cp_cmd = 0;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007198
7199 if ((sizeof(dma_addr_t) > 4) &&
7200 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
7201 tp->cp_cmd |= PCIDAC;
7202 dev->features |= NETIF_F_HIGHDMA;
7203 } else {
7204 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7205 if (rc < 0) {
7206 netif_err(tp, probe, dev, "DMA configuration failed\n");
7207 goto err_out_free_res_3;
7208 }
7209 }
7210
7211 /* ioremap MMIO region */
7212 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
7213 if (!ioaddr) {
7214 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
7215 rc = -EIO;
7216 goto err_out_free_res_3;
7217 }
7218 tp->mmio_addr = ioaddr;
7219
7220 if (!pci_is_pcie(pdev))
7221 netif_info(tp, probe, dev, "not PCI Express\n");
7222
7223 /* Identify chip attached to board */
7224 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
7225
7226 rtl_init_rxcfg(tp);
7227
7228 rtl_irq_disable(tp);
7229
Hayes Wangc5583862012-07-02 17:23:22 +08007230 rtl_hw_initialize(tp);
7231
Francois Romieu3b6cf252012-03-08 09:59:04 +01007232 rtl_hw_reset(tp);
7233
7234 rtl_ack_events(tp, 0xffff);
7235
7236 pci_set_master(pdev);
7237
Francois Romieu3b6cf252012-03-08 09:59:04 +01007238 rtl_init_mdio_ops(tp);
7239 rtl_init_pll_power_ops(tp);
7240 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08007241 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007242
7243 rtl8169_print_mac_version(tp);
7244
7245 chipset = tp->mac_version;
7246 tp->txd_version = rtl_chip_infos[chipset].txd_version;
7247
7248 RTL_W8(Cfg9346, Cfg9346_Unlock);
7249 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
Peter Wu8f9d5132013-08-17 11:00:02 +02007250 RTL_W8(Config5, RTL_R8(Config5) & (BWF | MWF | UWF | LanWake | PMEStatus));
Francois Romieu3b6cf252012-03-08 09:59:04 +01007251 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
7252 tp->features |= RTL_FEATURE_WOL;
7253 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
7254 tp->features |= RTL_FEATURE_WOL;
7255 tp->features |= rtl_try_msi(tp, cfg);
7256 RTL_W8(Cfg9346, Cfg9346_Lock);
7257
7258 if (rtl_tbi_enabled(tp)) {
7259 tp->set_speed = rtl8169_set_speed_tbi;
7260 tp->get_settings = rtl8169_gset_tbi;
7261 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
7262 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
7263 tp->link_ok = rtl8169_tbi_link_ok;
7264 tp->do_ioctl = rtl_tbi_ioctl;
7265 } else {
7266 tp->set_speed = rtl8169_set_speed_xmii;
7267 tp->get_settings = rtl8169_gset_xmii;
7268 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
7269 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
7270 tp->link_ok = rtl8169_xmii_link_ok;
7271 tp->do_ioctl = rtl_xmii_ioctl;
7272 }
7273
7274 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05007275 u64_stats_init(&tp->rx_stats.syncp);
7276 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007277
7278 /* Get MAC address */
7279 for (i = 0; i < ETH_ALEN; i++)
7280 dev->dev_addr[i] = RTL_R8(MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007281
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00007282 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007283 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007284
7285 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
7286
7287 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7288 * properly for all devices */
7289 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00007290 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007291
7292 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00007293 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7294 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007295 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7296 NETIF_F_HIGHDMA;
7297
hayeswang929a0312014-09-16 11:40:47 +08007298 tp->cp_cmd |= RxChkSum | RxVlan;
7299
7300 /*
7301 * Pretend we are using VLANs; This bypasses a nasty bug where
7302 * Interrupts stop flowing on high load on 8110SCd controllers.
7303 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01007304 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08007305 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00007306 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007307
hayeswang5888d3f2014-07-11 16:25:56 +08007308 if (tp->txd_version == RTL_TD_0)
7309 tp->tso_csum = rtl8169_tso_csum_v1;
hayeswange9746042014-07-11 16:25:58 +08007310 else if (tp->txd_version == RTL_TD_1) {
hayeswang5888d3f2014-07-11 16:25:56 +08007311 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08007312 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
7313 } else
hayeswang5888d3f2014-07-11 16:25:56 +08007314 WARN_ON_ONCE(1);
7315
Francois Romieu3b6cf252012-03-08 09:59:04 +01007316 dev->hw_features |= NETIF_F_RXALL;
7317 dev->hw_features |= NETIF_F_RXFCS;
7318
7319 tp->hw_start = cfg->hw_start;
7320 tp->event_slow = cfg->event_slow;
7321
7322 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
7323 ~(RxBOVF | RxFOVF) : ~0;
7324
7325 init_timer(&tp->timer);
7326 tp->timer.data = (unsigned long) dev;
7327 tp->timer.function = rtl8169_phy_timer;
7328
7329 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
7330
7331 rc = register_netdev(dev);
7332 if (rc < 0)
7333 goto err_out_msi_4;
7334
7335 pci_set_drvdata(pdev, dev);
7336
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007337 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
7338 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
7339 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007340 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
7341 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
7342 "tx checksumming: %s]\n",
7343 rtl_chip_infos[chipset].jumbo_max,
7344 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
7345 }
7346
7347 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
7348 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
7349 tp->mac_version == RTL_GIGA_MAC_VER_31) {
7350 rtl8168_driver_start(tp);
7351 }
7352
7353 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
7354
7355 if (pci_dev_run_wake(pdev))
7356 pm_runtime_put_noidle(&pdev->dev);
7357
7358 netif_carrier_off(dev);
7359
7360out:
7361 return rc;
7362
7363err_out_msi_4:
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007364 netif_napi_del(&tp->napi);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007365 rtl_disable_msi(pdev, tp);
7366 iounmap(ioaddr);
7367err_out_free_res_3:
7368 pci_release_regions(pdev);
7369err_out_mwi_2:
7370 pci_clear_mwi(pdev);
7371 pci_disable_device(pdev);
7372err_out_free_dev_1:
7373 free_netdev(dev);
7374 goto out;
7375}
7376
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377static struct pci_driver rtl8169_pci_driver = {
7378 .name = MODULENAME,
7379 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01007380 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05007381 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02007382 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007383 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384};
7385
Devendra Naga3eeb7da2012-10-26 09:27:42 +00007386module_pci_driver(rtl8169_pci_driver);