blob: 6d99b141a7aa3a4665fcde29325afac57b0c99c5 [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 Romieuf0e837d92005-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
Michal Schmidtaee77e42012-09-09 13:55:26 +000087#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
89
90#define R8169_REGS_SIZE 256
Heiner Kallweit1d0254d2018-04-17 23:25:46 +020091#define R8169_RX_BUF_SIZE (SZ_16K - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000093#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
95#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
96
97#define RTL8169_TX_TIMEOUT (6*HZ)
98#define RTL8169_PHY_TIMEOUT (10*HZ)
99
100/* write/read MMIO register */
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200101#define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
102#define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
103#define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
104#define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
105#define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
106#define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200109 RTL_GIGA_MAC_VER_01 = 0,
110 RTL_GIGA_MAC_VER_02,
111 RTL_GIGA_MAC_VER_03,
112 RTL_GIGA_MAC_VER_04,
113 RTL_GIGA_MAC_VER_05,
114 RTL_GIGA_MAC_VER_06,
115 RTL_GIGA_MAC_VER_07,
116 RTL_GIGA_MAC_VER_08,
117 RTL_GIGA_MAC_VER_09,
118 RTL_GIGA_MAC_VER_10,
119 RTL_GIGA_MAC_VER_11,
120 RTL_GIGA_MAC_VER_12,
121 RTL_GIGA_MAC_VER_13,
122 RTL_GIGA_MAC_VER_14,
123 RTL_GIGA_MAC_VER_15,
124 RTL_GIGA_MAC_VER_16,
125 RTL_GIGA_MAC_VER_17,
126 RTL_GIGA_MAC_VER_18,
127 RTL_GIGA_MAC_VER_19,
128 RTL_GIGA_MAC_VER_20,
129 RTL_GIGA_MAC_VER_21,
130 RTL_GIGA_MAC_VER_22,
131 RTL_GIGA_MAC_VER_23,
132 RTL_GIGA_MAC_VER_24,
133 RTL_GIGA_MAC_VER_25,
134 RTL_GIGA_MAC_VER_26,
135 RTL_GIGA_MAC_VER_27,
136 RTL_GIGA_MAC_VER_28,
137 RTL_GIGA_MAC_VER_29,
138 RTL_GIGA_MAC_VER_30,
139 RTL_GIGA_MAC_VER_31,
140 RTL_GIGA_MAC_VER_32,
141 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800142 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800143 RTL_GIGA_MAC_VER_35,
144 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800145 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800146 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800147 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800148 RTL_GIGA_MAC_VER_40,
149 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000150 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000151 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800152 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800153 RTL_GIGA_MAC_VER_45,
154 RTL_GIGA_MAC_VER_46,
155 RTL_GIGA_MAC_VER_47,
156 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800157 RTL_GIGA_MAC_VER_49,
158 RTL_GIGA_MAC_VER_50,
159 RTL_GIGA_MAC_VER_51,
Francois Romieu85bffe62011-04-27 08:22:39 +0200160 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
Francois Romieu2b7b4312011-04-18 22:53:24 -0700163enum rtl_tx_desc_version {
164 RTL_TD_0 = 0,
165 RTL_TD_1 = 1,
166};
167
Francois Romieud58d46b2011-05-03 16:38:29 +0200168#define JUMBO_1K ETH_DATA_LEN
169#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
170#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
171#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
172#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
173
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200174#define _R(NAME,TD,FW,SZ) { \
Francois Romieud58d46b2011-05-03 16:38:29 +0200175 .name = NAME, \
176 .txd_version = TD, \
177 .fw_name = FW, \
178 .jumbo_max = SZ, \
Francois Romieud58d46b2011-05-03 16:38:29 +0200179}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800181static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700183 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200184 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200185 u16 jumbo_max;
Francois Romieu85bffe62011-04-27 08:22:39 +0200186} rtl_chip_infos[] = {
187 /* PCI devices. */
188 [RTL_GIGA_MAC_VER_01] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200189 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200190 [RTL_GIGA_MAC_VER_02] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200191 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200192 [RTL_GIGA_MAC_VER_03] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200193 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200194 [RTL_GIGA_MAC_VER_04] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200195 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200196 [RTL_GIGA_MAC_VER_05] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200197 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200198 [RTL_GIGA_MAC_VER_06] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200199 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200200 /* PCI-E devices. */
201 [RTL_GIGA_MAC_VER_07] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200202 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200203 [RTL_GIGA_MAC_VER_08] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200204 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200205 [RTL_GIGA_MAC_VER_09] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200206 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200207 [RTL_GIGA_MAC_VER_10] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200208 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200209 [RTL_GIGA_MAC_VER_11] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200210 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200211 [RTL_GIGA_MAC_VER_12] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200212 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200213 [RTL_GIGA_MAC_VER_13] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200214 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200215 [RTL_GIGA_MAC_VER_14] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200216 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200217 [RTL_GIGA_MAC_VER_15] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200218 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200219 [RTL_GIGA_MAC_VER_16] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200220 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200221 [RTL_GIGA_MAC_VER_17] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200222 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200223 [RTL_GIGA_MAC_VER_18] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200224 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200225 [RTL_GIGA_MAC_VER_19] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200226 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200227 [RTL_GIGA_MAC_VER_20] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200228 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200229 [RTL_GIGA_MAC_VER_21] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200230 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200231 [RTL_GIGA_MAC_VER_22] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200232 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200233 [RTL_GIGA_MAC_VER_23] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200234 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200235 [RTL_GIGA_MAC_VER_24] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200236 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200237 [RTL_GIGA_MAC_VER_25] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200238 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200239 [RTL_GIGA_MAC_VER_26] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200240 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200241 [RTL_GIGA_MAC_VER_27] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200242 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200243 [RTL_GIGA_MAC_VER_28] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200244 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200245 [RTL_GIGA_MAC_VER_29] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200246 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200247 [RTL_GIGA_MAC_VER_30] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200248 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200249 [RTL_GIGA_MAC_VER_31] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200250 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200251 [RTL_GIGA_MAC_VER_32] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200252 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200253 [RTL_GIGA_MAC_VER_33] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200254 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2, JUMBO_9K),
Hayes Wang70090422011-07-06 15:58:06 +0800255 [RTL_GIGA_MAC_VER_34] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200256 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3, JUMBO_9K),
Hayes Wangc2218922011-09-06 16:55:18 +0800257 [RTL_GIGA_MAC_VER_35] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200258 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1, JUMBO_9K),
Hayes Wangc2218922011-09-06 16:55:18 +0800259 [RTL_GIGA_MAC_VER_36] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200260 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2, JUMBO_9K),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800261 [RTL_GIGA_MAC_VER_37] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200262 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1, JUMBO_1K),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800263 [RTL_GIGA_MAC_VER_38] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200264 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1, JUMBO_9K),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800265 [RTL_GIGA_MAC_VER_39] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200266 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1, JUMBO_1K),
Hayes Wangc5583862012-07-02 17:23:22 +0800267 [RTL_GIGA_MAC_VER_40] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200268 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2, JUMBO_9K),
Hayes Wangc5583862012-07-02 17:23:22 +0800269 [RTL_GIGA_MAC_VER_41] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200270 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K),
hayeswang57538c42013-04-01 22:23:40 +0000271 [RTL_GIGA_MAC_VER_42] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200272 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3, JUMBO_9K),
hayeswang58152cd2013-04-01 22:23:42 +0000273 [RTL_GIGA_MAC_VER_43] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200274 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2, JUMBO_1K),
hayeswang45dd95c2013-07-08 17:09:01 +0800275 [RTL_GIGA_MAC_VER_44] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200276 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2, JUMBO_9K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800277 [RTL_GIGA_MAC_VER_45] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200278 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1, JUMBO_9K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800279 [RTL_GIGA_MAC_VER_46] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200280 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2, JUMBO_9K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800281 [RTL_GIGA_MAC_VER_47] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200282 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1, JUMBO_1K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800283 [RTL_GIGA_MAC_VER_48] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200284 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2, JUMBO_1K),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800285 [RTL_GIGA_MAC_VER_49] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200286 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800287 [RTL_GIGA_MAC_VER_50] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200288 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800289 [RTL_GIGA_MAC_VER_51] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200290 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291};
292#undef _R
293
Francois Romieubcf0bf92006-07-26 23:14:13 +0200294enum cfg_version {
295 RTL_CFG_0 = 0x00,
296 RTL_CFG_1,
297 RTL_CFG_2
298};
299
Benoit Taine9baa3c32014-08-08 15:56:03 +0200300static const struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200301 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200302 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Chun-Hao Lin610c9082016-12-27 16:29:43 +0800303 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
Francois Romieud81bf552006-09-20 21:31:20 +0200304 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100305 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200306 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200307 { PCI_VENDOR_ID_DLINK, 0x4300,
308 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200309 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000310 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200311 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200312 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
313 { PCI_VENDOR_ID_LINKSYS, 0x1032,
314 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100315 { 0x0001, 0x8168,
316 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 {0,},
318};
319
320MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
321
Ard Biesheuvel27896c82016-05-14 22:40:15 +0200322static int use_dac = -1;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200323static struct {
324 u32 msg_enable;
325} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Francois Romieu07d3f512007-02-21 22:40:46 +0100327enum rtl_registers {
328 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100329 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100330 MAR0 = 8, /* Multicast filter. */
331 CounterAddrLow = 0x10,
332 CounterAddrHigh = 0x14,
333 TxDescStartAddrLow = 0x20,
334 TxDescStartAddrHigh = 0x24,
335 TxHDescStartAddrLow = 0x28,
336 TxHDescStartAddrHigh = 0x2c,
337 FLASH = 0x30,
338 ERSR = 0x36,
339 ChipCmd = 0x37,
340 TxPoll = 0x38,
341 IntrMask = 0x3c,
342 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700343
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800344 TxConfig = 0x40,
345#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
346#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
347
348 RxConfig = 0x44,
349#define RX128_INT_EN (1 << 15) /* 8111c and later */
350#define RX_MULTI_EN (1 << 14) /* 8111c only */
351#define RXCFG_FIFO_SHIFT 13
352 /* No threshold before first PCI xfer */
353#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000354#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800355#define RXCFG_DMA_SHIFT 8
356 /* Unlimited maximum PCI burst. */
357#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700358
Francois Romieu07d3f512007-02-21 22:40:46 +0100359 RxMissed = 0x4c,
360 Cfg9346 = 0x50,
361 Config0 = 0x51,
362 Config1 = 0x52,
363 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200364#define PME_SIGNAL (1 << 5) /* 8168c and later */
365
Francois Romieu07d3f512007-02-21 22:40:46 +0100366 Config3 = 0x54,
367 Config4 = 0x55,
368 Config5 = 0x56,
369 MultiIntr = 0x5c,
370 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100371 PHYstatus = 0x6c,
372 RxMaxSize = 0xda,
373 CPlusCmd = 0xe0,
374 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300375
376#define RTL_COALESCE_MASK 0x0f
377#define RTL_COALESCE_SHIFT 4
378#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
379#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
380
Francois Romieu07d3f512007-02-21 22:40:46 +0100381 RxDescAddrLow = 0xe4,
382 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000383 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
384
385#define NoEarlyTx 0x3f /* Max value : no early transmit. */
386
387 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
388
389#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800390#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000391
Francois Romieu07d3f512007-02-21 22:40:46 +0100392 FuncEvent = 0xf0,
393 FuncEventMask = 0xf4,
394 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800395 IBCR0 = 0xf8,
396 IBCR2 = 0xf9,
397 IBIMR0 = 0xfa,
398 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100399 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400};
401
Francois Romieuf162a5d2008-06-01 22:37:49 +0200402enum rtl8110_registers {
403 TBICSR = 0x64,
404 TBI_ANAR = 0x68,
405 TBI_LPAR = 0x6a,
406};
407
408enum rtl8168_8101_registers {
409 CSIDR = 0x64,
410 CSIAR = 0x68,
411#define CSIAR_FLAG 0x80000000
412#define CSIAR_WRITE_CMD 0x80000000
Heiner Kallweitff1d7332018-05-02 21:39:56 +0200413#define CSIAR_BYTE_ENABLE 0x0000f000
414#define CSIAR_ADDR_MASK 0x00000fff
françois romieu065c27c2011-01-03 15:08:12 +0000415 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200416 EPHYAR = 0x80,
417#define EPHYAR_FLAG 0x80000000
418#define EPHYAR_WRITE_CMD 0x80000000
419#define EPHYAR_REG_MASK 0x1f
420#define EPHYAR_REG_SHIFT 16
421#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800422 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800423#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800424#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200425 DBG_REG = 0xd1,
426#define FIX_NAK_1 (1 << 4)
427#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800428 TWSI = 0xd2,
429 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800430#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800431#define TX_EMPTY (1 << 5)
432#define RX_EMPTY (1 << 4)
433#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800434#define EN_NDP (1 << 3)
435#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800436#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000437 EFUSEAR = 0xdc,
438#define EFUSEAR_FLAG 0x80000000
439#define EFUSEAR_WRITE_CMD 0x80000000
440#define EFUSEAR_READ_CMD 0x00000000
441#define EFUSEAR_REG_MASK 0x03ff
442#define EFUSEAR_REG_SHIFT 8
443#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800444 MISC_1 = 0xf2,
445#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200446};
447
françois romieuc0e45c12011-01-03 15:08:04 +0000448enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800449 LED_FREQ = 0x1a,
450 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000451 ERIDR = 0x70,
452 ERIAR = 0x74,
453#define ERIAR_FLAG 0x80000000
454#define ERIAR_WRITE_CMD 0x80000000
455#define ERIAR_READ_CMD 0x00000000
456#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000457#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800458#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
459#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
460#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800461#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800462#define ERIAR_MASK_SHIFT 12
463#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
464#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800465#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800466#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800467#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000468 EPHY_RXER_NUM = 0x7c,
469 OCPDR = 0xb0, /* OCP GPHY access */
470#define OCPDR_WRITE_CMD 0x80000000
471#define OCPDR_READ_CMD 0x00000000
472#define OCPDR_REG_MASK 0x7f
473#define OCPDR_GPHY_REG_SHIFT 16
474#define OCPDR_DATA_MASK 0xffff
475 OCPAR = 0xb4,
476#define OCPAR_FLAG 0x80000000
477#define OCPAR_GPHY_WRITE_CMD 0x8000f060
478#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800479 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000480 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
481 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200482#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800483#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800484#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800485#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800486#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000487};
488
Francois Romieu07d3f512007-02-21 22:40:46 +0100489enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100491 SYSErr = 0x8000,
492 PCSTimeout = 0x4000,
493 SWInt = 0x0100,
494 TxDescUnavail = 0x0080,
495 RxFIFOOver = 0x0040,
496 LinkChg = 0x0020,
497 RxOverflow = 0x0010,
498 TxErr = 0x0008,
499 TxOK = 0x0004,
500 RxErr = 0x0002,
501 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400504 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200505 RxFOVF = (1 << 23),
506 RxRWT = (1 << 22),
507 RxRES = (1 << 21),
508 RxRUNT = (1 << 20),
509 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800512 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100513 CmdReset = 0x10,
514 CmdRxEnb = 0x08,
515 CmdTxEnb = 0x04,
516 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Francois Romieu275391a2007-02-23 23:50:28 +0100518 /* TXPoll register p.5 */
519 HPQ = 0x80, /* Poll cmd on the high prio queue */
520 NPQ = 0x40, /* Poll cmd on the low prio queue */
521 FSWInt = 0x01, /* Forced software interrupt */
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100524 Cfg9346_Lock = 0x00,
525 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100528 AcceptErr = 0x20,
529 AcceptRunt = 0x10,
530 AcceptBroadcast = 0x08,
531 AcceptMulticast = 0x04,
532 AcceptMyPhys = 0x02,
533 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200534#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /* TxConfigBits */
537 TxInterFrameGapShift = 24,
538 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
539
Francois Romieu5d06a992006-02-23 00:47:58 +0100540 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200541 LEDS1 = (1 << 7),
542 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200543 Speed_down = (1 << 4),
544 MEMMAP = (1 << 3),
545 IOMAP = (1 << 2),
546 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100547 PMEnable = (1 << 0), /* Power Management Enable */
548
Francois Romieu6dccd162007-02-13 23:38:05 +0100549 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000550 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000551 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100552 PCI_Clock_66MHz = 0x01,
553 PCI_Clock_33MHz = 0x00,
554
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100555 /* Config3 register p.25 */
556 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
557 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200558 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800559 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200560 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100561
Francois Romieud58d46b2011-05-03 16:38:29 +0200562 /* Config4 register */
563 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
564
Francois Romieu5d06a992006-02-23 00:47:58 +0100565 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100566 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
567 MWF = (1 << 5), /* Accept Multicast wakeup frame */
568 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200569 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100570 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100571 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000572 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 /* TBICSR p.28 */
575 TBIReset = 0x80000000,
576 TBILoopback = 0x40000000,
577 TBINwEnable = 0x20000000,
578 TBINwRestart = 0x10000000,
579 TBILinkOk = 0x02000000,
580 TBINwComplete = 0x01000000,
581
582 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200583 EnableBist = (1 << 15), // 8168 8101
584 Mac_dbgo_oe = (1 << 14), // 8168 8101
585 Normal_mode = (1 << 13), // unused
586 Force_half_dup = (1 << 12), // 8168 8101
587 Force_rxflow_en = (1 << 11), // 8168 8101
588 Force_txflow_en = (1 << 10), // 8168 8101
589 Cxpl_dbg_sel = (1 << 9), // 8168 8101
590 ASF = (1 << 8), // 8168 8101
591 PktCntrDisable = (1 << 7), // 8168 8101
592 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 RxVlan = (1 << 6),
594 RxChkSum = (1 << 5),
595 PCIDAC = (1 << 4),
596 PCIMulRW = (1 << 3),
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +0200597#define INTT_MASK GENMASK(1, 0)
Francois Romieu0e485152007-02-20 00:00:26 +0100598 INTT_0 = 0x0000, // 8168
599 INTT_1 = 0x0001, // 8168
600 INTT_2 = 0x0002, // 8168
601 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100604 TBI_Enable = 0x80,
605 TxFlowCtrl = 0x40,
606 RxFlowCtrl = 0x20,
607 _1000bpsF = 0x10,
608 _100bps = 0x08,
609 _10bps = 0x04,
610 LinkStatus = 0x02,
611 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100614 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200615
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200616 /* ResetCounterCommand */
617 CounterReset = 0x1,
618
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200619 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100620 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800621
622 /* magic enable v2 */
623 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624};
625
Francois Romieu2b7b4312011-04-18 22:53:24 -0700626enum rtl_desc_bit {
627 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
629 RingEnd = (1 << 30), /* End of descriptor ring */
630 FirstFrag = (1 << 29), /* First segment of a packet */
631 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700632};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Francois Romieu2b7b4312011-04-18 22:53:24 -0700634/* Generic case. */
635enum rtl_tx_desc_bit {
636 /* First doubleword. */
637 TD_LSO = (1 << 27), /* Large Send Offload */
638#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Francois Romieu2b7b4312011-04-18 22:53:24 -0700640 /* Second doubleword. */
641 TxVlanTag = (1 << 17), /* Add VLAN tag */
642};
643
644/* 8169, 8168b and 810x except 8102e. */
645enum rtl_tx_desc_bit_0 {
646 /* First doubleword. */
647#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
648 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
649 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
650 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
651};
652
653/* 8102e, 8168c and beyond. */
654enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800655 /* First doubleword. */
656 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800657 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800658#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800659#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800660
Francois Romieu2b7b4312011-04-18 22:53:24 -0700661 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800662#define TCPHO_SHIFT 18
663#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700664#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800665 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
666 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700667 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
668 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
669};
670
Francois Romieu2b7b4312011-04-18 22:53:24 -0700671enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /* Rx private */
673 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500674 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676#define RxProtoUDP (PID1)
677#define RxProtoTCP (PID0)
678#define RxProtoIP (PID1 | PID0)
679#define RxProtoMask RxProtoIP
680
681 IPFail = (1 << 16), /* IP checksum failed */
682 UDPFail = (1 << 15), /* UDP/IP checksum failed */
683 TCPFail = (1 << 14), /* TCP/IP checksum failed */
684 RxVlanTag = (1 << 16), /* VLAN tag available */
685};
686
687#define RsvdMask 0x3fffc000
Heiner Kallweit12d42c52018-04-28 22:19:30 +0200688#define CPCMD_QUIRK_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200691 __le32 opts1;
692 __le32 opts2;
693 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694};
695
696struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200697 __le32 opts1;
698 __le32 opts2;
699 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700};
701
702struct ring_info {
703 struct sk_buff *skb;
704 u32 len;
705 u8 __pad[sizeof(void *) - sizeof(u32)];
706};
707
Ivan Vecera355423d2009-02-06 21:49:57 -0800708struct rtl8169_counters {
709 __le64 tx_packets;
710 __le64 rx_packets;
711 __le64 tx_errors;
712 __le32 rx_errors;
713 __le16 rx_missed;
714 __le16 align_errors;
715 __le32 tx_one_collision;
716 __le32 tx_multi_collision;
717 __le64 rx_unicast;
718 __le64 rx_broadcast;
719 __le32 rx_multicast;
720 __le16 tx_aborted;
721 __le16 tx_underun;
722};
723
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200724struct rtl8169_tc_offsets {
725 bool inited;
726 __le64 tx_errors;
727 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200728 __le16 tx_aborted;
729};
730
Francois Romieuda78dbf2012-01-26 14:18:23 +0100731enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100732 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100733 RTL_FLAG_TASK_SLOW_PENDING,
734 RTL_FLAG_TASK_RESET_PENDING,
735 RTL_FLAG_TASK_PHY_PENDING,
736 RTL_FLAG_MAX
737};
738
Junchang Wang8027aa22012-03-04 23:30:32 +0100739struct rtl8169_stats {
740 u64 packets;
741 u64 bytes;
742 struct u64_stats_sync syncp;
743};
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745struct rtl8169_private {
746 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200747 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000748 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700749 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200750 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700751 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
753 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100755 struct rtl8169_stats rx_stats;
756 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
758 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
759 dma_addr_t TxPhyAddr;
760 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000761 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 struct timer_list timer;
764 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100765
766 u16 event_slow;
Francois Romieu50970832017-10-27 13:24:49 +0300767 const struct rtl_coalesce_info *coalesce_info;
françois romieuc0e45c12011-01-03 15:08:04 +0000768
769 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200770 void (*write)(struct rtl8169_private *, int, int);
771 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000772 } mdio_ops;
773
Francois Romieud58d46b2011-05-03 16:38:29 +0200774 struct jumbo_ops {
775 void (*enable)(struct rtl8169_private *);
776 void (*disable)(struct rtl8169_private *);
777 } jumbo_ops;
778
Oliver Neukum54405cd2011-01-06 21:55:13 +0100779 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +0100780 int (*get_link_ksettings)(struct net_device *,
781 struct ethtool_link_ksettings *);
françois romieu4da19632011-01-03 15:07:55 +0000782 void (*phy_reset_enable)(struct rtl8169_private *tp);
Heiner Kallweit61cb5322018-04-17 23:27:38 +0200783 void (*hw_start)(struct rtl8169_private *tp);
françois romieu4da19632011-01-03 15:07:55 +0000784 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200785 unsigned int (*link_ok)(struct rtl8169_private *tp);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800786 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800787 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100788
789 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100790 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
791 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100792 struct work_struct work;
793 } wk;
794
Francois Romieuccdffb92008-07-26 14:26:06 +0200795 struct mii_if_info mii;
Corinna Vinschen42020322015-09-10 10:47:35 +0200796 dma_addr_t counters_phys_addr;
797 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200798 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000799 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000800
Francois Romieub6ffd972011-06-17 17:00:05 +0200801 struct rtl_fw {
802 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200803
804#define RTL_VER_SIZE 32
805
806 char version[RTL_VER_SIZE];
807
808 struct rtl_fw_phy_action {
809 __le32 *code;
810 size_t size;
811 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200812 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300813#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800814
815 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816};
817
Ralf Baechle979b6c12005-06-13 14:30:40 -0700818MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700821MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200822module_param_named(debug, debug.msg_enable, int, 0);
823MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824MODULE_LICENSE("GPL");
825MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000826MODULE_FIRMWARE(FIRMWARE_8168D_1);
827MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000828MODULE_FIRMWARE(FIRMWARE_8168E_1);
829MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400830MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800831MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800832MODULE_FIRMWARE(FIRMWARE_8168F_1);
833MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800834MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800835MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800836MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800837MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000838MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000839MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000840MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800841MODULE_FIRMWARE(FIRMWARE_8168H_1);
842MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200843MODULE_FIRMWARE(FIRMWARE_8107E_1);
844MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Heiner Kallweit1e1205b2018-03-20 07:45:42 +0100846static inline struct device *tp_to_dev(struct rtl8169_private *tp)
847{
848 return &tp->pci_dev->dev;
849}
850
Francois Romieuda78dbf2012-01-26 14:18:23 +0100851static void rtl_lock_work(struct rtl8169_private *tp)
852{
853 mutex_lock(&tp->wk.mutex);
854}
855
856static void rtl_unlock_work(struct rtl8169_private *tp)
857{
858 mutex_unlock(&tp->wk.mutex);
859}
860
Heiner Kallweitcb732002018-03-20 07:45:35 +0100861static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
Francois Romieud58d46b2011-05-03 16:38:29 +0200862{
Heiner Kallweitcb732002018-03-20 07:45:35 +0100863 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +0800864 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200865}
866
Francois Romieuffc46952012-07-06 14:19:23 +0200867struct rtl_cond {
868 bool (*check)(struct rtl8169_private *);
869 const char *msg;
870};
871
872static void rtl_udelay(unsigned int d)
873{
874 udelay(d);
875}
876
877static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
878 void (*delay)(unsigned int), unsigned int d, int n,
879 bool high)
880{
881 int i;
882
883 for (i = 0; i < n; i++) {
884 delay(d);
885 if (c->check(tp) == high)
886 return true;
887 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200888 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
889 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200890 return false;
891}
892
893static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
894 const struct rtl_cond *c,
895 unsigned int d, int n)
896{
897 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
898}
899
900static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
901 const struct rtl_cond *c,
902 unsigned int d, int n)
903{
904 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
905}
906
907static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
908 const struct rtl_cond *c,
909 unsigned int d, int n)
910{
911 return rtl_loop_wait(tp, c, msleep, d, n, true);
912}
913
914static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
915 const struct rtl_cond *c,
916 unsigned int d, int n)
917{
918 return rtl_loop_wait(tp, c, msleep, d, n, false);
919}
920
921#define DECLARE_RTL_COND(name) \
922static bool name ## _check(struct rtl8169_private *); \
923 \
924static const struct rtl_cond name = { \
925 .check = name ## _check, \
926 .msg = #name \
927}; \
928 \
929static bool name ## _check(struct rtl8169_private *tp)
930
Hayes Wangc5583862012-07-02 17:23:22 +0800931static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
932{
933 if (reg & 0xffff0001) {
934 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
935 return true;
936 }
937 return false;
938}
939
940DECLARE_RTL_COND(rtl_ocp_gphy_cond)
941{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200942 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
Hayes Wangc5583862012-07-02 17:23:22 +0800943}
944
945static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
946{
Hayes Wangc5583862012-07-02 17:23:22 +0800947 if (rtl_ocp_reg_failure(tp, reg))
948 return;
949
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200950 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800951
952 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
953}
954
955static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
956{
Hayes Wangc5583862012-07-02 17:23:22 +0800957 if (rtl_ocp_reg_failure(tp, reg))
958 return 0;
959
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200960 RTL_W32(tp, GPHY_OCP, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800961
962 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200963 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
Hayes Wangc5583862012-07-02 17:23:22 +0800964}
965
Hayes Wangc5583862012-07-02 17:23:22 +0800966static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
967{
Hayes Wangc5583862012-07-02 17:23:22 +0800968 if (rtl_ocp_reg_failure(tp, reg))
969 return;
970
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200971 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800972}
973
974static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
975{
Hayes Wangc5583862012-07-02 17:23:22 +0800976 if (rtl_ocp_reg_failure(tp, reg))
977 return 0;
978
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200979 RTL_W32(tp, OCPDR, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800980
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200981 return RTL_R32(tp, OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +0800982}
983
984#define OCP_STD_PHY_BASE 0xa400
985
986static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
987{
988 if (reg == 0x1f) {
989 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
990 return;
991 }
992
993 if (tp->ocp_base != OCP_STD_PHY_BASE)
994 reg -= 0x10;
995
996 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
997}
998
999static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1000{
1001 if (tp->ocp_base != OCP_STD_PHY_BASE)
1002 reg -= 0x10;
1003
1004 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1005}
1006
hayeswangeee37862013-04-01 22:23:38 +00001007static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1008{
1009 if (reg == 0x1f) {
1010 tp->ocp_base = value << 4;
1011 return;
1012 }
1013
1014 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1015}
1016
1017static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1018{
1019 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1020}
1021
Francois Romieuffc46952012-07-06 14:19:23 +02001022DECLARE_RTL_COND(rtl_phyar_cond)
1023{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001024 return RTL_R32(tp, PHYAR) & 0x80000000;
Francois Romieuffc46952012-07-06 14:19:23 +02001025}
1026
Francois Romieu24192212012-07-06 20:19:42 +02001027static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001029 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Francois Romieuffc46952012-07-06 14:19:23 +02001031 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001032 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001033 * According to hardware specs a 20us delay is required after write
1034 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001035 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001036 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
Francois Romieu24192212012-07-06 20:19:42 +02001039static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Francois Romieuffc46952012-07-06 14:19:23 +02001041 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001043 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Francois Romieuffc46952012-07-06 14:19:23 +02001045 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001046 RTL_R32(tp, PHYAR) & 0xffff : ~0;
Francois Romieuffc46952012-07-06 14:19:23 +02001047
Timo Teräs81a95f02010-06-09 17:31:48 -07001048 /*
1049 * According to hardware specs a 20us delay is required after read
1050 * complete indication, but before sending next command.
1051 */
1052 udelay(20);
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return value;
1055}
1056
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001057DECLARE_RTL_COND(rtl_ocpar_cond)
1058{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001059 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001060}
1061
Francois Romieu24192212012-07-06 20:19:42 +02001062static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001063{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001064 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1065 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
1066 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001067
Francois Romieuffc46952012-07-06 14:19:23 +02001068 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001069}
1070
Francois Romieu24192212012-07-06 20:19:42 +02001071static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001072{
Francois Romieu24192212012-07-06 20:19:42 +02001073 r8168dp_1_mdio_access(tp, reg,
1074 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001075}
1076
Francois Romieu24192212012-07-06 20:19:42 +02001077static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001078{
Francois Romieu24192212012-07-06 20:19:42 +02001079 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001080
1081 mdelay(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001082 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
1083 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001084
Francois Romieuffc46952012-07-06 14:19:23 +02001085 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001086 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001087}
1088
françois romieue6de30d2011-01-03 15:08:37 +00001089#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1090
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001091static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001092{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001093 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001094}
1095
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001096static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001097{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001098 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001099}
1100
Francois Romieu24192212012-07-06 20:19:42 +02001101static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001102{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001103 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001104
Francois Romieu24192212012-07-06 20:19:42 +02001105 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001106
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001107 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001108}
1109
Francois Romieu24192212012-07-06 20:19:42 +02001110static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001111{
1112 int value;
1113
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001114 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001115
Francois Romieu24192212012-07-06 20:19:42 +02001116 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001117
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001118 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001119
1120 return value;
1121}
1122
françois romieu4da19632011-01-03 15:07:55 +00001123static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001124{
Francois Romieu24192212012-07-06 20:19:42 +02001125 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001126}
1127
françois romieu4da19632011-01-03 15:07:55 +00001128static int rtl_readphy(struct rtl8169_private *tp, int location)
1129{
Francois Romieu24192212012-07-06 20:19:42 +02001130 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001131}
1132
1133static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1134{
1135 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1136}
1137
Chun-Hao Lin76564422014-10-01 23:17:17 +08001138static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001139{
1140 int val;
1141
françois romieu4da19632011-01-03 15:07:55 +00001142 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001143 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001144}
1145
Francois Romieuccdffb92008-07-26 14:26:06 +02001146static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1147 int val)
1148{
1149 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001150
françois romieu4da19632011-01-03 15:07:55 +00001151 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001152}
1153
1154static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1155{
1156 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001157
françois romieu4da19632011-01-03 15:07:55 +00001158 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001159}
1160
Francois Romieuffc46952012-07-06 14:19:23 +02001161DECLARE_RTL_COND(rtl_ephyar_cond)
1162{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001163 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001164}
1165
Francois Romieufdf6fc02012-07-06 22:40:38 +02001166static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001167{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001168 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
Francois Romieudacf8152008-08-02 20:44:13 +02001169 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1170
Francois Romieuffc46952012-07-06 14:19:23 +02001171 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1172
1173 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001174}
1175
Francois Romieufdf6fc02012-07-06 22:40:38 +02001176static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001177{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001178 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
Francois Romieudacf8152008-08-02 20:44:13 +02001179
Francois Romieuffc46952012-07-06 14:19:23 +02001180 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001181 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001182}
1183
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001184DECLARE_RTL_COND(rtl_eriar_cond)
1185{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001186 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001187}
1188
Francois Romieufdf6fc02012-07-06 22:40:38 +02001189static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1190 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001191{
Hayes Wang133ac402011-07-06 15:58:05 +08001192 BUG_ON((addr & 3) || (mask == 0));
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001193 RTL_W32(tp, ERIDR, val);
1194 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001195
Francois Romieuffc46952012-07-06 14:19:23 +02001196 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001197}
1198
Francois Romieufdf6fc02012-07-06 22:40:38 +02001199static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001200{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001201 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001202
Francois Romieuffc46952012-07-06 14:19:23 +02001203 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001204 RTL_R32(tp, ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001205}
1206
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001207static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001208 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001209{
1210 u32 val;
1211
Francois Romieufdf6fc02012-07-06 22:40:38 +02001212 val = rtl_eri_read(tp, addr, type);
1213 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001214}
1215
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001216static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1217{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001218 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001219 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001220 RTL_R32(tp, OCPDR) : ~0;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001221}
1222
1223static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1224{
1225 return rtl_eri_read(tp, reg, ERIAR_OOB);
1226}
1227
1228static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1229{
1230 switch (tp->mac_version) {
1231 case RTL_GIGA_MAC_VER_27:
1232 case RTL_GIGA_MAC_VER_28:
1233 case RTL_GIGA_MAC_VER_31:
1234 return r8168dp_ocp_read(tp, mask, reg);
1235 case RTL_GIGA_MAC_VER_49:
1236 case RTL_GIGA_MAC_VER_50:
1237 case RTL_GIGA_MAC_VER_51:
1238 return r8168ep_ocp_read(tp, mask, reg);
1239 default:
1240 BUG();
1241 return ~0;
1242 }
1243}
1244
1245static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1246 u32 data)
1247{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001248 RTL_W32(tp, OCPDR, data);
1249 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001250 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1251}
1252
1253static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1254 u32 data)
1255{
1256 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1257 data, ERIAR_OOB);
1258}
1259
1260static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1261{
1262 switch (tp->mac_version) {
1263 case RTL_GIGA_MAC_VER_27:
1264 case RTL_GIGA_MAC_VER_28:
1265 case RTL_GIGA_MAC_VER_31:
1266 r8168dp_ocp_write(tp, mask, reg, data);
1267 break;
1268 case RTL_GIGA_MAC_VER_49:
1269 case RTL_GIGA_MAC_VER_50:
1270 case RTL_GIGA_MAC_VER_51:
1271 r8168ep_ocp_write(tp, mask, reg, data);
1272 break;
1273 default:
1274 BUG();
1275 break;
1276 }
1277}
1278
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001279static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1280{
1281 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1282
1283 ocp_write(tp, 0x1, 0x30, 0x00000001);
1284}
1285
1286#define OOB_CMD_RESET 0x00
1287#define OOB_CMD_DRIVER_START 0x05
1288#define OOB_CMD_DRIVER_STOP 0x06
1289
1290static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1291{
1292 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1293}
1294
1295DECLARE_RTL_COND(rtl_ocp_read_cond)
1296{
1297 u16 reg;
1298
1299 reg = rtl8168_get_ocp_reg(tp);
1300
1301 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1302}
1303
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001304DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1305{
1306 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1307}
1308
1309DECLARE_RTL_COND(rtl_ocp_tx_cond)
1310{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001311 return RTL_R8(tp, IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001312}
1313
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001314static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1315{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001316 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001317 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001318 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1319 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001320}
1321
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001322static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001323{
1324 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001325 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1326}
1327
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001328static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1329{
1330 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1331 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1332 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1333}
1334
1335static void rtl8168_driver_start(struct rtl8169_private *tp)
1336{
1337 switch (tp->mac_version) {
1338 case RTL_GIGA_MAC_VER_27:
1339 case RTL_GIGA_MAC_VER_28:
1340 case RTL_GIGA_MAC_VER_31:
1341 rtl8168dp_driver_start(tp);
1342 break;
1343 case RTL_GIGA_MAC_VER_49:
1344 case RTL_GIGA_MAC_VER_50:
1345 case RTL_GIGA_MAC_VER_51:
1346 rtl8168ep_driver_start(tp);
1347 break;
1348 default:
1349 BUG();
1350 break;
1351 }
1352}
1353
1354static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1355{
1356 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1357 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1358}
1359
1360static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1361{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001362 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001363 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1364 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1365 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1366}
1367
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001368static void rtl8168_driver_stop(struct rtl8169_private *tp)
1369{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001370 switch (tp->mac_version) {
1371 case RTL_GIGA_MAC_VER_27:
1372 case RTL_GIGA_MAC_VER_28:
1373 case RTL_GIGA_MAC_VER_31:
1374 rtl8168dp_driver_stop(tp);
1375 break;
1376 case RTL_GIGA_MAC_VER_49:
1377 case RTL_GIGA_MAC_VER_50:
1378 case RTL_GIGA_MAC_VER_51:
1379 rtl8168ep_driver_stop(tp);
1380 break;
1381 default:
1382 BUG();
1383 break;
1384 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001385}
1386
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001387static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001388{
1389 u16 reg = rtl8168_get_ocp_reg(tp);
1390
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001391 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001392}
1393
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001394static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001395{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001396 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001397}
1398
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001399static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001400{
1401 switch (tp->mac_version) {
1402 case RTL_GIGA_MAC_VER_27:
1403 case RTL_GIGA_MAC_VER_28:
1404 case RTL_GIGA_MAC_VER_31:
1405 return r8168dp_check_dash(tp);
1406 case RTL_GIGA_MAC_VER_49:
1407 case RTL_GIGA_MAC_VER_50:
1408 case RTL_GIGA_MAC_VER_51:
1409 return r8168ep_check_dash(tp);
1410 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001411 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001412 }
1413}
1414
françois romieuc28aa382011-08-02 03:53:43 +00001415struct exgmac_reg {
1416 u16 addr;
1417 u16 mask;
1418 u32 val;
1419};
1420
Francois Romieufdf6fc02012-07-06 22:40:38 +02001421static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001422 const struct exgmac_reg *r, int len)
1423{
1424 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001425 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001426 r++;
1427 }
1428}
1429
Francois Romieuffc46952012-07-06 14:19:23 +02001430DECLARE_RTL_COND(rtl_efusear_cond)
1431{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001432 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001433}
1434
Francois Romieufdf6fc02012-07-06 22:40:38 +02001435static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001436{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001437 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
françois romieudaf9df62009-10-07 12:44:20 +00001438
Francois Romieuffc46952012-07-06 14:19:23 +02001439 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001440 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001441}
1442
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001443static u16 rtl_get_events(struct rtl8169_private *tp)
1444{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001445 return RTL_R16(tp, IntrStatus);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001446}
1447
1448static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1449{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001450 RTL_W16(tp, IntrStatus, bits);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001451 mmiowb();
1452}
1453
1454static void rtl_irq_disable(struct rtl8169_private *tp)
1455{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001456 RTL_W16(tp, IntrMask, 0);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001457 mmiowb();
1458}
1459
Francois Romieu3e990ff2012-01-26 12:50:01 +01001460static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1461{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001462 RTL_W16(tp, IntrMask, bits);
Francois Romieu3e990ff2012-01-26 12:50:01 +01001463}
1464
Francois Romieuda78dbf2012-01-26 14:18:23 +01001465#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1466#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1467#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1468
1469static void rtl_irq_enable_all(struct rtl8169_private *tp)
1470{
1471 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1472}
1473
françois romieu811fd302011-12-04 20:30:45 +00001474static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001476 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001477 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001478 RTL_R8(tp, ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
françois romieu4da19632011-01-03 15:07:55 +00001481static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001483 return RTL_R32(tp, TBICSR) & TBIReset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
françois romieu4da19632011-01-03 15:07:55 +00001486static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
françois romieu4da19632011-01-03 15:07:55 +00001488 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001491static unsigned int rtl8169_tbi_link_ok(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001493 return RTL_R32(tp, TBICSR) & TBILinkOk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001496static unsigned int rtl8169_xmii_link_ok(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001498 return RTL_R8(tp, PHYstatus) & LinkStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
françois romieu4da19632011-01-03 15:07:55 +00001501static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001503 RTL_W32(tp, TBICSR, RTL_R32(tp, TBICSR) | TBIReset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
françois romieu4da19632011-01-03 15:07:55 +00001506static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
1508 unsigned int val;
1509
françois romieu4da19632011-01-03 15:07:55 +00001510 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1511 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
Hayes Wang70090422011-07-06 15:58:06 +08001514static void rtl_link_chg_patch(struct rtl8169_private *tp)
1515{
Hayes Wang70090422011-07-06 15:58:06 +08001516 struct net_device *dev = tp->dev;
1517
1518 if (!netif_running(dev))
1519 return;
1520
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001521 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1522 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001523 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001524 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1525 ERIAR_EXGMAC);
1526 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1527 ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001528 } else if (RTL_R8(tp, PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001529 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1530 ERIAR_EXGMAC);
1531 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1532 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001533 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001534 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1535 ERIAR_EXGMAC);
1536 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1537 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001538 }
1539 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001540 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001541 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001542 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001543 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001544 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1545 tp->mac_version == RTL_GIGA_MAC_VER_36) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001546 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001547 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1548 ERIAR_EXGMAC);
1549 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1550 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001551 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001552 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1553 ERIAR_EXGMAC);
1554 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1555 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001556 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001557 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001558 if (RTL_R8(tp, PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001559 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1560 ERIAR_EXGMAC);
1561 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1562 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001563 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001564 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1565 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001566 }
Hayes Wang70090422011-07-06 15:58:06 +08001567 }
1568}
1569
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01001570static void rtl8169_check_link_status(struct net_device *dev,
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001571 struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001573 struct device *d = tp_to_dev(tp);
1574
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001575 if (tp->link_ok(tp)) {
Hayes Wang70090422011-07-06 15:58:06 +08001576 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001577 /* This is to cancel a scheduled suspend if there's one. */
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001578 pm_request_resume(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001580 if (net_ratelimit())
1581 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001582 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001584 netif_info(tp, ifdown, dev, "link down\n");
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001585 pm_runtime_idle(d);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001589#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1590
1591static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1592{
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001593 u8 options;
1594 u32 wolopts = 0;
1595
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001596 options = RTL_R8(tp, Config1);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001597 if (!(options & PMEnable))
1598 return 0;
1599
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001600 options = RTL_R8(tp, Config3);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001601 if (options & LinkUp)
1602 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001603 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001604 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1605 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001606 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1607 wolopts |= WAKE_MAGIC;
1608 break;
1609 default:
1610 if (options & MagicPacket)
1611 wolopts |= WAKE_MAGIC;
1612 break;
1613 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001614
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001615 options = RTL_R8(tp, Config5);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001616 if (options & UWF)
1617 wolopts |= WAKE_UCAST;
1618 if (options & BWF)
1619 wolopts |= WAKE_BCAST;
1620 if (options & MWF)
1621 wolopts |= WAKE_MCAST;
1622
1623 return wolopts;
1624}
1625
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001626static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1627{
1628 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001629 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001630
1631 pm_runtime_get_noresume(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001632
Francois Romieuda78dbf2012-01-26 14:18:23 +01001633 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001634
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001635 wol->supported = WAKE_ANY;
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001636 if (pm_runtime_active(d))
1637 wol->wolopts = __rtl8169_get_wol(tp);
1638 else
1639 wol->wolopts = tp->saved_wolopts;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001640
Francois Romieuda78dbf2012-01-26 14:18:23 +01001641 rtl_unlock_work(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001642
1643 pm_runtime_put_noidle(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001644}
1645
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001646static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001647{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001648 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001649 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001650 u32 opt;
1651 u16 reg;
1652 u8 mask;
1653 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001654 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001655 { WAKE_UCAST, Config5, UWF },
1656 { WAKE_BCAST, Config5, BWF },
1657 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001658 { WAKE_ANY, Config5, LanWake },
1659 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001660 };
Francois Romieu851e6022012-04-17 11:10:11 +02001661 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001662
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001663 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001664
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001665 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001666 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1667 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001668 tmp = ARRAY_SIZE(cfg) - 1;
1669 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001670 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001671 0x0dc,
1672 ERIAR_MASK_0100,
1673 MagicPacket_v2,
1674 0x0000,
1675 ERIAR_EXGMAC);
1676 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001677 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001678 0x0dc,
1679 ERIAR_MASK_0100,
1680 0x0000,
1681 MagicPacket_v2,
1682 ERIAR_EXGMAC);
1683 break;
1684 default:
1685 tmp = ARRAY_SIZE(cfg);
1686 break;
1687 }
1688
1689 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001690 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001691 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001692 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001693 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001694 }
1695
Francois Romieu851e6022012-04-17 11:10:11 +02001696 switch (tp->mac_version) {
1697 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001698 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001699 if (wolopts)
1700 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001701 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001702 break;
1703 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001704 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001705 if (wolopts)
1706 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001707 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001708 break;
1709 }
1710
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001711 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001712}
1713
1714static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1715{
1716 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001717 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001718
1719 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001720
Francois Romieuda78dbf2012-01-26 14:18:23 +01001721 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001722
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001723 if (pm_runtime_active(d))
1724 __rtl8169_set_wol(tp, wol->wolopts);
1725 else
1726 tp->saved_wolopts = wol->wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001727
1728 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001729
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001730 device_set_wakeup_enable(d, wol->wolopts);
françois romieuea809072010-11-08 13:23:58 +00001731
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001732 pm_runtime_put_noidle(d);
1733
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001734 return 0;
1735}
1736
Francois Romieu31bd2042011-04-26 18:58:59 +02001737static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1738{
Francois Romieu85bffe62011-04-27 08:22:39 +02001739 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001740}
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742static void rtl8169_get_drvinfo(struct net_device *dev,
1743 struct ethtool_drvinfo *info)
1744{
1745 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001746 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Rick Jones68aad782011-11-07 13:29:27 +00001748 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1749 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1750 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001751 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001752 if (!IS_ERR_OR_NULL(rtl_fw))
1753 strlcpy(info->fw_version, rtl_fw->version,
1754 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755}
1756
1757static int rtl8169_get_regs_len(struct net_device *dev)
1758{
1759 return R8169_REGS_SIZE;
1760}
1761
1762static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001763 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
1765 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 int ret = 0;
1767 u32 reg;
1768
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001769 reg = RTL_R32(tp, TBICSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1771 (duplex == DUPLEX_FULL)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001772 RTL_W32(tp, TBICSR, reg & ~(TBINwEnable | TBINwRestart));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 } else if (autoneg == AUTONEG_ENABLE)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001774 RTL_W32(tp, TBICSR, reg | TBINwEnable | TBINwRestart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001776 netif_warn(tp, link, dev,
1777 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 ret = -EOPNOTSUPP;
1779 }
1780
1781 return ret;
1782}
1783
1784static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001785 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
1787 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001788 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001789 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Hayes Wang716b50a2011-02-22 17:26:18 +08001791 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001794 int auto_nego;
1795
françois romieu4da19632011-01-03 15:07:55 +00001796 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001797 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1798 ADVERTISE_100HALF | ADVERTISE_100FULL);
1799
1800 if (adv & ADVERTISED_10baseT_Half)
1801 auto_nego |= ADVERTISE_10HALF;
1802 if (adv & ADVERTISED_10baseT_Full)
1803 auto_nego |= ADVERTISE_10FULL;
1804 if (adv & ADVERTISED_100baseT_Half)
1805 auto_nego |= ADVERTISE_100HALF;
1806 if (adv & ADVERTISED_100baseT_Full)
1807 auto_nego |= ADVERTISE_100FULL;
1808
françois romieu3577aa12009-05-19 10:46:48 +00001809 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1810
françois romieu4da19632011-01-03 15:07:55 +00001811 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001812 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1813
1814 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001815 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001816 if (adv & ADVERTISED_1000baseT_Half)
1817 giga_ctrl |= ADVERTISE_1000HALF;
1818 if (adv & ADVERTISED_1000baseT_Full)
1819 giga_ctrl |= ADVERTISE_1000FULL;
1820 } else if (adv & (ADVERTISED_1000baseT_Half |
1821 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001822 netif_info(tp, link, dev,
1823 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001824 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
françois romieu3577aa12009-05-19 10:46:48 +00001827 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001828
françois romieu4da19632011-01-03 15:07:55 +00001829 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1830 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001831 } else {
françois romieu3577aa12009-05-19 10:46:48 +00001832 if (speed == SPEED_10)
1833 bmcr = 0;
1834 else if (speed == SPEED_100)
1835 bmcr = BMCR_SPEED100;
1836 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001837 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001838
1839 if (duplex == DUPLEX_FULL)
1840 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001841 }
1842
françois romieu4da19632011-01-03 15:07:55 +00001843 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001844
Francois Romieucecb5fd2011-04-01 10:21:07 +02001845 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1846 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001847 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001848 rtl_writephy(tp, 0x17, 0x2138);
1849 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001850 } else {
françois romieu4da19632011-01-03 15:07:55 +00001851 rtl_writephy(tp, 0x17, 0x2108);
1852 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001853 }
1854 }
1855
Oliver Neukum54405cd2011-01-06 21:55:13 +01001856 rc = 0;
1857out:
1858 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
1861static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001862 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
1864 struct rtl8169_private *tp = netdev_priv(dev);
1865 int ret;
1866
Oliver Neukum54405cd2011-01-06 21:55:13 +01001867 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001868 if (ret < 0)
1869 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Francois Romieu4876cc12011-03-11 21:07:11 +01001871 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
Chun-Hao Linc4556972016-03-11 14:21:14 +08001872 (advertising & ADVERTISED_1000baseT_Full) &&
1873 !pci_is_pcie(tp->pci_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001875 }
1876out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return ret;
1878}
1879
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001880static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1881 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
Francois Romieud58d46b2011-05-03 16:38:29 +02001883 struct rtl8169_private *tp = netdev_priv(dev);
1884
Francois Romieu2b7b4312011-04-18 22:53:24 -07001885 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001886 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Francois Romieud58d46b2011-05-03 16:38:29 +02001888 if (dev->mtu > JUMBO_1K &&
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02001889 tp->mac_version > RTL_GIGA_MAC_VER_06)
Francois Romieud58d46b2011-05-03 16:38:29 +02001890 features &= ~NETIF_F_IP_CSUM;
1891
Michał Mirosław350fb322011-04-08 06:35:56 +00001892 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
Heiner Kallweita3984572018-04-28 22:19:15 +02001895static int rtl8169_set_features(struct net_device *dev,
1896 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001899 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Heiner Kallweita3984572018-04-28 22:19:15 +02001901 rtl_lock_work(tp);
1902
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001903 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001904 if (features & NETIF_F_RXALL)
1905 rx_config |= (AcceptErr | AcceptRunt);
1906 else
1907 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001909 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001910
hayeswang929a0312014-09-16 11:40:47 +08001911 if (features & NETIF_F_RXCSUM)
1912 tp->cp_cmd |= RxChkSum;
1913 else
1914 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001915
hayeswang929a0312014-09-16 11:40:47 +08001916 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1917 tp->cp_cmd |= RxVlan;
1918 else
1919 tp->cp_cmd &= ~RxVlan;
1920
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001921 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1922 RTL_R16(tp, CPlusCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Francois Romieuda78dbf2012-01-26 14:18:23 +01001924 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 return 0;
1927}
1928
Kirill Smelkov810f4892012-11-10 21:11:02 +04001929static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001931 return (skb_vlan_tag_present(skb)) ?
1932 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
Francois Romieu7a8fc772011-03-01 17:18:33 +01001935static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
1937 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Francois Romieu7a8fc772011-03-01 17:18:33 +01001939 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001940 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001943static int rtl8169_get_link_ksettings_tbi(struct net_device *dev,
1944 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
1946 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 u32 status;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001948 u32 supported, advertising;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001950 supported =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001952 cmd->base.port = PORT_FIBRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001954 status = RTL_R32(tp, TBICSR);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001955 advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1956 cmd->base.autoneg = !!(status & TBINwEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001958 cmd->base.speed = SPEED_1000;
1959 cmd->base.duplex = DUPLEX_FULL; /* Always set */
1960
1961 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1962 supported);
1963 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1964 advertising);
Francois Romieuccdffb92008-07-26 14:26:06 +02001965
1966 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001969static int rtl8169_get_link_ksettings_xmii(struct net_device *dev,
1970 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
1972 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03001974 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
1975
1976 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001979static int rtl8169_get_link_ksettings(struct net_device *dev,
1980 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001983 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Francois Romieuda78dbf2012-01-26 14:18:23 +01001985 rtl_lock_work(tp);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001986 rc = tp->get_link_ksettings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001987 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Francois Romieuccdffb92008-07-26 14:26:06 +02001989 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
1991
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01001992static int rtl8169_set_link_ksettings(struct net_device *dev,
1993 const struct ethtool_link_ksettings *cmd)
1994{
1995 struct rtl8169_private *tp = netdev_priv(dev);
1996 int rc;
1997 u32 advertising;
1998
1999 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
2000 cmd->link_modes.advertising))
2001 return -EINVAL;
2002
2003 del_timer_sync(&tp->timer);
2004
2005 rtl_lock_work(tp);
2006 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
2007 cmd->base.duplex, advertising);
2008 rtl_unlock_work(tp);
2009
2010 return rc;
2011}
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2014 void *p)
2015{
Francois Romieu5b0384f2006-08-16 16:00:01 +02002016 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02002017 u32 __iomem *data = tp->mmio_addr;
2018 u32 *dw = p;
2019 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Francois Romieuda78dbf2012-01-26 14:18:23 +01002021 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02002022 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2023 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002024 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002027static u32 rtl8169_get_msglevel(struct net_device *dev)
2028{
2029 struct rtl8169_private *tp = netdev_priv(dev);
2030
2031 return tp->msg_enable;
2032}
2033
2034static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2035{
2036 struct rtl8169_private *tp = netdev_priv(dev);
2037
2038 tp->msg_enable = value;
2039}
2040
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002041static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2042 "tx_packets",
2043 "rx_packets",
2044 "tx_errors",
2045 "rx_errors",
2046 "rx_missed",
2047 "align_errors",
2048 "tx_single_collisions",
2049 "tx_multi_collisions",
2050 "unicast",
2051 "broadcast",
2052 "multicast",
2053 "tx_aborted",
2054 "tx_underrun",
2055};
2056
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002057static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002058{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002059 switch (sset) {
2060 case ETH_SS_STATS:
2061 return ARRAY_SIZE(rtl8169_gstrings);
2062 default:
2063 return -EOPNOTSUPP;
2064 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002065}
2066
Corinna Vinschen42020322015-09-10 10:47:35 +02002067DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002068{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002069 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002070}
2071
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002072static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002073{
Corinna Vinschen42020322015-09-10 10:47:35 +02002074 dma_addr_t paddr = tp->counters_phys_addr;
2075 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02002076
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002077 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
2078 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02002079 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002080 RTL_W32(tp, CounterAddrLow, cmd);
2081 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02002082
Francois Romieua78e9362018-01-26 01:53:26 +01002083 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002084}
2085
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002086static bool rtl8169_reset_counters(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002087{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002088 /*
2089 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2090 * tally counters.
2091 */
2092 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2093 return true;
2094
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002095 return rtl8169_do_counters(tp, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02002096}
2097
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002098static bool rtl8169_update_counters(struct rtl8169_private *tp)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002099{
Ivan Vecera355423d2009-02-06 21:49:57 -08002100 /*
2101 * Some chips are unable to dump tally counters when the receiver
2102 * is disabled.
2103 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002104 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002105 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002106
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002107 return rtl8169_do_counters(tp, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002108}
2109
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002110static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002111{
Corinna Vinschen42020322015-09-10 10:47:35 +02002112 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002113 bool ret = false;
2114
2115 /*
2116 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2117 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2118 * reset by a power cycle, while the counter values collected by the
2119 * driver are reset at every driver unload/load cycle.
2120 *
2121 * To make sure the HW values returned by @get_stats64 match the SW
2122 * values, we collect the initial values at first open(*) and use them
2123 * as offsets to normalize the values returned by @get_stats64.
2124 *
2125 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2126 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2127 * set at open time by rtl_hw_start.
2128 */
2129
2130 if (tp->tc_offset.inited)
2131 return true;
2132
2133 /* If both, reset and update fail, propagate to caller. */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002134 if (rtl8169_reset_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002135 ret = true;
2136
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002137 if (rtl8169_update_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002138 ret = true;
2139
Corinna Vinschen42020322015-09-10 10:47:35 +02002140 tp->tc_offset.tx_errors = counters->tx_errors;
2141 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2142 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002143 tp->tc_offset.inited = true;
2144
2145 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002146}
2147
Ivan Vecera355423d2009-02-06 21:49:57 -08002148static void rtl8169_get_ethtool_stats(struct net_device *dev,
2149 struct ethtool_stats *stats, u64 *data)
2150{
2151 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01002152 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02002153 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08002154
2155 ASSERT_RTNL();
2156
Chun-Hao Line0636232016-07-29 16:37:55 +08002157 pm_runtime_get_noresume(d);
2158
2159 if (pm_runtime_active(d))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002160 rtl8169_update_counters(tp);
Chun-Hao Line0636232016-07-29 16:37:55 +08002161
2162 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08002163
Corinna Vinschen42020322015-09-10 10:47:35 +02002164 data[0] = le64_to_cpu(counters->tx_packets);
2165 data[1] = le64_to_cpu(counters->rx_packets);
2166 data[2] = le64_to_cpu(counters->tx_errors);
2167 data[3] = le32_to_cpu(counters->rx_errors);
2168 data[4] = le16_to_cpu(counters->rx_missed);
2169 data[5] = le16_to_cpu(counters->align_errors);
2170 data[6] = le32_to_cpu(counters->tx_one_collision);
2171 data[7] = le32_to_cpu(counters->tx_multi_collision);
2172 data[8] = le64_to_cpu(counters->rx_unicast);
2173 data[9] = le64_to_cpu(counters->rx_broadcast);
2174 data[10] = le32_to_cpu(counters->rx_multicast);
2175 data[11] = le16_to_cpu(counters->tx_aborted);
2176 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08002177}
2178
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002179static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2180{
2181 switch(stringset) {
2182 case ETH_SS_STATS:
2183 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2184 break;
2185 }
2186}
2187
Florian Fainellif0903ea2016-12-03 12:01:19 -08002188static int rtl8169_nway_reset(struct net_device *dev)
2189{
2190 struct rtl8169_private *tp = netdev_priv(dev);
2191
2192 return mii_nway_restart(&tp->mii);
2193}
2194
Francois Romieu50970832017-10-27 13:24:49 +03002195/*
2196 * Interrupt coalescing
2197 *
2198 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2199 * > 8169, 8168 and 810x line of chipsets
2200 *
2201 * 8169, 8168, and 8136(810x) serial chipsets support it.
2202 *
2203 * > 2 - the Tx timer unit at gigabit speed
2204 *
2205 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2206 * (0xe0) bit 1 and bit 0.
2207 *
2208 * For 8169
2209 * bit[1:0] \ speed 1000M 100M 10M
2210 * 0 0 320ns 2.56us 40.96us
2211 * 0 1 2.56us 20.48us 327.7us
2212 * 1 0 5.12us 40.96us 655.4us
2213 * 1 1 10.24us 81.92us 1.31ms
2214 *
2215 * For the other
2216 * bit[1:0] \ speed 1000M 100M 10M
2217 * 0 0 5us 2.56us 40.96us
2218 * 0 1 40us 20.48us 327.7us
2219 * 1 0 80us 40.96us 655.4us
2220 * 1 1 160us 81.92us 1.31ms
2221 */
2222
2223/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2224struct rtl_coalesce_scale {
2225 /* Rx / Tx */
2226 u32 nsecs[2];
2227};
2228
2229/* rx/tx scale factors for all CPlusCmd[0:1] cases */
2230struct rtl_coalesce_info {
2231 u32 speed;
2232 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2233};
2234
2235/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2236#define rxtx_x1822(r, t) { \
2237 {{(r), (t)}}, \
2238 {{(r)*8, (t)*8}}, \
2239 {{(r)*8*2, (t)*8*2}}, \
2240 {{(r)*8*2*2, (t)*8*2*2}}, \
2241}
2242static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2243 /* speed delays: rx00 tx00 */
2244 { SPEED_10, rxtx_x1822(40960, 40960) },
2245 { SPEED_100, rxtx_x1822( 2560, 2560) },
2246 { SPEED_1000, rxtx_x1822( 320, 320) },
2247 { 0 },
2248};
2249
2250static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2251 /* speed delays: rx00 tx00 */
2252 { SPEED_10, rxtx_x1822(40960, 40960) },
2253 { SPEED_100, rxtx_x1822( 2560, 2560) },
2254 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2255 { 0 },
2256};
2257#undef rxtx_x1822
2258
2259/* get rx/tx scale vector corresponding to current speed */
2260static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2261{
2262 struct rtl8169_private *tp = netdev_priv(dev);
2263 struct ethtool_link_ksettings ecmd;
2264 const struct rtl_coalesce_info *ci;
2265 int rc;
2266
2267 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2268 if (rc < 0)
2269 return ERR_PTR(rc);
2270
2271 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2272 if (ecmd.base.speed == ci->speed) {
2273 return ci;
2274 }
2275 }
2276
2277 return ERR_PTR(-ELNRNG);
2278}
2279
2280static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2281{
2282 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002283 const struct rtl_coalesce_info *ci;
2284 const struct rtl_coalesce_scale *scale;
2285 struct {
2286 u32 *max_frames;
2287 u32 *usecs;
2288 } coal_settings [] = {
2289 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2290 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2291 }, *p = coal_settings;
2292 int i;
2293 u16 w;
2294
2295 memset(ec, 0, sizeof(*ec));
2296
2297 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2298 ci = rtl_coalesce_info(dev);
2299 if (IS_ERR(ci))
2300 return PTR_ERR(ci);
2301
Heiner Kallweit0ae09742018-04-28 22:19:26 +02002302 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
Francois Romieu50970832017-10-27 13:24:49 +03002303
2304 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002305 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03002306 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2307 w >>= RTL_COALESCE_SHIFT;
2308 *p->usecs = w & RTL_COALESCE_MASK;
2309 }
2310
2311 for (i = 0; i < 2; i++) {
2312 p = coal_settings + i;
2313 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2314
2315 /*
2316 * ethtool_coalesce says it is illegal to set both usecs and
2317 * max_frames to 0.
2318 */
2319 if (!*p->usecs && !*p->max_frames)
2320 *p->max_frames = 1;
2321 }
2322
2323 return 0;
2324}
2325
2326/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2327static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2328 struct net_device *dev, u32 nsec, u16 *cp01)
2329{
2330 const struct rtl_coalesce_info *ci;
2331 u16 i;
2332
2333 ci = rtl_coalesce_info(dev);
2334 if (IS_ERR(ci))
2335 return ERR_CAST(ci);
2336
2337 for (i = 0; i < 4; i++) {
2338 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2339 ci->scalev[i].nsecs[1]);
2340 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2341 *cp01 = i;
2342 return &ci->scalev[i];
2343 }
2344 }
2345
2346 return ERR_PTR(-EINVAL);
2347}
2348
2349static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2350{
2351 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002352 const struct rtl_coalesce_scale *scale;
2353 struct {
2354 u32 frames;
2355 u32 usecs;
2356 } coal_settings [] = {
2357 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2358 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2359 }, *p = coal_settings;
2360 u16 w = 0, cp01;
2361 int i;
2362
2363 scale = rtl_coalesce_choose_scale(dev,
2364 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2365 if (IS_ERR(scale))
2366 return PTR_ERR(scale);
2367
2368 for (i = 0; i < 2; i++, p++) {
2369 u32 units;
2370
2371 /*
2372 * accept max_frames=1 we returned in rtl_get_coalesce.
2373 * accept it not only when usecs=0 because of e.g. the following scenario:
2374 *
2375 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2376 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2377 * - then user does `ethtool -C eth0 rx-usecs 100`
2378 *
2379 * since ethtool sends to kernel whole ethtool_coalesce
2380 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2381 * we'll reject it below in `frames % 4 != 0`.
2382 */
2383 if (p->frames == 1) {
2384 p->frames = 0;
2385 }
2386
2387 units = p->usecs * 1000 / scale->nsecs[i];
2388 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2389 return -EINVAL;
2390
2391 w <<= RTL_COALESCE_SHIFT;
2392 w |= units;
2393 w <<= RTL_COALESCE_SHIFT;
2394 w |= p->frames >> 2;
2395 }
2396
2397 rtl_lock_work(tp);
2398
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002399 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03002400
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +02002401 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002402 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2403 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03002404
2405 rtl_unlock_work(tp);
2406
2407 return 0;
2408}
2409
Jeff Garzik7282d492006-09-13 14:30:00 -04002410static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 .get_drvinfo = rtl8169_get_drvinfo,
2412 .get_regs_len = rtl8169_get_regs_len,
2413 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002414 .get_coalesce = rtl_get_coalesce,
2415 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002416 .get_msglevel = rtl8169_get_msglevel,
2417 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002419 .get_wol = rtl8169_get_wol,
2420 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002421 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002422 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002423 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002424 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainellif0903ea2016-12-03 12:01:19 -08002425 .nway_reset = rtl8169_nway_reset,
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002426 .get_link_ksettings = rtl8169_get_link_ksettings,
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002427 .set_link_ksettings = rtl8169_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428};
2429
Francois Romieu07d3f512007-02-21 22:40:46 +01002430static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Heiner Kallweit22148df2018-04-22 17:15:15 +02002431 u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
Francois Romieu0e485152007-02-20 00:00:26 +01002433 /*
2434 * The driver currently handles the 8168Bf and the 8168Be identically
2435 * but they can be identified more specifically through the test below
2436 * if needed:
2437 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002438 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002439 *
2440 * Same thing for the 8101Eb and the 8101Ec:
2441 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002442 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002443 */
Francois Romieu37441002011-06-17 22:58:54 +02002444 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002446 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 int mac_version;
2448 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002449 /* 8168EP family. */
2450 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2451 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2452 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2453
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002454 /* 8168H family. */
2455 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2456 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2457
Hayes Wangc5583862012-07-02 17:23:22 +08002458 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002459 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002460 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002461 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2462 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2463
Hayes Wangc2218922011-09-06 16:55:18 +08002464 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002465 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002466 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2467 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2468
hayeswang01dc7fe2011-03-21 01:50:28 +00002469 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002470 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002471 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2472 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2473
Francois Romieu5b538df2008-07-20 16:22:45 +02002474 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002475 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002476 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002477
françois romieue6de30d2011-01-03 15:08:37 +00002478 /* 8168DP family. */
2479 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2480 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002481 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002482
Francois Romieuef808d52008-06-29 13:10:54 +02002483 /* 8168C family. */
Francois Romieuef3386f2008-06-29 12:24:30 +02002484 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002485 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002486 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002487 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2488 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002489 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieuef808d52008-06-29 13:10:54 +02002490 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002491
2492 /* 8168B family. */
2493 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002494 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2495 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2496
2497 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002498 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002499 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002500 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2501 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002502 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2503 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2504 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2505 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002506 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002507 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002508 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002509 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2510 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002511 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2512 /* FIXME: where did these entries come from ? -- FR */
2513 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2514 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2515
2516 /* 8110 family. */
2517 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2518 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2519 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2520 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2521 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2522 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2523
Jean Delvaref21b75e2009-05-26 20:54:48 -07002524 /* Catch-all */
2525 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002526 };
2527 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 u32 reg;
2529
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002530 reg = RTL_R32(tp, TxConfig);
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002531 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 p++;
2533 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002534
2535 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02002536 dev_notice(tp_to_dev(tp),
2537 "unknown MAC, using family default\n");
Francois Romieu5d320a22011-05-08 17:47:36 +02002538 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002539 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2540 tp->mac_version = tp->mii.supports_gmii ?
2541 RTL_GIGA_MAC_VER_42 :
2542 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002543 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2544 tp->mac_version = tp->mii.supports_gmii ?
2545 RTL_GIGA_MAC_VER_45 :
2546 RTL_GIGA_MAC_VER_47;
2547 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2548 tp->mac_version = tp->mii.supports_gmii ?
2549 RTL_GIGA_MAC_VER_46 :
2550 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
2553
2554static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2555{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002556 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
2558
Francois Romieu867763c2007-08-17 18:21:58 +02002559struct phy_reg {
2560 u16 reg;
2561 u16 val;
2562};
2563
françois romieu4da19632011-01-03 15:07:55 +00002564static void rtl_writephy_batch(struct rtl8169_private *tp,
2565 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002566{
2567 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002568 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002569 regs++;
2570 }
2571}
2572
françois romieubca03d52011-01-03 15:07:31 +00002573#define PHY_READ 0x00000000
2574#define PHY_DATA_OR 0x10000000
2575#define PHY_DATA_AND 0x20000000
2576#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002577#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002578#define PHY_CLEAR_READCOUNT 0x70000000
2579#define PHY_WRITE 0x80000000
2580#define PHY_READCOUNT_EQ_SKIP 0x90000000
2581#define PHY_COMP_EQ_SKIPN 0xa0000000
2582#define PHY_COMP_NEQ_SKIPN 0xb0000000
2583#define PHY_WRITE_PREVIOUS 0xc0000000
2584#define PHY_SKIPN 0xd0000000
2585#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002586
Hayes Wang960aee62011-06-18 11:37:48 +02002587struct fw_info {
2588 u32 magic;
2589 char version[RTL_VER_SIZE];
2590 __le32 fw_start;
2591 __le32 fw_len;
2592 u8 chksum;
2593} __packed;
2594
Francois Romieu1c361ef2011-06-17 17:16:24 +02002595#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2596
2597static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002598{
Francois Romieub6ffd972011-06-17 17:00:05 +02002599 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002600 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002601 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2602 char *version = rtl_fw->version;
2603 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002604
Francois Romieu1c361ef2011-06-17 17:16:24 +02002605 if (fw->size < FW_OPCODE_SIZE)
2606 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002607
2608 if (!fw_info->magic) {
2609 size_t i, size, start;
2610 u8 checksum = 0;
2611
2612 if (fw->size < sizeof(*fw_info))
2613 goto out;
2614
2615 for (i = 0; i < fw->size; i++)
2616 checksum += fw->data[i];
2617 if (checksum != 0)
2618 goto out;
2619
2620 start = le32_to_cpu(fw_info->fw_start);
2621 if (start > fw->size)
2622 goto out;
2623
2624 size = le32_to_cpu(fw_info->fw_len);
2625 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2626 goto out;
2627
2628 memcpy(version, fw_info->version, RTL_VER_SIZE);
2629
2630 pa->code = (__le32 *)(fw->data + start);
2631 pa->size = size;
2632 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002633 if (fw->size % FW_OPCODE_SIZE)
2634 goto out;
2635
2636 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2637
2638 pa->code = (__le32 *)fw->data;
2639 pa->size = fw->size / FW_OPCODE_SIZE;
2640 }
2641 version[RTL_VER_SIZE - 1] = 0;
2642
2643 rc = true;
2644out:
2645 return rc;
2646}
2647
Francois Romieufd112f22011-06-18 00:10:29 +02002648static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2649 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002650{
Francois Romieufd112f22011-06-18 00:10:29 +02002651 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002652 size_t index;
2653
Francois Romieu1c361ef2011-06-17 17:16:24 +02002654 for (index = 0; index < pa->size; index++) {
2655 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002656 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002657
hayeswang42b82dc2011-01-10 02:07:25 +00002658 switch(action & 0xf0000000) {
2659 case PHY_READ:
2660 case PHY_DATA_OR:
2661 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002662 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002663 case PHY_CLEAR_READCOUNT:
2664 case PHY_WRITE:
2665 case PHY_WRITE_PREVIOUS:
2666 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002667 break;
2668
hayeswang42b82dc2011-01-10 02:07:25 +00002669 case PHY_BJMPN:
2670 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002671 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002672 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002673 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002674 }
2675 break;
2676 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002677 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002678 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002679 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002680 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002681 }
2682 break;
2683 case PHY_COMP_EQ_SKIPN:
2684 case PHY_COMP_NEQ_SKIPN:
2685 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002686 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002687 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002688 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002689 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002690 }
2691 break;
2692
hayeswang42b82dc2011-01-10 02:07:25 +00002693 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002694 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002695 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002696 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002697 }
2698 }
Francois Romieufd112f22011-06-18 00:10:29 +02002699 rc = true;
2700out:
2701 return rc;
2702}
françois romieubca03d52011-01-03 15:07:31 +00002703
Francois Romieufd112f22011-06-18 00:10:29 +02002704static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2705{
2706 struct net_device *dev = tp->dev;
2707 int rc = -EINVAL;
2708
2709 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002710 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002711 goto out;
2712 }
2713
2714 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2715 rc = 0;
2716out:
2717 return rc;
2718}
2719
2720static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2721{
2722 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002723 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002724 u32 predata, count;
2725 size_t index;
2726
2727 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002728 org.write = ops->write;
2729 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002730
Francois Romieu1c361ef2011-06-17 17:16:24 +02002731 for (index = 0; index < pa->size; ) {
2732 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002733 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002734 u32 regno = (action & 0x0fff0000) >> 16;
2735
2736 if (!action)
2737 break;
françois romieubca03d52011-01-03 15:07:31 +00002738
2739 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002740 case PHY_READ:
2741 predata = rtl_readphy(tp, regno);
2742 count++;
2743 index++;
françois romieubca03d52011-01-03 15:07:31 +00002744 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002745 case PHY_DATA_OR:
2746 predata |= data;
2747 index++;
2748 break;
2749 case PHY_DATA_AND:
2750 predata &= data;
2751 index++;
2752 break;
2753 case PHY_BJMPN:
2754 index -= regno;
2755 break;
hayeswangeee37862013-04-01 22:23:38 +00002756 case PHY_MDIO_CHG:
2757 if (data == 0) {
2758 ops->write = org.write;
2759 ops->read = org.read;
2760 } else if (data == 1) {
2761 ops->write = mac_mcu_write;
2762 ops->read = mac_mcu_read;
2763 }
2764
hayeswang42b82dc2011-01-10 02:07:25 +00002765 index++;
2766 break;
2767 case PHY_CLEAR_READCOUNT:
2768 count = 0;
2769 index++;
2770 break;
2771 case PHY_WRITE:
2772 rtl_writephy(tp, regno, data);
2773 index++;
2774 break;
2775 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002776 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002777 break;
2778 case PHY_COMP_EQ_SKIPN:
2779 if (predata == data)
2780 index += regno;
2781 index++;
2782 break;
2783 case PHY_COMP_NEQ_SKIPN:
2784 if (predata != data)
2785 index += regno;
2786 index++;
2787 break;
2788 case PHY_WRITE_PREVIOUS:
2789 rtl_writephy(tp, regno, predata);
2790 index++;
2791 break;
2792 case PHY_SKIPN:
2793 index += regno + 1;
2794 break;
2795 case PHY_DELAY_MS:
2796 mdelay(data);
2797 index++;
2798 break;
2799
françois romieubca03d52011-01-03 15:07:31 +00002800 default:
2801 BUG();
2802 }
2803 }
hayeswangeee37862013-04-01 22:23:38 +00002804
2805 ops->write = org.write;
2806 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002807}
2808
françois romieuf1e02ed2011-01-13 13:07:53 +00002809static void rtl_release_firmware(struct rtl8169_private *tp)
2810{
Francois Romieub6ffd972011-06-17 17:00:05 +02002811 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2812 release_firmware(tp->rtl_fw->fw);
2813 kfree(tp->rtl_fw);
2814 }
2815 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002816}
2817
François Romieu953a12c2011-04-24 17:38:48 +02002818static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002819{
Francois Romieub6ffd972011-06-17 17:00:05 +02002820 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002821
2822 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002823 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002824 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002825}
2826
2827static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2828{
2829 if (rtl_readphy(tp, reg) != val)
2830 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2831 else
2832 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002833}
2834
françois romieu4da19632011-01-03 15:07:55 +00002835static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002837 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002838 { 0x1f, 0x0001 },
2839 { 0x06, 0x006e },
2840 { 0x08, 0x0708 },
2841 { 0x15, 0x4000 },
2842 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
françois romieu0b9b5712009-08-10 19:44:56 +00002844 { 0x1f, 0x0001 },
2845 { 0x03, 0x00a1 },
2846 { 0x02, 0x0008 },
2847 { 0x01, 0x0120 },
2848 { 0x00, 0x1000 },
2849 { 0x04, 0x0800 },
2850 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
françois romieu0b9b5712009-08-10 19:44:56 +00002852 { 0x03, 0xff41 },
2853 { 0x02, 0xdf60 },
2854 { 0x01, 0x0140 },
2855 { 0x00, 0x0077 },
2856 { 0x04, 0x7800 },
2857 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
françois romieu0b9b5712009-08-10 19:44:56 +00002859 { 0x03, 0x802f },
2860 { 0x02, 0x4f02 },
2861 { 0x01, 0x0409 },
2862 { 0x00, 0xf0f9 },
2863 { 0x04, 0x9800 },
2864 { 0x04, 0x9000 },
2865
2866 { 0x03, 0xdf01 },
2867 { 0x02, 0xdf20 },
2868 { 0x01, 0xff95 },
2869 { 0x00, 0xba00 },
2870 { 0x04, 0xa800 },
2871 { 0x04, 0xa000 },
2872
2873 { 0x03, 0xff41 },
2874 { 0x02, 0xdf20 },
2875 { 0x01, 0x0140 },
2876 { 0x00, 0x00bb },
2877 { 0x04, 0xb800 },
2878 { 0x04, 0xb000 },
2879
2880 { 0x03, 0xdf41 },
2881 { 0x02, 0xdc60 },
2882 { 0x01, 0x6340 },
2883 { 0x00, 0x007d },
2884 { 0x04, 0xd800 },
2885 { 0x04, 0xd000 },
2886
2887 { 0x03, 0xdf01 },
2888 { 0x02, 0xdf20 },
2889 { 0x01, 0x100a },
2890 { 0x00, 0xa0ff },
2891 { 0x04, 0xf800 },
2892 { 0x04, 0xf000 },
2893
2894 { 0x1f, 0x0000 },
2895 { 0x0b, 0x0000 },
2896 { 0x00, 0x9200 }
2897 };
2898
françois romieu4da19632011-01-03 15:07:55 +00002899 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900}
2901
françois romieu4da19632011-01-03 15:07:55 +00002902static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002903{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002904 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002905 { 0x1f, 0x0002 },
2906 { 0x01, 0x90d0 },
2907 { 0x1f, 0x0000 }
2908 };
2909
françois romieu4da19632011-01-03 15:07:55 +00002910 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002911}
2912
françois romieu4da19632011-01-03 15:07:55 +00002913static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002914{
2915 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002916
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002917 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2918 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002919 return;
2920
françois romieu4da19632011-01-03 15:07:55 +00002921 rtl_writephy(tp, 0x1f, 0x0001);
2922 rtl_writephy(tp, 0x10, 0xf01b);
2923 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002924}
2925
françois romieu4da19632011-01-03 15:07:55 +00002926static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002927{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002928 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002929 { 0x1f, 0x0001 },
2930 { 0x04, 0x0000 },
2931 { 0x03, 0x00a1 },
2932 { 0x02, 0x0008 },
2933 { 0x01, 0x0120 },
2934 { 0x00, 0x1000 },
2935 { 0x04, 0x0800 },
2936 { 0x04, 0x9000 },
2937 { 0x03, 0x802f },
2938 { 0x02, 0x4f02 },
2939 { 0x01, 0x0409 },
2940 { 0x00, 0xf099 },
2941 { 0x04, 0x9800 },
2942 { 0x04, 0xa000 },
2943 { 0x03, 0xdf01 },
2944 { 0x02, 0xdf20 },
2945 { 0x01, 0xff95 },
2946 { 0x00, 0xba00 },
2947 { 0x04, 0xa800 },
2948 { 0x04, 0xf000 },
2949 { 0x03, 0xdf01 },
2950 { 0x02, 0xdf20 },
2951 { 0x01, 0x101a },
2952 { 0x00, 0xa0ff },
2953 { 0x04, 0xf800 },
2954 { 0x04, 0x0000 },
2955 { 0x1f, 0x0000 },
2956
2957 { 0x1f, 0x0001 },
2958 { 0x10, 0xf41b },
2959 { 0x14, 0xfb54 },
2960 { 0x18, 0xf5c7 },
2961 { 0x1f, 0x0000 },
2962
2963 { 0x1f, 0x0001 },
2964 { 0x17, 0x0cc0 },
2965 { 0x1f, 0x0000 }
2966 };
2967
françois romieu4da19632011-01-03 15:07:55 +00002968 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002969
françois romieu4da19632011-01-03 15:07:55 +00002970 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002971}
2972
françois romieu4da19632011-01-03 15:07:55 +00002973static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002974{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002975 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002976 { 0x1f, 0x0001 },
2977 { 0x04, 0x0000 },
2978 { 0x03, 0x00a1 },
2979 { 0x02, 0x0008 },
2980 { 0x01, 0x0120 },
2981 { 0x00, 0x1000 },
2982 { 0x04, 0x0800 },
2983 { 0x04, 0x9000 },
2984 { 0x03, 0x802f },
2985 { 0x02, 0x4f02 },
2986 { 0x01, 0x0409 },
2987 { 0x00, 0xf099 },
2988 { 0x04, 0x9800 },
2989 { 0x04, 0xa000 },
2990 { 0x03, 0xdf01 },
2991 { 0x02, 0xdf20 },
2992 { 0x01, 0xff95 },
2993 { 0x00, 0xba00 },
2994 { 0x04, 0xa800 },
2995 { 0x04, 0xf000 },
2996 { 0x03, 0xdf01 },
2997 { 0x02, 0xdf20 },
2998 { 0x01, 0x101a },
2999 { 0x00, 0xa0ff },
3000 { 0x04, 0xf800 },
3001 { 0x04, 0x0000 },
3002 { 0x1f, 0x0000 },
3003
3004 { 0x1f, 0x0001 },
3005 { 0x0b, 0x8480 },
3006 { 0x1f, 0x0000 },
3007
3008 { 0x1f, 0x0001 },
3009 { 0x18, 0x67c7 },
3010 { 0x04, 0x2000 },
3011 { 0x03, 0x002f },
3012 { 0x02, 0x4360 },
3013 { 0x01, 0x0109 },
3014 { 0x00, 0x3022 },
3015 { 0x04, 0x2800 },
3016 { 0x1f, 0x0000 },
3017
3018 { 0x1f, 0x0001 },
3019 { 0x17, 0x0cc0 },
3020 { 0x1f, 0x0000 }
3021 };
3022
françois romieu4da19632011-01-03 15:07:55 +00003023 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00003024}
3025
françois romieu4da19632011-01-03 15:07:55 +00003026static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003027{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003028 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003029 { 0x10, 0xf41b },
3030 { 0x1f, 0x0000 }
3031 };
3032
françois romieu4da19632011-01-03 15:07:55 +00003033 rtl_writephy(tp, 0x1f, 0x0001);
3034 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02003035
françois romieu4da19632011-01-03 15:07:55 +00003036 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003037}
3038
françois romieu4da19632011-01-03 15:07:55 +00003039static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003040{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003041 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003042 { 0x1f, 0x0001 },
3043 { 0x10, 0xf41b },
3044 { 0x1f, 0x0000 }
3045 };
3046
françois romieu4da19632011-01-03 15:07:55 +00003047 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003048}
3049
françois romieu4da19632011-01-03 15:07:55 +00003050static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003051{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003052 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02003053 { 0x1f, 0x0000 },
3054 { 0x1d, 0x0f00 },
3055 { 0x1f, 0x0002 },
3056 { 0x0c, 0x1ec8 },
3057 { 0x1f, 0x0000 }
3058 };
3059
françois romieu4da19632011-01-03 15:07:55 +00003060 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02003061}
3062
françois romieu4da19632011-01-03 15:07:55 +00003063static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02003064{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003065 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02003066 { 0x1f, 0x0001 },
3067 { 0x1d, 0x3d98 },
3068 { 0x1f, 0x0000 }
3069 };
3070
françois romieu4da19632011-01-03 15:07:55 +00003071 rtl_writephy(tp, 0x1f, 0x0000);
3072 rtl_patchphy(tp, 0x14, 1 << 5);
3073 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02003074
françois romieu4da19632011-01-03 15:07:55 +00003075 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02003076}
3077
françois romieu4da19632011-01-03 15:07:55 +00003078static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003079{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003080 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02003081 { 0x1f, 0x0001 },
3082 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02003083 { 0x1f, 0x0002 },
3084 { 0x00, 0x88d4 },
3085 { 0x01, 0x82b1 },
3086 { 0x03, 0x7002 },
3087 { 0x08, 0x9e30 },
3088 { 0x09, 0x01f0 },
3089 { 0x0a, 0x5500 },
3090 { 0x0c, 0x00c8 },
3091 { 0x1f, 0x0003 },
3092 { 0x12, 0xc096 },
3093 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02003094 { 0x1f, 0x0000 },
3095 { 0x1f, 0x0000 },
3096 { 0x09, 0x2000 },
3097 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02003098 };
3099
françois romieu4da19632011-01-03 15:07:55 +00003100 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003101
françois romieu4da19632011-01-03 15:07:55 +00003102 rtl_patchphy(tp, 0x14, 1 << 5);
3103 rtl_patchphy(tp, 0x0d, 1 << 5);
3104 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02003105}
3106
françois romieu4da19632011-01-03 15:07:55 +00003107static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02003108{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003109 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02003110 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003111 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003112 { 0x03, 0x802f },
3113 { 0x02, 0x4f02 },
3114 { 0x01, 0x0409 },
3115 { 0x00, 0xf099 },
3116 { 0x04, 0x9800 },
3117 { 0x04, 0x9000 },
3118 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003119 { 0x1f, 0x0002 },
3120 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003121 { 0x06, 0x0761 },
3122 { 0x1f, 0x0003 },
3123 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003124 { 0x1f, 0x0000 }
3125 };
3126
françois romieu4da19632011-01-03 15:07:55 +00003127 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003128
françois romieu4da19632011-01-03 15:07:55 +00003129 rtl_patchphy(tp, 0x16, 1 << 0);
3130 rtl_patchphy(tp, 0x14, 1 << 5);
3131 rtl_patchphy(tp, 0x0d, 1 << 5);
3132 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003133}
3134
françois romieu4da19632011-01-03 15:07:55 +00003135static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02003136{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003137 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02003138 { 0x1f, 0x0001 },
3139 { 0x12, 0x2300 },
3140 { 0x1d, 0x3d98 },
3141 { 0x1f, 0x0002 },
3142 { 0x0c, 0x7eb8 },
3143 { 0x06, 0x5461 },
3144 { 0x1f, 0x0003 },
3145 { 0x16, 0x0f0a },
3146 { 0x1f, 0x0000 }
3147 };
3148
françois romieu4da19632011-01-03 15:07:55 +00003149 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02003150
françois romieu4da19632011-01-03 15:07:55 +00003151 rtl_patchphy(tp, 0x16, 1 << 0);
3152 rtl_patchphy(tp, 0x14, 1 << 5);
3153 rtl_patchphy(tp, 0x0d, 1 << 5);
3154 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003155}
3156
françois romieu4da19632011-01-03 15:07:55 +00003157static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003158{
françois romieu4da19632011-01-03 15:07:55 +00003159 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003160}
3161
françois romieubca03d52011-01-03 15:07:31 +00003162static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003163{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003164 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003165 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003166 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003167 { 0x06, 0x4064 },
3168 { 0x07, 0x2863 },
3169 { 0x08, 0x059c },
3170 { 0x09, 0x26b4 },
3171 { 0x0a, 0x6a19 },
3172 { 0x0b, 0xdcc8 },
3173 { 0x10, 0xf06d },
3174 { 0x14, 0x7f68 },
3175 { 0x18, 0x7fd9 },
3176 { 0x1c, 0xf0ff },
3177 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003178 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003179 { 0x12, 0xf49f },
3180 { 0x13, 0x070b },
3181 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003182 { 0x14, 0x94c0 },
3183
3184 /*
3185 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003186 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003187 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003188 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003189 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003190 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003191 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003192 { 0x06, 0x5561 },
3193
3194 /*
3195 * Can not link to 1Gbps with bad cable
3196 * Decrease SNR threshold form 21.07dB to 19.04dB
3197 */
3198 { 0x1f, 0x0001 },
3199 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003200
3201 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003202 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003203 };
3204
françois romieu4da19632011-01-03 15:07:55 +00003205 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003206
françois romieubca03d52011-01-03 15:07:31 +00003207 /*
3208 * Rx Error Issue
3209 * Fine Tune Switching regulator parameter
3210 */
françois romieu4da19632011-01-03 15:07:55 +00003211 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003212 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3213 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003214
Francois Romieufdf6fc02012-07-06 22:40:38 +02003215 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003216 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003217 { 0x1f, 0x0002 },
3218 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003219 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003220 { 0x05, 0x8330 },
3221 { 0x06, 0x669a },
3222 { 0x1f, 0x0002 }
3223 };
3224 int val;
3225
françois romieu4da19632011-01-03 15:07:55 +00003226 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003227
françois romieu4da19632011-01-03 15:07:55 +00003228 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003229
3230 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003231 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003232 0x0065, 0x0066, 0x0067, 0x0068,
3233 0x0069, 0x006a, 0x006b, 0x006c
3234 };
3235 int i;
3236
françois romieu4da19632011-01-03 15:07:55 +00003237 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003238
3239 val &= 0xff00;
3240 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003241 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003242 }
3243 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003244 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003245 { 0x1f, 0x0002 },
3246 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003247 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003248 { 0x05, 0x8330 },
3249 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003250 };
3251
françois romieu4da19632011-01-03 15:07:55 +00003252 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003253 }
3254
françois romieubca03d52011-01-03 15:07:31 +00003255 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003256 rtl_writephy(tp, 0x1f, 0x0002);
3257 rtl_patchphy(tp, 0x0d, 0x0300);
3258 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003259
françois romieubca03d52011-01-03 15:07:31 +00003260 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003261 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003262 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3263 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003264
françois romieu4da19632011-01-03 15:07:55 +00003265 rtl_writephy(tp, 0x1f, 0x0005);
3266 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003267
3268 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003269
françois romieu4da19632011-01-03 15:07:55 +00003270 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003271}
3272
françois romieubca03d52011-01-03 15:07:31 +00003273static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003274{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003275 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003276 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003277 { 0x1f, 0x0001 },
3278 { 0x06, 0x4064 },
3279 { 0x07, 0x2863 },
3280 { 0x08, 0x059c },
3281 { 0x09, 0x26b4 },
3282 { 0x0a, 0x6a19 },
3283 { 0x0b, 0xdcc8 },
3284 { 0x10, 0xf06d },
3285 { 0x14, 0x7f68 },
3286 { 0x18, 0x7fd9 },
3287 { 0x1c, 0xf0ff },
3288 { 0x1d, 0x3d9c },
3289 { 0x1f, 0x0003 },
3290 { 0x12, 0xf49f },
3291 { 0x13, 0x070b },
3292 { 0x1a, 0x05ad },
3293 { 0x14, 0x94c0 },
3294
françois romieubca03d52011-01-03 15:07:31 +00003295 /*
3296 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003297 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003298 */
françois romieudaf9df62009-10-07 12:44:20 +00003299 { 0x1f, 0x0002 },
3300 { 0x06, 0x5561 },
3301 { 0x1f, 0x0005 },
3302 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003303 { 0x06, 0x5561 },
3304
3305 /*
3306 * Can not link to 1Gbps with bad cable
3307 * Decrease SNR threshold form 21.07dB to 19.04dB
3308 */
3309 { 0x1f, 0x0001 },
3310 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003311
3312 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003313 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003314 };
3315
françois romieu4da19632011-01-03 15:07:55 +00003316 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003317
Francois Romieufdf6fc02012-07-06 22:40:38 +02003318 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003319 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003320 { 0x1f, 0x0002 },
3321 { 0x05, 0x669a },
3322 { 0x1f, 0x0005 },
3323 { 0x05, 0x8330 },
3324 { 0x06, 0x669a },
3325
3326 { 0x1f, 0x0002 }
3327 };
3328 int val;
3329
françois romieu4da19632011-01-03 15:07:55 +00003330 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003331
françois romieu4da19632011-01-03 15:07:55 +00003332 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003333 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003334 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003335 0x0065, 0x0066, 0x0067, 0x0068,
3336 0x0069, 0x006a, 0x006b, 0x006c
3337 };
3338 int i;
3339
françois romieu4da19632011-01-03 15:07:55 +00003340 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003341
3342 val &= 0xff00;
3343 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003344 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003345 }
3346 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003347 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003348 { 0x1f, 0x0002 },
3349 { 0x05, 0x2642 },
3350 { 0x1f, 0x0005 },
3351 { 0x05, 0x8330 },
3352 { 0x06, 0x2642 }
3353 };
3354
françois romieu4da19632011-01-03 15:07:55 +00003355 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003356 }
3357
françois romieubca03d52011-01-03 15:07:31 +00003358 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003359 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003360 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3361 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003362
françois romieubca03d52011-01-03 15:07:31 +00003363 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003364 rtl_writephy(tp, 0x1f, 0x0002);
3365 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003366
françois romieu4da19632011-01-03 15:07:55 +00003367 rtl_writephy(tp, 0x1f, 0x0005);
3368 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003369
3370 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003371
françois romieu4da19632011-01-03 15:07:55 +00003372 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003373}
3374
françois romieu4da19632011-01-03 15:07:55 +00003375static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003376{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003377 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003378 { 0x1f, 0x0002 },
3379 { 0x10, 0x0008 },
3380 { 0x0d, 0x006c },
3381
3382 { 0x1f, 0x0000 },
3383 { 0x0d, 0xf880 },
3384
3385 { 0x1f, 0x0001 },
3386 { 0x17, 0x0cc0 },
3387
3388 { 0x1f, 0x0001 },
3389 { 0x0b, 0xa4d8 },
3390 { 0x09, 0x281c },
3391 { 0x07, 0x2883 },
3392 { 0x0a, 0x6b35 },
3393 { 0x1d, 0x3da4 },
3394 { 0x1c, 0xeffd },
3395 { 0x14, 0x7f52 },
3396 { 0x18, 0x7fc6 },
3397 { 0x08, 0x0601 },
3398 { 0x06, 0x4063 },
3399 { 0x10, 0xf074 },
3400 { 0x1f, 0x0003 },
3401 { 0x13, 0x0789 },
3402 { 0x12, 0xf4bd },
3403 { 0x1a, 0x04fd },
3404 { 0x14, 0x84b0 },
3405 { 0x1f, 0x0000 },
3406 { 0x00, 0x9200 },
3407
3408 { 0x1f, 0x0005 },
3409 { 0x01, 0x0340 },
3410 { 0x1f, 0x0001 },
3411 { 0x04, 0x4000 },
3412 { 0x03, 0x1d21 },
3413 { 0x02, 0x0c32 },
3414 { 0x01, 0x0200 },
3415 { 0x00, 0x5554 },
3416 { 0x04, 0x4800 },
3417 { 0x04, 0x4000 },
3418 { 0x04, 0xf000 },
3419 { 0x03, 0xdf01 },
3420 { 0x02, 0xdf20 },
3421 { 0x01, 0x101a },
3422 { 0x00, 0xa0ff },
3423 { 0x04, 0xf800 },
3424 { 0x04, 0xf000 },
3425 { 0x1f, 0x0000 },
3426
3427 { 0x1f, 0x0007 },
3428 { 0x1e, 0x0023 },
3429 { 0x16, 0x0000 },
3430 { 0x1f, 0x0000 }
3431 };
3432
françois romieu4da19632011-01-03 15:07:55 +00003433 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003434}
3435
françois romieue6de30d2011-01-03 15:08:37 +00003436static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3437{
3438 static const struct phy_reg phy_reg_init[] = {
3439 { 0x1f, 0x0001 },
3440 { 0x17, 0x0cc0 },
3441
3442 { 0x1f, 0x0007 },
3443 { 0x1e, 0x002d },
3444 { 0x18, 0x0040 },
3445 { 0x1f, 0x0000 }
3446 };
3447
3448 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3449 rtl_patchphy(tp, 0x0d, 1 << 5);
3450}
3451
Hayes Wang70090422011-07-06 15:58:06 +08003452static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003453{
3454 static const struct phy_reg phy_reg_init[] = {
3455 /* Enable Delay cap */
3456 { 0x1f, 0x0005 },
3457 { 0x05, 0x8b80 },
3458 { 0x06, 0xc896 },
3459 { 0x1f, 0x0000 },
3460
3461 /* Channel estimation fine tune */
3462 { 0x1f, 0x0001 },
3463 { 0x0b, 0x6c20 },
3464 { 0x07, 0x2872 },
3465 { 0x1c, 0xefff },
3466 { 0x1f, 0x0003 },
3467 { 0x14, 0x6420 },
3468 { 0x1f, 0x0000 },
3469
3470 /* Update PFM & 10M TX idle timer */
3471 { 0x1f, 0x0007 },
3472 { 0x1e, 0x002f },
3473 { 0x15, 0x1919 },
3474 { 0x1f, 0x0000 },
3475
3476 { 0x1f, 0x0007 },
3477 { 0x1e, 0x00ac },
3478 { 0x18, 0x0006 },
3479 { 0x1f, 0x0000 }
3480 };
3481
Francois Romieu15ecd032011-04-27 13:52:22 -07003482 rtl_apply_firmware(tp);
3483
hayeswang01dc7fe2011-03-21 01:50:28 +00003484 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3485
3486 /* DCO enable for 10M IDLE Power */
3487 rtl_writephy(tp, 0x1f, 0x0007);
3488 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003489 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003490 rtl_writephy(tp, 0x1f, 0x0000);
3491
3492 /* For impedance matching */
3493 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003494 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003495 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003496
3497 /* PHY auto speed down */
3498 rtl_writephy(tp, 0x1f, 0x0007);
3499 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003500 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003501 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003502 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003503
3504 rtl_writephy(tp, 0x1f, 0x0005);
3505 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003506 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003507 rtl_writephy(tp, 0x1f, 0x0000);
3508
3509 rtl_writephy(tp, 0x1f, 0x0005);
3510 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003511 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003512 rtl_writephy(tp, 0x1f, 0x0007);
3513 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003514 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003515 rtl_writephy(tp, 0x1f, 0x0006);
3516 rtl_writephy(tp, 0x00, 0x5a00);
3517 rtl_writephy(tp, 0x1f, 0x0000);
3518 rtl_writephy(tp, 0x0d, 0x0007);
3519 rtl_writephy(tp, 0x0e, 0x003c);
3520 rtl_writephy(tp, 0x0d, 0x4007);
3521 rtl_writephy(tp, 0x0e, 0x0000);
3522 rtl_writephy(tp, 0x0d, 0x0000);
3523}
3524
françois romieu9ecb9aa2012-12-07 11:20:21 +00003525static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3526{
3527 const u16 w[] = {
3528 addr[0] | (addr[1] << 8),
3529 addr[2] | (addr[3] << 8),
3530 addr[4] | (addr[5] << 8)
3531 };
3532 const struct exgmac_reg e[] = {
3533 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3534 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3535 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3536 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3537 };
3538
3539 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3540}
3541
Hayes Wang70090422011-07-06 15:58:06 +08003542static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3543{
3544 static const struct phy_reg phy_reg_init[] = {
3545 /* Enable Delay cap */
3546 { 0x1f, 0x0004 },
3547 { 0x1f, 0x0007 },
3548 { 0x1e, 0x00ac },
3549 { 0x18, 0x0006 },
3550 { 0x1f, 0x0002 },
3551 { 0x1f, 0x0000 },
3552 { 0x1f, 0x0000 },
3553
3554 /* Channel estimation fine tune */
3555 { 0x1f, 0x0003 },
3556 { 0x09, 0xa20f },
3557 { 0x1f, 0x0000 },
3558 { 0x1f, 0x0000 },
3559
3560 /* Green Setting */
3561 { 0x1f, 0x0005 },
3562 { 0x05, 0x8b5b },
3563 { 0x06, 0x9222 },
3564 { 0x05, 0x8b6d },
3565 { 0x06, 0x8000 },
3566 { 0x05, 0x8b76 },
3567 { 0x06, 0x8000 },
3568 { 0x1f, 0x0000 }
3569 };
3570
3571 rtl_apply_firmware(tp);
3572
3573 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3574
3575 /* For 4-corner performance improve */
3576 rtl_writephy(tp, 0x1f, 0x0005);
3577 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003578 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003579 rtl_writephy(tp, 0x1f, 0x0000);
3580
3581 /* PHY auto speed down */
3582 rtl_writephy(tp, 0x1f, 0x0004);
3583 rtl_writephy(tp, 0x1f, 0x0007);
3584 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003585 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003586 rtl_writephy(tp, 0x1f, 0x0002);
3587 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003588 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003589
3590 /* improve 10M EEE waveform */
3591 rtl_writephy(tp, 0x1f, 0x0005);
3592 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003593 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003594 rtl_writephy(tp, 0x1f, 0x0000);
3595
3596 /* Improve 2-pair detection performance */
3597 rtl_writephy(tp, 0x1f, 0x0005);
3598 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003599 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003600 rtl_writephy(tp, 0x1f, 0x0000);
3601
3602 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003603 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003604 rtl_writephy(tp, 0x1f, 0x0005);
3605 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003606 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003607 rtl_writephy(tp, 0x1f, 0x0004);
3608 rtl_writephy(tp, 0x1f, 0x0007);
3609 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003610 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003611 rtl_writephy(tp, 0x1f, 0x0002);
3612 rtl_writephy(tp, 0x1f, 0x0000);
3613 rtl_writephy(tp, 0x0d, 0x0007);
3614 rtl_writephy(tp, 0x0e, 0x003c);
3615 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003616 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003617 rtl_writephy(tp, 0x0d, 0x0000);
3618
3619 /* Green feature */
3620 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003621 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3622 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003623 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003624 rtl_writephy(tp, 0x1f, 0x0005);
3625 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3626 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003627
françois romieu9ecb9aa2012-12-07 11:20:21 +00003628 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3629 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003630}
3631
Hayes Wang5f886e02012-03-30 14:33:03 +08003632static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3633{
3634 /* For 4-corner performance improve */
3635 rtl_writephy(tp, 0x1f, 0x0005);
3636 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003637 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003638 rtl_writephy(tp, 0x1f, 0x0000);
3639
3640 /* PHY auto speed down */
3641 rtl_writephy(tp, 0x1f, 0x0007);
3642 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003643 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003644 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003645 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003646
3647 /* Improve 10M EEE waveform */
3648 rtl_writephy(tp, 0x1f, 0x0005);
3649 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003650 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003651 rtl_writephy(tp, 0x1f, 0x0000);
3652}
3653
Hayes Wangc2218922011-09-06 16:55:18 +08003654static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3655{
3656 static const struct phy_reg phy_reg_init[] = {
3657 /* Channel estimation fine tune */
3658 { 0x1f, 0x0003 },
3659 { 0x09, 0xa20f },
3660 { 0x1f, 0x0000 },
3661
3662 /* Modify green table for giga & fnet */
3663 { 0x1f, 0x0005 },
3664 { 0x05, 0x8b55 },
3665 { 0x06, 0x0000 },
3666 { 0x05, 0x8b5e },
3667 { 0x06, 0x0000 },
3668 { 0x05, 0x8b67 },
3669 { 0x06, 0x0000 },
3670 { 0x05, 0x8b70 },
3671 { 0x06, 0x0000 },
3672 { 0x1f, 0x0000 },
3673 { 0x1f, 0x0007 },
3674 { 0x1e, 0x0078 },
3675 { 0x17, 0x0000 },
3676 { 0x19, 0x00fb },
3677 { 0x1f, 0x0000 },
3678
3679 /* Modify green table for 10M */
3680 { 0x1f, 0x0005 },
3681 { 0x05, 0x8b79 },
3682 { 0x06, 0xaa00 },
3683 { 0x1f, 0x0000 },
3684
3685 /* Disable hiimpedance detection (RTCT) */
3686 { 0x1f, 0x0003 },
3687 { 0x01, 0x328a },
3688 { 0x1f, 0x0000 }
3689 };
3690
3691 rtl_apply_firmware(tp);
3692
3693 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3694
Hayes Wang5f886e02012-03-30 14:33:03 +08003695 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003696
3697 /* Improve 2-pair detection performance */
3698 rtl_writephy(tp, 0x1f, 0x0005);
3699 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003700 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003701 rtl_writephy(tp, 0x1f, 0x0000);
3702}
3703
3704static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3705{
3706 rtl_apply_firmware(tp);
3707
Hayes Wang5f886e02012-03-30 14:33:03 +08003708 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003709}
3710
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003711static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3712{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003713 static const struct phy_reg phy_reg_init[] = {
3714 /* Channel estimation fine tune */
3715 { 0x1f, 0x0003 },
3716 { 0x09, 0xa20f },
3717 { 0x1f, 0x0000 },
3718
3719 /* Modify green table for giga & fnet */
3720 { 0x1f, 0x0005 },
3721 { 0x05, 0x8b55 },
3722 { 0x06, 0x0000 },
3723 { 0x05, 0x8b5e },
3724 { 0x06, 0x0000 },
3725 { 0x05, 0x8b67 },
3726 { 0x06, 0x0000 },
3727 { 0x05, 0x8b70 },
3728 { 0x06, 0x0000 },
3729 { 0x1f, 0x0000 },
3730 { 0x1f, 0x0007 },
3731 { 0x1e, 0x0078 },
3732 { 0x17, 0x0000 },
3733 { 0x19, 0x00aa },
3734 { 0x1f, 0x0000 },
3735
3736 /* Modify green table for 10M */
3737 { 0x1f, 0x0005 },
3738 { 0x05, 0x8b79 },
3739 { 0x06, 0xaa00 },
3740 { 0x1f, 0x0000 },
3741
3742 /* Disable hiimpedance detection (RTCT) */
3743 { 0x1f, 0x0003 },
3744 { 0x01, 0x328a },
3745 { 0x1f, 0x0000 }
3746 };
3747
3748
3749 rtl_apply_firmware(tp);
3750
3751 rtl8168f_hw_phy_config(tp);
3752
3753 /* Improve 2-pair detection performance */
3754 rtl_writephy(tp, 0x1f, 0x0005);
3755 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003756 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003757 rtl_writephy(tp, 0x1f, 0x0000);
3758
3759 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3760
3761 /* Modify green table for giga */
3762 rtl_writephy(tp, 0x1f, 0x0005);
3763 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003764 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003765 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003766 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003767 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003768 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003769 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003770 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003771 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003772 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003773 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003774 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003775 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003776 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003777 rtl_writephy(tp, 0x1f, 0x0000);
3778
3779 /* uc same-seed solution */
3780 rtl_writephy(tp, 0x1f, 0x0005);
3781 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003782 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003783 rtl_writephy(tp, 0x1f, 0x0000);
3784
3785 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003786 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003787 rtl_writephy(tp, 0x1f, 0x0005);
3788 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003789 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003790 rtl_writephy(tp, 0x1f, 0x0004);
3791 rtl_writephy(tp, 0x1f, 0x0007);
3792 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003793 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003794 rtl_writephy(tp, 0x1f, 0x0000);
3795 rtl_writephy(tp, 0x0d, 0x0007);
3796 rtl_writephy(tp, 0x0e, 0x003c);
3797 rtl_writephy(tp, 0x0d, 0x4007);
3798 rtl_writephy(tp, 0x0e, 0x0000);
3799 rtl_writephy(tp, 0x0d, 0x0000);
3800
3801 /* Green feature */
3802 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003803 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3804 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003805 rtl_writephy(tp, 0x1f, 0x0000);
3806}
3807
Hayes Wangc5583862012-07-02 17:23:22 +08003808static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3809{
Hayes Wangc5583862012-07-02 17:23:22 +08003810 rtl_apply_firmware(tp);
3811
hayeswang41f44d12013-04-01 22:23:36 +00003812 rtl_writephy(tp, 0x1f, 0x0a46);
3813 if (rtl_readphy(tp, 0x10) & 0x0100) {
3814 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003815 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003816 } else {
3817 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003818 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003819 }
Hayes Wangc5583862012-07-02 17:23:22 +08003820
hayeswang41f44d12013-04-01 22:23:36 +00003821 rtl_writephy(tp, 0x1f, 0x0a46);
3822 if (rtl_readphy(tp, 0x13) & 0x0100) {
3823 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003824 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003825 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003826 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003827 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003828 }
Hayes Wangc5583862012-07-02 17:23:22 +08003829
hayeswang41f44d12013-04-01 22:23:36 +00003830 /* Enable PHY auto speed down */
3831 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003832 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003833
hayeswangfe7524c2013-04-01 22:23:37 +00003834 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003835 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003836 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003837 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003838 rtl_writephy(tp, 0x1f, 0x0a43);
3839 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003840 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3841 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003842
hayeswang41f44d12013-04-01 22:23:36 +00003843 /* EEE auto-fallback function */
3844 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003845 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003846
hayeswang41f44d12013-04-01 22:23:36 +00003847 /* Enable UC LPF tune function */
3848 rtl_writephy(tp, 0x1f, 0x0a43);
3849 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003850 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003851
3852 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003853 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003854
hayeswangfe7524c2013-04-01 22:23:37 +00003855 /* Improve SWR Efficiency */
3856 rtl_writephy(tp, 0x1f, 0x0bcd);
3857 rtl_writephy(tp, 0x14, 0x5065);
3858 rtl_writephy(tp, 0x14, 0xd065);
3859 rtl_writephy(tp, 0x1f, 0x0bc8);
3860 rtl_writephy(tp, 0x11, 0x5655);
3861 rtl_writephy(tp, 0x1f, 0x0bcd);
3862 rtl_writephy(tp, 0x14, 0x1065);
3863 rtl_writephy(tp, 0x14, 0x9065);
3864 rtl_writephy(tp, 0x14, 0x1065);
3865
David Chang1bac1072013-11-27 15:48:36 +08003866 /* Check ALDPS bit, disable it if enabled */
3867 rtl_writephy(tp, 0x1f, 0x0a43);
3868 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003869 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08003870
hayeswang41f44d12013-04-01 22:23:36 +00003871 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003872}
3873
hayeswang57538c42013-04-01 22:23:40 +00003874static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3875{
3876 rtl_apply_firmware(tp);
3877}
3878
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003879static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3880{
3881 u16 dout_tapbin;
3882 u32 data;
3883
3884 rtl_apply_firmware(tp);
3885
3886 /* CHN EST parameters adjust - giga master */
3887 rtl_writephy(tp, 0x1f, 0x0a43);
3888 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003889 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003890 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003891 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003892 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003893 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003894 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003895 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003896 rtl_writephy(tp, 0x1f, 0x0000);
3897
3898 /* CHN EST parameters adjust - giga slave */
3899 rtl_writephy(tp, 0x1f, 0x0a43);
3900 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003901 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003902 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003903 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003904 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003905 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003906 rtl_writephy(tp, 0x1f, 0x0000);
3907
3908 /* CHN EST parameters adjust - fnet */
3909 rtl_writephy(tp, 0x1f, 0x0a43);
3910 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003911 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003912 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003913 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003914 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003915 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003916 rtl_writephy(tp, 0x1f, 0x0000);
3917
3918 /* enable R-tune & PGA-retune function */
3919 dout_tapbin = 0;
3920 rtl_writephy(tp, 0x1f, 0x0a46);
3921 data = rtl_readphy(tp, 0x13);
3922 data &= 3;
3923 data <<= 2;
3924 dout_tapbin |= data;
3925 data = rtl_readphy(tp, 0x12);
3926 data &= 0xc000;
3927 data >>= 14;
3928 dout_tapbin |= data;
3929 dout_tapbin = ~(dout_tapbin^0x08);
3930 dout_tapbin <<= 12;
3931 dout_tapbin &= 0xf000;
3932 rtl_writephy(tp, 0x1f, 0x0a43);
3933 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003934 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003935 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003936 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003937 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003938 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003939 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003940 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003941
3942 rtl_writephy(tp, 0x1f, 0x0a43);
3943 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003944 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003945 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003946 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003947 rtl_writephy(tp, 0x1f, 0x0000);
3948
3949 /* enable GPHY 10M */
3950 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003951 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003952 rtl_writephy(tp, 0x1f, 0x0000);
3953
3954 /* SAR ADC performance */
3955 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003956 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003957 rtl_writephy(tp, 0x1f, 0x0000);
3958
3959 rtl_writephy(tp, 0x1f, 0x0a43);
3960 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003961 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003962 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003963 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003964 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003965 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003966 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003967 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003968 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003969 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003970 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003971 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003972 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003973 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003974 rtl_writephy(tp, 0x1f, 0x0000);
3975
3976 /* disable phy pfm mode */
3977 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003978 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003979 rtl_writephy(tp, 0x1f, 0x0000);
3980
3981 /* Check ALDPS bit, disable it if enabled */
3982 rtl_writephy(tp, 0x1f, 0x0a43);
3983 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003984 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003985
3986 rtl_writephy(tp, 0x1f, 0x0000);
3987}
3988
3989static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3990{
3991 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3992 u16 rlen;
3993 u32 data;
3994
3995 rtl_apply_firmware(tp);
3996
3997 /* CHIN EST parameter update */
3998 rtl_writephy(tp, 0x1f, 0x0a43);
3999 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004000 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004001 rtl_writephy(tp, 0x1f, 0x0000);
4002
4003 /* enable R-tune & PGA-retune function */
4004 rtl_writephy(tp, 0x1f, 0x0a43);
4005 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004006 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004007 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004008 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004009 rtl_writephy(tp, 0x1f, 0x0000);
4010
4011 /* enable GPHY 10M */
4012 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004013 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004014 rtl_writephy(tp, 0x1f, 0x0000);
4015
4016 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
4017 data = r8168_mac_ocp_read(tp, 0xdd02);
4018 ioffset_p3 = ((data & 0x80)>>7);
4019 ioffset_p3 <<= 3;
4020
4021 data = r8168_mac_ocp_read(tp, 0xdd00);
4022 ioffset_p3 |= ((data & (0xe000))>>13);
4023 ioffset_p2 = ((data & (0x1e00))>>9);
4024 ioffset_p1 = ((data & (0x01e0))>>5);
4025 ioffset_p0 = ((data & 0x0010)>>4);
4026 ioffset_p0 <<= 3;
4027 ioffset_p0 |= (data & (0x07));
4028 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
4029
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004030 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08004031 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004032 rtl_writephy(tp, 0x1f, 0x0bcf);
4033 rtl_writephy(tp, 0x16, data);
4034 rtl_writephy(tp, 0x1f, 0x0000);
4035 }
4036
4037 /* Modify rlen (TX LPF corner frequency) level */
4038 rtl_writephy(tp, 0x1f, 0x0bcd);
4039 data = rtl_readphy(tp, 0x16);
4040 data &= 0x000f;
4041 rlen = 0;
4042 if (data > 3)
4043 rlen = data - 3;
4044 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
4045 rtl_writephy(tp, 0x17, data);
4046 rtl_writephy(tp, 0x1f, 0x0bcd);
4047 rtl_writephy(tp, 0x1f, 0x0000);
4048
4049 /* disable phy pfm mode */
4050 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004051 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004052 rtl_writephy(tp, 0x1f, 0x0000);
4053
4054 /* Check ALDPS bit, disable it if enabled */
4055 rtl_writephy(tp, 0x1f, 0x0a43);
4056 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004057 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004058
4059 rtl_writephy(tp, 0x1f, 0x0000);
4060}
4061
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004062static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4063{
4064 /* Enable PHY auto speed down */
4065 rtl_writephy(tp, 0x1f, 0x0a44);
4066 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4067 rtl_writephy(tp, 0x1f, 0x0000);
4068
4069 /* patch 10M & ALDPS */
4070 rtl_writephy(tp, 0x1f, 0x0bcc);
4071 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4072 rtl_writephy(tp, 0x1f, 0x0a44);
4073 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4074 rtl_writephy(tp, 0x1f, 0x0a43);
4075 rtl_writephy(tp, 0x13, 0x8084);
4076 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4077 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4078 rtl_writephy(tp, 0x1f, 0x0000);
4079
4080 /* Enable EEE auto-fallback function */
4081 rtl_writephy(tp, 0x1f, 0x0a4b);
4082 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4083 rtl_writephy(tp, 0x1f, 0x0000);
4084
4085 /* Enable UC LPF tune function */
4086 rtl_writephy(tp, 0x1f, 0x0a43);
4087 rtl_writephy(tp, 0x13, 0x8012);
4088 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4089 rtl_writephy(tp, 0x1f, 0x0000);
4090
4091 /* set rg_sel_sdm_rate */
4092 rtl_writephy(tp, 0x1f, 0x0c42);
4093 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4094 rtl_writephy(tp, 0x1f, 0x0000);
4095
4096 /* Check ALDPS bit, disable it if enabled */
4097 rtl_writephy(tp, 0x1f, 0x0a43);
4098 if (rtl_readphy(tp, 0x10) & 0x0004)
4099 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4100
4101 rtl_writephy(tp, 0x1f, 0x0000);
4102}
4103
4104static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4105{
4106 /* patch 10M & ALDPS */
4107 rtl_writephy(tp, 0x1f, 0x0bcc);
4108 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4109 rtl_writephy(tp, 0x1f, 0x0a44);
4110 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4111 rtl_writephy(tp, 0x1f, 0x0a43);
4112 rtl_writephy(tp, 0x13, 0x8084);
4113 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4114 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4115 rtl_writephy(tp, 0x1f, 0x0000);
4116
4117 /* Enable UC LPF tune function */
4118 rtl_writephy(tp, 0x1f, 0x0a43);
4119 rtl_writephy(tp, 0x13, 0x8012);
4120 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4121 rtl_writephy(tp, 0x1f, 0x0000);
4122
4123 /* Set rg_sel_sdm_rate */
4124 rtl_writephy(tp, 0x1f, 0x0c42);
4125 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4126 rtl_writephy(tp, 0x1f, 0x0000);
4127
4128 /* Channel estimation parameters */
4129 rtl_writephy(tp, 0x1f, 0x0a43);
4130 rtl_writephy(tp, 0x13, 0x80f3);
4131 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4132 rtl_writephy(tp, 0x13, 0x80f0);
4133 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4134 rtl_writephy(tp, 0x13, 0x80ef);
4135 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4136 rtl_writephy(tp, 0x13, 0x80f6);
4137 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4138 rtl_writephy(tp, 0x13, 0x80ec);
4139 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4140 rtl_writephy(tp, 0x13, 0x80ed);
4141 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4142 rtl_writephy(tp, 0x13, 0x80f2);
4143 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4144 rtl_writephy(tp, 0x13, 0x80f4);
4145 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4146 rtl_writephy(tp, 0x1f, 0x0a43);
4147 rtl_writephy(tp, 0x13, 0x8110);
4148 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4149 rtl_writephy(tp, 0x13, 0x810f);
4150 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4151 rtl_writephy(tp, 0x13, 0x8111);
4152 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4153 rtl_writephy(tp, 0x13, 0x8113);
4154 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4155 rtl_writephy(tp, 0x13, 0x8115);
4156 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4157 rtl_writephy(tp, 0x13, 0x810e);
4158 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4159 rtl_writephy(tp, 0x13, 0x810c);
4160 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4161 rtl_writephy(tp, 0x13, 0x810b);
4162 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4163 rtl_writephy(tp, 0x1f, 0x0a43);
4164 rtl_writephy(tp, 0x13, 0x80d1);
4165 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4166 rtl_writephy(tp, 0x13, 0x80cd);
4167 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4168 rtl_writephy(tp, 0x13, 0x80d3);
4169 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4170 rtl_writephy(tp, 0x13, 0x80d5);
4171 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4172 rtl_writephy(tp, 0x13, 0x80d7);
4173 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4174
4175 /* Force PWM-mode */
4176 rtl_writephy(tp, 0x1f, 0x0bcd);
4177 rtl_writephy(tp, 0x14, 0x5065);
4178 rtl_writephy(tp, 0x14, 0xd065);
4179 rtl_writephy(tp, 0x1f, 0x0bc8);
4180 rtl_writephy(tp, 0x12, 0x00ed);
4181 rtl_writephy(tp, 0x1f, 0x0bcd);
4182 rtl_writephy(tp, 0x14, 0x1065);
4183 rtl_writephy(tp, 0x14, 0x9065);
4184 rtl_writephy(tp, 0x14, 0x1065);
4185 rtl_writephy(tp, 0x1f, 0x0000);
4186
4187 /* Check ALDPS bit, disable it if enabled */
4188 rtl_writephy(tp, 0x1f, 0x0a43);
4189 if (rtl_readphy(tp, 0x10) & 0x0004)
4190 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4191
4192 rtl_writephy(tp, 0x1f, 0x0000);
4193}
4194
françois romieu4da19632011-01-03 15:07:55 +00004195static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004196{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004197 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004198 { 0x1f, 0x0003 },
4199 { 0x08, 0x441d },
4200 { 0x01, 0x9100 },
4201 { 0x1f, 0x0000 }
4202 };
4203
françois romieu4da19632011-01-03 15:07:55 +00004204 rtl_writephy(tp, 0x1f, 0x0000);
4205 rtl_patchphy(tp, 0x11, 1 << 12);
4206 rtl_patchphy(tp, 0x19, 1 << 13);
4207 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004208
françois romieu4da19632011-01-03 15:07:55 +00004209 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004210}
4211
Hayes Wang5a5e4442011-02-22 17:26:21 +08004212static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4213{
4214 static const struct phy_reg phy_reg_init[] = {
4215 { 0x1f, 0x0005 },
4216 { 0x1a, 0x0000 },
4217 { 0x1f, 0x0000 },
4218
4219 { 0x1f, 0x0004 },
4220 { 0x1c, 0x0000 },
4221 { 0x1f, 0x0000 },
4222
4223 { 0x1f, 0x0001 },
4224 { 0x15, 0x7701 },
4225 { 0x1f, 0x0000 }
4226 };
4227
4228 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004229 rtl_writephy(tp, 0x1f, 0x0000);
4230 rtl_writephy(tp, 0x18, 0x0310);
4231 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004232
François Romieu953a12c2011-04-24 17:38:48 +02004233 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004234
4235 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4236}
4237
Hayes Wang7e18dca2012-03-30 14:33:02 +08004238static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4239{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004240 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004241 rtl_writephy(tp, 0x1f, 0x0000);
4242 rtl_writephy(tp, 0x18, 0x0310);
4243 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004244
4245 rtl_apply_firmware(tp);
4246
4247 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004248 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004249 rtl_writephy(tp, 0x1f, 0x0004);
4250 rtl_writephy(tp, 0x10, 0x401f);
4251 rtl_writephy(tp, 0x19, 0x7030);
4252 rtl_writephy(tp, 0x1f, 0x0000);
4253}
4254
Hayes Wang5598bfe2012-07-02 17:23:21 +08004255static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4256{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004257 static const struct phy_reg phy_reg_init[] = {
4258 { 0x1f, 0x0004 },
4259 { 0x10, 0xc07f },
4260 { 0x19, 0x7030 },
4261 { 0x1f, 0x0000 }
4262 };
4263
4264 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004265 rtl_writephy(tp, 0x1f, 0x0000);
4266 rtl_writephy(tp, 0x18, 0x0310);
4267 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004268
4269 rtl_apply_firmware(tp);
4270
Francois Romieufdf6fc02012-07-06 22:40:38 +02004271 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004272 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4273
Francois Romieufdf6fc02012-07-06 22:40:38 +02004274 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004275}
4276
Francois Romieu5615d9f2007-08-17 17:50:46 +02004277static void rtl_hw_phy_config(struct net_device *dev)
4278{
4279 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004280
4281 rtl8169_print_mac_version(tp);
4282
4283 switch (tp->mac_version) {
4284 case RTL_GIGA_MAC_VER_01:
4285 break;
4286 case RTL_GIGA_MAC_VER_02:
4287 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004288 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004289 break;
4290 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004291 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004292 break;
françois romieu2e9558562009-08-10 19:44:19 +00004293 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004294 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004295 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004296 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004297 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004298 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004299 case RTL_GIGA_MAC_VER_07:
4300 case RTL_GIGA_MAC_VER_08:
4301 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004302 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004303 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004304 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004305 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004306 break;
4307 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004308 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004309 break;
4310 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004311 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004312 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004313 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004314 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004315 break;
4316 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004317 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004318 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004319 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004320 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004321 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004322 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004323 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004324 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004325 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004326 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004327 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004328 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004329 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004330 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004331 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004332 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004333 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004334 break;
4335 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004336 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004337 break;
4338 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004339 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004340 break;
françois romieue6de30d2011-01-03 15:08:37 +00004341 case RTL_GIGA_MAC_VER_28:
4342 rtl8168d_4_hw_phy_config(tp);
4343 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004344 case RTL_GIGA_MAC_VER_29:
4345 case RTL_GIGA_MAC_VER_30:
4346 rtl8105e_hw_phy_config(tp);
4347 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004348 case RTL_GIGA_MAC_VER_31:
4349 /* None. */
4350 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004351 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004352 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004353 rtl8168e_1_hw_phy_config(tp);
4354 break;
4355 case RTL_GIGA_MAC_VER_34:
4356 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004357 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004358 case RTL_GIGA_MAC_VER_35:
4359 rtl8168f_1_hw_phy_config(tp);
4360 break;
4361 case RTL_GIGA_MAC_VER_36:
4362 rtl8168f_2_hw_phy_config(tp);
4363 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004364
Hayes Wang7e18dca2012-03-30 14:33:02 +08004365 case RTL_GIGA_MAC_VER_37:
4366 rtl8402_hw_phy_config(tp);
4367 break;
4368
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004369 case RTL_GIGA_MAC_VER_38:
4370 rtl8411_hw_phy_config(tp);
4371 break;
4372
Hayes Wang5598bfe2012-07-02 17:23:21 +08004373 case RTL_GIGA_MAC_VER_39:
4374 rtl8106e_hw_phy_config(tp);
4375 break;
4376
Hayes Wangc5583862012-07-02 17:23:22 +08004377 case RTL_GIGA_MAC_VER_40:
4378 rtl8168g_1_hw_phy_config(tp);
4379 break;
hayeswang57538c42013-04-01 22:23:40 +00004380 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004381 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004382 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004383 rtl8168g_2_hw_phy_config(tp);
4384 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004385 case RTL_GIGA_MAC_VER_45:
4386 case RTL_GIGA_MAC_VER_47:
4387 rtl8168h_1_hw_phy_config(tp);
4388 break;
4389 case RTL_GIGA_MAC_VER_46:
4390 case RTL_GIGA_MAC_VER_48:
4391 rtl8168h_2_hw_phy_config(tp);
4392 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004393
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004394 case RTL_GIGA_MAC_VER_49:
4395 rtl8168ep_1_hw_phy_config(tp);
4396 break;
4397 case RTL_GIGA_MAC_VER_50:
4398 case RTL_GIGA_MAC_VER_51:
4399 rtl8168ep_2_hw_phy_config(tp);
4400 break;
4401
Hayes Wangc5583862012-07-02 17:23:22 +08004402 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004403 default:
4404 break;
4405 }
4406}
4407
Francois Romieuda78dbf2012-01-26 14:18:23 +01004408static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 struct timer_list *timer = &tp->timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4412
Francois Romieubcf0bf92006-07-26 23:14:13 +02004413 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
françois romieu4da19632011-01-03 15:07:55 +00004415 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02004416 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 * A busy loop could burn quite a few cycles on nowadays CPU.
4418 * Let's delay the execution of the timer for a few ticks.
4419 */
4420 timeout = HZ/10;
4421 goto out_mod_timer;
4422 }
4423
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004424 if (tp->link_ok(tp))
Francois Romieuda78dbf2012-01-26 14:18:23 +01004425 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02004427 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
françois romieu4da19632011-01-03 15:07:55 +00004429 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
4431out_mod_timer:
4432 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004433}
4434
4435static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4436{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004437 if (!test_and_set_bit(flag, tp->wk.flags))
4438 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004439}
4440
Kees Cook9de36cc2017-10-25 03:53:12 -07004441static void rtl8169_phy_timer(struct timer_list *t)
Francois Romieuda78dbf2012-01-26 14:18:23 +01004442{
Kees Cook9de36cc2017-10-25 03:53:12 -07004443 struct rtl8169_private *tp = from_timer(tp, t, timer);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004444
Francois Romieu98ddf982012-01-31 10:47:34 +01004445 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446}
4447
Francois Romieuffc46952012-07-06 14:19:23 +02004448DECLARE_RTL_COND(rtl_phy_reset_cond)
4449{
4450 return tp->phy_reset_pending(tp);
4451}
4452
Francois Romieubf793292006-11-01 00:53:05 +01004453static void rtl8169_phy_reset(struct net_device *dev,
4454 struct rtl8169_private *tp)
4455{
françois romieu4da19632011-01-03 15:07:55 +00004456 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004457 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004458}
4459
David S. Miller8decf862011-09-22 03:23:13 -04004460static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4461{
David S. Miller8decf862011-09-22 03:23:13 -04004462 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004463 (RTL_R8(tp, PHYstatus) & TBI_Enable);
David S. Miller8decf862011-09-22 03:23:13 -04004464}
4465
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004466static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467{
Francois Romieu5615d9f2007-08-17 17:50:46 +02004468 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004469
Marcus Sundberg773328942008-07-10 21:28:08 +02004470 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4471 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004472 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02004473 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004474
Francois Romieu6dccd162007-02-13 23:38:05 +01004475 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4476
4477 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4478 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004479
Francois Romieubcf0bf92006-07-26 23:14:13 +02004480 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004481 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004482 RTL_W8(tp, 0x82, 0x01);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004483 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004484 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004485 }
4486
Francois Romieubf793292006-11-01 00:53:05 +01004487 rtl8169_phy_reset(dev, tp);
4488
Oliver Neukum54405cd2011-01-06 21:55:13 +01004489 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004490 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4491 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4492 (tp->mii.supports_gmii ?
4493 ADVERTISED_1000baseT_Half |
4494 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004495
David S. Miller8decf862011-09-22 03:23:13 -04004496 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00004497 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004498}
4499
Francois Romieu773d2022007-01-31 23:47:43 +01004500static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4501{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004502 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004503
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004504 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00004505
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004506 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4507 RTL_R32(tp, MAC4);
françois romieu908ba2bf2010-04-26 11:42:58 +00004508
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004509 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4510 RTL_R32(tp, MAC0);
françois romieu908ba2bf2010-04-26 11:42:58 +00004511
françois romieu9ecb9aa2012-12-07 11:20:21 +00004512 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4513 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004514
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004515 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieu773d2022007-01-31 23:47:43 +01004516
Francois Romieuda78dbf2012-01-26 14:18:23 +01004517 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004518}
4519
4520static int rtl_set_mac_address(struct net_device *dev, void *p)
4521{
4522 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004523 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004524 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004525
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004526 ret = eth_mac_addr(dev, p);
4527 if (ret)
4528 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004529
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004530 pm_runtime_get_noresume(d);
4531
4532 if (pm_runtime_active(d))
4533 rtl_rar_set(tp, dev->dev_addr);
4534
4535 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004536
4537 return 0;
4538}
4539
Francois Romieu5f787a12006-08-17 13:02:36 +02004540static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4541{
4542 struct rtl8169_private *tp = netdev_priv(dev);
4543 struct mii_ioctl_data *data = if_mii(ifr);
4544
Francois Romieu8b4ab282008-11-19 22:05:25 -08004545 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4546}
Francois Romieu5f787a12006-08-17 13:02:36 +02004547
Francois Romieucecb5fd2011-04-01 10:21:07 +02004548static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4549 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004550{
Francois Romieu5f787a12006-08-17 13:02:36 +02004551 switch (cmd) {
4552 case SIOCGMIIPHY:
4553 data->phy_id = 32; /* Internal PHY */
4554 return 0;
4555
4556 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004557 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004558 return 0;
4559
4560 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004561 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004562 return 0;
4563 }
4564 return -EOPNOTSUPP;
4565}
4566
Francois Romieu8b4ab282008-11-19 22:05:25 -08004567static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4568{
4569 return -EOPNOTSUPP;
4570}
4571
Bill Pembertonbaf63292012-12-03 09:23:28 -05004572static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004573{
4574 struct mdio_ops *ops = &tp->mdio_ops;
4575
4576 switch (tp->mac_version) {
4577 case RTL_GIGA_MAC_VER_27:
4578 ops->write = r8168dp_1_mdio_write;
4579 ops->read = r8168dp_1_mdio_read;
4580 break;
françois romieue6de30d2011-01-03 15:08:37 +00004581 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004582 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004583 ops->write = r8168dp_2_mdio_write;
4584 ops->read = r8168dp_2_mdio_read;
4585 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004586 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004587 ops->write = r8168g_mdio_write;
4588 ops->read = r8168g_mdio_read;
4589 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004590 default:
4591 ops->write = r8169_mdio_write;
4592 ops->read = r8169_mdio_read;
4593 break;
4594 }
4595}
4596
hayeswange2409d82013-03-31 17:02:04 +00004597static void rtl_speed_down(struct rtl8169_private *tp)
4598{
4599 u32 adv;
4600 int lpa;
4601
4602 rtl_writephy(tp, 0x1f, 0x0000);
4603 lpa = rtl_readphy(tp, MII_LPA);
4604
4605 if (lpa & (LPA_10HALF | LPA_10FULL))
4606 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4607 else if (lpa & (LPA_100HALF | LPA_100FULL))
4608 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4609 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4610 else
4611 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4612 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4613 (tp->mii.supports_gmii ?
4614 ADVERTISED_1000baseT_Half |
4615 ADVERTISED_1000baseT_Full : 0);
4616
4617 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4618 adv);
4619}
4620
David S. Miller1805b2f2011-10-24 18:18:09 -04004621static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4622{
David S. Miller1805b2f2011-10-24 18:18:09 -04004623 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004624 case RTL_GIGA_MAC_VER_25:
4625 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004626 case RTL_GIGA_MAC_VER_29:
4627 case RTL_GIGA_MAC_VER_30:
4628 case RTL_GIGA_MAC_VER_32:
4629 case RTL_GIGA_MAC_VER_33:
4630 case RTL_GIGA_MAC_VER_34:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004631 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004632 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04004633 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4634 break;
4635 default:
4636 break;
4637 }
4638}
4639
4640static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4641{
4642 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4643 return false;
4644
hayeswange2409d82013-03-31 17:02:04 +00004645 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004646 rtl_wol_suspend_quirk(tp);
4647
4648 return true;
4649}
4650
françois romieu065c27c2011-01-03 15:08:12 +00004651static void r8168_phy_power_up(struct rtl8169_private *tp)
4652{
4653 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004654 switch (tp->mac_version) {
4655 case RTL_GIGA_MAC_VER_11:
4656 case RTL_GIGA_MAC_VER_12:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004657 case RTL_GIGA_MAC_VER_17 ... RTL_GIGA_MAC_VER_28:
hayeswang01dc7fe2011-03-21 01:50:28 +00004658 case RTL_GIGA_MAC_VER_31:
4659 rtl_writephy(tp, 0x0e, 0x0000);
4660 break;
4661 default:
4662 break;
4663 }
françois romieu065c27c2011-01-03 15:08:12 +00004664 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4665}
4666
4667static void r8168_phy_power_down(struct rtl8169_private *tp)
4668{
4669 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004670 switch (tp->mac_version) {
4671 case RTL_GIGA_MAC_VER_32:
4672 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004673 case RTL_GIGA_MAC_VER_40:
4674 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004675 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4676 break;
4677
4678 case RTL_GIGA_MAC_VER_11:
4679 case RTL_GIGA_MAC_VER_12:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004680 case RTL_GIGA_MAC_VER_17 ... RTL_GIGA_MAC_VER_28:
hayeswang01dc7fe2011-03-21 01:50:28 +00004681 case RTL_GIGA_MAC_VER_31:
4682 rtl_writephy(tp, 0x0e, 0x0200);
4683 default:
4684 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4685 break;
4686 }
françois romieu065c27c2011-01-03 15:08:12 +00004687}
4688
4689static void r8168_pll_power_down(struct rtl8169_private *tp)
4690{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01004691 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004692 return;
4693
hayeswang01dc7fe2011-03-21 01:50:28 +00004694 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4695 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004696 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004697
David S. Miller1805b2f2011-10-24 18:18:09 -04004698 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004699 return;
françois romieu065c27c2011-01-03 15:08:12 +00004700
4701 r8168_phy_power_down(tp);
4702
4703 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004704 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004705 case RTL_GIGA_MAC_VER_37:
4706 case RTL_GIGA_MAC_VER_39:
4707 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004708 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004709 case RTL_GIGA_MAC_VER_45:
4710 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004711 case RTL_GIGA_MAC_VER_47:
4712 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004713 case RTL_GIGA_MAC_VER_50:
4714 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004715 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004716 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004717 case RTL_GIGA_MAC_VER_40:
4718 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004719 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004720 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004721 0xfc000000, ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004722 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004723 break;
françois romieu065c27c2011-01-03 15:08:12 +00004724 }
4725}
4726
4727static void r8168_pll_power_up(struct rtl8169_private *tp)
4728{
françois romieu065c27c2011-01-03 15:08:12 +00004729 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004730 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004731 case RTL_GIGA_MAC_VER_37:
4732 case RTL_GIGA_MAC_VER_39:
4733 case RTL_GIGA_MAC_VER_43:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004734 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004735 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004736 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004737 case RTL_GIGA_MAC_VER_45:
4738 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004739 case RTL_GIGA_MAC_VER_47:
4740 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004741 case RTL_GIGA_MAC_VER_50:
4742 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004743 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004744 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004745 case RTL_GIGA_MAC_VER_40:
4746 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004747 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004748 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004749 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004750 0x00000000, ERIAR_EXGMAC);
4751 break;
françois romieu065c27c2011-01-03 15:08:12 +00004752 }
4753
4754 r8168_phy_power_up(tp);
4755}
4756
françois romieu065c27c2011-01-03 15:08:12 +00004757static void rtl_pll_power_down(struct rtl8169_private *tp)
4758{
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004759 switch (tp->mac_version) {
4760 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4761 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4762 break;
4763 default:
4764 r8168_pll_power_down(tp);
4765 }
françois romieu065c27c2011-01-03 15:08:12 +00004766}
4767
4768static void rtl_pll_power_up(struct rtl8169_private *tp)
4769{
françois romieu065c27c2011-01-03 15:08:12 +00004770 switch (tp->mac_version) {
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004771 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4772 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
françois romieu065c27c2011-01-03 15:08:12 +00004773 break;
françois romieu065c27c2011-01-03 15:08:12 +00004774 default:
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004775 r8168_pll_power_up(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004776 }
4777}
4778
Hayes Wange542a222011-07-06 15:58:04 +08004779static void rtl_init_rxcfg(struct rtl8169_private *tp)
4780{
Hayes Wange542a222011-07-06 15:58:04 +08004781 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004782 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4783 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004784 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004785 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004786 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004787 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02004788 case RTL_GIGA_MAC_VER_35:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004789 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004790 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004791 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004792 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00004793 break;
Hayes Wange542a222011-07-06 15:58:04 +08004794 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004795 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004796 break;
4797 }
4798}
4799
Hayes Wang92fc43b2011-07-06 15:58:03 +08004800static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4801{
Timo Teräs9fba0812013-01-15 21:01:24 +00004802 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004803}
4804
Francois Romieud58d46b2011-05-03 16:38:29 +02004805static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4806{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004807 if (tp->jumbo_ops.enable) {
4808 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4809 tp->jumbo_ops.enable(tp);
4810 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4811 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004812}
4813
4814static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4815{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004816 if (tp->jumbo_ops.disable) {
4817 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4818 tp->jumbo_ops.disable(tp);
4819 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4820 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004821}
4822
4823static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4824{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004825 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4826 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004827 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004828}
4829
4830static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4831{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004832 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4833 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004834 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004835}
4836
4837static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4838{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004839 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004840}
4841
4842static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4843{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004844 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004845}
4846
4847static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4848{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004849 RTL_W8(tp, MaxTxPacketSize, 0x3f);
4850 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4851 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004852 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004853}
4854
4855static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4856{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004857 RTL_W8(tp, MaxTxPacketSize, 0x0c);
4858 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4859 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004860 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004861}
4862
4863static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4864{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004865 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01004866 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004867}
4868
4869static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4870{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004871 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004872 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004873}
4874
4875static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4876{
Francois Romieud58d46b2011-05-03 16:38:29 +02004877 r8168b_0_hw_jumbo_enable(tp);
4878
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004879 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004880}
4881
4882static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4883{
Francois Romieud58d46b2011-05-03 16:38:29 +02004884 r8168b_0_hw_jumbo_disable(tp);
4885
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004886 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004887}
4888
Bill Pembertonbaf63292012-12-03 09:23:28 -05004889static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02004890{
4891 struct jumbo_ops *ops = &tp->jumbo_ops;
4892
4893 switch (tp->mac_version) {
4894 case RTL_GIGA_MAC_VER_11:
4895 ops->disable = r8168b_0_hw_jumbo_disable;
4896 ops->enable = r8168b_0_hw_jumbo_enable;
4897 break;
4898 case RTL_GIGA_MAC_VER_12:
4899 case RTL_GIGA_MAC_VER_17:
4900 ops->disable = r8168b_1_hw_jumbo_disable;
4901 ops->enable = r8168b_1_hw_jumbo_enable;
4902 break;
4903 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4904 case RTL_GIGA_MAC_VER_19:
4905 case RTL_GIGA_MAC_VER_20:
4906 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4907 case RTL_GIGA_MAC_VER_22:
4908 case RTL_GIGA_MAC_VER_23:
4909 case RTL_GIGA_MAC_VER_24:
4910 case RTL_GIGA_MAC_VER_25:
4911 case RTL_GIGA_MAC_VER_26:
4912 ops->disable = r8168c_hw_jumbo_disable;
4913 ops->enable = r8168c_hw_jumbo_enable;
4914 break;
4915 case RTL_GIGA_MAC_VER_27:
4916 case RTL_GIGA_MAC_VER_28:
4917 ops->disable = r8168dp_hw_jumbo_disable;
4918 ops->enable = r8168dp_hw_jumbo_enable;
4919 break;
4920 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4921 case RTL_GIGA_MAC_VER_32:
4922 case RTL_GIGA_MAC_VER_33:
4923 case RTL_GIGA_MAC_VER_34:
4924 ops->disable = r8168e_hw_jumbo_disable;
4925 ops->enable = r8168e_hw_jumbo_enable;
4926 break;
4927
4928 /*
4929 * No action needed for jumbo frames with 8169.
4930 * No jumbo for 810x at all.
4931 */
Heiner Kallweit2a718832018-05-02 21:39:49 +02004932 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02004933 default:
4934 ops->disable = NULL;
4935 ops->enable = NULL;
4936 break;
4937 }
4938}
4939
Francois Romieuffc46952012-07-06 14:19:23 +02004940DECLARE_RTL_COND(rtl_chipcmd_cond)
4941{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004942 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02004943}
4944
Francois Romieu6f43adc2011-04-29 15:05:51 +02004945static void rtl_hw_reset(struct rtl8169_private *tp)
4946{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004947 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004948
Francois Romieuffc46952012-07-06 14:19:23 +02004949 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004950}
4951
Francois Romieub6ffd972011-06-17 17:00:05 +02004952static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4953{
4954 struct rtl_fw *rtl_fw;
4955 const char *name;
4956 int rc = -ENOMEM;
4957
4958 name = rtl_lookup_firmware_name(tp);
4959 if (!name)
4960 goto out_no_firmware;
4961
4962 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4963 if (!rtl_fw)
4964 goto err_warn;
4965
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004966 rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
Francois Romieub6ffd972011-06-17 17:00:05 +02004967 if (rc < 0)
4968 goto err_free;
4969
Francois Romieufd112f22011-06-18 00:10:29 +02004970 rc = rtl_check_firmware(tp, rtl_fw);
4971 if (rc < 0)
4972 goto err_release_firmware;
4973
Francois Romieub6ffd972011-06-17 17:00:05 +02004974 tp->rtl_fw = rtl_fw;
4975out:
4976 return;
4977
Francois Romieufd112f22011-06-18 00:10:29 +02004978err_release_firmware:
4979 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004980err_free:
4981 kfree(rtl_fw);
4982err_warn:
4983 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4984 name, rc);
4985out_no_firmware:
4986 tp->rtl_fw = NULL;
4987 goto out;
4988}
4989
François Romieu953a12c2011-04-24 17:38:48 +02004990static void rtl_request_firmware(struct rtl8169_private *tp)
4991{
Francois Romieub6ffd972011-06-17 17:00:05 +02004992 if (IS_ERR(tp->rtl_fw))
4993 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004994}
4995
Hayes Wang92fc43b2011-07-06 15:58:03 +08004996static void rtl_rx_close(struct rtl8169_private *tp)
4997{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004998 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004999}
5000
Francois Romieuffc46952012-07-06 14:19:23 +02005001DECLARE_RTL_COND(rtl_npq_cond)
5002{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005003 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02005004}
5005
5006DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5007{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005008 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02005009}
5010
françois romieue6de30d2011-01-03 15:08:37 +00005011static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012{
5013 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00005014 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
Hayes Wang92fc43b2011-07-06 15:58:03 +08005016 rtl_rx_close(tp);
5017
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02005018 switch (tp->mac_version) {
5019 case RTL_GIGA_MAC_VER_27:
5020 case RTL_GIGA_MAC_VER_28:
5021 case RTL_GIGA_MAC_VER_31:
Francois Romieuffc46952012-07-06 14:19:23 +02005022 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02005023 break;
5024 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
5025 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005026 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02005027 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02005028 break;
5029 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005030 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005031 udelay(100);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02005032 break;
françois romieue6de30d2011-01-03 15:08:37 +00005033 }
5034
Hayes Wang92fc43b2011-07-06 15:58:03 +08005035 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036}
5037
Francois Romieu7f796d832007-06-11 23:04:41 +02005038static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005039{
Francois Romieu9cb427b2006-11-02 00:10:16 +01005040 /* Set DMA burst size and Interframe Gap Time */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005041 RTL_W32(tp, TxConfig, (TX_DMA_BURST << TxDMAShift) |
Francois Romieu9cb427b2006-11-02 00:10:16 +01005042 (InterFrameGap << TxInterFrameGapShift));
5043}
5044
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02005045static void rtl_set_rx_max_size(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02005047 /* Low hurts. Let's disable the filtering. */
5048 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
Francois Romieu07ce4062007-02-23 23:36:39 +01005049}
5050
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005051static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02005052{
5053 /*
5054 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5055 * register to be written before TxDescAddrLow to work.
5056 * Switching from MMIO to I/O access fixes the issue as well.
5057 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005058 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
5059 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
5060 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
5061 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005062}
5063
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02005064static void rtl_hw_start(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02005065{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02005066 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
5067
5068 tp->hw_start(tp);
5069
5070 rtl_set_rx_max_size(tp);
5071 rtl_set_rx_tx_desc_registers(tp);
5072 rtl_set_rx_tx_config_registers(tp);
5073 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
5074
5075 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5076 RTL_R8(tp, IntrMask);
5077 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
5078 /* no early-rx interrupts */
5079 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
5080 rtl_irq_enable_all(tp);
Francois Romieu7f796d832007-06-11 23:04:41 +02005081}
5082
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005083static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01005084{
Francois Romieu37441002011-06-17 22:58:54 +02005085 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01005086 u32 mac_version;
5087 u32 clk;
5088 u32 val;
5089 } cfg2_info [] = {
5090 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5091 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5092 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5093 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02005094 };
5095 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01005096 unsigned int i;
5097 u32 clk;
5098
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005099 clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01005100 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01005101 if ((p->mac_version == mac_version) && (p->clk == clk)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005102 RTL_W32(tp, 0x7c, p->val);
Francois Romieu6dccd162007-02-13 23:38:05 +01005103 break;
5104 }
5105 }
5106}
5107
Francois Romieue6b763e2012-03-08 09:35:39 +01005108static void rtl_set_rx_mode(struct net_device *dev)
5109{
5110 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieue6b763e2012-03-08 09:35:39 +01005111 u32 mc_filter[2]; /* Multicast hash filter */
5112 int rx_mode;
5113 u32 tmp = 0;
5114
5115 if (dev->flags & IFF_PROMISC) {
5116 /* Unconditionally log net taps. */
5117 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5118 rx_mode =
5119 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5120 AcceptAllPhys;
5121 mc_filter[1] = mc_filter[0] = 0xffffffff;
5122 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5123 (dev->flags & IFF_ALLMULTI)) {
5124 /* Too many to filter perfectly -- accept all multicasts. */
5125 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5126 mc_filter[1] = mc_filter[0] = 0xffffffff;
5127 } else {
5128 struct netdev_hw_addr *ha;
5129
5130 rx_mode = AcceptBroadcast | AcceptMyPhys;
5131 mc_filter[1] = mc_filter[0] = 0;
5132 netdev_for_each_mc_addr(ha, dev) {
5133 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5134 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5135 rx_mode |= AcceptMulticast;
5136 }
5137 }
5138
5139 if (dev->features & NETIF_F_RXALL)
5140 rx_mode |= (AcceptErr | AcceptRunt);
5141
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005142 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Francois Romieue6b763e2012-03-08 09:35:39 +01005143
5144 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5145 u32 data = mc_filter[0];
5146
5147 mc_filter[0] = swab32(mc_filter[1]);
5148 mc_filter[1] = swab32(data);
5149 }
5150
Nathan Walp04817762012-11-01 12:08:47 +00005151 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5152 mc_filter[1] = mc_filter[0] = 0xffffffff;
5153
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005154 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
5155 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01005156
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005157 RTL_W32(tp, RxConfig, tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01005158}
5159
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005160static void rtl_hw_start_8169(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005161{
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005162 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005163 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005164
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005165 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005167 tp->cp_cmd |= PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005168
Francois Romieucecb5fd2011-04-01 10:21:07 +02005169 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5170 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005171 dprintk("Set MAC Reg C+CR Offset 0xe0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005173 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 }
5175
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005176 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005177
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005178 rtl8169_set_magic_reg(tp, tp->mac_version);
Francois Romieu6dccd162007-02-13 23:38:05 +01005179
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 /*
5181 * Undocumented corner. Supposedly:
5182 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5183 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005184 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005186 RTL_W32(tp, RxMissed, 0);
Francois Romieu07ce4062007-02-23 23:36:39 +01005187}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188
Francois Romieuffc46952012-07-06 14:19:23 +02005189DECLARE_RTL_COND(rtl_csiar_cond)
5190{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005191 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02005192}
5193
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005194static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005195{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005196 u32 func = PCI_FUNC(tp->pci_dev->devfn);
5197
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005198 RTL_W32(tp, CSIDR, value);
5199 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005200 CSIAR_BYTE_ENABLE | func << 16);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005201
Francois Romieuffc46952012-07-06 14:19:23 +02005202 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005203}
5204
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005205static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005206{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005207 u32 func = PCI_FUNC(tp->pci_dev->devfn);
5208
5209 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
5210 CSIAR_BYTE_ENABLE);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005211
Francois Romieuffc46952012-07-06 14:19:23 +02005212 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005213 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005214}
5215
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005216static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005217{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005218 struct pci_dev *pdev = tp->pci_dev;
5219 u32 csi;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005220
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005221 /* According to Realtek the value at config space address 0x070f
5222 * controls the L0s/L1 entrance latency. We try standard ECAM access
5223 * first and if it fails fall back to CSI.
5224 */
5225 if (pdev->cfg_size > 0x070f &&
5226 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
5227 return;
5228
5229 netdev_notice_once(tp->dev,
5230 "No native access to PCI extended config space, falling back to CSI\n");
5231 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5232 rtl_csi_write(tp, 0x070c, csi | val << 24);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005233}
5234
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005235static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005236{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005237 rtl_csi_access_enable(tp, 0x17);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005238}
5239
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005240static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
hayeswang45dd95c2013-07-08 17:09:01 +08005241{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005242 rtl_csi_access_enable(tp, 0x27);
Francois Romieudacf8152008-08-02 20:44:13 +02005243}
5244
5245struct ephy_info {
5246 unsigned int offset;
5247 u16 mask;
5248 u16 bits;
5249};
5250
Francois Romieufdf6fc02012-07-06 22:40:38 +02005251static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5252 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005253{
5254 u16 w;
5255
5256 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005257 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5258 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005259 e++;
5260 }
5261}
5262
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005263static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02005264{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005265 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005266 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005267}
5268
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005269static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005270{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005271 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005272 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005273}
5274
hayeswangb51ecea2014-07-09 14:52:51 +08005275static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5276{
hayeswangb51ecea2014-07-09 14:52:51 +08005277 u8 data;
5278
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005279 data = RTL_R8(tp, Config3);
hayeswangb51ecea2014-07-09 14:52:51 +08005280
5281 if (enable)
5282 data |= Rdy_to_L23;
5283 else
5284 data &= ~Rdy_to_L23;
5285
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005286 RTL_W8(tp, Config3, data);
hayeswangb51ecea2014-07-09 14:52:51 +08005287}
5288
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005289static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005290{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005291 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005292
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005293 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005294 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieub726e492008-06-28 12:22:59 +02005295
françois romieufaf1e782013-02-27 13:01:57 +00005296 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005297 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00005298 PCI_EXP_DEVCTL_NOSNOOP_EN);
5299 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005300}
5301
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005302static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005303{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005304 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005305
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005306 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005307
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005308 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005309}
5310
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005311static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005312{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005313 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02005314
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005315 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005316
françois romieufaf1e782013-02-27 13:01:57 +00005317 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005318 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02005319
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005320 rtl_disable_clock_request(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005321
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005322 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005323 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu219a1e92008-06-28 11:58:39 +02005324}
5325
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005326static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005327{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005328 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005329 { 0x01, 0, 0x0001 },
5330 { 0x02, 0x0800, 0x1000 },
5331 { 0x03, 0, 0x0042 },
5332 { 0x06, 0x0080, 0x0000 },
5333 { 0x07, 0, 0x2000 }
5334 };
5335
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005336 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005337
Francois Romieufdf6fc02012-07-06 22:40:38 +02005338 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005339
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005340 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005341}
5342
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005343static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005344{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005345 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005346
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005347 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02005348
françois romieufaf1e782013-02-27 13:01:57 +00005349 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005350 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieuef3386f2008-06-29 12:24:30 +02005351
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005352 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005353 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieuef3386f2008-06-29 12:24:30 +02005354}
5355
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005356static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005357{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005358 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005359
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005360 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005361
5362 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005363 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005364
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005365 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005366
françois romieufaf1e782013-02-27 13:01:57 +00005367 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005368 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005369
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005370 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005371 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005372}
5373
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005374static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005375{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005376 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005377 { 0x02, 0x0800, 0x1000 },
5378 { 0x03, 0, 0x0002 },
5379 { 0x06, 0x0080, 0x0000 }
5380 };
5381
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005382 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005383
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005384 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02005385
Francois Romieufdf6fc02012-07-06 22:40:38 +02005386 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005387
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005388 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005389}
5390
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005391static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005392{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005393 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005394 { 0x01, 0, 0x0001 },
5395 { 0x03, 0x0400, 0x0220 }
5396 };
5397
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005398 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005399
Francois Romieufdf6fc02012-07-06 22:40:38 +02005400 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005401
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005402 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005403}
5404
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005405static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005406{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005407 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005408}
5409
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005410static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005411{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005412 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005413
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005414 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005415}
5416
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005417static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005418{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005419 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005420
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005421 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005422
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005423 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005424
françois romieufaf1e782013-02-27 13:01:57 +00005425 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005426 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02005427
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005428 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005429 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu5b538df2008-07-20 16:22:45 +02005430}
5431
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005432static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005433{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005434 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005435
françois romieufaf1e782013-02-27 13:01:57 +00005436 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005437 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang4804b3b2011-03-21 01:50:29 +00005438
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005439 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang4804b3b2011-03-21 01:50:29 +00005440
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005441 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005442}
5443
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005444static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005445{
5446 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005447 { 0x0b, 0x0000, 0x0048 },
5448 { 0x19, 0x0020, 0x0050 },
5449 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00005450 };
françois romieue6de30d2011-01-03 15:08:37 +00005451
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005452 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005453
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005454 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00005455
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005456 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
françois romieue6de30d2011-01-03 15:08:37 +00005457
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005458 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00005459
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005460 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005461}
5462
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005463static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005464{
Hayes Wang70090422011-07-06 15:58:06 +08005465 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005466 { 0x00, 0x0200, 0x0100 },
5467 { 0x00, 0x0000, 0x0004 },
5468 { 0x06, 0x0002, 0x0001 },
5469 { 0x06, 0x0000, 0x0030 },
5470 { 0x07, 0x0000, 0x2000 },
5471 { 0x00, 0x0000, 0x0020 },
5472 { 0x03, 0x5800, 0x2000 },
5473 { 0x03, 0x0000, 0x0001 },
5474 { 0x01, 0x0800, 0x1000 },
5475 { 0x07, 0x0000, 0x4000 },
5476 { 0x1e, 0x0000, 0x2000 },
5477 { 0x19, 0xffff, 0xfe6c },
5478 { 0x0a, 0x0000, 0x0040 }
5479 };
5480
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005481 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005482
Francois Romieufdf6fc02012-07-06 22:40:38 +02005483 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005484
françois romieufaf1e782013-02-27 13:01:57 +00005485 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005486 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00005487
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005488 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang01dc7fe2011-03-21 01:50:28 +00005489
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005490 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005491
5492 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005493 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
5494 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005495
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005496 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005497}
5498
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005499static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005500{
5501 static const struct ephy_info e_info_8168e_2[] = {
5502 { 0x09, 0x0000, 0x0080 },
5503 { 0x19, 0x0000, 0x0224 }
5504 };
5505
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005506 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005507
Francois Romieufdf6fc02012-07-06 22:40:38 +02005508 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005509
françois romieufaf1e782013-02-27 13:01:57 +00005510 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005511 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08005512
Francois Romieufdf6fc02012-07-06 22:40:38 +02005513 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5514 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5515 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5516 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5517 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5518 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005519 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5520 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005521
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005522 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005523
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005524 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005525
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005526 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5527 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08005528
5529 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005530 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang70090422011-07-06 15:58:06 +08005531
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005532 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5533 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5534 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08005535}
5536
Hayes Wang5f886e02012-03-30 14:33:03 +08005537static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005538{
Hayes Wang5f886e02012-03-30 14:33:03 +08005539 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005540
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005541 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08005542
Francois Romieufdf6fc02012-07-06 22:40:38 +02005543 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5544 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5545 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5546 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005547 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5548 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5549 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5550 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005551 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5552 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005553
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005554 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc2218922011-09-06 16:55:18 +08005555
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005556 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005557
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005558 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5559 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5560 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5561 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5562 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005563}
5564
Hayes Wang5f886e02012-03-30 14:33:03 +08005565static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5566{
Hayes Wang5f886e02012-03-30 14:33:03 +08005567 static const struct ephy_info e_info_8168f_1[] = {
5568 { 0x06, 0x00c0, 0x0020 },
5569 { 0x08, 0x0001, 0x0002 },
5570 { 0x09, 0x0000, 0x0080 },
5571 { 0x19, 0x0000, 0x0224 }
5572 };
5573
5574 rtl_hw_start_8168f(tp);
5575
Francois Romieufdf6fc02012-07-06 22:40:38 +02005576 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005577
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005578 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005579
5580 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005581 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang5f886e02012-03-30 14:33:03 +08005582}
5583
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005584static void rtl_hw_start_8411(struct rtl8169_private *tp)
5585{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005586 static const struct ephy_info e_info_8168f_1[] = {
5587 { 0x06, 0x00c0, 0x0020 },
5588 { 0x0f, 0xffff, 0x5200 },
5589 { 0x1e, 0x0000, 0x4000 },
5590 { 0x19, 0x0000, 0x0224 }
5591 };
5592
5593 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08005594 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005595
Francois Romieufdf6fc02012-07-06 22:40:38 +02005596 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005597
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005598 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005599}
5600
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005601static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08005602{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005603 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
hayeswangbeb330a2013-04-01 22:23:39 +00005604
Hayes Wangc5583862012-07-02 17:23:22 +08005605 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5606 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5607 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5608 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5609
5610 rtl_csi_access_enable_1(tp);
5611
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005612 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08005613
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005614 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5615 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00005616 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005617
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005618 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5619 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc5583862012-07-02 17:23:22 +08005620
5621 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5622 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5623
5624 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005625 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wangc5583862012-07-02 17:23:22 +08005626
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005627 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5628 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005629
5630 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08005631}
5632
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005633static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5634{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005635 static const struct ephy_info e_info_8168g_1[] = {
5636 { 0x00, 0x0000, 0x0008 },
5637 { 0x0c, 0x37d0, 0x0820 },
5638 { 0x1e, 0x0000, 0x0001 },
5639 { 0x19, 0x8000, 0x0000 }
5640 };
5641
5642 rtl_hw_start_8168g(tp);
5643
5644 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005645 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5646 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005647 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
5648}
5649
hayeswang57538c42013-04-01 22:23:40 +00005650static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5651{
hayeswang57538c42013-04-01 22:23:40 +00005652 static const struct ephy_info e_info_8168g_2[] = {
5653 { 0x00, 0x0000, 0x0008 },
5654 { 0x0c, 0x3df0, 0x0200 },
5655 { 0x19, 0xffff, 0xfc00 },
5656 { 0x1e, 0xffff, 0x20eb }
5657 };
5658
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005659 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00005660
5661 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005662 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5663 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang57538c42013-04-01 22:23:40 +00005664 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5665}
5666
hayeswang45dd95c2013-07-08 17:09:01 +08005667static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5668{
hayeswang45dd95c2013-07-08 17:09:01 +08005669 static const struct ephy_info e_info_8411_2[] = {
5670 { 0x00, 0x0000, 0x0008 },
5671 { 0x0c, 0x3df0, 0x0200 },
5672 { 0x0f, 0xffff, 0x5200 },
5673 { 0x19, 0x0020, 0x0000 },
5674 { 0x1e, 0x0000, 0x2000 }
5675 };
5676
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005677 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08005678
5679 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005680 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5681 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang45dd95c2013-07-08 17:09:01 +08005682 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
5683}
5684
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005685static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
5686{
Andrzej Hajda72521ea2015-09-24 16:00:24 +02005687 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005688 u32 data;
5689 static const struct ephy_info e_info_8168h_1[] = {
5690 { 0x1e, 0x0800, 0x0001 },
5691 { 0x1d, 0x0000, 0x0800 },
5692 { 0x05, 0xffff, 0x2089 },
5693 { 0x06, 0xffff, 0x5881 },
5694 { 0x04, 0xffff, 0x154a },
5695 { 0x01, 0xffff, 0x068b }
5696 };
5697
5698 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005699 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5700 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005701 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
5702
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005703 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005704
5705 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5706 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5707 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5708 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5709
5710 rtl_csi_access_enable_1(tp);
5711
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005712 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005713
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005714 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5715 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005716
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005717 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005718
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005719 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005720
5721 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5722
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005723 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5724 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005725
5726 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5727 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5728
5729 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005730 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005731
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005732 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5733 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005734
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005735 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005736
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005737 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005738
5739 rtl_pcie_state_l2l3_enable(tp, false);
5740
5741 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08005742 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005743 rtl_writephy(tp, 0x1f, 0x0000);
5744 if (rg_saw_cnt > 0) {
5745 u16 sw_cnt_1ms_ini;
5746
5747 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
5748 sw_cnt_1ms_ini &= 0x0fff;
5749 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005750 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005751 data |= sw_cnt_1ms_ini;
5752 r8168_mac_ocp_write(tp, 0xd412, data);
5753 }
5754
5755 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005756 data &= ~0xf0;
5757 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005758 r8168_mac_ocp_write(tp, 0xe056, data);
5759
5760 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005761 data &= ~0x6000;
5762 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005763 r8168_mac_ocp_write(tp, 0xe052, data);
5764
5765 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005766 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005767 data |= 0x017f;
5768 r8168_mac_ocp_write(tp, 0xe0d6, data);
5769
5770 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005771 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005772 data |= 0x047f;
5773 r8168_mac_ocp_write(tp, 0xd420, data);
5774
5775 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
5776 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
5777 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
5778 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
5779}
5780
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005781static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
5782{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08005783 rtl8168ep_stop_cmac(tp);
5784
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005785 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005786
5787 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5788 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
5789 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
5790 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5791
5792 rtl_csi_access_enable_1(tp);
5793
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005794 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005795
5796 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5797 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5798
5799 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
5800
5801 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5802
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005803 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5804 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005805
5806 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5807 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5808
5809 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005810 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005811
5812 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5813
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005814 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005815
5816 rtl_pcie_state_l2l3_enable(tp, false);
5817}
5818
5819static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
5820{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005821 static const struct ephy_info e_info_8168ep_1[] = {
5822 { 0x00, 0xffff, 0x10ab },
5823 { 0x06, 0xffff, 0xf030 },
5824 { 0x08, 0xffff, 0x2006 },
5825 { 0x0d, 0xffff, 0x1666 },
5826 { 0x0c, 0x3ff0, 0x0000 }
5827 };
5828
5829 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005830 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5831 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005832 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
5833
5834 rtl_hw_start_8168ep(tp);
5835}
5836
5837static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
5838{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005839 static const struct ephy_info e_info_8168ep_2[] = {
5840 { 0x00, 0xffff, 0x10a3 },
5841 { 0x19, 0xffff, 0xfc00 },
5842 { 0x1e, 0xffff, 0x20ea }
5843 };
5844
5845 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005846 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5847 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005848 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
5849
5850 rtl_hw_start_8168ep(tp);
5851
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005852 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5853 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005854}
5855
5856static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
5857{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005858 u32 data;
5859 static const struct ephy_info e_info_8168ep_3[] = {
5860 { 0x00, 0xffff, 0x10a3 },
5861 { 0x19, 0xffff, 0x7c00 },
5862 { 0x1e, 0xffff, 0x20eb },
5863 { 0x0d, 0xffff, 0x1666 }
5864 };
5865
5866 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005867 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5868 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005869 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
5870
5871 rtl_hw_start_8168ep(tp);
5872
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005873 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5874 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005875
5876 data = r8168_mac_ocp_read(tp, 0xd3e2);
5877 data &= 0xf000;
5878 data |= 0x0271;
5879 r8168_mac_ocp_write(tp, 0xd3e2, data);
5880
5881 data = r8168_mac_ocp_read(tp, 0xd3e4);
5882 data &= 0xff00;
5883 r8168_mac_ocp_write(tp, 0xd3e4, data);
5884
5885 data = r8168_mac_ocp_read(tp, 0xe860);
5886 data |= 0x0080;
5887 r8168_mac_ocp_write(tp, 0xe860, data);
5888}
5889
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005890static void rtl_hw_start_8168(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005891{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005892 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02005893
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005894 tp->cp_cmd &= ~INTT_MASK;
5895 tp->cp_cmd |= PktCntrDisable | INTT_1;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005896 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu2dd99532007-06-11 23:22:52 +02005897
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005898 RTL_W16(tp, IntrMitigate, 0x5151);
Francois Romieu0e485152007-02-20 00:00:26 +01005899
5900 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00005901 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005902 tp->event_slow |= RxFIFOOver | PCSTimeout;
5903 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01005904 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005905
Francois Romieu219a1e92008-06-28 11:58:39 +02005906 switch (tp->mac_version) {
5907 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005908 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005909 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005910
5911 case RTL_GIGA_MAC_VER_12:
5912 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005913 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005914 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005915
5916 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005917 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005918 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005919
5920 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005921 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005922 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005923
5924 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005925 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005926 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005927
Francois Romieu197ff762008-06-28 13:16:02 +02005928 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005929 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005930 break;
Francois Romieu197ff762008-06-28 13:16:02 +02005931
Francois Romieu6fb07052008-06-29 11:54:28 +02005932 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005933 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005934 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02005935
Francois Romieuef3386f2008-06-29 12:24:30 +02005936 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005937 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005938 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02005939
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005940 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005941 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005942 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005943
Francois Romieu5b538df2008-07-20 16:22:45 +02005944 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00005945 case RTL_GIGA_MAC_VER_26:
5946 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005947 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005948 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02005949
françois romieue6de30d2011-01-03 15:08:37 +00005950 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005951 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005952 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005953
hayeswang4804b3b2011-03-21 01:50:29 +00005954 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005955 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005956 break;
5957
hayeswang01dc7fe2011-03-21 01:50:28 +00005958 case RTL_GIGA_MAC_VER_32:
5959 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005960 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005961 break;
5962 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005963 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005964 break;
françois romieue6de30d2011-01-03 15:08:37 +00005965
Hayes Wangc2218922011-09-06 16:55:18 +08005966 case RTL_GIGA_MAC_VER_35:
5967 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005968 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005969 break;
5970
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005971 case RTL_GIGA_MAC_VER_38:
5972 rtl_hw_start_8411(tp);
5973 break;
5974
Hayes Wangc5583862012-07-02 17:23:22 +08005975 case RTL_GIGA_MAC_VER_40:
5976 case RTL_GIGA_MAC_VER_41:
5977 rtl_hw_start_8168g_1(tp);
5978 break;
hayeswang57538c42013-04-01 22:23:40 +00005979 case RTL_GIGA_MAC_VER_42:
5980 rtl_hw_start_8168g_2(tp);
5981 break;
Hayes Wangc5583862012-07-02 17:23:22 +08005982
hayeswang45dd95c2013-07-08 17:09:01 +08005983 case RTL_GIGA_MAC_VER_44:
5984 rtl_hw_start_8411_2(tp);
5985 break;
5986
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005987 case RTL_GIGA_MAC_VER_45:
5988 case RTL_GIGA_MAC_VER_46:
5989 rtl_hw_start_8168h_1(tp);
5990 break;
5991
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005992 case RTL_GIGA_MAC_VER_49:
5993 rtl_hw_start_8168ep_1(tp);
5994 break;
5995
5996 case RTL_GIGA_MAC_VER_50:
5997 rtl_hw_start_8168ep_2(tp);
5998 break;
5999
6000 case RTL_GIGA_MAC_VER_51:
6001 rtl_hw_start_8168ep_3(tp);
6002 break;
6003
Francois Romieu219a1e92008-06-28 11:58:39 +02006004 default:
6005 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006006 tp->dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00006007 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006008 }
Francois Romieu07ce4062007-02-23 23:36:39 +01006009}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006011static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006012{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006013 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02006014 { 0x01, 0, 0x6e65 },
6015 { 0x02, 0, 0x091f },
6016 { 0x03, 0, 0xc2f9 },
6017 { 0x06, 0, 0xafb5 },
6018 { 0x07, 0, 0x0e00 },
6019 { 0x19, 0, 0xec80 },
6020 { 0x01, 0, 0x2e65 },
6021 { 0x01, 0, 0x6e65 }
6022 };
6023 u8 cfg1;
6024
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006025 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006026
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006027 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006028
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006029 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006030
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006031 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02006032 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006033 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006034
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006035 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006036 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006037 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006038
Francois Romieufdf6fc02012-07-06 22:40:38 +02006039 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02006040}
6041
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006042static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006043{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006044 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006045
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006046 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006047
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006048 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
6049 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006050}
6051
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006052static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006053{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006054 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006055
Francois Romieufdf6fc02012-07-06 22:40:38 +02006056 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006057}
6058
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006059static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006060{
6061 static const struct ephy_info e_info_8105e_1[] = {
6062 { 0x07, 0, 0x4000 },
6063 { 0x19, 0, 0x0200 },
6064 { 0x19, 0, 0x0020 },
6065 { 0x1e, 0, 0x2000 },
6066 { 0x03, 0, 0x0001 },
6067 { 0x19, 0, 0x0100 },
6068 { 0x19, 0, 0x0004 },
6069 { 0x0a, 0, 0x0020 }
6070 };
6071
Francois Romieucecb5fd2011-04-01 10:21:07 +02006072 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006073 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006074
Francois Romieucecb5fd2011-04-01 10:21:07 +02006075 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006076 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006077
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006078 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6079 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006080
Francois Romieufdf6fc02012-07-06 22:40:38 +02006081 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08006082
6083 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006084}
6085
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006086static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006087{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006088 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006089 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006090}
6091
Hayes Wang7e18dca2012-03-30 14:33:02 +08006092static void rtl_hw_start_8402(struct rtl8169_private *tp)
6093{
Hayes Wang7e18dca2012-03-30 14:33:02 +08006094 static const struct ephy_info e_info_8402[] = {
6095 { 0x19, 0xffff, 0xff64 },
6096 { 0x1e, 0, 0x4000 }
6097 };
6098
6099 rtl_csi_access_enable_2(tp);
6100
6101 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006102 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006103
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006104 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
6105 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006106
Francois Romieufdf6fc02012-07-06 22:40:38 +02006107 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006108
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006109 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006110
Francois Romieufdf6fc02012-07-06 22:40:38 +02006111 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6112 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006113 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6114 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006115 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6116 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006117 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006118
6119 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006120}
6121
Hayes Wang5598bfe2012-07-02 17:23:21 +08006122static void rtl_hw_start_8106(struct rtl8169_private *tp)
6123{
Hayes Wang5598bfe2012-07-02 17:23:21 +08006124 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006125 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006126
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006127 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
6128 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6129 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006130
6131 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006132}
6133
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006134static void rtl_hw_start_8101(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01006135{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006136 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6137 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006138
Francois Romieucecb5fd2011-04-01 10:21:07 +02006139 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006140 tp->mac_version == RTL_GIGA_MAC_VER_16)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006141 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006142 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006143
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006144 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00006145
Heiner Kallweit12d42c52018-04-28 22:19:30 +02006146 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006147 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
hayeswang1a964642013-04-01 22:23:41 +00006148
Francois Romieu2857ffb2008-08-02 21:08:49 +02006149 switch (tp->mac_version) {
6150 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006151 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006152 break;
6153
6154 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006155 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006156 break;
6157
6158 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006159 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006160 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006161
6162 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006163 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006164 break;
6165 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006166 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006167 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006168
6169 case RTL_GIGA_MAC_VER_37:
6170 rtl_hw_start_8402(tp);
6171 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006172
6173 case RTL_GIGA_MAC_VER_39:
6174 rtl_hw_start_8106(tp);
6175 break;
hayeswang58152cd2013-04-01 22:23:42 +00006176 case RTL_GIGA_MAC_VER_43:
6177 rtl_hw_start_8168g_2(tp);
6178 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006179 case RTL_GIGA_MAC_VER_47:
6180 case RTL_GIGA_MAC_VER_48:
6181 rtl_hw_start_8168h_1(tp);
6182 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006183 }
6184
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006185 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186}
6187
6188static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6189{
Francois Romieud58d46b2011-05-03 16:38:29 +02006190 struct rtl8169_private *tp = netdev_priv(dev);
6191
Francois Romieud58d46b2011-05-03 16:38:29 +02006192 if (new_mtu > ETH_DATA_LEN)
6193 rtl_hw_jumbo_enable(tp);
6194 else
6195 rtl_hw_jumbo_disable(tp);
6196
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006198 netdev_update_features(dev);
6199
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201}
6202
6203static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6204{
Al Viro95e09182007-12-22 18:55:39 +00006205 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6207}
6208
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006209static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6210 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211{
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006212 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
6213 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006214
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006215 kfree(*data_buff);
6216 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 rtl8169_make_unusable_by_asic(desc);
6218}
6219
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006220static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221{
6222 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6223
Alexander Duycka0750132014-12-11 15:02:17 -08006224 /* Force memory writes to complete before releasing descriptor */
6225 dma_wmb();
6226
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006227 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228}
6229
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006230static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006232 return (void *)ALIGN((long)data, 16);
6233}
6234
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006235static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6236 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006237{
6238 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 dma_addr_t mapping;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006240 struct device *d = tp_to_dev(tp);
Heiner Kallweitd3b404c2018-04-17 23:22:14 +02006241 int node = dev_to_node(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006243 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006244 if (!data)
6245 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006246
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006247 if (rtl8169_align(data) != data) {
6248 kfree(data);
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006249 data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006250 if (!data)
6251 return NULL;
6252 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006253
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006254 mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006255 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006256 if (unlikely(dma_mapping_error(d, mapping))) {
6257 if (net_ratelimit())
6258 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006259 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261
Heiner Kallweitd731af72018-04-17 23:26:41 +02006262 desc->addr = cpu_to_le64(mapping);
6263 rtl8169_mark_to_asic(desc);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006264 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006265
6266err_out:
6267 kfree(data);
6268 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269}
6270
6271static void rtl8169_rx_clear(struct rtl8169_private *tp)
6272{
Francois Romieu07d3f512007-02-21 22:40:46 +01006273 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274
6275 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006276 if (tp->Rx_databuff[i]) {
6277 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278 tp->RxDescArray + i);
6279 }
6280 }
6281}
6282
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006283static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006285 desc->opts1 |= cpu_to_le32(RingEnd);
6286}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006287
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006288static int rtl8169_rx_fill(struct rtl8169_private *tp)
6289{
6290 unsigned int i;
6291
6292 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006293 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006294
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006295 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006296 if (!data) {
6297 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006298 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006299 }
6300 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006303 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6304 return 0;
6305
6306err_out:
6307 rtl8169_rx_clear(tp);
6308 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309}
6310
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006311static int rtl8169_init_ring(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 rtl8169_init_ring_indexes(tp);
6314
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006315 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
6316 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006318 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319}
6320
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006321static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 struct TxDesc *desc)
6323{
6324 unsigned int len = tx_skb->len;
6325
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006326 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6327
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 desc->opts1 = 0x00;
6329 desc->opts2 = 0x00;
6330 desc->addr = 0x00;
6331 tx_skb->len = 0;
6332}
6333
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006334static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6335 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336{
6337 unsigned int i;
6338
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006339 for (i = 0; i < n; i++) {
6340 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 struct ring_info *tx_skb = tp->tx_skb + entry;
6342 unsigned int len = tx_skb->len;
6343
6344 if (len) {
6345 struct sk_buff *skb = tx_skb->skb;
6346
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006347 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 tp->TxDescArray + entry);
6349 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006350 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 tx_skb->skb = NULL;
6352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 }
6354 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006355}
6356
6357static void rtl8169_tx_clear(struct rtl8169_private *tp)
6358{
6359 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 tp->cur_tx = tp->dirty_tx = 0;
6361}
6362
Francois Romieu4422bcd2012-01-26 11:23:32 +01006363static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364{
David Howellsc4028952006-11-22 14:57:56 +00006365 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01006366 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367
Francois Romieuda78dbf2012-01-26 14:18:23 +01006368 napi_disable(&tp->napi);
6369 netif_stop_queue(dev);
6370 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371
françois romieuc7c2c392011-12-04 20:30:52 +00006372 rtl8169_hw_reset(tp);
6373
Francois Romieu56de4142011-03-15 17:29:31 +01006374 for (i = 0; i < NUM_RX_DESC; i++)
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006375 rtl8169_mark_to_asic(tp->RxDescArray + i);
Francois Romieu56de4142011-03-15 17:29:31 +01006376
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00006378 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379
Francois Romieuda78dbf2012-01-26 14:18:23 +01006380 napi_enable(&tp->napi);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006381 rtl_hw_start(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01006382 netif_wake_queue(dev);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006383 rtl8169_check_link_status(dev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384}
6385
6386static void rtl8169_tx_timeout(struct net_device *dev)
6387{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006388 struct rtl8169_private *tp = netdev_priv(dev);
6389
6390 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391}
6392
6393static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07006394 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395{
6396 struct skb_shared_info *info = skb_shinfo(skb);
6397 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006398 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006399 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400
6401 entry = tp->cur_tx;
6402 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006403 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 dma_addr_t mapping;
6405 u32 status, len;
6406 void *addr;
6407
6408 entry = (entry + 1) % NUM_TX_DESC;
6409
6410 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00006411 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00006412 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006413 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006414 if (unlikely(dma_mapping_error(d, mapping))) {
6415 if (net_ratelimit())
6416 netif_err(tp, drv, tp->dev,
6417 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006418 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420
Francois Romieucecb5fd2011-04-01 10:21:07 +02006421 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006422 status = opts[0] | len |
6423 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424
6425 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07006426 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 txd->addr = cpu_to_le64(mapping);
6428
6429 tp->tx_skb[entry].len = len;
6430 }
6431
6432 if (cur_frag) {
6433 tp->tx_skb[entry].skb = skb;
6434 txd->opts1 |= cpu_to_le32(LastFrag);
6435 }
6436
6437 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006438
6439err_out:
6440 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6441 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442}
6443
françois romieub423e9a2013-05-18 01:24:46 +00006444static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6445{
6446 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6447}
6448
hayeswange9746042014-07-11 16:25:58 +08006449static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6450 struct net_device *dev);
6451/* r8169_csum_workaround()
6452 * The hw limites the value the transport offset. When the offset is out of the
6453 * range, calculate the checksum by sw.
6454 */
6455static void r8169_csum_workaround(struct rtl8169_private *tp,
6456 struct sk_buff *skb)
6457{
6458 if (skb_shinfo(skb)->gso_size) {
6459 netdev_features_t features = tp->dev->features;
6460 struct sk_buff *segs, *nskb;
6461
6462 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6463 segs = skb_gso_segment(skb, features);
6464 if (IS_ERR(segs) || !segs)
6465 goto drop;
6466
6467 do {
6468 nskb = segs;
6469 segs = segs->next;
6470 nskb->next = NULL;
6471 rtl8169_start_xmit(nskb, tp->dev);
6472 } while (segs);
6473
Alexander Duyckeb781392015-05-01 10:34:44 -07006474 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006475 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6476 if (skb_checksum_help(skb) < 0)
6477 goto drop;
6478
6479 rtl8169_start_xmit(skb, tp->dev);
6480 } else {
6481 struct net_device_stats *stats;
6482
6483drop:
6484 stats = &tp->dev->stats;
6485 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07006486 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006487 }
6488}
6489
6490/* msdn_giant_send_check()
6491 * According to the document of microsoft, the TCP Pseudo Header excludes the
6492 * packet length for IPv6 TCP large packets.
6493 */
6494static int msdn_giant_send_check(struct sk_buff *skb)
6495{
6496 const struct ipv6hdr *ipv6h;
6497 struct tcphdr *th;
6498 int ret;
6499
6500 ret = skb_cow_head(skb, 0);
6501 if (ret)
6502 return ret;
6503
6504 ipv6h = ipv6_hdr(skb);
6505 th = tcp_hdr(skb);
6506
6507 th->check = 0;
6508 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6509
6510 return ret;
6511}
6512
hayeswang5888d3f2014-07-11 16:25:56 +08006513static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6514 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515{
Michał Mirosław350fb322011-04-08 06:35:56 +00006516 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517
Francois Romieu2b7b4312011-04-18 22:53:24 -07006518 if (mss) {
6519 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08006520 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6521 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6522 const struct iphdr *ip = ip_hdr(skb);
6523
6524 if (ip->protocol == IPPROTO_TCP)
6525 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6526 else if (ip->protocol == IPPROTO_UDP)
6527 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6528 else
6529 WARN_ON_ONCE(1);
6530 }
6531
6532 return true;
6533}
6534
6535static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6536 struct sk_buff *skb, u32 *opts)
6537{
hayeswangbdfa4ed2014-07-11 16:25:57 +08006538 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08006539 u32 mss = skb_shinfo(skb)->gso_size;
6540
6541 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08006542 if (transport_offset > GTTCPHO_MAX) {
6543 netif_warn(tp, tx_err, tp->dev,
6544 "Invalid transport offset 0x%x for TSO\n",
6545 transport_offset);
6546 return false;
6547 }
6548
Heiner Kallweit4ff36462018-05-02 21:40:02 +02006549 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08006550 case htons(ETH_P_IP):
6551 opts[0] |= TD1_GTSENV4;
6552 break;
6553
6554 case htons(ETH_P_IPV6):
6555 if (msdn_giant_send_check(skb))
6556 return false;
6557
6558 opts[0] |= TD1_GTSENV6;
6559 break;
6560
6561 default:
6562 WARN_ON_ONCE(1);
6563 break;
6564 }
6565
hayeswangbdfa4ed2014-07-11 16:25:57 +08006566 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08006567 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006568 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08006569 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570
françois romieub423e9a2013-05-18 01:24:46 +00006571 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006572 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00006573
hayeswange9746042014-07-11 16:25:58 +08006574 if (transport_offset > TCPHO_MAX) {
6575 netif_warn(tp, tx_err, tp->dev,
6576 "Invalid transport offset 0x%x\n",
6577 transport_offset);
6578 return false;
6579 }
6580
Heiner Kallweit4ff36462018-05-02 21:40:02 +02006581 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08006582 case htons(ETH_P_IP):
6583 opts[1] |= TD1_IPv4_CS;
6584 ip_protocol = ip_hdr(skb)->protocol;
6585 break;
6586
6587 case htons(ETH_P_IPV6):
6588 opts[1] |= TD1_IPv6_CS;
6589 ip_protocol = ipv6_hdr(skb)->nexthdr;
6590 break;
6591
6592 default:
6593 ip_protocol = IPPROTO_RAW;
6594 break;
6595 }
6596
6597 if (ip_protocol == IPPROTO_TCP)
6598 opts[1] |= TD1_TCP_CS;
6599 else if (ip_protocol == IPPROTO_UDP)
6600 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006601 else
6602 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08006603
6604 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00006605 } else {
6606 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006607 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608 }
hayeswang5888d3f2014-07-11 16:25:56 +08006609
françois romieub423e9a2013-05-18 01:24:46 +00006610 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611}
6612
Stephen Hemminger613573252009-08-31 19:50:58 +00006613static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6614 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615{
6616 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006617 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006619 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620 dma_addr_t mapping;
6621 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006622 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006623 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02006624
Julien Ducourthial477206a2012-05-09 00:00:06 +02006625 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006626 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006627 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628 }
6629
6630 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006631 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632
françois romieub423e9a2013-05-18 01:24:46 +00006633 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6634 opts[0] = DescOwn;
6635
hayeswange9746042014-07-11 16:25:58 +08006636 if (!tp->tso_csum(tp, skb, opts)) {
6637 r8169_csum_workaround(tp, skb);
6638 return NETDEV_TX_OK;
6639 }
françois romieub423e9a2013-05-18 01:24:46 +00006640
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006641 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006642 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006643 if (unlikely(dma_mapping_error(d, mapping))) {
6644 if (net_ratelimit())
6645 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006646 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648
6649 tp->tx_skb[entry].len = len;
6650 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651
Francois Romieu2b7b4312011-04-18 22:53:24 -07006652 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006653 if (frags < 0)
6654 goto err_dma_1;
6655 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07006656 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006657 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07006658 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006659 tp->tx_skb[entry].skb = skb;
6660 }
6661
Francois Romieu2b7b4312011-04-18 22:53:24 -07006662 txd->opts2 = cpu_to_le32(opts[1]);
6663
Richard Cochran5047fb52012-03-10 07:29:42 +00006664 skb_tx_timestamp(skb);
6665
Alexander Duycka0750132014-12-11 15:02:17 -08006666 /* Force memory writes to complete before releasing descriptor */
6667 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668
Francois Romieucecb5fd2011-04-01 10:21:07 +02006669 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006670 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 txd->opts1 = cpu_to_le32(status);
6672
Alexander Duycka0750132014-12-11 15:02:17 -08006673 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00006674 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675
Alexander Duycka0750132014-12-11 15:02:17 -08006676 tp->cur_tx += frags + 1;
6677
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006678 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679
David S. Miller87cda7c2015-02-22 15:54:29 -05006680 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01006681
David S. Miller87cda7c2015-02-22 15:54:29 -05006682 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01006683 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6684 * not miss a ring update when it notices a stopped queue.
6685 */
6686 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01006688 /* Sync with rtl_tx:
6689 * - publish queue status and cur_tx ring index (write barrier)
6690 * - refresh dirty_tx ring index (read barrier).
6691 * May the current thread have a pessimistic view of the ring
6692 * status and forget to wake up queue, a racing rtl_tx thread
6693 * can't.
6694 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006695 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02006696 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697 netif_wake_queue(dev);
6698 }
6699
Stephen Hemminger613573252009-08-31 19:50:58 +00006700 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006702err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006703 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006704err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006705 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006706 dev->stats.tx_dropped++;
6707 return NETDEV_TX_OK;
6708
6709err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006711 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00006712 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713}
6714
6715static void rtl8169_pcierr_interrupt(struct net_device *dev)
6716{
6717 struct rtl8169_private *tp = netdev_priv(dev);
6718 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719 u16 pci_status, pci_cmd;
6720
6721 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6722 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6723
Joe Perchesbf82c182010-02-09 11:49:50 +00006724 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6725 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726
6727 /*
6728 * The recovery sequence below admits a very elaborated explanation:
6729 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01006730 * - I did not see what else could be done;
6731 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732 *
6733 * Feel free to adjust to your needs.
6734 */
Francois Romieua27993f2006-12-18 00:04:19 +01006735 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01006736 pci_cmd &= ~PCI_COMMAND_PARITY;
6737 else
6738 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6739
6740 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741
6742 pci_write_config_word(pdev, PCI_STATUS,
6743 pci_status & (PCI_STATUS_DETECTED_PARITY |
6744 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6745 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6746
6747 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00006748 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006749 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 tp->cp_cmd &= ~PCIDAC;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006751 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753 }
6754
françois romieue6de30d2011-01-03 15:08:37 +00006755 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01006756
Francois Romieu98ddf982012-01-31 10:47:34 +01006757 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758}
6759
Francois Romieuda78dbf2012-01-26 14:18:23 +01006760static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761{
6762 unsigned int dirty_tx, tx_left;
6763
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 dirty_tx = tp->dirty_tx;
6765 smp_rmb();
6766 tx_left = tp->cur_tx - dirty_tx;
6767
6768 while (tx_left > 0) {
6769 unsigned int entry = dirty_tx % NUM_TX_DESC;
6770 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 u32 status;
6772
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6774 if (status & DescOwn)
6775 break;
6776
Alexander Duycka0750132014-12-11 15:02:17 -08006777 /* This barrier is needed to keep us from reading
6778 * any other fields out of the Tx descriptor until
6779 * we know the status of DescOwn
6780 */
6781 dma_rmb();
6782
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006783 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006784 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05006786 u64_stats_update_begin(&tp->tx_stats.syncp);
6787 tp->tx_stats.packets++;
6788 tp->tx_stats.bytes += tx_skb->skb->len;
6789 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006790 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 tx_skb->skb = NULL;
6792 }
6793 dirty_tx++;
6794 tx_left--;
6795 }
6796
6797 if (tp->dirty_tx != dirty_tx) {
6798 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01006799 /* Sync with rtl8169_start_xmit:
6800 * - publish dirty_tx ring index (write barrier)
6801 * - refresh cur_tx ring index and queue status (read barrier)
6802 * May the current thread miss the stopped queue condition,
6803 * a racing xmit thread can only have a right view of the
6804 * ring status.
6805 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006806 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02006808 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 netif_wake_queue(dev);
6810 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02006811 /*
6812 * 8168 hack: TxPoll requests are lost when the Tx packets are
6813 * too close. Let's kick an extra TxPoll request when a burst
6814 * of start_xmit activity is detected (if it is not detected,
6815 * it is slow enough). -- FR
6816 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006817 if (tp->cur_tx != dirty_tx)
6818 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 }
6820}
6821
Francois Romieu126fa4b2005-05-12 20:09:17 -04006822static inline int rtl8169_fragmented_frame(u32 status)
6823{
6824 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6825}
6826
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006827static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829 u32 status = opts1 & RxProtoMask;
6830
6831 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00006832 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 skb->ip_summed = CHECKSUM_UNNECESSARY;
6834 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006835 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836}
6837
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006838static struct sk_buff *rtl8169_try_rx_copy(void *data,
6839 struct rtl8169_private *tp,
6840 int pkt_size,
6841 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02006843 struct sk_buff *skb;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006844 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006846 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006847 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006848 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08006849 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006850 if (skb)
Heiner Kallweit8a67aa82018-04-17 23:19:07 +02006851 skb_copy_to_linear_data(skb, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006852 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6853
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006854 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855}
6856
Francois Romieuda78dbf2012-01-26 14:18:23 +01006857static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858{
6859 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006860 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863
Timo Teräs9fba0812013-01-15 21:01:24 +00006864 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006866 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867 u32 status;
6868
Heiner Kallweit62028062018-04-17 23:30:29 +02006869 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870 if (status & DescOwn)
6871 break;
Alexander Duycka0750132014-12-11 15:02:17 -08006872
6873 /* This barrier is needed to keep us from reading
6874 * any other fields out of the Rx descriptor until
6875 * we know the status of DescOwn
6876 */
6877 dma_rmb();
6878
Richard Dawe4dcb7d32005-05-27 21:12:00 +02006879 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006880 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6881 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006882 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02006884 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02006886 dev->stats.rx_crc_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006887 /* RxFOVF is a reserved bit on later chip versions */
6888 if (tp->mac_version == RTL_GIGA_MAC_VER_01 &&
6889 status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006890 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006891 dev->stats.rx_fifo_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006892 } else if (status & (RxRUNT | RxCRC) &&
6893 !(status & RxRWT) &&
6894 dev->features & NETIF_F_RXALL) {
Ben Greear6bbe0212012-02-10 15:04:33 +00006895 goto process_pkt;
Heiner Kallweit62028062018-04-17 23:30:29 +02006896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006898 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00006899 dma_addr_t addr;
6900 int pkt_size;
6901
6902process_pkt:
6903 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00006904 if (likely(!(dev->features & NETIF_F_RXFCS)))
6905 pkt_size = (status & 0x00003fff) - 4;
6906 else
6907 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908
Francois Romieu126fa4b2005-05-12 20:09:17 -04006909 /*
6910 * The driver does not support incoming fragmented
6911 * frames. They are seen as a symptom of over-mtu
6912 * sized frames.
6913 */
6914 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02006915 dev->stats.rx_dropped++;
6916 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00006917 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006918 }
6919
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006920 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6921 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006922 if (!skb) {
6923 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00006924 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925 }
6926
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006927 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 skb_put(skb, pkt_size);
6929 skb->protocol = eth_type_trans(skb, dev);
6930
Francois Romieu7a8fc772011-03-01 17:18:33 +01006931 rtl8169_rx_vlan_tag(desc, skb);
6932
françois romieu39174292015-11-11 23:35:18 +01006933 if (skb->pkt_type == PACKET_MULTICAST)
6934 dev->stats.multicast++;
6935
Francois Romieu56de4142011-03-15 17:29:31 +01006936 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937
Junchang Wang8027aa22012-03-04 23:30:32 +01006938 u64_stats_update_begin(&tp->rx_stats.syncp);
6939 tp->rx_stats.packets++;
6940 tp->rx_stats.bytes += pkt_size;
6941 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942 }
françois romieuce11ff52013-01-24 13:30:06 +00006943release_descriptor:
6944 desc->opts2 = 0;
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006945 rtl8169_mark_to_asic(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946 }
6947
6948 count = cur_rx - tp->cur_rx;
6949 tp->cur_rx = cur_rx;
6950
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951 return count;
6952}
6953
Francois Romieu07d3f512007-02-21 22:40:46 +01006954static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955{
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006956 struct rtl8169_private *tp = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006958 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006960 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006961 if (status && status != 0xffff) {
6962 status &= RTL_EVENT_NAPI | tp->event_slow;
6963 if (status) {
6964 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00006965
Francois Romieuda78dbf2012-01-26 14:18:23 +01006966 rtl_irq_disable(tp);
Heiner Kallweit9a899a32018-04-17 23:21:01 +02006967 napi_schedule_irqoff(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 return IRQ_RETVAL(handled);
6971}
6972
Francois Romieuda78dbf2012-01-26 14:18:23 +01006973/*
6974 * Workqueue context.
6975 */
6976static void rtl_slow_event_work(struct rtl8169_private *tp)
6977{
6978 struct net_device *dev = tp->dev;
6979 u16 status;
6980
6981 status = rtl_get_events(tp) & tp->event_slow;
6982 rtl_ack_events(tp, status);
6983
6984 if (unlikely(status & RxFIFOOver)) {
6985 switch (tp->mac_version) {
6986 /* Work around for rx fifo overflow */
6987 case RTL_GIGA_MAC_VER_11:
6988 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01006989 /* XXX - Hack alert. See rtl_task(). */
6990 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006991 default:
6992 break;
6993 }
6994 }
6995
6996 if (unlikely(status & SYSErr))
6997 rtl8169_pcierr_interrupt(dev);
6998
6999 if (status & LinkChg)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007000 rtl8169_check_link_status(dev, tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007001
françois romieu7dbb4912012-06-09 10:53:16 +00007002 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007003}
7004
Francois Romieu4422bcd2012-01-26 11:23:32 +01007005static void rtl_task(struct work_struct *work)
7006{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007007 static const struct {
7008 int bitnr;
7009 void (*action)(struct rtl8169_private *);
7010 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01007011 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007012 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7013 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7014 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7015 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01007016 struct rtl8169_private *tp =
7017 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007018 struct net_device *dev = tp->dev;
7019 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01007020
Francois Romieuda78dbf2012-01-26 14:18:23 +01007021 rtl_lock_work(tp);
7022
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007023 if (!netif_running(dev) ||
7024 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01007025 goto out_unlock;
7026
7027 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7028 bool pending;
7029
Francois Romieuda78dbf2012-01-26 14:18:23 +01007030 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007031 if (pending)
7032 rtl_work[i].action(tp);
7033 }
7034
7035out_unlock:
7036 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01007037}
7038
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007039static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007041 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7042 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007043 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7044 int work_done= 0;
7045 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046
Francois Romieuda78dbf2012-01-26 14:18:23 +01007047 status = rtl_get_events(tp);
7048 rtl_ack_events(tp, status & ~tp->event_slow);
7049
7050 if (status & RTL_EVENT_NAPI_RX)
7051 work_done = rtl_rx(dev, tp, (u32) budget);
7052
7053 if (status & RTL_EVENT_NAPI_TX)
7054 rtl_tx(dev, tp);
7055
7056 if (status & tp->event_slow) {
7057 enable_mask &= ~tp->event_slow;
7058
7059 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007062 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08007063 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00007064
Francois Romieuda78dbf2012-01-26 14:18:23 +01007065 rtl_irq_enable(tp, enable_mask);
7066 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067 }
7068
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007069 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007072static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02007073{
7074 struct rtl8169_private *tp = netdev_priv(dev);
7075
7076 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7077 return;
7078
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007079 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
7080 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02007081}
7082
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083static void rtl8169_down(struct net_device *dev)
7084{
7085 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086
Francois Romieu4876cc12011-03-11 21:07:11 +01007087 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007089 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007090 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091
Hayes Wang92fc43b2011-07-06 15:58:03 +08007092 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007093 /*
7094 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007095 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7096 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007097 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007098 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007101 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 rtl8169_tx_clear(tp);
7104
7105 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007106
7107 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108}
7109
7110static int rtl8169_close(struct net_device *dev)
7111{
7112 struct rtl8169_private *tp = netdev_priv(dev);
7113 struct pci_dev *pdev = tp->pci_dev;
7114
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007115 pm_runtime_get_sync(&pdev->dev);
7116
Francois Romieucecb5fd2011-04-01 10:21:07 +02007117 /* Update counters before going down */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007118 rtl8169_update_counters(tp);
Ivan Vecera355423d2009-02-06 21:49:57 -08007119
Francois Romieuda78dbf2012-01-26 14:18:23 +01007120 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007121 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007122
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007124 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125
Lekensteyn4ea72442013-07-22 09:53:30 +02007126 cancel_work_sync(&tp->wk.work);
7127
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02007128 pci_free_irq(pdev, 0, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007130 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7131 tp->RxPhyAddr);
7132 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7133 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 tp->TxDescArray = NULL;
7135 tp->RxDescArray = NULL;
7136
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007137 pm_runtime_put_sync(&pdev->dev);
7138
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 return 0;
7140}
7141
Francois Romieudc1c00c2012-03-08 10:06:18 +01007142#ifdef CONFIG_NET_POLL_CONTROLLER
7143static void rtl8169_netpoll(struct net_device *dev)
7144{
7145 struct rtl8169_private *tp = netdev_priv(dev);
7146
Heiner Kallweit29274992018-02-28 20:43:38 +01007147 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), dev);
Francois Romieudc1c00c2012-03-08 10:06:18 +01007148}
7149#endif
7150
Francois Romieudf43ac72012-03-08 09:48:40 +01007151static int rtl_open(struct net_device *dev)
7152{
7153 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007154 struct pci_dev *pdev = tp->pci_dev;
7155 int retval = -ENOMEM;
7156
7157 pm_runtime_get_sync(&pdev->dev);
7158
7159 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007160 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007161 * dma_alloc_coherent provides more.
7162 */
7163 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7164 &tp->TxPhyAddr, GFP_KERNEL);
7165 if (!tp->TxDescArray)
7166 goto err_pm_runtime_put;
7167
7168 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7169 &tp->RxPhyAddr, GFP_KERNEL);
7170 if (!tp->RxDescArray)
7171 goto err_free_tx_0;
7172
Heiner Kallweitb1127e62018-04-17 23:23:35 +02007173 retval = rtl8169_init_ring(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007174 if (retval < 0)
7175 goto err_free_rx_1;
7176
7177 INIT_WORK(&tp->wk.work, rtl_task);
7178
7179 smp_mb();
7180
7181 rtl_request_firmware(tp);
7182
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02007183 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007184 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01007185 if (retval < 0)
7186 goto err_release_fw_2;
7187
7188 rtl_lock_work(tp);
7189
7190 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7191
7192 napi_enable(&tp->napi);
7193
7194 rtl8169_init_phy(dev, tp);
7195
Francois Romieudf43ac72012-03-08 09:48:40 +01007196 rtl_pll_power_up(tp);
7197
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007198 rtl_hw_start(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007199
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007200 if (!rtl8169_init_counter_offsets(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007201 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7202
Francois Romieudf43ac72012-03-08 09:48:40 +01007203 netif_start_queue(dev);
7204
7205 rtl_unlock_work(tp);
7206
7207 tp->saved_wolopts = 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007208 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007209
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007210 rtl8169_check_link_status(dev, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007211out:
7212 return retval;
7213
7214err_release_fw_2:
7215 rtl_release_firmware(tp);
7216 rtl8169_rx_clear(tp);
7217err_free_rx_1:
7218 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7219 tp->RxPhyAddr);
7220 tp->RxDescArray = NULL;
7221err_free_tx_0:
7222 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7223 tp->TxPhyAddr);
7224 tp->TxDescArray = NULL;
7225err_pm_runtime_put:
7226 pm_runtime_put_noidle(&pdev->dev);
7227 goto out;
7228}
7229
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007230static void
Junchang Wang8027aa22012-03-04 23:30:32 +01007231rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232{
7233 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007234 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02007235 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01007236 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007238 pm_runtime_get_noresume(&pdev->dev);
7239
7240 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007241 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007242
Junchang Wang8027aa22012-03-04 23:30:32 +01007243 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007244 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007245 stats->rx_packets = tp->rx_stats.packets;
7246 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007247 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007248
Junchang Wang8027aa22012-03-04 23:30:32 +01007249 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007250 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007251 stats->tx_packets = tp->tx_stats.packets;
7252 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007253 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007254
7255 stats->rx_dropped = dev->stats.rx_dropped;
7256 stats->tx_dropped = dev->stats.tx_dropped;
7257 stats->rx_length_errors = dev->stats.rx_length_errors;
7258 stats->rx_errors = dev->stats.rx_errors;
7259 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7260 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7261 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02007262 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01007263
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007264 /*
7265 * Fetch additonal counter values missing in stats collected by driver
7266 * from tally counters.
7267 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007268 if (pm_runtime_active(&pdev->dev))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007269 rtl8169_update_counters(tp);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007270
7271 /*
7272 * Subtract values fetched during initalization.
7273 * See rtl8169_init_counter_offsets for a description why we do that.
7274 */
Corinna Vinschen42020322015-09-10 10:47:35 +02007275 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007276 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02007277 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007278 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02007279 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007280 le16_to_cpu(tp->tc_offset.tx_aborted);
7281
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007282 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283}
7284
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007285static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01007286{
françois romieu065c27c2011-01-03 15:08:12 +00007287 struct rtl8169_private *tp = netdev_priv(dev);
7288
Francois Romieu5d06a992006-02-23 00:47:58 +01007289 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007290 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01007291
7292 netif_device_detach(dev);
7293 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007294
7295 rtl_lock_work(tp);
7296 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007297 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007298 rtl_unlock_work(tp);
7299
7300 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007301}
Francois Romieu5d06a992006-02-23 00:47:58 +01007302
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007303#ifdef CONFIG_PM
7304
7305static int rtl8169_suspend(struct device *device)
7306{
7307 struct pci_dev *pdev = to_pci_dev(device);
7308 struct net_device *dev = pci_get_drvdata(pdev);
7309
7310 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02007311
Francois Romieu5d06a992006-02-23 00:47:58 +01007312 return 0;
7313}
7314
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007315static void __rtl8169_resume(struct net_device *dev)
7316{
françois romieu065c27c2011-01-03 15:08:12 +00007317 struct rtl8169_private *tp = netdev_priv(dev);
7318
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007319 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00007320
7321 rtl_pll_power_up(tp);
7322
Artem Savkovcff4c162012-04-03 10:29:11 +00007323 rtl_lock_work(tp);
7324 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007325 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00007326 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007327
Francois Romieu98ddf982012-01-31 10:47:34 +01007328 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007329}
7330
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007331static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01007332{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007333 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01007334 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007335 struct rtl8169_private *tp = netdev_priv(dev);
7336
7337 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01007338
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007339 if (netif_running(dev))
7340 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01007341
Francois Romieu5d06a992006-02-23 00:47:58 +01007342 return 0;
7343}
7344
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007345static int rtl8169_runtime_suspend(struct device *device)
7346{
7347 struct pci_dev *pdev = to_pci_dev(device);
7348 struct net_device *dev = pci_get_drvdata(pdev);
7349 struct rtl8169_private *tp = netdev_priv(dev);
7350
Heiner Kallweita92a0842018-01-08 21:39:13 +01007351 if (!tp->TxDescArray) {
7352 rtl_pll_power_down(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007353 return 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007354 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007355
Francois Romieuda78dbf2012-01-26 14:18:23 +01007356 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007357 tp->saved_wolopts = __rtl8169_get_wol(tp);
7358 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007359 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007360
7361 rtl8169_net_suspend(dev);
7362
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007363 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007364 rtl8169_rx_missed(dev);
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007365 rtl8169_update_counters(tp);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007366
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007367 return 0;
7368}
7369
7370static int rtl8169_runtime_resume(struct device *device)
7371{
7372 struct pci_dev *pdev = to_pci_dev(device);
7373 struct net_device *dev = pci_get_drvdata(pdev);
7374 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08007375 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007376
7377 if (!tp->TxDescArray)
7378 return 0;
7379
Francois Romieuda78dbf2012-01-26 14:18:23 +01007380 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007381 __rtl8169_set_wol(tp, tp->saved_wolopts);
7382 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007383 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007384
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007385 rtl8169_init_phy(dev, tp);
7386
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007387 __rtl8169_resume(dev);
7388
7389 return 0;
7390}
7391
7392static int rtl8169_runtime_idle(struct device *device)
7393{
7394 struct pci_dev *pdev = to_pci_dev(device);
7395 struct net_device *dev = pci_get_drvdata(pdev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007396
Heiner Kallweita92a0842018-01-08 21:39:13 +01007397 if (!netif_running(dev) || !netif_carrier_ok(dev))
7398 pm_schedule_suspend(device, 10000);
7399
7400 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007401}
7402
Alexey Dobriyan47145212009-12-14 18:00:08 -08007403static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02007404 .suspend = rtl8169_suspend,
7405 .resume = rtl8169_resume,
7406 .freeze = rtl8169_suspend,
7407 .thaw = rtl8169_resume,
7408 .poweroff = rtl8169_suspend,
7409 .restore = rtl8169_resume,
7410 .runtime_suspend = rtl8169_runtime_suspend,
7411 .runtime_resume = rtl8169_runtime_resume,
7412 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007413};
7414
7415#define RTL8169_PM_OPS (&rtl8169_pm_ops)
7416
7417#else /* !CONFIG_PM */
7418
7419#define RTL8169_PM_OPS NULL
7420
7421#endif /* !CONFIG_PM */
7422
David S. Miller1805b2f2011-10-24 18:18:09 -04007423static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7424{
David S. Miller1805b2f2011-10-24 18:18:09 -04007425 /* WoL fails with 8168b when the receiver is disabled. */
7426 switch (tp->mac_version) {
7427 case RTL_GIGA_MAC_VER_11:
7428 case RTL_GIGA_MAC_VER_12:
7429 case RTL_GIGA_MAC_VER_17:
7430 pci_clear_master(tp->pci_dev);
7431
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007432 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007433 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007434 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04007435 break;
7436 default:
7437 break;
7438 }
7439}
7440
Francois Romieu1765f952008-09-13 17:21:40 +02007441static void rtl_shutdown(struct pci_dev *pdev)
7442{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007443 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00007444 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02007445
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007446 rtl8169_net_suspend(dev);
7447
Francois Romieucecb5fd2011-04-01 10:21:07 +02007448 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08007449 rtl_rar_set(tp, dev->perm_addr);
7450
Hayes Wang92fc43b2011-07-06 15:58:03 +08007451 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00007452
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007453 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04007454 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
7455 rtl_wol_suspend_quirk(tp);
7456 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00007457 }
7458
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007459 pci_wake_from_d3(pdev, true);
7460 pci_set_power_state(pdev, PCI_D3hot);
7461 }
7462}
Francois Romieu5d06a992006-02-23 00:47:58 +01007463
Bill Pembertonbaf63292012-12-03 09:23:28 -05007464static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01007465{
7466 struct net_device *dev = pci_get_drvdata(pdev);
7467 struct rtl8169_private *tp = netdev_priv(dev);
7468
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007469 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01007470 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01007471
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007472 netif_napi_del(&tp->napi);
7473
Francois Romieue27566e2012-03-08 09:54:01 +01007474 unregister_netdev(dev);
7475
7476 rtl_release_firmware(tp);
7477
7478 if (pci_dev_run_wake(pdev))
7479 pm_runtime_get_noresume(&pdev->dev);
7480
7481 /* restore original MAC address */
7482 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01007483}
7484
Francois Romieufa9c3852012-03-08 10:01:50 +01007485static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01007486 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01007487 .ndo_stop = rtl8169_close,
7488 .ndo_get_stats64 = rtl8169_get_stats64,
7489 .ndo_start_xmit = rtl8169_start_xmit,
7490 .ndo_tx_timeout = rtl8169_tx_timeout,
7491 .ndo_validate_addr = eth_validate_addr,
7492 .ndo_change_mtu = rtl8169_change_mtu,
7493 .ndo_fix_features = rtl8169_fix_features,
7494 .ndo_set_features = rtl8169_set_features,
7495 .ndo_set_mac_address = rtl_set_mac_address,
7496 .ndo_do_ioctl = rtl8169_ioctl,
7497 .ndo_set_rx_mode = rtl_set_rx_mode,
7498#ifdef CONFIG_NET_POLL_CONTROLLER
7499 .ndo_poll_controller = rtl8169_netpoll,
7500#endif
7501
7502};
7503
Francois Romieu31fa8b12012-03-08 10:09:40 +01007504static const struct rtl_cfg_info {
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007505 void (*hw_start)(struct rtl8169_private *tp);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007506 u16 event_slow;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007507 unsigned int has_gmii:1;
Francois Romieu50970832017-10-27 13:24:49 +03007508 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007509 u8 default_ver;
7510} rtl_cfg_infos [] = {
7511 [RTL_CFG_0] = {
7512 .hw_start = rtl_hw_start_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007513 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007514 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007515 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007516 .default_ver = RTL_GIGA_MAC_VER_01,
7517 },
7518 [RTL_CFG_1] = {
7519 .hw_start = rtl_hw_start_8168,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007520 .event_slow = SYSErr | LinkChg | RxOverflow,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007521 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007522 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007523 .default_ver = RTL_GIGA_MAC_VER_11,
7524 },
7525 [RTL_CFG_2] = {
7526 .hw_start = rtl_hw_start_8101,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007527 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
7528 PCSTimeout,
Francois Romieu50970832017-10-27 13:24:49 +03007529 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007530 .default_ver = RTL_GIGA_MAC_VER_13,
7531 }
7532};
7533
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007534static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01007535{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007536 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007537
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007538 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007539 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
7540 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
7541 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007542 flags = PCI_IRQ_LEGACY;
7543 } else {
7544 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007545 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007546
7547 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007548}
7549
Hayes Wangc5583862012-07-02 17:23:22 +08007550DECLARE_RTL_COND(rtl_link_list_ready_cond)
7551{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007552 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08007553}
7554
7555DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7556{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007557 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08007558}
7559
Bill Pembertonbaf63292012-12-03 09:23:28 -05007560static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007561{
Hayes Wangc5583862012-07-02 17:23:22 +08007562 u32 data;
7563
7564 tp->ocp_base = OCP_STD_PHY_BASE;
7565
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007566 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08007567
7568 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7569 return;
7570
7571 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7572 return;
7573
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007574 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08007575 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007576 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08007577
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007578 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007579 data &= ~(1 << 14);
7580 r8168_mac_ocp_write(tp, 0xe8de, data);
7581
7582 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7583 return;
7584
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007585 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007586 data |= (1 << 15);
7587 r8168_mac_ocp_write(tp, 0xe8de, data);
7588
7589 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7590 return;
7591}
7592
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007593static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
7594{
7595 rtl8168ep_stop_cmac(tp);
7596 rtl_hw_init_8168g(tp);
7597}
7598
Bill Pembertonbaf63292012-12-03 09:23:28 -05007599static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007600{
7601 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02007602 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007603 rtl_hw_init_8168g(tp);
7604 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02007605 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007606 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08007607 break;
Hayes Wangc5583862012-07-02 17:23:22 +08007608 default:
7609 break;
7610 }
7611}
7612
hayeswang929a0312014-09-16 11:40:47 +08007613static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01007614{
7615 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007616 struct rtl8169_private *tp;
7617 struct mii_if_info *mii;
7618 struct net_device *dev;
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007619 int chipset, region, i;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007620 int rc;
7621
7622 if (netif_msg_drv(&debug)) {
7623 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
7624 MODULENAME, RTL8169_VERSION);
7625 }
7626
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007627 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
7628 if (!dev)
7629 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007630
7631 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01007632 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007633 tp = netdev_priv(dev);
7634 tp->dev = dev;
7635 tp->pci_dev = pdev;
7636 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
7637
7638 mii = &tp->mii;
7639 mii->dev = dev;
7640 mii->mdio_read = rtl_mdio_read;
7641 mii->mdio_write = rtl_mdio_write;
7642 mii->phy_id_mask = 0x1f;
7643 mii->reg_num_mask = 0x1f;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007644 mii->supports_gmii = cfg->has_gmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007645
7646 /* disable ASPM completely as that cause random device stop working
7647 * problems as well as full system hangs for some PCIe devices users */
7648 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
7649 PCIE_LINK_STATE_CLKPM);
7650
7651 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007652 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007653 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007654 dev_err(&pdev->dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007655 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007656 }
7657
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007658 if (pcim_set_mwi(pdev) < 0)
Heiner Kallweit22148df2018-04-22 17:15:15 +02007659 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007660
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007661 /* use first MMIO region */
7662 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
7663 if (region < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007664 dev_err(&pdev->dev, "no MMIO resource found\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007665 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007666 }
7667
7668 /* check for weird/broken PCI region reporting */
7669 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007670 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007671 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007672 }
7673
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007674 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007675 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007676 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007677 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007678 }
7679
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007680 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01007681
7682 if (!pci_is_pcie(pdev))
Heiner Kallweit22148df2018-04-22 17:15:15 +02007683 dev_info(&pdev->dev, "not PCI Express\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007684
7685 /* Identify chip attached to board */
Heiner Kallweit22148df2018-04-22 17:15:15 +02007686 rtl8169_get_mac_version(tp, cfg->default_ver);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007687
Heiner Kallweit0ae09742018-04-28 22:19:26 +02007688 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007689
7690 if ((sizeof(dma_addr_t) > 4) &&
7691 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
7692 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01007693 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
7694 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007695
7696 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
7697 if (!pci_is_pcie(pdev))
7698 tp->cp_cmd |= PCIDAC;
7699 dev->features |= NETIF_F_HIGHDMA;
7700 } else {
7701 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7702 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007703 dev_err(&pdev->dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007704 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007705 }
7706 }
7707
Francois Romieu3b6cf252012-03-08 09:59:04 +01007708 rtl_init_rxcfg(tp);
7709
7710 rtl_irq_disable(tp);
7711
Hayes Wangc5583862012-07-02 17:23:22 +08007712 rtl_hw_initialize(tp);
7713
Francois Romieu3b6cf252012-03-08 09:59:04 +01007714 rtl_hw_reset(tp);
7715
7716 rtl_ack_events(tp, 0xffff);
7717
7718 pci_set_master(pdev);
7719
Francois Romieu3b6cf252012-03-08 09:59:04 +01007720 rtl_init_mdio_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007721 rtl_init_jumbo_ops(tp);
7722
7723 rtl8169_print_mac_version(tp);
7724
7725 chipset = tp->mac_version;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007726
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007727 rc = rtl_alloc_irq(tp);
7728 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007729 dev_err(&pdev->dev, "Can't allocate interrupt\n");
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007730 return rc;
7731 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007732
Heiner Kallweit7edf6d32018-02-22 21:22:40 +01007733 /* override BIOS settings, use userspace tools to enable WOL */
7734 __rtl8169_set_wol(tp, 0);
7735
Francois Romieu3b6cf252012-03-08 09:59:04 +01007736 if (rtl_tbi_enabled(tp)) {
7737 tp->set_speed = rtl8169_set_speed_tbi;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01007738 tp->get_link_ksettings = rtl8169_get_link_ksettings_tbi;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007739 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
7740 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
7741 tp->link_ok = rtl8169_tbi_link_ok;
7742 tp->do_ioctl = rtl_tbi_ioctl;
7743 } else {
7744 tp->set_speed = rtl8169_set_speed_xmii;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01007745 tp->get_link_ksettings = rtl8169_get_link_ksettings_xmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007746 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
7747 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
7748 tp->link_ok = rtl8169_xmii_link_ok;
7749 tp->do_ioctl = rtl_xmii_ioctl;
7750 }
7751
7752 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05007753 u64_stats_init(&tp->rx_stats.syncp);
7754 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007755
7756 /* Get MAC address */
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007757 switch (tp->mac_version) {
Heiner Kallweit353af852018-05-02 21:39:59 +02007758 u8 mac_addr[ETH_ALEN] __aligned(4);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007759 case RTL_GIGA_MAC_VER_35 ... RTL_GIGA_MAC_VER_38:
7760 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08007761 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
Heiner Kallweit353af852018-05-02 21:39:59 +02007762 *(u16 *)&mac_addr[4] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007763
Heiner Kallweit353af852018-05-02 21:39:59 +02007764 if (is_valid_ether_addr(mac_addr))
7765 rtl_rar_set(tp, mac_addr);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007766 break;
7767 default:
7768 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007769 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007770 for (i = 0; i < ETH_ALEN; i++)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007771 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007772
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00007773 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007774 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007775
Heiner Kallweit37621492018-04-17 23:20:03 +02007776 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007777
7778 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7779 * properly for all devices */
7780 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00007781 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007782
7783 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00007784 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7785 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007786 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7787 NETIF_F_HIGHDMA;
7788
hayeswang929a0312014-09-16 11:40:47 +08007789 tp->cp_cmd |= RxChkSum | RxVlan;
7790
7791 /*
7792 * Pretend we are using VLANs; This bypasses a nasty bug where
7793 * Interrupts stop flowing on high load on 8110SCd controllers.
7794 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01007795 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08007796 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00007797 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007798
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007799 switch (rtl_chip_infos[chipset].txd_version) {
7800 case RTL_TD_0:
hayeswang5888d3f2014-07-11 16:25:56 +08007801 tp->tso_csum = rtl8169_tso_csum_v1;
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007802 break;
7803 case RTL_TD_1:
hayeswang5888d3f2014-07-11 16:25:56 +08007804 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08007805 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007806 break;
7807 default:
hayeswang5888d3f2014-07-11 16:25:56 +08007808 WARN_ON_ONCE(1);
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007809 }
hayeswang5888d3f2014-07-11 16:25:56 +08007810
Francois Romieu3b6cf252012-03-08 09:59:04 +01007811 dev->hw_features |= NETIF_F_RXALL;
7812 dev->hw_features |= NETIF_F_RXFCS;
7813
Jarod Wilsonc7315a92016-10-17 15:54:09 -04007814 /* MTU range: 60 - hw-specific max */
7815 dev->min_mtu = ETH_ZLEN;
7816 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
7817
Francois Romieu3b6cf252012-03-08 09:59:04 +01007818 tp->hw_start = cfg->hw_start;
7819 tp->event_slow = cfg->event_slow;
Francois Romieu50970832017-10-27 13:24:49 +03007820 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007821
Kees Cook9de36cc2017-10-25 03:53:12 -07007822 timer_setup(&tp->timer, rtl8169_phy_timer, 0);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007823
7824 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
7825
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007826 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
7827 &tp->counters_phys_addr,
7828 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007829 if (!tp->counters)
7830 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02007831
Heiner Kallweit19c9ea32018-03-26 19:19:30 +02007832 pci_set_drvdata(pdev, dev);
7833
Francois Romieu3b6cf252012-03-08 09:59:04 +01007834 rc = register_netdev(dev);
7835 if (rc < 0)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007836 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007837
Heiner Kallweit2d6c5a62018-04-17 23:31:21 +02007838 netif_info(tp, probe, dev, "%s, %pM, XID %08x, IRQ %d\n",
7839 rtl_chip_infos[chipset].name, dev->dev_addr,
Heiner Kallweit90b989c2018-04-17 23:32:15 +02007840 (u32)(RTL_R32(tp, TxConfig) & 0xfcf0f8ff),
Heiner Kallweit29274992018-02-28 20:43:38 +01007841 pci_irq_vector(pdev, 0));
Francois Romieu3b6cf252012-03-08 09:59:04 +01007842 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
7843 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
7844 "tx checksumming: %s]\n",
7845 rtl_chip_infos[chipset].jumbo_max,
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02007846 tp->mac_version <= RTL_GIGA_MAC_VER_06 ? "ok" : "ko");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007847 }
7848
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007849 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01007850 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007851
Francois Romieu3b6cf252012-03-08 09:59:04 +01007852 netif_carrier_off(dev);
7853
Heiner Kallweita92a0842018-01-08 21:39:13 +01007854 if (pci_dev_run_wake(pdev))
7855 pm_runtime_put_sync(&pdev->dev);
7856
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007857 return 0;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007858}
7859
Linus Torvalds1da177e2005-04-16 15:20:36 -07007860static struct pci_driver rtl8169_pci_driver = {
7861 .name = MODULENAME,
7862 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01007863 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05007864 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02007865 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007866 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007867};
7868
Devendra Naga3eeb7da2012-10-26 09:27:42 +00007869module_pci_driver(rtl8169_pci_driver);