blob: f790f61ea78a2b4f1008da82eca29132ff5bdcc0 [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"
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +080055#define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
56#define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
57#define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
58#define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
françois romieubca03d52011-01-03 15:07:31 +000059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifdef RTL8169_DEBUG
61#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020062 if (!(expr)) { \
63 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070064 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020065 }
Joe Perches06fa7352007-10-18 21:15:00 +020066#define dprintk(fmt, args...) \
67 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#else
69#define assert(expr) do {} while (0)
70#define dprintk(fmt, args...) do {} while (0)
71#endif /* RTL8169_DEBUG */
72
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020073#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070074 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020075
Julien Ducourthial477206a2012-05-09 00:00:06 +020076#define TX_SLOTS_AVAIL(tp) \
77 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
78
79/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
80#define TX_FRAGS_READY_FOR(tp,nr_frags) \
81 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
84 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050085static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Francois Romieu9c14cea2008-07-05 00:21:15 +020087#define MAX_READ_REQUEST_SHIFT 12
Michal Schmidtaee77e42012-09-09 13:55:26 +000088#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
90
91#define R8169_REGS_SIZE 256
92#define R8169_NAPI_WEIGHT 64
93#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000094#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
96#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
97
98#define RTL8169_TX_TIMEOUT (6*HZ)
99#define RTL8169_PHY_TIMEOUT (10*HZ)
100
101/* write/read MMIO register */
102#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
103#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
104#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
105#define RTL_R8(reg) readb (ioaddr + (reg))
106#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +0000107#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200110 RTL_GIGA_MAC_VER_01 = 0,
111 RTL_GIGA_MAC_VER_02,
112 RTL_GIGA_MAC_VER_03,
113 RTL_GIGA_MAC_VER_04,
114 RTL_GIGA_MAC_VER_05,
115 RTL_GIGA_MAC_VER_06,
116 RTL_GIGA_MAC_VER_07,
117 RTL_GIGA_MAC_VER_08,
118 RTL_GIGA_MAC_VER_09,
119 RTL_GIGA_MAC_VER_10,
120 RTL_GIGA_MAC_VER_11,
121 RTL_GIGA_MAC_VER_12,
122 RTL_GIGA_MAC_VER_13,
123 RTL_GIGA_MAC_VER_14,
124 RTL_GIGA_MAC_VER_15,
125 RTL_GIGA_MAC_VER_16,
126 RTL_GIGA_MAC_VER_17,
127 RTL_GIGA_MAC_VER_18,
128 RTL_GIGA_MAC_VER_19,
129 RTL_GIGA_MAC_VER_20,
130 RTL_GIGA_MAC_VER_21,
131 RTL_GIGA_MAC_VER_22,
132 RTL_GIGA_MAC_VER_23,
133 RTL_GIGA_MAC_VER_24,
134 RTL_GIGA_MAC_VER_25,
135 RTL_GIGA_MAC_VER_26,
136 RTL_GIGA_MAC_VER_27,
137 RTL_GIGA_MAC_VER_28,
138 RTL_GIGA_MAC_VER_29,
139 RTL_GIGA_MAC_VER_30,
140 RTL_GIGA_MAC_VER_31,
141 RTL_GIGA_MAC_VER_32,
142 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800143 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800144 RTL_GIGA_MAC_VER_35,
145 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800146 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800147 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800148 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800149 RTL_GIGA_MAC_VER_40,
150 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000151 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000152 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800153 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800154 RTL_GIGA_MAC_VER_45,
155 RTL_GIGA_MAC_VER_46,
156 RTL_GIGA_MAC_VER_47,
157 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800158 RTL_GIGA_MAC_VER_49,
159 RTL_GIGA_MAC_VER_50,
160 RTL_GIGA_MAC_VER_51,
Francois Romieu85bffe62011-04-27 08:22:39 +0200161 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
Francois Romieu2b7b4312011-04-18 22:53:24 -0700164enum rtl_tx_desc_version {
165 RTL_TD_0 = 0,
166 RTL_TD_1 = 1,
167};
168
Francois Romieud58d46b2011-05-03 16:38:29 +0200169#define JUMBO_1K ETH_DATA_LEN
170#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
171#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
172#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
173#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
174
175#define _R(NAME,TD,FW,SZ,B) { \
176 .name = NAME, \
177 .txd_version = TD, \
178 .fw_name = FW, \
179 .jumbo_max = SZ, \
180 .jumbo_tx_csum = B \
181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800183static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700185 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200186 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200187 u16 jumbo_max;
188 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200189} rtl_chip_infos[] = {
190 /* PCI devices. */
191 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200192 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200193 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200194 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200195 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200196 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200197 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200198 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200199 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200200 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200201 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200202 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200203 /* PCI-E devices. */
204 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200205 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200206 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200207 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200208 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200209 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200210 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200211 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200212 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200213 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200214 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200215 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200216 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200217 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200218 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200219 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200220 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200221 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200222 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200223 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200224 [RTL_GIGA_MAC_VER_17] =
hayeswangf75761b2014-03-11 15:11:59 +0800225 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200226 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200227 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200228 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200229 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200230 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200231 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200232 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200233 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200234 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200235 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200236 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200237 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200238 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200239 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200240 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200241 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
242 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200243 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200244 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
245 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200246 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200247 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200248 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200249 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200250 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200251 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
252 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200253 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200254 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
255 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200256 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200257 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200258 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200259 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
260 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200261 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200262 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
263 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800264 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200265 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
266 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800267 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200268 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
269 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800270 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200271 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
272 JUMBO_9K, false),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800273 [RTL_GIGA_MAC_VER_37] =
274 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
275 JUMBO_1K, true),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800276 [RTL_GIGA_MAC_VER_38] =
277 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
278 JUMBO_9K, false),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800279 [RTL_GIGA_MAC_VER_39] =
280 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
281 JUMBO_1K, true),
Hayes Wangc5583862012-07-02 17:23:22 +0800282 [RTL_GIGA_MAC_VER_40] =
hayeswangbeb330a2013-04-01 22:23:39 +0000283 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
Hayes Wangc5583862012-07-02 17:23:22 +0800284 JUMBO_9K, false),
285 [RTL_GIGA_MAC_VER_41] =
286 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
hayeswang57538c42013-04-01 22:23:40 +0000287 [RTL_GIGA_MAC_VER_42] =
288 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3,
289 JUMBO_9K, false),
hayeswang58152cd2013-04-01 22:23:42 +0000290 [RTL_GIGA_MAC_VER_43] =
291 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2,
292 JUMBO_1K, true),
hayeswang45dd95c2013-07-08 17:09:01 +0800293 [RTL_GIGA_MAC_VER_44] =
294 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2,
295 JUMBO_9K, false),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800296 [RTL_GIGA_MAC_VER_45] =
297 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1,
298 JUMBO_9K, false),
299 [RTL_GIGA_MAC_VER_46] =
300 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2,
301 JUMBO_9K, false),
302 [RTL_GIGA_MAC_VER_47] =
303 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1,
304 JUMBO_1K, false),
305 [RTL_GIGA_MAC_VER_48] =
306 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2,
307 JUMBO_1K, false),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800308 [RTL_GIGA_MAC_VER_49] =
309 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
310 JUMBO_9K, false),
311 [RTL_GIGA_MAC_VER_50] =
312 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
313 JUMBO_9K, false),
314 [RTL_GIGA_MAC_VER_51] =
315 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
316 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317};
318#undef _R
319
Francois Romieubcf0bf92006-07-26 23:14:13 +0200320enum cfg_version {
321 RTL_CFG_0 = 0x00,
322 RTL_CFG_1,
323 RTL_CFG_2
324};
325
Benoit Taine9baa3c32014-08-08 15:56:03 +0200326static const struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200327 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200328 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200329 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100330 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200331 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200332 { PCI_VENDOR_ID_DLINK, 0x4300,
333 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200334 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000335 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200336 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200337 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
338 { PCI_VENDOR_ID_LINKSYS, 0x1032,
339 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100340 { 0x0001, 0x8168,
341 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 {0,},
343};
344
345MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
346
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000347static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700348static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200349static struct {
350 u32 msg_enable;
351} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Francois Romieu07d3f512007-02-21 22:40:46 +0100353enum rtl_registers {
354 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100355 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100356 MAR0 = 8, /* Multicast filter. */
357 CounterAddrLow = 0x10,
358 CounterAddrHigh = 0x14,
359 TxDescStartAddrLow = 0x20,
360 TxDescStartAddrHigh = 0x24,
361 TxHDescStartAddrLow = 0x28,
362 TxHDescStartAddrHigh = 0x2c,
363 FLASH = 0x30,
364 ERSR = 0x36,
365 ChipCmd = 0x37,
366 TxPoll = 0x38,
367 IntrMask = 0x3c,
368 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700369
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800370 TxConfig = 0x40,
371#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
372#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
373
374 RxConfig = 0x44,
375#define RX128_INT_EN (1 << 15) /* 8111c and later */
376#define RX_MULTI_EN (1 << 14) /* 8111c only */
377#define RXCFG_FIFO_SHIFT 13
378 /* No threshold before first PCI xfer */
379#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000380#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800381#define RXCFG_DMA_SHIFT 8
382 /* Unlimited maximum PCI burst. */
383#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700384
Francois Romieu07d3f512007-02-21 22:40:46 +0100385 RxMissed = 0x4c,
386 Cfg9346 = 0x50,
387 Config0 = 0x51,
388 Config1 = 0x52,
389 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200390#define PME_SIGNAL (1 << 5) /* 8168c and later */
391
Francois Romieu07d3f512007-02-21 22:40:46 +0100392 Config3 = 0x54,
393 Config4 = 0x55,
394 Config5 = 0x56,
395 MultiIntr = 0x5c,
396 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100397 PHYstatus = 0x6c,
398 RxMaxSize = 0xda,
399 CPlusCmd = 0xe0,
400 IntrMitigate = 0xe2,
401 RxDescAddrLow = 0xe4,
402 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000403 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
404
405#define NoEarlyTx 0x3f /* Max value : no early transmit. */
406
407 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
408
409#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800410#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000411
Francois Romieu07d3f512007-02-21 22:40:46 +0100412 FuncEvent = 0xf0,
413 FuncEventMask = 0xf4,
414 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800415 IBCR0 = 0xf8,
416 IBCR2 = 0xf9,
417 IBIMR0 = 0xfa,
418 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100419 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420};
421
Francois Romieuf162a5d2008-06-01 22:37:49 +0200422enum rtl8110_registers {
423 TBICSR = 0x64,
424 TBI_ANAR = 0x68,
425 TBI_LPAR = 0x6a,
426};
427
428enum rtl8168_8101_registers {
429 CSIDR = 0x64,
430 CSIAR = 0x68,
431#define CSIAR_FLAG 0x80000000
432#define CSIAR_WRITE_CMD 0x80000000
433#define CSIAR_BYTE_ENABLE 0x0f
434#define CSIAR_BYTE_ENABLE_SHIFT 12
435#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800436#define CSIAR_FUNC_CARD 0x00000000
437#define CSIAR_FUNC_SDIO 0x00010000
438#define CSIAR_FUNC_NIC 0x00020000
hayeswang45dd95c2013-07-08 17:09:01 +0800439#define CSIAR_FUNC_NIC2 0x00010000
françois romieu065c27c2011-01-03 15:08:12 +0000440 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200441 EPHYAR = 0x80,
442#define EPHYAR_FLAG 0x80000000
443#define EPHYAR_WRITE_CMD 0x80000000
444#define EPHYAR_REG_MASK 0x1f
445#define EPHYAR_REG_SHIFT 16
446#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800447 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800448#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800449#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200450 DBG_REG = 0xd1,
451#define FIX_NAK_1 (1 << 4)
452#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800453 TWSI = 0xd2,
454 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800455#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800456#define TX_EMPTY (1 << 5)
457#define RX_EMPTY (1 << 4)
458#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800459#define EN_NDP (1 << 3)
460#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800461#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000462 EFUSEAR = 0xdc,
463#define EFUSEAR_FLAG 0x80000000
464#define EFUSEAR_WRITE_CMD 0x80000000
465#define EFUSEAR_READ_CMD 0x00000000
466#define EFUSEAR_REG_MASK 0x03ff
467#define EFUSEAR_REG_SHIFT 8
468#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800469 MISC_1 = 0xf2,
470#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200471};
472
françois romieuc0e45c12011-01-03 15:08:04 +0000473enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800474 LED_FREQ = 0x1a,
475 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000476 ERIDR = 0x70,
477 ERIAR = 0x74,
478#define ERIAR_FLAG 0x80000000
479#define ERIAR_WRITE_CMD 0x80000000
480#define ERIAR_READ_CMD 0x00000000
481#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000482#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800483#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
484#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
485#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800486#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800487#define ERIAR_MASK_SHIFT 12
488#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
489#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800490#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800491#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800492#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000493 EPHY_RXER_NUM = 0x7c,
494 OCPDR = 0xb0, /* OCP GPHY access */
495#define OCPDR_WRITE_CMD 0x80000000
496#define OCPDR_READ_CMD 0x00000000
497#define OCPDR_REG_MASK 0x7f
498#define OCPDR_GPHY_REG_SHIFT 16
499#define OCPDR_DATA_MASK 0xffff
500 OCPAR = 0xb4,
501#define OCPAR_FLAG 0x80000000
502#define OCPAR_GPHY_WRITE_CMD 0x8000f060
503#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800504 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000505 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
506 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200507#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800508#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800509#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800510#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800511#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000512};
513
Francois Romieu07d3f512007-02-21 22:40:46 +0100514enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100516 SYSErr = 0x8000,
517 PCSTimeout = 0x4000,
518 SWInt = 0x0100,
519 TxDescUnavail = 0x0080,
520 RxFIFOOver = 0x0040,
521 LinkChg = 0x0020,
522 RxOverflow = 0x0010,
523 TxErr = 0x0008,
524 TxOK = 0x0004,
525 RxErr = 0x0002,
526 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400529 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200530 RxFOVF = (1 << 23),
531 RxRWT = (1 << 22),
532 RxRES = (1 << 21),
533 RxRUNT = (1 << 20),
534 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800537 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100538 CmdReset = 0x10,
539 CmdRxEnb = 0x08,
540 CmdTxEnb = 0x04,
541 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Francois Romieu275391a2007-02-23 23:50:28 +0100543 /* TXPoll register p.5 */
544 HPQ = 0x80, /* Poll cmd on the high prio queue */
545 NPQ = 0x40, /* Poll cmd on the low prio queue */
546 FSWInt = 0x01, /* Forced software interrupt */
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100549 Cfg9346_Lock = 0x00,
550 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100553 AcceptErr = 0x20,
554 AcceptRunt = 0x10,
555 AcceptBroadcast = 0x08,
556 AcceptMulticast = 0x04,
557 AcceptMyPhys = 0x02,
558 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200559#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* TxConfigBits */
562 TxInterFrameGapShift = 24,
563 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
564
Francois Romieu5d06a992006-02-23 00:47:58 +0100565 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200566 LEDS1 = (1 << 7),
567 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200568 Speed_down = (1 << 4),
569 MEMMAP = (1 << 3),
570 IOMAP = (1 << 2),
571 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100572 PMEnable = (1 << 0), /* Power Management Enable */
573
Francois Romieu6dccd162007-02-13 23:38:05 +0100574 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000575 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000576 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100577 PCI_Clock_66MHz = 0x01,
578 PCI_Clock_33MHz = 0x00,
579
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100580 /* Config3 register p.25 */
581 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
582 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200583 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800584 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200585 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100586
Francois Romieud58d46b2011-05-03 16:38:29 +0200587 /* Config4 register */
588 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
589
Francois Romieu5d06a992006-02-23 00:47:58 +0100590 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100591 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
592 MWF = (1 << 5), /* Accept Multicast wakeup frame */
593 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200594 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100595 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100596 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000597 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /* TBICSR p.28 */
600 TBIReset = 0x80000000,
601 TBILoopback = 0x40000000,
602 TBINwEnable = 0x20000000,
603 TBINwRestart = 0x10000000,
604 TBILinkOk = 0x02000000,
605 TBINwComplete = 0x01000000,
606
607 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200608 EnableBist = (1 << 15), // 8168 8101
609 Mac_dbgo_oe = (1 << 14), // 8168 8101
610 Normal_mode = (1 << 13), // unused
611 Force_half_dup = (1 << 12), // 8168 8101
612 Force_rxflow_en = (1 << 11), // 8168 8101
613 Force_txflow_en = (1 << 10), // 8168 8101
614 Cxpl_dbg_sel = (1 << 9), // 8168 8101
615 ASF = (1 << 8), // 8168 8101
616 PktCntrDisable = (1 << 7), // 8168 8101
617 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 RxVlan = (1 << 6),
619 RxChkSum = (1 << 5),
620 PCIDAC = (1 << 4),
621 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100622 INTT_0 = 0x0000, // 8168
623 INTT_1 = 0x0001, // 8168
624 INTT_2 = 0x0002, // 8168
625 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100628 TBI_Enable = 0x80,
629 TxFlowCtrl = 0x40,
630 RxFlowCtrl = 0x20,
631 _1000bpsF = 0x10,
632 _100bps = 0x08,
633 _10bps = 0x04,
634 LinkStatus = 0x02,
635 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100638 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200639
640 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100641 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800642
643 /* magic enable v2 */
644 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645};
646
Francois Romieu2b7b4312011-04-18 22:53:24 -0700647enum rtl_desc_bit {
648 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
650 RingEnd = (1 << 30), /* End of descriptor ring */
651 FirstFrag = (1 << 29), /* First segment of a packet */
652 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700653};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Francois Romieu2b7b4312011-04-18 22:53:24 -0700655/* Generic case. */
656enum rtl_tx_desc_bit {
657 /* First doubleword. */
658 TD_LSO = (1 << 27), /* Large Send Offload */
659#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Francois Romieu2b7b4312011-04-18 22:53:24 -0700661 /* Second doubleword. */
662 TxVlanTag = (1 << 17), /* Add VLAN tag */
663};
664
665/* 8169, 8168b and 810x except 8102e. */
666enum rtl_tx_desc_bit_0 {
667 /* First doubleword. */
668#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
669 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
670 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
671 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
672};
673
674/* 8102e, 8168c and beyond. */
675enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800676 /* First doubleword. */
677 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800678 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800679#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800680#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800681
Francois Romieu2b7b4312011-04-18 22:53:24 -0700682 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800683#define TCPHO_SHIFT 18
684#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700685#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800686 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
687 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700688 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
689 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
690};
691
Francois Romieu2b7b4312011-04-18 22:53:24 -0700692enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* Rx private */
694 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
695 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
696
697#define RxProtoUDP (PID1)
698#define RxProtoTCP (PID0)
699#define RxProtoIP (PID1 | PID0)
700#define RxProtoMask RxProtoIP
701
702 IPFail = (1 << 16), /* IP checksum failed */
703 UDPFail = (1 << 15), /* UDP/IP checksum failed */
704 TCPFail = (1 << 14), /* TCP/IP checksum failed */
705 RxVlanTag = (1 << 16), /* VLAN tag available */
706};
707
708#define RsvdMask 0x3fffc000
709
710struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200711 __le32 opts1;
712 __le32 opts2;
713 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714};
715
716struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200717 __le32 opts1;
718 __le32 opts2;
719 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720};
721
722struct ring_info {
723 struct sk_buff *skb;
724 u32 len;
725 u8 __pad[sizeof(void *) - sizeof(u32)];
726};
727
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200728enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200729 RTL_FEATURE_WOL = (1 << 0),
730 RTL_FEATURE_MSI = (1 << 1),
731 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200732};
733
Ivan Vecera355423d2009-02-06 21:49:57 -0800734struct rtl8169_counters {
735 __le64 tx_packets;
736 __le64 rx_packets;
737 __le64 tx_errors;
738 __le32 rx_errors;
739 __le16 rx_missed;
740 __le16 align_errors;
741 __le32 tx_one_collision;
742 __le32 tx_multi_collision;
743 __le64 rx_unicast;
744 __le64 rx_broadcast;
745 __le32 rx_multicast;
746 __le16 tx_aborted;
747 __le16 tx_underun;
748};
749
Francois Romieuda78dbf2012-01-26 14:18:23 +0100750enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100751 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100752 RTL_FLAG_TASK_SLOW_PENDING,
753 RTL_FLAG_TASK_RESET_PENDING,
754 RTL_FLAG_TASK_PHY_PENDING,
755 RTL_FLAG_MAX
756};
757
Junchang Wang8027aa22012-03-04 23:30:32 +0100758struct rtl8169_stats {
759 u64 packets;
760 u64 bytes;
761 struct u64_stats_sync syncp;
762};
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764struct rtl8169_private {
765 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200766 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000767 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700768 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200769 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700770 u16 txd_version;
771 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
773 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100775 struct rtl8169_stats rx_stats;
776 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
778 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
779 dma_addr_t TxPhyAddr;
780 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000781 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 struct timer_list timer;
784 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100785
786 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000787
788 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200789 void (*write)(struct rtl8169_private *, int, int);
790 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000791 } mdio_ops;
792
françois romieu065c27c2011-01-03 15:08:12 +0000793 struct pll_power_ops {
794 void (*down)(struct rtl8169_private *);
795 void (*up)(struct rtl8169_private *);
796 } pll_power_ops;
797
Francois Romieud58d46b2011-05-03 16:38:29 +0200798 struct jumbo_ops {
799 void (*enable)(struct rtl8169_private *);
800 void (*disable)(struct rtl8169_private *);
801 } jumbo_ops;
802
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800803 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200804 void (*write)(struct rtl8169_private *, int, int);
805 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800806 } csi_ops;
807
Oliver Neukum54405cd2011-01-06 21:55:13 +0100808 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200809 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000810 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100811 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000812 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800814 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800815 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100816
817 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100818 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
819 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100820 struct work_struct work;
821 } wk;
822
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200823 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200824
825 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800826 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000827 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400828 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000829
Francois Romieub6ffd972011-06-17 17:00:05 +0200830 struct rtl_fw {
831 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200832
833#define RTL_VER_SIZE 32
834
835 char version[RTL_VER_SIZE];
836
837 struct rtl_fw_phy_action {
838 __le32 *code;
839 size_t size;
840 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200841 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300842#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800843
844 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845};
846
Ralf Baechle979b6c12005-06-13 14:30:40 -0700847MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700850MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200851module_param_named(debug, debug.msg_enable, int, 0);
852MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853MODULE_LICENSE("GPL");
854MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000855MODULE_FIRMWARE(FIRMWARE_8168D_1);
856MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000857MODULE_FIRMWARE(FIRMWARE_8168E_1);
858MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400859MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800860MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800861MODULE_FIRMWARE(FIRMWARE_8168F_1);
862MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800863MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800864MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800865MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800866MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000867MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000868MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000869MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800870MODULE_FIRMWARE(FIRMWARE_8168H_1);
871MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200872MODULE_FIRMWARE(FIRMWARE_8107E_1);
873MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Francois Romieuda78dbf2012-01-26 14:18:23 +0100875static void rtl_lock_work(struct rtl8169_private *tp)
876{
877 mutex_lock(&tp->wk.mutex);
878}
879
880static void rtl_unlock_work(struct rtl8169_private *tp)
881{
882 mutex_unlock(&tp->wk.mutex);
883}
884
Francois Romieud58d46b2011-05-03 16:38:29 +0200885static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
886{
Jiang Liu7d7903b2012-07-24 17:20:16 +0800887 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
888 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200889}
890
Francois Romieuffc46952012-07-06 14:19:23 +0200891struct rtl_cond {
892 bool (*check)(struct rtl8169_private *);
893 const char *msg;
894};
895
896static void rtl_udelay(unsigned int d)
897{
898 udelay(d);
899}
900
901static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
902 void (*delay)(unsigned int), unsigned int d, int n,
903 bool high)
904{
905 int i;
906
907 for (i = 0; i < n; i++) {
908 delay(d);
909 if (c->check(tp) == high)
910 return true;
911 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200912 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
913 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200914 return false;
915}
916
917static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
918 const struct rtl_cond *c,
919 unsigned int d, int n)
920{
921 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
922}
923
924static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
925 const struct rtl_cond *c,
926 unsigned int d, int n)
927{
928 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
929}
930
931static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
932 const struct rtl_cond *c,
933 unsigned int d, int n)
934{
935 return rtl_loop_wait(tp, c, msleep, d, n, true);
936}
937
938static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
939 const struct rtl_cond *c,
940 unsigned int d, int n)
941{
942 return rtl_loop_wait(tp, c, msleep, d, n, false);
943}
944
945#define DECLARE_RTL_COND(name) \
946static bool name ## _check(struct rtl8169_private *); \
947 \
948static const struct rtl_cond name = { \
949 .check = name ## _check, \
950 .msg = #name \
951}; \
952 \
953static bool name ## _check(struct rtl8169_private *tp)
954
Hayes Wangc5583862012-07-02 17:23:22 +0800955static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
956{
957 if (reg & 0xffff0001) {
958 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
959 return true;
960 }
961 return false;
962}
963
964DECLARE_RTL_COND(rtl_ocp_gphy_cond)
965{
966 void __iomem *ioaddr = tp->mmio_addr;
967
968 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
969}
970
971static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
972{
973 void __iomem *ioaddr = tp->mmio_addr;
974
975 if (rtl_ocp_reg_failure(tp, reg))
976 return;
977
978 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
979
980 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
981}
982
983static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
984{
985 void __iomem *ioaddr = tp->mmio_addr;
986
987 if (rtl_ocp_reg_failure(tp, reg))
988 return 0;
989
990 RTL_W32(GPHY_OCP, reg << 15);
991
992 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
993 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
994}
995
Hayes Wangc5583862012-07-02 17:23:22 +0800996static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
997{
998 void __iomem *ioaddr = tp->mmio_addr;
999
1000 if (rtl_ocp_reg_failure(tp, reg))
1001 return;
1002
1003 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +08001004}
1005
1006static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1007{
1008 void __iomem *ioaddr = tp->mmio_addr;
1009
1010 if (rtl_ocp_reg_failure(tp, reg))
1011 return 0;
1012
1013 RTL_W32(OCPDR, reg << 15);
1014
Hayes Wang3a83ad12012-07-11 20:31:56 +08001015 return RTL_R32(OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +08001016}
1017
1018#define OCP_STD_PHY_BASE 0xa400
1019
1020static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1021{
1022 if (reg == 0x1f) {
1023 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1024 return;
1025 }
1026
1027 if (tp->ocp_base != OCP_STD_PHY_BASE)
1028 reg -= 0x10;
1029
1030 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1031}
1032
1033static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1034{
1035 if (tp->ocp_base != OCP_STD_PHY_BASE)
1036 reg -= 0x10;
1037
1038 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1039}
1040
hayeswangeee37862013-04-01 22:23:38 +00001041static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1042{
1043 if (reg == 0x1f) {
1044 tp->ocp_base = value << 4;
1045 return;
1046 }
1047
1048 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1049}
1050
1051static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1052{
1053 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1054}
1055
Francois Romieuffc46952012-07-06 14:19:23 +02001056DECLARE_RTL_COND(rtl_phyar_cond)
1057{
1058 void __iomem *ioaddr = tp->mmio_addr;
1059
1060 return RTL_R32(PHYAR) & 0x80000000;
1061}
1062
Francois Romieu24192212012-07-06 20:19:42 +02001063static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Francois Romieu24192212012-07-06 20:19:42 +02001065 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Francois Romieu24192212012-07-06 20:19:42 +02001067 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Francois Romieuffc46952012-07-06 14:19:23 +02001069 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001070 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001071 * According to hardware specs a 20us delay is required after write
1072 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001073 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001074 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Francois Romieu24192212012-07-06 20:19:42 +02001077static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Francois Romieu24192212012-07-06 20:19:42 +02001079 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieuffc46952012-07-06 14:19:23 +02001080 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Francois Romieu24192212012-07-06 20:19:42 +02001082 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Francois Romieuffc46952012-07-06 14:19:23 +02001084 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1085 RTL_R32(PHYAR) & 0xffff : ~0;
1086
Timo Teräs81a95f02010-06-09 17:31:48 -07001087 /*
1088 * According to hardware specs a 20us delay is required after read
1089 * complete indication, but before sending next command.
1090 */
1091 udelay(20);
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return value;
1094}
1095
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001096DECLARE_RTL_COND(rtl_ocpar_cond)
1097{
1098 void __iomem *ioaddr = tp->mmio_addr;
1099
1100 return RTL_R32(OCPAR) & OCPAR_FLAG;
1101}
1102
Francois Romieu24192212012-07-06 20:19:42 +02001103static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001104{
Francois Romieu24192212012-07-06 20:19:42 +02001105 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001106
Francois Romieu24192212012-07-06 20:19:42 +02001107 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
françois romieuc0e45c12011-01-03 15:08:04 +00001108 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1109 RTL_W32(EPHY_RXER_NUM, 0);
1110
Francois Romieuffc46952012-07-06 14:19:23 +02001111 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001112}
1113
Francois Romieu24192212012-07-06 20:19:42 +02001114static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001115{
Francois Romieu24192212012-07-06 20:19:42 +02001116 r8168dp_1_mdio_access(tp, reg,
1117 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001118}
1119
Francois Romieu24192212012-07-06 20:19:42 +02001120static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001121{
Francois Romieu24192212012-07-06 20:19:42 +02001122 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001123
Francois Romieu24192212012-07-06 20:19:42 +02001124 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001125
1126 mdelay(1);
1127 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1128 RTL_W32(EPHY_RXER_NUM, 0);
1129
Francois Romieuffc46952012-07-06 14:19:23 +02001130 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1131 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001132}
1133
françois romieue6de30d2011-01-03 15:08:37 +00001134#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1135
1136static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1137{
1138 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1139}
1140
1141static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1142{
1143 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1144}
1145
Francois Romieu24192212012-07-06 20:19:42 +02001146static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001147{
Francois Romieu24192212012-07-06 20:19:42 +02001148 void __iomem *ioaddr = tp->mmio_addr;
1149
françois romieue6de30d2011-01-03 15:08:37 +00001150 r8168dp_2_mdio_start(ioaddr);
1151
Francois Romieu24192212012-07-06 20:19:42 +02001152 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001153
1154 r8168dp_2_mdio_stop(ioaddr);
1155}
1156
Francois Romieu24192212012-07-06 20:19:42 +02001157static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001158{
Francois Romieu24192212012-07-06 20:19:42 +02001159 void __iomem *ioaddr = tp->mmio_addr;
françois romieue6de30d2011-01-03 15:08:37 +00001160 int value;
1161
1162 r8168dp_2_mdio_start(ioaddr);
1163
Francois Romieu24192212012-07-06 20:19:42 +02001164 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001165
1166 r8168dp_2_mdio_stop(ioaddr);
1167
1168 return value;
1169}
1170
françois romieu4da19632011-01-03 15:07:55 +00001171static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001172{
Francois Romieu24192212012-07-06 20:19:42 +02001173 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001174}
1175
françois romieu4da19632011-01-03 15:07:55 +00001176static int rtl_readphy(struct rtl8169_private *tp, int location)
1177{
Francois Romieu24192212012-07-06 20:19:42 +02001178 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001179}
1180
1181static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1182{
1183 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1184}
1185
Chun-Hao Lin76564422014-10-01 23:17:17 +08001186static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001187{
1188 int val;
1189
françois romieu4da19632011-01-03 15:07:55 +00001190 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001191 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001192}
1193
Francois Romieuccdffb92008-07-26 14:26:06 +02001194static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1195 int val)
1196{
1197 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001198
françois romieu4da19632011-01-03 15:07:55 +00001199 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001200}
1201
1202static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1203{
1204 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001205
françois romieu4da19632011-01-03 15:07:55 +00001206 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001207}
1208
Francois Romieuffc46952012-07-06 14:19:23 +02001209DECLARE_RTL_COND(rtl_ephyar_cond)
1210{
1211 void __iomem *ioaddr = tp->mmio_addr;
1212
1213 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1214}
1215
Francois Romieufdf6fc02012-07-06 22:40:38 +02001216static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001217{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001218 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001219
1220 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1221 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1222
Francois Romieuffc46952012-07-06 14:19:23 +02001223 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1224
1225 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001226}
1227
Francois Romieufdf6fc02012-07-06 22:40:38 +02001228static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001229{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001230 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001231
1232 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1233
Francois Romieuffc46952012-07-06 14:19:23 +02001234 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1235 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001236}
1237
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001238DECLARE_RTL_COND(rtl_eriar_cond)
1239{
1240 void __iomem *ioaddr = tp->mmio_addr;
1241
1242 return RTL_R32(ERIAR) & ERIAR_FLAG;
1243}
1244
Francois Romieufdf6fc02012-07-06 22:40:38 +02001245static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1246 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001247{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001248 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001249
1250 BUG_ON((addr & 3) || (mask == 0));
1251 RTL_W32(ERIDR, val);
1252 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1253
Francois Romieuffc46952012-07-06 14:19:23 +02001254 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001255}
1256
Francois Romieufdf6fc02012-07-06 22:40:38 +02001257static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001258{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001259 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001260
1261 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1262
Francois Romieuffc46952012-07-06 14:19:23 +02001263 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1264 RTL_R32(ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001265}
1266
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001267static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001268 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001269{
1270 u32 val;
1271
Francois Romieufdf6fc02012-07-06 22:40:38 +02001272 val = rtl_eri_read(tp, addr, type);
1273 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001274}
1275
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001276static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1277{
1278 void __iomem *ioaddr = tp->mmio_addr;
1279
1280 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1281 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1282 RTL_R32(OCPDR) : ~0;
1283}
1284
1285static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1286{
1287 return rtl_eri_read(tp, reg, ERIAR_OOB);
1288}
1289
1290static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1291{
1292 switch (tp->mac_version) {
1293 case RTL_GIGA_MAC_VER_27:
1294 case RTL_GIGA_MAC_VER_28:
1295 case RTL_GIGA_MAC_VER_31:
1296 return r8168dp_ocp_read(tp, mask, reg);
1297 case RTL_GIGA_MAC_VER_49:
1298 case RTL_GIGA_MAC_VER_50:
1299 case RTL_GIGA_MAC_VER_51:
1300 return r8168ep_ocp_read(tp, mask, reg);
1301 default:
1302 BUG();
1303 return ~0;
1304 }
1305}
1306
1307static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1308 u32 data)
1309{
1310 void __iomem *ioaddr = tp->mmio_addr;
1311
1312 RTL_W32(OCPDR, data);
1313 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1314 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1315}
1316
1317static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1318 u32 data)
1319{
1320 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1321 data, ERIAR_OOB);
1322}
1323
1324static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1325{
1326 switch (tp->mac_version) {
1327 case RTL_GIGA_MAC_VER_27:
1328 case RTL_GIGA_MAC_VER_28:
1329 case RTL_GIGA_MAC_VER_31:
1330 r8168dp_ocp_write(tp, mask, reg, data);
1331 break;
1332 case RTL_GIGA_MAC_VER_49:
1333 case RTL_GIGA_MAC_VER_50:
1334 case RTL_GIGA_MAC_VER_51:
1335 r8168ep_ocp_write(tp, mask, reg, data);
1336 break;
1337 default:
1338 BUG();
1339 break;
1340 }
1341}
1342
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001343static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1344{
1345 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1346
1347 ocp_write(tp, 0x1, 0x30, 0x00000001);
1348}
1349
1350#define OOB_CMD_RESET 0x00
1351#define OOB_CMD_DRIVER_START 0x05
1352#define OOB_CMD_DRIVER_STOP 0x06
1353
1354static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1355{
1356 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1357}
1358
1359DECLARE_RTL_COND(rtl_ocp_read_cond)
1360{
1361 u16 reg;
1362
1363 reg = rtl8168_get_ocp_reg(tp);
1364
1365 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1366}
1367
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001368DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1369{
1370 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1371}
1372
1373DECLARE_RTL_COND(rtl_ocp_tx_cond)
1374{
1375 void __iomem *ioaddr = tp->mmio_addr;
1376
1377 return RTL_R8(IBISR0) & 0x02;
1378}
1379
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001380static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1381{
1382 void __iomem *ioaddr = tp->mmio_addr;
1383
1384 RTL_W8(IBCR2, RTL_R8(IBCR2) & ~0x01);
1385 rtl_msleep_loop_wait_low(tp, &rtl_ocp_tx_cond, 50, 2000);
1386 RTL_W8(IBISR0, RTL_R8(IBISR0) | 0x20);
1387 RTL_W8(IBCR0, RTL_R8(IBCR0) & ~0x01);
1388}
1389
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001390static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001391{
1392 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001393 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1394}
1395
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001396static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1397{
1398 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1399 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1400 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1401}
1402
1403static void rtl8168_driver_start(struct rtl8169_private *tp)
1404{
1405 switch (tp->mac_version) {
1406 case RTL_GIGA_MAC_VER_27:
1407 case RTL_GIGA_MAC_VER_28:
1408 case RTL_GIGA_MAC_VER_31:
1409 rtl8168dp_driver_start(tp);
1410 break;
1411 case RTL_GIGA_MAC_VER_49:
1412 case RTL_GIGA_MAC_VER_50:
1413 case RTL_GIGA_MAC_VER_51:
1414 rtl8168ep_driver_start(tp);
1415 break;
1416 default:
1417 BUG();
1418 break;
1419 }
1420}
1421
1422static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1423{
1424 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1425 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1426}
1427
1428static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1429{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001430 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001431 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1432 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1433 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1434}
1435
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001436static void rtl8168_driver_stop(struct rtl8169_private *tp)
1437{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001438 switch (tp->mac_version) {
1439 case RTL_GIGA_MAC_VER_27:
1440 case RTL_GIGA_MAC_VER_28:
1441 case RTL_GIGA_MAC_VER_31:
1442 rtl8168dp_driver_stop(tp);
1443 break;
1444 case RTL_GIGA_MAC_VER_49:
1445 case RTL_GIGA_MAC_VER_50:
1446 case RTL_GIGA_MAC_VER_51:
1447 rtl8168ep_driver_stop(tp);
1448 break;
1449 default:
1450 BUG();
1451 break;
1452 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001453}
1454
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001455static int r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001456{
1457 u16 reg = rtl8168_get_ocp_reg(tp);
1458
1459 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
1460}
1461
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001462static int r8168ep_check_dash(struct rtl8169_private *tp)
1463{
1464 return (ocp_read(tp, 0x0f, 0x128) & 0x00000001) ? 1 : 0;
1465}
1466
1467static int r8168_check_dash(struct rtl8169_private *tp)
1468{
1469 switch (tp->mac_version) {
1470 case RTL_GIGA_MAC_VER_27:
1471 case RTL_GIGA_MAC_VER_28:
1472 case RTL_GIGA_MAC_VER_31:
1473 return r8168dp_check_dash(tp);
1474 case RTL_GIGA_MAC_VER_49:
1475 case RTL_GIGA_MAC_VER_50:
1476 case RTL_GIGA_MAC_VER_51:
1477 return r8168ep_check_dash(tp);
1478 default:
1479 return 0;
1480 }
1481}
1482
françois romieuc28aa382011-08-02 03:53:43 +00001483struct exgmac_reg {
1484 u16 addr;
1485 u16 mask;
1486 u32 val;
1487};
1488
Francois Romieufdf6fc02012-07-06 22:40:38 +02001489static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001490 const struct exgmac_reg *r, int len)
1491{
1492 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001493 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001494 r++;
1495 }
1496}
1497
Francois Romieuffc46952012-07-06 14:19:23 +02001498DECLARE_RTL_COND(rtl_efusear_cond)
1499{
1500 void __iomem *ioaddr = tp->mmio_addr;
1501
1502 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1503}
1504
Francois Romieufdf6fc02012-07-06 22:40:38 +02001505static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001506{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001507 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00001508
1509 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1510
Francois Romieuffc46952012-07-06 14:19:23 +02001511 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1512 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001513}
1514
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001515static u16 rtl_get_events(struct rtl8169_private *tp)
1516{
1517 void __iomem *ioaddr = tp->mmio_addr;
1518
1519 return RTL_R16(IntrStatus);
1520}
1521
1522static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1523{
1524 void __iomem *ioaddr = tp->mmio_addr;
1525
1526 RTL_W16(IntrStatus, bits);
1527 mmiowb();
1528}
1529
1530static void rtl_irq_disable(struct rtl8169_private *tp)
1531{
1532 void __iomem *ioaddr = tp->mmio_addr;
1533
1534 RTL_W16(IntrMask, 0);
1535 mmiowb();
1536}
1537
Francois Romieu3e990ff2012-01-26 12:50:01 +01001538static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1539{
1540 void __iomem *ioaddr = tp->mmio_addr;
1541
1542 RTL_W16(IntrMask, bits);
1543}
1544
Francois Romieuda78dbf2012-01-26 14:18:23 +01001545#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1546#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1547#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1548
1549static void rtl_irq_enable_all(struct rtl8169_private *tp)
1550{
1551 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1552}
1553
françois romieu811fd302011-12-04 20:30:45 +00001554static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
françois romieu811fd302011-12-04 20:30:45 +00001556 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001558 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001559 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001560 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
françois romieu4da19632011-01-03 15:07:55 +00001563static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
françois romieu4da19632011-01-03 15:07:55 +00001565 void __iomem *ioaddr = tp->mmio_addr;
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 return RTL_R32(TBICSR) & TBIReset;
1568}
1569
françois romieu4da19632011-01-03 15:07:55 +00001570static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
françois romieu4da19632011-01-03 15:07:55 +00001572 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
1575static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1576{
1577 return RTL_R32(TBICSR) & TBILinkOk;
1578}
1579
1580static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1581{
1582 return RTL_R8(PHYstatus) & LinkStatus;
1583}
1584
françois romieu4da19632011-01-03 15:07:55 +00001585static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
françois romieu4da19632011-01-03 15:07:55 +00001587 void __iomem *ioaddr = tp->mmio_addr;
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1590}
1591
françois romieu4da19632011-01-03 15:07:55 +00001592static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
1594 unsigned int val;
1595
françois romieu4da19632011-01-03 15:07:55 +00001596 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1597 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
Hayes Wang70090422011-07-06 15:58:06 +08001600static void rtl_link_chg_patch(struct rtl8169_private *tp)
1601{
1602 void __iomem *ioaddr = tp->mmio_addr;
1603 struct net_device *dev = tp->dev;
1604
1605 if (!netif_running(dev))
1606 return;
1607
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001608 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1609 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001610 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001611 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1612 ERIAR_EXGMAC);
1613 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1614 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001615 } else if (RTL_R8(PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001616 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1617 ERIAR_EXGMAC);
1618 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1619 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001620 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001621 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1622 ERIAR_EXGMAC);
1623 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1624 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001625 }
1626 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001627 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001628 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001629 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001630 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001631 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1632 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1633 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001634 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1635 ERIAR_EXGMAC);
1636 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1637 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001638 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001639 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1640 ERIAR_EXGMAC);
1641 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1642 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001643 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001644 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1645 if (RTL_R8(PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001646 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1647 ERIAR_EXGMAC);
1648 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1649 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001650 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001651 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1652 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001653 }
Hayes Wang70090422011-07-06 15:58:06 +08001654 }
1655}
1656
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001657static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001658 struct rtl8169_private *tp,
1659 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001662 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001663 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001664 if (pm)
1665 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001667 if (net_ratelimit())
1668 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001669 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001671 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001672 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001673 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001677static void rtl8169_check_link_status(struct net_device *dev,
1678 struct rtl8169_private *tp,
1679 void __iomem *ioaddr)
1680{
1681 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1682}
1683
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001684#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1685
1686static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1687{
1688 void __iomem *ioaddr = tp->mmio_addr;
1689 u8 options;
1690 u32 wolopts = 0;
1691
1692 options = RTL_R8(Config1);
1693 if (!(options & PMEnable))
1694 return 0;
1695
1696 options = RTL_R8(Config3);
1697 if (options & LinkUp)
1698 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001699 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001700 case RTL_GIGA_MAC_VER_34:
1701 case RTL_GIGA_MAC_VER_35:
1702 case RTL_GIGA_MAC_VER_36:
1703 case RTL_GIGA_MAC_VER_37:
1704 case RTL_GIGA_MAC_VER_38:
1705 case RTL_GIGA_MAC_VER_40:
1706 case RTL_GIGA_MAC_VER_41:
1707 case RTL_GIGA_MAC_VER_42:
1708 case RTL_GIGA_MAC_VER_43:
1709 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001710 case RTL_GIGA_MAC_VER_45:
1711 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001712 case RTL_GIGA_MAC_VER_47:
1713 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001714 case RTL_GIGA_MAC_VER_49:
1715 case RTL_GIGA_MAC_VER_50:
1716 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001717 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1718 wolopts |= WAKE_MAGIC;
1719 break;
1720 default:
1721 if (options & MagicPacket)
1722 wolopts |= WAKE_MAGIC;
1723 break;
1724 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001725
1726 options = RTL_R8(Config5);
1727 if (options & UWF)
1728 wolopts |= WAKE_UCAST;
1729 if (options & BWF)
1730 wolopts |= WAKE_BCAST;
1731 if (options & MWF)
1732 wolopts |= WAKE_MCAST;
1733
1734 return wolopts;
1735}
1736
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001737static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1738{
1739 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001740
Francois Romieuda78dbf2012-01-26 14:18:23 +01001741 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001742
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001743 wol->supported = WAKE_ANY;
1744 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001745
Francois Romieuda78dbf2012-01-26 14:18:23 +01001746 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001747}
1748
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001749static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001750{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001751 void __iomem *ioaddr = tp->mmio_addr;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001752 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001753 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001754 u32 opt;
1755 u16 reg;
1756 u8 mask;
1757 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001758 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001759 { WAKE_UCAST, Config5, UWF },
1760 { WAKE_BCAST, Config5, BWF },
1761 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001762 { WAKE_ANY, Config5, LanWake },
1763 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001764 };
Francois Romieu851e6022012-04-17 11:10:11 +02001765 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001766
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001767 RTL_W8(Cfg9346, Cfg9346_Unlock);
1768
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001769 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001770 case RTL_GIGA_MAC_VER_34:
1771 case RTL_GIGA_MAC_VER_35:
1772 case RTL_GIGA_MAC_VER_36:
1773 case RTL_GIGA_MAC_VER_37:
1774 case RTL_GIGA_MAC_VER_38:
1775 case RTL_GIGA_MAC_VER_40:
1776 case RTL_GIGA_MAC_VER_41:
1777 case RTL_GIGA_MAC_VER_42:
1778 case RTL_GIGA_MAC_VER_43:
1779 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001780 case RTL_GIGA_MAC_VER_45:
1781 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001782 case RTL_GIGA_MAC_VER_47:
1783 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001784 case RTL_GIGA_MAC_VER_49:
1785 case RTL_GIGA_MAC_VER_50:
1786 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001787 tmp = ARRAY_SIZE(cfg) - 1;
1788 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001789 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001790 0x0dc,
1791 ERIAR_MASK_0100,
1792 MagicPacket_v2,
1793 0x0000,
1794 ERIAR_EXGMAC);
1795 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001796 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001797 0x0dc,
1798 ERIAR_MASK_0100,
1799 0x0000,
1800 MagicPacket_v2,
1801 ERIAR_EXGMAC);
1802 break;
1803 default:
1804 tmp = ARRAY_SIZE(cfg);
1805 break;
1806 }
1807
1808 for (i = 0; i < tmp; i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001809 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001810 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001811 options |= cfg[i].mask;
1812 RTL_W8(cfg[i].reg, options);
1813 }
1814
Francois Romieu851e6022012-04-17 11:10:11 +02001815 switch (tp->mac_version) {
1816 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1817 options = RTL_R8(Config1) & ~PMEnable;
1818 if (wolopts)
1819 options |= PMEnable;
1820 RTL_W8(Config1, options);
1821 break;
1822 default:
Francois Romieud387b422012-04-17 11:12:01 +02001823 options = RTL_R8(Config2) & ~PME_SIGNAL;
1824 if (wolopts)
1825 options |= PME_SIGNAL;
1826 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001827 break;
1828 }
1829
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001830 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001831}
1832
1833static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1834{
1835 struct rtl8169_private *tp = netdev_priv(dev);
1836
Francois Romieuda78dbf2012-01-26 14:18:23 +01001837 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001838
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001839 if (wol->wolopts)
1840 tp->features |= RTL_FEATURE_WOL;
1841 else
1842 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001843 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001844
1845 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001846
françois romieuea809072010-11-08 13:23:58 +00001847 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1848
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001849 return 0;
1850}
1851
Francois Romieu31bd2042011-04-26 18:58:59 +02001852static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1853{
Francois Romieu85bffe62011-04-27 08:22:39 +02001854 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001855}
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857static void rtl8169_get_drvinfo(struct net_device *dev,
1858 struct ethtool_drvinfo *info)
1859{
1860 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001861 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Rick Jones68aad782011-11-07 13:29:27 +00001863 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1864 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1865 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001866 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001867 if (!IS_ERR_OR_NULL(rtl_fw))
1868 strlcpy(info->fw_version, rtl_fw->version,
1869 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
1871
1872static int rtl8169_get_regs_len(struct net_device *dev)
1873{
1874 return R8169_REGS_SIZE;
1875}
1876
1877static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001878 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
1880 struct rtl8169_private *tp = netdev_priv(dev);
1881 void __iomem *ioaddr = tp->mmio_addr;
1882 int ret = 0;
1883 u32 reg;
1884
1885 reg = RTL_R32(TBICSR);
1886 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1887 (duplex == DUPLEX_FULL)) {
1888 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1889 } else if (autoneg == AUTONEG_ENABLE)
1890 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1891 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001892 netif_warn(tp, link, dev,
1893 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 ret = -EOPNOTSUPP;
1895 }
1896
1897 return ret;
1898}
1899
1900static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001901 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
1903 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001904 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001905 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Hayes Wang716b50a2011-02-22 17:26:18 +08001907 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001910 int auto_nego;
1911
françois romieu4da19632011-01-03 15:07:55 +00001912 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001913 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1914 ADVERTISE_100HALF | ADVERTISE_100FULL);
1915
1916 if (adv & ADVERTISED_10baseT_Half)
1917 auto_nego |= ADVERTISE_10HALF;
1918 if (adv & ADVERTISED_10baseT_Full)
1919 auto_nego |= ADVERTISE_10FULL;
1920 if (adv & ADVERTISED_100baseT_Half)
1921 auto_nego |= ADVERTISE_100HALF;
1922 if (adv & ADVERTISED_100baseT_Full)
1923 auto_nego |= ADVERTISE_100FULL;
1924
françois romieu3577aa12009-05-19 10:46:48 +00001925 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1926
françois romieu4da19632011-01-03 15:07:55 +00001927 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001928 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1929
1930 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001931 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001932 if (adv & ADVERTISED_1000baseT_Half)
1933 giga_ctrl |= ADVERTISE_1000HALF;
1934 if (adv & ADVERTISED_1000baseT_Full)
1935 giga_ctrl |= ADVERTISE_1000FULL;
1936 } else if (adv & (ADVERTISED_1000baseT_Half |
1937 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001938 netif_info(tp, link, dev,
1939 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001940 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
françois romieu3577aa12009-05-19 10:46:48 +00001943 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001944
françois romieu4da19632011-01-03 15:07:55 +00001945 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1946 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001947 } else {
1948 giga_ctrl = 0;
1949
1950 if (speed == SPEED_10)
1951 bmcr = 0;
1952 else if (speed == SPEED_100)
1953 bmcr = BMCR_SPEED100;
1954 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001955 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001956
1957 if (duplex == DUPLEX_FULL)
1958 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001959 }
1960
françois romieu4da19632011-01-03 15:07:55 +00001961 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001962
Francois Romieucecb5fd2011-04-01 10:21:07 +02001963 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1964 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001965 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001966 rtl_writephy(tp, 0x17, 0x2138);
1967 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001968 } else {
françois romieu4da19632011-01-03 15:07:55 +00001969 rtl_writephy(tp, 0x17, 0x2108);
1970 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001971 }
1972 }
1973
Oliver Neukum54405cd2011-01-06 21:55:13 +01001974 rc = 0;
1975out:
1976 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
1979static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001980 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 struct rtl8169_private *tp = netdev_priv(dev);
1983 int ret;
1984
Oliver Neukum54405cd2011-01-06 21:55:13 +01001985 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001986 if (ret < 0)
1987 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Francois Romieu4876cc12011-03-11 21:07:11 +01001989 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1990 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001992 }
1993out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return ret;
1995}
1996
1997static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1998{
1999 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 int ret;
2001
Francois Romieu4876cc12011-03-11 21:07:11 +01002002 del_timer_sync(&tp->timer);
2003
Francois Romieuda78dbf2012-01-26 14:18:23 +01002004 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002005 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00002006 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002007 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02002008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return ret;
2010}
2011
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002012static netdev_features_t rtl8169_fix_features(struct net_device *dev,
2013 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
Francois Romieud58d46b2011-05-03 16:38:29 +02002015 struct rtl8169_private *tp = netdev_priv(dev);
2016
Francois Romieu2b7b4312011-04-18 22:53:24 -07002017 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00002018 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Francois Romieud58d46b2011-05-03 16:38:29 +02002020 if (dev->mtu > JUMBO_1K &&
2021 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
2022 features &= ~NETIF_F_IP_CSUM;
2023
Michał Mirosław350fb322011-04-08 06:35:56 +00002024 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
Francois Romieuda78dbf2012-01-26 14:18:23 +01002027static void __rtl8169_set_features(struct net_device *dev,
2028 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
2030 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002031 void __iomem *ioaddr = tp->mmio_addr;
hayeswang929a0312014-09-16 11:40:47 +08002032 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
hayeswang929a0312014-09-16 11:40:47 +08002034 rx_config = RTL_R32(RxConfig);
2035 if (features & NETIF_F_RXALL)
2036 rx_config |= (AcceptErr | AcceptRunt);
2037 else
2038 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
hayeswang929a0312014-09-16 11:40:47 +08002040 RTL_W32(RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00002041
hayeswang929a0312014-09-16 11:40:47 +08002042 if (features & NETIF_F_RXCSUM)
2043 tp->cp_cmd |= RxChkSum;
2044 else
2045 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00002046
hayeswang929a0312014-09-16 11:40:47 +08002047 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2048 tp->cp_cmd |= RxVlan;
2049 else
2050 tp->cp_cmd &= ~RxVlan;
2051
2052 tp->cp_cmd |= RTL_R16(CPlusCmd) & ~(RxVlan | RxChkSum);
2053
2054 RTL_W16(CPlusCmd, tp->cp_cmd);
2055 RTL_R16(CPlusCmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002056}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Francois Romieuda78dbf2012-01-26 14:18:23 +01002058static int rtl8169_set_features(struct net_device *dev,
2059 netdev_features_t features)
2060{
2061 struct rtl8169_private *tp = netdev_priv(dev);
2062
hayeswang929a0312014-09-16 11:40:47 +08002063 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
2064
Francois Romieuda78dbf2012-01-26 14:18:23 +01002065 rtl_lock_work(tp);
Dan Carpenter85911d72014-09-19 13:40:25 +03002066 if (features ^ dev->features)
hayeswang929a0312014-09-16 11:40:47 +08002067 __rtl8169_set_features(dev, features);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002068 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 return 0;
2071}
2072
Francois Romieuda78dbf2012-01-26 14:18:23 +01002073
Kirill Smelkov810f4892012-11-10 21:11:02 +04002074static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002076 return (skb_vlan_tag_present(skb)) ?
2077 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
Francois Romieu7a8fc772011-03-01 17:18:33 +01002080static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Francois Romieu7a8fc772011-03-01 17:18:33 +01002084 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00002085 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
Francois Romieuccdffb92008-07-26 14:26:06 +02002088static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
2090 struct rtl8169_private *tp = netdev_priv(dev);
2091 void __iomem *ioaddr = tp->mmio_addr;
2092 u32 status;
2093
2094 cmd->supported =
2095 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
2096 cmd->port = PORT_FIBRE;
2097 cmd->transceiver = XCVR_INTERNAL;
2098
2099 status = RTL_R32(TBICSR);
2100 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
2101 cmd->autoneg = !!(status & TBINwEnable);
2102
David Decotigny70739492011-04-27 18:32:40 +00002103 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02002105
2106 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
2108
Francois Romieuccdffb92008-07-26 14:26:06 +02002109static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
2111 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Francois Romieuccdffb92008-07-26 14:26:06 +02002113 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
2116static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2117{
2118 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02002119 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Francois Romieuda78dbf2012-01-26 14:18:23 +01002121 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02002122 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002123 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Francois Romieuccdffb92008-07-26 14:26:06 +02002125 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
2128static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2129 void *p)
2130{
Francois Romieu5b0384f2006-08-16 16:00:01 +02002131 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02002132 u32 __iomem *data = tp->mmio_addr;
2133 u32 *dw = p;
2134 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Francois Romieuda78dbf2012-01-26 14:18:23 +01002136 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02002137 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2138 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002139 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
2141
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002142static u32 rtl8169_get_msglevel(struct net_device *dev)
2143{
2144 struct rtl8169_private *tp = netdev_priv(dev);
2145
2146 return tp->msg_enable;
2147}
2148
2149static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2150{
2151 struct rtl8169_private *tp = netdev_priv(dev);
2152
2153 tp->msg_enable = value;
2154}
2155
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002156static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2157 "tx_packets",
2158 "rx_packets",
2159 "tx_errors",
2160 "rx_errors",
2161 "rx_missed",
2162 "align_errors",
2163 "tx_single_collisions",
2164 "tx_multi_collisions",
2165 "unicast",
2166 "broadcast",
2167 "multicast",
2168 "tx_aborted",
2169 "tx_underrun",
2170};
2171
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002172static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002173{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002174 switch (sset) {
2175 case ETH_SS_STATS:
2176 return ARRAY_SIZE(rtl8169_gstrings);
2177 default:
2178 return -EOPNOTSUPP;
2179 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002180}
2181
Francois Romieuffc46952012-07-06 14:19:23 +02002182DECLARE_RTL_COND(rtl_counters_cond)
2183{
2184 void __iomem *ioaddr = tp->mmio_addr;
2185
2186 return RTL_R32(CounterAddrLow) & CounterDump;
2187}
2188
Ivan Vecera355423d2009-02-06 21:49:57 -08002189static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002190{
2191 struct rtl8169_private *tp = netdev_priv(dev);
2192 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02002193 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002194 struct rtl8169_counters *counters;
2195 dma_addr_t paddr;
2196 u32 cmd;
2197
Ivan Vecera355423d2009-02-06 21:49:57 -08002198 /*
2199 * Some chips are unable to dump tally counters when the receiver
2200 * is disabled.
2201 */
2202 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
2203 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002204
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00002205 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002206 if (!counters)
2207 return;
2208
2209 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07002210 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002211 RTL_W32(CounterAddrLow, cmd);
2212 RTL_W32(CounterAddrLow, cmd | CounterDump);
2213
Francois Romieuffc46952012-07-06 14:19:23 +02002214 if (rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000))
2215 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002216
2217 RTL_W32(CounterAddrLow, 0);
2218 RTL_W32(CounterAddrHigh, 0);
2219
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00002220 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002221}
2222
Ivan Vecera355423d2009-02-06 21:49:57 -08002223static void rtl8169_get_ethtool_stats(struct net_device *dev,
2224 struct ethtool_stats *stats, u64 *data)
2225{
2226 struct rtl8169_private *tp = netdev_priv(dev);
2227
2228 ASSERT_RTNL();
2229
2230 rtl8169_update_counters(dev);
2231
2232 data[0] = le64_to_cpu(tp->counters.tx_packets);
2233 data[1] = le64_to_cpu(tp->counters.rx_packets);
2234 data[2] = le64_to_cpu(tp->counters.tx_errors);
2235 data[3] = le32_to_cpu(tp->counters.rx_errors);
2236 data[4] = le16_to_cpu(tp->counters.rx_missed);
2237 data[5] = le16_to_cpu(tp->counters.align_errors);
2238 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
2239 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
2240 data[8] = le64_to_cpu(tp->counters.rx_unicast);
2241 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
2242 data[10] = le32_to_cpu(tp->counters.rx_multicast);
2243 data[11] = le16_to_cpu(tp->counters.tx_aborted);
2244 data[12] = le16_to_cpu(tp->counters.tx_underun);
2245}
2246
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002247static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2248{
2249 switch(stringset) {
2250 case ETH_SS_STATS:
2251 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2252 break;
2253 }
2254}
2255
Jeff Garzik7282d492006-09-13 14:30:00 -04002256static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 .get_drvinfo = rtl8169_get_drvinfo,
2258 .get_regs_len = rtl8169_get_regs_len,
2259 .get_link = ethtool_op_get_link,
2260 .get_settings = rtl8169_get_settings,
2261 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002262 .get_msglevel = rtl8169_get_msglevel,
2263 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002265 .get_wol = rtl8169_get_wol,
2266 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002267 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002268 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002269 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002270 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271};
2272
Francois Romieu07d3f512007-02-21 22:40:46 +01002273static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02002274 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
Francois Romieu5d320a22011-05-08 17:47:36 +02002276 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01002277 /*
2278 * The driver currently handles the 8168Bf and the 8168Be identically
2279 * but they can be identified more specifically through the test below
2280 * if needed:
2281 *
2282 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002283 *
2284 * Same thing for the 8101Eb and the 8101Ec:
2285 *
2286 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002287 */
Francois Romieu37441002011-06-17 22:58:54 +02002288 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002290 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 int mac_version;
2292 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002293 /* 8168EP family. */
2294 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2295 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2296 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2297
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002298 /* 8168H family. */
2299 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2300 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2301
Hayes Wangc5583862012-07-02 17:23:22 +08002302 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002303 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002304 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002305 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2306 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2307
Hayes Wangc2218922011-09-06 16:55:18 +08002308 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002309 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002310 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2311 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2312
hayeswang01dc7fe2011-03-21 01:50:28 +00002313 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002314 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002315 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2316 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2317 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2318
Francois Romieu5b538df2008-07-20 16:22:45 +02002319 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002320 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2321 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002322 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002323
françois romieue6de30d2011-01-03 15:08:37 +00002324 /* 8168DP family. */
2325 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2326 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002327 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002328
Francois Romieuef808d52008-06-29 13:10:54 +02002329 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07002330 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02002331 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002332 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002333 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002334 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2335 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002336 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02002337 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02002338 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002339
2340 /* 8168B family. */
2341 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2342 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2343 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2344 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2345
2346 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002347 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2348 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002349 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00002350 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002351 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2352 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2353 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002354 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2355 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2356 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2357 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2358 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2359 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002360 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002361 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002362 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002363 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2364 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002365 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2366 /* FIXME: where did these entries come from ? -- FR */
2367 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2368 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2369
2370 /* 8110 family. */
2371 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2372 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2373 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2374 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2375 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2376 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2377
Jean Delvaref21b75e2009-05-26 20:54:48 -07002378 /* Catch-all */
2379 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002380 };
2381 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 u32 reg;
2383
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002384 reg = RTL_R32(TxConfig);
2385 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 p++;
2387 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002388
2389 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2390 netif_notice(tp, probe, dev,
2391 "unknown MAC, using family default\n");
2392 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002393 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2394 tp->mac_version = tp->mii.supports_gmii ?
2395 RTL_GIGA_MAC_VER_42 :
2396 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002397 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2398 tp->mac_version = tp->mii.supports_gmii ?
2399 RTL_GIGA_MAC_VER_45 :
2400 RTL_GIGA_MAC_VER_47;
2401 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2402 tp->mac_version = tp->mii.supports_gmii ?
2403 RTL_GIGA_MAC_VER_46 :
2404 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406}
2407
2408static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2409{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002410 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
2412
Francois Romieu867763c2007-08-17 18:21:58 +02002413struct phy_reg {
2414 u16 reg;
2415 u16 val;
2416};
2417
françois romieu4da19632011-01-03 15:07:55 +00002418static void rtl_writephy_batch(struct rtl8169_private *tp,
2419 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002420{
2421 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002422 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002423 regs++;
2424 }
2425}
2426
françois romieubca03d52011-01-03 15:07:31 +00002427#define PHY_READ 0x00000000
2428#define PHY_DATA_OR 0x10000000
2429#define PHY_DATA_AND 0x20000000
2430#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002431#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002432#define PHY_CLEAR_READCOUNT 0x70000000
2433#define PHY_WRITE 0x80000000
2434#define PHY_READCOUNT_EQ_SKIP 0x90000000
2435#define PHY_COMP_EQ_SKIPN 0xa0000000
2436#define PHY_COMP_NEQ_SKIPN 0xb0000000
2437#define PHY_WRITE_PREVIOUS 0xc0000000
2438#define PHY_SKIPN 0xd0000000
2439#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002440
Hayes Wang960aee62011-06-18 11:37:48 +02002441struct fw_info {
2442 u32 magic;
2443 char version[RTL_VER_SIZE];
2444 __le32 fw_start;
2445 __le32 fw_len;
2446 u8 chksum;
2447} __packed;
2448
Francois Romieu1c361ef2011-06-17 17:16:24 +02002449#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2450
2451static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002452{
Francois Romieub6ffd972011-06-17 17:00:05 +02002453 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002454 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002455 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2456 char *version = rtl_fw->version;
2457 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002458
Francois Romieu1c361ef2011-06-17 17:16:24 +02002459 if (fw->size < FW_OPCODE_SIZE)
2460 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002461
2462 if (!fw_info->magic) {
2463 size_t i, size, start;
2464 u8 checksum = 0;
2465
2466 if (fw->size < sizeof(*fw_info))
2467 goto out;
2468
2469 for (i = 0; i < fw->size; i++)
2470 checksum += fw->data[i];
2471 if (checksum != 0)
2472 goto out;
2473
2474 start = le32_to_cpu(fw_info->fw_start);
2475 if (start > fw->size)
2476 goto out;
2477
2478 size = le32_to_cpu(fw_info->fw_len);
2479 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2480 goto out;
2481
2482 memcpy(version, fw_info->version, RTL_VER_SIZE);
2483
2484 pa->code = (__le32 *)(fw->data + start);
2485 pa->size = size;
2486 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002487 if (fw->size % FW_OPCODE_SIZE)
2488 goto out;
2489
2490 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2491
2492 pa->code = (__le32 *)fw->data;
2493 pa->size = fw->size / FW_OPCODE_SIZE;
2494 }
2495 version[RTL_VER_SIZE - 1] = 0;
2496
2497 rc = true;
2498out:
2499 return rc;
2500}
2501
Francois Romieufd112f22011-06-18 00:10:29 +02002502static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2503 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002504{
Francois Romieufd112f22011-06-18 00:10:29 +02002505 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002506 size_t index;
2507
Francois Romieu1c361ef2011-06-17 17:16:24 +02002508 for (index = 0; index < pa->size; index++) {
2509 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002510 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002511
hayeswang42b82dc2011-01-10 02:07:25 +00002512 switch(action & 0xf0000000) {
2513 case PHY_READ:
2514 case PHY_DATA_OR:
2515 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002516 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002517 case PHY_CLEAR_READCOUNT:
2518 case PHY_WRITE:
2519 case PHY_WRITE_PREVIOUS:
2520 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002521 break;
2522
hayeswang42b82dc2011-01-10 02:07:25 +00002523 case PHY_BJMPN:
2524 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002525 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002526 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002527 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002528 }
2529 break;
2530 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002531 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002532 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002533 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002534 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002535 }
2536 break;
2537 case PHY_COMP_EQ_SKIPN:
2538 case PHY_COMP_NEQ_SKIPN:
2539 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002540 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002541 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002542 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002543 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002544 }
2545 break;
2546
hayeswang42b82dc2011-01-10 02:07:25 +00002547 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002548 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002549 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002550 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002551 }
2552 }
Francois Romieufd112f22011-06-18 00:10:29 +02002553 rc = true;
2554out:
2555 return rc;
2556}
françois romieubca03d52011-01-03 15:07:31 +00002557
Francois Romieufd112f22011-06-18 00:10:29 +02002558static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2559{
2560 struct net_device *dev = tp->dev;
2561 int rc = -EINVAL;
2562
2563 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002564 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002565 goto out;
2566 }
2567
2568 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2569 rc = 0;
2570out:
2571 return rc;
2572}
2573
2574static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2575{
2576 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002577 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002578 u32 predata, count;
2579 size_t index;
2580
2581 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002582 org.write = ops->write;
2583 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002584
Francois Romieu1c361ef2011-06-17 17:16:24 +02002585 for (index = 0; index < pa->size; ) {
2586 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002587 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002588 u32 regno = (action & 0x0fff0000) >> 16;
2589
2590 if (!action)
2591 break;
françois romieubca03d52011-01-03 15:07:31 +00002592
2593 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002594 case PHY_READ:
2595 predata = rtl_readphy(tp, regno);
2596 count++;
2597 index++;
françois romieubca03d52011-01-03 15:07:31 +00002598 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002599 case PHY_DATA_OR:
2600 predata |= data;
2601 index++;
2602 break;
2603 case PHY_DATA_AND:
2604 predata &= data;
2605 index++;
2606 break;
2607 case PHY_BJMPN:
2608 index -= regno;
2609 break;
hayeswangeee37862013-04-01 22:23:38 +00002610 case PHY_MDIO_CHG:
2611 if (data == 0) {
2612 ops->write = org.write;
2613 ops->read = org.read;
2614 } else if (data == 1) {
2615 ops->write = mac_mcu_write;
2616 ops->read = mac_mcu_read;
2617 }
2618
hayeswang42b82dc2011-01-10 02:07:25 +00002619 index++;
2620 break;
2621 case PHY_CLEAR_READCOUNT:
2622 count = 0;
2623 index++;
2624 break;
2625 case PHY_WRITE:
2626 rtl_writephy(tp, regno, data);
2627 index++;
2628 break;
2629 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002630 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002631 break;
2632 case PHY_COMP_EQ_SKIPN:
2633 if (predata == data)
2634 index += regno;
2635 index++;
2636 break;
2637 case PHY_COMP_NEQ_SKIPN:
2638 if (predata != data)
2639 index += regno;
2640 index++;
2641 break;
2642 case PHY_WRITE_PREVIOUS:
2643 rtl_writephy(tp, regno, predata);
2644 index++;
2645 break;
2646 case PHY_SKIPN:
2647 index += regno + 1;
2648 break;
2649 case PHY_DELAY_MS:
2650 mdelay(data);
2651 index++;
2652 break;
2653
françois romieubca03d52011-01-03 15:07:31 +00002654 default:
2655 BUG();
2656 }
2657 }
hayeswangeee37862013-04-01 22:23:38 +00002658
2659 ops->write = org.write;
2660 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002661}
2662
françois romieuf1e02ed2011-01-13 13:07:53 +00002663static void rtl_release_firmware(struct rtl8169_private *tp)
2664{
Francois Romieub6ffd972011-06-17 17:00:05 +02002665 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2666 release_firmware(tp->rtl_fw->fw);
2667 kfree(tp->rtl_fw);
2668 }
2669 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002670}
2671
François Romieu953a12c2011-04-24 17:38:48 +02002672static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002673{
Francois Romieub6ffd972011-06-17 17:00:05 +02002674 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002675
2676 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002677 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002678 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002679}
2680
2681static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2682{
2683 if (rtl_readphy(tp, reg) != val)
2684 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2685 else
2686 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002687}
2688
françois romieu4da19632011-01-03 15:07:55 +00002689static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002691 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002692 { 0x1f, 0x0001 },
2693 { 0x06, 0x006e },
2694 { 0x08, 0x0708 },
2695 { 0x15, 0x4000 },
2696 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
françois romieu0b9b5712009-08-10 19:44:56 +00002698 { 0x1f, 0x0001 },
2699 { 0x03, 0x00a1 },
2700 { 0x02, 0x0008 },
2701 { 0x01, 0x0120 },
2702 { 0x00, 0x1000 },
2703 { 0x04, 0x0800 },
2704 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
françois romieu0b9b5712009-08-10 19:44:56 +00002706 { 0x03, 0xff41 },
2707 { 0x02, 0xdf60 },
2708 { 0x01, 0x0140 },
2709 { 0x00, 0x0077 },
2710 { 0x04, 0x7800 },
2711 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
françois romieu0b9b5712009-08-10 19:44:56 +00002713 { 0x03, 0x802f },
2714 { 0x02, 0x4f02 },
2715 { 0x01, 0x0409 },
2716 { 0x00, 0xf0f9 },
2717 { 0x04, 0x9800 },
2718 { 0x04, 0x9000 },
2719
2720 { 0x03, 0xdf01 },
2721 { 0x02, 0xdf20 },
2722 { 0x01, 0xff95 },
2723 { 0x00, 0xba00 },
2724 { 0x04, 0xa800 },
2725 { 0x04, 0xa000 },
2726
2727 { 0x03, 0xff41 },
2728 { 0x02, 0xdf20 },
2729 { 0x01, 0x0140 },
2730 { 0x00, 0x00bb },
2731 { 0x04, 0xb800 },
2732 { 0x04, 0xb000 },
2733
2734 { 0x03, 0xdf41 },
2735 { 0x02, 0xdc60 },
2736 { 0x01, 0x6340 },
2737 { 0x00, 0x007d },
2738 { 0x04, 0xd800 },
2739 { 0x04, 0xd000 },
2740
2741 { 0x03, 0xdf01 },
2742 { 0x02, 0xdf20 },
2743 { 0x01, 0x100a },
2744 { 0x00, 0xa0ff },
2745 { 0x04, 0xf800 },
2746 { 0x04, 0xf000 },
2747
2748 { 0x1f, 0x0000 },
2749 { 0x0b, 0x0000 },
2750 { 0x00, 0x9200 }
2751 };
2752
françois romieu4da19632011-01-03 15:07:55 +00002753 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754}
2755
françois romieu4da19632011-01-03 15:07:55 +00002756static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002757{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002758 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002759 { 0x1f, 0x0002 },
2760 { 0x01, 0x90d0 },
2761 { 0x1f, 0x0000 }
2762 };
2763
françois romieu4da19632011-01-03 15:07:55 +00002764 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002765}
2766
françois romieu4da19632011-01-03 15:07:55 +00002767static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002768{
2769 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002770
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002771 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2772 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002773 return;
2774
françois romieu4da19632011-01-03 15:07:55 +00002775 rtl_writephy(tp, 0x1f, 0x0001);
2776 rtl_writephy(tp, 0x10, 0xf01b);
2777 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002778}
2779
françois romieu4da19632011-01-03 15:07:55 +00002780static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002781{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002782 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002783 { 0x1f, 0x0001 },
2784 { 0x04, 0x0000 },
2785 { 0x03, 0x00a1 },
2786 { 0x02, 0x0008 },
2787 { 0x01, 0x0120 },
2788 { 0x00, 0x1000 },
2789 { 0x04, 0x0800 },
2790 { 0x04, 0x9000 },
2791 { 0x03, 0x802f },
2792 { 0x02, 0x4f02 },
2793 { 0x01, 0x0409 },
2794 { 0x00, 0xf099 },
2795 { 0x04, 0x9800 },
2796 { 0x04, 0xa000 },
2797 { 0x03, 0xdf01 },
2798 { 0x02, 0xdf20 },
2799 { 0x01, 0xff95 },
2800 { 0x00, 0xba00 },
2801 { 0x04, 0xa800 },
2802 { 0x04, 0xf000 },
2803 { 0x03, 0xdf01 },
2804 { 0x02, 0xdf20 },
2805 { 0x01, 0x101a },
2806 { 0x00, 0xa0ff },
2807 { 0x04, 0xf800 },
2808 { 0x04, 0x0000 },
2809 { 0x1f, 0x0000 },
2810
2811 { 0x1f, 0x0001 },
2812 { 0x10, 0xf41b },
2813 { 0x14, 0xfb54 },
2814 { 0x18, 0xf5c7 },
2815 { 0x1f, 0x0000 },
2816
2817 { 0x1f, 0x0001 },
2818 { 0x17, 0x0cc0 },
2819 { 0x1f, 0x0000 }
2820 };
2821
françois romieu4da19632011-01-03 15:07:55 +00002822 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002823
françois romieu4da19632011-01-03 15:07:55 +00002824 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002825}
2826
françois romieu4da19632011-01-03 15:07:55 +00002827static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002828{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002829 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002830 { 0x1f, 0x0001 },
2831 { 0x04, 0x0000 },
2832 { 0x03, 0x00a1 },
2833 { 0x02, 0x0008 },
2834 { 0x01, 0x0120 },
2835 { 0x00, 0x1000 },
2836 { 0x04, 0x0800 },
2837 { 0x04, 0x9000 },
2838 { 0x03, 0x802f },
2839 { 0x02, 0x4f02 },
2840 { 0x01, 0x0409 },
2841 { 0x00, 0xf099 },
2842 { 0x04, 0x9800 },
2843 { 0x04, 0xa000 },
2844 { 0x03, 0xdf01 },
2845 { 0x02, 0xdf20 },
2846 { 0x01, 0xff95 },
2847 { 0x00, 0xba00 },
2848 { 0x04, 0xa800 },
2849 { 0x04, 0xf000 },
2850 { 0x03, 0xdf01 },
2851 { 0x02, 0xdf20 },
2852 { 0x01, 0x101a },
2853 { 0x00, 0xa0ff },
2854 { 0x04, 0xf800 },
2855 { 0x04, 0x0000 },
2856 { 0x1f, 0x0000 },
2857
2858 { 0x1f, 0x0001 },
2859 { 0x0b, 0x8480 },
2860 { 0x1f, 0x0000 },
2861
2862 { 0x1f, 0x0001 },
2863 { 0x18, 0x67c7 },
2864 { 0x04, 0x2000 },
2865 { 0x03, 0x002f },
2866 { 0x02, 0x4360 },
2867 { 0x01, 0x0109 },
2868 { 0x00, 0x3022 },
2869 { 0x04, 0x2800 },
2870 { 0x1f, 0x0000 },
2871
2872 { 0x1f, 0x0001 },
2873 { 0x17, 0x0cc0 },
2874 { 0x1f, 0x0000 }
2875 };
2876
françois romieu4da19632011-01-03 15:07:55 +00002877 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002878}
2879
françois romieu4da19632011-01-03 15:07:55 +00002880static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002881{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002882 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002883 { 0x10, 0xf41b },
2884 { 0x1f, 0x0000 }
2885 };
2886
françois romieu4da19632011-01-03 15:07:55 +00002887 rtl_writephy(tp, 0x1f, 0x0001);
2888 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002889
françois romieu4da19632011-01-03 15:07:55 +00002890 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002891}
2892
françois romieu4da19632011-01-03 15:07:55 +00002893static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002894{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002895 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002896 { 0x1f, 0x0001 },
2897 { 0x10, 0xf41b },
2898 { 0x1f, 0x0000 }
2899 };
2900
françois romieu4da19632011-01-03 15:07:55 +00002901 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002902}
2903
françois romieu4da19632011-01-03 15:07:55 +00002904static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002905{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002906 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002907 { 0x1f, 0x0000 },
2908 { 0x1d, 0x0f00 },
2909 { 0x1f, 0x0002 },
2910 { 0x0c, 0x1ec8 },
2911 { 0x1f, 0x0000 }
2912 };
2913
françois romieu4da19632011-01-03 15:07:55 +00002914 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002915}
2916
françois romieu4da19632011-01-03 15:07:55 +00002917static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002918{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002919 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002920 { 0x1f, 0x0001 },
2921 { 0x1d, 0x3d98 },
2922 { 0x1f, 0x0000 }
2923 };
2924
françois romieu4da19632011-01-03 15:07:55 +00002925 rtl_writephy(tp, 0x1f, 0x0000);
2926 rtl_patchphy(tp, 0x14, 1 << 5);
2927 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002928
françois romieu4da19632011-01-03 15:07:55 +00002929 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002930}
2931
françois romieu4da19632011-01-03 15:07:55 +00002932static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002933{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002934 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002935 { 0x1f, 0x0001 },
2936 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002937 { 0x1f, 0x0002 },
2938 { 0x00, 0x88d4 },
2939 { 0x01, 0x82b1 },
2940 { 0x03, 0x7002 },
2941 { 0x08, 0x9e30 },
2942 { 0x09, 0x01f0 },
2943 { 0x0a, 0x5500 },
2944 { 0x0c, 0x00c8 },
2945 { 0x1f, 0x0003 },
2946 { 0x12, 0xc096 },
2947 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002948 { 0x1f, 0x0000 },
2949 { 0x1f, 0x0000 },
2950 { 0x09, 0x2000 },
2951 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002952 };
2953
françois romieu4da19632011-01-03 15:07:55 +00002954 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002955
françois romieu4da19632011-01-03 15:07:55 +00002956 rtl_patchphy(tp, 0x14, 1 << 5);
2957 rtl_patchphy(tp, 0x0d, 1 << 5);
2958 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002959}
2960
françois romieu4da19632011-01-03 15:07:55 +00002961static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002962{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002963 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002964 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002965 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002966 { 0x03, 0x802f },
2967 { 0x02, 0x4f02 },
2968 { 0x01, 0x0409 },
2969 { 0x00, 0xf099 },
2970 { 0x04, 0x9800 },
2971 { 0x04, 0x9000 },
2972 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002973 { 0x1f, 0x0002 },
2974 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002975 { 0x06, 0x0761 },
2976 { 0x1f, 0x0003 },
2977 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002978 { 0x1f, 0x0000 }
2979 };
2980
françois romieu4da19632011-01-03 15:07:55 +00002981 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002982
françois romieu4da19632011-01-03 15:07:55 +00002983 rtl_patchphy(tp, 0x16, 1 << 0);
2984 rtl_patchphy(tp, 0x14, 1 << 5);
2985 rtl_patchphy(tp, 0x0d, 1 << 5);
2986 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002987}
2988
françois romieu4da19632011-01-03 15:07:55 +00002989static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002990{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002991 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002992 { 0x1f, 0x0001 },
2993 { 0x12, 0x2300 },
2994 { 0x1d, 0x3d98 },
2995 { 0x1f, 0x0002 },
2996 { 0x0c, 0x7eb8 },
2997 { 0x06, 0x5461 },
2998 { 0x1f, 0x0003 },
2999 { 0x16, 0x0f0a },
3000 { 0x1f, 0x0000 }
3001 };
3002
françois romieu4da19632011-01-03 15:07:55 +00003003 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02003004
françois romieu4da19632011-01-03 15:07:55 +00003005 rtl_patchphy(tp, 0x16, 1 << 0);
3006 rtl_patchphy(tp, 0x14, 1 << 5);
3007 rtl_patchphy(tp, 0x0d, 1 << 5);
3008 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003009}
3010
françois romieu4da19632011-01-03 15:07:55 +00003011static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003012{
françois romieu4da19632011-01-03 15:07:55 +00003013 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003014}
3015
françois romieubca03d52011-01-03 15:07:31 +00003016static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003017{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003018 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003019 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003020 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003021 { 0x06, 0x4064 },
3022 { 0x07, 0x2863 },
3023 { 0x08, 0x059c },
3024 { 0x09, 0x26b4 },
3025 { 0x0a, 0x6a19 },
3026 { 0x0b, 0xdcc8 },
3027 { 0x10, 0xf06d },
3028 { 0x14, 0x7f68 },
3029 { 0x18, 0x7fd9 },
3030 { 0x1c, 0xf0ff },
3031 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003032 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003033 { 0x12, 0xf49f },
3034 { 0x13, 0x070b },
3035 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003036 { 0x14, 0x94c0 },
3037
3038 /*
3039 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003040 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003041 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003042 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003043 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003044 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003045 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003046 { 0x06, 0x5561 },
3047
3048 /*
3049 * Can not link to 1Gbps with bad cable
3050 * Decrease SNR threshold form 21.07dB to 19.04dB
3051 */
3052 { 0x1f, 0x0001 },
3053 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003054
3055 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003056 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003057 };
3058
françois romieu4da19632011-01-03 15:07:55 +00003059 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003060
françois romieubca03d52011-01-03 15:07:31 +00003061 /*
3062 * Rx Error Issue
3063 * Fine Tune Switching regulator parameter
3064 */
françois romieu4da19632011-01-03 15:07:55 +00003065 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003066 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3067 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003068
Francois Romieufdf6fc02012-07-06 22:40:38 +02003069 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003070 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003071 { 0x1f, 0x0002 },
3072 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003073 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003074 { 0x05, 0x8330 },
3075 { 0x06, 0x669a },
3076 { 0x1f, 0x0002 }
3077 };
3078 int val;
3079
françois romieu4da19632011-01-03 15:07:55 +00003080 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003081
françois romieu4da19632011-01-03 15:07:55 +00003082 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003083
3084 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003085 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003086 0x0065, 0x0066, 0x0067, 0x0068,
3087 0x0069, 0x006a, 0x006b, 0x006c
3088 };
3089 int i;
3090
françois romieu4da19632011-01-03 15:07:55 +00003091 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003092
3093 val &= 0xff00;
3094 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003095 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003096 }
3097 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003098 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003099 { 0x1f, 0x0002 },
3100 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003101 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003102 { 0x05, 0x8330 },
3103 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003104 };
3105
françois romieu4da19632011-01-03 15:07:55 +00003106 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003107 }
3108
françois romieubca03d52011-01-03 15:07:31 +00003109 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003110 rtl_writephy(tp, 0x1f, 0x0002);
3111 rtl_patchphy(tp, 0x0d, 0x0300);
3112 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003113
françois romieubca03d52011-01-03 15:07:31 +00003114 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003115 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003116 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3117 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003118
françois romieu4da19632011-01-03 15:07:55 +00003119 rtl_writephy(tp, 0x1f, 0x0005);
3120 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003121
3122 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003123
françois romieu4da19632011-01-03 15:07:55 +00003124 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003125}
3126
françois romieubca03d52011-01-03 15:07:31 +00003127static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003128{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003129 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003130 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003131 { 0x1f, 0x0001 },
3132 { 0x06, 0x4064 },
3133 { 0x07, 0x2863 },
3134 { 0x08, 0x059c },
3135 { 0x09, 0x26b4 },
3136 { 0x0a, 0x6a19 },
3137 { 0x0b, 0xdcc8 },
3138 { 0x10, 0xf06d },
3139 { 0x14, 0x7f68 },
3140 { 0x18, 0x7fd9 },
3141 { 0x1c, 0xf0ff },
3142 { 0x1d, 0x3d9c },
3143 { 0x1f, 0x0003 },
3144 { 0x12, 0xf49f },
3145 { 0x13, 0x070b },
3146 { 0x1a, 0x05ad },
3147 { 0x14, 0x94c0 },
3148
françois romieubca03d52011-01-03 15:07:31 +00003149 /*
3150 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003151 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003152 */
françois romieudaf9df62009-10-07 12:44:20 +00003153 { 0x1f, 0x0002 },
3154 { 0x06, 0x5561 },
3155 { 0x1f, 0x0005 },
3156 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003157 { 0x06, 0x5561 },
3158
3159 /*
3160 * Can not link to 1Gbps with bad cable
3161 * Decrease SNR threshold form 21.07dB to 19.04dB
3162 */
3163 { 0x1f, 0x0001 },
3164 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003165
3166 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003167 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003168 };
3169
françois romieu4da19632011-01-03 15:07:55 +00003170 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003171
Francois Romieufdf6fc02012-07-06 22:40:38 +02003172 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003173 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003174 { 0x1f, 0x0002 },
3175 { 0x05, 0x669a },
3176 { 0x1f, 0x0005 },
3177 { 0x05, 0x8330 },
3178 { 0x06, 0x669a },
3179
3180 { 0x1f, 0x0002 }
3181 };
3182 int val;
3183
françois romieu4da19632011-01-03 15:07:55 +00003184 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003185
françois romieu4da19632011-01-03 15:07:55 +00003186 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003187 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003188 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003189 0x0065, 0x0066, 0x0067, 0x0068,
3190 0x0069, 0x006a, 0x006b, 0x006c
3191 };
3192 int i;
3193
françois romieu4da19632011-01-03 15:07:55 +00003194 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003195
3196 val &= 0xff00;
3197 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003198 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003199 }
3200 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003201 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003202 { 0x1f, 0x0002 },
3203 { 0x05, 0x2642 },
3204 { 0x1f, 0x0005 },
3205 { 0x05, 0x8330 },
3206 { 0x06, 0x2642 }
3207 };
3208
françois romieu4da19632011-01-03 15:07:55 +00003209 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003210 }
3211
françois romieubca03d52011-01-03 15:07:31 +00003212 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003213 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003214 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3215 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003216
françois romieubca03d52011-01-03 15:07:31 +00003217 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003218 rtl_writephy(tp, 0x1f, 0x0002);
3219 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003220
françois romieu4da19632011-01-03 15:07:55 +00003221 rtl_writephy(tp, 0x1f, 0x0005);
3222 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003223
3224 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003225
françois romieu4da19632011-01-03 15:07:55 +00003226 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003227}
3228
françois romieu4da19632011-01-03 15:07:55 +00003229static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003230{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003231 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003232 { 0x1f, 0x0002 },
3233 { 0x10, 0x0008 },
3234 { 0x0d, 0x006c },
3235
3236 { 0x1f, 0x0000 },
3237 { 0x0d, 0xf880 },
3238
3239 { 0x1f, 0x0001 },
3240 { 0x17, 0x0cc0 },
3241
3242 { 0x1f, 0x0001 },
3243 { 0x0b, 0xa4d8 },
3244 { 0x09, 0x281c },
3245 { 0x07, 0x2883 },
3246 { 0x0a, 0x6b35 },
3247 { 0x1d, 0x3da4 },
3248 { 0x1c, 0xeffd },
3249 { 0x14, 0x7f52 },
3250 { 0x18, 0x7fc6 },
3251 { 0x08, 0x0601 },
3252 { 0x06, 0x4063 },
3253 { 0x10, 0xf074 },
3254 { 0x1f, 0x0003 },
3255 { 0x13, 0x0789 },
3256 { 0x12, 0xf4bd },
3257 { 0x1a, 0x04fd },
3258 { 0x14, 0x84b0 },
3259 { 0x1f, 0x0000 },
3260 { 0x00, 0x9200 },
3261
3262 { 0x1f, 0x0005 },
3263 { 0x01, 0x0340 },
3264 { 0x1f, 0x0001 },
3265 { 0x04, 0x4000 },
3266 { 0x03, 0x1d21 },
3267 { 0x02, 0x0c32 },
3268 { 0x01, 0x0200 },
3269 { 0x00, 0x5554 },
3270 { 0x04, 0x4800 },
3271 { 0x04, 0x4000 },
3272 { 0x04, 0xf000 },
3273 { 0x03, 0xdf01 },
3274 { 0x02, 0xdf20 },
3275 { 0x01, 0x101a },
3276 { 0x00, 0xa0ff },
3277 { 0x04, 0xf800 },
3278 { 0x04, 0xf000 },
3279 { 0x1f, 0x0000 },
3280
3281 { 0x1f, 0x0007 },
3282 { 0x1e, 0x0023 },
3283 { 0x16, 0x0000 },
3284 { 0x1f, 0x0000 }
3285 };
3286
françois romieu4da19632011-01-03 15:07:55 +00003287 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003288}
3289
françois romieue6de30d2011-01-03 15:08:37 +00003290static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3291{
3292 static const struct phy_reg phy_reg_init[] = {
3293 { 0x1f, 0x0001 },
3294 { 0x17, 0x0cc0 },
3295
3296 { 0x1f, 0x0007 },
3297 { 0x1e, 0x002d },
3298 { 0x18, 0x0040 },
3299 { 0x1f, 0x0000 }
3300 };
3301
3302 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3303 rtl_patchphy(tp, 0x0d, 1 << 5);
3304}
3305
Hayes Wang70090422011-07-06 15:58:06 +08003306static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003307{
3308 static const struct phy_reg phy_reg_init[] = {
3309 /* Enable Delay cap */
3310 { 0x1f, 0x0005 },
3311 { 0x05, 0x8b80 },
3312 { 0x06, 0xc896 },
3313 { 0x1f, 0x0000 },
3314
3315 /* Channel estimation fine tune */
3316 { 0x1f, 0x0001 },
3317 { 0x0b, 0x6c20 },
3318 { 0x07, 0x2872 },
3319 { 0x1c, 0xefff },
3320 { 0x1f, 0x0003 },
3321 { 0x14, 0x6420 },
3322 { 0x1f, 0x0000 },
3323
3324 /* Update PFM & 10M TX idle timer */
3325 { 0x1f, 0x0007 },
3326 { 0x1e, 0x002f },
3327 { 0x15, 0x1919 },
3328 { 0x1f, 0x0000 },
3329
3330 { 0x1f, 0x0007 },
3331 { 0x1e, 0x00ac },
3332 { 0x18, 0x0006 },
3333 { 0x1f, 0x0000 }
3334 };
3335
Francois Romieu15ecd032011-04-27 13:52:22 -07003336 rtl_apply_firmware(tp);
3337
hayeswang01dc7fe2011-03-21 01:50:28 +00003338 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3339
3340 /* DCO enable for 10M IDLE Power */
3341 rtl_writephy(tp, 0x1f, 0x0007);
3342 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003343 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003344 rtl_writephy(tp, 0x1f, 0x0000);
3345
3346 /* For impedance matching */
3347 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003348 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003349 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003350
3351 /* PHY auto speed down */
3352 rtl_writephy(tp, 0x1f, 0x0007);
3353 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003354 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003355 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003356 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003357
3358 rtl_writephy(tp, 0x1f, 0x0005);
3359 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003360 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003361 rtl_writephy(tp, 0x1f, 0x0000);
3362
3363 rtl_writephy(tp, 0x1f, 0x0005);
3364 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003365 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003366 rtl_writephy(tp, 0x1f, 0x0007);
3367 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003368 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003369 rtl_writephy(tp, 0x1f, 0x0006);
3370 rtl_writephy(tp, 0x00, 0x5a00);
3371 rtl_writephy(tp, 0x1f, 0x0000);
3372 rtl_writephy(tp, 0x0d, 0x0007);
3373 rtl_writephy(tp, 0x0e, 0x003c);
3374 rtl_writephy(tp, 0x0d, 0x4007);
3375 rtl_writephy(tp, 0x0e, 0x0000);
3376 rtl_writephy(tp, 0x0d, 0x0000);
3377}
3378
françois romieu9ecb9aa2012-12-07 11:20:21 +00003379static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3380{
3381 const u16 w[] = {
3382 addr[0] | (addr[1] << 8),
3383 addr[2] | (addr[3] << 8),
3384 addr[4] | (addr[5] << 8)
3385 };
3386 const struct exgmac_reg e[] = {
3387 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3388 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3389 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3390 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3391 };
3392
3393 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3394}
3395
Hayes Wang70090422011-07-06 15:58:06 +08003396static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3397{
3398 static const struct phy_reg phy_reg_init[] = {
3399 /* Enable Delay cap */
3400 { 0x1f, 0x0004 },
3401 { 0x1f, 0x0007 },
3402 { 0x1e, 0x00ac },
3403 { 0x18, 0x0006 },
3404 { 0x1f, 0x0002 },
3405 { 0x1f, 0x0000 },
3406 { 0x1f, 0x0000 },
3407
3408 /* Channel estimation fine tune */
3409 { 0x1f, 0x0003 },
3410 { 0x09, 0xa20f },
3411 { 0x1f, 0x0000 },
3412 { 0x1f, 0x0000 },
3413
3414 /* Green Setting */
3415 { 0x1f, 0x0005 },
3416 { 0x05, 0x8b5b },
3417 { 0x06, 0x9222 },
3418 { 0x05, 0x8b6d },
3419 { 0x06, 0x8000 },
3420 { 0x05, 0x8b76 },
3421 { 0x06, 0x8000 },
3422 { 0x1f, 0x0000 }
3423 };
3424
3425 rtl_apply_firmware(tp);
3426
3427 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3428
3429 /* For 4-corner performance improve */
3430 rtl_writephy(tp, 0x1f, 0x0005);
3431 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003432 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003433 rtl_writephy(tp, 0x1f, 0x0000);
3434
3435 /* PHY auto speed down */
3436 rtl_writephy(tp, 0x1f, 0x0004);
3437 rtl_writephy(tp, 0x1f, 0x0007);
3438 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003439 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003440 rtl_writephy(tp, 0x1f, 0x0002);
3441 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003442 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003443
3444 /* improve 10M EEE waveform */
3445 rtl_writephy(tp, 0x1f, 0x0005);
3446 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003447 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003448 rtl_writephy(tp, 0x1f, 0x0000);
3449
3450 /* Improve 2-pair detection performance */
3451 rtl_writephy(tp, 0x1f, 0x0005);
3452 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003453 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003454 rtl_writephy(tp, 0x1f, 0x0000);
3455
3456 /* EEE setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003457 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003458 rtl_writephy(tp, 0x1f, 0x0005);
3459 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003460 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wang70090422011-07-06 15:58:06 +08003461 rtl_writephy(tp, 0x1f, 0x0004);
3462 rtl_writephy(tp, 0x1f, 0x0007);
3463 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003464 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003465 rtl_writephy(tp, 0x1f, 0x0002);
3466 rtl_writephy(tp, 0x1f, 0x0000);
3467 rtl_writephy(tp, 0x0d, 0x0007);
3468 rtl_writephy(tp, 0x0e, 0x003c);
3469 rtl_writephy(tp, 0x0d, 0x4007);
3470 rtl_writephy(tp, 0x0e, 0x0000);
3471 rtl_writephy(tp, 0x0d, 0x0000);
3472
3473 /* Green feature */
3474 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003475 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3476 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wang70090422011-07-06 15:58:06 +08003477 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003478
françois romieu9ecb9aa2012-12-07 11:20:21 +00003479 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3480 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003481}
3482
Hayes Wang5f886e02012-03-30 14:33:03 +08003483static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3484{
3485 /* For 4-corner performance improve */
3486 rtl_writephy(tp, 0x1f, 0x0005);
3487 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003488 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003489 rtl_writephy(tp, 0x1f, 0x0000);
3490
3491 /* PHY auto speed down */
3492 rtl_writephy(tp, 0x1f, 0x0007);
3493 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003494 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003495 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003496 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003497
3498 /* Improve 10M EEE waveform */
3499 rtl_writephy(tp, 0x1f, 0x0005);
3500 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003501 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003502 rtl_writephy(tp, 0x1f, 0x0000);
3503}
3504
Hayes Wangc2218922011-09-06 16:55:18 +08003505static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3506{
3507 static const struct phy_reg phy_reg_init[] = {
3508 /* Channel estimation fine tune */
3509 { 0x1f, 0x0003 },
3510 { 0x09, 0xa20f },
3511 { 0x1f, 0x0000 },
3512
3513 /* Modify green table for giga & fnet */
3514 { 0x1f, 0x0005 },
3515 { 0x05, 0x8b55 },
3516 { 0x06, 0x0000 },
3517 { 0x05, 0x8b5e },
3518 { 0x06, 0x0000 },
3519 { 0x05, 0x8b67 },
3520 { 0x06, 0x0000 },
3521 { 0x05, 0x8b70 },
3522 { 0x06, 0x0000 },
3523 { 0x1f, 0x0000 },
3524 { 0x1f, 0x0007 },
3525 { 0x1e, 0x0078 },
3526 { 0x17, 0x0000 },
3527 { 0x19, 0x00fb },
3528 { 0x1f, 0x0000 },
3529
3530 /* Modify green table for 10M */
3531 { 0x1f, 0x0005 },
3532 { 0x05, 0x8b79 },
3533 { 0x06, 0xaa00 },
3534 { 0x1f, 0x0000 },
3535
3536 /* Disable hiimpedance detection (RTCT) */
3537 { 0x1f, 0x0003 },
3538 { 0x01, 0x328a },
3539 { 0x1f, 0x0000 }
3540 };
3541
3542 rtl_apply_firmware(tp);
3543
3544 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3545
Hayes Wang5f886e02012-03-30 14:33:03 +08003546 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003547
3548 /* Improve 2-pair detection performance */
3549 rtl_writephy(tp, 0x1f, 0x0005);
3550 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003551 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003552 rtl_writephy(tp, 0x1f, 0x0000);
3553}
3554
3555static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3556{
3557 rtl_apply_firmware(tp);
3558
Hayes Wang5f886e02012-03-30 14:33:03 +08003559 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003560}
3561
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003562static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3563{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003564 static const struct phy_reg phy_reg_init[] = {
3565 /* Channel estimation fine tune */
3566 { 0x1f, 0x0003 },
3567 { 0x09, 0xa20f },
3568 { 0x1f, 0x0000 },
3569
3570 /* Modify green table for giga & fnet */
3571 { 0x1f, 0x0005 },
3572 { 0x05, 0x8b55 },
3573 { 0x06, 0x0000 },
3574 { 0x05, 0x8b5e },
3575 { 0x06, 0x0000 },
3576 { 0x05, 0x8b67 },
3577 { 0x06, 0x0000 },
3578 { 0x05, 0x8b70 },
3579 { 0x06, 0x0000 },
3580 { 0x1f, 0x0000 },
3581 { 0x1f, 0x0007 },
3582 { 0x1e, 0x0078 },
3583 { 0x17, 0x0000 },
3584 { 0x19, 0x00aa },
3585 { 0x1f, 0x0000 },
3586
3587 /* Modify green table for 10M */
3588 { 0x1f, 0x0005 },
3589 { 0x05, 0x8b79 },
3590 { 0x06, 0xaa00 },
3591 { 0x1f, 0x0000 },
3592
3593 /* Disable hiimpedance detection (RTCT) */
3594 { 0x1f, 0x0003 },
3595 { 0x01, 0x328a },
3596 { 0x1f, 0x0000 }
3597 };
3598
3599
3600 rtl_apply_firmware(tp);
3601
3602 rtl8168f_hw_phy_config(tp);
3603
3604 /* Improve 2-pair detection performance */
3605 rtl_writephy(tp, 0x1f, 0x0005);
3606 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003607 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003608 rtl_writephy(tp, 0x1f, 0x0000);
3609
3610 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3611
3612 /* Modify green table for giga */
3613 rtl_writephy(tp, 0x1f, 0x0005);
3614 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003615 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003616 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003617 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003618 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003619 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003620 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003621 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003622 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003623 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003624 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003625 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003626 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003627 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003628 rtl_writephy(tp, 0x1f, 0x0000);
3629
3630 /* uc same-seed solution */
3631 rtl_writephy(tp, 0x1f, 0x0005);
3632 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003633 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003634 rtl_writephy(tp, 0x1f, 0x0000);
3635
3636 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003637 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003638 rtl_writephy(tp, 0x1f, 0x0005);
3639 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003640 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003641 rtl_writephy(tp, 0x1f, 0x0004);
3642 rtl_writephy(tp, 0x1f, 0x0007);
3643 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003644 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003645 rtl_writephy(tp, 0x1f, 0x0000);
3646 rtl_writephy(tp, 0x0d, 0x0007);
3647 rtl_writephy(tp, 0x0e, 0x003c);
3648 rtl_writephy(tp, 0x0d, 0x4007);
3649 rtl_writephy(tp, 0x0e, 0x0000);
3650 rtl_writephy(tp, 0x0d, 0x0000);
3651
3652 /* Green feature */
3653 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003654 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3655 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003656 rtl_writephy(tp, 0x1f, 0x0000);
3657}
3658
Hayes Wangc5583862012-07-02 17:23:22 +08003659static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3660{
Hayes Wangc5583862012-07-02 17:23:22 +08003661 rtl_apply_firmware(tp);
3662
hayeswang41f44d12013-04-01 22:23:36 +00003663 rtl_writephy(tp, 0x1f, 0x0a46);
3664 if (rtl_readphy(tp, 0x10) & 0x0100) {
3665 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003666 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003667 } else {
3668 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003669 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003670 }
Hayes Wangc5583862012-07-02 17:23:22 +08003671
hayeswang41f44d12013-04-01 22:23:36 +00003672 rtl_writephy(tp, 0x1f, 0x0a46);
3673 if (rtl_readphy(tp, 0x13) & 0x0100) {
3674 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003675 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003676 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003677 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003678 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003679 }
Hayes Wangc5583862012-07-02 17:23:22 +08003680
hayeswang41f44d12013-04-01 22:23:36 +00003681 /* Enable PHY auto speed down */
3682 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003683 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003684
hayeswangfe7524c2013-04-01 22:23:37 +00003685 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003686 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003687 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003688 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003689 rtl_writephy(tp, 0x1f, 0x0a43);
3690 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003691 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3692 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003693
hayeswang41f44d12013-04-01 22:23:36 +00003694 /* EEE auto-fallback function */
3695 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003696 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003697
hayeswang41f44d12013-04-01 22:23:36 +00003698 /* Enable UC LPF tune function */
3699 rtl_writephy(tp, 0x1f, 0x0a43);
3700 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003701 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003702
3703 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003704 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003705
hayeswangfe7524c2013-04-01 22:23:37 +00003706 /* Improve SWR Efficiency */
3707 rtl_writephy(tp, 0x1f, 0x0bcd);
3708 rtl_writephy(tp, 0x14, 0x5065);
3709 rtl_writephy(tp, 0x14, 0xd065);
3710 rtl_writephy(tp, 0x1f, 0x0bc8);
3711 rtl_writephy(tp, 0x11, 0x5655);
3712 rtl_writephy(tp, 0x1f, 0x0bcd);
3713 rtl_writephy(tp, 0x14, 0x1065);
3714 rtl_writephy(tp, 0x14, 0x9065);
3715 rtl_writephy(tp, 0x14, 0x1065);
3716
David Chang1bac1072013-11-27 15:48:36 +08003717 /* Check ALDPS bit, disable it if enabled */
3718 rtl_writephy(tp, 0x1f, 0x0a43);
3719 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003720 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08003721
hayeswang41f44d12013-04-01 22:23:36 +00003722 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003723}
3724
hayeswang57538c42013-04-01 22:23:40 +00003725static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3726{
3727 rtl_apply_firmware(tp);
3728}
3729
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003730static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3731{
3732 u16 dout_tapbin;
3733 u32 data;
3734
3735 rtl_apply_firmware(tp);
3736
3737 /* CHN EST parameters adjust - giga master */
3738 rtl_writephy(tp, 0x1f, 0x0a43);
3739 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003740 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003741 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003742 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003743 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003744 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003745 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003746 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003747 rtl_writephy(tp, 0x1f, 0x0000);
3748
3749 /* CHN EST parameters adjust - giga slave */
3750 rtl_writephy(tp, 0x1f, 0x0a43);
3751 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003752 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003753 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003754 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003755 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003756 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003757 rtl_writephy(tp, 0x1f, 0x0000);
3758
3759 /* CHN EST parameters adjust - fnet */
3760 rtl_writephy(tp, 0x1f, 0x0a43);
3761 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003762 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003763 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003764 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003765 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003766 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003767 rtl_writephy(tp, 0x1f, 0x0000);
3768
3769 /* enable R-tune & PGA-retune function */
3770 dout_tapbin = 0;
3771 rtl_writephy(tp, 0x1f, 0x0a46);
3772 data = rtl_readphy(tp, 0x13);
3773 data &= 3;
3774 data <<= 2;
3775 dout_tapbin |= data;
3776 data = rtl_readphy(tp, 0x12);
3777 data &= 0xc000;
3778 data >>= 14;
3779 dout_tapbin |= data;
3780 dout_tapbin = ~(dout_tapbin^0x08);
3781 dout_tapbin <<= 12;
3782 dout_tapbin &= 0xf000;
3783 rtl_writephy(tp, 0x1f, 0x0a43);
3784 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003785 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003786 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003787 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003788 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003789 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003790 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003791 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003792
3793 rtl_writephy(tp, 0x1f, 0x0a43);
3794 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003795 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003796 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003797 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003798 rtl_writephy(tp, 0x1f, 0x0000);
3799
3800 /* enable GPHY 10M */
3801 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003802 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003803 rtl_writephy(tp, 0x1f, 0x0000);
3804
3805 /* SAR ADC performance */
3806 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003807 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003808 rtl_writephy(tp, 0x1f, 0x0000);
3809
3810 rtl_writephy(tp, 0x1f, 0x0a43);
3811 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003812 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003813 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003814 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003815 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003816 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003817 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003818 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003819 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003820 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003821 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003822 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003823 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003824 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003825 rtl_writephy(tp, 0x1f, 0x0000);
3826
3827 /* disable phy pfm mode */
3828 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003829 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003830 rtl_writephy(tp, 0x1f, 0x0000);
3831
3832 /* Check ALDPS bit, disable it if enabled */
3833 rtl_writephy(tp, 0x1f, 0x0a43);
3834 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003835 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003836
3837 rtl_writephy(tp, 0x1f, 0x0000);
3838}
3839
3840static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3841{
3842 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3843 u16 rlen;
3844 u32 data;
3845
3846 rtl_apply_firmware(tp);
3847
3848 /* CHIN EST parameter update */
3849 rtl_writephy(tp, 0x1f, 0x0a43);
3850 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003851 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003852 rtl_writephy(tp, 0x1f, 0x0000);
3853
3854 /* enable R-tune & PGA-retune function */
3855 rtl_writephy(tp, 0x1f, 0x0a43);
3856 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003857 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003858 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003859 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003860 rtl_writephy(tp, 0x1f, 0x0000);
3861
3862 /* enable GPHY 10M */
3863 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003864 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003865 rtl_writephy(tp, 0x1f, 0x0000);
3866
3867 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3868 data = r8168_mac_ocp_read(tp, 0xdd02);
3869 ioffset_p3 = ((data & 0x80)>>7);
3870 ioffset_p3 <<= 3;
3871
3872 data = r8168_mac_ocp_read(tp, 0xdd00);
3873 ioffset_p3 |= ((data & (0xe000))>>13);
3874 ioffset_p2 = ((data & (0x1e00))>>9);
3875 ioffset_p1 = ((data & (0x01e0))>>5);
3876 ioffset_p0 = ((data & 0x0010)>>4);
3877 ioffset_p0 <<= 3;
3878 ioffset_p0 |= (data & (0x07));
3879 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3880
Chun-Hao Lin05b96872014-10-01 23:17:12 +08003881 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
3882 (ioffset_p1 != 0x0f) || (ioffset_p0 == 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003883 rtl_writephy(tp, 0x1f, 0x0bcf);
3884 rtl_writephy(tp, 0x16, data);
3885 rtl_writephy(tp, 0x1f, 0x0000);
3886 }
3887
3888 /* Modify rlen (TX LPF corner frequency) level */
3889 rtl_writephy(tp, 0x1f, 0x0bcd);
3890 data = rtl_readphy(tp, 0x16);
3891 data &= 0x000f;
3892 rlen = 0;
3893 if (data > 3)
3894 rlen = data - 3;
3895 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3896 rtl_writephy(tp, 0x17, data);
3897 rtl_writephy(tp, 0x1f, 0x0bcd);
3898 rtl_writephy(tp, 0x1f, 0x0000);
3899
3900 /* disable phy pfm mode */
3901 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003902 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003903 rtl_writephy(tp, 0x1f, 0x0000);
3904
3905 /* Check ALDPS bit, disable it if enabled */
3906 rtl_writephy(tp, 0x1f, 0x0a43);
3907 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003908 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003909
3910 rtl_writephy(tp, 0x1f, 0x0000);
3911}
3912
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003913static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3914{
3915 /* Enable PHY auto speed down */
3916 rtl_writephy(tp, 0x1f, 0x0a44);
3917 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3918 rtl_writephy(tp, 0x1f, 0x0000);
3919
3920 /* patch 10M & ALDPS */
3921 rtl_writephy(tp, 0x1f, 0x0bcc);
3922 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3923 rtl_writephy(tp, 0x1f, 0x0a44);
3924 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3925 rtl_writephy(tp, 0x1f, 0x0a43);
3926 rtl_writephy(tp, 0x13, 0x8084);
3927 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3928 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3929 rtl_writephy(tp, 0x1f, 0x0000);
3930
3931 /* Enable EEE auto-fallback function */
3932 rtl_writephy(tp, 0x1f, 0x0a4b);
3933 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3934 rtl_writephy(tp, 0x1f, 0x0000);
3935
3936 /* Enable UC LPF tune function */
3937 rtl_writephy(tp, 0x1f, 0x0a43);
3938 rtl_writephy(tp, 0x13, 0x8012);
3939 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3940 rtl_writephy(tp, 0x1f, 0x0000);
3941
3942 /* set rg_sel_sdm_rate */
3943 rtl_writephy(tp, 0x1f, 0x0c42);
3944 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3945 rtl_writephy(tp, 0x1f, 0x0000);
3946
3947 /* Check ALDPS bit, disable it if enabled */
3948 rtl_writephy(tp, 0x1f, 0x0a43);
3949 if (rtl_readphy(tp, 0x10) & 0x0004)
3950 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3951
3952 rtl_writephy(tp, 0x1f, 0x0000);
3953}
3954
3955static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3956{
3957 /* patch 10M & ALDPS */
3958 rtl_writephy(tp, 0x1f, 0x0bcc);
3959 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3960 rtl_writephy(tp, 0x1f, 0x0a44);
3961 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3962 rtl_writephy(tp, 0x1f, 0x0a43);
3963 rtl_writephy(tp, 0x13, 0x8084);
3964 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3965 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3966 rtl_writephy(tp, 0x1f, 0x0000);
3967
3968 /* Enable UC LPF tune function */
3969 rtl_writephy(tp, 0x1f, 0x0a43);
3970 rtl_writephy(tp, 0x13, 0x8012);
3971 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3972 rtl_writephy(tp, 0x1f, 0x0000);
3973
3974 /* Set rg_sel_sdm_rate */
3975 rtl_writephy(tp, 0x1f, 0x0c42);
3976 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3977 rtl_writephy(tp, 0x1f, 0x0000);
3978
3979 /* Channel estimation parameters */
3980 rtl_writephy(tp, 0x1f, 0x0a43);
3981 rtl_writephy(tp, 0x13, 0x80f3);
3982 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
3983 rtl_writephy(tp, 0x13, 0x80f0);
3984 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
3985 rtl_writephy(tp, 0x13, 0x80ef);
3986 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
3987 rtl_writephy(tp, 0x13, 0x80f6);
3988 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
3989 rtl_writephy(tp, 0x13, 0x80ec);
3990 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
3991 rtl_writephy(tp, 0x13, 0x80ed);
3992 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3993 rtl_writephy(tp, 0x13, 0x80f2);
3994 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
3995 rtl_writephy(tp, 0x13, 0x80f4);
3996 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
3997 rtl_writephy(tp, 0x1f, 0x0a43);
3998 rtl_writephy(tp, 0x13, 0x8110);
3999 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4000 rtl_writephy(tp, 0x13, 0x810f);
4001 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4002 rtl_writephy(tp, 0x13, 0x8111);
4003 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4004 rtl_writephy(tp, 0x13, 0x8113);
4005 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4006 rtl_writephy(tp, 0x13, 0x8115);
4007 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4008 rtl_writephy(tp, 0x13, 0x810e);
4009 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4010 rtl_writephy(tp, 0x13, 0x810c);
4011 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4012 rtl_writephy(tp, 0x13, 0x810b);
4013 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4014 rtl_writephy(tp, 0x1f, 0x0a43);
4015 rtl_writephy(tp, 0x13, 0x80d1);
4016 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4017 rtl_writephy(tp, 0x13, 0x80cd);
4018 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4019 rtl_writephy(tp, 0x13, 0x80d3);
4020 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4021 rtl_writephy(tp, 0x13, 0x80d5);
4022 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4023 rtl_writephy(tp, 0x13, 0x80d7);
4024 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4025
4026 /* Force PWM-mode */
4027 rtl_writephy(tp, 0x1f, 0x0bcd);
4028 rtl_writephy(tp, 0x14, 0x5065);
4029 rtl_writephy(tp, 0x14, 0xd065);
4030 rtl_writephy(tp, 0x1f, 0x0bc8);
4031 rtl_writephy(tp, 0x12, 0x00ed);
4032 rtl_writephy(tp, 0x1f, 0x0bcd);
4033 rtl_writephy(tp, 0x14, 0x1065);
4034 rtl_writephy(tp, 0x14, 0x9065);
4035 rtl_writephy(tp, 0x14, 0x1065);
4036 rtl_writephy(tp, 0x1f, 0x0000);
4037
4038 /* Check ALDPS bit, disable it if enabled */
4039 rtl_writephy(tp, 0x1f, 0x0a43);
4040 if (rtl_readphy(tp, 0x10) & 0x0004)
4041 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4042
4043 rtl_writephy(tp, 0x1f, 0x0000);
4044}
4045
françois romieu4da19632011-01-03 15:07:55 +00004046static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004047{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004048 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004049 { 0x1f, 0x0003 },
4050 { 0x08, 0x441d },
4051 { 0x01, 0x9100 },
4052 { 0x1f, 0x0000 }
4053 };
4054
françois romieu4da19632011-01-03 15:07:55 +00004055 rtl_writephy(tp, 0x1f, 0x0000);
4056 rtl_patchphy(tp, 0x11, 1 << 12);
4057 rtl_patchphy(tp, 0x19, 1 << 13);
4058 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004059
françois romieu4da19632011-01-03 15:07:55 +00004060 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004061}
4062
Hayes Wang5a5e4442011-02-22 17:26:21 +08004063static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4064{
4065 static const struct phy_reg phy_reg_init[] = {
4066 { 0x1f, 0x0005 },
4067 { 0x1a, 0x0000 },
4068 { 0x1f, 0x0000 },
4069
4070 { 0x1f, 0x0004 },
4071 { 0x1c, 0x0000 },
4072 { 0x1f, 0x0000 },
4073
4074 { 0x1f, 0x0001 },
4075 { 0x15, 0x7701 },
4076 { 0x1f, 0x0000 }
4077 };
4078
4079 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004080 rtl_writephy(tp, 0x1f, 0x0000);
4081 rtl_writephy(tp, 0x18, 0x0310);
4082 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004083
François Romieu953a12c2011-04-24 17:38:48 +02004084 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004085
4086 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4087}
4088
Hayes Wang7e18dca2012-03-30 14:33:02 +08004089static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4090{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004091 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004092 rtl_writephy(tp, 0x1f, 0x0000);
4093 rtl_writephy(tp, 0x18, 0x0310);
4094 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004095
4096 rtl_apply_firmware(tp);
4097
4098 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004099 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004100 rtl_writephy(tp, 0x1f, 0x0004);
4101 rtl_writephy(tp, 0x10, 0x401f);
4102 rtl_writephy(tp, 0x19, 0x7030);
4103 rtl_writephy(tp, 0x1f, 0x0000);
4104}
4105
Hayes Wang5598bfe2012-07-02 17:23:21 +08004106static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4107{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004108 static const struct phy_reg phy_reg_init[] = {
4109 { 0x1f, 0x0004 },
4110 { 0x10, 0xc07f },
4111 { 0x19, 0x7030 },
4112 { 0x1f, 0x0000 }
4113 };
4114
4115 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004116 rtl_writephy(tp, 0x1f, 0x0000);
4117 rtl_writephy(tp, 0x18, 0x0310);
4118 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004119
4120 rtl_apply_firmware(tp);
4121
Francois Romieufdf6fc02012-07-06 22:40:38 +02004122 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004123 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4124
Francois Romieufdf6fc02012-07-06 22:40:38 +02004125 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004126}
4127
Francois Romieu5615d9f2007-08-17 17:50:46 +02004128static void rtl_hw_phy_config(struct net_device *dev)
4129{
4130 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004131
4132 rtl8169_print_mac_version(tp);
4133
4134 switch (tp->mac_version) {
4135 case RTL_GIGA_MAC_VER_01:
4136 break;
4137 case RTL_GIGA_MAC_VER_02:
4138 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004139 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004140 break;
4141 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004142 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004143 break;
françois romieu2e9558562009-08-10 19:44:19 +00004144 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004145 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004146 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004147 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004148 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004149 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004150 case RTL_GIGA_MAC_VER_07:
4151 case RTL_GIGA_MAC_VER_08:
4152 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004153 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004154 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004155 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004156 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004157 break;
4158 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004159 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004160 break;
4161 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004162 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004163 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004164 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004165 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004166 break;
4167 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004168 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004169 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004170 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004171 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004172 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004173 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004174 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004175 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004176 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004177 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004178 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004179 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004180 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004181 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004182 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004183 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004184 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004185 break;
4186 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004187 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004188 break;
4189 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004190 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004191 break;
françois romieue6de30d2011-01-03 15:08:37 +00004192 case RTL_GIGA_MAC_VER_28:
4193 rtl8168d_4_hw_phy_config(tp);
4194 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004195 case RTL_GIGA_MAC_VER_29:
4196 case RTL_GIGA_MAC_VER_30:
4197 rtl8105e_hw_phy_config(tp);
4198 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004199 case RTL_GIGA_MAC_VER_31:
4200 /* None. */
4201 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004202 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004203 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004204 rtl8168e_1_hw_phy_config(tp);
4205 break;
4206 case RTL_GIGA_MAC_VER_34:
4207 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004208 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004209 case RTL_GIGA_MAC_VER_35:
4210 rtl8168f_1_hw_phy_config(tp);
4211 break;
4212 case RTL_GIGA_MAC_VER_36:
4213 rtl8168f_2_hw_phy_config(tp);
4214 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004215
Hayes Wang7e18dca2012-03-30 14:33:02 +08004216 case RTL_GIGA_MAC_VER_37:
4217 rtl8402_hw_phy_config(tp);
4218 break;
4219
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004220 case RTL_GIGA_MAC_VER_38:
4221 rtl8411_hw_phy_config(tp);
4222 break;
4223
Hayes Wang5598bfe2012-07-02 17:23:21 +08004224 case RTL_GIGA_MAC_VER_39:
4225 rtl8106e_hw_phy_config(tp);
4226 break;
4227
Hayes Wangc5583862012-07-02 17:23:22 +08004228 case RTL_GIGA_MAC_VER_40:
4229 rtl8168g_1_hw_phy_config(tp);
4230 break;
hayeswang57538c42013-04-01 22:23:40 +00004231 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004232 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004233 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004234 rtl8168g_2_hw_phy_config(tp);
4235 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004236 case RTL_GIGA_MAC_VER_45:
4237 case RTL_GIGA_MAC_VER_47:
4238 rtl8168h_1_hw_phy_config(tp);
4239 break;
4240 case RTL_GIGA_MAC_VER_46:
4241 case RTL_GIGA_MAC_VER_48:
4242 rtl8168h_2_hw_phy_config(tp);
4243 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004244
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004245 case RTL_GIGA_MAC_VER_49:
4246 rtl8168ep_1_hw_phy_config(tp);
4247 break;
4248 case RTL_GIGA_MAC_VER_50:
4249 case RTL_GIGA_MAC_VER_51:
4250 rtl8168ep_2_hw_phy_config(tp);
4251 break;
4252
Hayes Wangc5583862012-07-02 17:23:22 +08004253 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004254 default:
4255 break;
4256 }
4257}
4258
Francois Romieuda78dbf2012-01-26 14:18:23 +01004259static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 struct timer_list *timer = &tp->timer;
4262 void __iomem *ioaddr = tp->mmio_addr;
4263 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4264
Francois Romieubcf0bf92006-07-26 23:14:13 +02004265 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
françois romieu4da19632011-01-03 15:07:55 +00004267 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02004268 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 * A busy loop could burn quite a few cycles on nowadays CPU.
4270 * Let's delay the execution of the timer for a few ticks.
4271 */
4272 timeout = HZ/10;
4273 goto out_mod_timer;
4274 }
4275
4276 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01004277 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02004279 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
françois romieu4da19632011-01-03 15:07:55 +00004281 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
4283out_mod_timer:
4284 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004285}
4286
4287static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4288{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004289 if (!test_and_set_bit(flag, tp->wk.flags))
4290 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004291}
4292
4293static void rtl8169_phy_timer(unsigned long __opaque)
4294{
4295 struct net_device *dev = (struct net_device *)__opaque;
4296 struct rtl8169_private *tp = netdev_priv(dev);
4297
Francois Romieu98ddf982012-01-31 10:47:34 +01004298 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299}
4300
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
4302 void __iomem *ioaddr)
4303{
4304 iounmap(ioaddr);
4305 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00004306 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 pci_disable_device(pdev);
4308 free_netdev(dev);
4309}
4310
Francois Romieuffc46952012-07-06 14:19:23 +02004311DECLARE_RTL_COND(rtl_phy_reset_cond)
4312{
4313 return tp->phy_reset_pending(tp);
4314}
4315
Francois Romieubf793292006-11-01 00:53:05 +01004316static void rtl8169_phy_reset(struct net_device *dev,
4317 struct rtl8169_private *tp)
4318{
françois romieu4da19632011-01-03 15:07:55 +00004319 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004320 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004321}
4322
David S. Miller8decf862011-09-22 03:23:13 -04004323static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4324{
4325 void __iomem *ioaddr = tp->mmio_addr;
4326
4327 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4328 (RTL_R8(PHYstatus) & TBI_Enable);
4329}
4330
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004331static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004333 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004334
Francois Romieu5615d9f2007-08-17 17:50:46 +02004335 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004336
Marcus Sundberg773328942008-07-10 21:28:08 +02004337 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4338 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4339 RTL_W8(0x82, 0x01);
4340 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004341
Francois Romieu6dccd162007-02-13 23:38:05 +01004342 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4343
4344 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4345 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004346
Francois Romieubcf0bf92006-07-26 23:14:13 +02004347 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004348 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4349 RTL_W8(0x82, 0x01);
4350 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004351 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004352 }
4353
Francois Romieubf793292006-11-01 00:53:05 +01004354 rtl8169_phy_reset(dev, tp);
4355
Oliver Neukum54405cd2011-01-06 21:55:13 +01004356 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004357 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4358 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4359 (tp->mii.supports_gmii ?
4360 ADVERTISED_1000baseT_Half |
4361 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004362
David S. Miller8decf862011-09-22 03:23:13 -04004363 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00004364 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004365}
4366
Francois Romieu773d2022007-01-31 23:47:43 +01004367static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4368{
4369 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu773d2022007-01-31 23:47:43 +01004370
Francois Romieuda78dbf2012-01-26 14:18:23 +01004371 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004372
4373 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00004374
françois romieu9ecb9aa2012-12-07 11:20:21 +00004375 RTL_W32(MAC4, addr[4] | addr[5] << 8);
françois romieu908ba2b2010-04-26 11:42:58 +00004376 RTL_R32(MAC4);
4377
françois romieu9ecb9aa2012-12-07 11:20:21 +00004378 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
françois romieu908ba2b2010-04-26 11:42:58 +00004379 RTL_R32(MAC0);
4380
françois romieu9ecb9aa2012-12-07 11:20:21 +00004381 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4382 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004383
Francois Romieu773d2022007-01-31 23:47:43 +01004384 RTL_W8(Cfg9346, Cfg9346_Lock);
4385
Francois Romieuda78dbf2012-01-26 14:18:23 +01004386 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004387}
4388
4389static int rtl_set_mac_address(struct net_device *dev, void *p)
4390{
4391 struct rtl8169_private *tp = netdev_priv(dev);
4392 struct sockaddr *addr = p;
4393
4394 if (!is_valid_ether_addr(addr->sa_data))
4395 return -EADDRNOTAVAIL;
4396
4397 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
4398
4399 rtl_rar_set(tp, dev->dev_addr);
4400
4401 return 0;
4402}
4403
Francois Romieu5f787a12006-08-17 13:02:36 +02004404static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4405{
4406 struct rtl8169_private *tp = netdev_priv(dev);
4407 struct mii_ioctl_data *data = if_mii(ifr);
4408
Francois Romieu8b4ab282008-11-19 22:05:25 -08004409 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4410}
Francois Romieu5f787a12006-08-17 13:02:36 +02004411
Francois Romieucecb5fd2011-04-01 10:21:07 +02004412static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4413 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004414{
Francois Romieu5f787a12006-08-17 13:02:36 +02004415 switch (cmd) {
4416 case SIOCGMIIPHY:
4417 data->phy_id = 32; /* Internal PHY */
4418 return 0;
4419
4420 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004421 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004422 return 0;
4423
4424 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004425 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004426 return 0;
4427 }
4428 return -EOPNOTSUPP;
4429}
4430
Francois Romieu8b4ab282008-11-19 22:05:25 -08004431static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4432{
4433 return -EOPNOTSUPP;
4434}
4435
Francois Romieufbac58f2007-10-04 22:51:38 +02004436static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
4437{
4438 if (tp->features & RTL_FEATURE_MSI) {
4439 pci_disable_msi(pdev);
4440 tp->features &= ~RTL_FEATURE_MSI;
4441 }
4442}
4443
Bill Pembertonbaf63292012-12-03 09:23:28 -05004444static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004445{
4446 struct mdio_ops *ops = &tp->mdio_ops;
4447
4448 switch (tp->mac_version) {
4449 case RTL_GIGA_MAC_VER_27:
4450 ops->write = r8168dp_1_mdio_write;
4451 ops->read = r8168dp_1_mdio_read;
4452 break;
françois romieue6de30d2011-01-03 15:08:37 +00004453 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004454 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004455 ops->write = r8168dp_2_mdio_write;
4456 ops->read = r8168dp_2_mdio_read;
4457 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004458 case RTL_GIGA_MAC_VER_40:
4459 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004460 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004461 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004462 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004463 case RTL_GIGA_MAC_VER_45:
4464 case RTL_GIGA_MAC_VER_46:
4465 case RTL_GIGA_MAC_VER_47:
4466 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004467 case RTL_GIGA_MAC_VER_49:
4468 case RTL_GIGA_MAC_VER_50:
4469 case RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004470 ops->write = r8168g_mdio_write;
4471 ops->read = r8168g_mdio_read;
4472 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004473 default:
4474 ops->write = r8169_mdio_write;
4475 ops->read = r8169_mdio_read;
4476 break;
4477 }
4478}
4479
hayeswange2409d82013-03-31 17:02:04 +00004480static void rtl_speed_down(struct rtl8169_private *tp)
4481{
4482 u32 adv;
4483 int lpa;
4484
4485 rtl_writephy(tp, 0x1f, 0x0000);
4486 lpa = rtl_readphy(tp, MII_LPA);
4487
4488 if (lpa & (LPA_10HALF | LPA_10FULL))
4489 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4490 else if (lpa & (LPA_100HALF | LPA_100FULL))
4491 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4492 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4493 else
4494 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4495 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4496 (tp->mii.supports_gmii ?
4497 ADVERTISED_1000baseT_Half |
4498 ADVERTISED_1000baseT_Full : 0);
4499
4500 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4501 adv);
4502}
4503
David S. Miller1805b2f2011-10-24 18:18:09 -04004504static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4505{
4506 void __iomem *ioaddr = tp->mmio_addr;
4507
4508 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004509 case RTL_GIGA_MAC_VER_25:
4510 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004511 case RTL_GIGA_MAC_VER_29:
4512 case RTL_GIGA_MAC_VER_30:
4513 case RTL_GIGA_MAC_VER_32:
4514 case RTL_GIGA_MAC_VER_33:
4515 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004516 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004517 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004518 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08004519 case RTL_GIGA_MAC_VER_40:
4520 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004521 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004522 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004523 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004524 case RTL_GIGA_MAC_VER_45:
4525 case RTL_GIGA_MAC_VER_46:
4526 case RTL_GIGA_MAC_VER_47:
4527 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004528 case RTL_GIGA_MAC_VER_49:
4529 case RTL_GIGA_MAC_VER_50:
4530 case RTL_GIGA_MAC_VER_51:
David S. Miller1805b2f2011-10-24 18:18:09 -04004531 RTL_W32(RxConfig, RTL_R32(RxConfig) |
4532 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4533 break;
4534 default:
4535 break;
4536 }
4537}
4538
4539static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4540{
4541 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4542 return false;
4543
hayeswange2409d82013-03-31 17:02:04 +00004544 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004545 rtl_wol_suspend_quirk(tp);
4546
4547 return true;
4548}
4549
françois romieu065c27c2011-01-03 15:08:12 +00004550static void r810x_phy_power_down(struct rtl8169_private *tp)
4551{
4552 rtl_writephy(tp, 0x1f, 0x0000);
4553 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4554}
4555
4556static void r810x_phy_power_up(struct rtl8169_private *tp)
4557{
4558 rtl_writephy(tp, 0x1f, 0x0000);
4559 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4560}
4561
4562static void r810x_pll_power_down(struct rtl8169_private *tp)
4563{
Hayes Wang00042992012-03-30 14:33:00 +08004564 void __iomem *ioaddr = tp->mmio_addr;
4565
David S. Miller1805b2f2011-10-24 18:18:09 -04004566 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004567 return;
françois romieu065c27c2011-01-03 15:08:12 +00004568
4569 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004570
4571 switch (tp->mac_version) {
4572 case RTL_GIGA_MAC_VER_07:
4573 case RTL_GIGA_MAC_VER_08:
4574 case RTL_GIGA_MAC_VER_09:
4575 case RTL_GIGA_MAC_VER_10:
4576 case RTL_GIGA_MAC_VER_13:
4577 case RTL_GIGA_MAC_VER_16:
4578 break;
4579 default:
4580 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4581 break;
4582 }
françois romieu065c27c2011-01-03 15:08:12 +00004583}
4584
4585static void r810x_pll_power_up(struct rtl8169_private *tp)
4586{
Hayes Wang00042992012-03-30 14:33:00 +08004587 void __iomem *ioaddr = tp->mmio_addr;
4588
françois romieu065c27c2011-01-03 15:08:12 +00004589 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004590
4591 switch (tp->mac_version) {
4592 case RTL_GIGA_MAC_VER_07:
4593 case RTL_GIGA_MAC_VER_08:
4594 case RTL_GIGA_MAC_VER_09:
4595 case RTL_GIGA_MAC_VER_10:
4596 case RTL_GIGA_MAC_VER_13:
4597 case RTL_GIGA_MAC_VER_16:
4598 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004599 case RTL_GIGA_MAC_VER_47:
4600 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004601 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004602 break;
Hayes Wang00042992012-03-30 14:33:00 +08004603 default:
4604 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4605 break;
4606 }
françois romieu065c27c2011-01-03 15:08:12 +00004607}
4608
4609static void r8168_phy_power_up(struct rtl8169_private *tp)
4610{
4611 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004612 switch (tp->mac_version) {
4613 case RTL_GIGA_MAC_VER_11:
4614 case RTL_GIGA_MAC_VER_12:
4615 case RTL_GIGA_MAC_VER_17:
4616 case RTL_GIGA_MAC_VER_18:
4617 case RTL_GIGA_MAC_VER_19:
4618 case RTL_GIGA_MAC_VER_20:
4619 case RTL_GIGA_MAC_VER_21:
4620 case RTL_GIGA_MAC_VER_22:
4621 case RTL_GIGA_MAC_VER_23:
4622 case RTL_GIGA_MAC_VER_24:
4623 case RTL_GIGA_MAC_VER_25:
4624 case RTL_GIGA_MAC_VER_26:
4625 case RTL_GIGA_MAC_VER_27:
4626 case RTL_GIGA_MAC_VER_28:
4627 case RTL_GIGA_MAC_VER_31:
4628 rtl_writephy(tp, 0x0e, 0x0000);
4629 break;
4630 default:
4631 break;
4632 }
françois romieu065c27c2011-01-03 15:08:12 +00004633 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4634}
4635
4636static void r8168_phy_power_down(struct rtl8169_private *tp)
4637{
4638 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004639 switch (tp->mac_version) {
4640 case RTL_GIGA_MAC_VER_32:
4641 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004642 case RTL_GIGA_MAC_VER_40:
4643 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004644 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4645 break;
4646
4647 case RTL_GIGA_MAC_VER_11:
4648 case RTL_GIGA_MAC_VER_12:
4649 case RTL_GIGA_MAC_VER_17:
4650 case RTL_GIGA_MAC_VER_18:
4651 case RTL_GIGA_MAC_VER_19:
4652 case RTL_GIGA_MAC_VER_20:
4653 case RTL_GIGA_MAC_VER_21:
4654 case RTL_GIGA_MAC_VER_22:
4655 case RTL_GIGA_MAC_VER_23:
4656 case RTL_GIGA_MAC_VER_24:
4657 case RTL_GIGA_MAC_VER_25:
4658 case RTL_GIGA_MAC_VER_26:
4659 case RTL_GIGA_MAC_VER_27:
4660 case RTL_GIGA_MAC_VER_28:
4661 case RTL_GIGA_MAC_VER_31:
4662 rtl_writephy(tp, 0x0e, 0x0200);
4663 default:
4664 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4665 break;
4666 }
françois romieu065c27c2011-01-03 15:08:12 +00004667}
4668
4669static void r8168_pll_power_down(struct rtl8169_private *tp)
4670{
4671 void __iomem *ioaddr = tp->mmio_addr;
4672
Francois Romieucecb5fd2011-04-01 10:21:07 +02004673 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4674 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004675 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
4676 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
4677 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
4678 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Lin2f8c0402014-10-01 23:17:19 +08004679 r8168_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00004680 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08004681 }
françois romieu065c27c2011-01-03 15:08:12 +00004682
Francois Romieucecb5fd2011-04-01 10:21:07 +02004683 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4684 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00004685 (RTL_R16(CPlusCmd) & ASF)) {
4686 return;
4687 }
4688
hayeswang01dc7fe2011-03-21 01:50:28 +00004689 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4690 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004691 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004692
David S. Miller1805b2f2011-10-24 18:18:09 -04004693 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004694 return;
françois romieu065c27c2011-01-03 15:08:12 +00004695
4696 r8168_phy_power_down(tp);
4697
4698 switch (tp->mac_version) {
4699 case RTL_GIGA_MAC_VER_25:
4700 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004701 case RTL_GIGA_MAC_VER_27:
4702 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004703 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004704 case RTL_GIGA_MAC_VER_32:
4705 case RTL_GIGA_MAC_VER_33:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004706 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004707 case RTL_GIGA_MAC_VER_45:
4708 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004709 case RTL_GIGA_MAC_VER_50:
4710 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00004711 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4712 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004713 case RTL_GIGA_MAC_VER_40:
4714 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004715 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004716 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004717 0xfc000000, ERIAR_EXGMAC);
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08004718 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004719 break;
françois romieu065c27c2011-01-03 15:08:12 +00004720 }
4721}
4722
4723static void r8168_pll_power_up(struct rtl8169_private *tp)
4724{
4725 void __iomem *ioaddr = tp->mmio_addr;
4726
françois romieu065c27c2011-01-03 15:08:12 +00004727 switch (tp->mac_version) {
4728 case RTL_GIGA_MAC_VER_25:
4729 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004730 case RTL_GIGA_MAC_VER_27:
4731 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004732 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004733 case RTL_GIGA_MAC_VER_32:
4734 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00004735 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4736 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004737 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004738 case RTL_GIGA_MAC_VER_45:
4739 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004740 case RTL_GIGA_MAC_VER_50:
4741 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004742 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004743 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004744 case RTL_GIGA_MAC_VER_40:
4745 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004746 case RTL_GIGA_MAC_VER_49:
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08004747 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004748 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004749 0x00000000, ERIAR_EXGMAC);
4750 break;
françois romieu065c27c2011-01-03 15:08:12 +00004751 }
4752
4753 r8168_phy_power_up(tp);
4754}
4755
Francois Romieud58d46b2011-05-03 16:38:29 +02004756static void rtl_generic_op(struct rtl8169_private *tp,
4757 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00004758{
4759 if (op)
4760 op(tp);
4761}
4762
4763static void rtl_pll_power_down(struct rtl8169_private *tp)
4764{
Francois Romieud58d46b2011-05-03 16:38:29 +02004765 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00004766}
4767
4768static void rtl_pll_power_up(struct rtl8169_private *tp)
4769{
Francois Romieud58d46b2011-05-03 16:38:29 +02004770 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00004771}
4772
Bill Pembertonbaf63292012-12-03 09:23:28 -05004773static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00004774{
4775 struct pll_power_ops *ops = &tp->pll_power_ops;
4776
4777 switch (tp->mac_version) {
4778 case RTL_GIGA_MAC_VER_07:
4779 case RTL_GIGA_MAC_VER_08:
4780 case RTL_GIGA_MAC_VER_09:
4781 case RTL_GIGA_MAC_VER_10:
4782 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08004783 case RTL_GIGA_MAC_VER_29:
4784 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004785 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004786 case RTL_GIGA_MAC_VER_39:
hayeswang58152cd2013-04-01 22:23:42 +00004787 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004788 case RTL_GIGA_MAC_VER_47:
4789 case RTL_GIGA_MAC_VER_48:
françois romieu065c27c2011-01-03 15:08:12 +00004790 ops->down = r810x_pll_power_down;
4791 ops->up = r810x_pll_power_up;
4792 break;
4793
4794 case RTL_GIGA_MAC_VER_11:
4795 case RTL_GIGA_MAC_VER_12:
4796 case RTL_GIGA_MAC_VER_17:
4797 case RTL_GIGA_MAC_VER_18:
4798 case RTL_GIGA_MAC_VER_19:
4799 case RTL_GIGA_MAC_VER_20:
4800 case RTL_GIGA_MAC_VER_21:
4801 case RTL_GIGA_MAC_VER_22:
4802 case RTL_GIGA_MAC_VER_23:
4803 case RTL_GIGA_MAC_VER_24:
4804 case RTL_GIGA_MAC_VER_25:
4805 case RTL_GIGA_MAC_VER_26:
4806 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00004807 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004808 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004809 case RTL_GIGA_MAC_VER_32:
4810 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004811 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08004812 case RTL_GIGA_MAC_VER_35:
4813 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004814 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08004815 case RTL_GIGA_MAC_VER_40:
4816 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004817 case RTL_GIGA_MAC_VER_42:
hayeswang45dd95c2013-07-08 17:09:01 +08004818 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004819 case RTL_GIGA_MAC_VER_45:
4820 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004821 case RTL_GIGA_MAC_VER_49:
4822 case RTL_GIGA_MAC_VER_50:
4823 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00004824 ops->down = r8168_pll_power_down;
4825 ops->up = r8168_pll_power_up;
4826 break;
4827
4828 default:
4829 ops->down = NULL;
4830 ops->up = NULL;
4831 break;
4832 }
4833}
4834
Hayes Wange542a222011-07-06 15:58:04 +08004835static void rtl_init_rxcfg(struct rtl8169_private *tp)
4836{
4837 void __iomem *ioaddr = tp->mmio_addr;
4838
4839 switch (tp->mac_version) {
4840 case RTL_GIGA_MAC_VER_01:
4841 case RTL_GIGA_MAC_VER_02:
4842 case RTL_GIGA_MAC_VER_03:
4843 case RTL_GIGA_MAC_VER_04:
4844 case RTL_GIGA_MAC_VER_05:
4845 case RTL_GIGA_MAC_VER_06:
4846 case RTL_GIGA_MAC_VER_10:
4847 case RTL_GIGA_MAC_VER_11:
4848 case RTL_GIGA_MAC_VER_12:
4849 case RTL_GIGA_MAC_VER_13:
4850 case RTL_GIGA_MAC_VER_14:
4851 case RTL_GIGA_MAC_VER_15:
4852 case RTL_GIGA_MAC_VER_16:
4853 case RTL_GIGA_MAC_VER_17:
4854 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4855 break;
4856 case RTL_GIGA_MAC_VER_18:
4857 case RTL_GIGA_MAC_VER_19:
4858 case RTL_GIGA_MAC_VER_20:
4859 case RTL_GIGA_MAC_VER_21:
4860 case RTL_GIGA_MAC_VER_22:
4861 case RTL_GIGA_MAC_VER_23:
4862 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004863 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02004864 case RTL_GIGA_MAC_VER_35:
Hayes Wange542a222011-07-06 15:58:04 +08004865 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4866 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004867 case RTL_GIGA_MAC_VER_40:
Michel Dänzer7a9810e2014-07-17 12:55:40 +09004868 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
4869 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004870 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004871 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004872 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004873 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004874 case RTL_GIGA_MAC_VER_45:
4875 case RTL_GIGA_MAC_VER_46:
4876 case RTL_GIGA_MAC_VER_47:
4877 case RTL_GIGA_MAC_VER_48:
Ivan Vecera7ebc4822015-08-04 22:11:43 +02004878 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST | RX_EARLY_OFF);
4879 break;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004880 case RTL_GIGA_MAC_VER_49:
4881 case RTL_GIGA_MAC_VER_50:
4882 case RTL_GIGA_MAC_VER_51:
Ivan Vecera7ebc4822015-08-04 22:11:43 +02004883 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00004884 break;
Hayes Wange542a222011-07-06 15:58:04 +08004885 default:
4886 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
4887 break;
4888 }
4889}
4890
Hayes Wang92fc43b2011-07-06 15:58:03 +08004891static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4892{
Timo Teräs9fba0812013-01-15 21:01:24 +00004893 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004894}
4895
Francois Romieud58d46b2011-05-03 16:38:29 +02004896static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4897{
françois romieu9c5028e2012-03-02 04:43:14 +00004898 void __iomem *ioaddr = tp->mmio_addr;
4899
4900 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004901 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00004902 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004903}
4904
4905static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4906{
françois romieu9c5028e2012-03-02 04:43:14 +00004907 void __iomem *ioaddr = tp->mmio_addr;
4908
4909 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004910 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00004911 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004912}
4913
4914static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4915{
4916 void __iomem *ioaddr = tp->mmio_addr;
4917
4918 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4919 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01004920 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004921}
4922
4923static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4924{
4925 void __iomem *ioaddr = tp->mmio_addr;
4926
4927 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4928 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
4929 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
4930}
4931
4932static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4933{
4934 void __iomem *ioaddr = tp->mmio_addr;
4935
4936 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4937}
4938
4939static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4940{
4941 void __iomem *ioaddr = tp->mmio_addr;
4942
4943 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4944}
4945
4946static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4947{
4948 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004949
4950 RTL_W8(MaxTxPacketSize, 0x3f);
4951 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4952 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01004953 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004954}
4955
4956static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4957{
4958 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004959
4960 RTL_W8(MaxTxPacketSize, 0x0c);
4961 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4962 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004963 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004964}
4965
4966static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4967{
4968 rtl_tx_performance_tweak(tp->pci_dev,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01004969 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004970}
4971
4972static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4973{
4974 rtl_tx_performance_tweak(tp->pci_dev,
4975 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4976}
4977
4978static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4979{
4980 void __iomem *ioaddr = tp->mmio_addr;
4981
4982 r8168b_0_hw_jumbo_enable(tp);
4983
4984 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
4985}
4986
4987static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4988{
4989 void __iomem *ioaddr = tp->mmio_addr;
4990
4991 r8168b_0_hw_jumbo_disable(tp);
4992
4993 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4994}
4995
Bill Pembertonbaf63292012-12-03 09:23:28 -05004996static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02004997{
4998 struct jumbo_ops *ops = &tp->jumbo_ops;
4999
5000 switch (tp->mac_version) {
5001 case RTL_GIGA_MAC_VER_11:
5002 ops->disable = r8168b_0_hw_jumbo_disable;
5003 ops->enable = r8168b_0_hw_jumbo_enable;
5004 break;
5005 case RTL_GIGA_MAC_VER_12:
5006 case RTL_GIGA_MAC_VER_17:
5007 ops->disable = r8168b_1_hw_jumbo_disable;
5008 ops->enable = r8168b_1_hw_jumbo_enable;
5009 break;
5010 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5011 case RTL_GIGA_MAC_VER_19:
5012 case RTL_GIGA_MAC_VER_20:
5013 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5014 case RTL_GIGA_MAC_VER_22:
5015 case RTL_GIGA_MAC_VER_23:
5016 case RTL_GIGA_MAC_VER_24:
5017 case RTL_GIGA_MAC_VER_25:
5018 case RTL_GIGA_MAC_VER_26:
5019 ops->disable = r8168c_hw_jumbo_disable;
5020 ops->enable = r8168c_hw_jumbo_enable;
5021 break;
5022 case RTL_GIGA_MAC_VER_27:
5023 case RTL_GIGA_MAC_VER_28:
5024 ops->disable = r8168dp_hw_jumbo_disable;
5025 ops->enable = r8168dp_hw_jumbo_enable;
5026 break;
5027 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5028 case RTL_GIGA_MAC_VER_32:
5029 case RTL_GIGA_MAC_VER_33:
5030 case RTL_GIGA_MAC_VER_34:
5031 ops->disable = r8168e_hw_jumbo_disable;
5032 ops->enable = r8168e_hw_jumbo_enable;
5033 break;
5034
5035 /*
5036 * No action needed for jumbo frames with 8169.
5037 * No jumbo for 810x at all.
5038 */
Hayes Wangc5583862012-07-02 17:23:22 +08005039 case RTL_GIGA_MAC_VER_40:
5040 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005041 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005042 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005043 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005044 case RTL_GIGA_MAC_VER_45:
5045 case RTL_GIGA_MAC_VER_46:
5046 case RTL_GIGA_MAC_VER_47:
5047 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005048 case RTL_GIGA_MAC_VER_49:
5049 case RTL_GIGA_MAC_VER_50:
5050 case RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02005051 default:
5052 ops->disable = NULL;
5053 ops->enable = NULL;
5054 break;
5055 }
5056}
5057
Francois Romieuffc46952012-07-06 14:19:23 +02005058DECLARE_RTL_COND(rtl_chipcmd_cond)
5059{
5060 void __iomem *ioaddr = tp->mmio_addr;
5061
5062 return RTL_R8(ChipCmd) & CmdReset;
5063}
5064
Francois Romieu6f43adc2011-04-29 15:05:51 +02005065static void rtl_hw_reset(struct rtl8169_private *tp)
5066{
5067 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu6f43adc2011-04-29 15:05:51 +02005068
Francois Romieu6f43adc2011-04-29 15:05:51 +02005069 RTL_W8(ChipCmd, CmdReset);
5070
Francois Romieuffc46952012-07-06 14:19:23 +02005071 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005072}
5073
Francois Romieub6ffd972011-06-17 17:00:05 +02005074static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
5075{
5076 struct rtl_fw *rtl_fw;
5077 const char *name;
5078 int rc = -ENOMEM;
5079
5080 name = rtl_lookup_firmware_name(tp);
5081 if (!name)
5082 goto out_no_firmware;
5083
5084 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5085 if (!rtl_fw)
5086 goto err_warn;
5087
5088 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
5089 if (rc < 0)
5090 goto err_free;
5091
Francois Romieufd112f22011-06-18 00:10:29 +02005092 rc = rtl_check_firmware(tp, rtl_fw);
5093 if (rc < 0)
5094 goto err_release_firmware;
5095
Francois Romieub6ffd972011-06-17 17:00:05 +02005096 tp->rtl_fw = rtl_fw;
5097out:
5098 return;
5099
Francois Romieufd112f22011-06-18 00:10:29 +02005100err_release_firmware:
5101 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02005102err_free:
5103 kfree(rtl_fw);
5104err_warn:
5105 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5106 name, rc);
5107out_no_firmware:
5108 tp->rtl_fw = NULL;
5109 goto out;
5110}
5111
François Romieu953a12c2011-04-24 17:38:48 +02005112static void rtl_request_firmware(struct rtl8169_private *tp)
5113{
Francois Romieub6ffd972011-06-17 17:00:05 +02005114 if (IS_ERR(tp->rtl_fw))
5115 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02005116}
5117
Hayes Wang92fc43b2011-07-06 15:58:03 +08005118static void rtl_rx_close(struct rtl8169_private *tp)
5119{
5120 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005121
Francois Romieu1687b562011-07-19 17:21:29 +02005122 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005123}
5124
Francois Romieuffc46952012-07-06 14:19:23 +02005125DECLARE_RTL_COND(rtl_npq_cond)
5126{
5127 void __iomem *ioaddr = tp->mmio_addr;
5128
5129 return RTL_R8(TxPoll) & NPQ;
5130}
5131
5132DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5133{
5134 void __iomem *ioaddr = tp->mmio_addr;
5135
5136 return RTL_R32(TxConfig) & TXCFG_EMPTY;
5137}
5138
françois romieue6de30d2011-01-03 15:08:37 +00005139static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140{
françois romieue6de30d2011-01-03 15:08:37 +00005141 void __iomem *ioaddr = tp->mmio_addr;
5142
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00005144 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145
Hayes Wang92fc43b2011-07-06 15:58:03 +08005146 rtl_rx_close(tp);
5147
Hayes Wang5d2e1952011-02-22 17:26:22 +08005148 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00005149 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5150 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02005151 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08005152 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005153 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5154 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5155 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5156 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5157 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5158 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5159 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5160 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5161 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5162 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5163 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5164 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005165 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5166 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5167 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5168 tp->mac_version == RTL_GIGA_MAC_VER_51) {
David S. Miller8decf862011-09-22 03:23:13 -04005169 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02005170 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005171 } else {
5172 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
5173 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00005174 }
5175
Hayes Wang92fc43b2011-07-06 15:58:03 +08005176 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177}
5178
Francois Romieu7f796d832007-06-11 23:04:41 +02005179static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005180{
5181 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01005182
5183 /* Set DMA burst size and Interframe Gap Time */
5184 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
5185 (InterFrameGap << TxInterFrameGapShift));
5186}
5187
Francois Romieu07ce4062007-02-23 23:36:39 +01005188static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189{
5190 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191
Francois Romieu07ce4062007-02-23 23:36:39 +01005192 tp->hw_start(dev);
5193
Francois Romieuda78dbf2012-01-26 14:18:23 +01005194 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01005195}
5196
Francois Romieu7f796d832007-06-11 23:04:41 +02005197static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
5198 void __iomem *ioaddr)
5199{
5200 /*
5201 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5202 * register to be written before TxDescAddrLow to work.
5203 * Switching from MMIO to I/O access fixes the issue as well.
5204 */
5205 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005206 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005207 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005208 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005209}
5210
5211static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
5212{
5213 u16 cmd;
5214
5215 cmd = RTL_R16(CPlusCmd);
5216 RTL_W16(CPlusCmd, cmd);
5217 return cmd;
5218}
5219
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07005220static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02005221{
5222 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00005223 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02005224}
5225
Francois Romieu6dccd162007-02-13 23:38:05 +01005226static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
5227{
Francois Romieu37441002011-06-17 22:58:54 +02005228 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01005229 u32 mac_version;
5230 u32 clk;
5231 u32 val;
5232 } cfg2_info [] = {
5233 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5234 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5235 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5236 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02005237 };
5238 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01005239 unsigned int i;
5240 u32 clk;
5241
5242 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01005243 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01005244 if ((p->mac_version == mac_version) && (p->clk == clk)) {
5245 RTL_W32(0x7c, p->val);
5246 break;
5247 }
5248 }
5249}
5250
Francois Romieue6b763e2012-03-08 09:35:39 +01005251static void rtl_set_rx_mode(struct net_device *dev)
5252{
5253 struct rtl8169_private *tp = netdev_priv(dev);
5254 void __iomem *ioaddr = tp->mmio_addr;
5255 u32 mc_filter[2]; /* Multicast hash filter */
5256 int rx_mode;
5257 u32 tmp = 0;
5258
5259 if (dev->flags & IFF_PROMISC) {
5260 /* Unconditionally log net taps. */
5261 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5262 rx_mode =
5263 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5264 AcceptAllPhys;
5265 mc_filter[1] = mc_filter[0] = 0xffffffff;
5266 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5267 (dev->flags & IFF_ALLMULTI)) {
5268 /* Too many to filter perfectly -- accept all multicasts. */
5269 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5270 mc_filter[1] = mc_filter[0] = 0xffffffff;
5271 } else {
5272 struct netdev_hw_addr *ha;
5273
5274 rx_mode = AcceptBroadcast | AcceptMyPhys;
5275 mc_filter[1] = mc_filter[0] = 0;
5276 netdev_for_each_mc_addr(ha, dev) {
5277 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5278 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5279 rx_mode |= AcceptMulticast;
5280 }
5281 }
5282
5283 if (dev->features & NETIF_F_RXALL)
5284 rx_mode |= (AcceptErr | AcceptRunt);
5285
5286 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
5287
5288 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5289 u32 data = mc_filter[0];
5290
5291 mc_filter[0] = swab32(mc_filter[1]);
5292 mc_filter[1] = swab32(data);
5293 }
5294
Nathan Walp04817762012-11-01 12:08:47 +00005295 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5296 mc_filter[1] = mc_filter[0] = 0xffffffff;
5297
Francois Romieue6b763e2012-03-08 09:35:39 +01005298 RTL_W32(MAR0 + 4, mc_filter[1]);
5299 RTL_W32(MAR0 + 0, mc_filter[0]);
5300
5301 RTL_W32(RxConfig, tmp);
5302}
5303
Francois Romieu07ce4062007-02-23 23:36:39 +01005304static void rtl_hw_start_8169(struct net_device *dev)
5305{
5306 struct rtl8169_private *tp = netdev_priv(dev);
5307 void __iomem *ioaddr = tp->mmio_addr;
5308 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01005309
Francois Romieu9cb427b2006-11-02 00:10:16 +01005310 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
5311 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
5312 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
5313 }
5314
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02005316 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5317 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5318 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5319 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005320 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5321
Hayes Wange542a222011-07-06 15:58:04 +08005322 rtl_init_rxcfg(tp);
5323
françois romieuf0298f82011-01-03 15:07:42 +00005324 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005326 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327
Francois Romieucecb5fd2011-04-01 10:21:07 +02005328 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5329 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5330 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5331 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02005332 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333
Francois Romieu7f796d832007-06-11 23:04:41 +02005334 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005335
Francois Romieucecb5fd2011-04-01 10:21:07 +02005336 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5337 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005338 dprintk("Set MAC Reg C+CR Offset 0xe0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005340 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 }
5342
Francois Romieubcf0bf92006-07-26 23:14:13 +02005343 RTL_W16(CPlusCmd, tp->cp_cmd);
5344
Francois Romieu6dccd162007-02-13 23:38:05 +01005345 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
5346
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 /*
5348 * Undocumented corner. Supposedly:
5349 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5350 */
5351 RTL_W16(IntrMitigate, 0x0000);
5352
Francois Romieu7f796d832007-06-11 23:04:41 +02005353 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005354
Francois Romieucecb5fd2011-04-01 10:21:07 +02005355 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5356 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5357 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5358 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02005359 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5360 rtl_set_rx_tx_config_registers(tp);
5361 }
5362
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02005364
5365 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5366 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367
5368 RTL_W32(RxMissed, 0);
5369
Francois Romieu07ce4062007-02-23 23:36:39 +01005370 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371
5372 /* no early-rx interrupts */
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005373 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005374}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005376static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5377{
5378 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02005379 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005380}
5381
5382static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5383{
Francois Romieu52989f02012-07-06 13:37:00 +02005384 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005385}
5386
5387static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02005388{
5389 u32 csi;
5390
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005391 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5392 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00005393}
5394
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005395static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005396{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005397 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00005398}
5399
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005400static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00005401{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005402 rtl_csi_access_enable(tp, 0x27000000);
5403}
5404
Francois Romieuffc46952012-07-06 14:19:23 +02005405DECLARE_RTL_COND(rtl_csiar_cond)
5406{
5407 void __iomem *ioaddr = tp->mmio_addr;
5408
5409 return RTL_R32(CSIAR) & CSIAR_FLAG;
5410}
5411
Francois Romieu52989f02012-07-06 13:37:00 +02005412static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005413{
Francois Romieu52989f02012-07-06 13:37:00 +02005414 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005415
5416 RTL_W32(CSIDR, value);
5417 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5418 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5419
Francois Romieuffc46952012-07-06 14:19:23 +02005420 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005421}
5422
Francois Romieu52989f02012-07-06 13:37:00 +02005423static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005424{
Francois Romieu52989f02012-07-06 13:37:00 +02005425 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005426
5427 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
5428 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5429
Francois Romieuffc46952012-07-06 14:19:23 +02005430 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5431 RTL_R32(CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005432}
5433
Francois Romieu52989f02012-07-06 13:37:00 +02005434static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005435{
Francois Romieu52989f02012-07-06 13:37:00 +02005436 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005437
5438 RTL_W32(CSIDR, value);
5439 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5440 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5441 CSIAR_FUNC_NIC);
5442
Francois Romieuffc46952012-07-06 14:19:23 +02005443 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005444}
5445
Francois Romieu52989f02012-07-06 13:37:00 +02005446static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005447{
Francois Romieu52989f02012-07-06 13:37:00 +02005448 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005449
5450 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
5451 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5452
Francois Romieuffc46952012-07-06 14:19:23 +02005453 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5454 RTL_R32(CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005455}
5456
hayeswang45dd95c2013-07-08 17:09:01 +08005457static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5458{
5459 void __iomem *ioaddr = tp->mmio_addr;
5460
5461 RTL_W32(CSIDR, value);
5462 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5463 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5464 CSIAR_FUNC_NIC2);
5465
5466 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5467}
5468
5469static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5470{
5471 void __iomem *ioaddr = tp->mmio_addr;
5472
5473 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
5474 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5475
5476 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5477 RTL_R32(CSIDR) : ~0;
5478}
5479
Bill Pembertonbaf63292012-12-03 09:23:28 -05005480static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005481{
5482 struct csi_ops *ops = &tp->csi_ops;
5483
5484 switch (tp->mac_version) {
5485 case RTL_GIGA_MAC_VER_01:
5486 case RTL_GIGA_MAC_VER_02:
5487 case RTL_GIGA_MAC_VER_03:
5488 case RTL_GIGA_MAC_VER_04:
5489 case RTL_GIGA_MAC_VER_05:
5490 case RTL_GIGA_MAC_VER_06:
5491 case RTL_GIGA_MAC_VER_10:
5492 case RTL_GIGA_MAC_VER_11:
5493 case RTL_GIGA_MAC_VER_12:
5494 case RTL_GIGA_MAC_VER_13:
5495 case RTL_GIGA_MAC_VER_14:
5496 case RTL_GIGA_MAC_VER_15:
5497 case RTL_GIGA_MAC_VER_16:
5498 case RTL_GIGA_MAC_VER_17:
5499 ops->write = NULL;
5500 ops->read = NULL;
5501 break;
5502
Hayes Wang7e18dca2012-03-30 14:33:02 +08005503 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005504 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005505 ops->write = r8402_csi_write;
5506 ops->read = r8402_csi_read;
5507 break;
5508
hayeswang45dd95c2013-07-08 17:09:01 +08005509 case RTL_GIGA_MAC_VER_44:
5510 ops->write = r8411_csi_write;
5511 ops->read = r8411_csi_read;
5512 break;
5513
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005514 default:
5515 ops->write = r8169_csi_write;
5516 ops->read = r8169_csi_read;
5517 break;
5518 }
Francois Romieudacf8152008-08-02 20:44:13 +02005519}
5520
5521struct ephy_info {
5522 unsigned int offset;
5523 u16 mask;
5524 u16 bits;
5525};
5526
Francois Romieufdf6fc02012-07-06 22:40:38 +02005527static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5528 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005529{
5530 u16 w;
5531
5532 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005533 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5534 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005535 e++;
5536 }
5537}
5538
Francois Romieub726e492008-06-28 12:22:59 +02005539static void rtl_disable_clock_request(struct pci_dev *pdev)
5540{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005541 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
5542 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005543}
5544
françois romieue6de30d2011-01-03 15:08:37 +00005545static void rtl_enable_clock_request(struct pci_dev *pdev)
5546{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005547 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
5548 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005549}
5550
hayeswangb51ecea2014-07-09 14:52:51 +08005551static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5552{
5553 void __iomem *ioaddr = tp->mmio_addr;
5554 u8 data;
5555
5556 data = RTL_R8(Config3);
5557
5558 if (enable)
5559 data |= Rdy_to_L23;
5560 else
5561 data &= ~Rdy_to_L23;
5562
5563 RTL_W8(Config3, data);
5564}
5565
Francois Romieub726e492008-06-28 12:22:59 +02005566#define R8168_CPCMD_QUIRK_MASK (\
5567 EnableBist | \
5568 Mac_dbgo_oe | \
5569 Force_half_dup | \
5570 Force_rxflow_en | \
5571 Force_txflow_en | \
5572 Cxpl_dbg_sel | \
5573 ASF | \
5574 PktCntrDisable | \
5575 Mac_dbgo_sel)
5576
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005577static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005578{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005579 void __iomem *ioaddr = tp->mmio_addr;
5580 struct pci_dev *pdev = tp->pci_dev;
5581
Francois Romieub726e492008-06-28 12:22:59 +02005582 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5583
5584 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5585
françois romieufaf1e782013-02-27 13:01:57 +00005586 if (tp->dev->mtu <= ETH_DATA_LEN) {
5587 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
5588 PCI_EXP_DEVCTL_NOSNOOP_EN);
5589 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005590}
5591
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005592static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005593{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005594 void __iomem *ioaddr = tp->mmio_addr;
5595
5596 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005597
françois romieuf0298f82011-01-03 15:07:42 +00005598 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005599
5600 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005601}
5602
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005603static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005604{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005605 void __iomem *ioaddr = tp->mmio_addr;
5606 struct pci_dev *pdev = tp->pci_dev;
5607
Francois Romieub726e492008-06-28 12:22:59 +02005608 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
5609
5610 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5611
françois romieufaf1e782013-02-27 13:01:57 +00005612 if (tp->dev->mtu <= ETH_DATA_LEN)
5613 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02005614
5615 rtl_disable_clock_request(pdev);
5616
5617 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02005618}
5619
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005620static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005621{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005622 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005623 { 0x01, 0, 0x0001 },
5624 { 0x02, 0x0800, 0x1000 },
5625 { 0x03, 0, 0x0042 },
5626 { 0x06, 0x0080, 0x0000 },
5627 { 0x07, 0, 0x2000 }
5628 };
5629
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005630 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005631
Francois Romieufdf6fc02012-07-06 22:40:38 +02005632 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005633
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005634 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005635}
5636
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005637static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005638{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005639 void __iomem *ioaddr = tp->mmio_addr;
5640 struct pci_dev *pdev = tp->pci_dev;
5641
5642 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005643
5644 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5645
françois romieufaf1e782013-02-27 13:01:57 +00005646 if (tp->dev->mtu <= ETH_DATA_LEN)
5647 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieuef3386f2008-06-29 12:24:30 +02005648
5649 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5650}
5651
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005652static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005653{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005654 void __iomem *ioaddr = tp->mmio_addr;
5655 struct pci_dev *pdev = tp->pci_dev;
5656
5657 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005658
5659 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5660
5661 /* Magic. */
5662 RTL_W8(DBG_REG, 0x20);
5663
françois romieuf0298f82011-01-03 15:07:42 +00005664 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005665
françois romieufaf1e782013-02-27 13:01:57 +00005666 if (tp->dev->mtu <= ETH_DATA_LEN)
5667 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005668
5669 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5670}
5671
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005672static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005673{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005674 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005675 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005676 { 0x02, 0x0800, 0x1000 },
5677 { 0x03, 0, 0x0002 },
5678 { 0x06, 0x0080, 0x0000 }
5679 };
5680
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005681 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005682
5683 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
5684
Francois Romieufdf6fc02012-07-06 22:40:38 +02005685 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005686
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005687 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005688}
5689
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005690static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005691{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005692 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005693 { 0x01, 0, 0x0001 },
5694 { 0x03, 0x0400, 0x0220 }
5695 };
5696
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005697 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005698
Francois Romieufdf6fc02012-07-06 22:40:38 +02005699 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005700
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005701 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005702}
5703
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005704static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005705{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005706 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005707}
5708
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005709static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005710{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005711 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005712
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005713 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005714}
5715
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005716static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005717{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005718 void __iomem *ioaddr = tp->mmio_addr;
5719 struct pci_dev *pdev = tp->pci_dev;
5720
5721 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005722
5723 rtl_disable_clock_request(pdev);
5724
françois romieuf0298f82011-01-03 15:07:42 +00005725 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005726
françois romieufaf1e782013-02-27 13:01:57 +00005727 if (tp->dev->mtu <= ETH_DATA_LEN)
5728 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu5b538df2008-07-20 16:22:45 +02005729
5730 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5731}
5732
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005733static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005734{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005735 void __iomem *ioaddr = tp->mmio_addr;
5736 struct pci_dev *pdev = tp->pci_dev;
5737
5738 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005739
françois romieufaf1e782013-02-27 13:01:57 +00005740 if (tp->dev->mtu <= ETH_DATA_LEN)
5741 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang4804b3b2011-03-21 01:50:29 +00005742
5743 RTL_W8(MaxTxPacketSize, TxPacketMax);
5744
5745 rtl_disable_clock_request(pdev);
5746}
5747
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005748static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005749{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005750 void __iomem *ioaddr = tp->mmio_addr;
5751 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00005752 static const struct ephy_info e_info_8168d_4[] = {
5753 { 0x0b, ~0, 0x48 },
5754 { 0x19, 0x20, 0x50 },
5755 { 0x0c, ~0, 0x20 }
5756 };
5757 int i;
5758
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005759 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005760
5761 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5762
5763 RTL_W8(MaxTxPacketSize, TxPacketMax);
5764
5765 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
5766 const struct ephy_info *e = e_info_8168d_4 + i;
5767 u16 w;
5768
Francois Romieufdf6fc02012-07-06 22:40:38 +02005769 w = rtl_ephy_read(tp, e->offset);
5770 rtl_ephy_write(tp, 0x03, (w & e->mask) | e->bits);
françois romieue6de30d2011-01-03 15:08:37 +00005771 }
5772
5773 rtl_enable_clock_request(pdev);
5774}
5775
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005776static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005777{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005778 void __iomem *ioaddr = tp->mmio_addr;
5779 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005780 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005781 { 0x00, 0x0200, 0x0100 },
5782 { 0x00, 0x0000, 0x0004 },
5783 { 0x06, 0x0002, 0x0001 },
5784 { 0x06, 0x0000, 0x0030 },
5785 { 0x07, 0x0000, 0x2000 },
5786 { 0x00, 0x0000, 0x0020 },
5787 { 0x03, 0x5800, 0x2000 },
5788 { 0x03, 0x0000, 0x0001 },
5789 { 0x01, 0x0800, 0x1000 },
5790 { 0x07, 0x0000, 0x4000 },
5791 { 0x1e, 0x0000, 0x2000 },
5792 { 0x19, 0xffff, 0xfe6c },
5793 { 0x0a, 0x0000, 0x0040 }
5794 };
5795
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005796 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005797
Francois Romieufdf6fc02012-07-06 22:40:38 +02005798 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005799
françois romieufaf1e782013-02-27 13:01:57 +00005800 if (tp->dev->mtu <= ETH_DATA_LEN)
5801 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang01dc7fe2011-03-21 01:50:28 +00005802
5803 RTL_W8(MaxTxPacketSize, TxPacketMax);
5804
5805 rtl_disable_clock_request(pdev);
5806
5807 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02005808 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
5809 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005810
Francois Romieucecb5fd2011-04-01 10:21:07 +02005811 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005812}
5813
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005814static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005815{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005816 void __iomem *ioaddr = tp->mmio_addr;
5817 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005818 static const struct ephy_info e_info_8168e_2[] = {
5819 { 0x09, 0x0000, 0x0080 },
5820 { 0x19, 0x0000, 0x0224 }
5821 };
5822
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005823 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005824
Francois Romieufdf6fc02012-07-06 22:40:38 +02005825 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005826
françois romieufaf1e782013-02-27 13:01:57 +00005827 if (tp->dev->mtu <= ETH_DATA_LEN)
5828 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Hayes Wang70090422011-07-06 15:58:06 +08005829
Francois Romieufdf6fc02012-07-06 22:40:38 +02005830 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5831 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5832 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5833 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5834 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5835 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005836 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5837 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005838
Hayes Wang3090bd92011-09-06 16:55:15 +08005839 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005840
Francois Romieu4521e1a92012-11-01 16:46:28 +00005841 rtl_disable_clock_request(pdev);
5842
Hayes Wang70090422011-07-06 15:58:06 +08005843 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5844 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5845
5846 /* Adjust EEE LED frequency */
5847 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5848
5849 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5850 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005851 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08005852}
5853
Hayes Wang5f886e02012-03-30 14:33:03 +08005854static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005855{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005856 void __iomem *ioaddr = tp->mmio_addr;
5857 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08005858
Hayes Wang5f886e02012-03-30 14:33:03 +08005859 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005860
5861 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5862
Francois Romieufdf6fc02012-07-06 22:40:38 +02005863 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5864 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5865 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5866 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005867 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5868 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5869 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5870 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005871 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5872 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005873
5874 RTL_W8(MaxTxPacketSize, EarlySize);
5875
Francois Romieu4521e1a92012-11-01 16:46:28 +00005876 rtl_disable_clock_request(pdev);
5877
Hayes Wangc2218922011-09-06 16:55:18 +08005878 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5879 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08005880 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005881 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5882 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005883}
5884
Hayes Wang5f886e02012-03-30 14:33:03 +08005885static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5886{
5887 void __iomem *ioaddr = tp->mmio_addr;
5888 static const struct ephy_info e_info_8168f_1[] = {
5889 { 0x06, 0x00c0, 0x0020 },
5890 { 0x08, 0x0001, 0x0002 },
5891 { 0x09, 0x0000, 0x0080 },
5892 { 0x19, 0x0000, 0x0224 }
5893 };
5894
5895 rtl_hw_start_8168f(tp);
5896
Francois Romieufdf6fc02012-07-06 22:40:38 +02005897 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005898
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005899 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005900
5901 /* Adjust EEE LED frequency */
5902 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5903}
5904
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005905static void rtl_hw_start_8411(struct rtl8169_private *tp)
5906{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005907 static const struct ephy_info e_info_8168f_1[] = {
5908 { 0x06, 0x00c0, 0x0020 },
5909 { 0x0f, 0xffff, 0x5200 },
5910 { 0x1e, 0x0000, 0x4000 },
5911 { 0x19, 0x0000, 0x0224 }
5912 };
5913
5914 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08005915 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005916
Francois Romieufdf6fc02012-07-06 22:40:38 +02005917 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005918
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005919 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005920}
5921
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005922static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08005923{
5924 void __iomem *ioaddr = tp->mmio_addr;
5925 struct pci_dev *pdev = tp->pci_dev;
5926
hayeswangbeb330a2013-04-01 22:23:39 +00005927 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5928
Hayes Wangc5583862012-07-02 17:23:22 +08005929 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5930 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5931 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5932 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5933
5934 rtl_csi_access_enable_1(tp);
5935
5936 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5937
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005938 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5939 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00005940 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005941
Francois Romieu4521e1a92012-11-01 16:46:28 +00005942 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08005943 RTL_W8(MaxTxPacketSize, EarlySize);
5944
5945 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5946 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5947
5948 /* Adjust EEE LED frequency */
5949 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5950
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005951 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5952 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005953
5954 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08005955}
5956
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005957static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5958{
5959 void __iomem *ioaddr = tp->mmio_addr;
5960 static const struct ephy_info e_info_8168g_1[] = {
5961 { 0x00, 0x0000, 0x0008 },
5962 { 0x0c, 0x37d0, 0x0820 },
5963 { 0x1e, 0x0000, 0x0001 },
5964 { 0x19, 0x8000, 0x0000 }
5965 };
5966
5967 rtl_hw_start_8168g(tp);
5968
5969 /* disable aspm and clock request before access ephy */
5970 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
5971 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
5972 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
5973}
5974
hayeswang57538c42013-04-01 22:23:40 +00005975static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5976{
5977 void __iomem *ioaddr = tp->mmio_addr;
5978 static const struct ephy_info e_info_8168g_2[] = {
5979 { 0x00, 0x0000, 0x0008 },
5980 { 0x0c, 0x3df0, 0x0200 },
5981 { 0x19, 0xffff, 0xfc00 },
5982 { 0x1e, 0xffff, 0x20eb }
5983 };
5984
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005985 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00005986
5987 /* disable aspm and clock request before access ephy */
5988 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
5989 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
5990 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5991}
5992
hayeswang45dd95c2013-07-08 17:09:01 +08005993static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5994{
5995 void __iomem *ioaddr = tp->mmio_addr;
5996 static const struct ephy_info e_info_8411_2[] = {
5997 { 0x00, 0x0000, 0x0008 },
5998 { 0x0c, 0x3df0, 0x0200 },
5999 { 0x0f, 0xffff, 0x5200 },
6000 { 0x19, 0x0020, 0x0000 },
6001 { 0x1e, 0x0000, 0x2000 }
6002 };
6003
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006004 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08006005
6006 /* disable aspm and clock request before access ephy */
6007 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6008 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6009 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
6010}
6011
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006012static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
6013{
6014 void __iomem *ioaddr = tp->mmio_addr;
6015 struct pci_dev *pdev = tp->pci_dev;
6016 u16 rg_saw_cnt;
6017 u32 data;
6018 static const struct ephy_info e_info_8168h_1[] = {
6019 { 0x1e, 0x0800, 0x0001 },
6020 { 0x1d, 0x0000, 0x0800 },
6021 { 0x05, 0xffff, 0x2089 },
6022 { 0x06, 0xffff, 0x5881 },
6023 { 0x04, 0xffff, 0x154a },
6024 { 0x01, 0xffff, 0x068b }
6025 };
6026
6027 /* disable aspm and clock request before access ephy */
6028 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6029 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6030 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
6031
6032 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6033
6034 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6035 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6036 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6037 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6038
6039 rtl_csi_access_enable_1(tp);
6040
6041 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6042
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006043 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6044 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006045
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006046 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006047
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006048 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006049
6050 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6051
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006052 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6053 RTL_W8(MaxTxPacketSize, EarlySize);
6054
6055 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6056 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6057
6058 /* Adjust EEE LED frequency */
6059 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6060
6061 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
6062 RTL_W8(DLLPR, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
6063
6064 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6065
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006066 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006067
6068 rtl_pcie_state_l2l3_enable(tp, false);
6069
6070 rtl_writephy(tp, 0x1f, 0x0c42);
6071 rg_saw_cnt = rtl_readphy(tp, 0x13);
6072 rtl_writephy(tp, 0x1f, 0x0000);
6073 if (rg_saw_cnt > 0) {
6074 u16 sw_cnt_1ms_ini;
6075
6076 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
6077 sw_cnt_1ms_ini &= 0x0fff;
6078 data = r8168_mac_ocp_read(tp, 0xd412);
6079 data &= 0x0fff;
6080 data |= sw_cnt_1ms_ini;
6081 r8168_mac_ocp_write(tp, 0xd412, data);
6082 }
6083
6084 data = r8168_mac_ocp_read(tp, 0xe056);
6085 data &= 0xf0;
6086 data |= 0x07;
6087 r8168_mac_ocp_write(tp, 0xe056, data);
6088
6089 data = r8168_mac_ocp_read(tp, 0xe052);
6090 data &= 0x8008;
6091 data |= 0x6000;
6092 r8168_mac_ocp_write(tp, 0xe052, data);
6093
6094 data = r8168_mac_ocp_read(tp, 0xe0d6);
6095 data &= 0x01ff;
6096 data |= 0x017f;
6097 r8168_mac_ocp_write(tp, 0xe0d6, data);
6098
6099 data = r8168_mac_ocp_read(tp, 0xd420);
6100 data &= 0x0fff;
6101 data |= 0x047f;
6102 r8168_mac_ocp_write(tp, 0xd420, data);
6103
6104 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6105 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6106 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6107 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6108}
6109
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006110static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6111{
6112 void __iomem *ioaddr = tp->mmio_addr;
6113 struct pci_dev *pdev = tp->pci_dev;
6114
Chun-Hao Lin003609d2014-12-02 16:48:31 +08006115 rtl8168ep_stop_cmac(tp);
6116
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006117 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6118
6119 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6120 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6121 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6122 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6123
6124 rtl_csi_access_enable_1(tp);
6125
6126 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6127
6128 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6129 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6130
6131 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6132
6133 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6134
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006135 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6136 RTL_W8(MaxTxPacketSize, EarlySize);
6137
6138 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6139 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6140
6141 /* Adjust EEE LED frequency */
6142 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6143
6144 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6145
6146 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6147
6148 rtl_pcie_state_l2l3_enable(tp, false);
6149}
6150
6151static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6152{
6153 void __iomem *ioaddr = tp->mmio_addr;
6154 static const struct ephy_info e_info_8168ep_1[] = {
6155 { 0x00, 0xffff, 0x10ab },
6156 { 0x06, 0xffff, 0xf030 },
6157 { 0x08, 0xffff, 0x2006 },
6158 { 0x0d, 0xffff, 0x1666 },
6159 { 0x0c, 0x3ff0, 0x0000 }
6160 };
6161
6162 /* disable aspm and clock request before access ephy */
6163 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6164 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6165 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6166
6167 rtl_hw_start_8168ep(tp);
6168}
6169
6170static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6171{
6172 void __iomem *ioaddr = tp->mmio_addr;
6173 static const struct ephy_info e_info_8168ep_2[] = {
6174 { 0x00, 0xffff, 0x10a3 },
6175 { 0x19, 0xffff, 0xfc00 },
6176 { 0x1e, 0xffff, 0x20ea }
6177 };
6178
6179 /* disable aspm and clock request before access ephy */
6180 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6181 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6182 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6183
6184 rtl_hw_start_8168ep(tp);
6185
6186 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
6187 RTL_W8(DLLPR, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
6188}
6189
6190static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6191{
6192 void __iomem *ioaddr = tp->mmio_addr;
6193 u32 data;
6194 static const struct ephy_info e_info_8168ep_3[] = {
6195 { 0x00, 0xffff, 0x10a3 },
6196 { 0x19, 0xffff, 0x7c00 },
6197 { 0x1e, 0xffff, 0x20eb },
6198 { 0x0d, 0xffff, 0x1666 }
6199 };
6200
6201 /* disable aspm and clock request before access ephy */
6202 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6203 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6204 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6205
6206 rtl_hw_start_8168ep(tp);
6207
6208 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
6209 RTL_W8(DLLPR, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
6210
6211 data = r8168_mac_ocp_read(tp, 0xd3e2);
6212 data &= 0xf000;
6213 data |= 0x0271;
6214 r8168_mac_ocp_write(tp, 0xd3e2, data);
6215
6216 data = r8168_mac_ocp_read(tp, 0xd3e4);
6217 data &= 0xff00;
6218 r8168_mac_ocp_write(tp, 0xd3e4, data);
6219
6220 data = r8168_mac_ocp_read(tp, 0xe860);
6221 data |= 0x0080;
6222 r8168_mac_ocp_write(tp, 0xe860, data);
6223}
6224
Francois Romieu07ce4062007-02-23 23:36:39 +01006225static void rtl_hw_start_8168(struct net_device *dev)
6226{
Francois Romieu2dd99532007-06-11 23:22:52 +02006227 struct rtl8169_private *tp = netdev_priv(dev);
6228 void __iomem *ioaddr = tp->mmio_addr;
6229
6230 RTL_W8(Cfg9346, Cfg9346_Unlock);
6231
françois romieuf0298f82011-01-03 15:07:42 +00006232 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02006233
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006234 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02006235
Francois Romieu0e485152007-02-20 00:00:26 +01006236 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02006237
6238 RTL_W16(CPlusCmd, tp->cp_cmd);
6239
Francois Romieu0e485152007-02-20 00:00:26 +01006240 RTL_W16(IntrMitigate, 0x5151);
6241
6242 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00006243 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006244 tp->event_slow |= RxFIFOOver | PCSTimeout;
6245 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01006246 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006247
6248 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6249
hayeswang1a964642013-04-01 22:23:41 +00006250 rtl_set_rx_tx_config_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006251
6252 RTL_R8(IntrMask);
6253
Francois Romieu219a1e92008-06-28 11:58:39 +02006254 switch (tp->mac_version) {
6255 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006256 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006257 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006258
6259 case RTL_GIGA_MAC_VER_12:
6260 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006261 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006262 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006263
6264 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006265 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006266 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006267
6268 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006269 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006270 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006271
6272 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006273 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006274 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006275
Francois Romieu197ff762008-06-28 13:16:02 +02006276 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006277 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006278 break;
Francois Romieu197ff762008-06-28 13:16:02 +02006279
Francois Romieu6fb07052008-06-29 11:54:28 +02006280 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006281 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006282 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02006283
Francois Romieuef3386f2008-06-29 12:24:30 +02006284 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006285 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006286 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02006287
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006288 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006289 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006290 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006291
Francois Romieu5b538df2008-07-20 16:22:45 +02006292 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00006293 case RTL_GIGA_MAC_VER_26:
6294 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006295 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006296 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02006297
françois romieue6de30d2011-01-03 15:08:37 +00006298 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006299 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006300 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02006301
hayeswang4804b3b2011-03-21 01:50:29 +00006302 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006303 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006304 break;
6305
hayeswang01dc7fe2011-03-21 01:50:28 +00006306 case RTL_GIGA_MAC_VER_32:
6307 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006308 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006309 break;
6310 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006311 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006312 break;
françois romieue6de30d2011-01-03 15:08:37 +00006313
Hayes Wangc2218922011-09-06 16:55:18 +08006314 case RTL_GIGA_MAC_VER_35:
6315 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006316 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006317 break;
6318
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006319 case RTL_GIGA_MAC_VER_38:
6320 rtl_hw_start_8411(tp);
6321 break;
6322
Hayes Wangc5583862012-07-02 17:23:22 +08006323 case RTL_GIGA_MAC_VER_40:
6324 case RTL_GIGA_MAC_VER_41:
6325 rtl_hw_start_8168g_1(tp);
6326 break;
hayeswang57538c42013-04-01 22:23:40 +00006327 case RTL_GIGA_MAC_VER_42:
6328 rtl_hw_start_8168g_2(tp);
6329 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006330
hayeswang45dd95c2013-07-08 17:09:01 +08006331 case RTL_GIGA_MAC_VER_44:
6332 rtl_hw_start_8411_2(tp);
6333 break;
6334
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006335 case RTL_GIGA_MAC_VER_45:
6336 case RTL_GIGA_MAC_VER_46:
6337 rtl_hw_start_8168h_1(tp);
6338 break;
6339
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006340 case RTL_GIGA_MAC_VER_49:
6341 rtl_hw_start_8168ep_1(tp);
6342 break;
6343
6344 case RTL_GIGA_MAC_VER_50:
6345 rtl_hw_start_8168ep_2(tp);
6346 break;
6347
6348 case RTL_GIGA_MAC_VER_51:
6349 rtl_hw_start_8168ep_3(tp);
6350 break;
6351
Francois Romieu219a1e92008-06-28 11:58:39 +02006352 default:
6353 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
6354 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00006355 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006356 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006357
hayeswang1a964642013-04-01 22:23:41 +00006358 RTL_W8(Cfg9346, Cfg9346_Lock);
6359
Francois Romieu0e485152007-02-20 00:00:26 +01006360 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
6361
hayeswang1a964642013-04-01 22:23:41 +00006362 rtl_set_rx_mode(dev);
Francois Romieub8363902008-06-01 12:31:57 +02006363
Chun-Hao Lin05b96872014-10-01 23:17:12 +08006364 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01006365}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366
Francois Romieu2857ffb2008-08-02 21:08:49 +02006367#define R810X_CPCMD_QUIRK_MASK (\
6368 EnableBist | \
6369 Mac_dbgo_oe | \
6370 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00006371 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02006372 Force_txflow_en | \
6373 Cxpl_dbg_sel | \
6374 ASF | \
6375 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006376 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006377
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006378static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006379{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006380 void __iomem *ioaddr = tp->mmio_addr;
6381 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006382 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02006383 { 0x01, 0, 0x6e65 },
6384 { 0x02, 0, 0x091f },
6385 { 0x03, 0, 0xc2f9 },
6386 { 0x06, 0, 0xafb5 },
6387 { 0x07, 0, 0x0e00 },
6388 { 0x19, 0, 0xec80 },
6389 { 0x01, 0, 0x2e65 },
6390 { 0x01, 0, 0x6e65 }
6391 };
6392 u8 cfg1;
6393
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006394 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006395
6396 RTL_W8(DBG_REG, FIX_NAK_1);
6397
6398 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6399
6400 RTL_W8(Config1,
6401 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
6402 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
6403
6404 cfg1 = RTL_R8(Config1);
6405 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
6406 RTL_W8(Config1, cfg1 & ~LEDS0);
6407
Francois Romieufdf6fc02012-07-06 22:40:38 +02006408 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02006409}
6410
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006411static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006412{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006413 void __iomem *ioaddr = tp->mmio_addr;
6414 struct pci_dev *pdev = tp->pci_dev;
6415
6416 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006417
6418 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6419
6420 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
6421 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006422}
6423
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006424static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006425{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006426 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006427
Francois Romieufdf6fc02012-07-06 22:40:38 +02006428 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006429}
6430
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006431static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006432{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006433 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006434 static const struct ephy_info e_info_8105e_1[] = {
6435 { 0x07, 0, 0x4000 },
6436 { 0x19, 0, 0x0200 },
6437 { 0x19, 0, 0x0020 },
6438 { 0x1e, 0, 0x2000 },
6439 { 0x03, 0, 0x0001 },
6440 { 0x19, 0, 0x0100 },
6441 { 0x19, 0, 0x0004 },
6442 { 0x0a, 0, 0x0020 }
6443 };
6444
Francois Romieucecb5fd2011-04-01 10:21:07 +02006445 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006446 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6447
Francois Romieucecb5fd2011-04-01 10:21:07 +02006448 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006449 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
6450
6451 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08006452 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006453
Francois Romieufdf6fc02012-07-06 22:40:38 +02006454 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08006455
6456 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006457}
6458
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006459static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006460{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006461 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006462 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006463}
6464
Hayes Wang7e18dca2012-03-30 14:33:02 +08006465static void rtl_hw_start_8402(struct rtl8169_private *tp)
6466{
6467 void __iomem *ioaddr = tp->mmio_addr;
6468 static const struct ephy_info e_info_8402[] = {
6469 { 0x19, 0xffff, 0xff64 },
6470 { 0x1e, 0, 0x4000 }
6471 };
6472
6473 rtl_csi_access_enable_2(tp);
6474
6475 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6476 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6477
6478 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6479 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6480
Francois Romieufdf6fc02012-07-06 22:40:38 +02006481 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006482
6483 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
6484
Francois Romieufdf6fc02012-07-06 22:40:38 +02006485 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6486 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006487 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6488 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006489 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6490 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006491 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006492
6493 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006494}
6495
Hayes Wang5598bfe2012-07-02 17:23:21 +08006496static void rtl_hw_start_8106(struct rtl8169_private *tp)
6497{
6498 void __iomem *ioaddr = tp->mmio_addr;
6499
6500 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6501 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6502
Francois Romieu4521e1a92012-11-01 16:46:28 +00006503 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006504 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
6505 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006506
6507 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006508}
6509
Francois Romieu07ce4062007-02-23 23:36:39 +01006510static void rtl_hw_start_8101(struct net_device *dev)
6511{
Francois Romieucdf1a602007-06-11 23:29:50 +02006512 struct rtl8169_private *tp = netdev_priv(dev);
6513 void __iomem *ioaddr = tp->mmio_addr;
6514 struct pci_dev *pdev = tp->pci_dev;
6515
Francois Romieuda78dbf2012-01-26 14:18:23 +01006516 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6517 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006518
Francois Romieucecb5fd2011-04-01 10:21:07 +02006519 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006520 tp->mac_version == RTL_GIGA_MAC_VER_16)
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006521 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
6522 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006523
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006524 RTL_W8(Cfg9346, Cfg9346_Unlock);
6525
hayeswang1a964642013-04-01 22:23:41 +00006526 RTL_W8(MaxTxPacketSize, TxPacketMax);
6527
6528 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
6529
6530 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
6531 RTL_W16(CPlusCmd, tp->cp_cmd);
6532
6533 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6534
6535 rtl_set_rx_tx_config_registers(tp);
6536
Francois Romieu2857ffb2008-08-02 21:08:49 +02006537 switch (tp->mac_version) {
6538 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006539 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006540 break;
6541
6542 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006543 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006544 break;
6545
6546 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006547 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006548 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006549
6550 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006551 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006552 break;
6553 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006554 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006555 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006556
6557 case RTL_GIGA_MAC_VER_37:
6558 rtl_hw_start_8402(tp);
6559 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006560
6561 case RTL_GIGA_MAC_VER_39:
6562 rtl_hw_start_8106(tp);
6563 break;
hayeswang58152cd2013-04-01 22:23:42 +00006564 case RTL_GIGA_MAC_VER_43:
6565 rtl_hw_start_8168g_2(tp);
6566 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006567 case RTL_GIGA_MAC_VER_47:
6568 case RTL_GIGA_MAC_VER_48:
6569 rtl_hw_start_8168h_1(tp);
6570 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006571 }
6572
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006573 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02006574
Francois Romieucdf1a602007-06-11 23:29:50 +02006575 RTL_W16(IntrMitigate, 0x0000);
6576
Francois Romieucdf1a602007-06-11 23:29:50 +02006577 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieucdf1a602007-06-11 23:29:50 +02006578
Francois Romieucdf1a602007-06-11 23:29:50 +02006579 rtl_set_rx_mode(dev);
6580
hayeswang1a964642013-04-01 22:23:41 +00006581 RTL_R8(IntrMask);
6582
Francois Romieucdf1a602007-06-11 23:29:50 +02006583 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584}
6585
6586static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6587{
Francois Romieud58d46b2011-05-03 16:38:29 +02006588 struct rtl8169_private *tp = netdev_priv(dev);
6589
6590 if (new_mtu < ETH_ZLEN ||
6591 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 return -EINVAL;
6593
Francois Romieud58d46b2011-05-03 16:38:29 +02006594 if (new_mtu > ETH_DATA_LEN)
6595 rtl_hw_jumbo_enable(tp);
6596 else
6597 rtl_hw_jumbo_disable(tp);
6598
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006600 netdev_update_features(dev);
6601
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603}
6604
6605static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6606{
Al Viro95e09182007-12-22 18:55:39 +00006607 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6609}
6610
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006611static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6612 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006614 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006615 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006616
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006617 kfree(*data_buff);
6618 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619 rtl8169_make_unusable_by_asic(desc);
6620}
6621
6622static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
6623{
6624 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6625
Alexander Duycka0750132014-12-11 15:02:17 -08006626 /* Force memory writes to complete before releasing descriptor */
6627 dma_wmb();
6628
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
6630}
6631
6632static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
6633 u32 rx_buf_sz)
6634{
6635 desc->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 rtl8169_mark_to_asic(desc, rx_buf_sz);
6637}
6638
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006639static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006641 return (void *)ALIGN((long)data, 16);
6642}
6643
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006644static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6645 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006646{
6647 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006649 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006650 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006651 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006653 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
6654 if (!data)
6655 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006656
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006657 if (rtl8169_align(data) != data) {
6658 kfree(data);
6659 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
6660 if (!data)
6661 return NULL;
6662 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006663
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006664 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006665 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006666 if (unlikely(dma_mapping_error(d, mapping))) {
6667 if (net_ratelimit())
6668 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006669 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671
6672 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006673 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006674
6675err_out:
6676 kfree(data);
6677 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678}
6679
6680static void rtl8169_rx_clear(struct rtl8169_private *tp)
6681{
Francois Romieu07d3f512007-02-21 22:40:46 +01006682 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683
6684 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006685 if (tp->Rx_databuff[i]) {
6686 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687 tp->RxDescArray + i);
6688 }
6689 }
6690}
6691
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006692static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006694 desc->opts1 |= cpu_to_le32(RingEnd);
6695}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006696
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006697static int rtl8169_rx_fill(struct rtl8169_private *tp)
6698{
6699 unsigned int i;
6700
6701 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006702 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006703
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006704 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02006706
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006707 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006708 if (!data) {
6709 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006710 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006711 }
6712 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006715 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6716 return 0;
6717
6718err_out:
6719 rtl8169_rx_clear(tp);
6720 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721}
6722
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723static int rtl8169_init_ring(struct net_device *dev)
6724{
6725 struct rtl8169_private *tp = netdev_priv(dev);
6726
6727 rtl8169_init_ring_indexes(tp);
6728
6729 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006730 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006732 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733}
6734
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006735static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 struct TxDesc *desc)
6737{
6738 unsigned int len = tx_skb->len;
6739
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006740 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6741
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742 desc->opts1 = 0x00;
6743 desc->opts2 = 0x00;
6744 desc->addr = 0x00;
6745 tx_skb->len = 0;
6746}
6747
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006748static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6749 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750{
6751 unsigned int i;
6752
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006753 for (i = 0; i < n; i++) {
6754 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755 struct ring_info *tx_skb = tp->tx_skb + entry;
6756 unsigned int len = tx_skb->len;
6757
6758 if (len) {
6759 struct sk_buff *skb = tx_skb->skb;
6760
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006761 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 tp->TxDescArray + entry);
6763 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00006764 tp->dev->stats.tx_dropped++;
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006765 dev_kfree_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766 tx_skb->skb = NULL;
6767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 }
6769 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006770}
6771
6772static void rtl8169_tx_clear(struct rtl8169_private *tp)
6773{
6774 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775 tp->cur_tx = tp->dirty_tx = 0;
6776}
6777
Francois Romieu4422bcd2012-01-26 11:23:32 +01006778static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779{
David Howellsc4028952006-11-22 14:57:56 +00006780 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01006781 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782
Francois Romieuda78dbf2012-01-26 14:18:23 +01006783 napi_disable(&tp->napi);
6784 netif_stop_queue(dev);
6785 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786
françois romieuc7c2c392011-12-04 20:30:52 +00006787 rtl8169_hw_reset(tp);
6788
Francois Romieu56de4142011-03-15 17:29:31 +01006789 for (i = 0; i < NUM_RX_DESC; i++)
6790 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
6791
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00006793 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794
Francois Romieuda78dbf2012-01-26 14:18:23 +01006795 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01006796 rtl_hw_start(dev);
6797 netif_wake_queue(dev);
6798 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799}
6800
6801static void rtl8169_tx_timeout(struct net_device *dev)
6802{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006803 struct rtl8169_private *tp = netdev_priv(dev);
6804
6805 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806}
6807
6808static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07006809 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810{
6811 struct skb_shared_info *info = skb_shinfo(skb);
6812 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006813 struct TxDesc *uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006814 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815
6816 entry = tp->cur_tx;
6817 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006818 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 dma_addr_t mapping;
6820 u32 status, len;
6821 void *addr;
6822
6823 entry = (entry + 1) % NUM_TX_DESC;
6824
6825 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00006826 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00006827 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006828 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006829 if (unlikely(dma_mapping_error(d, mapping))) {
6830 if (net_ratelimit())
6831 netif_err(tp, drv, tp->dev,
6832 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006833 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835
Francois Romieucecb5fd2011-04-01 10:21:07 +02006836 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006837 status = opts[0] | len |
6838 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839
6840 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07006841 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 txd->addr = cpu_to_le64(mapping);
6843
6844 tp->tx_skb[entry].len = len;
6845 }
6846
6847 if (cur_frag) {
6848 tp->tx_skb[entry].skb = skb;
6849 txd->opts1 |= cpu_to_le32(LastFrag);
6850 }
6851
6852 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006853
6854err_out:
6855 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6856 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857}
6858
françois romieub423e9a2013-05-18 01:24:46 +00006859static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6860{
6861 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6862}
6863
hayeswange9746042014-07-11 16:25:58 +08006864static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6865 struct net_device *dev);
6866/* r8169_csum_workaround()
6867 * The hw limites the value the transport offset. When the offset is out of the
6868 * range, calculate the checksum by sw.
6869 */
6870static void r8169_csum_workaround(struct rtl8169_private *tp,
6871 struct sk_buff *skb)
6872{
6873 if (skb_shinfo(skb)->gso_size) {
6874 netdev_features_t features = tp->dev->features;
6875 struct sk_buff *segs, *nskb;
6876
6877 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6878 segs = skb_gso_segment(skb, features);
6879 if (IS_ERR(segs) || !segs)
6880 goto drop;
6881
6882 do {
6883 nskb = segs;
6884 segs = segs->next;
6885 nskb->next = NULL;
6886 rtl8169_start_xmit(nskb, tp->dev);
6887 } while (segs);
6888
Alexander Duyckeb781392015-05-01 10:34:44 -07006889 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006890 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6891 if (skb_checksum_help(skb) < 0)
6892 goto drop;
6893
6894 rtl8169_start_xmit(skb, tp->dev);
6895 } else {
6896 struct net_device_stats *stats;
6897
6898drop:
6899 stats = &tp->dev->stats;
6900 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07006901 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006902 }
6903}
6904
6905/* msdn_giant_send_check()
6906 * According to the document of microsoft, the TCP Pseudo Header excludes the
6907 * packet length for IPv6 TCP large packets.
6908 */
6909static int msdn_giant_send_check(struct sk_buff *skb)
6910{
6911 const struct ipv6hdr *ipv6h;
6912 struct tcphdr *th;
6913 int ret;
6914
6915 ret = skb_cow_head(skb, 0);
6916 if (ret)
6917 return ret;
6918
6919 ipv6h = ipv6_hdr(skb);
6920 th = tcp_hdr(skb);
6921
6922 th->check = 0;
6923 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6924
6925 return ret;
6926}
6927
6928static inline __be16 get_protocol(struct sk_buff *skb)
6929{
6930 __be16 protocol;
6931
6932 if (skb->protocol == htons(ETH_P_8021Q))
6933 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
6934 else
6935 protocol = skb->protocol;
6936
6937 return protocol;
6938}
6939
hayeswang5888d3f2014-07-11 16:25:56 +08006940static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6941 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942{
Michał Mirosław350fb322011-04-08 06:35:56 +00006943 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944
Francois Romieu2b7b4312011-04-18 22:53:24 -07006945 if (mss) {
6946 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08006947 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6948 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6949 const struct iphdr *ip = ip_hdr(skb);
6950
6951 if (ip->protocol == IPPROTO_TCP)
6952 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6953 else if (ip->protocol == IPPROTO_UDP)
6954 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6955 else
6956 WARN_ON_ONCE(1);
6957 }
6958
6959 return true;
6960}
6961
6962static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6963 struct sk_buff *skb, u32 *opts)
6964{
hayeswangbdfa4ed2014-07-11 16:25:57 +08006965 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08006966 u32 mss = skb_shinfo(skb)->gso_size;
6967
6968 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08006969 if (transport_offset > GTTCPHO_MAX) {
6970 netif_warn(tp, tx_err, tp->dev,
6971 "Invalid transport offset 0x%x for TSO\n",
6972 transport_offset);
6973 return false;
6974 }
6975
6976 switch (get_protocol(skb)) {
6977 case htons(ETH_P_IP):
6978 opts[0] |= TD1_GTSENV4;
6979 break;
6980
6981 case htons(ETH_P_IPV6):
6982 if (msdn_giant_send_check(skb))
6983 return false;
6984
6985 opts[0] |= TD1_GTSENV6;
6986 break;
6987
6988 default:
6989 WARN_ON_ONCE(1);
6990 break;
6991 }
6992
hayeswangbdfa4ed2014-07-11 16:25:57 +08006993 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08006994 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006995 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08006996 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997
françois romieub423e9a2013-05-18 01:24:46 +00006998 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006999 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00007000
hayeswange9746042014-07-11 16:25:58 +08007001 if (transport_offset > TCPHO_MAX) {
7002 netif_warn(tp, tx_err, tp->dev,
7003 "Invalid transport offset 0x%x\n",
7004 transport_offset);
7005 return false;
7006 }
7007
7008 switch (get_protocol(skb)) {
7009 case htons(ETH_P_IP):
7010 opts[1] |= TD1_IPv4_CS;
7011 ip_protocol = ip_hdr(skb)->protocol;
7012 break;
7013
7014 case htons(ETH_P_IPV6):
7015 opts[1] |= TD1_IPv6_CS;
7016 ip_protocol = ipv6_hdr(skb)->nexthdr;
7017 break;
7018
7019 default:
7020 ip_protocol = IPPROTO_RAW;
7021 break;
7022 }
7023
7024 if (ip_protocol == IPPROTO_TCP)
7025 opts[1] |= TD1_TCP_CS;
7026 else if (ip_protocol == IPPROTO_UDP)
7027 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007028 else
7029 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08007030
7031 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00007032 } else {
7033 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007034 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035 }
hayeswang5888d3f2014-07-11 16:25:56 +08007036
françois romieub423e9a2013-05-18 01:24:46 +00007037 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038}
7039
Stephen Hemminger613573252009-08-31 19:50:58 +00007040static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7041 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042{
7043 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007044 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045 struct TxDesc *txd = tp->TxDescArray + entry;
7046 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007047 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048 dma_addr_t mapping;
7049 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007050 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007051 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02007052
Julien Ducourthial477206a2012-05-09 00:00:06 +02007053 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007054 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007055 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056 }
7057
7058 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007059 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060
françois romieub423e9a2013-05-18 01:24:46 +00007061 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
7062 opts[0] = DescOwn;
7063
hayeswange9746042014-07-11 16:25:58 +08007064 if (!tp->tso_csum(tp, skb, opts)) {
7065 r8169_csum_workaround(tp, skb);
7066 return NETDEV_TX_OK;
7067 }
françois romieub423e9a2013-05-18 01:24:46 +00007068
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007069 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007070 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007071 if (unlikely(dma_mapping_error(d, mapping))) {
7072 if (net_ratelimit())
7073 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007074 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076
7077 tp->tx_skb[entry].len = len;
7078 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079
Francois Romieu2b7b4312011-04-18 22:53:24 -07007080 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007081 if (frags < 0)
7082 goto err_dma_1;
7083 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07007084 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007085 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07007086 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007087 tp->tx_skb[entry].skb = skb;
7088 }
7089
Francois Romieu2b7b4312011-04-18 22:53:24 -07007090 txd->opts2 = cpu_to_le32(opts[1]);
7091
Richard Cochran5047fb52012-03-10 07:29:42 +00007092 skb_tx_timestamp(skb);
7093
Alexander Duycka0750132014-12-11 15:02:17 -08007094 /* Force memory writes to complete before releasing descriptor */
7095 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096
Francois Romieucecb5fd2011-04-01 10:21:07 +02007097 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007098 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099 txd->opts1 = cpu_to_le32(status);
7100
Alexander Duycka0750132014-12-11 15:02:17 -08007101 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00007102 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103
Alexander Duycka0750132014-12-11 15:02:17 -08007104 tp->cur_tx += frags + 1;
7105
David S. Miller87cda7c2015-02-22 15:54:29 -05007106 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107
David S. Miller87cda7c2015-02-22 15:54:29 -05007108 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01007109
David S. Miller87cda7c2015-02-22 15:54:29 -05007110 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01007111 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
7112 * not miss a ring update when it notices a stopped queue.
7113 */
7114 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01007116 /* Sync with rtl_tx:
7117 * - publish queue status and cur_tx ring index (write barrier)
7118 * - refresh dirty_tx ring index (read barrier).
7119 * May the current thread have a pessimistic view of the ring
7120 * status and forget to wake up queue, a racing rtl_tx thread
7121 * can't.
7122 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007123 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02007124 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125 netif_wake_queue(dev);
7126 }
7127
Stephen Hemminger613573252009-08-31 19:50:58 +00007128 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007130err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007131 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007132err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07007133 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007134 dev->stats.tx_dropped++;
7135 return NETDEV_TX_OK;
7136
7137err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007139 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00007140 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141}
7142
7143static void rtl8169_pcierr_interrupt(struct net_device *dev)
7144{
7145 struct rtl8169_private *tp = netdev_priv(dev);
7146 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 u16 pci_status, pci_cmd;
7148
7149 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
7150 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
7151
Joe Perchesbf82c182010-02-09 11:49:50 +00007152 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
7153 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154
7155 /*
7156 * The recovery sequence below admits a very elaborated explanation:
7157 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01007158 * - I did not see what else could be done;
7159 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160 *
7161 * Feel free to adjust to your needs.
7162 */
Francois Romieua27993f2006-12-18 00:04:19 +01007163 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01007164 pci_cmd &= ~PCI_COMMAND_PARITY;
7165 else
7166 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
7167
7168 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169
7170 pci_write_config_word(pdev, PCI_STATUS,
7171 pci_status & (PCI_STATUS_DETECTED_PARITY |
7172 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
7173 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
7174
7175 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00007176 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00007177 void __iomem *ioaddr = tp->mmio_addr;
7178
Joe Perchesbf82c182010-02-09 11:49:50 +00007179 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180 tp->cp_cmd &= ~PCIDAC;
7181 RTL_W16(CPlusCmd, tp->cp_cmd);
7182 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183 }
7184
françois romieue6de30d2011-01-03 15:08:37 +00007185 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01007186
Francois Romieu98ddf982012-01-31 10:47:34 +01007187 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188}
7189
Francois Romieuda78dbf2012-01-26 14:18:23 +01007190static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191{
7192 unsigned int dirty_tx, tx_left;
7193
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194 dirty_tx = tp->dirty_tx;
7195 smp_rmb();
7196 tx_left = tp->cur_tx - dirty_tx;
7197
7198 while (tx_left > 0) {
7199 unsigned int entry = dirty_tx % NUM_TX_DESC;
7200 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201 u32 status;
7202
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7204 if (status & DescOwn)
7205 break;
7206
Alexander Duycka0750132014-12-11 15:02:17 -08007207 /* This barrier is needed to keep us from reading
7208 * any other fields out of the Tx descriptor until
7209 * we know the status of DescOwn
7210 */
7211 dma_rmb();
7212
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007213 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
7214 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05007216 u64_stats_update_begin(&tp->tx_stats.syncp);
7217 tp->tx_stats.packets++;
7218 tp->tx_stats.bytes += tx_skb->skb->len;
7219 u64_stats_update_end(&tp->tx_stats.syncp);
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07007220 dev_kfree_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 tx_skb->skb = NULL;
7222 }
7223 dirty_tx++;
7224 tx_left--;
7225 }
7226
7227 if (tp->dirty_tx != dirty_tx) {
7228 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01007229 /* Sync with rtl8169_start_xmit:
7230 * - publish dirty_tx ring index (write barrier)
7231 * - refresh cur_tx ring index and queue status (read barrier)
7232 * May the current thread miss the stopped queue condition,
7233 * a racing xmit thread can only have a right view of the
7234 * ring status.
7235 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007236 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02007238 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239 netif_wake_queue(dev);
7240 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02007241 /*
7242 * 8168 hack: TxPoll requests are lost when the Tx packets are
7243 * too close. Let's kick an extra TxPoll request when a burst
7244 * of start_xmit activity is detected (if it is not detected,
7245 * it is slow enough). -- FR
7246 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007247 if (tp->cur_tx != dirty_tx) {
7248 void __iomem *ioaddr = tp->mmio_addr;
7249
Francois Romieud78ae2d2007-08-26 20:08:19 +02007250 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252 }
7253}
7254
Francois Romieu126fa4b2005-05-12 20:09:17 -04007255static inline int rtl8169_fragmented_frame(u32 status)
7256{
7257 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7258}
7259
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007260static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007262 u32 status = opts1 & RxProtoMask;
7263
7264 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00007265 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266 skb->ip_summed = CHECKSUM_UNNECESSARY;
7267 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07007268 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269}
7270
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007271static struct sk_buff *rtl8169_try_rx_copy(void *data,
7272 struct rtl8169_private *tp,
7273 int pkt_size,
7274 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02007276 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007277 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007279 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007280 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007281 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08007282 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007283 if (skb)
7284 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007285 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7286
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007287 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288}
7289
Francois Romieuda78dbf2012-01-26 14:18:23 +01007290static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291{
7292 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007293 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296
Timo Teräs9fba0812013-01-15 21:01:24 +00007297 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007298 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007299 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300 u32 status;
7301
David S. Miller8decf862011-09-22 03:23:13 -04007302 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303 if (status & DescOwn)
7304 break;
Alexander Duycka0750132014-12-11 15:02:17 -08007305
7306 /* This barrier is needed to keep us from reading
7307 * any other fields out of the Rx descriptor until
7308 * we know the status of DescOwn
7309 */
7310 dma_rmb();
7311
Richard Dawe4dcb7d32005-05-27 21:12:00 +02007312 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007313 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7314 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007315 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02007317 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02007319 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007320 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01007321 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007322 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007323 }
Ben Greear6bbe0212012-02-10 15:04:33 +00007324 if ((status & (RxRUNT | RxCRC)) &&
7325 !(status & (RxRWT | RxFOVF)) &&
7326 (dev->features & NETIF_F_RXALL))
7327 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007328 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007329 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00007330 dma_addr_t addr;
7331 int pkt_size;
7332
7333process_pkt:
7334 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00007335 if (likely(!(dev->features & NETIF_F_RXFCS)))
7336 pkt_size = (status & 0x00003fff) - 4;
7337 else
7338 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339
Francois Romieu126fa4b2005-05-12 20:09:17 -04007340 /*
7341 * The driver does not support incoming fragmented
7342 * frames. They are seen as a symptom of over-mtu
7343 * sized frames.
7344 */
7345 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02007346 dev->stats.rx_dropped++;
7347 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00007348 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007349 }
7350
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007351 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7352 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007353 if (!skb) {
7354 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00007355 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356 }
7357
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007358 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007359 skb_put(skb, pkt_size);
7360 skb->protocol = eth_type_trans(skb, dev);
7361
Francois Romieu7a8fc772011-03-01 17:18:33 +01007362 rtl8169_rx_vlan_tag(desc, skb);
7363
Francois Romieu56de4142011-03-15 17:29:31 +01007364 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365
Junchang Wang8027aa22012-03-04 23:30:32 +01007366 u64_stats_update_begin(&tp->rx_stats.syncp);
7367 tp->rx_stats.packets++;
7368 tp->rx_stats.bytes += pkt_size;
7369 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370 }
françois romieuce11ff52013-01-24 13:30:06 +00007371release_descriptor:
7372 desc->opts2 = 0;
françois romieuce11ff52013-01-24 13:30:06 +00007373 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374 }
7375
7376 count = cur_rx - tp->cur_rx;
7377 tp->cur_rx = cur_rx;
7378
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379 return count;
7380}
7381
Francois Romieu07d3f512007-02-21 22:40:46 +01007382static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383{
Francois Romieu07d3f512007-02-21 22:40:46 +01007384 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007387 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007389 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007390 if (status && status != 0xffff) {
7391 status &= RTL_EVENT_NAPI | tp->event_slow;
7392 if (status) {
7393 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00007394
Francois Romieuda78dbf2012-01-26 14:18:23 +01007395 rtl_irq_disable(tp);
7396 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399 return IRQ_RETVAL(handled);
7400}
7401
Francois Romieuda78dbf2012-01-26 14:18:23 +01007402/*
7403 * Workqueue context.
7404 */
7405static void rtl_slow_event_work(struct rtl8169_private *tp)
7406{
7407 struct net_device *dev = tp->dev;
7408 u16 status;
7409
7410 status = rtl_get_events(tp) & tp->event_slow;
7411 rtl_ack_events(tp, status);
7412
7413 if (unlikely(status & RxFIFOOver)) {
7414 switch (tp->mac_version) {
7415 /* Work around for rx fifo overflow */
7416 case RTL_GIGA_MAC_VER_11:
7417 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01007418 /* XXX - Hack alert. See rtl_task(). */
7419 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007420 default:
7421 break;
7422 }
7423 }
7424
7425 if (unlikely(status & SYSErr))
7426 rtl8169_pcierr_interrupt(dev);
7427
7428 if (status & LinkChg)
7429 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
7430
françois romieu7dbb4912012-06-09 10:53:16 +00007431 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007432}
7433
Francois Romieu4422bcd2012-01-26 11:23:32 +01007434static void rtl_task(struct work_struct *work)
7435{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007436 static const struct {
7437 int bitnr;
7438 void (*action)(struct rtl8169_private *);
7439 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01007440 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007441 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7442 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7443 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7444 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01007445 struct rtl8169_private *tp =
7446 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007447 struct net_device *dev = tp->dev;
7448 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01007449
Francois Romieuda78dbf2012-01-26 14:18:23 +01007450 rtl_lock_work(tp);
7451
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007452 if (!netif_running(dev) ||
7453 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01007454 goto out_unlock;
7455
7456 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7457 bool pending;
7458
Francois Romieuda78dbf2012-01-26 14:18:23 +01007459 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007460 if (pending)
7461 rtl_work[i].action(tp);
7462 }
7463
7464out_unlock:
7465 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01007466}
7467
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007468static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007470 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7471 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007472 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7473 int work_done= 0;
7474 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475
Francois Romieuda78dbf2012-01-26 14:18:23 +01007476 status = rtl_get_events(tp);
7477 rtl_ack_events(tp, status & ~tp->event_slow);
7478
7479 if (status & RTL_EVENT_NAPI_RX)
7480 work_done = rtl_rx(dev, tp, (u32) budget);
7481
7482 if (status & RTL_EVENT_NAPI_TX)
7483 rtl_tx(dev, tp);
7484
7485 if (status & tp->event_slow) {
7486 enable_mask &= ~tp->event_slow;
7487
7488 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007491 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08007492 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00007493
Francois Romieuda78dbf2012-01-26 14:18:23 +01007494 rtl_irq_enable(tp, enable_mask);
7495 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496 }
7497
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007498 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500
Francois Romieu523a6092008-09-10 22:28:56 +02007501static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
7502{
7503 struct rtl8169_private *tp = netdev_priv(dev);
7504
7505 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7506 return;
7507
7508 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
7509 RTL_W32(RxMissed, 0);
7510}
7511
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512static void rtl8169_down(struct net_device *dev)
7513{
7514 struct rtl8169_private *tp = netdev_priv(dev);
7515 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516
Francois Romieu4876cc12011-03-11 21:07:11 +01007517 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007519 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007520 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521
Hayes Wang92fc43b2011-07-06 15:58:03 +08007522 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007523 /*
7524 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007525 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7526 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007527 */
Francois Romieu523a6092008-09-10 22:28:56 +02007528 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007529
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007531 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533 rtl8169_tx_clear(tp);
7534
7535 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007536
7537 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538}
7539
7540static int rtl8169_close(struct net_device *dev)
7541{
7542 struct rtl8169_private *tp = netdev_priv(dev);
7543 struct pci_dev *pdev = tp->pci_dev;
7544
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007545 pm_runtime_get_sync(&pdev->dev);
7546
Francois Romieucecb5fd2011-04-01 10:21:07 +02007547 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08007548 rtl8169_update_counters(dev);
7549
Francois Romieuda78dbf2012-01-26 14:18:23 +01007550 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007551 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007552
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007554 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555
Lekensteyn4ea72442013-07-22 09:53:30 +02007556 cancel_work_sync(&tp->wk.work);
7557
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007558 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007560 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7561 tp->RxPhyAddr);
7562 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7563 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564 tp->TxDescArray = NULL;
7565 tp->RxDescArray = NULL;
7566
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007567 pm_runtime_put_sync(&pdev->dev);
7568
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 return 0;
7570}
7571
Francois Romieudc1c00c2012-03-08 10:06:18 +01007572#ifdef CONFIG_NET_POLL_CONTROLLER
7573static void rtl8169_netpoll(struct net_device *dev)
7574{
7575 struct rtl8169_private *tp = netdev_priv(dev);
7576
7577 rtl8169_interrupt(tp->pci_dev->irq, dev);
7578}
7579#endif
7580
Francois Romieudf43ac72012-03-08 09:48:40 +01007581static int rtl_open(struct net_device *dev)
7582{
7583 struct rtl8169_private *tp = netdev_priv(dev);
7584 void __iomem *ioaddr = tp->mmio_addr;
7585 struct pci_dev *pdev = tp->pci_dev;
7586 int retval = -ENOMEM;
7587
7588 pm_runtime_get_sync(&pdev->dev);
7589
7590 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007591 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007592 * dma_alloc_coherent provides more.
7593 */
7594 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7595 &tp->TxPhyAddr, GFP_KERNEL);
7596 if (!tp->TxDescArray)
7597 goto err_pm_runtime_put;
7598
7599 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7600 &tp->RxPhyAddr, GFP_KERNEL);
7601 if (!tp->RxDescArray)
7602 goto err_free_tx_0;
7603
7604 retval = rtl8169_init_ring(dev);
7605 if (retval < 0)
7606 goto err_free_rx_1;
7607
7608 INIT_WORK(&tp->wk.work, rtl_task);
7609
7610 smp_mb();
7611
7612 rtl_request_firmware(tp);
7613
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007614 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01007615 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
7616 dev->name, dev);
7617 if (retval < 0)
7618 goto err_release_fw_2;
7619
7620 rtl_lock_work(tp);
7621
7622 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7623
7624 napi_enable(&tp->napi);
7625
7626 rtl8169_init_phy(dev, tp);
7627
7628 __rtl8169_set_features(dev, dev->features);
7629
7630 rtl_pll_power_up(tp);
7631
7632 rtl_hw_start(dev);
7633
7634 netif_start_queue(dev);
7635
7636 rtl_unlock_work(tp);
7637
7638 tp->saved_wolopts = 0;
7639 pm_runtime_put_noidle(&pdev->dev);
7640
7641 rtl8169_check_link_status(dev, tp, ioaddr);
7642out:
7643 return retval;
7644
7645err_release_fw_2:
7646 rtl_release_firmware(tp);
7647 rtl8169_rx_clear(tp);
7648err_free_rx_1:
7649 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7650 tp->RxPhyAddr);
7651 tp->RxDescArray = NULL;
7652err_free_tx_0:
7653 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7654 tp->TxPhyAddr);
7655 tp->TxDescArray = NULL;
7656err_pm_runtime_put:
7657 pm_runtime_put_noidle(&pdev->dev);
7658 goto out;
7659}
7660
Junchang Wang8027aa22012-03-04 23:30:32 +01007661static struct rtnl_link_stats64 *
7662rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007663{
7664 struct rtl8169_private *tp = netdev_priv(dev);
7665 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01007666 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667
Francois Romieuda78dbf2012-01-26 14:18:23 +01007668 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02007669 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007670
Junchang Wang8027aa22012-03-04 23:30:32 +01007671 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007672 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007673 stats->rx_packets = tp->rx_stats.packets;
7674 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007675 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007676
7677
7678 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007679 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007680 stats->tx_packets = tp->tx_stats.packets;
7681 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007682 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007683
7684 stats->rx_dropped = dev->stats.rx_dropped;
7685 stats->tx_dropped = dev->stats.tx_dropped;
7686 stats->rx_length_errors = dev->stats.rx_length_errors;
7687 stats->rx_errors = dev->stats.rx_errors;
7688 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7689 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7690 stats->rx_missed_errors = dev->stats.rx_missed_errors;
7691
7692 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007693}
7694
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007695static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01007696{
françois romieu065c27c2011-01-03 15:08:12 +00007697 struct rtl8169_private *tp = netdev_priv(dev);
7698
Francois Romieu5d06a992006-02-23 00:47:58 +01007699 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007700 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01007701
7702 netif_device_detach(dev);
7703 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007704
7705 rtl_lock_work(tp);
7706 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007707 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007708 rtl_unlock_work(tp);
7709
7710 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007711}
Francois Romieu5d06a992006-02-23 00:47:58 +01007712
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007713#ifdef CONFIG_PM
7714
7715static int rtl8169_suspend(struct device *device)
7716{
7717 struct pci_dev *pdev = to_pci_dev(device);
7718 struct net_device *dev = pci_get_drvdata(pdev);
7719
7720 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02007721
Francois Romieu5d06a992006-02-23 00:47:58 +01007722 return 0;
7723}
7724
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007725static void __rtl8169_resume(struct net_device *dev)
7726{
françois romieu065c27c2011-01-03 15:08:12 +00007727 struct rtl8169_private *tp = netdev_priv(dev);
7728
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007729 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00007730
7731 rtl_pll_power_up(tp);
7732
Artem Savkovcff4c162012-04-03 10:29:11 +00007733 rtl_lock_work(tp);
7734 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007735 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00007736 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007737
Francois Romieu98ddf982012-01-31 10:47:34 +01007738 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007739}
7740
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007741static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01007742{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007743 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01007744 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007745 struct rtl8169_private *tp = netdev_priv(dev);
7746
7747 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01007748
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007749 if (netif_running(dev))
7750 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01007751
Francois Romieu5d06a992006-02-23 00:47:58 +01007752 return 0;
7753}
7754
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007755static int rtl8169_runtime_suspend(struct device *device)
7756{
7757 struct pci_dev *pdev = to_pci_dev(device);
7758 struct net_device *dev = pci_get_drvdata(pdev);
7759 struct rtl8169_private *tp = netdev_priv(dev);
7760
7761 if (!tp->TxDescArray)
7762 return 0;
7763
Francois Romieuda78dbf2012-01-26 14:18:23 +01007764 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007765 tp->saved_wolopts = __rtl8169_get_wol(tp);
7766 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007767 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007768
7769 rtl8169_net_suspend(dev);
7770
7771 return 0;
7772}
7773
7774static int rtl8169_runtime_resume(struct device *device)
7775{
7776 struct pci_dev *pdev = to_pci_dev(device);
7777 struct net_device *dev = pci_get_drvdata(pdev);
7778 struct rtl8169_private *tp = netdev_priv(dev);
7779
7780 if (!tp->TxDescArray)
7781 return 0;
7782
Francois Romieuda78dbf2012-01-26 14:18:23 +01007783 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007784 __rtl8169_set_wol(tp, tp->saved_wolopts);
7785 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007786 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007787
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007788 rtl8169_init_phy(dev, tp);
7789
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007790 __rtl8169_resume(dev);
7791
7792 return 0;
7793}
7794
7795static int rtl8169_runtime_idle(struct device *device)
7796{
7797 struct pci_dev *pdev = to_pci_dev(device);
7798 struct net_device *dev = pci_get_drvdata(pdev);
7799 struct rtl8169_private *tp = netdev_priv(dev);
7800
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00007801 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007802}
7803
Alexey Dobriyan47145212009-12-14 18:00:08 -08007804static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02007805 .suspend = rtl8169_suspend,
7806 .resume = rtl8169_resume,
7807 .freeze = rtl8169_suspend,
7808 .thaw = rtl8169_resume,
7809 .poweroff = rtl8169_suspend,
7810 .restore = rtl8169_resume,
7811 .runtime_suspend = rtl8169_runtime_suspend,
7812 .runtime_resume = rtl8169_runtime_resume,
7813 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007814};
7815
7816#define RTL8169_PM_OPS (&rtl8169_pm_ops)
7817
7818#else /* !CONFIG_PM */
7819
7820#define RTL8169_PM_OPS NULL
7821
7822#endif /* !CONFIG_PM */
7823
David S. Miller1805b2f2011-10-24 18:18:09 -04007824static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7825{
7826 void __iomem *ioaddr = tp->mmio_addr;
7827
7828 /* WoL fails with 8168b when the receiver is disabled. */
7829 switch (tp->mac_version) {
7830 case RTL_GIGA_MAC_VER_11:
7831 case RTL_GIGA_MAC_VER_12:
7832 case RTL_GIGA_MAC_VER_17:
7833 pci_clear_master(tp->pci_dev);
7834
7835 RTL_W8(ChipCmd, CmdRxEnb);
7836 /* PCI commit */
7837 RTL_R8(ChipCmd);
7838 break;
7839 default:
7840 break;
7841 }
7842}
7843
Francois Romieu1765f952008-09-13 17:21:40 +02007844static void rtl_shutdown(struct pci_dev *pdev)
7845{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007846 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00007847 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00007848 struct device *d = &pdev->dev;
7849
7850 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02007851
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007852 rtl8169_net_suspend(dev);
7853
Francois Romieucecb5fd2011-04-01 10:21:07 +02007854 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08007855 rtl_rar_set(tp, dev->perm_addr);
7856
Hayes Wang92fc43b2011-07-06 15:58:03 +08007857 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00007858
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007859 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04007860 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
7861 rtl_wol_suspend_quirk(tp);
7862 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00007863 }
7864
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007865 pci_wake_from_d3(pdev, true);
7866 pci_set_power_state(pdev, PCI_D3hot);
7867 }
françois romieu2a15cd22012-03-06 01:14:12 +00007868
7869 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007870}
Francois Romieu5d06a992006-02-23 00:47:58 +01007871
Bill Pembertonbaf63292012-12-03 09:23:28 -05007872static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01007873{
7874 struct net_device *dev = pci_get_drvdata(pdev);
7875 struct rtl8169_private *tp = netdev_priv(dev);
7876
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08007877 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
7878 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08007879 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
7880 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
7881 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
7882 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08007883 r8168_check_dash(tp)) {
Francois Romieue27566e2012-03-08 09:54:01 +01007884 rtl8168_driver_stop(tp);
7885 }
7886
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007887 netif_napi_del(&tp->napi);
7888
Francois Romieue27566e2012-03-08 09:54:01 +01007889 unregister_netdev(dev);
7890
7891 rtl_release_firmware(tp);
7892
7893 if (pci_dev_run_wake(pdev))
7894 pm_runtime_get_noresume(&pdev->dev);
7895
7896 /* restore original MAC address */
7897 rtl_rar_set(tp, dev->perm_addr);
7898
7899 rtl_disable_msi(pdev, tp);
7900 rtl8169_release_board(pdev, dev, tp->mmio_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01007901}
7902
Francois Romieufa9c3852012-03-08 10:01:50 +01007903static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01007904 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01007905 .ndo_stop = rtl8169_close,
7906 .ndo_get_stats64 = rtl8169_get_stats64,
7907 .ndo_start_xmit = rtl8169_start_xmit,
7908 .ndo_tx_timeout = rtl8169_tx_timeout,
7909 .ndo_validate_addr = eth_validate_addr,
7910 .ndo_change_mtu = rtl8169_change_mtu,
7911 .ndo_fix_features = rtl8169_fix_features,
7912 .ndo_set_features = rtl8169_set_features,
7913 .ndo_set_mac_address = rtl_set_mac_address,
7914 .ndo_do_ioctl = rtl8169_ioctl,
7915 .ndo_set_rx_mode = rtl_set_rx_mode,
7916#ifdef CONFIG_NET_POLL_CONTROLLER
7917 .ndo_poll_controller = rtl8169_netpoll,
7918#endif
7919
7920};
7921
Francois Romieu31fa8b12012-03-08 10:09:40 +01007922static const struct rtl_cfg_info {
7923 void (*hw_start)(struct net_device *);
7924 unsigned int region;
7925 unsigned int align;
7926 u16 event_slow;
7927 unsigned features;
7928 u8 default_ver;
7929} rtl_cfg_infos [] = {
7930 [RTL_CFG_0] = {
7931 .hw_start = rtl_hw_start_8169,
7932 .region = 1,
7933 .align = 0,
7934 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
7935 .features = RTL_FEATURE_GMII,
7936 .default_ver = RTL_GIGA_MAC_VER_01,
7937 },
7938 [RTL_CFG_1] = {
7939 .hw_start = rtl_hw_start_8168,
7940 .region = 2,
7941 .align = 8,
7942 .event_slow = SYSErr | LinkChg | RxOverflow,
7943 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
7944 .default_ver = RTL_GIGA_MAC_VER_11,
7945 },
7946 [RTL_CFG_2] = {
7947 .hw_start = rtl_hw_start_8101,
7948 .region = 2,
7949 .align = 8,
7950 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
7951 PCSTimeout,
7952 .features = RTL_FEATURE_MSI,
7953 .default_ver = RTL_GIGA_MAC_VER_13,
7954 }
7955};
7956
7957/* Cfg9346_Unlock assumed. */
7958static unsigned rtl_try_msi(struct rtl8169_private *tp,
7959 const struct rtl_cfg_info *cfg)
7960{
7961 void __iomem *ioaddr = tp->mmio_addr;
7962 unsigned msi = 0;
7963 u8 cfg2;
7964
7965 cfg2 = RTL_R8(Config2) & ~MSIEnable;
7966 if (cfg->features & RTL_FEATURE_MSI) {
7967 if (pci_enable_msi(tp->pci_dev)) {
7968 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
7969 } else {
7970 cfg2 |= MSIEnable;
7971 msi = RTL_FEATURE_MSI;
7972 }
7973 }
7974 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
7975 RTL_W8(Config2, cfg2);
7976 return msi;
7977}
7978
Hayes Wangc5583862012-07-02 17:23:22 +08007979DECLARE_RTL_COND(rtl_link_list_ready_cond)
7980{
7981 void __iomem *ioaddr = tp->mmio_addr;
7982
7983 return RTL_R8(MCU) & LINK_LIST_RDY;
7984}
7985
7986DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7987{
7988 void __iomem *ioaddr = tp->mmio_addr;
7989
7990 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
7991}
7992
Bill Pembertonbaf63292012-12-03 09:23:28 -05007993static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007994{
7995 void __iomem *ioaddr = tp->mmio_addr;
7996 u32 data;
7997
7998 tp->ocp_base = OCP_STD_PHY_BASE;
7999
8000 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
8001
8002 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
8003 return;
8004
8005 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
8006 return;
8007
8008 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
8009 msleep(1);
8010 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
8011
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008012 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008013 data &= ~(1 << 14);
8014 r8168_mac_ocp_write(tp, 0xe8de, data);
8015
8016 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8017 return;
8018
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008019 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008020 data |= (1 << 15);
8021 r8168_mac_ocp_write(tp, 0xe8de, data);
8022
8023 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8024 return;
8025}
8026
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008027static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
8028{
8029 rtl8168ep_stop_cmac(tp);
8030 rtl_hw_init_8168g(tp);
8031}
8032
Bill Pembertonbaf63292012-12-03 09:23:28 -05008033static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008034{
8035 switch (tp->mac_version) {
8036 case RTL_GIGA_MAC_VER_40:
8037 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00008038 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00008039 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08008040 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008041 case RTL_GIGA_MAC_VER_45:
8042 case RTL_GIGA_MAC_VER_46:
8043 case RTL_GIGA_MAC_VER_47:
8044 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008045 rtl_hw_init_8168g(tp);
8046 break;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008047 case RTL_GIGA_MAC_VER_49:
8048 case RTL_GIGA_MAC_VER_50:
8049 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008050 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08008051 break;
Hayes Wangc5583862012-07-02 17:23:22 +08008052 default:
8053 break;
8054 }
8055}
8056
hayeswang929a0312014-09-16 11:40:47 +08008057static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008058{
8059 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
8060 const unsigned int region = cfg->region;
8061 struct rtl8169_private *tp;
8062 struct mii_if_info *mii;
8063 struct net_device *dev;
8064 void __iomem *ioaddr;
8065 int chipset, i;
8066 int rc;
8067
8068 if (netif_msg_drv(&debug)) {
8069 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
8070 MODULENAME, RTL8169_VERSION);
8071 }
8072
8073 dev = alloc_etherdev(sizeof (*tp));
8074 if (!dev) {
8075 rc = -ENOMEM;
8076 goto out;
8077 }
8078
8079 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01008080 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008081 tp = netdev_priv(dev);
8082 tp->dev = dev;
8083 tp->pci_dev = pdev;
8084 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
8085
8086 mii = &tp->mii;
8087 mii->dev = dev;
8088 mii->mdio_read = rtl_mdio_read;
8089 mii->mdio_write = rtl_mdio_write;
8090 mii->phy_id_mask = 0x1f;
8091 mii->reg_num_mask = 0x1f;
8092 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
8093
8094 /* disable ASPM completely as that cause random device stop working
8095 * problems as well as full system hangs for some PCIe devices users */
8096 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
8097 PCIE_LINK_STATE_CLKPM);
8098
8099 /* enable device (incl. PCI PM wakeup and hotplug setup) */
8100 rc = pci_enable_device(pdev);
8101 if (rc < 0) {
8102 netif_err(tp, probe, dev, "enable failure\n");
8103 goto err_out_free_dev_1;
8104 }
8105
8106 if (pci_set_mwi(pdev) < 0)
8107 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
8108
8109 /* make sure PCI base addr 1 is MMIO */
8110 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
8111 netif_err(tp, probe, dev,
8112 "region #%d not an MMIO resource, aborting\n",
8113 region);
8114 rc = -ENODEV;
8115 goto err_out_mwi_2;
8116 }
8117
8118 /* check for weird/broken PCI region reporting */
8119 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
8120 netif_err(tp, probe, dev,
8121 "Invalid PCI region size(s), aborting\n");
8122 rc = -ENODEV;
8123 goto err_out_mwi_2;
8124 }
8125
8126 rc = pci_request_regions(pdev, MODULENAME);
8127 if (rc < 0) {
8128 netif_err(tp, probe, dev, "could not request regions\n");
8129 goto err_out_mwi_2;
8130 }
8131
hayeswang929a0312014-09-16 11:40:47 +08008132 tp->cp_cmd = 0;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008133
8134 if ((sizeof(dma_addr_t) > 4) &&
8135 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
8136 tp->cp_cmd |= PCIDAC;
8137 dev->features |= NETIF_F_HIGHDMA;
8138 } else {
8139 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8140 if (rc < 0) {
8141 netif_err(tp, probe, dev, "DMA configuration failed\n");
8142 goto err_out_free_res_3;
8143 }
8144 }
8145
8146 /* ioremap MMIO region */
8147 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
8148 if (!ioaddr) {
8149 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
8150 rc = -EIO;
8151 goto err_out_free_res_3;
8152 }
8153 tp->mmio_addr = ioaddr;
8154
8155 if (!pci_is_pcie(pdev))
8156 netif_info(tp, probe, dev, "not PCI Express\n");
8157
8158 /* Identify chip attached to board */
8159 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
8160
8161 rtl_init_rxcfg(tp);
8162
8163 rtl_irq_disable(tp);
8164
Hayes Wangc5583862012-07-02 17:23:22 +08008165 rtl_hw_initialize(tp);
8166
Francois Romieu3b6cf252012-03-08 09:59:04 +01008167 rtl_hw_reset(tp);
8168
8169 rtl_ack_events(tp, 0xffff);
8170
8171 pci_set_master(pdev);
8172
Francois Romieu3b6cf252012-03-08 09:59:04 +01008173 rtl_init_mdio_ops(tp);
8174 rtl_init_pll_power_ops(tp);
8175 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08008176 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008177
8178 rtl8169_print_mac_version(tp);
8179
8180 chipset = tp->mac_version;
8181 tp->txd_version = rtl_chip_infos[chipset].txd_version;
8182
8183 RTL_W8(Cfg9346, Cfg9346_Unlock);
8184 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
Peter Wu8f9d5132013-08-17 11:00:02 +02008185 RTL_W8(Config5, RTL_R8(Config5) & (BWF | MWF | UWF | LanWake | PMEStatus));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008186 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08008187 case RTL_GIGA_MAC_VER_34:
8188 case RTL_GIGA_MAC_VER_35:
8189 case RTL_GIGA_MAC_VER_36:
8190 case RTL_GIGA_MAC_VER_37:
8191 case RTL_GIGA_MAC_VER_38:
8192 case RTL_GIGA_MAC_VER_40:
8193 case RTL_GIGA_MAC_VER_41:
8194 case RTL_GIGA_MAC_VER_42:
8195 case RTL_GIGA_MAC_VER_43:
8196 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008197 case RTL_GIGA_MAC_VER_45:
8198 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08008199 case RTL_GIGA_MAC_VER_47:
8200 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008201 case RTL_GIGA_MAC_VER_49:
8202 case RTL_GIGA_MAC_VER_50:
8203 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008204 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
8205 tp->features |= RTL_FEATURE_WOL;
8206 if ((RTL_R8(Config3) & LinkUp) != 0)
8207 tp->features |= RTL_FEATURE_WOL;
8208 break;
8209 default:
8210 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
8211 tp->features |= RTL_FEATURE_WOL;
8212 break;
8213 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008214 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
8215 tp->features |= RTL_FEATURE_WOL;
8216 tp->features |= rtl_try_msi(tp, cfg);
8217 RTL_W8(Cfg9346, Cfg9346_Lock);
8218
8219 if (rtl_tbi_enabled(tp)) {
8220 tp->set_speed = rtl8169_set_speed_tbi;
8221 tp->get_settings = rtl8169_gset_tbi;
8222 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
8223 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
8224 tp->link_ok = rtl8169_tbi_link_ok;
8225 tp->do_ioctl = rtl_tbi_ioctl;
8226 } else {
8227 tp->set_speed = rtl8169_set_speed_xmii;
8228 tp->get_settings = rtl8169_gset_xmii;
8229 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8230 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8231 tp->link_ok = rtl8169_xmii_link_ok;
8232 tp->do_ioctl = rtl_xmii_ioctl;
8233 }
8234
8235 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05008236 u64_stats_init(&tp->rx_stats.syncp);
8237 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008238
8239 /* Get MAC address */
Chun-Hao Lin89cceb22014-10-01 23:17:15 +08008240 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8241 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8242 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8243 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8244 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8245 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8246 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8247 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8248 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8249 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008250 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8251 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008252 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8253 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8254 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8255 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008256 u16 mac_addr[3];
8257
Chun-Hao Lin05b96872014-10-01 23:17:12 +08008258 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8259 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008260
8261 if (is_valid_ether_addr((u8 *)mac_addr))
8262 rtl_rar_set(tp, (u8 *)mac_addr);
8263 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008264 for (i = 0; i < ETH_ALEN; i++)
8265 dev->dev_addr[i] = RTL_R8(MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008266
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00008267 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008268 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008269
8270 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
8271
8272 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8273 * properly for all devices */
8274 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00008275 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008276
8277 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00008278 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8279 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008280 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8281 NETIF_F_HIGHDMA;
8282
hayeswang929a0312014-09-16 11:40:47 +08008283 tp->cp_cmd |= RxChkSum | RxVlan;
8284
8285 /*
8286 * Pretend we are using VLANs; This bypasses a nasty bug where
8287 * Interrupts stop flowing on high load on 8110SCd controllers.
8288 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01008289 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08008290 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00008291 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008292
hayeswang5888d3f2014-07-11 16:25:56 +08008293 if (tp->txd_version == RTL_TD_0)
8294 tp->tso_csum = rtl8169_tso_csum_v1;
hayeswange9746042014-07-11 16:25:58 +08008295 else if (tp->txd_version == RTL_TD_1) {
hayeswang5888d3f2014-07-11 16:25:56 +08008296 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08008297 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
8298 } else
hayeswang5888d3f2014-07-11 16:25:56 +08008299 WARN_ON_ONCE(1);
8300
Francois Romieu3b6cf252012-03-08 09:59:04 +01008301 dev->hw_features |= NETIF_F_RXALL;
8302 dev->hw_features |= NETIF_F_RXFCS;
8303
8304 tp->hw_start = cfg->hw_start;
8305 tp->event_slow = cfg->event_slow;
8306
8307 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
8308 ~(RxBOVF | RxFOVF) : ~0;
8309
8310 init_timer(&tp->timer);
8311 tp->timer.data = (unsigned long) dev;
8312 tp->timer.function = rtl8169_phy_timer;
8313
8314 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8315
8316 rc = register_netdev(dev);
8317 if (rc < 0)
8318 goto err_out_msi_4;
8319
8320 pci_set_drvdata(pdev, dev);
8321
Francois Romieu92a7c4e2012-03-10 10:42:12 +01008322 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
8323 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
8324 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008325 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8326 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8327 "tx checksumming: %s]\n",
8328 rtl_chip_infos[chipset].jumbo_max,
8329 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
8330 }
8331
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008332 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
8333 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008334 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
8335 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8336 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8337 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008338 r8168_check_dash(tp)) {
Francois Romieu3b6cf252012-03-08 09:59:04 +01008339 rtl8168_driver_start(tp);
8340 }
8341
8342 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
8343
8344 if (pci_dev_run_wake(pdev))
8345 pm_runtime_put_noidle(&pdev->dev);
8346
8347 netif_carrier_off(dev);
8348
8349out:
8350 return rc;
8351
8352err_out_msi_4:
Devendra Nagaad1be8d2012-05-31 01:51:20 +00008353 netif_napi_del(&tp->napi);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008354 rtl_disable_msi(pdev, tp);
8355 iounmap(ioaddr);
8356err_out_free_res_3:
8357 pci_release_regions(pdev);
8358err_out_mwi_2:
8359 pci_clear_mwi(pdev);
8360 pci_disable_device(pdev);
8361err_out_free_dev_1:
8362 free_netdev(dev);
8363 goto out;
8364}
8365
Linus Torvalds1da177e2005-04-16 15:20:36 -07008366static struct pci_driver rtl8169_pci_driver = {
8367 .name = MODULENAME,
8368 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01008369 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05008370 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02008371 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008372 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008373};
8374
Devendra Naga3eeb7da2012-10-26 09:27:42 +00008375module_pci_driver(rtl8169_pci_driver);