blob: 1d33672c650df017a064c13d4c15239e38fbc34f [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>
Paul Gortmaker70c71602011-05-22 16:47:17 -040028#include <linux/prefetch.h>
hayeswange9746042014-07-11 16:25:58 +080029#include <linux/ipv6.h>
30#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/io.h>
33#include <asm/irq.h>
34
Francois Romieu865c6522008-05-11 14:51:00 +020035#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define MODULENAME "r8169"
37#define PFX MODULENAME ": "
38
françois romieubca03d52011-01-03 15:07:31 +000039#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
40#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000041#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
42#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080043#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080044#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
45#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080046#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080047#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080048#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080049#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080050#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000051#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000052#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000053#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +080054#define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
55#define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
56#define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
57#define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
françois romieubca03d52011-01-03 15:07:31 +000058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#ifdef RTL8169_DEBUG
60#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020061 if (!(expr)) { \
62 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070063 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020064 }
Joe Perches06fa7352007-10-18 21:15:00 +020065#define dprintk(fmt, args...) \
66 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#else
68#define assert(expr) do {} while (0)
69#define dprintk(fmt, args...) do {} while (0)
70#endif /* RTL8169_DEBUG */
71
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020072#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d92005-09-30 16:54:02 -070073 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020074
Julien Ducourthial477206a2012-05-09 00:00:06 +020075#define TX_SLOTS_AVAIL(tp) \
76 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
77
78/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
79#define TX_FRAGS_READY_FOR(tp,nr_frags) \
80 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
83 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050084static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Michal Schmidtaee77e42012-09-09 13:55:26 +000086#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
88
89#define R8169_REGS_SIZE 256
Heiner Kallweit1d0254d2018-04-17 23:25:46 +020090#define R8169_RX_BUF_SIZE (SZ_16K - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000092#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
94#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
95
96#define RTL8169_TX_TIMEOUT (6*HZ)
97#define RTL8169_PHY_TIMEOUT (10*HZ)
98
99/* write/read MMIO register */
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200100#define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
101#define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
102#define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
103#define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
104#define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
105#define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200108 RTL_GIGA_MAC_VER_01 = 0,
109 RTL_GIGA_MAC_VER_02,
110 RTL_GIGA_MAC_VER_03,
111 RTL_GIGA_MAC_VER_04,
112 RTL_GIGA_MAC_VER_05,
113 RTL_GIGA_MAC_VER_06,
114 RTL_GIGA_MAC_VER_07,
115 RTL_GIGA_MAC_VER_08,
116 RTL_GIGA_MAC_VER_09,
117 RTL_GIGA_MAC_VER_10,
118 RTL_GIGA_MAC_VER_11,
119 RTL_GIGA_MAC_VER_12,
120 RTL_GIGA_MAC_VER_13,
121 RTL_GIGA_MAC_VER_14,
122 RTL_GIGA_MAC_VER_15,
123 RTL_GIGA_MAC_VER_16,
124 RTL_GIGA_MAC_VER_17,
125 RTL_GIGA_MAC_VER_18,
126 RTL_GIGA_MAC_VER_19,
127 RTL_GIGA_MAC_VER_20,
128 RTL_GIGA_MAC_VER_21,
129 RTL_GIGA_MAC_VER_22,
130 RTL_GIGA_MAC_VER_23,
131 RTL_GIGA_MAC_VER_24,
132 RTL_GIGA_MAC_VER_25,
133 RTL_GIGA_MAC_VER_26,
134 RTL_GIGA_MAC_VER_27,
135 RTL_GIGA_MAC_VER_28,
136 RTL_GIGA_MAC_VER_29,
137 RTL_GIGA_MAC_VER_30,
138 RTL_GIGA_MAC_VER_31,
139 RTL_GIGA_MAC_VER_32,
140 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800141 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800142 RTL_GIGA_MAC_VER_35,
143 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800144 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800145 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800146 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800147 RTL_GIGA_MAC_VER_40,
148 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000149 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000150 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800151 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800152 RTL_GIGA_MAC_VER_45,
153 RTL_GIGA_MAC_VER_46,
154 RTL_GIGA_MAC_VER_47,
155 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800156 RTL_GIGA_MAC_VER_49,
157 RTL_GIGA_MAC_VER_50,
158 RTL_GIGA_MAC_VER_51,
Francois Romieu85bffe62011-04-27 08:22:39 +0200159 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
Francois Romieu2b7b4312011-04-18 22:53:24 -0700162enum rtl_tx_desc_version {
163 RTL_TD_0 = 0,
164 RTL_TD_1 = 1,
165};
166
Francois Romieud58d46b2011-05-03 16:38:29 +0200167#define JUMBO_1K ETH_DATA_LEN
168#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
169#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
170#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
171#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
172
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200173#define _R(NAME,TD,FW,SZ) { \
Francois Romieud58d46b2011-05-03 16:38:29 +0200174 .name = NAME, \
175 .txd_version = TD, \
176 .fw_name = FW, \
177 .jumbo_max = SZ, \
Francois Romieud58d46b2011-05-03 16:38:29 +0200178}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800180static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700182 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200183 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200184 u16 jumbo_max;
Francois Romieu85bffe62011-04-27 08:22:39 +0200185} rtl_chip_infos[] = {
186 /* PCI devices. */
187 [RTL_GIGA_MAC_VER_01] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200188 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200189 [RTL_GIGA_MAC_VER_02] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200190 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200191 [RTL_GIGA_MAC_VER_03] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200192 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200193 [RTL_GIGA_MAC_VER_04] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200194 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200195 [RTL_GIGA_MAC_VER_05] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200196 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200197 [RTL_GIGA_MAC_VER_06] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200198 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200199 /* PCI-E devices. */
200 [RTL_GIGA_MAC_VER_07] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200201 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200202 [RTL_GIGA_MAC_VER_08] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200203 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200204 [RTL_GIGA_MAC_VER_09] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200205 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200206 [RTL_GIGA_MAC_VER_10] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200207 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200208 [RTL_GIGA_MAC_VER_11] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200209 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200210 [RTL_GIGA_MAC_VER_12] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200211 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200212 [RTL_GIGA_MAC_VER_13] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200213 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200214 [RTL_GIGA_MAC_VER_14] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200215 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200216 [RTL_GIGA_MAC_VER_15] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200217 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200218 [RTL_GIGA_MAC_VER_16] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200219 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200220 [RTL_GIGA_MAC_VER_17] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200221 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200222 [RTL_GIGA_MAC_VER_18] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200223 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200224 [RTL_GIGA_MAC_VER_19] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200225 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200226 [RTL_GIGA_MAC_VER_20] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200227 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200228 [RTL_GIGA_MAC_VER_21] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200229 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200230 [RTL_GIGA_MAC_VER_22] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200231 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200232 [RTL_GIGA_MAC_VER_23] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200233 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200234 [RTL_GIGA_MAC_VER_24] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200235 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200236 [RTL_GIGA_MAC_VER_25] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200237 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200238 [RTL_GIGA_MAC_VER_26] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200239 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200240 [RTL_GIGA_MAC_VER_27] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200241 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200242 [RTL_GIGA_MAC_VER_28] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200243 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200244 [RTL_GIGA_MAC_VER_29] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200245 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200246 [RTL_GIGA_MAC_VER_30] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200247 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200248 [RTL_GIGA_MAC_VER_31] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200249 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200250 [RTL_GIGA_MAC_VER_32] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200251 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200252 [RTL_GIGA_MAC_VER_33] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200253 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2, JUMBO_9K),
Hayes Wang70090422011-07-06 15:58:06 +0800254 [RTL_GIGA_MAC_VER_34] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200255 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3, JUMBO_9K),
Hayes Wangc2218922011-09-06 16:55:18 +0800256 [RTL_GIGA_MAC_VER_35] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200257 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1, JUMBO_9K),
Hayes Wangc2218922011-09-06 16:55:18 +0800258 [RTL_GIGA_MAC_VER_36] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200259 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2, JUMBO_9K),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800260 [RTL_GIGA_MAC_VER_37] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200261 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1, JUMBO_1K),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800262 [RTL_GIGA_MAC_VER_38] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200263 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1, JUMBO_9K),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800264 [RTL_GIGA_MAC_VER_39] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200265 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1, JUMBO_1K),
Hayes Wangc5583862012-07-02 17:23:22 +0800266 [RTL_GIGA_MAC_VER_40] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200267 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2, JUMBO_9K),
Hayes Wangc5583862012-07-02 17:23:22 +0800268 [RTL_GIGA_MAC_VER_41] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200269 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K),
hayeswang57538c42013-04-01 22:23:40 +0000270 [RTL_GIGA_MAC_VER_42] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200271 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3, JUMBO_9K),
hayeswang58152cd2013-04-01 22:23:42 +0000272 [RTL_GIGA_MAC_VER_43] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200273 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2, JUMBO_1K),
hayeswang45dd95c2013-07-08 17:09:01 +0800274 [RTL_GIGA_MAC_VER_44] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200275 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2, JUMBO_9K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800276 [RTL_GIGA_MAC_VER_45] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200277 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1, JUMBO_9K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800278 [RTL_GIGA_MAC_VER_46] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200279 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2, JUMBO_9K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800280 [RTL_GIGA_MAC_VER_47] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200281 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1, JUMBO_1K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800282 [RTL_GIGA_MAC_VER_48] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200283 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2, JUMBO_1K),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800284 [RTL_GIGA_MAC_VER_49] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200285 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800286 [RTL_GIGA_MAC_VER_50] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200287 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800288 [RTL_GIGA_MAC_VER_51] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200289 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290};
291#undef _R
292
Francois Romieubcf0bf92006-07-26 23:14:13 +0200293enum cfg_version {
294 RTL_CFG_0 = 0x00,
295 RTL_CFG_1,
296 RTL_CFG_2
297};
298
Benoit Taine9baa3c32014-08-08 15:56:03 +0200299static const struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200300 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200301 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Chun-Hao Lin610c9082016-12-27 16:29:43 +0800302 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
Francois Romieud81bf552006-09-20 21:31:20 +0200303 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100304 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200305 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200306 { PCI_VENDOR_ID_DLINK, 0x4300,
307 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200308 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000309 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200310 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200311 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
312 { PCI_VENDOR_ID_LINKSYS, 0x1032,
313 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100314 { 0x0001, 0x8168,
315 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 {0,},
317};
318
319MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
320
Ard Biesheuvel27896c82016-05-14 22:40:15 +0200321static int use_dac = -1;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200322static struct {
323 u32 msg_enable;
324} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Francois Romieu07d3f512007-02-21 22:40:46 +0100326enum rtl_registers {
327 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100328 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100329 MAR0 = 8, /* Multicast filter. */
330 CounterAddrLow = 0x10,
331 CounterAddrHigh = 0x14,
332 TxDescStartAddrLow = 0x20,
333 TxDescStartAddrHigh = 0x24,
334 TxHDescStartAddrLow = 0x28,
335 TxHDescStartAddrHigh = 0x2c,
336 FLASH = 0x30,
337 ERSR = 0x36,
338 ChipCmd = 0x37,
339 TxPoll = 0x38,
340 IntrMask = 0x3c,
341 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700342
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800343 TxConfig = 0x40,
344#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
345#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
346
347 RxConfig = 0x44,
348#define RX128_INT_EN (1 << 15) /* 8111c and later */
349#define RX_MULTI_EN (1 << 14) /* 8111c only */
350#define RXCFG_FIFO_SHIFT 13
351 /* No threshold before first PCI xfer */
352#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000353#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800354#define RXCFG_DMA_SHIFT 8
355 /* Unlimited maximum PCI burst. */
356#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700357
Francois Romieu07d3f512007-02-21 22:40:46 +0100358 RxMissed = 0x4c,
359 Cfg9346 = 0x50,
360 Config0 = 0x51,
361 Config1 = 0x52,
362 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200363#define PME_SIGNAL (1 << 5) /* 8168c and later */
364
Francois Romieu07d3f512007-02-21 22:40:46 +0100365 Config3 = 0x54,
366 Config4 = 0x55,
367 Config5 = 0x56,
368 MultiIntr = 0x5c,
369 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100370 PHYstatus = 0x6c,
371 RxMaxSize = 0xda,
372 CPlusCmd = 0xe0,
373 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300374
375#define RTL_COALESCE_MASK 0x0f
376#define RTL_COALESCE_SHIFT 4
377#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
378#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
379
Francois Romieu07d3f512007-02-21 22:40:46 +0100380 RxDescAddrLow = 0xe4,
381 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000382 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
383
384#define NoEarlyTx 0x3f /* Max value : no early transmit. */
385
386 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
387
388#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800389#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000390
Francois Romieu07d3f512007-02-21 22:40:46 +0100391 FuncEvent = 0xf0,
392 FuncEventMask = 0xf4,
393 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800394 IBCR0 = 0xf8,
395 IBCR2 = 0xf9,
396 IBIMR0 = 0xfa,
397 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100398 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399};
400
Francois Romieuf162a5d2008-06-01 22:37:49 +0200401enum rtl8110_registers {
402 TBICSR = 0x64,
403 TBI_ANAR = 0x68,
404 TBI_LPAR = 0x6a,
405};
406
407enum rtl8168_8101_registers {
408 CSIDR = 0x64,
409 CSIAR = 0x68,
410#define CSIAR_FLAG 0x80000000
411#define CSIAR_WRITE_CMD 0x80000000
Heiner Kallweitff1d7332018-05-02 21:39:56 +0200412#define CSIAR_BYTE_ENABLE 0x0000f000
413#define CSIAR_ADDR_MASK 0x00000fff
françois romieu065c27c2011-01-03 15:08:12 +0000414 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200415 EPHYAR = 0x80,
416#define EPHYAR_FLAG 0x80000000
417#define EPHYAR_WRITE_CMD 0x80000000
418#define EPHYAR_REG_MASK 0x1f
419#define EPHYAR_REG_SHIFT 16
420#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800421 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800422#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800423#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200424 DBG_REG = 0xd1,
425#define FIX_NAK_1 (1 << 4)
426#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800427 TWSI = 0xd2,
428 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800429#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800430#define TX_EMPTY (1 << 5)
431#define RX_EMPTY (1 << 4)
432#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800433#define EN_NDP (1 << 3)
434#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800435#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000436 EFUSEAR = 0xdc,
437#define EFUSEAR_FLAG 0x80000000
438#define EFUSEAR_WRITE_CMD 0x80000000
439#define EFUSEAR_READ_CMD 0x00000000
440#define EFUSEAR_REG_MASK 0x03ff
441#define EFUSEAR_REG_SHIFT 8
442#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800443 MISC_1 = 0xf2,
444#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200445};
446
françois romieuc0e45c12011-01-03 15:08:04 +0000447enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800448 LED_FREQ = 0x1a,
449 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000450 ERIDR = 0x70,
451 ERIAR = 0x74,
452#define ERIAR_FLAG 0x80000000
453#define ERIAR_WRITE_CMD 0x80000000
454#define ERIAR_READ_CMD 0x00000000
455#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000456#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800457#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
458#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
459#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800460#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800461#define ERIAR_MASK_SHIFT 12
462#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
463#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800464#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800465#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800466#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000467 EPHY_RXER_NUM = 0x7c,
468 OCPDR = 0xb0, /* OCP GPHY access */
469#define OCPDR_WRITE_CMD 0x80000000
470#define OCPDR_READ_CMD 0x00000000
471#define OCPDR_REG_MASK 0x7f
472#define OCPDR_GPHY_REG_SHIFT 16
473#define OCPDR_DATA_MASK 0xffff
474 OCPAR = 0xb4,
475#define OCPAR_FLAG 0x80000000
476#define OCPAR_GPHY_WRITE_CMD 0x8000f060
477#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800478 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000479 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
480 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200481#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800482#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800483#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800484#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800485#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000486};
487
Francois Romieu07d3f512007-02-21 22:40:46 +0100488enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100490 SYSErr = 0x8000,
491 PCSTimeout = 0x4000,
492 SWInt = 0x0100,
493 TxDescUnavail = 0x0080,
494 RxFIFOOver = 0x0040,
495 LinkChg = 0x0020,
496 RxOverflow = 0x0010,
497 TxErr = 0x0008,
498 TxOK = 0x0004,
499 RxErr = 0x0002,
500 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400503 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200504 RxFOVF = (1 << 23),
505 RxRWT = (1 << 22),
506 RxRES = (1 << 21),
507 RxRUNT = (1 << 20),
508 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800511 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100512 CmdReset = 0x10,
513 CmdRxEnb = 0x08,
514 CmdTxEnb = 0x04,
515 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Francois Romieu275391a2007-02-23 23:50:28 +0100517 /* TXPoll register p.5 */
518 HPQ = 0x80, /* Poll cmd on the high prio queue */
519 NPQ = 0x40, /* Poll cmd on the low prio queue */
520 FSWInt = 0x01, /* Forced software interrupt */
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100523 Cfg9346_Lock = 0x00,
524 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100527 AcceptErr = 0x20,
528 AcceptRunt = 0x10,
529 AcceptBroadcast = 0x08,
530 AcceptMulticast = 0x04,
531 AcceptMyPhys = 0x02,
532 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200533#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* TxConfigBits */
536 TxInterFrameGapShift = 24,
537 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
538
Francois Romieu5d06a992006-02-23 00:47:58 +0100539 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200540 LEDS1 = (1 << 7),
541 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200542 Speed_down = (1 << 4),
543 MEMMAP = (1 << 3),
544 IOMAP = (1 << 2),
545 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100546 PMEnable = (1 << 0), /* Power Management Enable */
547
Francois Romieu6dccd162007-02-13 23:38:05 +0100548 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000549 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000550 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100551 PCI_Clock_66MHz = 0x01,
552 PCI_Clock_33MHz = 0x00,
553
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100554 /* Config3 register p.25 */
555 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
556 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200557 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800558 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200559 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100560
Francois Romieud58d46b2011-05-03 16:38:29 +0200561 /* Config4 register */
562 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
563
Francois Romieu5d06a992006-02-23 00:47:58 +0100564 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100565 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
566 MWF = (1 << 5), /* Accept Multicast wakeup frame */
567 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200568 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100569 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100570 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000571 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* TBICSR p.28 */
574 TBIReset = 0x80000000,
575 TBILoopback = 0x40000000,
576 TBINwEnable = 0x20000000,
577 TBINwRestart = 0x10000000,
578 TBILinkOk = 0x02000000,
579 TBINwComplete = 0x01000000,
580
581 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200582 EnableBist = (1 << 15), // 8168 8101
583 Mac_dbgo_oe = (1 << 14), // 8168 8101
584 Normal_mode = (1 << 13), // unused
585 Force_half_dup = (1 << 12), // 8168 8101
586 Force_rxflow_en = (1 << 11), // 8168 8101
587 Force_txflow_en = (1 << 10), // 8168 8101
588 Cxpl_dbg_sel = (1 << 9), // 8168 8101
589 ASF = (1 << 8), // 8168 8101
590 PktCntrDisable = (1 << 7), // 8168 8101
591 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 RxVlan = (1 << 6),
593 RxChkSum = (1 << 5),
594 PCIDAC = (1 << 4),
595 PCIMulRW = (1 << 3),
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +0200596#define INTT_MASK GENMASK(1, 0)
Francois Romieu0e485152007-02-20 00:00:26 +0100597 INTT_0 = 0x0000, // 8168
598 INTT_1 = 0x0001, // 8168
599 INTT_2 = 0x0002, // 8168
600 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100603 TBI_Enable = 0x80,
604 TxFlowCtrl = 0x40,
605 RxFlowCtrl = 0x20,
606 _1000bpsF = 0x10,
607 _100bps = 0x08,
608 _10bps = 0x04,
609 LinkStatus = 0x02,
610 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100613 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200614
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200615 /* ResetCounterCommand */
616 CounterReset = 0x1,
617
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200618 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100619 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800620
621 /* magic enable v2 */
622 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623};
624
Francois Romieu2b7b4312011-04-18 22:53:24 -0700625enum rtl_desc_bit {
626 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
628 RingEnd = (1 << 30), /* End of descriptor ring */
629 FirstFrag = (1 << 29), /* First segment of a packet */
630 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700631};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Francois Romieu2b7b4312011-04-18 22:53:24 -0700633/* Generic case. */
634enum rtl_tx_desc_bit {
635 /* First doubleword. */
636 TD_LSO = (1 << 27), /* Large Send Offload */
637#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Francois Romieu2b7b4312011-04-18 22:53:24 -0700639 /* Second doubleword. */
640 TxVlanTag = (1 << 17), /* Add VLAN tag */
641};
642
643/* 8169, 8168b and 810x except 8102e. */
644enum rtl_tx_desc_bit_0 {
645 /* First doubleword. */
646#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
647 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
648 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
649 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
650};
651
652/* 8102e, 8168c and beyond. */
653enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800654 /* First doubleword. */
655 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800656 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800657#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800658#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800659
Francois Romieu2b7b4312011-04-18 22:53:24 -0700660 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800661#define TCPHO_SHIFT 18
662#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700663#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800664 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
665 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700666 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
667 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
668};
669
Francois Romieu2b7b4312011-04-18 22:53:24 -0700670enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /* Rx private */
672 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500673 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675#define RxProtoUDP (PID1)
676#define RxProtoTCP (PID0)
677#define RxProtoIP (PID1 | PID0)
678#define RxProtoMask RxProtoIP
679
680 IPFail = (1 << 16), /* IP checksum failed */
681 UDPFail = (1 << 15), /* UDP/IP checksum failed */
682 TCPFail = (1 << 14), /* TCP/IP checksum failed */
683 RxVlanTag = (1 << 16), /* VLAN tag available */
684};
685
686#define RsvdMask 0x3fffc000
Heiner Kallweit12d42c52018-04-28 22:19:30 +0200687#define CPCMD_QUIRK_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200690 __le32 opts1;
691 __le32 opts2;
692 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693};
694
695struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200696 __le32 opts1;
697 __le32 opts2;
698 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699};
700
701struct ring_info {
702 struct sk_buff *skb;
703 u32 len;
704 u8 __pad[sizeof(void *) - sizeof(u32)];
705};
706
Ivan Vecera355423d2009-02-06 21:49:57 -0800707struct rtl8169_counters {
708 __le64 tx_packets;
709 __le64 rx_packets;
710 __le64 tx_errors;
711 __le32 rx_errors;
712 __le16 rx_missed;
713 __le16 align_errors;
714 __le32 tx_one_collision;
715 __le32 tx_multi_collision;
716 __le64 rx_unicast;
717 __le64 rx_broadcast;
718 __le32 rx_multicast;
719 __le16 tx_aborted;
720 __le16 tx_underun;
721};
722
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200723struct rtl8169_tc_offsets {
724 bool inited;
725 __le64 tx_errors;
726 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200727 __le16 tx_aborted;
728};
729
Francois Romieuda78dbf2012-01-26 14:18:23 +0100730enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100731 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100732 RTL_FLAG_TASK_SLOW_PENDING,
733 RTL_FLAG_TASK_RESET_PENDING,
734 RTL_FLAG_TASK_PHY_PENDING,
735 RTL_FLAG_MAX
736};
737
Junchang Wang8027aa22012-03-04 23:30:32 +0100738struct rtl8169_stats {
739 u64 packets;
740 u64 bytes;
741 struct u64_stats_sync syncp;
742};
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744struct rtl8169_private {
745 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200746 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000747 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700748 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200749 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700750 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
752 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100754 struct rtl8169_stats rx_stats;
755 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
757 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
758 dma_addr_t TxPhyAddr;
759 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000760 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 struct timer_list timer;
763 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100764
765 u16 event_slow;
Francois Romieu50970832017-10-27 13:24:49 +0300766 const struct rtl_coalesce_info *coalesce_info;
françois romieuc0e45c12011-01-03 15:08:04 +0000767
768 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200769 void (*write)(struct rtl8169_private *, int, int);
770 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000771 } mdio_ops;
772
Francois Romieud58d46b2011-05-03 16:38:29 +0200773 struct jumbo_ops {
774 void (*enable)(struct rtl8169_private *);
775 void (*disable)(struct rtl8169_private *);
776 } jumbo_ops;
777
Oliver Neukum54405cd2011-01-06 21:55:13 +0100778 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +0100779 int (*get_link_ksettings)(struct net_device *,
780 struct ethtool_link_ksettings *);
françois romieu4da19632011-01-03 15:07:55 +0000781 void (*phy_reset_enable)(struct rtl8169_private *tp);
Heiner Kallweit61cb5322018-04-17 23:27:38 +0200782 void (*hw_start)(struct rtl8169_private *tp);
françois romieu4da19632011-01-03 15:07:55 +0000783 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200784 unsigned int (*link_ok)(struct rtl8169_private *tp);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800785 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800786 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100787
788 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100789 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
790 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100791 struct work_struct work;
792 } wk;
793
Francois Romieuccdffb92008-07-26 14:26:06 +0200794 struct mii_if_info mii;
Corinna Vinschen42020322015-09-10 10:47:35 +0200795 dma_addr_t counters_phys_addr;
796 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200797 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000798 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000799
Francois Romieub6ffd972011-06-17 17:00:05 +0200800 struct rtl_fw {
801 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200802
803#define RTL_VER_SIZE 32
804
805 char version[RTL_VER_SIZE];
806
807 struct rtl_fw_phy_action {
808 __le32 *code;
809 size_t size;
810 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200811 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300812#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800813
814 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815};
816
Ralf Baechle979b6c12005-06-13 14:30:40 -0700817MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700820MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200821module_param_named(debug, debug.msg_enable, int, 0);
822MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823MODULE_LICENSE("GPL");
824MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000825MODULE_FIRMWARE(FIRMWARE_8168D_1);
826MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000827MODULE_FIRMWARE(FIRMWARE_8168E_1);
828MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400829MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800830MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800831MODULE_FIRMWARE(FIRMWARE_8168F_1);
832MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800833MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800834MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800835MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800836MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000837MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000838MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000839MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800840MODULE_FIRMWARE(FIRMWARE_8168H_1);
841MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200842MODULE_FIRMWARE(FIRMWARE_8107E_1);
843MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Heiner Kallweit1e1205b2018-03-20 07:45:42 +0100845static inline struct device *tp_to_dev(struct rtl8169_private *tp)
846{
847 return &tp->pci_dev->dev;
848}
849
Francois Romieuda78dbf2012-01-26 14:18:23 +0100850static void rtl_lock_work(struct rtl8169_private *tp)
851{
852 mutex_lock(&tp->wk.mutex);
853}
854
855static void rtl_unlock_work(struct rtl8169_private *tp)
856{
857 mutex_unlock(&tp->wk.mutex);
858}
859
Heiner Kallweitcb732002018-03-20 07:45:35 +0100860static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
Francois Romieud58d46b2011-05-03 16:38:29 +0200861{
Heiner Kallweitcb732002018-03-20 07:45:35 +0100862 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +0800863 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200864}
865
Francois Romieuffc46952012-07-06 14:19:23 +0200866struct rtl_cond {
867 bool (*check)(struct rtl8169_private *);
868 const char *msg;
869};
870
871static void rtl_udelay(unsigned int d)
872{
873 udelay(d);
874}
875
876static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
877 void (*delay)(unsigned int), unsigned int d, int n,
878 bool high)
879{
880 int i;
881
882 for (i = 0; i < n; i++) {
883 delay(d);
884 if (c->check(tp) == high)
885 return true;
886 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200887 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
888 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200889 return false;
890}
891
892static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
893 const struct rtl_cond *c,
894 unsigned int d, int n)
895{
896 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
897}
898
899static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
900 const struct rtl_cond *c,
901 unsigned int d, int n)
902{
903 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
904}
905
906static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
907 const struct rtl_cond *c,
908 unsigned int d, int n)
909{
910 return rtl_loop_wait(tp, c, msleep, d, n, true);
911}
912
913static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
914 const struct rtl_cond *c,
915 unsigned int d, int n)
916{
917 return rtl_loop_wait(tp, c, msleep, d, n, false);
918}
919
920#define DECLARE_RTL_COND(name) \
921static bool name ## _check(struct rtl8169_private *); \
922 \
923static const struct rtl_cond name = { \
924 .check = name ## _check, \
925 .msg = #name \
926}; \
927 \
928static bool name ## _check(struct rtl8169_private *tp)
929
Hayes Wangc5583862012-07-02 17:23:22 +0800930static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
931{
932 if (reg & 0xffff0001) {
933 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
934 return true;
935 }
936 return false;
937}
938
939DECLARE_RTL_COND(rtl_ocp_gphy_cond)
940{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200941 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
Hayes Wangc5583862012-07-02 17:23:22 +0800942}
943
944static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
945{
Hayes Wangc5583862012-07-02 17:23:22 +0800946 if (rtl_ocp_reg_failure(tp, reg))
947 return;
948
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200949 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800950
951 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
952}
953
954static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
955{
Hayes Wangc5583862012-07-02 17:23:22 +0800956 if (rtl_ocp_reg_failure(tp, reg))
957 return 0;
958
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200959 RTL_W32(tp, GPHY_OCP, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800960
961 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200962 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
Hayes Wangc5583862012-07-02 17:23:22 +0800963}
964
Hayes Wangc5583862012-07-02 17:23:22 +0800965static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
966{
Hayes Wangc5583862012-07-02 17:23:22 +0800967 if (rtl_ocp_reg_failure(tp, reg))
968 return;
969
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200970 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800971}
972
973static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
974{
Hayes Wangc5583862012-07-02 17:23:22 +0800975 if (rtl_ocp_reg_failure(tp, reg))
976 return 0;
977
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200978 RTL_W32(tp, OCPDR, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800979
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200980 return RTL_R32(tp, OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +0800981}
982
983#define OCP_STD_PHY_BASE 0xa400
984
985static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
986{
987 if (reg == 0x1f) {
988 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
989 return;
990 }
991
992 if (tp->ocp_base != OCP_STD_PHY_BASE)
993 reg -= 0x10;
994
995 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
996}
997
998static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
999{
1000 if (tp->ocp_base != OCP_STD_PHY_BASE)
1001 reg -= 0x10;
1002
1003 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1004}
1005
hayeswangeee37862013-04-01 22:23:38 +00001006static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1007{
1008 if (reg == 0x1f) {
1009 tp->ocp_base = value << 4;
1010 return;
1011 }
1012
1013 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1014}
1015
1016static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1017{
1018 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1019}
1020
Francois Romieuffc46952012-07-06 14:19:23 +02001021DECLARE_RTL_COND(rtl_phyar_cond)
1022{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001023 return RTL_R32(tp, PHYAR) & 0x80000000;
Francois Romieuffc46952012-07-06 14:19:23 +02001024}
1025
Francois Romieu24192212012-07-06 20:19:42 +02001026static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001028 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Francois Romieuffc46952012-07-06 14:19:23 +02001030 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001031 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001032 * According to hardware specs a 20us delay is required after write
1033 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001034 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001035 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Francois Romieu24192212012-07-06 20:19:42 +02001038static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Francois Romieuffc46952012-07-06 14:19:23 +02001040 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001042 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Francois Romieuffc46952012-07-06 14:19:23 +02001044 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001045 RTL_R32(tp, PHYAR) & 0xffff : ~0;
Francois Romieuffc46952012-07-06 14:19:23 +02001046
Timo Teräs81a95f02010-06-09 17:31:48 -07001047 /*
1048 * According to hardware specs a 20us delay is required after read
1049 * complete indication, but before sending next command.
1050 */
1051 udelay(20);
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return value;
1054}
1055
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001056DECLARE_RTL_COND(rtl_ocpar_cond)
1057{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001058 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001059}
1060
Francois Romieu24192212012-07-06 20:19:42 +02001061static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001062{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001063 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1064 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
1065 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001066
Francois Romieuffc46952012-07-06 14:19:23 +02001067 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001068}
1069
Francois Romieu24192212012-07-06 20:19:42 +02001070static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001071{
Francois Romieu24192212012-07-06 20:19:42 +02001072 r8168dp_1_mdio_access(tp, reg,
1073 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001074}
1075
Francois Romieu24192212012-07-06 20:19:42 +02001076static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001077{
Francois Romieu24192212012-07-06 20:19:42 +02001078 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001079
1080 mdelay(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001081 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
1082 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001083
Francois Romieuffc46952012-07-06 14:19:23 +02001084 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001085 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001086}
1087
françois romieue6de30d2011-01-03 15:08:37 +00001088#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1089
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001090static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001091{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001092 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001093}
1094
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001095static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001096{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001097 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001098}
1099
Francois Romieu24192212012-07-06 20:19:42 +02001100static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001101{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001102 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001103
Francois Romieu24192212012-07-06 20:19:42 +02001104 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001105
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001106 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001107}
1108
Francois Romieu24192212012-07-06 20:19:42 +02001109static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001110{
1111 int value;
1112
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001113 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001114
Francois Romieu24192212012-07-06 20:19:42 +02001115 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001116
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001117 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001118
1119 return value;
1120}
1121
françois romieu4da19632011-01-03 15:07:55 +00001122static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001123{
Francois Romieu24192212012-07-06 20:19:42 +02001124 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001125}
1126
françois romieu4da19632011-01-03 15:07:55 +00001127static int rtl_readphy(struct rtl8169_private *tp, int location)
1128{
Francois Romieu24192212012-07-06 20:19:42 +02001129 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001130}
1131
1132static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1133{
1134 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1135}
1136
Chun-Hao Lin76564422014-10-01 23:17:17 +08001137static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001138{
1139 int val;
1140
françois romieu4da19632011-01-03 15:07:55 +00001141 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001142 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001143}
1144
Francois Romieuccdffb92008-07-26 14:26:06 +02001145static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1146 int val)
1147{
1148 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001149
françois romieu4da19632011-01-03 15:07:55 +00001150 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001151}
1152
1153static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1154{
1155 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001156
françois romieu4da19632011-01-03 15:07:55 +00001157 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001158}
1159
Francois Romieuffc46952012-07-06 14:19:23 +02001160DECLARE_RTL_COND(rtl_ephyar_cond)
1161{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001162 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001163}
1164
Francois Romieufdf6fc02012-07-06 22:40:38 +02001165static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001166{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001167 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
Francois Romieudacf8152008-08-02 20:44:13 +02001168 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1169
Francois Romieuffc46952012-07-06 14:19:23 +02001170 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1171
1172 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001173}
1174
Francois Romieufdf6fc02012-07-06 22:40:38 +02001175static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001176{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001177 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
Francois Romieudacf8152008-08-02 20:44:13 +02001178
Francois Romieuffc46952012-07-06 14:19:23 +02001179 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001180 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001181}
1182
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001183DECLARE_RTL_COND(rtl_eriar_cond)
1184{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001185 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001186}
1187
Francois Romieufdf6fc02012-07-06 22:40:38 +02001188static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1189 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001190{
Hayes Wang133ac402011-07-06 15:58:05 +08001191 BUG_ON((addr & 3) || (mask == 0));
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001192 RTL_W32(tp, ERIDR, val);
1193 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001194
Francois Romieuffc46952012-07-06 14:19:23 +02001195 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001196}
1197
Francois Romieufdf6fc02012-07-06 22:40:38 +02001198static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001199{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001200 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001201
Francois Romieuffc46952012-07-06 14:19:23 +02001202 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001203 RTL_R32(tp, ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001204}
1205
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001206static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001207 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001208{
1209 u32 val;
1210
Francois Romieufdf6fc02012-07-06 22:40:38 +02001211 val = rtl_eri_read(tp, addr, type);
1212 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001213}
1214
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001215static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1216{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001217 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001218 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001219 RTL_R32(tp, OCPDR) : ~0;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001220}
1221
1222static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1223{
1224 return rtl_eri_read(tp, reg, ERIAR_OOB);
1225}
1226
1227static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1228{
1229 switch (tp->mac_version) {
1230 case RTL_GIGA_MAC_VER_27:
1231 case RTL_GIGA_MAC_VER_28:
1232 case RTL_GIGA_MAC_VER_31:
1233 return r8168dp_ocp_read(tp, mask, reg);
1234 case RTL_GIGA_MAC_VER_49:
1235 case RTL_GIGA_MAC_VER_50:
1236 case RTL_GIGA_MAC_VER_51:
1237 return r8168ep_ocp_read(tp, mask, reg);
1238 default:
1239 BUG();
1240 return ~0;
1241 }
1242}
1243
1244static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1245 u32 data)
1246{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001247 RTL_W32(tp, OCPDR, data);
1248 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001249 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1250}
1251
1252static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1253 u32 data)
1254{
1255 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1256 data, ERIAR_OOB);
1257}
1258
1259static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1260{
1261 switch (tp->mac_version) {
1262 case RTL_GIGA_MAC_VER_27:
1263 case RTL_GIGA_MAC_VER_28:
1264 case RTL_GIGA_MAC_VER_31:
1265 r8168dp_ocp_write(tp, mask, reg, data);
1266 break;
1267 case RTL_GIGA_MAC_VER_49:
1268 case RTL_GIGA_MAC_VER_50:
1269 case RTL_GIGA_MAC_VER_51:
1270 r8168ep_ocp_write(tp, mask, reg, data);
1271 break;
1272 default:
1273 BUG();
1274 break;
1275 }
1276}
1277
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001278static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1279{
1280 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1281
1282 ocp_write(tp, 0x1, 0x30, 0x00000001);
1283}
1284
1285#define OOB_CMD_RESET 0x00
1286#define OOB_CMD_DRIVER_START 0x05
1287#define OOB_CMD_DRIVER_STOP 0x06
1288
1289static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1290{
1291 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1292}
1293
1294DECLARE_RTL_COND(rtl_ocp_read_cond)
1295{
1296 u16 reg;
1297
1298 reg = rtl8168_get_ocp_reg(tp);
1299
1300 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1301}
1302
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001303DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1304{
1305 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1306}
1307
1308DECLARE_RTL_COND(rtl_ocp_tx_cond)
1309{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001310 return RTL_R8(tp, IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001311}
1312
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001313static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1314{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001315 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001316 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001317 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1318 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001319}
1320
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001321static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001322{
1323 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001324 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1325}
1326
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001327static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1328{
1329 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1330 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1331 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1332}
1333
1334static void rtl8168_driver_start(struct rtl8169_private *tp)
1335{
1336 switch (tp->mac_version) {
1337 case RTL_GIGA_MAC_VER_27:
1338 case RTL_GIGA_MAC_VER_28:
1339 case RTL_GIGA_MAC_VER_31:
1340 rtl8168dp_driver_start(tp);
1341 break;
1342 case RTL_GIGA_MAC_VER_49:
1343 case RTL_GIGA_MAC_VER_50:
1344 case RTL_GIGA_MAC_VER_51:
1345 rtl8168ep_driver_start(tp);
1346 break;
1347 default:
1348 BUG();
1349 break;
1350 }
1351}
1352
1353static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1354{
1355 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1356 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1357}
1358
1359static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1360{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001361 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001362 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1363 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1364 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1365}
1366
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001367static void rtl8168_driver_stop(struct rtl8169_private *tp)
1368{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001369 switch (tp->mac_version) {
1370 case RTL_GIGA_MAC_VER_27:
1371 case RTL_GIGA_MAC_VER_28:
1372 case RTL_GIGA_MAC_VER_31:
1373 rtl8168dp_driver_stop(tp);
1374 break;
1375 case RTL_GIGA_MAC_VER_49:
1376 case RTL_GIGA_MAC_VER_50:
1377 case RTL_GIGA_MAC_VER_51:
1378 rtl8168ep_driver_stop(tp);
1379 break;
1380 default:
1381 BUG();
1382 break;
1383 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001384}
1385
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001386static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001387{
1388 u16 reg = rtl8168_get_ocp_reg(tp);
1389
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001390 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001391}
1392
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001393static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001394{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001395 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001396}
1397
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001398static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001399{
1400 switch (tp->mac_version) {
1401 case RTL_GIGA_MAC_VER_27:
1402 case RTL_GIGA_MAC_VER_28:
1403 case RTL_GIGA_MAC_VER_31:
1404 return r8168dp_check_dash(tp);
1405 case RTL_GIGA_MAC_VER_49:
1406 case RTL_GIGA_MAC_VER_50:
1407 case RTL_GIGA_MAC_VER_51:
1408 return r8168ep_check_dash(tp);
1409 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001410 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001411 }
1412}
1413
françois romieuc28aa382011-08-02 03:53:43 +00001414struct exgmac_reg {
1415 u16 addr;
1416 u16 mask;
1417 u32 val;
1418};
1419
Francois Romieufdf6fc02012-07-06 22:40:38 +02001420static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001421 const struct exgmac_reg *r, int len)
1422{
1423 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001424 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001425 r++;
1426 }
1427}
1428
Francois Romieuffc46952012-07-06 14:19:23 +02001429DECLARE_RTL_COND(rtl_efusear_cond)
1430{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001431 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001432}
1433
Francois Romieufdf6fc02012-07-06 22:40:38 +02001434static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001435{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001436 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
françois romieudaf9df62009-10-07 12:44:20 +00001437
Francois Romieuffc46952012-07-06 14:19:23 +02001438 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001439 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001440}
1441
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001442static u16 rtl_get_events(struct rtl8169_private *tp)
1443{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001444 return RTL_R16(tp, IntrStatus);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001445}
1446
1447static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1448{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001449 RTL_W16(tp, IntrStatus, bits);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001450 mmiowb();
1451}
1452
1453static void rtl_irq_disable(struct rtl8169_private *tp)
1454{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001455 RTL_W16(tp, IntrMask, 0);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001456 mmiowb();
1457}
1458
Francois Romieu3e990ff2012-01-26 12:50:01 +01001459static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1460{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001461 RTL_W16(tp, IntrMask, bits);
Francois Romieu3e990ff2012-01-26 12:50:01 +01001462}
1463
Francois Romieuda78dbf2012-01-26 14:18:23 +01001464#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1465#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1466#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1467
1468static void rtl_irq_enable_all(struct rtl8169_private *tp)
1469{
1470 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1471}
1472
françois romieu811fd302011-12-04 20:30:45 +00001473static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001475 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001476 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001477 RTL_R8(tp, ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
françois romieu4da19632011-01-03 15:07:55 +00001480static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001482 return RTL_R32(tp, TBICSR) & TBIReset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
françois romieu4da19632011-01-03 15:07:55 +00001485static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
françois romieu4da19632011-01-03 15:07:55 +00001487 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001490static unsigned int rtl8169_tbi_link_ok(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001492 return RTL_R32(tp, TBICSR) & TBILinkOk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001495static unsigned int rtl8169_xmii_link_ok(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001497 return RTL_R8(tp, PHYstatus) & LinkStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
françois romieu4da19632011-01-03 15:07:55 +00001500static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001502 RTL_W32(tp, TBICSR, RTL_R32(tp, TBICSR) | TBIReset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
françois romieu4da19632011-01-03 15:07:55 +00001505static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 unsigned int val;
1508
françois romieu4da19632011-01-03 15:07:55 +00001509 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1510 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
Hayes Wang70090422011-07-06 15:58:06 +08001513static void rtl_link_chg_patch(struct rtl8169_private *tp)
1514{
Hayes Wang70090422011-07-06 15:58:06 +08001515 struct net_device *dev = tp->dev;
1516
1517 if (!netif_running(dev))
1518 return;
1519
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001520 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1521 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001522 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001523 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1524 ERIAR_EXGMAC);
1525 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1526 ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001527 } else if (RTL_R8(tp, PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001528 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1529 ERIAR_EXGMAC);
1530 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1531 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001532 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001533 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1534 ERIAR_EXGMAC);
1535 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1536 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001537 }
1538 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001539 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001540 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001541 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001542 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001543 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1544 tp->mac_version == RTL_GIGA_MAC_VER_36) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001545 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001546 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1547 ERIAR_EXGMAC);
1548 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1549 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001550 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001551 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1552 ERIAR_EXGMAC);
1553 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1554 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001555 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001556 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001557 if (RTL_R8(tp, PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001558 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1559 ERIAR_EXGMAC);
1560 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1561 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001562 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001563 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1564 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001565 }
Hayes Wang70090422011-07-06 15:58:06 +08001566 }
1567}
1568
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01001569static void rtl8169_check_link_status(struct net_device *dev,
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001570 struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001572 struct device *d = tp_to_dev(tp);
1573
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001574 if (tp->link_ok(tp)) {
Hayes Wang70090422011-07-06 15:58:06 +08001575 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001576 /* This is to cancel a scheduled suspend if there's one. */
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001577 pm_request_resume(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001579 if (net_ratelimit())
1580 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001581 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001583 netif_info(tp, ifdown, dev, "link down\n");
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001584 pm_runtime_idle(d);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586}
1587
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001588#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1589
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001590/* Currently we only enable WoL if explicitly told by userspace to circumvent
1591 * issues on certain platforms, see commit bde135a672bf ("r8169: only enable
1592 * PCI wakeups when WOL is active"). Let's keep __rtl8169_get_wol() for the
1593 * case that we want to respect BIOS settings again.
1594 */
1595#if 0
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001596static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1597{
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001598 u8 options;
1599 u32 wolopts = 0;
1600
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001601 options = RTL_R8(tp, Config1);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001602 if (!(options & PMEnable))
1603 return 0;
1604
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001605 options = RTL_R8(tp, Config3);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001606 if (options & LinkUp)
1607 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001608 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001609 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1610 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001611 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1612 wolopts |= WAKE_MAGIC;
1613 break;
1614 default:
1615 if (options & MagicPacket)
1616 wolopts |= WAKE_MAGIC;
1617 break;
1618 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001619
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001620 options = RTL_R8(tp, Config5);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001621 if (options & UWF)
1622 wolopts |= WAKE_UCAST;
1623 if (options & BWF)
1624 wolopts |= WAKE_BCAST;
1625 if (options & MWF)
1626 wolopts |= WAKE_MCAST;
1627
1628 return wolopts;
1629}
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001630#endif
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001631
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001632static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1633{
1634 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001635
Francois Romieuda78dbf2012-01-26 14:18:23 +01001636 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001637 wol->supported = WAKE_ANY;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001638 wol->wolopts = tp->saved_wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001639 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001640}
1641
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001642static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001643{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001644 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001645 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001646 u32 opt;
1647 u16 reg;
1648 u8 mask;
1649 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001650 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001651 { WAKE_UCAST, Config5, UWF },
1652 { WAKE_BCAST, Config5, BWF },
1653 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001654 { WAKE_ANY, Config5, LanWake },
1655 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001656 };
Francois Romieu851e6022012-04-17 11:10:11 +02001657 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001658
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001659 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001660
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001661 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001662 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1663 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001664 tmp = ARRAY_SIZE(cfg) - 1;
1665 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001666 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001667 0x0dc,
1668 ERIAR_MASK_0100,
1669 MagicPacket_v2,
1670 0x0000,
1671 ERIAR_EXGMAC);
1672 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001673 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001674 0x0dc,
1675 ERIAR_MASK_0100,
1676 0x0000,
1677 MagicPacket_v2,
1678 ERIAR_EXGMAC);
1679 break;
1680 default:
1681 tmp = ARRAY_SIZE(cfg);
1682 break;
1683 }
1684
1685 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001686 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001687 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001688 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001689 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001690 }
1691
Francois Romieu851e6022012-04-17 11:10:11 +02001692 switch (tp->mac_version) {
1693 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001694 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001695 if (wolopts)
1696 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001697 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001698 break;
1699 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001700 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001701 if (wolopts)
1702 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001703 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001704 break;
1705 }
1706
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001707 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001708}
1709
1710static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1711{
1712 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001713 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001714
1715 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001716
Francois Romieuda78dbf2012-01-26 14:18:23 +01001717 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001718
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001719 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
1720
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001721 if (pm_runtime_active(d))
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001722 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001723
1724 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001725
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001726 device_set_wakeup_enable(d, tp->saved_wolopts);
françois romieuea809072010-11-08 13:23:58 +00001727
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001728 pm_runtime_put_noidle(d);
1729
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001730 return 0;
1731}
1732
Francois Romieu31bd2042011-04-26 18:58:59 +02001733static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1734{
Francois Romieu85bffe62011-04-27 08:22:39 +02001735 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001736}
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738static void rtl8169_get_drvinfo(struct net_device *dev,
1739 struct ethtool_drvinfo *info)
1740{
1741 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001742 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Rick Jones68aad782011-11-07 13:29:27 +00001744 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1745 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1746 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001747 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001748 if (!IS_ERR_OR_NULL(rtl_fw))
1749 strlcpy(info->fw_version, rtl_fw->version,
1750 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751}
1752
1753static int rtl8169_get_regs_len(struct net_device *dev)
1754{
1755 return R8169_REGS_SIZE;
1756}
1757
1758static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001759 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 int ret = 0;
1763 u32 reg;
1764
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001765 reg = RTL_R32(tp, TBICSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1767 (duplex == DUPLEX_FULL)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001768 RTL_W32(tp, TBICSR, reg & ~(TBINwEnable | TBINwRestart));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 } else if (autoneg == AUTONEG_ENABLE)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001770 RTL_W32(tp, TBICSR, reg | TBINwEnable | TBINwRestart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001772 netif_warn(tp, link, dev,
1773 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 ret = -EOPNOTSUPP;
1775 }
1776
1777 return ret;
1778}
1779
1780static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001781 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
1783 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001784 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001785 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Hayes Wang716b50a2011-02-22 17:26:18 +08001787 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001790 int auto_nego;
1791
françois romieu4da19632011-01-03 15:07:55 +00001792 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001793 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1794 ADVERTISE_100HALF | ADVERTISE_100FULL);
1795
1796 if (adv & ADVERTISED_10baseT_Half)
1797 auto_nego |= ADVERTISE_10HALF;
1798 if (adv & ADVERTISED_10baseT_Full)
1799 auto_nego |= ADVERTISE_10FULL;
1800 if (adv & ADVERTISED_100baseT_Half)
1801 auto_nego |= ADVERTISE_100HALF;
1802 if (adv & ADVERTISED_100baseT_Full)
1803 auto_nego |= ADVERTISE_100FULL;
1804
françois romieu3577aa12009-05-19 10:46:48 +00001805 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1806
françois romieu4da19632011-01-03 15:07:55 +00001807 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001808 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1809
1810 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001811 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001812 if (adv & ADVERTISED_1000baseT_Half)
1813 giga_ctrl |= ADVERTISE_1000HALF;
1814 if (adv & ADVERTISED_1000baseT_Full)
1815 giga_ctrl |= ADVERTISE_1000FULL;
1816 } else if (adv & (ADVERTISED_1000baseT_Half |
1817 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001818 netif_info(tp, link, dev,
1819 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001820 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
françois romieu3577aa12009-05-19 10:46:48 +00001823 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001824
françois romieu4da19632011-01-03 15:07:55 +00001825 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1826 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001827 } else {
françois romieu3577aa12009-05-19 10:46:48 +00001828 if (speed == SPEED_10)
1829 bmcr = 0;
1830 else if (speed == SPEED_100)
1831 bmcr = BMCR_SPEED100;
1832 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001833 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001834
1835 if (duplex == DUPLEX_FULL)
1836 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001837 }
1838
françois romieu4da19632011-01-03 15:07:55 +00001839 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001840
Francois Romieucecb5fd2011-04-01 10:21:07 +02001841 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1842 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001843 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001844 rtl_writephy(tp, 0x17, 0x2138);
1845 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001846 } else {
françois romieu4da19632011-01-03 15:07:55 +00001847 rtl_writephy(tp, 0x17, 0x2108);
1848 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001849 }
1850 }
1851
Oliver Neukum54405cd2011-01-06 21:55:13 +01001852 rc = 0;
1853out:
1854 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
1857static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001858 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
1860 struct rtl8169_private *tp = netdev_priv(dev);
1861 int ret;
1862
Oliver Neukum54405cd2011-01-06 21:55:13 +01001863 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001864 if (ret < 0)
1865 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Francois Romieu4876cc12011-03-11 21:07:11 +01001867 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
Chun-Hao Linc4556972016-03-11 14:21:14 +08001868 (advertising & ADVERTISED_1000baseT_Full) &&
1869 !pci_is_pcie(tp->pci_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001871 }
1872out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return ret;
1874}
1875
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001876static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1877 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Francois Romieud58d46b2011-05-03 16:38:29 +02001879 struct rtl8169_private *tp = netdev_priv(dev);
1880
Francois Romieu2b7b4312011-04-18 22:53:24 -07001881 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001882 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Francois Romieud58d46b2011-05-03 16:38:29 +02001884 if (dev->mtu > JUMBO_1K &&
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02001885 tp->mac_version > RTL_GIGA_MAC_VER_06)
Francois Romieud58d46b2011-05-03 16:38:29 +02001886 features &= ~NETIF_F_IP_CSUM;
1887
Michał Mirosław350fb322011-04-08 06:35:56 +00001888 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
1890
Heiner Kallweita3984572018-04-28 22:19:15 +02001891static int rtl8169_set_features(struct net_device *dev,
1892 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
1894 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001895 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Heiner Kallweita3984572018-04-28 22:19:15 +02001897 rtl_lock_work(tp);
1898
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001899 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001900 if (features & NETIF_F_RXALL)
1901 rx_config |= (AcceptErr | AcceptRunt);
1902 else
1903 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001905 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001906
hayeswang929a0312014-09-16 11:40:47 +08001907 if (features & NETIF_F_RXCSUM)
1908 tp->cp_cmd |= RxChkSum;
1909 else
1910 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001911
hayeswang929a0312014-09-16 11:40:47 +08001912 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1913 tp->cp_cmd |= RxVlan;
1914 else
1915 tp->cp_cmd &= ~RxVlan;
1916
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001917 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1918 RTL_R16(tp, CPlusCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Francois Romieuda78dbf2012-01-26 14:18:23 +01001920 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 return 0;
1923}
1924
Kirill Smelkov810f4892012-11-10 21:11:02 +04001925static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001927 return (skb_vlan_tag_present(skb)) ?
1928 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
Francois Romieu7a8fc772011-03-01 17:18:33 +01001931static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
1933 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Francois Romieu7a8fc772011-03-01 17:18:33 +01001935 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001936 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937}
1938
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001939static int rtl8169_get_link_ksettings_tbi(struct net_device *dev,
1940 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
1942 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 u32 status;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001944 u32 supported, advertising;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001946 supported =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001948 cmd->base.port = PORT_FIBRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001950 status = RTL_R32(tp, TBICSR);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001951 advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1952 cmd->base.autoneg = !!(status & TBINwEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001954 cmd->base.speed = SPEED_1000;
1955 cmd->base.duplex = DUPLEX_FULL; /* Always set */
1956
1957 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1958 supported);
1959 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1960 advertising);
Francois Romieuccdffb92008-07-26 14:26:06 +02001961
1962 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001965static int rtl8169_get_link_ksettings_xmii(struct net_device *dev,
1966 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
1968 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03001970 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
1971
1972 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001975static int rtl8169_get_link_ksettings(struct net_device *dev,
1976 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
1978 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001979 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Francois Romieuda78dbf2012-01-26 14:18:23 +01001981 rtl_lock_work(tp);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001982 rc = tp->get_link_ksettings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001983 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Francois Romieuccdffb92008-07-26 14:26:06 +02001985 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
1987
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01001988static int rtl8169_set_link_ksettings(struct net_device *dev,
1989 const struct ethtool_link_ksettings *cmd)
1990{
1991 struct rtl8169_private *tp = netdev_priv(dev);
1992 int rc;
1993 u32 advertising;
1994
1995 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
1996 cmd->link_modes.advertising))
1997 return -EINVAL;
1998
1999 del_timer_sync(&tp->timer);
2000
2001 rtl_lock_work(tp);
2002 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
2003 cmd->base.duplex, advertising);
2004 rtl_unlock_work(tp);
2005
2006 return rc;
2007}
2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2010 void *p)
2011{
Francois Romieu5b0384f2006-08-16 16:00:01 +02002012 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02002013 u32 __iomem *data = tp->mmio_addr;
2014 u32 *dw = p;
2015 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Francois Romieuda78dbf2012-01-26 14:18:23 +01002017 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02002018 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2019 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002020 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002023static u32 rtl8169_get_msglevel(struct net_device *dev)
2024{
2025 struct rtl8169_private *tp = netdev_priv(dev);
2026
2027 return tp->msg_enable;
2028}
2029
2030static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2031{
2032 struct rtl8169_private *tp = netdev_priv(dev);
2033
2034 tp->msg_enable = value;
2035}
2036
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002037static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2038 "tx_packets",
2039 "rx_packets",
2040 "tx_errors",
2041 "rx_errors",
2042 "rx_missed",
2043 "align_errors",
2044 "tx_single_collisions",
2045 "tx_multi_collisions",
2046 "unicast",
2047 "broadcast",
2048 "multicast",
2049 "tx_aborted",
2050 "tx_underrun",
2051};
2052
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002053static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002054{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002055 switch (sset) {
2056 case ETH_SS_STATS:
2057 return ARRAY_SIZE(rtl8169_gstrings);
2058 default:
2059 return -EOPNOTSUPP;
2060 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002061}
2062
Corinna Vinschen42020322015-09-10 10:47:35 +02002063DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002064{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002065 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002066}
2067
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002068static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002069{
Corinna Vinschen42020322015-09-10 10:47:35 +02002070 dma_addr_t paddr = tp->counters_phys_addr;
2071 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02002072
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002073 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
2074 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02002075 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002076 RTL_W32(tp, CounterAddrLow, cmd);
2077 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02002078
Francois Romieua78e9362018-01-26 01:53:26 +01002079 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002080}
2081
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002082static bool rtl8169_reset_counters(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002083{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002084 /*
2085 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2086 * tally counters.
2087 */
2088 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2089 return true;
2090
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002091 return rtl8169_do_counters(tp, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02002092}
2093
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002094static bool rtl8169_update_counters(struct rtl8169_private *tp)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002095{
Ivan Vecera355423d2009-02-06 21:49:57 -08002096 /*
2097 * Some chips are unable to dump tally counters when the receiver
2098 * is disabled.
2099 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002100 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002101 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002102
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002103 return rtl8169_do_counters(tp, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002104}
2105
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002106static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002107{
Corinna Vinschen42020322015-09-10 10:47:35 +02002108 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002109 bool ret = false;
2110
2111 /*
2112 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2113 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2114 * reset by a power cycle, while the counter values collected by the
2115 * driver are reset at every driver unload/load cycle.
2116 *
2117 * To make sure the HW values returned by @get_stats64 match the SW
2118 * values, we collect the initial values at first open(*) and use them
2119 * as offsets to normalize the values returned by @get_stats64.
2120 *
2121 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2122 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2123 * set at open time by rtl_hw_start.
2124 */
2125
2126 if (tp->tc_offset.inited)
2127 return true;
2128
2129 /* If both, reset and update fail, propagate to caller. */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002130 if (rtl8169_reset_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002131 ret = true;
2132
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002133 if (rtl8169_update_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002134 ret = true;
2135
Corinna Vinschen42020322015-09-10 10:47:35 +02002136 tp->tc_offset.tx_errors = counters->tx_errors;
2137 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2138 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002139 tp->tc_offset.inited = true;
2140
2141 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002142}
2143
Ivan Vecera355423d2009-02-06 21:49:57 -08002144static void rtl8169_get_ethtool_stats(struct net_device *dev,
2145 struct ethtool_stats *stats, u64 *data)
2146{
2147 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01002148 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02002149 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08002150
2151 ASSERT_RTNL();
2152
Chun-Hao Line0636232016-07-29 16:37:55 +08002153 pm_runtime_get_noresume(d);
2154
2155 if (pm_runtime_active(d))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002156 rtl8169_update_counters(tp);
Chun-Hao Line0636232016-07-29 16:37:55 +08002157
2158 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08002159
Corinna Vinschen42020322015-09-10 10:47:35 +02002160 data[0] = le64_to_cpu(counters->tx_packets);
2161 data[1] = le64_to_cpu(counters->rx_packets);
2162 data[2] = le64_to_cpu(counters->tx_errors);
2163 data[3] = le32_to_cpu(counters->rx_errors);
2164 data[4] = le16_to_cpu(counters->rx_missed);
2165 data[5] = le16_to_cpu(counters->align_errors);
2166 data[6] = le32_to_cpu(counters->tx_one_collision);
2167 data[7] = le32_to_cpu(counters->tx_multi_collision);
2168 data[8] = le64_to_cpu(counters->rx_unicast);
2169 data[9] = le64_to_cpu(counters->rx_broadcast);
2170 data[10] = le32_to_cpu(counters->rx_multicast);
2171 data[11] = le16_to_cpu(counters->tx_aborted);
2172 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08002173}
2174
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002175static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2176{
2177 switch(stringset) {
2178 case ETH_SS_STATS:
2179 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2180 break;
2181 }
2182}
2183
Florian Fainellif0903ea2016-12-03 12:01:19 -08002184static int rtl8169_nway_reset(struct net_device *dev)
2185{
2186 struct rtl8169_private *tp = netdev_priv(dev);
2187
2188 return mii_nway_restart(&tp->mii);
2189}
2190
Francois Romieu50970832017-10-27 13:24:49 +03002191/*
2192 * Interrupt coalescing
2193 *
2194 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2195 * > 8169, 8168 and 810x line of chipsets
2196 *
2197 * 8169, 8168, and 8136(810x) serial chipsets support it.
2198 *
2199 * > 2 - the Tx timer unit at gigabit speed
2200 *
2201 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2202 * (0xe0) bit 1 and bit 0.
2203 *
2204 * For 8169
2205 * bit[1:0] \ speed 1000M 100M 10M
2206 * 0 0 320ns 2.56us 40.96us
2207 * 0 1 2.56us 20.48us 327.7us
2208 * 1 0 5.12us 40.96us 655.4us
2209 * 1 1 10.24us 81.92us 1.31ms
2210 *
2211 * For the other
2212 * bit[1:0] \ speed 1000M 100M 10M
2213 * 0 0 5us 2.56us 40.96us
2214 * 0 1 40us 20.48us 327.7us
2215 * 1 0 80us 40.96us 655.4us
2216 * 1 1 160us 81.92us 1.31ms
2217 */
2218
2219/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2220struct rtl_coalesce_scale {
2221 /* Rx / Tx */
2222 u32 nsecs[2];
2223};
2224
2225/* rx/tx scale factors for all CPlusCmd[0:1] cases */
2226struct rtl_coalesce_info {
2227 u32 speed;
2228 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2229};
2230
2231/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2232#define rxtx_x1822(r, t) { \
2233 {{(r), (t)}}, \
2234 {{(r)*8, (t)*8}}, \
2235 {{(r)*8*2, (t)*8*2}}, \
2236 {{(r)*8*2*2, (t)*8*2*2}}, \
2237}
2238static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2239 /* speed delays: rx00 tx00 */
2240 { SPEED_10, rxtx_x1822(40960, 40960) },
2241 { SPEED_100, rxtx_x1822( 2560, 2560) },
2242 { SPEED_1000, rxtx_x1822( 320, 320) },
2243 { 0 },
2244};
2245
2246static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2247 /* speed delays: rx00 tx00 */
2248 { SPEED_10, rxtx_x1822(40960, 40960) },
2249 { SPEED_100, rxtx_x1822( 2560, 2560) },
2250 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2251 { 0 },
2252};
2253#undef rxtx_x1822
2254
2255/* get rx/tx scale vector corresponding to current speed */
2256static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2257{
2258 struct rtl8169_private *tp = netdev_priv(dev);
2259 struct ethtool_link_ksettings ecmd;
2260 const struct rtl_coalesce_info *ci;
2261 int rc;
2262
2263 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2264 if (rc < 0)
2265 return ERR_PTR(rc);
2266
2267 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2268 if (ecmd.base.speed == ci->speed) {
2269 return ci;
2270 }
2271 }
2272
2273 return ERR_PTR(-ELNRNG);
2274}
2275
2276static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2277{
2278 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002279 const struct rtl_coalesce_info *ci;
2280 const struct rtl_coalesce_scale *scale;
2281 struct {
2282 u32 *max_frames;
2283 u32 *usecs;
2284 } coal_settings [] = {
2285 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2286 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2287 }, *p = coal_settings;
2288 int i;
2289 u16 w;
2290
2291 memset(ec, 0, sizeof(*ec));
2292
2293 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2294 ci = rtl_coalesce_info(dev);
2295 if (IS_ERR(ci))
2296 return PTR_ERR(ci);
2297
Heiner Kallweit0ae09742018-04-28 22:19:26 +02002298 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
Francois Romieu50970832017-10-27 13:24:49 +03002299
2300 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002301 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03002302 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2303 w >>= RTL_COALESCE_SHIFT;
2304 *p->usecs = w & RTL_COALESCE_MASK;
2305 }
2306
2307 for (i = 0; i < 2; i++) {
2308 p = coal_settings + i;
2309 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2310
2311 /*
2312 * ethtool_coalesce says it is illegal to set both usecs and
2313 * max_frames to 0.
2314 */
2315 if (!*p->usecs && !*p->max_frames)
2316 *p->max_frames = 1;
2317 }
2318
2319 return 0;
2320}
2321
2322/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2323static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2324 struct net_device *dev, u32 nsec, u16 *cp01)
2325{
2326 const struct rtl_coalesce_info *ci;
2327 u16 i;
2328
2329 ci = rtl_coalesce_info(dev);
2330 if (IS_ERR(ci))
2331 return ERR_CAST(ci);
2332
2333 for (i = 0; i < 4; i++) {
2334 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2335 ci->scalev[i].nsecs[1]);
2336 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2337 *cp01 = i;
2338 return &ci->scalev[i];
2339 }
2340 }
2341
2342 return ERR_PTR(-EINVAL);
2343}
2344
2345static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2346{
2347 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002348 const struct rtl_coalesce_scale *scale;
2349 struct {
2350 u32 frames;
2351 u32 usecs;
2352 } coal_settings [] = {
2353 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2354 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2355 }, *p = coal_settings;
2356 u16 w = 0, cp01;
2357 int i;
2358
2359 scale = rtl_coalesce_choose_scale(dev,
2360 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2361 if (IS_ERR(scale))
2362 return PTR_ERR(scale);
2363
2364 for (i = 0; i < 2; i++, p++) {
2365 u32 units;
2366
2367 /*
2368 * accept max_frames=1 we returned in rtl_get_coalesce.
2369 * accept it not only when usecs=0 because of e.g. the following scenario:
2370 *
2371 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2372 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2373 * - then user does `ethtool -C eth0 rx-usecs 100`
2374 *
2375 * since ethtool sends to kernel whole ethtool_coalesce
2376 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2377 * we'll reject it below in `frames % 4 != 0`.
2378 */
2379 if (p->frames == 1) {
2380 p->frames = 0;
2381 }
2382
2383 units = p->usecs * 1000 / scale->nsecs[i];
2384 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2385 return -EINVAL;
2386
2387 w <<= RTL_COALESCE_SHIFT;
2388 w |= units;
2389 w <<= RTL_COALESCE_SHIFT;
2390 w |= p->frames >> 2;
2391 }
2392
2393 rtl_lock_work(tp);
2394
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002395 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03002396
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +02002397 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002398 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2399 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03002400
2401 rtl_unlock_work(tp);
2402
2403 return 0;
2404}
2405
Jeff Garzik7282d492006-09-13 14:30:00 -04002406static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 .get_drvinfo = rtl8169_get_drvinfo,
2408 .get_regs_len = rtl8169_get_regs_len,
2409 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002410 .get_coalesce = rtl_get_coalesce,
2411 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002412 .get_msglevel = rtl8169_get_msglevel,
2413 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002415 .get_wol = rtl8169_get_wol,
2416 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002417 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002418 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002419 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002420 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainellif0903ea2016-12-03 12:01:19 -08002421 .nway_reset = rtl8169_nway_reset,
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002422 .get_link_ksettings = rtl8169_get_link_ksettings,
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002423 .set_link_ksettings = rtl8169_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424};
2425
Francois Romieu07d3f512007-02-21 22:40:46 +01002426static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Heiner Kallweit22148df2018-04-22 17:15:15 +02002427 u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
Francois Romieu0e485152007-02-20 00:00:26 +01002429 /*
2430 * The driver currently handles the 8168Bf and the 8168Be identically
2431 * but they can be identified more specifically through the test below
2432 * if needed:
2433 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002434 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002435 *
2436 * Same thing for the 8101Eb and the 8101Ec:
2437 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002438 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002439 */
Francois Romieu37441002011-06-17 22:58:54 +02002440 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002442 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 int mac_version;
2444 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002445 /* 8168EP family. */
2446 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2447 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2448 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2449
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002450 /* 8168H family. */
2451 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2452 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2453
Hayes Wangc5583862012-07-02 17:23:22 +08002454 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002455 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002456 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002457 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2458 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2459
Hayes Wangc2218922011-09-06 16:55:18 +08002460 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002461 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002462 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2463 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2464
hayeswang01dc7fe2011-03-21 01:50:28 +00002465 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002466 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002467 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2468 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2469
Francois Romieu5b538df2008-07-20 16:22:45 +02002470 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002471 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002472 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002473
françois romieue6de30d2011-01-03 15:08:37 +00002474 /* 8168DP family. */
2475 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2476 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002477 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002478
Francois Romieuef808d52008-06-29 13:10:54 +02002479 /* 8168C family. */
Francois Romieuef3386f2008-06-29 12:24:30 +02002480 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002481 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002482 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002483 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2484 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002485 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieuef808d52008-06-29 13:10:54 +02002486 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002487
2488 /* 8168B family. */
2489 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002490 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2491 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2492
2493 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002494 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002495 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002496 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2497 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002498 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2499 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2500 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2501 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002502 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002503 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002504 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002505 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2506 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002507 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2508 /* FIXME: where did these entries come from ? -- FR */
2509 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2510 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2511
2512 /* 8110 family. */
2513 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2514 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2515 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2516 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2517 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2518 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2519
Jean Delvaref21b75e2009-05-26 20:54:48 -07002520 /* Catch-all */
2521 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002522 };
2523 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 u32 reg;
2525
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002526 reg = RTL_R32(tp, TxConfig);
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002527 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 p++;
2529 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002530
2531 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02002532 dev_notice(tp_to_dev(tp),
2533 "unknown MAC, using family default\n");
Francois Romieu5d320a22011-05-08 17:47:36 +02002534 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002535 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2536 tp->mac_version = tp->mii.supports_gmii ?
2537 RTL_GIGA_MAC_VER_42 :
2538 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002539 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2540 tp->mac_version = tp->mii.supports_gmii ?
2541 RTL_GIGA_MAC_VER_45 :
2542 RTL_GIGA_MAC_VER_47;
2543 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2544 tp->mac_version = tp->mii.supports_gmii ?
2545 RTL_GIGA_MAC_VER_46 :
2546 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548}
2549
2550static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2551{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002552 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553}
2554
Francois Romieu867763c2007-08-17 18:21:58 +02002555struct phy_reg {
2556 u16 reg;
2557 u16 val;
2558};
2559
françois romieu4da19632011-01-03 15:07:55 +00002560static void rtl_writephy_batch(struct rtl8169_private *tp,
2561 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002562{
2563 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002564 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002565 regs++;
2566 }
2567}
2568
françois romieubca03d52011-01-03 15:07:31 +00002569#define PHY_READ 0x00000000
2570#define PHY_DATA_OR 0x10000000
2571#define PHY_DATA_AND 0x20000000
2572#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002573#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002574#define PHY_CLEAR_READCOUNT 0x70000000
2575#define PHY_WRITE 0x80000000
2576#define PHY_READCOUNT_EQ_SKIP 0x90000000
2577#define PHY_COMP_EQ_SKIPN 0xa0000000
2578#define PHY_COMP_NEQ_SKIPN 0xb0000000
2579#define PHY_WRITE_PREVIOUS 0xc0000000
2580#define PHY_SKIPN 0xd0000000
2581#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002582
Hayes Wang960aee62011-06-18 11:37:48 +02002583struct fw_info {
2584 u32 magic;
2585 char version[RTL_VER_SIZE];
2586 __le32 fw_start;
2587 __le32 fw_len;
2588 u8 chksum;
2589} __packed;
2590
Francois Romieu1c361ef2011-06-17 17:16:24 +02002591#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2592
2593static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002594{
Francois Romieub6ffd972011-06-17 17:00:05 +02002595 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002596 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002597 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2598 char *version = rtl_fw->version;
2599 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002600
Francois Romieu1c361ef2011-06-17 17:16:24 +02002601 if (fw->size < FW_OPCODE_SIZE)
2602 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002603
2604 if (!fw_info->magic) {
2605 size_t i, size, start;
2606 u8 checksum = 0;
2607
2608 if (fw->size < sizeof(*fw_info))
2609 goto out;
2610
2611 for (i = 0; i < fw->size; i++)
2612 checksum += fw->data[i];
2613 if (checksum != 0)
2614 goto out;
2615
2616 start = le32_to_cpu(fw_info->fw_start);
2617 if (start > fw->size)
2618 goto out;
2619
2620 size = le32_to_cpu(fw_info->fw_len);
2621 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2622 goto out;
2623
2624 memcpy(version, fw_info->version, RTL_VER_SIZE);
2625
2626 pa->code = (__le32 *)(fw->data + start);
2627 pa->size = size;
2628 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002629 if (fw->size % FW_OPCODE_SIZE)
2630 goto out;
2631
2632 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2633
2634 pa->code = (__le32 *)fw->data;
2635 pa->size = fw->size / FW_OPCODE_SIZE;
2636 }
2637 version[RTL_VER_SIZE - 1] = 0;
2638
2639 rc = true;
2640out:
2641 return rc;
2642}
2643
Francois Romieufd112f22011-06-18 00:10:29 +02002644static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2645 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002646{
Francois Romieufd112f22011-06-18 00:10:29 +02002647 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002648 size_t index;
2649
Francois Romieu1c361ef2011-06-17 17:16:24 +02002650 for (index = 0; index < pa->size; index++) {
2651 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002652 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002653
hayeswang42b82dc2011-01-10 02:07:25 +00002654 switch(action & 0xf0000000) {
2655 case PHY_READ:
2656 case PHY_DATA_OR:
2657 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002658 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002659 case PHY_CLEAR_READCOUNT:
2660 case PHY_WRITE:
2661 case PHY_WRITE_PREVIOUS:
2662 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002663 break;
2664
hayeswang42b82dc2011-01-10 02:07:25 +00002665 case PHY_BJMPN:
2666 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002667 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002668 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002669 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002670 }
2671 break;
2672 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002673 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002674 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002675 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002676 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002677 }
2678 break;
2679 case PHY_COMP_EQ_SKIPN:
2680 case PHY_COMP_NEQ_SKIPN:
2681 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002682 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002683 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002684 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002685 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002686 }
2687 break;
2688
hayeswang42b82dc2011-01-10 02:07:25 +00002689 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002690 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002691 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002692 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002693 }
2694 }
Francois Romieufd112f22011-06-18 00:10:29 +02002695 rc = true;
2696out:
2697 return rc;
2698}
françois romieubca03d52011-01-03 15:07:31 +00002699
Francois Romieufd112f22011-06-18 00:10:29 +02002700static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2701{
2702 struct net_device *dev = tp->dev;
2703 int rc = -EINVAL;
2704
2705 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002706 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002707 goto out;
2708 }
2709
2710 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2711 rc = 0;
2712out:
2713 return rc;
2714}
2715
2716static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2717{
2718 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002719 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002720 u32 predata, count;
2721 size_t index;
2722
2723 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002724 org.write = ops->write;
2725 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002726
Francois Romieu1c361ef2011-06-17 17:16:24 +02002727 for (index = 0; index < pa->size; ) {
2728 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002729 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002730 u32 regno = (action & 0x0fff0000) >> 16;
2731
2732 if (!action)
2733 break;
françois romieubca03d52011-01-03 15:07:31 +00002734
2735 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002736 case PHY_READ:
2737 predata = rtl_readphy(tp, regno);
2738 count++;
2739 index++;
françois romieubca03d52011-01-03 15:07:31 +00002740 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002741 case PHY_DATA_OR:
2742 predata |= data;
2743 index++;
2744 break;
2745 case PHY_DATA_AND:
2746 predata &= data;
2747 index++;
2748 break;
2749 case PHY_BJMPN:
2750 index -= regno;
2751 break;
hayeswangeee37862013-04-01 22:23:38 +00002752 case PHY_MDIO_CHG:
2753 if (data == 0) {
2754 ops->write = org.write;
2755 ops->read = org.read;
2756 } else if (data == 1) {
2757 ops->write = mac_mcu_write;
2758 ops->read = mac_mcu_read;
2759 }
2760
hayeswang42b82dc2011-01-10 02:07:25 +00002761 index++;
2762 break;
2763 case PHY_CLEAR_READCOUNT:
2764 count = 0;
2765 index++;
2766 break;
2767 case PHY_WRITE:
2768 rtl_writephy(tp, regno, data);
2769 index++;
2770 break;
2771 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002772 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002773 break;
2774 case PHY_COMP_EQ_SKIPN:
2775 if (predata == data)
2776 index += regno;
2777 index++;
2778 break;
2779 case PHY_COMP_NEQ_SKIPN:
2780 if (predata != data)
2781 index += regno;
2782 index++;
2783 break;
2784 case PHY_WRITE_PREVIOUS:
2785 rtl_writephy(tp, regno, predata);
2786 index++;
2787 break;
2788 case PHY_SKIPN:
2789 index += regno + 1;
2790 break;
2791 case PHY_DELAY_MS:
2792 mdelay(data);
2793 index++;
2794 break;
2795
françois romieubca03d52011-01-03 15:07:31 +00002796 default:
2797 BUG();
2798 }
2799 }
hayeswangeee37862013-04-01 22:23:38 +00002800
2801 ops->write = org.write;
2802 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002803}
2804
françois romieuf1e02ed2011-01-13 13:07:53 +00002805static void rtl_release_firmware(struct rtl8169_private *tp)
2806{
Francois Romieub6ffd972011-06-17 17:00:05 +02002807 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2808 release_firmware(tp->rtl_fw->fw);
2809 kfree(tp->rtl_fw);
2810 }
2811 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002812}
2813
François Romieu953a12c2011-04-24 17:38:48 +02002814static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002815{
Francois Romieub6ffd972011-06-17 17:00:05 +02002816 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002817
2818 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002819 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002820 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002821}
2822
2823static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2824{
2825 if (rtl_readphy(tp, reg) != val)
2826 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2827 else
2828 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002829}
2830
françois romieu4da19632011-01-03 15:07:55 +00002831static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002833 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002834 { 0x1f, 0x0001 },
2835 { 0x06, 0x006e },
2836 { 0x08, 0x0708 },
2837 { 0x15, 0x4000 },
2838 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
françois romieu0b9b5712009-08-10 19:44:56 +00002840 { 0x1f, 0x0001 },
2841 { 0x03, 0x00a1 },
2842 { 0x02, 0x0008 },
2843 { 0x01, 0x0120 },
2844 { 0x00, 0x1000 },
2845 { 0x04, 0x0800 },
2846 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
françois romieu0b9b5712009-08-10 19:44:56 +00002848 { 0x03, 0xff41 },
2849 { 0x02, 0xdf60 },
2850 { 0x01, 0x0140 },
2851 { 0x00, 0x0077 },
2852 { 0x04, 0x7800 },
2853 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
françois romieu0b9b5712009-08-10 19:44:56 +00002855 { 0x03, 0x802f },
2856 { 0x02, 0x4f02 },
2857 { 0x01, 0x0409 },
2858 { 0x00, 0xf0f9 },
2859 { 0x04, 0x9800 },
2860 { 0x04, 0x9000 },
2861
2862 { 0x03, 0xdf01 },
2863 { 0x02, 0xdf20 },
2864 { 0x01, 0xff95 },
2865 { 0x00, 0xba00 },
2866 { 0x04, 0xa800 },
2867 { 0x04, 0xa000 },
2868
2869 { 0x03, 0xff41 },
2870 { 0x02, 0xdf20 },
2871 { 0x01, 0x0140 },
2872 { 0x00, 0x00bb },
2873 { 0x04, 0xb800 },
2874 { 0x04, 0xb000 },
2875
2876 { 0x03, 0xdf41 },
2877 { 0x02, 0xdc60 },
2878 { 0x01, 0x6340 },
2879 { 0x00, 0x007d },
2880 { 0x04, 0xd800 },
2881 { 0x04, 0xd000 },
2882
2883 { 0x03, 0xdf01 },
2884 { 0x02, 0xdf20 },
2885 { 0x01, 0x100a },
2886 { 0x00, 0xa0ff },
2887 { 0x04, 0xf800 },
2888 { 0x04, 0xf000 },
2889
2890 { 0x1f, 0x0000 },
2891 { 0x0b, 0x0000 },
2892 { 0x00, 0x9200 }
2893 };
2894
françois romieu4da19632011-01-03 15:07:55 +00002895 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896}
2897
françois romieu4da19632011-01-03 15:07:55 +00002898static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002899{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002900 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002901 { 0x1f, 0x0002 },
2902 { 0x01, 0x90d0 },
2903 { 0x1f, 0x0000 }
2904 };
2905
françois romieu4da19632011-01-03 15:07:55 +00002906 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002907}
2908
françois romieu4da19632011-01-03 15:07:55 +00002909static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002910{
2911 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002912
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002913 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2914 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002915 return;
2916
françois romieu4da19632011-01-03 15:07:55 +00002917 rtl_writephy(tp, 0x1f, 0x0001);
2918 rtl_writephy(tp, 0x10, 0xf01b);
2919 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002920}
2921
françois romieu4da19632011-01-03 15:07:55 +00002922static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002923{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002924 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002925 { 0x1f, 0x0001 },
2926 { 0x04, 0x0000 },
2927 { 0x03, 0x00a1 },
2928 { 0x02, 0x0008 },
2929 { 0x01, 0x0120 },
2930 { 0x00, 0x1000 },
2931 { 0x04, 0x0800 },
2932 { 0x04, 0x9000 },
2933 { 0x03, 0x802f },
2934 { 0x02, 0x4f02 },
2935 { 0x01, 0x0409 },
2936 { 0x00, 0xf099 },
2937 { 0x04, 0x9800 },
2938 { 0x04, 0xa000 },
2939 { 0x03, 0xdf01 },
2940 { 0x02, 0xdf20 },
2941 { 0x01, 0xff95 },
2942 { 0x00, 0xba00 },
2943 { 0x04, 0xa800 },
2944 { 0x04, 0xf000 },
2945 { 0x03, 0xdf01 },
2946 { 0x02, 0xdf20 },
2947 { 0x01, 0x101a },
2948 { 0x00, 0xa0ff },
2949 { 0x04, 0xf800 },
2950 { 0x04, 0x0000 },
2951 { 0x1f, 0x0000 },
2952
2953 { 0x1f, 0x0001 },
2954 { 0x10, 0xf41b },
2955 { 0x14, 0xfb54 },
2956 { 0x18, 0xf5c7 },
2957 { 0x1f, 0x0000 },
2958
2959 { 0x1f, 0x0001 },
2960 { 0x17, 0x0cc0 },
2961 { 0x1f, 0x0000 }
2962 };
2963
françois romieu4da19632011-01-03 15:07:55 +00002964 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002965
françois romieu4da19632011-01-03 15:07:55 +00002966 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002967}
2968
françois romieu4da19632011-01-03 15:07:55 +00002969static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002970{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002971 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002972 { 0x1f, 0x0001 },
2973 { 0x04, 0x0000 },
2974 { 0x03, 0x00a1 },
2975 { 0x02, 0x0008 },
2976 { 0x01, 0x0120 },
2977 { 0x00, 0x1000 },
2978 { 0x04, 0x0800 },
2979 { 0x04, 0x9000 },
2980 { 0x03, 0x802f },
2981 { 0x02, 0x4f02 },
2982 { 0x01, 0x0409 },
2983 { 0x00, 0xf099 },
2984 { 0x04, 0x9800 },
2985 { 0x04, 0xa000 },
2986 { 0x03, 0xdf01 },
2987 { 0x02, 0xdf20 },
2988 { 0x01, 0xff95 },
2989 { 0x00, 0xba00 },
2990 { 0x04, 0xa800 },
2991 { 0x04, 0xf000 },
2992 { 0x03, 0xdf01 },
2993 { 0x02, 0xdf20 },
2994 { 0x01, 0x101a },
2995 { 0x00, 0xa0ff },
2996 { 0x04, 0xf800 },
2997 { 0x04, 0x0000 },
2998 { 0x1f, 0x0000 },
2999
3000 { 0x1f, 0x0001 },
3001 { 0x0b, 0x8480 },
3002 { 0x1f, 0x0000 },
3003
3004 { 0x1f, 0x0001 },
3005 { 0x18, 0x67c7 },
3006 { 0x04, 0x2000 },
3007 { 0x03, 0x002f },
3008 { 0x02, 0x4360 },
3009 { 0x01, 0x0109 },
3010 { 0x00, 0x3022 },
3011 { 0x04, 0x2800 },
3012 { 0x1f, 0x0000 },
3013
3014 { 0x1f, 0x0001 },
3015 { 0x17, 0x0cc0 },
3016 { 0x1f, 0x0000 }
3017 };
3018
françois romieu4da19632011-01-03 15:07:55 +00003019 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00003020}
3021
françois romieu4da19632011-01-03 15:07:55 +00003022static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003023{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003024 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003025 { 0x10, 0xf41b },
3026 { 0x1f, 0x0000 }
3027 };
3028
françois romieu4da19632011-01-03 15:07:55 +00003029 rtl_writephy(tp, 0x1f, 0x0001);
3030 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02003031
françois romieu4da19632011-01-03 15:07:55 +00003032 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003033}
3034
françois romieu4da19632011-01-03 15:07:55 +00003035static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003036{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003037 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003038 { 0x1f, 0x0001 },
3039 { 0x10, 0xf41b },
3040 { 0x1f, 0x0000 }
3041 };
3042
françois romieu4da19632011-01-03 15:07:55 +00003043 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003044}
3045
françois romieu4da19632011-01-03 15:07:55 +00003046static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003047{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003048 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02003049 { 0x1f, 0x0000 },
3050 { 0x1d, 0x0f00 },
3051 { 0x1f, 0x0002 },
3052 { 0x0c, 0x1ec8 },
3053 { 0x1f, 0x0000 }
3054 };
3055
françois romieu4da19632011-01-03 15:07:55 +00003056 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02003057}
3058
françois romieu4da19632011-01-03 15:07:55 +00003059static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02003060{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003061 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02003062 { 0x1f, 0x0001 },
3063 { 0x1d, 0x3d98 },
3064 { 0x1f, 0x0000 }
3065 };
3066
françois romieu4da19632011-01-03 15:07:55 +00003067 rtl_writephy(tp, 0x1f, 0x0000);
3068 rtl_patchphy(tp, 0x14, 1 << 5);
3069 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02003070
françois romieu4da19632011-01-03 15:07:55 +00003071 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02003072}
3073
françois romieu4da19632011-01-03 15:07:55 +00003074static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003075{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003076 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02003077 { 0x1f, 0x0001 },
3078 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02003079 { 0x1f, 0x0002 },
3080 { 0x00, 0x88d4 },
3081 { 0x01, 0x82b1 },
3082 { 0x03, 0x7002 },
3083 { 0x08, 0x9e30 },
3084 { 0x09, 0x01f0 },
3085 { 0x0a, 0x5500 },
3086 { 0x0c, 0x00c8 },
3087 { 0x1f, 0x0003 },
3088 { 0x12, 0xc096 },
3089 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02003090 { 0x1f, 0x0000 },
3091 { 0x1f, 0x0000 },
3092 { 0x09, 0x2000 },
3093 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02003094 };
3095
françois romieu4da19632011-01-03 15:07:55 +00003096 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003097
françois romieu4da19632011-01-03 15:07:55 +00003098 rtl_patchphy(tp, 0x14, 1 << 5);
3099 rtl_patchphy(tp, 0x0d, 1 << 5);
3100 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02003101}
3102
françois romieu4da19632011-01-03 15:07:55 +00003103static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02003104{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003105 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02003106 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003107 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003108 { 0x03, 0x802f },
3109 { 0x02, 0x4f02 },
3110 { 0x01, 0x0409 },
3111 { 0x00, 0xf099 },
3112 { 0x04, 0x9800 },
3113 { 0x04, 0x9000 },
3114 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003115 { 0x1f, 0x0002 },
3116 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003117 { 0x06, 0x0761 },
3118 { 0x1f, 0x0003 },
3119 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003120 { 0x1f, 0x0000 }
3121 };
3122
françois romieu4da19632011-01-03 15:07:55 +00003123 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003124
françois romieu4da19632011-01-03 15:07:55 +00003125 rtl_patchphy(tp, 0x16, 1 << 0);
3126 rtl_patchphy(tp, 0x14, 1 << 5);
3127 rtl_patchphy(tp, 0x0d, 1 << 5);
3128 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003129}
3130
françois romieu4da19632011-01-03 15:07:55 +00003131static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02003132{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003133 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02003134 { 0x1f, 0x0001 },
3135 { 0x12, 0x2300 },
3136 { 0x1d, 0x3d98 },
3137 { 0x1f, 0x0002 },
3138 { 0x0c, 0x7eb8 },
3139 { 0x06, 0x5461 },
3140 { 0x1f, 0x0003 },
3141 { 0x16, 0x0f0a },
3142 { 0x1f, 0x0000 }
3143 };
3144
françois romieu4da19632011-01-03 15:07:55 +00003145 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02003146
françois romieu4da19632011-01-03 15:07:55 +00003147 rtl_patchphy(tp, 0x16, 1 << 0);
3148 rtl_patchphy(tp, 0x14, 1 << 5);
3149 rtl_patchphy(tp, 0x0d, 1 << 5);
3150 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003151}
3152
françois romieu4da19632011-01-03 15:07:55 +00003153static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003154{
françois romieu4da19632011-01-03 15:07:55 +00003155 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003156}
3157
françois romieubca03d52011-01-03 15:07:31 +00003158static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003159{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003160 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003161 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003162 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003163 { 0x06, 0x4064 },
3164 { 0x07, 0x2863 },
3165 { 0x08, 0x059c },
3166 { 0x09, 0x26b4 },
3167 { 0x0a, 0x6a19 },
3168 { 0x0b, 0xdcc8 },
3169 { 0x10, 0xf06d },
3170 { 0x14, 0x7f68 },
3171 { 0x18, 0x7fd9 },
3172 { 0x1c, 0xf0ff },
3173 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003174 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003175 { 0x12, 0xf49f },
3176 { 0x13, 0x070b },
3177 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003178 { 0x14, 0x94c0 },
3179
3180 /*
3181 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003182 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003183 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003184 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003185 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003186 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003187 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003188 { 0x06, 0x5561 },
3189
3190 /*
3191 * Can not link to 1Gbps with bad cable
3192 * Decrease SNR threshold form 21.07dB to 19.04dB
3193 */
3194 { 0x1f, 0x0001 },
3195 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003196
3197 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003198 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003199 };
3200
françois romieu4da19632011-01-03 15:07:55 +00003201 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003202
françois romieubca03d52011-01-03 15:07:31 +00003203 /*
3204 * Rx Error Issue
3205 * Fine Tune Switching regulator parameter
3206 */
françois romieu4da19632011-01-03 15:07:55 +00003207 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003208 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3209 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003210
Francois Romieufdf6fc02012-07-06 22:40:38 +02003211 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003212 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003213 { 0x1f, 0x0002 },
3214 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003215 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003216 { 0x05, 0x8330 },
3217 { 0x06, 0x669a },
3218 { 0x1f, 0x0002 }
3219 };
3220 int val;
3221
françois romieu4da19632011-01-03 15:07:55 +00003222 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003223
françois romieu4da19632011-01-03 15:07:55 +00003224 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003225
3226 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003227 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003228 0x0065, 0x0066, 0x0067, 0x0068,
3229 0x0069, 0x006a, 0x006b, 0x006c
3230 };
3231 int i;
3232
françois romieu4da19632011-01-03 15:07:55 +00003233 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003234
3235 val &= 0xff00;
3236 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003237 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003238 }
3239 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003240 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003241 { 0x1f, 0x0002 },
3242 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003243 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003244 { 0x05, 0x8330 },
3245 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003246 };
3247
françois romieu4da19632011-01-03 15:07:55 +00003248 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003249 }
3250
françois romieubca03d52011-01-03 15:07:31 +00003251 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003252 rtl_writephy(tp, 0x1f, 0x0002);
3253 rtl_patchphy(tp, 0x0d, 0x0300);
3254 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003255
françois romieubca03d52011-01-03 15:07:31 +00003256 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003257 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003258 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3259 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003260
françois romieu4da19632011-01-03 15:07:55 +00003261 rtl_writephy(tp, 0x1f, 0x0005);
3262 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003263
3264 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003265
françois romieu4da19632011-01-03 15:07:55 +00003266 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003267}
3268
françois romieubca03d52011-01-03 15:07:31 +00003269static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003270{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003271 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003272 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003273 { 0x1f, 0x0001 },
3274 { 0x06, 0x4064 },
3275 { 0x07, 0x2863 },
3276 { 0x08, 0x059c },
3277 { 0x09, 0x26b4 },
3278 { 0x0a, 0x6a19 },
3279 { 0x0b, 0xdcc8 },
3280 { 0x10, 0xf06d },
3281 { 0x14, 0x7f68 },
3282 { 0x18, 0x7fd9 },
3283 { 0x1c, 0xf0ff },
3284 { 0x1d, 0x3d9c },
3285 { 0x1f, 0x0003 },
3286 { 0x12, 0xf49f },
3287 { 0x13, 0x070b },
3288 { 0x1a, 0x05ad },
3289 { 0x14, 0x94c0 },
3290
françois romieubca03d52011-01-03 15:07:31 +00003291 /*
3292 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003293 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003294 */
françois romieudaf9df62009-10-07 12:44:20 +00003295 { 0x1f, 0x0002 },
3296 { 0x06, 0x5561 },
3297 { 0x1f, 0x0005 },
3298 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003299 { 0x06, 0x5561 },
3300
3301 /*
3302 * Can not link to 1Gbps with bad cable
3303 * Decrease SNR threshold form 21.07dB to 19.04dB
3304 */
3305 { 0x1f, 0x0001 },
3306 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003307
3308 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003309 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003310 };
3311
françois romieu4da19632011-01-03 15:07:55 +00003312 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003313
Francois Romieufdf6fc02012-07-06 22:40:38 +02003314 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003315 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003316 { 0x1f, 0x0002 },
3317 { 0x05, 0x669a },
3318 { 0x1f, 0x0005 },
3319 { 0x05, 0x8330 },
3320 { 0x06, 0x669a },
3321
3322 { 0x1f, 0x0002 }
3323 };
3324 int val;
3325
françois romieu4da19632011-01-03 15:07:55 +00003326 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003327
françois romieu4da19632011-01-03 15:07:55 +00003328 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003329 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003330 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003331 0x0065, 0x0066, 0x0067, 0x0068,
3332 0x0069, 0x006a, 0x006b, 0x006c
3333 };
3334 int i;
3335
françois romieu4da19632011-01-03 15:07:55 +00003336 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003337
3338 val &= 0xff00;
3339 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003340 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003341 }
3342 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003343 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003344 { 0x1f, 0x0002 },
3345 { 0x05, 0x2642 },
3346 { 0x1f, 0x0005 },
3347 { 0x05, 0x8330 },
3348 { 0x06, 0x2642 }
3349 };
3350
françois romieu4da19632011-01-03 15:07:55 +00003351 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003352 }
3353
françois romieubca03d52011-01-03 15:07:31 +00003354 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003355 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003356 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3357 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003358
françois romieubca03d52011-01-03 15:07:31 +00003359 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003360 rtl_writephy(tp, 0x1f, 0x0002);
3361 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003362
françois romieu4da19632011-01-03 15:07:55 +00003363 rtl_writephy(tp, 0x1f, 0x0005);
3364 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003365
3366 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003367
françois romieu4da19632011-01-03 15:07:55 +00003368 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003369}
3370
françois romieu4da19632011-01-03 15:07:55 +00003371static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003372{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003373 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003374 { 0x1f, 0x0002 },
3375 { 0x10, 0x0008 },
3376 { 0x0d, 0x006c },
3377
3378 { 0x1f, 0x0000 },
3379 { 0x0d, 0xf880 },
3380
3381 { 0x1f, 0x0001 },
3382 { 0x17, 0x0cc0 },
3383
3384 { 0x1f, 0x0001 },
3385 { 0x0b, 0xa4d8 },
3386 { 0x09, 0x281c },
3387 { 0x07, 0x2883 },
3388 { 0x0a, 0x6b35 },
3389 { 0x1d, 0x3da4 },
3390 { 0x1c, 0xeffd },
3391 { 0x14, 0x7f52 },
3392 { 0x18, 0x7fc6 },
3393 { 0x08, 0x0601 },
3394 { 0x06, 0x4063 },
3395 { 0x10, 0xf074 },
3396 { 0x1f, 0x0003 },
3397 { 0x13, 0x0789 },
3398 { 0x12, 0xf4bd },
3399 { 0x1a, 0x04fd },
3400 { 0x14, 0x84b0 },
3401 { 0x1f, 0x0000 },
3402 { 0x00, 0x9200 },
3403
3404 { 0x1f, 0x0005 },
3405 { 0x01, 0x0340 },
3406 { 0x1f, 0x0001 },
3407 { 0x04, 0x4000 },
3408 { 0x03, 0x1d21 },
3409 { 0x02, 0x0c32 },
3410 { 0x01, 0x0200 },
3411 { 0x00, 0x5554 },
3412 { 0x04, 0x4800 },
3413 { 0x04, 0x4000 },
3414 { 0x04, 0xf000 },
3415 { 0x03, 0xdf01 },
3416 { 0x02, 0xdf20 },
3417 { 0x01, 0x101a },
3418 { 0x00, 0xa0ff },
3419 { 0x04, 0xf800 },
3420 { 0x04, 0xf000 },
3421 { 0x1f, 0x0000 },
3422
3423 { 0x1f, 0x0007 },
3424 { 0x1e, 0x0023 },
3425 { 0x16, 0x0000 },
3426 { 0x1f, 0x0000 }
3427 };
3428
françois romieu4da19632011-01-03 15:07:55 +00003429 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003430}
3431
françois romieue6de30d2011-01-03 15:08:37 +00003432static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3433{
3434 static const struct phy_reg phy_reg_init[] = {
3435 { 0x1f, 0x0001 },
3436 { 0x17, 0x0cc0 },
3437
3438 { 0x1f, 0x0007 },
3439 { 0x1e, 0x002d },
3440 { 0x18, 0x0040 },
3441 { 0x1f, 0x0000 }
3442 };
3443
3444 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3445 rtl_patchphy(tp, 0x0d, 1 << 5);
3446}
3447
Hayes Wang70090422011-07-06 15:58:06 +08003448static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003449{
3450 static const struct phy_reg phy_reg_init[] = {
3451 /* Enable Delay cap */
3452 { 0x1f, 0x0005 },
3453 { 0x05, 0x8b80 },
3454 { 0x06, 0xc896 },
3455 { 0x1f, 0x0000 },
3456
3457 /* Channel estimation fine tune */
3458 { 0x1f, 0x0001 },
3459 { 0x0b, 0x6c20 },
3460 { 0x07, 0x2872 },
3461 { 0x1c, 0xefff },
3462 { 0x1f, 0x0003 },
3463 { 0x14, 0x6420 },
3464 { 0x1f, 0x0000 },
3465
3466 /* Update PFM & 10M TX idle timer */
3467 { 0x1f, 0x0007 },
3468 { 0x1e, 0x002f },
3469 { 0x15, 0x1919 },
3470 { 0x1f, 0x0000 },
3471
3472 { 0x1f, 0x0007 },
3473 { 0x1e, 0x00ac },
3474 { 0x18, 0x0006 },
3475 { 0x1f, 0x0000 }
3476 };
3477
Francois Romieu15ecd032011-04-27 13:52:22 -07003478 rtl_apply_firmware(tp);
3479
hayeswang01dc7fe2011-03-21 01:50:28 +00003480 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3481
3482 /* DCO enable for 10M IDLE Power */
3483 rtl_writephy(tp, 0x1f, 0x0007);
3484 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003485 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003486 rtl_writephy(tp, 0x1f, 0x0000);
3487
3488 /* For impedance matching */
3489 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003490 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003491 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003492
3493 /* PHY auto speed down */
3494 rtl_writephy(tp, 0x1f, 0x0007);
3495 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003496 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003497 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003498 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003499
3500 rtl_writephy(tp, 0x1f, 0x0005);
3501 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003502 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003503 rtl_writephy(tp, 0x1f, 0x0000);
3504
3505 rtl_writephy(tp, 0x1f, 0x0005);
3506 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003507 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003508 rtl_writephy(tp, 0x1f, 0x0007);
3509 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003510 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003511 rtl_writephy(tp, 0x1f, 0x0006);
3512 rtl_writephy(tp, 0x00, 0x5a00);
3513 rtl_writephy(tp, 0x1f, 0x0000);
3514 rtl_writephy(tp, 0x0d, 0x0007);
3515 rtl_writephy(tp, 0x0e, 0x003c);
3516 rtl_writephy(tp, 0x0d, 0x4007);
3517 rtl_writephy(tp, 0x0e, 0x0000);
3518 rtl_writephy(tp, 0x0d, 0x0000);
3519}
3520
françois romieu9ecb9aa2012-12-07 11:20:21 +00003521static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3522{
3523 const u16 w[] = {
3524 addr[0] | (addr[1] << 8),
3525 addr[2] | (addr[3] << 8),
3526 addr[4] | (addr[5] << 8)
3527 };
3528 const struct exgmac_reg e[] = {
3529 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3530 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3531 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3532 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3533 };
3534
3535 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3536}
3537
Hayes Wang70090422011-07-06 15:58:06 +08003538static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3539{
3540 static const struct phy_reg phy_reg_init[] = {
3541 /* Enable Delay cap */
3542 { 0x1f, 0x0004 },
3543 { 0x1f, 0x0007 },
3544 { 0x1e, 0x00ac },
3545 { 0x18, 0x0006 },
3546 { 0x1f, 0x0002 },
3547 { 0x1f, 0x0000 },
3548 { 0x1f, 0x0000 },
3549
3550 /* Channel estimation fine tune */
3551 { 0x1f, 0x0003 },
3552 { 0x09, 0xa20f },
3553 { 0x1f, 0x0000 },
3554 { 0x1f, 0x0000 },
3555
3556 /* Green Setting */
3557 { 0x1f, 0x0005 },
3558 { 0x05, 0x8b5b },
3559 { 0x06, 0x9222 },
3560 { 0x05, 0x8b6d },
3561 { 0x06, 0x8000 },
3562 { 0x05, 0x8b76 },
3563 { 0x06, 0x8000 },
3564 { 0x1f, 0x0000 }
3565 };
3566
3567 rtl_apply_firmware(tp);
3568
3569 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3570
3571 /* For 4-corner performance improve */
3572 rtl_writephy(tp, 0x1f, 0x0005);
3573 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003574 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003575 rtl_writephy(tp, 0x1f, 0x0000);
3576
3577 /* PHY auto speed down */
3578 rtl_writephy(tp, 0x1f, 0x0004);
3579 rtl_writephy(tp, 0x1f, 0x0007);
3580 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003581 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003582 rtl_writephy(tp, 0x1f, 0x0002);
3583 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003584 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003585
3586 /* improve 10M EEE waveform */
3587 rtl_writephy(tp, 0x1f, 0x0005);
3588 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003589 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003590 rtl_writephy(tp, 0x1f, 0x0000);
3591
3592 /* Improve 2-pair detection performance */
3593 rtl_writephy(tp, 0x1f, 0x0005);
3594 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003595 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003596 rtl_writephy(tp, 0x1f, 0x0000);
3597
3598 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003599 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003600 rtl_writephy(tp, 0x1f, 0x0005);
3601 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003602 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003603 rtl_writephy(tp, 0x1f, 0x0004);
3604 rtl_writephy(tp, 0x1f, 0x0007);
3605 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003606 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003607 rtl_writephy(tp, 0x1f, 0x0002);
3608 rtl_writephy(tp, 0x1f, 0x0000);
3609 rtl_writephy(tp, 0x0d, 0x0007);
3610 rtl_writephy(tp, 0x0e, 0x003c);
3611 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003612 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003613 rtl_writephy(tp, 0x0d, 0x0000);
3614
3615 /* Green feature */
3616 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003617 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3618 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003619 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003620 rtl_writephy(tp, 0x1f, 0x0005);
3621 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3622 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003623
françois romieu9ecb9aa2012-12-07 11:20:21 +00003624 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3625 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003626}
3627
Hayes Wang5f886e02012-03-30 14:33:03 +08003628static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3629{
3630 /* For 4-corner performance improve */
3631 rtl_writephy(tp, 0x1f, 0x0005);
3632 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003633 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003634 rtl_writephy(tp, 0x1f, 0x0000);
3635
3636 /* PHY auto speed down */
3637 rtl_writephy(tp, 0x1f, 0x0007);
3638 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003639 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003640 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003641 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003642
3643 /* Improve 10M EEE waveform */
3644 rtl_writephy(tp, 0x1f, 0x0005);
3645 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003646 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003647 rtl_writephy(tp, 0x1f, 0x0000);
3648}
3649
Hayes Wangc2218922011-09-06 16:55:18 +08003650static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3651{
3652 static const struct phy_reg phy_reg_init[] = {
3653 /* Channel estimation fine tune */
3654 { 0x1f, 0x0003 },
3655 { 0x09, 0xa20f },
3656 { 0x1f, 0x0000 },
3657
3658 /* Modify green table for giga & fnet */
3659 { 0x1f, 0x0005 },
3660 { 0x05, 0x8b55 },
3661 { 0x06, 0x0000 },
3662 { 0x05, 0x8b5e },
3663 { 0x06, 0x0000 },
3664 { 0x05, 0x8b67 },
3665 { 0x06, 0x0000 },
3666 { 0x05, 0x8b70 },
3667 { 0x06, 0x0000 },
3668 { 0x1f, 0x0000 },
3669 { 0x1f, 0x0007 },
3670 { 0x1e, 0x0078 },
3671 { 0x17, 0x0000 },
3672 { 0x19, 0x00fb },
3673 { 0x1f, 0x0000 },
3674
3675 /* Modify green table for 10M */
3676 { 0x1f, 0x0005 },
3677 { 0x05, 0x8b79 },
3678 { 0x06, 0xaa00 },
3679 { 0x1f, 0x0000 },
3680
3681 /* Disable hiimpedance detection (RTCT) */
3682 { 0x1f, 0x0003 },
3683 { 0x01, 0x328a },
3684 { 0x1f, 0x0000 }
3685 };
3686
3687 rtl_apply_firmware(tp);
3688
3689 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3690
Hayes Wang5f886e02012-03-30 14:33:03 +08003691 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003692
3693 /* Improve 2-pair detection performance */
3694 rtl_writephy(tp, 0x1f, 0x0005);
3695 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003696 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003697 rtl_writephy(tp, 0x1f, 0x0000);
3698}
3699
3700static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3701{
3702 rtl_apply_firmware(tp);
3703
Hayes Wang5f886e02012-03-30 14:33:03 +08003704 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003705}
3706
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003707static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3708{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003709 static const struct phy_reg phy_reg_init[] = {
3710 /* Channel estimation fine tune */
3711 { 0x1f, 0x0003 },
3712 { 0x09, 0xa20f },
3713 { 0x1f, 0x0000 },
3714
3715 /* Modify green table for giga & fnet */
3716 { 0x1f, 0x0005 },
3717 { 0x05, 0x8b55 },
3718 { 0x06, 0x0000 },
3719 { 0x05, 0x8b5e },
3720 { 0x06, 0x0000 },
3721 { 0x05, 0x8b67 },
3722 { 0x06, 0x0000 },
3723 { 0x05, 0x8b70 },
3724 { 0x06, 0x0000 },
3725 { 0x1f, 0x0000 },
3726 { 0x1f, 0x0007 },
3727 { 0x1e, 0x0078 },
3728 { 0x17, 0x0000 },
3729 { 0x19, 0x00aa },
3730 { 0x1f, 0x0000 },
3731
3732 /* Modify green table for 10M */
3733 { 0x1f, 0x0005 },
3734 { 0x05, 0x8b79 },
3735 { 0x06, 0xaa00 },
3736 { 0x1f, 0x0000 },
3737
3738 /* Disable hiimpedance detection (RTCT) */
3739 { 0x1f, 0x0003 },
3740 { 0x01, 0x328a },
3741 { 0x1f, 0x0000 }
3742 };
3743
3744
3745 rtl_apply_firmware(tp);
3746
3747 rtl8168f_hw_phy_config(tp);
3748
3749 /* Improve 2-pair detection performance */
3750 rtl_writephy(tp, 0x1f, 0x0005);
3751 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003752 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003753 rtl_writephy(tp, 0x1f, 0x0000);
3754
3755 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3756
3757 /* Modify green table for giga */
3758 rtl_writephy(tp, 0x1f, 0x0005);
3759 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003760 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003761 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003762 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003763 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003764 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003765 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003766 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003767 rtl_writephy(tp, 0x05, 0x8a82);
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, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003770 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003771 rtl_writephy(tp, 0x05, 0x8a88);
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, 0x1f, 0x0000);
3774
3775 /* uc same-seed solution */
3776 rtl_writephy(tp, 0x1f, 0x0005);
3777 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003778 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003779 rtl_writephy(tp, 0x1f, 0x0000);
3780
3781 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003782 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003783 rtl_writephy(tp, 0x1f, 0x0005);
3784 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003785 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003786 rtl_writephy(tp, 0x1f, 0x0004);
3787 rtl_writephy(tp, 0x1f, 0x0007);
3788 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003789 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003790 rtl_writephy(tp, 0x1f, 0x0000);
3791 rtl_writephy(tp, 0x0d, 0x0007);
3792 rtl_writephy(tp, 0x0e, 0x003c);
3793 rtl_writephy(tp, 0x0d, 0x4007);
3794 rtl_writephy(tp, 0x0e, 0x0000);
3795 rtl_writephy(tp, 0x0d, 0x0000);
3796
3797 /* Green feature */
3798 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003799 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3800 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003801 rtl_writephy(tp, 0x1f, 0x0000);
3802}
3803
Hayes Wangc5583862012-07-02 17:23:22 +08003804static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3805{
Hayes Wangc5583862012-07-02 17:23:22 +08003806 rtl_apply_firmware(tp);
3807
hayeswang41f44d12013-04-01 22:23:36 +00003808 rtl_writephy(tp, 0x1f, 0x0a46);
3809 if (rtl_readphy(tp, 0x10) & 0x0100) {
3810 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003811 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003812 } else {
3813 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003814 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003815 }
Hayes Wangc5583862012-07-02 17:23:22 +08003816
hayeswang41f44d12013-04-01 22:23:36 +00003817 rtl_writephy(tp, 0x1f, 0x0a46);
3818 if (rtl_readphy(tp, 0x13) & 0x0100) {
3819 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003820 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003821 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003822 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003823 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003824 }
Hayes Wangc5583862012-07-02 17:23:22 +08003825
hayeswang41f44d12013-04-01 22:23:36 +00003826 /* Enable PHY auto speed down */
3827 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003828 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003829
hayeswangfe7524c2013-04-01 22:23:37 +00003830 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003831 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003832 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003833 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003834 rtl_writephy(tp, 0x1f, 0x0a43);
3835 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003836 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3837 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003838
hayeswang41f44d12013-04-01 22:23:36 +00003839 /* EEE auto-fallback function */
3840 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003841 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003842
hayeswang41f44d12013-04-01 22:23:36 +00003843 /* Enable UC LPF tune function */
3844 rtl_writephy(tp, 0x1f, 0x0a43);
3845 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003846 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003847
3848 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003849 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003850
hayeswangfe7524c2013-04-01 22:23:37 +00003851 /* Improve SWR Efficiency */
3852 rtl_writephy(tp, 0x1f, 0x0bcd);
3853 rtl_writephy(tp, 0x14, 0x5065);
3854 rtl_writephy(tp, 0x14, 0xd065);
3855 rtl_writephy(tp, 0x1f, 0x0bc8);
3856 rtl_writephy(tp, 0x11, 0x5655);
3857 rtl_writephy(tp, 0x1f, 0x0bcd);
3858 rtl_writephy(tp, 0x14, 0x1065);
3859 rtl_writephy(tp, 0x14, 0x9065);
3860 rtl_writephy(tp, 0x14, 0x1065);
3861
David Chang1bac1072013-11-27 15:48:36 +08003862 /* Check ALDPS bit, disable it if enabled */
3863 rtl_writephy(tp, 0x1f, 0x0a43);
3864 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003865 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08003866
hayeswang41f44d12013-04-01 22:23:36 +00003867 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003868}
3869
hayeswang57538c42013-04-01 22:23:40 +00003870static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3871{
3872 rtl_apply_firmware(tp);
3873}
3874
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003875static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3876{
3877 u16 dout_tapbin;
3878 u32 data;
3879
3880 rtl_apply_firmware(tp);
3881
3882 /* CHN EST parameters adjust - giga master */
3883 rtl_writephy(tp, 0x1f, 0x0a43);
3884 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003885 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003886 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003887 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003888 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003889 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003890 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003891 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003892 rtl_writephy(tp, 0x1f, 0x0000);
3893
3894 /* CHN EST parameters adjust - giga slave */
3895 rtl_writephy(tp, 0x1f, 0x0a43);
3896 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003897 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003898 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003899 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003900 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003901 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003902 rtl_writephy(tp, 0x1f, 0x0000);
3903
3904 /* CHN EST parameters adjust - fnet */
3905 rtl_writephy(tp, 0x1f, 0x0a43);
3906 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003907 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003908 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003909 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003910 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003911 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003912 rtl_writephy(tp, 0x1f, 0x0000);
3913
3914 /* enable R-tune & PGA-retune function */
3915 dout_tapbin = 0;
3916 rtl_writephy(tp, 0x1f, 0x0a46);
3917 data = rtl_readphy(tp, 0x13);
3918 data &= 3;
3919 data <<= 2;
3920 dout_tapbin |= data;
3921 data = rtl_readphy(tp, 0x12);
3922 data &= 0xc000;
3923 data >>= 14;
3924 dout_tapbin |= data;
3925 dout_tapbin = ~(dout_tapbin^0x08);
3926 dout_tapbin <<= 12;
3927 dout_tapbin &= 0xf000;
3928 rtl_writephy(tp, 0x1f, 0x0a43);
3929 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003930 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003931 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003932 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003933 rtl_writephy(tp, 0x13, 0x827c);
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, 0x827d);
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
3938 rtl_writephy(tp, 0x1f, 0x0a43);
3939 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003940 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003941 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003942 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003943 rtl_writephy(tp, 0x1f, 0x0000);
3944
3945 /* enable GPHY 10M */
3946 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003947 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003948 rtl_writephy(tp, 0x1f, 0x0000);
3949
3950 /* SAR ADC performance */
3951 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003952 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003953 rtl_writephy(tp, 0x1f, 0x0000);
3954
3955 rtl_writephy(tp, 0x1f, 0x0a43);
3956 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003957 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003958 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003959 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003960 rtl_writephy(tp, 0x13, 0x804f);
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, 0x8057);
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, 0x805f);
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, 0x8067);
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, 0x806f);
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, 0x1f, 0x0000);
3971
3972 /* disable phy pfm mode */
3973 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003974 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003975 rtl_writephy(tp, 0x1f, 0x0000);
3976
3977 /* Check ALDPS bit, disable it if enabled */
3978 rtl_writephy(tp, 0x1f, 0x0a43);
3979 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003980 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003981
3982 rtl_writephy(tp, 0x1f, 0x0000);
3983}
3984
3985static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3986{
3987 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3988 u16 rlen;
3989 u32 data;
3990
3991 rtl_apply_firmware(tp);
3992
3993 /* CHIN EST parameter update */
3994 rtl_writephy(tp, 0x1f, 0x0a43);
3995 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003996 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003997 rtl_writephy(tp, 0x1f, 0x0000);
3998
3999 /* enable R-tune & PGA-retune function */
4000 rtl_writephy(tp, 0x1f, 0x0a43);
4001 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004002 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004003 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004004 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004005 rtl_writephy(tp, 0x1f, 0x0000);
4006
4007 /* enable GPHY 10M */
4008 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004009 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004010 rtl_writephy(tp, 0x1f, 0x0000);
4011
4012 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
4013 data = r8168_mac_ocp_read(tp, 0xdd02);
4014 ioffset_p3 = ((data & 0x80)>>7);
4015 ioffset_p3 <<= 3;
4016
4017 data = r8168_mac_ocp_read(tp, 0xdd00);
4018 ioffset_p3 |= ((data & (0xe000))>>13);
4019 ioffset_p2 = ((data & (0x1e00))>>9);
4020 ioffset_p1 = ((data & (0x01e0))>>5);
4021 ioffset_p0 = ((data & 0x0010)>>4);
4022 ioffset_p0 <<= 3;
4023 ioffset_p0 |= (data & (0x07));
4024 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
4025
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004026 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08004027 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004028 rtl_writephy(tp, 0x1f, 0x0bcf);
4029 rtl_writephy(tp, 0x16, data);
4030 rtl_writephy(tp, 0x1f, 0x0000);
4031 }
4032
4033 /* Modify rlen (TX LPF corner frequency) level */
4034 rtl_writephy(tp, 0x1f, 0x0bcd);
4035 data = rtl_readphy(tp, 0x16);
4036 data &= 0x000f;
4037 rlen = 0;
4038 if (data > 3)
4039 rlen = data - 3;
4040 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
4041 rtl_writephy(tp, 0x17, data);
4042 rtl_writephy(tp, 0x1f, 0x0bcd);
4043 rtl_writephy(tp, 0x1f, 0x0000);
4044
4045 /* disable phy pfm mode */
4046 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004047 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004048 rtl_writephy(tp, 0x1f, 0x0000);
4049
4050 /* Check ALDPS bit, disable it if enabled */
4051 rtl_writephy(tp, 0x1f, 0x0a43);
4052 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004053 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004054
4055 rtl_writephy(tp, 0x1f, 0x0000);
4056}
4057
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004058static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4059{
4060 /* Enable PHY auto speed down */
4061 rtl_writephy(tp, 0x1f, 0x0a44);
4062 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4063 rtl_writephy(tp, 0x1f, 0x0000);
4064
4065 /* patch 10M & ALDPS */
4066 rtl_writephy(tp, 0x1f, 0x0bcc);
4067 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4068 rtl_writephy(tp, 0x1f, 0x0a44);
4069 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4070 rtl_writephy(tp, 0x1f, 0x0a43);
4071 rtl_writephy(tp, 0x13, 0x8084);
4072 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4073 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4074 rtl_writephy(tp, 0x1f, 0x0000);
4075
4076 /* Enable EEE auto-fallback function */
4077 rtl_writephy(tp, 0x1f, 0x0a4b);
4078 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4079 rtl_writephy(tp, 0x1f, 0x0000);
4080
4081 /* Enable UC LPF tune function */
4082 rtl_writephy(tp, 0x1f, 0x0a43);
4083 rtl_writephy(tp, 0x13, 0x8012);
4084 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4085 rtl_writephy(tp, 0x1f, 0x0000);
4086
4087 /* set rg_sel_sdm_rate */
4088 rtl_writephy(tp, 0x1f, 0x0c42);
4089 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4090 rtl_writephy(tp, 0x1f, 0x0000);
4091
4092 /* Check ALDPS bit, disable it if enabled */
4093 rtl_writephy(tp, 0x1f, 0x0a43);
4094 if (rtl_readphy(tp, 0x10) & 0x0004)
4095 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4096
4097 rtl_writephy(tp, 0x1f, 0x0000);
4098}
4099
4100static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4101{
4102 /* patch 10M & ALDPS */
4103 rtl_writephy(tp, 0x1f, 0x0bcc);
4104 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4105 rtl_writephy(tp, 0x1f, 0x0a44);
4106 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4107 rtl_writephy(tp, 0x1f, 0x0a43);
4108 rtl_writephy(tp, 0x13, 0x8084);
4109 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4110 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4111 rtl_writephy(tp, 0x1f, 0x0000);
4112
4113 /* Enable UC LPF tune function */
4114 rtl_writephy(tp, 0x1f, 0x0a43);
4115 rtl_writephy(tp, 0x13, 0x8012);
4116 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4117 rtl_writephy(tp, 0x1f, 0x0000);
4118
4119 /* Set rg_sel_sdm_rate */
4120 rtl_writephy(tp, 0x1f, 0x0c42);
4121 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4122 rtl_writephy(tp, 0x1f, 0x0000);
4123
4124 /* Channel estimation parameters */
4125 rtl_writephy(tp, 0x1f, 0x0a43);
4126 rtl_writephy(tp, 0x13, 0x80f3);
4127 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4128 rtl_writephy(tp, 0x13, 0x80f0);
4129 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4130 rtl_writephy(tp, 0x13, 0x80ef);
4131 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4132 rtl_writephy(tp, 0x13, 0x80f6);
4133 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4134 rtl_writephy(tp, 0x13, 0x80ec);
4135 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4136 rtl_writephy(tp, 0x13, 0x80ed);
4137 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4138 rtl_writephy(tp, 0x13, 0x80f2);
4139 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4140 rtl_writephy(tp, 0x13, 0x80f4);
4141 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4142 rtl_writephy(tp, 0x1f, 0x0a43);
4143 rtl_writephy(tp, 0x13, 0x8110);
4144 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4145 rtl_writephy(tp, 0x13, 0x810f);
4146 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4147 rtl_writephy(tp, 0x13, 0x8111);
4148 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4149 rtl_writephy(tp, 0x13, 0x8113);
4150 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4151 rtl_writephy(tp, 0x13, 0x8115);
4152 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4153 rtl_writephy(tp, 0x13, 0x810e);
4154 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4155 rtl_writephy(tp, 0x13, 0x810c);
4156 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4157 rtl_writephy(tp, 0x13, 0x810b);
4158 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4159 rtl_writephy(tp, 0x1f, 0x0a43);
4160 rtl_writephy(tp, 0x13, 0x80d1);
4161 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4162 rtl_writephy(tp, 0x13, 0x80cd);
4163 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4164 rtl_writephy(tp, 0x13, 0x80d3);
4165 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4166 rtl_writephy(tp, 0x13, 0x80d5);
4167 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4168 rtl_writephy(tp, 0x13, 0x80d7);
4169 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4170
4171 /* Force PWM-mode */
4172 rtl_writephy(tp, 0x1f, 0x0bcd);
4173 rtl_writephy(tp, 0x14, 0x5065);
4174 rtl_writephy(tp, 0x14, 0xd065);
4175 rtl_writephy(tp, 0x1f, 0x0bc8);
4176 rtl_writephy(tp, 0x12, 0x00ed);
4177 rtl_writephy(tp, 0x1f, 0x0bcd);
4178 rtl_writephy(tp, 0x14, 0x1065);
4179 rtl_writephy(tp, 0x14, 0x9065);
4180 rtl_writephy(tp, 0x14, 0x1065);
4181 rtl_writephy(tp, 0x1f, 0x0000);
4182
4183 /* Check ALDPS bit, disable it if enabled */
4184 rtl_writephy(tp, 0x1f, 0x0a43);
4185 if (rtl_readphy(tp, 0x10) & 0x0004)
4186 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4187
4188 rtl_writephy(tp, 0x1f, 0x0000);
4189}
4190
françois romieu4da19632011-01-03 15:07:55 +00004191static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004192{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004193 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004194 { 0x1f, 0x0003 },
4195 { 0x08, 0x441d },
4196 { 0x01, 0x9100 },
4197 { 0x1f, 0x0000 }
4198 };
4199
françois romieu4da19632011-01-03 15:07:55 +00004200 rtl_writephy(tp, 0x1f, 0x0000);
4201 rtl_patchphy(tp, 0x11, 1 << 12);
4202 rtl_patchphy(tp, 0x19, 1 << 13);
4203 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004204
françois romieu4da19632011-01-03 15:07:55 +00004205 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004206}
4207
Hayes Wang5a5e4442011-02-22 17:26:21 +08004208static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4209{
4210 static const struct phy_reg phy_reg_init[] = {
4211 { 0x1f, 0x0005 },
4212 { 0x1a, 0x0000 },
4213 { 0x1f, 0x0000 },
4214
4215 { 0x1f, 0x0004 },
4216 { 0x1c, 0x0000 },
4217 { 0x1f, 0x0000 },
4218
4219 { 0x1f, 0x0001 },
4220 { 0x15, 0x7701 },
4221 { 0x1f, 0x0000 }
4222 };
4223
4224 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004225 rtl_writephy(tp, 0x1f, 0x0000);
4226 rtl_writephy(tp, 0x18, 0x0310);
4227 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004228
François Romieu953a12c2011-04-24 17:38:48 +02004229 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004230
4231 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4232}
4233
Hayes Wang7e18dca2012-03-30 14:33:02 +08004234static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4235{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004236 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004237 rtl_writephy(tp, 0x1f, 0x0000);
4238 rtl_writephy(tp, 0x18, 0x0310);
4239 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004240
4241 rtl_apply_firmware(tp);
4242
4243 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004244 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004245 rtl_writephy(tp, 0x1f, 0x0004);
4246 rtl_writephy(tp, 0x10, 0x401f);
4247 rtl_writephy(tp, 0x19, 0x7030);
4248 rtl_writephy(tp, 0x1f, 0x0000);
4249}
4250
Hayes Wang5598bfe2012-07-02 17:23:21 +08004251static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4252{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004253 static const struct phy_reg phy_reg_init[] = {
4254 { 0x1f, 0x0004 },
4255 { 0x10, 0xc07f },
4256 { 0x19, 0x7030 },
4257 { 0x1f, 0x0000 }
4258 };
4259
4260 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004261 rtl_writephy(tp, 0x1f, 0x0000);
4262 rtl_writephy(tp, 0x18, 0x0310);
4263 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004264
4265 rtl_apply_firmware(tp);
4266
Francois Romieufdf6fc02012-07-06 22:40:38 +02004267 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004268 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4269
Francois Romieufdf6fc02012-07-06 22:40:38 +02004270 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004271}
4272
Francois Romieu5615d9f2007-08-17 17:50:46 +02004273static void rtl_hw_phy_config(struct net_device *dev)
4274{
4275 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004276
4277 rtl8169_print_mac_version(tp);
4278
4279 switch (tp->mac_version) {
4280 case RTL_GIGA_MAC_VER_01:
4281 break;
4282 case RTL_GIGA_MAC_VER_02:
4283 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004284 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004285 break;
4286 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004287 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004288 break;
françois romieu2e9558562009-08-10 19:44:19 +00004289 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004290 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004291 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004292 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004293 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004294 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004295 case RTL_GIGA_MAC_VER_07:
4296 case RTL_GIGA_MAC_VER_08:
4297 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004298 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004299 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004300 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004301 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004302 break;
4303 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004304 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004305 break;
4306 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004307 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004308 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004309 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004310 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004311 break;
4312 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004313 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004314 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004315 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004316 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004317 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004318 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004319 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004320 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004321 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004322 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004323 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004324 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004325 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004326 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004327 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004328 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004329 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004330 break;
4331 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004332 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004333 break;
4334 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004335 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004336 break;
françois romieue6de30d2011-01-03 15:08:37 +00004337 case RTL_GIGA_MAC_VER_28:
4338 rtl8168d_4_hw_phy_config(tp);
4339 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004340 case RTL_GIGA_MAC_VER_29:
4341 case RTL_GIGA_MAC_VER_30:
4342 rtl8105e_hw_phy_config(tp);
4343 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004344 case RTL_GIGA_MAC_VER_31:
4345 /* None. */
4346 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004347 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004348 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004349 rtl8168e_1_hw_phy_config(tp);
4350 break;
4351 case RTL_GIGA_MAC_VER_34:
4352 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004353 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004354 case RTL_GIGA_MAC_VER_35:
4355 rtl8168f_1_hw_phy_config(tp);
4356 break;
4357 case RTL_GIGA_MAC_VER_36:
4358 rtl8168f_2_hw_phy_config(tp);
4359 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004360
Hayes Wang7e18dca2012-03-30 14:33:02 +08004361 case RTL_GIGA_MAC_VER_37:
4362 rtl8402_hw_phy_config(tp);
4363 break;
4364
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004365 case RTL_GIGA_MAC_VER_38:
4366 rtl8411_hw_phy_config(tp);
4367 break;
4368
Hayes Wang5598bfe2012-07-02 17:23:21 +08004369 case RTL_GIGA_MAC_VER_39:
4370 rtl8106e_hw_phy_config(tp);
4371 break;
4372
Hayes Wangc5583862012-07-02 17:23:22 +08004373 case RTL_GIGA_MAC_VER_40:
4374 rtl8168g_1_hw_phy_config(tp);
4375 break;
hayeswang57538c42013-04-01 22:23:40 +00004376 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004377 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004378 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004379 rtl8168g_2_hw_phy_config(tp);
4380 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004381 case RTL_GIGA_MAC_VER_45:
4382 case RTL_GIGA_MAC_VER_47:
4383 rtl8168h_1_hw_phy_config(tp);
4384 break;
4385 case RTL_GIGA_MAC_VER_46:
4386 case RTL_GIGA_MAC_VER_48:
4387 rtl8168h_2_hw_phy_config(tp);
4388 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004389
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004390 case RTL_GIGA_MAC_VER_49:
4391 rtl8168ep_1_hw_phy_config(tp);
4392 break;
4393 case RTL_GIGA_MAC_VER_50:
4394 case RTL_GIGA_MAC_VER_51:
4395 rtl8168ep_2_hw_phy_config(tp);
4396 break;
4397
Hayes Wangc5583862012-07-02 17:23:22 +08004398 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004399 default:
4400 break;
4401 }
4402}
4403
Francois Romieuda78dbf2012-01-26 14:18:23 +01004404static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 struct timer_list *timer = &tp->timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4408
Francois Romieubcf0bf92006-07-26 23:14:13 +02004409 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
françois romieu4da19632011-01-03 15:07:55 +00004411 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02004412 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 * A busy loop could burn quite a few cycles on nowadays CPU.
4414 * Let's delay the execution of the timer for a few ticks.
4415 */
4416 timeout = HZ/10;
4417 goto out_mod_timer;
4418 }
4419
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004420 if (tp->link_ok(tp))
Francois Romieuda78dbf2012-01-26 14:18:23 +01004421 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02004423 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
françois romieu4da19632011-01-03 15:07:55 +00004425 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
4427out_mod_timer:
4428 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004429}
4430
4431static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4432{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004433 if (!test_and_set_bit(flag, tp->wk.flags))
4434 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004435}
4436
Kees Cook9de36cc2017-10-25 03:53:12 -07004437static void rtl8169_phy_timer(struct timer_list *t)
Francois Romieuda78dbf2012-01-26 14:18:23 +01004438{
Kees Cook9de36cc2017-10-25 03:53:12 -07004439 struct rtl8169_private *tp = from_timer(tp, t, timer);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004440
Francois Romieu98ddf982012-01-31 10:47:34 +01004441 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442}
4443
Francois Romieuffc46952012-07-06 14:19:23 +02004444DECLARE_RTL_COND(rtl_phy_reset_cond)
4445{
4446 return tp->phy_reset_pending(tp);
4447}
4448
Francois Romieubf793292006-11-01 00:53:05 +01004449static void rtl8169_phy_reset(struct net_device *dev,
4450 struct rtl8169_private *tp)
4451{
françois romieu4da19632011-01-03 15:07:55 +00004452 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004453 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004454}
4455
David S. Miller8decf862011-09-22 03:23:13 -04004456static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4457{
David S. Miller8decf862011-09-22 03:23:13 -04004458 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004459 (RTL_R8(tp, PHYstatus) & TBI_Enable);
David S. Miller8decf862011-09-22 03:23:13 -04004460}
4461
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004462static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463{
Francois Romieu5615d9f2007-08-17 17:50:46 +02004464 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004465
Marcus Sundberg773328942008-07-10 21:28:08 +02004466 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4467 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004468 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02004469 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004470
Francois Romieu6dccd162007-02-13 23:38:05 +01004471 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4472
4473 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4474 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004475
Francois Romieubcf0bf92006-07-26 23:14:13 +02004476 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004477 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004478 RTL_W8(tp, 0x82, 0x01);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004479 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004480 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004481 }
4482
Francois Romieubf793292006-11-01 00:53:05 +01004483 rtl8169_phy_reset(dev, tp);
4484
Oliver Neukum54405cd2011-01-06 21:55:13 +01004485 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004486 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4487 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4488 (tp->mii.supports_gmii ?
4489 ADVERTISED_1000baseT_Half |
4490 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004491
David S. Miller8decf862011-09-22 03:23:13 -04004492 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00004493 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004494}
4495
Francois Romieu773d2022007-01-31 23:47:43 +01004496static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4497{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004498 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004499
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004500 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00004501
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004502 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4503 RTL_R32(tp, MAC4);
françois romieu908ba2bf2010-04-26 11:42:58 +00004504
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004505 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4506 RTL_R32(tp, MAC0);
françois romieu908ba2bf2010-04-26 11:42:58 +00004507
françois romieu9ecb9aa2012-12-07 11:20:21 +00004508 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4509 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004510
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004511 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieu773d2022007-01-31 23:47:43 +01004512
Francois Romieuda78dbf2012-01-26 14:18:23 +01004513 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004514}
4515
4516static int rtl_set_mac_address(struct net_device *dev, void *p)
4517{
4518 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004519 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004520 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004521
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004522 ret = eth_mac_addr(dev, p);
4523 if (ret)
4524 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004525
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004526 pm_runtime_get_noresume(d);
4527
4528 if (pm_runtime_active(d))
4529 rtl_rar_set(tp, dev->dev_addr);
4530
4531 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004532
4533 return 0;
4534}
4535
Francois Romieu5f787a12006-08-17 13:02:36 +02004536static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4537{
4538 struct rtl8169_private *tp = netdev_priv(dev);
4539 struct mii_ioctl_data *data = if_mii(ifr);
4540
Francois Romieu8b4ab282008-11-19 22:05:25 -08004541 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4542}
Francois Romieu5f787a12006-08-17 13:02:36 +02004543
Francois Romieucecb5fd2011-04-01 10:21:07 +02004544static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4545 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004546{
Francois Romieu5f787a12006-08-17 13:02:36 +02004547 switch (cmd) {
4548 case SIOCGMIIPHY:
4549 data->phy_id = 32; /* Internal PHY */
4550 return 0;
4551
4552 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004553 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004554 return 0;
4555
4556 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004557 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004558 return 0;
4559 }
4560 return -EOPNOTSUPP;
4561}
4562
Francois Romieu8b4ab282008-11-19 22:05:25 -08004563static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4564{
4565 return -EOPNOTSUPP;
4566}
4567
Bill Pembertonbaf63292012-12-03 09:23:28 -05004568static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004569{
4570 struct mdio_ops *ops = &tp->mdio_ops;
4571
4572 switch (tp->mac_version) {
4573 case RTL_GIGA_MAC_VER_27:
4574 ops->write = r8168dp_1_mdio_write;
4575 ops->read = r8168dp_1_mdio_read;
4576 break;
françois romieue6de30d2011-01-03 15:08:37 +00004577 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004578 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004579 ops->write = r8168dp_2_mdio_write;
4580 ops->read = r8168dp_2_mdio_read;
4581 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004582 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004583 ops->write = r8168g_mdio_write;
4584 ops->read = r8168g_mdio_read;
4585 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004586 default:
4587 ops->write = r8169_mdio_write;
4588 ops->read = r8169_mdio_read;
4589 break;
4590 }
4591}
4592
hayeswange2409d82013-03-31 17:02:04 +00004593static void rtl_speed_down(struct rtl8169_private *tp)
4594{
4595 u32 adv;
4596 int lpa;
4597
4598 rtl_writephy(tp, 0x1f, 0x0000);
4599 lpa = rtl_readphy(tp, MII_LPA);
4600
4601 if (lpa & (LPA_10HALF | LPA_10FULL))
4602 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4603 else if (lpa & (LPA_100HALF | LPA_100FULL))
4604 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4605 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4606 else
4607 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4608 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4609 (tp->mii.supports_gmii ?
4610 ADVERTISED_1000baseT_Half |
4611 ADVERTISED_1000baseT_Full : 0);
4612
4613 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4614 adv);
4615}
4616
David S. Miller1805b2f2011-10-24 18:18:09 -04004617static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4618{
David S. Miller1805b2f2011-10-24 18:18:09 -04004619 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004620 case RTL_GIGA_MAC_VER_25:
4621 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004622 case RTL_GIGA_MAC_VER_29:
4623 case RTL_GIGA_MAC_VER_30:
4624 case RTL_GIGA_MAC_VER_32:
4625 case RTL_GIGA_MAC_VER_33:
4626 case RTL_GIGA_MAC_VER_34:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004627 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004628 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04004629 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4630 break;
4631 default:
4632 break;
4633 }
4634}
4635
4636static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4637{
Heiner Kallweitfe87bef2018-06-24 18:40:23 +02004638 if (!netif_running(tp->dev) || !tp->saved_wolopts)
David S. Miller1805b2f2011-10-24 18:18:09 -04004639 return false;
4640
hayeswange2409d82013-03-31 17:02:04 +00004641 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004642 rtl_wol_suspend_quirk(tp);
4643
4644 return true;
4645}
4646
françois romieu065c27c2011-01-03 15:08:12 +00004647static void r8168_phy_power_up(struct rtl8169_private *tp)
4648{
4649 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004650 switch (tp->mac_version) {
4651 case RTL_GIGA_MAC_VER_11:
4652 case RTL_GIGA_MAC_VER_12:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004653 case RTL_GIGA_MAC_VER_17 ... RTL_GIGA_MAC_VER_28:
hayeswang01dc7fe2011-03-21 01:50:28 +00004654 case RTL_GIGA_MAC_VER_31:
4655 rtl_writephy(tp, 0x0e, 0x0000);
4656 break;
4657 default:
4658 break;
4659 }
françois romieu065c27c2011-01-03 15:08:12 +00004660 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
David S. Millerb2d6cee2018-05-11 20:53:22 -04004661
4662 /* give MAC/PHY some time to resume */
4663 msleep(20);
françois romieu065c27c2011-01-03 15:08:12 +00004664}
4665
4666static void r8168_phy_power_down(struct rtl8169_private *tp)
4667{
4668 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004669 switch (tp->mac_version) {
4670 case RTL_GIGA_MAC_VER_32:
4671 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004672 case RTL_GIGA_MAC_VER_40:
4673 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004674 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4675 break;
4676
4677 case RTL_GIGA_MAC_VER_11:
4678 case RTL_GIGA_MAC_VER_12:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004679 case RTL_GIGA_MAC_VER_17 ... RTL_GIGA_MAC_VER_28:
hayeswang01dc7fe2011-03-21 01:50:28 +00004680 case RTL_GIGA_MAC_VER_31:
4681 rtl_writephy(tp, 0x0e, 0x0200);
4682 default:
4683 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4684 break;
4685 }
françois romieu065c27c2011-01-03 15:08:12 +00004686}
4687
4688static void r8168_pll_power_down(struct rtl8169_private *tp)
4689{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01004690 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004691 return;
4692
hayeswang01dc7fe2011-03-21 01:50:28 +00004693 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4694 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004695 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004696
David S. Miller1805b2f2011-10-24 18:18:09 -04004697 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004698 return;
françois romieu065c27c2011-01-03 15:08:12 +00004699
4700 r8168_phy_power_down(tp);
4701
4702 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004703 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004704 case RTL_GIGA_MAC_VER_37:
4705 case RTL_GIGA_MAC_VER_39:
4706 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004707 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004708 case RTL_GIGA_MAC_VER_45:
4709 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004710 case RTL_GIGA_MAC_VER_47:
4711 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004712 case RTL_GIGA_MAC_VER_50:
4713 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004714 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004715 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004716 case RTL_GIGA_MAC_VER_40:
4717 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004718 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004719 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004720 0xfc000000, ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004721 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004722 break;
françois romieu065c27c2011-01-03 15:08:12 +00004723 }
4724}
4725
4726static void r8168_pll_power_up(struct rtl8169_private *tp)
4727{
françois romieu065c27c2011-01-03 15:08:12 +00004728 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004729 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004730 case RTL_GIGA_MAC_VER_37:
4731 case RTL_GIGA_MAC_VER_39:
4732 case RTL_GIGA_MAC_VER_43:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004733 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004734 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004735 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004736 case RTL_GIGA_MAC_VER_45:
4737 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004738 case RTL_GIGA_MAC_VER_47:
4739 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004740 case RTL_GIGA_MAC_VER_50:
4741 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004742 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004743 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004744 case RTL_GIGA_MAC_VER_40:
4745 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004746 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004747 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004748 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004749 0x00000000, ERIAR_EXGMAC);
4750 break;
françois romieu065c27c2011-01-03 15:08:12 +00004751 }
4752
4753 r8168_phy_power_up(tp);
4754}
4755
françois romieu065c27c2011-01-03 15:08:12 +00004756static void rtl_pll_power_down(struct rtl8169_private *tp)
4757{
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004758 switch (tp->mac_version) {
4759 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4760 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4761 break;
4762 default:
4763 r8168_pll_power_down(tp);
4764 }
françois romieu065c27c2011-01-03 15:08:12 +00004765}
4766
4767static void rtl_pll_power_up(struct rtl8169_private *tp)
4768{
françois romieu065c27c2011-01-03 15:08:12 +00004769 switch (tp->mac_version) {
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004770 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4771 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
françois romieu065c27c2011-01-03 15:08:12 +00004772 break;
françois romieu065c27c2011-01-03 15:08:12 +00004773 default:
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004774 r8168_pll_power_up(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004775 }
4776}
4777
Hayes Wange542a222011-07-06 15:58:04 +08004778static void rtl_init_rxcfg(struct rtl8169_private *tp)
4779{
Hayes Wange542a222011-07-06 15:58:04 +08004780 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004781 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4782 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004783 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004784 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004785 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004786 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02004787 case RTL_GIGA_MAC_VER_35:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004788 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004789 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004790 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004791 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00004792 break;
Hayes Wange542a222011-07-06 15:58:04 +08004793 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004794 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004795 break;
4796 }
4797}
4798
Hayes Wang92fc43b2011-07-06 15:58:03 +08004799static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4800{
Timo Teräs9fba0812013-01-15 21:01:24 +00004801 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004802}
4803
Francois Romieud58d46b2011-05-03 16:38:29 +02004804static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4805{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004806 if (tp->jumbo_ops.enable) {
4807 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4808 tp->jumbo_ops.enable(tp);
4809 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4810 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004811}
4812
4813static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4814{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004815 if (tp->jumbo_ops.disable) {
4816 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4817 tp->jumbo_ops.disable(tp);
4818 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4819 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004820}
4821
4822static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4823{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004824 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4825 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004826 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004827}
4828
4829static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4830{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004831 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4832 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004833 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004834}
4835
4836static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4837{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004838 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004839}
4840
4841static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4842{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004843 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004844}
4845
4846static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4847{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004848 RTL_W8(tp, MaxTxPacketSize, 0x3f);
4849 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4850 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004851 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004852}
4853
4854static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4855{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004856 RTL_W8(tp, MaxTxPacketSize, 0x0c);
4857 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4858 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004859 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004860}
4861
4862static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4863{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004864 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01004865 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004866}
4867
4868static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4869{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004870 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004871 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004872}
4873
4874static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4875{
Francois Romieud58d46b2011-05-03 16:38:29 +02004876 r8168b_0_hw_jumbo_enable(tp);
4877
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004878 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004879}
4880
4881static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4882{
Francois Romieud58d46b2011-05-03 16:38:29 +02004883 r8168b_0_hw_jumbo_disable(tp);
4884
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004885 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004886}
4887
Bill Pembertonbaf63292012-12-03 09:23:28 -05004888static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02004889{
4890 struct jumbo_ops *ops = &tp->jumbo_ops;
4891
4892 switch (tp->mac_version) {
4893 case RTL_GIGA_MAC_VER_11:
4894 ops->disable = r8168b_0_hw_jumbo_disable;
4895 ops->enable = r8168b_0_hw_jumbo_enable;
4896 break;
4897 case RTL_GIGA_MAC_VER_12:
4898 case RTL_GIGA_MAC_VER_17:
4899 ops->disable = r8168b_1_hw_jumbo_disable;
4900 ops->enable = r8168b_1_hw_jumbo_enable;
4901 break;
4902 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4903 case RTL_GIGA_MAC_VER_19:
4904 case RTL_GIGA_MAC_VER_20:
4905 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4906 case RTL_GIGA_MAC_VER_22:
4907 case RTL_GIGA_MAC_VER_23:
4908 case RTL_GIGA_MAC_VER_24:
4909 case RTL_GIGA_MAC_VER_25:
4910 case RTL_GIGA_MAC_VER_26:
4911 ops->disable = r8168c_hw_jumbo_disable;
4912 ops->enable = r8168c_hw_jumbo_enable;
4913 break;
4914 case RTL_GIGA_MAC_VER_27:
4915 case RTL_GIGA_MAC_VER_28:
4916 ops->disable = r8168dp_hw_jumbo_disable;
4917 ops->enable = r8168dp_hw_jumbo_enable;
4918 break;
4919 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4920 case RTL_GIGA_MAC_VER_32:
4921 case RTL_GIGA_MAC_VER_33:
4922 case RTL_GIGA_MAC_VER_34:
4923 ops->disable = r8168e_hw_jumbo_disable;
4924 ops->enable = r8168e_hw_jumbo_enable;
4925 break;
4926
4927 /*
4928 * No action needed for jumbo frames with 8169.
4929 * No jumbo for 810x at all.
4930 */
Heiner Kallweit2a718832018-05-02 21:39:49 +02004931 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02004932 default:
4933 ops->disable = NULL;
4934 ops->enable = NULL;
4935 break;
4936 }
4937}
4938
Francois Romieuffc46952012-07-06 14:19:23 +02004939DECLARE_RTL_COND(rtl_chipcmd_cond)
4940{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004941 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02004942}
4943
Francois Romieu6f43adc2011-04-29 15:05:51 +02004944static void rtl_hw_reset(struct rtl8169_private *tp)
4945{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004946 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004947
Francois Romieuffc46952012-07-06 14:19:23 +02004948 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004949}
4950
Francois Romieub6ffd972011-06-17 17:00:05 +02004951static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4952{
4953 struct rtl_fw *rtl_fw;
4954 const char *name;
4955 int rc = -ENOMEM;
4956
4957 name = rtl_lookup_firmware_name(tp);
4958 if (!name)
4959 goto out_no_firmware;
4960
4961 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4962 if (!rtl_fw)
4963 goto err_warn;
4964
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004965 rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
Francois Romieub6ffd972011-06-17 17:00:05 +02004966 if (rc < 0)
4967 goto err_free;
4968
Francois Romieufd112f22011-06-18 00:10:29 +02004969 rc = rtl_check_firmware(tp, rtl_fw);
4970 if (rc < 0)
4971 goto err_release_firmware;
4972
Francois Romieub6ffd972011-06-17 17:00:05 +02004973 tp->rtl_fw = rtl_fw;
4974out:
4975 return;
4976
Francois Romieufd112f22011-06-18 00:10:29 +02004977err_release_firmware:
4978 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004979err_free:
4980 kfree(rtl_fw);
4981err_warn:
4982 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4983 name, rc);
4984out_no_firmware:
4985 tp->rtl_fw = NULL;
4986 goto out;
4987}
4988
François Romieu953a12c2011-04-24 17:38:48 +02004989static void rtl_request_firmware(struct rtl8169_private *tp)
4990{
Francois Romieub6ffd972011-06-17 17:00:05 +02004991 if (IS_ERR(tp->rtl_fw))
4992 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004993}
4994
Hayes Wang92fc43b2011-07-06 15:58:03 +08004995static void rtl_rx_close(struct rtl8169_private *tp)
4996{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004997 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004998}
4999
Francois Romieuffc46952012-07-06 14:19:23 +02005000DECLARE_RTL_COND(rtl_npq_cond)
5001{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005002 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02005003}
5004
5005DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5006{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005007 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02005008}
5009
françois romieue6de30d2011-01-03 15:08:37 +00005010static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011{
5012 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00005013 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014
Hayes Wang92fc43b2011-07-06 15:58:03 +08005015 rtl_rx_close(tp);
5016
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02005017 switch (tp->mac_version) {
5018 case RTL_GIGA_MAC_VER_27:
5019 case RTL_GIGA_MAC_VER_28:
5020 case RTL_GIGA_MAC_VER_31:
Francois Romieuffc46952012-07-06 14:19:23 +02005021 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02005022 break;
5023 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
5024 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005025 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02005026 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02005027 break;
5028 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005029 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005030 udelay(100);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02005031 break;
françois romieue6de30d2011-01-03 15:08:37 +00005032 }
5033
Hayes Wang92fc43b2011-07-06 15:58:03 +08005034 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035}
5036
Francois Romieu7f796d832007-06-11 23:04:41 +02005037static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005038{
Francois Romieu9cb427b2006-11-02 00:10:16 +01005039 /* Set DMA burst size and Interframe Gap Time */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005040 RTL_W32(tp, TxConfig, (TX_DMA_BURST << TxDMAShift) |
Francois Romieu9cb427b2006-11-02 00:10:16 +01005041 (InterFrameGap << TxInterFrameGapShift));
5042}
5043
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02005044static void rtl_set_rx_max_size(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02005046 /* Low hurts. Let's disable the filtering. */
5047 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
Francois Romieu07ce4062007-02-23 23:36:39 +01005048}
5049
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005050static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02005051{
5052 /*
5053 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5054 * register to be written before TxDescAddrLow to work.
5055 * Switching from MMIO to I/O access fixes the issue as well.
5056 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005057 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
5058 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
5059 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
5060 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005061}
5062
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005063static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01005064{
Francois Romieu37441002011-06-17 22:58:54 +02005065 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01005066 u32 mac_version;
5067 u32 clk;
5068 u32 val;
5069 } cfg2_info [] = {
5070 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5071 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5072 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5073 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02005074 };
5075 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01005076 unsigned int i;
5077 u32 clk;
5078
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005079 clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01005080 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01005081 if ((p->mac_version == mac_version) && (p->clk == clk)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005082 RTL_W32(tp, 0x7c, p->val);
Francois Romieu6dccd162007-02-13 23:38:05 +01005083 break;
5084 }
5085 }
5086}
5087
Francois Romieue6b763e2012-03-08 09:35:39 +01005088static void rtl_set_rx_mode(struct net_device *dev)
5089{
5090 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieue6b763e2012-03-08 09:35:39 +01005091 u32 mc_filter[2]; /* Multicast hash filter */
5092 int rx_mode;
5093 u32 tmp = 0;
5094
5095 if (dev->flags & IFF_PROMISC) {
5096 /* Unconditionally log net taps. */
5097 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5098 rx_mode =
5099 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5100 AcceptAllPhys;
5101 mc_filter[1] = mc_filter[0] = 0xffffffff;
5102 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5103 (dev->flags & IFF_ALLMULTI)) {
5104 /* Too many to filter perfectly -- accept all multicasts. */
5105 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5106 mc_filter[1] = mc_filter[0] = 0xffffffff;
5107 } else {
5108 struct netdev_hw_addr *ha;
5109
5110 rx_mode = AcceptBroadcast | AcceptMyPhys;
5111 mc_filter[1] = mc_filter[0] = 0;
5112 netdev_for_each_mc_addr(ha, dev) {
5113 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5114 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5115 rx_mode |= AcceptMulticast;
5116 }
5117 }
5118
5119 if (dev->features & NETIF_F_RXALL)
5120 rx_mode |= (AcceptErr | AcceptRunt);
5121
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005122 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Francois Romieue6b763e2012-03-08 09:35:39 +01005123
5124 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5125 u32 data = mc_filter[0];
5126
5127 mc_filter[0] = swab32(mc_filter[1]);
5128 mc_filter[1] = swab32(data);
5129 }
5130
Nathan Walp04817762012-11-01 12:08:47 +00005131 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5132 mc_filter[1] = mc_filter[0] = 0xffffffff;
5133
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005134 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
5135 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01005136
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005137 RTL_W32(tp, RxConfig, tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01005138}
5139
Heiner Kallweit52f85602018-05-19 10:29:33 +02005140static void rtl_hw_start(struct rtl8169_private *tp)
5141{
5142 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
5143
5144 tp->hw_start(tp);
5145
5146 rtl_set_rx_max_size(tp);
5147 rtl_set_rx_tx_desc_registers(tp);
5148 rtl_set_rx_tx_config_registers(tp);
5149 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
5150
5151 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5152 RTL_R8(tp, IntrMask);
5153 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
5154 rtl_set_rx_mode(tp->dev);
5155 /* no early-rx interrupts */
5156 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
5157 rtl_irq_enable_all(tp);
5158}
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 Kallweitf37658d2018-06-23 09:51:28 +02005235static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
hayeswang45dd95c2013-07-08 17:09:01 +08005236{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005237 rtl_csi_access_enable(tp, 0x27);
Francois Romieudacf8152008-08-02 20:44:13 +02005238}
5239
5240struct ephy_info {
5241 unsigned int offset;
5242 u16 mask;
5243 u16 bits;
5244};
5245
Francois Romieufdf6fc02012-07-06 22:40:38 +02005246static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5247 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005248{
5249 u16 w;
5250
5251 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005252 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5253 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005254 e++;
5255 }
5256}
5257
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005258static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02005259{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005260 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005261 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005262}
5263
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005264static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005265{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005266 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005267 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005268}
5269
hayeswangb51ecea2014-07-09 14:52:51 +08005270static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5271{
hayeswangb51ecea2014-07-09 14:52:51 +08005272 u8 data;
5273
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005274 data = RTL_R8(tp, Config3);
hayeswangb51ecea2014-07-09 14:52:51 +08005275
5276 if (enable)
5277 data |= Rdy_to_L23;
5278 else
5279 data &= ~Rdy_to_L23;
5280
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005281 RTL_W8(tp, Config3, data);
hayeswangb51ecea2014-07-09 14:52:51 +08005282}
5283
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005284static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
5285{
5286 if (enable) {
5287 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
5288 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
5289 } else {
5290 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5291 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
5292 }
5293}
5294
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005295static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005296{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005297 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005298
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005299 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005300 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieub726e492008-06-28 12:22:59 +02005301
françois romieufaf1e782013-02-27 13:01:57 +00005302 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005303 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00005304 PCI_EXP_DEVCTL_NOSNOOP_EN);
5305 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005306}
5307
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005308static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005309{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005310 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005311
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005312 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005313
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005314 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005315}
5316
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005317static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005318{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005319 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02005320
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005321 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005322
françois romieufaf1e782013-02-27 13:01:57 +00005323 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005324 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02005325
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005326 rtl_disable_clock_request(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005327
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005328 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005329 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu219a1e92008-06-28 11:58:39 +02005330}
5331
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005332static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005333{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005334 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005335 { 0x01, 0, 0x0001 },
5336 { 0x02, 0x0800, 0x1000 },
5337 { 0x03, 0, 0x0042 },
5338 { 0x06, 0x0080, 0x0000 },
5339 { 0x07, 0, 0x2000 }
5340 };
5341
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005342 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005343
Francois Romieufdf6fc02012-07-06 22:40:38 +02005344 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005345
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005346 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005347}
5348
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005349static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005350{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005351 rtl_set_def_aspm_entry_latency(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005352
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005353 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02005354
françois romieufaf1e782013-02-27 13:01:57 +00005355 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005356 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieuef3386f2008-06-29 12:24:30 +02005357
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005358 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005359 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieuef3386f2008-06-29 12:24:30 +02005360}
5361
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005362static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005363{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005364 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005365
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005366 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005367
5368 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005369 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005370
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005371 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005372
françois romieufaf1e782013-02-27 13:01:57 +00005373 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005374 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005375
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005376 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005377 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005378}
5379
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005380static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005381{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005382 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005383 { 0x02, 0x0800, 0x1000 },
5384 { 0x03, 0, 0x0002 },
5385 { 0x06, 0x0080, 0x0000 }
5386 };
5387
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005388 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005389
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005390 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02005391
Francois Romieufdf6fc02012-07-06 22:40:38 +02005392 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005393
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005394 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005395}
5396
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005397static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005398{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005399 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005400 { 0x01, 0, 0x0001 },
5401 { 0x03, 0x0400, 0x0220 }
5402 };
5403
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005404 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005405
Francois Romieufdf6fc02012-07-06 22:40:38 +02005406 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005407
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005408 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005409}
5410
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005411static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005412{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005413 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005414}
5415
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005416static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005417{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005418 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005419
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005420 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005421}
5422
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005423static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005424{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005425 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005426
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005427 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005428
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005429 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005430
françois romieufaf1e782013-02-27 13:01:57 +00005431 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005432 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02005433
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005434 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005435 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu5b538df2008-07-20 16:22:45 +02005436}
5437
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005438static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005439{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005440 rtl_set_def_aspm_entry_latency(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005441
françois romieufaf1e782013-02-27 13:01:57 +00005442 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005443 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang4804b3b2011-03-21 01:50:29 +00005444
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005445 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang4804b3b2011-03-21 01:50:29 +00005446
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005447 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005448}
5449
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005450static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005451{
5452 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005453 { 0x0b, 0x0000, 0x0048 },
5454 { 0x19, 0x0020, 0x0050 },
5455 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00005456 };
françois romieue6de30d2011-01-03 15:08:37 +00005457
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005458 rtl_set_def_aspm_entry_latency(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005459
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005460 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00005461
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005462 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
françois romieue6de30d2011-01-03 15:08:37 +00005463
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005464 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00005465
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005466 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005467}
5468
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005469static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005470{
Hayes Wang70090422011-07-06 15:58:06 +08005471 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005472 { 0x00, 0x0200, 0x0100 },
5473 { 0x00, 0x0000, 0x0004 },
5474 { 0x06, 0x0002, 0x0001 },
5475 { 0x06, 0x0000, 0x0030 },
5476 { 0x07, 0x0000, 0x2000 },
5477 { 0x00, 0x0000, 0x0020 },
5478 { 0x03, 0x5800, 0x2000 },
5479 { 0x03, 0x0000, 0x0001 },
5480 { 0x01, 0x0800, 0x1000 },
5481 { 0x07, 0x0000, 0x4000 },
5482 { 0x1e, 0x0000, 0x2000 },
5483 { 0x19, 0xffff, 0xfe6c },
5484 { 0x0a, 0x0000, 0x0040 }
5485 };
5486
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005487 rtl_set_def_aspm_entry_latency(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005488
Francois Romieufdf6fc02012-07-06 22:40:38 +02005489 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005490
françois romieufaf1e782013-02-27 13:01:57 +00005491 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005492 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00005493
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005494 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang01dc7fe2011-03-21 01:50:28 +00005495
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005496 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005497
5498 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005499 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
5500 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005501
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005502 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005503}
5504
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005505static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005506{
5507 static const struct ephy_info e_info_8168e_2[] = {
5508 { 0x09, 0x0000, 0x0080 },
5509 { 0x19, 0x0000, 0x0224 }
5510 };
5511
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005512 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005513
Francois Romieufdf6fc02012-07-06 22:40:38 +02005514 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005515
françois romieufaf1e782013-02-27 13:01:57 +00005516 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005517 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08005518
Francois Romieufdf6fc02012-07-06 22:40:38 +02005519 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5520 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5521 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5522 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5523 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5524 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005525 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5526 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005527
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005528 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005529
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005530 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005531
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005532 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5533 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08005534
5535 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005536 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang70090422011-07-06 15:58:06 +08005537
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005538 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5539 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5540 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Heiner Kallweitaa1e7d22018-06-23 09:53:00 +02005541
5542 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang70090422011-07-06 15:58:06 +08005543}
5544
Hayes Wang5f886e02012-03-30 14:33:03 +08005545static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005546{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005547 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005548
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005549 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08005550
Francois Romieufdf6fc02012-07-06 22:40:38 +02005551 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5552 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5553 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5554 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005555 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5556 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5557 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5558 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005559 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5560 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005561
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005562 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc2218922011-09-06 16:55:18 +08005563
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005564 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005565
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005566 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5567 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5568 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5569 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5570 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005571}
5572
Hayes Wang5f886e02012-03-30 14:33:03 +08005573static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5574{
Hayes Wang5f886e02012-03-30 14:33:03 +08005575 static const struct ephy_info e_info_8168f_1[] = {
5576 { 0x06, 0x00c0, 0x0020 },
5577 { 0x08, 0x0001, 0x0002 },
5578 { 0x09, 0x0000, 0x0080 },
5579 { 0x19, 0x0000, 0x0224 }
5580 };
5581
5582 rtl_hw_start_8168f(tp);
5583
Francois Romieufdf6fc02012-07-06 22:40:38 +02005584 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005585
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005586 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005587
5588 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005589 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang5f886e02012-03-30 14:33:03 +08005590}
5591
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005592static void rtl_hw_start_8411(struct rtl8169_private *tp)
5593{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005594 static const struct ephy_info e_info_8168f_1[] = {
5595 { 0x06, 0x00c0, 0x0020 },
5596 { 0x0f, 0xffff, 0x5200 },
5597 { 0x1e, 0x0000, 0x4000 },
5598 { 0x19, 0x0000, 0x0224 }
5599 };
5600
5601 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08005602 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005603
Francois Romieufdf6fc02012-07-06 22:40:38 +02005604 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005605
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005606 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005607}
5608
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005609static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08005610{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005611 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
hayeswangbeb330a2013-04-01 22:23:39 +00005612
Hayes Wangc5583862012-07-02 17:23:22 +08005613 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5614 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5615 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5616 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5617
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005618 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08005619
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005620 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08005621
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005622 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5623 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00005624 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005625
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005626 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5627 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc5583862012-07-02 17:23:22 +08005628
5629 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5630 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5631
5632 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005633 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wangc5583862012-07-02 17:23:22 +08005634
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005635 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5636 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005637
5638 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08005639}
5640
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005641static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5642{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005643 static const struct ephy_info e_info_8168g_1[] = {
5644 { 0x00, 0x0000, 0x0008 },
5645 { 0x0c, 0x37d0, 0x0820 },
5646 { 0x1e, 0x0000, 0x0001 },
5647 { 0x19, 0x8000, 0x0000 }
5648 };
5649
5650 rtl_hw_start_8168g(tp);
5651
5652 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005653 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005654 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005655 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005656}
5657
hayeswang57538c42013-04-01 22:23:40 +00005658static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5659{
hayeswang57538c42013-04-01 22:23:40 +00005660 static const struct ephy_info e_info_8168g_2[] = {
5661 { 0x00, 0x0000, 0x0008 },
5662 { 0x0c, 0x3df0, 0x0200 },
5663 { 0x19, 0xffff, 0xfc00 },
5664 { 0x1e, 0xffff, 0x20eb }
5665 };
5666
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005667 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00005668
5669 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005670 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5671 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang57538c42013-04-01 22:23:40 +00005672 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5673}
5674
hayeswang45dd95c2013-07-08 17:09:01 +08005675static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5676{
hayeswang45dd95c2013-07-08 17:09:01 +08005677 static const struct ephy_info e_info_8411_2[] = {
5678 { 0x00, 0x0000, 0x0008 },
5679 { 0x0c, 0x3df0, 0x0200 },
5680 { 0x0f, 0xffff, 0x5200 },
5681 { 0x19, 0x0020, 0x0000 },
5682 { 0x1e, 0x0000, 0x2000 }
5683 };
5684
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005685 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08005686
5687 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005688 rtl_hw_aspm_clkreq_enable(tp, false);
hayeswang45dd95c2013-07-08 17:09:01 +08005689 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005690 rtl_hw_aspm_clkreq_enable(tp, true);
hayeswang45dd95c2013-07-08 17:09:01 +08005691}
5692
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005693static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
5694{
Andrzej Hajda72521ea2015-09-24 16:00:24 +02005695 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005696 u32 data;
5697 static const struct ephy_info e_info_8168h_1[] = {
5698 { 0x1e, 0x0800, 0x0001 },
5699 { 0x1d, 0x0000, 0x0800 },
5700 { 0x05, 0xffff, 0x2089 },
5701 { 0x06, 0xffff, 0x5881 },
5702 { 0x04, 0xffff, 0x154a },
5703 { 0x01, 0xffff, 0x068b }
5704 };
5705
5706 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005707 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005708 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
5709
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005710 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005711
5712 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5713 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5714 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5715 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5716
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005717 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005718
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005719 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005720
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005721 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5722 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005723
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005724 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005725
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005726 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005727
5728 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5729
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005730 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5731 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005732
5733 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5734 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5735
5736 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005737 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005738
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005739 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5740 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005741
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005742 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005743
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005744 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005745
5746 rtl_pcie_state_l2l3_enable(tp, false);
5747
5748 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08005749 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005750 rtl_writephy(tp, 0x1f, 0x0000);
5751 if (rg_saw_cnt > 0) {
5752 u16 sw_cnt_1ms_ini;
5753
5754 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
5755 sw_cnt_1ms_ini &= 0x0fff;
5756 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005757 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005758 data |= sw_cnt_1ms_ini;
5759 r8168_mac_ocp_write(tp, 0xd412, data);
5760 }
5761
5762 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005763 data &= ~0xf0;
5764 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005765 r8168_mac_ocp_write(tp, 0xe056, data);
5766
5767 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005768 data &= ~0x6000;
5769 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005770 r8168_mac_ocp_write(tp, 0xe052, data);
5771
5772 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005773 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005774 data |= 0x017f;
5775 r8168_mac_ocp_write(tp, 0xe0d6, data);
5776
5777 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005778 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005779 data |= 0x047f;
5780 r8168_mac_ocp_write(tp, 0xd420, data);
5781
5782 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
5783 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
5784 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
5785 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005786
5787 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005788}
5789
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005790static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
5791{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08005792 rtl8168ep_stop_cmac(tp);
5793
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005794 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005795
5796 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5797 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
5798 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
5799 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5800
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005801 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005802
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005803 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005804
5805 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5806 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5807
5808 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
5809
5810 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5811
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005812 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5813 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005814
5815 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5816 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5817
5818 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005819 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005820
5821 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5822
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005823 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005824
5825 rtl_pcie_state_l2l3_enable(tp, false);
5826}
5827
5828static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
5829{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005830 static const struct ephy_info e_info_8168ep_1[] = {
5831 { 0x00, 0xffff, 0x10ab },
5832 { 0x06, 0xffff, 0xf030 },
5833 { 0x08, 0xffff, 0x2006 },
5834 { 0x0d, 0xffff, 0x1666 },
5835 { 0x0c, 0x3ff0, 0x0000 }
5836 };
5837
5838 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005839 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005840 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
5841
5842 rtl_hw_start_8168ep(tp);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005843
5844 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005845}
5846
5847static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
5848{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005849 static const struct ephy_info e_info_8168ep_2[] = {
5850 { 0x00, 0xffff, 0x10a3 },
5851 { 0x19, 0xffff, 0xfc00 },
5852 { 0x1e, 0xffff, 0x20ea }
5853 };
5854
5855 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005856 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005857 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
5858
5859 rtl_hw_start_8168ep(tp);
5860
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005861 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5862 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005863
5864 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005865}
5866
5867static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
5868{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005869 u32 data;
5870 static const struct ephy_info e_info_8168ep_3[] = {
5871 { 0x00, 0xffff, 0x10a3 },
5872 { 0x19, 0xffff, 0x7c00 },
5873 { 0x1e, 0xffff, 0x20eb },
5874 { 0x0d, 0xffff, 0x1666 }
5875 };
5876
5877 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005878 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005879 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
5880
5881 rtl_hw_start_8168ep(tp);
5882
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005883 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5884 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005885
5886 data = r8168_mac_ocp_read(tp, 0xd3e2);
5887 data &= 0xf000;
5888 data |= 0x0271;
5889 r8168_mac_ocp_write(tp, 0xd3e2, data);
5890
5891 data = r8168_mac_ocp_read(tp, 0xd3e4);
5892 data &= 0xff00;
5893 r8168_mac_ocp_write(tp, 0xd3e4, data);
5894
5895 data = r8168_mac_ocp_read(tp, 0xe860);
5896 data |= 0x0080;
5897 r8168_mac_ocp_write(tp, 0xe860, data);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005898
5899 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005900}
5901
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005902static void rtl_hw_start_8168(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005903{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005904 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02005905
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005906 tp->cp_cmd &= ~INTT_MASK;
5907 tp->cp_cmd |= PktCntrDisable | INTT_1;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005908 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu2dd99532007-06-11 23:22:52 +02005909
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005910 RTL_W16(tp, IntrMitigate, 0x5151);
Francois Romieu0e485152007-02-20 00:00:26 +01005911
5912 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00005913 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005914 tp->event_slow |= RxFIFOOver | PCSTimeout;
5915 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01005916 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005917
Francois Romieu219a1e92008-06-28 11:58:39 +02005918 switch (tp->mac_version) {
5919 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005920 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005921 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005922
5923 case RTL_GIGA_MAC_VER_12:
5924 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005925 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005926 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005927
5928 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005929 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005930 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005931
5932 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005933 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005934 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005935
5936 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005937 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005938 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005939
Francois Romieu197ff762008-06-28 13:16:02 +02005940 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005941 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005942 break;
Francois Romieu197ff762008-06-28 13:16:02 +02005943
Francois Romieu6fb07052008-06-29 11:54:28 +02005944 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005945 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005946 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02005947
Francois Romieuef3386f2008-06-29 12:24:30 +02005948 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005949 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005950 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02005951
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005952 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005953 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005954 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005955
Francois Romieu5b538df2008-07-20 16:22:45 +02005956 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00005957 case RTL_GIGA_MAC_VER_26:
5958 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005959 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005960 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02005961
françois romieue6de30d2011-01-03 15:08:37 +00005962 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005963 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005964 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005965
hayeswang4804b3b2011-03-21 01:50:29 +00005966 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005967 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005968 break;
5969
hayeswang01dc7fe2011-03-21 01:50:28 +00005970 case RTL_GIGA_MAC_VER_32:
5971 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005972 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005973 break;
5974 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005975 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005976 break;
françois romieue6de30d2011-01-03 15:08:37 +00005977
Hayes Wangc2218922011-09-06 16:55:18 +08005978 case RTL_GIGA_MAC_VER_35:
5979 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005980 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005981 break;
5982
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005983 case RTL_GIGA_MAC_VER_38:
5984 rtl_hw_start_8411(tp);
5985 break;
5986
Hayes Wangc5583862012-07-02 17:23:22 +08005987 case RTL_GIGA_MAC_VER_40:
5988 case RTL_GIGA_MAC_VER_41:
5989 rtl_hw_start_8168g_1(tp);
5990 break;
hayeswang57538c42013-04-01 22:23:40 +00005991 case RTL_GIGA_MAC_VER_42:
5992 rtl_hw_start_8168g_2(tp);
5993 break;
Hayes Wangc5583862012-07-02 17:23:22 +08005994
hayeswang45dd95c2013-07-08 17:09:01 +08005995 case RTL_GIGA_MAC_VER_44:
5996 rtl_hw_start_8411_2(tp);
5997 break;
5998
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005999 case RTL_GIGA_MAC_VER_45:
6000 case RTL_GIGA_MAC_VER_46:
6001 rtl_hw_start_8168h_1(tp);
6002 break;
6003
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006004 case RTL_GIGA_MAC_VER_49:
6005 rtl_hw_start_8168ep_1(tp);
6006 break;
6007
6008 case RTL_GIGA_MAC_VER_50:
6009 rtl_hw_start_8168ep_2(tp);
6010 break;
6011
6012 case RTL_GIGA_MAC_VER_51:
6013 rtl_hw_start_8168ep_3(tp);
6014 break;
6015
Francois Romieu219a1e92008-06-28 11:58:39 +02006016 default:
6017 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006018 tp->dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00006019 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006020 }
Francois Romieu07ce4062007-02-23 23:36:39 +01006021}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006023static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006024{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006025 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02006026 { 0x01, 0, 0x6e65 },
6027 { 0x02, 0, 0x091f },
6028 { 0x03, 0, 0xc2f9 },
6029 { 0x06, 0, 0xafb5 },
6030 { 0x07, 0, 0x0e00 },
6031 { 0x19, 0, 0xec80 },
6032 { 0x01, 0, 0x2e65 },
6033 { 0x01, 0, 0x6e65 }
6034 };
6035 u8 cfg1;
6036
Heiner Kallweitf37658d2018-06-23 09:51:28 +02006037 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006038
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006039 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006040
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006041 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006042
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006043 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02006044 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006045 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006046
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006047 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006048 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006049 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006050
Francois Romieufdf6fc02012-07-06 22:40:38 +02006051 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02006052}
6053
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006054static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006055{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02006056 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006057
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006058 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006059
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006060 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
6061 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006062}
6063
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006064static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006065{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006066 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006067
Francois Romieufdf6fc02012-07-06 22:40:38 +02006068 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006069}
6070
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006071static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006072{
6073 static const struct ephy_info e_info_8105e_1[] = {
6074 { 0x07, 0, 0x4000 },
6075 { 0x19, 0, 0x0200 },
6076 { 0x19, 0, 0x0020 },
6077 { 0x1e, 0, 0x2000 },
6078 { 0x03, 0, 0x0001 },
6079 { 0x19, 0, 0x0100 },
6080 { 0x19, 0, 0x0004 },
6081 { 0x0a, 0, 0x0020 }
6082 };
6083
Francois Romieucecb5fd2011-04-01 10:21:07 +02006084 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006085 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006086
Francois Romieucecb5fd2011-04-01 10:21:07 +02006087 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006088 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006089
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006090 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6091 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006092
Francois Romieufdf6fc02012-07-06 22:40:38 +02006093 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08006094
6095 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006096}
6097
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006098static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006099{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006100 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006101 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006102}
6103
Hayes Wang7e18dca2012-03-30 14:33:02 +08006104static void rtl_hw_start_8402(struct rtl8169_private *tp)
6105{
Hayes Wang7e18dca2012-03-30 14:33:02 +08006106 static const struct ephy_info e_info_8402[] = {
6107 { 0x19, 0xffff, 0xff64 },
6108 { 0x1e, 0, 0x4000 }
6109 };
6110
Heiner Kallweitf37658d2018-06-23 09:51:28 +02006111 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006112
6113 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006114 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006115
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006116 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
6117 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006118
Francois Romieufdf6fc02012-07-06 22:40:38 +02006119 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006120
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006121 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006122
Francois Romieufdf6fc02012-07-06 22:40:38 +02006123 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6124 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006125 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6126 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006127 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6128 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006129 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006130
6131 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006132}
6133
Hayes Wang5598bfe2012-07-02 17:23:21 +08006134static void rtl_hw_start_8106(struct rtl8169_private *tp)
6135{
Hayes Wang5598bfe2012-07-02 17:23:21 +08006136 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006137 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006138
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006139 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
6140 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6141 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006142
6143 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006144}
6145
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006146static void rtl_hw_start_8101(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01006147{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006148 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6149 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006150
Francois Romieucecb5fd2011-04-01 10:21:07 +02006151 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006152 tp->mac_version == RTL_GIGA_MAC_VER_16)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006153 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006154 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006155
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006156 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00006157
Heiner Kallweit12d42c52018-04-28 22:19:30 +02006158 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006159 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
hayeswang1a964642013-04-01 22:23:41 +00006160
Francois Romieu2857ffb2008-08-02 21:08:49 +02006161 switch (tp->mac_version) {
6162 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006163 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006164 break;
6165
6166 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006167 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006168 break;
6169
6170 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006171 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006172 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006173
6174 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006175 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006176 break;
6177 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006178 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006179 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006180
6181 case RTL_GIGA_MAC_VER_37:
6182 rtl_hw_start_8402(tp);
6183 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006184
6185 case RTL_GIGA_MAC_VER_39:
6186 rtl_hw_start_8106(tp);
6187 break;
hayeswang58152cd2013-04-01 22:23:42 +00006188 case RTL_GIGA_MAC_VER_43:
6189 rtl_hw_start_8168g_2(tp);
6190 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006191 case RTL_GIGA_MAC_VER_47:
6192 case RTL_GIGA_MAC_VER_48:
6193 rtl_hw_start_8168h_1(tp);
6194 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006195 }
6196
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006197 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198}
6199
6200static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6201{
Francois Romieud58d46b2011-05-03 16:38:29 +02006202 struct rtl8169_private *tp = netdev_priv(dev);
6203
Francois Romieud58d46b2011-05-03 16:38:29 +02006204 if (new_mtu > ETH_DATA_LEN)
6205 rtl_hw_jumbo_enable(tp);
6206 else
6207 rtl_hw_jumbo_disable(tp);
6208
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006210 netdev_update_features(dev);
6211
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213}
6214
6215static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6216{
Al Viro95e09182007-12-22 18:55:39 +00006217 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6219}
6220
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006221static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6222 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223{
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006224 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
6225 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006226
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006227 kfree(*data_buff);
6228 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 rtl8169_make_unusable_by_asic(desc);
6230}
6231
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006232static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233{
6234 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6235
Alexander Duycka0750132014-12-11 15:02:17 -08006236 /* Force memory writes to complete before releasing descriptor */
6237 dma_wmb();
6238
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006239 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240}
6241
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006242static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006244 return (void *)ALIGN((long)data, 16);
6245}
6246
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006247static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6248 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006249{
6250 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 dma_addr_t mapping;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006252 struct device *d = tp_to_dev(tp);
Heiner Kallweitd3b404c2018-04-17 23:22:14 +02006253 int node = dev_to_node(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006255 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006256 if (!data)
6257 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006258
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006259 if (rtl8169_align(data) != data) {
6260 kfree(data);
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006261 data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006262 if (!data)
6263 return NULL;
6264 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006265
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006266 mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006267 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006268 if (unlikely(dma_mapping_error(d, mapping))) {
6269 if (net_ratelimit())
6270 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006271 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273
Heiner Kallweitd731af72018-04-17 23:26:41 +02006274 desc->addr = cpu_to_le64(mapping);
6275 rtl8169_mark_to_asic(desc);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006276 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006277
6278err_out:
6279 kfree(data);
6280 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281}
6282
6283static void rtl8169_rx_clear(struct rtl8169_private *tp)
6284{
Francois Romieu07d3f512007-02-21 22:40:46 +01006285 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286
6287 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006288 if (tp->Rx_databuff[i]) {
6289 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 tp->RxDescArray + i);
6291 }
6292 }
6293}
6294
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006295static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006297 desc->opts1 |= cpu_to_le32(RingEnd);
6298}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006299
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006300static int rtl8169_rx_fill(struct rtl8169_private *tp)
6301{
6302 unsigned int i;
6303
6304 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006305 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006306
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006307 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006308 if (!data) {
6309 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006310 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006311 }
6312 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006315 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6316 return 0;
6317
6318err_out:
6319 rtl8169_rx_clear(tp);
6320 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321}
6322
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006323static int rtl8169_init_ring(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 rtl8169_init_ring_indexes(tp);
6326
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006327 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
6328 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006330 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331}
6332
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006333static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 struct TxDesc *desc)
6335{
6336 unsigned int len = tx_skb->len;
6337
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006338 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6339
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 desc->opts1 = 0x00;
6341 desc->opts2 = 0x00;
6342 desc->addr = 0x00;
6343 tx_skb->len = 0;
6344}
6345
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006346static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6347 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348{
6349 unsigned int i;
6350
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006351 for (i = 0; i < n; i++) {
6352 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 struct ring_info *tx_skb = tp->tx_skb + entry;
6354 unsigned int len = tx_skb->len;
6355
6356 if (len) {
6357 struct sk_buff *skb = tx_skb->skb;
6358
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006359 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 tp->TxDescArray + entry);
6361 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006362 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 tx_skb->skb = NULL;
6364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 }
6366 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006367}
6368
6369static void rtl8169_tx_clear(struct rtl8169_private *tp)
6370{
6371 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372 tp->cur_tx = tp->dirty_tx = 0;
6373}
6374
Francois Romieu4422bcd2012-01-26 11:23:32 +01006375static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376{
David Howellsc4028952006-11-22 14:57:56 +00006377 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01006378 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379
Francois Romieuda78dbf2012-01-26 14:18:23 +01006380 napi_disable(&tp->napi);
6381 netif_stop_queue(dev);
6382 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383
françois romieuc7c2c392011-12-04 20:30:52 +00006384 rtl8169_hw_reset(tp);
6385
Francois Romieu56de4142011-03-15 17:29:31 +01006386 for (i = 0; i < NUM_RX_DESC; i++)
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006387 rtl8169_mark_to_asic(tp->RxDescArray + i);
Francois Romieu56de4142011-03-15 17:29:31 +01006388
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00006390 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391
Francois Romieuda78dbf2012-01-26 14:18:23 +01006392 napi_enable(&tp->napi);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006393 rtl_hw_start(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01006394 netif_wake_queue(dev);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006395 rtl8169_check_link_status(dev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396}
6397
6398static void rtl8169_tx_timeout(struct net_device *dev)
6399{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006400 struct rtl8169_private *tp = netdev_priv(dev);
6401
6402 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403}
6404
6405static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07006406 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407{
6408 struct skb_shared_info *info = skb_shinfo(skb);
6409 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006410 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006411 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412
6413 entry = tp->cur_tx;
6414 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006415 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 dma_addr_t mapping;
6417 u32 status, len;
6418 void *addr;
6419
6420 entry = (entry + 1) % NUM_TX_DESC;
6421
6422 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00006423 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00006424 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006425 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006426 if (unlikely(dma_mapping_error(d, mapping))) {
6427 if (net_ratelimit())
6428 netif_err(tp, drv, tp->dev,
6429 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006430 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432
Francois Romieucecb5fd2011-04-01 10:21:07 +02006433 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006434 status = opts[0] | len |
6435 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436
6437 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07006438 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439 txd->addr = cpu_to_le64(mapping);
6440
6441 tp->tx_skb[entry].len = len;
6442 }
6443
6444 if (cur_frag) {
6445 tp->tx_skb[entry].skb = skb;
6446 txd->opts1 |= cpu_to_le32(LastFrag);
6447 }
6448
6449 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006450
6451err_out:
6452 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6453 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454}
6455
françois romieub423e9a2013-05-18 01:24:46 +00006456static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6457{
6458 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6459}
6460
hayeswange9746042014-07-11 16:25:58 +08006461static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6462 struct net_device *dev);
6463/* r8169_csum_workaround()
6464 * The hw limites the value the transport offset. When the offset is out of the
6465 * range, calculate the checksum by sw.
6466 */
6467static void r8169_csum_workaround(struct rtl8169_private *tp,
6468 struct sk_buff *skb)
6469{
6470 if (skb_shinfo(skb)->gso_size) {
6471 netdev_features_t features = tp->dev->features;
6472 struct sk_buff *segs, *nskb;
6473
6474 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6475 segs = skb_gso_segment(skb, features);
6476 if (IS_ERR(segs) || !segs)
6477 goto drop;
6478
6479 do {
6480 nskb = segs;
6481 segs = segs->next;
6482 nskb->next = NULL;
6483 rtl8169_start_xmit(nskb, tp->dev);
6484 } while (segs);
6485
Alexander Duyckeb781392015-05-01 10:34:44 -07006486 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006487 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6488 if (skb_checksum_help(skb) < 0)
6489 goto drop;
6490
6491 rtl8169_start_xmit(skb, tp->dev);
6492 } else {
6493 struct net_device_stats *stats;
6494
6495drop:
6496 stats = &tp->dev->stats;
6497 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07006498 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006499 }
6500}
6501
6502/* msdn_giant_send_check()
6503 * According to the document of microsoft, the TCP Pseudo Header excludes the
6504 * packet length for IPv6 TCP large packets.
6505 */
6506static int msdn_giant_send_check(struct sk_buff *skb)
6507{
6508 const struct ipv6hdr *ipv6h;
6509 struct tcphdr *th;
6510 int ret;
6511
6512 ret = skb_cow_head(skb, 0);
6513 if (ret)
6514 return ret;
6515
6516 ipv6h = ipv6_hdr(skb);
6517 th = tcp_hdr(skb);
6518
6519 th->check = 0;
6520 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6521
6522 return ret;
6523}
6524
hayeswang5888d3f2014-07-11 16:25:56 +08006525static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6526 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527{
Michał Mirosław350fb322011-04-08 06:35:56 +00006528 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529
Francois Romieu2b7b4312011-04-18 22:53:24 -07006530 if (mss) {
6531 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08006532 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6533 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6534 const struct iphdr *ip = ip_hdr(skb);
6535
6536 if (ip->protocol == IPPROTO_TCP)
6537 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6538 else if (ip->protocol == IPPROTO_UDP)
6539 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6540 else
6541 WARN_ON_ONCE(1);
6542 }
6543
6544 return true;
6545}
6546
6547static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6548 struct sk_buff *skb, u32 *opts)
6549{
hayeswangbdfa4ed2014-07-11 16:25:57 +08006550 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08006551 u32 mss = skb_shinfo(skb)->gso_size;
6552
6553 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08006554 if (transport_offset > GTTCPHO_MAX) {
6555 netif_warn(tp, tx_err, tp->dev,
6556 "Invalid transport offset 0x%x for TSO\n",
6557 transport_offset);
6558 return false;
6559 }
6560
Heiner Kallweit4ff36462018-05-02 21:40:02 +02006561 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08006562 case htons(ETH_P_IP):
6563 opts[0] |= TD1_GTSENV4;
6564 break;
6565
6566 case htons(ETH_P_IPV6):
6567 if (msdn_giant_send_check(skb))
6568 return false;
6569
6570 opts[0] |= TD1_GTSENV6;
6571 break;
6572
6573 default:
6574 WARN_ON_ONCE(1);
6575 break;
6576 }
6577
hayeswangbdfa4ed2014-07-11 16:25:57 +08006578 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08006579 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006580 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08006581 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582
françois romieub423e9a2013-05-18 01:24:46 +00006583 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006584 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00006585
hayeswange9746042014-07-11 16:25:58 +08006586 if (transport_offset > TCPHO_MAX) {
6587 netif_warn(tp, tx_err, tp->dev,
6588 "Invalid transport offset 0x%x\n",
6589 transport_offset);
6590 return false;
6591 }
6592
Heiner Kallweit4ff36462018-05-02 21:40:02 +02006593 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08006594 case htons(ETH_P_IP):
6595 opts[1] |= TD1_IPv4_CS;
6596 ip_protocol = ip_hdr(skb)->protocol;
6597 break;
6598
6599 case htons(ETH_P_IPV6):
6600 opts[1] |= TD1_IPv6_CS;
6601 ip_protocol = ipv6_hdr(skb)->nexthdr;
6602 break;
6603
6604 default:
6605 ip_protocol = IPPROTO_RAW;
6606 break;
6607 }
6608
6609 if (ip_protocol == IPPROTO_TCP)
6610 opts[1] |= TD1_TCP_CS;
6611 else if (ip_protocol == IPPROTO_UDP)
6612 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006613 else
6614 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08006615
6616 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00006617 } else {
6618 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006619 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620 }
hayeswang5888d3f2014-07-11 16:25:56 +08006621
françois romieub423e9a2013-05-18 01:24:46 +00006622 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623}
6624
Stephen Hemminger613573252009-08-31 19:50:58 +00006625static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6626 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627{
6628 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006629 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006631 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 dma_addr_t mapping;
6633 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006634 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006635 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02006636
Julien Ducourthial477206a2012-05-09 00:00:06 +02006637 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006638 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006639 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640 }
6641
6642 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006643 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644
françois romieub423e9a2013-05-18 01:24:46 +00006645 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6646 opts[0] = DescOwn;
6647
hayeswange9746042014-07-11 16:25:58 +08006648 if (!tp->tso_csum(tp, skb, opts)) {
6649 r8169_csum_workaround(tp, skb);
6650 return NETDEV_TX_OK;
6651 }
françois romieub423e9a2013-05-18 01:24:46 +00006652
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006653 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006654 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006655 if (unlikely(dma_mapping_error(d, mapping))) {
6656 if (net_ratelimit())
6657 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006658 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660
6661 tp->tx_skb[entry].len = len;
6662 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663
Francois Romieu2b7b4312011-04-18 22:53:24 -07006664 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006665 if (frags < 0)
6666 goto err_dma_1;
6667 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07006668 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006669 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07006670 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006671 tp->tx_skb[entry].skb = skb;
6672 }
6673
Francois Romieu2b7b4312011-04-18 22:53:24 -07006674 txd->opts2 = cpu_to_le32(opts[1]);
6675
Richard Cochran5047fb52012-03-10 07:29:42 +00006676 skb_tx_timestamp(skb);
6677
Alexander Duycka0750132014-12-11 15:02:17 -08006678 /* Force memory writes to complete before releasing descriptor */
6679 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680
Francois Romieucecb5fd2011-04-01 10:21:07 +02006681 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006682 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683 txd->opts1 = cpu_to_le32(status);
6684
Alexander Duycka0750132014-12-11 15:02:17 -08006685 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00006686 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687
Alexander Duycka0750132014-12-11 15:02:17 -08006688 tp->cur_tx += frags + 1;
6689
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006690 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691
David S. Miller87cda7c2015-02-22 15:54:29 -05006692 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01006693
David S. Miller87cda7c2015-02-22 15:54:29 -05006694 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01006695 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6696 * not miss a ring update when it notices a stopped queue.
6697 */
6698 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01006700 /* Sync with rtl_tx:
6701 * - publish queue status and cur_tx ring index (write barrier)
6702 * - refresh dirty_tx ring index (read barrier).
6703 * May the current thread have a pessimistic view of the ring
6704 * status and forget to wake up queue, a racing rtl_tx thread
6705 * can't.
6706 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006707 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02006708 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 netif_wake_queue(dev);
6710 }
6711
Stephen Hemminger613573252009-08-31 19:50:58 +00006712 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006714err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006715 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006716err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006717 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006718 dev->stats.tx_dropped++;
6719 return NETDEV_TX_OK;
6720
6721err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006723 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00006724 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725}
6726
6727static void rtl8169_pcierr_interrupt(struct net_device *dev)
6728{
6729 struct rtl8169_private *tp = netdev_priv(dev);
6730 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 u16 pci_status, pci_cmd;
6732
6733 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6734 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6735
Joe Perchesbf82c182010-02-09 11:49:50 +00006736 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6737 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738
6739 /*
6740 * The recovery sequence below admits a very elaborated explanation:
6741 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01006742 * - I did not see what else could be done;
6743 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 *
6745 * Feel free to adjust to your needs.
6746 */
Francois Romieua27993f2006-12-18 00:04:19 +01006747 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01006748 pci_cmd &= ~PCI_COMMAND_PARITY;
6749 else
6750 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6751
6752 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753
6754 pci_write_config_word(pdev, PCI_STATUS,
6755 pci_status & (PCI_STATUS_DETECTED_PARITY |
6756 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6757 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6758
6759 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00006760 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006761 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 tp->cp_cmd &= ~PCIDAC;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006763 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 }
6766
françois romieue6de30d2011-01-03 15:08:37 +00006767 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01006768
Francois Romieu98ddf982012-01-31 10:47:34 +01006769 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770}
6771
Francois Romieuda78dbf2012-01-26 14:18:23 +01006772static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773{
6774 unsigned int dirty_tx, tx_left;
6775
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 dirty_tx = tp->dirty_tx;
6777 smp_rmb();
6778 tx_left = tp->cur_tx - dirty_tx;
6779
6780 while (tx_left > 0) {
6781 unsigned int entry = dirty_tx % NUM_TX_DESC;
6782 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 u32 status;
6784
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6786 if (status & DescOwn)
6787 break;
6788
Alexander Duycka0750132014-12-11 15:02:17 -08006789 /* This barrier is needed to keep us from reading
6790 * any other fields out of the Tx descriptor until
6791 * we know the status of DescOwn
6792 */
6793 dma_rmb();
6794
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006795 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006796 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05006798 u64_stats_update_begin(&tp->tx_stats.syncp);
6799 tp->tx_stats.packets++;
6800 tp->tx_stats.bytes += tx_skb->skb->len;
6801 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006802 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803 tx_skb->skb = NULL;
6804 }
6805 dirty_tx++;
6806 tx_left--;
6807 }
6808
6809 if (tp->dirty_tx != dirty_tx) {
6810 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01006811 /* Sync with rtl8169_start_xmit:
6812 * - publish dirty_tx ring index (write barrier)
6813 * - refresh cur_tx ring index and queue status (read barrier)
6814 * May the current thread miss the stopped queue condition,
6815 * a racing xmit thread can only have a right view of the
6816 * ring status.
6817 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006818 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02006820 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821 netif_wake_queue(dev);
6822 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02006823 /*
6824 * 8168 hack: TxPoll requests are lost when the Tx packets are
6825 * too close. Let's kick an extra TxPoll request when a burst
6826 * of start_xmit activity is detected (if it is not detected,
6827 * it is slow enough). -- FR
6828 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006829 if (tp->cur_tx != dirty_tx)
6830 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 }
6832}
6833
Francois Romieu126fa4b2005-05-12 20:09:17 -04006834static inline int rtl8169_fragmented_frame(u32 status)
6835{
6836 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6837}
6838
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006839static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841 u32 status = opts1 & RxProtoMask;
6842
6843 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00006844 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 skb->ip_summed = CHECKSUM_UNNECESSARY;
6846 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006847 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848}
6849
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006850static struct sk_buff *rtl8169_try_rx_copy(void *data,
6851 struct rtl8169_private *tp,
6852 int pkt_size,
6853 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02006855 struct sk_buff *skb;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006856 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006858 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006859 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006860 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08006861 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006862 if (skb)
Heiner Kallweit8a67aa82018-04-17 23:19:07 +02006863 skb_copy_to_linear_data(skb, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006864 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6865
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006866 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867}
6868
Francois Romieuda78dbf2012-01-26 14:18:23 +01006869static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870{
6871 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006872 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875
Timo Teräs9fba0812013-01-15 21:01:24 +00006876 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006878 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 u32 status;
6880
Heiner Kallweit62028062018-04-17 23:30:29 +02006881 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882 if (status & DescOwn)
6883 break;
Alexander Duycka0750132014-12-11 15:02:17 -08006884
6885 /* This barrier is needed to keep us from reading
6886 * any other fields out of the Rx descriptor until
6887 * we know the status of DescOwn
6888 */
6889 dma_rmb();
6890
Richard Dawe4dcb7d32005-05-27 21:12:00 +02006891 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006892 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6893 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006894 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02006896 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02006898 dev->stats.rx_crc_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006899 /* RxFOVF is a reserved bit on later chip versions */
6900 if (tp->mac_version == RTL_GIGA_MAC_VER_01 &&
6901 status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006902 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006903 dev->stats.rx_fifo_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006904 } else if (status & (RxRUNT | RxCRC) &&
6905 !(status & RxRWT) &&
6906 dev->features & NETIF_F_RXALL) {
Ben Greear6bbe0212012-02-10 15:04:33 +00006907 goto process_pkt;
Heiner Kallweit62028062018-04-17 23:30:29 +02006908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006910 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00006911 dma_addr_t addr;
6912 int pkt_size;
6913
6914process_pkt:
6915 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00006916 if (likely(!(dev->features & NETIF_F_RXFCS)))
6917 pkt_size = (status & 0x00003fff) - 4;
6918 else
6919 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920
Francois Romieu126fa4b2005-05-12 20:09:17 -04006921 /*
6922 * The driver does not support incoming fragmented
6923 * frames. They are seen as a symptom of over-mtu
6924 * sized frames.
6925 */
6926 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02006927 dev->stats.rx_dropped++;
6928 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00006929 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006930 }
6931
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006932 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6933 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006934 if (!skb) {
6935 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00006936 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937 }
6938
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006939 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940 skb_put(skb, pkt_size);
6941 skb->protocol = eth_type_trans(skb, dev);
6942
Francois Romieu7a8fc772011-03-01 17:18:33 +01006943 rtl8169_rx_vlan_tag(desc, skb);
6944
françois romieu39174292015-11-11 23:35:18 +01006945 if (skb->pkt_type == PACKET_MULTICAST)
6946 dev->stats.multicast++;
6947
Francois Romieu56de4142011-03-15 17:29:31 +01006948 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006949
Junchang Wang8027aa22012-03-04 23:30:32 +01006950 u64_stats_update_begin(&tp->rx_stats.syncp);
6951 tp->rx_stats.packets++;
6952 tp->rx_stats.bytes += pkt_size;
6953 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954 }
françois romieuce11ff52013-01-24 13:30:06 +00006955release_descriptor:
6956 desc->opts2 = 0;
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006957 rtl8169_mark_to_asic(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 }
6959
6960 count = cur_rx - tp->cur_rx;
6961 tp->cur_rx = cur_rx;
6962
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963 return count;
6964}
6965
Francois Romieu07d3f512007-02-21 22:40:46 +01006966static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967{
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006968 struct rtl8169_private *tp = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006970 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006972 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006973 if (status && status != 0xffff) {
6974 status &= RTL_EVENT_NAPI | tp->event_slow;
6975 if (status) {
6976 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00006977
Francois Romieuda78dbf2012-01-26 14:18:23 +01006978 rtl_irq_disable(tp);
Heiner Kallweit9a899a32018-04-17 23:21:01 +02006979 napi_schedule_irqoff(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982 return IRQ_RETVAL(handled);
6983}
6984
Francois Romieuda78dbf2012-01-26 14:18:23 +01006985/*
6986 * Workqueue context.
6987 */
6988static void rtl_slow_event_work(struct rtl8169_private *tp)
6989{
6990 struct net_device *dev = tp->dev;
6991 u16 status;
6992
6993 status = rtl_get_events(tp) & tp->event_slow;
6994 rtl_ack_events(tp, status);
6995
6996 if (unlikely(status & RxFIFOOver)) {
6997 switch (tp->mac_version) {
6998 /* Work around for rx fifo overflow */
6999 case RTL_GIGA_MAC_VER_11:
7000 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01007001 /* XXX - Hack alert. See rtl_task(). */
7002 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007003 default:
7004 break;
7005 }
7006 }
7007
7008 if (unlikely(status & SYSErr))
7009 rtl8169_pcierr_interrupt(dev);
7010
7011 if (status & LinkChg)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007012 rtl8169_check_link_status(dev, tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007013
françois romieu7dbb4912012-06-09 10:53:16 +00007014 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007015}
7016
Francois Romieu4422bcd2012-01-26 11:23:32 +01007017static void rtl_task(struct work_struct *work)
7018{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007019 static const struct {
7020 int bitnr;
7021 void (*action)(struct rtl8169_private *);
7022 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01007023 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007024 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7025 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7026 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7027 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01007028 struct rtl8169_private *tp =
7029 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007030 struct net_device *dev = tp->dev;
7031 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01007032
Francois Romieuda78dbf2012-01-26 14:18:23 +01007033 rtl_lock_work(tp);
7034
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007035 if (!netif_running(dev) ||
7036 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01007037 goto out_unlock;
7038
7039 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7040 bool pending;
7041
Francois Romieuda78dbf2012-01-26 14:18:23 +01007042 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007043 if (pending)
7044 rtl_work[i].action(tp);
7045 }
7046
7047out_unlock:
7048 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01007049}
7050
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007051static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007053 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7054 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007055 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7056 int work_done= 0;
7057 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058
Francois Romieuda78dbf2012-01-26 14:18:23 +01007059 status = rtl_get_events(tp);
7060 rtl_ack_events(tp, status & ~tp->event_slow);
7061
7062 if (status & RTL_EVENT_NAPI_RX)
7063 work_done = rtl_rx(dev, tp, (u32) budget);
7064
7065 if (status & RTL_EVENT_NAPI_TX)
7066 rtl_tx(dev, tp);
7067
7068 if (status & tp->event_slow) {
7069 enable_mask &= ~tp->event_slow;
7070
7071 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007074 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08007075 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00007076
Francois Romieuda78dbf2012-01-26 14:18:23 +01007077 rtl_irq_enable(tp, enable_mask);
7078 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079 }
7080
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007081 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007084static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02007085{
7086 struct rtl8169_private *tp = netdev_priv(dev);
7087
7088 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7089 return;
7090
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007091 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
7092 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02007093}
7094
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095static void rtl8169_down(struct net_device *dev)
7096{
7097 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098
Francois Romieu4876cc12011-03-11 21:07:11 +01007099 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007101 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007102 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103
Hayes Wang92fc43b2011-07-06 15:58:03 +08007104 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007105 /*
7106 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007107 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7108 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007109 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007110 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007113 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115 rtl8169_tx_clear(tp);
7116
7117 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007118
7119 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120}
7121
7122static int rtl8169_close(struct net_device *dev)
7123{
7124 struct rtl8169_private *tp = netdev_priv(dev);
7125 struct pci_dev *pdev = tp->pci_dev;
7126
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007127 pm_runtime_get_sync(&pdev->dev);
7128
Francois Romieucecb5fd2011-04-01 10:21:07 +02007129 /* Update counters before going down */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007130 rtl8169_update_counters(tp);
Ivan Vecera355423d2009-02-06 21:49:57 -08007131
Francois Romieuda78dbf2012-01-26 14:18:23 +01007132 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007133 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007134
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007136 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137
Lekensteyn4ea72442013-07-22 09:53:30 +02007138 cancel_work_sync(&tp->wk.work);
7139
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02007140 pci_free_irq(pdev, 0, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007142 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7143 tp->RxPhyAddr);
7144 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7145 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 tp->TxDescArray = NULL;
7147 tp->RxDescArray = NULL;
7148
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007149 pm_runtime_put_sync(&pdev->dev);
7150
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151 return 0;
7152}
7153
Francois Romieudc1c00c2012-03-08 10:06:18 +01007154#ifdef CONFIG_NET_POLL_CONTROLLER
7155static void rtl8169_netpoll(struct net_device *dev)
7156{
7157 struct rtl8169_private *tp = netdev_priv(dev);
7158
Ville Syrjälä6d8b8342018-06-20 15:01:53 +03007159 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
Francois Romieudc1c00c2012-03-08 10:06:18 +01007160}
7161#endif
7162
Francois Romieudf43ac72012-03-08 09:48:40 +01007163static int rtl_open(struct net_device *dev)
7164{
7165 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007166 struct pci_dev *pdev = tp->pci_dev;
7167 int retval = -ENOMEM;
7168
7169 pm_runtime_get_sync(&pdev->dev);
7170
7171 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007172 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007173 * dma_alloc_coherent provides more.
7174 */
7175 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7176 &tp->TxPhyAddr, GFP_KERNEL);
7177 if (!tp->TxDescArray)
7178 goto err_pm_runtime_put;
7179
7180 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7181 &tp->RxPhyAddr, GFP_KERNEL);
7182 if (!tp->RxDescArray)
7183 goto err_free_tx_0;
7184
Heiner Kallweitb1127e62018-04-17 23:23:35 +02007185 retval = rtl8169_init_ring(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007186 if (retval < 0)
7187 goto err_free_rx_1;
7188
7189 INIT_WORK(&tp->wk.work, rtl_task);
7190
7191 smp_mb();
7192
7193 rtl_request_firmware(tp);
7194
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02007195 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007196 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01007197 if (retval < 0)
7198 goto err_release_fw_2;
7199
7200 rtl_lock_work(tp);
7201
7202 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7203
7204 napi_enable(&tp->napi);
7205
7206 rtl8169_init_phy(dev, tp);
7207
Francois Romieudf43ac72012-03-08 09:48:40 +01007208 rtl_pll_power_up(tp);
7209
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007210 rtl_hw_start(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007211
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007212 if (!rtl8169_init_counter_offsets(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007213 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7214
Francois Romieudf43ac72012-03-08 09:48:40 +01007215 netif_start_queue(dev);
7216
7217 rtl_unlock_work(tp);
7218
Heiner Kallweita92a0842018-01-08 21:39:13 +01007219 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007220
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007221 rtl8169_check_link_status(dev, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007222out:
7223 return retval;
7224
7225err_release_fw_2:
7226 rtl_release_firmware(tp);
7227 rtl8169_rx_clear(tp);
7228err_free_rx_1:
7229 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7230 tp->RxPhyAddr);
7231 tp->RxDescArray = NULL;
7232err_free_tx_0:
7233 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7234 tp->TxPhyAddr);
7235 tp->TxDescArray = NULL;
7236err_pm_runtime_put:
7237 pm_runtime_put_noidle(&pdev->dev);
7238 goto out;
7239}
7240
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007241static void
Junchang Wang8027aa22012-03-04 23:30:32 +01007242rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243{
7244 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007245 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02007246 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01007247 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007249 pm_runtime_get_noresume(&pdev->dev);
7250
7251 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007252 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007253
Junchang Wang8027aa22012-03-04 23:30:32 +01007254 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007255 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007256 stats->rx_packets = tp->rx_stats.packets;
7257 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007258 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007259
Junchang Wang8027aa22012-03-04 23:30:32 +01007260 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007261 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007262 stats->tx_packets = tp->tx_stats.packets;
7263 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007264 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007265
7266 stats->rx_dropped = dev->stats.rx_dropped;
7267 stats->tx_dropped = dev->stats.tx_dropped;
7268 stats->rx_length_errors = dev->stats.rx_length_errors;
7269 stats->rx_errors = dev->stats.rx_errors;
7270 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7271 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7272 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02007273 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01007274
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007275 /*
7276 * Fetch additonal counter values missing in stats collected by driver
7277 * from tally counters.
7278 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007279 if (pm_runtime_active(&pdev->dev))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007280 rtl8169_update_counters(tp);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007281
7282 /*
7283 * Subtract values fetched during initalization.
7284 * See rtl8169_init_counter_offsets for a description why we do that.
7285 */
Corinna Vinschen42020322015-09-10 10:47:35 +02007286 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007287 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02007288 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007289 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02007290 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007291 le16_to_cpu(tp->tc_offset.tx_aborted);
7292
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007293 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294}
7295
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007296static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01007297{
françois romieu065c27c2011-01-03 15:08:12 +00007298 struct rtl8169_private *tp = netdev_priv(dev);
7299
Francois Romieu5d06a992006-02-23 00:47:58 +01007300 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007301 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01007302
7303 netif_device_detach(dev);
7304 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007305
7306 rtl_lock_work(tp);
7307 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007308 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007309 rtl_unlock_work(tp);
7310
7311 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007312}
Francois Romieu5d06a992006-02-23 00:47:58 +01007313
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007314#ifdef CONFIG_PM
7315
7316static int rtl8169_suspend(struct device *device)
7317{
7318 struct pci_dev *pdev = to_pci_dev(device);
7319 struct net_device *dev = pci_get_drvdata(pdev);
7320
7321 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02007322
Francois Romieu5d06a992006-02-23 00:47:58 +01007323 return 0;
7324}
7325
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007326static void __rtl8169_resume(struct net_device *dev)
7327{
françois romieu065c27c2011-01-03 15:08:12 +00007328 struct rtl8169_private *tp = netdev_priv(dev);
7329
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007330 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00007331
7332 rtl_pll_power_up(tp);
Heiner Kallweit92bad852018-06-24 18:37:36 +02007333 rtl8169_init_phy(dev, tp);
françois romieu065c27c2011-01-03 15:08:12 +00007334
Artem Savkovcff4c162012-04-03 10:29:11 +00007335 rtl_lock_work(tp);
7336 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007337 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00007338 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007339
Francois Romieu98ddf982012-01-31 10:47:34 +01007340 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007341}
7342
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007343static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01007344{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007345 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01007346 struct net_device *dev = pci_get_drvdata(pdev);
7347
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007348 if (netif_running(dev))
7349 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01007350
Francois Romieu5d06a992006-02-23 00:47:58 +01007351 return 0;
7352}
7353
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007354static int rtl8169_runtime_suspend(struct device *device)
7355{
7356 struct pci_dev *pdev = to_pci_dev(device);
7357 struct net_device *dev = pci_get_drvdata(pdev);
7358 struct rtl8169_private *tp = netdev_priv(dev);
7359
Heiner Kallweita92a0842018-01-08 21:39:13 +01007360 if (!tp->TxDescArray) {
7361 rtl_pll_power_down(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007362 return 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007363 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007364
Francois Romieuda78dbf2012-01-26 14:18:23 +01007365 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007366 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007367 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007368
7369 rtl8169_net_suspend(dev);
7370
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007371 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007372 rtl8169_rx_missed(dev);
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007373 rtl8169_update_counters(tp);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007374
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007375 return 0;
7376}
7377
7378static int rtl8169_runtime_resume(struct device *device)
7379{
7380 struct pci_dev *pdev = to_pci_dev(device);
7381 struct net_device *dev = pci_get_drvdata(pdev);
7382 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08007383 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007384
7385 if (!tp->TxDescArray)
7386 return 0;
7387
Francois Romieuda78dbf2012-01-26 14:18:23 +01007388 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007389 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007390 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007391
7392 __rtl8169_resume(dev);
7393
7394 return 0;
7395}
7396
7397static int rtl8169_runtime_idle(struct device *device)
7398{
7399 struct pci_dev *pdev = to_pci_dev(device);
7400 struct net_device *dev = pci_get_drvdata(pdev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007401
Heiner Kallweita92a0842018-01-08 21:39:13 +01007402 if (!netif_running(dev) || !netif_carrier_ok(dev))
7403 pm_schedule_suspend(device, 10000);
7404
7405 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007406}
7407
Alexey Dobriyan47145212009-12-14 18:00:08 -08007408static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02007409 .suspend = rtl8169_suspend,
7410 .resume = rtl8169_resume,
7411 .freeze = rtl8169_suspend,
7412 .thaw = rtl8169_resume,
7413 .poweroff = rtl8169_suspend,
7414 .restore = rtl8169_resume,
7415 .runtime_suspend = rtl8169_runtime_suspend,
7416 .runtime_resume = rtl8169_runtime_resume,
7417 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007418};
7419
7420#define RTL8169_PM_OPS (&rtl8169_pm_ops)
7421
7422#else /* !CONFIG_PM */
7423
7424#define RTL8169_PM_OPS NULL
7425
7426#endif /* !CONFIG_PM */
7427
David S. Miller1805b2f2011-10-24 18:18:09 -04007428static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7429{
David S. Miller1805b2f2011-10-24 18:18:09 -04007430 /* WoL fails with 8168b when the receiver is disabled. */
7431 switch (tp->mac_version) {
7432 case RTL_GIGA_MAC_VER_11:
7433 case RTL_GIGA_MAC_VER_12:
7434 case RTL_GIGA_MAC_VER_17:
7435 pci_clear_master(tp->pci_dev);
7436
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007437 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007438 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007439 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04007440 break;
7441 default:
7442 break;
7443 }
7444}
7445
Francois Romieu1765f952008-09-13 17:21:40 +02007446static void rtl_shutdown(struct pci_dev *pdev)
7447{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007448 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00007449 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02007450
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007451 rtl8169_net_suspend(dev);
7452
Francois Romieucecb5fd2011-04-01 10:21:07 +02007453 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08007454 rtl_rar_set(tp, dev->perm_addr);
7455
Hayes Wang92fc43b2011-07-06 15:58:03 +08007456 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00007457
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007458 if (system_state == SYSTEM_POWER_OFF) {
Heiner Kallweit433f9d02018-06-24 18:39:06 +02007459 if (tp->saved_wolopts) {
David S. Miller1805b2f2011-10-24 18:18:09 -04007460 rtl_wol_suspend_quirk(tp);
7461 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00007462 }
7463
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007464 pci_wake_from_d3(pdev, true);
7465 pci_set_power_state(pdev, PCI_D3hot);
7466 }
7467}
Francois Romieu5d06a992006-02-23 00:47:58 +01007468
Bill Pembertonbaf63292012-12-03 09:23:28 -05007469static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01007470{
7471 struct net_device *dev = pci_get_drvdata(pdev);
7472 struct rtl8169_private *tp = netdev_priv(dev);
7473
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007474 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01007475 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01007476
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007477 netif_napi_del(&tp->napi);
7478
Francois Romieue27566e2012-03-08 09:54:01 +01007479 unregister_netdev(dev);
7480
7481 rtl_release_firmware(tp);
7482
7483 if (pci_dev_run_wake(pdev))
7484 pm_runtime_get_noresume(&pdev->dev);
7485
7486 /* restore original MAC address */
7487 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01007488}
7489
Francois Romieufa9c3852012-03-08 10:01:50 +01007490static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01007491 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01007492 .ndo_stop = rtl8169_close,
7493 .ndo_get_stats64 = rtl8169_get_stats64,
7494 .ndo_start_xmit = rtl8169_start_xmit,
7495 .ndo_tx_timeout = rtl8169_tx_timeout,
7496 .ndo_validate_addr = eth_validate_addr,
7497 .ndo_change_mtu = rtl8169_change_mtu,
7498 .ndo_fix_features = rtl8169_fix_features,
7499 .ndo_set_features = rtl8169_set_features,
7500 .ndo_set_mac_address = rtl_set_mac_address,
7501 .ndo_do_ioctl = rtl8169_ioctl,
7502 .ndo_set_rx_mode = rtl_set_rx_mode,
7503#ifdef CONFIG_NET_POLL_CONTROLLER
7504 .ndo_poll_controller = rtl8169_netpoll,
7505#endif
7506
7507};
7508
Francois Romieu31fa8b12012-03-08 10:09:40 +01007509static const struct rtl_cfg_info {
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007510 void (*hw_start)(struct rtl8169_private *tp);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007511 u16 event_slow;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007512 unsigned int has_gmii:1;
Francois Romieu50970832017-10-27 13:24:49 +03007513 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007514 u8 default_ver;
7515} rtl_cfg_infos [] = {
7516 [RTL_CFG_0] = {
7517 .hw_start = rtl_hw_start_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007518 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007519 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007520 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007521 .default_ver = RTL_GIGA_MAC_VER_01,
7522 },
7523 [RTL_CFG_1] = {
7524 .hw_start = rtl_hw_start_8168,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007525 .event_slow = SYSErr | LinkChg | RxOverflow,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007526 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007527 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007528 .default_ver = RTL_GIGA_MAC_VER_11,
7529 },
7530 [RTL_CFG_2] = {
7531 .hw_start = rtl_hw_start_8101,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007532 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
7533 PCSTimeout,
Francois Romieu50970832017-10-27 13:24:49 +03007534 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007535 .default_ver = RTL_GIGA_MAC_VER_13,
7536 }
7537};
7538
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007539static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01007540{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007541 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007542
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007543 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007544 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
7545 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
7546 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007547 flags = PCI_IRQ_LEGACY;
7548 } else {
7549 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007550 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007551
7552 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007553}
7554
Hayes Wangc5583862012-07-02 17:23:22 +08007555DECLARE_RTL_COND(rtl_link_list_ready_cond)
7556{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007557 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08007558}
7559
7560DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7561{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007562 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08007563}
7564
Bill Pembertonbaf63292012-12-03 09:23:28 -05007565static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007566{
Hayes Wangc5583862012-07-02 17:23:22 +08007567 u32 data;
7568
7569 tp->ocp_base = OCP_STD_PHY_BASE;
7570
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007571 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08007572
7573 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7574 return;
7575
7576 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7577 return;
7578
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007579 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08007580 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007581 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08007582
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007583 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007584 data &= ~(1 << 14);
7585 r8168_mac_ocp_write(tp, 0xe8de, data);
7586
7587 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7588 return;
7589
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007590 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007591 data |= (1 << 15);
7592 r8168_mac_ocp_write(tp, 0xe8de, data);
7593
7594 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7595 return;
7596}
7597
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007598static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
7599{
7600 rtl8168ep_stop_cmac(tp);
7601 rtl_hw_init_8168g(tp);
7602}
7603
Bill Pembertonbaf63292012-12-03 09:23:28 -05007604static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007605{
7606 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02007607 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007608 rtl_hw_init_8168g(tp);
7609 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02007610 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007611 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08007612 break;
Hayes Wangc5583862012-07-02 17:23:22 +08007613 default:
7614 break;
7615 }
7616}
7617
hayeswang929a0312014-09-16 11:40:47 +08007618static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01007619{
7620 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007621 struct rtl8169_private *tp;
7622 struct mii_if_info *mii;
7623 struct net_device *dev;
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007624 int chipset, region, i;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007625 int rc;
7626
7627 if (netif_msg_drv(&debug)) {
7628 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
7629 MODULENAME, RTL8169_VERSION);
7630 }
7631
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007632 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
7633 if (!dev)
7634 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007635
7636 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01007637 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007638 tp = netdev_priv(dev);
7639 tp->dev = dev;
7640 tp->pci_dev = pdev;
7641 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
7642
7643 mii = &tp->mii;
7644 mii->dev = dev;
7645 mii->mdio_read = rtl_mdio_read;
7646 mii->mdio_write = rtl_mdio_write;
7647 mii->phy_id_mask = 0x1f;
7648 mii->reg_num_mask = 0x1f;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007649 mii->supports_gmii = cfg->has_gmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007650
Francois Romieu3b6cf252012-03-08 09:59:04 +01007651 /* 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);