blob: 7055db161b1b353d04c46683e861e36fed7e4a7d [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 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200102#define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
103#define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
104#define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
105#define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
106#define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
107#define RTL_R32(tp, reg) readl(tp->mmio_addr + (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 },
Chun-Hao Lin610c9082016-12-27 16:29:43 +0800329 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
Francois Romieud81bf552006-09-20 21:31:20 +0200330 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100331 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200332 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200333 { PCI_VENDOR_ID_DLINK, 0x4300,
334 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200335 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000336 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200337 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200338 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
339 { PCI_VENDOR_ID_LINKSYS, 0x1032,
340 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100341 { 0x0001, 0x8168,
342 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 {0,},
344};
345
346MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
347
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000348static int rx_buf_sz = 16383;
Ard Biesheuvel27896c82016-05-14 22:40:15 +0200349static int use_dac = -1;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200350static struct {
351 u32 msg_enable;
352} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Francois Romieu07d3f512007-02-21 22:40:46 +0100354enum rtl_registers {
355 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100356 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100357 MAR0 = 8, /* Multicast filter. */
358 CounterAddrLow = 0x10,
359 CounterAddrHigh = 0x14,
360 TxDescStartAddrLow = 0x20,
361 TxDescStartAddrHigh = 0x24,
362 TxHDescStartAddrLow = 0x28,
363 TxHDescStartAddrHigh = 0x2c,
364 FLASH = 0x30,
365 ERSR = 0x36,
366 ChipCmd = 0x37,
367 TxPoll = 0x38,
368 IntrMask = 0x3c,
369 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700370
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800371 TxConfig = 0x40,
372#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
373#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
374
375 RxConfig = 0x44,
376#define RX128_INT_EN (1 << 15) /* 8111c and later */
377#define RX_MULTI_EN (1 << 14) /* 8111c only */
378#define RXCFG_FIFO_SHIFT 13
379 /* No threshold before first PCI xfer */
380#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000381#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800382#define RXCFG_DMA_SHIFT 8
383 /* Unlimited maximum PCI burst. */
384#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700385
Francois Romieu07d3f512007-02-21 22:40:46 +0100386 RxMissed = 0x4c,
387 Cfg9346 = 0x50,
388 Config0 = 0x51,
389 Config1 = 0x52,
390 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200391#define PME_SIGNAL (1 << 5) /* 8168c and later */
392
Francois Romieu07d3f512007-02-21 22:40:46 +0100393 Config3 = 0x54,
394 Config4 = 0x55,
395 Config5 = 0x56,
396 MultiIntr = 0x5c,
397 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100398 PHYstatus = 0x6c,
399 RxMaxSize = 0xda,
400 CPlusCmd = 0xe0,
401 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300402
403#define RTL_COALESCE_MASK 0x0f
404#define RTL_COALESCE_SHIFT 4
405#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
406#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
407
Francois Romieu07d3f512007-02-21 22:40:46 +0100408 RxDescAddrLow = 0xe4,
409 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000410 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
411
412#define NoEarlyTx 0x3f /* Max value : no early transmit. */
413
414 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
415
416#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800417#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000418
Francois Romieu07d3f512007-02-21 22:40:46 +0100419 FuncEvent = 0xf0,
420 FuncEventMask = 0xf4,
421 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800422 IBCR0 = 0xf8,
423 IBCR2 = 0xf9,
424 IBIMR0 = 0xfa,
425 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100426 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427};
428
Francois Romieuf162a5d2008-06-01 22:37:49 +0200429enum rtl8110_registers {
430 TBICSR = 0x64,
431 TBI_ANAR = 0x68,
432 TBI_LPAR = 0x6a,
433};
434
435enum rtl8168_8101_registers {
436 CSIDR = 0x64,
437 CSIAR = 0x68,
438#define CSIAR_FLAG 0x80000000
439#define CSIAR_WRITE_CMD 0x80000000
440#define CSIAR_BYTE_ENABLE 0x0f
441#define CSIAR_BYTE_ENABLE_SHIFT 12
442#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800443#define CSIAR_FUNC_CARD 0x00000000
444#define CSIAR_FUNC_SDIO 0x00010000
445#define CSIAR_FUNC_NIC 0x00020000
hayeswang45dd95c2013-07-08 17:09:01 +0800446#define CSIAR_FUNC_NIC2 0x00010000
françois romieu065c27c2011-01-03 15:08:12 +0000447 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200448 EPHYAR = 0x80,
449#define EPHYAR_FLAG 0x80000000
450#define EPHYAR_WRITE_CMD 0x80000000
451#define EPHYAR_REG_MASK 0x1f
452#define EPHYAR_REG_SHIFT 16
453#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800454 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800455#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800456#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200457 DBG_REG = 0xd1,
458#define FIX_NAK_1 (1 << 4)
459#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800460 TWSI = 0xd2,
461 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800462#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800463#define TX_EMPTY (1 << 5)
464#define RX_EMPTY (1 << 4)
465#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800466#define EN_NDP (1 << 3)
467#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800468#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000469 EFUSEAR = 0xdc,
470#define EFUSEAR_FLAG 0x80000000
471#define EFUSEAR_WRITE_CMD 0x80000000
472#define EFUSEAR_READ_CMD 0x00000000
473#define EFUSEAR_REG_MASK 0x03ff
474#define EFUSEAR_REG_SHIFT 8
475#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800476 MISC_1 = 0xf2,
477#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200478};
479
françois romieuc0e45c12011-01-03 15:08:04 +0000480enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800481 LED_FREQ = 0x1a,
482 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000483 ERIDR = 0x70,
484 ERIAR = 0x74,
485#define ERIAR_FLAG 0x80000000
486#define ERIAR_WRITE_CMD 0x80000000
487#define ERIAR_READ_CMD 0x00000000
488#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000489#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800490#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
491#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
492#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800493#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800494#define ERIAR_MASK_SHIFT 12
495#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
496#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800497#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800498#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800499#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000500 EPHY_RXER_NUM = 0x7c,
501 OCPDR = 0xb0, /* OCP GPHY access */
502#define OCPDR_WRITE_CMD 0x80000000
503#define OCPDR_READ_CMD 0x00000000
504#define OCPDR_REG_MASK 0x7f
505#define OCPDR_GPHY_REG_SHIFT 16
506#define OCPDR_DATA_MASK 0xffff
507 OCPAR = 0xb4,
508#define OCPAR_FLAG 0x80000000
509#define OCPAR_GPHY_WRITE_CMD 0x8000f060
510#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800511 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000512 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
513 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200514#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800515#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800516#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800517#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800518#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000519};
520
Francois Romieu07d3f512007-02-21 22:40:46 +0100521enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100523 SYSErr = 0x8000,
524 PCSTimeout = 0x4000,
525 SWInt = 0x0100,
526 TxDescUnavail = 0x0080,
527 RxFIFOOver = 0x0040,
528 LinkChg = 0x0020,
529 RxOverflow = 0x0010,
530 TxErr = 0x0008,
531 TxOK = 0x0004,
532 RxErr = 0x0002,
533 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400536 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200537 RxFOVF = (1 << 23),
538 RxRWT = (1 << 22),
539 RxRES = (1 << 21),
540 RxRUNT = (1 << 20),
541 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800544 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100545 CmdReset = 0x10,
546 CmdRxEnb = 0x08,
547 CmdTxEnb = 0x04,
548 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Francois Romieu275391a2007-02-23 23:50:28 +0100550 /* TXPoll register p.5 */
551 HPQ = 0x80, /* Poll cmd on the high prio queue */
552 NPQ = 0x40, /* Poll cmd on the low prio queue */
553 FSWInt = 0x01, /* Forced software interrupt */
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100556 Cfg9346_Lock = 0x00,
557 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100560 AcceptErr = 0x20,
561 AcceptRunt = 0x10,
562 AcceptBroadcast = 0x08,
563 AcceptMulticast = 0x04,
564 AcceptMyPhys = 0x02,
565 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200566#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* TxConfigBits */
569 TxInterFrameGapShift = 24,
570 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
571
Francois Romieu5d06a992006-02-23 00:47:58 +0100572 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200573 LEDS1 = (1 << 7),
574 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200575 Speed_down = (1 << 4),
576 MEMMAP = (1 << 3),
577 IOMAP = (1 << 2),
578 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100579 PMEnable = (1 << 0), /* Power Management Enable */
580
Francois Romieu6dccd162007-02-13 23:38:05 +0100581 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000582 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000583 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100584 PCI_Clock_66MHz = 0x01,
585 PCI_Clock_33MHz = 0x00,
586
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100587 /* Config3 register p.25 */
588 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
589 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200590 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800591 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200592 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100593
Francois Romieud58d46b2011-05-03 16:38:29 +0200594 /* Config4 register */
595 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
596
Francois Romieu5d06a992006-02-23 00:47:58 +0100597 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100598 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
599 MWF = (1 << 5), /* Accept Multicast wakeup frame */
600 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200601 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100602 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100603 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000604 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /* TBICSR p.28 */
607 TBIReset = 0x80000000,
608 TBILoopback = 0x40000000,
609 TBINwEnable = 0x20000000,
610 TBINwRestart = 0x10000000,
611 TBILinkOk = 0x02000000,
612 TBINwComplete = 0x01000000,
613
614 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200615 EnableBist = (1 << 15), // 8168 8101
616 Mac_dbgo_oe = (1 << 14), // 8168 8101
617 Normal_mode = (1 << 13), // unused
618 Force_half_dup = (1 << 12), // 8168 8101
619 Force_rxflow_en = (1 << 11), // 8168 8101
620 Force_txflow_en = (1 << 10), // 8168 8101
621 Cxpl_dbg_sel = (1 << 9), // 8168 8101
622 ASF = (1 << 8), // 8168 8101
623 PktCntrDisable = (1 << 7), // 8168 8101
624 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 RxVlan = (1 << 6),
626 RxChkSum = (1 << 5),
627 PCIDAC = (1 << 4),
628 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100629 INTT_0 = 0x0000, // 8168
630 INTT_1 = 0x0001, // 8168
631 INTT_2 = 0x0002, // 8168
632 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100635 TBI_Enable = 0x80,
636 TxFlowCtrl = 0x40,
637 RxFlowCtrl = 0x20,
638 _1000bpsF = 0x10,
639 _100bps = 0x08,
640 _10bps = 0x04,
641 LinkStatus = 0x02,
642 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100645 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200646
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200647 /* ResetCounterCommand */
648 CounterReset = 0x1,
649
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200650 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100651 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800652
653 /* magic enable v2 */
654 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655};
656
Francois Romieu2b7b4312011-04-18 22:53:24 -0700657enum rtl_desc_bit {
658 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
660 RingEnd = (1 << 30), /* End of descriptor ring */
661 FirstFrag = (1 << 29), /* First segment of a packet */
662 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700663};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Francois Romieu2b7b4312011-04-18 22:53:24 -0700665/* Generic case. */
666enum rtl_tx_desc_bit {
667 /* First doubleword. */
668 TD_LSO = (1 << 27), /* Large Send Offload */
669#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Francois Romieu2b7b4312011-04-18 22:53:24 -0700671 /* Second doubleword. */
672 TxVlanTag = (1 << 17), /* Add VLAN tag */
673};
674
675/* 8169, 8168b and 810x except 8102e. */
676enum rtl_tx_desc_bit_0 {
677 /* First doubleword. */
678#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
679 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
680 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
681 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
682};
683
684/* 8102e, 8168c and beyond. */
685enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800686 /* First doubleword. */
687 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800688 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800689#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800690#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800691
Francois Romieu2b7b4312011-04-18 22:53:24 -0700692 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800693#define TCPHO_SHIFT 18
694#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700695#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800696 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
697 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700698 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
699 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
700};
701
Francois Romieu2b7b4312011-04-18 22:53:24 -0700702enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /* Rx private */
704 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500705 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707#define RxProtoUDP (PID1)
708#define RxProtoTCP (PID0)
709#define RxProtoIP (PID1 | PID0)
710#define RxProtoMask RxProtoIP
711
712 IPFail = (1 << 16), /* IP checksum failed */
713 UDPFail = (1 << 15), /* UDP/IP checksum failed */
714 TCPFail = (1 << 14), /* TCP/IP checksum failed */
715 RxVlanTag = (1 << 16), /* VLAN tag available */
716};
717
718#define RsvdMask 0x3fffc000
719
720struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200721 __le32 opts1;
722 __le32 opts2;
723 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724};
725
726struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200727 __le32 opts1;
728 __le32 opts2;
729 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730};
731
732struct ring_info {
733 struct sk_buff *skb;
734 u32 len;
735 u8 __pad[sizeof(void *) - sizeof(u32)];
736};
737
Ivan Vecera355423d2009-02-06 21:49:57 -0800738struct rtl8169_counters {
739 __le64 tx_packets;
740 __le64 rx_packets;
741 __le64 tx_errors;
742 __le32 rx_errors;
743 __le16 rx_missed;
744 __le16 align_errors;
745 __le32 tx_one_collision;
746 __le32 tx_multi_collision;
747 __le64 rx_unicast;
748 __le64 rx_broadcast;
749 __le32 rx_multicast;
750 __le16 tx_aborted;
751 __le16 tx_underun;
752};
753
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200754struct rtl8169_tc_offsets {
755 bool inited;
756 __le64 tx_errors;
757 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200758 __le16 tx_aborted;
759};
760
Francois Romieuda78dbf2012-01-26 14:18:23 +0100761enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100762 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100763 RTL_FLAG_TASK_SLOW_PENDING,
764 RTL_FLAG_TASK_RESET_PENDING,
765 RTL_FLAG_TASK_PHY_PENDING,
766 RTL_FLAG_MAX
767};
768
Junchang Wang8027aa22012-03-04 23:30:32 +0100769struct rtl8169_stats {
770 u64 packets;
771 u64 bytes;
772 struct u64_stats_sync syncp;
773};
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775struct rtl8169_private {
776 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200777 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000778 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700779 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200780 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700781 u16 txd_version;
782 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
784 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100786 struct rtl8169_stats rx_stats;
787 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
789 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
790 dma_addr_t TxPhyAddr;
791 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000792 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 struct timer_list timer;
795 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100796
797 u16 event_slow;
Francois Romieu50970832017-10-27 13:24:49 +0300798 const struct rtl_coalesce_info *coalesce_info;
françois romieuc0e45c12011-01-03 15:08:04 +0000799
800 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200801 void (*write)(struct rtl8169_private *, int, int);
802 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000803 } mdio_ops;
804
françois romieu065c27c2011-01-03 15:08:12 +0000805 struct pll_power_ops {
806 void (*down)(struct rtl8169_private *);
807 void (*up)(struct rtl8169_private *);
808 } pll_power_ops;
809
Francois Romieud58d46b2011-05-03 16:38:29 +0200810 struct jumbo_ops {
811 void (*enable)(struct rtl8169_private *);
812 void (*disable)(struct rtl8169_private *);
813 } jumbo_ops;
814
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800815 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200816 void (*write)(struct rtl8169_private *, int, int);
817 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800818 } csi_ops;
819
Oliver Neukum54405cd2011-01-06 21:55:13 +0100820 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +0100821 int (*get_link_ksettings)(struct net_device *,
822 struct ethtool_link_ksettings *);
françois romieu4da19632011-01-03 15:07:55 +0000823 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100824 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000825 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200826 unsigned int (*link_ok)(struct rtl8169_private *tp);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800827 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800828 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100829
830 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100831 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
832 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100833 struct work_struct work;
834 } wk;
835
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200836 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200837
838 struct mii_if_info mii;
Corinna Vinschen42020322015-09-10 10:47:35 +0200839 dma_addr_t counters_phys_addr;
840 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200841 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000842 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400843 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000844
Francois Romieub6ffd972011-06-17 17:00:05 +0200845 struct rtl_fw {
846 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200847
848#define RTL_VER_SIZE 32
849
850 char version[RTL_VER_SIZE];
851
852 struct rtl_fw_phy_action {
853 __le32 *code;
854 size_t size;
855 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200856 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300857#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800858
859 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860};
861
Ralf Baechle979b6c12005-06-13 14:30:40 -0700862MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700865MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200866module_param_named(debug, debug.msg_enable, int, 0);
867MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868MODULE_LICENSE("GPL");
869MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000870MODULE_FIRMWARE(FIRMWARE_8168D_1);
871MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000872MODULE_FIRMWARE(FIRMWARE_8168E_1);
873MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400874MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800875MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800876MODULE_FIRMWARE(FIRMWARE_8168F_1);
877MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800878MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800879MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800880MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800881MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000882MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000883MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000884MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800885MODULE_FIRMWARE(FIRMWARE_8168H_1);
886MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200887MODULE_FIRMWARE(FIRMWARE_8107E_1);
888MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Francois Romieuda78dbf2012-01-26 14:18:23 +0100890static void rtl_lock_work(struct rtl8169_private *tp)
891{
892 mutex_lock(&tp->wk.mutex);
893}
894
895static void rtl_unlock_work(struct rtl8169_private *tp)
896{
897 mutex_unlock(&tp->wk.mutex);
898}
899
Francois Romieud58d46b2011-05-03 16:38:29 +0200900static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
901{
Jiang Liu7d7903b2012-07-24 17:20:16 +0800902 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
903 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200904}
905
Francois Romieuffc46952012-07-06 14:19:23 +0200906struct rtl_cond {
907 bool (*check)(struct rtl8169_private *);
908 const char *msg;
909};
910
911static void rtl_udelay(unsigned int d)
912{
913 udelay(d);
914}
915
916static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
917 void (*delay)(unsigned int), unsigned int d, int n,
918 bool high)
919{
920 int i;
921
922 for (i = 0; i < n; i++) {
923 delay(d);
924 if (c->check(tp) == high)
925 return true;
926 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200927 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
928 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200929 return false;
930}
931
932static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
933 const struct rtl_cond *c,
934 unsigned int d, int n)
935{
936 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
937}
938
939static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
940 const struct rtl_cond *c,
941 unsigned int d, int n)
942{
943 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
944}
945
946static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
947 const struct rtl_cond *c,
948 unsigned int d, int n)
949{
950 return rtl_loop_wait(tp, c, msleep, d, n, true);
951}
952
953static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
954 const struct rtl_cond *c,
955 unsigned int d, int n)
956{
957 return rtl_loop_wait(tp, c, msleep, d, n, false);
958}
959
960#define DECLARE_RTL_COND(name) \
961static bool name ## _check(struct rtl8169_private *); \
962 \
963static const struct rtl_cond name = { \
964 .check = name ## _check, \
965 .msg = #name \
966}; \
967 \
968static bool name ## _check(struct rtl8169_private *tp)
969
Hayes Wangc5583862012-07-02 17:23:22 +0800970static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
971{
972 if (reg & 0xffff0001) {
973 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
974 return true;
975 }
976 return false;
977}
978
979DECLARE_RTL_COND(rtl_ocp_gphy_cond)
980{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200981 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
Hayes Wangc5583862012-07-02 17:23:22 +0800982}
983
984static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
985{
Hayes Wangc5583862012-07-02 17:23:22 +0800986 if (rtl_ocp_reg_failure(tp, reg))
987 return;
988
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200989 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800990
991 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
992}
993
994static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
995{
Hayes Wangc5583862012-07-02 17:23:22 +0800996 if (rtl_ocp_reg_failure(tp, reg))
997 return 0;
998
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200999 RTL_W32(tp, GPHY_OCP, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +08001000
1001 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001002 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
Hayes Wangc5583862012-07-02 17:23:22 +08001003}
1004
Hayes Wangc5583862012-07-02 17:23:22 +08001005static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1006{
Hayes Wangc5583862012-07-02 17:23:22 +08001007 if (rtl_ocp_reg_failure(tp, reg))
1008 return;
1009
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001010 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +08001011}
1012
1013static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1014{
Hayes Wangc5583862012-07-02 17:23:22 +08001015 if (rtl_ocp_reg_failure(tp, reg))
1016 return 0;
1017
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001018 RTL_W32(tp, OCPDR, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +08001019
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001020 return RTL_R32(tp, OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +08001021}
1022
1023#define OCP_STD_PHY_BASE 0xa400
1024
1025static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1026{
1027 if (reg == 0x1f) {
1028 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1029 return;
1030 }
1031
1032 if (tp->ocp_base != OCP_STD_PHY_BASE)
1033 reg -= 0x10;
1034
1035 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1036}
1037
1038static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1039{
1040 if (tp->ocp_base != OCP_STD_PHY_BASE)
1041 reg -= 0x10;
1042
1043 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1044}
1045
hayeswangeee37862013-04-01 22:23:38 +00001046static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1047{
1048 if (reg == 0x1f) {
1049 tp->ocp_base = value << 4;
1050 return;
1051 }
1052
1053 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1054}
1055
1056static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1057{
1058 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1059}
1060
Francois Romieuffc46952012-07-06 14:19:23 +02001061DECLARE_RTL_COND(rtl_phyar_cond)
1062{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001063 return RTL_R32(tp, PHYAR) & 0x80000000;
Francois Romieuffc46952012-07-06 14:19:23 +02001064}
1065
Francois Romieu24192212012-07-06 20:19:42 +02001066static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001068 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Francois Romieuffc46952012-07-06 14:19:23 +02001070 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001071 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001072 * According to hardware specs a 20us delay is required after write
1073 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001074 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001075 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
Francois Romieu24192212012-07-06 20:19:42 +02001078static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Francois Romieuffc46952012-07-06 14:19:23 +02001080 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001082 RTL_W32(tp, 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) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001085 RTL_R32(tp, PHYAR) & 0xffff : ~0;
Francois Romieuffc46952012-07-06 14:19:23 +02001086
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{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001098 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001099}
1100
Francois Romieu24192212012-07-06 20:19:42 +02001101static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001102{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001103 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1104 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
1105 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001106
Francois Romieuffc46952012-07-06 14:19:23 +02001107 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001108}
1109
Francois Romieu24192212012-07-06 20:19:42 +02001110static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001111{
Francois Romieu24192212012-07-06 20:19:42 +02001112 r8168dp_1_mdio_access(tp, reg,
1113 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001114}
1115
Francois Romieu24192212012-07-06 20:19:42 +02001116static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001117{
Francois Romieu24192212012-07-06 20:19:42 +02001118 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001119
1120 mdelay(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001121 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
1122 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001123
Francois Romieuffc46952012-07-06 14:19:23 +02001124 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001125 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001126}
1127
françois romieue6de30d2011-01-03 15:08:37 +00001128#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1129
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001130static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001131{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001132 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001133}
1134
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001135static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001136{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001137 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001138}
1139
Francois Romieu24192212012-07-06 20:19:42 +02001140static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001141{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001142 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001143
Francois Romieu24192212012-07-06 20:19:42 +02001144 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001145
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001146 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001147}
1148
Francois Romieu24192212012-07-06 20:19:42 +02001149static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001150{
1151 int value;
1152
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001153 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001154
Francois Romieu24192212012-07-06 20:19:42 +02001155 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001156
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001157 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001158
1159 return value;
1160}
1161
françois romieu4da19632011-01-03 15:07:55 +00001162static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001163{
Francois Romieu24192212012-07-06 20:19:42 +02001164 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001165}
1166
françois romieu4da19632011-01-03 15:07:55 +00001167static int rtl_readphy(struct rtl8169_private *tp, int location)
1168{
Francois Romieu24192212012-07-06 20:19:42 +02001169 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001170}
1171
1172static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1173{
1174 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1175}
1176
Chun-Hao Lin76564422014-10-01 23:17:17 +08001177static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001178{
1179 int val;
1180
françois romieu4da19632011-01-03 15:07:55 +00001181 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001182 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001183}
1184
Francois Romieuccdffb92008-07-26 14:26:06 +02001185static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1186 int val)
1187{
1188 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001189
françois romieu4da19632011-01-03 15:07:55 +00001190 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001191}
1192
1193static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1194{
1195 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001196
françois romieu4da19632011-01-03 15:07:55 +00001197 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001198}
1199
Francois Romieuffc46952012-07-06 14:19:23 +02001200DECLARE_RTL_COND(rtl_ephyar_cond)
1201{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001202 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001203}
1204
Francois Romieufdf6fc02012-07-06 22:40:38 +02001205static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001206{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001207 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
Francois Romieudacf8152008-08-02 20:44:13 +02001208 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1209
Francois Romieuffc46952012-07-06 14:19:23 +02001210 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1211
1212 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001213}
1214
Francois Romieufdf6fc02012-07-06 22:40:38 +02001215static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001216{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001217 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
Francois Romieudacf8152008-08-02 20:44:13 +02001218
Francois Romieuffc46952012-07-06 14:19:23 +02001219 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001220 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001221}
1222
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001223DECLARE_RTL_COND(rtl_eriar_cond)
1224{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001225 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001226}
1227
Francois Romieufdf6fc02012-07-06 22:40:38 +02001228static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1229 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001230{
Hayes Wang133ac402011-07-06 15:58:05 +08001231 BUG_ON((addr & 3) || (mask == 0));
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001232 RTL_W32(tp, ERIDR, val);
1233 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001234
Francois Romieuffc46952012-07-06 14:19:23 +02001235 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001236}
1237
Francois Romieufdf6fc02012-07-06 22:40:38 +02001238static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001239{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001240 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001241
Francois Romieuffc46952012-07-06 14:19:23 +02001242 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001243 RTL_R32(tp, ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001244}
1245
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001246static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001247 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001248{
1249 u32 val;
1250
Francois Romieufdf6fc02012-07-06 22:40:38 +02001251 val = rtl_eri_read(tp, addr, type);
1252 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001253}
1254
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001255static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1256{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001257 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001258 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001259 RTL_R32(tp, OCPDR) : ~0;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001260}
1261
1262static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1263{
1264 return rtl_eri_read(tp, reg, ERIAR_OOB);
1265}
1266
1267static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1268{
1269 switch (tp->mac_version) {
1270 case RTL_GIGA_MAC_VER_27:
1271 case RTL_GIGA_MAC_VER_28:
1272 case RTL_GIGA_MAC_VER_31:
1273 return r8168dp_ocp_read(tp, mask, reg);
1274 case RTL_GIGA_MAC_VER_49:
1275 case RTL_GIGA_MAC_VER_50:
1276 case RTL_GIGA_MAC_VER_51:
1277 return r8168ep_ocp_read(tp, mask, reg);
1278 default:
1279 BUG();
1280 return ~0;
1281 }
1282}
1283
1284static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1285 u32 data)
1286{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001287 RTL_W32(tp, OCPDR, data);
1288 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001289 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1290}
1291
1292static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1293 u32 data)
1294{
1295 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1296 data, ERIAR_OOB);
1297}
1298
1299static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1300{
1301 switch (tp->mac_version) {
1302 case RTL_GIGA_MAC_VER_27:
1303 case RTL_GIGA_MAC_VER_28:
1304 case RTL_GIGA_MAC_VER_31:
1305 r8168dp_ocp_write(tp, mask, reg, data);
1306 break;
1307 case RTL_GIGA_MAC_VER_49:
1308 case RTL_GIGA_MAC_VER_50:
1309 case RTL_GIGA_MAC_VER_51:
1310 r8168ep_ocp_write(tp, mask, reg, data);
1311 break;
1312 default:
1313 BUG();
1314 break;
1315 }
1316}
1317
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001318static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1319{
1320 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1321
1322 ocp_write(tp, 0x1, 0x30, 0x00000001);
1323}
1324
1325#define OOB_CMD_RESET 0x00
1326#define OOB_CMD_DRIVER_START 0x05
1327#define OOB_CMD_DRIVER_STOP 0x06
1328
1329static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1330{
1331 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1332}
1333
1334DECLARE_RTL_COND(rtl_ocp_read_cond)
1335{
1336 u16 reg;
1337
1338 reg = rtl8168_get_ocp_reg(tp);
1339
1340 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1341}
1342
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001343DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1344{
1345 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1346}
1347
1348DECLARE_RTL_COND(rtl_ocp_tx_cond)
1349{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001350 return RTL_R8(tp, IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001351}
1352
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001353static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1354{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001355 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001356 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001357 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1358 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001359}
1360
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001361static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001362{
1363 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001364 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1365}
1366
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001367static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1368{
1369 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1370 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1371 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1372}
1373
1374static void rtl8168_driver_start(struct rtl8169_private *tp)
1375{
1376 switch (tp->mac_version) {
1377 case RTL_GIGA_MAC_VER_27:
1378 case RTL_GIGA_MAC_VER_28:
1379 case RTL_GIGA_MAC_VER_31:
1380 rtl8168dp_driver_start(tp);
1381 break;
1382 case RTL_GIGA_MAC_VER_49:
1383 case RTL_GIGA_MAC_VER_50:
1384 case RTL_GIGA_MAC_VER_51:
1385 rtl8168ep_driver_start(tp);
1386 break;
1387 default:
1388 BUG();
1389 break;
1390 }
1391}
1392
1393static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1394{
1395 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1396 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1397}
1398
1399static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1400{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001401 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001402 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1403 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1404 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1405}
1406
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001407static void rtl8168_driver_stop(struct rtl8169_private *tp)
1408{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001409 switch (tp->mac_version) {
1410 case RTL_GIGA_MAC_VER_27:
1411 case RTL_GIGA_MAC_VER_28:
1412 case RTL_GIGA_MAC_VER_31:
1413 rtl8168dp_driver_stop(tp);
1414 break;
1415 case RTL_GIGA_MAC_VER_49:
1416 case RTL_GIGA_MAC_VER_50:
1417 case RTL_GIGA_MAC_VER_51:
1418 rtl8168ep_driver_stop(tp);
1419 break;
1420 default:
1421 BUG();
1422 break;
1423 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001424}
1425
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001426static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001427{
1428 u16 reg = rtl8168_get_ocp_reg(tp);
1429
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001430 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001431}
1432
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001433static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001434{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001435 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001436}
1437
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001438static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001439{
1440 switch (tp->mac_version) {
1441 case RTL_GIGA_MAC_VER_27:
1442 case RTL_GIGA_MAC_VER_28:
1443 case RTL_GIGA_MAC_VER_31:
1444 return r8168dp_check_dash(tp);
1445 case RTL_GIGA_MAC_VER_49:
1446 case RTL_GIGA_MAC_VER_50:
1447 case RTL_GIGA_MAC_VER_51:
1448 return r8168ep_check_dash(tp);
1449 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001450 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001451 }
1452}
1453
françois romieuc28aa382011-08-02 03:53:43 +00001454struct exgmac_reg {
1455 u16 addr;
1456 u16 mask;
1457 u32 val;
1458};
1459
Francois Romieufdf6fc02012-07-06 22:40:38 +02001460static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001461 const struct exgmac_reg *r, int len)
1462{
1463 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001464 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001465 r++;
1466 }
1467}
1468
Francois Romieuffc46952012-07-06 14:19:23 +02001469DECLARE_RTL_COND(rtl_efusear_cond)
1470{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001471 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001472}
1473
Francois Romieufdf6fc02012-07-06 22:40:38 +02001474static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001475{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001476 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
françois romieudaf9df62009-10-07 12:44:20 +00001477
Francois Romieuffc46952012-07-06 14:19:23 +02001478 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001479 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001480}
1481
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001482static u16 rtl_get_events(struct rtl8169_private *tp)
1483{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001484 return RTL_R16(tp, IntrStatus);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001485}
1486
1487static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1488{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001489 RTL_W16(tp, IntrStatus, bits);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001490 mmiowb();
1491}
1492
1493static void rtl_irq_disable(struct rtl8169_private *tp)
1494{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001495 RTL_W16(tp, IntrMask, 0);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001496 mmiowb();
1497}
1498
Francois Romieu3e990ff2012-01-26 12:50:01 +01001499static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1500{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001501 RTL_W16(tp, IntrMask, bits);
Francois Romieu3e990ff2012-01-26 12:50:01 +01001502}
1503
Francois Romieuda78dbf2012-01-26 14:18:23 +01001504#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1505#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1506#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1507
1508static void rtl_irq_enable_all(struct rtl8169_private *tp)
1509{
1510 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1511}
1512
françois romieu811fd302011-12-04 20:30:45 +00001513static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001515 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001516 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001517 RTL_R8(tp, ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
1519
françois romieu4da19632011-01-03 15:07:55 +00001520static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001522 return RTL_R32(tp, TBICSR) & TBIReset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
françois romieu4da19632011-01-03 15:07:55 +00001525static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
françois romieu4da19632011-01-03 15:07:55 +00001527 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001530static unsigned int rtl8169_tbi_link_ok(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001532 return RTL_R32(tp, TBICSR) & TBILinkOk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001535static unsigned int rtl8169_xmii_link_ok(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001537 return RTL_R8(tp, PHYstatus) & LinkStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
françois romieu4da19632011-01-03 15:07:55 +00001540static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001542 RTL_W32(tp, TBICSR, RTL_R32(tp, TBICSR) | TBIReset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
1544
françois romieu4da19632011-01-03 15:07:55 +00001545static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
1547 unsigned int val;
1548
françois romieu4da19632011-01-03 15:07:55 +00001549 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1550 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
Hayes Wang70090422011-07-06 15:58:06 +08001553static void rtl_link_chg_patch(struct rtl8169_private *tp)
1554{
Hayes Wang70090422011-07-06 15:58:06 +08001555 struct net_device *dev = tp->dev;
1556
1557 if (!netif_running(dev))
1558 return;
1559
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001560 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1561 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001562 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001563 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1564 ERIAR_EXGMAC);
1565 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1566 ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001567 } else if (RTL_R8(tp, PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001568 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1569 ERIAR_EXGMAC);
1570 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1571 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001572 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001573 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1574 ERIAR_EXGMAC);
1575 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1576 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001577 }
1578 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001579 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001580 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001581 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001582 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001583 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1584 tp->mac_version == RTL_GIGA_MAC_VER_36) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001585 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001586 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1587 ERIAR_EXGMAC);
1588 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1589 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001590 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001591 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1592 ERIAR_EXGMAC);
1593 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1594 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001595 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001596 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001597 if (RTL_R8(tp, PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001598 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1599 ERIAR_EXGMAC);
1600 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1601 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001602 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001603 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1604 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001605 }
Hayes Wang70090422011-07-06 15:58:06 +08001606 }
1607}
1608
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01001609static void rtl8169_check_link_status(struct net_device *dev,
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001610 struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001612 if (tp->link_ok(tp)) {
Hayes Wang70090422011-07-06 15:58:06 +08001613 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001614 /* This is to cancel a scheduled suspend if there's one. */
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01001615 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001617 if (net_ratelimit())
1618 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001619 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001621 netif_info(tp, ifdown, dev, "link down\n");
Heiner Kallweita92a0842018-01-08 21:39:13 +01001622 pm_runtime_idle(&tp->pci_dev->dev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
1625
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001626#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1627
1628static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1629{
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001630 u8 options;
1631 u32 wolopts = 0;
1632
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001633 options = RTL_R8(tp, Config1);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001634 if (!(options & PMEnable))
1635 return 0;
1636
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001637 options = RTL_R8(tp, Config3);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001638 if (options & LinkUp)
1639 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001640 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001641 case RTL_GIGA_MAC_VER_34:
1642 case RTL_GIGA_MAC_VER_35:
1643 case RTL_GIGA_MAC_VER_36:
1644 case RTL_GIGA_MAC_VER_37:
1645 case RTL_GIGA_MAC_VER_38:
1646 case RTL_GIGA_MAC_VER_40:
1647 case RTL_GIGA_MAC_VER_41:
1648 case RTL_GIGA_MAC_VER_42:
1649 case RTL_GIGA_MAC_VER_43:
1650 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001651 case RTL_GIGA_MAC_VER_45:
1652 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001653 case RTL_GIGA_MAC_VER_47:
1654 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001655 case RTL_GIGA_MAC_VER_49:
1656 case RTL_GIGA_MAC_VER_50:
1657 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001658 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1659 wolopts |= WAKE_MAGIC;
1660 break;
1661 default:
1662 if (options & MagicPacket)
1663 wolopts |= WAKE_MAGIC;
1664 break;
1665 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001666
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001667 options = RTL_R8(tp, Config5);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001668 if (options & UWF)
1669 wolopts |= WAKE_UCAST;
1670 if (options & BWF)
1671 wolopts |= WAKE_BCAST;
1672 if (options & MWF)
1673 wolopts |= WAKE_MCAST;
1674
1675 return wolopts;
1676}
1677
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001678static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1679{
1680 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001681 struct device *d = &tp->pci_dev->dev;
1682
1683 pm_runtime_get_noresume(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001684
Francois Romieuda78dbf2012-01-26 14:18:23 +01001685 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001686
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001687 wol->supported = WAKE_ANY;
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001688 if (pm_runtime_active(d))
1689 wol->wolopts = __rtl8169_get_wol(tp);
1690 else
1691 wol->wolopts = tp->saved_wolopts;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001692
Francois Romieuda78dbf2012-01-26 14:18:23 +01001693 rtl_unlock_work(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001694
1695 pm_runtime_put_noidle(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001696}
1697
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001698static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001699{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001700 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001701 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001702 u32 opt;
1703 u16 reg;
1704 u8 mask;
1705 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001706 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001707 { WAKE_UCAST, Config5, UWF },
1708 { WAKE_BCAST, Config5, BWF },
1709 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001710 { WAKE_ANY, Config5, LanWake },
1711 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001712 };
Francois Romieu851e6022012-04-17 11:10:11 +02001713 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001714
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001715 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001716
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001717 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001718 case RTL_GIGA_MAC_VER_34:
1719 case RTL_GIGA_MAC_VER_35:
1720 case RTL_GIGA_MAC_VER_36:
1721 case RTL_GIGA_MAC_VER_37:
1722 case RTL_GIGA_MAC_VER_38:
1723 case RTL_GIGA_MAC_VER_40:
1724 case RTL_GIGA_MAC_VER_41:
1725 case RTL_GIGA_MAC_VER_42:
1726 case RTL_GIGA_MAC_VER_43:
1727 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001728 case RTL_GIGA_MAC_VER_45:
1729 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001730 case RTL_GIGA_MAC_VER_47:
1731 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001732 case RTL_GIGA_MAC_VER_49:
1733 case RTL_GIGA_MAC_VER_50:
1734 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001735 tmp = ARRAY_SIZE(cfg) - 1;
1736 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001737 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001738 0x0dc,
1739 ERIAR_MASK_0100,
1740 MagicPacket_v2,
1741 0x0000,
1742 ERIAR_EXGMAC);
1743 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001744 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001745 0x0dc,
1746 ERIAR_MASK_0100,
1747 0x0000,
1748 MagicPacket_v2,
1749 ERIAR_EXGMAC);
1750 break;
1751 default:
1752 tmp = ARRAY_SIZE(cfg);
1753 break;
1754 }
1755
1756 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001757 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001758 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001759 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001760 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001761 }
1762
Francois Romieu851e6022012-04-17 11:10:11 +02001763 switch (tp->mac_version) {
1764 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001765 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001766 if (wolopts)
1767 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001768 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001769 break;
1770 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001771 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001772 if (wolopts)
1773 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001774 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001775 break;
1776 }
1777
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001778 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001779}
1780
1781static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1782{
1783 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001784 struct device *d = &tp->pci_dev->dev;
1785
1786 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001787
Francois Romieuda78dbf2012-01-26 14:18:23 +01001788 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001789
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001790 if (pm_runtime_active(d))
1791 __rtl8169_set_wol(tp, wol->wolopts);
1792 else
1793 tp->saved_wolopts = wol->wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001794
1795 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001796
françois romieuea809072010-11-08 13:23:58 +00001797 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1798
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001799 pm_runtime_put_noidle(d);
1800
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001801 return 0;
1802}
1803
Francois Romieu31bd2042011-04-26 18:58:59 +02001804static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1805{
Francois Romieu85bffe62011-04-27 08:22:39 +02001806 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001807}
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809static void rtl8169_get_drvinfo(struct net_device *dev,
1810 struct ethtool_drvinfo *info)
1811{
1812 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001813 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Rick Jones68aad782011-11-07 13:29:27 +00001815 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1816 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1817 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001818 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001819 if (!IS_ERR_OR_NULL(rtl_fw))
1820 strlcpy(info->fw_version, rtl_fw->version,
1821 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
1824static int rtl8169_get_regs_len(struct net_device *dev)
1825{
1826 return R8169_REGS_SIZE;
1827}
1828
1829static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001830 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
1832 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 int ret = 0;
1834 u32 reg;
1835
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001836 reg = RTL_R32(tp, TBICSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1838 (duplex == DUPLEX_FULL)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001839 RTL_W32(tp, TBICSR, reg & ~(TBINwEnable | TBINwRestart));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 } else if (autoneg == AUTONEG_ENABLE)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001841 RTL_W32(tp, TBICSR, reg | TBINwEnable | TBINwRestart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001843 netif_warn(tp, link, dev,
1844 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 ret = -EOPNOTSUPP;
1846 }
1847
1848 return ret;
1849}
1850
1851static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001852 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
1854 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001855 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001856 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Hayes Wang716b50a2011-02-22 17:26:18 +08001858 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001861 int auto_nego;
1862
françois romieu4da19632011-01-03 15:07:55 +00001863 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001864 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1865 ADVERTISE_100HALF | ADVERTISE_100FULL);
1866
1867 if (adv & ADVERTISED_10baseT_Half)
1868 auto_nego |= ADVERTISE_10HALF;
1869 if (adv & ADVERTISED_10baseT_Full)
1870 auto_nego |= ADVERTISE_10FULL;
1871 if (adv & ADVERTISED_100baseT_Half)
1872 auto_nego |= ADVERTISE_100HALF;
1873 if (adv & ADVERTISED_100baseT_Full)
1874 auto_nego |= ADVERTISE_100FULL;
1875
françois romieu3577aa12009-05-19 10:46:48 +00001876 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1877
françois romieu4da19632011-01-03 15:07:55 +00001878 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001879 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1880
1881 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001882 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001883 if (adv & ADVERTISED_1000baseT_Half)
1884 giga_ctrl |= ADVERTISE_1000HALF;
1885 if (adv & ADVERTISED_1000baseT_Full)
1886 giga_ctrl |= ADVERTISE_1000FULL;
1887 } else if (adv & (ADVERTISED_1000baseT_Half |
1888 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001889 netif_info(tp, link, dev,
1890 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001891 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
françois romieu3577aa12009-05-19 10:46:48 +00001894 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001895
françois romieu4da19632011-01-03 15:07:55 +00001896 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1897 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001898 } else {
françois romieu3577aa12009-05-19 10:46:48 +00001899 if (speed == SPEED_10)
1900 bmcr = 0;
1901 else if (speed == SPEED_100)
1902 bmcr = BMCR_SPEED100;
1903 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001904 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001905
1906 if (duplex == DUPLEX_FULL)
1907 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001908 }
1909
françois romieu4da19632011-01-03 15:07:55 +00001910 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001911
Francois Romieucecb5fd2011-04-01 10:21:07 +02001912 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1913 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001914 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001915 rtl_writephy(tp, 0x17, 0x2138);
1916 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001917 } else {
françois romieu4da19632011-01-03 15:07:55 +00001918 rtl_writephy(tp, 0x17, 0x2108);
1919 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001920 }
1921 }
1922
Oliver Neukum54405cd2011-01-06 21:55:13 +01001923 rc = 0;
1924out:
1925 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
1928static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001929 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
1931 struct rtl8169_private *tp = netdev_priv(dev);
1932 int ret;
1933
Oliver Neukum54405cd2011-01-06 21:55:13 +01001934 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001935 if (ret < 0)
1936 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Francois Romieu4876cc12011-03-11 21:07:11 +01001938 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
Chun-Hao Linc4556972016-03-11 14:21:14 +08001939 (advertising & ADVERTISED_1000baseT_Full) &&
1940 !pci_is_pcie(tp->pci_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001942 }
1943out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 return ret;
1945}
1946
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001947static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1948 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Francois Romieud58d46b2011-05-03 16:38:29 +02001950 struct rtl8169_private *tp = netdev_priv(dev);
1951
Francois Romieu2b7b4312011-04-18 22:53:24 -07001952 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001953 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Francois Romieud58d46b2011-05-03 16:38:29 +02001955 if (dev->mtu > JUMBO_1K &&
1956 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1957 features &= ~NETIF_F_IP_CSUM;
1958
Michał Mirosław350fb322011-04-08 06:35:56 +00001959 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
Francois Romieuda78dbf2012-01-26 14:18:23 +01001962static void __rtl8169_set_features(struct net_device *dev,
1963 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001966 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001968 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001969 if (features & NETIF_F_RXALL)
1970 rx_config |= (AcceptErr | AcceptRunt);
1971 else
1972 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001974 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001975
hayeswang929a0312014-09-16 11:40:47 +08001976 if (features & NETIF_F_RXCSUM)
1977 tp->cp_cmd |= RxChkSum;
1978 else
1979 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001980
hayeswang929a0312014-09-16 11:40:47 +08001981 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1982 tp->cp_cmd |= RxVlan;
1983 else
1984 tp->cp_cmd &= ~RxVlan;
1985
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001986 tp->cp_cmd |= RTL_R16(tp, CPlusCmd) & ~(RxVlan | RxChkSum);
hayeswang929a0312014-09-16 11:40:47 +08001987
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001988 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1989 RTL_R16(tp, CPlusCmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001990}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Francois Romieuda78dbf2012-01-26 14:18:23 +01001992static int rtl8169_set_features(struct net_device *dev,
1993 netdev_features_t features)
1994{
1995 struct rtl8169_private *tp = netdev_priv(dev);
1996
hayeswang929a0312014-09-16 11:40:47 +08001997 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
1998
Francois Romieuda78dbf2012-01-26 14:18:23 +01001999 rtl_lock_work(tp);
Dan Carpenter85911d72014-09-19 13:40:25 +03002000 if (features ^ dev->features)
hayeswang929a0312014-09-16 11:40:47 +08002001 __rtl8169_set_features(dev, features);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002002 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 return 0;
2005}
2006
Francois Romieuda78dbf2012-01-26 14:18:23 +01002007
Kirill Smelkov810f4892012-11-10 21:11:02 +04002008static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002010 return (skb_vlan_tag_present(skb)) ?
2011 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
Francois Romieu7a8fc772011-03-01 17:18:33 +01002014static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
2016 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Francois Romieu7a8fc772011-03-01 17:18:33 +01002018 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00002019 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002022static int rtl8169_get_link_ksettings_tbi(struct net_device *dev,
2023 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
2025 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 u32 status;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002027 u32 supported, advertising;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002029 supported =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002031 cmd->base.port = PORT_FIBRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002033 status = RTL_R32(tp, TBICSR);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002034 advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
2035 cmd->base.autoneg = !!(status & TBINwEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002037 cmd->base.speed = SPEED_1000;
2038 cmd->base.duplex = DUPLEX_FULL; /* Always set */
2039
2040 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2041 supported);
2042 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2043 advertising);
Francois Romieuccdffb92008-07-26 14:26:06 +02002044
2045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
2047
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002048static int rtl8169_get_link_ksettings_xmii(struct net_device *dev,
2049 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03002053 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
2054
2055 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
2057
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002058static int rtl8169_get_link_ksettings(struct net_device *dev,
2059 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
2061 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02002062 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Francois Romieuda78dbf2012-01-26 14:18:23 +01002064 rtl_lock_work(tp);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002065 rc = tp->get_link_ksettings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002066 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Francois Romieuccdffb92008-07-26 14:26:06 +02002068 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002071static int rtl8169_set_link_ksettings(struct net_device *dev,
2072 const struct ethtool_link_ksettings *cmd)
2073{
2074 struct rtl8169_private *tp = netdev_priv(dev);
2075 int rc;
2076 u32 advertising;
2077
2078 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
2079 cmd->link_modes.advertising))
2080 return -EINVAL;
2081
2082 del_timer_sync(&tp->timer);
2083
2084 rtl_lock_work(tp);
2085 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
2086 cmd->base.duplex, advertising);
2087 rtl_unlock_work(tp);
2088
2089 return rc;
2090}
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2093 void *p)
2094{
Francois Romieu5b0384f2006-08-16 16:00:01 +02002095 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02002096 u32 __iomem *data = tp->mmio_addr;
2097 u32 *dw = p;
2098 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Francois Romieuda78dbf2012-01-26 14:18:23 +01002100 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02002101 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2102 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002103 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104}
2105
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002106static u32 rtl8169_get_msglevel(struct net_device *dev)
2107{
2108 struct rtl8169_private *tp = netdev_priv(dev);
2109
2110 return tp->msg_enable;
2111}
2112
2113static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2114{
2115 struct rtl8169_private *tp = netdev_priv(dev);
2116
2117 tp->msg_enable = value;
2118}
2119
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002120static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2121 "tx_packets",
2122 "rx_packets",
2123 "tx_errors",
2124 "rx_errors",
2125 "rx_missed",
2126 "align_errors",
2127 "tx_single_collisions",
2128 "tx_multi_collisions",
2129 "unicast",
2130 "broadcast",
2131 "multicast",
2132 "tx_aborted",
2133 "tx_underrun",
2134};
2135
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002136static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002137{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002138 switch (sset) {
2139 case ETH_SS_STATS:
2140 return ARRAY_SIZE(rtl8169_gstrings);
2141 default:
2142 return -EOPNOTSUPP;
2143 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002144}
2145
Corinna Vinschen42020322015-09-10 10:47:35 +02002146DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002147{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002148 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002149}
2150
Corinna Vinschen42020322015-09-10 10:47:35 +02002151static bool rtl8169_do_counters(struct net_device *dev, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002152{
2153 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen42020322015-09-10 10:47:35 +02002154 dma_addr_t paddr = tp->counters_phys_addr;
2155 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02002156
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002157 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
2158 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02002159 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002160 RTL_W32(tp, CounterAddrLow, cmd);
2161 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02002162
Francois Romieua78e9362018-01-26 01:53:26 +01002163 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002164}
2165
2166static bool rtl8169_reset_counters(struct net_device *dev)
2167{
2168 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002169
2170 /*
2171 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2172 * tally counters.
2173 */
2174 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2175 return true;
2176
Corinna Vinschen42020322015-09-10 10:47:35 +02002177 return rtl8169_do_counters(dev, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02002178}
2179
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002180static bool rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002181{
2182 struct rtl8169_private *tp = netdev_priv(dev);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002183
Ivan Vecera355423d2009-02-06 21:49:57 -08002184 /*
2185 * Some chips are unable to dump tally counters when the receiver
2186 * is disabled.
2187 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002188 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002189 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002190
Corinna Vinschen42020322015-09-10 10:47:35 +02002191 return rtl8169_do_counters(dev, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002192}
2193
2194static bool rtl8169_init_counter_offsets(struct net_device *dev)
2195{
2196 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen42020322015-09-10 10:47:35 +02002197 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002198 bool ret = false;
2199
2200 /*
2201 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2202 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2203 * reset by a power cycle, while the counter values collected by the
2204 * driver are reset at every driver unload/load cycle.
2205 *
2206 * To make sure the HW values returned by @get_stats64 match the SW
2207 * values, we collect the initial values at first open(*) and use them
2208 * as offsets to normalize the values returned by @get_stats64.
2209 *
2210 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2211 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2212 * set at open time by rtl_hw_start.
2213 */
2214
2215 if (tp->tc_offset.inited)
2216 return true;
2217
2218 /* If both, reset and update fail, propagate to caller. */
2219 if (rtl8169_reset_counters(dev))
2220 ret = true;
2221
2222 if (rtl8169_update_counters(dev))
2223 ret = true;
2224
Corinna Vinschen42020322015-09-10 10:47:35 +02002225 tp->tc_offset.tx_errors = counters->tx_errors;
2226 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2227 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002228 tp->tc_offset.inited = true;
2229
2230 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002231}
2232
Ivan Vecera355423d2009-02-06 21:49:57 -08002233static void rtl8169_get_ethtool_stats(struct net_device *dev,
2234 struct ethtool_stats *stats, u64 *data)
2235{
2236 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Line0636232016-07-29 16:37:55 +08002237 struct device *d = &tp->pci_dev->dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02002238 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08002239
2240 ASSERT_RTNL();
2241
Chun-Hao Line0636232016-07-29 16:37:55 +08002242 pm_runtime_get_noresume(d);
2243
2244 if (pm_runtime_active(d))
2245 rtl8169_update_counters(dev);
2246
2247 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08002248
Corinna Vinschen42020322015-09-10 10:47:35 +02002249 data[0] = le64_to_cpu(counters->tx_packets);
2250 data[1] = le64_to_cpu(counters->rx_packets);
2251 data[2] = le64_to_cpu(counters->tx_errors);
2252 data[3] = le32_to_cpu(counters->rx_errors);
2253 data[4] = le16_to_cpu(counters->rx_missed);
2254 data[5] = le16_to_cpu(counters->align_errors);
2255 data[6] = le32_to_cpu(counters->tx_one_collision);
2256 data[7] = le32_to_cpu(counters->tx_multi_collision);
2257 data[8] = le64_to_cpu(counters->rx_unicast);
2258 data[9] = le64_to_cpu(counters->rx_broadcast);
2259 data[10] = le32_to_cpu(counters->rx_multicast);
2260 data[11] = le16_to_cpu(counters->tx_aborted);
2261 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08002262}
2263
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002264static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2265{
2266 switch(stringset) {
2267 case ETH_SS_STATS:
2268 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2269 break;
2270 }
2271}
2272
Florian Fainellif0903ea2016-12-03 12:01:19 -08002273static int rtl8169_nway_reset(struct net_device *dev)
2274{
2275 struct rtl8169_private *tp = netdev_priv(dev);
2276
2277 return mii_nway_restart(&tp->mii);
2278}
2279
Francois Romieu50970832017-10-27 13:24:49 +03002280/*
2281 * Interrupt coalescing
2282 *
2283 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2284 * > 8169, 8168 and 810x line of chipsets
2285 *
2286 * 8169, 8168, and 8136(810x) serial chipsets support it.
2287 *
2288 * > 2 - the Tx timer unit at gigabit speed
2289 *
2290 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2291 * (0xe0) bit 1 and bit 0.
2292 *
2293 * For 8169
2294 * bit[1:0] \ speed 1000M 100M 10M
2295 * 0 0 320ns 2.56us 40.96us
2296 * 0 1 2.56us 20.48us 327.7us
2297 * 1 0 5.12us 40.96us 655.4us
2298 * 1 1 10.24us 81.92us 1.31ms
2299 *
2300 * For the other
2301 * bit[1:0] \ speed 1000M 100M 10M
2302 * 0 0 5us 2.56us 40.96us
2303 * 0 1 40us 20.48us 327.7us
2304 * 1 0 80us 40.96us 655.4us
2305 * 1 1 160us 81.92us 1.31ms
2306 */
2307
2308/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2309struct rtl_coalesce_scale {
2310 /* Rx / Tx */
2311 u32 nsecs[2];
2312};
2313
2314/* rx/tx scale factors for all CPlusCmd[0:1] cases */
2315struct rtl_coalesce_info {
2316 u32 speed;
2317 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2318};
2319
2320/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2321#define rxtx_x1822(r, t) { \
2322 {{(r), (t)}}, \
2323 {{(r)*8, (t)*8}}, \
2324 {{(r)*8*2, (t)*8*2}}, \
2325 {{(r)*8*2*2, (t)*8*2*2}}, \
2326}
2327static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2328 /* speed delays: rx00 tx00 */
2329 { SPEED_10, rxtx_x1822(40960, 40960) },
2330 { SPEED_100, rxtx_x1822( 2560, 2560) },
2331 { SPEED_1000, rxtx_x1822( 320, 320) },
2332 { 0 },
2333};
2334
2335static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2336 /* speed delays: rx00 tx00 */
2337 { SPEED_10, rxtx_x1822(40960, 40960) },
2338 { SPEED_100, rxtx_x1822( 2560, 2560) },
2339 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2340 { 0 },
2341};
2342#undef rxtx_x1822
2343
2344/* get rx/tx scale vector corresponding to current speed */
2345static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2346{
2347 struct rtl8169_private *tp = netdev_priv(dev);
2348 struct ethtool_link_ksettings ecmd;
2349 const struct rtl_coalesce_info *ci;
2350 int rc;
2351
2352 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2353 if (rc < 0)
2354 return ERR_PTR(rc);
2355
2356 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2357 if (ecmd.base.speed == ci->speed) {
2358 return ci;
2359 }
2360 }
2361
2362 return ERR_PTR(-ELNRNG);
2363}
2364
2365static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2366{
2367 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002368 const struct rtl_coalesce_info *ci;
2369 const struct rtl_coalesce_scale *scale;
2370 struct {
2371 u32 *max_frames;
2372 u32 *usecs;
2373 } coal_settings [] = {
2374 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2375 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2376 }, *p = coal_settings;
2377 int i;
2378 u16 w;
2379
2380 memset(ec, 0, sizeof(*ec));
2381
2382 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2383 ci = rtl_coalesce_info(dev);
2384 if (IS_ERR(ci))
2385 return PTR_ERR(ci);
2386
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002387 scale = &ci->scalev[RTL_R16(tp, CPlusCmd) & 3];
Francois Romieu50970832017-10-27 13:24:49 +03002388
2389 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002390 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03002391 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2392 w >>= RTL_COALESCE_SHIFT;
2393 *p->usecs = w & RTL_COALESCE_MASK;
2394 }
2395
2396 for (i = 0; i < 2; i++) {
2397 p = coal_settings + i;
2398 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2399
2400 /*
2401 * ethtool_coalesce says it is illegal to set both usecs and
2402 * max_frames to 0.
2403 */
2404 if (!*p->usecs && !*p->max_frames)
2405 *p->max_frames = 1;
2406 }
2407
2408 return 0;
2409}
2410
2411/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2412static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2413 struct net_device *dev, u32 nsec, u16 *cp01)
2414{
2415 const struct rtl_coalesce_info *ci;
2416 u16 i;
2417
2418 ci = rtl_coalesce_info(dev);
2419 if (IS_ERR(ci))
2420 return ERR_CAST(ci);
2421
2422 for (i = 0; i < 4; i++) {
2423 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2424 ci->scalev[i].nsecs[1]);
2425 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2426 *cp01 = i;
2427 return &ci->scalev[i];
2428 }
2429 }
2430
2431 return ERR_PTR(-EINVAL);
2432}
2433
2434static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2435{
2436 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002437 const struct rtl_coalesce_scale *scale;
2438 struct {
2439 u32 frames;
2440 u32 usecs;
2441 } coal_settings [] = {
2442 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2443 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2444 }, *p = coal_settings;
2445 u16 w = 0, cp01;
2446 int i;
2447
2448 scale = rtl_coalesce_choose_scale(dev,
2449 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2450 if (IS_ERR(scale))
2451 return PTR_ERR(scale);
2452
2453 for (i = 0; i < 2; i++, p++) {
2454 u32 units;
2455
2456 /*
2457 * accept max_frames=1 we returned in rtl_get_coalesce.
2458 * accept it not only when usecs=0 because of e.g. the following scenario:
2459 *
2460 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2461 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2462 * - then user does `ethtool -C eth0 rx-usecs 100`
2463 *
2464 * since ethtool sends to kernel whole ethtool_coalesce
2465 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2466 * we'll reject it below in `frames % 4 != 0`.
2467 */
2468 if (p->frames == 1) {
2469 p->frames = 0;
2470 }
2471
2472 units = p->usecs * 1000 / scale->nsecs[i];
2473 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2474 return -EINVAL;
2475
2476 w <<= RTL_COALESCE_SHIFT;
2477 w |= units;
2478 w <<= RTL_COALESCE_SHIFT;
2479 w |= p->frames >> 2;
2480 }
2481
2482 rtl_lock_work(tp);
2483
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002484 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03002485
2486 tp->cp_cmd = (tp->cp_cmd & ~3) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002487 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2488 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03002489
2490 rtl_unlock_work(tp);
2491
2492 return 0;
2493}
2494
Jeff Garzik7282d492006-09-13 14:30:00 -04002495static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 .get_drvinfo = rtl8169_get_drvinfo,
2497 .get_regs_len = rtl8169_get_regs_len,
2498 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002499 .get_coalesce = rtl_get_coalesce,
2500 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002501 .get_msglevel = rtl8169_get_msglevel,
2502 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002504 .get_wol = rtl8169_get_wol,
2505 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002506 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002507 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002508 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002509 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainellif0903ea2016-12-03 12:01:19 -08002510 .nway_reset = rtl8169_nway_reset,
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002511 .get_link_ksettings = rtl8169_get_link_ksettings,
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002512 .set_link_ksettings = rtl8169_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513};
2514
Francois Romieu07d3f512007-02-21 22:40:46 +01002515static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02002516 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
Francois Romieu0e485152007-02-20 00:00:26 +01002518 /*
2519 * The driver currently handles the 8168Bf and the 8168Be identically
2520 * but they can be identified more specifically through the test below
2521 * if needed:
2522 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002523 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002524 *
2525 * Same thing for the 8101Eb and the 8101Ec:
2526 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002527 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002528 */
Francois Romieu37441002011-06-17 22:58:54 +02002529 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002531 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 int mac_version;
2533 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002534 /* 8168EP family. */
2535 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2536 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2537 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2538
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002539 /* 8168H family. */
2540 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2541 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2542
Hayes Wangc5583862012-07-02 17:23:22 +08002543 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002544 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002545 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002546 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2547 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2548
Hayes Wangc2218922011-09-06 16:55:18 +08002549 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002550 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002551 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2552 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2553
hayeswang01dc7fe2011-03-21 01:50:28 +00002554 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002555 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002556 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2557 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2558 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2559
Francois Romieu5b538df2008-07-20 16:22:45 +02002560 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002561 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2562 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002563 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002564
françois romieue6de30d2011-01-03 15:08:37 +00002565 /* 8168DP family. */
2566 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2567 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002568 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002569
Francois Romieuef808d52008-06-29 13:10:54 +02002570 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07002571 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02002572 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002573 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002574 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002575 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2576 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002577 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02002578 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02002579 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002580
2581 /* 8168B family. */
2582 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2583 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2584 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2585 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2586
2587 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002588 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2589 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002590 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00002591 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002592 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2593 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2594 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002595 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2596 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2597 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2598 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2599 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2600 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002601 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002602 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002603 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002604 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2605 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002606 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2607 /* FIXME: where did these entries come from ? -- FR */
2608 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2609 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2610
2611 /* 8110 family. */
2612 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2613 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2614 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2615 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2616 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2617 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2618
Jean Delvaref21b75e2009-05-26 20:54:48 -07002619 /* Catch-all */
2620 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002621 };
2622 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 u32 reg;
2624
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002625 reg = RTL_R32(tp, TxConfig);
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002626 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 p++;
2628 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002629
2630 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2631 netif_notice(tp, probe, dev,
2632 "unknown MAC, using family default\n");
2633 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002634 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2635 tp->mac_version = tp->mii.supports_gmii ?
2636 RTL_GIGA_MAC_VER_42 :
2637 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002638 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2639 tp->mac_version = tp->mii.supports_gmii ?
2640 RTL_GIGA_MAC_VER_45 :
2641 RTL_GIGA_MAC_VER_47;
2642 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2643 tp->mac_version = tp->mii.supports_gmii ?
2644 RTL_GIGA_MAC_VER_46 :
2645 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647}
2648
2649static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2650{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002651 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652}
2653
Francois Romieu867763c2007-08-17 18:21:58 +02002654struct phy_reg {
2655 u16 reg;
2656 u16 val;
2657};
2658
françois romieu4da19632011-01-03 15:07:55 +00002659static void rtl_writephy_batch(struct rtl8169_private *tp,
2660 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002661{
2662 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002663 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002664 regs++;
2665 }
2666}
2667
françois romieubca03d52011-01-03 15:07:31 +00002668#define PHY_READ 0x00000000
2669#define PHY_DATA_OR 0x10000000
2670#define PHY_DATA_AND 0x20000000
2671#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002672#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002673#define PHY_CLEAR_READCOUNT 0x70000000
2674#define PHY_WRITE 0x80000000
2675#define PHY_READCOUNT_EQ_SKIP 0x90000000
2676#define PHY_COMP_EQ_SKIPN 0xa0000000
2677#define PHY_COMP_NEQ_SKIPN 0xb0000000
2678#define PHY_WRITE_PREVIOUS 0xc0000000
2679#define PHY_SKIPN 0xd0000000
2680#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002681
Hayes Wang960aee62011-06-18 11:37:48 +02002682struct fw_info {
2683 u32 magic;
2684 char version[RTL_VER_SIZE];
2685 __le32 fw_start;
2686 __le32 fw_len;
2687 u8 chksum;
2688} __packed;
2689
Francois Romieu1c361ef2011-06-17 17:16:24 +02002690#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2691
2692static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002693{
Francois Romieub6ffd972011-06-17 17:00:05 +02002694 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002695 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002696 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2697 char *version = rtl_fw->version;
2698 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002699
Francois Romieu1c361ef2011-06-17 17:16:24 +02002700 if (fw->size < FW_OPCODE_SIZE)
2701 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002702
2703 if (!fw_info->magic) {
2704 size_t i, size, start;
2705 u8 checksum = 0;
2706
2707 if (fw->size < sizeof(*fw_info))
2708 goto out;
2709
2710 for (i = 0; i < fw->size; i++)
2711 checksum += fw->data[i];
2712 if (checksum != 0)
2713 goto out;
2714
2715 start = le32_to_cpu(fw_info->fw_start);
2716 if (start > fw->size)
2717 goto out;
2718
2719 size = le32_to_cpu(fw_info->fw_len);
2720 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2721 goto out;
2722
2723 memcpy(version, fw_info->version, RTL_VER_SIZE);
2724
2725 pa->code = (__le32 *)(fw->data + start);
2726 pa->size = size;
2727 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002728 if (fw->size % FW_OPCODE_SIZE)
2729 goto out;
2730
2731 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2732
2733 pa->code = (__le32 *)fw->data;
2734 pa->size = fw->size / FW_OPCODE_SIZE;
2735 }
2736 version[RTL_VER_SIZE - 1] = 0;
2737
2738 rc = true;
2739out:
2740 return rc;
2741}
2742
Francois Romieufd112f22011-06-18 00:10:29 +02002743static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2744 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002745{
Francois Romieufd112f22011-06-18 00:10:29 +02002746 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002747 size_t index;
2748
Francois Romieu1c361ef2011-06-17 17:16:24 +02002749 for (index = 0; index < pa->size; index++) {
2750 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002751 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002752
hayeswang42b82dc2011-01-10 02:07:25 +00002753 switch(action & 0xf0000000) {
2754 case PHY_READ:
2755 case PHY_DATA_OR:
2756 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002757 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002758 case PHY_CLEAR_READCOUNT:
2759 case PHY_WRITE:
2760 case PHY_WRITE_PREVIOUS:
2761 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002762 break;
2763
hayeswang42b82dc2011-01-10 02:07:25 +00002764 case PHY_BJMPN:
2765 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002766 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002767 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002768 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002769 }
2770 break;
2771 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002772 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002773 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002774 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002775 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002776 }
2777 break;
2778 case PHY_COMP_EQ_SKIPN:
2779 case PHY_COMP_NEQ_SKIPN:
2780 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002781 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002782 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002783 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002784 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002785 }
2786 break;
2787
hayeswang42b82dc2011-01-10 02:07:25 +00002788 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002789 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002790 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002791 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002792 }
2793 }
Francois Romieufd112f22011-06-18 00:10:29 +02002794 rc = true;
2795out:
2796 return rc;
2797}
françois romieubca03d52011-01-03 15:07:31 +00002798
Francois Romieufd112f22011-06-18 00:10:29 +02002799static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2800{
2801 struct net_device *dev = tp->dev;
2802 int rc = -EINVAL;
2803
2804 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002805 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002806 goto out;
2807 }
2808
2809 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2810 rc = 0;
2811out:
2812 return rc;
2813}
2814
2815static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2816{
2817 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002818 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002819 u32 predata, count;
2820 size_t index;
2821
2822 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002823 org.write = ops->write;
2824 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002825
Francois Romieu1c361ef2011-06-17 17:16:24 +02002826 for (index = 0; index < pa->size; ) {
2827 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002828 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002829 u32 regno = (action & 0x0fff0000) >> 16;
2830
2831 if (!action)
2832 break;
françois romieubca03d52011-01-03 15:07:31 +00002833
2834 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002835 case PHY_READ:
2836 predata = rtl_readphy(tp, regno);
2837 count++;
2838 index++;
françois romieubca03d52011-01-03 15:07:31 +00002839 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002840 case PHY_DATA_OR:
2841 predata |= data;
2842 index++;
2843 break;
2844 case PHY_DATA_AND:
2845 predata &= data;
2846 index++;
2847 break;
2848 case PHY_BJMPN:
2849 index -= regno;
2850 break;
hayeswangeee37862013-04-01 22:23:38 +00002851 case PHY_MDIO_CHG:
2852 if (data == 0) {
2853 ops->write = org.write;
2854 ops->read = org.read;
2855 } else if (data == 1) {
2856 ops->write = mac_mcu_write;
2857 ops->read = mac_mcu_read;
2858 }
2859
hayeswang42b82dc2011-01-10 02:07:25 +00002860 index++;
2861 break;
2862 case PHY_CLEAR_READCOUNT:
2863 count = 0;
2864 index++;
2865 break;
2866 case PHY_WRITE:
2867 rtl_writephy(tp, regno, data);
2868 index++;
2869 break;
2870 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002871 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002872 break;
2873 case PHY_COMP_EQ_SKIPN:
2874 if (predata == data)
2875 index += regno;
2876 index++;
2877 break;
2878 case PHY_COMP_NEQ_SKIPN:
2879 if (predata != data)
2880 index += regno;
2881 index++;
2882 break;
2883 case PHY_WRITE_PREVIOUS:
2884 rtl_writephy(tp, regno, predata);
2885 index++;
2886 break;
2887 case PHY_SKIPN:
2888 index += regno + 1;
2889 break;
2890 case PHY_DELAY_MS:
2891 mdelay(data);
2892 index++;
2893 break;
2894
françois romieubca03d52011-01-03 15:07:31 +00002895 default:
2896 BUG();
2897 }
2898 }
hayeswangeee37862013-04-01 22:23:38 +00002899
2900 ops->write = org.write;
2901 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002902}
2903
françois romieuf1e02ed2011-01-13 13:07:53 +00002904static void rtl_release_firmware(struct rtl8169_private *tp)
2905{
Francois Romieub6ffd972011-06-17 17:00:05 +02002906 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2907 release_firmware(tp->rtl_fw->fw);
2908 kfree(tp->rtl_fw);
2909 }
2910 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002911}
2912
François Romieu953a12c2011-04-24 17:38:48 +02002913static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002914{
Francois Romieub6ffd972011-06-17 17:00:05 +02002915 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002916
2917 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002918 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002919 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002920}
2921
2922static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2923{
2924 if (rtl_readphy(tp, reg) != val)
2925 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2926 else
2927 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002928}
2929
françois romieu4da19632011-01-03 15:07:55 +00002930static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002932 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002933 { 0x1f, 0x0001 },
2934 { 0x06, 0x006e },
2935 { 0x08, 0x0708 },
2936 { 0x15, 0x4000 },
2937 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
françois romieu0b9b5712009-08-10 19:44:56 +00002939 { 0x1f, 0x0001 },
2940 { 0x03, 0x00a1 },
2941 { 0x02, 0x0008 },
2942 { 0x01, 0x0120 },
2943 { 0x00, 0x1000 },
2944 { 0x04, 0x0800 },
2945 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
françois romieu0b9b5712009-08-10 19:44:56 +00002947 { 0x03, 0xff41 },
2948 { 0x02, 0xdf60 },
2949 { 0x01, 0x0140 },
2950 { 0x00, 0x0077 },
2951 { 0x04, 0x7800 },
2952 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
françois romieu0b9b5712009-08-10 19:44:56 +00002954 { 0x03, 0x802f },
2955 { 0x02, 0x4f02 },
2956 { 0x01, 0x0409 },
2957 { 0x00, 0xf0f9 },
2958 { 0x04, 0x9800 },
2959 { 0x04, 0x9000 },
2960
2961 { 0x03, 0xdf01 },
2962 { 0x02, 0xdf20 },
2963 { 0x01, 0xff95 },
2964 { 0x00, 0xba00 },
2965 { 0x04, 0xa800 },
2966 { 0x04, 0xa000 },
2967
2968 { 0x03, 0xff41 },
2969 { 0x02, 0xdf20 },
2970 { 0x01, 0x0140 },
2971 { 0x00, 0x00bb },
2972 { 0x04, 0xb800 },
2973 { 0x04, 0xb000 },
2974
2975 { 0x03, 0xdf41 },
2976 { 0x02, 0xdc60 },
2977 { 0x01, 0x6340 },
2978 { 0x00, 0x007d },
2979 { 0x04, 0xd800 },
2980 { 0x04, 0xd000 },
2981
2982 { 0x03, 0xdf01 },
2983 { 0x02, 0xdf20 },
2984 { 0x01, 0x100a },
2985 { 0x00, 0xa0ff },
2986 { 0x04, 0xf800 },
2987 { 0x04, 0xf000 },
2988
2989 { 0x1f, 0x0000 },
2990 { 0x0b, 0x0000 },
2991 { 0x00, 0x9200 }
2992 };
2993
françois romieu4da19632011-01-03 15:07:55 +00002994 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995}
2996
françois romieu4da19632011-01-03 15:07:55 +00002997static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002998{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002999 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02003000 { 0x1f, 0x0002 },
3001 { 0x01, 0x90d0 },
3002 { 0x1f, 0x0000 }
3003 };
3004
françois romieu4da19632011-01-03 15:07:55 +00003005 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02003006}
3007
françois romieu4da19632011-01-03 15:07:55 +00003008static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00003009{
3010 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00003011
Sergei Shtylyovccbae552011-07-22 05:37:24 +00003012 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
3013 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00003014 return;
3015
françois romieu4da19632011-01-03 15:07:55 +00003016 rtl_writephy(tp, 0x1f, 0x0001);
3017 rtl_writephy(tp, 0x10, 0xf01b);
3018 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00003019}
3020
françois romieu4da19632011-01-03 15:07:55 +00003021static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00003022{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003023 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00003024 { 0x1f, 0x0001 },
3025 { 0x04, 0x0000 },
3026 { 0x03, 0x00a1 },
3027 { 0x02, 0x0008 },
3028 { 0x01, 0x0120 },
3029 { 0x00, 0x1000 },
3030 { 0x04, 0x0800 },
3031 { 0x04, 0x9000 },
3032 { 0x03, 0x802f },
3033 { 0x02, 0x4f02 },
3034 { 0x01, 0x0409 },
3035 { 0x00, 0xf099 },
3036 { 0x04, 0x9800 },
3037 { 0x04, 0xa000 },
3038 { 0x03, 0xdf01 },
3039 { 0x02, 0xdf20 },
3040 { 0x01, 0xff95 },
3041 { 0x00, 0xba00 },
3042 { 0x04, 0xa800 },
3043 { 0x04, 0xf000 },
3044 { 0x03, 0xdf01 },
3045 { 0x02, 0xdf20 },
3046 { 0x01, 0x101a },
3047 { 0x00, 0xa0ff },
3048 { 0x04, 0xf800 },
3049 { 0x04, 0x0000 },
3050 { 0x1f, 0x0000 },
3051
3052 { 0x1f, 0x0001 },
3053 { 0x10, 0xf41b },
3054 { 0x14, 0xfb54 },
3055 { 0x18, 0xf5c7 },
3056 { 0x1f, 0x0000 },
3057
3058 { 0x1f, 0x0001 },
3059 { 0x17, 0x0cc0 },
3060 { 0x1f, 0x0000 }
3061 };
3062
françois romieu4da19632011-01-03 15:07:55 +00003063 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00003064
françois romieu4da19632011-01-03 15:07:55 +00003065 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003066}
3067
françois romieu4da19632011-01-03 15:07:55 +00003068static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00003069{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003070 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00003071 { 0x1f, 0x0001 },
3072 { 0x04, 0x0000 },
3073 { 0x03, 0x00a1 },
3074 { 0x02, 0x0008 },
3075 { 0x01, 0x0120 },
3076 { 0x00, 0x1000 },
3077 { 0x04, 0x0800 },
3078 { 0x04, 0x9000 },
3079 { 0x03, 0x802f },
3080 { 0x02, 0x4f02 },
3081 { 0x01, 0x0409 },
3082 { 0x00, 0xf099 },
3083 { 0x04, 0x9800 },
3084 { 0x04, 0xa000 },
3085 { 0x03, 0xdf01 },
3086 { 0x02, 0xdf20 },
3087 { 0x01, 0xff95 },
3088 { 0x00, 0xba00 },
3089 { 0x04, 0xa800 },
3090 { 0x04, 0xf000 },
3091 { 0x03, 0xdf01 },
3092 { 0x02, 0xdf20 },
3093 { 0x01, 0x101a },
3094 { 0x00, 0xa0ff },
3095 { 0x04, 0xf800 },
3096 { 0x04, 0x0000 },
3097 { 0x1f, 0x0000 },
3098
3099 { 0x1f, 0x0001 },
3100 { 0x0b, 0x8480 },
3101 { 0x1f, 0x0000 },
3102
3103 { 0x1f, 0x0001 },
3104 { 0x18, 0x67c7 },
3105 { 0x04, 0x2000 },
3106 { 0x03, 0x002f },
3107 { 0x02, 0x4360 },
3108 { 0x01, 0x0109 },
3109 { 0x00, 0x3022 },
3110 { 0x04, 0x2800 },
3111 { 0x1f, 0x0000 },
3112
3113 { 0x1f, 0x0001 },
3114 { 0x17, 0x0cc0 },
3115 { 0x1f, 0x0000 }
3116 };
3117
françois romieu4da19632011-01-03 15:07:55 +00003118 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00003119}
3120
françois romieu4da19632011-01-03 15:07:55 +00003121static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003122{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003123 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003124 { 0x10, 0xf41b },
3125 { 0x1f, 0x0000 }
3126 };
3127
françois romieu4da19632011-01-03 15:07:55 +00003128 rtl_writephy(tp, 0x1f, 0x0001);
3129 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02003130
françois romieu4da19632011-01-03 15:07:55 +00003131 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003132}
3133
françois romieu4da19632011-01-03 15:07:55 +00003134static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003135{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003136 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003137 { 0x1f, 0x0001 },
3138 { 0x10, 0xf41b },
3139 { 0x1f, 0x0000 }
3140 };
3141
françois romieu4da19632011-01-03 15:07:55 +00003142 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003143}
3144
françois romieu4da19632011-01-03 15:07:55 +00003145static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003146{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003147 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02003148 { 0x1f, 0x0000 },
3149 { 0x1d, 0x0f00 },
3150 { 0x1f, 0x0002 },
3151 { 0x0c, 0x1ec8 },
3152 { 0x1f, 0x0000 }
3153 };
3154
françois romieu4da19632011-01-03 15:07:55 +00003155 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02003156}
3157
françois romieu4da19632011-01-03 15:07:55 +00003158static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02003159{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003160 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02003161 { 0x1f, 0x0001 },
3162 { 0x1d, 0x3d98 },
3163 { 0x1f, 0x0000 }
3164 };
3165
françois romieu4da19632011-01-03 15:07:55 +00003166 rtl_writephy(tp, 0x1f, 0x0000);
3167 rtl_patchphy(tp, 0x14, 1 << 5);
3168 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02003169
françois romieu4da19632011-01-03 15:07:55 +00003170 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02003171}
3172
françois romieu4da19632011-01-03 15:07:55 +00003173static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003174{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003175 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02003176 { 0x1f, 0x0001 },
3177 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02003178 { 0x1f, 0x0002 },
3179 { 0x00, 0x88d4 },
3180 { 0x01, 0x82b1 },
3181 { 0x03, 0x7002 },
3182 { 0x08, 0x9e30 },
3183 { 0x09, 0x01f0 },
3184 { 0x0a, 0x5500 },
3185 { 0x0c, 0x00c8 },
3186 { 0x1f, 0x0003 },
3187 { 0x12, 0xc096 },
3188 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02003189 { 0x1f, 0x0000 },
3190 { 0x1f, 0x0000 },
3191 { 0x09, 0x2000 },
3192 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02003193 };
3194
françois romieu4da19632011-01-03 15:07:55 +00003195 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003196
françois romieu4da19632011-01-03 15:07:55 +00003197 rtl_patchphy(tp, 0x14, 1 << 5);
3198 rtl_patchphy(tp, 0x0d, 1 << 5);
3199 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02003200}
3201
françois romieu4da19632011-01-03 15:07:55 +00003202static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02003203{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003204 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02003205 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003206 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003207 { 0x03, 0x802f },
3208 { 0x02, 0x4f02 },
3209 { 0x01, 0x0409 },
3210 { 0x00, 0xf099 },
3211 { 0x04, 0x9800 },
3212 { 0x04, 0x9000 },
3213 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003214 { 0x1f, 0x0002 },
3215 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003216 { 0x06, 0x0761 },
3217 { 0x1f, 0x0003 },
3218 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003219 { 0x1f, 0x0000 }
3220 };
3221
françois romieu4da19632011-01-03 15:07:55 +00003222 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003223
françois romieu4da19632011-01-03 15:07:55 +00003224 rtl_patchphy(tp, 0x16, 1 << 0);
3225 rtl_patchphy(tp, 0x14, 1 << 5);
3226 rtl_patchphy(tp, 0x0d, 1 << 5);
3227 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003228}
3229
françois romieu4da19632011-01-03 15:07:55 +00003230static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02003231{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003232 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02003233 { 0x1f, 0x0001 },
3234 { 0x12, 0x2300 },
3235 { 0x1d, 0x3d98 },
3236 { 0x1f, 0x0002 },
3237 { 0x0c, 0x7eb8 },
3238 { 0x06, 0x5461 },
3239 { 0x1f, 0x0003 },
3240 { 0x16, 0x0f0a },
3241 { 0x1f, 0x0000 }
3242 };
3243
françois romieu4da19632011-01-03 15:07:55 +00003244 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02003245
françois romieu4da19632011-01-03 15:07:55 +00003246 rtl_patchphy(tp, 0x16, 1 << 0);
3247 rtl_patchphy(tp, 0x14, 1 << 5);
3248 rtl_patchphy(tp, 0x0d, 1 << 5);
3249 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003250}
3251
françois romieu4da19632011-01-03 15:07:55 +00003252static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003253{
françois romieu4da19632011-01-03 15:07:55 +00003254 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003255}
3256
françois romieubca03d52011-01-03 15:07:31 +00003257static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003258{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003259 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003260 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003261 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003262 { 0x06, 0x4064 },
3263 { 0x07, 0x2863 },
3264 { 0x08, 0x059c },
3265 { 0x09, 0x26b4 },
3266 { 0x0a, 0x6a19 },
3267 { 0x0b, 0xdcc8 },
3268 { 0x10, 0xf06d },
3269 { 0x14, 0x7f68 },
3270 { 0x18, 0x7fd9 },
3271 { 0x1c, 0xf0ff },
3272 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003273 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003274 { 0x12, 0xf49f },
3275 { 0x13, 0x070b },
3276 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003277 { 0x14, 0x94c0 },
3278
3279 /*
3280 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003281 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003282 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003283 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003284 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003285 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003286 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003287 { 0x06, 0x5561 },
3288
3289 /*
3290 * Can not link to 1Gbps with bad cable
3291 * Decrease SNR threshold form 21.07dB to 19.04dB
3292 */
3293 { 0x1f, 0x0001 },
3294 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003295
3296 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003297 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003298 };
3299
françois romieu4da19632011-01-03 15:07:55 +00003300 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003301
françois romieubca03d52011-01-03 15:07:31 +00003302 /*
3303 * Rx Error Issue
3304 * Fine Tune Switching regulator parameter
3305 */
françois romieu4da19632011-01-03 15:07:55 +00003306 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003307 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3308 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003309
Francois Romieufdf6fc02012-07-06 22:40:38 +02003310 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003311 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003312 { 0x1f, 0x0002 },
3313 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003314 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003315 { 0x05, 0x8330 },
3316 { 0x06, 0x669a },
3317 { 0x1f, 0x0002 }
3318 };
3319 int val;
3320
françois romieu4da19632011-01-03 15:07:55 +00003321 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003322
françois romieu4da19632011-01-03 15:07:55 +00003323 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003324
3325 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003326 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003327 0x0065, 0x0066, 0x0067, 0x0068,
3328 0x0069, 0x006a, 0x006b, 0x006c
3329 };
3330 int i;
3331
françois romieu4da19632011-01-03 15:07:55 +00003332 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003333
3334 val &= 0xff00;
3335 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003336 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003337 }
3338 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003339 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003340 { 0x1f, 0x0002 },
3341 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003342 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003343 { 0x05, 0x8330 },
3344 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003345 };
3346
françois romieu4da19632011-01-03 15:07:55 +00003347 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003348 }
3349
françois romieubca03d52011-01-03 15:07:31 +00003350 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003351 rtl_writephy(tp, 0x1f, 0x0002);
3352 rtl_patchphy(tp, 0x0d, 0x0300);
3353 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003354
françois romieubca03d52011-01-03 15:07:31 +00003355 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003356 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003357 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3358 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003359
françois romieu4da19632011-01-03 15:07:55 +00003360 rtl_writephy(tp, 0x1f, 0x0005);
3361 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003362
3363 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003364
françois romieu4da19632011-01-03 15:07:55 +00003365 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003366}
3367
françois romieubca03d52011-01-03 15:07:31 +00003368static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003369{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003370 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003371 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003372 { 0x1f, 0x0001 },
3373 { 0x06, 0x4064 },
3374 { 0x07, 0x2863 },
3375 { 0x08, 0x059c },
3376 { 0x09, 0x26b4 },
3377 { 0x0a, 0x6a19 },
3378 { 0x0b, 0xdcc8 },
3379 { 0x10, 0xf06d },
3380 { 0x14, 0x7f68 },
3381 { 0x18, 0x7fd9 },
3382 { 0x1c, 0xf0ff },
3383 { 0x1d, 0x3d9c },
3384 { 0x1f, 0x0003 },
3385 { 0x12, 0xf49f },
3386 { 0x13, 0x070b },
3387 { 0x1a, 0x05ad },
3388 { 0x14, 0x94c0 },
3389
françois romieubca03d52011-01-03 15:07:31 +00003390 /*
3391 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003392 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003393 */
françois romieudaf9df62009-10-07 12:44:20 +00003394 { 0x1f, 0x0002 },
3395 { 0x06, 0x5561 },
3396 { 0x1f, 0x0005 },
3397 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003398 { 0x06, 0x5561 },
3399
3400 /*
3401 * Can not link to 1Gbps with bad cable
3402 * Decrease SNR threshold form 21.07dB to 19.04dB
3403 */
3404 { 0x1f, 0x0001 },
3405 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003406
3407 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003408 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003409 };
3410
françois romieu4da19632011-01-03 15:07:55 +00003411 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003412
Francois Romieufdf6fc02012-07-06 22:40:38 +02003413 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003414 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003415 { 0x1f, 0x0002 },
3416 { 0x05, 0x669a },
3417 { 0x1f, 0x0005 },
3418 { 0x05, 0x8330 },
3419 { 0x06, 0x669a },
3420
3421 { 0x1f, 0x0002 }
3422 };
3423 int val;
3424
françois romieu4da19632011-01-03 15:07:55 +00003425 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003426
françois romieu4da19632011-01-03 15:07:55 +00003427 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003428 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003429 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003430 0x0065, 0x0066, 0x0067, 0x0068,
3431 0x0069, 0x006a, 0x006b, 0x006c
3432 };
3433 int i;
3434
françois romieu4da19632011-01-03 15:07:55 +00003435 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003436
3437 val &= 0xff00;
3438 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003439 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003440 }
3441 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003442 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003443 { 0x1f, 0x0002 },
3444 { 0x05, 0x2642 },
3445 { 0x1f, 0x0005 },
3446 { 0x05, 0x8330 },
3447 { 0x06, 0x2642 }
3448 };
3449
françois romieu4da19632011-01-03 15:07:55 +00003450 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003451 }
3452
françois romieubca03d52011-01-03 15:07:31 +00003453 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003454 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003455 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3456 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003457
françois romieubca03d52011-01-03 15:07:31 +00003458 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003459 rtl_writephy(tp, 0x1f, 0x0002);
3460 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003461
françois romieu4da19632011-01-03 15:07:55 +00003462 rtl_writephy(tp, 0x1f, 0x0005);
3463 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003464
3465 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003466
françois romieu4da19632011-01-03 15:07:55 +00003467 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003468}
3469
françois romieu4da19632011-01-03 15:07:55 +00003470static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003471{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003472 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003473 { 0x1f, 0x0002 },
3474 { 0x10, 0x0008 },
3475 { 0x0d, 0x006c },
3476
3477 { 0x1f, 0x0000 },
3478 { 0x0d, 0xf880 },
3479
3480 { 0x1f, 0x0001 },
3481 { 0x17, 0x0cc0 },
3482
3483 { 0x1f, 0x0001 },
3484 { 0x0b, 0xa4d8 },
3485 { 0x09, 0x281c },
3486 { 0x07, 0x2883 },
3487 { 0x0a, 0x6b35 },
3488 { 0x1d, 0x3da4 },
3489 { 0x1c, 0xeffd },
3490 { 0x14, 0x7f52 },
3491 { 0x18, 0x7fc6 },
3492 { 0x08, 0x0601 },
3493 { 0x06, 0x4063 },
3494 { 0x10, 0xf074 },
3495 { 0x1f, 0x0003 },
3496 { 0x13, 0x0789 },
3497 { 0x12, 0xf4bd },
3498 { 0x1a, 0x04fd },
3499 { 0x14, 0x84b0 },
3500 { 0x1f, 0x0000 },
3501 { 0x00, 0x9200 },
3502
3503 { 0x1f, 0x0005 },
3504 { 0x01, 0x0340 },
3505 { 0x1f, 0x0001 },
3506 { 0x04, 0x4000 },
3507 { 0x03, 0x1d21 },
3508 { 0x02, 0x0c32 },
3509 { 0x01, 0x0200 },
3510 { 0x00, 0x5554 },
3511 { 0x04, 0x4800 },
3512 { 0x04, 0x4000 },
3513 { 0x04, 0xf000 },
3514 { 0x03, 0xdf01 },
3515 { 0x02, 0xdf20 },
3516 { 0x01, 0x101a },
3517 { 0x00, 0xa0ff },
3518 { 0x04, 0xf800 },
3519 { 0x04, 0xf000 },
3520 { 0x1f, 0x0000 },
3521
3522 { 0x1f, 0x0007 },
3523 { 0x1e, 0x0023 },
3524 { 0x16, 0x0000 },
3525 { 0x1f, 0x0000 }
3526 };
3527
françois romieu4da19632011-01-03 15:07:55 +00003528 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003529}
3530
françois romieue6de30d2011-01-03 15:08:37 +00003531static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3532{
3533 static const struct phy_reg phy_reg_init[] = {
3534 { 0x1f, 0x0001 },
3535 { 0x17, 0x0cc0 },
3536
3537 { 0x1f, 0x0007 },
3538 { 0x1e, 0x002d },
3539 { 0x18, 0x0040 },
3540 { 0x1f, 0x0000 }
3541 };
3542
3543 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3544 rtl_patchphy(tp, 0x0d, 1 << 5);
3545}
3546
Hayes Wang70090422011-07-06 15:58:06 +08003547static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003548{
3549 static const struct phy_reg phy_reg_init[] = {
3550 /* Enable Delay cap */
3551 { 0x1f, 0x0005 },
3552 { 0x05, 0x8b80 },
3553 { 0x06, 0xc896 },
3554 { 0x1f, 0x0000 },
3555
3556 /* Channel estimation fine tune */
3557 { 0x1f, 0x0001 },
3558 { 0x0b, 0x6c20 },
3559 { 0x07, 0x2872 },
3560 { 0x1c, 0xefff },
3561 { 0x1f, 0x0003 },
3562 { 0x14, 0x6420 },
3563 { 0x1f, 0x0000 },
3564
3565 /* Update PFM & 10M TX idle timer */
3566 { 0x1f, 0x0007 },
3567 { 0x1e, 0x002f },
3568 { 0x15, 0x1919 },
3569 { 0x1f, 0x0000 },
3570
3571 { 0x1f, 0x0007 },
3572 { 0x1e, 0x00ac },
3573 { 0x18, 0x0006 },
3574 { 0x1f, 0x0000 }
3575 };
3576
Francois Romieu15ecd032011-04-27 13:52:22 -07003577 rtl_apply_firmware(tp);
3578
hayeswang01dc7fe2011-03-21 01:50:28 +00003579 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3580
3581 /* DCO enable for 10M IDLE Power */
3582 rtl_writephy(tp, 0x1f, 0x0007);
3583 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003584 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003585 rtl_writephy(tp, 0x1f, 0x0000);
3586
3587 /* For impedance matching */
3588 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003589 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003590 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003591
3592 /* PHY auto speed down */
3593 rtl_writephy(tp, 0x1f, 0x0007);
3594 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003595 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003596 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003597 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003598
3599 rtl_writephy(tp, 0x1f, 0x0005);
3600 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003601 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003602 rtl_writephy(tp, 0x1f, 0x0000);
3603
3604 rtl_writephy(tp, 0x1f, 0x0005);
3605 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003606 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003607 rtl_writephy(tp, 0x1f, 0x0007);
3608 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003609 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003610 rtl_writephy(tp, 0x1f, 0x0006);
3611 rtl_writephy(tp, 0x00, 0x5a00);
3612 rtl_writephy(tp, 0x1f, 0x0000);
3613 rtl_writephy(tp, 0x0d, 0x0007);
3614 rtl_writephy(tp, 0x0e, 0x003c);
3615 rtl_writephy(tp, 0x0d, 0x4007);
3616 rtl_writephy(tp, 0x0e, 0x0000);
3617 rtl_writephy(tp, 0x0d, 0x0000);
3618}
3619
françois romieu9ecb9aa2012-12-07 11:20:21 +00003620static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3621{
3622 const u16 w[] = {
3623 addr[0] | (addr[1] << 8),
3624 addr[2] | (addr[3] << 8),
3625 addr[4] | (addr[5] << 8)
3626 };
3627 const struct exgmac_reg e[] = {
3628 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3629 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3630 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3631 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3632 };
3633
3634 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3635}
3636
Hayes Wang70090422011-07-06 15:58:06 +08003637static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3638{
3639 static const struct phy_reg phy_reg_init[] = {
3640 /* Enable Delay cap */
3641 { 0x1f, 0x0004 },
3642 { 0x1f, 0x0007 },
3643 { 0x1e, 0x00ac },
3644 { 0x18, 0x0006 },
3645 { 0x1f, 0x0002 },
3646 { 0x1f, 0x0000 },
3647 { 0x1f, 0x0000 },
3648
3649 /* Channel estimation fine tune */
3650 { 0x1f, 0x0003 },
3651 { 0x09, 0xa20f },
3652 { 0x1f, 0x0000 },
3653 { 0x1f, 0x0000 },
3654
3655 /* Green Setting */
3656 { 0x1f, 0x0005 },
3657 { 0x05, 0x8b5b },
3658 { 0x06, 0x9222 },
3659 { 0x05, 0x8b6d },
3660 { 0x06, 0x8000 },
3661 { 0x05, 0x8b76 },
3662 { 0x06, 0x8000 },
3663 { 0x1f, 0x0000 }
3664 };
3665
3666 rtl_apply_firmware(tp);
3667
3668 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3669
3670 /* For 4-corner performance improve */
3671 rtl_writephy(tp, 0x1f, 0x0005);
3672 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003673 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003674 rtl_writephy(tp, 0x1f, 0x0000);
3675
3676 /* PHY auto speed down */
3677 rtl_writephy(tp, 0x1f, 0x0004);
3678 rtl_writephy(tp, 0x1f, 0x0007);
3679 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003680 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003681 rtl_writephy(tp, 0x1f, 0x0002);
3682 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003683 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003684
3685 /* improve 10M EEE waveform */
3686 rtl_writephy(tp, 0x1f, 0x0005);
3687 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003688 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003689 rtl_writephy(tp, 0x1f, 0x0000);
3690
3691 /* Improve 2-pair detection performance */
3692 rtl_writephy(tp, 0x1f, 0x0005);
3693 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003694 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003695 rtl_writephy(tp, 0x1f, 0x0000);
3696
3697 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003698 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003699 rtl_writephy(tp, 0x1f, 0x0005);
3700 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003701 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003702 rtl_writephy(tp, 0x1f, 0x0004);
3703 rtl_writephy(tp, 0x1f, 0x0007);
3704 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003705 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003706 rtl_writephy(tp, 0x1f, 0x0002);
3707 rtl_writephy(tp, 0x1f, 0x0000);
3708 rtl_writephy(tp, 0x0d, 0x0007);
3709 rtl_writephy(tp, 0x0e, 0x003c);
3710 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003711 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003712 rtl_writephy(tp, 0x0d, 0x0000);
3713
3714 /* Green feature */
3715 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003716 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3717 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003718 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003719 rtl_writephy(tp, 0x1f, 0x0005);
3720 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3721 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003722
françois romieu9ecb9aa2012-12-07 11:20:21 +00003723 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3724 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003725}
3726
Hayes Wang5f886e02012-03-30 14:33:03 +08003727static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3728{
3729 /* For 4-corner performance improve */
3730 rtl_writephy(tp, 0x1f, 0x0005);
3731 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003732 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003733 rtl_writephy(tp, 0x1f, 0x0000);
3734
3735 /* PHY auto speed down */
3736 rtl_writephy(tp, 0x1f, 0x0007);
3737 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003738 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003739 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003740 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003741
3742 /* Improve 10M EEE waveform */
3743 rtl_writephy(tp, 0x1f, 0x0005);
3744 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003745 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003746 rtl_writephy(tp, 0x1f, 0x0000);
3747}
3748
Hayes Wangc2218922011-09-06 16:55:18 +08003749static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3750{
3751 static const struct phy_reg phy_reg_init[] = {
3752 /* Channel estimation fine tune */
3753 { 0x1f, 0x0003 },
3754 { 0x09, 0xa20f },
3755 { 0x1f, 0x0000 },
3756
3757 /* Modify green table for giga & fnet */
3758 { 0x1f, 0x0005 },
3759 { 0x05, 0x8b55 },
3760 { 0x06, 0x0000 },
3761 { 0x05, 0x8b5e },
3762 { 0x06, 0x0000 },
3763 { 0x05, 0x8b67 },
3764 { 0x06, 0x0000 },
3765 { 0x05, 0x8b70 },
3766 { 0x06, 0x0000 },
3767 { 0x1f, 0x0000 },
3768 { 0x1f, 0x0007 },
3769 { 0x1e, 0x0078 },
3770 { 0x17, 0x0000 },
3771 { 0x19, 0x00fb },
3772 { 0x1f, 0x0000 },
3773
3774 /* Modify green table for 10M */
3775 { 0x1f, 0x0005 },
3776 { 0x05, 0x8b79 },
3777 { 0x06, 0xaa00 },
3778 { 0x1f, 0x0000 },
3779
3780 /* Disable hiimpedance detection (RTCT) */
3781 { 0x1f, 0x0003 },
3782 { 0x01, 0x328a },
3783 { 0x1f, 0x0000 }
3784 };
3785
3786 rtl_apply_firmware(tp);
3787
3788 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3789
Hayes Wang5f886e02012-03-30 14:33:03 +08003790 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003791
3792 /* Improve 2-pair detection performance */
3793 rtl_writephy(tp, 0x1f, 0x0005);
3794 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003795 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003796 rtl_writephy(tp, 0x1f, 0x0000);
3797}
3798
3799static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3800{
3801 rtl_apply_firmware(tp);
3802
Hayes Wang5f886e02012-03-30 14:33:03 +08003803 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003804}
3805
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003806static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3807{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003808 static const struct phy_reg phy_reg_init[] = {
3809 /* Channel estimation fine tune */
3810 { 0x1f, 0x0003 },
3811 { 0x09, 0xa20f },
3812 { 0x1f, 0x0000 },
3813
3814 /* Modify green table for giga & fnet */
3815 { 0x1f, 0x0005 },
3816 { 0x05, 0x8b55 },
3817 { 0x06, 0x0000 },
3818 { 0x05, 0x8b5e },
3819 { 0x06, 0x0000 },
3820 { 0x05, 0x8b67 },
3821 { 0x06, 0x0000 },
3822 { 0x05, 0x8b70 },
3823 { 0x06, 0x0000 },
3824 { 0x1f, 0x0000 },
3825 { 0x1f, 0x0007 },
3826 { 0x1e, 0x0078 },
3827 { 0x17, 0x0000 },
3828 { 0x19, 0x00aa },
3829 { 0x1f, 0x0000 },
3830
3831 /* Modify green table for 10M */
3832 { 0x1f, 0x0005 },
3833 { 0x05, 0x8b79 },
3834 { 0x06, 0xaa00 },
3835 { 0x1f, 0x0000 },
3836
3837 /* Disable hiimpedance detection (RTCT) */
3838 { 0x1f, 0x0003 },
3839 { 0x01, 0x328a },
3840 { 0x1f, 0x0000 }
3841 };
3842
3843
3844 rtl_apply_firmware(tp);
3845
3846 rtl8168f_hw_phy_config(tp);
3847
3848 /* Improve 2-pair detection performance */
3849 rtl_writephy(tp, 0x1f, 0x0005);
3850 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003851 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003852 rtl_writephy(tp, 0x1f, 0x0000);
3853
3854 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3855
3856 /* Modify green table for giga */
3857 rtl_writephy(tp, 0x1f, 0x0005);
3858 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003859 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003860 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003861 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003862 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003863 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003864 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003865 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003866 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003867 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003868 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003869 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003870 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003871 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003872 rtl_writephy(tp, 0x1f, 0x0000);
3873
3874 /* uc same-seed solution */
3875 rtl_writephy(tp, 0x1f, 0x0005);
3876 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003877 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003878 rtl_writephy(tp, 0x1f, 0x0000);
3879
3880 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003881 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003882 rtl_writephy(tp, 0x1f, 0x0005);
3883 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003884 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003885 rtl_writephy(tp, 0x1f, 0x0004);
3886 rtl_writephy(tp, 0x1f, 0x0007);
3887 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003888 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003889 rtl_writephy(tp, 0x1f, 0x0000);
3890 rtl_writephy(tp, 0x0d, 0x0007);
3891 rtl_writephy(tp, 0x0e, 0x003c);
3892 rtl_writephy(tp, 0x0d, 0x4007);
3893 rtl_writephy(tp, 0x0e, 0x0000);
3894 rtl_writephy(tp, 0x0d, 0x0000);
3895
3896 /* Green feature */
3897 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003898 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3899 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003900 rtl_writephy(tp, 0x1f, 0x0000);
3901}
3902
Hayes Wangc5583862012-07-02 17:23:22 +08003903static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3904{
Hayes Wangc5583862012-07-02 17:23:22 +08003905 rtl_apply_firmware(tp);
3906
hayeswang41f44d12013-04-01 22:23:36 +00003907 rtl_writephy(tp, 0x1f, 0x0a46);
3908 if (rtl_readphy(tp, 0x10) & 0x0100) {
3909 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003910 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003911 } else {
3912 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003913 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003914 }
Hayes Wangc5583862012-07-02 17:23:22 +08003915
hayeswang41f44d12013-04-01 22:23:36 +00003916 rtl_writephy(tp, 0x1f, 0x0a46);
3917 if (rtl_readphy(tp, 0x13) & 0x0100) {
3918 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003919 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003920 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003921 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003922 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003923 }
Hayes Wangc5583862012-07-02 17:23:22 +08003924
hayeswang41f44d12013-04-01 22:23:36 +00003925 /* Enable PHY auto speed down */
3926 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003927 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003928
hayeswangfe7524c2013-04-01 22:23:37 +00003929 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003930 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003931 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003932 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003933 rtl_writephy(tp, 0x1f, 0x0a43);
3934 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003935 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3936 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003937
hayeswang41f44d12013-04-01 22:23:36 +00003938 /* EEE auto-fallback function */
3939 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003940 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003941
hayeswang41f44d12013-04-01 22:23:36 +00003942 /* Enable UC LPF tune function */
3943 rtl_writephy(tp, 0x1f, 0x0a43);
3944 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003945 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003946
3947 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003948 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003949
hayeswangfe7524c2013-04-01 22:23:37 +00003950 /* Improve SWR Efficiency */
3951 rtl_writephy(tp, 0x1f, 0x0bcd);
3952 rtl_writephy(tp, 0x14, 0x5065);
3953 rtl_writephy(tp, 0x14, 0xd065);
3954 rtl_writephy(tp, 0x1f, 0x0bc8);
3955 rtl_writephy(tp, 0x11, 0x5655);
3956 rtl_writephy(tp, 0x1f, 0x0bcd);
3957 rtl_writephy(tp, 0x14, 0x1065);
3958 rtl_writephy(tp, 0x14, 0x9065);
3959 rtl_writephy(tp, 0x14, 0x1065);
3960
David Chang1bac1072013-11-27 15:48:36 +08003961 /* Check ALDPS bit, disable it if enabled */
3962 rtl_writephy(tp, 0x1f, 0x0a43);
3963 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003964 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08003965
hayeswang41f44d12013-04-01 22:23:36 +00003966 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003967}
3968
hayeswang57538c42013-04-01 22:23:40 +00003969static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3970{
3971 rtl_apply_firmware(tp);
3972}
3973
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003974static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3975{
3976 u16 dout_tapbin;
3977 u32 data;
3978
3979 rtl_apply_firmware(tp);
3980
3981 /* CHN EST parameters adjust - giga master */
3982 rtl_writephy(tp, 0x1f, 0x0a43);
3983 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003984 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003985 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003986 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003987 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003988 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003989 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003990 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003991 rtl_writephy(tp, 0x1f, 0x0000);
3992
3993 /* CHN EST parameters adjust - giga slave */
3994 rtl_writephy(tp, 0x1f, 0x0a43);
3995 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003996 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003997 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003998 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003999 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004000 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004001 rtl_writephy(tp, 0x1f, 0x0000);
4002
4003 /* CHN EST parameters adjust - fnet */
4004 rtl_writephy(tp, 0x1f, 0x0a43);
4005 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004006 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004007 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004008 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004009 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004010 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004011 rtl_writephy(tp, 0x1f, 0x0000);
4012
4013 /* enable R-tune & PGA-retune function */
4014 dout_tapbin = 0;
4015 rtl_writephy(tp, 0x1f, 0x0a46);
4016 data = rtl_readphy(tp, 0x13);
4017 data &= 3;
4018 data <<= 2;
4019 dout_tapbin |= data;
4020 data = rtl_readphy(tp, 0x12);
4021 data &= 0xc000;
4022 data >>= 14;
4023 dout_tapbin |= data;
4024 dout_tapbin = ~(dout_tapbin^0x08);
4025 dout_tapbin <<= 12;
4026 dout_tapbin &= 0xf000;
4027 rtl_writephy(tp, 0x1f, 0x0a43);
4028 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004029 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004030 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004031 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004032 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004033 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004034 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004035 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004036
4037 rtl_writephy(tp, 0x1f, 0x0a43);
4038 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004039 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004040 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004041 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004042 rtl_writephy(tp, 0x1f, 0x0000);
4043
4044 /* enable GPHY 10M */
4045 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004046 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004047 rtl_writephy(tp, 0x1f, 0x0000);
4048
4049 /* SAR ADC performance */
4050 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004051 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004052 rtl_writephy(tp, 0x1f, 0x0000);
4053
4054 rtl_writephy(tp, 0x1f, 0x0a43);
4055 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004056 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004057 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004058 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004059 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004060 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004061 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004062 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004063 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004064 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004065 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004066 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004067 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004068 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004069 rtl_writephy(tp, 0x1f, 0x0000);
4070
4071 /* disable phy pfm mode */
4072 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004073 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004074 rtl_writephy(tp, 0x1f, 0x0000);
4075
4076 /* Check ALDPS bit, disable it if enabled */
4077 rtl_writephy(tp, 0x1f, 0x0a43);
4078 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004079 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004080
4081 rtl_writephy(tp, 0x1f, 0x0000);
4082}
4083
4084static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
4085{
4086 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
4087 u16 rlen;
4088 u32 data;
4089
4090 rtl_apply_firmware(tp);
4091
4092 /* CHIN EST parameter update */
4093 rtl_writephy(tp, 0x1f, 0x0a43);
4094 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004095 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004096 rtl_writephy(tp, 0x1f, 0x0000);
4097
4098 /* enable R-tune & PGA-retune function */
4099 rtl_writephy(tp, 0x1f, 0x0a43);
4100 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004101 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004102 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004103 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004104 rtl_writephy(tp, 0x1f, 0x0000);
4105
4106 /* enable GPHY 10M */
4107 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004108 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004109 rtl_writephy(tp, 0x1f, 0x0000);
4110
4111 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
4112 data = r8168_mac_ocp_read(tp, 0xdd02);
4113 ioffset_p3 = ((data & 0x80)>>7);
4114 ioffset_p3 <<= 3;
4115
4116 data = r8168_mac_ocp_read(tp, 0xdd00);
4117 ioffset_p3 |= ((data & (0xe000))>>13);
4118 ioffset_p2 = ((data & (0x1e00))>>9);
4119 ioffset_p1 = ((data & (0x01e0))>>5);
4120 ioffset_p0 = ((data & 0x0010)>>4);
4121 ioffset_p0 <<= 3;
4122 ioffset_p0 |= (data & (0x07));
4123 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
4124
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004125 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08004126 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004127 rtl_writephy(tp, 0x1f, 0x0bcf);
4128 rtl_writephy(tp, 0x16, data);
4129 rtl_writephy(tp, 0x1f, 0x0000);
4130 }
4131
4132 /* Modify rlen (TX LPF corner frequency) level */
4133 rtl_writephy(tp, 0x1f, 0x0bcd);
4134 data = rtl_readphy(tp, 0x16);
4135 data &= 0x000f;
4136 rlen = 0;
4137 if (data > 3)
4138 rlen = data - 3;
4139 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
4140 rtl_writephy(tp, 0x17, data);
4141 rtl_writephy(tp, 0x1f, 0x0bcd);
4142 rtl_writephy(tp, 0x1f, 0x0000);
4143
4144 /* disable phy pfm mode */
4145 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004146 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004147 rtl_writephy(tp, 0x1f, 0x0000);
4148
4149 /* Check ALDPS bit, disable it if enabled */
4150 rtl_writephy(tp, 0x1f, 0x0a43);
4151 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004152 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004153
4154 rtl_writephy(tp, 0x1f, 0x0000);
4155}
4156
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004157static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4158{
4159 /* Enable PHY auto speed down */
4160 rtl_writephy(tp, 0x1f, 0x0a44);
4161 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4162 rtl_writephy(tp, 0x1f, 0x0000);
4163
4164 /* patch 10M & ALDPS */
4165 rtl_writephy(tp, 0x1f, 0x0bcc);
4166 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4167 rtl_writephy(tp, 0x1f, 0x0a44);
4168 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4169 rtl_writephy(tp, 0x1f, 0x0a43);
4170 rtl_writephy(tp, 0x13, 0x8084);
4171 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4172 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4173 rtl_writephy(tp, 0x1f, 0x0000);
4174
4175 /* Enable EEE auto-fallback function */
4176 rtl_writephy(tp, 0x1f, 0x0a4b);
4177 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4178 rtl_writephy(tp, 0x1f, 0x0000);
4179
4180 /* Enable UC LPF tune function */
4181 rtl_writephy(tp, 0x1f, 0x0a43);
4182 rtl_writephy(tp, 0x13, 0x8012);
4183 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4184 rtl_writephy(tp, 0x1f, 0x0000);
4185
4186 /* set rg_sel_sdm_rate */
4187 rtl_writephy(tp, 0x1f, 0x0c42);
4188 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4189 rtl_writephy(tp, 0x1f, 0x0000);
4190
4191 /* Check ALDPS bit, disable it if enabled */
4192 rtl_writephy(tp, 0x1f, 0x0a43);
4193 if (rtl_readphy(tp, 0x10) & 0x0004)
4194 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4195
4196 rtl_writephy(tp, 0x1f, 0x0000);
4197}
4198
4199static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4200{
4201 /* patch 10M & ALDPS */
4202 rtl_writephy(tp, 0x1f, 0x0bcc);
4203 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4204 rtl_writephy(tp, 0x1f, 0x0a44);
4205 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4206 rtl_writephy(tp, 0x1f, 0x0a43);
4207 rtl_writephy(tp, 0x13, 0x8084);
4208 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4209 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4210 rtl_writephy(tp, 0x1f, 0x0000);
4211
4212 /* Enable UC LPF tune function */
4213 rtl_writephy(tp, 0x1f, 0x0a43);
4214 rtl_writephy(tp, 0x13, 0x8012);
4215 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4216 rtl_writephy(tp, 0x1f, 0x0000);
4217
4218 /* Set rg_sel_sdm_rate */
4219 rtl_writephy(tp, 0x1f, 0x0c42);
4220 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4221 rtl_writephy(tp, 0x1f, 0x0000);
4222
4223 /* Channel estimation parameters */
4224 rtl_writephy(tp, 0x1f, 0x0a43);
4225 rtl_writephy(tp, 0x13, 0x80f3);
4226 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4227 rtl_writephy(tp, 0x13, 0x80f0);
4228 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4229 rtl_writephy(tp, 0x13, 0x80ef);
4230 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4231 rtl_writephy(tp, 0x13, 0x80f6);
4232 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4233 rtl_writephy(tp, 0x13, 0x80ec);
4234 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4235 rtl_writephy(tp, 0x13, 0x80ed);
4236 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4237 rtl_writephy(tp, 0x13, 0x80f2);
4238 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4239 rtl_writephy(tp, 0x13, 0x80f4);
4240 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4241 rtl_writephy(tp, 0x1f, 0x0a43);
4242 rtl_writephy(tp, 0x13, 0x8110);
4243 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4244 rtl_writephy(tp, 0x13, 0x810f);
4245 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4246 rtl_writephy(tp, 0x13, 0x8111);
4247 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4248 rtl_writephy(tp, 0x13, 0x8113);
4249 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4250 rtl_writephy(tp, 0x13, 0x8115);
4251 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4252 rtl_writephy(tp, 0x13, 0x810e);
4253 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4254 rtl_writephy(tp, 0x13, 0x810c);
4255 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4256 rtl_writephy(tp, 0x13, 0x810b);
4257 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4258 rtl_writephy(tp, 0x1f, 0x0a43);
4259 rtl_writephy(tp, 0x13, 0x80d1);
4260 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4261 rtl_writephy(tp, 0x13, 0x80cd);
4262 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4263 rtl_writephy(tp, 0x13, 0x80d3);
4264 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4265 rtl_writephy(tp, 0x13, 0x80d5);
4266 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4267 rtl_writephy(tp, 0x13, 0x80d7);
4268 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4269
4270 /* Force PWM-mode */
4271 rtl_writephy(tp, 0x1f, 0x0bcd);
4272 rtl_writephy(tp, 0x14, 0x5065);
4273 rtl_writephy(tp, 0x14, 0xd065);
4274 rtl_writephy(tp, 0x1f, 0x0bc8);
4275 rtl_writephy(tp, 0x12, 0x00ed);
4276 rtl_writephy(tp, 0x1f, 0x0bcd);
4277 rtl_writephy(tp, 0x14, 0x1065);
4278 rtl_writephy(tp, 0x14, 0x9065);
4279 rtl_writephy(tp, 0x14, 0x1065);
4280 rtl_writephy(tp, 0x1f, 0x0000);
4281
4282 /* Check ALDPS bit, disable it if enabled */
4283 rtl_writephy(tp, 0x1f, 0x0a43);
4284 if (rtl_readphy(tp, 0x10) & 0x0004)
4285 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4286
4287 rtl_writephy(tp, 0x1f, 0x0000);
4288}
4289
françois romieu4da19632011-01-03 15:07:55 +00004290static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004291{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004292 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004293 { 0x1f, 0x0003 },
4294 { 0x08, 0x441d },
4295 { 0x01, 0x9100 },
4296 { 0x1f, 0x0000 }
4297 };
4298
françois romieu4da19632011-01-03 15:07:55 +00004299 rtl_writephy(tp, 0x1f, 0x0000);
4300 rtl_patchphy(tp, 0x11, 1 << 12);
4301 rtl_patchphy(tp, 0x19, 1 << 13);
4302 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004303
françois romieu4da19632011-01-03 15:07:55 +00004304 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004305}
4306
Hayes Wang5a5e4442011-02-22 17:26:21 +08004307static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4308{
4309 static const struct phy_reg phy_reg_init[] = {
4310 { 0x1f, 0x0005 },
4311 { 0x1a, 0x0000 },
4312 { 0x1f, 0x0000 },
4313
4314 { 0x1f, 0x0004 },
4315 { 0x1c, 0x0000 },
4316 { 0x1f, 0x0000 },
4317
4318 { 0x1f, 0x0001 },
4319 { 0x15, 0x7701 },
4320 { 0x1f, 0x0000 }
4321 };
4322
4323 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004324 rtl_writephy(tp, 0x1f, 0x0000);
4325 rtl_writephy(tp, 0x18, 0x0310);
4326 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004327
François Romieu953a12c2011-04-24 17:38:48 +02004328 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004329
4330 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4331}
4332
Hayes Wang7e18dca2012-03-30 14:33:02 +08004333static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4334{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004335 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004336 rtl_writephy(tp, 0x1f, 0x0000);
4337 rtl_writephy(tp, 0x18, 0x0310);
4338 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004339
4340 rtl_apply_firmware(tp);
4341
4342 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004343 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004344 rtl_writephy(tp, 0x1f, 0x0004);
4345 rtl_writephy(tp, 0x10, 0x401f);
4346 rtl_writephy(tp, 0x19, 0x7030);
4347 rtl_writephy(tp, 0x1f, 0x0000);
4348}
4349
Hayes Wang5598bfe2012-07-02 17:23:21 +08004350static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4351{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004352 static const struct phy_reg phy_reg_init[] = {
4353 { 0x1f, 0x0004 },
4354 { 0x10, 0xc07f },
4355 { 0x19, 0x7030 },
4356 { 0x1f, 0x0000 }
4357 };
4358
4359 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004360 rtl_writephy(tp, 0x1f, 0x0000);
4361 rtl_writephy(tp, 0x18, 0x0310);
4362 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004363
4364 rtl_apply_firmware(tp);
4365
Francois Romieufdf6fc02012-07-06 22:40:38 +02004366 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004367 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4368
Francois Romieufdf6fc02012-07-06 22:40:38 +02004369 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004370}
4371
Francois Romieu5615d9f2007-08-17 17:50:46 +02004372static void rtl_hw_phy_config(struct net_device *dev)
4373{
4374 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004375
4376 rtl8169_print_mac_version(tp);
4377
4378 switch (tp->mac_version) {
4379 case RTL_GIGA_MAC_VER_01:
4380 break;
4381 case RTL_GIGA_MAC_VER_02:
4382 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004383 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004384 break;
4385 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004386 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004387 break;
françois romieu2e9558562009-08-10 19:44:19 +00004388 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004389 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004390 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004391 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004392 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004393 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004394 case RTL_GIGA_MAC_VER_07:
4395 case RTL_GIGA_MAC_VER_08:
4396 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004397 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004398 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004399 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004400 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004401 break;
4402 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004403 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004404 break;
4405 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004406 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004407 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004408 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004409 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004410 break;
4411 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004412 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004413 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004414 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004415 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004416 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004417 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004418 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004419 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004420 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004421 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004422 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004423 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004424 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004425 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004426 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004427 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004428 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004429 break;
4430 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004431 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004432 break;
4433 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004434 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004435 break;
françois romieue6de30d2011-01-03 15:08:37 +00004436 case RTL_GIGA_MAC_VER_28:
4437 rtl8168d_4_hw_phy_config(tp);
4438 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004439 case RTL_GIGA_MAC_VER_29:
4440 case RTL_GIGA_MAC_VER_30:
4441 rtl8105e_hw_phy_config(tp);
4442 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004443 case RTL_GIGA_MAC_VER_31:
4444 /* None. */
4445 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004446 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004447 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004448 rtl8168e_1_hw_phy_config(tp);
4449 break;
4450 case RTL_GIGA_MAC_VER_34:
4451 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004452 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004453 case RTL_GIGA_MAC_VER_35:
4454 rtl8168f_1_hw_phy_config(tp);
4455 break;
4456 case RTL_GIGA_MAC_VER_36:
4457 rtl8168f_2_hw_phy_config(tp);
4458 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004459
Hayes Wang7e18dca2012-03-30 14:33:02 +08004460 case RTL_GIGA_MAC_VER_37:
4461 rtl8402_hw_phy_config(tp);
4462 break;
4463
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004464 case RTL_GIGA_MAC_VER_38:
4465 rtl8411_hw_phy_config(tp);
4466 break;
4467
Hayes Wang5598bfe2012-07-02 17:23:21 +08004468 case RTL_GIGA_MAC_VER_39:
4469 rtl8106e_hw_phy_config(tp);
4470 break;
4471
Hayes Wangc5583862012-07-02 17:23:22 +08004472 case RTL_GIGA_MAC_VER_40:
4473 rtl8168g_1_hw_phy_config(tp);
4474 break;
hayeswang57538c42013-04-01 22:23:40 +00004475 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004476 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004477 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004478 rtl8168g_2_hw_phy_config(tp);
4479 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004480 case RTL_GIGA_MAC_VER_45:
4481 case RTL_GIGA_MAC_VER_47:
4482 rtl8168h_1_hw_phy_config(tp);
4483 break;
4484 case RTL_GIGA_MAC_VER_46:
4485 case RTL_GIGA_MAC_VER_48:
4486 rtl8168h_2_hw_phy_config(tp);
4487 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004488
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004489 case RTL_GIGA_MAC_VER_49:
4490 rtl8168ep_1_hw_phy_config(tp);
4491 break;
4492 case RTL_GIGA_MAC_VER_50:
4493 case RTL_GIGA_MAC_VER_51:
4494 rtl8168ep_2_hw_phy_config(tp);
4495 break;
4496
Hayes Wangc5583862012-07-02 17:23:22 +08004497 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004498 default:
4499 break;
4500 }
4501}
4502
Francois Romieuda78dbf2012-01-26 14:18:23 +01004503static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 struct timer_list *timer = &tp->timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4507
Francois Romieubcf0bf92006-07-26 23:14:13 +02004508 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
françois romieu4da19632011-01-03 15:07:55 +00004510 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02004511 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 * A busy loop could burn quite a few cycles on nowadays CPU.
4513 * Let's delay the execution of the timer for a few ticks.
4514 */
4515 timeout = HZ/10;
4516 goto out_mod_timer;
4517 }
4518
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004519 if (tp->link_ok(tp))
Francois Romieuda78dbf2012-01-26 14:18:23 +01004520 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02004522 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
françois romieu4da19632011-01-03 15:07:55 +00004524 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
4526out_mod_timer:
4527 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004528}
4529
4530static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4531{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004532 if (!test_and_set_bit(flag, tp->wk.flags))
4533 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004534}
4535
Kees Cook9de36cc2017-10-25 03:53:12 -07004536static void rtl8169_phy_timer(struct timer_list *t)
Francois Romieuda78dbf2012-01-26 14:18:23 +01004537{
Kees Cook9de36cc2017-10-25 03:53:12 -07004538 struct rtl8169_private *tp = from_timer(tp, t, timer);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004539
Francois Romieu98ddf982012-01-31 10:47:34 +01004540 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541}
4542
Francois Romieuffc46952012-07-06 14:19:23 +02004543DECLARE_RTL_COND(rtl_phy_reset_cond)
4544{
4545 return tp->phy_reset_pending(tp);
4546}
4547
Francois Romieubf793292006-11-01 00:53:05 +01004548static void rtl8169_phy_reset(struct net_device *dev,
4549 struct rtl8169_private *tp)
4550{
françois romieu4da19632011-01-03 15:07:55 +00004551 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004552 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004553}
4554
David S. Miller8decf862011-09-22 03:23:13 -04004555static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4556{
David S. Miller8decf862011-09-22 03:23:13 -04004557 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004558 (RTL_R8(tp, PHYstatus) & TBI_Enable);
David S. Miller8decf862011-09-22 03:23:13 -04004559}
4560
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004561static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562{
Francois Romieu5615d9f2007-08-17 17:50:46 +02004563 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004564
Marcus Sundberg773328942008-07-10 21:28:08 +02004565 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4566 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004567 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02004568 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004569
Francois Romieu6dccd162007-02-13 23:38:05 +01004570 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4571
4572 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4573 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004574
Francois Romieubcf0bf92006-07-26 23:14:13 +02004575 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004576 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004577 RTL_W8(tp, 0x82, 0x01);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004578 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004579 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004580 }
4581
Francois Romieubf793292006-11-01 00:53:05 +01004582 rtl8169_phy_reset(dev, tp);
4583
Oliver Neukum54405cd2011-01-06 21:55:13 +01004584 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004585 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4586 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4587 (tp->mii.supports_gmii ?
4588 ADVERTISED_1000baseT_Half |
4589 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004590
David S. Miller8decf862011-09-22 03:23:13 -04004591 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00004592 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004593}
4594
Francois Romieu773d2022007-01-31 23:47:43 +01004595static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4596{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004597 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004598
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004599 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00004600
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004601 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4602 RTL_R32(tp, MAC4);
françois romieu908ba2b2010-04-26 11:42:58 +00004603
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004604 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4605 RTL_R32(tp, MAC0);
françois romieu908ba2b2010-04-26 11:42:58 +00004606
françois romieu9ecb9aa2012-12-07 11:20:21 +00004607 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4608 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004609
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004610 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieu773d2022007-01-31 23:47:43 +01004611
Francois Romieuda78dbf2012-01-26 14:18:23 +01004612 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004613}
4614
4615static int rtl_set_mac_address(struct net_device *dev, void *p)
4616{
4617 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004618 struct device *d = &tp->pci_dev->dev;
Francois Romieu773d2022007-01-31 23:47:43 +01004619 struct sockaddr *addr = p;
4620
4621 if (!is_valid_ether_addr(addr->sa_data))
4622 return -EADDRNOTAVAIL;
4623
4624 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
4625
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004626 pm_runtime_get_noresume(d);
4627
4628 if (pm_runtime_active(d))
4629 rtl_rar_set(tp, dev->dev_addr);
4630
4631 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004632
4633 return 0;
4634}
4635
Francois Romieu5f787a12006-08-17 13:02:36 +02004636static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4637{
4638 struct rtl8169_private *tp = netdev_priv(dev);
4639 struct mii_ioctl_data *data = if_mii(ifr);
4640
Francois Romieu8b4ab282008-11-19 22:05:25 -08004641 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4642}
Francois Romieu5f787a12006-08-17 13:02:36 +02004643
Francois Romieucecb5fd2011-04-01 10:21:07 +02004644static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4645 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004646{
Francois Romieu5f787a12006-08-17 13:02:36 +02004647 switch (cmd) {
4648 case SIOCGMIIPHY:
4649 data->phy_id = 32; /* Internal PHY */
4650 return 0;
4651
4652 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004653 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004654 return 0;
4655
4656 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004657 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004658 return 0;
4659 }
4660 return -EOPNOTSUPP;
4661}
4662
Francois Romieu8b4ab282008-11-19 22:05:25 -08004663static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4664{
4665 return -EOPNOTSUPP;
4666}
4667
Bill Pembertonbaf63292012-12-03 09:23:28 -05004668static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004669{
4670 struct mdio_ops *ops = &tp->mdio_ops;
4671
4672 switch (tp->mac_version) {
4673 case RTL_GIGA_MAC_VER_27:
4674 ops->write = r8168dp_1_mdio_write;
4675 ops->read = r8168dp_1_mdio_read;
4676 break;
françois romieue6de30d2011-01-03 15:08:37 +00004677 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004678 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004679 ops->write = r8168dp_2_mdio_write;
4680 ops->read = r8168dp_2_mdio_read;
4681 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004682 case RTL_GIGA_MAC_VER_40:
4683 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004684 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004685 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004686 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004687 case RTL_GIGA_MAC_VER_45:
4688 case RTL_GIGA_MAC_VER_46:
4689 case RTL_GIGA_MAC_VER_47:
4690 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004691 case RTL_GIGA_MAC_VER_49:
4692 case RTL_GIGA_MAC_VER_50:
4693 case RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004694 ops->write = r8168g_mdio_write;
4695 ops->read = r8168g_mdio_read;
4696 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004697 default:
4698 ops->write = r8169_mdio_write;
4699 ops->read = r8169_mdio_read;
4700 break;
4701 }
4702}
4703
hayeswange2409d82013-03-31 17:02:04 +00004704static void rtl_speed_down(struct rtl8169_private *tp)
4705{
4706 u32 adv;
4707 int lpa;
4708
4709 rtl_writephy(tp, 0x1f, 0x0000);
4710 lpa = rtl_readphy(tp, MII_LPA);
4711
4712 if (lpa & (LPA_10HALF | LPA_10FULL))
4713 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4714 else if (lpa & (LPA_100HALF | LPA_100FULL))
4715 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4716 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4717 else
4718 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4719 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4720 (tp->mii.supports_gmii ?
4721 ADVERTISED_1000baseT_Half |
4722 ADVERTISED_1000baseT_Full : 0);
4723
4724 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4725 adv);
4726}
4727
David S. Miller1805b2f2011-10-24 18:18:09 -04004728static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4729{
David S. Miller1805b2f2011-10-24 18:18:09 -04004730 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004731 case RTL_GIGA_MAC_VER_25:
4732 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004733 case RTL_GIGA_MAC_VER_29:
4734 case RTL_GIGA_MAC_VER_30:
4735 case RTL_GIGA_MAC_VER_32:
4736 case RTL_GIGA_MAC_VER_33:
4737 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004738 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004739 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004740 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08004741 case RTL_GIGA_MAC_VER_40:
4742 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004743 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004744 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004745 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004746 case RTL_GIGA_MAC_VER_45:
4747 case RTL_GIGA_MAC_VER_46:
4748 case RTL_GIGA_MAC_VER_47:
4749 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004750 case RTL_GIGA_MAC_VER_49:
4751 case RTL_GIGA_MAC_VER_50:
4752 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004753 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04004754 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4755 break;
4756 default:
4757 break;
4758 }
4759}
4760
4761static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4762{
4763 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4764 return false;
4765
hayeswange2409d82013-03-31 17:02:04 +00004766 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004767 rtl_wol_suspend_quirk(tp);
4768
4769 return true;
4770}
4771
françois romieu065c27c2011-01-03 15:08:12 +00004772static void r810x_phy_power_down(struct rtl8169_private *tp)
4773{
4774 rtl_writephy(tp, 0x1f, 0x0000);
4775 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4776}
4777
4778static void r810x_phy_power_up(struct rtl8169_private *tp)
4779{
4780 rtl_writephy(tp, 0x1f, 0x0000);
4781 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4782}
4783
4784static void r810x_pll_power_down(struct rtl8169_private *tp)
4785{
David S. Miller1805b2f2011-10-24 18:18:09 -04004786 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004787 return;
françois romieu065c27c2011-01-03 15:08:12 +00004788
4789 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004790
4791 switch (tp->mac_version) {
4792 case RTL_GIGA_MAC_VER_07:
4793 case RTL_GIGA_MAC_VER_08:
4794 case RTL_GIGA_MAC_VER_09:
4795 case RTL_GIGA_MAC_VER_10:
4796 case RTL_GIGA_MAC_VER_13:
4797 case RTL_GIGA_MAC_VER_16:
4798 break;
4799 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004800 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
Hayes Wang00042992012-03-30 14:33:00 +08004801 break;
4802 }
françois romieu065c27c2011-01-03 15:08:12 +00004803}
4804
4805static void r810x_pll_power_up(struct rtl8169_private *tp)
4806{
4807 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004808
4809 switch (tp->mac_version) {
4810 case RTL_GIGA_MAC_VER_07:
4811 case RTL_GIGA_MAC_VER_08:
4812 case RTL_GIGA_MAC_VER_09:
4813 case RTL_GIGA_MAC_VER_10:
4814 case RTL_GIGA_MAC_VER_13:
4815 case RTL_GIGA_MAC_VER_16:
4816 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004817 case RTL_GIGA_MAC_VER_47:
4818 case RTL_GIGA_MAC_VER_48:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004819 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004820 break;
Hayes Wang00042992012-03-30 14:33:00 +08004821 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004822 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
Hayes Wang00042992012-03-30 14:33:00 +08004823 break;
4824 }
françois romieu065c27c2011-01-03 15:08:12 +00004825}
4826
4827static void r8168_phy_power_up(struct rtl8169_private *tp)
4828{
4829 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004830 switch (tp->mac_version) {
4831 case RTL_GIGA_MAC_VER_11:
4832 case RTL_GIGA_MAC_VER_12:
4833 case RTL_GIGA_MAC_VER_17:
4834 case RTL_GIGA_MAC_VER_18:
4835 case RTL_GIGA_MAC_VER_19:
4836 case RTL_GIGA_MAC_VER_20:
4837 case RTL_GIGA_MAC_VER_21:
4838 case RTL_GIGA_MAC_VER_22:
4839 case RTL_GIGA_MAC_VER_23:
4840 case RTL_GIGA_MAC_VER_24:
4841 case RTL_GIGA_MAC_VER_25:
4842 case RTL_GIGA_MAC_VER_26:
4843 case RTL_GIGA_MAC_VER_27:
4844 case RTL_GIGA_MAC_VER_28:
4845 case RTL_GIGA_MAC_VER_31:
4846 rtl_writephy(tp, 0x0e, 0x0000);
4847 break;
4848 default:
4849 break;
4850 }
françois romieu065c27c2011-01-03 15:08:12 +00004851 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4852}
4853
4854static void r8168_phy_power_down(struct rtl8169_private *tp)
4855{
4856 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004857 switch (tp->mac_version) {
4858 case RTL_GIGA_MAC_VER_32:
4859 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004860 case RTL_GIGA_MAC_VER_40:
4861 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004862 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4863 break;
4864
4865 case RTL_GIGA_MAC_VER_11:
4866 case RTL_GIGA_MAC_VER_12:
4867 case RTL_GIGA_MAC_VER_17:
4868 case RTL_GIGA_MAC_VER_18:
4869 case RTL_GIGA_MAC_VER_19:
4870 case RTL_GIGA_MAC_VER_20:
4871 case RTL_GIGA_MAC_VER_21:
4872 case RTL_GIGA_MAC_VER_22:
4873 case RTL_GIGA_MAC_VER_23:
4874 case RTL_GIGA_MAC_VER_24:
4875 case RTL_GIGA_MAC_VER_25:
4876 case RTL_GIGA_MAC_VER_26:
4877 case RTL_GIGA_MAC_VER_27:
4878 case RTL_GIGA_MAC_VER_28:
4879 case RTL_GIGA_MAC_VER_31:
4880 rtl_writephy(tp, 0x0e, 0x0200);
4881 default:
4882 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4883 break;
4884 }
françois romieu065c27c2011-01-03 15:08:12 +00004885}
4886
4887static void r8168_pll_power_down(struct rtl8169_private *tp)
4888{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01004889 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004890 return;
4891
Francois Romieucecb5fd2011-04-01 10:21:07 +02004892 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4893 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004894 (RTL_R16(tp, CPlusCmd) & ASF)) {
françois romieu065c27c2011-01-03 15:08:12 +00004895 return;
4896 }
4897
hayeswang01dc7fe2011-03-21 01:50:28 +00004898 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4899 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004900 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004901
David S. Miller1805b2f2011-10-24 18:18:09 -04004902 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004903 return;
françois romieu065c27c2011-01-03 15:08:12 +00004904
4905 r8168_phy_power_down(tp);
4906
4907 switch (tp->mac_version) {
4908 case RTL_GIGA_MAC_VER_25:
4909 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004910 case RTL_GIGA_MAC_VER_27:
4911 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004912 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004913 case RTL_GIGA_MAC_VER_32:
4914 case RTL_GIGA_MAC_VER_33:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004915 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004916 case RTL_GIGA_MAC_VER_45:
4917 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004918 case RTL_GIGA_MAC_VER_50:
4919 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004920 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004921 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004922 case RTL_GIGA_MAC_VER_40:
4923 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004924 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004925 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004926 0xfc000000, ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004927 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004928 break;
françois romieu065c27c2011-01-03 15:08:12 +00004929 }
4930}
4931
4932static void r8168_pll_power_up(struct rtl8169_private *tp)
4933{
françois romieu065c27c2011-01-03 15:08:12 +00004934 switch (tp->mac_version) {
4935 case RTL_GIGA_MAC_VER_25:
4936 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004937 case RTL_GIGA_MAC_VER_27:
4938 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004939 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004940 case RTL_GIGA_MAC_VER_32:
4941 case RTL_GIGA_MAC_VER_33:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004942 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004943 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004944 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004945 case RTL_GIGA_MAC_VER_45:
4946 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004947 case RTL_GIGA_MAC_VER_50:
4948 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004949 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004950 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004951 case RTL_GIGA_MAC_VER_40:
4952 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004953 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004954 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004955 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004956 0x00000000, ERIAR_EXGMAC);
4957 break;
françois romieu065c27c2011-01-03 15:08:12 +00004958 }
4959
4960 r8168_phy_power_up(tp);
4961}
4962
Francois Romieud58d46b2011-05-03 16:38:29 +02004963static void rtl_generic_op(struct rtl8169_private *tp,
4964 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00004965{
4966 if (op)
4967 op(tp);
4968}
4969
4970static void rtl_pll_power_down(struct rtl8169_private *tp)
4971{
Francois Romieud58d46b2011-05-03 16:38:29 +02004972 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00004973}
4974
4975static void rtl_pll_power_up(struct rtl8169_private *tp)
4976{
Francois Romieud58d46b2011-05-03 16:38:29 +02004977 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00004978}
4979
Bill Pembertonbaf63292012-12-03 09:23:28 -05004980static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00004981{
4982 struct pll_power_ops *ops = &tp->pll_power_ops;
4983
4984 switch (tp->mac_version) {
4985 case RTL_GIGA_MAC_VER_07:
4986 case RTL_GIGA_MAC_VER_08:
4987 case RTL_GIGA_MAC_VER_09:
4988 case RTL_GIGA_MAC_VER_10:
4989 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08004990 case RTL_GIGA_MAC_VER_29:
4991 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004992 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004993 case RTL_GIGA_MAC_VER_39:
hayeswang58152cd2013-04-01 22:23:42 +00004994 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004995 case RTL_GIGA_MAC_VER_47:
4996 case RTL_GIGA_MAC_VER_48:
françois romieu065c27c2011-01-03 15:08:12 +00004997 ops->down = r810x_pll_power_down;
4998 ops->up = r810x_pll_power_up;
4999 break;
5000
5001 case RTL_GIGA_MAC_VER_11:
5002 case RTL_GIGA_MAC_VER_12:
5003 case RTL_GIGA_MAC_VER_17:
5004 case RTL_GIGA_MAC_VER_18:
5005 case RTL_GIGA_MAC_VER_19:
5006 case RTL_GIGA_MAC_VER_20:
5007 case RTL_GIGA_MAC_VER_21:
5008 case RTL_GIGA_MAC_VER_22:
5009 case RTL_GIGA_MAC_VER_23:
5010 case RTL_GIGA_MAC_VER_24:
5011 case RTL_GIGA_MAC_VER_25:
5012 case RTL_GIGA_MAC_VER_26:
5013 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00005014 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005015 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005016 case RTL_GIGA_MAC_VER_32:
5017 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08005018 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08005019 case RTL_GIGA_MAC_VER_35:
5020 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005021 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08005022 case RTL_GIGA_MAC_VER_40:
5023 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005024 case RTL_GIGA_MAC_VER_42:
hayeswang45dd95c2013-07-08 17:09:01 +08005025 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005026 case RTL_GIGA_MAC_VER_45:
5027 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005028 case RTL_GIGA_MAC_VER_49:
5029 case RTL_GIGA_MAC_VER_50:
5030 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00005031 ops->down = r8168_pll_power_down;
5032 ops->up = r8168_pll_power_up;
5033 break;
5034
5035 default:
5036 ops->down = NULL;
5037 ops->up = NULL;
5038 break;
5039 }
5040}
5041
Hayes Wange542a222011-07-06 15:58:04 +08005042static void rtl_init_rxcfg(struct rtl8169_private *tp)
5043{
Hayes Wange542a222011-07-06 15:58:04 +08005044 switch (tp->mac_version) {
5045 case RTL_GIGA_MAC_VER_01:
5046 case RTL_GIGA_MAC_VER_02:
5047 case RTL_GIGA_MAC_VER_03:
5048 case RTL_GIGA_MAC_VER_04:
5049 case RTL_GIGA_MAC_VER_05:
5050 case RTL_GIGA_MAC_VER_06:
5051 case RTL_GIGA_MAC_VER_10:
5052 case RTL_GIGA_MAC_VER_11:
5053 case RTL_GIGA_MAC_VER_12:
5054 case RTL_GIGA_MAC_VER_13:
5055 case RTL_GIGA_MAC_VER_14:
5056 case RTL_GIGA_MAC_VER_15:
5057 case RTL_GIGA_MAC_VER_16:
5058 case RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005059 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08005060 break;
5061 case RTL_GIGA_MAC_VER_18:
5062 case RTL_GIGA_MAC_VER_19:
5063 case RTL_GIGA_MAC_VER_20:
5064 case RTL_GIGA_MAC_VER_21:
5065 case RTL_GIGA_MAC_VER_22:
5066 case RTL_GIGA_MAC_VER_23:
5067 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00005068 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02005069 case RTL_GIGA_MAC_VER_35:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005070 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08005071 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005072 case RTL_GIGA_MAC_VER_40:
5073 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005074 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005075 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005076 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005077 case RTL_GIGA_MAC_VER_45:
5078 case RTL_GIGA_MAC_VER_46:
5079 case RTL_GIGA_MAC_VER_47:
5080 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005081 case RTL_GIGA_MAC_VER_49:
5082 case RTL_GIGA_MAC_VER_50:
5083 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005084 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00005085 break;
Hayes Wange542a222011-07-06 15:58:04 +08005086 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005087 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08005088 break;
5089 }
5090}
5091
Hayes Wang92fc43b2011-07-06 15:58:03 +08005092static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
5093{
Timo Teräs9fba0812013-01-15 21:01:24 +00005094 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005095}
5096
Francois Romieud58d46b2011-05-03 16:38:29 +02005097static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
5098{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005099 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005100 rtl_generic_op(tp, tp->jumbo_ops.enable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005101 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005102}
5103
5104static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
5105{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005106 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005107 rtl_generic_op(tp, tp->jumbo_ops.disable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005108 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005109}
5110
5111static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
5112{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005113 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
5114 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005115 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005116}
5117
5118static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
5119{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005120 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
5121 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Francois Romieud58d46b2011-05-03 16:38:29 +02005122 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5123}
5124
5125static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
5126{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005127 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02005128}
5129
5130static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
5131{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005132 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02005133}
5134
5135static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
5136{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005137 RTL_W8(tp, MaxTxPacketSize, 0x3f);
5138 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
5139 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005140 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005141}
5142
5143static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
5144{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005145 RTL_W8(tp, MaxTxPacketSize, 0x0c);
5146 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
5147 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01005148 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02005149}
5150
5151static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
5152{
5153 rtl_tx_performance_tweak(tp->pci_dev,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005154 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02005155}
5156
5157static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
5158{
5159 rtl_tx_performance_tweak(tp->pci_dev,
5160 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
5161}
5162
5163static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
5164{
Francois Romieud58d46b2011-05-03 16:38:29 +02005165 r8168b_0_hw_jumbo_enable(tp);
5166
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005167 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02005168}
5169
5170static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
5171{
Francois Romieud58d46b2011-05-03 16:38:29 +02005172 r8168b_0_hw_jumbo_disable(tp);
5173
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005174 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02005175}
5176
Bill Pembertonbaf63292012-12-03 09:23:28 -05005177static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02005178{
5179 struct jumbo_ops *ops = &tp->jumbo_ops;
5180
5181 switch (tp->mac_version) {
5182 case RTL_GIGA_MAC_VER_11:
5183 ops->disable = r8168b_0_hw_jumbo_disable;
5184 ops->enable = r8168b_0_hw_jumbo_enable;
5185 break;
5186 case RTL_GIGA_MAC_VER_12:
5187 case RTL_GIGA_MAC_VER_17:
5188 ops->disable = r8168b_1_hw_jumbo_disable;
5189 ops->enable = r8168b_1_hw_jumbo_enable;
5190 break;
5191 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5192 case RTL_GIGA_MAC_VER_19:
5193 case RTL_GIGA_MAC_VER_20:
5194 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5195 case RTL_GIGA_MAC_VER_22:
5196 case RTL_GIGA_MAC_VER_23:
5197 case RTL_GIGA_MAC_VER_24:
5198 case RTL_GIGA_MAC_VER_25:
5199 case RTL_GIGA_MAC_VER_26:
5200 ops->disable = r8168c_hw_jumbo_disable;
5201 ops->enable = r8168c_hw_jumbo_enable;
5202 break;
5203 case RTL_GIGA_MAC_VER_27:
5204 case RTL_GIGA_MAC_VER_28:
5205 ops->disable = r8168dp_hw_jumbo_disable;
5206 ops->enable = r8168dp_hw_jumbo_enable;
5207 break;
5208 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5209 case RTL_GIGA_MAC_VER_32:
5210 case RTL_GIGA_MAC_VER_33:
5211 case RTL_GIGA_MAC_VER_34:
5212 ops->disable = r8168e_hw_jumbo_disable;
5213 ops->enable = r8168e_hw_jumbo_enable;
5214 break;
5215
5216 /*
5217 * No action needed for jumbo frames with 8169.
5218 * No jumbo for 810x at all.
5219 */
Hayes Wangc5583862012-07-02 17:23:22 +08005220 case RTL_GIGA_MAC_VER_40:
5221 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005222 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005223 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005224 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005225 case RTL_GIGA_MAC_VER_45:
5226 case RTL_GIGA_MAC_VER_46:
5227 case RTL_GIGA_MAC_VER_47:
5228 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005229 case RTL_GIGA_MAC_VER_49:
5230 case RTL_GIGA_MAC_VER_50:
5231 case RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02005232 default:
5233 ops->disable = NULL;
5234 ops->enable = NULL;
5235 break;
5236 }
5237}
5238
Francois Romieuffc46952012-07-06 14:19:23 +02005239DECLARE_RTL_COND(rtl_chipcmd_cond)
5240{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005241 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02005242}
5243
Francois Romieu6f43adc2011-04-29 15:05:51 +02005244static void rtl_hw_reset(struct rtl8169_private *tp)
5245{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005246 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005247
Francois Romieuffc46952012-07-06 14:19:23 +02005248 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005249}
5250
Francois Romieub6ffd972011-06-17 17:00:05 +02005251static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
5252{
5253 struct rtl_fw *rtl_fw;
5254 const char *name;
5255 int rc = -ENOMEM;
5256
5257 name = rtl_lookup_firmware_name(tp);
5258 if (!name)
5259 goto out_no_firmware;
5260
5261 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5262 if (!rtl_fw)
5263 goto err_warn;
5264
5265 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
5266 if (rc < 0)
5267 goto err_free;
5268
Francois Romieufd112f22011-06-18 00:10:29 +02005269 rc = rtl_check_firmware(tp, rtl_fw);
5270 if (rc < 0)
5271 goto err_release_firmware;
5272
Francois Romieub6ffd972011-06-17 17:00:05 +02005273 tp->rtl_fw = rtl_fw;
5274out:
5275 return;
5276
Francois Romieufd112f22011-06-18 00:10:29 +02005277err_release_firmware:
5278 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02005279err_free:
5280 kfree(rtl_fw);
5281err_warn:
5282 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5283 name, rc);
5284out_no_firmware:
5285 tp->rtl_fw = NULL;
5286 goto out;
5287}
5288
François Romieu953a12c2011-04-24 17:38:48 +02005289static void rtl_request_firmware(struct rtl8169_private *tp)
5290{
Francois Romieub6ffd972011-06-17 17:00:05 +02005291 if (IS_ERR(tp->rtl_fw))
5292 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02005293}
5294
Hayes Wang92fc43b2011-07-06 15:58:03 +08005295static void rtl_rx_close(struct rtl8169_private *tp)
5296{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005297 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005298}
5299
Francois Romieuffc46952012-07-06 14:19:23 +02005300DECLARE_RTL_COND(rtl_npq_cond)
5301{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005302 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02005303}
5304
5305DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5306{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005307 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02005308}
5309
françois romieue6de30d2011-01-03 15:08:37 +00005310static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311{
5312 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00005313 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314
Hayes Wang92fc43b2011-07-06 15:58:03 +08005315 rtl_rx_close(tp);
5316
Hayes Wang5d2e1952011-02-22 17:26:22 +08005317 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00005318 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5319 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02005320 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08005321 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005322 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5323 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5324 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5325 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5326 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5327 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5328 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5329 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5330 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5331 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5332 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5333 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005334 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5335 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5336 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5337 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005338 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02005339 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005340 } else {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005341 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005342 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00005343 }
5344
Hayes Wang92fc43b2011-07-06 15:58:03 +08005345 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346}
5347
Francois Romieu7f796d832007-06-11 23:04:41 +02005348static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005349{
Francois Romieu9cb427b2006-11-02 00:10:16 +01005350 /* Set DMA burst size and Interframe Gap Time */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005351 RTL_W32(tp, TxConfig, (TX_DMA_BURST << TxDMAShift) |
Francois Romieu9cb427b2006-11-02 00:10:16 +01005352 (InterFrameGap << TxInterFrameGapShift));
5353}
5354
Francois Romieu07ce4062007-02-23 23:36:39 +01005355static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356{
5357 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
Francois Romieu07ce4062007-02-23 23:36:39 +01005359 tp->hw_start(dev);
5360
Francois Romieuda78dbf2012-01-26 14:18:23 +01005361 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01005362}
5363
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005364static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02005365{
5366 /*
5367 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5368 * register to be written before TxDescAddrLow to work.
5369 * Switching from MMIO to I/O access fixes the issue as well.
5370 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005371 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
5372 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
5373 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
5374 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005375}
5376
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005377static u16 rtl_rw_cpluscmd(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02005378{
5379 u16 cmd;
5380
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005381 cmd = RTL_R16(tp, CPlusCmd);
5382 RTL_W16(tp, CPlusCmd, cmd);
Francois Romieu7f796d832007-06-11 23:04:41 +02005383 return cmd;
5384}
5385
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005386static void rtl_set_rx_max_size(struct rtl8169_private *tp, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02005387{
5388 /* Low hurts. Let's disable the filtering. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005389 RTL_W16(tp, RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02005390}
5391
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005392static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01005393{
Francois Romieu37441002011-06-17 22:58:54 +02005394 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01005395 u32 mac_version;
5396 u32 clk;
5397 u32 val;
5398 } cfg2_info [] = {
5399 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5400 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5401 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5402 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02005403 };
5404 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01005405 unsigned int i;
5406 u32 clk;
5407
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005408 clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01005409 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01005410 if ((p->mac_version == mac_version) && (p->clk == clk)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005411 RTL_W32(tp, 0x7c, p->val);
Francois Romieu6dccd162007-02-13 23:38:05 +01005412 break;
5413 }
5414 }
5415}
5416
Francois Romieue6b763e2012-03-08 09:35:39 +01005417static void rtl_set_rx_mode(struct net_device *dev)
5418{
5419 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieue6b763e2012-03-08 09:35:39 +01005420 u32 mc_filter[2]; /* Multicast hash filter */
5421 int rx_mode;
5422 u32 tmp = 0;
5423
5424 if (dev->flags & IFF_PROMISC) {
5425 /* Unconditionally log net taps. */
5426 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5427 rx_mode =
5428 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5429 AcceptAllPhys;
5430 mc_filter[1] = mc_filter[0] = 0xffffffff;
5431 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5432 (dev->flags & IFF_ALLMULTI)) {
5433 /* Too many to filter perfectly -- accept all multicasts. */
5434 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5435 mc_filter[1] = mc_filter[0] = 0xffffffff;
5436 } else {
5437 struct netdev_hw_addr *ha;
5438
5439 rx_mode = AcceptBroadcast | AcceptMyPhys;
5440 mc_filter[1] = mc_filter[0] = 0;
5441 netdev_for_each_mc_addr(ha, dev) {
5442 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5443 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5444 rx_mode |= AcceptMulticast;
5445 }
5446 }
5447
5448 if (dev->features & NETIF_F_RXALL)
5449 rx_mode |= (AcceptErr | AcceptRunt);
5450
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005451 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Francois Romieue6b763e2012-03-08 09:35:39 +01005452
5453 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5454 u32 data = mc_filter[0];
5455
5456 mc_filter[0] = swab32(mc_filter[1]);
5457 mc_filter[1] = swab32(data);
5458 }
5459
Nathan Walp04817762012-11-01 12:08:47 +00005460 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5461 mc_filter[1] = mc_filter[0] = 0xffffffff;
5462
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005463 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
5464 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01005465
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005466 RTL_W32(tp, RxConfig, tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01005467}
5468
Francois Romieu07ce4062007-02-23 23:36:39 +01005469static void rtl_hw_start_8169(struct net_device *dev)
5470{
5471 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu07ce4062007-02-23 23:36:39 +01005472 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01005473
Francois Romieu9cb427b2006-11-02 00:10:16 +01005474 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005475 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) | PCIMulRW);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005476 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
5477 }
5478
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005479 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02005480 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5481 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5482 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5483 tp->mac_version == RTL_GIGA_MAC_VER_04)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005484 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005485
Hayes Wange542a222011-07-06 15:58:04 +08005486 rtl_init_rxcfg(tp);
5487
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005488 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005490 rtl_set_rx_max_size(tp, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491
Francois Romieucecb5fd2011-04-01 10:21:07 +02005492 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5493 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5494 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5495 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02005496 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005498 tp->cp_cmd |= rtl_rw_cpluscmd(tp) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005499
Francois Romieucecb5fd2011-04-01 10:21:07 +02005500 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5501 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005502 dprintk("Set MAC Reg C+CR Offset 0xe0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005504 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 }
5506
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005507 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005508
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005509 rtl8169_set_magic_reg(tp, tp->mac_version);
Francois Romieu6dccd162007-02-13 23:38:05 +01005510
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 /*
5512 * Undocumented corner. Supposedly:
5513 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5514 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005515 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005517 rtl_set_rx_tx_desc_registers(tp);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005518
Francois Romieucecb5fd2011-04-01 10:21:07 +02005519 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5520 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5521 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5522 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005523 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieuc946b302007-10-04 00:42:50 +02005524 rtl_set_rx_tx_config_registers(tp);
5525 }
5526
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005527 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02005528
5529 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005530 RTL_R8(tp, IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005532 RTL_W32(tp, RxMissed, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533
Francois Romieu07ce4062007-02-23 23:36:39 +01005534 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
5536 /* no early-rx interrupts */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005537 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005538}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005540static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5541{
5542 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02005543 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005544}
5545
5546static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5547{
Francois Romieu52989f02012-07-06 13:37:00 +02005548 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005549}
5550
5551static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02005552{
5553 u32 csi;
5554
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005555 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5556 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00005557}
5558
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005559static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005560{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005561 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00005562}
5563
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005564static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00005565{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005566 rtl_csi_access_enable(tp, 0x27000000);
5567}
5568
Francois Romieuffc46952012-07-06 14:19:23 +02005569DECLARE_RTL_COND(rtl_csiar_cond)
5570{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005571 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02005572}
5573
Francois Romieu52989f02012-07-06 13:37:00 +02005574static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005575{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005576 RTL_W32(tp, CSIDR, value);
5577 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005578 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5579
Francois Romieuffc46952012-07-06 14:19:23 +02005580 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005581}
5582
Francois Romieu52989f02012-07-06 13:37:00 +02005583static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005584{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005585 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) |
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005586 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5587
Francois Romieuffc46952012-07-06 14:19:23 +02005588 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005589 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005590}
5591
Francois Romieu52989f02012-07-06 13:37:00 +02005592static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005593{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005594 RTL_W32(tp, CSIDR, value);
5595 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Hayes Wang7e18dca2012-03-30 14:33:02 +08005596 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5597 CSIAR_FUNC_NIC);
5598
Francois Romieuffc46952012-07-06 14:19:23 +02005599 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005600}
5601
Francois Romieu52989f02012-07-06 13:37:00 +02005602static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005603{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005604 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
Hayes Wang7e18dca2012-03-30 14:33:02 +08005605 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5606
Francois Romieuffc46952012-07-06 14:19:23 +02005607 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005608 RTL_R32(tp, CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005609}
5610
hayeswang45dd95c2013-07-08 17:09:01 +08005611static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5612{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005613 RTL_W32(tp, CSIDR, value);
5614 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
hayeswang45dd95c2013-07-08 17:09:01 +08005615 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5616 CSIAR_FUNC_NIC2);
5617
5618 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5619}
5620
5621static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5622{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005623 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
hayeswang45dd95c2013-07-08 17:09:01 +08005624 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5625
5626 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005627 RTL_R32(tp, CSIDR) : ~0;
hayeswang45dd95c2013-07-08 17:09:01 +08005628}
5629
Bill Pembertonbaf63292012-12-03 09:23:28 -05005630static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005631{
5632 struct csi_ops *ops = &tp->csi_ops;
5633
5634 switch (tp->mac_version) {
5635 case RTL_GIGA_MAC_VER_01:
5636 case RTL_GIGA_MAC_VER_02:
5637 case RTL_GIGA_MAC_VER_03:
5638 case RTL_GIGA_MAC_VER_04:
5639 case RTL_GIGA_MAC_VER_05:
5640 case RTL_GIGA_MAC_VER_06:
5641 case RTL_GIGA_MAC_VER_10:
5642 case RTL_GIGA_MAC_VER_11:
5643 case RTL_GIGA_MAC_VER_12:
5644 case RTL_GIGA_MAC_VER_13:
5645 case RTL_GIGA_MAC_VER_14:
5646 case RTL_GIGA_MAC_VER_15:
5647 case RTL_GIGA_MAC_VER_16:
5648 case RTL_GIGA_MAC_VER_17:
5649 ops->write = NULL;
5650 ops->read = NULL;
5651 break;
5652
Hayes Wang7e18dca2012-03-30 14:33:02 +08005653 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005654 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005655 ops->write = r8402_csi_write;
5656 ops->read = r8402_csi_read;
5657 break;
5658
hayeswang45dd95c2013-07-08 17:09:01 +08005659 case RTL_GIGA_MAC_VER_44:
5660 ops->write = r8411_csi_write;
5661 ops->read = r8411_csi_read;
5662 break;
5663
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005664 default:
5665 ops->write = r8169_csi_write;
5666 ops->read = r8169_csi_read;
5667 break;
5668 }
Francois Romieudacf8152008-08-02 20:44:13 +02005669}
5670
5671struct ephy_info {
5672 unsigned int offset;
5673 u16 mask;
5674 u16 bits;
5675};
5676
Francois Romieufdf6fc02012-07-06 22:40:38 +02005677static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5678 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005679{
5680 u16 w;
5681
5682 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005683 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5684 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005685 e++;
5686 }
5687}
5688
Francois Romieub726e492008-06-28 12:22:59 +02005689static void rtl_disable_clock_request(struct pci_dev *pdev)
5690{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005691 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
5692 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005693}
5694
françois romieue6de30d2011-01-03 15:08:37 +00005695static void rtl_enable_clock_request(struct pci_dev *pdev)
5696{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005697 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
5698 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005699}
5700
hayeswangb51ecea2014-07-09 14:52:51 +08005701static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5702{
hayeswangb51ecea2014-07-09 14:52:51 +08005703 u8 data;
5704
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005705 data = RTL_R8(tp, Config3);
hayeswangb51ecea2014-07-09 14:52:51 +08005706
5707 if (enable)
5708 data |= Rdy_to_L23;
5709 else
5710 data &= ~Rdy_to_L23;
5711
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005712 RTL_W8(tp, Config3, data);
hayeswangb51ecea2014-07-09 14:52:51 +08005713}
5714
Francois Romieub726e492008-06-28 12:22:59 +02005715#define R8168_CPCMD_QUIRK_MASK (\
5716 EnableBist | \
5717 Mac_dbgo_oe | \
5718 Force_half_dup | \
5719 Force_rxflow_en | \
5720 Force_txflow_en | \
5721 Cxpl_dbg_sel | \
5722 ASF | \
5723 PktCntrDisable | \
5724 Mac_dbgo_sel)
5725
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005726static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005727{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005728 struct pci_dev *pdev = tp->pci_dev;
5729
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005730 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005731
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005732 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieub726e492008-06-28 12:22:59 +02005733
françois romieufaf1e782013-02-27 13:01:57 +00005734 if (tp->dev->mtu <= ETH_DATA_LEN) {
5735 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
5736 PCI_EXP_DEVCTL_NOSNOOP_EN);
5737 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005738}
5739
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005740static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005741{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005742 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005743
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005744 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005745
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005746 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005747}
5748
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005749static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005750{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005751 struct pci_dev *pdev = tp->pci_dev;
5752
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005753 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02005754
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005755 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005756
françois romieufaf1e782013-02-27 13:01:57 +00005757 if (tp->dev->mtu <= ETH_DATA_LEN)
5758 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02005759
5760 rtl_disable_clock_request(pdev);
5761
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005762 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02005763}
5764
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005765static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005766{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005767 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005768 { 0x01, 0, 0x0001 },
5769 { 0x02, 0x0800, 0x1000 },
5770 { 0x03, 0, 0x0042 },
5771 { 0x06, 0x0080, 0x0000 },
5772 { 0x07, 0, 0x2000 }
5773 };
5774
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005775 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005776
Francois Romieufdf6fc02012-07-06 22:40:38 +02005777 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005778
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005779 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005780}
5781
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005782static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005783{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005784 struct pci_dev *pdev = tp->pci_dev;
5785
5786 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005787
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005788 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02005789
françois romieufaf1e782013-02-27 13:01:57 +00005790 if (tp->dev->mtu <= ETH_DATA_LEN)
5791 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieuef3386f2008-06-29 12:24:30 +02005792
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005793 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieuef3386f2008-06-29 12:24:30 +02005794}
5795
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005796static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005797{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005798 struct pci_dev *pdev = tp->pci_dev;
5799
5800 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005801
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005802 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005803
5804 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005805 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005806
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005807 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005808
françois romieufaf1e782013-02-27 13:01:57 +00005809 if (tp->dev->mtu <= ETH_DATA_LEN)
5810 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005811
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005812 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005813}
5814
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005815static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005816{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005817 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005818 { 0x02, 0x0800, 0x1000 },
5819 { 0x03, 0, 0x0002 },
5820 { 0x06, 0x0080, 0x0000 }
5821 };
5822
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005823 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005824
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005825 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02005826
Francois Romieufdf6fc02012-07-06 22:40:38 +02005827 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005828
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005829 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005830}
5831
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005832static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005833{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005834 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005835 { 0x01, 0, 0x0001 },
5836 { 0x03, 0x0400, 0x0220 }
5837 };
5838
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005839 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005840
Francois Romieufdf6fc02012-07-06 22:40:38 +02005841 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005842
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005843 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005844}
5845
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005846static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005847{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005848 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005849}
5850
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005851static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005852{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005853 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005854
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005855 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005856}
5857
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005858static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005859{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005860 struct pci_dev *pdev = tp->pci_dev;
5861
5862 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005863
5864 rtl_disable_clock_request(pdev);
5865
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005866 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005867
françois romieufaf1e782013-02-27 13:01:57 +00005868 if (tp->dev->mtu <= ETH_DATA_LEN)
5869 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu5b538df2008-07-20 16:22:45 +02005870
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005871 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu5b538df2008-07-20 16:22:45 +02005872}
5873
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005874static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005875{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005876 struct pci_dev *pdev = tp->pci_dev;
5877
5878 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005879
françois romieufaf1e782013-02-27 13:01:57 +00005880 if (tp->dev->mtu <= ETH_DATA_LEN)
5881 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang4804b3b2011-03-21 01:50:29 +00005882
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005883 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang4804b3b2011-03-21 01:50:29 +00005884
5885 rtl_disable_clock_request(pdev);
5886}
5887
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005888static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005889{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005890 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00005891 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005892 { 0x0b, 0x0000, 0x0048 },
5893 { 0x19, 0x0020, 0x0050 },
5894 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00005895 };
françois romieue6de30d2011-01-03 15:08:37 +00005896
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005897 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005898
5899 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5900
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005901 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
françois romieue6de30d2011-01-03 15:08:37 +00005902
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005903 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00005904
5905 rtl_enable_clock_request(pdev);
5906}
5907
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005908static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005909{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005910 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005911 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005912 { 0x00, 0x0200, 0x0100 },
5913 { 0x00, 0x0000, 0x0004 },
5914 { 0x06, 0x0002, 0x0001 },
5915 { 0x06, 0x0000, 0x0030 },
5916 { 0x07, 0x0000, 0x2000 },
5917 { 0x00, 0x0000, 0x0020 },
5918 { 0x03, 0x5800, 0x2000 },
5919 { 0x03, 0x0000, 0x0001 },
5920 { 0x01, 0x0800, 0x1000 },
5921 { 0x07, 0x0000, 0x4000 },
5922 { 0x1e, 0x0000, 0x2000 },
5923 { 0x19, 0xffff, 0xfe6c },
5924 { 0x0a, 0x0000, 0x0040 }
5925 };
5926
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005927 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005928
Francois Romieufdf6fc02012-07-06 22:40:38 +02005929 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005930
françois romieufaf1e782013-02-27 13:01:57 +00005931 if (tp->dev->mtu <= ETH_DATA_LEN)
5932 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang01dc7fe2011-03-21 01:50:28 +00005933
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005934 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang01dc7fe2011-03-21 01:50:28 +00005935
5936 rtl_disable_clock_request(pdev);
5937
5938 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005939 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
5940 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005941
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005942 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005943}
5944
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005945static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005946{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005947 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005948 static const struct ephy_info e_info_8168e_2[] = {
5949 { 0x09, 0x0000, 0x0080 },
5950 { 0x19, 0x0000, 0x0224 }
5951 };
5952
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005953 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005954
Francois Romieufdf6fc02012-07-06 22:40:38 +02005955 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005956
françois romieufaf1e782013-02-27 13:01:57 +00005957 if (tp->dev->mtu <= ETH_DATA_LEN)
5958 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Hayes Wang70090422011-07-06 15:58:06 +08005959
Francois Romieufdf6fc02012-07-06 22:40:38 +02005960 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5961 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5962 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5963 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5964 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5965 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005966 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5967 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005968
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005969 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005970
Francois Romieu4521e1a92012-11-01 16:46:28 +00005971 rtl_disable_clock_request(pdev);
5972
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005973 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5974 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08005975
5976 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005977 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang70090422011-07-06 15:58:06 +08005978
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005979 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5980 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5981 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08005982}
5983
Hayes Wang5f886e02012-03-30 14:33:03 +08005984static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005985{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005986 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08005987
Hayes Wang5f886e02012-03-30 14:33:03 +08005988 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005989
5990 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5991
Francois Romieufdf6fc02012-07-06 22:40:38 +02005992 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5993 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5994 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5995 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005996 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5997 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5998 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5999 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006000 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
6001 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08006002
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006003 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc2218922011-09-06 16:55:18 +08006004
Francois Romieu4521e1a92012-11-01 16:46:28 +00006005 rtl_disable_clock_request(pdev);
6006
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006007 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
6008 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
6009 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
6010 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
6011 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08006012}
6013
Hayes Wang5f886e02012-03-30 14:33:03 +08006014static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
6015{
Hayes Wang5f886e02012-03-30 14:33:03 +08006016 static const struct ephy_info e_info_8168f_1[] = {
6017 { 0x06, 0x00c0, 0x0020 },
6018 { 0x08, 0x0001, 0x0002 },
6019 { 0x09, 0x0000, 0x0080 },
6020 { 0x19, 0x0000, 0x0224 }
6021 };
6022
6023 rtl_hw_start_8168f(tp);
6024
Francois Romieufdf6fc02012-07-06 22:40:38 +02006025 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08006026
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006027 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08006028
6029 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006030 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang5f886e02012-03-30 14:33:03 +08006031}
6032
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006033static void rtl_hw_start_8411(struct rtl8169_private *tp)
6034{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006035 static const struct ephy_info e_info_8168f_1[] = {
6036 { 0x06, 0x00c0, 0x0020 },
6037 { 0x0f, 0xffff, 0x5200 },
6038 { 0x1e, 0x0000, 0x4000 },
6039 { 0x19, 0x0000, 0x0224 }
6040 };
6041
6042 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08006043 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006044
Francois Romieufdf6fc02012-07-06 22:40:38 +02006045 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006046
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006047 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006048}
6049
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006050static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006051{
Hayes Wangc5583862012-07-02 17:23:22 +08006052 struct pci_dev *pdev = tp->pci_dev;
6053
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006054 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
hayeswangbeb330a2013-04-01 22:23:39 +00006055
Hayes Wangc5583862012-07-02 17:23:22 +08006056 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
6057 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6058 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6059 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6060
6061 rtl_csi_access_enable_1(tp);
6062
6063 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6064
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006065 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6066 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00006067 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08006068
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006069 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
6070 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc5583862012-07-02 17:23:22 +08006071
6072 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6073 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6074
6075 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006076 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wangc5583862012-07-02 17:23:22 +08006077
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006078 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6079 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006080
6081 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08006082}
6083
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006084static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
6085{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006086 static const struct ephy_info e_info_8168g_1[] = {
6087 { 0x00, 0x0000, 0x0008 },
6088 { 0x0c, 0x37d0, 0x0820 },
6089 { 0x1e, 0x0000, 0x0001 },
6090 { 0x19, 0x8000, 0x0000 }
6091 };
6092
6093 rtl_hw_start_8168g(tp);
6094
6095 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006096 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6097 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006098 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
6099}
6100
hayeswang57538c42013-04-01 22:23:40 +00006101static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
6102{
hayeswang57538c42013-04-01 22:23:40 +00006103 static const struct ephy_info e_info_8168g_2[] = {
6104 { 0x00, 0x0000, 0x0008 },
6105 { 0x0c, 0x3df0, 0x0200 },
6106 { 0x19, 0xffff, 0xfc00 },
6107 { 0x1e, 0xffff, 0x20eb }
6108 };
6109
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006110 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00006111
6112 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006113 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6114 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang57538c42013-04-01 22:23:40 +00006115 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
6116}
6117
hayeswang45dd95c2013-07-08 17:09:01 +08006118static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
6119{
hayeswang45dd95c2013-07-08 17:09:01 +08006120 static const struct ephy_info e_info_8411_2[] = {
6121 { 0x00, 0x0000, 0x0008 },
6122 { 0x0c, 0x3df0, 0x0200 },
6123 { 0x0f, 0xffff, 0x5200 },
6124 { 0x19, 0x0020, 0x0000 },
6125 { 0x1e, 0x0000, 0x2000 }
6126 };
6127
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006128 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08006129
6130 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006131 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6132 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang45dd95c2013-07-08 17:09:01 +08006133 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
6134}
6135
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006136static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
6137{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006138 struct pci_dev *pdev = tp->pci_dev;
Andrzej Hajda72521ea2015-09-24 16:00:24 +02006139 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006140 u32 data;
6141 static const struct ephy_info e_info_8168h_1[] = {
6142 { 0x1e, 0x0800, 0x0001 },
6143 { 0x1d, 0x0000, 0x0800 },
6144 { 0x05, 0xffff, 0x2089 },
6145 { 0x06, 0xffff, 0x5881 },
6146 { 0x04, 0xffff, 0x154a },
6147 { 0x01, 0xffff, 0x068b }
6148 };
6149
6150 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006151 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6152 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006153 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
6154
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006155 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006156
6157 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6158 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6159 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6160 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6161
6162 rtl_csi_access_enable_1(tp);
6163
6164 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6165
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006166 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6167 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006168
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006169 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006170
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006171 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006172
6173 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6174
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006175 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
6176 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006177
6178 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6179 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6180
6181 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006182 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006183
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006184 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6185 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006186
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006187 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006188
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006189 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006190
6191 rtl_pcie_state_l2l3_enable(tp, false);
6192
6193 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08006194 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006195 rtl_writephy(tp, 0x1f, 0x0000);
6196 if (rg_saw_cnt > 0) {
6197 u16 sw_cnt_1ms_ini;
6198
6199 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
6200 sw_cnt_1ms_ini &= 0x0fff;
6201 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006202 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006203 data |= sw_cnt_1ms_ini;
6204 r8168_mac_ocp_write(tp, 0xd412, data);
6205 }
6206
6207 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006208 data &= ~0xf0;
6209 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006210 r8168_mac_ocp_write(tp, 0xe056, data);
6211
6212 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006213 data &= ~0x6000;
6214 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006215 r8168_mac_ocp_write(tp, 0xe052, data);
6216
6217 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006218 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006219 data |= 0x017f;
6220 r8168_mac_ocp_write(tp, 0xe0d6, data);
6221
6222 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006223 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006224 data |= 0x047f;
6225 r8168_mac_ocp_write(tp, 0xd420, data);
6226
6227 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6228 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6229 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6230 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6231}
6232
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006233static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6234{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006235 struct pci_dev *pdev = tp->pci_dev;
6236
Chun-Hao Lin003609d2014-12-02 16:48:31 +08006237 rtl8168ep_stop_cmac(tp);
6238
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006239 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006240
6241 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6242 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6243 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6244 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6245
6246 rtl_csi_access_enable_1(tp);
6247
6248 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6249
6250 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6251 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6252
6253 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6254
6255 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6256
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006257 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
6258 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006259
6260 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6261 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6262
6263 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006264 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006265
6266 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6267
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006268 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006269
6270 rtl_pcie_state_l2l3_enable(tp, false);
6271}
6272
6273static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6274{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006275 static const struct ephy_info e_info_8168ep_1[] = {
6276 { 0x00, 0xffff, 0x10ab },
6277 { 0x06, 0xffff, 0xf030 },
6278 { 0x08, 0xffff, 0x2006 },
6279 { 0x0d, 0xffff, 0x1666 },
6280 { 0x0c, 0x3ff0, 0x0000 }
6281 };
6282
6283 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006284 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6285 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006286 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6287
6288 rtl_hw_start_8168ep(tp);
6289}
6290
6291static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6292{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006293 static const struct ephy_info e_info_8168ep_2[] = {
6294 { 0x00, 0xffff, 0x10a3 },
6295 { 0x19, 0xffff, 0xfc00 },
6296 { 0x1e, 0xffff, 0x20ea }
6297 };
6298
6299 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006300 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6301 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006302 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6303
6304 rtl_hw_start_8168ep(tp);
6305
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006306 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6307 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006308}
6309
6310static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6311{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006312 u32 data;
6313 static const struct ephy_info e_info_8168ep_3[] = {
6314 { 0x00, 0xffff, 0x10a3 },
6315 { 0x19, 0xffff, 0x7c00 },
6316 { 0x1e, 0xffff, 0x20eb },
6317 { 0x0d, 0xffff, 0x1666 }
6318 };
6319
6320 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006321 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6322 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006323 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6324
6325 rtl_hw_start_8168ep(tp);
6326
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006327 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6328 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006329
6330 data = r8168_mac_ocp_read(tp, 0xd3e2);
6331 data &= 0xf000;
6332 data |= 0x0271;
6333 r8168_mac_ocp_write(tp, 0xd3e2, data);
6334
6335 data = r8168_mac_ocp_read(tp, 0xd3e4);
6336 data &= 0xff00;
6337 r8168_mac_ocp_write(tp, 0xd3e4, data);
6338
6339 data = r8168_mac_ocp_read(tp, 0xe860);
6340 data |= 0x0080;
6341 r8168_mac_ocp_write(tp, 0xe860, data);
6342}
6343
Francois Romieu07ce4062007-02-23 23:36:39 +01006344static void rtl_hw_start_8168(struct net_device *dev)
6345{
Francois Romieu2dd99532007-06-11 23:22:52 +02006346 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu2dd99532007-06-11 23:22:52 +02006347
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006348 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieu2dd99532007-06-11 23:22:52 +02006349
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006350 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02006351
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006352 rtl_set_rx_max_size(tp, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02006353
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006354 tp->cp_cmd |= RTL_R16(tp, CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02006355
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006356 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu2dd99532007-06-11 23:22:52 +02006357
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006358 RTL_W16(tp, IntrMitigate, 0x5151);
Francois Romieu0e485152007-02-20 00:00:26 +01006359
6360 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00006361 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006362 tp->event_slow |= RxFIFOOver | PCSTimeout;
6363 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01006364 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006365
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006366 rtl_set_rx_tx_desc_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006367
hayeswang1a964642013-04-01 22:23:41 +00006368 rtl_set_rx_tx_config_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006369
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006370 RTL_R8(tp, IntrMask);
Francois Romieu2dd99532007-06-11 23:22:52 +02006371
Francois Romieu219a1e92008-06-28 11:58:39 +02006372 switch (tp->mac_version) {
6373 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006374 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006375 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006376
6377 case RTL_GIGA_MAC_VER_12:
6378 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006379 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006380 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006381
6382 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006383 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006384 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006385
6386 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006387 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006388 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006389
6390 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006391 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006392 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006393
Francois Romieu197ff762008-06-28 13:16:02 +02006394 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006395 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006396 break;
Francois Romieu197ff762008-06-28 13:16:02 +02006397
Francois Romieu6fb07052008-06-29 11:54:28 +02006398 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006399 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006400 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02006401
Francois Romieuef3386f2008-06-29 12:24:30 +02006402 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006403 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006404 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02006405
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006406 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006407 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006408 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006409
Francois Romieu5b538df2008-07-20 16:22:45 +02006410 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00006411 case RTL_GIGA_MAC_VER_26:
6412 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006413 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006414 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02006415
françois romieue6de30d2011-01-03 15:08:37 +00006416 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006417 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006418 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02006419
hayeswang4804b3b2011-03-21 01:50:29 +00006420 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006421 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006422 break;
6423
hayeswang01dc7fe2011-03-21 01:50:28 +00006424 case RTL_GIGA_MAC_VER_32:
6425 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006426 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006427 break;
6428 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006429 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006430 break;
françois romieue6de30d2011-01-03 15:08:37 +00006431
Hayes Wangc2218922011-09-06 16:55:18 +08006432 case RTL_GIGA_MAC_VER_35:
6433 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006434 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006435 break;
6436
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006437 case RTL_GIGA_MAC_VER_38:
6438 rtl_hw_start_8411(tp);
6439 break;
6440
Hayes Wangc5583862012-07-02 17:23:22 +08006441 case RTL_GIGA_MAC_VER_40:
6442 case RTL_GIGA_MAC_VER_41:
6443 rtl_hw_start_8168g_1(tp);
6444 break;
hayeswang57538c42013-04-01 22:23:40 +00006445 case RTL_GIGA_MAC_VER_42:
6446 rtl_hw_start_8168g_2(tp);
6447 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006448
hayeswang45dd95c2013-07-08 17:09:01 +08006449 case RTL_GIGA_MAC_VER_44:
6450 rtl_hw_start_8411_2(tp);
6451 break;
6452
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006453 case RTL_GIGA_MAC_VER_45:
6454 case RTL_GIGA_MAC_VER_46:
6455 rtl_hw_start_8168h_1(tp);
6456 break;
6457
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006458 case RTL_GIGA_MAC_VER_49:
6459 rtl_hw_start_8168ep_1(tp);
6460 break;
6461
6462 case RTL_GIGA_MAC_VER_50:
6463 rtl_hw_start_8168ep_2(tp);
6464 break;
6465
6466 case RTL_GIGA_MAC_VER_51:
6467 rtl_hw_start_8168ep_3(tp);
6468 break;
6469
Francois Romieu219a1e92008-06-28 11:58:39 +02006470 default:
6471 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
6472 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00006473 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006474 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006475
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006476 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
hayeswang1a964642013-04-01 22:23:41 +00006477
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006478 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieu0e485152007-02-20 00:00:26 +01006479
hayeswang1a964642013-04-01 22:23:41 +00006480 rtl_set_rx_mode(dev);
Francois Romieub8363902008-06-01 12:31:57 +02006481
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006482 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01006483}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484
Francois Romieu2857ffb2008-08-02 21:08:49 +02006485#define R810X_CPCMD_QUIRK_MASK (\
6486 EnableBist | \
6487 Mac_dbgo_oe | \
6488 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00006489 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02006490 Force_txflow_en | \
6491 Cxpl_dbg_sel | \
6492 ASF | \
6493 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006494 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006495
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006496static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006497{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006498 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006499 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02006500 { 0x01, 0, 0x6e65 },
6501 { 0x02, 0, 0x091f },
6502 { 0x03, 0, 0xc2f9 },
6503 { 0x06, 0, 0xafb5 },
6504 { 0x07, 0, 0x0e00 },
6505 { 0x19, 0, 0xec80 },
6506 { 0x01, 0, 0x2e65 },
6507 { 0x01, 0, 0x6e65 }
6508 };
6509 u8 cfg1;
6510
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006511 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006512
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006513 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006514
6515 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6516
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006517 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02006518 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006519 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006520
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006521 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006522 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006523 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006524
Francois Romieufdf6fc02012-07-06 22:40:38 +02006525 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02006526}
6527
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006528static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006529{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006530 struct pci_dev *pdev = tp->pci_dev;
6531
6532 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006533
6534 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6535
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006536 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
6537 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006538}
6539
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006540static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006541{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006542 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006543
Francois Romieufdf6fc02012-07-06 22:40:38 +02006544 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006545}
6546
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006547static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006548{
6549 static const struct ephy_info e_info_8105e_1[] = {
6550 { 0x07, 0, 0x4000 },
6551 { 0x19, 0, 0x0200 },
6552 { 0x19, 0, 0x0020 },
6553 { 0x1e, 0, 0x2000 },
6554 { 0x03, 0, 0x0001 },
6555 { 0x19, 0, 0x0100 },
6556 { 0x19, 0, 0x0004 },
6557 { 0x0a, 0, 0x0020 }
6558 };
6559
Francois Romieucecb5fd2011-04-01 10:21:07 +02006560 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006561 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006562
Francois Romieucecb5fd2011-04-01 10:21:07 +02006563 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006564 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006565
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006566 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6567 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006568
Francois Romieufdf6fc02012-07-06 22:40:38 +02006569 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08006570
6571 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006572}
6573
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006574static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006575{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006576 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006577 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006578}
6579
Hayes Wang7e18dca2012-03-30 14:33:02 +08006580static void rtl_hw_start_8402(struct rtl8169_private *tp)
6581{
Hayes Wang7e18dca2012-03-30 14:33:02 +08006582 static const struct ephy_info e_info_8402[] = {
6583 { 0x19, 0xffff, 0xff64 },
6584 { 0x1e, 0, 0x4000 }
6585 };
6586
6587 rtl_csi_access_enable_2(tp);
6588
6589 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006590 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006591
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006592 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
6593 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006594
Francois Romieufdf6fc02012-07-06 22:40:38 +02006595 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006596
6597 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
6598
Francois Romieufdf6fc02012-07-06 22:40:38 +02006599 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6600 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006601 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6602 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006603 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6604 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006605 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006606
6607 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006608}
6609
Hayes Wang5598bfe2012-07-02 17:23:21 +08006610static void rtl_hw_start_8106(struct rtl8169_private *tp)
6611{
Hayes Wang5598bfe2012-07-02 17:23:21 +08006612 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006613 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006614
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006615 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
6616 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6617 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006618
6619 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006620}
6621
Francois Romieu07ce4062007-02-23 23:36:39 +01006622static void rtl_hw_start_8101(struct net_device *dev)
6623{
Francois Romieucdf1a602007-06-11 23:29:50 +02006624 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieucdf1a602007-06-11 23:29:50 +02006625 struct pci_dev *pdev = tp->pci_dev;
6626
Francois Romieuda78dbf2012-01-26 14:18:23 +01006627 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6628 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006629
Francois Romieucecb5fd2011-04-01 10:21:07 +02006630 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006631 tp->mac_version == RTL_GIGA_MAC_VER_16)
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006632 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
6633 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006634
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006635 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006636
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006637 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00006638
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006639 rtl_set_rx_max_size(tp, rx_buf_sz);
hayeswang1a964642013-04-01 22:23:41 +00006640
6641 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006642 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
hayeswang1a964642013-04-01 22:23:41 +00006643
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006644 rtl_set_rx_tx_desc_registers(tp);
hayeswang1a964642013-04-01 22:23:41 +00006645
6646 rtl_set_rx_tx_config_registers(tp);
6647
Francois Romieu2857ffb2008-08-02 21:08:49 +02006648 switch (tp->mac_version) {
6649 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006650 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006651 break;
6652
6653 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006654 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006655 break;
6656
6657 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006658 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006659 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006660
6661 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006662 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006663 break;
6664 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006665 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006666 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006667
6668 case RTL_GIGA_MAC_VER_37:
6669 rtl_hw_start_8402(tp);
6670 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006671
6672 case RTL_GIGA_MAC_VER_39:
6673 rtl_hw_start_8106(tp);
6674 break;
hayeswang58152cd2013-04-01 22:23:42 +00006675 case RTL_GIGA_MAC_VER_43:
6676 rtl_hw_start_8168g_2(tp);
6677 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006678 case RTL_GIGA_MAC_VER_47:
6679 case RTL_GIGA_MAC_VER_48:
6680 rtl_hw_start_8168h_1(tp);
6681 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006682 }
6683
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006684 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02006685
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006686 RTL_W16(tp, IntrMitigate, 0x0000);
Francois Romieucdf1a602007-06-11 23:29:50 +02006687
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006688 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieucdf1a602007-06-11 23:29:50 +02006689
Francois Romieucdf1a602007-06-11 23:29:50 +02006690 rtl_set_rx_mode(dev);
6691
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006692 RTL_R8(tp, IntrMask);
hayeswang1a964642013-04-01 22:23:41 +00006693
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006694 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695}
6696
6697static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6698{
Francois Romieud58d46b2011-05-03 16:38:29 +02006699 struct rtl8169_private *tp = netdev_priv(dev);
6700
Francois Romieud58d46b2011-05-03 16:38:29 +02006701 if (new_mtu > ETH_DATA_LEN)
6702 rtl_hw_jumbo_enable(tp);
6703 else
6704 rtl_hw_jumbo_disable(tp);
6705
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006707 netdev_update_features(dev);
6708
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006709 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710}
6711
6712static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6713{
Al Viro95e09182007-12-22 18:55:39 +00006714 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6716}
6717
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006718static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6719 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006721 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006722 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006723
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006724 kfree(*data_buff);
6725 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 rtl8169_make_unusable_by_asic(desc);
6727}
6728
6729static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
6730{
6731 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6732
Alexander Duycka0750132014-12-11 15:02:17 -08006733 /* Force memory writes to complete before releasing descriptor */
6734 dma_wmb();
6735
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
6737}
6738
6739static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
6740 u32 rx_buf_sz)
6741{
6742 desc->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743 rtl8169_mark_to_asic(desc, rx_buf_sz);
6744}
6745
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006746static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006748 return (void *)ALIGN((long)data, 16);
6749}
6750
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006751static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6752 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006753{
6754 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006756 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006757 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006758 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006760 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
6761 if (!data)
6762 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006763
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006764 if (rtl8169_align(data) != data) {
6765 kfree(data);
6766 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
6767 if (!data)
6768 return NULL;
6769 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006770
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006771 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006772 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006773 if (unlikely(dma_mapping_error(d, mapping))) {
6774 if (net_ratelimit())
6775 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006776 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778
6779 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006780 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006781
6782err_out:
6783 kfree(data);
6784 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785}
6786
6787static void rtl8169_rx_clear(struct rtl8169_private *tp)
6788{
Francois Romieu07d3f512007-02-21 22:40:46 +01006789 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790
6791 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006792 if (tp->Rx_databuff[i]) {
6793 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794 tp->RxDescArray + i);
6795 }
6796 }
6797}
6798
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006799static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006801 desc->opts1 |= cpu_to_le32(RingEnd);
6802}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006803
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006804static int rtl8169_rx_fill(struct rtl8169_private *tp)
6805{
6806 unsigned int i;
6807
6808 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006809 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006810
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006811 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02006813
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006814 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006815 if (!data) {
6816 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006817 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006818 }
6819 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006822 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6823 return 0;
6824
6825err_out:
6826 rtl8169_rx_clear(tp);
6827 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828}
6829
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830static int rtl8169_init_ring(struct net_device *dev)
6831{
6832 struct rtl8169_private *tp = netdev_priv(dev);
6833
6834 rtl8169_init_ring_indexes(tp);
6835
6836 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006837 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006839 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840}
6841
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006842static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843 struct TxDesc *desc)
6844{
6845 unsigned int len = tx_skb->len;
6846
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006847 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6848
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849 desc->opts1 = 0x00;
6850 desc->opts2 = 0x00;
6851 desc->addr = 0x00;
6852 tx_skb->len = 0;
6853}
6854
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006855static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6856 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857{
6858 unsigned int i;
6859
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006860 for (i = 0; i < n; i++) {
6861 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 struct ring_info *tx_skb = tp->tx_skb + entry;
6863 unsigned int len = tx_skb->len;
6864
6865 if (len) {
6866 struct sk_buff *skb = tx_skb->skb;
6867
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006868 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869 tp->TxDescArray + entry);
6870 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006871 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872 tx_skb->skb = NULL;
6873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874 }
6875 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006876}
6877
6878static void rtl8169_tx_clear(struct rtl8169_private *tp)
6879{
6880 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881 tp->cur_tx = tp->dirty_tx = 0;
6882}
6883
Francois Romieu4422bcd2012-01-26 11:23:32 +01006884static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885{
David Howellsc4028952006-11-22 14:57:56 +00006886 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01006887 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888
Francois Romieuda78dbf2012-01-26 14:18:23 +01006889 napi_disable(&tp->napi);
6890 netif_stop_queue(dev);
6891 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892
françois romieuc7c2c392011-12-04 20:30:52 +00006893 rtl8169_hw_reset(tp);
6894
Francois Romieu56de4142011-03-15 17:29:31 +01006895 for (i = 0; i < NUM_RX_DESC; i++)
6896 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
6897
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00006899 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900
Francois Romieuda78dbf2012-01-26 14:18:23 +01006901 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01006902 rtl_hw_start(dev);
6903 netif_wake_queue(dev);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006904 rtl8169_check_link_status(dev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905}
6906
6907static void rtl8169_tx_timeout(struct net_device *dev)
6908{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006909 struct rtl8169_private *tp = netdev_priv(dev);
6910
6911 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912}
6913
6914static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07006915 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916{
6917 struct skb_shared_info *info = skb_shinfo(skb);
6918 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006919 struct TxDesc *uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006920 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921
6922 entry = tp->cur_tx;
6923 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006924 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925 dma_addr_t mapping;
6926 u32 status, len;
6927 void *addr;
6928
6929 entry = (entry + 1) % NUM_TX_DESC;
6930
6931 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00006932 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00006933 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006934 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006935 if (unlikely(dma_mapping_error(d, mapping))) {
6936 if (net_ratelimit())
6937 netif_err(tp, drv, tp->dev,
6938 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006939 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941
Francois Romieucecb5fd2011-04-01 10:21:07 +02006942 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006943 status = opts[0] | len |
6944 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945
6946 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07006947 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 txd->addr = cpu_to_le64(mapping);
6949
6950 tp->tx_skb[entry].len = len;
6951 }
6952
6953 if (cur_frag) {
6954 tp->tx_skb[entry].skb = skb;
6955 txd->opts1 |= cpu_to_le32(LastFrag);
6956 }
6957
6958 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006959
6960err_out:
6961 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6962 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963}
6964
françois romieub423e9a2013-05-18 01:24:46 +00006965static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6966{
6967 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6968}
6969
hayeswange9746042014-07-11 16:25:58 +08006970static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6971 struct net_device *dev);
6972/* r8169_csum_workaround()
6973 * The hw limites the value the transport offset. When the offset is out of the
6974 * range, calculate the checksum by sw.
6975 */
6976static void r8169_csum_workaround(struct rtl8169_private *tp,
6977 struct sk_buff *skb)
6978{
6979 if (skb_shinfo(skb)->gso_size) {
6980 netdev_features_t features = tp->dev->features;
6981 struct sk_buff *segs, *nskb;
6982
6983 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6984 segs = skb_gso_segment(skb, features);
6985 if (IS_ERR(segs) || !segs)
6986 goto drop;
6987
6988 do {
6989 nskb = segs;
6990 segs = segs->next;
6991 nskb->next = NULL;
6992 rtl8169_start_xmit(nskb, tp->dev);
6993 } while (segs);
6994
Alexander Duyckeb781392015-05-01 10:34:44 -07006995 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006996 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6997 if (skb_checksum_help(skb) < 0)
6998 goto drop;
6999
7000 rtl8169_start_xmit(skb, tp->dev);
7001 } else {
7002 struct net_device_stats *stats;
7003
7004drop:
7005 stats = &tp->dev->stats;
7006 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07007007 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08007008 }
7009}
7010
7011/* msdn_giant_send_check()
7012 * According to the document of microsoft, the TCP Pseudo Header excludes the
7013 * packet length for IPv6 TCP large packets.
7014 */
7015static int msdn_giant_send_check(struct sk_buff *skb)
7016{
7017 const struct ipv6hdr *ipv6h;
7018 struct tcphdr *th;
7019 int ret;
7020
7021 ret = skb_cow_head(skb, 0);
7022 if (ret)
7023 return ret;
7024
7025 ipv6h = ipv6_hdr(skb);
7026 th = tcp_hdr(skb);
7027
7028 th->check = 0;
7029 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
7030
7031 return ret;
7032}
7033
7034static inline __be16 get_protocol(struct sk_buff *skb)
7035{
7036 __be16 protocol;
7037
7038 if (skb->protocol == htons(ETH_P_8021Q))
7039 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
7040 else
7041 protocol = skb->protocol;
7042
7043 return protocol;
7044}
7045
hayeswang5888d3f2014-07-11 16:25:56 +08007046static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
7047 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048{
Michał Mirosław350fb322011-04-08 06:35:56 +00007049 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050
Francois Romieu2b7b4312011-04-18 22:53:24 -07007051 if (mss) {
7052 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08007053 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
7054 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7055 const struct iphdr *ip = ip_hdr(skb);
7056
7057 if (ip->protocol == IPPROTO_TCP)
7058 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
7059 else if (ip->protocol == IPPROTO_UDP)
7060 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
7061 else
7062 WARN_ON_ONCE(1);
7063 }
7064
7065 return true;
7066}
7067
7068static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
7069 struct sk_buff *skb, u32 *opts)
7070{
hayeswangbdfa4ed2014-07-11 16:25:57 +08007071 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08007072 u32 mss = skb_shinfo(skb)->gso_size;
7073
7074 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08007075 if (transport_offset > GTTCPHO_MAX) {
7076 netif_warn(tp, tx_err, tp->dev,
7077 "Invalid transport offset 0x%x for TSO\n",
7078 transport_offset);
7079 return false;
7080 }
7081
7082 switch (get_protocol(skb)) {
7083 case htons(ETH_P_IP):
7084 opts[0] |= TD1_GTSENV4;
7085 break;
7086
7087 case htons(ETH_P_IPV6):
7088 if (msdn_giant_send_check(skb))
7089 return false;
7090
7091 opts[0] |= TD1_GTSENV6;
7092 break;
7093
7094 default:
7095 WARN_ON_ONCE(1);
7096 break;
7097 }
7098
hayeswangbdfa4ed2014-07-11 16:25:57 +08007099 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08007100 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007101 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08007102 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103
françois romieub423e9a2013-05-18 01:24:46 +00007104 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007105 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00007106
hayeswange9746042014-07-11 16:25:58 +08007107 if (transport_offset > TCPHO_MAX) {
7108 netif_warn(tp, tx_err, tp->dev,
7109 "Invalid transport offset 0x%x\n",
7110 transport_offset);
7111 return false;
7112 }
7113
7114 switch (get_protocol(skb)) {
7115 case htons(ETH_P_IP):
7116 opts[1] |= TD1_IPv4_CS;
7117 ip_protocol = ip_hdr(skb)->protocol;
7118 break;
7119
7120 case htons(ETH_P_IPV6):
7121 opts[1] |= TD1_IPv6_CS;
7122 ip_protocol = ipv6_hdr(skb)->nexthdr;
7123 break;
7124
7125 default:
7126 ip_protocol = IPPROTO_RAW;
7127 break;
7128 }
7129
7130 if (ip_protocol == IPPROTO_TCP)
7131 opts[1] |= TD1_TCP_CS;
7132 else if (ip_protocol == IPPROTO_UDP)
7133 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007134 else
7135 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08007136
7137 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00007138 } else {
7139 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007140 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 }
hayeswang5888d3f2014-07-11 16:25:56 +08007142
françois romieub423e9a2013-05-18 01:24:46 +00007143 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144}
7145
Stephen Hemminger613573252009-08-31 19:50:58 +00007146static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7147 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148{
7149 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007150 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151 struct TxDesc *txd = tp->TxDescArray + entry;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007152 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153 dma_addr_t mapping;
7154 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007155 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007156 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02007157
Julien Ducourthial477206a2012-05-09 00:00:06 +02007158 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007159 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007160 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161 }
7162
7163 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007164 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165
françois romieub423e9a2013-05-18 01:24:46 +00007166 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
7167 opts[0] = DescOwn;
7168
hayeswange9746042014-07-11 16:25:58 +08007169 if (!tp->tso_csum(tp, skb, opts)) {
7170 r8169_csum_workaround(tp, skb);
7171 return NETDEV_TX_OK;
7172 }
françois romieub423e9a2013-05-18 01:24:46 +00007173
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007174 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007175 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007176 if (unlikely(dma_mapping_error(d, mapping))) {
7177 if (net_ratelimit())
7178 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007179 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181
7182 tp->tx_skb[entry].len = len;
7183 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184
Francois Romieu2b7b4312011-04-18 22:53:24 -07007185 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007186 if (frags < 0)
7187 goto err_dma_1;
7188 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07007189 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007190 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07007191 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007192 tp->tx_skb[entry].skb = skb;
7193 }
7194
Francois Romieu2b7b4312011-04-18 22:53:24 -07007195 txd->opts2 = cpu_to_le32(opts[1]);
7196
Richard Cochran5047fb52012-03-10 07:29:42 +00007197 skb_tx_timestamp(skb);
7198
Alexander Duycka0750132014-12-11 15:02:17 -08007199 /* Force memory writes to complete before releasing descriptor */
7200 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201
Francois Romieucecb5fd2011-04-01 10:21:07 +02007202 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007203 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204 txd->opts1 = cpu_to_le32(status);
7205
Alexander Duycka0750132014-12-11 15:02:17 -08007206 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00007207 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208
Alexander Duycka0750132014-12-11 15:02:17 -08007209 tp->cur_tx += frags + 1;
7210
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007211 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212
David S. Miller87cda7c2015-02-22 15:54:29 -05007213 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01007214
David S. Miller87cda7c2015-02-22 15:54:29 -05007215 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01007216 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
7217 * not miss a ring update when it notices a stopped queue.
7218 */
7219 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01007221 /* Sync with rtl_tx:
7222 * - publish queue status and cur_tx ring index (write barrier)
7223 * - refresh dirty_tx ring index (read barrier).
7224 * May the current thread have a pessimistic view of the ring
7225 * status and forget to wake up queue, a racing rtl_tx thread
7226 * can't.
7227 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007228 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02007229 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230 netif_wake_queue(dev);
7231 }
7232
Stephen Hemminger613573252009-08-31 19:50:58 +00007233 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007235err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007236 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007237err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07007238 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007239 dev->stats.tx_dropped++;
7240 return NETDEV_TX_OK;
7241
7242err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007244 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00007245 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246}
7247
7248static void rtl8169_pcierr_interrupt(struct net_device *dev)
7249{
7250 struct rtl8169_private *tp = netdev_priv(dev);
7251 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252 u16 pci_status, pci_cmd;
7253
7254 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
7255 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
7256
Joe Perchesbf82c182010-02-09 11:49:50 +00007257 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
7258 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259
7260 /*
7261 * The recovery sequence below admits a very elaborated explanation:
7262 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01007263 * - I did not see what else could be done;
7264 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265 *
7266 * Feel free to adjust to your needs.
7267 */
Francois Romieua27993f2006-12-18 00:04:19 +01007268 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01007269 pci_cmd &= ~PCI_COMMAND_PARITY;
7270 else
7271 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
7272
7273 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274
7275 pci_write_config_word(pdev, PCI_STATUS,
7276 pci_status & (PCI_STATUS_DETECTED_PARITY |
7277 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
7278 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
7279
7280 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00007281 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007282 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 tp->cp_cmd &= ~PCIDAC;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007284 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286 }
7287
françois romieue6de30d2011-01-03 15:08:37 +00007288 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01007289
Francois Romieu98ddf982012-01-31 10:47:34 +01007290 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291}
7292
Francois Romieuda78dbf2012-01-26 14:18:23 +01007293static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294{
7295 unsigned int dirty_tx, tx_left;
7296
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297 dirty_tx = tp->dirty_tx;
7298 smp_rmb();
7299 tx_left = tp->cur_tx - dirty_tx;
7300
7301 while (tx_left > 0) {
7302 unsigned int entry = dirty_tx % NUM_TX_DESC;
7303 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304 u32 status;
7305
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7307 if (status & DescOwn)
7308 break;
7309
Alexander Duycka0750132014-12-11 15:02:17 -08007310 /* This barrier is needed to keep us from reading
7311 * any other fields out of the Tx descriptor until
7312 * we know the status of DescOwn
7313 */
7314 dma_rmb();
7315
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007316 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
7317 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05007319 u64_stats_update_begin(&tp->tx_stats.syncp);
7320 tp->tx_stats.packets++;
7321 tp->tx_stats.bytes += tx_skb->skb->len;
7322 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07007323 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324 tx_skb->skb = NULL;
7325 }
7326 dirty_tx++;
7327 tx_left--;
7328 }
7329
7330 if (tp->dirty_tx != dirty_tx) {
7331 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01007332 /* Sync with rtl8169_start_xmit:
7333 * - publish dirty_tx ring index (write barrier)
7334 * - refresh cur_tx ring index and queue status (read barrier)
7335 * May the current thread miss the stopped queue condition,
7336 * a racing xmit thread can only have a right view of the
7337 * ring status.
7338 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007339 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02007341 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342 netif_wake_queue(dev);
7343 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02007344 /*
7345 * 8168 hack: TxPoll requests are lost when the Tx packets are
7346 * too close. Let's kick an extra TxPoll request when a burst
7347 * of start_xmit activity is detected (if it is not detected,
7348 * it is slow enough). -- FR
7349 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007350 if (tp->cur_tx != dirty_tx)
7351 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352 }
7353}
7354
Francois Romieu126fa4b2005-05-12 20:09:17 -04007355static inline int rtl8169_fragmented_frame(u32 status)
7356{
7357 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7358}
7359
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007360static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362 u32 status = opts1 & RxProtoMask;
7363
7364 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00007365 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366 skb->ip_summed = CHECKSUM_UNNECESSARY;
7367 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07007368 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369}
7370
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007371static struct sk_buff *rtl8169_try_rx_copy(void *data,
7372 struct rtl8169_private *tp,
7373 int pkt_size,
7374 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02007376 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007377 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007379 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007380 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007381 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08007382 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007383 if (skb)
7384 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007385 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7386
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007387 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388}
7389
Francois Romieuda78dbf2012-01-26 14:18:23 +01007390static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391{
7392 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007393 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396
Timo Teräs9fba0812013-01-15 21:01:24 +00007397 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007399 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400 u32 status;
7401
David S. Miller8decf862011-09-22 03:23:13 -04007402 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403 if (status & DescOwn)
7404 break;
Alexander Duycka0750132014-12-11 15:02:17 -08007405
7406 /* This barrier is needed to keep us from reading
7407 * any other fields out of the Rx descriptor until
7408 * we know the status of DescOwn
7409 */
7410 dma_rmb();
7411
Richard Dawe4dcb7d32005-05-27 21:12:00 +02007412 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007413 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7414 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007415 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02007417 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02007419 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007420 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01007421 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007422 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007423 }
Ben Greear6bbe0212012-02-10 15:04:33 +00007424 if ((status & (RxRUNT | RxCRC)) &&
7425 !(status & (RxRWT | RxFOVF)) &&
7426 (dev->features & NETIF_F_RXALL))
7427 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007428 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007429 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00007430 dma_addr_t addr;
7431 int pkt_size;
7432
7433process_pkt:
7434 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00007435 if (likely(!(dev->features & NETIF_F_RXFCS)))
7436 pkt_size = (status & 0x00003fff) - 4;
7437 else
7438 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439
Francois Romieu126fa4b2005-05-12 20:09:17 -04007440 /*
7441 * The driver does not support incoming fragmented
7442 * frames. They are seen as a symptom of over-mtu
7443 * sized frames.
7444 */
7445 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02007446 dev->stats.rx_dropped++;
7447 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00007448 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007449 }
7450
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007451 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7452 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007453 if (!skb) {
7454 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00007455 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456 }
7457
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007458 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 skb_put(skb, pkt_size);
7460 skb->protocol = eth_type_trans(skb, dev);
7461
Francois Romieu7a8fc772011-03-01 17:18:33 +01007462 rtl8169_rx_vlan_tag(desc, skb);
7463
françois romieu39174292015-11-11 23:35:18 +01007464 if (skb->pkt_type == PACKET_MULTICAST)
7465 dev->stats.multicast++;
7466
Francois Romieu56de4142011-03-15 17:29:31 +01007467 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468
Junchang Wang8027aa22012-03-04 23:30:32 +01007469 u64_stats_update_begin(&tp->rx_stats.syncp);
7470 tp->rx_stats.packets++;
7471 tp->rx_stats.bytes += pkt_size;
7472 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473 }
françois romieuce11ff52013-01-24 13:30:06 +00007474release_descriptor:
7475 desc->opts2 = 0;
françois romieuce11ff52013-01-24 13:30:06 +00007476 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007477 }
7478
7479 count = cur_rx - tp->cur_rx;
7480 tp->cur_rx = cur_rx;
7481
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482 return count;
7483}
7484
Francois Romieu07d3f512007-02-21 22:40:46 +01007485static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007486{
Francois Romieu07d3f512007-02-21 22:40:46 +01007487 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007490 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007492 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007493 if (status && status != 0xffff) {
7494 status &= RTL_EVENT_NAPI | tp->event_slow;
7495 if (status) {
7496 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00007497
Francois Romieuda78dbf2012-01-26 14:18:23 +01007498 rtl_irq_disable(tp);
7499 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502 return IRQ_RETVAL(handled);
7503}
7504
Francois Romieuda78dbf2012-01-26 14:18:23 +01007505/*
7506 * Workqueue context.
7507 */
7508static void rtl_slow_event_work(struct rtl8169_private *tp)
7509{
7510 struct net_device *dev = tp->dev;
7511 u16 status;
7512
7513 status = rtl_get_events(tp) & tp->event_slow;
7514 rtl_ack_events(tp, status);
7515
7516 if (unlikely(status & RxFIFOOver)) {
7517 switch (tp->mac_version) {
7518 /* Work around for rx fifo overflow */
7519 case RTL_GIGA_MAC_VER_11:
7520 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01007521 /* XXX - Hack alert. See rtl_task(). */
7522 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007523 default:
7524 break;
7525 }
7526 }
7527
7528 if (unlikely(status & SYSErr))
7529 rtl8169_pcierr_interrupt(dev);
7530
7531 if (status & LinkChg)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007532 rtl8169_check_link_status(dev, tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007533
françois romieu7dbb4912012-06-09 10:53:16 +00007534 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007535}
7536
Francois Romieu4422bcd2012-01-26 11:23:32 +01007537static void rtl_task(struct work_struct *work)
7538{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007539 static const struct {
7540 int bitnr;
7541 void (*action)(struct rtl8169_private *);
7542 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01007543 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007544 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7545 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7546 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7547 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01007548 struct rtl8169_private *tp =
7549 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007550 struct net_device *dev = tp->dev;
7551 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01007552
Francois Romieuda78dbf2012-01-26 14:18:23 +01007553 rtl_lock_work(tp);
7554
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007555 if (!netif_running(dev) ||
7556 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01007557 goto out_unlock;
7558
7559 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7560 bool pending;
7561
Francois Romieuda78dbf2012-01-26 14:18:23 +01007562 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007563 if (pending)
7564 rtl_work[i].action(tp);
7565 }
7566
7567out_unlock:
7568 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01007569}
7570
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007571static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007573 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7574 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007575 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7576 int work_done= 0;
7577 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578
Francois Romieuda78dbf2012-01-26 14:18:23 +01007579 status = rtl_get_events(tp);
7580 rtl_ack_events(tp, status & ~tp->event_slow);
7581
7582 if (status & RTL_EVENT_NAPI_RX)
7583 work_done = rtl_rx(dev, tp, (u32) budget);
7584
7585 if (status & RTL_EVENT_NAPI_TX)
7586 rtl_tx(dev, tp);
7587
7588 if (status & tp->event_slow) {
7589 enable_mask &= ~tp->event_slow;
7590
7591 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007594 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08007595 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00007596
Francois Romieuda78dbf2012-01-26 14:18:23 +01007597 rtl_irq_enable(tp, enable_mask);
7598 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599 }
7600
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007601 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007602}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007603
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007604static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02007605{
7606 struct rtl8169_private *tp = netdev_priv(dev);
7607
7608 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7609 return;
7610
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007611 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
7612 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02007613}
7614
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615static void rtl8169_down(struct net_device *dev)
7616{
7617 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007618
Francois Romieu4876cc12011-03-11 21:07:11 +01007619 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007621 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007622 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623
Hayes Wang92fc43b2011-07-06 15:58:03 +08007624 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007625 /*
7626 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007627 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7628 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007629 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007630 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007633 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635 rtl8169_tx_clear(tp);
7636
7637 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007638
7639 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640}
7641
7642static int rtl8169_close(struct net_device *dev)
7643{
7644 struct rtl8169_private *tp = netdev_priv(dev);
7645 struct pci_dev *pdev = tp->pci_dev;
7646
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007647 pm_runtime_get_sync(&pdev->dev);
7648
Francois Romieucecb5fd2011-04-01 10:21:07 +02007649 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08007650 rtl8169_update_counters(dev);
7651
Francois Romieuda78dbf2012-01-26 14:18:23 +01007652 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007653 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007654
Linus Torvalds1da177e2005-04-16 15:20:36 -07007655 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007656 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007657
Lekensteyn4ea72442013-07-22 09:53:30 +02007658 cancel_work_sync(&tp->wk.work);
7659
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007660 pci_free_irq(pdev, 0, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007662 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7663 tp->RxPhyAddr);
7664 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7665 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666 tp->TxDescArray = NULL;
7667 tp->RxDescArray = NULL;
7668
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007669 pm_runtime_put_sync(&pdev->dev);
7670
Linus Torvalds1da177e2005-04-16 15:20:36 -07007671 return 0;
7672}
7673
Francois Romieudc1c00c2012-03-08 10:06:18 +01007674#ifdef CONFIG_NET_POLL_CONTROLLER
7675static void rtl8169_netpoll(struct net_device *dev)
7676{
7677 struct rtl8169_private *tp = netdev_priv(dev);
7678
Heiner Kallweit29274992018-02-28 20:43:38 +01007679 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), dev);
Francois Romieudc1c00c2012-03-08 10:06:18 +01007680}
7681#endif
7682
Francois Romieudf43ac72012-03-08 09:48:40 +01007683static int rtl_open(struct net_device *dev)
7684{
7685 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007686 struct pci_dev *pdev = tp->pci_dev;
7687 int retval = -ENOMEM;
7688
7689 pm_runtime_get_sync(&pdev->dev);
7690
7691 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007692 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007693 * dma_alloc_coherent provides more.
7694 */
7695 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7696 &tp->TxPhyAddr, GFP_KERNEL);
7697 if (!tp->TxDescArray)
7698 goto err_pm_runtime_put;
7699
7700 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7701 &tp->RxPhyAddr, GFP_KERNEL);
7702 if (!tp->RxDescArray)
7703 goto err_free_tx_0;
7704
7705 retval = rtl8169_init_ring(dev);
7706 if (retval < 0)
7707 goto err_free_rx_1;
7708
7709 INIT_WORK(&tp->wk.work, rtl_task);
7710
7711 smp_mb();
7712
7713 rtl_request_firmware(tp);
7714
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007715 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, dev,
7716 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01007717 if (retval < 0)
7718 goto err_release_fw_2;
7719
7720 rtl_lock_work(tp);
7721
7722 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7723
7724 napi_enable(&tp->napi);
7725
7726 rtl8169_init_phy(dev, tp);
7727
7728 __rtl8169_set_features(dev, dev->features);
7729
7730 rtl_pll_power_up(tp);
7731
7732 rtl_hw_start(dev);
7733
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007734 if (!rtl8169_init_counter_offsets(dev))
7735 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7736
Francois Romieudf43ac72012-03-08 09:48:40 +01007737 netif_start_queue(dev);
7738
7739 rtl_unlock_work(tp);
7740
7741 tp->saved_wolopts = 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007742 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007743
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007744 rtl8169_check_link_status(dev, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007745out:
7746 return retval;
7747
7748err_release_fw_2:
7749 rtl_release_firmware(tp);
7750 rtl8169_rx_clear(tp);
7751err_free_rx_1:
7752 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7753 tp->RxPhyAddr);
7754 tp->RxDescArray = NULL;
7755err_free_tx_0:
7756 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7757 tp->TxPhyAddr);
7758 tp->TxDescArray = NULL;
7759err_pm_runtime_put:
7760 pm_runtime_put_noidle(&pdev->dev);
7761 goto out;
7762}
7763
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007764static void
Junchang Wang8027aa22012-03-04 23:30:32 +01007765rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766{
7767 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007768 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02007769 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01007770 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007772 pm_runtime_get_noresume(&pdev->dev);
7773
7774 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007775 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007776
Junchang Wang8027aa22012-03-04 23:30:32 +01007777 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007778 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007779 stats->rx_packets = tp->rx_stats.packets;
7780 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007781 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007782
Junchang Wang8027aa22012-03-04 23:30:32 +01007783 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007784 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007785 stats->tx_packets = tp->tx_stats.packets;
7786 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007787 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007788
7789 stats->rx_dropped = dev->stats.rx_dropped;
7790 stats->tx_dropped = dev->stats.tx_dropped;
7791 stats->rx_length_errors = dev->stats.rx_length_errors;
7792 stats->rx_errors = dev->stats.rx_errors;
7793 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7794 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7795 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02007796 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01007797
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007798 /*
7799 * Fetch additonal counter values missing in stats collected by driver
7800 * from tally counters.
7801 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007802 if (pm_runtime_active(&pdev->dev))
7803 rtl8169_update_counters(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007804
7805 /*
7806 * Subtract values fetched during initalization.
7807 * See rtl8169_init_counter_offsets for a description why we do that.
7808 */
Corinna Vinschen42020322015-09-10 10:47:35 +02007809 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007810 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02007811 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007812 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02007813 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007814 le16_to_cpu(tp->tc_offset.tx_aborted);
7815
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007816 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007817}
7818
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007819static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01007820{
françois romieu065c27c2011-01-03 15:08:12 +00007821 struct rtl8169_private *tp = netdev_priv(dev);
7822
Francois Romieu5d06a992006-02-23 00:47:58 +01007823 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007824 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01007825
7826 netif_device_detach(dev);
7827 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007828
7829 rtl_lock_work(tp);
7830 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007831 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007832 rtl_unlock_work(tp);
7833
7834 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007835}
Francois Romieu5d06a992006-02-23 00:47:58 +01007836
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007837#ifdef CONFIG_PM
7838
7839static int rtl8169_suspend(struct device *device)
7840{
7841 struct pci_dev *pdev = to_pci_dev(device);
7842 struct net_device *dev = pci_get_drvdata(pdev);
7843
7844 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02007845
Francois Romieu5d06a992006-02-23 00:47:58 +01007846 return 0;
7847}
7848
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007849static void __rtl8169_resume(struct net_device *dev)
7850{
françois romieu065c27c2011-01-03 15:08:12 +00007851 struct rtl8169_private *tp = netdev_priv(dev);
7852
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007853 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00007854
7855 rtl_pll_power_up(tp);
7856
Artem Savkovcff4c162012-04-03 10:29:11 +00007857 rtl_lock_work(tp);
7858 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007859 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00007860 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007861
Francois Romieu98ddf982012-01-31 10:47:34 +01007862 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007863}
7864
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007865static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01007866{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007867 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01007868 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007869 struct rtl8169_private *tp = netdev_priv(dev);
7870
7871 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01007872
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007873 if (netif_running(dev))
7874 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01007875
Francois Romieu5d06a992006-02-23 00:47:58 +01007876 return 0;
7877}
7878
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007879static int rtl8169_runtime_suspend(struct device *device)
7880{
7881 struct pci_dev *pdev = to_pci_dev(device);
7882 struct net_device *dev = pci_get_drvdata(pdev);
7883 struct rtl8169_private *tp = netdev_priv(dev);
7884
Heiner Kallweita92a0842018-01-08 21:39:13 +01007885 if (!tp->TxDescArray) {
7886 rtl_pll_power_down(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007887 return 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007888 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007889
Francois Romieuda78dbf2012-01-26 14:18:23 +01007890 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007891 tp->saved_wolopts = __rtl8169_get_wol(tp);
7892 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007893 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007894
7895 rtl8169_net_suspend(dev);
7896
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007897 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007898 rtl8169_rx_missed(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007899 rtl8169_update_counters(dev);
7900
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007901 return 0;
7902}
7903
7904static int rtl8169_runtime_resume(struct device *device)
7905{
7906 struct pci_dev *pdev = to_pci_dev(device);
7907 struct net_device *dev = pci_get_drvdata(pdev);
7908 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08007909 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007910
7911 if (!tp->TxDescArray)
7912 return 0;
7913
Francois Romieuda78dbf2012-01-26 14:18:23 +01007914 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007915 __rtl8169_set_wol(tp, tp->saved_wolopts);
7916 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007917 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007918
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007919 rtl8169_init_phy(dev, tp);
7920
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007921 __rtl8169_resume(dev);
7922
7923 return 0;
7924}
7925
7926static int rtl8169_runtime_idle(struct device *device)
7927{
7928 struct pci_dev *pdev = to_pci_dev(device);
7929 struct net_device *dev = pci_get_drvdata(pdev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007930
Heiner Kallweita92a0842018-01-08 21:39:13 +01007931 if (!netif_running(dev) || !netif_carrier_ok(dev))
7932 pm_schedule_suspend(device, 10000);
7933
7934 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007935}
7936
Alexey Dobriyan47145212009-12-14 18:00:08 -08007937static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02007938 .suspend = rtl8169_suspend,
7939 .resume = rtl8169_resume,
7940 .freeze = rtl8169_suspend,
7941 .thaw = rtl8169_resume,
7942 .poweroff = rtl8169_suspend,
7943 .restore = rtl8169_resume,
7944 .runtime_suspend = rtl8169_runtime_suspend,
7945 .runtime_resume = rtl8169_runtime_resume,
7946 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007947};
7948
7949#define RTL8169_PM_OPS (&rtl8169_pm_ops)
7950
7951#else /* !CONFIG_PM */
7952
7953#define RTL8169_PM_OPS NULL
7954
7955#endif /* !CONFIG_PM */
7956
David S. Miller1805b2f2011-10-24 18:18:09 -04007957static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7958{
David S. Miller1805b2f2011-10-24 18:18:09 -04007959 /* WoL fails with 8168b when the receiver is disabled. */
7960 switch (tp->mac_version) {
7961 case RTL_GIGA_MAC_VER_11:
7962 case RTL_GIGA_MAC_VER_12:
7963 case RTL_GIGA_MAC_VER_17:
7964 pci_clear_master(tp->pci_dev);
7965
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007966 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007967 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007968 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04007969 break;
7970 default:
7971 break;
7972 }
7973}
7974
Francois Romieu1765f952008-09-13 17:21:40 +02007975static void rtl_shutdown(struct pci_dev *pdev)
7976{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007977 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00007978 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02007979
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007980 rtl8169_net_suspend(dev);
7981
Francois Romieucecb5fd2011-04-01 10:21:07 +02007982 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08007983 rtl_rar_set(tp, dev->perm_addr);
7984
Hayes Wang92fc43b2011-07-06 15:58:03 +08007985 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00007986
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007987 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04007988 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
7989 rtl_wol_suspend_quirk(tp);
7990 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00007991 }
7992
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007993 pci_wake_from_d3(pdev, true);
7994 pci_set_power_state(pdev, PCI_D3hot);
7995 }
7996}
Francois Romieu5d06a992006-02-23 00:47:58 +01007997
Bill Pembertonbaf63292012-12-03 09:23:28 -05007998static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01007999{
8000 struct net_device *dev = pci_get_drvdata(pdev);
8001 struct rtl8169_private *tp = netdev_priv(dev);
8002
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01008003 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01008004 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01008005
Devendra Nagaad1be8d2012-05-31 01:51:20 +00008006 netif_napi_del(&tp->napi);
8007
Francois Romieue27566e2012-03-08 09:54:01 +01008008 unregister_netdev(dev);
8009
8010 rtl_release_firmware(tp);
8011
8012 if (pci_dev_run_wake(pdev))
8013 pm_runtime_get_noresume(&pdev->dev);
8014
8015 /* restore original MAC address */
8016 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01008017}
8018
Francois Romieufa9c3852012-03-08 10:01:50 +01008019static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01008020 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01008021 .ndo_stop = rtl8169_close,
8022 .ndo_get_stats64 = rtl8169_get_stats64,
8023 .ndo_start_xmit = rtl8169_start_xmit,
8024 .ndo_tx_timeout = rtl8169_tx_timeout,
8025 .ndo_validate_addr = eth_validate_addr,
8026 .ndo_change_mtu = rtl8169_change_mtu,
8027 .ndo_fix_features = rtl8169_fix_features,
8028 .ndo_set_features = rtl8169_set_features,
8029 .ndo_set_mac_address = rtl_set_mac_address,
8030 .ndo_do_ioctl = rtl8169_ioctl,
8031 .ndo_set_rx_mode = rtl_set_rx_mode,
8032#ifdef CONFIG_NET_POLL_CONTROLLER
8033 .ndo_poll_controller = rtl8169_netpoll,
8034#endif
8035
8036};
8037
Francois Romieu31fa8b12012-03-08 10:09:40 +01008038static const struct rtl_cfg_info {
8039 void (*hw_start)(struct net_device *);
8040 unsigned int region;
8041 unsigned int align;
8042 u16 event_slow;
Heiner Kallweit14967f92018-02-28 07:55:20 +01008043 unsigned int has_gmii:1;
Francois Romieu50970832017-10-27 13:24:49 +03008044 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008045 u8 default_ver;
8046} rtl_cfg_infos [] = {
8047 [RTL_CFG_0] = {
8048 .hw_start = rtl_hw_start_8169,
8049 .region = 1,
8050 .align = 0,
8051 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Heiner Kallweit14967f92018-02-28 07:55:20 +01008052 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03008053 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008054 .default_ver = RTL_GIGA_MAC_VER_01,
8055 },
8056 [RTL_CFG_1] = {
8057 .hw_start = rtl_hw_start_8168,
8058 .region = 2,
8059 .align = 8,
8060 .event_slow = SYSErr | LinkChg | RxOverflow,
Heiner Kallweit14967f92018-02-28 07:55:20 +01008061 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03008062 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008063 .default_ver = RTL_GIGA_MAC_VER_11,
8064 },
8065 [RTL_CFG_2] = {
8066 .hw_start = rtl_hw_start_8101,
8067 .region = 2,
8068 .align = 8,
8069 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
8070 PCSTimeout,
Francois Romieu50970832017-10-27 13:24:49 +03008071 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008072 .default_ver = RTL_GIGA_MAC_VER_13,
8073 }
8074};
8075
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008076static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01008077{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008078 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008079
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008080 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008081 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
8082 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
8083 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008084 flags = PCI_IRQ_LEGACY;
8085 } else {
8086 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008087 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008088
8089 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01008090}
8091
Hayes Wangc5583862012-07-02 17:23:22 +08008092DECLARE_RTL_COND(rtl_link_list_ready_cond)
8093{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008094 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08008095}
8096
8097DECLARE_RTL_COND(rtl_rxtx_empty_cond)
8098{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008099 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08008100}
8101
Bill Pembertonbaf63292012-12-03 09:23:28 -05008102static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008103{
Hayes Wangc5583862012-07-02 17:23:22 +08008104 u32 data;
8105
8106 tp->ocp_base = OCP_STD_PHY_BASE;
8107
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008108 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08008109
8110 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
8111 return;
8112
8113 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
8114 return;
8115
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008116 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08008117 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008118 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08008119
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008120 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008121 data &= ~(1 << 14);
8122 r8168_mac_ocp_write(tp, 0xe8de, data);
8123
8124 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8125 return;
8126
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008127 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008128 data |= (1 << 15);
8129 r8168_mac_ocp_write(tp, 0xe8de, data);
8130
8131 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8132 return;
8133}
8134
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008135static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
8136{
8137 rtl8168ep_stop_cmac(tp);
8138 rtl_hw_init_8168g(tp);
8139}
8140
Bill Pembertonbaf63292012-12-03 09:23:28 -05008141static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008142{
8143 switch (tp->mac_version) {
8144 case RTL_GIGA_MAC_VER_40:
8145 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00008146 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00008147 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08008148 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008149 case RTL_GIGA_MAC_VER_45:
8150 case RTL_GIGA_MAC_VER_46:
8151 case RTL_GIGA_MAC_VER_47:
8152 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008153 rtl_hw_init_8168g(tp);
8154 break;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008155 case RTL_GIGA_MAC_VER_49:
8156 case RTL_GIGA_MAC_VER_50:
8157 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008158 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08008159 break;
Hayes Wangc5583862012-07-02 17:23:22 +08008160 default:
8161 break;
8162 }
8163}
8164
hayeswang929a0312014-09-16 11:40:47 +08008165static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008166{
8167 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
8168 const unsigned int region = cfg->region;
8169 struct rtl8169_private *tp;
8170 struct mii_if_info *mii;
8171 struct net_device *dev;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008172 int chipset, i;
8173 int rc;
8174
8175 if (netif_msg_drv(&debug)) {
8176 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
8177 MODULENAME, RTL8169_VERSION);
8178 }
8179
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008180 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
8181 if (!dev)
8182 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008183
8184 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01008185 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008186 tp = netdev_priv(dev);
8187 tp->dev = dev;
8188 tp->pci_dev = pdev;
8189 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
8190
8191 mii = &tp->mii;
8192 mii->dev = dev;
8193 mii->mdio_read = rtl_mdio_read;
8194 mii->mdio_write = rtl_mdio_write;
8195 mii->phy_id_mask = 0x1f;
8196 mii->reg_num_mask = 0x1f;
Heiner Kallweit14967f92018-02-28 07:55:20 +01008197 mii->supports_gmii = cfg->has_gmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008198
8199 /* disable ASPM completely as that cause random device stop working
8200 * problems as well as full system hangs for some PCIe devices users */
8201 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
8202 PCIE_LINK_STATE_CLKPM);
8203
8204 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008205 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008206 if (rc < 0) {
8207 netif_err(tp, probe, dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008208 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008209 }
8210
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008211 if (pcim_set_mwi(pdev) < 0)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008212 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
8213
8214 /* make sure PCI base addr 1 is MMIO */
8215 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
8216 netif_err(tp, probe, dev,
8217 "region #%d not an MMIO resource, aborting\n",
8218 region);
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008219 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008220 }
8221
8222 /* check for weird/broken PCI region reporting */
8223 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
8224 netif_err(tp, probe, dev,
8225 "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008226 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008227 }
8228
Andy Shevchenko93a00d42018-03-01 13:27:35 +02008229 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008230 if (rc < 0) {
Andy Shevchenko93a00d42018-03-01 13:27:35 +02008231 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008232 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008233 }
8234
Andy Shevchenko93a00d42018-03-01 13:27:35 +02008235 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01008236
8237 if (!pci_is_pcie(pdev))
8238 netif_info(tp, probe, dev, "not PCI Express\n");
8239
8240 /* Identify chip attached to board */
8241 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
8242
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008243 tp->cp_cmd = 0;
8244
8245 if ((sizeof(dma_addr_t) > 4) &&
8246 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
8247 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01008248 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
8249 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008250
8251 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
8252 if (!pci_is_pcie(pdev))
8253 tp->cp_cmd |= PCIDAC;
8254 dev->features |= NETIF_F_HIGHDMA;
8255 } else {
8256 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8257 if (rc < 0) {
8258 netif_err(tp, probe, dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008259 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008260 }
8261 }
8262
Francois Romieu3b6cf252012-03-08 09:59:04 +01008263 rtl_init_rxcfg(tp);
8264
8265 rtl_irq_disable(tp);
8266
Hayes Wangc5583862012-07-02 17:23:22 +08008267 rtl_hw_initialize(tp);
8268
Francois Romieu3b6cf252012-03-08 09:59:04 +01008269 rtl_hw_reset(tp);
8270
8271 rtl_ack_events(tp, 0xffff);
8272
8273 pci_set_master(pdev);
8274
Francois Romieu3b6cf252012-03-08 09:59:04 +01008275 rtl_init_mdio_ops(tp);
8276 rtl_init_pll_power_ops(tp);
8277 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08008278 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008279
8280 rtl8169_print_mac_version(tp);
8281
8282 chipset = tp->mac_version;
8283 tp->txd_version = rtl_chip_infos[chipset].txd_version;
8284
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008285 rc = rtl_alloc_irq(tp);
8286 if (rc < 0) {
8287 netif_err(tp, probe, dev, "Can't allocate interrupt\n");
8288 return rc;
8289 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008290
Heiner Kallweit7edf6d32018-02-22 21:22:40 +01008291 /* override BIOS settings, use userspace tools to enable WOL */
8292 __rtl8169_set_wol(tp, 0);
8293
Francois Romieu3b6cf252012-03-08 09:59:04 +01008294 if (rtl_tbi_enabled(tp)) {
8295 tp->set_speed = rtl8169_set_speed_tbi;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008296 tp->get_link_ksettings = rtl8169_get_link_ksettings_tbi;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008297 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
8298 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
8299 tp->link_ok = rtl8169_tbi_link_ok;
8300 tp->do_ioctl = rtl_tbi_ioctl;
8301 } else {
8302 tp->set_speed = rtl8169_set_speed_xmii;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008303 tp->get_link_ksettings = rtl8169_get_link_ksettings_xmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008304 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8305 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8306 tp->link_ok = rtl8169_xmii_link_ok;
8307 tp->do_ioctl = rtl_xmii_ioctl;
8308 }
8309
8310 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05008311 u64_stats_init(&tp->rx_stats.syncp);
8312 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008313
8314 /* Get MAC address */
Chun-Hao Lin89cceb22014-10-01 23:17:15 +08008315 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8316 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8317 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8318 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8319 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8320 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8321 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8322 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8323 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8324 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008325 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8326 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008327 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8328 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8329 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8330 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008331 u16 mac_addr[3];
8332
Chun-Hao Lin05b96872014-10-01 23:17:12 +08008333 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8334 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008335
8336 if (is_valid_ether_addr((u8 *)mac_addr))
8337 rtl_rar_set(tp, (u8 *)mac_addr);
8338 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008339 for (i = 0; i < ETH_ALEN; i++)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008340 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008341
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00008342 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008343 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008344
8345 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
8346
8347 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8348 * properly for all devices */
8349 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00008350 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008351
8352 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00008353 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8354 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008355 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8356 NETIF_F_HIGHDMA;
8357
hayeswang929a0312014-09-16 11:40:47 +08008358 tp->cp_cmd |= RxChkSum | RxVlan;
8359
8360 /*
8361 * Pretend we are using VLANs; This bypasses a nasty bug where
8362 * Interrupts stop flowing on high load on 8110SCd controllers.
8363 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01008364 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08008365 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00008366 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008367
hayeswang5888d3f2014-07-11 16:25:56 +08008368 if (tp->txd_version == RTL_TD_0)
8369 tp->tso_csum = rtl8169_tso_csum_v1;
hayeswange9746042014-07-11 16:25:58 +08008370 else if (tp->txd_version == RTL_TD_1) {
hayeswang5888d3f2014-07-11 16:25:56 +08008371 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08008372 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
8373 } else
hayeswang5888d3f2014-07-11 16:25:56 +08008374 WARN_ON_ONCE(1);
8375
Francois Romieu3b6cf252012-03-08 09:59:04 +01008376 dev->hw_features |= NETIF_F_RXALL;
8377 dev->hw_features |= NETIF_F_RXFCS;
8378
Jarod Wilsonc7315a92016-10-17 15:54:09 -04008379 /* MTU range: 60 - hw-specific max */
8380 dev->min_mtu = ETH_ZLEN;
8381 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
8382
Francois Romieu3b6cf252012-03-08 09:59:04 +01008383 tp->hw_start = cfg->hw_start;
8384 tp->event_slow = cfg->event_slow;
Francois Romieu50970832017-10-27 13:24:49 +03008385 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008386
8387 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
8388 ~(RxBOVF | RxFOVF) : ~0;
8389
Kees Cook9de36cc2017-10-25 03:53:12 -07008390 timer_setup(&tp->timer, rtl8169_phy_timer, 0);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008391
8392 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8393
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008394 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
8395 &tp->counters_phys_addr,
8396 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01008397 if (!tp->counters)
8398 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02008399
Francois Romieu3b6cf252012-03-08 09:59:04 +01008400 rc = register_netdev(dev);
8401 if (rc < 0)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01008402 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008403
8404 pci_set_drvdata(pdev, dev);
8405
Francois Romieu92a7c4e2012-03-10 10:42:12 +01008406 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
Andy Shevchenko93a00d42018-03-01 13:27:35 +02008407 rtl_chip_infos[chipset].name, tp->mmio_addr, dev->dev_addr,
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008408 (u32)(RTL_R32(tp, TxConfig) & 0x9cf0f8ff),
Heiner Kallweit29274992018-02-28 20:43:38 +01008409 pci_irq_vector(pdev, 0));
Francois Romieu3b6cf252012-03-08 09:59:04 +01008410 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8411 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8412 "tx checksumming: %s]\n",
8413 rtl_chip_infos[chipset].jumbo_max,
8414 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
8415 }
8416
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01008417 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01008418 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008419
Francois Romieu3b6cf252012-03-08 09:59:04 +01008420 netif_carrier_off(dev);
8421
Heiner Kallweita92a0842018-01-08 21:39:13 +01008422 if (pci_dev_run_wake(pdev))
8423 pm_runtime_put_sync(&pdev->dev);
8424
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008425 return 0;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008426}
8427
Linus Torvalds1da177e2005-04-16 15:20:36 -07008428static struct pci_driver rtl8169_pci_driver = {
8429 .name = MODULENAME,
8430 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01008431 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05008432 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02008433 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008434 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008435};
8436
Devendra Naga3eeb7da2012-10-26 09:27:42 +00008437module_pci_driver(rtl8169_pci_driver);