blob: 3573d8256ff9301e9a7ff70ee9359c07b46e27c0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000024#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000026#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000027#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000028#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040029#include <linux/prefetch.h>
hayeswange9746042014-07-11 16:25:58 +080030#include <linux/ipv6.h>
31#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
françois romieubca03d52011-01-03 15:07:31 +000040#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000042#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080044#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080045#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
46#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080047#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080048#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080049#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080050#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080051#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000052#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000053#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000054#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +080055#define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
56#define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
57#define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
58#define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
françois romieubca03d52011-01-03 15:07:31 +000059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifdef RTL8169_DEBUG
61#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020062 if (!(expr)) { \
63 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070064 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020065 }
Joe Perches06fa7352007-10-18 21:15:00 +020066#define dprintk(fmt, args...) \
67 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#else
69#define assert(expr) do {} while (0)
70#define dprintk(fmt, args...) do {} while (0)
71#endif /* RTL8169_DEBUG */
72
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020073#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070074 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020075
Julien Ducourthial477206a2012-05-09 00:00:06 +020076#define TX_SLOTS_AVAIL(tp) \
77 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
78
79/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
80#define TX_FRAGS_READY_FOR(tp,nr_frags) \
81 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
84 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050085static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Michal Schmidtaee77e42012-09-09 13:55:26 +000087#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
89
90#define R8169_REGS_SIZE 256
Heiner Kallweit1d0254d2018-04-17 23:25:46 +020091#define R8169_RX_BUF_SIZE (SZ_16K - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000093#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
95#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
96
97#define RTL8169_TX_TIMEOUT (6*HZ)
98#define RTL8169_PHY_TIMEOUT (10*HZ)
99
100/* write/read MMIO register */
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200101#define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
102#define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
103#define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
104#define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
105#define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
106#define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200109 RTL_GIGA_MAC_VER_01 = 0,
110 RTL_GIGA_MAC_VER_02,
111 RTL_GIGA_MAC_VER_03,
112 RTL_GIGA_MAC_VER_04,
113 RTL_GIGA_MAC_VER_05,
114 RTL_GIGA_MAC_VER_06,
115 RTL_GIGA_MAC_VER_07,
116 RTL_GIGA_MAC_VER_08,
117 RTL_GIGA_MAC_VER_09,
118 RTL_GIGA_MAC_VER_10,
119 RTL_GIGA_MAC_VER_11,
120 RTL_GIGA_MAC_VER_12,
121 RTL_GIGA_MAC_VER_13,
122 RTL_GIGA_MAC_VER_14,
123 RTL_GIGA_MAC_VER_15,
124 RTL_GIGA_MAC_VER_16,
125 RTL_GIGA_MAC_VER_17,
126 RTL_GIGA_MAC_VER_18,
127 RTL_GIGA_MAC_VER_19,
128 RTL_GIGA_MAC_VER_20,
129 RTL_GIGA_MAC_VER_21,
130 RTL_GIGA_MAC_VER_22,
131 RTL_GIGA_MAC_VER_23,
132 RTL_GIGA_MAC_VER_24,
133 RTL_GIGA_MAC_VER_25,
134 RTL_GIGA_MAC_VER_26,
135 RTL_GIGA_MAC_VER_27,
136 RTL_GIGA_MAC_VER_28,
137 RTL_GIGA_MAC_VER_29,
138 RTL_GIGA_MAC_VER_30,
139 RTL_GIGA_MAC_VER_31,
140 RTL_GIGA_MAC_VER_32,
141 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800142 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800143 RTL_GIGA_MAC_VER_35,
144 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800145 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800146 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800147 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800148 RTL_GIGA_MAC_VER_40,
149 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000150 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000151 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800152 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800153 RTL_GIGA_MAC_VER_45,
154 RTL_GIGA_MAC_VER_46,
155 RTL_GIGA_MAC_VER_47,
156 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800157 RTL_GIGA_MAC_VER_49,
158 RTL_GIGA_MAC_VER_50,
159 RTL_GIGA_MAC_VER_51,
Francois Romieu85bffe62011-04-27 08:22:39 +0200160 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
Francois Romieu2b7b4312011-04-18 22:53:24 -0700163enum rtl_tx_desc_version {
164 RTL_TD_0 = 0,
165 RTL_TD_1 = 1,
166};
167
Francois Romieud58d46b2011-05-03 16:38:29 +0200168#define JUMBO_1K ETH_DATA_LEN
169#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
170#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
171#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
172#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
173
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200174#define _R(NAME,TD,FW,SZ) { \
Francois Romieud58d46b2011-05-03 16:38:29 +0200175 .name = NAME, \
176 .txd_version = TD, \
177 .fw_name = FW, \
178 .jumbo_max = SZ, \
Francois Romieud58d46b2011-05-03 16:38:29 +0200179}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800181static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700183 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200184 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200185 u16 jumbo_max;
Francois Romieu85bffe62011-04-27 08:22:39 +0200186} rtl_chip_infos[] = {
187 /* PCI devices. */
188 [RTL_GIGA_MAC_VER_01] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200189 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200190 [RTL_GIGA_MAC_VER_02] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200191 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200192 [RTL_GIGA_MAC_VER_03] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200193 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200194 [RTL_GIGA_MAC_VER_04] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200195 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200196 [RTL_GIGA_MAC_VER_05] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200197 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200198 [RTL_GIGA_MAC_VER_06] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200199 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200200 /* PCI-E devices. */
201 [RTL_GIGA_MAC_VER_07] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200202 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200203 [RTL_GIGA_MAC_VER_08] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200204 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200205 [RTL_GIGA_MAC_VER_09] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200206 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200207 [RTL_GIGA_MAC_VER_10] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200208 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200209 [RTL_GIGA_MAC_VER_11] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200210 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200211 [RTL_GIGA_MAC_VER_12] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200212 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200213 [RTL_GIGA_MAC_VER_13] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200214 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200215 [RTL_GIGA_MAC_VER_14] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200216 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200217 [RTL_GIGA_MAC_VER_15] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200218 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200219 [RTL_GIGA_MAC_VER_16] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200220 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200221 [RTL_GIGA_MAC_VER_17] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200222 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200223 [RTL_GIGA_MAC_VER_18] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200224 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200225 [RTL_GIGA_MAC_VER_19] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200226 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200227 [RTL_GIGA_MAC_VER_20] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200228 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200229 [RTL_GIGA_MAC_VER_21] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200230 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200231 [RTL_GIGA_MAC_VER_22] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200232 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200233 [RTL_GIGA_MAC_VER_23] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200234 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200235 [RTL_GIGA_MAC_VER_24] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200236 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200237 [RTL_GIGA_MAC_VER_25] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200238 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200239 [RTL_GIGA_MAC_VER_26] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200240 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200241 [RTL_GIGA_MAC_VER_27] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200242 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200243 [RTL_GIGA_MAC_VER_28] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200244 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200245 [RTL_GIGA_MAC_VER_29] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200246 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200247 [RTL_GIGA_MAC_VER_30] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200248 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200249 [RTL_GIGA_MAC_VER_31] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200250 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200251 [RTL_GIGA_MAC_VER_32] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200252 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200253 [RTL_GIGA_MAC_VER_33] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200254 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2, JUMBO_9K),
Hayes Wang70090422011-07-06 15:58:06 +0800255 [RTL_GIGA_MAC_VER_34] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200256 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3, JUMBO_9K),
Hayes Wangc2218922011-09-06 16:55:18 +0800257 [RTL_GIGA_MAC_VER_35] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200258 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1, JUMBO_9K),
Hayes Wangc2218922011-09-06 16:55:18 +0800259 [RTL_GIGA_MAC_VER_36] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200260 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2, JUMBO_9K),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800261 [RTL_GIGA_MAC_VER_37] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200262 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1, JUMBO_1K),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800263 [RTL_GIGA_MAC_VER_38] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200264 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1, JUMBO_9K),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800265 [RTL_GIGA_MAC_VER_39] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200266 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1, JUMBO_1K),
Hayes Wangc5583862012-07-02 17:23:22 +0800267 [RTL_GIGA_MAC_VER_40] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200268 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2, JUMBO_9K),
Hayes Wangc5583862012-07-02 17:23:22 +0800269 [RTL_GIGA_MAC_VER_41] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200270 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K),
hayeswang57538c42013-04-01 22:23:40 +0000271 [RTL_GIGA_MAC_VER_42] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200272 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3, JUMBO_9K),
hayeswang58152cd2013-04-01 22:23:42 +0000273 [RTL_GIGA_MAC_VER_43] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200274 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2, JUMBO_1K),
hayeswang45dd95c2013-07-08 17:09:01 +0800275 [RTL_GIGA_MAC_VER_44] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200276 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2, JUMBO_9K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800277 [RTL_GIGA_MAC_VER_45] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200278 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1, JUMBO_9K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800279 [RTL_GIGA_MAC_VER_46] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200280 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2, JUMBO_9K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800281 [RTL_GIGA_MAC_VER_47] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200282 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1, JUMBO_1K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800283 [RTL_GIGA_MAC_VER_48] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200284 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2, JUMBO_1K),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800285 [RTL_GIGA_MAC_VER_49] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200286 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800287 [RTL_GIGA_MAC_VER_50] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200288 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800289 [RTL_GIGA_MAC_VER_51] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200290 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291};
292#undef _R
293
Francois Romieubcf0bf92006-07-26 23:14:13 +0200294enum cfg_version {
295 RTL_CFG_0 = 0x00,
296 RTL_CFG_1,
297 RTL_CFG_2
298};
299
Benoit Taine9baa3c32014-08-08 15:56:03 +0200300static const struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200301 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200302 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Chun-Hao Lin610c9082016-12-27 16:29:43 +0800303 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
Francois Romieud81bf552006-09-20 21:31:20 +0200304 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100305 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200306 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200307 { PCI_VENDOR_ID_DLINK, 0x4300,
308 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200309 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000310 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200311 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200312 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
313 { PCI_VENDOR_ID_LINKSYS, 0x1032,
314 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100315 { 0x0001, 0x8168,
316 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 {0,},
318};
319
320MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
321
Ard Biesheuvel27896c82016-05-14 22:40:15 +0200322static int use_dac = -1;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200323static struct {
324 u32 msg_enable;
325} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Francois Romieu07d3f512007-02-21 22:40:46 +0100327enum rtl_registers {
328 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100329 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100330 MAR0 = 8, /* Multicast filter. */
331 CounterAddrLow = 0x10,
332 CounterAddrHigh = 0x14,
333 TxDescStartAddrLow = 0x20,
334 TxDescStartAddrHigh = 0x24,
335 TxHDescStartAddrLow = 0x28,
336 TxHDescStartAddrHigh = 0x2c,
337 FLASH = 0x30,
338 ERSR = 0x36,
339 ChipCmd = 0x37,
340 TxPoll = 0x38,
341 IntrMask = 0x3c,
342 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700343
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800344 TxConfig = 0x40,
345#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
346#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
347
348 RxConfig = 0x44,
349#define RX128_INT_EN (1 << 15) /* 8111c and later */
350#define RX_MULTI_EN (1 << 14) /* 8111c only */
351#define RXCFG_FIFO_SHIFT 13
352 /* No threshold before first PCI xfer */
353#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000354#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800355#define RXCFG_DMA_SHIFT 8
356 /* Unlimited maximum PCI burst. */
357#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700358
Francois Romieu07d3f512007-02-21 22:40:46 +0100359 RxMissed = 0x4c,
360 Cfg9346 = 0x50,
361 Config0 = 0x51,
362 Config1 = 0x52,
363 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200364#define PME_SIGNAL (1 << 5) /* 8168c and later */
365
Francois Romieu07d3f512007-02-21 22:40:46 +0100366 Config3 = 0x54,
367 Config4 = 0x55,
368 Config5 = 0x56,
369 MultiIntr = 0x5c,
370 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100371 PHYstatus = 0x6c,
372 RxMaxSize = 0xda,
373 CPlusCmd = 0xe0,
374 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300375
376#define RTL_COALESCE_MASK 0x0f
377#define RTL_COALESCE_SHIFT 4
378#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
379#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
380
Francois Romieu07d3f512007-02-21 22:40:46 +0100381 RxDescAddrLow = 0xe4,
382 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000383 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
384
385#define NoEarlyTx 0x3f /* Max value : no early transmit. */
386
387 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
388
389#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800390#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000391
Francois Romieu07d3f512007-02-21 22:40:46 +0100392 FuncEvent = 0xf0,
393 FuncEventMask = 0xf4,
394 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800395 IBCR0 = 0xf8,
396 IBCR2 = 0xf9,
397 IBIMR0 = 0xfa,
398 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100399 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400};
401
Francois Romieuf162a5d2008-06-01 22:37:49 +0200402enum rtl8110_registers {
403 TBICSR = 0x64,
404 TBI_ANAR = 0x68,
405 TBI_LPAR = 0x6a,
406};
407
408enum rtl8168_8101_registers {
409 CSIDR = 0x64,
410 CSIAR = 0x68,
411#define CSIAR_FLAG 0x80000000
412#define CSIAR_WRITE_CMD 0x80000000
413#define CSIAR_BYTE_ENABLE 0x0f
414#define CSIAR_BYTE_ENABLE_SHIFT 12
415#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800416#define CSIAR_FUNC_CARD 0x00000000
417#define CSIAR_FUNC_SDIO 0x00010000
418#define CSIAR_FUNC_NIC 0x00020000
hayeswang45dd95c2013-07-08 17:09:01 +0800419#define CSIAR_FUNC_NIC2 0x00010000
françois romieu065c27c2011-01-03 15:08:12 +0000420 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200421 EPHYAR = 0x80,
422#define EPHYAR_FLAG 0x80000000
423#define EPHYAR_WRITE_CMD 0x80000000
424#define EPHYAR_REG_MASK 0x1f
425#define EPHYAR_REG_SHIFT 16
426#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800427 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800428#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800429#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200430 DBG_REG = 0xd1,
431#define FIX_NAK_1 (1 << 4)
432#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800433 TWSI = 0xd2,
434 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800435#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800436#define TX_EMPTY (1 << 5)
437#define RX_EMPTY (1 << 4)
438#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800439#define EN_NDP (1 << 3)
440#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800441#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000442 EFUSEAR = 0xdc,
443#define EFUSEAR_FLAG 0x80000000
444#define EFUSEAR_WRITE_CMD 0x80000000
445#define EFUSEAR_READ_CMD 0x00000000
446#define EFUSEAR_REG_MASK 0x03ff
447#define EFUSEAR_REG_SHIFT 8
448#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800449 MISC_1 = 0xf2,
450#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200451};
452
françois romieuc0e45c12011-01-03 15:08:04 +0000453enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800454 LED_FREQ = 0x1a,
455 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000456 ERIDR = 0x70,
457 ERIAR = 0x74,
458#define ERIAR_FLAG 0x80000000
459#define ERIAR_WRITE_CMD 0x80000000
460#define ERIAR_READ_CMD 0x00000000
461#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000462#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800463#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
464#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
465#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800466#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800467#define ERIAR_MASK_SHIFT 12
468#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
469#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800470#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800471#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800472#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000473 EPHY_RXER_NUM = 0x7c,
474 OCPDR = 0xb0, /* OCP GPHY access */
475#define OCPDR_WRITE_CMD 0x80000000
476#define OCPDR_READ_CMD 0x00000000
477#define OCPDR_REG_MASK 0x7f
478#define OCPDR_GPHY_REG_SHIFT 16
479#define OCPDR_DATA_MASK 0xffff
480 OCPAR = 0xb4,
481#define OCPAR_FLAG 0x80000000
482#define OCPAR_GPHY_WRITE_CMD 0x8000f060
483#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800484 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000485 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
486 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200487#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800488#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800489#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800490#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800491#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000492};
493
Francois Romieu07d3f512007-02-21 22:40:46 +0100494enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100496 SYSErr = 0x8000,
497 PCSTimeout = 0x4000,
498 SWInt = 0x0100,
499 TxDescUnavail = 0x0080,
500 RxFIFOOver = 0x0040,
501 LinkChg = 0x0020,
502 RxOverflow = 0x0010,
503 TxErr = 0x0008,
504 TxOK = 0x0004,
505 RxErr = 0x0002,
506 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400509 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200510 RxFOVF = (1 << 23),
511 RxRWT = (1 << 22),
512 RxRES = (1 << 21),
513 RxRUNT = (1 << 20),
514 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800517 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100518 CmdReset = 0x10,
519 CmdRxEnb = 0x08,
520 CmdTxEnb = 0x04,
521 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Francois Romieu275391a2007-02-23 23:50:28 +0100523 /* TXPoll register p.5 */
524 HPQ = 0x80, /* Poll cmd on the high prio queue */
525 NPQ = 0x40, /* Poll cmd on the low prio queue */
526 FSWInt = 0x01, /* Forced software interrupt */
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100529 Cfg9346_Lock = 0x00,
530 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100533 AcceptErr = 0x20,
534 AcceptRunt = 0x10,
535 AcceptBroadcast = 0x08,
536 AcceptMulticast = 0x04,
537 AcceptMyPhys = 0x02,
538 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200539#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /* TxConfigBits */
542 TxInterFrameGapShift = 24,
543 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
544
Francois Romieu5d06a992006-02-23 00:47:58 +0100545 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200546 LEDS1 = (1 << 7),
547 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200548 Speed_down = (1 << 4),
549 MEMMAP = (1 << 3),
550 IOMAP = (1 << 2),
551 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100552 PMEnable = (1 << 0), /* Power Management Enable */
553
Francois Romieu6dccd162007-02-13 23:38:05 +0100554 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000555 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000556 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100557 PCI_Clock_66MHz = 0x01,
558 PCI_Clock_33MHz = 0x00,
559
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100560 /* Config3 register p.25 */
561 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
562 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200563 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800564 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200565 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100566
Francois Romieud58d46b2011-05-03 16:38:29 +0200567 /* Config4 register */
568 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
569
Francois Romieu5d06a992006-02-23 00:47:58 +0100570 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100571 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
572 MWF = (1 << 5), /* Accept Multicast wakeup frame */
573 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200574 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100575 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100576 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000577 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* TBICSR p.28 */
580 TBIReset = 0x80000000,
581 TBILoopback = 0x40000000,
582 TBINwEnable = 0x20000000,
583 TBINwRestart = 0x10000000,
584 TBILinkOk = 0x02000000,
585 TBINwComplete = 0x01000000,
586
587 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200588 EnableBist = (1 << 15), // 8168 8101
589 Mac_dbgo_oe = (1 << 14), // 8168 8101
590 Normal_mode = (1 << 13), // unused
591 Force_half_dup = (1 << 12), // 8168 8101
592 Force_rxflow_en = (1 << 11), // 8168 8101
593 Force_txflow_en = (1 << 10), // 8168 8101
594 Cxpl_dbg_sel = (1 << 9), // 8168 8101
595 ASF = (1 << 8), // 8168 8101
596 PktCntrDisable = (1 << 7), // 8168 8101
597 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 RxVlan = (1 << 6),
599 RxChkSum = (1 << 5),
600 PCIDAC = (1 << 4),
601 PCIMulRW = (1 << 3),
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +0200602#define INTT_MASK GENMASK(1, 0)
Francois Romieu0e485152007-02-20 00:00:26 +0100603 INTT_0 = 0x0000, // 8168
604 INTT_1 = 0x0001, // 8168
605 INTT_2 = 0x0002, // 8168
606 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100609 TBI_Enable = 0x80,
610 TxFlowCtrl = 0x40,
611 RxFlowCtrl = 0x20,
612 _1000bpsF = 0x10,
613 _100bps = 0x08,
614 _10bps = 0x04,
615 LinkStatus = 0x02,
616 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100619 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200620
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200621 /* ResetCounterCommand */
622 CounterReset = 0x1,
623
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200624 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100625 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800626
627 /* magic enable v2 */
628 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629};
630
Francois Romieu2b7b4312011-04-18 22:53:24 -0700631enum rtl_desc_bit {
632 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
634 RingEnd = (1 << 30), /* End of descriptor ring */
635 FirstFrag = (1 << 29), /* First segment of a packet */
636 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700637};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Francois Romieu2b7b4312011-04-18 22:53:24 -0700639/* Generic case. */
640enum rtl_tx_desc_bit {
641 /* First doubleword. */
642 TD_LSO = (1 << 27), /* Large Send Offload */
643#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Francois Romieu2b7b4312011-04-18 22:53:24 -0700645 /* Second doubleword. */
646 TxVlanTag = (1 << 17), /* Add VLAN tag */
647};
648
649/* 8169, 8168b and 810x except 8102e. */
650enum rtl_tx_desc_bit_0 {
651 /* First doubleword. */
652#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
653 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
654 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
655 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
656};
657
658/* 8102e, 8168c and beyond. */
659enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800660 /* First doubleword. */
661 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800662 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800663#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800664#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800665
Francois Romieu2b7b4312011-04-18 22:53:24 -0700666 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800667#define TCPHO_SHIFT 18
668#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700669#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800670 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
671 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700672 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
673 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
674};
675
Francois Romieu2b7b4312011-04-18 22:53:24 -0700676enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /* Rx private */
678 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500679 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681#define RxProtoUDP (PID1)
682#define RxProtoTCP (PID0)
683#define RxProtoIP (PID1 | PID0)
684#define RxProtoMask RxProtoIP
685
686 IPFail = (1 << 16), /* IP checksum failed */
687 UDPFail = (1 << 15), /* UDP/IP checksum failed */
688 TCPFail = (1 << 14), /* TCP/IP checksum failed */
689 RxVlanTag = (1 << 16), /* VLAN tag available */
690};
691
692#define RsvdMask 0x3fffc000
Heiner Kallweit12d42c52018-04-28 22:19:30 +0200693#define CPCMD_QUIRK_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695struct TxDesc {
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 RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200702 __le32 opts1;
703 __le32 opts2;
704 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705};
706
707struct ring_info {
708 struct sk_buff *skb;
709 u32 len;
710 u8 __pad[sizeof(void *) - sizeof(u32)];
711};
712
Ivan Vecera355423d2009-02-06 21:49:57 -0800713struct rtl8169_counters {
714 __le64 tx_packets;
715 __le64 rx_packets;
716 __le64 tx_errors;
717 __le32 rx_errors;
718 __le16 rx_missed;
719 __le16 align_errors;
720 __le32 tx_one_collision;
721 __le32 tx_multi_collision;
722 __le64 rx_unicast;
723 __le64 rx_broadcast;
724 __le32 rx_multicast;
725 __le16 tx_aborted;
726 __le16 tx_underun;
727};
728
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200729struct rtl8169_tc_offsets {
730 bool inited;
731 __le64 tx_errors;
732 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200733 __le16 tx_aborted;
734};
735
Francois Romieuda78dbf2012-01-26 14:18:23 +0100736enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100737 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100738 RTL_FLAG_TASK_SLOW_PENDING,
739 RTL_FLAG_TASK_RESET_PENDING,
740 RTL_FLAG_TASK_PHY_PENDING,
741 RTL_FLAG_MAX
742};
743
Junchang Wang8027aa22012-03-04 23:30:32 +0100744struct rtl8169_stats {
745 u64 packets;
746 u64 bytes;
747 struct u64_stats_sync syncp;
748};
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750struct rtl8169_private {
751 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200752 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000753 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700754 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200755 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700756 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
758 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100760 struct rtl8169_stats rx_stats;
761 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
763 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
764 dma_addr_t TxPhyAddr;
765 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000766 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 struct timer_list timer;
769 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100770
771 u16 event_slow;
Francois Romieu50970832017-10-27 13:24:49 +0300772 const struct rtl_coalesce_info *coalesce_info;
françois romieuc0e45c12011-01-03 15:08:04 +0000773
774 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200775 void (*write)(struct rtl8169_private *, int, int);
776 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000777 } mdio_ops;
778
Francois Romieud58d46b2011-05-03 16:38:29 +0200779 struct jumbo_ops {
780 void (*enable)(struct rtl8169_private *);
781 void (*disable)(struct rtl8169_private *);
782 } jumbo_ops;
783
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800784 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200785 void (*write)(struct rtl8169_private *, int, int);
786 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800787 } csi_ops;
788
Oliver Neukum54405cd2011-01-06 21:55:13 +0100789 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +0100790 int (*get_link_ksettings)(struct net_device *,
791 struct ethtool_link_ksettings *);
françois romieu4da19632011-01-03 15:07:55 +0000792 void (*phy_reset_enable)(struct rtl8169_private *tp);
Heiner Kallweit61cb5322018-04-17 23:27:38 +0200793 void (*hw_start)(struct rtl8169_private *tp);
françois romieu4da19632011-01-03 15:07:55 +0000794 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200795 unsigned int (*link_ok)(struct rtl8169_private *tp);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800796 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800797 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100798
799 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100800 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
801 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100802 struct work_struct work;
803 } wk;
804
Francois Romieuccdffb92008-07-26 14:26:06 +0200805 struct mii_if_info mii;
Corinna Vinschen42020322015-09-10 10:47:35 +0200806 dma_addr_t counters_phys_addr;
807 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200808 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000809 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000810
Francois Romieub6ffd972011-06-17 17:00:05 +0200811 struct rtl_fw {
812 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200813
814#define RTL_VER_SIZE 32
815
816 char version[RTL_VER_SIZE];
817
818 struct rtl_fw_phy_action {
819 __le32 *code;
820 size_t size;
821 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200822 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300823#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800824
825 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826};
827
Ralf Baechle979b6c12005-06-13 14:30:40 -0700828MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700831MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200832module_param_named(debug, debug.msg_enable, int, 0);
833MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834MODULE_LICENSE("GPL");
835MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000836MODULE_FIRMWARE(FIRMWARE_8168D_1);
837MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000838MODULE_FIRMWARE(FIRMWARE_8168E_1);
839MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400840MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800841MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800842MODULE_FIRMWARE(FIRMWARE_8168F_1);
843MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800844MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800845MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800846MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800847MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000848MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000849MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000850MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800851MODULE_FIRMWARE(FIRMWARE_8168H_1);
852MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200853MODULE_FIRMWARE(FIRMWARE_8107E_1);
854MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Heiner Kallweit1e1205b2018-03-20 07:45:42 +0100856static inline struct device *tp_to_dev(struct rtl8169_private *tp)
857{
858 return &tp->pci_dev->dev;
859}
860
Francois Romieuda78dbf2012-01-26 14:18:23 +0100861static void rtl_lock_work(struct rtl8169_private *tp)
862{
863 mutex_lock(&tp->wk.mutex);
864}
865
866static void rtl_unlock_work(struct rtl8169_private *tp)
867{
868 mutex_unlock(&tp->wk.mutex);
869}
870
Heiner Kallweitcb732002018-03-20 07:45:35 +0100871static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
Francois Romieud58d46b2011-05-03 16:38:29 +0200872{
Heiner Kallweitcb732002018-03-20 07:45:35 +0100873 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +0800874 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200875}
876
Francois Romieuffc46952012-07-06 14:19:23 +0200877struct rtl_cond {
878 bool (*check)(struct rtl8169_private *);
879 const char *msg;
880};
881
882static void rtl_udelay(unsigned int d)
883{
884 udelay(d);
885}
886
887static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
888 void (*delay)(unsigned int), unsigned int d, int n,
889 bool high)
890{
891 int i;
892
893 for (i = 0; i < n; i++) {
894 delay(d);
895 if (c->check(tp) == high)
896 return true;
897 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200898 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
899 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200900 return false;
901}
902
903static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
904 const struct rtl_cond *c,
905 unsigned int d, int n)
906{
907 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
908}
909
910static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
911 const struct rtl_cond *c,
912 unsigned int d, int n)
913{
914 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
915}
916
917static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
918 const struct rtl_cond *c,
919 unsigned int d, int n)
920{
921 return rtl_loop_wait(tp, c, msleep, d, n, true);
922}
923
924static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
925 const struct rtl_cond *c,
926 unsigned int d, int n)
927{
928 return rtl_loop_wait(tp, c, msleep, d, n, false);
929}
930
931#define DECLARE_RTL_COND(name) \
932static bool name ## _check(struct rtl8169_private *); \
933 \
934static const struct rtl_cond name = { \
935 .check = name ## _check, \
936 .msg = #name \
937}; \
938 \
939static bool name ## _check(struct rtl8169_private *tp)
940
Hayes Wangc5583862012-07-02 17:23:22 +0800941static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
942{
943 if (reg & 0xffff0001) {
944 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
945 return true;
946 }
947 return false;
948}
949
950DECLARE_RTL_COND(rtl_ocp_gphy_cond)
951{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200952 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
Hayes Wangc5583862012-07-02 17:23:22 +0800953}
954
955static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
956{
Hayes Wangc5583862012-07-02 17:23:22 +0800957 if (rtl_ocp_reg_failure(tp, reg))
958 return;
959
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200960 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800961
962 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
963}
964
965static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
966{
Hayes Wangc5583862012-07-02 17:23:22 +0800967 if (rtl_ocp_reg_failure(tp, reg))
968 return 0;
969
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200970 RTL_W32(tp, GPHY_OCP, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800971
972 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200973 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
Hayes Wangc5583862012-07-02 17:23:22 +0800974}
975
Hayes Wangc5583862012-07-02 17:23:22 +0800976static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
977{
Hayes Wangc5583862012-07-02 17:23:22 +0800978 if (rtl_ocp_reg_failure(tp, reg))
979 return;
980
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200981 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800982}
983
984static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
985{
Hayes Wangc5583862012-07-02 17:23:22 +0800986 if (rtl_ocp_reg_failure(tp, reg))
987 return 0;
988
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200989 RTL_W32(tp, OCPDR, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800990
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200991 return RTL_R32(tp, OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +0800992}
993
994#define OCP_STD_PHY_BASE 0xa400
995
996static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
997{
998 if (reg == 0x1f) {
999 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1000 return;
1001 }
1002
1003 if (tp->ocp_base != OCP_STD_PHY_BASE)
1004 reg -= 0x10;
1005
1006 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1007}
1008
1009static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1010{
1011 if (tp->ocp_base != OCP_STD_PHY_BASE)
1012 reg -= 0x10;
1013
1014 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1015}
1016
hayeswangeee37862013-04-01 22:23:38 +00001017static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1018{
1019 if (reg == 0x1f) {
1020 tp->ocp_base = value << 4;
1021 return;
1022 }
1023
1024 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1025}
1026
1027static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1028{
1029 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1030}
1031
Francois Romieuffc46952012-07-06 14:19:23 +02001032DECLARE_RTL_COND(rtl_phyar_cond)
1033{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001034 return RTL_R32(tp, PHYAR) & 0x80000000;
Francois Romieuffc46952012-07-06 14:19:23 +02001035}
1036
Francois Romieu24192212012-07-06 20:19:42 +02001037static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001039 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Francois Romieuffc46952012-07-06 14:19:23 +02001041 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001042 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001043 * According to hardware specs a 20us delay is required after write
1044 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001045 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001046 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
Francois Romieu24192212012-07-06 20:19:42 +02001049static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Francois Romieuffc46952012-07-06 14:19:23 +02001051 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001053 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Francois Romieuffc46952012-07-06 14:19:23 +02001055 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001056 RTL_R32(tp, PHYAR) & 0xffff : ~0;
Francois Romieuffc46952012-07-06 14:19:23 +02001057
Timo Teräs81a95f02010-06-09 17:31:48 -07001058 /*
1059 * According to hardware specs a 20us delay is required after read
1060 * complete indication, but before sending next command.
1061 */
1062 udelay(20);
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return value;
1065}
1066
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001067DECLARE_RTL_COND(rtl_ocpar_cond)
1068{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001069 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001070}
1071
Francois Romieu24192212012-07-06 20:19:42 +02001072static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001073{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001074 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1075 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
1076 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001077
Francois Romieuffc46952012-07-06 14:19:23 +02001078 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001079}
1080
Francois Romieu24192212012-07-06 20:19:42 +02001081static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001082{
Francois Romieu24192212012-07-06 20:19:42 +02001083 r8168dp_1_mdio_access(tp, reg,
1084 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001085}
1086
Francois Romieu24192212012-07-06 20:19:42 +02001087static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001088{
Francois Romieu24192212012-07-06 20:19:42 +02001089 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001090
1091 mdelay(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001092 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
1093 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001094
Francois Romieuffc46952012-07-06 14:19:23 +02001095 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001096 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001097}
1098
françois romieue6de30d2011-01-03 15:08:37 +00001099#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1100
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001101static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001102{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001103 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001104}
1105
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001106static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001107{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001108 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001109}
1110
Francois Romieu24192212012-07-06 20:19:42 +02001111static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001112{
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 r8169_mdio_write(tp, reg, value);
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
Francois Romieu24192212012-07-06 20:19:42 +02001120static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001121{
1122 int value;
1123
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001124 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001125
Francois Romieu24192212012-07-06 20:19:42 +02001126 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001127
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001128 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001129
1130 return value;
1131}
1132
françois romieu4da19632011-01-03 15:07:55 +00001133static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001134{
Francois Romieu24192212012-07-06 20:19:42 +02001135 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001136}
1137
françois romieu4da19632011-01-03 15:07:55 +00001138static int rtl_readphy(struct rtl8169_private *tp, int location)
1139{
Francois Romieu24192212012-07-06 20:19:42 +02001140 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001141}
1142
1143static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1144{
1145 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1146}
1147
Chun-Hao Lin76564422014-10-01 23:17:17 +08001148static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001149{
1150 int val;
1151
françois romieu4da19632011-01-03 15:07:55 +00001152 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001153 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001154}
1155
Francois Romieuccdffb92008-07-26 14:26:06 +02001156static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1157 int val)
1158{
1159 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001160
françois romieu4da19632011-01-03 15:07:55 +00001161 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001162}
1163
1164static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1165{
1166 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001167
françois romieu4da19632011-01-03 15:07:55 +00001168 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001169}
1170
Francois Romieuffc46952012-07-06 14:19:23 +02001171DECLARE_RTL_COND(rtl_ephyar_cond)
1172{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001173 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001174}
1175
Francois Romieufdf6fc02012-07-06 22:40:38 +02001176static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001177{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001178 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
Francois Romieudacf8152008-08-02 20:44:13 +02001179 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1180
Francois Romieuffc46952012-07-06 14:19:23 +02001181 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1182
1183 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001184}
1185
Francois Romieufdf6fc02012-07-06 22:40:38 +02001186static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001187{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001188 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
Francois Romieudacf8152008-08-02 20:44:13 +02001189
Francois Romieuffc46952012-07-06 14:19:23 +02001190 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001191 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001192}
1193
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001194DECLARE_RTL_COND(rtl_eriar_cond)
1195{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001196 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001197}
1198
Francois Romieufdf6fc02012-07-06 22:40:38 +02001199static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1200 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001201{
Hayes Wang133ac402011-07-06 15:58:05 +08001202 BUG_ON((addr & 3) || (mask == 0));
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001203 RTL_W32(tp, ERIDR, val);
1204 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001205
Francois Romieuffc46952012-07-06 14:19:23 +02001206 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001207}
1208
Francois Romieufdf6fc02012-07-06 22:40:38 +02001209static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001210{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001211 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001212
Francois Romieuffc46952012-07-06 14:19:23 +02001213 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001214 RTL_R32(tp, ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001215}
1216
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001217static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001218 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001219{
1220 u32 val;
1221
Francois Romieufdf6fc02012-07-06 22:40:38 +02001222 val = rtl_eri_read(tp, addr, type);
1223 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001224}
1225
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001226static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1227{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001228 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001229 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001230 RTL_R32(tp, OCPDR) : ~0;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001231}
1232
1233static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1234{
1235 return rtl_eri_read(tp, reg, ERIAR_OOB);
1236}
1237
1238static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1239{
1240 switch (tp->mac_version) {
1241 case RTL_GIGA_MAC_VER_27:
1242 case RTL_GIGA_MAC_VER_28:
1243 case RTL_GIGA_MAC_VER_31:
1244 return r8168dp_ocp_read(tp, mask, reg);
1245 case RTL_GIGA_MAC_VER_49:
1246 case RTL_GIGA_MAC_VER_50:
1247 case RTL_GIGA_MAC_VER_51:
1248 return r8168ep_ocp_read(tp, mask, reg);
1249 default:
1250 BUG();
1251 return ~0;
1252 }
1253}
1254
1255static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1256 u32 data)
1257{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001258 RTL_W32(tp, OCPDR, data);
1259 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001260 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1261}
1262
1263static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1264 u32 data)
1265{
1266 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1267 data, ERIAR_OOB);
1268}
1269
1270static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1271{
1272 switch (tp->mac_version) {
1273 case RTL_GIGA_MAC_VER_27:
1274 case RTL_GIGA_MAC_VER_28:
1275 case RTL_GIGA_MAC_VER_31:
1276 r8168dp_ocp_write(tp, mask, reg, data);
1277 break;
1278 case RTL_GIGA_MAC_VER_49:
1279 case RTL_GIGA_MAC_VER_50:
1280 case RTL_GIGA_MAC_VER_51:
1281 r8168ep_ocp_write(tp, mask, reg, data);
1282 break;
1283 default:
1284 BUG();
1285 break;
1286 }
1287}
1288
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001289static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1290{
1291 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1292
1293 ocp_write(tp, 0x1, 0x30, 0x00000001);
1294}
1295
1296#define OOB_CMD_RESET 0x00
1297#define OOB_CMD_DRIVER_START 0x05
1298#define OOB_CMD_DRIVER_STOP 0x06
1299
1300static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1301{
1302 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1303}
1304
1305DECLARE_RTL_COND(rtl_ocp_read_cond)
1306{
1307 u16 reg;
1308
1309 reg = rtl8168_get_ocp_reg(tp);
1310
1311 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1312}
1313
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001314DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1315{
1316 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1317}
1318
1319DECLARE_RTL_COND(rtl_ocp_tx_cond)
1320{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001321 return RTL_R8(tp, IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001322}
1323
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001324static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1325{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001326 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001327 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001328 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1329 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001330}
1331
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001332static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001333{
1334 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001335 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1336}
1337
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001338static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1339{
1340 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1341 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1342 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1343}
1344
1345static void rtl8168_driver_start(struct rtl8169_private *tp)
1346{
1347 switch (tp->mac_version) {
1348 case RTL_GIGA_MAC_VER_27:
1349 case RTL_GIGA_MAC_VER_28:
1350 case RTL_GIGA_MAC_VER_31:
1351 rtl8168dp_driver_start(tp);
1352 break;
1353 case RTL_GIGA_MAC_VER_49:
1354 case RTL_GIGA_MAC_VER_50:
1355 case RTL_GIGA_MAC_VER_51:
1356 rtl8168ep_driver_start(tp);
1357 break;
1358 default:
1359 BUG();
1360 break;
1361 }
1362}
1363
1364static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1365{
1366 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1367 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1368}
1369
1370static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1371{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001372 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001373 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1374 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1375 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1376}
1377
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001378static void rtl8168_driver_stop(struct rtl8169_private *tp)
1379{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001380 switch (tp->mac_version) {
1381 case RTL_GIGA_MAC_VER_27:
1382 case RTL_GIGA_MAC_VER_28:
1383 case RTL_GIGA_MAC_VER_31:
1384 rtl8168dp_driver_stop(tp);
1385 break;
1386 case RTL_GIGA_MAC_VER_49:
1387 case RTL_GIGA_MAC_VER_50:
1388 case RTL_GIGA_MAC_VER_51:
1389 rtl8168ep_driver_stop(tp);
1390 break;
1391 default:
1392 BUG();
1393 break;
1394 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001395}
1396
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001397static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001398{
1399 u16 reg = rtl8168_get_ocp_reg(tp);
1400
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001401 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001402}
1403
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001404static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001405{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001406 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001407}
1408
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001409static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001410{
1411 switch (tp->mac_version) {
1412 case RTL_GIGA_MAC_VER_27:
1413 case RTL_GIGA_MAC_VER_28:
1414 case RTL_GIGA_MAC_VER_31:
1415 return r8168dp_check_dash(tp);
1416 case RTL_GIGA_MAC_VER_49:
1417 case RTL_GIGA_MAC_VER_50:
1418 case RTL_GIGA_MAC_VER_51:
1419 return r8168ep_check_dash(tp);
1420 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001421 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001422 }
1423}
1424
françois romieuc28aa382011-08-02 03:53:43 +00001425struct exgmac_reg {
1426 u16 addr;
1427 u16 mask;
1428 u32 val;
1429};
1430
Francois Romieufdf6fc02012-07-06 22:40:38 +02001431static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001432 const struct exgmac_reg *r, int len)
1433{
1434 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001435 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001436 r++;
1437 }
1438}
1439
Francois Romieuffc46952012-07-06 14:19:23 +02001440DECLARE_RTL_COND(rtl_efusear_cond)
1441{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001442 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001443}
1444
Francois Romieufdf6fc02012-07-06 22:40:38 +02001445static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001446{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001447 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
françois romieudaf9df62009-10-07 12:44:20 +00001448
Francois Romieuffc46952012-07-06 14:19:23 +02001449 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001450 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001451}
1452
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001453static u16 rtl_get_events(struct rtl8169_private *tp)
1454{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001455 return RTL_R16(tp, IntrStatus);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001456}
1457
1458static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1459{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001460 RTL_W16(tp, IntrStatus, bits);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001461 mmiowb();
1462}
1463
1464static void rtl_irq_disable(struct rtl8169_private *tp)
1465{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001466 RTL_W16(tp, IntrMask, 0);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001467 mmiowb();
1468}
1469
Francois Romieu3e990ff2012-01-26 12:50:01 +01001470static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1471{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001472 RTL_W16(tp, IntrMask, bits);
Francois Romieu3e990ff2012-01-26 12:50:01 +01001473}
1474
Francois Romieuda78dbf2012-01-26 14:18:23 +01001475#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1476#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1477#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1478
1479static void rtl_irq_enable_all(struct rtl8169_private *tp)
1480{
1481 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1482}
1483
françois romieu811fd302011-12-04 20:30:45 +00001484static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001486 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001487 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001488 RTL_R8(tp, ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
françois romieu4da19632011-01-03 15:07:55 +00001491static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001493 return RTL_R32(tp, TBICSR) & TBIReset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
françois romieu4da19632011-01-03 15:07:55 +00001496static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
françois romieu4da19632011-01-03 15:07:55 +00001498 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001501static unsigned int rtl8169_tbi_link_ok(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001503 return RTL_R32(tp, TBICSR) & TBILinkOk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001506static unsigned int rtl8169_xmii_link_ok(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001508 return RTL_R8(tp, PHYstatus) & LinkStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
françois romieu4da19632011-01-03 15:07:55 +00001511static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001513 RTL_W32(tp, TBICSR, RTL_R32(tp, TBICSR) | TBIReset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
1515
françois romieu4da19632011-01-03 15:07:55 +00001516static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
1518 unsigned int val;
1519
françois romieu4da19632011-01-03 15:07:55 +00001520 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1521 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
Hayes Wang70090422011-07-06 15:58:06 +08001524static void rtl_link_chg_patch(struct rtl8169_private *tp)
1525{
Hayes Wang70090422011-07-06 15:58:06 +08001526 struct net_device *dev = tp->dev;
1527
1528 if (!netif_running(dev))
1529 return;
1530
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001531 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1532 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001533 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001534 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1535 ERIAR_EXGMAC);
1536 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1537 ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001538 } else if (RTL_R8(tp, PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001539 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1540 ERIAR_EXGMAC);
1541 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1542 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001543 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001544 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1545 ERIAR_EXGMAC);
1546 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1547 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001548 }
1549 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001550 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001551 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001552 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001553 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001554 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1555 tp->mac_version == RTL_GIGA_MAC_VER_36) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001556 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001557 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1558 ERIAR_EXGMAC);
1559 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1560 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001561 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001562 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1563 ERIAR_EXGMAC);
1564 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1565 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001566 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001567 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001568 if (RTL_R8(tp, PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001569 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1570 ERIAR_EXGMAC);
1571 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1572 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001573 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001574 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1575 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001576 }
Hayes Wang70090422011-07-06 15:58:06 +08001577 }
1578}
1579
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01001580static void rtl8169_check_link_status(struct net_device *dev,
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001581 struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001583 struct device *d = tp_to_dev(tp);
1584
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001585 if (tp->link_ok(tp)) {
Hayes Wang70090422011-07-06 15:58:06 +08001586 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001587 /* This is to cancel a scheduled suspend if there's one. */
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001588 pm_request_resume(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001590 if (net_ratelimit())
1591 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001592 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001594 netif_info(tp, ifdown, dev, "link down\n");
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001595 pm_runtime_idle(d);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
1598
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001599#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1600
1601static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1602{
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001603 u8 options;
1604 u32 wolopts = 0;
1605
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001606 options = RTL_R8(tp, Config1);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001607 if (!(options & PMEnable))
1608 return 0;
1609
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001610 options = RTL_R8(tp, Config3);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001611 if (options & LinkUp)
1612 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001613 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001614 case RTL_GIGA_MAC_VER_34:
1615 case RTL_GIGA_MAC_VER_35:
1616 case RTL_GIGA_MAC_VER_36:
1617 case RTL_GIGA_MAC_VER_37:
1618 case RTL_GIGA_MAC_VER_38:
1619 case RTL_GIGA_MAC_VER_40:
1620 case RTL_GIGA_MAC_VER_41:
1621 case RTL_GIGA_MAC_VER_42:
1622 case RTL_GIGA_MAC_VER_43:
1623 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001624 case RTL_GIGA_MAC_VER_45:
1625 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001626 case RTL_GIGA_MAC_VER_47:
1627 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001628 case RTL_GIGA_MAC_VER_49:
1629 case RTL_GIGA_MAC_VER_50:
1630 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001631 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1632 wolopts |= WAKE_MAGIC;
1633 break;
1634 default:
1635 if (options & MagicPacket)
1636 wolopts |= WAKE_MAGIC;
1637 break;
1638 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001639
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001640 options = RTL_R8(tp, Config5);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001641 if (options & UWF)
1642 wolopts |= WAKE_UCAST;
1643 if (options & BWF)
1644 wolopts |= WAKE_BCAST;
1645 if (options & MWF)
1646 wolopts |= WAKE_MCAST;
1647
1648 return wolopts;
1649}
1650
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001651static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1652{
1653 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001654 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001655
1656 pm_runtime_get_noresume(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001657
Francois Romieuda78dbf2012-01-26 14:18:23 +01001658 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001659
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001660 wol->supported = WAKE_ANY;
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001661 if (pm_runtime_active(d))
1662 wol->wolopts = __rtl8169_get_wol(tp);
1663 else
1664 wol->wolopts = tp->saved_wolopts;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001665
Francois Romieuda78dbf2012-01-26 14:18:23 +01001666 rtl_unlock_work(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001667
1668 pm_runtime_put_noidle(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001669}
1670
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001671static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001672{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001673 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001674 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001675 u32 opt;
1676 u16 reg;
1677 u8 mask;
1678 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001679 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001680 { WAKE_UCAST, Config5, UWF },
1681 { WAKE_BCAST, Config5, BWF },
1682 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001683 { WAKE_ANY, Config5, LanWake },
1684 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001685 };
Francois Romieu851e6022012-04-17 11:10:11 +02001686 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001687
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001688 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001689
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001690 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001691 case RTL_GIGA_MAC_VER_34:
1692 case RTL_GIGA_MAC_VER_35:
1693 case RTL_GIGA_MAC_VER_36:
1694 case RTL_GIGA_MAC_VER_37:
1695 case RTL_GIGA_MAC_VER_38:
1696 case RTL_GIGA_MAC_VER_40:
1697 case RTL_GIGA_MAC_VER_41:
1698 case RTL_GIGA_MAC_VER_42:
1699 case RTL_GIGA_MAC_VER_43:
1700 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001701 case RTL_GIGA_MAC_VER_45:
1702 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001703 case RTL_GIGA_MAC_VER_47:
1704 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001705 case RTL_GIGA_MAC_VER_49:
1706 case RTL_GIGA_MAC_VER_50:
1707 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001708 tmp = ARRAY_SIZE(cfg) - 1;
1709 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001710 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001711 0x0dc,
1712 ERIAR_MASK_0100,
1713 MagicPacket_v2,
1714 0x0000,
1715 ERIAR_EXGMAC);
1716 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001717 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001718 0x0dc,
1719 ERIAR_MASK_0100,
1720 0x0000,
1721 MagicPacket_v2,
1722 ERIAR_EXGMAC);
1723 break;
1724 default:
1725 tmp = ARRAY_SIZE(cfg);
1726 break;
1727 }
1728
1729 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001730 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001731 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001732 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001733 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001734 }
1735
Francois Romieu851e6022012-04-17 11:10:11 +02001736 switch (tp->mac_version) {
1737 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001738 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001739 if (wolopts)
1740 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001741 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001742 break;
1743 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001744 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001745 if (wolopts)
1746 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001747 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001748 break;
1749 }
1750
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001751 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001752}
1753
1754static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1755{
1756 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001757 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001758
1759 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001760
Francois Romieuda78dbf2012-01-26 14:18:23 +01001761 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001762
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001763 if (pm_runtime_active(d))
1764 __rtl8169_set_wol(tp, wol->wolopts);
1765 else
1766 tp->saved_wolopts = wol->wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001767
1768 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001769
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001770 device_set_wakeup_enable(d, wol->wolopts);
françois romieuea809072010-11-08 13:23:58 +00001771
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001772 pm_runtime_put_noidle(d);
1773
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001774 return 0;
1775}
1776
Francois Romieu31bd2042011-04-26 18:58:59 +02001777static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1778{
Francois Romieu85bffe62011-04-27 08:22:39 +02001779 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001780}
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782static void rtl8169_get_drvinfo(struct net_device *dev,
1783 struct ethtool_drvinfo *info)
1784{
1785 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001786 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Rick Jones68aad782011-11-07 13:29:27 +00001788 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1789 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1790 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001791 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001792 if (!IS_ERR_OR_NULL(rtl_fw))
1793 strlcpy(info->fw_version, rtl_fw->version,
1794 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
1796
1797static int rtl8169_get_regs_len(struct net_device *dev)
1798{
1799 return R8169_REGS_SIZE;
1800}
1801
1802static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001803 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
1805 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 int ret = 0;
1807 u32 reg;
1808
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001809 reg = RTL_R32(tp, TBICSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1811 (duplex == DUPLEX_FULL)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001812 RTL_W32(tp, TBICSR, reg & ~(TBINwEnable | TBINwRestart));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 } else if (autoneg == AUTONEG_ENABLE)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001814 RTL_W32(tp, TBICSR, reg | TBINwEnable | TBINwRestart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001816 netif_warn(tp, link, dev,
1817 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 ret = -EOPNOTSUPP;
1819 }
1820
1821 return ret;
1822}
1823
1824static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001825 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
1827 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001828 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001829 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Hayes Wang716b50a2011-02-22 17:26:18 +08001831 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001834 int auto_nego;
1835
françois romieu4da19632011-01-03 15:07:55 +00001836 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001837 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1838 ADVERTISE_100HALF | ADVERTISE_100FULL);
1839
1840 if (adv & ADVERTISED_10baseT_Half)
1841 auto_nego |= ADVERTISE_10HALF;
1842 if (adv & ADVERTISED_10baseT_Full)
1843 auto_nego |= ADVERTISE_10FULL;
1844 if (adv & ADVERTISED_100baseT_Half)
1845 auto_nego |= ADVERTISE_100HALF;
1846 if (adv & ADVERTISED_100baseT_Full)
1847 auto_nego |= ADVERTISE_100FULL;
1848
françois romieu3577aa12009-05-19 10:46:48 +00001849 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1850
françois romieu4da19632011-01-03 15:07:55 +00001851 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001852 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1853
1854 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001855 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001856 if (adv & ADVERTISED_1000baseT_Half)
1857 giga_ctrl |= ADVERTISE_1000HALF;
1858 if (adv & ADVERTISED_1000baseT_Full)
1859 giga_ctrl |= ADVERTISE_1000FULL;
1860 } else if (adv & (ADVERTISED_1000baseT_Half |
1861 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001862 netif_info(tp, link, dev,
1863 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001864 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
françois romieu3577aa12009-05-19 10:46:48 +00001867 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001868
françois romieu4da19632011-01-03 15:07:55 +00001869 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1870 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001871 } else {
françois romieu3577aa12009-05-19 10:46:48 +00001872 if (speed == SPEED_10)
1873 bmcr = 0;
1874 else if (speed == SPEED_100)
1875 bmcr = BMCR_SPEED100;
1876 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001877 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001878
1879 if (duplex == DUPLEX_FULL)
1880 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001881 }
1882
françois romieu4da19632011-01-03 15:07:55 +00001883 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001884
Francois Romieucecb5fd2011-04-01 10:21:07 +02001885 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1886 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001887 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001888 rtl_writephy(tp, 0x17, 0x2138);
1889 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001890 } else {
françois romieu4da19632011-01-03 15:07:55 +00001891 rtl_writephy(tp, 0x17, 0x2108);
1892 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001893 }
1894 }
1895
Oliver Neukum54405cd2011-01-06 21:55:13 +01001896 rc = 0;
1897out:
1898 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
1901static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001902 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 struct rtl8169_private *tp = netdev_priv(dev);
1905 int ret;
1906
Oliver Neukum54405cd2011-01-06 21:55:13 +01001907 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001908 if (ret < 0)
1909 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Francois Romieu4876cc12011-03-11 21:07:11 +01001911 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
Chun-Hao Linc4556972016-03-11 14:21:14 +08001912 (advertising & ADVERTISED_1000baseT_Full) &&
1913 !pci_is_pcie(tp->pci_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001915 }
1916out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return ret;
1918}
1919
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001920static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1921 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Francois Romieud58d46b2011-05-03 16:38:29 +02001923 struct rtl8169_private *tp = netdev_priv(dev);
1924
Francois Romieu2b7b4312011-04-18 22:53:24 -07001925 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001926 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Francois Romieud58d46b2011-05-03 16:38:29 +02001928 if (dev->mtu > JUMBO_1K &&
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02001929 tp->mac_version > RTL_GIGA_MAC_VER_06)
Francois Romieud58d46b2011-05-03 16:38:29 +02001930 features &= ~NETIF_F_IP_CSUM;
1931
Michał Mirosław350fb322011-04-08 06:35:56 +00001932 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
Heiner Kallweita3984572018-04-28 22:19:15 +02001935static int rtl8169_set_features(struct net_device *dev,
1936 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
1938 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001939 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Heiner Kallweita3984572018-04-28 22:19:15 +02001941 rtl_lock_work(tp);
1942
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001943 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001944 if (features & NETIF_F_RXALL)
1945 rx_config |= (AcceptErr | AcceptRunt);
1946 else
1947 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001949 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001950
hayeswang929a0312014-09-16 11:40:47 +08001951 if (features & NETIF_F_RXCSUM)
1952 tp->cp_cmd |= RxChkSum;
1953 else
1954 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001955
hayeswang929a0312014-09-16 11:40:47 +08001956 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1957 tp->cp_cmd |= RxVlan;
1958 else
1959 tp->cp_cmd &= ~RxVlan;
1960
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001961 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1962 RTL_R16(tp, CPlusCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Francois Romieuda78dbf2012-01-26 14:18:23 +01001964 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 return 0;
1967}
1968
Kirill Smelkov810f4892012-11-10 21:11:02 +04001969static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001971 return (skb_vlan_tag_present(skb)) ?
1972 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
Francois Romieu7a8fc772011-03-01 17:18:33 +01001975static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Francois Romieu7a8fc772011-03-01 17:18:33 +01001979 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001980 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001983static int rtl8169_get_link_ksettings_tbi(struct net_device *dev,
1984 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
1986 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 u32 status;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001988 u32 supported, advertising;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001990 supported =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001992 cmd->base.port = PORT_FIBRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001994 status = RTL_R32(tp, TBICSR);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001995 advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1996 cmd->base.autoneg = !!(status & TBINwEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001998 cmd->base.speed = SPEED_1000;
1999 cmd->base.duplex = DUPLEX_FULL; /* Always set */
2000
2001 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2002 supported);
2003 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2004 advertising);
Francois Romieuccdffb92008-07-26 14:26:06 +02002005
2006 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002009static int rtl8169_get_link_ksettings_xmii(struct net_device *dev,
2010 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
2012 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03002014 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
2015
2016 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002019static int rtl8169_get_link_ksettings(struct net_device *dev,
2020 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
2022 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02002023 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Francois Romieuda78dbf2012-01-26 14:18:23 +01002025 rtl_lock_work(tp);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002026 rc = tp->get_link_ksettings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002027 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Francois Romieuccdffb92008-07-26 14:26:06 +02002029 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002032static int rtl8169_set_link_ksettings(struct net_device *dev,
2033 const struct ethtool_link_ksettings *cmd)
2034{
2035 struct rtl8169_private *tp = netdev_priv(dev);
2036 int rc;
2037 u32 advertising;
2038
2039 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
2040 cmd->link_modes.advertising))
2041 return -EINVAL;
2042
2043 del_timer_sync(&tp->timer);
2044
2045 rtl_lock_work(tp);
2046 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
2047 cmd->base.duplex, advertising);
2048 rtl_unlock_work(tp);
2049
2050 return rc;
2051}
2052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2054 void *p)
2055{
Francois Romieu5b0384f2006-08-16 16:00:01 +02002056 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02002057 u32 __iomem *data = tp->mmio_addr;
2058 u32 *dw = p;
2059 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Francois Romieuda78dbf2012-01-26 14:18:23 +01002061 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02002062 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2063 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002064 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065}
2066
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002067static u32 rtl8169_get_msglevel(struct net_device *dev)
2068{
2069 struct rtl8169_private *tp = netdev_priv(dev);
2070
2071 return tp->msg_enable;
2072}
2073
2074static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2075{
2076 struct rtl8169_private *tp = netdev_priv(dev);
2077
2078 tp->msg_enable = value;
2079}
2080
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002081static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2082 "tx_packets",
2083 "rx_packets",
2084 "tx_errors",
2085 "rx_errors",
2086 "rx_missed",
2087 "align_errors",
2088 "tx_single_collisions",
2089 "tx_multi_collisions",
2090 "unicast",
2091 "broadcast",
2092 "multicast",
2093 "tx_aborted",
2094 "tx_underrun",
2095};
2096
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002097static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002098{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002099 switch (sset) {
2100 case ETH_SS_STATS:
2101 return ARRAY_SIZE(rtl8169_gstrings);
2102 default:
2103 return -EOPNOTSUPP;
2104 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002105}
2106
Corinna Vinschen42020322015-09-10 10:47:35 +02002107DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002108{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002109 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002110}
2111
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002112static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002113{
Corinna Vinschen42020322015-09-10 10:47:35 +02002114 dma_addr_t paddr = tp->counters_phys_addr;
2115 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02002116
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002117 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
2118 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02002119 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002120 RTL_W32(tp, CounterAddrLow, cmd);
2121 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02002122
Francois Romieua78e9362018-01-26 01:53:26 +01002123 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002124}
2125
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002126static bool rtl8169_reset_counters(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002127{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002128 /*
2129 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2130 * tally counters.
2131 */
2132 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2133 return true;
2134
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002135 return rtl8169_do_counters(tp, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02002136}
2137
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002138static bool rtl8169_update_counters(struct rtl8169_private *tp)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002139{
Ivan Vecera355423d2009-02-06 21:49:57 -08002140 /*
2141 * Some chips are unable to dump tally counters when the receiver
2142 * is disabled.
2143 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002144 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002145 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002146
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002147 return rtl8169_do_counters(tp, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002148}
2149
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002150static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002151{
Corinna Vinschen42020322015-09-10 10:47:35 +02002152 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002153 bool ret = false;
2154
2155 /*
2156 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2157 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2158 * reset by a power cycle, while the counter values collected by the
2159 * driver are reset at every driver unload/load cycle.
2160 *
2161 * To make sure the HW values returned by @get_stats64 match the SW
2162 * values, we collect the initial values at first open(*) and use them
2163 * as offsets to normalize the values returned by @get_stats64.
2164 *
2165 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2166 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2167 * set at open time by rtl_hw_start.
2168 */
2169
2170 if (tp->tc_offset.inited)
2171 return true;
2172
2173 /* If both, reset and update fail, propagate to caller. */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002174 if (rtl8169_reset_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002175 ret = true;
2176
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002177 if (rtl8169_update_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002178 ret = true;
2179
Corinna Vinschen42020322015-09-10 10:47:35 +02002180 tp->tc_offset.tx_errors = counters->tx_errors;
2181 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2182 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002183 tp->tc_offset.inited = true;
2184
2185 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002186}
2187
Ivan Vecera355423d2009-02-06 21:49:57 -08002188static void rtl8169_get_ethtool_stats(struct net_device *dev,
2189 struct ethtool_stats *stats, u64 *data)
2190{
2191 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01002192 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02002193 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08002194
2195 ASSERT_RTNL();
2196
Chun-Hao Line0636232016-07-29 16:37:55 +08002197 pm_runtime_get_noresume(d);
2198
2199 if (pm_runtime_active(d))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002200 rtl8169_update_counters(tp);
Chun-Hao Line0636232016-07-29 16:37:55 +08002201
2202 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08002203
Corinna Vinschen42020322015-09-10 10:47:35 +02002204 data[0] = le64_to_cpu(counters->tx_packets);
2205 data[1] = le64_to_cpu(counters->rx_packets);
2206 data[2] = le64_to_cpu(counters->tx_errors);
2207 data[3] = le32_to_cpu(counters->rx_errors);
2208 data[4] = le16_to_cpu(counters->rx_missed);
2209 data[5] = le16_to_cpu(counters->align_errors);
2210 data[6] = le32_to_cpu(counters->tx_one_collision);
2211 data[7] = le32_to_cpu(counters->tx_multi_collision);
2212 data[8] = le64_to_cpu(counters->rx_unicast);
2213 data[9] = le64_to_cpu(counters->rx_broadcast);
2214 data[10] = le32_to_cpu(counters->rx_multicast);
2215 data[11] = le16_to_cpu(counters->tx_aborted);
2216 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08002217}
2218
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002219static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2220{
2221 switch(stringset) {
2222 case ETH_SS_STATS:
2223 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2224 break;
2225 }
2226}
2227
Florian Fainellif0903ea2016-12-03 12:01:19 -08002228static int rtl8169_nway_reset(struct net_device *dev)
2229{
2230 struct rtl8169_private *tp = netdev_priv(dev);
2231
2232 return mii_nway_restart(&tp->mii);
2233}
2234
Francois Romieu50970832017-10-27 13:24:49 +03002235/*
2236 * Interrupt coalescing
2237 *
2238 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2239 * > 8169, 8168 and 810x line of chipsets
2240 *
2241 * 8169, 8168, and 8136(810x) serial chipsets support it.
2242 *
2243 * > 2 - the Tx timer unit at gigabit speed
2244 *
2245 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2246 * (0xe0) bit 1 and bit 0.
2247 *
2248 * For 8169
2249 * bit[1:0] \ speed 1000M 100M 10M
2250 * 0 0 320ns 2.56us 40.96us
2251 * 0 1 2.56us 20.48us 327.7us
2252 * 1 0 5.12us 40.96us 655.4us
2253 * 1 1 10.24us 81.92us 1.31ms
2254 *
2255 * For the other
2256 * bit[1:0] \ speed 1000M 100M 10M
2257 * 0 0 5us 2.56us 40.96us
2258 * 0 1 40us 20.48us 327.7us
2259 * 1 0 80us 40.96us 655.4us
2260 * 1 1 160us 81.92us 1.31ms
2261 */
2262
2263/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2264struct rtl_coalesce_scale {
2265 /* Rx / Tx */
2266 u32 nsecs[2];
2267};
2268
2269/* rx/tx scale factors for all CPlusCmd[0:1] cases */
2270struct rtl_coalesce_info {
2271 u32 speed;
2272 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2273};
2274
2275/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2276#define rxtx_x1822(r, t) { \
2277 {{(r), (t)}}, \
2278 {{(r)*8, (t)*8}}, \
2279 {{(r)*8*2, (t)*8*2}}, \
2280 {{(r)*8*2*2, (t)*8*2*2}}, \
2281}
2282static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2283 /* speed delays: rx00 tx00 */
2284 { SPEED_10, rxtx_x1822(40960, 40960) },
2285 { SPEED_100, rxtx_x1822( 2560, 2560) },
2286 { SPEED_1000, rxtx_x1822( 320, 320) },
2287 { 0 },
2288};
2289
2290static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2291 /* speed delays: rx00 tx00 */
2292 { SPEED_10, rxtx_x1822(40960, 40960) },
2293 { SPEED_100, rxtx_x1822( 2560, 2560) },
2294 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2295 { 0 },
2296};
2297#undef rxtx_x1822
2298
2299/* get rx/tx scale vector corresponding to current speed */
2300static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2301{
2302 struct rtl8169_private *tp = netdev_priv(dev);
2303 struct ethtool_link_ksettings ecmd;
2304 const struct rtl_coalesce_info *ci;
2305 int rc;
2306
2307 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2308 if (rc < 0)
2309 return ERR_PTR(rc);
2310
2311 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2312 if (ecmd.base.speed == ci->speed) {
2313 return ci;
2314 }
2315 }
2316
2317 return ERR_PTR(-ELNRNG);
2318}
2319
2320static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2321{
2322 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002323 const struct rtl_coalesce_info *ci;
2324 const struct rtl_coalesce_scale *scale;
2325 struct {
2326 u32 *max_frames;
2327 u32 *usecs;
2328 } coal_settings [] = {
2329 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2330 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2331 }, *p = coal_settings;
2332 int i;
2333 u16 w;
2334
2335 memset(ec, 0, sizeof(*ec));
2336
2337 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2338 ci = rtl_coalesce_info(dev);
2339 if (IS_ERR(ci))
2340 return PTR_ERR(ci);
2341
Heiner Kallweit0ae09742018-04-28 22:19:26 +02002342 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
Francois Romieu50970832017-10-27 13:24:49 +03002343
2344 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002345 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03002346 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2347 w >>= RTL_COALESCE_SHIFT;
2348 *p->usecs = w & RTL_COALESCE_MASK;
2349 }
2350
2351 for (i = 0; i < 2; i++) {
2352 p = coal_settings + i;
2353 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2354
2355 /*
2356 * ethtool_coalesce says it is illegal to set both usecs and
2357 * max_frames to 0.
2358 */
2359 if (!*p->usecs && !*p->max_frames)
2360 *p->max_frames = 1;
2361 }
2362
2363 return 0;
2364}
2365
2366/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2367static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2368 struct net_device *dev, u32 nsec, u16 *cp01)
2369{
2370 const struct rtl_coalesce_info *ci;
2371 u16 i;
2372
2373 ci = rtl_coalesce_info(dev);
2374 if (IS_ERR(ci))
2375 return ERR_CAST(ci);
2376
2377 for (i = 0; i < 4; i++) {
2378 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2379 ci->scalev[i].nsecs[1]);
2380 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2381 *cp01 = i;
2382 return &ci->scalev[i];
2383 }
2384 }
2385
2386 return ERR_PTR(-EINVAL);
2387}
2388
2389static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2390{
2391 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002392 const struct rtl_coalesce_scale *scale;
2393 struct {
2394 u32 frames;
2395 u32 usecs;
2396 } coal_settings [] = {
2397 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2398 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2399 }, *p = coal_settings;
2400 u16 w = 0, cp01;
2401 int i;
2402
2403 scale = rtl_coalesce_choose_scale(dev,
2404 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2405 if (IS_ERR(scale))
2406 return PTR_ERR(scale);
2407
2408 for (i = 0; i < 2; i++, p++) {
2409 u32 units;
2410
2411 /*
2412 * accept max_frames=1 we returned in rtl_get_coalesce.
2413 * accept it not only when usecs=0 because of e.g. the following scenario:
2414 *
2415 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2416 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2417 * - then user does `ethtool -C eth0 rx-usecs 100`
2418 *
2419 * since ethtool sends to kernel whole ethtool_coalesce
2420 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2421 * we'll reject it below in `frames % 4 != 0`.
2422 */
2423 if (p->frames == 1) {
2424 p->frames = 0;
2425 }
2426
2427 units = p->usecs * 1000 / scale->nsecs[i];
2428 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2429 return -EINVAL;
2430
2431 w <<= RTL_COALESCE_SHIFT;
2432 w |= units;
2433 w <<= RTL_COALESCE_SHIFT;
2434 w |= p->frames >> 2;
2435 }
2436
2437 rtl_lock_work(tp);
2438
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002439 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03002440
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +02002441 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002442 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2443 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03002444
2445 rtl_unlock_work(tp);
2446
2447 return 0;
2448}
2449
Jeff Garzik7282d492006-09-13 14:30:00 -04002450static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 .get_drvinfo = rtl8169_get_drvinfo,
2452 .get_regs_len = rtl8169_get_regs_len,
2453 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002454 .get_coalesce = rtl_get_coalesce,
2455 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002456 .get_msglevel = rtl8169_get_msglevel,
2457 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002459 .get_wol = rtl8169_get_wol,
2460 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002461 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002462 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002463 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002464 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainellif0903ea2016-12-03 12:01:19 -08002465 .nway_reset = rtl8169_nway_reset,
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002466 .get_link_ksettings = rtl8169_get_link_ksettings,
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002467 .set_link_ksettings = rtl8169_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468};
2469
Francois Romieu07d3f512007-02-21 22:40:46 +01002470static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Heiner Kallweit22148df2018-04-22 17:15:15 +02002471 u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
Francois Romieu0e485152007-02-20 00:00:26 +01002473 /*
2474 * The driver currently handles the 8168Bf and the 8168Be identically
2475 * but they can be identified more specifically through the test below
2476 * if needed:
2477 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002478 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002479 *
2480 * Same thing for the 8101Eb and the 8101Ec:
2481 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002482 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002483 */
Francois Romieu37441002011-06-17 22:58:54 +02002484 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002486 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 int mac_version;
2488 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002489 /* 8168EP family. */
2490 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2491 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2492 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2493
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002494 /* 8168H family. */
2495 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2496 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2497
Hayes Wangc5583862012-07-02 17:23:22 +08002498 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002499 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002500 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002501 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2502 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2503
Hayes Wangc2218922011-09-06 16:55:18 +08002504 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002505 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002506 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2507 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2508
hayeswang01dc7fe2011-03-21 01:50:28 +00002509 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002510 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002511 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2512 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2513
Francois Romieu5b538df2008-07-20 16:22:45 +02002514 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002515 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002516 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002517
françois romieue6de30d2011-01-03 15:08:37 +00002518 /* 8168DP family. */
2519 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2520 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002521 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002522
Francois Romieuef808d52008-06-29 13:10:54 +02002523 /* 8168C family. */
Francois Romieuef3386f2008-06-29 12:24:30 +02002524 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002525 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002526 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002527 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2528 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002529 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieuef808d52008-06-29 13:10:54 +02002530 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002531
2532 /* 8168B family. */
2533 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002534 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2535 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2536
2537 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002538 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002539 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002540 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2541 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002542 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2543 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2544 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2545 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002546 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002547 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002548 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002549 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2550 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002551 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2552 /* FIXME: where did these entries come from ? -- FR */
2553 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2554 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2555
2556 /* 8110 family. */
2557 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2558 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2559 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2560 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2561 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2562 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2563
Jean Delvaref21b75e2009-05-26 20:54:48 -07002564 /* Catch-all */
2565 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002566 };
2567 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 u32 reg;
2569
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002570 reg = RTL_R32(tp, TxConfig);
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002571 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 p++;
2573 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002574
2575 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02002576 dev_notice(tp_to_dev(tp),
2577 "unknown MAC, using family default\n");
Francois Romieu5d320a22011-05-08 17:47:36 +02002578 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002579 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2580 tp->mac_version = tp->mii.supports_gmii ?
2581 RTL_GIGA_MAC_VER_42 :
2582 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002583 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2584 tp->mac_version = tp->mii.supports_gmii ?
2585 RTL_GIGA_MAC_VER_45 :
2586 RTL_GIGA_MAC_VER_47;
2587 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2588 tp->mac_version = tp->mii.supports_gmii ?
2589 RTL_GIGA_MAC_VER_46 :
2590 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
2593
2594static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2595{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002596 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597}
2598
Francois Romieu867763c2007-08-17 18:21:58 +02002599struct phy_reg {
2600 u16 reg;
2601 u16 val;
2602};
2603
françois romieu4da19632011-01-03 15:07:55 +00002604static void rtl_writephy_batch(struct rtl8169_private *tp,
2605 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002606{
2607 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002608 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002609 regs++;
2610 }
2611}
2612
françois romieubca03d52011-01-03 15:07:31 +00002613#define PHY_READ 0x00000000
2614#define PHY_DATA_OR 0x10000000
2615#define PHY_DATA_AND 0x20000000
2616#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002617#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002618#define PHY_CLEAR_READCOUNT 0x70000000
2619#define PHY_WRITE 0x80000000
2620#define PHY_READCOUNT_EQ_SKIP 0x90000000
2621#define PHY_COMP_EQ_SKIPN 0xa0000000
2622#define PHY_COMP_NEQ_SKIPN 0xb0000000
2623#define PHY_WRITE_PREVIOUS 0xc0000000
2624#define PHY_SKIPN 0xd0000000
2625#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002626
Hayes Wang960aee62011-06-18 11:37:48 +02002627struct fw_info {
2628 u32 magic;
2629 char version[RTL_VER_SIZE];
2630 __le32 fw_start;
2631 __le32 fw_len;
2632 u8 chksum;
2633} __packed;
2634
Francois Romieu1c361ef2011-06-17 17:16:24 +02002635#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2636
2637static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002638{
Francois Romieub6ffd972011-06-17 17:00:05 +02002639 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002640 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002641 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2642 char *version = rtl_fw->version;
2643 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002644
Francois Romieu1c361ef2011-06-17 17:16:24 +02002645 if (fw->size < FW_OPCODE_SIZE)
2646 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002647
2648 if (!fw_info->magic) {
2649 size_t i, size, start;
2650 u8 checksum = 0;
2651
2652 if (fw->size < sizeof(*fw_info))
2653 goto out;
2654
2655 for (i = 0; i < fw->size; i++)
2656 checksum += fw->data[i];
2657 if (checksum != 0)
2658 goto out;
2659
2660 start = le32_to_cpu(fw_info->fw_start);
2661 if (start > fw->size)
2662 goto out;
2663
2664 size = le32_to_cpu(fw_info->fw_len);
2665 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2666 goto out;
2667
2668 memcpy(version, fw_info->version, RTL_VER_SIZE);
2669
2670 pa->code = (__le32 *)(fw->data + start);
2671 pa->size = size;
2672 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002673 if (fw->size % FW_OPCODE_SIZE)
2674 goto out;
2675
2676 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2677
2678 pa->code = (__le32 *)fw->data;
2679 pa->size = fw->size / FW_OPCODE_SIZE;
2680 }
2681 version[RTL_VER_SIZE - 1] = 0;
2682
2683 rc = true;
2684out:
2685 return rc;
2686}
2687
Francois Romieufd112f22011-06-18 00:10:29 +02002688static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2689 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002690{
Francois Romieufd112f22011-06-18 00:10:29 +02002691 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002692 size_t index;
2693
Francois Romieu1c361ef2011-06-17 17:16:24 +02002694 for (index = 0; index < pa->size; index++) {
2695 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002696 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002697
hayeswang42b82dc2011-01-10 02:07:25 +00002698 switch(action & 0xf0000000) {
2699 case PHY_READ:
2700 case PHY_DATA_OR:
2701 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002702 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002703 case PHY_CLEAR_READCOUNT:
2704 case PHY_WRITE:
2705 case PHY_WRITE_PREVIOUS:
2706 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002707 break;
2708
hayeswang42b82dc2011-01-10 02:07:25 +00002709 case PHY_BJMPN:
2710 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002711 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002712 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002713 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002714 }
2715 break;
2716 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002717 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002718 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002719 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002720 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002721 }
2722 break;
2723 case PHY_COMP_EQ_SKIPN:
2724 case PHY_COMP_NEQ_SKIPN:
2725 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002726 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002727 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002728 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002729 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002730 }
2731 break;
2732
hayeswang42b82dc2011-01-10 02:07:25 +00002733 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002734 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002735 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002736 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002737 }
2738 }
Francois Romieufd112f22011-06-18 00:10:29 +02002739 rc = true;
2740out:
2741 return rc;
2742}
françois romieubca03d52011-01-03 15:07:31 +00002743
Francois Romieufd112f22011-06-18 00:10:29 +02002744static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2745{
2746 struct net_device *dev = tp->dev;
2747 int rc = -EINVAL;
2748
2749 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002750 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002751 goto out;
2752 }
2753
2754 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2755 rc = 0;
2756out:
2757 return rc;
2758}
2759
2760static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2761{
2762 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002763 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002764 u32 predata, count;
2765 size_t index;
2766
2767 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002768 org.write = ops->write;
2769 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002770
Francois Romieu1c361ef2011-06-17 17:16:24 +02002771 for (index = 0; index < pa->size; ) {
2772 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002773 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002774 u32 regno = (action & 0x0fff0000) >> 16;
2775
2776 if (!action)
2777 break;
françois romieubca03d52011-01-03 15:07:31 +00002778
2779 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002780 case PHY_READ:
2781 predata = rtl_readphy(tp, regno);
2782 count++;
2783 index++;
françois romieubca03d52011-01-03 15:07:31 +00002784 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002785 case PHY_DATA_OR:
2786 predata |= data;
2787 index++;
2788 break;
2789 case PHY_DATA_AND:
2790 predata &= data;
2791 index++;
2792 break;
2793 case PHY_BJMPN:
2794 index -= regno;
2795 break;
hayeswangeee37862013-04-01 22:23:38 +00002796 case PHY_MDIO_CHG:
2797 if (data == 0) {
2798 ops->write = org.write;
2799 ops->read = org.read;
2800 } else if (data == 1) {
2801 ops->write = mac_mcu_write;
2802 ops->read = mac_mcu_read;
2803 }
2804
hayeswang42b82dc2011-01-10 02:07:25 +00002805 index++;
2806 break;
2807 case PHY_CLEAR_READCOUNT:
2808 count = 0;
2809 index++;
2810 break;
2811 case PHY_WRITE:
2812 rtl_writephy(tp, regno, data);
2813 index++;
2814 break;
2815 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002816 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002817 break;
2818 case PHY_COMP_EQ_SKIPN:
2819 if (predata == data)
2820 index += regno;
2821 index++;
2822 break;
2823 case PHY_COMP_NEQ_SKIPN:
2824 if (predata != data)
2825 index += regno;
2826 index++;
2827 break;
2828 case PHY_WRITE_PREVIOUS:
2829 rtl_writephy(tp, regno, predata);
2830 index++;
2831 break;
2832 case PHY_SKIPN:
2833 index += regno + 1;
2834 break;
2835 case PHY_DELAY_MS:
2836 mdelay(data);
2837 index++;
2838 break;
2839
françois romieubca03d52011-01-03 15:07:31 +00002840 default:
2841 BUG();
2842 }
2843 }
hayeswangeee37862013-04-01 22:23:38 +00002844
2845 ops->write = org.write;
2846 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002847}
2848
françois romieuf1e02ed2011-01-13 13:07:53 +00002849static void rtl_release_firmware(struct rtl8169_private *tp)
2850{
Francois Romieub6ffd972011-06-17 17:00:05 +02002851 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2852 release_firmware(tp->rtl_fw->fw);
2853 kfree(tp->rtl_fw);
2854 }
2855 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002856}
2857
François Romieu953a12c2011-04-24 17:38:48 +02002858static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002859{
Francois Romieub6ffd972011-06-17 17:00:05 +02002860 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002861
2862 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002863 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002864 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002865}
2866
2867static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2868{
2869 if (rtl_readphy(tp, reg) != val)
2870 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2871 else
2872 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002873}
2874
françois romieu4da19632011-01-03 15:07:55 +00002875static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002877 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002878 { 0x1f, 0x0001 },
2879 { 0x06, 0x006e },
2880 { 0x08, 0x0708 },
2881 { 0x15, 0x4000 },
2882 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
françois romieu0b9b5712009-08-10 19:44:56 +00002884 { 0x1f, 0x0001 },
2885 { 0x03, 0x00a1 },
2886 { 0x02, 0x0008 },
2887 { 0x01, 0x0120 },
2888 { 0x00, 0x1000 },
2889 { 0x04, 0x0800 },
2890 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
françois romieu0b9b5712009-08-10 19:44:56 +00002892 { 0x03, 0xff41 },
2893 { 0x02, 0xdf60 },
2894 { 0x01, 0x0140 },
2895 { 0x00, 0x0077 },
2896 { 0x04, 0x7800 },
2897 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
françois romieu0b9b5712009-08-10 19:44:56 +00002899 { 0x03, 0x802f },
2900 { 0x02, 0x4f02 },
2901 { 0x01, 0x0409 },
2902 { 0x00, 0xf0f9 },
2903 { 0x04, 0x9800 },
2904 { 0x04, 0x9000 },
2905
2906 { 0x03, 0xdf01 },
2907 { 0x02, 0xdf20 },
2908 { 0x01, 0xff95 },
2909 { 0x00, 0xba00 },
2910 { 0x04, 0xa800 },
2911 { 0x04, 0xa000 },
2912
2913 { 0x03, 0xff41 },
2914 { 0x02, 0xdf20 },
2915 { 0x01, 0x0140 },
2916 { 0x00, 0x00bb },
2917 { 0x04, 0xb800 },
2918 { 0x04, 0xb000 },
2919
2920 { 0x03, 0xdf41 },
2921 { 0x02, 0xdc60 },
2922 { 0x01, 0x6340 },
2923 { 0x00, 0x007d },
2924 { 0x04, 0xd800 },
2925 { 0x04, 0xd000 },
2926
2927 { 0x03, 0xdf01 },
2928 { 0x02, 0xdf20 },
2929 { 0x01, 0x100a },
2930 { 0x00, 0xa0ff },
2931 { 0x04, 0xf800 },
2932 { 0x04, 0xf000 },
2933
2934 { 0x1f, 0x0000 },
2935 { 0x0b, 0x0000 },
2936 { 0x00, 0x9200 }
2937 };
2938
françois romieu4da19632011-01-03 15:07:55 +00002939 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940}
2941
françois romieu4da19632011-01-03 15:07:55 +00002942static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002943{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002944 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002945 { 0x1f, 0x0002 },
2946 { 0x01, 0x90d0 },
2947 { 0x1f, 0x0000 }
2948 };
2949
françois romieu4da19632011-01-03 15:07:55 +00002950 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002951}
2952
françois romieu4da19632011-01-03 15:07:55 +00002953static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002954{
2955 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002956
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002957 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2958 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002959 return;
2960
françois romieu4da19632011-01-03 15:07:55 +00002961 rtl_writephy(tp, 0x1f, 0x0001);
2962 rtl_writephy(tp, 0x10, 0xf01b);
2963 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002964}
2965
françois romieu4da19632011-01-03 15:07:55 +00002966static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002967{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002968 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002969 { 0x1f, 0x0001 },
2970 { 0x04, 0x0000 },
2971 { 0x03, 0x00a1 },
2972 { 0x02, 0x0008 },
2973 { 0x01, 0x0120 },
2974 { 0x00, 0x1000 },
2975 { 0x04, 0x0800 },
2976 { 0x04, 0x9000 },
2977 { 0x03, 0x802f },
2978 { 0x02, 0x4f02 },
2979 { 0x01, 0x0409 },
2980 { 0x00, 0xf099 },
2981 { 0x04, 0x9800 },
2982 { 0x04, 0xa000 },
2983 { 0x03, 0xdf01 },
2984 { 0x02, 0xdf20 },
2985 { 0x01, 0xff95 },
2986 { 0x00, 0xba00 },
2987 { 0x04, 0xa800 },
2988 { 0x04, 0xf000 },
2989 { 0x03, 0xdf01 },
2990 { 0x02, 0xdf20 },
2991 { 0x01, 0x101a },
2992 { 0x00, 0xa0ff },
2993 { 0x04, 0xf800 },
2994 { 0x04, 0x0000 },
2995 { 0x1f, 0x0000 },
2996
2997 { 0x1f, 0x0001 },
2998 { 0x10, 0xf41b },
2999 { 0x14, 0xfb54 },
3000 { 0x18, 0xf5c7 },
3001 { 0x1f, 0x0000 },
3002
3003 { 0x1f, 0x0001 },
3004 { 0x17, 0x0cc0 },
3005 { 0x1f, 0x0000 }
3006 };
3007
françois romieu4da19632011-01-03 15:07:55 +00003008 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00003009
françois romieu4da19632011-01-03 15:07:55 +00003010 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003011}
3012
françois romieu4da19632011-01-03 15:07:55 +00003013static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00003014{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003015 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00003016 { 0x1f, 0x0001 },
3017 { 0x04, 0x0000 },
3018 { 0x03, 0x00a1 },
3019 { 0x02, 0x0008 },
3020 { 0x01, 0x0120 },
3021 { 0x00, 0x1000 },
3022 { 0x04, 0x0800 },
3023 { 0x04, 0x9000 },
3024 { 0x03, 0x802f },
3025 { 0x02, 0x4f02 },
3026 { 0x01, 0x0409 },
3027 { 0x00, 0xf099 },
3028 { 0x04, 0x9800 },
3029 { 0x04, 0xa000 },
3030 { 0x03, 0xdf01 },
3031 { 0x02, 0xdf20 },
3032 { 0x01, 0xff95 },
3033 { 0x00, 0xba00 },
3034 { 0x04, 0xa800 },
3035 { 0x04, 0xf000 },
3036 { 0x03, 0xdf01 },
3037 { 0x02, 0xdf20 },
3038 { 0x01, 0x101a },
3039 { 0x00, 0xa0ff },
3040 { 0x04, 0xf800 },
3041 { 0x04, 0x0000 },
3042 { 0x1f, 0x0000 },
3043
3044 { 0x1f, 0x0001 },
3045 { 0x0b, 0x8480 },
3046 { 0x1f, 0x0000 },
3047
3048 { 0x1f, 0x0001 },
3049 { 0x18, 0x67c7 },
3050 { 0x04, 0x2000 },
3051 { 0x03, 0x002f },
3052 { 0x02, 0x4360 },
3053 { 0x01, 0x0109 },
3054 { 0x00, 0x3022 },
3055 { 0x04, 0x2800 },
3056 { 0x1f, 0x0000 },
3057
3058 { 0x1f, 0x0001 },
3059 { 0x17, 0x0cc0 },
3060 { 0x1f, 0x0000 }
3061 };
3062
françois romieu4da19632011-01-03 15:07:55 +00003063 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00003064}
3065
françois romieu4da19632011-01-03 15:07:55 +00003066static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003067{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003068 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003069 { 0x10, 0xf41b },
3070 { 0x1f, 0x0000 }
3071 };
3072
françois romieu4da19632011-01-03 15:07:55 +00003073 rtl_writephy(tp, 0x1f, 0x0001);
3074 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02003075
françois romieu4da19632011-01-03 15:07:55 +00003076 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003077}
3078
françois romieu4da19632011-01-03 15:07:55 +00003079static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003080{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003081 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003082 { 0x1f, 0x0001 },
3083 { 0x10, 0xf41b },
3084 { 0x1f, 0x0000 }
3085 };
3086
françois romieu4da19632011-01-03 15:07:55 +00003087 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003088}
3089
françois romieu4da19632011-01-03 15:07:55 +00003090static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003091{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003092 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02003093 { 0x1f, 0x0000 },
3094 { 0x1d, 0x0f00 },
3095 { 0x1f, 0x0002 },
3096 { 0x0c, 0x1ec8 },
3097 { 0x1f, 0x0000 }
3098 };
3099
françois romieu4da19632011-01-03 15:07:55 +00003100 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02003101}
3102
françois romieu4da19632011-01-03 15:07:55 +00003103static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02003104{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003105 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02003106 { 0x1f, 0x0001 },
3107 { 0x1d, 0x3d98 },
3108 { 0x1f, 0x0000 }
3109 };
3110
françois romieu4da19632011-01-03 15:07:55 +00003111 rtl_writephy(tp, 0x1f, 0x0000);
3112 rtl_patchphy(tp, 0x14, 1 << 5);
3113 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02003114
françois romieu4da19632011-01-03 15:07:55 +00003115 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02003116}
3117
françois romieu4da19632011-01-03 15:07:55 +00003118static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003119{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003120 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02003121 { 0x1f, 0x0001 },
3122 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02003123 { 0x1f, 0x0002 },
3124 { 0x00, 0x88d4 },
3125 { 0x01, 0x82b1 },
3126 { 0x03, 0x7002 },
3127 { 0x08, 0x9e30 },
3128 { 0x09, 0x01f0 },
3129 { 0x0a, 0x5500 },
3130 { 0x0c, 0x00c8 },
3131 { 0x1f, 0x0003 },
3132 { 0x12, 0xc096 },
3133 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02003134 { 0x1f, 0x0000 },
3135 { 0x1f, 0x0000 },
3136 { 0x09, 0x2000 },
3137 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02003138 };
3139
françois romieu4da19632011-01-03 15:07:55 +00003140 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003141
françois romieu4da19632011-01-03 15:07:55 +00003142 rtl_patchphy(tp, 0x14, 1 << 5);
3143 rtl_patchphy(tp, 0x0d, 1 << 5);
3144 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02003145}
3146
françois romieu4da19632011-01-03 15:07:55 +00003147static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02003148{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003149 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02003150 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003151 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003152 { 0x03, 0x802f },
3153 { 0x02, 0x4f02 },
3154 { 0x01, 0x0409 },
3155 { 0x00, 0xf099 },
3156 { 0x04, 0x9800 },
3157 { 0x04, 0x9000 },
3158 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003159 { 0x1f, 0x0002 },
3160 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003161 { 0x06, 0x0761 },
3162 { 0x1f, 0x0003 },
3163 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003164 { 0x1f, 0x0000 }
3165 };
3166
françois romieu4da19632011-01-03 15:07:55 +00003167 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003168
françois romieu4da19632011-01-03 15:07:55 +00003169 rtl_patchphy(tp, 0x16, 1 << 0);
3170 rtl_patchphy(tp, 0x14, 1 << 5);
3171 rtl_patchphy(tp, 0x0d, 1 << 5);
3172 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003173}
3174
françois romieu4da19632011-01-03 15:07:55 +00003175static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02003176{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003177 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02003178 { 0x1f, 0x0001 },
3179 { 0x12, 0x2300 },
3180 { 0x1d, 0x3d98 },
3181 { 0x1f, 0x0002 },
3182 { 0x0c, 0x7eb8 },
3183 { 0x06, 0x5461 },
3184 { 0x1f, 0x0003 },
3185 { 0x16, 0x0f0a },
3186 { 0x1f, 0x0000 }
3187 };
3188
françois romieu4da19632011-01-03 15:07:55 +00003189 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02003190
françois romieu4da19632011-01-03 15:07:55 +00003191 rtl_patchphy(tp, 0x16, 1 << 0);
3192 rtl_patchphy(tp, 0x14, 1 << 5);
3193 rtl_patchphy(tp, 0x0d, 1 << 5);
3194 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003195}
3196
françois romieu4da19632011-01-03 15:07:55 +00003197static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003198{
françois romieu4da19632011-01-03 15:07:55 +00003199 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003200}
3201
françois romieubca03d52011-01-03 15:07:31 +00003202static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003203{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003204 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003205 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003206 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003207 { 0x06, 0x4064 },
3208 { 0x07, 0x2863 },
3209 { 0x08, 0x059c },
3210 { 0x09, 0x26b4 },
3211 { 0x0a, 0x6a19 },
3212 { 0x0b, 0xdcc8 },
3213 { 0x10, 0xf06d },
3214 { 0x14, 0x7f68 },
3215 { 0x18, 0x7fd9 },
3216 { 0x1c, 0xf0ff },
3217 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003218 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003219 { 0x12, 0xf49f },
3220 { 0x13, 0x070b },
3221 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003222 { 0x14, 0x94c0 },
3223
3224 /*
3225 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003226 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003227 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003228 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003229 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003230 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003231 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003232 { 0x06, 0x5561 },
3233
3234 /*
3235 * Can not link to 1Gbps with bad cable
3236 * Decrease SNR threshold form 21.07dB to 19.04dB
3237 */
3238 { 0x1f, 0x0001 },
3239 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003240
3241 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003242 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003243 };
3244
françois romieu4da19632011-01-03 15:07:55 +00003245 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003246
françois romieubca03d52011-01-03 15:07:31 +00003247 /*
3248 * Rx Error Issue
3249 * Fine Tune Switching regulator parameter
3250 */
françois romieu4da19632011-01-03 15:07:55 +00003251 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003252 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3253 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003254
Francois Romieufdf6fc02012-07-06 22:40:38 +02003255 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003256 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003257 { 0x1f, 0x0002 },
3258 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003259 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003260 { 0x05, 0x8330 },
3261 { 0x06, 0x669a },
3262 { 0x1f, 0x0002 }
3263 };
3264 int val;
3265
françois romieu4da19632011-01-03 15:07:55 +00003266 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003267
françois romieu4da19632011-01-03 15:07:55 +00003268 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003269
3270 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003271 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003272 0x0065, 0x0066, 0x0067, 0x0068,
3273 0x0069, 0x006a, 0x006b, 0x006c
3274 };
3275 int i;
3276
françois romieu4da19632011-01-03 15:07:55 +00003277 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003278
3279 val &= 0xff00;
3280 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003281 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003282 }
3283 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003284 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003285 { 0x1f, 0x0002 },
3286 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003287 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003288 { 0x05, 0x8330 },
3289 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003290 };
3291
françois romieu4da19632011-01-03 15:07:55 +00003292 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003293 }
3294
françois romieubca03d52011-01-03 15:07:31 +00003295 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003296 rtl_writephy(tp, 0x1f, 0x0002);
3297 rtl_patchphy(tp, 0x0d, 0x0300);
3298 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003299
françois romieubca03d52011-01-03 15:07:31 +00003300 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003301 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003302 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3303 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003304
françois romieu4da19632011-01-03 15:07:55 +00003305 rtl_writephy(tp, 0x1f, 0x0005);
3306 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003307
3308 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003309
françois romieu4da19632011-01-03 15:07:55 +00003310 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003311}
3312
françois romieubca03d52011-01-03 15:07:31 +00003313static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003314{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003315 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003316 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003317 { 0x1f, 0x0001 },
3318 { 0x06, 0x4064 },
3319 { 0x07, 0x2863 },
3320 { 0x08, 0x059c },
3321 { 0x09, 0x26b4 },
3322 { 0x0a, 0x6a19 },
3323 { 0x0b, 0xdcc8 },
3324 { 0x10, 0xf06d },
3325 { 0x14, 0x7f68 },
3326 { 0x18, 0x7fd9 },
3327 { 0x1c, 0xf0ff },
3328 { 0x1d, 0x3d9c },
3329 { 0x1f, 0x0003 },
3330 { 0x12, 0xf49f },
3331 { 0x13, 0x070b },
3332 { 0x1a, 0x05ad },
3333 { 0x14, 0x94c0 },
3334
françois romieubca03d52011-01-03 15:07:31 +00003335 /*
3336 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003337 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003338 */
françois romieudaf9df62009-10-07 12:44:20 +00003339 { 0x1f, 0x0002 },
3340 { 0x06, 0x5561 },
3341 { 0x1f, 0x0005 },
3342 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003343 { 0x06, 0x5561 },
3344
3345 /*
3346 * Can not link to 1Gbps with bad cable
3347 * Decrease SNR threshold form 21.07dB to 19.04dB
3348 */
3349 { 0x1f, 0x0001 },
3350 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003351
3352 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003353 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003354 };
3355
françois romieu4da19632011-01-03 15:07:55 +00003356 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003357
Francois Romieufdf6fc02012-07-06 22:40:38 +02003358 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003359 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003360 { 0x1f, 0x0002 },
3361 { 0x05, 0x669a },
3362 { 0x1f, 0x0005 },
3363 { 0x05, 0x8330 },
3364 { 0x06, 0x669a },
3365
3366 { 0x1f, 0x0002 }
3367 };
3368 int val;
3369
françois romieu4da19632011-01-03 15:07:55 +00003370 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003371
françois romieu4da19632011-01-03 15:07:55 +00003372 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003373 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003374 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003375 0x0065, 0x0066, 0x0067, 0x0068,
3376 0x0069, 0x006a, 0x006b, 0x006c
3377 };
3378 int i;
3379
françois romieu4da19632011-01-03 15:07:55 +00003380 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003381
3382 val &= 0xff00;
3383 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003384 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003385 }
3386 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003387 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003388 { 0x1f, 0x0002 },
3389 { 0x05, 0x2642 },
3390 { 0x1f, 0x0005 },
3391 { 0x05, 0x8330 },
3392 { 0x06, 0x2642 }
3393 };
3394
françois romieu4da19632011-01-03 15:07:55 +00003395 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003396 }
3397
françois romieubca03d52011-01-03 15:07:31 +00003398 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003399 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003400 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3401 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003402
françois romieubca03d52011-01-03 15:07:31 +00003403 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003404 rtl_writephy(tp, 0x1f, 0x0002);
3405 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003406
françois romieu4da19632011-01-03 15:07:55 +00003407 rtl_writephy(tp, 0x1f, 0x0005);
3408 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003409
3410 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003411
françois romieu4da19632011-01-03 15:07:55 +00003412 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003413}
3414
françois romieu4da19632011-01-03 15:07:55 +00003415static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003416{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003417 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003418 { 0x1f, 0x0002 },
3419 { 0x10, 0x0008 },
3420 { 0x0d, 0x006c },
3421
3422 { 0x1f, 0x0000 },
3423 { 0x0d, 0xf880 },
3424
3425 { 0x1f, 0x0001 },
3426 { 0x17, 0x0cc0 },
3427
3428 { 0x1f, 0x0001 },
3429 { 0x0b, 0xa4d8 },
3430 { 0x09, 0x281c },
3431 { 0x07, 0x2883 },
3432 { 0x0a, 0x6b35 },
3433 { 0x1d, 0x3da4 },
3434 { 0x1c, 0xeffd },
3435 { 0x14, 0x7f52 },
3436 { 0x18, 0x7fc6 },
3437 { 0x08, 0x0601 },
3438 { 0x06, 0x4063 },
3439 { 0x10, 0xf074 },
3440 { 0x1f, 0x0003 },
3441 { 0x13, 0x0789 },
3442 { 0x12, 0xf4bd },
3443 { 0x1a, 0x04fd },
3444 { 0x14, 0x84b0 },
3445 { 0x1f, 0x0000 },
3446 { 0x00, 0x9200 },
3447
3448 { 0x1f, 0x0005 },
3449 { 0x01, 0x0340 },
3450 { 0x1f, 0x0001 },
3451 { 0x04, 0x4000 },
3452 { 0x03, 0x1d21 },
3453 { 0x02, 0x0c32 },
3454 { 0x01, 0x0200 },
3455 { 0x00, 0x5554 },
3456 { 0x04, 0x4800 },
3457 { 0x04, 0x4000 },
3458 { 0x04, 0xf000 },
3459 { 0x03, 0xdf01 },
3460 { 0x02, 0xdf20 },
3461 { 0x01, 0x101a },
3462 { 0x00, 0xa0ff },
3463 { 0x04, 0xf800 },
3464 { 0x04, 0xf000 },
3465 { 0x1f, 0x0000 },
3466
3467 { 0x1f, 0x0007 },
3468 { 0x1e, 0x0023 },
3469 { 0x16, 0x0000 },
3470 { 0x1f, 0x0000 }
3471 };
3472
françois romieu4da19632011-01-03 15:07:55 +00003473 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003474}
3475
françois romieue6de30d2011-01-03 15:08:37 +00003476static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3477{
3478 static const struct phy_reg phy_reg_init[] = {
3479 { 0x1f, 0x0001 },
3480 { 0x17, 0x0cc0 },
3481
3482 { 0x1f, 0x0007 },
3483 { 0x1e, 0x002d },
3484 { 0x18, 0x0040 },
3485 { 0x1f, 0x0000 }
3486 };
3487
3488 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3489 rtl_patchphy(tp, 0x0d, 1 << 5);
3490}
3491
Hayes Wang70090422011-07-06 15:58:06 +08003492static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003493{
3494 static const struct phy_reg phy_reg_init[] = {
3495 /* Enable Delay cap */
3496 { 0x1f, 0x0005 },
3497 { 0x05, 0x8b80 },
3498 { 0x06, 0xc896 },
3499 { 0x1f, 0x0000 },
3500
3501 /* Channel estimation fine tune */
3502 { 0x1f, 0x0001 },
3503 { 0x0b, 0x6c20 },
3504 { 0x07, 0x2872 },
3505 { 0x1c, 0xefff },
3506 { 0x1f, 0x0003 },
3507 { 0x14, 0x6420 },
3508 { 0x1f, 0x0000 },
3509
3510 /* Update PFM & 10M TX idle timer */
3511 { 0x1f, 0x0007 },
3512 { 0x1e, 0x002f },
3513 { 0x15, 0x1919 },
3514 { 0x1f, 0x0000 },
3515
3516 { 0x1f, 0x0007 },
3517 { 0x1e, 0x00ac },
3518 { 0x18, 0x0006 },
3519 { 0x1f, 0x0000 }
3520 };
3521
Francois Romieu15ecd032011-04-27 13:52:22 -07003522 rtl_apply_firmware(tp);
3523
hayeswang01dc7fe2011-03-21 01:50:28 +00003524 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3525
3526 /* DCO enable for 10M IDLE Power */
3527 rtl_writephy(tp, 0x1f, 0x0007);
3528 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003529 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003530 rtl_writephy(tp, 0x1f, 0x0000);
3531
3532 /* For impedance matching */
3533 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003534 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003535 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003536
3537 /* PHY auto speed down */
3538 rtl_writephy(tp, 0x1f, 0x0007);
3539 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003540 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003541 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003542 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003543
3544 rtl_writephy(tp, 0x1f, 0x0005);
3545 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003546 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003547 rtl_writephy(tp, 0x1f, 0x0000);
3548
3549 rtl_writephy(tp, 0x1f, 0x0005);
3550 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003551 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003552 rtl_writephy(tp, 0x1f, 0x0007);
3553 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003554 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003555 rtl_writephy(tp, 0x1f, 0x0006);
3556 rtl_writephy(tp, 0x00, 0x5a00);
3557 rtl_writephy(tp, 0x1f, 0x0000);
3558 rtl_writephy(tp, 0x0d, 0x0007);
3559 rtl_writephy(tp, 0x0e, 0x003c);
3560 rtl_writephy(tp, 0x0d, 0x4007);
3561 rtl_writephy(tp, 0x0e, 0x0000);
3562 rtl_writephy(tp, 0x0d, 0x0000);
3563}
3564
françois romieu9ecb9aa2012-12-07 11:20:21 +00003565static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3566{
3567 const u16 w[] = {
3568 addr[0] | (addr[1] << 8),
3569 addr[2] | (addr[3] << 8),
3570 addr[4] | (addr[5] << 8)
3571 };
3572 const struct exgmac_reg e[] = {
3573 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3574 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3575 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3576 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3577 };
3578
3579 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3580}
3581
Hayes Wang70090422011-07-06 15:58:06 +08003582static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3583{
3584 static const struct phy_reg phy_reg_init[] = {
3585 /* Enable Delay cap */
3586 { 0x1f, 0x0004 },
3587 { 0x1f, 0x0007 },
3588 { 0x1e, 0x00ac },
3589 { 0x18, 0x0006 },
3590 { 0x1f, 0x0002 },
3591 { 0x1f, 0x0000 },
3592 { 0x1f, 0x0000 },
3593
3594 /* Channel estimation fine tune */
3595 { 0x1f, 0x0003 },
3596 { 0x09, 0xa20f },
3597 { 0x1f, 0x0000 },
3598 { 0x1f, 0x0000 },
3599
3600 /* Green Setting */
3601 { 0x1f, 0x0005 },
3602 { 0x05, 0x8b5b },
3603 { 0x06, 0x9222 },
3604 { 0x05, 0x8b6d },
3605 { 0x06, 0x8000 },
3606 { 0x05, 0x8b76 },
3607 { 0x06, 0x8000 },
3608 { 0x1f, 0x0000 }
3609 };
3610
3611 rtl_apply_firmware(tp);
3612
3613 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3614
3615 /* For 4-corner performance improve */
3616 rtl_writephy(tp, 0x1f, 0x0005);
3617 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003618 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003619 rtl_writephy(tp, 0x1f, 0x0000);
3620
3621 /* PHY auto speed down */
3622 rtl_writephy(tp, 0x1f, 0x0004);
3623 rtl_writephy(tp, 0x1f, 0x0007);
3624 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003625 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003626 rtl_writephy(tp, 0x1f, 0x0002);
3627 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003628 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003629
3630 /* improve 10M EEE waveform */
3631 rtl_writephy(tp, 0x1f, 0x0005);
3632 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003633 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003634 rtl_writephy(tp, 0x1f, 0x0000);
3635
3636 /* Improve 2-pair detection performance */
3637 rtl_writephy(tp, 0x1f, 0x0005);
3638 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003639 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003640 rtl_writephy(tp, 0x1f, 0x0000);
3641
3642 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003643 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003644 rtl_writephy(tp, 0x1f, 0x0005);
3645 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003646 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003647 rtl_writephy(tp, 0x1f, 0x0004);
3648 rtl_writephy(tp, 0x1f, 0x0007);
3649 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003650 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003651 rtl_writephy(tp, 0x1f, 0x0002);
3652 rtl_writephy(tp, 0x1f, 0x0000);
3653 rtl_writephy(tp, 0x0d, 0x0007);
3654 rtl_writephy(tp, 0x0e, 0x003c);
3655 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003656 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003657 rtl_writephy(tp, 0x0d, 0x0000);
3658
3659 /* Green feature */
3660 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003661 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3662 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003663 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003664 rtl_writephy(tp, 0x1f, 0x0005);
3665 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3666 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003667
françois romieu9ecb9aa2012-12-07 11:20:21 +00003668 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3669 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003670}
3671
Hayes Wang5f886e02012-03-30 14:33:03 +08003672static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3673{
3674 /* For 4-corner performance improve */
3675 rtl_writephy(tp, 0x1f, 0x0005);
3676 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003677 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003678 rtl_writephy(tp, 0x1f, 0x0000);
3679
3680 /* PHY auto speed down */
3681 rtl_writephy(tp, 0x1f, 0x0007);
3682 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003683 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003684 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003685 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003686
3687 /* Improve 10M EEE waveform */
3688 rtl_writephy(tp, 0x1f, 0x0005);
3689 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003690 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003691 rtl_writephy(tp, 0x1f, 0x0000);
3692}
3693
Hayes Wangc2218922011-09-06 16:55:18 +08003694static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3695{
3696 static const struct phy_reg phy_reg_init[] = {
3697 /* Channel estimation fine tune */
3698 { 0x1f, 0x0003 },
3699 { 0x09, 0xa20f },
3700 { 0x1f, 0x0000 },
3701
3702 /* Modify green table for giga & fnet */
3703 { 0x1f, 0x0005 },
3704 { 0x05, 0x8b55 },
3705 { 0x06, 0x0000 },
3706 { 0x05, 0x8b5e },
3707 { 0x06, 0x0000 },
3708 { 0x05, 0x8b67 },
3709 { 0x06, 0x0000 },
3710 { 0x05, 0x8b70 },
3711 { 0x06, 0x0000 },
3712 { 0x1f, 0x0000 },
3713 { 0x1f, 0x0007 },
3714 { 0x1e, 0x0078 },
3715 { 0x17, 0x0000 },
3716 { 0x19, 0x00fb },
3717 { 0x1f, 0x0000 },
3718
3719 /* Modify green table for 10M */
3720 { 0x1f, 0x0005 },
3721 { 0x05, 0x8b79 },
3722 { 0x06, 0xaa00 },
3723 { 0x1f, 0x0000 },
3724
3725 /* Disable hiimpedance detection (RTCT) */
3726 { 0x1f, 0x0003 },
3727 { 0x01, 0x328a },
3728 { 0x1f, 0x0000 }
3729 };
3730
3731 rtl_apply_firmware(tp);
3732
3733 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3734
Hayes Wang5f886e02012-03-30 14:33:03 +08003735 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003736
3737 /* Improve 2-pair detection performance */
3738 rtl_writephy(tp, 0x1f, 0x0005);
3739 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003740 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003741 rtl_writephy(tp, 0x1f, 0x0000);
3742}
3743
3744static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3745{
3746 rtl_apply_firmware(tp);
3747
Hayes Wang5f886e02012-03-30 14:33:03 +08003748 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003749}
3750
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003751static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3752{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003753 static const struct phy_reg phy_reg_init[] = {
3754 /* Channel estimation fine tune */
3755 { 0x1f, 0x0003 },
3756 { 0x09, 0xa20f },
3757 { 0x1f, 0x0000 },
3758
3759 /* Modify green table for giga & fnet */
3760 { 0x1f, 0x0005 },
3761 { 0x05, 0x8b55 },
3762 { 0x06, 0x0000 },
3763 { 0x05, 0x8b5e },
3764 { 0x06, 0x0000 },
3765 { 0x05, 0x8b67 },
3766 { 0x06, 0x0000 },
3767 { 0x05, 0x8b70 },
3768 { 0x06, 0x0000 },
3769 { 0x1f, 0x0000 },
3770 { 0x1f, 0x0007 },
3771 { 0x1e, 0x0078 },
3772 { 0x17, 0x0000 },
3773 { 0x19, 0x00aa },
3774 { 0x1f, 0x0000 },
3775
3776 /* Modify green table for 10M */
3777 { 0x1f, 0x0005 },
3778 { 0x05, 0x8b79 },
3779 { 0x06, 0xaa00 },
3780 { 0x1f, 0x0000 },
3781
3782 /* Disable hiimpedance detection (RTCT) */
3783 { 0x1f, 0x0003 },
3784 { 0x01, 0x328a },
3785 { 0x1f, 0x0000 }
3786 };
3787
3788
3789 rtl_apply_firmware(tp);
3790
3791 rtl8168f_hw_phy_config(tp);
3792
3793 /* Improve 2-pair detection performance */
3794 rtl_writephy(tp, 0x1f, 0x0005);
3795 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003796 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003797 rtl_writephy(tp, 0x1f, 0x0000);
3798
3799 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3800
3801 /* Modify green table for giga */
3802 rtl_writephy(tp, 0x1f, 0x0005);
3803 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003804 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003805 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003806 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003807 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003808 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003809 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003810 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003811 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003812 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003813 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003814 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003815 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003816 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003817 rtl_writephy(tp, 0x1f, 0x0000);
3818
3819 /* uc same-seed solution */
3820 rtl_writephy(tp, 0x1f, 0x0005);
3821 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003822 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003823 rtl_writephy(tp, 0x1f, 0x0000);
3824
3825 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003826 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003827 rtl_writephy(tp, 0x1f, 0x0005);
3828 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003829 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003830 rtl_writephy(tp, 0x1f, 0x0004);
3831 rtl_writephy(tp, 0x1f, 0x0007);
3832 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003833 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003834 rtl_writephy(tp, 0x1f, 0x0000);
3835 rtl_writephy(tp, 0x0d, 0x0007);
3836 rtl_writephy(tp, 0x0e, 0x003c);
3837 rtl_writephy(tp, 0x0d, 0x4007);
3838 rtl_writephy(tp, 0x0e, 0x0000);
3839 rtl_writephy(tp, 0x0d, 0x0000);
3840
3841 /* Green feature */
3842 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003843 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3844 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003845 rtl_writephy(tp, 0x1f, 0x0000);
3846}
3847
Hayes Wangc5583862012-07-02 17:23:22 +08003848static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3849{
Hayes Wangc5583862012-07-02 17:23:22 +08003850 rtl_apply_firmware(tp);
3851
hayeswang41f44d12013-04-01 22:23:36 +00003852 rtl_writephy(tp, 0x1f, 0x0a46);
3853 if (rtl_readphy(tp, 0x10) & 0x0100) {
3854 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003855 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003856 } else {
3857 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003858 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003859 }
Hayes Wangc5583862012-07-02 17:23:22 +08003860
hayeswang41f44d12013-04-01 22:23:36 +00003861 rtl_writephy(tp, 0x1f, 0x0a46);
3862 if (rtl_readphy(tp, 0x13) & 0x0100) {
3863 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003864 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003865 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003866 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003867 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003868 }
Hayes Wangc5583862012-07-02 17:23:22 +08003869
hayeswang41f44d12013-04-01 22:23:36 +00003870 /* Enable PHY auto speed down */
3871 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003872 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003873
hayeswangfe7524c2013-04-01 22:23:37 +00003874 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003875 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003876 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003877 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003878 rtl_writephy(tp, 0x1f, 0x0a43);
3879 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003880 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3881 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003882
hayeswang41f44d12013-04-01 22:23:36 +00003883 /* EEE auto-fallback function */
3884 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003885 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003886
hayeswang41f44d12013-04-01 22:23:36 +00003887 /* Enable UC LPF tune function */
3888 rtl_writephy(tp, 0x1f, 0x0a43);
3889 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003890 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003891
3892 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003893 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003894
hayeswangfe7524c2013-04-01 22:23:37 +00003895 /* Improve SWR Efficiency */
3896 rtl_writephy(tp, 0x1f, 0x0bcd);
3897 rtl_writephy(tp, 0x14, 0x5065);
3898 rtl_writephy(tp, 0x14, 0xd065);
3899 rtl_writephy(tp, 0x1f, 0x0bc8);
3900 rtl_writephy(tp, 0x11, 0x5655);
3901 rtl_writephy(tp, 0x1f, 0x0bcd);
3902 rtl_writephy(tp, 0x14, 0x1065);
3903 rtl_writephy(tp, 0x14, 0x9065);
3904 rtl_writephy(tp, 0x14, 0x1065);
3905
David Chang1bac1072013-11-27 15:48:36 +08003906 /* Check ALDPS bit, disable it if enabled */
3907 rtl_writephy(tp, 0x1f, 0x0a43);
3908 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003909 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08003910
hayeswang41f44d12013-04-01 22:23:36 +00003911 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003912}
3913
hayeswang57538c42013-04-01 22:23:40 +00003914static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3915{
3916 rtl_apply_firmware(tp);
3917}
3918
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003919static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3920{
3921 u16 dout_tapbin;
3922 u32 data;
3923
3924 rtl_apply_firmware(tp);
3925
3926 /* CHN EST parameters adjust - giga master */
3927 rtl_writephy(tp, 0x1f, 0x0a43);
3928 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003929 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003930 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003931 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003932 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003933 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003934 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003935 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003936 rtl_writephy(tp, 0x1f, 0x0000);
3937
3938 /* CHN EST parameters adjust - giga slave */
3939 rtl_writephy(tp, 0x1f, 0x0a43);
3940 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003941 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003942 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003943 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003944 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003945 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003946 rtl_writephy(tp, 0x1f, 0x0000);
3947
3948 /* CHN EST parameters adjust - fnet */
3949 rtl_writephy(tp, 0x1f, 0x0a43);
3950 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003951 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003952 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003953 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003954 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003955 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003956 rtl_writephy(tp, 0x1f, 0x0000);
3957
3958 /* enable R-tune & PGA-retune function */
3959 dout_tapbin = 0;
3960 rtl_writephy(tp, 0x1f, 0x0a46);
3961 data = rtl_readphy(tp, 0x13);
3962 data &= 3;
3963 data <<= 2;
3964 dout_tapbin |= data;
3965 data = rtl_readphy(tp, 0x12);
3966 data &= 0xc000;
3967 data >>= 14;
3968 dout_tapbin |= data;
3969 dout_tapbin = ~(dout_tapbin^0x08);
3970 dout_tapbin <<= 12;
3971 dout_tapbin &= 0xf000;
3972 rtl_writephy(tp, 0x1f, 0x0a43);
3973 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003974 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003975 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003976 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003977 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003978 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003979 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003980 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003981
3982 rtl_writephy(tp, 0x1f, 0x0a43);
3983 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003984 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003985 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003986 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003987 rtl_writephy(tp, 0x1f, 0x0000);
3988
3989 /* enable GPHY 10M */
3990 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003991 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003992 rtl_writephy(tp, 0x1f, 0x0000);
3993
3994 /* SAR ADC performance */
3995 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003996 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003997 rtl_writephy(tp, 0x1f, 0x0000);
3998
3999 rtl_writephy(tp, 0x1f, 0x0a43);
4000 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004001 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004002 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004003 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004004 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004005 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004006 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004007 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004008 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004009 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004010 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004011 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004012 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004013 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004014 rtl_writephy(tp, 0x1f, 0x0000);
4015
4016 /* disable phy pfm mode */
4017 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004018 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004019 rtl_writephy(tp, 0x1f, 0x0000);
4020
4021 /* Check ALDPS bit, disable it if enabled */
4022 rtl_writephy(tp, 0x1f, 0x0a43);
4023 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004024 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004025
4026 rtl_writephy(tp, 0x1f, 0x0000);
4027}
4028
4029static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
4030{
4031 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
4032 u16 rlen;
4033 u32 data;
4034
4035 rtl_apply_firmware(tp);
4036
4037 /* CHIN EST parameter update */
4038 rtl_writephy(tp, 0x1f, 0x0a43);
4039 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004040 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004041 rtl_writephy(tp, 0x1f, 0x0000);
4042
4043 /* enable R-tune & PGA-retune function */
4044 rtl_writephy(tp, 0x1f, 0x0a43);
4045 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004046 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004047 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004048 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004049 rtl_writephy(tp, 0x1f, 0x0000);
4050
4051 /* enable GPHY 10M */
4052 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004053 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004054 rtl_writephy(tp, 0x1f, 0x0000);
4055
4056 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
4057 data = r8168_mac_ocp_read(tp, 0xdd02);
4058 ioffset_p3 = ((data & 0x80)>>7);
4059 ioffset_p3 <<= 3;
4060
4061 data = r8168_mac_ocp_read(tp, 0xdd00);
4062 ioffset_p3 |= ((data & (0xe000))>>13);
4063 ioffset_p2 = ((data & (0x1e00))>>9);
4064 ioffset_p1 = ((data & (0x01e0))>>5);
4065 ioffset_p0 = ((data & 0x0010)>>4);
4066 ioffset_p0 <<= 3;
4067 ioffset_p0 |= (data & (0x07));
4068 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
4069
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004070 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08004071 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004072 rtl_writephy(tp, 0x1f, 0x0bcf);
4073 rtl_writephy(tp, 0x16, data);
4074 rtl_writephy(tp, 0x1f, 0x0000);
4075 }
4076
4077 /* Modify rlen (TX LPF corner frequency) level */
4078 rtl_writephy(tp, 0x1f, 0x0bcd);
4079 data = rtl_readphy(tp, 0x16);
4080 data &= 0x000f;
4081 rlen = 0;
4082 if (data > 3)
4083 rlen = data - 3;
4084 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
4085 rtl_writephy(tp, 0x17, data);
4086 rtl_writephy(tp, 0x1f, 0x0bcd);
4087 rtl_writephy(tp, 0x1f, 0x0000);
4088
4089 /* disable phy pfm mode */
4090 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004091 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004092 rtl_writephy(tp, 0x1f, 0x0000);
4093
4094 /* Check ALDPS bit, disable it if enabled */
4095 rtl_writephy(tp, 0x1f, 0x0a43);
4096 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004097 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004098
4099 rtl_writephy(tp, 0x1f, 0x0000);
4100}
4101
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004102static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4103{
4104 /* Enable PHY auto speed down */
4105 rtl_writephy(tp, 0x1f, 0x0a44);
4106 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4107 rtl_writephy(tp, 0x1f, 0x0000);
4108
4109 /* patch 10M & ALDPS */
4110 rtl_writephy(tp, 0x1f, 0x0bcc);
4111 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4112 rtl_writephy(tp, 0x1f, 0x0a44);
4113 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4114 rtl_writephy(tp, 0x1f, 0x0a43);
4115 rtl_writephy(tp, 0x13, 0x8084);
4116 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4117 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4118 rtl_writephy(tp, 0x1f, 0x0000);
4119
4120 /* Enable EEE auto-fallback function */
4121 rtl_writephy(tp, 0x1f, 0x0a4b);
4122 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4123 rtl_writephy(tp, 0x1f, 0x0000);
4124
4125 /* Enable UC LPF tune function */
4126 rtl_writephy(tp, 0x1f, 0x0a43);
4127 rtl_writephy(tp, 0x13, 0x8012);
4128 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4129 rtl_writephy(tp, 0x1f, 0x0000);
4130
4131 /* set rg_sel_sdm_rate */
4132 rtl_writephy(tp, 0x1f, 0x0c42);
4133 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4134 rtl_writephy(tp, 0x1f, 0x0000);
4135
4136 /* Check ALDPS bit, disable it if enabled */
4137 rtl_writephy(tp, 0x1f, 0x0a43);
4138 if (rtl_readphy(tp, 0x10) & 0x0004)
4139 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4140
4141 rtl_writephy(tp, 0x1f, 0x0000);
4142}
4143
4144static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4145{
4146 /* patch 10M & ALDPS */
4147 rtl_writephy(tp, 0x1f, 0x0bcc);
4148 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4149 rtl_writephy(tp, 0x1f, 0x0a44);
4150 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4151 rtl_writephy(tp, 0x1f, 0x0a43);
4152 rtl_writephy(tp, 0x13, 0x8084);
4153 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4154 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4155 rtl_writephy(tp, 0x1f, 0x0000);
4156
4157 /* Enable UC LPF tune function */
4158 rtl_writephy(tp, 0x1f, 0x0a43);
4159 rtl_writephy(tp, 0x13, 0x8012);
4160 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4161 rtl_writephy(tp, 0x1f, 0x0000);
4162
4163 /* Set rg_sel_sdm_rate */
4164 rtl_writephy(tp, 0x1f, 0x0c42);
4165 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4166 rtl_writephy(tp, 0x1f, 0x0000);
4167
4168 /* Channel estimation parameters */
4169 rtl_writephy(tp, 0x1f, 0x0a43);
4170 rtl_writephy(tp, 0x13, 0x80f3);
4171 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4172 rtl_writephy(tp, 0x13, 0x80f0);
4173 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4174 rtl_writephy(tp, 0x13, 0x80ef);
4175 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4176 rtl_writephy(tp, 0x13, 0x80f6);
4177 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4178 rtl_writephy(tp, 0x13, 0x80ec);
4179 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4180 rtl_writephy(tp, 0x13, 0x80ed);
4181 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4182 rtl_writephy(tp, 0x13, 0x80f2);
4183 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4184 rtl_writephy(tp, 0x13, 0x80f4);
4185 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4186 rtl_writephy(tp, 0x1f, 0x0a43);
4187 rtl_writephy(tp, 0x13, 0x8110);
4188 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4189 rtl_writephy(tp, 0x13, 0x810f);
4190 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4191 rtl_writephy(tp, 0x13, 0x8111);
4192 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4193 rtl_writephy(tp, 0x13, 0x8113);
4194 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4195 rtl_writephy(tp, 0x13, 0x8115);
4196 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4197 rtl_writephy(tp, 0x13, 0x810e);
4198 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4199 rtl_writephy(tp, 0x13, 0x810c);
4200 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4201 rtl_writephy(tp, 0x13, 0x810b);
4202 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4203 rtl_writephy(tp, 0x1f, 0x0a43);
4204 rtl_writephy(tp, 0x13, 0x80d1);
4205 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4206 rtl_writephy(tp, 0x13, 0x80cd);
4207 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4208 rtl_writephy(tp, 0x13, 0x80d3);
4209 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4210 rtl_writephy(tp, 0x13, 0x80d5);
4211 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4212 rtl_writephy(tp, 0x13, 0x80d7);
4213 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4214
4215 /* Force PWM-mode */
4216 rtl_writephy(tp, 0x1f, 0x0bcd);
4217 rtl_writephy(tp, 0x14, 0x5065);
4218 rtl_writephy(tp, 0x14, 0xd065);
4219 rtl_writephy(tp, 0x1f, 0x0bc8);
4220 rtl_writephy(tp, 0x12, 0x00ed);
4221 rtl_writephy(tp, 0x1f, 0x0bcd);
4222 rtl_writephy(tp, 0x14, 0x1065);
4223 rtl_writephy(tp, 0x14, 0x9065);
4224 rtl_writephy(tp, 0x14, 0x1065);
4225 rtl_writephy(tp, 0x1f, 0x0000);
4226
4227 /* Check ALDPS bit, disable it if enabled */
4228 rtl_writephy(tp, 0x1f, 0x0a43);
4229 if (rtl_readphy(tp, 0x10) & 0x0004)
4230 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4231
4232 rtl_writephy(tp, 0x1f, 0x0000);
4233}
4234
françois romieu4da19632011-01-03 15:07:55 +00004235static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004236{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004237 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004238 { 0x1f, 0x0003 },
4239 { 0x08, 0x441d },
4240 { 0x01, 0x9100 },
4241 { 0x1f, 0x0000 }
4242 };
4243
françois romieu4da19632011-01-03 15:07:55 +00004244 rtl_writephy(tp, 0x1f, 0x0000);
4245 rtl_patchphy(tp, 0x11, 1 << 12);
4246 rtl_patchphy(tp, 0x19, 1 << 13);
4247 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004248
françois romieu4da19632011-01-03 15:07:55 +00004249 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004250}
4251
Hayes Wang5a5e4442011-02-22 17:26:21 +08004252static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4253{
4254 static const struct phy_reg phy_reg_init[] = {
4255 { 0x1f, 0x0005 },
4256 { 0x1a, 0x0000 },
4257 { 0x1f, 0x0000 },
4258
4259 { 0x1f, 0x0004 },
4260 { 0x1c, 0x0000 },
4261 { 0x1f, 0x0000 },
4262
4263 { 0x1f, 0x0001 },
4264 { 0x15, 0x7701 },
4265 { 0x1f, 0x0000 }
4266 };
4267
4268 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004269 rtl_writephy(tp, 0x1f, 0x0000);
4270 rtl_writephy(tp, 0x18, 0x0310);
4271 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004272
François Romieu953a12c2011-04-24 17:38:48 +02004273 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004274
4275 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4276}
4277
Hayes Wang7e18dca2012-03-30 14:33:02 +08004278static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4279{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004280 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004281 rtl_writephy(tp, 0x1f, 0x0000);
4282 rtl_writephy(tp, 0x18, 0x0310);
4283 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004284
4285 rtl_apply_firmware(tp);
4286
4287 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004288 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004289 rtl_writephy(tp, 0x1f, 0x0004);
4290 rtl_writephy(tp, 0x10, 0x401f);
4291 rtl_writephy(tp, 0x19, 0x7030);
4292 rtl_writephy(tp, 0x1f, 0x0000);
4293}
4294
Hayes Wang5598bfe2012-07-02 17:23:21 +08004295static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4296{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004297 static const struct phy_reg phy_reg_init[] = {
4298 { 0x1f, 0x0004 },
4299 { 0x10, 0xc07f },
4300 { 0x19, 0x7030 },
4301 { 0x1f, 0x0000 }
4302 };
4303
4304 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004305 rtl_writephy(tp, 0x1f, 0x0000);
4306 rtl_writephy(tp, 0x18, 0x0310);
4307 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004308
4309 rtl_apply_firmware(tp);
4310
Francois Romieufdf6fc02012-07-06 22:40:38 +02004311 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004312 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4313
Francois Romieufdf6fc02012-07-06 22:40:38 +02004314 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004315}
4316
Francois Romieu5615d9f2007-08-17 17:50:46 +02004317static void rtl_hw_phy_config(struct net_device *dev)
4318{
4319 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004320
4321 rtl8169_print_mac_version(tp);
4322
4323 switch (tp->mac_version) {
4324 case RTL_GIGA_MAC_VER_01:
4325 break;
4326 case RTL_GIGA_MAC_VER_02:
4327 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004328 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004329 break;
4330 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004331 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004332 break;
françois romieu2e9558562009-08-10 19:44:19 +00004333 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004334 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004335 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004336 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004337 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004338 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004339 case RTL_GIGA_MAC_VER_07:
4340 case RTL_GIGA_MAC_VER_08:
4341 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004342 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004343 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004344 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004345 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004346 break;
4347 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004348 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004349 break;
4350 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004351 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004352 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004353 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004354 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004355 break;
4356 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004357 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004358 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004359 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004360 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004361 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004362 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004363 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004364 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004365 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004366 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004367 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004368 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004369 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004370 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004371 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004372 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004373 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004374 break;
4375 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004376 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004377 break;
4378 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004379 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004380 break;
françois romieue6de30d2011-01-03 15:08:37 +00004381 case RTL_GIGA_MAC_VER_28:
4382 rtl8168d_4_hw_phy_config(tp);
4383 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004384 case RTL_GIGA_MAC_VER_29:
4385 case RTL_GIGA_MAC_VER_30:
4386 rtl8105e_hw_phy_config(tp);
4387 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004388 case RTL_GIGA_MAC_VER_31:
4389 /* None. */
4390 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004391 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004392 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004393 rtl8168e_1_hw_phy_config(tp);
4394 break;
4395 case RTL_GIGA_MAC_VER_34:
4396 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004397 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004398 case RTL_GIGA_MAC_VER_35:
4399 rtl8168f_1_hw_phy_config(tp);
4400 break;
4401 case RTL_GIGA_MAC_VER_36:
4402 rtl8168f_2_hw_phy_config(tp);
4403 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004404
Hayes Wang7e18dca2012-03-30 14:33:02 +08004405 case RTL_GIGA_MAC_VER_37:
4406 rtl8402_hw_phy_config(tp);
4407 break;
4408
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004409 case RTL_GIGA_MAC_VER_38:
4410 rtl8411_hw_phy_config(tp);
4411 break;
4412
Hayes Wang5598bfe2012-07-02 17:23:21 +08004413 case RTL_GIGA_MAC_VER_39:
4414 rtl8106e_hw_phy_config(tp);
4415 break;
4416
Hayes Wangc5583862012-07-02 17:23:22 +08004417 case RTL_GIGA_MAC_VER_40:
4418 rtl8168g_1_hw_phy_config(tp);
4419 break;
hayeswang57538c42013-04-01 22:23:40 +00004420 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004421 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004422 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004423 rtl8168g_2_hw_phy_config(tp);
4424 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004425 case RTL_GIGA_MAC_VER_45:
4426 case RTL_GIGA_MAC_VER_47:
4427 rtl8168h_1_hw_phy_config(tp);
4428 break;
4429 case RTL_GIGA_MAC_VER_46:
4430 case RTL_GIGA_MAC_VER_48:
4431 rtl8168h_2_hw_phy_config(tp);
4432 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004433
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004434 case RTL_GIGA_MAC_VER_49:
4435 rtl8168ep_1_hw_phy_config(tp);
4436 break;
4437 case RTL_GIGA_MAC_VER_50:
4438 case RTL_GIGA_MAC_VER_51:
4439 rtl8168ep_2_hw_phy_config(tp);
4440 break;
4441
Hayes Wangc5583862012-07-02 17:23:22 +08004442 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004443 default:
4444 break;
4445 }
4446}
4447
Francois Romieuda78dbf2012-01-26 14:18:23 +01004448static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 struct timer_list *timer = &tp->timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4452
Francois Romieubcf0bf92006-07-26 23:14:13 +02004453 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
françois romieu4da19632011-01-03 15:07:55 +00004455 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02004456 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 * A busy loop could burn quite a few cycles on nowadays CPU.
4458 * Let's delay the execution of the timer for a few ticks.
4459 */
4460 timeout = HZ/10;
4461 goto out_mod_timer;
4462 }
4463
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004464 if (tp->link_ok(tp))
Francois Romieuda78dbf2012-01-26 14:18:23 +01004465 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02004467 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
françois romieu4da19632011-01-03 15:07:55 +00004469 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
4471out_mod_timer:
4472 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004473}
4474
4475static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4476{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004477 if (!test_and_set_bit(flag, tp->wk.flags))
4478 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004479}
4480
Kees Cook9de36cc2017-10-25 03:53:12 -07004481static void rtl8169_phy_timer(struct timer_list *t)
Francois Romieuda78dbf2012-01-26 14:18:23 +01004482{
Kees Cook9de36cc2017-10-25 03:53:12 -07004483 struct rtl8169_private *tp = from_timer(tp, t, timer);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004484
Francois Romieu98ddf982012-01-31 10:47:34 +01004485 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486}
4487
Francois Romieuffc46952012-07-06 14:19:23 +02004488DECLARE_RTL_COND(rtl_phy_reset_cond)
4489{
4490 return tp->phy_reset_pending(tp);
4491}
4492
Francois Romieubf793292006-11-01 00:53:05 +01004493static void rtl8169_phy_reset(struct net_device *dev,
4494 struct rtl8169_private *tp)
4495{
françois romieu4da19632011-01-03 15:07:55 +00004496 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004497 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004498}
4499
David S. Miller8decf862011-09-22 03:23:13 -04004500static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4501{
David S. Miller8decf862011-09-22 03:23:13 -04004502 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004503 (RTL_R8(tp, PHYstatus) & TBI_Enable);
David S. Miller8decf862011-09-22 03:23:13 -04004504}
4505
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004506static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507{
Francois Romieu5615d9f2007-08-17 17:50:46 +02004508 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004509
Marcus Sundberg773328942008-07-10 21:28:08 +02004510 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4511 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004512 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02004513 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004514
Francois Romieu6dccd162007-02-13 23:38:05 +01004515 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4516
4517 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4518 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004519
Francois Romieubcf0bf92006-07-26 23:14:13 +02004520 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004521 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004522 RTL_W8(tp, 0x82, 0x01);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004523 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004524 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004525 }
4526
Francois Romieubf793292006-11-01 00:53:05 +01004527 rtl8169_phy_reset(dev, tp);
4528
Oliver Neukum54405cd2011-01-06 21:55:13 +01004529 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004530 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4531 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4532 (tp->mii.supports_gmii ?
4533 ADVERTISED_1000baseT_Half |
4534 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004535
David S. Miller8decf862011-09-22 03:23:13 -04004536 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00004537 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004538}
4539
Francois Romieu773d2022007-01-31 23:47:43 +01004540static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4541{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004542 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004543
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004544 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00004545
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004546 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4547 RTL_R32(tp, MAC4);
françois romieu908ba2b2010-04-26 11:42:58 +00004548
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004549 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4550 RTL_R32(tp, MAC0);
françois romieu908ba2b2010-04-26 11:42:58 +00004551
françois romieu9ecb9aa2012-12-07 11:20:21 +00004552 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4553 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004554
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004555 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieu773d2022007-01-31 23:47:43 +01004556
Francois Romieuda78dbf2012-01-26 14:18:23 +01004557 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004558}
4559
4560static int rtl_set_mac_address(struct net_device *dev, void *p)
4561{
4562 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004563 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004564 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004565
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004566 ret = eth_mac_addr(dev, p);
4567 if (ret)
4568 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004569
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004570 pm_runtime_get_noresume(d);
4571
4572 if (pm_runtime_active(d))
4573 rtl_rar_set(tp, dev->dev_addr);
4574
4575 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004576
4577 return 0;
4578}
4579
Francois Romieu5f787a12006-08-17 13:02:36 +02004580static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4581{
4582 struct rtl8169_private *tp = netdev_priv(dev);
4583 struct mii_ioctl_data *data = if_mii(ifr);
4584
Francois Romieu8b4ab282008-11-19 22:05:25 -08004585 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4586}
Francois Romieu5f787a12006-08-17 13:02:36 +02004587
Francois Romieucecb5fd2011-04-01 10:21:07 +02004588static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4589 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004590{
Francois Romieu5f787a12006-08-17 13:02:36 +02004591 switch (cmd) {
4592 case SIOCGMIIPHY:
4593 data->phy_id = 32; /* Internal PHY */
4594 return 0;
4595
4596 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004597 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004598 return 0;
4599
4600 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004601 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004602 return 0;
4603 }
4604 return -EOPNOTSUPP;
4605}
4606
Francois Romieu8b4ab282008-11-19 22:05:25 -08004607static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4608{
4609 return -EOPNOTSUPP;
4610}
4611
Bill Pembertonbaf63292012-12-03 09:23:28 -05004612static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004613{
4614 struct mdio_ops *ops = &tp->mdio_ops;
4615
4616 switch (tp->mac_version) {
4617 case RTL_GIGA_MAC_VER_27:
4618 ops->write = r8168dp_1_mdio_write;
4619 ops->read = r8168dp_1_mdio_read;
4620 break;
françois romieue6de30d2011-01-03 15:08:37 +00004621 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004622 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004623 ops->write = r8168dp_2_mdio_write;
4624 ops->read = r8168dp_2_mdio_read;
4625 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004626 case RTL_GIGA_MAC_VER_40:
4627 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004628 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004629 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004630 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004631 case RTL_GIGA_MAC_VER_45:
4632 case RTL_GIGA_MAC_VER_46:
4633 case RTL_GIGA_MAC_VER_47:
4634 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004635 case RTL_GIGA_MAC_VER_49:
4636 case RTL_GIGA_MAC_VER_50:
4637 case RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004638 ops->write = r8168g_mdio_write;
4639 ops->read = r8168g_mdio_read;
4640 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004641 default:
4642 ops->write = r8169_mdio_write;
4643 ops->read = r8169_mdio_read;
4644 break;
4645 }
4646}
4647
hayeswange2409d82013-03-31 17:02:04 +00004648static void rtl_speed_down(struct rtl8169_private *tp)
4649{
4650 u32 adv;
4651 int lpa;
4652
4653 rtl_writephy(tp, 0x1f, 0x0000);
4654 lpa = rtl_readphy(tp, MII_LPA);
4655
4656 if (lpa & (LPA_10HALF | LPA_10FULL))
4657 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4658 else if (lpa & (LPA_100HALF | LPA_100FULL))
4659 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4660 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4661 else
4662 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4663 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4664 (tp->mii.supports_gmii ?
4665 ADVERTISED_1000baseT_Half |
4666 ADVERTISED_1000baseT_Full : 0);
4667
4668 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4669 adv);
4670}
4671
David S. Miller1805b2f2011-10-24 18:18:09 -04004672static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4673{
David S. Miller1805b2f2011-10-24 18:18:09 -04004674 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004675 case RTL_GIGA_MAC_VER_25:
4676 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004677 case RTL_GIGA_MAC_VER_29:
4678 case RTL_GIGA_MAC_VER_30:
4679 case RTL_GIGA_MAC_VER_32:
4680 case RTL_GIGA_MAC_VER_33:
4681 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004682 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004683 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004684 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08004685 case RTL_GIGA_MAC_VER_40:
4686 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004687 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004688 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004689 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004690 case RTL_GIGA_MAC_VER_45:
4691 case RTL_GIGA_MAC_VER_46:
4692 case RTL_GIGA_MAC_VER_47:
4693 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004694 case RTL_GIGA_MAC_VER_49:
4695 case RTL_GIGA_MAC_VER_50:
4696 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004697 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04004698 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4699 break;
4700 default:
4701 break;
4702 }
4703}
4704
4705static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4706{
4707 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4708 return false;
4709
hayeswange2409d82013-03-31 17:02:04 +00004710 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004711 rtl_wol_suspend_quirk(tp);
4712
4713 return true;
4714}
4715
françois romieu065c27c2011-01-03 15:08:12 +00004716static void r8168_phy_power_up(struct rtl8169_private *tp)
4717{
4718 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004719 switch (tp->mac_version) {
4720 case RTL_GIGA_MAC_VER_11:
4721 case RTL_GIGA_MAC_VER_12:
4722 case RTL_GIGA_MAC_VER_17:
4723 case RTL_GIGA_MAC_VER_18:
4724 case RTL_GIGA_MAC_VER_19:
4725 case RTL_GIGA_MAC_VER_20:
4726 case RTL_GIGA_MAC_VER_21:
4727 case RTL_GIGA_MAC_VER_22:
4728 case RTL_GIGA_MAC_VER_23:
4729 case RTL_GIGA_MAC_VER_24:
4730 case RTL_GIGA_MAC_VER_25:
4731 case RTL_GIGA_MAC_VER_26:
4732 case RTL_GIGA_MAC_VER_27:
4733 case RTL_GIGA_MAC_VER_28:
4734 case RTL_GIGA_MAC_VER_31:
4735 rtl_writephy(tp, 0x0e, 0x0000);
4736 break;
4737 default:
4738 break;
4739 }
françois romieu065c27c2011-01-03 15:08:12 +00004740 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4741}
4742
4743static void r8168_phy_power_down(struct rtl8169_private *tp)
4744{
4745 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004746 switch (tp->mac_version) {
4747 case RTL_GIGA_MAC_VER_32:
4748 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004749 case RTL_GIGA_MAC_VER_40:
4750 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004751 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4752 break;
4753
4754 case RTL_GIGA_MAC_VER_11:
4755 case RTL_GIGA_MAC_VER_12:
4756 case RTL_GIGA_MAC_VER_17:
4757 case RTL_GIGA_MAC_VER_18:
4758 case RTL_GIGA_MAC_VER_19:
4759 case RTL_GIGA_MAC_VER_20:
4760 case RTL_GIGA_MAC_VER_21:
4761 case RTL_GIGA_MAC_VER_22:
4762 case RTL_GIGA_MAC_VER_23:
4763 case RTL_GIGA_MAC_VER_24:
4764 case RTL_GIGA_MAC_VER_25:
4765 case RTL_GIGA_MAC_VER_26:
4766 case RTL_GIGA_MAC_VER_27:
4767 case RTL_GIGA_MAC_VER_28:
4768 case RTL_GIGA_MAC_VER_31:
4769 rtl_writephy(tp, 0x0e, 0x0200);
4770 default:
4771 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4772 break;
4773 }
françois romieu065c27c2011-01-03 15:08:12 +00004774}
4775
4776static void r8168_pll_power_down(struct rtl8169_private *tp)
4777{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01004778 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004779 return;
4780
hayeswang01dc7fe2011-03-21 01:50:28 +00004781 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4782 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004783 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004784
David S. Miller1805b2f2011-10-24 18:18:09 -04004785 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004786 return;
françois romieu065c27c2011-01-03 15:08:12 +00004787
4788 r8168_phy_power_down(tp);
4789
4790 switch (tp->mac_version) {
4791 case RTL_GIGA_MAC_VER_25:
4792 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004793 case RTL_GIGA_MAC_VER_27:
4794 case RTL_GIGA_MAC_VER_28:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004795 case RTL_GIGA_MAC_VER_29:
4796 case RTL_GIGA_MAC_VER_30:
hayeswang4804b3b2011-03-21 01:50:29 +00004797 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004798 case RTL_GIGA_MAC_VER_32:
4799 case RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004800 case RTL_GIGA_MAC_VER_37:
4801 case RTL_GIGA_MAC_VER_39:
4802 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004803 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004804 case RTL_GIGA_MAC_VER_45:
4805 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004806 case RTL_GIGA_MAC_VER_47:
4807 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004808 case RTL_GIGA_MAC_VER_50:
4809 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004810 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004811 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004812 case RTL_GIGA_MAC_VER_40:
4813 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004814 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004815 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004816 0xfc000000, ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004817 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004818 break;
françois romieu065c27c2011-01-03 15:08:12 +00004819 }
4820}
4821
4822static void r8168_pll_power_up(struct rtl8169_private *tp)
4823{
françois romieu065c27c2011-01-03 15:08:12 +00004824 switch (tp->mac_version) {
4825 case RTL_GIGA_MAC_VER_25:
4826 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004827 case RTL_GIGA_MAC_VER_27:
4828 case RTL_GIGA_MAC_VER_28:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004829 case RTL_GIGA_MAC_VER_29:
4830 case RTL_GIGA_MAC_VER_30:
hayeswang4804b3b2011-03-21 01:50:29 +00004831 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004832 case RTL_GIGA_MAC_VER_32:
4833 case RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004834 case RTL_GIGA_MAC_VER_37:
4835 case RTL_GIGA_MAC_VER_39:
4836 case RTL_GIGA_MAC_VER_43:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004837 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004838 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004839 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004840 case RTL_GIGA_MAC_VER_45:
4841 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004842 case RTL_GIGA_MAC_VER_47:
4843 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004844 case RTL_GIGA_MAC_VER_50:
4845 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004846 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004847 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004848 case RTL_GIGA_MAC_VER_40:
4849 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004850 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004851 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004852 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004853 0x00000000, ERIAR_EXGMAC);
4854 break;
françois romieu065c27c2011-01-03 15:08:12 +00004855 }
4856
4857 r8168_phy_power_up(tp);
4858}
4859
Francois Romieud58d46b2011-05-03 16:38:29 +02004860static void rtl_generic_op(struct rtl8169_private *tp,
4861 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00004862{
4863 if (op)
4864 op(tp);
4865}
4866
4867static void rtl_pll_power_down(struct rtl8169_private *tp)
4868{
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004869 switch (tp->mac_version) {
4870 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4871 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4872 break;
4873 default:
4874 r8168_pll_power_down(tp);
4875 }
françois romieu065c27c2011-01-03 15:08:12 +00004876}
4877
4878static void rtl_pll_power_up(struct rtl8169_private *tp)
4879{
françois romieu065c27c2011-01-03 15:08:12 +00004880 switch (tp->mac_version) {
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004881 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4882 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
françois romieu065c27c2011-01-03 15:08:12 +00004883 break;
françois romieu065c27c2011-01-03 15:08:12 +00004884 default:
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004885 r8168_pll_power_up(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004886 }
4887}
4888
Hayes Wange542a222011-07-06 15:58:04 +08004889static void rtl_init_rxcfg(struct rtl8169_private *tp)
4890{
Hayes Wange542a222011-07-06 15:58:04 +08004891 switch (tp->mac_version) {
4892 case RTL_GIGA_MAC_VER_01:
4893 case RTL_GIGA_MAC_VER_02:
4894 case RTL_GIGA_MAC_VER_03:
4895 case RTL_GIGA_MAC_VER_04:
4896 case RTL_GIGA_MAC_VER_05:
4897 case RTL_GIGA_MAC_VER_06:
4898 case RTL_GIGA_MAC_VER_10:
4899 case RTL_GIGA_MAC_VER_11:
4900 case RTL_GIGA_MAC_VER_12:
4901 case RTL_GIGA_MAC_VER_13:
4902 case RTL_GIGA_MAC_VER_14:
4903 case RTL_GIGA_MAC_VER_15:
4904 case RTL_GIGA_MAC_VER_16:
4905 case RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004906 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004907 break;
4908 case RTL_GIGA_MAC_VER_18:
4909 case RTL_GIGA_MAC_VER_19:
4910 case RTL_GIGA_MAC_VER_20:
4911 case RTL_GIGA_MAC_VER_21:
4912 case RTL_GIGA_MAC_VER_22:
4913 case RTL_GIGA_MAC_VER_23:
4914 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004915 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02004916 case RTL_GIGA_MAC_VER_35:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004917 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004918 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004919 case RTL_GIGA_MAC_VER_40:
4920 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004921 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004922 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004923 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004924 case RTL_GIGA_MAC_VER_45:
4925 case RTL_GIGA_MAC_VER_46:
4926 case RTL_GIGA_MAC_VER_47:
4927 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004928 case RTL_GIGA_MAC_VER_49:
4929 case RTL_GIGA_MAC_VER_50:
4930 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004931 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00004932 break;
Hayes Wange542a222011-07-06 15:58:04 +08004933 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004934 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004935 break;
4936 }
4937}
4938
Hayes Wang92fc43b2011-07-06 15:58:03 +08004939static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4940{
Timo Teräs9fba0812013-01-15 21:01:24 +00004941 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004942}
4943
Francois Romieud58d46b2011-05-03 16:38:29 +02004944static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4945{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004946 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004947 rtl_generic_op(tp, tp->jumbo_ops.enable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004948 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004949}
4950
4951static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4952{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004953 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004954 rtl_generic_op(tp, tp->jumbo_ops.disable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004955 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004956}
4957
4958static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4959{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004960 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4961 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004962 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004963}
4964
4965static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4966{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004967 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4968 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004969 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004970}
4971
4972static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4973{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004974 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004975}
4976
4977static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4978{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004979 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004980}
4981
4982static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4983{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004984 RTL_W8(tp, MaxTxPacketSize, 0x3f);
4985 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4986 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004987 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004988}
4989
4990static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4991{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004992 RTL_W8(tp, MaxTxPacketSize, 0x0c);
4993 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4994 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004995 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004996}
4997
4998static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4999{
Heiner Kallweitcb732002018-03-20 07:45:35 +01005000 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005001 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02005002}
5003
5004static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
5005{
Heiner Kallweitcb732002018-03-20 07:45:35 +01005006 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005007 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02005008}
5009
5010static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
5011{
Francois Romieud58d46b2011-05-03 16:38:29 +02005012 r8168b_0_hw_jumbo_enable(tp);
5013
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005014 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02005015}
5016
5017static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
5018{
Francois Romieud58d46b2011-05-03 16:38:29 +02005019 r8168b_0_hw_jumbo_disable(tp);
5020
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005021 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02005022}
5023
Bill Pembertonbaf63292012-12-03 09:23:28 -05005024static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02005025{
5026 struct jumbo_ops *ops = &tp->jumbo_ops;
5027
5028 switch (tp->mac_version) {
5029 case RTL_GIGA_MAC_VER_11:
5030 ops->disable = r8168b_0_hw_jumbo_disable;
5031 ops->enable = r8168b_0_hw_jumbo_enable;
5032 break;
5033 case RTL_GIGA_MAC_VER_12:
5034 case RTL_GIGA_MAC_VER_17:
5035 ops->disable = r8168b_1_hw_jumbo_disable;
5036 ops->enable = r8168b_1_hw_jumbo_enable;
5037 break;
5038 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5039 case RTL_GIGA_MAC_VER_19:
5040 case RTL_GIGA_MAC_VER_20:
5041 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5042 case RTL_GIGA_MAC_VER_22:
5043 case RTL_GIGA_MAC_VER_23:
5044 case RTL_GIGA_MAC_VER_24:
5045 case RTL_GIGA_MAC_VER_25:
5046 case RTL_GIGA_MAC_VER_26:
5047 ops->disable = r8168c_hw_jumbo_disable;
5048 ops->enable = r8168c_hw_jumbo_enable;
5049 break;
5050 case RTL_GIGA_MAC_VER_27:
5051 case RTL_GIGA_MAC_VER_28:
5052 ops->disable = r8168dp_hw_jumbo_disable;
5053 ops->enable = r8168dp_hw_jumbo_enable;
5054 break;
5055 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5056 case RTL_GIGA_MAC_VER_32:
5057 case RTL_GIGA_MAC_VER_33:
5058 case RTL_GIGA_MAC_VER_34:
5059 ops->disable = r8168e_hw_jumbo_disable;
5060 ops->enable = r8168e_hw_jumbo_enable;
5061 break;
5062
5063 /*
5064 * No action needed for jumbo frames with 8169.
5065 * No jumbo for 810x at all.
5066 */
Hayes Wangc5583862012-07-02 17:23:22 +08005067 case RTL_GIGA_MAC_VER_40:
5068 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005069 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005070 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005071 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005072 case RTL_GIGA_MAC_VER_45:
5073 case RTL_GIGA_MAC_VER_46:
5074 case RTL_GIGA_MAC_VER_47:
5075 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005076 case RTL_GIGA_MAC_VER_49:
5077 case RTL_GIGA_MAC_VER_50:
5078 case RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02005079 default:
5080 ops->disable = NULL;
5081 ops->enable = NULL;
5082 break;
5083 }
5084}
5085
Francois Romieuffc46952012-07-06 14:19:23 +02005086DECLARE_RTL_COND(rtl_chipcmd_cond)
5087{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005088 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02005089}
5090
Francois Romieu6f43adc2011-04-29 15:05:51 +02005091static void rtl_hw_reset(struct rtl8169_private *tp)
5092{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005093 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005094
Francois Romieuffc46952012-07-06 14:19:23 +02005095 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005096}
5097
Francois Romieub6ffd972011-06-17 17:00:05 +02005098static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
5099{
5100 struct rtl_fw *rtl_fw;
5101 const char *name;
5102 int rc = -ENOMEM;
5103
5104 name = rtl_lookup_firmware_name(tp);
5105 if (!name)
5106 goto out_no_firmware;
5107
5108 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5109 if (!rtl_fw)
5110 goto err_warn;
5111
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005112 rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
Francois Romieub6ffd972011-06-17 17:00:05 +02005113 if (rc < 0)
5114 goto err_free;
5115
Francois Romieufd112f22011-06-18 00:10:29 +02005116 rc = rtl_check_firmware(tp, rtl_fw);
5117 if (rc < 0)
5118 goto err_release_firmware;
5119
Francois Romieub6ffd972011-06-17 17:00:05 +02005120 tp->rtl_fw = rtl_fw;
5121out:
5122 return;
5123
Francois Romieufd112f22011-06-18 00:10:29 +02005124err_release_firmware:
5125 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02005126err_free:
5127 kfree(rtl_fw);
5128err_warn:
5129 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5130 name, rc);
5131out_no_firmware:
5132 tp->rtl_fw = NULL;
5133 goto out;
5134}
5135
François Romieu953a12c2011-04-24 17:38:48 +02005136static void rtl_request_firmware(struct rtl8169_private *tp)
5137{
Francois Romieub6ffd972011-06-17 17:00:05 +02005138 if (IS_ERR(tp->rtl_fw))
5139 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02005140}
5141
Hayes Wang92fc43b2011-07-06 15:58:03 +08005142static void rtl_rx_close(struct rtl8169_private *tp)
5143{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005144 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005145}
5146
Francois Romieuffc46952012-07-06 14:19:23 +02005147DECLARE_RTL_COND(rtl_npq_cond)
5148{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005149 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02005150}
5151
5152DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5153{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005154 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02005155}
5156
françois romieue6de30d2011-01-03 15:08:37 +00005157static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158{
5159 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00005160 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161
Hayes Wang92fc43b2011-07-06 15:58:03 +08005162 rtl_rx_close(tp);
5163
Hayes Wang5d2e1952011-02-22 17:26:22 +08005164 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00005165 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5166 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02005167 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08005168 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005169 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5170 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5171 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5172 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5173 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5174 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5175 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5176 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5177 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5178 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5179 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5180 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005181 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5182 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5183 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5184 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005185 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02005186 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005187 } else {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005188 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005189 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00005190 }
5191
Hayes Wang92fc43b2011-07-06 15:58:03 +08005192 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193}
5194
Francois Romieu7f796d832007-06-11 23:04:41 +02005195static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005196{
Francois Romieu9cb427b2006-11-02 00:10:16 +01005197 /* Set DMA burst size and Interframe Gap Time */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005198 RTL_W32(tp, TxConfig, (TX_DMA_BURST << TxDMAShift) |
Francois Romieu9cb427b2006-11-02 00:10:16 +01005199 (InterFrameGap << TxInterFrameGapShift));
5200}
5201
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02005202static void rtl_set_rx_max_size(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02005204 /* Low hurts. Let's disable the filtering. */
5205 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
Francois Romieu07ce4062007-02-23 23:36:39 +01005206}
5207
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005208static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02005209{
5210 /*
5211 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5212 * register to be written before TxDescAddrLow to work.
5213 * Switching from MMIO to I/O access fixes the issue as well.
5214 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005215 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
5216 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
5217 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
5218 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005219}
5220
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02005221static void rtl_hw_start(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02005222{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02005223 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
5224
5225 tp->hw_start(tp);
5226
5227 rtl_set_rx_max_size(tp);
5228 rtl_set_rx_tx_desc_registers(tp);
5229 rtl_set_rx_tx_config_registers(tp);
5230 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
5231
5232 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5233 RTL_R8(tp, IntrMask);
5234 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
5235 /* no early-rx interrupts */
5236 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
5237 rtl_irq_enable_all(tp);
Francois Romieu7f796d832007-06-11 23:04:41 +02005238}
5239
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005240static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01005241{
Francois Romieu37441002011-06-17 22:58:54 +02005242 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01005243 u32 mac_version;
5244 u32 clk;
5245 u32 val;
5246 } cfg2_info [] = {
5247 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5248 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5249 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5250 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02005251 };
5252 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01005253 unsigned int i;
5254 u32 clk;
5255
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005256 clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01005257 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01005258 if ((p->mac_version == mac_version) && (p->clk == clk)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005259 RTL_W32(tp, 0x7c, p->val);
Francois Romieu6dccd162007-02-13 23:38:05 +01005260 break;
5261 }
5262 }
5263}
5264
Francois Romieue6b763e2012-03-08 09:35:39 +01005265static void rtl_set_rx_mode(struct net_device *dev)
5266{
5267 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieue6b763e2012-03-08 09:35:39 +01005268 u32 mc_filter[2]; /* Multicast hash filter */
5269 int rx_mode;
5270 u32 tmp = 0;
5271
5272 if (dev->flags & IFF_PROMISC) {
5273 /* Unconditionally log net taps. */
5274 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5275 rx_mode =
5276 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5277 AcceptAllPhys;
5278 mc_filter[1] = mc_filter[0] = 0xffffffff;
5279 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5280 (dev->flags & IFF_ALLMULTI)) {
5281 /* Too many to filter perfectly -- accept all multicasts. */
5282 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5283 mc_filter[1] = mc_filter[0] = 0xffffffff;
5284 } else {
5285 struct netdev_hw_addr *ha;
5286
5287 rx_mode = AcceptBroadcast | AcceptMyPhys;
5288 mc_filter[1] = mc_filter[0] = 0;
5289 netdev_for_each_mc_addr(ha, dev) {
5290 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5291 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5292 rx_mode |= AcceptMulticast;
5293 }
5294 }
5295
5296 if (dev->features & NETIF_F_RXALL)
5297 rx_mode |= (AcceptErr | AcceptRunt);
5298
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005299 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Francois Romieue6b763e2012-03-08 09:35:39 +01005300
5301 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5302 u32 data = mc_filter[0];
5303
5304 mc_filter[0] = swab32(mc_filter[1]);
5305 mc_filter[1] = swab32(data);
5306 }
5307
Nathan Walp04817762012-11-01 12:08:47 +00005308 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5309 mc_filter[1] = mc_filter[0] = 0xffffffff;
5310
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005311 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
5312 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01005313
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005314 RTL_W32(tp, RxConfig, tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01005315}
5316
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005317static void rtl_hw_start_8169(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005318{
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005319 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005320 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005321
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005322 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005324 tp->cp_cmd |= PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005325
Francois Romieucecb5fd2011-04-01 10:21:07 +02005326 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5327 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005328 dprintk("Set MAC Reg C+CR Offset 0xe0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005330 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 }
5332
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005333 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005334
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005335 rtl8169_set_magic_reg(tp, tp->mac_version);
Francois Romieu6dccd162007-02-13 23:38:05 +01005336
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 /*
5338 * Undocumented corner. Supposedly:
5339 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5340 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005341 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005343 RTL_W32(tp, RxMissed, 0);
Francois Romieu07ce4062007-02-23 23:36:39 +01005344}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005346static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5347{
5348 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02005349 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005350}
5351
5352static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5353{
Francois Romieu52989f02012-07-06 13:37:00 +02005354 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005355}
5356
5357static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02005358{
5359 u32 csi;
5360
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005361 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5362 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00005363}
5364
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005365static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005366{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005367 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00005368}
5369
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005370static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00005371{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005372 rtl_csi_access_enable(tp, 0x27000000);
5373}
5374
Francois Romieuffc46952012-07-06 14:19:23 +02005375DECLARE_RTL_COND(rtl_csiar_cond)
5376{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005377 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02005378}
5379
Francois Romieu52989f02012-07-06 13:37:00 +02005380static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005381{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005382 RTL_W32(tp, CSIDR, value);
5383 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005384 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5385
Francois Romieuffc46952012-07-06 14:19:23 +02005386 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005387}
5388
Francois Romieu52989f02012-07-06 13:37:00 +02005389static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005390{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005391 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) |
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005392 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5393
Francois Romieuffc46952012-07-06 14:19:23 +02005394 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005395 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005396}
5397
Francois Romieu52989f02012-07-06 13:37:00 +02005398static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005399{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005400 RTL_W32(tp, CSIDR, value);
5401 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Hayes Wang7e18dca2012-03-30 14:33:02 +08005402 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5403 CSIAR_FUNC_NIC);
5404
Francois Romieuffc46952012-07-06 14:19:23 +02005405 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005406}
5407
Francois Romieu52989f02012-07-06 13:37:00 +02005408static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005409{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005410 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
Hayes Wang7e18dca2012-03-30 14:33:02 +08005411 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5412
Francois Romieuffc46952012-07-06 14:19:23 +02005413 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005414 RTL_R32(tp, CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005415}
5416
hayeswang45dd95c2013-07-08 17:09:01 +08005417static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5418{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005419 RTL_W32(tp, CSIDR, value);
5420 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
hayeswang45dd95c2013-07-08 17:09:01 +08005421 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5422 CSIAR_FUNC_NIC2);
5423
5424 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5425}
5426
5427static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5428{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005429 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
hayeswang45dd95c2013-07-08 17:09:01 +08005430 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5431
5432 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005433 RTL_R32(tp, CSIDR) : ~0;
hayeswang45dd95c2013-07-08 17:09:01 +08005434}
5435
Bill Pembertonbaf63292012-12-03 09:23:28 -05005436static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005437{
5438 struct csi_ops *ops = &tp->csi_ops;
5439
5440 switch (tp->mac_version) {
5441 case RTL_GIGA_MAC_VER_01:
5442 case RTL_GIGA_MAC_VER_02:
5443 case RTL_GIGA_MAC_VER_03:
5444 case RTL_GIGA_MAC_VER_04:
5445 case RTL_GIGA_MAC_VER_05:
5446 case RTL_GIGA_MAC_VER_06:
5447 case RTL_GIGA_MAC_VER_10:
5448 case RTL_GIGA_MAC_VER_11:
5449 case RTL_GIGA_MAC_VER_12:
5450 case RTL_GIGA_MAC_VER_13:
5451 case RTL_GIGA_MAC_VER_14:
5452 case RTL_GIGA_MAC_VER_15:
5453 case RTL_GIGA_MAC_VER_16:
5454 case RTL_GIGA_MAC_VER_17:
5455 ops->write = NULL;
5456 ops->read = NULL;
5457 break;
5458
Hayes Wang7e18dca2012-03-30 14:33:02 +08005459 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005460 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005461 ops->write = r8402_csi_write;
5462 ops->read = r8402_csi_read;
5463 break;
5464
hayeswang45dd95c2013-07-08 17:09:01 +08005465 case RTL_GIGA_MAC_VER_44:
5466 ops->write = r8411_csi_write;
5467 ops->read = r8411_csi_read;
5468 break;
5469
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005470 default:
5471 ops->write = r8169_csi_write;
5472 ops->read = r8169_csi_read;
5473 break;
5474 }
Francois Romieudacf8152008-08-02 20:44:13 +02005475}
5476
5477struct ephy_info {
5478 unsigned int offset;
5479 u16 mask;
5480 u16 bits;
5481};
5482
Francois Romieufdf6fc02012-07-06 22:40:38 +02005483static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5484 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005485{
5486 u16 w;
5487
5488 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005489 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5490 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005491 e++;
5492 }
5493}
5494
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005495static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02005496{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005497 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005498 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005499}
5500
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005501static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005502{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005503 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005504 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005505}
5506
hayeswangb51ecea2014-07-09 14:52:51 +08005507static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5508{
hayeswangb51ecea2014-07-09 14:52:51 +08005509 u8 data;
5510
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005511 data = RTL_R8(tp, Config3);
hayeswangb51ecea2014-07-09 14:52:51 +08005512
5513 if (enable)
5514 data |= Rdy_to_L23;
5515 else
5516 data &= ~Rdy_to_L23;
5517
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005518 RTL_W8(tp, Config3, data);
hayeswangb51ecea2014-07-09 14:52:51 +08005519}
5520
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005521static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005522{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005523 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005524
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005525 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005526 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieub726e492008-06-28 12:22:59 +02005527
françois romieufaf1e782013-02-27 13:01:57 +00005528 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005529 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00005530 PCI_EXP_DEVCTL_NOSNOOP_EN);
5531 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005532}
5533
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005534static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005535{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005536 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005537
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005538 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005539
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005540 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005541}
5542
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005543static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005544{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005545 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02005546
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005547 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005548
françois romieufaf1e782013-02-27 13:01:57 +00005549 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005550 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02005551
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005552 rtl_disable_clock_request(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005553
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005554 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005555 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu219a1e92008-06-28 11:58:39 +02005556}
5557
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005558static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005559{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005560 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005561 { 0x01, 0, 0x0001 },
5562 { 0x02, 0x0800, 0x1000 },
5563 { 0x03, 0, 0x0042 },
5564 { 0x06, 0x0080, 0x0000 },
5565 { 0x07, 0, 0x2000 }
5566 };
5567
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005568 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005569
Francois Romieufdf6fc02012-07-06 22:40:38 +02005570 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005571
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005572 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005573}
5574
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005575static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005576{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005577 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005578
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005579 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02005580
françois romieufaf1e782013-02-27 13:01:57 +00005581 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005582 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieuef3386f2008-06-29 12:24:30 +02005583
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005584 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005585 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieuef3386f2008-06-29 12:24:30 +02005586}
5587
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005588static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005589{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005590 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005591
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005592 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005593
5594 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005595 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005596
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005597 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005598
françois romieufaf1e782013-02-27 13:01:57 +00005599 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005600 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005601
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005602 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005603 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005604}
5605
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005606static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005607{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005608 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005609 { 0x02, 0x0800, 0x1000 },
5610 { 0x03, 0, 0x0002 },
5611 { 0x06, 0x0080, 0x0000 }
5612 };
5613
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005614 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005615
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005616 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02005617
Francois Romieufdf6fc02012-07-06 22:40:38 +02005618 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005619
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005620 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005621}
5622
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005623static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005624{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005625 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005626 { 0x01, 0, 0x0001 },
5627 { 0x03, 0x0400, 0x0220 }
5628 };
5629
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005630 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005631
Francois Romieufdf6fc02012-07-06 22:40:38 +02005632 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005633
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005634 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005635}
5636
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005637static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005638{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005639 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005640}
5641
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005642static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005643{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005644 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005645
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005646 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005647}
5648
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005649static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005650{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005651 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005652
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005653 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005654
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005655 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005656
françois romieufaf1e782013-02-27 13:01:57 +00005657 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005658 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02005659
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005660 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005661 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu5b538df2008-07-20 16:22:45 +02005662}
5663
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005664static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005665{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005666 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005667
françois romieufaf1e782013-02-27 13:01:57 +00005668 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005669 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang4804b3b2011-03-21 01:50:29 +00005670
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005671 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang4804b3b2011-03-21 01:50:29 +00005672
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005673 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005674}
5675
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005676static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005677{
5678 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005679 { 0x0b, 0x0000, 0x0048 },
5680 { 0x19, 0x0020, 0x0050 },
5681 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00005682 };
françois romieue6de30d2011-01-03 15:08:37 +00005683
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005684 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005685
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005686 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00005687
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005688 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
françois romieue6de30d2011-01-03 15:08:37 +00005689
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005690 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00005691
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005692 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005693}
5694
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005695static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005696{
Hayes Wang70090422011-07-06 15:58:06 +08005697 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005698 { 0x00, 0x0200, 0x0100 },
5699 { 0x00, 0x0000, 0x0004 },
5700 { 0x06, 0x0002, 0x0001 },
5701 { 0x06, 0x0000, 0x0030 },
5702 { 0x07, 0x0000, 0x2000 },
5703 { 0x00, 0x0000, 0x0020 },
5704 { 0x03, 0x5800, 0x2000 },
5705 { 0x03, 0x0000, 0x0001 },
5706 { 0x01, 0x0800, 0x1000 },
5707 { 0x07, 0x0000, 0x4000 },
5708 { 0x1e, 0x0000, 0x2000 },
5709 { 0x19, 0xffff, 0xfe6c },
5710 { 0x0a, 0x0000, 0x0040 }
5711 };
5712
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005713 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005714
Francois Romieufdf6fc02012-07-06 22:40:38 +02005715 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005716
françois romieufaf1e782013-02-27 13:01:57 +00005717 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005718 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00005719
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005720 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang01dc7fe2011-03-21 01:50:28 +00005721
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005722 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005723
5724 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005725 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
5726 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005727
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005728 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005729}
5730
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005731static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005732{
5733 static const struct ephy_info e_info_8168e_2[] = {
5734 { 0x09, 0x0000, 0x0080 },
5735 { 0x19, 0x0000, 0x0224 }
5736 };
5737
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005738 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005739
Francois Romieufdf6fc02012-07-06 22:40:38 +02005740 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005741
françois romieufaf1e782013-02-27 13:01:57 +00005742 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005743 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08005744
Francois Romieufdf6fc02012-07-06 22:40:38 +02005745 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5746 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5747 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5748 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5749 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5750 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005751 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5752 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005753
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005754 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005755
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005756 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005757
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005758 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5759 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08005760
5761 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005762 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang70090422011-07-06 15:58:06 +08005763
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005764 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5765 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5766 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08005767}
5768
Hayes Wang5f886e02012-03-30 14:33:03 +08005769static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005770{
Hayes Wang5f886e02012-03-30 14:33:03 +08005771 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005772
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005773 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08005774
Francois Romieufdf6fc02012-07-06 22:40:38 +02005775 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5776 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5777 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5778 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005779 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5780 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5781 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5782 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005783 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5784 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005785
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005786 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc2218922011-09-06 16:55:18 +08005787
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005788 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005789
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005790 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5791 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5792 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5793 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5794 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005795}
5796
Hayes Wang5f886e02012-03-30 14:33:03 +08005797static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5798{
Hayes Wang5f886e02012-03-30 14:33:03 +08005799 static const struct ephy_info e_info_8168f_1[] = {
5800 { 0x06, 0x00c0, 0x0020 },
5801 { 0x08, 0x0001, 0x0002 },
5802 { 0x09, 0x0000, 0x0080 },
5803 { 0x19, 0x0000, 0x0224 }
5804 };
5805
5806 rtl_hw_start_8168f(tp);
5807
Francois Romieufdf6fc02012-07-06 22:40:38 +02005808 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005809
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005810 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005811
5812 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005813 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang5f886e02012-03-30 14:33:03 +08005814}
5815
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005816static void rtl_hw_start_8411(struct rtl8169_private *tp)
5817{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005818 static const struct ephy_info e_info_8168f_1[] = {
5819 { 0x06, 0x00c0, 0x0020 },
5820 { 0x0f, 0xffff, 0x5200 },
5821 { 0x1e, 0x0000, 0x4000 },
5822 { 0x19, 0x0000, 0x0224 }
5823 };
5824
5825 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08005826 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005827
Francois Romieufdf6fc02012-07-06 22:40:38 +02005828 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005829
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005830 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005831}
5832
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005833static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08005834{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005835 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
hayeswangbeb330a2013-04-01 22:23:39 +00005836
Hayes Wangc5583862012-07-02 17:23:22 +08005837 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5838 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5839 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5840 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5841
5842 rtl_csi_access_enable_1(tp);
5843
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005844 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08005845
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005846 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5847 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00005848 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005849
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005850 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5851 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc5583862012-07-02 17:23:22 +08005852
5853 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5854 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5855
5856 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005857 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wangc5583862012-07-02 17:23:22 +08005858
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005859 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5860 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005861
5862 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08005863}
5864
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005865static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5866{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005867 static const struct ephy_info e_info_8168g_1[] = {
5868 { 0x00, 0x0000, 0x0008 },
5869 { 0x0c, 0x37d0, 0x0820 },
5870 { 0x1e, 0x0000, 0x0001 },
5871 { 0x19, 0x8000, 0x0000 }
5872 };
5873
5874 rtl_hw_start_8168g(tp);
5875
5876 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005877 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5878 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005879 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
5880}
5881
hayeswang57538c42013-04-01 22:23:40 +00005882static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5883{
hayeswang57538c42013-04-01 22:23:40 +00005884 static const struct ephy_info e_info_8168g_2[] = {
5885 { 0x00, 0x0000, 0x0008 },
5886 { 0x0c, 0x3df0, 0x0200 },
5887 { 0x19, 0xffff, 0xfc00 },
5888 { 0x1e, 0xffff, 0x20eb }
5889 };
5890
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005891 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00005892
5893 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005894 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5895 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang57538c42013-04-01 22:23:40 +00005896 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5897}
5898
hayeswang45dd95c2013-07-08 17:09:01 +08005899static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5900{
hayeswang45dd95c2013-07-08 17:09:01 +08005901 static const struct ephy_info e_info_8411_2[] = {
5902 { 0x00, 0x0000, 0x0008 },
5903 { 0x0c, 0x3df0, 0x0200 },
5904 { 0x0f, 0xffff, 0x5200 },
5905 { 0x19, 0x0020, 0x0000 },
5906 { 0x1e, 0x0000, 0x2000 }
5907 };
5908
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005909 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08005910
5911 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005912 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5913 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang45dd95c2013-07-08 17:09:01 +08005914 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
5915}
5916
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005917static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
5918{
Andrzej Hajda72521ea2015-09-24 16:00:24 +02005919 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005920 u32 data;
5921 static const struct ephy_info e_info_8168h_1[] = {
5922 { 0x1e, 0x0800, 0x0001 },
5923 { 0x1d, 0x0000, 0x0800 },
5924 { 0x05, 0xffff, 0x2089 },
5925 { 0x06, 0xffff, 0x5881 },
5926 { 0x04, 0xffff, 0x154a },
5927 { 0x01, 0xffff, 0x068b }
5928 };
5929
5930 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005931 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5932 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005933 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
5934
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005935 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005936
5937 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5938 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5939 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5940 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5941
5942 rtl_csi_access_enable_1(tp);
5943
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005944 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005945
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005946 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5947 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005948
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005949 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005950
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005951 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005952
5953 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5954
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005955 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5956 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005957
5958 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5959 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5960
5961 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005962 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005963
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005964 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5965 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005966
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005967 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005968
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005969 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005970
5971 rtl_pcie_state_l2l3_enable(tp, false);
5972
5973 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08005974 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005975 rtl_writephy(tp, 0x1f, 0x0000);
5976 if (rg_saw_cnt > 0) {
5977 u16 sw_cnt_1ms_ini;
5978
5979 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
5980 sw_cnt_1ms_ini &= 0x0fff;
5981 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005982 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005983 data |= sw_cnt_1ms_ini;
5984 r8168_mac_ocp_write(tp, 0xd412, data);
5985 }
5986
5987 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005988 data &= ~0xf0;
5989 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005990 r8168_mac_ocp_write(tp, 0xe056, data);
5991
5992 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005993 data &= ~0x6000;
5994 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005995 r8168_mac_ocp_write(tp, 0xe052, data);
5996
5997 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005998 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005999 data |= 0x017f;
6000 r8168_mac_ocp_write(tp, 0xe0d6, data);
6001
6002 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006003 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006004 data |= 0x047f;
6005 r8168_mac_ocp_write(tp, 0xd420, data);
6006
6007 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6008 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6009 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6010 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6011}
6012
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006013static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6014{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08006015 rtl8168ep_stop_cmac(tp);
6016
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006017 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006018
6019 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6020 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6021 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6022 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6023
6024 rtl_csi_access_enable_1(tp);
6025
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006026 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006027
6028 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6029 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6030
6031 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6032
6033 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6034
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006035 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
6036 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006037
6038 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6039 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6040
6041 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006042 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006043
6044 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6045
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006046 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006047
6048 rtl_pcie_state_l2l3_enable(tp, false);
6049}
6050
6051static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6052{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006053 static const struct ephy_info e_info_8168ep_1[] = {
6054 { 0x00, 0xffff, 0x10ab },
6055 { 0x06, 0xffff, 0xf030 },
6056 { 0x08, 0xffff, 0x2006 },
6057 { 0x0d, 0xffff, 0x1666 },
6058 { 0x0c, 0x3ff0, 0x0000 }
6059 };
6060
6061 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006062 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6063 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006064 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6065
6066 rtl_hw_start_8168ep(tp);
6067}
6068
6069static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6070{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006071 static const struct ephy_info e_info_8168ep_2[] = {
6072 { 0x00, 0xffff, 0x10a3 },
6073 { 0x19, 0xffff, 0xfc00 },
6074 { 0x1e, 0xffff, 0x20ea }
6075 };
6076
6077 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006078 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6079 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006080 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6081
6082 rtl_hw_start_8168ep(tp);
6083
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006084 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6085 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006086}
6087
6088static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6089{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006090 u32 data;
6091 static const struct ephy_info e_info_8168ep_3[] = {
6092 { 0x00, 0xffff, 0x10a3 },
6093 { 0x19, 0xffff, 0x7c00 },
6094 { 0x1e, 0xffff, 0x20eb },
6095 { 0x0d, 0xffff, 0x1666 }
6096 };
6097
6098 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006099 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6100 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006101 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6102
6103 rtl_hw_start_8168ep(tp);
6104
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006105 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6106 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006107
6108 data = r8168_mac_ocp_read(tp, 0xd3e2);
6109 data &= 0xf000;
6110 data |= 0x0271;
6111 r8168_mac_ocp_write(tp, 0xd3e2, data);
6112
6113 data = r8168_mac_ocp_read(tp, 0xd3e4);
6114 data &= 0xff00;
6115 r8168_mac_ocp_write(tp, 0xd3e4, data);
6116
6117 data = r8168_mac_ocp_read(tp, 0xe860);
6118 data |= 0x0080;
6119 r8168_mac_ocp_write(tp, 0xe860, data);
6120}
6121
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006122static void rtl_hw_start_8168(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01006123{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006124 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02006125
Heiner Kallweit0ae09742018-04-28 22:19:26 +02006126 tp->cp_cmd &= ~INTT_MASK;
6127 tp->cp_cmd |= PktCntrDisable | INTT_1;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006128 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu2dd99532007-06-11 23:22:52 +02006129
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006130 RTL_W16(tp, IntrMitigate, 0x5151);
Francois Romieu0e485152007-02-20 00:00:26 +01006131
6132 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00006133 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006134 tp->event_slow |= RxFIFOOver | PCSTimeout;
6135 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01006136 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006137
Francois Romieu219a1e92008-06-28 11:58:39 +02006138 switch (tp->mac_version) {
6139 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006140 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006141 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006142
6143 case RTL_GIGA_MAC_VER_12:
6144 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006145 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006146 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006147
6148 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006149 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006150 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006151
6152 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006153 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006154 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006155
6156 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006157 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006158 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006159
Francois Romieu197ff762008-06-28 13:16:02 +02006160 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006161 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006162 break;
Francois Romieu197ff762008-06-28 13:16:02 +02006163
Francois Romieu6fb07052008-06-29 11:54:28 +02006164 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006165 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006166 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02006167
Francois Romieuef3386f2008-06-29 12:24:30 +02006168 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006169 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006170 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02006171
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006172 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006173 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006174 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006175
Francois Romieu5b538df2008-07-20 16:22:45 +02006176 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00006177 case RTL_GIGA_MAC_VER_26:
6178 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006179 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006180 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02006181
françois romieue6de30d2011-01-03 15:08:37 +00006182 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006183 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006184 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02006185
hayeswang4804b3b2011-03-21 01:50:29 +00006186 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006187 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006188 break;
6189
hayeswang01dc7fe2011-03-21 01:50:28 +00006190 case RTL_GIGA_MAC_VER_32:
6191 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006192 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006193 break;
6194 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006195 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006196 break;
françois romieue6de30d2011-01-03 15:08:37 +00006197
Hayes Wangc2218922011-09-06 16:55:18 +08006198 case RTL_GIGA_MAC_VER_35:
6199 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006200 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006201 break;
6202
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006203 case RTL_GIGA_MAC_VER_38:
6204 rtl_hw_start_8411(tp);
6205 break;
6206
Hayes Wangc5583862012-07-02 17:23:22 +08006207 case RTL_GIGA_MAC_VER_40:
6208 case RTL_GIGA_MAC_VER_41:
6209 rtl_hw_start_8168g_1(tp);
6210 break;
hayeswang57538c42013-04-01 22:23:40 +00006211 case RTL_GIGA_MAC_VER_42:
6212 rtl_hw_start_8168g_2(tp);
6213 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006214
hayeswang45dd95c2013-07-08 17:09:01 +08006215 case RTL_GIGA_MAC_VER_44:
6216 rtl_hw_start_8411_2(tp);
6217 break;
6218
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006219 case RTL_GIGA_MAC_VER_45:
6220 case RTL_GIGA_MAC_VER_46:
6221 rtl_hw_start_8168h_1(tp);
6222 break;
6223
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006224 case RTL_GIGA_MAC_VER_49:
6225 rtl_hw_start_8168ep_1(tp);
6226 break;
6227
6228 case RTL_GIGA_MAC_VER_50:
6229 rtl_hw_start_8168ep_2(tp);
6230 break;
6231
6232 case RTL_GIGA_MAC_VER_51:
6233 rtl_hw_start_8168ep_3(tp);
6234 break;
6235
Francois Romieu219a1e92008-06-28 11:58:39 +02006236 default:
6237 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006238 tp->dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00006239 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006240 }
Francois Romieu07ce4062007-02-23 23:36:39 +01006241}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006243static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006244{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006245 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02006246 { 0x01, 0, 0x6e65 },
6247 { 0x02, 0, 0x091f },
6248 { 0x03, 0, 0xc2f9 },
6249 { 0x06, 0, 0xafb5 },
6250 { 0x07, 0, 0x0e00 },
6251 { 0x19, 0, 0xec80 },
6252 { 0x01, 0, 0x2e65 },
6253 { 0x01, 0, 0x6e65 }
6254 };
6255 u8 cfg1;
6256
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006257 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006258
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006259 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006260
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006261 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006262
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006263 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02006264 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006265 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006266
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006267 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006268 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006269 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006270
Francois Romieufdf6fc02012-07-06 22:40:38 +02006271 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02006272}
6273
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006274static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006275{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006276 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006277
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006278 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006279
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006280 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
6281 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006282}
6283
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006284static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006285{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006286 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006287
Francois Romieufdf6fc02012-07-06 22:40:38 +02006288 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006289}
6290
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006291static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006292{
6293 static const struct ephy_info e_info_8105e_1[] = {
6294 { 0x07, 0, 0x4000 },
6295 { 0x19, 0, 0x0200 },
6296 { 0x19, 0, 0x0020 },
6297 { 0x1e, 0, 0x2000 },
6298 { 0x03, 0, 0x0001 },
6299 { 0x19, 0, 0x0100 },
6300 { 0x19, 0, 0x0004 },
6301 { 0x0a, 0, 0x0020 }
6302 };
6303
Francois Romieucecb5fd2011-04-01 10:21:07 +02006304 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006305 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006306
Francois Romieucecb5fd2011-04-01 10:21:07 +02006307 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006308 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006309
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006310 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6311 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006312
Francois Romieufdf6fc02012-07-06 22:40:38 +02006313 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08006314
6315 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006316}
6317
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006318static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006319{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006320 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006321 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006322}
6323
Hayes Wang7e18dca2012-03-30 14:33:02 +08006324static void rtl_hw_start_8402(struct rtl8169_private *tp)
6325{
Hayes Wang7e18dca2012-03-30 14:33:02 +08006326 static const struct ephy_info e_info_8402[] = {
6327 { 0x19, 0xffff, 0xff64 },
6328 { 0x1e, 0, 0x4000 }
6329 };
6330
6331 rtl_csi_access_enable_2(tp);
6332
6333 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006334 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006335
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006336 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
6337 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006338
Francois Romieufdf6fc02012-07-06 22:40:38 +02006339 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006340
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006341 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006342
Francois Romieufdf6fc02012-07-06 22:40:38 +02006343 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6344 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006345 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6346 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006347 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6348 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006349 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006350
6351 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006352}
6353
Hayes Wang5598bfe2012-07-02 17:23:21 +08006354static void rtl_hw_start_8106(struct rtl8169_private *tp)
6355{
Hayes Wang5598bfe2012-07-02 17:23:21 +08006356 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006357 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006358
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006359 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
6360 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6361 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006362
6363 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006364}
6365
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006366static void rtl_hw_start_8101(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01006367{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006368 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6369 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006370
Francois Romieucecb5fd2011-04-01 10:21:07 +02006371 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006372 tp->mac_version == RTL_GIGA_MAC_VER_16)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006373 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006374 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006375
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006376 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00006377
Heiner Kallweit12d42c52018-04-28 22:19:30 +02006378 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006379 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
hayeswang1a964642013-04-01 22:23:41 +00006380
Francois Romieu2857ffb2008-08-02 21:08:49 +02006381 switch (tp->mac_version) {
6382 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006383 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006384 break;
6385
6386 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006387 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006388 break;
6389
6390 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006391 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006392 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006393
6394 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006395 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006396 break;
6397 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006398 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006399 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006400
6401 case RTL_GIGA_MAC_VER_37:
6402 rtl_hw_start_8402(tp);
6403 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006404
6405 case RTL_GIGA_MAC_VER_39:
6406 rtl_hw_start_8106(tp);
6407 break;
hayeswang58152cd2013-04-01 22:23:42 +00006408 case RTL_GIGA_MAC_VER_43:
6409 rtl_hw_start_8168g_2(tp);
6410 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006411 case RTL_GIGA_MAC_VER_47:
6412 case RTL_GIGA_MAC_VER_48:
6413 rtl_hw_start_8168h_1(tp);
6414 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006415 }
6416
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006417 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418}
6419
6420static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6421{
Francois Romieud58d46b2011-05-03 16:38:29 +02006422 struct rtl8169_private *tp = netdev_priv(dev);
6423
Francois Romieud58d46b2011-05-03 16:38:29 +02006424 if (new_mtu > ETH_DATA_LEN)
6425 rtl_hw_jumbo_enable(tp);
6426 else
6427 rtl_hw_jumbo_disable(tp);
6428
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006430 netdev_update_features(dev);
6431
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433}
6434
6435static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6436{
Al Viro95e09182007-12-22 18:55:39 +00006437 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6439}
6440
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006441static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6442 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443{
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006444 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
6445 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006446
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006447 kfree(*data_buff);
6448 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449 rtl8169_make_unusable_by_asic(desc);
6450}
6451
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006452static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453{
6454 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6455
Alexander Duycka0750132014-12-11 15:02:17 -08006456 /* Force memory writes to complete before releasing descriptor */
6457 dma_wmb();
6458
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006459 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460}
6461
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006462static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006464 return (void *)ALIGN((long)data, 16);
6465}
6466
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006467static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6468 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006469{
6470 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 dma_addr_t mapping;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006472 struct device *d = tp_to_dev(tp);
Heiner Kallweitd3b404c2018-04-17 23:22:14 +02006473 int node = dev_to_node(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006475 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006476 if (!data)
6477 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006478
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006479 if (rtl8169_align(data) != data) {
6480 kfree(data);
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006481 data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006482 if (!data)
6483 return NULL;
6484 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006485
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006486 mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006487 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006488 if (unlikely(dma_mapping_error(d, mapping))) {
6489 if (net_ratelimit())
6490 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006491 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493
Heiner Kallweitd731af72018-04-17 23:26:41 +02006494 desc->addr = cpu_to_le64(mapping);
6495 rtl8169_mark_to_asic(desc);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006496 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006497
6498err_out:
6499 kfree(data);
6500 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501}
6502
6503static void rtl8169_rx_clear(struct rtl8169_private *tp)
6504{
Francois Romieu07d3f512007-02-21 22:40:46 +01006505 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506
6507 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006508 if (tp->Rx_databuff[i]) {
6509 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510 tp->RxDescArray + i);
6511 }
6512 }
6513}
6514
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006515static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006517 desc->opts1 |= cpu_to_le32(RingEnd);
6518}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006519
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006520static int rtl8169_rx_fill(struct rtl8169_private *tp)
6521{
6522 unsigned int i;
6523
6524 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006525 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006526
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006527 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006528 if (!data) {
6529 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006530 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006531 }
6532 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006535 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6536 return 0;
6537
6538err_out:
6539 rtl8169_rx_clear(tp);
6540 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541}
6542
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006543static int rtl8169_init_ring(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545 rtl8169_init_ring_indexes(tp);
6546
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006547 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
6548 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006550 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551}
6552
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006553static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554 struct TxDesc *desc)
6555{
6556 unsigned int len = tx_skb->len;
6557
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006558 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6559
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 desc->opts1 = 0x00;
6561 desc->opts2 = 0x00;
6562 desc->addr = 0x00;
6563 tx_skb->len = 0;
6564}
6565
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006566static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6567 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568{
6569 unsigned int i;
6570
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006571 for (i = 0; i < n; i++) {
6572 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 struct ring_info *tx_skb = tp->tx_skb + entry;
6574 unsigned int len = tx_skb->len;
6575
6576 if (len) {
6577 struct sk_buff *skb = tx_skb->skb;
6578
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006579 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 tp->TxDescArray + entry);
6581 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006582 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583 tx_skb->skb = NULL;
6584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 }
6586 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006587}
6588
6589static void rtl8169_tx_clear(struct rtl8169_private *tp)
6590{
6591 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 tp->cur_tx = tp->dirty_tx = 0;
6593}
6594
Francois Romieu4422bcd2012-01-26 11:23:32 +01006595static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596{
David Howellsc4028952006-11-22 14:57:56 +00006597 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01006598 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599
Francois Romieuda78dbf2012-01-26 14:18:23 +01006600 napi_disable(&tp->napi);
6601 netif_stop_queue(dev);
6602 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603
françois romieuc7c2c392011-12-04 20:30:52 +00006604 rtl8169_hw_reset(tp);
6605
Francois Romieu56de4142011-03-15 17:29:31 +01006606 for (i = 0; i < NUM_RX_DESC; i++)
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006607 rtl8169_mark_to_asic(tp->RxDescArray + i);
Francois Romieu56de4142011-03-15 17:29:31 +01006608
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00006610 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611
Francois Romieuda78dbf2012-01-26 14:18:23 +01006612 napi_enable(&tp->napi);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006613 rtl_hw_start(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01006614 netif_wake_queue(dev);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006615 rtl8169_check_link_status(dev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616}
6617
6618static void rtl8169_tx_timeout(struct net_device *dev)
6619{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006620 struct rtl8169_private *tp = netdev_priv(dev);
6621
6622 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623}
6624
6625static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07006626 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627{
6628 struct skb_shared_info *info = skb_shinfo(skb);
6629 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006630 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006631 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632
6633 entry = tp->cur_tx;
6634 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006635 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 dma_addr_t mapping;
6637 u32 status, len;
6638 void *addr;
6639
6640 entry = (entry + 1) % NUM_TX_DESC;
6641
6642 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00006643 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00006644 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006645 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006646 if (unlikely(dma_mapping_error(d, mapping))) {
6647 if (net_ratelimit())
6648 netif_err(tp, drv, tp->dev,
6649 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006650 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652
Francois Romieucecb5fd2011-04-01 10:21:07 +02006653 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006654 status = opts[0] | len |
6655 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656
6657 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07006658 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 txd->addr = cpu_to_le64(mapping);
6660
6661 tp->tx_skb[entry].len = len;
6662 }
6663
6664 if (cur_frag) {
6665 tp->tx_skb[entry].skb = skb;
6666 txd->opts1 |= cpu_to_le32(LastFrag);
6667 }
6668
6669 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006670
6671err_out:
6672 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6673 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674}
6675
françois romieub423e9a2013-05-18 01:24:46 +00006676static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6677{
6678 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6679}
6680
hayeswange9746042014-07-11 16:25:58 +08006681static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6682 struct net_device *dev);
6683/* r8169_csum_workaround()
6684 * The hw limites the value the transport offset. When the offset is out of the
6685 * range, calculate the checksum by sw.
6686 */
6687static void r8169_csum_workaround(struct rtl8169_private *tp,
6688 struct sk_buff *skb)
6689{
6690 if (skb_shinfo(skb)->gso_size) {
6691 netdev_features_t features = tp->dev->features;
6692 struct sk_buff *segs, *nskb;
6693
6694 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6695 segs = skb_gso_segment(skb, features);
6696 if (IS_ERR(segs) || !segs)
6697 goto drop;
6698
6699 do {
6700 nskb = segs;
6701 segs = segs->next;
6702 nskb->next = NULL;
6703 rtl8169_start_xmit(nskb, tp->dev);
6704 } while (segs);
6705
Alexander Duyckeb781392015-05-01 10:34:44 -07006706 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006707 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6708 if (skb_checksum_help(skb) < 0)
6709 goto drop;
6710
6711 rtl8169_start_xmit(skb, tp->dev);
6712 } else {
6713 struct net_device_stats *stats;
6714
6715drop:
6716 stats = &tp->dev->stats;
6717 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07006718 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006719 }
6720}
6721
6722/* msdn_giant_send_check()
6723 * According to the document of microsoft, the TCP Pseudo Header excludes the
6724 * packet length for IPv6 TCP large packets.
6725 */
6726static int msdn_giant_send_check(struct sk_buff *skb)
6727{
6728 const struct ipv6hdr *ipv6h;
6729 struct tcphdr *th;
6730 int ret;
6731
6732 ret = skb_cow_head(skb, 0);
6733 if (ret)
6734 return ret;
6735
6736 ipv6h = ipv6_hdr(skb);
6737 th = tcp_hdr(skb);
6738
6739 th->check = 0;
6740 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6741
6742 return ret;
6743}
6744
6745static inline __be16 get_protocol(struct sk_buff *skb)
6746{
6747 __be16 protocol;
6748
6749 if (skb->protocol == htons(ETH_P_8021Q))
6750 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
6751 else
6752 protocol = skb->protocol;
6753
6754 return protocol;
6755}
6756
hayeswang5888d3f2014-07-11 16:25:56 +08006757static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6758 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759{
Michał Mirosław350fb322011-04-08 06:35:56 +00006760 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761
Francois Romieu2b7b4312011-04-18 22:53:24 -07006762 if (mss) {
6763 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08006764 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6765 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6766 const struct iphdr *ip = ip_hdr(skb);
6767
6768 if (ip->protocol == IPPROTO_TCP)
6769 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6770 else if (ip->protocol == IPPROTO_UDP)
6771 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6772 else
6773 WARN_ON_ONCE(1);
6774 }
6775
6776 return true;
6777}
6778
6779static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6780 struct sk_buff *skb, u32 *opts)
6781{
hayeswangbdfa4ed2014-07-11 16:25:57 +08006782 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08006783 u32 mss = skb_shinfo(skb)->gso_size;
6784
6785 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08006786 if (transport_offset > GTTCPHO_MAX) {
6787 netif_warn(tp, tx_err, tp->dev,
6788 "Invalid transport offset 0x%x for TSO\n",
6789 transport_offset);
6790 return false;
6791 }
6792
6793 switch (get_protocol(skb)) {
6794 case htons(ETH_P_IP):
6795 opts[0] |= TD1_GTSENV4;
6796 break;
6797
6798 case htons(ETH_P_IPV6):
6799 if (msdn_giant_send_check(skb))
6800 return false;
6801
6802 opts[0] |= TD1_GTSENV6;
6803 break;
6804
6805 default:
6806 WARN_ON_ONCE(1);
6807 break;
6808 }
6809
hayeswangbdfa4ed2014-07-11 16:25:57 +08006810 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08006811 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006812 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08006813 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814
françois romieub423e9a2013-05-18 01:24:46 +00006815 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006816 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00006817
hayeswange9746042014-07-11 16:25:58 +08006818 if (transport_offset > TCPHO_MAX) {
6819 netif_warn(tp, tx_err, tp->dev,
6820 "Invalid transport offset 0x%x\n",
6821 transport_offset);
6822 return false;
6823 }
6824
6825 switch (get_protocol(skb)) {
6826 case htons(ETH_P_IP):
6827 opts[1] |= TD1_IPv4_CS;
6828 ip_protocol = ip_hdr(skb)->protocol;
6829 break;
6830
6831 case htons(ETH_P_IPV6):
6832 opts[1] |= TD1_IPv6_CS;
6833 ip_protocol = ipv6_hdr(skb)->nexthdr;
6834 break;
6835
6836 default:
6837 ip_protocol = IPPROTO_RAW;
6838 break;
6839 }
6840
6841 if (ip_protocol == IPPROTO_TCP)
6842 opts[1] |= TD1_TCP_CS;
6843 else if (ip_protocol == IPPROTO_UDP)
6844 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006845 else
6846 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08006847
6848 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00006849 } else {
6850 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006851 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852 }
hayeswang5888d3f2014-07-11 16:25:56 +08006853
françois romieub423e9a2013-05-18 01:24:46 +00006854 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855}
6856
Stephen Hemminger613573252009-08-31 19:50:58 +00006857static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6858 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859{
6860 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006861 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006863 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864 dma_addr_t mapping;
6865 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006866 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006867 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02006868
Julien Ducourthial477206a2012-05-09 00:00:06 +02006869 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006870 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006871 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872 }
6873
6874 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006875 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876
françois romieub423e9a2013-05-18 01:24:46 +00006877 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6878 opts[0] = DescOwn;
6879
hayeswange9746042014-07-11 16:25:58 +08006880 if (!tp->tso_csum(tp, skb, opts)) {
6881 r8169_csum_workaround(tp, skb);
6882 return NETDEV_TX_OK;
6883 }
françois romieub423e9a2013-05-18 01:24:46 +00006884
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006885 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006886 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006887 if (unlikely(dma_mapping_error(d, mapping))) {
6888 if (net_ratelimit())
6889 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006890 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892
6893 tp->tx_skb[entry].len = len;
6894 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895
Francois Romieu2b7b4312011-04-18 22:53:24 -07006896 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006897 if (frags < 0)
6898 goto err_dma_1;
6899 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07006900 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006901 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07006902 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006903 tp->tx_skb[entry].skb = skb;
6904 }
6905
Francois Romieu2b7b4312011-04-18 22:53:24 -07006906 txd->opts2 = cpu_to_le32(opts[1]);
6907
Richard Cochran5047fb52012-03-10 07:29:42 +00006908 skb_tx_timestamp(skb);
6909
Alexander Duycka0750132014-12-11 15:02:17 -08006910 /* Force memory writes to complete before releasing descriptor */
6911 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912
Francois Romieucecb5fd2011-04-01 10:21:07 +02006913 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006914 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915 txd->opts1 = cpu_to_le32(status);
6916
Alexander Duycka0750132014-12-11 15:02:17 -08006917 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00006918 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919
Alexander Duycka0750132014-12-11 15:02:17 -08006920 tp->cur_tx += frags + 1;
6921
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006922 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923
David S. Miller87cda7c2015-02-22 15:54:29 -05006924 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01006925
David S. Miller87cda7c2015-02-22 15:54:29 -05006926 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01006927 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6928 * not miss a ring update when it notices a stopped queue.
6929 */
6930 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01006932 /* Sync with rtl_tx:
6933 * - publish queue status and cur_tx ring index (write barrier)
6934 * - refresh dirty_tx ring index (read barrier).
6935 * May the current thread have a pessimistic view of the ring
6936 * status and forget to wake up queue, a racing rtl_tx thread
6937 * can't.
6938 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006939 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02006940 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 netif_wake_queue(dev);
6942 }
6943
Stephen Hemminger613573252009-08-31 19:50:58 +00006944 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006946err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006947 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006948err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006949 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006950 dev->stats.tx_dropped++;
6951 return NETDEV_TX_OK;
6952
6953err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006955 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00006956 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957}
6958
6959static void rtl8169_pcierr_interrupt(struct net_device *dev)
6960{
6961 struct rtl8169_private *tp = netdev_priv(dev);
6962 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963 u16 pci_status, pci_cmd;
6964
6965 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6966 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6967
Joe Perchesbf82c182010-02-09 11:49:50 +00006968 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6969 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970
6971 /*
6972 * The recovery sequence below admits a very elaborated explanation:
6973 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01006974 * - I did not see what else could be done;
6975 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976 *
6977 * Feel free to adjust to your needs.
6978 */
Francois Romieua27993f2006-12-18 00:04:19 +01006979 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01006980 pci_cmd &= ~PCI_COMMAND_PARITY;
6981 else
6982 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6983
6984 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985
6986 pci_write_config_word(pdev, PCI_STATUS,
6987 pci_status & (PCI_STATUS_DETECTED_PARITY |
6988 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6989 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6990
6991 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00006992 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006993 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994 tp->cp_cmd &= ~PCIDAC;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006995 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997 }
6998
françois romieue6de30d2011-01-03 15:08:37 +00006999 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01007000
Francois Romieu98ddf982012-01-31 10:47:34 +01007001 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002}
7003
Francois Romieuda78dbf2012-01-26 14:18:23 +01007004static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005{
7006 unsigned int dirty_tx, tx_left;
7007
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008 dirty_tx = tp->dirty_tx;
7009 smp_rmb();
7010 tx_left = tp->cur_tx - dirty_tx;
7011
7012 while (tx_left > 0) {
7013 unsigned int entry = dirty_tx % NUM_TX_DESC;
7014 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015 u32 status;
7016
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7018 if (status & DescOwn)
7019 break;
7020
Alexander Duycka0750132014-12-11 15:02:17 -08007021 /* This barrier is needed to keep us from reading
7022 * any other fields out of the Tx descriptor until
7023 * we know the status of DescOwn
7024 */
7025 dma_rmb();
7026
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01007027 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007028 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05007030 u64_stats_update_begin(&tp->tx_stats.syncp);
7031 tp->tx_stats.packets++;
7032 tp->tx_stats.bytes += tx_skb->skb->len;
7033 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07007034 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035 tx_skb->skb = NULL;
7036 }
7037 dirty_tx++;
7038 tx_left--;
7039 }
7040
7041 if (tp->dirty_tx != dirty_tx) {
7042 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01007043 /* Sync with rtl8169_start_xmit:
7044 * - publish dirty_tx ring index (write barrier)
7045 * - refresh cur_tx ring index and queue status (read barrier)
7046 * May the current thread miss the stopped queue condition,
7047 * a racing xmit thread can only have a right view of the
7048 * ring status.
7049 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007050 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02007052 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053 netif_wake_queue(dev);
7054 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02007055 /*
7056 * 8168 hack: TxPoll requests are lost when the Tx packets are
7057 * too close. Let's kick an extra TxPoll request when a burst
7058 * of start_xmit activity is detected (if it is not detected,
7059 * it is slow enough). -- FR
7060 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007061 if (tp->cur_tx != dirty_tx)
7062 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063 }
7064}
7065
Francois Romieu126fa4b2005-05-12 20:09:17 -04007066static inline int rtl8169_fragmented_frame(u32 status)
7067{
7068 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7069}
7070
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007071static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 u32 status = opts1 & RxProtoMask;
7074
7075 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00007076 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077 skb->ip_summed = CHECKSUM_UNNECESSARY;
7078 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07007079 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080}
7081
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007082static struct sk_buff *rtl8169_try_rx_copy(void *data,
7083 struct rtl8169_private *tp,
7084 int pkt_size,
7085 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02007087 struct sk_buff *skb;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01007088 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007090 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007091 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007092 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08007093 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007094 if (skb)
Heiner Kallweit8a67aa82018-04-17 23:19:07 +02007095 skb_copy_to_linear_data(skb, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007096 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7097
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007098 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099}
7100
Francois Romieuda78dbf2012-01-26 14:18:23 +01007101static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102{
7103 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007104 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107
Timo Teräs9fba0812013-01-15 21:01:24 +00007108 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007110 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111 u32 status;
7112
Heiner Kallweit62028062018-04-17 23:30:29 +02007113 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114 if (status & DescOwn)
7115 break;
Alexander Duycka0750132014-12-11 15:02:17 -08007116
7117 /* This barrier is needed to keep us from reading
7118 * any other fields out of the Rx descriptor until
7119 * we know the status of DescOwn
7120 */
7121 dma_rmb();
7122
Richard Dawe4dcb7d32005-05-27 21:12:00 +02007123 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007124 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7125 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007126 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02007128 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02007130 dev->stats.rx_crc_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02007131 /* RxFOVF is a reserved bit on later chip versions */
7132 if (tp->mac_version == RTL_GIGA_MAC_VER_01 &&
7133 status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01007134 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007135 dev->stats.rx_fifo_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02007136 } else if (status & (RxRUNT | RxCRC) &&
7137 !(status & RxRWT) &&
7138 dev->features & NETIF_F_RXALL) {
Ben Greear6bbe0212012-02-10 15:04:33 +00007139 goto process_pkt;
Heiner Kallweit62028062018-04-17 23:30:29 +02007140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007142 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00007143 dma_addr_t addr;
7144 int pkt_size;
7145
7146process_pkt:
7147 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00007148 if (likely(!(dev->features & NETIF_F_RXFCS)))
7149 pkt_size = (status & 0x00003fff) - 4;
7150 else
7151 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152
Francois Romieu126fa4b2005-05-12 20:09:17 -04007153 /*
7154 * The driver does not support incoming fragmented
7155 * frames. They are seen as a symptom of over-mtu
7156 * sized frames.
7157 */
7158 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02007159 dev->stats.rx_dropped++;
7160 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00007161 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007162 }
7163
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007164 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7165 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007166 if (!skb) {
7167 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00007168 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169 }
7170
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007171 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172 skb_put(skb, pkt_size);
7173 skb->protocol = eth_type_trans(skb, dev);
7174
Francois Romieu7a8fc772011-03-01 17:18:33 +01007175 rtl8169_rx_vlan_tag(desc, skb);
7176
françois romieu39174292015-11-11 23:35:18 +01007177 if (skb->pkt_type == PACKET_MULTICAST)
7178 dev->stats.multicast++;
7179
Francois Romieu56de4142011-03-15 17:29:31 +01007180 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181
Junchang Wang8027aa22012-03-04 23:30:32 +01007182 u64_stats_update_begin(&tp->rx_stats.syncp);
7183 tp->rx_stats.packets++;
7184 tp->rx_stats.bytes += pkt_size;
7185 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186 }
françois romieuce11ff52013-01-24 13:30:06 +00007187release_descriptor:
7188 desc->opts2 = 0;
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02007189 rtl8169_mark_to_asic(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190 }
7191
7192 count = cur_rx - tp->cur_rx;
7193 tp->cur_rx = cur_rx;
7194
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195 return count;
7196}
7197
Francois Romieu07d3f512007-02-21 22:40:46 +01007198static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199{
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02007200 struct rtl8169_private *tp = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007202 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007204 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007205 if (status && status != 0xffff) {
7206 status &= RTL_EVENT_NAPI | tp->event_slow;
7207 if (status) {
7208 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00007209
Francois Romieuda78dbf2012-01-26 14:18:23 +01007210 rtl_irq_disable(tp);
Heiner Kallweit9a899a32018-04-17 23:21:01 +02007211 napi_schedule_irqoff(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214 return IRQ_RETVAL(handled);
7215}
7216
Francois Romieuda78dbf2012-01-26 14:18:23 +01007217/*
7218 * Workqueue context.
7219 */
7220static void rtl_slow_event_work(struct rtl8169_private *tp)
7221{
7222 struct net_device *dev = tp->dev;
7223 u16 status;
7224
7225 status = rtl_get_events(tp) & tp->event_slow;
7226 rtl_ack_events(tp, status);
7227
7228 if (unlikely(status & RxFIFOOver)) {
7229 switch (tp->mac_version) {
7230 /* Work around for rx fifo overflow */
7231 case RTL_GIGA_MAC_VER_11:
7232 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01007233 /* XXX - Hack alert. See rtl_task(). */
7234 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007235 default:
7236 break;
7237 }
7238 }
7239
7240 if (unlikely(status & SYSErr))
7241 rtl8169_pcierr_interrupt(dev);
7242
7243 if (status & LinkChg)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007244 rtl8169_check_link_status(dev, tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007245
françois romieu7dbb4912012-06-09 10:53:16 +00007246 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007247}
7248
Francois Romieu4422bcd2012-01-26 11:23:32 +01007249static void rtl_task(struct work_struct *work)
7250{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007251 static const struct {
7252 int bitnr;
7253 void (*action)(struct rtl8169_private *);
7254 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01007255 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007256 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7257 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7258 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7259 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01007260 struct rtl8169_private *tp =
7261 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007262 struct net_device *dev = tp->dev;
7263 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01007264
Francois Romieuda78dbf2012-01-26 14:18:23 +01007265 rtl_lock_work(tp);
7266
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007267 if (!netif_running(dev) ||
7268 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01007269 goto out_unlock;
7270
7271 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7272 bool pending;
7273
Francois Romieuda78dbf2012-01-26 14:18:23 +01007274 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007275 if (pending)
7276 rtl_work[i].action(tp);
7277 }
7278
7279out_unlock:
7280 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01007281}
7282
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007283static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007285 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7286 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007287 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7288 int work_done= 0;
7289 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290
Francois Romieuda78dbf2012-01-26 14:18:23 +01007291 status = rtl_get_events(tp);
7292 rtl_ack_events(tp, status & ~tp->event_slow);
7293
7294 if (status & RTL_EVENT_NAPI_RX)
7295 work_done = rtl_rx(dev, tp, (u32) budget);
7296
7297 if (status & RTL_EVENT_NAPI_TX)
7298 rtl_tx(dev, tp);
7299
7300 if (status & tp->event_slow) {
7301 enable_mask &= ~tp->event_slow;
7302
7303 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007306 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08007307 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00007308
Francois Romieuda78dbf2012-01-26 14:18:23 +01007309 rtl_irq_enable(tp, enable_mask);
7310 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311 }
7312
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007313 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007315
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007316static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02007317{
7318 struct rtl8169_private *tp = netdev_priv(dev);
7319
7320 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7321 return;
7322
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007323 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
7324 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02007325}
7326
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327static void rtl8169_down(struct net_device *dev)
7328{
7329 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330
Francois Romieu4876cc12011-03-11 21:07:11 +01007331 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007333 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007334 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335
Hayes Wang92fc43b2011-07-06 15:58:03 +08007336 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007337 /*
7338 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007339 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7340 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007341 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007342 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007345 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347 rtl8169_tx_clear(tp);
7348
7349 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007350
7351 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352}
7353
7354static int rtl8169_close(struct net_device *dev)
7355{
7356 struct rtl8169_private *tp = netdev_priv(dev);
7357 struct pci_dev *pdev = tp->pci_dev;
7358
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007359 pm_runtime_get_sync(&pdev->dev);
7360
Francois Romieucecb5fd2011-04-01 10:21:07 +02007361 /* Update counters before going down */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007362 rtl8169_update_counters(tp);
Ivan Vecera355423d2009-02-06 21:49:57 -08007363
Francois Romieuda78dbf2012-01-26 14:18:23 +01007364 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007365 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007366
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007368 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369
Lekensteyn4ea72442013-07-22 09:53:30 +02007370 cancel_work_sync(&tp->wk.work);
7371
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02007372 pci_free_irq(pdev, 0, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007374 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7375 tp->RxPhyAddr);
7376 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7377 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378 tp->TxDescArray = NULL;
7379 tp->RxDescArray = NULL;
7380
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007381 pm_runtime_put_sync(&pdev->dev);
7382
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383 return 0;
7384}
7385
Francois Romieudc1c00c2012-03-08 10:06:18 +01007386#ifdef CONFIG_NET_POLL_CONTROLLER
7387static void rtl8169_netpoll(struct net_device *dev)
7388{
7389 struct rtl8169_private *tp = netdev_priv(dev);
7390
Heiner Kallweit29274992018-02-28 20:43:38 +01007391 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), dev);
Francois Romieudc1c00c2012-03-08 10:06:18 +01007392}
7393#endif
7394
Francois Romieudf43ac72012-03-08 09:48:40 +01007395static int rtl_open(struct net_device *dev)
7396{
7397 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007398 struct pci_dev *pdev = tp->pci_dev;
7399 int retval = -ENOMEM;
7400
7401 pm_runtime_get_sync(&pdev->dev);
7402
7403 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007404 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007405 * dma_alloc_coherent provides more.
7406 */
7407 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7408 &tp->TxPhyAddr, GFP_KERNEL);
7409 if (!tp->TxDescArray)
7410 goto err_pm_runtime_put;
7411
7412 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7413 &tp->RxPhyAddr, GFP_KERNEL);
7414 if (!tp->RxDescArray)
7415 goto err_free_tx_0;
7416
Heiner Kallweitb1127e62018-04-17 23:23:35 +02007417 retval = rtl8169_init_ring(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007418 if (retval < 0)
7419 goto err_free_rx_1;
7420
7421 INIT_WORK(&tp->wk.work, rtl_task);
7422
7423 smp_mb();
7424
7425 rtl_request_firmware(tp);
7426
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02007427 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007428 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01007429 if (retval < 0)
7430 goto err_release_fw_2;
7431
7432 rtl_lock_work(tp);
7433
7434 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7435
7436 napi_enable(&tp->napi);
7437
7438 rtl8169_init_phy(dev, tp);
7439
Francois Romieudf43ac72012-03-08 09:48:40 +01007440 rtl_pll_power_up(tp);
7441
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007442 rtl_hw_start(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007443
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007444 if (!rtl8169_init_counter_offsets(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007445 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7446
Francois Romieudf43ac72012-03-08 09:48:40 +01007447 netif_start_queue(dev);
7448
7449 rtl_unlock_work(tp);
7450
7451 tp->saved_wolopts = 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007452 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007453
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007454 rtl8169_check_link_status(dev, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007455out:
7456 return retval;
7457
7458err_release_fw_2:
7459 rtl_release_firmware(tp);
7460 rtl8169_rx_clear(tp);
7461err_free_rx_1:
7462 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7463 tp->RxPhyAddr);
7464 tp->RxDescArray = NULL;
7465err_free_tx_0:
7466 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7467 tp->TxPhyAddr);
7468 tp->TxDescArray = NULL;
7469err_pm_runtime_put:
7470 pm_runtime_put_noidle(&pdev->dev);
7471 goto out;
7472}
7473
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007474static void
Junchang Wang8027aa22012-03-04 23:30:32 +01007475rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476{
7477 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007478 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02007479 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01007480 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007482 pm_runtime_get_noresume(&pdev->dev);
7483
7484 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007485 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007486
Junchang Wang8027aa22012-03-04 23:30:32 +01007487 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007488 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007489 stats->rx_packets = tp->rx_stats.packets;
7490 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007491 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007492
Junchang Wang8027aa22012-03-04 23:30:32 +01007493 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007494 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007495 stats->tx_packets = tp->tx_stats.packets;
7496 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007497 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007498
7499 stats->rx_dropped = dev->stats.rx_dropped;
7500 stats->tx_dropped = dev->stats.tx_dropped;
7501 stats->rx_length_errors = dev->stats.rx_length_errors;
7502 stats->rx_errors = dev->stats.rx_errors;
7503 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7504 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7505 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02007506 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01007507
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007508 /*
7509 * Fetch additonal counter values missing in stats collected by driver
7510 * from tally counters.
7511 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007512 if (pm_runtime_active(&pdev->dev))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007513 rtl8169_update_counters(tp);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007514
7515 /*
7516 * Subtract values fetched during initalization.
7517 * See rtl8169_init_counter_offsets for a description why we do that.
7518 */
Corinna Vinschen42020322015-09-10 10:47:35 +02007519 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007520 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02007521 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007522 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02007523 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007524 le16_to_cpu(tp->tc_offset.tx_aborted);
7525
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007526 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527}
7528
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007529static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01007530{
françois romieu065c27c2011-01-03 15:08:12 +00007531 struct rtl8169_private *tp = netdev_priv(dev);
7532
Francois Romieu5d06a992006-02-23 00:47:58 +01007533 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007534 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01007535
7536 netif_device_detach(dev);
7537 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007538
7539 rtl_lock_work(tp);
7540 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007541 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007542 rtl_unlock_work(tp);
7543
7544 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007545}
Francois Romieu5d06a992006-02-23 00:47:58 +01007546
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007547#ifdef CONFIG_PM
7548
7549static int rtl8169_suspend(struct device *device)
7550{
7551 struct pci_dev *pdev = to_pci_dev(device);
7552 struct net_device *dev = pci_get_drvdata(pdev);
7553
7554 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02007555
Francois Romieu5d06a992006-02-23 00:47:58 +01007556 return 0;
7557}
7558
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007559static void __rtl8169_resume(struct net_device *dev)
7560{
françois romieu065c27c2011-01-03 15:08:12 +00007561 struct rtl8169_private *tp = netdev_priv(dev);
7562
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007563 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00007564
7565 rtl_pll_power_up(tp);
7566
Artem Savkovcff4c162012-04-03 10:29:11 +00007567 rtl_lock_work(tp);
7568 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007569 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00007570 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007571
Francois Romieu98ddf982012-01-31 10:47:34 +01007572 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007573}
7574
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007575static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01007576{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007577 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01007578 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007579 struct rtl8169_private *tp = netdev_priv(dev);
7580
7581 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01007582
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007583 if (netif_running(dev))
7584 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01007585
Francois Romieu5d06a992006-02-23 00:47:58 +01007586 return 0;
7587}
7588
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007589static int rtl8169_runtime_suspend(struct device *device)
7590{
7591 struct pci_dev *pdev = to_pci_dev(device);
7592 struct net_device *dev = pci_get_drvdata(pdev);
7593 struct rtl8169_private *tp = netdev_priv(dev);
7594
Heiner Kallweita92a0842018-01-08 21:39:13 +01007595 if (!tp->TxDescArray) {
7596 rtl_pll_power_down(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007597 return 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007598 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007599
Francois Romieuda78dbf2012-01-26 14:18:23 +01007600 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007601 tp->saved_wolopts = __rtl8169_get_wol(tp);
7602 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007603 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007604
7605 rtl8169_net_suspend(dev);
7606
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007607 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007608 rtl8169_rx_missed(dev);
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007609 rtl8169_update_counters(tp);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007610
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007611 return 0;
7612}
7613
7614static int rtl8169_runtime_resume(struct device *device)
7615{
7616 struct pci_dev *pdev = to_pci_dev(device);
7617 struct net_device *dev = pci_get_drvdata(pdev);
7618 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08007619 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007620
7621 if (!tp->TxDescArray)
7622 return 0;
7623
Francois Romieuda78dbf2012-01-26 14:18:23 +01007624 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007625 __rtl8169_set_wol(tp, tp->saved_wolopts);
7626 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007627 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007628
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007629 rtl8169_init_phy(dev, tp);
7630
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007631 __rtl8169_resume(dev);
7632
7633 return 0;
7634}
7635
7636static int rtl8169_runtime_idle(struct device *device)
7637{
7638 struct pci_dev *pdev = to_pci_dev(device);
7639 struct net_device *dev = pci_get_drvdata(pdev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007640
Heiner Kallweita92a0842018-01-08 21:39:13 +01007641 if (!netif_running(dev) || !netif_carrier_ok(dev))
7642 pm_schedule_suspend(device, 10000);
7643
7644 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007645}
7646
Alexey Dobriyan47145212009-12-14 18:00:08 -08007647static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02007648 .suspend = rtl8169_suspend,
7649 .resume = rtl8169_resume,
7650 .freeze = rtl8169_suspend,
7651 .thaw = rtl8169_resume,
7652 .poweroff = rtl8169_suspend,
7653 .restore = rtl8169_resume,
7654 .runtime_suspend = rtl8169_runtime_suspend,
7655 .runtime_resume = rtl8169_runtime_resume,
7656 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007657};
7658
7659#define RTL8169_PM_OPS (&rtl8169_pm_ops)
7660
7661#else /* !CONFIG_PM */
7662
7663#define RTL8169_PM_OPS NULL
7664
7665#endif /* !CONFIG_PM */
7666
David S. Miller1805b2f2011-10-24 18:18:09 -04007667static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7668{
David S. Miller1805b2f2011-10-24 18:18:09 -04007669 /* WoL fails with 8168b when the receiver is disabled. */
7670 switch (tp->mac_version) {
7671 case RTL_GIGA_MAC_VER_11:
7672 case RTL_GIGA_MAC_VER_12:
7673 case RTL_GIGA_MAC_VER_17:
7674 pci_clear_master(tp->pci_dev);
7675
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007676 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007677 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007678 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04007679 break;
7680 default:
7681 break;
7682 }
7683}
7684
Francois Romieu1765f952008-09-13 17:21:40 +02007685static void rtl_shutdown(struct pci_dev *pdev)
7686{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007687 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00007688 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02007689
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007690 rtl8169_net_suspend(dev);
7691
Francois Romieucecb5fd2011-04-01 10:21:07 +02007692 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08007693 rtl_rar_set(tp, dev->perm_addr);
7694
Hayes Wang92fc43b2011-07-06 15:58:03 +08007695 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00007696
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007697 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04007698 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
7699 rtl_wol_suspend_quirk(tp);
7700 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00007701 }
7702
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007703 pci_wake_from_d3(pdev, true);
7704 pci_set_power_state(pdev, PCI_D3hot);
7705 }
7706}
Francois Romieu5d06a992006-02-23 00:47:58 +01007707
Bill Pembertonbaf63292012-12-03 09:23:28 -05007708static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01007709{
7710 struct net_device *dev = pci_get_drvdata(pdev);
7711 struct rtl8169_private *tp = netdev_priv(dev);
7712
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007713 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01007714 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01007715
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007716 netif_napi_del(&tp->napi);
7717
Francois Romieue27566e2012-03-08 09:54:01 +01007718 unregister_netdev(dev);
7719
7720 rtl_release_firmware(tp);
7721
7722 if (pci_dev_run_wake(pdev))
7723 pm_runtime_get_noresume(&pdev->dev);
7724
7725 /* restore original MAC address */
7726 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01007727}
7728
Francois Romieufa9c3852012-03-08 10:01:50 +01007729static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01007730 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01007731 .ndo_stop = rtl8169_close,
7732 .ndo_get_stats64 = rtl8169_get_stats64,
7733 .ndo_start_xmit = rtl8169_start_xmit,
7734 .ndo_tx_timeout = rtl8169_tx_timeout,
7735 .ndo_validate_addr = eth_validate_addr,
7736 .ndo_change_mtu = rtl8169_change_mtu,
7737 .ndo_fix_features = rtl8169_fix_features,
7738 .ndo_set_features = rtl8169_set_features,
7739 .ndo_set_mac_address = rtl_set_mac_address,
7740 .ndo_do_ioctl = rtl8169_ioctl,
7741 .ndo_set_rx_mode = rtl_set_rx_mode,
7742#ifdef CONFIG_NET_POLL_CONTROLLER
7743 .ndo_poll_controller = rtl8169_netpoll,
7744#endif
7745
7746};
7747
Francois Romieu31fa8b12012-03-08 10:09:40 +01007748static const struct rtl_cfg_info {
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007749 void (*hw_start)(struct rtl8169_private *tp);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007750 u16 event_slow;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007751 unsigned int has_gmii:1;
Francois Romieu50970832017-10-27 13:24:49 +03007752 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007753 u8 default_ver;
7754} rtl_cfg_infos [] = {
7755 [RTL_CFG_0] = {
7756 .hw_start = rtl_hw_start_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007757 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007758 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007759 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007760 .default_ver = RTL_GIGA_MAC_VER_01,
7761 },
7762 [RTL_CFG_1] = {
7763 .hw_start = rtl_hw_start_8168,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007764 .event_slow = SYSErr | LinkChg | RxOverflow,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007765 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007766 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007767 .default_ver = RTL_GIGA_MAC_VER_11,
7768 },
7769 [RTL_CFG_2] = {
7770 .hw_start = rtl_hw_start_8101,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007771 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
7772 PCSTimeout,
Francois Romieu50970832017-10-27 13:24:49 +03007773 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007774 .default_ver = RTL_GIGA_MAC_VER_13,
7775 }
7776};
7777
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007778static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01007779{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007780 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007781
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007782 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007783 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
7784 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
7785 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007786 flags = PCI_IRQ_LEGACY;
7787 } else {
7788 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007789 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007790
7791 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007792}
7793
Hayes Wangc5583862012-07-02 17:23:22 +08007794DECLARE_RTL_COND(rtl_link_list_ready_cond)
7795{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007796 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08007797}
7798
7799DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7800{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007801 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08007802}
7803
Bill Pembertonbaf63292012-12-03 09:23:28 -05007804static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007805{
Hayes Wangc5583862012-07-02 17:23:22 +08007806 u32 data;
7807
7808 tp->ocp_base = OCP_STD_PHY_BASE;
7809
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007810 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08007811
7812 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7813 return;
7814
7815 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7816 return;
7817
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007818 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08007819 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007820 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08007821
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007822 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007823 data &= ~(1 << 14);
7824 r8168_mac_ocp_write(tp, 0xe8de, data);
7825
7826 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7827 return;
7828
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007829 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007830 data |= (1 << 15);
7831 r8168_mac_ocp_write(tp, 0xe8de, data);
7832
7833 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7834 return;
7835}
7836
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007837static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
7838{
7839 rtl8168ep_stop_cmac(tp);
7840 rtl_hw_init_8168g(tp);
7841}
7842
Bill Pembertonbaf63292012-12-03 09:23:28 -05007843static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007844{
7845 switch (tp->mac_version) {
7846 case RTL_GIGA_MAC_VER_40:
7847 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00007848 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00007849 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08007850 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007851 case RTL_GIGA_MAC_VER_45:
7852 case RTL_GIGA_MAC_VER_46:
7853 case RTL_GIGA_MAC_VER_47:
7854 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007855 rtl_hw_init_8168g(tp);
7856 break;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08007857 case RTL_GIGA_MAC_VER_49:
7858 case RTL_GIGA_MAC_VER_50:
7859 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007860 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08007861 break;
Hayes Wangc5583862012-07-02 17:23:22 +08007862 default:
7863 break;
7864 }
7865}
7866
hayeswang929a0312014-09-16 11:40:47 +08007867static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01007868{
7869 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007870 struct rtl8169_private *tp;
7871 struct mii_if_info *mii;
7872 struct net_device *dev;
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007873 int chipset, region, i;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007874 int rc;
7875
7876 if (netif_msg_drv(&debug)) {
7877 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
7878 MODULENAME, RTL8169_VERSION);
7879 }
7880
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007881 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
7882 if (!dev)
7883 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007884
7885 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01007886 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007887 tp = netdev_priv(dev);
7888 tp->dev = dev;
7889 tp->pci_dev = pdev;
7890 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
7891
7892 mii = &tp->mii;
7893 mii->dev = dev;
7894 mii->mdio_read = rtl_mdio_read;
7895 mii->mdio_write = rtl_mdio_write;
7896 mii->phy_id_mask = 0x1f;
7897 mii->reg_num_mask = 0x1f;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007898 mii->supports_gmii = cfg->has_gmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007899
7900 /* disable ASPM completely as that cause random device stop working
7901 * problems as well as full system hangs for some PCIe devices users */
7902 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
7903 PCIE_LINK_STATE_CLKPM);
7904
7905 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007906 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007907 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007908 dev_err(&pdev->dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007909 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007910 }
7911
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007912 if (pcim_set_mwi(pdev) < 0)
Heiner Kallweit22148df2018-04-22 17:15:15 +02007913 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007914
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007915 /* use first MMIO region */
7916 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
7917 if (region < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007918 dev_err(&pdev->dev, "no MMIO resource found\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007919 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007920 }
7921
7922 /* check for weird/broken PCI region reporting */
7923 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007924 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007925 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007926 }
7927
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007928 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007929 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007930 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007931 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007932 }
7933
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007934 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01007935
7936 if (!pci_is_pcie(pdev))
Heiner Kallweit22148df2018-04-22 17:15:15 +02007937 dev_info(&pdev->dev, "not PCI Express\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007938
7939 /* Identify chip attached to board */
Heiner Kallweit22148df2018-04-22 17:15:15 +02007940 rtl8169_get_mac_version(tp, cfg->default_ver);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007941
Heiner Kallweit0ae09742018-04-28 22:19:26 +02007942 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007943
7944 if ((sizeof(dma_addr_t) > 4) &&
7945 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
7946 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01007947 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
7948 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007949
7950 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
7951 if (!pci_is_pcie(pdev))
7952 tp->cp_cmd |= PCIDAC;
7953 dev->features |= NETIF_F_HIGHDMA;
7954 } else {
7955 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7956 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007957 dev_err(&pdev->dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007958 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007959 }
7960 }
7961
Francois Romieu3b6cf252012-03-08 09:59:04 +01007962 rtl_init_rxcfg(tp);
7963
7964 rtl_irq_disable(tp);
7965
Hayes Wangc5583862012-07-02 17:23:22 +08007966 rtl_hw_initialize(tp);
7967
Francois Romieu3b6cf252012-03-08 09:59:04 +01007968 rtl_hw_reset(tp);
7969
7970 rtl_ack_events(tp, 0xffff);
7971
7972 pci_set_master(pdev);
7973
Francois Romieu3b6cf252012-03-08 09:59:04 +01007974 rtl_init_mdio_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007975 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08007976 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007977
7978 rtl8169_print_mac_version(tp);
7979
7980 chipset = tp->mac_version;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007981
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007982 rc = rtl_alloc_irq(tp);
7983 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007984 dev_err(&pdev->dev, "Can't allocate interrupt\n");
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007985 return rc;
7986 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007987
Heiner Kallweit7edf6d32018-02-22 21:22:40 +01007988 /* override BIOS settings, use userspace tools to enable WOL */
7989 __rtl8169_set_wol(tp, 0);
7990
Francois Romieu3b6cf252012-03-08 09:59:04 +01007991 if (rtl_tbi_enabled(tp)) {
7992 tp->set_speed = rtl8169_set_speed_tbi;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01007993 tp->get_link_ksettings = rtl8169_get_link_ksettings_tbi;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007994 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
7995 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
7996 tp->link_ok = rtl8169_tbi_link_ok;
7997 tp->do_ioctl = rtl_tbi_ioctl;
7998 } else {
7999 tp->set_speed = rtl8169_set_speed_xmii;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008000 tp->get_link_ksettings = rtl8169_get_link_ksettings_xmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008001 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8002 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8003 tp->link_ok = rtl8169_xmii_link_ok;
8004 tp->do_ioctl = rtl_xmii_ioctl;
8005 }
8006
8007 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05008008 u64_stats_init(&tp->rx_stats.syncp);
8009 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008010
8011 /* Get MAC address */
Chun-Hao Lin89cceb22014-10-01 23:17:15 +08008012 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8013 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8014 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8015 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8016 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8017 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8018 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8019 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8020 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8021 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008022 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8023 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008024 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8025 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8026 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8027 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008028 u16 mac_addr[3];
8029
Chun-Hao Lin05b96872014-10-01 23:17:12 +08008030 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8031 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008032
8033 if (is_valid_ether_addr((u8 *)mac_addr))
8034 rtl_rar_set(tp, (u8 *)mac_addr);
8035 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008036 for (i = 0; i < ETH_ALEN; i++)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008037 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008038
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00008039 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008040 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008041
Heiner Kallweit37621492018-04-17 23:20:03 +02008042 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008043
8044 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8045 * properly for all devices */
8046 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00008047 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008048
8049 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00008050 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8051 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008052 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8053 NETIF_F_HIGHDMA;
8054
hayeswang929a0312014-09-16 11:40:47 +08008055 tp->cp_cmd |= RxChkSum | RxVlan;
8056
8057 /*
8058 * Pretend we are using VLANs; This bypasses a nasty bug where
8059 * Interrupts stop flowing on high load on 8110SCd controllers.
8060 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01008061 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08008062 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00008063 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008064
Heiner Kallweita4328dd2018-04-17 23:33:03 +02008065 switch (rtl_chip_infos[chipset].txd_version) {
8066 case RTL_TD_0:
hayeswang5888d3f2014-07-11 16:25:56 +08008067 tp->tso_csum = rtl8169_tso_csum_v1;
Heiner Kallweita4328dd2018-04-17 23:33:03 +02008068 break;
8069 case RTL_TD_1:
hayeswang5888d3f2014-07-11 16:25:56 +08008070 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08008071 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
Heiner Kallweita4328dd2018-04-17 23:33:03 +02008072 break;
8073 default:
hayeswang5888d3f2014-07-11 16:25:56 +08008074 WARN_ON_ONCE(1);
Heiner Kallweita4328dd2018-04-17 23:33:03 +02008075 }
hayeswang5888d3f2014-07-11 16:25:56 +08008076
Francois Romieu3b6cf252012-03-08 09:59:04 +01008077 dev->hw_features |= NETIF_F_RXALL;
8078 dev->hw_features |= NETIF_F_RXFCS;
8079
Jarod Wilsonc7315a92016-10-17 15:54:09 -04008080 /* MTU range: 60 - hw-specific max */
8081 dev->min_mtu = ETH_ZLEN;
8082 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
8083
Francois Romieu3b6cf252012-03-08 09:59:04 +01008084 tp->hw_start = cfg->hw_start;
8085 tp->event_slow = cfg->event_slow;
Francois Romieu50970832017-10-27 13:24:49 +03008086 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008087
Kees Cook9de36cc2017-10-25 03:53:12 -07008088 timer_setup(&tp->timer, rtl8169_phy_timer, 0);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008089
8090 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8091
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008092 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
8093 &tp->counters_phys_addr,
8094 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01008095 if (!tp->counters)
8096 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02008097
Heiner Kallweit19c9ea32018-03-26 19:19:30 +02008098 pci_set_drvdata(pdev, dev);
8099
Francois Romieu3b6cf252012-03-08 09:59:04 +01008100 rc = register_netdev(dev);
8101 if (rc < 0)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01008102 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008103
Heiner Kallweit2d6c5a62018-04-17 23:31:21 +02008104 netif_info(tp, probe, dev, "%s, %pM, XID %08x, IRQ %d\n",
8105 rtl_chip_infos[chipset].name, dev->dev_addr,
Heiner Kallweit90b989c2018-04-17 23:32:15 +02008106 (u32)(RTL_R32(tp, TxConfig) & 0xfcf0f8ff),
Heiner Kallweit29274992018-02-28 20:43:38 +01008107 pci_irq_vector(pdev, 0));
Francois Romieu3b6cf252012-03-08 09:59:04 +01008108 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8109 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8110 "tx checksumming: %s]\n",
8111 rtl_chip_infos[chipset].jumbo_max,
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02008112 tp->mac_version <= RTL_GIGA_MAC_VER_06 ? "ok" : "ko");
Francois Romieu3b6cf252012-03-08 09:59:04 +01008113 }
8114
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01008115 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01008116 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008117
Francois Romieu3b6cf252012-03-08 09:59:04 +01008118 netif_carrier_off(dev);
8119
Heiner Kallweita92a0842018-01-08 21:39:13 +01008120 if (pci_dev_run_wake(pdev))
8121 pm_runtime_put_sync(&pdev->dev);
8122
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008123 return 0;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008124}
8125
Linus Torvalds1da177e2005-04-16 15:20:36 -07008126static struct pci_driver rtl8169_pci_driver = {
8127 .name = MODULENAME,
8128 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01008129 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05008130 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02008131 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008132 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008133};
8134
Devendra Naga3eeb7da2012-10-26 09:27:42 +00008135module_pci_driver(rtl8169_pci_driver);