blob: d5e380d909dddf6731375f80f4fe1e4f3fb5c044 [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
1590static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1591{
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001592 u8 options;
1593 u32 wolopts = 0;
1594
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001595 options = RTL_R8(tp, Config1);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001596 if (!(options & PMEnable))
1597 return 0;
1598
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001599 options = RTL_R8(tp, Config3);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001600 if (options & LinkUp)
1601 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001602 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001603 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1604 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001605 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1606 wolopts |= WAKE_MAGIC;
1607 break;
1608 default:
1609 if (options & MagicPacket)
1610 wolopts |= WAKE_MAGIC;
1611 break;
1612 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001613
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001614 options = RTL_R8(tp, Config5);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001615 if (options & UWF)
1616 wolopts |= WAKE_UCAST;
1617 if (options & BWF)
1618 wolopts |= WAKE_BCAST;
1619 if (options & MWF)
1620 wolopts |= WAKE_MCAST;
1621
1622 return wolopts;
1623}
1624
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001625static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1626{
1627 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001628 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001629
1630 pm_runtime_get_noresume(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001631
Francois Romieuda78dbf2012-01-26 14:18:23 +01001632 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001633
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001634 wol->supported = WAKE_ANY;
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001635 if (pm_runtime_active(d))
1636 wol->wolopts = __rtl8169_get_wol(tp);
1637 else
1638 wol->wolopts = tp->saved_wolopts;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001639
Francois Romieuda78dbf2012-01-26 14:18:23 +01001640 rtl_unlock_work(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001641
1642 pm_runtime_put_noidle(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001643}
1644
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001645static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001646{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001647 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001648 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001649 u32 opt;
1650 u16 reg;
1651 u8 mask;
1652 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001653 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001654 { WAKE_UCAST, Config5, UWF },
1655 { WAKE_BCAST, Config5, BWF },
1656 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001657 { WAKE_ANY, Config5, LanWake },
1658 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001659 };
Francois Romieu851e6022012-04-17 11:10:11 +02001660 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001661
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001662 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001663
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001664 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001665 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1666 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001667 tmp = ARRAY_SIZE(cfg) - 1;
1668 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001669 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001670 0x0dc,
1671 ERIAR_MASK_0100,
1672 MagicPacket_v2,
1673 0x0000,
1674 ERIAR_EXGMAC);
1675 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001676 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001677 0x0dc,
1678 ERIAR_MASK_0100,
1679 0x0000,
1680 MagicPacket_v2,
1681 ERIAR_EXGMAC);
1682 break;
1683 default:
1684 tmp = ARRAY_SIZE(cfg);
1685 break;
1686 }
1687
1688 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001689 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001690 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001691 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001692 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001693 }
1694
Francois Romieu851e6022012-04-17 11:10:11 +02001695 switch (tp->mac_version) {
1696 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001697 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001698 if (wolopts)
1699 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001700 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001701 break;
1702 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001703 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001704 if (wolopts)
1705 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001706 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001707 break;
1708 }
1709
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001710 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001711}
1712
1713static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1714{
1715 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001716 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001717
1718 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001719
Francois Romieuda78dbf2012-01-26 14:18:23 +01001720 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001721
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001722 if (pm_runtime_active(d))
1723 __rtl8169_set_wol(tp, wol->wolopts);
1724 else
1725 tp->saved_wolopts = wol->wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001726
1727 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001728
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001729 device_set_wakeup_enable(d, wol->wolopts);
françois romieuea809072010-11-08 13:23:58 +00001730
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001731 pm_runtime_put_noidle(d);
1732
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001733 return 0;
1734}
1735
Francois Romieu31bd2042011-04-26 18:58:59 +02001736static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1737{
Francois Romieu85bffe62011-04-27 08:22:39 +02001738 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001739}
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741static void rtl8169_get_drvinfo(struct net_device *dev,
1742 struct ethtool_drvinfo *info)
1743{
1744 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001745 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Rick Jones68aad782011-11-07 13:29:27 +00001747 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1748 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1749 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001750 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001751 if (!IS_ERR_OR_NULL(rtl_fw))
1752 strlcpy(info->fw_version, rtl_fw->version,
1753 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
1756static int rtl8169_get_regs_len(struct net_device *dev)
1757{
1758 return R8169_REGS_SIZE;
1759}
1760
1761static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001762 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
1764 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 int ret = 0;
1766 u32 reg;
1767
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001768 reg = RTL_R32(tp, TBICSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1770 (duplex == DUPLEX_FULL)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001771 RTL_W32(tp, TBICSR, reg & ~(TBINwEnable | TBINwRestart));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 } else if (autoneg == AUTONEG_ENABLE)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001773 RTL_W32(tp, TBICSR, reg | TBINwEnable | TBINwRestart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001775 netif_warn(tp, link, dev,
1776 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 ret = -EOPNOTSUPP;
1778 }
1779
1780 return ret;
1781}
1782
1783static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001784 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
1786 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001787 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001788 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Hayes Wang716b50a2011-02-22 17:26:18 +08001790 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001793 int auto_nego;
1794
françois romieu4da19632011-01-03 15:07:55 +00001795 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001796 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1797 ADVERTISE_100HALF | ADVERTISE_100FULL);
1798
1799 if (adv & ADVERTISED_10baseT_Half)
1800 auto_nego |= ADVERTISE_10HALF;
1801 if (adv & ADVERTISED_10baseT_Full)
1802 auto_nego |= ADVERTISE_10FULL;
1803 if (adv & ADVERTISED_100baseT_Half)
1804 auto_nego |= ADVERTISE_100HALF;
1805 if (adv & ADVERTISED_100baseT_Full)
1806 auto_nego |= ADVERTISE_100FULL;
1807
françois romieu3577aa12009-05-19 10:46:48 +00001808 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1809
françois romieu4da19632011-01-03 15:07:55 +00001810 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001811 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1812
1813 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001814 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001815 if (adv & ADVERTISED_1000baseT_Half)
1816 giga_ctrl |= ADVERTISE_1000HALF;
1817 if (adv & ADVERTISED_1000baseT_Full)
1818 giga_ctrl |= ADVERTISE_1000FULL;
1819 } else if (adv & (ADVERTISED_1000baseT_Half |
1820 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001821 netif_info(tp, link, dev,
1822 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001823 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
françois romieu3577aa12009-05-19 10:46:48 +00001826 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001827
françois romieu4da19632011-01-03 15:07:55 +00001828 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1829 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001830 } else {
françois romieu3577aa12009-05-19 10:46:48 +00001831 if (speed == SPEED_10)
1832 bmcr = 0;
1833 else if (speed == SPEED_100)
1834 bmcr = BMCR_SPEED100;
1835 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001836 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001837
1838 if (duplex == DUPLEX_FULL)
1839 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001840 }
1841
françois romieu4da19632011-01-03 15:07:55 +00001842 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001843
Francois Romieucecb5fd2011-04-01 10:21:07 +02001844 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1845 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001846 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001847 rtl_writephy(tp, 0x17, 0x2138);
1848 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001849 } else {
françois romieu4da19632011-01-03 15:07:55 +00001850 rtl_writephy(tp, 0x17, 0x2108);
1851 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001852 }
1853 }
1854
Oliver Neukum54405cd2011-01-06 21:55:13 +01001855 rc = 0;
1856out:
1857 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
1859
1860static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001861 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 struct rtl8169_private *tp = netdev_priv(dev);
1864 int ret;
1865
Oliver Neukum54405cd2011-01-06 21:55:13 +01001866 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001867 if (ret < 0)
1868 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Francois Romieu4876cc12011-03-11 21:07:11 +01001870 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
Chun-Hao Linc4556972016-03-11 14:21:14 +08001871 (advertising & ADVERTISED_1000baseT_Full) &&
1872 !pci_is_pcie(tp->pci_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001874 }
1875out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return ret;
1877}
1878
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001879static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1880 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Francois Romieud58d46b2011-05-03 16:38:29 +02001882 struct rtl8169_private *tp = netdev_priv(dev);
1883
Francois Romieu2b7b4312011-04-18 22:53:24 -07001884 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001885 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Francois Romieud58d46b2011-05-03 16:38:29 +02001887 if (dev->mtu > JUMBO_1K &&
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02001888 tp->mac_version > RTL_GIGA_MAC_VER_06)
Francois Romieud58d46b2011-05-03 16:38:29 +02001889 features &= ~NETIF_F_IP_CSUM;
1890
Michał Mirosław350fb322011-04-08 06:35:56 +00001891 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
1893
Heiner Kallweita3984572018-04-28 22:19:15 +02001894static int rtl8169_set_features(struct net_device *dev,
1895 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
1897 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001898 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Heiner Kallweita3984572018-04-28 22:19:15 +02001900 rtl_lock_work(tp);
1901
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001902 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001903 if (features & NETIF_F_RXALL)
1904 rx_config |= (AcceptErr | AcceptRunt);
1905 else
1906 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001908 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001909
hayeswang929a0312014-09-16 11:40:47 +08001910 if (features & NETIF_F_RXCSUM)
1911 tp->cp_cmd |= RxChkSum;
1912 else
1913 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001914
hayeswang929a0312014-09-16 11:40:47 +08001915 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1916 tp->cp_cmd |= RxVlan;
1917 else
1918 tp->cp_cmd &= ~RxVlan;
1919
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001920 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1921 RTL_R16(tp, CPlusCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Francois Romieuda78dbf2012-01-26 14:18:23 +01001923 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 return 0;
1926}
1927
Kirill Smelkov810f4892012-11-10 21:11:02 +04001928static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001930 return (skb_vlan_tag_present(skb)) ?
1931 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
1933
Francois Romieu7a8fc772011-03-01 17:18:33 +01001934static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
1936 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Francois Romieu7a8fc772011-03-01 17:18:33 +01001938 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001939 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940}
1941
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001942static int rtl8169_get_link_ksettings_tbi(struct net_device *dev,
1943 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
1945 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 u32 status;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001947 u32 supported, advertising;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001949 supported =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001951 cmd->base.port = PORT_FIBRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001953 status = RTL_R32(tp, TBICSR);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001954 advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1955 cmd->base.autoneg = !!(status & TBINwEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001957 cmd->base.speed = SPEED_1000;
1958 cmd->base.duplex = DUPLEX_FULL; /* Always set */
1959
1960 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1961 supported);
1962 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1963 advertising);
Francois Romieuccdffb92008-07-26 14:26:06 +02001964
1965 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001968static int rtl8169_get_link_ksettings_xmii(struct net_device *dev,
1969 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03001973 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
1974
1975 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001978static int rtl8169_get_link_ksettings(struct net_device *dev,
1979 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
1981 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001982 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Francois Romieuda78dbf2012-01-26 14:18:23 +01001984 rtl_lock_work(tp);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001985 rc = tp->get_link_ksettings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001986 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Francois Romieuccdffb92008-07-26 14:26:06 +02001988 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01001991static int rtl8169_set_link_ksettings(struct net_device *dev,
1992 const struct ethtool_link_ksettings *cmd)
1993{
1994 struct rtl8169_private *tp = netdev_priv(dev);
1995 int rc;
1996 u32 advertising;
1997
1998 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
1999 cmd->link_modes.advertising))
2000 return -EINVAL;
2001
2002 del_timer_sync(&tp->timer);
2003
2004 rtl_lock_work(tp);
2005 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
2006 cmd->base.duplex, advertising);
2007 rtl_unlock_work(tp);
2008
2009 return rc;
2010}
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2013 void *p)
2014{
Francois Romieu5b0384f2006-08-16 16:00:01 +02002015 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02002016 u32 __iomem *data = tp->mmio_addr;
2017 u32 *dw = p;
2018 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Francois Romieuda78dbf2012-01-26 14:18:23 +01002020 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02002021 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2022 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002023 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002026static u32 rtl8169_get_msglevel(struct net_device *dev)
2027{
2028 struct rtl8169_private *tp = netdev_priv(dev);
2029
2030 return tp->msg_enable;
2031}
2032
2033static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2034{
2035 struct rtl8169_private *tp = netdev_priv(dev);
2036
2037 tp->msg_enable = value;
2038}
2039
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002040static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2041 "tx_packets",
2042 "rx_packets",
2043 "tx_errors",
2044 "rx_errors",
2045 "rx_missed",
2046 "align_errors",
2047 "tx_single_collisions",
2048 "tx_multi_collisions",
2049 "unicast",
2050 "broadcast",
2051 "multicast",
2052 "tx_aborted",
2053 "tx_underrun",
2054};
2055
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002056static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002057{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002058 switch (sset) {
2059 case ETH_SS_STATS:
2060 return ARRAY_SIZE(rtl8169_gstrings);
2061 default:
2062 return -EOPNOTSUPP;
2063 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002064}
2065
Corinna Vinschen42020322015-09-10 10:47:35 +02002066DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002067{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002068 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002069}
2070
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002071static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002072{
Corinna Vinschen42020322015-09-10 10:47:35 +02002073 dma_addr_t paddr = tp->counters_phys_addr;
2074 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02002075
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002076 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
2077 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02002078 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002079 RTL_W32(tp, CounterAddrLow, cmd);
2080 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02002081
Francois Romieua78e9362018-01-26 01:53:26 +01002082 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002083}
2084
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002085static bool rtl8169_reset_counters(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002086{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002087 /*
2088 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2089 * tally counters.
2090 */
2091 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2092 return true;
2093
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002094 return rtl8169_do_counters(tp, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02002095}
2096
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002097static bool rtl8169_update_counters(struct rtl8169_private *tp)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002098{
Ivan Vecera355423d2009-02-06 21:49:57 -08002099 /*
2100 * Some chips are unable to dump tally counters when the receiver
2101 * is disabled.
2102 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002103 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002104 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002105
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002106 return rtl8169_do_counters(tp, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002107}
2108
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002109static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002110{
Corinna Vinschen42020322015-09-10 10:47:35 +02002111 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002112 bool ret = false;
2113
2114 /*
2115 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2116 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2117 * reset by a power cycle, while the counter values collected by the
2118 * driver are reset at every driver unload/load cycle.
2119 *
2120 * To make sure the HW values returned by @get_stats64 match the SW
2121 * values, we collect the initial values at first open(*) and use them
2122 * as offsets to normalize the values returned by @get_stats64.
2123 *
2124 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2125 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2126 * set at open time by rtl_hw_start.
2127 */
2128
2129 if (tp->tc_offset.inited)
2130 return true;
2131
2132 /* If both, reset and update fail, propagate to caller. */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002133 if (rtl8169_reset_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002134 ret = true;
2135
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002136 if (rtl8169_update_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002137 ret = true;
2138
Corinna Vinschen42020322015-09-10 10:47:35 +02002139 tp->tc_offset.tx_errors = counters->tx_errors;
2140 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2141 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002142 tp->tc_offset.inited = true;
2143
2144 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002145}
2146
Ivan Vecera355423d2009-02-06 21:49:57 -08002147static void rtl8169_get_ethtool_stats(struct net_device *dev,
2148 struct ethtool_stats *stats, u64 *data)
2149{
2150 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01002151 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02002152 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08002153
2154 ASSERT_RTNL();
2155
Chun-Hao Line0636232016-07-29 16:37:55 +08002156 pm_runtime_get_noresume(d);
2157
2158 if (pm_runtime_active(d))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002159 rtl8169_update_counters(tp);
Chun-Hao Line0636232016-07-29 16:37:55 +08002160
2161 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08002162
Corinna Vinschen42020322015-09-10 10:47:35 +02002163 data[0] = le64_to_cpu(counters->tx_packets);
2164 data[1] = le64_to_cpu(counters->rx_packets);
2165 data[2] = le64_to_cpu(counters->tx_errors);
2166 data[3] = le32_to_cpu(counters->rx_errors);
2167 data[4] = le16_to_cpu(counters->rx_missed);
2168 data[5] = le16_to_cpu(counters->align_errors);
2169 data[6] = le32_to_cpu(counters->tx_one_collision);
2170 data[7] = le32_to_cpu(counters->tx_multi_collision);
2171 data[8] = le64_to_cpu(counters->rx_unicast);
2172 data[9] = le64_to_cpu(counters->rx_broadcast);
2173 data[10] = le32_to_cpu(counters->rx_multicast);
2174 data[11] = le16_to_cpu(counters->tx_aborted);
2175 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08002176}
2177
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002178static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2179{
2180 switch(stringset) {
2181 case ETH_SS_STATS:
2182 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2183 break;
2184 }
2185}
2186
Florian Fainellif0903ea2016-12-03 12:01:19 -08002187static int rtl8169_nway_reset(struct net_device *dev)
2188{
2189 struct rtl8169_private *tp = netdev_priv(dev);
2190
2191 return mii_nway_restart(&tp->mii);
2192}
2193
Francois Romieu50970832017-10-27 13:24:49 +03002194/*
2195 * Interrupt coalescing
2196 *
2197 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2198 * > 8169, 8168 and 810x line of chipsets
2199 *
2200 * 8169, 8168, and 8136(810x) serial chipsets support it.
2201 *
2202 * > 2 - the Tx timer unit at gigabit speed
2203 *
2204 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2205 * (0xe0) bit 1 and bit 0.
2206 *
2207 * For 8169
2208 * bit[1:0] \ speed 1000M 100M 10M
2209 * 0 0 320ns 2.56us 40.96us
2210 * 0 1 2.56us 20.48us 327.7us
2211 * 1 0 5.12us 40.96us 655.4us
2212 * 1 1 10.24us 81.92us 1.31ms
2213 *
2214 * For the other
2215 * bit[1:0] \ speed 1000M 100M 10M
2216 * 0 0 5us 2.56us 40.96us
2217 * 0 1 40us 20.48us 327.7us
2218 * 1 0 80us 40.96us 655.4us
2219 * 1 1 160us 81.92us 1.31ms
2220 */
2221
2222/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2223struct rtl_coalesce_scale {
2224 /* Rx / Tx */
2225 u32 nsecs[2];
2226};
2227
2228/* rx/tx scale factors for all CPlusCmd[0:1] cases */
2229struct rtl_coalesce_info {
2230 u32 speed;
2231 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2232};
2233
2234/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2235#define rxtx_x1822(r, t) { \
2236 {{(r), (t)}}, \
2237 {{(r)*8, (t)*8}}, \
2238 {{(r)*8*2, (t)*8*2}}, \
2239 {{(r)*8*2*2, (t)*8*2*2}}, \
2240}
2241static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2242 /* speed delays: rx00 tx00 */
2243 { SPEED_10, rxtx_x1822(40960, 40960) },
2244 { SPEED_100, rxtx_x1822( 2560, 2560) },
2245 { SPEED_1000, rxtx_x1822( 320, 320) },
2246 { 0 },
2247};
2248
2249static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2250 /* speed delays: rx00 tx00 */
2251 { SPEED_10, rxtx_x1822(40960, 40960) },
2252 { SPEED_100, rxtx_x1822( 2560, 2560) },
2253 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2254 { 0 },
2255};
2256#undef rxtx_x1822
2257
2258/* get rx/tx scale vector corresponding to current speed */
2259static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2260{
2261 struct rtl8169_private *tp = netdev_priv(dev);
2262 struct ethtool_link_ksettings ecmd;
2263 const struct rtl_coalesce_info *ci;
2264 int rc;
2265
2266 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2267 if (rc < 0)
2268 return ERR_PTR(rc);
2269
2270 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2271 if (ecmd.base.speed == ci->speed) {
2272 return ci;
2273 }
2274 }
2275
2276 return ERR_PTR(-ELNRNG);
2277}
2278
2279static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2280{
2281 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002282 const struct rtl_coalesce_info *ci;
2283 const struct rtl_coalesce_scale *scale;
2284 struct {
2285 u32 *max_frames;
2286 u32 *usecs;
2287 } coal_settings [] = {
2288 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2289 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2290 }, *p = coal_settings;
2291 int i;
2292 u16 w;
2293
2294 memset(ec, 0, sizeof(*ec));
2295
2296 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2297 ci = rtl_coalesce_info(dev);
2298 if (IS_ERR(ci))
2299 return PTR_ERR(ci);
2300
Heiner Kallweit0ae09742018-04-28 22:19:26 +02002301 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
Francois Romieu50970832017-10-27 13:24:49 +03002302
2303 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002304 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03002305 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2306 w >>= RTL_COALESCE_SHIFT;
2307 *p->usecs = w & RTL_COALESCE_MASK;
2308 }
2309
2310 for (i = 0; i < 2; i++) {
2311 p = coal_settings + i;
2312 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2313
2314 /*
2315 * ethtool_coalesce says it is illegal to set both usecs and
2316 * max_frames to 0.
2317 */
2318 if (!*p->usecs && !*p->max_frames)
2319 *p->max_frames = 1;
2320 }
2321
2322 return 0;
2323}
2324
2325/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2326static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2327 struct net_device *dev, u32 nsec, u16 *cp01)
2328{
2329 const struct rtl_coalesce_info *ci;
2330 u16 i;
2331
2332 ci = rtl_coalesce_info(dev);
2333 if (IS_ERR(ci))
2334 return ERR_CAST(ci);
2335
2336 for (i = 0; i < 4; i++) {
2337 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2338 ci->scalev[i].nsecs[1]);
2339 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2340 *cp01 = i;
2341 return &ci->scalev[i];
2342 }
2343 }
2344
2345 return ERR_PTR(-EINVAL);
2346}
2347
2348static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2349{
2350 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002351 const struct rtl_coalesce_scale *scale;
2352 struct {
2353 u32 frames;
2354 u32 usecs;
2355 } coal_settings [] = {
2356 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2357 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2358 }, *p = coal_settings;
2359 u16 w = 0, cp01;
2360 int i;
2361
2362 scale = rtl_coalesce_choose_scale(dev,
2363 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2364 if (IS_ERR(scale))
2365 return PTR_ERR(scale);
2366
2367 for (i = 0; i < 2; i++, p++) {
2368 u32 units;
2369
2370 /*
2371 * accept max_frames=1 we returned in rtl_get_coalesce.
2372 * accept it not only when usecs=0 because of e.g. the following scenario:
2373 *
2374 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2375 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2376 * - then user does `ethtool -C eth0 rx-usecs 100`
2377 *
2378 * since ethtool sends to kernel whole ethtool_coalesce
2379 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2380 * we'll reject it below in `frames % 4 != 0`.
2381 */
2382 if (p->frames == 1) {
2383 p->frames = 0;
2384 }
2385
2386 units = p->usecs * 1000 / scale->nsecs[i];
2387 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2388 return -EINVAL;
2389
2390 w <<= RTL_COALESCE_SHIFT;
2391 w |= units;
2392 w <<= RTL_COALESCE_SHIFT;
2393 w |= p->frames >> 2;
2394 }
2395
2396 rtl_lock_work(tp);
2397
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002398 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03002399
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +02002400 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002401 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2402 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03002403
2404 rtl_unlock_work(tp);
2405
2406 return 0;
2407}
2408
Jeff Garzik7282d492006-09-13 14:30:00 -04002409static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 .get_drvinfo = rtl8169_get_drvinfo,
2411 .get_regs_len = rtl8169_get_regs_len,
2412 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002413 .get_coalesce = rtl_get_coalesce,
2414 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002415 .get_msglevel = rtl8169_get_msglevel,
2416 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002418 .get_wol = rtl8169_get_wol,
2419 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002420 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002421 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002422 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002423 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainellif0903ea2016-12-03 12:01:19 -08002424 .nway_reset = rtl8169_nway_reset,
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002425 .get_link_ksettings = rtl8169_get_link_ksettings,
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002426 .set_link_ksettings = rtl8169_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427};
2428
Francois Romieu07d3f512007-02-21 22:40:46 +01002429static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Heiner Kallweit22148df2018-04-22 17:15:15 +02002430 u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
Francois Romieu0e485152007-02-20 00:00:26 +01002432 /*
2433 * The driver currently handles the 8168Bf and the 8168Be identically
2434 * but they can be identified more specifically through the test below
2435 * if needed:
2436 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002437 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002438 *
2439 * Same thing for the 8101Eb and the 8101Ec:
2440 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002441 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002442 */
Francois Romieu37441002011-06-17 22:58:54 +02002443 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002445 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 int mac_version;
2447 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002448 /* 8168EP family. */
2449 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2450 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2451 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2452
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002453 /* 8168H family. */
2454 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2455 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2456
Hayes Wangc5583862012-07-02 17:23:22 +08002457 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002458 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002459 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002460 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2461 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2462
Hayes Wangc2218922011-09-06 16:55:18 +08002463 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002464 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002465 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2466 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2467
hayeswang01dc7fe2011-03-21 01:50:28 +00002468 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002469 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002470 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2471 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2472
Francois Romieu5b538df2008-07-20 16:22:45 +02002473 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002474 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002475 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002476
françois romieue6de30d2011-01-03 15:08:37 +00002477 /* 8168DP family. */
2478 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2479 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002480 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002481
Francois Romieuef808d52008-06-29 13:10:54 +02002482 /* 8168C family. */
Francois Romieuef3386f2008-06-29 12:24:30 +02002483 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002484 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002485 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002486 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2487 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002488 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieuef808d52008-06-29 13:10:54 +02002489 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002490
2491 /* 8168B family. */
2492 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002493 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2494 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2495
2496 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002497 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002498 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002499 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2500 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002501 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2502 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2503 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2504 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002505 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002506 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002507 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002508 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2509 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002510 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2511 /* FIXME: where did these entries come from ? -- FR */
2512 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2513 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2514
2515 /* 8110 family. */
2516 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2517 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2518 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2519 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2520 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2521 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2522
Jean Delvaref21b75e2009-05-26 20:54:48 -07002523 /* Catch-all */
2524 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002525 };
2526 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 u32 reg;
2528
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002529 reg = RTL_R32(tp, TxConfig);
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002530 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 p++;
2532 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002533
2534 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02002535 dev_notice(tp_to_dev(tp),
2536 "unknown MAC, using family default\n");
Francois Romieu5d320a22011-05-08 17:47:36 +02002537 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002538 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2539 tp->mac_version = tp->mii.supports_gmii ?
2540 RTL_GIGA_MAC_VER_42 :
2541 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002542 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2543 tp->mac_version = tp->mii.supports_gmii ?
2544 RTL_GIGA_MAC_VER_45 :
2545 RTL_GIGA_MAC_VER_47;
2546 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2547 tp->mac_version = tp->mii.supports_gmii ?
2548 RTL_GIGA_MAC_VER_46 :
2549 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551}
2552
2553static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2554{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002555 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556}
2557
Francois Romieu867763c2007-08-17 18:21:58 +02002558struct phy_reg {
2559 u16 reg;
2560 u16 val;
2561};
2562
françois romieu4da19632011-01-03 15:07:55 +00002563static void rtl_writephy_batch(struct rtl8169_private *tp,
2564 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002565{
2566 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002567 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002568 regs++;
2569 }
2570}
2571
françois romieubca03d52011-01-03 15:07:31 +00002572#define PHY_READ 0x00000000
2573#define PHY_DATA_OR 0x10000000
2574#define PHY_DATA_AND 0x20000000
2575#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002576#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002577#define PHY_CLEAR_READCOUNT 0x70000000
2578#define PHY_WRITE 0x80000000
2579#define PHY_READCOUNT_EQ_SKIP 0x90000000
2580#define PHY_COMP_EQ_SKIPN 0xa0000000
2581#define PHY_COMP_NEQ_SKIPN 0xb0000000
2582#define PHY_WRITE_PREVIOUS 0xc0000000
2583#define PHY_SKIPN 0xd0000000
2584#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002585
Hayes Wang960aee62011-06-18 11:37:48 +02002586struct fw_info {
2587 u32 magic;
2588 char version[RTL_VER_SIZE];
2589 __le32 fw_start;
2590 __le32 fw_len;
2591 u8 chksum;
2592} __packed;
2593
Francois Romieu1c361ef2011-06-17 17:16:24 +02002594#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2595
2596static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002597{
Francois Romieub6ffd972011-06-17 17:00:05 +02002598 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002599 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002600 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2601 char *version = rtl_fw->version;
2602 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002603
Francois Romieu1c361ef2011-06-17 17:16:24 +02002604 if (fw->size < FW_OPCODE_SIZE)
2605 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002606
2607 if (!fw_info->magic) {
2608 size_t i, size, start;
2609 u8 checksum = 0;
2610
2611 if (fw->size < sizeof(*fw_info))
2612 goto out;
2613
2614 for (i = 0; i < fw->size; i++)
2615 checksum += fw->data[i];
2616 if (checksum != 0)
2617 goto out;
2618
2619 start = le32_to_cpu(fw_info->fw_start);
2620 if (start > fw->size)
2621 goto out;
2622
2623 size = le32_to_cpu(fw_info->fw_len);
2624 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2625 goto out;
2626
2627 memcpy(version, fw_info->version, RTL_VER_SIZE);
2628
2629 pa->code = (__le32 *)(fw->data + start);
2630 pa->size = size;
2631 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002632 if (fw->size % FW_OPCODE_SIZE)
2633 goto out;
2634
2635 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2636
2637 pa->code = (__le32 *)fw->data;
2638 pa->size = fw->size / FW_OPCODE_SIZE;
2639 }
2640 version[RTL_VER_SIZE - 1] = 0;
2641
2642 rc = true;
2643out:
2644 return rc;
2645}
2646
Francois Romieufd112f22011-06-18 00:10:29 +02002647static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2648 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002649{
Francois Romieufd112f22011-06-18 00:10:29 +02002650 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002651 size_t index;
2652
Francois Romieu1c361ef2011-06-17 17:16:24 +02002653 for (index = 0; index < pa->size; index++) {
2654 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002655 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002656
hayeswang42b82dc2011-01-10 02:07:25 +00002657 switch(action & 0xf0000000) {
2658 case PHY_READ:
2659 case PHY_DATA_OR:
2660 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002661 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002662 case PHY_CLEAR_READCOUNT:
2663 case PHY_WRITE:
2664 case PHY_WRITE_PREVIOUS:
2665 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002666 break;
2667
hayeswang42b82dc2011-01-10 02:07:25 +00002668 case PHY_BJMPN:
2669 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002670 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002671 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002672 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002673 }
2674 break;
2675 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002676 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002677 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002678 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002679 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002680 }
2681 break;
2682 case PHY_COMP_EQ_SKIPN:
2683 case PHY_COMP_NEQ_SKIPN:
2684 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002685 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002686 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002687 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002688 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002689 }
2690 break;
2691
hayeswang42b82dc2011-01-10 02:07:25 +00002692 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002693 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002694 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002695 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002696 }
2697 }
Francois Romieufd112f22011-06-18 00:10:29 +02002698 rc = true;
2699out:
2700 return rc;
2701}
françois romieubca03d52011-01-03 15:07:31 +00002702
Francois Romieufd112f22011-06-18 00:10:29 +02002703static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2704{
2705 struct net_device *dev = tp->dev;
2706 int rc = -EINVAL;
2707
2708 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002709 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002710 goto out;
2711 }
2712
2713 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2714 rc = 0;
2715out:
2716 return rc;
2717}
2718
2719static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2720{
2721 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002722 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002723 u32 predata, count;
2724 size_t index;
2725
2726 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002727 org.write = ops->write;
2728 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002729
Francois Romieu1c361ef2011-06-17 17:16:24 +02002730 for (index = 0; index < pa->size; ) {
2731 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002732 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002733 u32 regno = (action & 0x0fff0000) >> 16;
2734
2735 if (!action)
2736 break;
françois romieubca03d52011-01-03 15:07:31 +00002737
2738 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002739 case PHY_READ:
2740 predata = rtl_readphy(tp, regno);
2741 count++;
2742 index++;
françois romieubca03d52011-01-03 15:07:31 +00002743 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002744 case PHY_DATA_OR:
2745 predata |= data;
2746 index++;
2747 break;
2748 case PHY_DATA_AND:
2749 predata &= data;
2750 index++;
2751 break;
2752 case PHY_BJMPN:
2753 index -= regno;
2754 break;
hayeswangeee37862013-04-01 22:23:38 +00002755 case PHY_MDIO_CHG:
2756 if (data == 0) {
2757 ops->write = org.write;
2758 ops->read = org.read;
2759 } else if (data == 1) {
2760 ops->write = mac_mcu_write;
2761 ops->read = mac_mcu_read;
2762 }
2763
hayeswang42b82dc2011-01-10 02:07:25 +00002764 index++;
2765 break;
2766 case PHY_CLEAR_READCOUNT:
2767 count = 0;
2768 index++;
2769 break;
2770 case PHY_WRITE:
2771 rtl_writephy(tp, regno, data);
2772 index++;
2773 break;
2774 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002775 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002776 break;
2777 case PHY_COMP_EQ_SKIPN:
2778 if (predata == data)
2779 index += regno;
2780 index++;
2781 break;
2782 case PHY_COMP_NEQ_SKIPN:
2783 if (predata != data)
2784 index += regno;
2785 index++;
2786 break;
2787 case PHY_WRITE_PREVIOUS:
2788 rtl_writephy(tp, regno, predata);
2789 index++;
2790 break;
2791 case PHY_SKIPN:
2792 index += regno + 1;
2793 break;
2794 case PHY_DELAY_MS:
2795 mdelay(data);
2796 index++;
2797 break;
2798
françois romieubca03d52011-01-03 15:07:31 +00002799 default:
2800 BUG();
2801 }
2802 }
hayeswangeee37862013-04-01 22:23:38 +00002803
2804 ops->write = org.write;
2805 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002806}
2807
françois romieuf1e02ed2011-01-13 13:07:53 +00002808static void rtl_release_firmware(struct rtl8169_private *tp)
2809{
Francois Romieub6ffd972011-06-17 17:00:05 +02002810 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2811 release_firmware(tp->rtl_fw->fw);
2812 kfree(tp->rtl_fw);
2813 }
2814 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002815}
2816
François Romieu953a12c2011-04-24 17:38:48 +02002817static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002818{
Francois Romieub6ffd972011-06-17 17:00:05 +02002819 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002820
2821 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002822 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002823 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002824}
2825
2826static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2827{
2828 if (rtl_readphy(tp, reg) != val)
2829 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2830 else
2831 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002832}
2833
françois romieu4da19632011-01-03 15:07:55 +00002834static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002836 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002837 { 0x1f, 0x0001 },
2838 { 0x06, 0x006e },
2839 { 0x08, 0x0708 },
2840 { 0x15, 0x4000 },
2841 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
françois romieu0b9b5712009-08-10 19:44:56 +00002843 { 0x1f, 0x0001 },
2844 { 0x03, 0x00a1 },
2845 { 0x02, 0x0008 },
2846 { 0x01, 0x0120 },
2847 { 0x00, 0x1000 },
2848 { 0x04, 0x0800 },
2849 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
françois romieu0b9b5712009-08-10 19:44:56 +00002851 { 0x03, 0xff41 },
2852 { 0x02, 0xdf60 },
2853 { 0x01, 0x0140 },
2854 { 0x00, 0x0077 },
2855 { 0x04, 0x7800 },
2856 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
françois romieu0b9b5712009-08-10 19:44:56 +00002858 { 0x03, 0x802f },
2859 { 0x02, 0x4f02 },
2860 { 0x01, 0x0409 },
2861 { 0x00, 0xf0f9 },
2862 { 0x04, 0x9800 },
2863 { 0x04, 0x9000 },
2864
2865 { 0x03, 0xdf01 },
2866 { 0x02, 0xdf20 },
2867 { 0x01, 0xff95 },
2868 { 0x00, 0xba00 },
2869 { 0x04, 0xa800 },
2870 { 0x04, 0xa000 },
2871
2872 { 0x03, 0xff41 },
2873 { 0x02, 0xdf20 },
2874 { 0x01, 0x0140 },
2875 { 0x00, 0x00bb },
2876 { 0x04, 0xb800 },
2877 { 0x04, 0xb000 },
2878
2879 { 0x03, 0xdf41 },
2880 { 0x02, 0xdc60 },
2881 { 0x01, 0x6340 },
2882 { 0x00, 0x007d },
2883 { 0x04, 0xd800 },
2884 { 0x04, 0xd000 },
2885
2886 { 0x03, 0xdf01 },
2887 { 0x02, 0xdf20 },
2888 { 0x01, 0x100a },
2889 { 0x00, 0xa0ff },
2890 { 0x04, 0xf800 },
2891 { 0x04, 0xf000 },
2892
2893 { 0x1f, 0x0000 },
2894 { 0x0b, 0x0000 },
2895 { 0x00, 0x9200 }
2896 };
2897
françois romieu4da19632011-01-03 15:07:55 +00002898 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899}
2900
françois romieu4da19632011-01-03 15:07:55 +00002901static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002902{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002903 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002904 { 0x1f, 0x0002 },
2905 { 0x01, 0x90d0 },
2906 { 0x1f, 0x0000 }
2907 };
2908
françois romieu4da19632011-01-03 15:07:55 +00002909 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002910}
2911
françois romieu4da19632011-01-03 15:07:55 +00002912static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002913{
2914 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002915
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002916 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2917 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002918 return;
2919
françois romieu4da19632011-01-03 15:07:55 +00002920 rtl_writephy(tp, 0x1f, 0x0001);
2921 rtl_writephy(tp, 0x10, 0xf01b);
2922 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002923}
2924
françois romieu4da19632011-01-03 15:07:55 +00002925static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002926{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002927 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002928 { 0x1f, 0x0001 },
2929 { 0x04, 0x0000 },
2930 { 0x03, 0x00a1 },
2931 { 0x02, 0x0008 },
2932 { 0x01, 0x0120 },
2933 { 0x00, 0x1000 },
2934 { 0x04, 0x0800 },
2935 { 0x04, 0x9000 },
2936 { 0x03, 0x802f },
2937 { 0x02, 0x4f02 },
2938 { 0x01, 0x0409 },
2939 { 0x00, 0xf099 },
2940 { 0x04, 0x9800 },
2941 { 0x04, 0xa000 },
2942 { 0x03, 0xdf01 },
2943 { 0x02, 0xdf20 },
2944 { 0x01, 0xff95 },
2945 { 0x00, 0xba00 },
2946 { 0x04, 0xa800 },
2947 { 0x04, 0xf000 },
2948 { 0x03, 0xdf01 },
2949 { 0x02, 0xdf20 },
2950 { 0x01, 0x101a },
2951 { 0x00, 0xa0ff },
2952 { 0x04, 0xf800 },
2953 { 0x04, 0x0000 },
2954 { 0x1f, 0x0000 },
2955
2956 { 0x1f, 0x0001 },
2957 { 0x10, 0xf41b },
2958 { 0x14, 0xfb54 },
2959 { 0x18, 0xf5c7 },
2960 { 0x1f, 0x0000 },
2961
2962 { 0x1f, 0x0001 },
2963 { 0x17, 0x0cc0 },
2964 { 0x1f, 0x0000 }
2965 };
2966
françois romieu4da19632011-01-03 15:07:55 +00002967 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002968
françois romieu4da19632011-01-03 15:07:55 +00002969 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002970}
2971
françois romieu4da19632011-01-03 15:07:55 +00002972static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002973{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002974 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002975 { 0x1f, 0x0001 },
2976 { 0x04, 0x0000 },
2977 { 0x03, 0x00a1 },
2978 { 0x02, 0x0008 },
2979 { 0x01, 0x0120 },
2980 { 0x00, 0x1000 },
2981 { 0x04, 0x0800 },
2982 { 0x04, 0x9000 },
2983 { 0x03, 0x802f },
2984 { 0x02, 0x4f02 },
2985 { 0x01, 0x0409 },
2986 { 0x00, 0xf099 },
2987 { 0x04, 0x9800 },
2988 { 0x04, 0xa000 },
2989 { 0x03, 0xdf01 },
2990 { 0x02, 0xdf20 },
2991 { 0x01, 0xff95 },
2992 { 0x00, 0xba00 },
2993 { 0x04, 0xa800 },
2994 { 0x04, 0xf000 },
2995 { 0x03, 0xdf01 },
2996 { 0x02, 0xdf20 },
2997 { 0x01, 0x101a },
2998 { 0x00, 0xa0ff },
2999 { 0x04, 0xf800 },
3000 { 0x04, 0x0000 },
3001 { 0x1f, 0x0000 },
3002
3003 { 0x1f, 0x0001 },
3004 { 0x0b, 0x8480 },
3005 { 0x1f, 0x0000 },
3006
3007 { 0x1f, 0x0001 },
3008 { 0x18, 0x67c7 },
3009 { 0x04, 0x2000 },
3010 { 0x03, 0x002f },
3011 { 0x02, 0x4360 },
3012 { 0x01, 0x0109 },
3013 { 0x00, 0x3022 },
3014 { 0x04, 0x2800 },
3015 { 0x1f, 0x0000 },
3016
3017 { 0x1f, 0x0001 },
3018 { 0x17, 0x0cc0 },
3019 { 0x1f, 0x0000 }
3020 };
3021
françois romieu4da19632011-01-03 15:07:55 +00003022 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00003023}
3024
françois romieu4da19632011-01-03 15:07:55 +00003025static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003026{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003027 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003028 { 0x10, 0xf41b },
3029 { 0x1f, 0x0000 }
3030 };
3031
françois romieu4da19632011-01-03 15:07:55 +00003032 rtl_writephy(tp, 0x1f, 0x0001);
3033 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02003034
françois romieu4da19632011-01-03 15:07:55 +00003035 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003036}
3037
françois romieu4da19632011-01-03 15:07:55 +00003038static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003039{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003040 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003041 { 0x1f, 0x0001 },
3042 { 0x10, 0xf41b },
3043 { 0x1f, 0x0000 }
3044 };
3045
françois romieu4da19632011-01-03 15:07:55 +00003046 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003047}
3048
françois romieu4da19632011-01-03 15:07:55 +00003049static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003050{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003051 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02003052 { 0x1f, 0x0000 },
3053 { 0x1d, 0x0f00 },
3054 { 0x1f, 0x0002 },
3055 { 0x0c, 0x1ec8 },
3056 { 0x1f, 0x0000 }
3057 };
3058
françois romieu4da19632011-01-03 15:07:55 +00003059 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02003060}
3061
françois romieu4da19632011-01-03 15:07:55 +00003062static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02003063{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003064 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02003065 { 0x1f, 0x0001 },
3066 { 0x1d, 0x3d98 },
3067 { 0x1f, 0x0000 }
3068 };
3069
françois romieu4da19632011-01-03 15:07:55 +00003070 rtl_writephy(tp, 0x1f, 0x0000);
3071 rtl_patchphy(tp, 0x14, 1 << 5);
3072 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02003073
françois romieu4da19632011-01-03 15:07:55 +00003074 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02003075}
3076
françois romieu4da19632011-01-03 15:07:55 +00003077static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003078{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003079 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02003080 { 0x1f, 0x0001 },
3081 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02003082 { 0x1f, 0x0002 },
3083 { 0x00, 0x88d4 },
3084 { 0x01, 0x82b1 },
3085 { 0x03, 0x7002 },
3086 { 0x08, 0x9e30 },
3087 { 0x09, 0x01f0 },
3088 { 0x0a, 0x5500 },
3089 { 0x0c, 0x00c8 },
3090 { 0x1f, 0x0003 },
3091 { 0x12, 0xc096 },
3092 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02003093 { 0x1f, 0x0000 },
3094 { 0x1f, 0x0000 },
3095 { 0x09, 0x2000 },
3096 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02003097 };
3098
françois romieu4da19632011-01-03 15:07:55 +00003099 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003100
françois romieu4da19632011-01-03 15:07:55 +00003101 rtl_patchphy(tp, 0x14, 1 << 5);
3102 rtl_patchphy(tp, 0x0d, 1 << 5);
3103 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02003104}
3105
françois romieu4da19632011-01-03 15:07:55 +00003106static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02003107{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003108 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02003109 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003110 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003111 { 0x03, 0x802f },
3112 { 0x02, 0x4f02 },
3113 { 0x01, 0x0409 },
3114 { 0x00, 0xf099 },
3115 { 0x04, 0x9800 },
3116 { 0x04, 0x9000 },
3117 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003118 { 0x1f, 0x0002 },
3119 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003120 { 0x06, 0x0761 },
3121 { 0x1f, 0x0003 },
3122 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003123 { 0x1f, 0x0000 }
3124 };
3125
françois romieu4da19632011-01-03 15:07:55 +00003126 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003127
françois romieu4da19632011-01-03 15:07:55 +00003128 rtl_patchphy(tp, 0x16, 1 << 0);
3129 rtl_patchphy(tp, 0x14, 1 << 5);
3130 rtl_patchphy(tp, 0x0d, 1 << 5);
3131 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003132}
3133
françois romieu4da19632011-01-03 15:07:55 +00003134static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02003135{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003136 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02003137 { 0x1f, 0x0001 },
3138 { 0x12, 0x2300 },
3139 { 0x1d, 0x3d98 },
3140 { 0x1f, 0x0002 },
3141 { 0x0c, 0x7eb8 },
3142 { 0x06, 0x5461 },
3143 { 0x1f, 0x0003 },
3144 { 0x16, 0x0f0a },
3145 { 0x1f, 0x0000 }
3146 };
3147
françois romieu4da19632011-01-03 15:07:55 +00003148 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02003149
françois romieu4da19632011-01-03 15:07:55 +00003150 rtl_patchphy(tp, 0x16, 1 << 0);
3151 rtl_patchphy(tp, 0x14, 1 << 5);
3152 rtl_patchphy(tp, 0x0d, 1 << 5);
3153 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003154}
3155
françois romieu4da19632011-01-03 15:07:55 +00003156static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003157{
françois romieu4da19632011-01-03 15:07:55 +00003158 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003159}
3160
françois romieubca03d52011-01-03 15:07:31 +00003161static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003162{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003163 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003164 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003165 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003166 { 0x06, 0x4064 },
3167 { 0x07, 0x2863 },
3168 { 0x08, 0x059c },
3169 { 0x09, 0x26b4 },
3170 { 0x0a, 0x6a19 },
3171 { 0x0b, 0xdcc8 },
3172 { 0x10, 0xf06d },
3173 { 0x14, 0x7f68 },
3174 { 0x18, 0x7fd9 },
3175 { 0x1c, 0xf0ff },
3176 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003177 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003178 { 0x12, 0xf49f },
3179 { 0x13, 0x070b },
3180 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003181 { 0x14, 0x94c0 },
3182
3183 /*
3184 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003185 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003186 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003187 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003188 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003189 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003190 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003191 { 0x06, 0x5561 },
3192
3193 /*
3194 * Can not link to 1Gbps with bad cable
3195 * Decrease SNR threshold form 21.07dB to 19.04dB
3196 */
3197 { 0x1f, 0x0001 },
3198 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003199
3200 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003201 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003202 };
3203
françois romieu4da19632011-01-03 15:07:55 +00003204 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003205
françois romieubca03d52011-01-03 15:07:31 +00003206 /*
3207 * Rx Error Issue
3208 * Fine Tune Switching regulator parameter
3209 */
françois romieu4da19632011-01-03 15:07:55 +00003210 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003211 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3212 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003213
Francois Romieufdf6fc02012-07-06 22:40:38 +02003214 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003215 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003216 { 0x1f, 0x0002 },
3217 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003218 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003219 { 0x05, 0x8330 },
3220 { 0x06, 0x669a },
3221 { 0x1f, 0x0002 }
3222 };
3223 int val;
3224
françois romieu4da19632011-01-03 15:07:55 +00003225 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003226
françois romieu4da19632011-01-03 15:07:55 +00003227 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003228
3229 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003230 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003231 0x0065, 0x0066, 0x0067, 0x0068,
3232 0x0069, 0x006a, 0x006b, 0x006c
3233 };
3234 int i;
3235
françois romieu4da19632011-01-03 15:07:55 +00003236 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003237
3238 val &= 0xff00;
3239 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003240 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003241 }
3242 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003243 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003244 { 0x1f, 0x0002 },
3245 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003246 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003247 { 0x05, 0x8330 },
3248 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003249 };
3250
françois romieu4da19632011-01-03 15:07:55 +00003251 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003252 }
3253
françois romieubca03d52011-01-03 15:07:31 +00003254 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003255 rtl_writephy(tp, 0x1f, 0x0002);
3256 rtl_patchphy(tp, 0x0d, 0x0300);
3257 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003258
françois romieubca03d52011-01-03 15:07:31 +00003259 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003260 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003261 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3262 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003263
françois romieu4da19632011-01-03 15:07:55 +00003264 rtl_writephy(tp, 0x1f, 0x0005);
3265 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003266
3267 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003268
françois romieu4da19632011-01-03 15:07:55 +00003269 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003270}
3271
françois romieubca03d52011-01-03 15:07:31 +00003272static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003273{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003274 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003275 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003276 { 0x1f, 0x0001 },
3277 { 0x06, 0x4064 },
3278 { 0x07, 0x2863 },
3279 { 0x08, 0x059c },
3280 { 0x09, 0x26b4 },
3281 { 0x0a, 0x6a19 },
3282 { 0x0b, 0xdcc8 },
3283 { 0x10, 0xf06d },
3284 { 0x14, 0x7f68 },
3285 { 0x18, 0x7fd9 },
3286 { 0x1c, 0xf0ff },
3287 { 0x1d, 0x3d9c },
3288 { 0x1f, 0x0003 },
3289 { 0x12, 0xf49f },
3290 { 0x13, 0x070b },
3291 { 0x1a, 0x05ad },
3292 { 0x14, 0x94c0 },
3293
françois romieubca03d52011-01-03 15:07:31 +00003294 /*
3295 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003296 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003297 */
françois romieudaf9df62009-10-07 12:44:20 +00003298 { 0x1f, 0x0002 },
3299 { 0x06, 0x5561 },
3300 { 0x1f, 0x0005 },
3301 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003302 { 0x06, 0x5561 },
3303
3304 /*
3305 * Can not link to 1Gbps with bad cable
3306 * Decrease SNR threshold form 21.07dB to 19.04dB
3307 */
3308 { 0x1f, 0x0001 },
3309 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003310
3311 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003312 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003313 };
3314
françois romieu4da19632011-01-03 15:07:55 +00003315 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003316
Francois Romieufdf6fc02012-07-06 22:40:38 +02003317 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003318 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003319 { 0x1f, 0x0002 },
3320 { 0x05, 0x669a },
3321 { 0x1f, 0x0005 },
3322 { 0x05, 0x8330 },
3323 { 0x06, 0x669a },
3324
3325 { 0x1f, 0x0002 }
3326 };
3327 int val;
3328
françois romieu4da19632011-01-03 15:07:55 +00003329 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003330
françois romieu4da19632011-01-03 15:07:55 +00003331 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003332 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003333 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003334 0x0065, 0x0066, 0x0067, 0x0068,
3335 0x0069, 0x006a, 0x006b, 0x006c
3336 };
3337 int i;
3338
françois romieu4da19632011-01-03 15:07:55 +00003339 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003340
3341 val &= 0xff00;
3342 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003343 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003344 }
3345 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003346 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003347 { 0x1f, 0x0002 },
3348 { 0x05, 0x2642 },
3349 { 0x1f, 0x0005 },
3350 { 0x05, 0x8330 },
3351 { 0x06, 0x2642 }
3352 };
3353
françois romieu4da19632011-01-03 15:07:55 +00003354 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003355 }
3356
françois romieubca03d52011-01-03 15:07:31 +00003357 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003358 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003359 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3360 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003361
françois romieubca03d52011-01-03 15:07:31 +00003362 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003363 rtl_writephy(tp, 0x1f, 0x0002);
3364 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003365
françois romieu4da19632011-01-03 15:07:55 +00003366 rtl_writephy(tp, 0x1f, 0x0005);
3367 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003368
3369 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003370
françois romieu4da19632011-01-03 15:07:55 +00003371 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003372}
3373
françois romieu4da19632011-01-03 15:07:55 +00003374static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003375{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003376 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003377 { 0x1f, 0x0002 },
3378 { 0x10, 0x0008 },
3379 { 0x0d, 0x006c },
3380
3381 { 0x1f, 0x0000 },
3382 { 0x0d, 0xf880 },
3383
3384 { 0x1f, 0x0001 },
3385 { 0x17, 0x0cc0 },
3386
3387 { 0x1f, 0x0001 },
3388 { 0x0b, 0xa4d8 },
3389 { 0x09, 0x281c },
3390 { 0x07, 0x2883 },
3391 { 0x0a, 0x6b35 },
3392 { 0x1d, 0x3da4 },
3393 { 0x1c, 0xeffd },
3394 { 0x14, 0x7f52 },
3395 { 0x18, 0x7fc6 },
3396 { 0x08, 0x0601 },
3397 { 0x06, 0x4063 },
3398 { 0x10, 0xf074 },
3399 { 0x1f, 0x0003 },
3400 { 0x13, 0x0789 },
3401 { 0x12, 0xf4bd },
3402 { 0x1a, 0x04fd },
3403 { 0x14, 0x84b0 },
3404 { 0x1f, 0x0000 },
3405 { 0x00, 0x9200 },
3406
3407 { 0x1f, 0x0005 },
3408 { 0x01, 0x0340 },
3409 { 0x1f, 0x0001 },
3410 { 0x04, 0x4000 },
3411 { 0x03, 0x1d21 },
3412 { 0x02, 0x0c32 },
3413 { 0x01, 0x0200 },
3414 { 0x00, 0x5554 },
3415 { 0x04, 0x4800 },
3416 { 0x04, 0x4000 },
3417 { 0x04, 0xf000 },
3418 { 0x03, 0xdf01 },
3419 { 0x02, 0xdf20 },
3420 { 0x01, 0x101a },
3421 { 0x00, 0xa0ff },
3422 { 0x04, 0xf800 },
3423 { 0x04, 0xf000 },
3424 { 0x1f, 0x0000 },
3425
3426 { 0x1f, 0x0007 },
3427 { 0x1e, 0x0023 },
3428 { 0x16, 0x0000 },
3429 { 0x1f, 0x0000 }
3430 };
3431
françois romieu4da19632011-01-03 15:07:55 +00003432 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003433}
3434
françois romieue6de30d2011-01-03 15:08:37 +00003435static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3436{
3437 static const struct phy_reg phy_reg_init[] = {
3438 { 0x1f, 0x0001 },
3439 { 0x17, 0x0cc0 },
3440
3441 { 0x1f, 0x0007 },
3442 { 0x1e, 0x002d },
3443 { 0x18, 0x0040 },
3444 { 0x1f, 0x0000 }
3445 };
3446
3447 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3448 rtl_patchphy(tp, 0x0d, 1 << 5);
3449}
3450
Hayes Wang70090422011-07-06 15:58:06 +08003451static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003452{
3453 static const struct phy_reg phy_reg_init[] = {
3454 /* Enable Delay cap */
3455 { 0x1f, 0x0005 },
3456 { 0x05, 0x8b80 },
3457 { 0x06, 0xc896 },
3458 { 0x1f, 0x0000 },
3459
3460 /* Channel estimation fine tune */
3461 { 0x1f, 0x0001 },
3462 { 0x0b, 0x6c20 },
3463 { 0x07, 0x2872 },
3464 { 0x1c, 0xefff },
3465 { 0x1f, 0x0003 },
3466 { 0x14, 0x6420 },
3467 { 0x1f, 0x0000 },
3468
3469 /* Update PFM & 10M TX idle timer */
3470 { 0x1f, 0x0007 },
3471 { 0x1e, 0x002f },
3472 { 0x15, 0x1919 },
3473 { 0x1f, 0x0000 },
3474
3475 { 0x1f, 0x0007 },
3476 { 0x1e, 0x00ac },
3477 { 0x18, 0x0006 },
3478 { 0x1f, 0x0000 }
3479 };
3480
Francois Romieu15ecd032011-04-27 13:52:22 -07003481 rtl_apply_firmware(tp);
3482
hayeswang01dc7fe2011-03-21 01:50:28 +00003483 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3484
3485 /* DCO enable for 10M IDLE Power */
3486 rtl_writephy(tp, 0x1f, 0x0007);
3487 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003488 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003489 rtl_writephy(tp, 0x1f, 0x0000);
3490
3491 /* For impedance matching */
3492 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003493 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003494 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003495
3496 /* PHY auto speed down */
3497 rtl_writephy(tp, 0x1f, 0x0007);
3498 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003499 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003500 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003501 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003502
3503 rtl_writephy(tp, 0x1f, 0x0005);
3504 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003505 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003506 rtl_writephy(tp, 0x1f, 0x0000);
3507
3508 rtl_writephy(tp, 0x1f, 0x0005);
3509 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003510 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003511 rtl_writephy(tp, 0x1f, 0x0007);
3512 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003513 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003514 rtl_writephy(tp, 0x1f, 0x0006);
3515 rtl_writephy(tp, 0x00, 0x5a00);
3516 rtl_writephy(tp, 0x1f, 0x0000);
3517 rtl_writephy(tp, 0x0d, 0x0007);
3518 rtl_writephy(tp, 0x0e, 0x003c);
3519 rtl_writephy(tp, 0x0d, 0x4007);
3520 rtl_writephy(tp, 0x0e, 0x0000);
3521 rtl_writephy(tp, 0x0d, 0x0000);
3522}
3523
françois romieu9ecb9aa2012-12-07 11:20:21 +00003524static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3525{
3526 const u16 w[] = {
3527 addr[0] | (addr[1] << 8),
3528 addr[2] | (addr[3] << 8),
3529 addr[4] | (addr[5] << 8)
3530 };
3531 const struct exgmac_reg e[] = {
3532 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3533 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3534 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3535 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3536 };
3537
3538 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3539}
3540
Hayes Wang70090422011-07-06 15:58:06 +08003541static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3542{
3543 static const struct phy_reg phy_reg_init[] = {
3544 /* Enable Delay cap */
3545 { 0x1f, 0x0004 },
3546 { 0x1f, 0x0007 },
3547 { 0x1e, 0x00ac },
3548 { 0x18, 0x0006 },
3549 { 0x1f, 0x0002 },
3550 { 0x1f, 0x0000 },
3551 { 0x1f, 0x0000 },
3552
3553 /* Channel estimation fine tune */
3554 { 0x1f, 0x0003 },
3555 { 0x09, 0xa20f },
3556 { 0x1f, 0x0000 },
3557 { 0x1f, 0x0000 },
3558
3559 /* Green Setting */
3560 { 0x1f, 0x0005 },
3561 { 0x05, 0x8b5b },
3562 { 0x06, 0x9222 },
3563 { 0x05, 0x8b6d },
3564 { 0x06, 0x8000 },
3565 { 0x05, 0x8b76 },
3566 { 0x06, 0x8000 },
3567 { 0x1f, 0x0000 }
3568 };
3569
3570 rtl_apply_firmware(tp);
3571
3572 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3573
3574 /* For 4-corner performance improve */
3575 rtl_writephy(tp, 0x1f, 0x0005);
3576 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003577 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003578 rtl_writephy(tp, 0x1f, 0x0000);
3579
3580 /* PHY auto speed down */
3581 rtl_writephy(tp, 0x1f, 0x0004);
3582 rtl_writephy(tp, 0x1f, 0x0007);
3583 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003584 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003585 rtl_writephy(tp, 0x1f, 0x0002);
3586 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003587 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003588
3589 /* improve 10M EEE waveform */
3590 rtl_writephy(tp, 0x1f, 0x0005);
3591 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003592 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003593 rtl_writephy(tp, 0x1f, 0x0000);
3594
3595 /* Improve 2-pair detection performance */
3596 rtl_writephy(tp, 0x1f, 0x0005);
3597 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003598 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003599 rtl_writephy(tp, 0x1f, 0x0000);
3600
3601 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003602 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003603 rtl_writephy(tp, 0x1f, 0x0005);
3604 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003605 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003606 rtl_writephy(tp, 0x1f, 0x0004);
3607 rtl_writephy(tp, 0x1f, 0x0007);
3608 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003609 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003610 rtl_writephy(tp, 0x1f, 0x0002);
3611 rtl_writephy(tp, 0x1f, 0x0000);
3612 rtl_writephy(tp, 0x0d, 0x0007);
3613 rtl_writephy(tp, 0x0e, 0x003c);
3614 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003615 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003616 rtl_writephy(tp, 0x0d, 0x0000);
3617
3618 /* Green feature */
3619 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003620 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3621 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003622 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003623 rtl_writephy(tp, 0x1f, 0x0005);
3624 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3625 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003626
françois romieu9ecb9aa2012-12-07 11:20:21 +00003627 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3628 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003629}
3630
Hayes Wang5f886e02012-03-30 14:33:03 +08003631static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3632{
3633 /* For 4-corner performance improve */
3634 rtl_writephy(tp, 0x1f, 0x0005);
3635 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003636 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003637 rtl_writephy(tp, 0x1f, 0x0000);
3638
3639 /* PHY auto speed down */
3640 rtl_writephy(tp, 0x1f, 0x0007);
3641 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003642 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003643 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003644 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003645
3646 /* Improve 10M EEE waveform */
3647 rtl_writephy(tp, 0x1f, 0x0005);
3648 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003649 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003650 rtl_writephy(tp, 0x1f, 0x0000);
3651}
3652
Hayes Wangc2218922011-09-06 16:55:18 +08003653static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3654{
3655 static const struct phy_reg phy_reg_init[] = {
3656 /* Channel estimation fine tune */
3657 { 0x1f, 0x0003 },
3658 { 0x09, 0xa20f },
3659 { 0x1f, 0x0000 },
3660
3661 /* Modify green table for giga & fnet */
3662 { 0x1f, 0x0005 },
3663 { 0x05, 0x8b55 },
3664 { 0x06, 0x0000 },
3665 { 0x05, 0x8b5e },
3666 { 0x06, 0x0000 },
3667 { 0x05, 0x8b67 },
3668 { 0x06, 0x0000 },
3669 { 0x05, 0x8b70 },
3670 { 0x06, 0x0000 },
3671 { 0x1f, 0x0000 },
3672 { 0x1f, 0x0007 },
3673 { 0x1e, 0x0078 },
3674 { 0x17, 0x0000 },
3675 { 0x19, 0x00fb },
3676 { 0x1f, 0x0000 },
3677
3678 /* Modify green table for 10M */
3679 { 0x1f, 0x0005 },
3680 { 0x05, 0x8b79 },
3681 { 0x06, 0xaa00 },
3682 { 0x1f, 0x0000 },
3683
3684 /* Disable hiimpedance detection (RTCT) */
3685 { 0x1f, 0x0003 },
3686 { 0x01, 0x328a },
3687 { 0x1f, 0x0000 }
3688 };
3689
3690 rtl_apply_firmware(tp);
3691
3692 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3693
Hayes Wang5f886e02012-03-30 14:33:03 +08003694 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003695
3696 /* Improve 2-pair detection performance */
3697 rtl_writephy(tp, 0x1f, 0x0005);
3698 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003699 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003700 rtl_writephy(tp, 0x1f, 0x0000);
3701}
3702
3703static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3704{
3705 rtl_apply_firmware(tp);
3706
Hayes Wang5f886e02012-03-30 14:33:03 +08003707 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003708}
3709
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003710static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3711{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003712 static const struct phy_reg phy_reg_init[] = {
3713 /* Channel estimation fine tune */
3714 { 0x1f, 0x0003 },
3715 { 0x09, 0xa20f },
3716 { 0x1f, 0x0000 },
3717
3718 /* Modify green table for giga & fnet */
3719 { 0x1f, 0x0005 },
3720 { 0x05, 0x8b55 },
3721 { 0x06, 0x0000 },
3722 { 0x05, 0x8b5e },
3723 { 0x06, 0x0000 },
3724 { 0x05, 0x8b67 },
3725 { 0x06, 0x0000 },
3726 { 0x05, 0x8b70 },
3727 { 0x06, 0x0000 },
3728 { 0x1f, 0x0000 },
3729 { 0x1f, 0x0007 },
3730 { 0x1e, 0x0078 },
3731 { 0x17, 0x0000 },
3732 { 0x19, 0x00aa },
3733 { 0x1f, 0x0000 },
3734
3735 /* Modify green table for 10M */
3736 { 0x1f, 0x0005 },
3737 { 0x05, 0x8b79 },
3738 { 0x06, 0xaa00 },
3739 { 0x1f, 0x0000 },
3740
3741 /* Disable hiimpedance detection (RTCT) */
3742 { 0x1f, 0x0003 },
3743 { 0x01, 0x328a },
3744 { 0x1f, 0x0000 }
3745 };
3746
3747
3748 rtl_apply_firmware(tp);
3749
3750 rtl8168f_hw_phy_config(tp);
3751
3752 /* Improve 2-pair detection performance */
3753 rtl_writephy(tp, 0x1f, 0x0005);
3754 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003755 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003756 rtl_writephy(tp, 0x1f, 0x0000);
3757
3758 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3759
3760 /* Modify green table for giga */
3761 rtl_writephy(tp, 0x1f, 0x0005);
3762 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003763 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003764 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003765 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003766 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003767 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003768 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003769 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003770 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003771 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003772 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003773 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003774 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003775 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003776 rtl_writephy(tp, 0x1f, 0x0000);
3777
3778 /* uc same-seed solution */
3779 rtl_writephy(tp, 0x1f, 0x0005);
3780 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003781 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003782 rtl_writephy(tp, 0x1f, 0x0000);
3783
3784 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003785 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003786 rtl_writephy(tp, 0x1f, 0x0005);
3787 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003788 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003789 rtl_writephy(tp, 0x1f, 0x0004);
3790 rtl_writephy(tp, 0x1f, 0x0007);
3791 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003792 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003793 rtl_writephy(tp, 0x1f, 0x0000);
3794 rtl_writephy(tp, 0x0d, 0x0007);
3795 rtl_writephy(tp, 0x0e, 0x003c);
3796 rtl_writephy(tp, 0x0d, 0x4007);
3797 rtl_writephy(tp, 0x0e, 0x0000);
3798 rtl_writephy(tp, 0x0d, 0x0000);
3799
3800 /* Green feature */
3801 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003802 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3803 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003804 rtl_writephy(tp, 0x1f, 0x0000);
3805}
3806
Hayes Wangc5583862012-07-02 17:23:22 +08003807static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3808{
Hayes Wangc5583862012-07-02 17:23:22 +08003809 rtl_apply_firmware(tp);
3810
hayeswang41f44d12013-04-01 22:23:36 +00003811 rtl_writephy(tp, 0x1f, 0x0a46);
3812 if (rtl_readphy(tp, 0x10) & 0x0100) {
3813 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003814 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003815 } else {
3816 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003817 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003818 }
Hayes Wangc5583862012-07-02 17:23:22 +08003819
hayeswang41f44d12013-04-01 22:23:36 +00003820 rtl_writephy(tp, 0x1f, 0x0a46);
3821 if (rtl_readphy(tp, 0x13) & 0x0100) {
3822 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003823 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003824 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003825 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003826 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003827 }
Hayes Wangc5583862012-07-02 17:23:22 +08003828
hayeswang41f44d12013-04-01 22:23:36 +00003829 /* Enable PHY auto speed down */
3830 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003831 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003832
hayeswangfe7524c2013-04-01 22:23:37 +00003833 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003834 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003835 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003836 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003837 rtl_writephy(tp, 0x1f, 0x0a43);
3838 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003839 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3840 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003841
hayeswang41f44d12013-04-01 22:23:36 +00003842 /* EEE auto-fallback function */
3843 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003844 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003845
hayeswang41f44d12013-04-01 22:23:36 +00003846 /* Enable UC LPF tune function */
3847 rtl_writephy(tp, 0x1f, 0x0a43);
3848 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003849 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003850
3851 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003852 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003853
hayeswangfe7524c2013-04-01 22:23:37 +00003854 /* Improve SWR Efficiency */
3855 rtl_writephy(tp, 0x1f, 0x0bcd);
3856 rtl_writephy(tp, 0x14, 0x5065);
3857 rtl_writephy(tp, 0x14, 0xd065);
3858 rtl_writephy(tp, 0x1f, 0x0bc8);
3859 rtl_writephy(tp, 0x11, 0x5655);
3860 rtl_writephy(tp, 0x1f, 0x0bcd);
3861 rtl_writephy(tp, 0x14, 0x1065);
3862 rtl_writephy(tp, 0x14, 0x9065);
3863 rtl_writephy(tp, 0x14, 0x1065);
3864
David Chang1bac1072013-11-27 15:48:36 +08003865 /* Check ALDPS bit, disable it if enabled */
3866 rtl_writephy(tp, 0x1f, 0x0a43);
3867 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003868 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08003869
hayeswang41f44d12013-04-01 22:23:36 +00003870 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003871}
3872
hayeswang57538c42013-04-01 22:23:40 +00003873static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3874{
3875 rtl_apply_firmware(tp);
3876}
3877
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003878static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3879{
3880 u16 dout_tapbin;
3881 u32 data;
3882
3883 rtl_apply_firmware(tp);
3884
3885 /* CHN EST parameters adjust - giga master */
3886 rtl_writephy(tp, 0x1f, 0x0a43);
3887 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003888 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003889 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003890 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003891 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003892 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003893 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003894 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003895 rtl_writephy(tp, 0x1f, 0x0000);
3896
3897 /* CHN EST parameters adjust - giga slave */
3898 rtl_writephy(tp, 0x1f, 0x0a43);
3899 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003900 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003901 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003902 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003903 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003904 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003905 rtl_writephy(tp, 0x1f, 0x0000);
3906
3907 /* CHN EST parameters adjust - fnet */
3908 rtl_writephy(tp, 0x1f, 0x0a43);
3909 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003910 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003911 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003912 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003913 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003914 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003915 rtl_writephy(tp, 0x1f, 0x0000);
3916
3917 /* enable R-tune & PGA-retune function */
3918 dout_tapbin = 0;
3919 rtl_writephy(tp, 0x1f, 0x0a46);
3920 data = rtl_readphy(tp, 0x13);
3921 data &= 3;
3922 data <<= 2;
3923 dout_tapbin |= data;
3924 data = rtl_readphy(tp, 0x12);
3925 data &= 0xc000;
3926 data >>= 14;
3927 dout_tapbin |= data;
3928 dout_tapbin = ~(dout_tapbin^0x08);
3929 dout_tapbin <<= 12;
3930 dout_tapbin &= 0xf000;
3931 rtl_writephy(tp, 0x1f, 0x0a43);
3932 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003933 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003934 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003935 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003936 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003937 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003938 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003939 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003940
3941 rtl_writephy(tp, 0x1f, 0x0a43);
3942 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003943 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003944 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003945 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003946 rtl_writephy(tp, 0x1f, 0x0000);
3947
3948 /* enable GPHY 10M */
3949 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003950 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003951 rtl_writephy(tp, 0x1f, 0x0000);
3952
3953 /* SAR ADC performance */
3954 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003955 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003956 rtl_writephy(tp, 0x1f, 0x0000);
3957
3958 rtl_writephy(tp, 0x1f, 0x0a43);
3959 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003960 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003961 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003962 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003963 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003964 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003965 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003966 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003967 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003968 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003969 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003970 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003971 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003972 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003973 rtl_writephy(tp, 0x1f, 0x0000);
3974
3975 /* disable phy pfm mode */
3976 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003977 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003978 rtl_writephy(tp, 0x1f, 0x0000);
3979
3980 /* Check ALDPS bit, disable it if enabled */
3981 rtl_writephy(tp, 0x1f, 0x0a43);
3982 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003983 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003984
3985 rtl_writephy(tp, 0x1f, 0x0000);
3986}
3987
3988static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3989{
3990 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3991 u16 rlen;
3992 u32 data;
3993
3994 rtl_apply_firmware(tp);
3995
3996 /* CHIN EST parameter update */
3997 rtl_writephy(tp, 0x1f, 0x0a43);
3998 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003999 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004000 rtl_writephy(tp, 0x1f, 0x0000);
4001
4002 /* enable R-tune & PGA-retune function */
4003 rtl_writephy(tp, 0x1f, 0x0a43);
4004 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004005 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004006 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004007 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004008 rtl_writephy(tp, 0x1f, 0x0000);
4009
4010 /* enable GPHY 10M */
4011 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004012 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004013 rtl_writephy(tp, 0x1f, 0x0000);
4014
4015 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
4016 data = r8168_mac_ocp_read(tp, 0xdd02);
4017 ioffset_p3 = ((data & 0x80)>>7);
4018 ioffset_p3 <<= 3;
4019
4020 data = r8168_mac_ocp_read(tp, 0xdd00);
4021 ioffset_p3 |= ((data & (0xe000))>>13);
4022 ioffset_p2 = ((data & (0x1e00))>>9);
4023 ioffset_p1 = ((data & (0x01e0))>>5);
4024 ioffset_p0 = ((data & 0x0010)>>4);
4025 ioffset_p0 <<= 3;
4026 ioffset_p0 |= (data & (0x07));
4027 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
4028
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004029 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08004030 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004031 rtl_writephy(tp, 0x1f, 0x0bcf);
4032 rtl_writephy(tp, 0x16, data);
4033 rtl_writephy(tp, 0x1f, 0x0000);
4034 }
4035
4036 /* Modify rlen (TX LPF corner frequency) level */
4037 rtl_writephy(tp, 0x1f, 0x0bcd);
4038 data = rtl_readphy(tp, 0x16);
4039 data &= 0x000f;
4040 rlen = 0;
4041 if (data > 3)
4042 rlen = data - 3;
4043 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
4044 rtl_writephy(tp, 0x17, data);
4045 rtl_writephy(tp, 0x1f, 0x0bcd);
4046 rtl_writephy(tp, 0x1f, 0x0000);
4047
4048 /* disable phy pfm mode */
4049 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004050 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004051 rtl_writephy(tp, 0x1f, 0x0000);
4052
4053 /* Check ALDPS bit, disable it if enabled */
4054 rtl_writephy(tp, 0x1f, 0x0a43);
4055 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004056 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004057
4058 rtl_writephy(tp, 0x1f, 0x0000);
4059}
4060
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004061static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4062{
4063 /* Enable PHY auto speed down */
4064 rtl_writephy(tp, 0x1f, 0x0a44);
4065 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4066 rtl_writephy(tp, 0x1f, 0x0000);
4067
4068 /* patch 10M & ALDPS */
4069 rtl_writephy(tp, 0x1f, 0x0bcc);
4070 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4071 rtl_writephy(tp, 0x1f, 0x0a44);
4072 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4073 rtl_writephy(tp, 0x1f, 0x0a43);
4074 rtl_writephy(tp, 0x13, 0x8084);
4075 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4076 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4077 rtl_writephy(tp, 0x1f, 0x0000);
4078
4079 /* Enable EEE auto-fallback function */
4080 rtl_writephy(tp, 0x1f, 0x0a4b);
4081 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4082 rtl_writephy(tp, 0x1f, 0x0000);
4083
4084 /* Enable UC LPF tune function */
4085 rtl_writephy(tp, 0x1f, 0x0a43);
4086 rtl_writephy(tp, 0x13, 0x8012);
4087 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4088 rtl_writephy(tp, 0x1f, 0x0000);
4089
4090 /* set rg_sel_sdm_rate */
4091 rtl_writephy(tp, 0x1f, 0x0c42);
4092 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4093 rtl_writephy(tp, 0x1f, 0x0000);
4094
4095 /* Check ALDPS bit, disable it if enabled */
4096 rtl_writephy(tp, 0x1f, 0x0a43);
4097 if (rtl_readphy(tp, 0x10) & 0x0004)
4098 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4099
4100 rtl_writephy(tp, 0x1f, 0x0000);
4101}
4102
4103static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4104{
4105 /* patch 10M & ALDPS */
4106 rtl_writephy(tp, 0x1f, 0x0bcc);
4107 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4108 rtl_writephy(tp, 0x1f, 0x0a44);
4109 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4110 rtl_writephy(tp, 0x1f, 0x0a43);
4111 rtl_writephy(tp, 0x13, 0x8084);
4112 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4113 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4114 rtl_writephy(tp, 0x1f, 0x0000);
4115
4116 /* Enable UC LPF tune function */
4117 rtl_writephy(tp, 0x1f, 0x0a43);
4118 rtl_writephy(tp, 0x13, 0x8012);
4119 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4120 rtl_writephy(tp, 0x1f, 0x0000);
4121
4122 /* Set rg_sel_sdm_rate */
4123 rtl_writephy(tp, 0x1f, 0x0c42);
4124 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4125 rtl_writephy(tp, 0x1f, 0x0000);
4126
4127 /* Channel estimation parameters */
4128 rtl_writephy(tp, 0x1f, 0x0a43);
4129 rtl_writephy(tp, 0x13, 0x80f3);
4130 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4131 rtl_writephy(tp, 0x13, 0x80f0);
4132 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4133 rtl_writephy(tp, 0x13, 0x80ef);
4134 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4135 rtl_writephy(tp, 0x13, 0x80f6);
4136 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4137 rtl_writephy(tp, 0x13, 0x80ec);
4138 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4139 rtl_writephy(tp, 0x13, 0x80ed);
4140 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4141 rtl_writephy(tp, 0x13, 0x80f2);
4142 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4143 rtl_writephy(tp, 0x13, 0x80f4);
4144 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4145 rtl_writephy(tp, 0x1f, 0x0a43);
4146 rtl_writephy(tp, 0x13, 0x8110);
4147 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4148 rtl_writephy(tp, 0x13, 0x810f);
4149 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4150 rtl_writephy(tp, 0x13, 0x8111);
4151 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4152 rtl_writephy(tp, 0x13, 0x8113);
4153 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4154 rtl_writephy(tp, 0x13, 0x8115);
4155 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4156 rtl_writephy(tp, 0x13, 0x810e);
4157 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4158 rtl_writephy(tp, 0x13, 0x810c);
4159 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4160 rtl_writephy(tp, 0x13, 0x810b);
4161 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4162 rtl_writephy(tp, 0x1f, 0x0a43);
4163 rtl_writephy(tp, 0x13, 0x80d1);
4164 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4165 rtl_writephy(tp, 0x13, 0x80cd);
4166 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4167 rtl_writephy(tp, 0x13, 0x80d3);
4168 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4169 rtl_writephy(tp, 0x13, 0x80d5);
4170 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4171 rtl_writephy(tp, 0x13, 0x80d7);
4172 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4173
4174 /* Force PWM-mode */
4175 rtl_writephy(tp, 0x1f, 0x0bcd);
4176 rtl_writephy(tp, 0x14, 0x5065);
4177 rtl_writephy(tp, 0x14, 0xd065);
4178 rtl_writephy(tp, 0x1f, 0x0bc8);
4179 rtl_writephy(tp, 0x12, 0x00ed);
4180 rtl_writephy(tp, 0x1f, 0x0bcd);
4181 rtl_writephy(tp, 0x14, 0x1065);
4182 rtl_writephy(tp, 0x14, 0x9065);
4183 rtl_writephy(tp, 0x14, 0x1065);
4184 rtl_writephy(tp, 0x1f, 0x0000);
4185
4186 /* Check ALDPS bit, disable it if enabled */
4187 rtl_writephy(tp, 0x1f, 0x0a43);
4188 if (rtl_readphy(tp, 0x10) & 0x0004)
4189 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4190
4191 rtl_writephy(tp, 0x1f, 0x0000);
4192}
4193
françois romieu4da19632011-01-03 15:07:55 +00004194static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004195{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004196 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004197 { 0x1f, 0x0003 },
4198 { 0x08, 0x441d },
4199 { 0x01, 0x9100 },
4200 { 0x1f, 0x0000 }
4201 };
4202
françois romieu4da19632011-01-03 15:07:55 +00004203 rtl_writephy(tp, 0x1f, 0x0000);
4204 rtl_patchphy(tp, 0x11, 1 << 12);
4205 rtl_patchphy(tp, 0x19, 1 << 13);
4206 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004207
françois romieu4da19632011-01-03 15:07:55 +00004208 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004209}
4210
Hayes Wang5a5e4442011-02-22 17:26:21 +08004211static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4212{
4213 static const struct phy_reg phy_reg_init[] = {
4214 { 0x1f, 0x0005 },
4215 { 0x1a, 0x0000 },
4216 { 0x1f, 0x0000 },
4217
4218 { 0x1f, 0x0004 },
4219 { 0x1c, 0x0000 },
4220 { 0x1f, 0x0000 },
4221
4222 { 0x1f, 0x0001 },
4223 { 0x15, 0x7701 },
4224 { 0x1f, 0x0000 }
4225 };
4226
4227 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004228 rtl_writephy(tp, 0x1f, 0x0000);
4229 rtl_writephy(tp, 0x18, 0x0310);
4230 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004231
François Romieu953a12c2011-04-24 17:38:48 +02004232 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004233
4234 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4235}
4236
Hayes Wang7e18dca2012-03-30 14:33:02 +08004237static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4238{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004239 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004240 rtl_writephy(tp, 0x1f, 0x0000);
4241 rtl_writephy(tp, 0x18, 0x0310);
4242 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004243
4244 rtl_apply_firmware(tp);
4245
4246 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004247 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004248 rtl_writephy(tp, 0x1f, 0x0004);
4249 rtl_writephy(tp, 0x10, 0x401f);
4250 rtl_writephy(tp, 0x19, 0x7030);
4251 rtl_writephy(tp, 0x1f, 0x0000);
4252}
4253
Hayes Wang5598bfe2012-07-02 17:23:21 +08004254static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4255{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004256 static const struct phy_reg phy_reg_init[] = {
4257 { 0x1f, 0x0004 },
4258 { 0x10, 0xc07f },
4259 { 0x19, 0x7030 },
4260 { 0x1f, 0x0000 }
4261 };
4262
4263 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004264 rtl_writephy(tp, 0x1f, 0x0000);
4265 rtl_writephy(tp, 0x18, 0x0310);
4266 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004267
4268 rtl_apply_firmware(tp);
4269
Francois Romieufdf6fc02012-07-06 22:40:38 +02004270 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004271 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4272
Francois Romieufdf6fc02012-07-06 22:40:38 +02004273 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004274}
4275
Francois Romieu5615d9f2007-08-17 17:50:46 +02004276static void rtl_hw_phy_config(struct net_device *dev)
4277{
4278 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004279
4280 rtl8169_print_mac_version(tp);
4281
4282 switch (tp->mac_version) {
4283 case RTL_GIGA_MAC_VER_01:
4284 break;
4285 case RTL_GIGA_MAC_VER_02:
4286 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004287 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004288 break;
4289 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004290 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004291 break;
françois romieu2e9558562009-08-10 19:44:19 +00004292 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004293 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004294 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004295 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004296 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004297 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004298 case RTL_GIGA_MAC_VER_07:
4299 case RTL_GIGA_MAC_VER_08:
4300 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004301 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004302 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004303 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004304 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004305 break;
4306 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004307 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004308 break;
4309 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004310 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004311 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004312 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004313 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004314 break;
4315 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004316 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004317 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004318 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004319 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004320 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004321 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004322 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004323 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004324 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004325 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004326 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004327 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004328 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004329 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004330 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004331 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004332 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004333 break;
4334 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004335 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004336 break;
4337 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004338 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004339 break;
françois romieue6de30d2011-01-03 15:08:37 +00004340 case RTL_GIGA_MAC_VER_28:
4341 rtl8168d_4_hw_phy_config(tp);
4342 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004343 case RTL_GIGA_MAC_VER_29:
4344 case RTL_GIGA_MAC_VER_30:
4345 rtl8105e_hw_phy_config(tp);
4346 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004347 case RTL_GIGA_MAC_VER_31:
4348 /* None. */
4349 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004350 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004351 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004352 rtl8168e_1_hw_phy_config(tp);
4353 break;
4354 case RTL_GIGA_MAC_VER_34:
4355 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004356 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004357 case RTL_GIGA_MAC_VER_35:
4358 rtl8168f_1_hw_phy_config(tp);
4359 break;
4360 case RTL_GIGA_MAC_VER_36:
4361 rtl8168f_2_hw_phy_config(tp);
4362 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004363
Hayes Wang7e18dca2012-03-30 14:33:02 +08004364 case RTL_GIGA_MAC_VER_37:
4365 rtl8402_hw_phy_config(tp);
4366 break;
4367
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004368 case RTL_GIGA_MAC_VER_38:
4369 rtl8411_hw_phy_config(tp);
4370 break;
4371
Hayes Wang5598bfe2012-07-02 17:23:21 +08004372 case RTL_GIGA_MAC_VER_39:
4373 rtl8106e_hw_phy_config(tp);
4374 break;
4375
Hayes Wangc5583862012-07-02 17:23:22 +08004376 case RTL_GIGA_MAC_VER_40:
4377 rtl8168g_1_hw_phy_config(tp);
4378 break;
hayeswang57538c42013-04-01 22:23:40 +00004379 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004380 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004381 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004382 rtl8168g_2_hw_phy_config(tp);
4383 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004384 case RTL_GIGA_MAC_VER_45:
4385 case RTL_GIGA_MAC_VER_47:
4386 rtl8168h_1_hw_phy_config(tp);
4387 break;
4388 case RTL_GIGA_MAC_VER_46:
4389 case RTL_GIGA_MAC_VER_48:
4390 rtl8168h_2_hw_phy_config(tp);
4391 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004392
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004393 case RTL_GIGA_MAC_VER_49:
4394 rtl8168ep_1_hw_phy_config(tp);
4395 break;
4396 case RTL_GIGA_MAC_VER_50:
4397 case RTL_GIGA_MAC_VER_51:
4398 rtl8168ep_2_hw_phy_config(tp);
4399 break;
4400
Hayes Wangc5583862012-07-02 17:23:22 +08004401 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004402 default:
4403 break;
4404 }
4405}
4406
Francois Romieuda78dbf2012-01-26 14:18:23 +01004407static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 struct timer_list *timer = &tp->timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4411
Francois Romieubcf0bf92006-07-26 23:14:13 +02004412 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
françois romieu4da19632011-01-03 15:07:55 +00004414 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02004415 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 * A busy loop could burn quite a few cycles on nowadays CPU.
4417 * Let's delay the execution of the timer for a few ticks.
4418 */
4419 timeout = HZ/10;
4420 goto out_mod_timer;
4421 }
4422
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004423 if (tp->link_ok(tp))
Francois Romieuda78dbf2012-01-26 14:18:23 +01004424 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02004426 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
françois romieu4da19632011-01-03 15:07:55 +00004428 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
4430out_mod_timer:
4431 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004432}
4433
4434static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4435{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004436 if (!test_and_set_bit(flag, tp->wk.flags))
4437 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004438}
4439
Kees Cook9de36cc2017-10-25 03:53:12 -07004440static void rtl8169_phy_timer(struct timer_list *t)
Francois Romieuda78dbf2012-01-26 14:18:23 +01004441{
Kees Cook9de36cc2017-10-25 03:53:12 -07004442 struct rtl8169_private *tp = from_timer(tp, t, timer);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004443
Francois Romieu98ddf982012-01-31 10:47:34 +01004444 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445}
4446
Francois Romieuffc46952012-07-06 14:19:23 +02004447DECLARE_RTL_COND(rtl_phy_reset_cond)
4448{
4449 return tp->phy_reset_pending(tp);
4450}
4451
Francois Romieubf793292006-11-01 00:53:05 +01004452static void rtl8169_phy_reset(struct net_device *dev,
4453 struct rtl8169_private *tp)
4454{
françois romieu4da19632011-01-03 15:07:55 +00004455 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004456 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004457}
4458
David S. Miller8decf862011-09-22 03:23:13 -04004459static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4460{
David S. Miller8decf862011-09-22 03:23:13 -04004461 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004462 (RTL_R8(tp, PHYstatus) & TBI_Enable);
David S. Miller8decf862011-09-22 03:23:13 -04004463}
4464
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004465static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466{
Francois Romieu5615d9f2007-08-17 17:50:46 +02004467 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004468
Marcus Sundberg773328942008-07-10 21:28:08 +02004469 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4470 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004471 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02004472 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004473
Francois Romieu6dccd162007-02-13 23:38:05 +01004474 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4475
4476 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4477 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004478
Francois Romieubcf0bf92006-07-26 23:14:13 +02004479 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004480 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004481 RTL_W8(tp, 0x82, 0x01);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004482 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004483 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004484 }
4485
Francois Romieubf793292006-11-01 00:53:05 +01004486 rtl8169_phy_reset(dev, tp);
4487
Oliver Neukum54405cd2011-01-06 21:55:13 +01004488 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004489 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4490 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4491 (tp->mii.supports_gmii ?
4492 ADVERTISED_1000baseT_Half |
4493 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004494
David S. Miller8decf862011-09-22 03:23:13 -04004495 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00004496 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004497}
4498
Francois Romieu773d2022007-01-31 23:47:43 +01004499static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4500{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004501 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004502
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004503 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00004504
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004505 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4506 RTL_R32(tp, MAC4);
françois romieu908ba2bf2010-04-26 11:42:58 +00004507
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004508 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4509 RTL_R32(tp, MAC0);
françois romieu908ba2bf2010-04-26 11:42:58 +00004510
françois romieu9ecb9aa2012-12-07 11:20:21 +00004511 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4512 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004513
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004514 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieu773d2022007-01-31 23:47:43 +01004515
Francois Romieuda78dbf2012-01-26 14:18:23 +01004516 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004517}
4518
4519static int rtl_set_mac_address(struct net_device *dev, void *p)
4520{
4521 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004522 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004523 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004524
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004525 ret = eth_mac_addr(dev, p);
4526 if (ret)
4527 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004528
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004529 pm_runtime_get_noresume(d);
4530
4531 if (pm_runtime_active(d))
4532 rtl_rar_set(tp, dev->dev_addr);
4533
4534 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004535
4536 return 0;
4537}
4538
Francois Romieu5f787a12006-08-17 13:02:36 +02004539static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4540{
4541 struct rtl8169_private *tp = netdev_priv(dev);
4542 struct mii_ioctl_data *data = if_mii(ifr);
4543
Francois Romieu8b4ab282008-11-19 22:05:25 -08004544 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4545}
Francois Romieu5f787a12006-08-17 13:02:36 +02004546
Francois Romieucecb5fd2011-04-01 10:21:07 +02004547static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4548 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004549{
Francois Romieu5f787a12006-08-17 13:02:36 +02004550 switch (cmd) {
4551 case SIOCGMIIPHY:
4552 data->phy_id = 32; /* Internal PHY */
4553 return 0;
4554
4555 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004556 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004557 return 0;
4558
4559 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004560 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004561 return 0;
4562 }
4563 return -EOPNOTSUPP;
4564}
4565
Francois Romieu8b4ab282008-11-19 22:05:25 -08004566static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4567{
4568 return -EOPNOTSUPP;
4569}
4570
Bill Pembertonbaf63292012-12-03 09:23:28 -05004571static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004572{
4573 struct mdio_ops *ops = &tp->mdio_ops;
4574
4575 switch (tp->mac_version) {
4576 case RTL_GIGA_MAC_VER_27:
4577 ops->write = r8168dp_1_mdio_write;
4578 ops->read = r8168dp_1_mdio_read;
4579 break;
françois romieue6de30d2011-01-03 15:08:37 +00004580 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004581 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004582 ops->write = r8168dp_2_mdio_write;
4583 ops->read = r8168dp_2_mdio_read;
4584 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004585 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004586 ops->write = r8168g_mdio_write;
4587 ops->read = r8168g_mdio_read;
4588 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004589 default:
4590 ops->write = r8169_mdio_write;
4591 ops->read = r8169_mdio_read;
4592 break;
4593 }
4594}
4595
hayeswange2409d82013-03-31 17:02:04 +00004596static void rtl_speed_down(struct rtl8169_private *tp)
4597{
4598 u32 adv;
4599 int lpa;
4600
4601 rtl_writephy(tp, 0x1f, 0x0000);
4602 lpa = rtl_readphy(tp, MII_LPA);
4603
4604 if (lpa & (LPA_10HALF | LPA_10FULL))
4605 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4606 else if (lpa & (LPA_100HALF | LPA_100FULL))
4607 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4608 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4609 else
4610 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4611 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4612 (tp->mii.supports_gmii ?
4613 ADVERTISED_1000baseT_Half |
4614 ADVERTISED_1000baseT_Full : 0);
4615
4616 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4617 adv);
4618}
4619
David S. Miller1805b2f2011-10-24 18:18:09 -04004620static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4621{
David S. Miller1805b2f2011-10-24 18:18:09 -04004622 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004623 case RTL_GIGA_MAC_VER_25:
4624 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004625 case RTL_GIGA_MAC_VER_29:
4626 case RTL_GIGA_MAC_VER_30:
4627 case RTL_GIGA_MAC_VER_32:
4628 case RTL_GIGA_MAC_VER_33:
4629 case RTL_GIGA_MAC_VER_34:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004630 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004631 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04004632 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4633 break;
4634 default:
4635 break;
4636 }
4637}
4638
4639static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4640{
4641 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4642 return false;
4643
hayeswange2409d82013-03-31 17:02:04 +00004644 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004645 rtl_wol_suspend_quirk(tp);
4646
4647 return true;
4648}
4649
françois romieu065c27c2011-01-03 15:08:12 +00004650static void r8168_phy_power_up(struct rtl8169_private *tp)
4651{
4652 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004653 switch (tp->mac_version) {
4654 case RTL_GIGA_MAC_VER_11:
4655 case RTL_GIGA_MAC_VER_12:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004656 case RTL_GIGA_MAC_VER_17 ... RTL_GIGA_MAC_VER_28:
hayeswang01dc7fe2011-03-21 01:50:28 +00004657 case RTL_GIGA_MAC_VER_31:
4658 rtl_writephy(tp, 0x0e, 0x0000);
4659 break;
4660 default:
4661 break;
4662 }
françois romieu065c27c2011-01-03 15:08:12 +00004663 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
David S. Millerb2d6cee2018-05-11 20:53:22 -04004664
4665 /* give MAC/PHY some time to resume */
4666 msleep(20);
françois romieu065c27c2011-01-03 15:08:12 +00004667}
4668
4669static void r8168_phy_power_down(struct rtl8169_private *tp)
4670{
4671 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004672 switch (tp->mac_version) {
4673 case RTL_GIGA_MAC_VER_32:
4674 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004675 case RTL_GIGA_MAC_VER_40:
4676 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004677 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4678 break;
4679
4680 case RTL_GIGA_MAC_VER_11:
4681 case RTL_GIGA_MAC_VER_12:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004682 case RTL_GIGA_MAC_VER_17 ... RTL_GIGA_MAC_VER_28:
hayeswang01dc7fe2011-03-21 01:50:28 +00004683 case RTL_GIGA_MAC_VER_31:
4684 rtl_writephy(tp, 0x0e, 0x0200);
4685 default:
4686 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4687 break;
4688 }
françois romieu065c27c2011-01-03 15:08:12 +00004689}
4690
4691static void r8168_pll_power_down(struct rtl8169_private *tp)
4692{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01004693 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004694 return;
4695
hayeswang01dc7fe2011-03-21 01:50:28 +00004696 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4697 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004698 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004699
David S. Miller1805b2f2011-10-24 18:18:09 -04004700 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004701 return;
françois romieu065c27c2011-01-03 15:08:12 +00004702
4703 r8168_phy_power_down(tp);
4704
4705 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004706 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004707 case RTL_GIGA_MAC_VER_37:
4708 case RTL_GIGA_MAC_VER_39:
4709 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004710 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004711 case RTL_GIGA_MAC_VER_45:
4712 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004713 case RTL_GIGA_MAC_VER_47:
4714 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004715 case RTL_GIGA_MAC_VER_50:
4716 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004717 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004718 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004719 case RTL_GIGA_MAC_VER_40:
4720 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004721 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004722 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004723 0xfc000000, ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004724 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004725 break;
françois romieu065c27c2011-01-03 15:08:12 +00004726 }
4727}
4728
4729static void r8168_pll_power_up(struct rtl8169_private *tp)
4730{
françois romieu065c27c2011-01-03 15:08:12 +00004731 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004732 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004733 case RTL_GIGA_MAC_VER_37:
4734 case RTL_GIGA_MAC_VER_39:
4735 case RTL_GIGA_MAC_VER_43:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004736 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004737 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004738 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004739 case RTL_GIGA_MAC_VER_45:
4740 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004741 case RTL_GIGA_MAC_VER_47:
4742 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004743 case RTL_GIGA_MAC_VER_50:
4744 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004745 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004746 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004747 case RTL_GIGA_MAC_VER_40:
4748 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004749 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004750 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004751 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004752 0x00000000, ERIAR_EXGMAC);
4753 break;
françois romieu065c27c2011-01-03 15:08:12 +00004754 }
4755
4756 r8168_phy_power_up(tp);
4757}
4758
françois romieu065c27c2011-01-03 15:08:12 +00004759static void rtl_pll_power_down(struct rtl8169_private *tp)
4760{
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004761 switch (tp->mac_version) {
4762 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4763 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4764 break;
4765 default:
4766 r8168_pll_power_down(tp);
4767 }
françois romieu065c27c2011-01-03 15:08:12 +00004768}
4769
4770static void rtl_pll_power_up(struct rtl8169_private *tp)
4771{
françois romieu065c27c2011-01-03 15:08:12 +00004772 switch (tp->mac_version) {
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004773 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4774 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
françois romieu065c27c2011-01-03 15:08:12 +00004775 break;
françois romieu065c27c2011-01-03 15:08:12 +00004776 default:
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004777 r8168_pll_power_up(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004778 }
4779}
4780
Hayes Wange542a222011-07-06 15:58:04 +08004781static void rtl_init_rxcfg(struct rtl8169_private *tp)
4782{
Hayes Wange542a222011-07-06 15:58:04 +08004783 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004784 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4785 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004786 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004787 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004788 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004789 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02004790 case RTL_GIGA_MAC_VER_35:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004791 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004792 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004793 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004794 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00004795 break;
Hayes Wange542a222011-07-06 15:58:04 +08004796 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004797 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004798 break;
4799 }
4800}
4801
Hayes Wang92fc43b2011-07-06 15:58:03 +08004802static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4803{
Timo Teräs9fba0812013-01-15 21:01:24 +00004804 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004805}
4806
Francois Romieud58d46b2011-05-03 16:38:29 +02004807static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4808{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004809 if (tp->jumbo_ops.enable) {
4810 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4811 tp->jumbo_ops.enable(tp);
4812 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4813 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004814}
4815
4816static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4817{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004818 if (tp->jumbo_ops.disable) {
4819 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4820 tp->jumbo_ops.disable(tp);
4821 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4822 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004823}
4824
4825static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4826{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004827 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4828 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004829 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004830}
4831
4832static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4833{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004834 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4835 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004836 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004837}
4838
4839static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4840{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004841 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004842}
4843
4844static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4845{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004846 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004847}
4848
4849static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4850{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004851 RTL_W8(tp, MaxTxPacketSize, 0x3f);
4852 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4853 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004854 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004855}
4856
4857static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4858{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004859 RTL_W8(tp, MaxTxPacketSize, 0x0c);
4860 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4861 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004862 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004863}
4864
4865static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4866{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004867 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01004868 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004869}
4870
4871static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4872{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004873 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004874 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004875}
4876
4877static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4878{
Francois Romieud58d46b2011-05-03 16:38:29 +02004879 r8168b_0_hw_jumbo_enable(tp);
4880
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004881 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004882}
4883
4884static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4885{
Francois Romieud58d46b2011-05-03 16:38:29 +02004886 r8168b_0_hw_jumbo_disable(tp);
4887
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004888 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004889}
4890
Bill Pembertonbaf63292012-12-03 09:23:28 -05004891static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02004892{
4893 struct jumbo_ops *ops = &tp->jumbo_ops;
4894
4895 switch (tp->mac_version) {
4896 case RTL_GIGA_MAC_VER_11:
4897 ops->disable = r8168b_0_hw_jumbo_disable;
4898 ops->enable = r8168b_0_hw_jumbo_enable;
4899 break;
4900 case RTL_GIGA_MAC_VER_12:
4901 case RTL_GIGA_MAC_VER_17:
4902 ops->disable = r8168b_1_hw_jumbo_disable;
4903 ops->enable = r8168b_1_hw_jumbo_enable;
4904 break;
4905 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4906 case RTL_GIGA_MAC_VER_19:
4907 case RTL_GIGA_MAC_VER_20:
4908 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4909 case RTL_GIGA_MAC_VER_22:
4910 case RTL_GIGA_MAC_VER_23:
4911 case RTL_GIGA_MAC_VER_24:
4912 case RTL_GIGA_MAC_VER_25:
4913 case RTL_GIGA_MAC_VER_26:
4914 ops->disable = r8168c_hw_jumbo_disable;
4915 ops->enable = r8168c_hw_jumbo_enable;
4916 break;
4917 case RTL_GIGA_MAC_VER_27:
4918 case RTL_GIGA_MAC_VER_28:
4919 ops->disable = r8168dp_hw_jumbo_disable;
4920 ops->enable = r8168dp_hw_jumbo_enable;
4921 break;
4922 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4923 case RTL_GIGA_MAC_VER_32:
4924 case RTL_GIGA_MAC_VER_33:
4925 case RTL_GIGA_MAC_VER_34:
4926 ops->disable = r8168e_hw_jumbo_disable;
4927 ops->enable = r8168e_hw_jumbo_enable;
4928 break;
4929
4930 /*
4931 * No action needed for jumbo frames with 8169.
4932 * No jumbo for 810x at all.
4933 */
Heiner Kallweit2a718832018-05-02 21:39:49 +02004934 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02004935 default:
4936 ops->disable = NULL;
4937 ops->enable = NULL;
4938 break;
4939 }
4940}
4941
Francois Romieuffc46952012-07-06 14:19:23 +02004942DECLARE_RTL_COND(rtl_chipcmd_cond)
4943{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004944 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02004945}
4946
Francois Romieu6f43adc2011-04-29 15:05:51 +02004947static void rtl_hw_reset(struct rtl8169_private *tp)
4948{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004949 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004950
Francois Romieuffc46952012-07-06 14:19:23 +02004951 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004952}
4953
Francois Romieub6ffd972011-06-17 17:00:05 +02004954static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4955{
4956 struct rtl_fw *rtl_fw;
4957 const char *name;
4958 int rc = -ENOMEM;
4959
4960 name = rtl_lookup_firmware_name(tp);
4961 if (!name)
4962 goto out_no_firmware;
4963
4964 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4965 if (!rtl_fw)
4966 goto err_warn;
4967
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004968 rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
Francois Romieub6ffd972011-06-17 17:00:05 +02004969 if (rc < 0)
4970 goto err_free;
4971
Francois Romieufd112f22011-06-18 00:10:29 +02004972 rc = rtl_check_firmware(tp, rtl_fw);
4973 if (rc < 0)
4974 goto err_release_firmware;
4975
Francois Romieub6ffd972011-06-17 17:00:05 +02004976 tp->rtl_fw = rtl_fw;
4977out:
4978 return;
4979
Francois Romieufd112f22011-06-18 00:10:29 +02004980err_release_firmware:
4981 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004982err_free:
4983 kfree(rtl_fw);
4984err_warn:
4985 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4986 name, rc);
4987out_no_firmware:
4988 tp->rtl_fw = NULL;
4989 goto out;
4990}
4991
François Romieu953a12c2011-04-24 17:38:48 +02004992static void rtl_request_firmware(struct rtl8169_private *tp)
4993{
Francois Romieub6ffd972011-06-17 17:00:05 +02004994 if (IS_ERR(tp->rtl_fw))
4995 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004996}
4997
Hayes Wang92fc43b2011-07-06 15:58:03 +08004998static void rtl_rx_close(struct rtl8169_private *tp)
4999{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005000 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005001}
5002
Francois Romieuffc46952012-07-06 14:19:23 +02005003DECLARE_RTL_COND(rtl_npq_cond)
5004{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005005 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02005006}
5007
5008DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5009{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005010 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02005011}
5012
françois romieue6de30d2011-01-03 15:08:37 +00005013static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014{
5015 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00005016 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017
Hayes Wang92fc43b2011-07-06 15:58:03 +08005018 rtl_rx_close(tp);
5019
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02005020 switch (tp->mac_version) {
5021 case RTL_GIGA_MAC_VER_27:
5022 case RTL_GIGA_MAC_VER_28:
5023 case RTL_GIGA_MAC_VER_31:
Francois Romieuffc46952012-07-06 14:19:23 +02005024 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02005025 break;
5026 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
5027 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005028 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02005029 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02005030 break;
5031 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005032 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005033 udelay(100);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02005034 break;
françois romieue6de30d2011-01-03 15:08:37 +00005035 }
5036
Hayes Wang92fc43b2011-07-06 15:58:03 +08005037 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038}
5039
Francois Romieu7f796d832007-06-11 23:04:41 +02005040static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005041{
Francois Romieu9cb427b2006-11-02 00:10:16 +01005042 /* Set DMA burst size and Interframe Gap Time */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005043 RTL_W32(tp, TxConfig, (TX_DMA_BURST << TxDMAShift) |
Francois Romieu9cb427b2006-11-02 00:10:16 +01005044 (InterFrameGap << TxInterFrameGapShift));
5045}
5046
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02005047static void rtl_set_rx_max_size(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02005049 /* Low hurts. Let's disable the filtering. */
5050 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
Francois Romieu07ce4062007-02-23 23:36:39 +01005051}
5052
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005053static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02005054{
5055 /*
5056 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5057 * register to be written before TxDescAddrLow to work.
5058 * Switching from MMIO to I/O access fixes the issue as well.
5059 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005060 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
5061 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
5062 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
5063 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005064}
5065
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005066static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01005067{
Francois Romieu37441002011-06-17 22:58:54 +02005068 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01005069 u32 mac_version;
5070 u32 clk;
5071 u32 val;
5072 } cfg2_info [] = {
5073 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5074 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5075 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5076 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02005077 };
5078 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01005079 unsigned int i;
5080 u32 clk;
5081
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005082 clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01005083 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01005084 if ((p->mac_version == mac_version) && (p->clk == clk)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005085 RTL_W32(tp, 0x7c, p->val);
Francois Romieu6dccd162007-02-13 23:38:05 +01005086 break;
5087 }
5088 }
5089}
5090
Francois Romieue6b763e2012-03-08 09:35:39 +01005091static void rtl_set_rx_mode(struct net_device *dev)
5092{
5093 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieue6b763e2012-03-08 09:35:39 +01005094 u32 mc_filter[2]; /* Multicast hash filter */
5095 int rx_mode;
5096 u32 tmp = 0;
5097
5098 if (dev->flags & IFF_PROMISC) {
5099 /* Unconditionally log net taps. */
5100 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5101 rx_mode =
5102 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5103 AcceptAllPhys;
5104 mc_filter[1] = mc_filter[0] = 0xffffffff;
5105 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5106 (dev->flags & IFF_ALLMULTI)) {
5107 /* Too many to filter perfectly -- accept all multicasts. */
5108 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5109 mc_filter[1] = mc_filter[0] = 0xffffffff;
5110 } else {
5111 struct netdev_hw_addr *ha;
5112
5113 rx_mode = AcceptBroadcast | AcceptMyPhys;
5114 mc_filter[1] = mc_filter[0] = 0;
5115 netdev_for_each_mc_addr(ha, dev) {
5116 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5117 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5118 rx_mode |= AcceptMulticast;
5119 }
5120 }
5121
5122 if (dev->features & NETIF_F_RXALL)
5123 rx_mode |= (AcceptErr | AcceptRunt);
5124
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005125 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Francois Romieue6b763e2012-03-08 09:35:39 +01005126
5127 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5128 u32 data = mc_filter[0];
5129
5130 mc_filter[0] = swab32(mc_filter[1]);
5131 mc_filter[1] = swab32(data);
5132 }
5133
Nathan Walp04817762012-11-01 12:08:47 +00005134 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5135 mc_filter[1] = mc_filter[0] = 0xffffffff;
5136
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005137 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
5138 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01005139
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005140 RTL_W32(tp, RxConfig, tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01005141}
5142
Heiner Kallweit52f85602018-05-19 10:29:33 +02005143static void rtl_hw_start(struct rtl8169_private *tp)
5144{
5145 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
5146
5147 tp->hw_start(tp);
5148
5149 rtl_set_rx_max_size(tp);
5150 rtl_set_rx_tx_desc_registers(tp);
5151 rtl_set_rx_tx_config_registers(tp);
5152 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
5153
5154 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5155 RTL_R8(tp, IntrMask);
5156 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
5157 rtl_set_rx_mode(tp->dev);
5158 /* no early-rx interrupts */
5159 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
5160 rtl_irq_enable_all(tp);
5161}
5162
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005163static void rtl_hw_start_8169(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005164{
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005165 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005166 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005167
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005168 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005170 tp->cp_cmd |= PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005171
Francois Romieucecb5fd2011-04-01 10:21:07 +02005172 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5173 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005174 dprintk("Set MAC Reg C+CR Offset 0xe0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005176 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 }
5178
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005179 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005180
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005181 rtl8169_set_magic_reg(tp, tp->mac_version);
Francois Romieu6dccd162007-02-13 23:38:05 +01005182
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 /*
5184 * Undocumented corner. Supposedly:
5185 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5186 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005187 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005189 RTL_W32(tp, RxMissed, 0);
Francois Romieu07ce4062007-02-23 23:36:39 +01005190}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191
Francois Romieuffc46952012-07-06 14:19:23 +02005192DECLARE_RTL_COND(rtl_csiar_cond)
5193{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005194 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02005195}
5196
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005197static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005198{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005199 u32 func = PCI_FUNC(tp->pci_dev->devfn);
5200
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005201 RTL_W32(tp, CSIDR, value);
5202 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005203 CSIAR_BYTE_ENABLE | func << 16);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005204
Francois Romieuffc46952012-07-06 14:19:23 +02005205 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005206}
5207
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005208static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005209{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005210 u32 func = PCI_FUNC(tp->pci_dev->devfn);
5211
5212 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
5213 CSIAR_BYTE_ENABLE);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005214
Francois Romieuffc46952012-07-06 14:19:23 +02005215 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005216 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005217}
5218
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005219static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005220{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005221 struct pci_dev *pdev = tp->pci_dev;
5222 u32 csi;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005223
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005224 /* According to Realtek the value at config space address 0x070f
5225 * controls the L0s/L1 entrance latency. We try standard ECAM access
5226 * first and if it fails fall back to CSI.
5227 */
5228 if (pdev->cfg_size > 0x070f &&
5229 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
5230 return;
5231
5232 netdev_notice_once(tp->dev,
5233 "No native access to PCI extended config space, falling back to CSI\n");
5234 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5235 rtl_csi_write(tp, 0x070c, csi | val << 24);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005236}
5237
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005238static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
hayeswang45dd95c2013-07-08 17:09:01 +08005239{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005240 rtl_csi_access_enable(tp, 0x27);
Francois Romieudacf8152008-08-02 20:44:13 +02005241}
5242
5243struct ephy_info {
5244 unsigned int offset;
5245 u16 mask;
5246 u16 bits;
5247};
5248
Francois Romieufdf6fc02012-07-06 22:40:38 +02005249static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5250 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005251{
5252 u16 w;
5253
5254 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005255 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5256 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005257 e++;
5258 }
5259}
5260
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005261static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02005262{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005263 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005264 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005265}
5266
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005267static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005268{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005269 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005270 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005271}
5272
hayeswangb51ecea2014-07-09 14:52:51 +08005273static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5274{
hayeswangb51ecea2014-07-09 14:52:51 +08005275 u8 data;
5276
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005277 data = RTL_R8(tp, Config3);
hayeswangb51ecea2014-07-09 14:52:51 +08005278
5279 if (enable)
5280 data |= Rdy_to_L23;
5281 else
5282 data &= ~Rdy_to_L23;
5283
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005284 RTL_W8(tp, Config3, data);
hayeswangb51ecea2014-07-09 14:52:51 +08005285}
5286
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005287static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
5288{
5289 if (enable) {
5290 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
5291 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
5292 } else {
5293 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5294 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
5295 }
5296}
5297
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005298static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005299{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005300 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005301
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005302 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005303 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieub726e492008-06-28 12:22:59 +02005304
françois romieufaf1e782013-02-27 13:01:57 +00005305 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005306 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00005307 PCI_EXP_DEVCTL_NOSNOOP_EN);
5308 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005309}
5310
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005311static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005312{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005313 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005314
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005315 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005316
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005317 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005318}
5319
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005320static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005321{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005322 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02005323
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005324 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005325
françois romieufaf1e782013-02-27 13:01:57 +00005326 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005327 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02005328
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005329 rtl_disable_clock_request(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005330
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005331 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005332 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu219a1e92008-06-28 11:58:39 +02005333}
5334
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005335static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005336{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005337 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005338 { 0x01, 0, 0x0001 },
5339 { 0x02, 0x0800, 0x1000 },
5340 { 0x03, 0, 0x0042 },
5341 { 0x06, 0x0080, 0x0000 },
5342 { 0x07, 0, 0x2000 }
5343 };
5344
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005345 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005346
Francois Romieufdf6fc02012-07-06 22:40:38 +02005347 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005348
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005349 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005350}
5351
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005352static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005353{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005354 rtl_set_def_aspm_entry_latency(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005355
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005356 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02005357
françois romieufaf1e782013-02-27 13:01:57 +00005358 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005359 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieuef3386f2008-06-29 12:24:30 +02005360
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005361 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005362 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieuef3386f2008-06-29 12:24:30 +02005363}
5364
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005365static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005366{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005367 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005368
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005369 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005370
5371 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005372 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005373
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005374 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005375
françois romieufaf1e782013-02-27 13:01:57 +00005376 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005377 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005378
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005379 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005380 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005381}
5382
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005383static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005384{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005385 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005386 { 0x02, 0x0800, 0x1000 },
5387 { 0x03, 0, 0x0002 },
5388 { 0x06, 0x0080, 0x0000 }
5389 };
5390
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005391 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005392
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005393 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02005394
Francois Romieufdf6fc02012-07-06 22:40:38 +02005395 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005396
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005397 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005398}
5399
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005400static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005401{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005402 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005403 { 0x01, 0, 0x0001 },
5404 { 0x03, 0x0400, 0x0220 }
5405 };
5406
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005407 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005408
Francois Romieufdf6fc02012-07-06 22:40:38 +02005409 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005410
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005411 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005412}
5413
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005414static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005415{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005416 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005417}
5418
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005419static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005420{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005421 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005422
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005423 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005424}
5425
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005426static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005427{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005428 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005429
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005430 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005431
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005432 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005433
françois romieufaf1e782013-02-27 13:01:57 +00005434 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005435 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02005436
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005437 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005438 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu5b538df2008-07-20 16:22:45 +02005439}
5440
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005441static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005442{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005443 rtl_set_def_aspm_entry_latency(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005444
françois romieufaf1e782013-02-27 13:01:57 +00005445 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005446 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang4804b3b2011-03-21 01:50:29 +00005447
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005448 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang4804b3b2011-03-21 01:50:29 +00005449
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005450 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005451}
5452
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005453static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005454{
5455 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005456 { 0x0b, 0x0000, 0x0048 },
5457 { 0x19, 0x0020, 0x0050 },
5458 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00005459 };
françois romieue6de30d2011-01-03 15:08:37 +00005460
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005461 rtl_set_def_aspm_entry_latency(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005462
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005463 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00005464
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005465 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
françois romieue6de30d2011-01-03 15:08:37 +00005466
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005467 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00005468
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005469 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005470}
5471
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005472static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005473{
Hayes Wang70090422011-07-06 15:58:06 +08005474 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005475 { 0x00, 0x0200, 0x0100 },
5476 { 0x00, 0x0000, 0x0004 },
5477 { 0x06, 0x0002, 0x0001 },
5478 { 0x06, 0x0000, 0x0030 },
5479 { 0x07, 0x0000, 0x2000 },
5480 { 0x00, 0x0000, 0x0020 },
5481 { 0x03, 0x5800, 0x2000 },
5482 { 0x03, 0x0000, 0x0001 },
5483 { 0x01, 0x0800, 0x1000 },
5484 { 0x07, 0x0000, 0x4000 },
5485 { 0x1e, 0x0000, 0x2000 },
5486 { 0x19, 0xffff, 0xfe6c },
5487 { 0x0a, 0x0000, 0x0040 }
5488 };
5489
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005490 rtl_set_def_aspm_entry_latency(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005491
Francois Romieufdf6fc02012-07-06 22:40:38 +02005492 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005493
françois romieufaf1e782013-02-27 13:01:57 +00005494 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005495 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00005496
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005497 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang01dc7fe2011-03-21 01:50:28 +00005498
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005499 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005500
5501 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005502 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
5503 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005504
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005505 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005506}
5507
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005508static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005509{
5510 static const struct ephy_info e_info_8168e_2[] = {
5511 { 0x09, 0x0000, 0x0080 },
5512 { 0x19, 0x0000, 0x0224 }
5513 };
5514
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005515 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005516
Francois Romieufdf6fc02012-07-06 22:40:38 +02005517 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005518
françois romieufaf1e782013-02-27 13:01:57 +00005519 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005520 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08005521
Francois Romieufdf6fc02012-07-06 22:40:38 +02005522 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5523 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5524 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5525 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5526 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5527 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005528 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5529 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005530
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005531 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005532
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005533 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005534
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005535 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5536 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08005537
5538 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005539 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang70090422011-07-06 15:58:06 +08005540
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005541 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5542 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5543 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08005544}
5545
Hayes Wang5f886e02012-03-30 14:33:03 +08005546static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005547{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005548 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005549
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005550 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08005551
Francois Romieufdf6fc02012-07-06 22:40:38 +02005552 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5553 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5554 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5555 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005556 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5557 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5558 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5559 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005560 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5561 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005562
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005563 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc2218922011-09-06 16:55:18 +08005564
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005565 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005566
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005567 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5568 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5569 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5570 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5571 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005572}
5573
Hayes Wang5f886e02012-03-30 14:33:03 +08005574static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5575{
Hayes Wang5f886e02012-03-30 14:33:03 +08005576 static const struct ephy_info e_info_8168f_1[] = {
5577 { 0x06, 0x00c0, 0x0020 },
5578 { 0x08, 0x0001, 0x0002 },
5579 { 0x09, 0x0000, 0x0080 },
5580 { 0x19, 0x0000, 0x0224 }
5581 };
5582
5583 rtl_hw_start_8168f(tp);
5584
Francois Romieufdf6fc02012-07-06 22:40:38 +02005585 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005586
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005587 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005588
5589 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005590 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang5f886e02012-03-30 14:33:03 +08005591}
5592
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005593static void rtl_hw_start_8411(struct rtl8169_private *tp)
5594{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005595 static const struct ephy_info e_info_8168f_1[] = {
5596 { 0x06, 0x00c0, 0x0020 },
5597 { 0x0f, 0xffff, 0x5200 },
5598 { 0x1e, 0x0000, 0x4000 },
5599 { 0x19, 0x0000, 0x0224 }
5600 };
5601
5602 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08005603 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005604
Francois Romieufdf6fc02012-07-06 22:40:38 +02005605 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005606
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005607 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005608}
5609
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005610static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08005611{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005612 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
hayeswangbeb330a2013-04-01 22:23:39 +00005613
Hayes Wangc5583862012-07-02 17:23:22 +08005614 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5615 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5616 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5617 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5618
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005619 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08005620
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005621 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08005622
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005623 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5624 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00005625 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005626
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005627 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5628 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc5583862012-07-02 17:23:22 +08005629
5630 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5631 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5632
5633 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005634 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wangc5583862012-07-02 17:23:22 +08005635
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005636 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5637 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005638
5639 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08005640}
5641
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005642static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5643{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005644 static const struct ephy_info e_info_8168g_1[] = {
5645 { 0x00, 0x0000, 0x0008 },
5646 { 0x0c, 0x37d0, 0x0820 },
5647 { 0x1e, 0x0000, 0x0001 },
5648 { 0x19, 0x8000, 0x0000 }
5649 };
5650
5651 rtl_hw_start_8168g(tp);
5652
5653 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005654 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005655 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005656 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005657}
5658
hayeswang57538c42013-04-01 22:23:40 +00005659static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5660{
hayeswang57538c42013-04-01 22:23:40 +00005661 static const struct ephy_info e_info_8168g_2[] = {
5662 { 0x00, 0x0000, 0x0008 },
5663 { 0x0c, 0x3df0, 0x0200 },
5664 { 0x19, 0xffff, 0xfc00 },
5665 { 0x1e, 0xffff, 0x20eb }
5666 };
5667
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005668 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00005669
5670 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005671 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5672 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang57538c42013-04-01 22:23:40 +00005673 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5674}
5675
hayeswang45dd95c2013-07-08 17:09:01 +08005676static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5677{
hayeswang45dd95c2013-07-08 17:09:01 +08005678 static const struct ephy_info e_info_8411_2[] = {
5679 { 0x00, 0x0000, 0x0008 },
5680 { 0x0c, 0x3df0, 0x0200 },
5681 { 0x0f, 0xffff, 0x5200 },
5682 { 0x19, 0x0020, 0x0000 },
5683 { 0x1e, 0x0000, 0x2000 }
5684 };
5685
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005686 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08005687
5688 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005689 rtl_hw_aspm_clkreq_enable(tp, false);
hayeswang45dd95c2013-07-08 17:09:01 +08005690 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005691 rtl_hw_aspm_clkreq_enable(tp, true);
hayeswang45dd95c2013-07-08 17:09:01 +08005692}
5693
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005694static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
5695{
Andrzej Hajda72521ea2015-09-24 16:00:24 +02005696 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005697 u32 data;
5698 static const struct ephy_info e_info_8168h_1[] = {
5699 { 0x1e, 0x0800, 0x0001 },
5700 { 0x1d, 0x0000, 0x0800 },
5701 { 0x05, 0xffff, 0x2089 },
5702 { 0x06, 0xffff, 0x5881 },
5703 { 0x04, 0xffff, 0x154a },
5704 { 0x01, 0xffff, 0x068b }
5705 };
5706
5707 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005708 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005709 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
5710
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005711 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005712
5713 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5714 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5715 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5716 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5717
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005718 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005719
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005720 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005721
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005722 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5723 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005724
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005725 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005726
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005727 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005728
5729 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5730
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005731 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5732 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005733
5734 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5735 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5736
5737 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005738 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005739
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005740 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5741 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005742
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005743 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005744
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005745 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005746
5747 rtl_pcie_state_l2l3_enable(tp, false);
5748
5749 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08005750 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005751 rtl_writephy(tp, 0x1f, 0x0000);
5752 if (rg_saw_cnt > 0) {
5753 u16 sw_cnt_1ms_ini;
5754
5755 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
5756 sw_cnt_1ms_ini &= 0x0fff;
5757 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005758 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005759 data |= sw_cnt_1ms_ini;
5760 r8168_mac_ocp_write(tp, 0xd412, data);
5761 }
5762
5763 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005764 data &= ~0xf0;
5765 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005766 r8168_mac_ocp_write(tp, 0xe056, data);
5767
5768 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005769 data &= ~0x6000;
5770 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005771 r8168_mac_ocp_write(tp, 0xe052, data);
5772
5773 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005774 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005775 data |= 0x017f;
5776 r8168_mac_ocp_write(tp, 0xe0d6, data);
5777
5778 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005779 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005780 data |= 0x047f;
5781 r8168_mac_ocp_write(tp, 0xd420, data);
5782
5783 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
5784 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
5785 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
5786 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005787
5788 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005789}
5790
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005791static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
5792{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08005793 rtl8168ep_stop_cmac(tp);
5794
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005795 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005796
5797 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5798 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
5799 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
5800 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5801
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005802 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005803
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005804 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005805
5806 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5807 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5808
5809 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
5810
5811 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5812
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005813 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5814 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005815
5816 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5817 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5818
5819 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005820 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005821
5822 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5823
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005824 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005825
5826 rtl_pcie_state_l2l3_enable(tp, false);
5827}
5828
5829static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
5830{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005831 static const struct ephy_info e_info_8168ep_1[] = {
5832 { 0x00, 0xffff, 0x10ab },
5833 { 0x06, 0xffff, 0xf030 },
5834 { 0x08, 0xffff, 0x2006 },
5835 { 0x0d, 0xffff, 0x1666 },
5836 { 0x0c, 0x3ff0, 0x0000 }
5837 };
5838
5839 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005840 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005841 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
5842
5843 rtl_hw_start_8168ep(tp);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005844
5845 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005846}
5847
5848static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
5849{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005850 static const struct ephy_info e_info_8168ep_2[] = {
5851 { 0x00, 0xffff, 0x10a3 },
5852 { 0x19, 0xffff, 0xfc00 },
5853 { 0x1e, 0xffff, 0x20ea }
5854 };
5855
5856 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005857 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005858 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
5859
5860 rtl_hw_start_8168ep(tp);
5861
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005862 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5863 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005864
5865 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005866}
5867
5868static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
5869{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005870 u32 data;
5871 static const struct ephy_info e_info_8168ep_3[] = {
5872 { 0x00, 0xffff, 0x10a3 },
5873 { 0x19, 0xffff, 0x7c00 },
5874 { 0x1e, 0xffff, 0x20eb },
5875 { 0x0d, 0xffff, 0x1666 }
5876 };
5877
5878 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005879 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005880 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
5881
5882 rtl_hw_start_8168ep(tp);
5883
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005884 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5885 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005886
5887 data = r8168_mac_ocp_read(tp, 0xd3e2);
5888 data &= 0xf000;
5889 data |= 0x0271;
5890 r8168_mac_ocp_write(tp, 0xd3e2, data);
5891
5892 data = r8168_mac_ocp_read(tp, 0xd3e4);
5893 data &= 0xff00;
5894 r8168_mac_ocp_write(tp, 0xd3e4, data);
5895
5896 data = r8168_mac_ocp_read(tp, 0xe860);
5897 data |= 0x0080;
5898 r8168_mac_ocp_write(tp, 0xe860, data);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005899
5900 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005901}
5902
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005903static void rtl_hw_start_8168(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005904{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005905 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02005906
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005907 tp->cp_cmd &= ~INTT_MASK;
5908 tp->cp_cmd |= PktCntrDisable | INTT_1;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005909 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu2dd99532007-06-11 23:22:52 +02005910
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005911 RTL_W16(tp, IntrMitigate, 0x5151);
Francois Romieu0e485152007-02-20 00:00:26 +01005912
5913 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00005914 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005915 tp->event_slow |= RxFIFOOver | PCSTimeout;
5916 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01005917 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005918
Francois Romieu219a1e92008-06-28 11:58:39 +02005919 switch (tp->mac_version) {
5920 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005921 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005922 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005923
5924 case RTL_GIGA_MAC_VER_12:
5925 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005926 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005927 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005928
5929 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005930 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005931 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005932
5933 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005934 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005935 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005936
5937 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005938 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005939 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005940
Francois Romieu197ff762008-06-28 13:16:02 +02005941 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005942 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005943 break;
Francois Romieu197ff762008-06-28 13:16:02 +02005944
Francois Romieu6fb07052008-06-29 11:54:28 +02005945 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005946 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005947 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02005948
Francois Romieuef3386f2008-06-29 12:24:30 +02005949 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005950 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005951 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02005952
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005953 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005954 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005955 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005956
Francois Romieu5b538df2008-07-20 16:22:45 +02005957 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00005958 case RTL_GIGA_MAC_VER_26:
5959 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005960 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005961 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02005962
françois romieue6de30d2011-01-03 15:08:37 +00005963 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005964 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005965 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005966
hayeswang4804b3b2011-03-21 01:50:29 +00005967 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005968 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005969 break;
5970
hayeswang01dc7fe2011-03-21 01:50:28 +00005971 case RTL_GIGA_MAC_VER_32:
5972 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005973 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005974 break;
5975 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005976 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005977 break;
françois romieue6de30d2011-01-03 15:08:37 +00005978
Hayes Wangc2218922011-09-06 16:55:18 +08005979 case RTL_GIGA_MAC_VER_35:
5980 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005981 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005982 break;
5983
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005984 case RTL_GIGA_MAC_VER_38:
5985 rtl_hw_start_8411(tp);
5986 break;
5987
Hayes Wangc5583862012-07-02 17:23:22 +08005988 case RTL_GIGA_MAC_VER_40:
5989 case RTL_GIGA_MAC_VER_41:
5990 rtl_hw_start_8168g_1(tp);
5991 break;
hayeswang57538c42013-04-01 22:23:40 +00005992 case RTL_GIGA_MAC_VER_42:
5993 rtl_hw_start_8168g_2(tp);
5994 break;
Hayes Wangc5583862012-07-02 17:23:22 +08005995
hayeswang45dd95c2013-07-08 17:09:01 +08005996 case RTL_GIGA_MAC_VER_44:
5997 rtl_hw_start_8411_2(tp);
5998 break;
5999
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006000 case RTL_GIGA_MAC_VER_45:
6001 case RTL_GIGA_MAC_VER_46:
6002 rtl_hw_start_8168h_1(tp);
6003 break;
6004
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006005 case RTL_GIGA_MAC_VER_49:
6006 rtl_hw_start_8168ep_1(tp);
6007 break;
6008
6009 case RTL_GIGA_MAC_VER_50:
6010 rtl_hw_start_8168ep_2(tp);
6011 break;
6012
6013 case RTL_GIGA_MAC_VER_51:
6014 rtl_hw_start_8168ep_3(tp);
6015 break;
6016
Francois Romieu219a1e92008-06-28 11:58:39 +02006017 default:
6018 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006019 tp->dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00006020 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006021 }
Francois Romieu07ce4062007-02-23 23:36:39 +01006022}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006024static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006025{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006026 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02006027 { 0x01, 0, 0x6e65 },
6028 { 0x02, 0, 0x091f },
6029 { 0x03, 0, 0xc2f9 },
6030 { 0x06, 0, 0xafb5 },
6031 { 0x07, 0, 0x0e00 },
6032 { 0x19, 0, 0xec80 },
6033 { 0x01, 0, 0x2e65 },
6034 { 0x01, 0, 0x6e65 }
6035 };
6036 u8 cfg1;
6037
Heiner Kallweitf37658d2018-06-23 09:51:28 +02006038 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006039
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006040 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006041
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006042 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006043
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006044 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02006045 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006046 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006047
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006048 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006049 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006050 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006051
Francois Romieufdf6fc02012-07-06 22:40:38 +02006052 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02006053}
6054
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006055static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006056{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02006057 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006058
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006059 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006060
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006061 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
6062 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006063}
6064
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006065static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006066{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006067 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006068
Francois Romieufdf6fc02012-07-06 22:40:38 +02006069 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006070}
6071
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006072static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006073{
6074 static const struct ephy_info e_info_8105e_1[] = {
6075 { 0x07, 0, 0x4000 },
6076 { 0x19, 0, 0x0200 },
6077 { 0x19, 0, 0x0020 },
6078 { 0x1e, 0, 0x2000 },
6079 { 0x03, 0, 0x0001 },
6080 { 0x19, 0, 0x0100 },
6081 { 0x19, 0, 0x0004 },
6082 { 0x0a, 0, 0x0020 }
6083 };
6084
Francois Romieucecb5fd2011-04-01 10:21:07 +02006085 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006086 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006087
Francois Romieucecb5fd2011-04-01 10:21:07 +02006088 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006089 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006090
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006091 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6092 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006093
Francois Romieufdf6fc02012-07-06 22:40:38 +02006094 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08006095
6096 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006097}
6098
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006099static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006100{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006101 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006102 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006103}
6104
Hayes Wang7e18dca2012-03-30 14:33:02 +08006105static void rtl_hw_start_8402(struct rtl8169_private *tp)
6106{
Hayes Wang7e18dca2012-03-30 14:33:02 +08006107 static const struct ephy_info e_info_8402[] = {
6108 { 0x19, 0xffff, 0xff64 },
6109 { 0x1e, 0, 0x4000 }
6110 };
6111
Heiner Kallweitf37658d2018-06-23 09:51:28 +02006112 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006113
6114 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006115 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006116
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006117 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
6118 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006119
Francois Romieufdf6fc02012-07-06 22:40:38 +02006120 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006121
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006122 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006123
Francois Romieufdf6fc02012-07-06 22:40:38 +02006124 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6125 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006126 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6127 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006128 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6129 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006130 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006131
6132 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006133}
6134
Hayes Wang5598bfe2012-07-02 17:23:21 +08006135static void rtl_hw_start_8106(struct rtl8169_private *tp)
6136{
Hayes Wang5598bfe2012-07-02 17:23:21 +08006137 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006138 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006139
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006140 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
6141 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6142 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006143
6144 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006145}
6146
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006147static void rtl_hw_start_8101(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01006148{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006149 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6150 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006151
Francois Romieucecb5fd2011-04-01 10:21:07 +02006152 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006153 tp->mac_version == RTL_GIGA_MAC_VER_16)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006154 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006155 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006156
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006157 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00006158
Heiner Kallweit12d42c52018-04-28 22:19:30 +02006159 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006160 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
hayeswang1a964642013-04-01 22:23:41 +00006161
Francois Romieu2857ffb2008-08-02 21:08:49 +02006162 switch (tp->mac_version) {
6163 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006164 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006165 break;
6166
6167 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006168 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006169 break;
6170
6171 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006172 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006173 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006174
6175 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006176 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006177 break;
6178 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006179 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006180 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006181
6182 case RTL_GIGA_MAC_VER_37:
6183 rtl_hw_start_8402(tp);
6184 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006185
6186 case RTL_GIGA_MAC_VER_39:
6187 rtl_hw_start_8106(tp);
6188 break;
hayeswang58152cd2013-04-01 22:23:42 +00006189 case RTL_GIGA_MAC_VER_43:
6190 rtl_hw_start_8168g_2(tp);
6191 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006192 case RTL_GIGA_MAC_VER_47:
6193 case RTL_GIGA_MAC_VER_48:
6194 rtl_hw_start_8168h_1(tp);
6195 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006196 }
6197
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006198 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199}
6200
6201static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6202{
Francois Romieud58d46b2011-05-03 16:38:29 +02006203 struct rtl8169_private *tp = netdev_priv(dev);
6204
Francois Romieud58d46b2011-05-03 16:38:29 +02006205 if (new_mtu > ETH_DATA_LEN)
6206 rtl_hw_jumbo_enable(tp);
6207 else
6208 rtl_hw_jumbo_disable(tp);
6209
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006211 netdev_update_features(dev);
6212
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006213 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214}
6215
6216static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6217{
Al Viro95e09182007-12-22 18:55:39 +00006218 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6220}
6221
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006222static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6223 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224{
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006225 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
6226 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006227
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006228 kfree(*data_buff);
6229 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 rtl8169_make_unusable_by_asic(desc);
6231}
6232
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006233static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234{
6235 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6236
Alexander Duycka0750132014-12-11 15:02:17 -08006237 /* Force memory writes to complete before releasing descriptor */
6238 dma_wmb();
6239
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006240 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241}
6242
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006243static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006245 return (void *)ALIGN((long)data, 16);
6246}
6247
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006248static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6249 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006250{
6251 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 dma_addr_t mapping;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006253 struct device *d = tp_to_dev(tp);
Heiner Kallweitd3b404c2018-04-17 23:22:14 +02006254 int node = dev_to_node(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006256 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006257 if (!data)
6258 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006259
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006260 if (rtl8169_align(data) != data) {
6261 kfree(data);
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006262 data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006263 if (!data)
6264 return NULL;
6265 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006266
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006267 mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006268 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006269 if (unlikely(dma_mapping_error(d, mapping))) {
6270 if (net_ratelimit())
6271 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006272 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274
Heiner Kallweitd731af72018-04-17 23:26:41 +02006275 desc->addr = cpu_to_le64(mapping);
6276 rtl8169_mark_to_asic(desc);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006277 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006278
6279err_out:
6280 kfree(data);
6281 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282}
6283
6284static void rtl8169_rx_clear(struct rtl8169_private *tp)
6285{
Francois Romieu07d3f512007-02-21 22:40:46 +01006286 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287
6288 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006289 if (tp->Rx_databuff[i]) {
6290 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 tp->RxDescArray + i);
6292 }
6293 }
6294}
6295
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006296static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006298 desc->opts1 |= cpu_to_le32(RingEnd);
6299}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006300
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006301static int rtl8169_rx_fill(struct rtl8169_private *tp)
6302{
6303 unsigned int i;
6304
6305 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006306 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006307
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006308 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006309 if (!data) {
6310 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006311 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006312 }
6313 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006316 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6317 return 0;
6318
6319err_out:
6320 rtl8169_rx_clear(tp);
6321 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322}
6323
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006324static int rtl8169_init_ring(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 rtl8169_init_ring_indexes(tp);
6327
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006328 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
6329 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006331 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332}
6333
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006334static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 struct TxDesc *desc)
6336{
6337 unsigned int len = tx_skb->len;
6338
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006339 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6340
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 desc->opts1 = 0x00;
6342 desc->opts2 = 0x00;
6343 desc->addr = 0x00;
6344 tx_skb->len = 0;
6345}
6346
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006347static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6348 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349{
6350 unsigned int i;
6351
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006352 for (i = 0; i < n; i++) {
6353 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 struct ring_info *tx_skb = tp->tx_skb + entry;
6355 unsigned int len = tx_skb->len;
6356
6357 if (len) {
6358 struct sk_buff *skb = tx_skb->skb;
6359
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006360 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 tp->TxDescArray + entry);
6362 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006363 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364 tx_skb->skb = NULL;
6365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 }
6367 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006368}
6369
6370static void rtl8169_tx_clear(struct rtl8169_private *tp)
6371{
6372 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373 tp->cur_tx = tp->dirty_tx = 0;
6374}
6375
Francois Romieu4422bcd2012-01-26 11:23:32 +01006376static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377{
David Howellsc4028952006-11-22 14:57:56 +00006378 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01006379 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380
Francois Romieuda78dbf2012-01-26 14:18:23 +01006381 napi_disable(&tp->napi);
6382 netif_stop_queue(dev);
6383 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384
françois romieuc7c2c392011-12-04 20:30:52 +00006385 rtl8169_hw_reset(tp);
6386
Francois Romieu56de4142011-03-15 17:29:31 +01006387 for (i = 0; i < NUM_RX_DESC; i++)
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006388 rtl8169_mark_to_asic(tp->RxDescArray + i);
Francois Romieu56de4142011-03-15 17:29:31 +01006389
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00006391 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392
Francois Romieuda78dbf2012-01-26 14:18:23 +01006393 napi_enable(&tp->napi);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006394 rtl_hw_start(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01006395 netif_wake_queue(dev);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006396 rtl8169_check_link_status(dev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397}
6398
6399static void rtl8169_tx_timeout(struct net_device *dev)
6400{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006401 struct rtl8169_private *tp = netdev_priv(dev);
6402
6403 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404}
6405
6406static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07006407 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408{
6409 struct skb_shared_info *info = skb_shinfo(skb);
6410 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006411 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006412 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413
6414 entry = tp->cur_tx;
6415 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006416 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 dma_addr_t mapping;
6418 u32 status, len;
6419 void *addr;
6420
6421 entry = (entry + 1) % NUM_TX_DESC;
6422
6423 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00006424 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00006425 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006426 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006427 if (unlikely(dma_mapping_error(d, mapping))) {
6428 if (net_ratelimit())
6429 netif_err(tp, drv, tp->dev,
6430 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006431 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433
Francois Romieucecb5fd2011-04-01 10:21:07 +02006434 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006435 status = opts[0] | len |
6436 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437
6438 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07006439 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 txd->addr = cpu_to_le64(mapping);
6441
6442 tp->tx_skb[entry].len = len;
6443 }
6444
6445 if (cur_frag) {
6446 tp->tx_skb[entry].skb = skb;
6447 txd->opts1 |= cpu_to_le32(LastFrag);
6448 }
6449
6450 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006451
6452err_out:
6453 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6454 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455}
6456
françois romieub423e9a2013-05-18 01:24:46 +00006457static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6458{
6459 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6460}
6461
hayeswange9746042014-07-11 16:25:58 +08006462static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6463 struct net_device *dev);
6464/* r8169_csum_workaround()
6465 * The hw limites the value the transport offset. When the offset is out of the
6466 * range, calculate the checksum by sw.
6467 */
6468static void r8169_csum_workaround(struct rtl8169_private *tp,
6469 struct sk_buff *skb)
6470{
6471 if (skb_shinfo(skb)->gso_size) {
6472 netdev_features_t features = tp->dev->features;
6473 struct sk_buff *segs, *nskb;
6474
6475 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6476 segs = skb_gso_segment(skb, features);
6477 if (IS_ERR(segs) || !segs)
6478 goto drop;
6479
6480 do {
6481 nskb = segs;
6482 segs = segs->next;
6483 nskb->next = NULL;
6484 rtl8169_start_xmit(nskb, tp->dev);
6485 } while (segs);
6486
Alexander Duyckeb781392015-05-01 10:34:44 -07006487 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006488 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6489 if (skb_checksum_help(skb) < 0)
6490 goto drop;
6491
6492 rtl8169_start_xmit(skb, tp->dev);
6493 } else {
6494 struct net_device_stats *stats;
6495
6496drop:
6497 stats = &tp->dev->stats;
6498 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07006499 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006500 }
6501}
6502
6503/* msdn_giant_send_check()
6504 * According to the document of microsoft, the TCP Pseudo Header excludes the
6505 * packet length for IPv6 TCP large packets.
6506 */
6507static int msdn_giant_send_check(struct sk_buff *skb)
6508{
6509 const struct ipv6hdr *ipv6h;
6510 struct tcphdr *th;
6511 int ret;
6512
6513 ret = skb_cow_head(skb, 0);
6514 if (ret)
6515 return ret;
6516
6517 ipv6h = ipv6_hdr(skb);
6518 th = tcp_hdr(skb);
6519
6520 th->check = 0;
6521 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6522
6523 return ret;
6524}
6525
hayeswang5888d3f2014-07-11 16:25:56 +08006526static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6527 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528{
Michał Mirosław350fb322011-04-08 06:35:56 +00006529 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530
Francois Romieu2b7b4312011-04-18 22:53:24 -07006531 if (mss) {
6532 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08006533 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6534 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6535 const struct iphdr *ip = ip_hdr(skb);
6536
6537 if (ip->protocol == IPPROTO_TCP)
6538 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6539 else if (ip->protocol == IPPROTO_UDP)
6540 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6541 else
6542 WARN_ON_ONCE(1);
6543 }
6544
6545 return true;
6546}
6547
6548static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6549 struct sk_buff *skb, u32 *opts)
6550{
hayeswangbdfa4ed2014-07-11 16:25:57 +08006551 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08006552 u32 mss = skb_shinfo(skb)->gso_size;
6553
6554 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08006555 if (transport_offset > GTTCPHO_MAX) {
6556 netif_warn(tp, tx_err, tp->dev,
6557 "Invalid transport offset 0x%x for TSO\n",
6558 transport_offset);
6559 return false;
6560 }
6561
Heiner Kallweit4ff36462018-05-02 21:40:02 +02006562 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08006563 case htons(ETH_P_IP):
6564 opts[0] |= TD1_GTSENV4;
6565 break;
6566
6567 case htons(ETH_P_IPV6):
6568 if (msdn_giant_send_check(skb))
6569 return false;
6570
6571 opts[0] |= TD1_GTSENV6;
6572 break;
6573
6574 default:
6575 WARN_ON_ONCE(1);
6576 break;
6577 }
6578
hayeswangbdfa4ed2014-07-11 16:25:57 +08006579 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08006580 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006581 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08006582 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583
françois romieub423e9a2013-05-18 01:24:46 +00006584 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006585 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00006586
hayeswange9746042014-07-11 16:25:58 +08006587 if (transport_offset > TCPHO_MAX) {
6588 netif_warn(tp, tx_err, tp->dev,
6589 "Invalid transport offset 0x%x\n",
6590 transport_offset);
6591 return false;
6592 }
6593
Heiner Kallweit4ff36462018-05-02 21:40:02 +02006594 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08006595 case htons(ETH_P_IP):
6596 opts[1] |= TD1_IPv4_CS;
6597 ip_protocol = ip_hdr(skb)->protocol;
6598 break;
6599
6600 case htons(ETH_P_IPV6):
6601 opts[1] |= TD1_IPv6_CS;
6602 ip_protocol = ipv6_hdr(skb)->nexthdr;
6603 break;
6604
6605 default:
6606 ip_protocol = IPPROTO_RAW;
6607 break;
6608 }
6609
6610 if (ip_protocol == IPPROTO_TCP)
6611 opts[1] |= TD1_TCP_CS;
6612 else if (ip_protocol == IPPROTO_UDP)
6613 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006614 else
6615 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08006616
6617 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00006618 } else {
6619 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006620 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621 }
hayeswang5888d3f2014-07-11 16:25:56 +08006622
françois romieub423e9a2013-05-18 01:24:46 +00006623 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624}
6625
Stephen Hemminger613573252009-08-31 19:50:58 +00006626static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6627 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628{
6629 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006630 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006632 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633 dma_addr_t mapping;
6634 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006635 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006636 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02006637
Julien Ducourthial477206a2012-05-09 00:00:06 +02006638 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006639 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006640 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641 }
6642
6643 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006644 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645
françois romieub423e9a2013-05-18 01:24:46 +00006646 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6647 opts[0] = DescOwn;
6648
hayeswange9746042014-07-11 16:25:58 +08006649 if (!tp->tso_csum(tp, skb, opts)) {
6650 r8169_csum_workaround(tp, skb);
6651 return NETDEV_TX_OK;
6652 }
françois romieub423e9a2013-05-18 01:24:46 +00006653
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006654 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006655 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006656 if (unlikely(dma_mapping_error(d, mapping))) {
6657 if (net_ratelimit())
6658 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006659 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661
6662 tp->tx_skb[entry].len = len;
6663 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664
Francois Romieu2b7b4312011-04-18 22:53:24 -07006665 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006666 if (frags < 0)
6667 goto err_dma_1;
6668 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07006669 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006670 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07006671 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006672 tp->tx_skb[entry].skb = skb;
6673 }
6674
Francois Romieu2b7b4312011-04-18 22:53:24 -07006675 txd->opts2 = cpu_to_le32(opts[1]);
6676
Richard Cochran5047fb52012-03-10 07:29:42 +00006677 skb_tx_timestamp(skb);
6678
Alexander Duycka0750132014-12-11 15:02:17 -08006679 /* Force memory writes to complete before releasing descriptor */
6680 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681
Francois Romieucecb5fd2011-04-01 10:21:07 +02006682 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006683 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 txd->opts1 = cpu_to_le32(status);
6685
Alexander Duycka0750132014-12-11 15:02:17 -08006686 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00006687 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688
Alexander Duycka0750132014-12-11 15:02:17 -08006689 tp->cur_tx += frags + 1;
6690
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006691 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692
David S. Miller87cda7c2015-02-22 15:54:29 -05006693 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01006694
David S. Miller87cda7c2015-02-22 15:54:29 -05006695 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01006696 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6697 * not miss a ring update when it notices a stopped queue.
6698 */
6699 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01006701 /* Sync with rtl_tx:
6702 * - publish queue status and cur_tx ring index (write barrier)
6703 * - refresh dirty_tx ring index (read barrier).
6704 * May the current thread have a pessimistic view of the ring
6705 * status and forget to wake up queue, a racing rtl_tx thread
6706 * can't.
6707 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006708 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02006709 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710 netif_wake_queue(dev);
6711 }
6712
Stephen Hemminger613573252009-08-31 19:50:58 +00006713 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006715err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006716 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006717err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006718 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006719 dev->stats.tx_dropped++;
6720 return NETDEV_TX_OK;
6721
6722err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006724 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00006725 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726}
6727
6728static void rtl8169_pcierr_interrupt(struct net_device *dev)
6729{
6730 struct rtl8169_private *tp = netdev_priv(dev);
6731 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732 u16 pci_status, pci_cmd;
6733
6734 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6735 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6736
Joe Perchesbf82c182010-02-09 11:49:50 +00006737 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6738 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739
6740 /*
6741 * The recovery sequence below admits a very elaborated explanation:
6742 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01006743 * - I did not see what else could be done;
6744 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745 *
6746 * Feel free to adjust to your needs.
6747 */
Francois Romieua27993f2006-12-18 00:04:19 +01006748 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01006749 pci_cmd &= ~PCI_COMMAND_PARITY;
6750 else
6751 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6752
6753 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754
6755 pci_write_config_word(pdev, PCI_STATUS,
6756 pci_status & (PCI_STATUS_DETECTED_PARITY |
6757 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6758 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6759
6760 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00006761 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006762 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763 tp->cp_cmd &= ~PCIDAC;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006764 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766 }
6767
françois romieue6de30d2011-01-03 15:08:37 +00006768 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01006769
Francois Romieu98ddf982012-01-31 10:47:34 +01006770 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771}
6772
Francois Romieuda78dbf2012-01-26 14:18:23 +01006773static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774{
6775 unsigned int dirty_tx, tx_left;
6776
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 dirty_tx = tp->dirty_tx;
6778 smp_rmb();
6779 tx_left = tp->cur_tx - dirty_tx;
6780
6781 while (tx_left > 0) {
6782 unsigned int entry = dirty_tx % NUM_TX_DESC;
6783 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 u32 status;
6785
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6787 if (status & DescOwn)
6788 break;
6789
Alexander Duycka0750132014-12-11 15:02:17 -08006790 /* This barrier is needed to keep us from reading
6791 * any other fields out of the Tx descriptor until
6792 * we know the status of DescOwn
6793 */
6794 dma_rmb();
6795
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006796 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006797 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05006799 u64_stats_update_begin(&tp->tx_stats.syncp);
6800 tp->tx_stats.packets++;
6801 tp->tx_stats.bytes += tx_skb->skb->len;
6802 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006803 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804 tx_skb->skb = NULL;
6805 }
6806 dirty_tx++;
6807 tx_left--;
6808 }
6809
6810 if (tp->dirty_tx != dirty_tx) {
6811 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01006812 /* Sync with rtl8169_start_xmit:
6813 * - publish dirty_tx ring index (write barrier)
6814 * - refresh cur_tx ring index and queue status (read barrier)
6815 * May the current thread miss the stopped queue condition,
6816 * a racing xmit thread can only have a right view of the
6817 * ring status.
6818 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006819 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02006821 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822 netif_wake_queue(dev);
6823 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02006824 /*
6825 * 8168 hack: TxPoll requests are lost when the Tx packets are
6826 * too close. Let's kick an extra TxPoll request when a burst
6827 * of start_xmit activity is detected (if it is not detected,
6828 * it is slow enough). -- FR
6829 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006830 if (tp->cur_tx != dirty_tx)
6831 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832 }
6833}
6834
Francois Romieu126fa4b2005-05-12 20:09:17 -04006835static inline int rtl8169_fragmented_frame(u32 status)
6836{
6837 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6838}
6839
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006840static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 u32 status = opts1 & RxProtoMask;
6843
6844 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00006845 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846 skb->ip_summed = CHECKSUM_UNNECESSARY;
6847 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006848 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849}
6850
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006851static struct sk_buff *rtl8169_try_rx_copy(void *data,
6852 struct rtl8169_private *tp,
6853 int pkt_size,
6854 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02006856 struct sk_buff *skb;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006857 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006859 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006860 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006861 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08006862 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006863 if (skb)
Heiner Kallweit8a67aa82018-04-17 23:19:07 +02006864 skb_copy_to_linear_data(skb, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006865 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6866
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006867 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868}
6869
Francois Romieuda78dbf2012-01-26 14:18:23 +01006870static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871{
6872 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006873 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876
Timo Teräs9fba0812013-01-15 21:01:24 +00006877 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006879 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880 u32 status;
6881
Heiner Kallweit62028062018-04-17 23:30:29 +02006882 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883 if (status & DescOwn)
6884 break;
Alexander Duycka0750132014-12-11 15:02:17 -08006885
6886 /* This barrier is needed to keep us from reading
6887 * any other fields out of the Rx descriptor until
6888 * we know the status of DescOwn
6889 */
6890 dma_rmb();
6891
Richard Dawe4dcb7d32005-05-27 21:12:00 +02006892 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006893 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6894 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006895 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02006897 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02006899 dev->stats.rx_crc_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006900 /* RxFOVF is a reserved bit on later chip versions */
6901 if (tp->mac_version == RTL_GIGA_MAC_VER_01 &&
6902 status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006903 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006904 dev->stats.rx_fifo_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006905 } else if (status & (RxRUNT | RxCRC) &&
6906 !(status & RxRWT) &&
6907 dev->features & NETIF_F_RXALL) {
Ben Greear6bbe0212012-02-10 15:04:33 +00006908 goto process_pkt;
Heiner Kallweit62028062018-04-17 23:30:29 +02006909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006911 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00006912 dma_addr_t addr;
6913 int pkt_size;
6914
6915process_pkt:
6916 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00006917 if (likely(!(dev->features & NETIF_F_RXFCS)))
6918 pkt_size = (status & 0x00003fff) - 4;
6919 else
6920 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921
Francois Romieu126fa4b2005-05-12 20:09:17 -04006922 /*
6923 * The driver does not support incoming fragmented
6924 * frames. They are seen as a symptom of over-mtu
6925 * sized frames.
6926 */
6927 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02006928 dev->stats.rx_dropped++;
6929 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00006930 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006931 }
6932
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006933 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6934 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006935 if (!skb) {
6936 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00006937 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 }
6939
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006940 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 skb_put(skb, pkt_size);
6942 skb->protocol = eth_type_trans(skb, dev);
6943
Francois Romieu7a8fc772011-03-01 17:18:33 +01006944 rtl8169_rx_vlan_tag(desc, skb);
6945
françois romieu39174292015-11-11 23:35:18 +01006946 if (skb->pkt_type == PACKET_MULTICAST)
6947 dev->stats.multicast++;
6948
Francois Romieu56de4142011-03-15 17:29:31 +01006949 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950
Junchang Wang8027aa22012-03-04 23:30:32 +01006951 u64_stats_update_begin(&tp->rx_stats.syncp);
6952 tp->rx_stats.packets++;
6953 tp->rx_stats.bytes += pkt_size;
6954 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955 }
françois romieuce11ff52013-01-24 13:30:06 +00006956release_descriptor:
6957 desc->opts2 = 0;
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006958 rtl8169_mark_to_asic(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 }
6960
6961 count = cur_rx - tp->cur_rx;
6962 tp->cur_rx = cur_rx;
6963
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964 return count;
6965}
6966
Francois Romieu07d3f512007-02-21 22:40:46 +01006967static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968{
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006969 struct rtl8169_private *tp = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006971 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006973 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006974 if (status && status != 0xffff) {
6975 status &= RTL_EVENT_NAPI | tp->event_slow;
6976 if (status) {
6977 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00006978
Francois Romieuda78dbf2012-01-26 14:18:23 +01006979 rtl_irq_disable(tp);
Heiner Kallweit9a899a32018-04-17 23:21:01 +02006980 napi_schedule_irqoff(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983 return IRQ_RETVAL(handled);
6984}
6985
Francois Romieuda78dbf2012-01-26 14:18:23 +01006986/*
6987 * Workqueue context.
6988 */
6989static void rtl_slow_event_work(struct rtl8169_private *tp)
6990{
6991 struct net_device *dev = tp->dev;
6992 u16 status;
6993
6994 status = rtl_get_events(tp) & tp->event_slow;
6995 rtl_ack_events(tp, status);
6996
6997 if (unlikely(status & RxFIFOOver)) {
6998 switch (tp->mac_version) {
6999 /* Work around for rx fifo overflow */
7000 case RTL_GIGA_MAC_VER_11:
7001 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01007002 /* XXX - Hack alert. See rtl_task(). */
7003 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007004 default:
7005 break;
7006 }
7007 }
7008
7009 if (unlikely(status & SYSErr))
7010 rtl8169_pcierr_interrupt(dev);
7011
7012 if (status & LinkChg)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007013 rtl8169_check_link_status(dev, tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007014
françois romieu7dbb4912012-06-09 10:53:16 +00007015 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007016}
7017
Francois Romieu4422bcd2012-01-26 11:23:32 +01007018static void rtl_task(struct work_struct *work)
7019{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007020 static const struct {
7021 int bitnr;
7022 void (*action)(struct rtl8169_private *);
7023 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01007024 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007025 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7026 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7027 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7028 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01007029 struct rtl8169_private *tp =
7030 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007031 struct net_device *dev = tp->dev;
7032 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01007033
Francois Romieuda78dbf2012-01-26 14:18:23 +01007034 rtl_lock_work(tp);
7035
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007036 if (!netif_running(dev) ||
7037 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01007038 goto out_unlock;
7039
7040 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7041 bool pending;
7042
Francois Romieuda78dbf2012-01-26 14:18:23 +01007043 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007044 if (pending)
7045 rtl_work[i].action(tp);
7046 }
7047
7048out_unlock:
7049 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01007050}
7051
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007052static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007054 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7055 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007056 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7057 int work_done= 0;
7058 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059
Francois Romieuda78dbf2012-01-26 14:18:23 +01007060 status = rtl_get_events(tp);
7061 rtl_ack_events(tp, status & ~tp->event_slow);
7062
7063 if (status & RTL_EVENT_NAPI_RX)
7064 work_done = rtl_rx(dev, tp, (u32) budget);
7065
7066 if (status & RTL_EVENT_NAPI_TX)
7067 rtl_tx(dev, tp);
7068
7069 if (status & tp->event_slow) {
7070 enable_mask &= ~tp->event_slow;
7071
7072 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007075 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08007076 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00007077
Francois Romieuda78dbf2012-01-26 14:18:23 +01007078 rtl_irq_enable(tp, enable_mask);
7079 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080 }
7081
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007082 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007085static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02007086{
7087 struct rtl8169_private *tp = netdev_priv(dev);
7088
7089 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7090 return;
7091
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007092 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
7093 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02007094}
7095
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096static void rtl8169_down(struct net_device *dev)
7097{
7098 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099
Francois Romieu4876cc12011-03-11 21:07:11 +01007100 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007102 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007103 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104
Hayes Wang92fc43b2011-07-06 15:58:03 +08007105 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007106 /*
7107 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007108 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7109 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007110 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007111 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007114 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116 rtl8169_tx_clear(tp);
7117
7118 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007119
7120 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121}
7122
7123static int rtl8169_close(struct net_device *dev)
7124{
7125 struct rtl8169_private *tp = netdev_priv(dev);
7126 struct pci_dev *pdev = tp->pci_dev;
7127
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007128 pm_runtime_get_sync(&pdev->dev);
7129
Francois Romieucecb5fd2011-04-01 10:21:07 +02007130 /* Update counters before going down */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007131 rtl8169_update_counters(tp);
Ivan Vecera355423d2009-02-06 21:49:57 -08007132
Francois Romieuda78dbf2012-01-26 14:18:23 +01007133 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007134 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007135
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007137 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138
Lekensteyn4ea72442013-07-22 09:53:30 +02007139 cancel_work_sync(&tp->wk.work);
7140
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02007141 pci_free_irq(pdev, 0, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007143 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7144 tp->RxPhyAddr);
7145 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7146 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 tp->TxDescArray = NULL;
7148 tp->RxDescArray = NULL;
7149
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007150 pm_runtime_put_sync(&pdev->dev);
7151
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152 return 0;
7153}
7154
Francois Romieudc1c00c2012-03-08 10:06:18 +01007155#ifdef CONFIG_NET_POLL_CONTROLLER
7156static void rtl8169_netpoll(struct net_device *dev)
7157{
7158 struct rtl8169_private *tp = netdev_priv(dev);
7159
Ville Syrjälä6d8b8342018-06-20 15:01:53 +03007160 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
Francois Romieudc1c00c2012-03-08 10:06:18 +01007161}
7162#endif
7163
Francois Romieudf43ac72012-03-08 09:48:40 +01007164static int rtl_open(struct net_device *dev)
7165{
7166 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007167 struct pci_dev *pdev = tp->pci_dev;
7168 int retval = -ENOMEM;
7169
7170 pm_runtime_get_sync(&pdev->dev);
7171
7172 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007173 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007174 * dma_alloc_coherent provides more.
7175 */
7176 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7177 &tp->TxPhyAddr, GFP_KERNEL);
7178 if (!tp->TxDescArray)
7179 goto err_pm_runtime_put;
7180
7181 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7182 &tp->RxPhyAddr, GFP_KERNEL);
7183 if (!tp->RxDescArray)
7184 goto err_free_tx_0;
7185
Heiner Kallweitb1127e62018-04-17 23:23:35 +02007186 retval = rtl8169_init_ring(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007187 if (retval < 0)
7188 goto err_free_rx_1;
7189
7190 INIT_WORK(&tp->wk.work, rtl_task);
7191
7192 smp_mb();
7193
7194 rtl_request_firmware(tp);
7195
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02007196 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007197 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01007198 if (retval < 0)
7199 goto err_release_fw_2;
7200
7201 rtl_lock_work(tp);
7202
7203 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7204
7205 napi_enable(&tp->napi);
7206
7207 rtl8169_init_phy(dev, tp);
7208
Francois Romieudf43ac72012-03-08 09:48:40 +01007209 rtl_pll_power_up(tp);
7210
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007211 rtl_hw_start(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007212
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007213 if (!rtl8169_init_counter_offsets(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007214 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7215
Francois Romieudf43ac72012-03-08 09:48:40 +01007216 netif_start_queue(dev);
7217
7218 rtl_unlock_work(tp);
7219
7220 tp->saved_wolopts = 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007221 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007222
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007223 rtl8169_check_link_status(dev, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007224out:
7225 return retval;
7226
7227err_release_fw_2:
7228 rtl_release_firmware(tp);
7229 rtl8169_rx_clear(tp);
7230err_free_rx_1:
7231 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7232 tp->RxPhyAddr);
7233 tp->RxDescArray = NULL;
7234err_free_tx_0:
7235 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7236 tp->TxPhyAddr);
7237 tp->TxDescArray = NULL;
7238err_pm_runtime_put:
7239 pm_runtime_put_noidle(&pdev->dev);
7240 goto out;
7241}
7242
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007243static void
Junchang Wang8027aa22012-03-04 23:30:32 +01007244rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245{
7246 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007247 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02007248 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01007249 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007251 pm_runtime_get_noresume(&pdev->dev);
7252
7253 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007254 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007255
Junchang Wang8027aa22012-03-04 23:30:32 +01007256 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007257 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007258 stats->rx_packets = tp->rx_stats.packets;
7259 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007260 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007261
Junchang Wang8027aa22012-03-04 23:30:32 +01007262 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007263 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007264 stats->tx_packets = tp->tx_stats.packets;
7265 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007266 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007267
7268 stats->rx_dropped = dev->stats.rx_dropped;
7269 stats->tx_dropped = dev->stats.tx_dropped;
7270 stats->rx_length_errors = dev->stats.rx_length_errors;
7271 stats->rx_errors = dev->stats.rx_errors;
7272 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7273 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7274 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02007275 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01007276
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007277 /*
7278 * Fetch additonal counter values missing in stats collected by driver
7279 * from tally counters.
7280 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007281 if (pm_runtime_active(&pdev->dev))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007282 rtl8169_update_counters(tp);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007283
7284 /*
7285 * Subtract values fetched during initalization.
7286 * See rtl8169_init_counter_offsets for a description why we do that.
7287 */
Corinna Vinschen42020322015-09-10 10:47:35 +02007288 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007289 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02007290 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007291 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02007292 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007293 le16_to_cpu(tp->tc_offset.tx_aborted);
7294
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007295 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296}
7297
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007298static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01007299{
françois romieu065c27c2011-01-03 15:08:12 +00007300 struct rtl8169_private *tp = netdev_priv(dev);
7301
Francois Romieu5d06a992006-02-23 00:47:58 +01007302 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007303 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01007304
7305 netif_device_detach(dev);
7306 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007307
7308 rtl_lock_work(tp);
7309 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007310 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007311 rtl_unlock_work(tp);
7312
7313 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007314}
Francois Romieu5d06a992006-02-23 00:47:58 +01007315
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007316#ifdef CONFIG_PM
7317
7318static int rtl8169_suspend(struct device *device)
7319{
7320 struct pci_dev *pdev = to_pci_dev(device);
7321 struct net_device *dev = pci_get_drvdata(pdev);
7322
7323 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02007324
Francois Romieu5d06a992006-02-23 00:47:58 +01007325 return 0;
7326}
7327
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007328static void __rtl8169_resume(struct net_device *dev)
7329{
françois romieu065c27c2011-01-03 15:08:12 +00007330 struct rtl8169_private *tp = netdev_priv(dev);
7331
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007332 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00007333
7334 rtl_pll_power_up(tp);
7335
Artem Savkovcff4c162012-04-03 10:29:11 +00007336 rtl_lock_work(tp);
7337 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007338 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00007339 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007340
Francois Romieu98ddf982012-01-31 10:47:34 +01007341 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007342}
7343
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007344static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01007345{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007346 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01007347 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007348 struct rtl8169_private *tp = netdev_priv(dev);
7349
7350 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01007351
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007352 if (netif_running(dev))
7353 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01007354
Francois Romieu5d06a992006-02-23 00:47:58 +01007355 return 0;
7356}
7357
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007358static int rtl8169_runtime_suspend(struct device *device)
7359{
7360 struct pci_dev *pdev = to_pci_dev(device);
7361 struct net_device *dev = pci_get_drvdata(pdev);
7362 struct rtl8169_private *tp = netdev_priv(dev);
7363
Heiner Kallweita92a0842018-01-08 21:39:13 +01007364 if (!tp->TxDescArray) {
7365 rtl_pll_power_down(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007366 return 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007367 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007368
Francois Romieuda78dbf2012-01-26 14:18:23 +01007369 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007370 tp->saved_wolopts = __rtl8169_get_wol(tp);
7371 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007372 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007373
7374 rtl8169_net_suspend(dev);
7375
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007376 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007377 rtl8169_rx_missed(dev);
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007378 rtl8169_update_counters(tp);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007379
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007380 return 0;
7381}
7382
7383static int rtl8169_runtime_resume(struct device *device)
7384{
7385 struct pci_dev *pdev = to_pci_dev(device);
7386 struct net_device *dev = pci_get_drvdata(pdev);
7387 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08007388 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007389
7390 if (!tp->TxDescArray)
7391 return 0;
7392
Francois Romieuda78dbf2012-01-26 14:18:23 +01007393 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007394 __rtl8169_set_wol(tp, tp->saved_wolopts);
7395 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007396 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007397
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007398 rtl8169_init_phy(dev, tp);
7399
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007400 __rtl8169_resume(dev);
7401
7402 return 0;
7403}
7404
7405static int rtl8169_runtime_idle(struct device *device)
7406{
7407 struct pci_dev *pdev = to_pci_dev(device);
7408 struct net_device *dev = pci_get_drvdata(pdev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007409
Heiner Kallweita92a0842018-01-08 21:39:13 +01007410 if (!netif_running(dev) || !netif_carrier_ok(dev))
7411 pm_schedule_suspend(device, 10000);
7412
7413 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007414}
7415
Alexey Dobriyan47145212009-12-14 18:00:08 -08007416static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02007417 .suspend = rtl8169_suspend,
7418 .resume = rtl8169_resume,
7419 .freeze = rtl8169_suspend,
7420 .thaw = rtl8169_resume,
7421 .poweroff = rtl8169_suspend,
7422 .restore = rtl8169_resume,
7423 .runtime_suspend = rtl8169_runtime_suspend,
7424 .runtime_resume = rtl8169_runtime_resume,
7425 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007426};
7427
7428#define RTL8169_PM_OPS (&rtl8169_pm_ops)
7429
7430#else /* !CONFIG_PM */
7431
7432#define RTL8169_PM_OPS NULL
7433
7434#endif /* !CONFIG_PM */
7435
David S. Miller1805b2f2011-10-24 18:18:09 -04007436static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7437{
David S. Miller1805b2f2011-10-24 18:18:09 -04007438 /* WoL fails with 8168b when the receiver is disabled. */
7439 switch (tp->mac_version) {
7440 case RTL_GIGA_MAC_VER_11:
7441 case RTL_GIGA_MAC_VER_12:
7442 case RTL_GIGA_MAC_VER_17:
7443 pci_clear_master(tp->pci_dev);
7444
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007445 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007446 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007447 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04007448 break;
7449 default:
7450 break;
7451 }
7452}
7453
Francois Romieu1765f952008-09-13 17:21:40 +02007454static void rtl_shutdown(struct pci_dev *pdev)
7455{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007456 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00007457 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02007458
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007459 rtl8169_net_suspend(dev);
7460
Francois Romieucecb5fd2011-04-01 10:21:07 +02007461 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08007462 rtl_rar_set(tp, dev->perm_addr);
7463
Hayes Wang92fc43b2011-07-06 15:58:03 +08007464 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00007465
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007466 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04007467 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
7468 rtl_wol_suspend_quirk(tp);
7469 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00007470 }
7471
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007472 pci_wake_from_d3(pdev, true);
7473 pci_set_power_state(pdev, PCI_D3hot);
7474 }
7475}
Francois Romieu5d06a992006-02-23 00:47:58 +01007476
Bill Pembertonbaf63292012-12-03 09:23:28 -05007477static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01007478{
7479 struct net_device *dev = pci_get_drvdata(pdev);
7480 struct rtl8169_private *tp = netdev_priv(dev);
7481
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007482 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01007483 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01007484
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007485 netif_napi_del(&tp->napi);
7486
Francois Romieue27566e2012-03-08 09:54:01 +01007487 unregister_netdev(dev);
7488
7489 rtl_release_firmware(tp);
7490
7491 if (pci_dev_run_wake(pdev))
7492 pm_runtime_get_noresume(&pdev->dev);
7493
7494 /* restore original MAC address */
7495 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01007496}
7497
Francois Romieufa9c3852012-03-08 10:01:50 +01007498static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01007499 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01007500 .ndo_stop = rtl8169_close,
7501 .ndo_get_stats64 = rtl8169_get_stats64,
7502 .ndo_start_xmit = rtl8169_start_xmit,
7503 .ndo_tx_timeout = rtl8169_tx_timeout,
7504 .ndo_validate_addr = eth_validate_addr,
7505 .ndo_change_mtu = rtl8169_change_mtu,
7506 .ndo_fix_features = rtl8169_fix_features,
7507 .ndo_set_features = rtl8169_set_features,
7508 .ndo_set_mac_address = rtl_set_mac_address,
7509 .ndo_do_ioctl = rtl8169_ioctl,
7510 .ndo_set_rx_mode = rtl_set_rx_mode,
7511#ifdef CONFIG_NET_POLL_CONTROLLER
7512 .ndo_poll_controller = rtl8169_netpoll,
7513#endif
7514
7515};
7516
Francois Romieu31fa8b12012-03-08 10:09:40 +01007517static const struct rtl_cfg_info {
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007518 void (*hw_start)(struct rtl8169_private *tp);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007519 u16 event_slow;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007520 unsigned int has_gmii:1;
Francois Romieu50970832017-10-27 13:24:49 +03007521 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007522 u8 default_ver;
7523} rtl_cfg_infos [] = {
7524 [RTL_CFG_0] = {
7525 .hw_start = rtl_hw_start_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007526 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007527 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007528 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007529 .default_ver = RTL_GIGA_MAC_VER_01,
7530 },
7531 [RTL_CFG_1] = {
7532 .hw_start = rtl_hw_start_8168,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007533 .event_slow = SYSErr | LinkChg | RxOverflow,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007534 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007535 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007536 .default_ver = RTL_GIGA_MAC_VER_11,
7537 },
7538 [RTL_CFG_2] = {
7539 .hw_start = rtl_hw_start_8101,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007540 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
7541 PCSTimeout,
Francois Romieu50970832017-10-27 13:24:49 +03007542 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007543 .default_ver = RTL_GIGA_MAC_VER_13,
7544 }
7545};
7546
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007547static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01007548{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007549 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007550
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007551 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007552 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
7553 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
7554 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007555 flags = PCI_IRQ_LEGACY;
7556 } else {
7557 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007558 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007559
7560 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007561}
7562
Hayes Wangc5583862012-07-02 17:23:22 +08007563DECLARE_RTL_COND(rtl_link_list_ready_cond)
7564{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007565 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08007566}
7567
7568DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7569{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007570 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08007571}
7572
Bill Pembertonbaf63292012-12-03 09:23:28 -05007573static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007574{
Hayes Wangc5583862012-07-02 17:23:22 +08007575 u32 data;
7576
7577 tp->ocp_base = OCP_STD_PHY_BASE;
7578
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007579 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08007580
7581 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7582 return;
7583
7584 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7585 return;
7586
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007587 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08007588 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007589 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08007590
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007591 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007592 data &= ~(1 << 14);
7593 r8168_mac_ocp_write(tp, 0xe8de, data);
7594
7595 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7596 return;
7597
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007598 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007599 data |= (1 << 15);
7600 r8168_mac_ocp_write(tp, 0xe8de, data);
7601
7602 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7603 return;
7604}
7605
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007606static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
7607{
7608 rtl8168ep_stop_cmac(tp);
7609 rtl_hw_init_8168g(tp);
7610}
7611
Bill Pembertonbaf63292012-12-03 09:23:28 -05007612static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007613{
7614 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02007615 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007616 rtl_hw_init_8168g(tp);
7617 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02007618 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007619 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08007620 break;
Hayes Wangc5583862012-07-02 17:23:22 +08007621 default:
7622 break;
7623 }
7624}
7625
hayeswang929a0312014-09-16 11:40:47 +08007626static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01007627{
7628 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007629 struct rtl8169_private *tp;
7630 struct mii_if_info *mii;
7631 struct net_device *dev;
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007632 int chipset, region, i;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007633 int rc;
7634
7635 if (netif_msg_drv(&debug)) {
7636 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
7637 MODULENAME, RTL8169_VERSION);
7638 }
7639
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007640 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
7641 if (!dev)
7642 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007643
7644 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01007645 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007646 tp = netdev_priv(dev);
7647 tp->dev = dev;
7648 tp->pci_dev = pdev;
7649 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
7650
7651 mii = &tp->mii;
7652 mii->dev = dev;
7653 mii->mdio_read = rtl_mdio_read;
7654 mii->mdio_write = rtl_mdio_write;
7655 mii->phy_id_mask = 0x1f;
7656 mii->reg_num_mask = 0x1f;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007657 mii->supports_gmii = cfg->has_gmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007658
Francois Romieu3b6cf252012-03-08 09:59:04 +01007659 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007660 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007661 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007662 dev_err(&pdev->dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007663 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007664 }
7665
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007666 if (pcim_set_mwi(pdev) < 0)
Heiner Kallweit22148df2018-04-22 17:15:15 +02007667 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007668
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007669 /* use first MMIO region */
7670 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
7671 if (region < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007672 dev_err(&pdev->dev, "no MMIO resource found\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007673 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007674 }
7675
7676 /* check for weird/broken PCI region reporting */
7677 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007678 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007679 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007680 }
7681
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007682 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007683 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007684 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007685 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007686 }
7687
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007688 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01007689
7690 if (!pci_is_pcie(pdev))
Heiner Kallweit22148df2018-04-22 17:15:15 +02007691 dev_info(&pdev->dev, "not PCI Express\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007692
7693 /* Identify chip attached to board */
Heiner Kallweit22148df2018-04-22 17:15:15 +02007694 rtl8169_get_mac_version(tp, cfg->default_ver);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007695
Heiner Kallweit0ae09742018-04-28 22:19:26 +02007696 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007697
7698 if ((sizeof(dma_addr_t) > 4) &&
7699 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
7700 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01007701 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
7702 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007703
7704 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
7705 if (!pci_is_pcie(pdev))
7706 tp->cp_cmd |= PCIDAC;
7707 dev->features |= NETIF_F_HIGHDMA;
7708 } else {
7709 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7710 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007711 dev_err(&pdev->dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007712 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007713 }
7714 }
7715
Francois Romieu3b6cf252012-03-08 09:59:04 +01007716 rtl_init_rxcfg(tp);
7717
7718 rtl_irq_disable(tp);
7719
Hayes Wangc5583862012-07-02 17:23:22 +08007720 rtl_hw_initialize(tp);
7721
Francois Romieu3b6cf252012-03-08 09:59:04 +01007722 rtl_hw_reset(tp);
7723
7724 rtl_ack_events(tp, 0xffff);
7725
7726 pci_set_master(pdev);
7727
Francois Romieu3b6cf252012-03-08 09:59:04 +01007728 rtl_init_mdio_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007729 rtl_init_jumbo_ops(tp);
7730
7731 rtl8169_print_mac_version(tp);
7732
7733 chipset = tp->mac_version;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007734
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007735 rc = rtl_alloc_irq(tp);
7736 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007737 dev_err(&pdev->dev, "Can't allocate interrupt\n");
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007738 return rc;
7739 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007740
Heiner Kallweit7edf6d32018-02-22 21:22:40 +01007741 /* override BIOS settings, use userspace tools to enable WOL */
7742 __rtl8169_set_wol(tp, 0);
7743
Francois Romieu3b6cf252012-03-08 09:59:04 +01007744 if (rtl_tbi_enabled(tp)) {
7745 tp->set_speed = rtl8169_set_speed_tbi;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01007746 tp->get_link_ksettings = rtl8169_get_link_ksettings_tbi;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007747 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
7748 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
7749 tp->link_ok = rtl8169_tbi_link_ok;
7750 tp->do_ioctl = rtl_tbi_ioctl;
7751 } else {
7752 tp->set_speed = rtl8169_set_speed_xmii;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01007753 tp->get_link_ksettings = rtl8169_get_link_ksettings_xmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007754 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
7755 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
7756 tp->link_ok = rtl8169_xmii_link_ok;
7757 tp->do_ioctl = rtl_xmii_ioctl;
7758 }
7759
7760 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05007761 u64_stats_init(&tp->rx_stats.syncp);
7762 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007763
7764 /* Get MAC address */
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007765 switch (tp->mac_version) {
Heiner Kallweit353af852018-05-02 21:39:59 +02007766 u8 mac_addr[ETH_ALEN] __aligned(4);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007767 case RTL_GIGA_MAC_VER_35 ... RTL_GIGA_MAC_VER_38:
7768 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08007769 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
Heiner Kallweit353af852018-05-02 21:39:59 +02007770 *(u16 *)&mac_addr[4] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007771
Heiner Kallweit353af852018-05-02 21:39:59 +02007772 if (is_valid_ether_addr(mac_addr))
7773 rtl_rar_set(tp, mac_addr);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007774 break;
7775 default:
7776 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007777 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007778 for (i = 0; i < ETH_ALEN; i++)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007779 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007780
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00007781 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007782 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007783
Heiner Kallweit37621492018-04-17 23:20:03 +02007784 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007785
7786 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7787 * properly for all devices */
7788 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00007789 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007790
7791 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00007792 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7793 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007794 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7795 NETIF_F_HIGHDMA;
7796
hayeswang929a0312014-09-16 11:40:47 +08007797 tp->cp_cmd |= RxChkSum | RxVlan;
7798
7799 /*
7800 * Pretend we are using VLANs; This bypasses a nasty bug where
7801 * Interrupts stop flowing on high load on 8110SCd controllers.
7802 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01007803 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08007804 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00007805 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007806
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007807 switch (rtl_chip_infos[chipset].txd_version) {
7808 case RTL_TD_0:
hayeswang5888d3f2014-07-11 16:25:56 +08007809 tp->tso_csum = rtl8169_tso_csum_v1;
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007810 break;
7811 case RTL_TD_1:
hayeswang5888d3f2014-07-11 16:25:56 +08007812 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08007813 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007814 break;
7815 default:
hayeswang5888d3f2014-07-11 16:25:56 +08007816 WARN_ON_ONCE(1);
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007817 }
hayeswang5888d3f2014-07-11 16:25:56 +08007818
Francois Romieu3b6cf252012-03-08 09:59:04 +01007819 dev->hw_features |= NETIF_F_RXALL;
7820 dev->hw_features |= NETIF_F_RXFCS;
7821
Jarod Wilsonc7315a92016-10-17 15:54:09 -04007822 /* MTU range: 60 - hw-specific max */
7823 dev->min_mtu = ETH_ZLEN;
7824 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
7825
Francois Romieu3b6cf252012-03-08 09:59:04 +01007826 tp->hw_start = cfg->hw_start;
7827 tp->event_slow = cfg->event_slow;
Francois Romieu50970832017-10-27 13:24:49 +03007828 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007829
Kees Cook9de36cc2017-10-25 03:53:12 -07007830 timer_setup(&tp->timer, rtl8169_phy_timer, 0);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007831
7832 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
7833
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007834 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
7835 &tp->counters_phys_addr,
7836 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007837 if (!tp->counters)
7838 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02007839
Heiner Kallweit19c9ea32018-03-26 19:19:30 +02007840 pci_set_drvdata(pdev, dev);
7841
Francois Romieu3b6cf252012-03-08 09:59:04 +01007842 rc = register_netdev(dev);
7843 if (rc < 0)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007844 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007845
Heiner Kallweit2d6c5a62018-04-17 23:31:21 +02007846 netif_info(tp, probe, dev, "%s, %pM, XID %08x, IRQ %d\n",
7847 rtl_chip_infos[chipset].name, dev->dev_addr,
Heiner Kallweit90b989c2018-04-17 23:32:15 +02007848 (u32)(RTL_R32(tp, TxConfig) & 0xfcf0f8ff),
Heiner Kallweit29274992018-02-28 20:43:38 +01007849 pci_irq_vector(pdev, 0));
Francois Romieu3b6cf252012-03-08 09:59:04 +01007850 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
7851 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
7852 "tx checksumming: %s]\n",
7853 rtl_chip_infos[chipset].jumbo_max,
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02007854 tp->mac_version <= RTL_GIGA_MAC_VER_06 ? "ok" : "ko");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007855 }
7856
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007857 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01007858 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007859
Francois Romieu3b6cf252012-03-08 09:59:04 +01007860 netif_carrier_off(dev);
7861
Heiner Kallweita92a0842018-01-08 21:39:13 +01007862 if (pci_dev_run_wake(pdev))
7863 pm_runtime_put_sync(&pdev->dev);
7864
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007865 return 0;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007866}
7867
Linus Torvalds1da177e2005-04-16 15:20:36 -07007868static struct pci_driver rtl8169_pci_driver = {
7869 .name = MODULENAME,
7870 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01007871 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05007872 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02007873 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007874 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875};
7876
Devendra Naga3eeb7da2012-10-26 09:27:42 +00007877module_pci_driver(rtl8169_pci_driver);