blob: 41f9b5c2bc0dd63bc9163bce6372439181a01853 [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) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001614 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1615 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001616 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1617 wolopts |= WAKE_MAGIC;
1618 break;
1619 default:
1620 if (options & MagicPacket)
1621 wolopts |= WAKE_MAGIC;
1622 break;
1623 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001624
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001625 options = RTL_R8(tp, Config5);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001626 if (options & UWF)
1627 wolopts |= WAKE_UCAST;
1628 if (options & BWF)
1629 wolopts |= WAKE_BCAST;
1630 if (options & MWF)
1631 wolopts |= WAKE_MCAST;
1632
1633 return wolopts;
1634}
1635
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001636static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1637{
1638 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001639 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001640
1641 pm_runtime_get_noresume(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001642
Francois Romieuda78dbf2012-01-26 14:18:23 +01001643 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001644
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001645 wol->supported = WAKE_ANY;
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001646 if (pm_runtime_active(d))
1647 wol->wolopts = __rtl8169_get_wol(tp);
1648 else
1649 wol->wolopts = tp->saved_wolopts;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001650
Francois Romieuda78dbf2012-01-26 14:18:23 +01001651 rtl_unlock_work(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001652
1653 pm_runtime_put_noidle(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001654}
1655
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001656static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001657{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001658 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001659 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001660 u32 opt;
1661 u16 reg;
1662 u8 mask;
1663 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001664 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001665 { WAKE_UCAST, Config5, UWF },
1666 { WAKE_BCAST, Config5, BWF },
1667 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001668 { WAKE_ANY, Config5, LanWake },
1669 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001670 };
Francois Romieu851e6022012-04-17 11:10:11 +02001671 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001672
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001673 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001674
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001675 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001676 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1677 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001678 tmp = ARRAY_SIZE(cfg) - 1;
1679 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001680 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001681 0x0dc,
1682 ERIAR_MASK_0100,
1683 MagicPacket_v2,
1684 0x0000,
1685 ERIAR_EXGMAC);
1686 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001687 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001688 0x0dc,
1689 ERIAR_MASK_0100,
1690 0x0000,
1691 MagicPacket_v2,
1692 ERIAR_EXGMAC);
1693 break;
1694 default:
1695 tmp = ARRAY_SIZE(cfg);
1696 break;
1697 }
1698
1699 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001700 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001701 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001702 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001703 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001704 }
1705
Francois Romieu851e6022012-04-17 11:10:11 +02001706 switch (tp->mac_version) {
1707 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001708 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001709 if (wolopts)
1710 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001711 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001712 break;
1713 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001714 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001715 if (wolopts)
1716 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001717 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001718 break;
1719 }
1720
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001721 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001722}
1723
1724static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1725{
1726 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001727 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001728
1729 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001730
Francois Romieuda78dbf2012-01-26 14:18:23 +01001731 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001732
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001733 if (pm_runtime_active(d))
1734 __rtl8169_set_wol(tp, wol->wolopts);
1735 else
1736 tp->saved_wolopts = wol->wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001737
1738 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001739
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001740 device_set_wakeup_enable(d, wol->wolopts);
françois romieuea809072010-11-08 13:23:58 +00001741
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001742 pm_runtime_put_noidle(d);
1743
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001744 return 0;
1745}
1746
Francois Romieu31bd2042011-04-26 18:58:59 +02001747static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1748{
Francois Romieu85bffe62011-04-27 08:22:39 +02001749 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001750}
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752static void rtl8169_get_drvinfo(struct net_device *dev,
1753 struct ethtool_drvinfo *info)
1754{
1755 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001756 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Rick Jones68aad782011-11-07 13:29:27 +00001758 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1759 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1760 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001761 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001762 if (!IS_ERR_OR_NULL(rtl_fw))
1763 strlcpy(info->fw_version, rtl_fw->version,
1764 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766
1767static int rtl8169_get_regs_len(struct net_device *dev)
1768{
1769 return R8169_REGS_SIZE;
1770}
1771
1772static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001773 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
1775 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 int ret = 0;
1777 u32 reg;
1778
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001779 reg = RTL_R32(tp, TBICSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1781 (duplex == DUPLEX_FULL)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001782 RTL_W32(tp, TBICSR, reg & ~(TBINwEnable | TBINwRestart));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 } else if (autoneg == AUTONEG_ENABLE)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001784 RTL_W32(tp, TBICSR, reg | TBINwEnable | TBINwRestart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001786 netif_warn(tp, link, dev,
1787 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 ret = -EOPNOTSUPP;
1789 }
1790
1791 return ret;
1792}
1793
1794static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001795 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
1797 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001798 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001799 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Hayes Wang716b50a2011-02-22 17:26:18 +08001801 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001804 int auto_nego;
1805
françois romieu4da19632011-01-03 15:07:55 +00001806 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001807 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1808 ADVERTISE_100HALF | ADVERTISE_100FULL);
1809
1810 if (adv & ADVERTISED_10baseT_Half)
1811 auto_nego |= ADVERTISE_10HALF;
1812 if (adv & ADVERTISED_10baseT_Full)
1813 auto_nego |= ADVERTISE_10FULL;
1814 if (adv & ADVERTISED_100baseT_Half)
1815 auto_nego |= ADVERTISE_100HALF;
1816 if (adv & ADVERTISED_100baseT_Full)
1817 auto_nego |= ADVERTISE_100FULL;
1818
françois romieu3577aa12009-05-19 10:46:48 +00001819 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1820
françois romieu4da19632011-01-03 15:07:55 +00001821 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001822 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1823
1824 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001825 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001826 if (adv & ADVERTISED_1000baseT_Half)
1827 giga_ctrl |= ADVERTISE_1000HALF;
1828 if (adv & ADVERTISED_1000baseT_Full)
1829 giga_ctrl |= ADVERTISE_1000FULL;
1830 } else if (adv & (ADVERTISED_1000baseT_Half |
1831 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001832 netif_info(tp, link, dev,
1833 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001834 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
françois romieu3577aa12009-05-19 10:46:48 +00001837 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001838
françois romieu4da19632011-01-03 15:07:55 +00001839 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1840 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001841 } else {
françois romieu3577aa12009-05-19 10:46:48 +00001842 if (speed == SPEED_10)
1843 bmcr = 0;
1844 else if (speed == SPEED_100)
1845 bmcr = BMCR_SPEED100;
1846 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001847 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001848
1849 if (duplex == DUPLEX_FULL)
1850 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001851 }
1852
françois romieu4da19632011-01-03 15:07:55 +00001853 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001854
Francois Romieucecb5fd2011-04-01 10:21:07 +02001855 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1856 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001857 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001858 rtl_writephy(tp, 0x17, 0x2138);
1859 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001860 } else {
françois romieu4da19632011-01-03 15:07:55 +00001861 rtl_writephy(tp, 0x17, 0x2108);
1862 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001863 }
1864 }
1865
Oliver Neukum54405cd2011-01-06 21:55:13 +01001866 rc = 0;
1867out:
1868 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870
1871static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001872 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
1874 struct rtl8169_private *tp = netdev_priv(dev);
1875 int ret;
1876
Oliver Neukum54405cd2011-01-06 21:55:13 +01001877 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001878 if (ret < 0)
1879 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Francois Romieu4876cc12011-03-11 21:07:11 +01001881 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
Chun-Hao Linc4556972016-03-11 14:21:14 +08001882 (advertising & ADVERTISED_1000baseT_Full) &&
1883 !pci_is_pcie(tp->pci_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001885 }
1886out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return ret;
1888}
1889
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001890static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1891 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Francois Romieud58d46b2011-05-03 16:38:29 +02001893 struct rtl8169_private *tp = netdev_priv(dev);
1894
Francois Romieu2b7b4312011-04-18 22:53:24 -07001895 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001896 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Francois Romieud58d46b2011-05-03 16:38:29 +02001898 if (dev->mtu > JUMBO_1K &&
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02001899 tp->mac_version > RTL_GIGA_MAC_VER_06)
Francois Romieud58d46b2011-05-03 16:38:29 +02001900 features &= ~NETIF_F_IP_CSUM;
1901
Michał Mirosław350fb322011-04-08 06:35:56 +00001902 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904
Heiner Kallweita3984572018-04-28 22:19:15 +02001905static int rtl8169_set_features(struct net_device *dev,
1906 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
1908 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001909 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Heiner Kallweita3984572018-04-28 22:19:15 +02001911 rtl_lock_work(tp);
1912
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001913 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001914 if (features & NETIF_F_RXALL)
1915 rx_config |= (AcceptErr | AcceptRunt);
1916 else
1917 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001919 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001920
hayeswang929a0312014-09-16 11:40:47 +08001921 if (features & NETIF_F_RXCSUM)
1922 tp->cp_cmd |= RxChkSum;
1923 else
1924 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001925
hayeswang929a0312014-09-16 11:40:47 +08001926 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1927 tp->cp_cmd |= RxVlan;
1928 else
1929 tp->cp_cmd &= ~RxVlan;
1930
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001931 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1932 RTL_R16(tp, CPlusCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Francois Romieuda78dbf2012-01-26 14:18:23 +01001934 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 return 0;
1937}
1938
Kirill Smelkov810f4892012-11-10 21:11:02 +04001939static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001941 return (skb_vlan_tag_present(skb)) ?
1942 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
Francois Romieu7a8fc772011-03-01 17:18:33 +01001945static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
1947 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Francois Romieu7a8fc772011-03-01 17:18:33 +01001949 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001950 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
1952
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001953static int rtl8169_get_link_ksettings_tbi(struct net_device *dev,
1954 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
1956 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 u32 status;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001958 u32 supported, advertising;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001960 supported =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001962 cmd->base.port = PORT_FIBRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001964 status = RTL_R32(tp, TBICSR);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001965 advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1966 cmd->base.autoneg = !!(status & TBINwEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001968 cmd->base.speed = SPEED_1000;
1969 cmd->base.duplex = DUPLEX_FULL; /* Always set */
1970
1971 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1972 supported);
1973 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1974 advertising);
Francois Romieuccdffb92008-07-26 14:26:06 +02001975
1976 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001979static int rtl8169_get_link_ksettings_xmii(struct net_device *dev,
1980 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03001984 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
1985
1986 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
1988
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001989static int rtl8169_get_link_ksettings(struct net_device *dev,
1990 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
1992 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001993 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Francois Romieuda78dbf2012-01-26 14:18:23 +01001995 rtl_lock_work(tp);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01001996 rc = tp->get_link_ksettings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001997 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Francois Romieuccdffb92008-07-26 14:26:06 +02001999 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002002static int rtl8169_set_link_ksettings(struct net_device *dev,
2003 const struct ethtool_link_ksettings *cmd)
2004{
2005 struct rtl8169_private *tp = netdev_priv(dev);
2006 int rc;
2007 u32 advertising;
2008
2009 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
2010 cmd->link_modes.advertising))
2011 return -EINVAL;
2012
2013 del_timer_sync(&tp->timer);
2014
2015 rtl_lock_work(tp);
2016 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
2017 cmd->base.duplex, advertising);
2018 rtl_unlock_work(tp);
2019
2020 return rc;
2021}
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2024 void *p)
2025{
Francois Romieu5b0384f2006-08-16 16:00:01 +02002026 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02002027 u32 __iomem *data = tp->mmio_addr;
2028 u32 *dw = p;
2029 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Francois Romieuda78dbf2012-01-26 14:18:23 +01002031 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02002032 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2033 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002034 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002037static u32 rtl8169_get_msglevel(struct net_device *dev)
2038{
2039 struct rtl8169_private *tp = netdev_priv(dev);
2040
2041 return tp->msg_enable;
2042}
2043
2044static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2045{
2046 struct rtl8169_private *tp = netdev_priv(dev);
2047
2048 tp->msg_enable = value;
2049}
2050
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002051static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2052 "tx_packets",
2053 "rx_packets",
2054 "tx_errors",
2055 "rx_errors",
2056 "rx_missed",
2057 "align_errors",
2058 "tx_single_collisions",
2059 "tx_multi_collisions",
2060 "unicast",
2061 "broadcast",
2062 "multicast",
2063 "tx_aborted",
2064 "tx_underrun",
2065};
2066
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002067static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002068{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002069 switch (sset) {
2070 case ETH_SS_STATS:
2071 return ARRAY_SIZE(rtl8169_gstrings);
2072 default:
2073 return -EOPNOTSUPP;
2074 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002075}
2076
Corinna Vinschen42020322015-09-10 10:47:35 +02002077DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002078{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002079 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002080}
2081
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002082static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002083{
Corinna Vinschen42020322015-09-10 10:47:35 +02002084 dma_addr_t paddr = tp->counters_phys_addr;
2085 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02002086
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002087 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
2088 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02002089 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002090 RTL_W32(tp, CounterAddrLow, cmd);
2091 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02002092
Francois Romieua78e9362018-01-26 01:53:26 +01002093 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002094}
2095
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002096static bool rtl8169_reset_counters(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002097{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002098 /*
2099 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2100 * tally counters.
2101 */
2102 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2103 return true;
2104
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002105 return rtl8169_do_counters(tp, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02002106}
2107
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002108static bool rtl8169_update_counters(struct rtl8169_private *tp)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002109{
Ivan Vecera355423d2009-02-06 21:49:57 -08002110 /*
2111 * Some chips are unable to dump tally counters when the receiver
2112 * is disabled.
2113 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002114 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002115 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002116
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002117 return rtl8169_do_counters(tp, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002118}
2119
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002120static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002121{
Corinna Vinschen42020322015-09-10 10:47:35 +02002122 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002123 bool ret = false;
2124
2125 /*
2126 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2127 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2128 * reset by a power cycle, while the counter values collected by the
2129 * driver are reset at every driver unload/load cycle.
2130 *
2131 * To make sure the HW values returned by @get_stats64 match the SW
2132 * values, we collect the initial values at first open(*) and use them
2133 * as offsets to normalize the values returned by @get_stats64.
2134 *
2135 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2136 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2137 * set at open time by rtl_hw_start.
2138 */
2139
2140 if (tp->tc_offset.inited)
2141 return true;
2142
2143 /* If both, reset and update fail, propagate to caller. */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002144 if (rtl8169_reset_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002145 ret = true;
2146
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002147 if (rtl8169_update_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002148 ret = true;
2149
Corinna Vinschen42020322015-09-10 10:47:35 +02002150 tp->tc_offset.tx_errors = counters->tx_errors;
2151 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2152 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002153 tp->tc_offset.inited = true;
2154
2155 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002156}
2157
Ivan Vecera355423d2009-02-06 21:49:57 -08002158static void rtl8169_get_ethtool_stats(struct net_device *dev,
2159 struct ethtool_stats *stats, u64 *data)
2160{
2161 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01002162 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02002163 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08002164
2165 ASSERT_RTNL();
2166
Chun-Hao Line0636232016-07-29 16:37:55 +08002167 pm_runtime_get_noresume(d);
2168
2169 if (pm_runtime_active(d))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002170 rtl8169_update_counters(tp);
Chun-Hao Line0636232016-07-29 16:37:55 +08002171
2172 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08002173
Corinna Vinschen42020322015-09-10 10:47:35 +02002174 data[0] = le64_to_cpu(counters->tx_packets);
2175 data[1] = le64_to_cpu(counters->rx_packets);
2176 data[2] = le64_to_cpu(counters->tx_errors);
2177 data[3] = le32_to_cpu(counters->rx_errors);
2178 data[4] = le16_to_cpu(counters->rx_missed);
2179 data[5] = le16_to_cpu(counters->align_errors);
2180 data[6] = le32_to_cpu(counters->tx_one_collision);
2181 data[7] = le32_to_cpu(counters->tx_multi_collision);
2182 data[8] = le64_to_cpu(counters->rx_unicast);
2183 data[9] = le64_to_cpu(counters->rx_broadcast);
2184 data[10] = le32_to_cpu(counters->rx_multicast);
2185 data[11] = le16_to_cpu(counters->tx_aborted);
2186 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08002187}
2188
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002189static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2190{
2191 switch(stringset) {
2192 case ETH_SS_STATS:
2193 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2194 break;
2195 }
2196}
2197
Florian Fainellif0903ea2016-12-03 12:01:19 -08002198static int rtl8169_nway_reset(struct net_device *dev)
2199{
2200 struct rtl8169_private *tp = netdev_priv(dev);
2201
2202 return mii_nway_restart(&tp->mii);
2203}
2204
Francois Romieu50970832017-10-27 13:24:49 +03002205/*
2206 * Interrupt coalescing
2207 *
2208 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2209 * > 8169, 8168 and 810x line of chipsets
2210 *
2211 * 8169, 8168, and 8136(810x) serial chipsets support it.
2212 *
2213 * > 2 - the Tx timer unit at gigabit speed
2214 *
2215 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2216 * (0xe0) bit 1 and bit 0.
2217 *
2218 * For 8169
2219 * bit[1:0] \ speed 1000M 100M 10M
2220 * 0 0 320ns 2.56us 40.96us
2221 * 0 1 2.56us 20.48us 327.7us
2222 * 1 0 5.12us 40.96us 655.4us
2223 * 1 1 10.24us 81.92us 1.31ms
2224 *
2225 * For the other
2226 * bit[1:0] \ speed 1000M 100M 10M
2227 * 0 0 5us 2.56us 40.96us
2228 * 0 1 40us 20.48us 327.7us
2229 * 1 0 80us 40.96us 655.4us
2230 * 1 1 160us 81.92us 1.31ms
2231 */
2232
2233/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2234struct rtl_coalesce_scale {
2235 /* Rx / Tx */
2236 u32 nsecs[2];
2237};
2238
2239/* rx/tx scale factors for all CPlusCmd[0:1] cases */
2240struct rtl_coalesce_info {
2241 u32 speed;
2242 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2243};
2244
2245/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2246#define rxtx_x1822(r, t) { \
2247 {{(r), (t)}}, \
2248 {{(r)*8, (t)*8}}, \
2249 {{(r)*8*2, (t)*8*2}}, \
2250 {{(r)*8*2*2, (t)*8*2*2}}, \
2251}
2252static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2253 /* speed delays: rx00 tx00 */
2254 { SPEED_10, rxtx_x1822(40960, 40960) },
2255 { SPEED_100, rxtx_x1822( 2560, 2560) },
2256 { SPEED_1000, rxtx_x1822( 320, 320) },
2257 { 0 },
2258};
2259
2260static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2261 /* speed delays: rx00 tx00 */
2262 { SPEED_10, rxtx_x1822(40960, 40960) },
2263 { SPEED_100, rxtx_x1822( 2560, 2560) },
2264 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2265 { 0 },
2266};
2267#undef rxtx_x1822
2268
2269/* get rx/tx scale vector corresponding to current speed */
2270static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2271{
2272 struct rtl8169_private *tp = netdev_priv(dev);
2273 struct ethtool_link_ksettings ecmd;
2274 const struct rtl_coalesce_info *ci;
2275 int rc;
2276
2277 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2278 if (rc < 0)
2279 return ERR_PTR(rc);
2280
2281 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2282 if (ecmd.base.speed == ci->speed) {
2283 return ci;
2284 }
2285 }
2286
2287 return ERR_PTR(-ELNRNG);
2288}
2289
2290static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2291{
2292 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002293 const struct rtl_coalesce_info *ci;
2294 const struct rtl_coalesce_scale *scale;
2295 struct {
2296 u32 *max_frames;
2297 u32 *usecs;
2298 } coal_settings [] = {
2299 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2300 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2301 }, *p = coal_settings;
2302 int i;
2303 u16 w;
2304
2305 memset(ec, 0, sizeof(*ec));
2306
2307 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2308 ci = rtl_coalesce_info(dev);
2309 if (IS_ERR(ci))
2310 return PTR_ERR(ci);
2311
Heiner Kallweit0ae09742018-04-28 22:19:26 +02002312 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
Francois Romieu50970832017-10-27 13:24:49 +03002313
2314 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002315 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03002316 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2317 w >>= RTL_COALESCE_SHIFT;
2318 *p->usecs = w & RTL_COALESCE_MASK;
2319 }
2320
2321 for (i = 0; i < 2; i++) {
2322 p = coal_settings + i;
2323 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2324
2325 /*
2326 * ethtool_coalesce says it is illegal to set both usecs and
2327 * max_frames to 0.
2328 */
2329 if (!*p->usecs && !*p->max_frames)
2330 *p->max_frames = 1;
2331 }
2332
2333 return 0;
2334}
2335
2336/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2337static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2338 struct net_device *dev, u32 nsec, u16 *cp01)
2339{
2340 const struct rtl_coalesce_info *ci;
2341 u16 i;
2342
2343 ci = rtl_coalesce_info(dev);
2344 if (IS_ERR(ci))
2345 return ERR_CAST(ci);
2346
2347 for (i = 0; i < 4; i++) {
2348 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2349 ci->scalev[i].nsecs[1]);
2350 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2351 *cp01 = i;
2352 return &ci->scalev[i];
2353 }
2354 }
2355
2356 return ERR_PTR(-EINVAL);
2357}
2358
2359static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2360{
2361 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002362 const struct rtl_coalesce_scale *scale;
2363 struct {
2364 u32 frames;
2365 u32 usecs;
2366 } coal_settings [] = {
2367 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2368 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2369 }, *p = coal_settings;
2370 u16 w = 0, cp01;
2371 int i;
2372
2373 scale = rtl_coalesce_choose_scale(dev,
2374 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2375 if (IS_ERR(scale))
2376 return PTR_ERR(scale);
2377
2378 for (i = 0; i < 2; i++, p++) {
2379 u32 units;
2380
2381 /*
2382 * accept max_frames=1 we returned in rtl_get_coalesce.
2383 * accept it not only when usecs=0 because of e.g. the following scenario:
2384 *
2385 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2386 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2387 * - then user does `ethtool -C eth0 rx-usecs 100`
2388 *
2389 * since ethtool sends to kernel whole ethtool_coalesce
2390 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2391 * we'll reject it below in `frames % 4 != 0`.
2392 */
2393 if (p->frames == 1) {
2394 p->frames = 0;
2395 }
2396
2397 units = p->usecs * 1000 / scale->nsecs[i];
2398 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2399 return -EINVAL;
2400
2401 w <<= RTL_COALESCE_SHIFT;
2402 w |= units;
2403 w <<= RTL_COALESCE_SHIFT;
2404 w |= p->frames >> 2;
2405 }
2406
2407 rtl_lock_work(tp);
2408
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002409 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03002410
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +02002411 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002412 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2413 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03002414
2415 rtl_unlock_work(tp);
2416
2417 return 0;
2418}
2419
Jeff Garzik7282d492006-09-13 14:30:00 -04002420static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 .get_drvinfo = rtl8169_get_drvinfo,
2422 .get_regs_len = rtl8169_get_regs_len,
2423 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002424 .get_coalesce = rtl_get_coalesce,
2425 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002426 .get_msglevel = rtl8169_get_msglevel,
2427 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002429 .get_wol = rtl8169_get_wol,
2430 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002431 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002432 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002433 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002434 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainellif0903ea2016-12-03 12:01:19 -08002435 .nway_reset = rtl8169_nway_reset,
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002436 .get_link_ksettings = rtl8169_get_link_ksettings,
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002437 .set_link_ksettings = rtl8169_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438};
2439
Francois Romieu07d3f512007-02-21 22:40:46 +01002440static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Heiner Kallweit22148df2018-04-22 17:15:15 +02002441 u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Francois Romieu0e485152007-02-20 00:00:26 +01002443 /*
2444 * The driver currently handles the 8168Bf and the 8168Be identically
2445 * but they can be identified more specifically through the test below
2446 * if needed:
2447 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002448 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002449 *
2450 * Same thing for the 8101Eb and the 8101Ec:
2451 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002452 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002453 */
Francois Romieu37441002011-06-17 22:58:54 +02002454 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002456 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 int mac_version;
2458 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002459 /* 8168EP family. */
2460 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2461 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2462 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2463
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002464 /* 8168H family. */
2465 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2466 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2467
Hayes Wangc5583862012-07-02 17:23:22 +08002468 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002469 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002470 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002471 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2472 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2473
Hayes Wangc2218922011-09-06 16:55:18 +08002474 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002475 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002476 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2477 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2478
hayeswang01dc7fe2011-03-21 01:50:28 +00002479 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002480 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002481 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2482 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2483
Francois Romieu5b538df2008-07-20 16:22:45 +02002484 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002485 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002486 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002487
françois romieue6de30d2011-01-03 15:08:37 +00002488 /* 8168DP family. */
2489 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2490 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002491 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002492
Francois Romieuef808d52008-06-29 13:10:54 +02002493 /* 8168C family. */
Francois Romieuef3386f2008-06-29 12:24:30 +02002494 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002495 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002496 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002497 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2498 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002499 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieuef808d52008-06-29 13:10:54 +02002500 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002501
2502 /* 8168B family. */
2503 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002504 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2505 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2506
2507 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002508 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002509 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002510 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2511 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002512 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2513 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2514 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2515 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002516 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002517 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002518 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002519 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2520 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002521 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2522 /* FIXME: where did these entries come from ? -- FR */
2523 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2524 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2525
2526 /* 8110 family. */
2527 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2528 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2529 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2530 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2531 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2532 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2533
Jean Delvaref21b75e2009-05-26 20:54:48 -07002534 /* Catch-all */
2535 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002536 };
2537 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 u32 reg;
2539
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002540 reg = RTL_R32(tp, TxConfig);
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002541 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 p++;
2543 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002544
2545 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02002546 dev_notice(tp_to_dev(tp),
2547 "unknown MAC, using family default\n");
Francois Romieu5d320a22011-05-08 17:47:36 +02002548 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002549 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2550 tp->mac_version = tp->mii.supports_gmii ?
2551 RTL_GIGA_MAC_VER_42 :
2552 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002553 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2554 tp->mac_version = tp->mii.supports_gmii ?
2555 RTL_GIGA_MAC_VER_45 :
2556 RTL_GIGA_MAC_VER_47;
2557 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2558 tp->mac_version = tp->mii.supports_gmii ?
2559 RTL_GIGA_MAC_VER_46 :
2560 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562}
2563
2564static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2565{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002566 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567}
2568
Francois Romieu867763c2007-08-17 18:21:58 +02002569struct phy_reg {
2570 u16 reg;
2571 u16 val;
2572};
2573
françois romieu4da19632011-01-03 15:07:55 +00002574static void rtl_writephy_batch(struct rtl8169_private *tp,
2575 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002576{
2577 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002578 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002579 regs++;
2580 }
2581}
2582
françois romieubca03d52011-01-03 15:07:31 +00002583#define PHY_READ 0x00000000
2584#define PHY_DATA_OR 0x10000000
2585#define PHY_DATA_AND 0x20000000
2586#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002587#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002588#define PHY_CLEAR_READCOUNT 0x70000000
2589#define PHY_WRITE 0x80000000
2590#define PHY_READCOUNT_EQ_SKIP 0x90000000
2591#define PHY_COMP_EQ_SKIPN 0xa0000000
2592#define PHY_COMP_NEQ_SKIPN 0xb0000000
2593#define PHY_WRITE_PREVIOUS 0xc0000000
2594#define PHY_SKIPN 0xd0000000
2595#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002596
Hayes Wang960aee62011-06-18 11:37:48 +02002597struct fw_info {
2598 u32 magic;
2599 char version[RTL_VER_SIZE];
2600 __le32 fw_start;
2601 __le32 fw_len;
2602 u8 chksum;
2603} __packed;
2604
Francois Romieu1c361ef2011-06-17 17:16:24 +02002605#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2606
2607static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002608{
Francois Romieub6ffd972011-06-17 17:00:05 +02002609 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002610 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002611 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2612 char *version = rtl_fw->version;
2613 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002614
Francois Romieu1c361ef2011-06-17 17:16:24 +02002615 if (fw->size < FW_OPCODE_SIZE)
2616 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002617
2618 if (!fw_info->magic) {
2619 size_t i, size, start;
2620 u8 checksum = 0;
2621
2622 if (fw->size < sizeof(*fw_info))
2623 goto out;
2624
2625 for (i = 0; i < fw->size; i++)
2626 checksum += fw->data[i];
2627 if (checksum != 0)
2628 goto out;
2629
2630 start = le32_to_cpu(fw_info->fw_start);
2631 if (start > fw->size)
2632 goto out;
2633
2634 size = le32_to_cpu(fw_info->fw_len);
2635 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2636 goto out;
2637
2638 memcpy(version, fw_info->version, RTL_VER_SIZE);
2639
2640 pa->code = (__le32 *)(fw->data + start);
2641 pa->size = size;
2642 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002643 if (fw->size % FW_OPCODE_SIZE)
2644 goto out;
2645
2646 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2647
2648 pa->code = (__le32 *)fw->data;
2649 pa->size = fw->size / FW_OPCODE_SIZE;
2650 }
2651 version[RTL_VER_SIZE - 1] = 0;
2652
2653 rc = true;
2654out:
2655 return rc;
2656}
2657
Francois Romieufd112f22011-06-18 00:10:29 +02002658static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2659 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002660{
Francois Romieufd112f22011-06-18 00:10:29 +02002661 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002662 size_t index;
2663
Francois Romieu1c361ef2011-06-17 17:16:24 +02002664 for (index = 0; index < pa->size; index++) {
2665 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002666 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002667
hayeswang42b82dc2011-01-10 02:07:25 +00002668 switch(action & 0xf0000000) {
2669 case PHY_READ:
2670 case PHY_DATA_OR:
2671 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002672 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002673 case PHY_CLEAR_READCOUNT:
2674 case PHY_WRITE:
2675 case PHY_WRITE_PREVIOUS:
2676 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002677 break;
2678
hayeswang42b82dc2011-01-10 02:07:25 +00002679 case PHY_BJMPN:
2680 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002681 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002682 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002683 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002684 }
2685 break;
2686 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002687 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002688 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002689 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002690 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002691 }
2692 break;
2693 case PHY_COMP_EQ_SKIPN:
2694 case PHY_COMP_NEQ_SKIPN:
2695 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002696 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002697 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002698 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002699 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002700 }
2701 break;
2702
hayeswang42b82dc2011-01-10 02:07:25 +00002703 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002704 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002705 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002706 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002707 }
2708 }
Francois Romieufd112f22011-06-18 00:10:29 +02002709 rc = true;
2710out:
2711 return rc;
2712}
françois romieubca03d52011-01-03 15:07:31 +00002713
Francois Romieufd112f22011-06-18 00:10:29 +02002714static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2715{
2716 struct net_device *dev = tp->dev;
2717 int rc = -EINVAL;
2718
2719 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002720 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002721 goto out;
2722 }
2723
2724 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2725 rc = 0;
2726out:
2727 return rc;
2728}
2729
2730static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2731{
2732 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002733 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002734 u32 predata, count;
2735 size_t index;
2736
2737 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002738 org.write = ops->write;
2739 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002740
Francois Romieu1c361ef2011-06-17 17:16:24 +02002741 for (index = 0; index < pa->size; ) {
2742 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002743 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002744 u32 regno = (action & 0x0fff0000) >> 16;
2745
2746 if (!action)
2747 break;
françois romieubca03d52011-01-03 15:07:31 +00002748
2749 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002750 case PHY_READ:
2751 predata = rtl_readphy(tp, regno);
2752 count++;
2753 index++;
françois romieubca03d52011-01-03 15:07:31 +00002754 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002755 case PHY_DATA_OR:
2756 predata |= data;
2757 index++;
2758 break;
2759 case PHY_DATA_AND:
2760 predata &= data;
2761 index++;
2762 break;
2763 case PHY_BJMPN:
2764 index -= regno;
2765 break;
hayeswangeee37862013-04-01 22:23:38 +00002766 case PHY_MDIO_CHG:
2767 if (data == 0) {
2768 ops->write = org.write;
2769 ops->read = org.read;
2770 } else if (data == 1) {
2771 ops->write = mac_mcu_write;
2772 ops->read = mac_mcu_read;
2773 }
2774
hayeswang42b82dc2011-01-10 02:07:25 +00002775 index++;
2776 break;
2777 case PHY_CLEAR_READCOUNT:
2778 count = 0;
2779 index++;
2780 break;
2781 case PHY_WRITE:
2782 rtl_writephy(tp, regno, data);
2783 index++;
2784 break;
2785 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002786 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002787 break;
2788 case PHY_COMP_EQ_SKIPN:
2789 if (predata == data)
2790 index += regno;
2791 index++;
2792 break;
2793 case PHY_COMP_NEQ_SKIPN:
2794 if (predata != data)
2795 index += regno;
2796 index++;
2797 break;
2798 case PHY_WRITE_PREVIOUS:
2799 rtl_writephy(tp, regno, predata);
2800 index++;
2801 break;
2802 case PHY_SKIPN:
2803 index += regno + 1;
2804 break;
2805 case PHY_DELAY_MS:
2806 mdelay(data);
2807 index++;
2808 break;
2809
françois romieubca03d52011-01-03 15:07:31 +00002810 default:
2811 BUG();
2812 }
2813 }
hayeswangeee37862013-04-01 22:23:38 +00002814
2815 ops->write = org.write;
2816 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002817}
2818
françois romieuf1e02ed2011-01-13 13:07:53 +00002819static void rtl_release_firmware(struct rtl8169_private *tp)
2820{
Francois Romieub6ffd972011-06-17 17:00:05 +02002821 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2822 release_firmware(tp->rtl_fw->fw);
2823 kfree(tp->rtl_fw);
2824 }
2825 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002826}
2827
François Romieu953a12c2011-04-24 17:38:48 +02002828static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002829{
Francois Romieub6ffd972011-06-17 17:00:05 +02002830 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002831
2832 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002833 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002834 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002835}
2836
2837static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2838{
2839 if (rtl_readphy(tp, reg) != val)
2840 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2841 else
2842 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002843}
2844
françois romieu4da19632011-01-03 15:07:55 +00002845static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002847 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002848 { 0x1f, 0x0001 },
2849 { 0x06, 0x006e },
2850 { 0x08, 0x0708 },
2851 { 0x15, 0x4000 },
2852 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
françois romieu0b9b5712009-08-10 19:44:56 +00002854 { 0x1f, 0x0001 },
2855 { 0x03, 0x00a1 },
2856 { 0x02, 0x0008 },
2857 { 0x01, 0x0120 },
2858 { 0x00, 0x1000 },
2859 { 0x04, 0x0800 },
2860 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
françois romieu0b9b5712009-08-10 19:44:56 +00002862 { 0x03, 0xff41 },
2863 { 0x02, 0xdf60 },
2864 { 0x01, 0x0140 },
2865 { 0x00, 0x0077 },
2866 { 0x04, 0x7800 },
2867 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
françois romieu0b9b5712009-08-10 19:44:56 +00002869 { 0x03, 0x802f },
2870 { 0x02, 0x4f02 },
2871 { 0x01, 0x0409 },
2872 { 0x00, 0xf0f9 },
2873 { 0x04, 0x9800 },
2874 { 0x04, 0x9000 },
2875
2876 { 0x03, 0xdf01 },
2877 { 0x02, 0xdf20 },
2878 { 0x01, 0xff95 },
2879 { 0x00, 0xba00 },
2880 { 0x04, 0xa800 },
2881 { 0x04, 0xa000 },
2882
2883 { 0x03, 0xff41 },
2884 { 0x02, 0xdf20 },
2885 { 0x01, 0x0140 },
2886 { 0x00, 0x00bb },
2887 { 0x04, 0xb800 },
2888 { 0x04, 0xb000 },
2889
2890 { 0x03, 0xdf41 },
2891 { 0x02, 0xdc60 },
2892 { 0x01, 0x6340 },
2893 { 0x00, 0x007d },
2894 { 0x04, 0xd800 },
2895 { 0x04, 0xd000 },
2896
2897 { 0x03, 0xdf01 },
2898 { 0x02, 0xdf20 },
2899 { 0x01, 0x100a },
2900 { 0x00, 0xa0ff },
2901 { 0x04, 0xf800 },
2902 { 0x04, 0xf000 },
2903
2904 { 0x1f, 0x0000 },
2905 { 0x0b, 0x0000 },
2906 { 0x00, 0x9200 }
2907 };
2908
françois romieu4da19632011-01-03 15:07:55 +00002909 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910}
2911
françois romieu4da19632011-01-03 15:07:55 +00002912static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002913{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002914 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002915 { 0x1f, 0x0002 },
2916 { 0x01, 0x90d0 },
2917 { 0x1f, 0x0000 }
2918 };
2919
françois romieu4da19632011-01-03 15:07:55 +00002920 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002921}
2922
françois romieu4da19632011-01-03 15:07:55 +00002923static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002924{
2925 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002926
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002927 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2928 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002929 return;
2930
françois romieu4da19632011-01-03 15:07:55 +00002931 rtl_writephy(tp, 0x1f, 0x0001);
2932 rtl_writephy(tp, 0x10, 0xf01b);
2933 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002934}
2935
françois romieu4da19632011-01-03 15:07:55 +00002936static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002937{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002938 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002939 { 0x1f, 0x0001 },
2940 { 0x04, 0x0000 },
2941 { 0x03, 0x00a1 },
2942 { 0x02, 0x0008 },
2943 { 0x01, 0x0120 },
2944 { 0x00, 0x1000 },
2945 { 0x04, 0x0800 },
2946 { 0x04, 0x9000 },
2947 { 0x03, 0x802f },
2948 { 0x02, 0x4f02 },
2949 { 0x01, 0x0409 },
2950 { 0x00, 0xf099 },
2951 { 0x04, 0x9800 },
2952 { 0x04, 0xa000 },
2953 { 0x03, 0xdf01 },
2954 { 0x02, 0xdf20 },
2955 { 0x01, 0xff95 },
2956 { 0x00, 0xba00 },
2957 { 0x04, 0xa800 },
2958 { 0x04, 0xf000 },
2959 { 0x03, 0xdf01 },
2960 { 0x02, 0xdf20 },
2961 { 0x01, 0x101a },
2962 { 0x00, 0xa0ff },
2963 { 0x04, 0xf800 },
2964 { 0x04, 0x0000 },
2965 { 0x1f, 0x0000 },
2966
2967 { 0x1f, 0x0001 },
2968 { 0x10, 0xf41b },
2969 { 0x14, 0xfb54 },
2970 { 0x18, 0xf5c7 },
2971 { 0x1f, 0x0000 },
2972
2973 { 0x1f, 0x0001 },
2974 { 0x17, 0x0cc0 },
2975 { 0x1f, 0x0000 }
2976 };
2977
françois romieu4da19632011-01-03 15:07:55 +00002978 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002979
françois romieu4da19632011-01-03 15:07:55 +00002980 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002981}
2982
françois romieu4da19632011-01-03 15:07:55 +00002983static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002984{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002985 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002986 { 0x1f, 0x0001 },
2987 { 0x04, 0x0000 },
2988 { 0x03, 0x00a1 },
2989 { 0x02, 0x0008 },
2990 { 0x01, 0x0120 },
2991 { 0x00, 0x1000 },
2992 { 0x04, 0x0800 },
2993 { 0x04, 0x9000 },
2994 { 0x03, 0x802f },
2995 { 0x02, 0x4f02 },
2996 { 0x01, 0x0409 },
2997 { 0x00, 0xf099 },
2998 { 0x04, 0x9800 },
2999 { 0x04, 0xa000 },
3000 { 0x03, 0xdf01 },
3001 { 0x02, 0xdf20 },
3002 { 0x01, 0xff95 },
3003 { 0x00, 0xba00 },
3004 { 0x04, 0xa800 },
3005 { 0x04, 0xf000 },
3006 { 0x03, 0xdf01 },
3007 { 0x02, 0xdf20 },
3008 { 0x01, 0x101a },
3009 { 0x00, 0xa0ff },
3010 { 0x04, 0xf800 },
3011 { 0x04, 0x0000 },
3012 { 0x1f, 0x0000 },
3013
3014 { 0x1f, 0x0001 },
3015 { 0x0b, 0x8480 },
3016 { 0x1f, 0x0000 },
3017
3018 { 0x1f, 0x0001 },
3019 { 0x18, 0x67c7 },
3020 { 0x04, 0x2000 },
3021 { 0x03, 0x002f },
3022 { 0x02, 0x4360 },
3023 { 0x01, 0x0109 },
3024 { 0x00, 0x3022 },
3025 { 0x04, 0x2800 },
3026 { 0x1f, 0x0000 },
3027
3028 { 0x1f, 0x0001 },
3029 { 0x17, 0x0cc0 },
3030 { 0x1f, 0x0000 }
3031 };
3032
françois romieu4da19632011-01-03 15:07:55 +00003033 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00003034}
3035
françois romieu4da19632011-01-03 15:07:55 +00003036static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003037{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003038 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003039 { 0x10, 0xf41b },
3040 { 0x1f, 0x0000 }
3041 };
3042
françois romieu4da19632011-01-03 15:07:55 +00003043 rtl_writephy(tp, 0x1f, 0x0001);
3044 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02003045
françois romieu4da19632011-01-03 15:07:55 +00003046 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003047}
3048
françois romieu4da19632011-01-03 15:07:55 +00003049static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003050{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003051 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003052 { 0x1f, 0x0001 },
3053 { 0x10, 0xf41b },
3054 { 0x1f, 0x0000 }
3055 };
3056
françois romieu4da19632011-01-03 15:07:55 +00003057 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003058}
3059
françois romieu4da19632011-01-03 15:07:55 +00003060static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003061{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003062 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02003063 { 0x1f, 0x0000 },
3064 { 0x1d, 0x0f00 },
3065 { 0x1f, 0x0002 },
3066 { 0x0c, 0x1ec8 },
3067 { 0x1f, 0x0000 }
3068 };
3069
françois romieu4da19632011-01-03 15:07:55 +00003070 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02003071}
3072
françois romieu4da19632011-01-03 15:07:55 +00003073static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02003074{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003075 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02003076 { 0x1f, 0x0001 },
3077 { 0x1d, 0x3d98 },
3078 { 0x1f, 0x0000 }
3079 };
3080
françois romieu4da19632011-01-03 15:07:55 +00003081 rtl_writephy(tp, 0x1f, 0x0000);
3082 rtl_patchphy(tp, 0x14, 1 << 5);
3083 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02003084
françois romieu4da19632011-01-03 15:07:55 +00003085 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02003086}
3087
françois romieu4da19632011-01-03 15:07:55 +00003088static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003089{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003090 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02003091 { 0x1f, 0x0001 },
3092 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02003093 { 0x1f, 0x0002 },
3094 { 0x00, 0x88d4 },
3095 { 0x01, 0x82b1 },
3096 { 0x03, 0x7002 },
3097 { 0x08, 0x9e30 },
3098 { 0x09, 0x01f0 },
3099 { 0x0a, 0x5500 },
3100 { 0x0c, 0x00c8 },
3101 { 0x1f, 0x0003 },
3102 { 0x12, 0xc096 },
3103 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02003104 { 0x1f, 0x0000 },
3105 { 0x1f, 0x0000 },
3106 { 0x09, 0x2000 },
3107 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02003108 };
3109
françois romieu4da19632011-01-03 15:07:55 +00003110 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003111
françois romieu4da19632011-01-03 15:07:55 +00003112 rtl_patchphy(tp, 0x14, 1 << 5);
3113 rtl_patchphy(tp, 0x0d, 1 << 5);
3114 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02003115}
3116
françois romieu4da19632011-01-03 15:07:55 +00003117static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02003118{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003119 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02003120 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003121 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003122 { 0x03, 0x802f },
3123 { 0x02, 0x4f02 },
3124 { 0x01, 0x0409 },
3125 { 0x00, 0xf099 },
3126 { 0x04, 0x9800 },
3127 { 0x04, 0x9000 },
3128 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003129 { 0x1f, 0x0002 },
3130 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003131 { 0x06, 0x0761 },
3132 { 0x1f, 0x0003 },
3133 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003134 { 0x1f, 0x0000 }
3135 };
3136
françois romieu4da19632011-01-03 15:07:55 +00003137 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003138
françois romieu4da19632011-01-03 15:07:55 +00003139 rtl_patchphy(tp, 0x16, 1 << 0);
3140 rtl_patchphy(tp, 0x14, 1 << 5);
3141 rtl_patchphy(tp, 0x0d, 1 << 5);
3142 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003143}
3144
françois romieu4da19632011-01-03 15:07:55 +00003145static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02003146{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003147 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02003148 { 0x1f, 0x0001 },
3149 { 0x12, 0x2300 },
3150 { 0x1d, 0x3d98 },
3151 { 0x1f, 0x0002 },
3152 { 0x0c, 0x7eb8 },
3153 { 0x06, 0x5461 },
3154 { 0x1f, 0x0003 },
3155 { 0x16, 0x0f0a },
3156 { 0x1f, 0x0000 }
3157 };
3158
françois romieu4da19632011-01-03 15:07:55 +00003159 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02003160
françois romieu4da19632011-01-03 15:07:55 +00003161 rtl_patchphy(tp, 0x16, 1 << 0);
3162 rtl_patchphy(tp, 0x14, 1 << 5);
3163 rtl_patchphy(tp, 0x0d, 1 << 5);
3164 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003165}
3166
françois romieu4da19632011-01-03 15:07:55 +00003167static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003168{
françois romieu4da19632011-01-03 15:07:55 +00003169 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003170}
3171
françois romieubca03d52011-01-03 15:07:31 +00003172static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003173{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003174 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003175 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003176 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003177 { 0x06, 0x4064 },
3178 { 0x07, 0x2863 },
3179 { 0x08, 0x059c },
3180 { 0x09, 0x26b4 },
3181 { 0x0a, 0x6a19 },
3182 { 0x0b, 0xdcc8 },
3183 { 0x10, 0xf06d },
3184 { 0x14, 0x7f68 },
3185 { 0x18, 0x7fd9 },
3186 { 0x1c, 0xf0ff },
3187 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003188 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003189 { 0x12, 0xf49f },
3190 { 0x13, 0x070b },
3191 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003192 { 0x14, 0x94c0 },
3193
3194 /*
3195 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003196 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003197 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003198 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003199 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003200 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003201 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003202 { 0x06, 0x5561 },
3203
3204 /*
3205 * Can not link to 1Gbps with bad cable
3206 * Decrease SNR threshold form 21.07dB to 19.04dB
3207 */
3208 { 0x1f, 0x0001 },
3209 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003210
3211 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003212 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003213 };
3214
françois romieu4da19632011-01-03 15:07:55 +00003215 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003216
françois romieubca03d52011-01-03 15:07:31 +00003217 /*
3218 * Rx Error Issue
3219 * Fine Tune Switching regulator parameter
3220 */
françois romieu4da19632011-01-03 15:07:55 +00003221 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003222 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3223 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003224
Francois Romieufdf6fc02012-07-06 22:40:38 +02003225 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003226 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003227 { 0x1f, 0x0002 },
3228 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003229 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003230 { 0x05, 0x8330 },
3231 { 0x06, 0x669a },
3232 { 0x1f, 0x0002 }
3233 };
3234 int val;
3235
françois romieu4da19632011-01-03 15:07:55 +00003236 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003237
françois romieu4da19632011-01-03 15:07:55 +00003238 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003239
3240 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003241 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003242 0x0065, 0x0066, 0x0067, 0x0068,
3243 0x0069, 0x006a, 0x006b, 0x006c
3244 };
3245 int i;
3246
françois romieu4da19632011-01-03 15:07:55 +00003247 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003248
3249 val &= 0xff00;
3250 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003251 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003252 }
3253 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003254 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003255 { 0x1f, 0x0002 },
3256 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003257 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003258 { 0x05, 0x8330 },
3259 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003260 };
3261
françois romieu4da19632011-01-03 15:07:55 +00003262 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003263 }
3264
françois romieubca03d52011-01-03 15:07:31 +00003265 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003266 rtl_writephy(tp, 0x1f, 0x0002);
3267 rtl_patchphy(tp, 0x0d, 0x0300);
3268 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003269
françois romieubca03d52011-01-03 15:07:31 +00003270 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003271 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003272 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3273 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003274
françois romieu4da19632011-01-03 15:07:55 +00003275 rtl_writephy(tp, 0x1f, 0x0005);
3276 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003277
3278 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003279
françois romieu4da19632011-01-03 15:07:55 +00003280 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003281}
3282
françois romieubca03d52011-01-03 15:07:31 +00003283static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003284{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003285 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003286 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003287 { 0x1f, 0x0001 },
3288 { 0x06, 0x4064 },
3289 { 0x07, 0x2863 },
3290 { 0x08, 0x059c },
3291 { 0x09, 0x26b4 },
3292 { 0x0a, 0x6a19 },
3293 { 0x0b, 0xdcc8 },
3294 { 0x10, 0xf06d },
3295 { 0x14, 0x7f68 },
3296 { 0x18, 0x7fd9 },
3297 { 0x1c, 0xf0ff },
3298 { 0x1d, 0x3d9c },
3299 { 0x1f, 0x0003 },
3300 { 0x12, 0xf49f },
3301 { 0x13, 0x070b },
3302 { 0x1a, 0x05ad },
3303 { 0x14, 0x94c0 },
3304
françois romieubca03d52011-01-03 15:07:31 +00003305 /*
3306 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003307 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003308 */
françois romieudaf9df62009-10-07 12:44:20 +00003309 { 0x1f, 0x0002 },
3310 { 0x06, 0x5561 },
3311 { 0x1f, 0x0005 },
3312 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003313 { 0x06, 0x5561 },
3314
3315 /*
3316 * Can not link to 1Gbps with bad cable
3317 * Decrease SNR threshold form 21.07dB to 19.04dB
3318 */
3319 { 0x1f, 0x0001 },
3320 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003321
3322 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003323 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003324 };
3325
françois romieu4da19632011-01-03 15:07:55 +00003326 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003327
Francois Romieufdf6fc02012-07-06 22:40:38 +02003328 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003329 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003330 { 0x1f, 0x0002 },
3331 { 0x05, 0x669a },
3332 { 0x1f, 0x0005 },
3333 { 0x05, 0x8330 },
3334 { 0x06, 0x669a },
3335
3336 { 0x1f, 0x0002 }
3337 };
3338 int val;
3339
françois romieu4da19632011-01-03 15:07:55 +00003340 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003341
françois romieu4da19632011-01-03 15:07:55 +00003342 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003343 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003344 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003345 0x0065, 0x0066, 0x0067, 0x0068,
3346 0x0069, 0x006a, 0x006b, 0x006c
3347 };
3348 int i;
3349
françois romieu4da19632011-01-03 15:07:55 +00003350 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003351
3352 val &= 0xff00;
3353 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003354 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003355 }
3356 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003357 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003358 { 0x1f, 0x0002 },
3359 { 0x05, 0x2642 },
3360 { 0x1f, 0x0005 },
3361 { 0x05, 0x8330 },
3362 { 0x06, 0x2642 }
3363 };
3364
françois romieu4da19632011-01-03 15:07:55 +00003365 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003366 }
3367
françois romieubca03d52011-01-03 15:07:31 +00003368 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003369 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003370 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3371 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003372
françois romieubca03d52011-01-03 15:07:31 +00003373 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003374 rtl_writephy(tp, 0x1f, 0x0002);
3375 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003376
françois romieu4da19632011-01-03 15:07:55 +00003377 rtl_writephy(tp, 0x1f, 0x0005);
3378 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003379
3380 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003381
françois romieu4da19632011-01-03 15:07:55 +00003382 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003383}
3384
françois romieu4da19632011-01-03 15:07:55 +00003385static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003386{
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 { 0x10, 0x0008 },
3390 { 0x0d, 0x006c },
3391
3392 { 0x1f, 0x0000 },
3393 { 0x0d, 0xf880 },
3394
3395 { 0x1f, 0x0001 },
3396 { 0x17, 0x0cc0 },
3397
3398 { 0x1f, 0x0001 },
3399 { 0x0b, 0xa4d8 },
3400 { 0x09, 0x281c },
3401 { 0x07, 0x2883 },
3402 { 0x0a, 0x6b35 },
3403 { 0x1d, 0x3da4 },
3404 { 0x1c, 0xeffd },
3405 { 0x14, 0x7f52 },
3406 { 0x18, 0x7fc6 },
3407 { 0x08, 0x0601 },
3408 { 0x06, 0x4063 },
3409 { 0x10, 0xf074 },
3410 { 0x1f, 0x0003 },
3411 { 0x13, 0x0789 },
3412 { 0x12, 0xf4bd },
3413 { 0x1a, 0x04fd },
3414 { 0x14, 0x84b0 },
3415 { 0x1f, 0x0000 },
3416 { 0x00, 0x9200 },
3417
3418 { 0x1f, 0x0005 },
3419 { 0x01, 0x0340 },
3420 { 0x1f, 0x0001 },
3421 { 0x04, 0x4000 },
3422 { 0x03, 0x1d21 },
3423 { 0x02, 0x0c32 },
3424 { 0x01, 0x0200 },
3425 { 0x00, 0x5554 },
3426 { 0x04, 0x4800 },
3427 { 0x04, 0x4000 },
3428 { 0x04, 0xf000 },
3429 { 0x03, 0xdf01 },
3430 { 0x02, 0xdf20 },
3431 { 0x01, 0x101a },
3432 { 0x00, 0xa0ff },
3433 { 0x04, 0xf800 },
3434 { 0x04, 0xf000 },
3435 { 0x1f, 0x0000 },
3436
3437 { 0x1f, 0x0007 },
3438 { 0x1e, 0x0023 },
3439 { 0x16, 0x0000 },
3440 { 0x1f, 0x0000 }
3441 };
3442
françois romieu4da19632011-01-03 15:07:55 +00003443 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003444}
3445
françois romieue6de30d2011-01-03 15:08:37 +00003446static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3447{
3448 static const struct phy_reg phy_reg_init[] = {
3449 { 0x1f, 0x0001 },
3450 { 0x17, 0x0cc0 },
3451
3452 { 0x1f, 0x0007 },
3453 { 0x1e, 0x002d },
3454 { 0x18, 0x0040 },
3455 { 0x1f, 0x0000 }
3456 };
3457
3458 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3459 rtl_patchphy(tp, 0x0d, 1 << 5);
3460}
3461
Hayes Wang70090422011-07-06 15:58:06 +08003462static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003463{
3464 static const struct phy_reg phy_reg_init[] = {
3465 /* Enable Delay cap */
3466 { 0x1f, 0x0005 },
3467 { 0x05, 0x8b80 },
3468 { 0x06, 0xc896 },
3469 { 0x1f, 0x0000 },
3470
3471 /* Channel estimation fine tune */
3472 { 0x1f, 0x0001 },
3473 { 0x0b, 0x6c20 },
3474 { 0x07, 0x2872 },
3475 { 0x1c, 0xefff },
3476 { 0x1f, 0x0003 },
3477 { 0x14, 0x6420 },
3478 { 0x1f, 0x0000 },
3479
3480 /* Update PFM & 10M TX idle timer */
3481 { 0x1f, 0x0007 },
3482 { 0x1e, 0x002f },
3483 { 0x15, 0x1919 },
3484 { 0x1f, 0x0000 },
3485
3486 { 0x1f, 0x0007 },
3487 { 0x1e, 0x00ac },
3488 { 0x18, 0x0006 },
3489 { 0x1f, 0x0000 }
3490 };
3491
Francois Romieu15ecd032011-04-27 13:52:22 -07003492 rtl_apply_firmware(tp);
3493
hayeswang01dc7fe2011-03-21 01:50:28 +00003494 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3495
3496 /* DCO enable for 10M IDLE Power */
3497 rtl_writephy(tp, 0x1f, 0x0007);
3498 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003499 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003500 rtl_writephy(tp, 0x1f, 0x0000);
3501
3502 /* For impedance matching */
3503 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003504 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003505 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003506
3507 /* PHY auto speed down */
3508 rtl_writephy(tp, 0x1f, 0x0007);
3509 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003510 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003511 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003512 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003513
3514 rtl_writephy(tp, 0x1f, 0x0005);
3515 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003516 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003517 rtl_writephy(tp, 0x1f, 0x0000);
3518
3519 rtl_writephy(tp, 0x1f, 0x0005);
3520 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003521 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003522 rtl_writephy(tp, 0x1f, 0x0007);
3523 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003524 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003525 rtl_writephy(tp, 0x1f, 0x0006);
3526 rtl_writephy(tp, 0x00, 0x5a00);
3527 rtl_writephy(tp, 0x1f, 0x0000);
3528 rtl_writephy(tp, 0x0d, 0x0007);
3529 rtl_writephy(tp, 0x0e, 0x003c);
3530 rtl_writephy(tp, 0x0d, 0x4007);
3531 rtl_writephy(tp, 0x0e, 0x0000);
3532 rtl_writephy(tp, 0x0d, 0x0000);
3533}
3534
françois romieu9ecb9aa2012-12-07 11:20:21 +00003535static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3536{
3537 const u16 w[] = {
3538 addr[0] | (addr[1] << 8),
3539 addr[2] | (addr[3] << 8),
3540 addr[4] | (addr[5] << 8)
3541 };
3542 const struct exgmac_reg e[] = {
3543 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3544 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3545 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3546 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3547 };
3548
3549 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3550}
3551
Hayes Wang70090422011-07-06 15:58:06 +08003552static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3553{
3554 static const struct phy_reg phy_reg_init[] = {
3555 /* Enable Delay cap */
3556 { 0x1f, 0x0004 },
3557 { 0x1f, 0x0007 },
3558 { 0x1e, 0x00ac },
3559 { 0x18, 0x0006 },
3560 { 0x1f, 0x0002 },
3561 { 0x1f, 0x0000 },
3562 { 0x1f, 0x0000 },
3563
3564 /* Channel estimation fine tune */
3565 { 0x1f, 0x0003 },
3566 { 0x09, 0xa20f },
3567 { 0x1f, 0x0000 },
3568 { 0x1f, 0x0000 },
3569
3570 /* Green Setting */
3571 { 0x1f, 0x0005 },
3572 { 0x05, 0x8b5b },
3573 { 0x06, 0x9222 },
3574 { 0x05, 0x8b6d },
3575 { 0x06, 0x8000 },
3576 { 0x05, 0x8b76 },
3577 { 0x06, 0x8000 },
3578 { 0x1f, 0x0000 }
3579 };
3580
3581 rtl_apply_firmware(tp);
3582
3583 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3584
3585 /* For 4-corner performance improve */
3586 rtl_writephy(tp, 0x1f, 0x0005);
3587 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003588 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003589 rtl_writephy(tp, 0x1f, 0x0000);
3590
3591 /* PHY auto speed down */
3592 rtl_writephy(tp, 0x1f, 0x0004);
3593 rtl_writephy(tp, 0x1f, 0x0007);
3594 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003595 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003596 rtl_writephy(tp, 0x1f, 0x0002);
3597 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003598 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003599
3600 /* improve 10M EEE waveform */
3601 rtl_writephy(tp, 0x1f, 0x0005);
3602 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003603 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003604 rtl_writephy(tp, 0x1f, 0x0000);
3605
3606 /* Improve 2-pair detection performance */
3607 rtl_writephy(tp, 0x1f, 0x0005);
3608 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003609 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003610 rtl_writephy(tp, 0x1f, 0x0000);
3611
3612 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003613 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003614 rtl_writephy(tp, 0x1f, 0x0005);
3615 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003616 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003617 rtl_writephy(tp, 0x1f, 0x0004);
3618 rtl_writephy(tp, 0x1f, 0x0007);
3619 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003620 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003621 rtl_writephy(tp, 0x1f, 0x0002);
3622 rtl_writephy(tp, 0x1f, 0x0000);
3623 rtl_writephy(tp, 0x0d, 0x0007);
3624 rtl_writephy(tp, 0x0e, 0x003c);
3625 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003626 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003627 rtl_writephy(tp, 0x0d, 0x0000);
3628
3629 /* Green feature */
3630 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003631 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3632 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003633 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003634 rtl_writephy(tp, 0x1f, 0x0005);
3635 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3636 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003637
françois romieu9ecb9aa2012-12-07 11:20:21 +00003638 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3639 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003640}
3641
Hayes Wang5f886e02012-03-30 14:33:03 +08003642static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3643{
3644 /* For 4-corner performance improve */
3645 rtl_writephy(tp, 0x1f, 0x0005);
3646 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003647 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003648 rtl_writephy(tp, 0x1f, 0x0000);
3649
3650 /* PHY auto speed down */
3651 rtl_writephy(tp, 0x1f, 0x0007);
3652 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003653 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003654 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003655 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003656
3657 /* Improve 10M EEE waveform */
3658 rtl_writephy(tp, 0x1f, 0x0005);
3659 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003660 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003661 rtl_writephy(tp, 0x1f, 0x0000);
3662}
3663
Hayes Wangc2218922011-09-06 16:55:18 +08003664static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3665{
3666 static const struct phy_reg phy_reg_init[] = {
3667 /* Channel estimation fine tune */
3668 { 0x1f, 0x0003 },
3669 { 0x09, 0xa20f },
3670 { 0x1f, 0x0000 },
3671
3672 /* Modify green table for giga & fnet */
3673 { 0x1f, 0x0005 },
3674 { 0x05, 0x8b55 },
3675 { 0x06, 0x0000 },
3676 { 0x05, 0x8b5e },
3677 { 0x06, 0x0000 },
3678 { 0x05, 0x8b67 },
3679 { 0x06, 0x0000 },
3680 { 0x05, 0x8b70 },
3681 { 0x06, 0x0000 },
3682 { 0x1f, 0x0000 },
3683 { 0x1f, 0x0007 },
3684 { 0x1e, 0x0078 },
3685 { 0x17, 0x0000 },
3686 { 0x19, 0x00fb },
3687 { 0x1f, 0x0000 },
3688
3689 /* Modify green table for 10M */
3690 { 0x1f, 0x0005 },
3691 { 0x05, 0x8b79 },
3692 { 0x06, 0xaa00 },
3693 { 0x1f, 0x0000 },
3694
3695 /* Disable hiimpedance detection (RTCT) */
3696 { 0x1f, 0x0003 },
3697 { 0x01, 0x328a },
3698 { 0x1f, 0x0000 }
3699 };
3700
3701 rtl_apply_firmware(tp);
3702
3703 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3704
Hayes Wang5f886e02012-03-30 14:33:03 +08003705 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003706
3707 /* Improve 2-pair detection performance */
3708 rtl_writephy(tp, 0x1f, 0x0005);
3709 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003710 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003711 rtl_writephy(tp, 0x1f, 0x0000);
3712}
3713
3714static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3715{
3716 rtl_apply_firmware(tp);
3717
Hayes Wang5f886e02012-03-30 14:33:03 +08003718 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003719}
3720
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003721static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3722{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003723 static const struct phy_reg phy_reg_init[] = {
3724 /* Channel estimation fine tune */
3725 { 0x1f, 0x0003 },
3726 { 0x09, 0xa20f },
3727 { 0x1f, 0x0000 },
3728
3729 /* Modify green table for giga & fnet */
3730 { 0x1f, 0x0005 },
3731 { 0x05, 0x8b55 },
3732 { 0x06, 0x0000 },
3733 { 0x05, 0x8b5e },
3734 { 0x06, 0x0000 },
3735 { 0x05, 0x8b67 },
3736 { 0x06, 0x0000 },
3737 { 0x05, 0x8b70 },
3738 { 0x06, 0x0000 },
3739 { 0x1f, 0x0000 },
3740 { 0x1f, 0x0007 },
3741 { 0x1e, 0x0078 },
3742 { 0x17, 0x0000 },
3743 { 0x19, 0x00aa },
3744 { 0x1f, 0x0000 },
3745
3746 /* Modify green table for 10M */
3747 { 0x1f, 0x0005 },
3748 { 0x05, 0x8b79 },
3749 { 0x06, 0xaa00 },
3750 { 0x1f, 0x0000 },
3751
3752 /* Disable hiimpedance detection (RTCT) */
3753 { 0x1f, 0x0003 },
3754 { 0x01, 0x328a },
3755 { 0x1f, 0x0000 }
3756 };
3757
3758
3759 rtl_apply_firmware(tp);
3760
3761 rtl8168f_hw_phy_config(tp);
3762
3763 /* Improve 2-pair detection performance */
3764 rtl_writephy(tp, 0x1f, 0x0005);
3765 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003766 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003767 rtl_writephy(tp, 0x1f, 0x0000);
3768
3769 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3770
3771 /* Modify green table for giga */
3772 rtl_writephy(tp, 0x1f, 0x0005);
3773 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003774 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003775 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003776 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003777 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003778 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003779 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003780 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003781 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003782 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003783 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003784 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003785 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003786 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003787 rtl_writephy(tp, 0x1f, 0x0000);
3788
3789 /* uc same-seed solution */
3790 rtl_writephy(tp, 0x1f, 0x0005);
3791 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003792 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003793 rtl_writephy(tp, 0x1f, 0x0000);
3794
3795 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003796 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003797 rtl_writephy(tp, 0x1f, 0x0005);
3798 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003799 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003800 rtl_writephy(tp, 0x1f, 0x0004);
3801 rtl_writephy(tp, 0x1f, 0x0007);
3802 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003803 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003804 rtl_writephy(tp, 0x1f, 0x0000);
3805 rtl_writephy(tp, 0x0d, 0x0007);
3806 rtl_writephy(tp, 0x0e, 0x003c);
3807 rtl_writephy(tp, 0x0d, 0x4007);
3808 rtl_writephy(tp, 0x0e, 0x0000);
3809 rtl_writephy(tp, 0x0d, 0x0000);
3810
3811 /* Green feature */
3812 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003813 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3814 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003815 rtl_writephy(tp, 0x1f, 0x0000);
3816}
3817
Hayes Wangc5583862012-07-02 17:23:22 +08003818static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3819{
Hayes Wangc5583862012-07-02 17:23:22 +08003820 rtl_apply_firmware(tp);
3821
hayeswang41f44d12013-04-01 22:23:36 +00003822 rtl_writephy(tp, 0x1f, 0x0a46);
3823 if (rtl_readphy(tp, 0x10) & 0x0100) {
3824 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003825 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003826 } else {
3827 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003828 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003829 }
Hayes Wangc5583862012-07-02 17:23:22 +08003830
hayeswang41f44d12013-04-01 22:23:36 +00003831 rtl_writephy(tp, 0x1f, 0x0a46);
3832 if (rtl_readphy(tp, 0x13) & 0x0100) {
3833 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003834 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003835 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003836 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003837 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003838 }
Hayes Wangc5583862012-07-02 17:23:22 +08003839
hayeswang41f44d12013-04-01 22:23:36 +00003840 /* Enable PHY auto speed down */
3841 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003842 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003843
hayeswangfe7524c2013-04-01 22:23:37 +00003844 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003845 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003846 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003847 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003848 rtl_writephy(tp, 0x1f, 0x0a43);
3849 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003850 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3851 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003852
hayeswang41f44d12013-04-01 22:23:36 +00003853 /* EEE auto-fallback function */
3854 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003855 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003856
hayeswang41f44d12013-04-01 22:23:36 +00003857 /* Enable UC LPF tune function */
3858 rtl_writephy(tp, 0x1f, 0x0a43);
3859 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003860 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003861
3862 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003863 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003864
hayeswangfe7524c2013-04-01 22:23:37 +00003865 /* Improve SWR Efficiency */
3866 rtl_writephy(tp, 0x1f, 0x0bcd);
3867 rtl_writephy(tp, 0x14, 0x5065);
3868 rtl_writephy(tp, 0x14, 0xd065);
3869 rtl_writephy(tp, 0x1f, 0x0bc8);
3870 rtl_writephy(tp, 0x11, 0x5655);
3871 rtl_writephy(tp, 0x1f, 0x0bcd);
3872 rtl_writephy(tp, 0x14, 0x1065);
3873 rtl_writephy(tp, 0x14, 0x9065);
3874 rtl_writephy(tp, 0x14, 0x1065);
3875
David Chang1bac1072013-11-27 15:48:36 +08003876 /* Check ALDPS bit, disable it if enabled */
3877 rtl_writephy(tp, 0x1f, 0x0a43);
3878 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003879 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08003880
hayeswang41f44d12013-04-01 22:23:36 +00003881 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003882}
3883
hayeswang57538c42013-04-01 22:23:40 +00003884static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3885{
3886 rtl_apply_firmware(tp);
3887}
3888
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003889static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3890{
3891 u16 dout_tapbin;
3892 u32 data;
3893
3894 rtl_apply_firmware(tp);
3895
3896 /* CHN EST parameters adjust - giga master */
3897 rtl_writephy(tp, 0x1f, 0x0a43);
3898 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003899 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003900 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003901 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003902 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003903 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003904 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003905 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003906 rtl_writephy(tp, 0x1f, 0x0000);
3907
3908 /* CHN EST parameters adjust - giga slave */
3909 rtl_writephy(tp, 0x1f, 0x0a43);
3910 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003911 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003912 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003913 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003914 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003915 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003916 rtl_writephy(tp, 0x1f, 0x0000);
3917
3918 /* CHN EST parameters adjust - fnet */
3919 rtl_writephy(tp, 0x1f, 0x0a43);
3920 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003921 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003922 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003923 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003924 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003925 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003926 rtl_writephy(tp, 0x1f, 0x0000);
3927
3928 /* enable R-tune & PGA-retune function */
3929 dout_tapbin = 0;
3930 rtl_writephy(tp, 0x1f, 0x0a46);
3931 data = rtl_readphy(tp, 0x13);
3932 data &= 3;
3933 data <<= 2;
3934 dout_tapbin |= data;
3935 data = rtl_readphy(tp, 0x12);
3936 data &= 0xc000;
3937 data >>= 14;
3938 dout_tapbin |= data;
3939 dout_tapbin = ~(dout_tapbin^0x08);
3940 dout_tapbin <<= 12;
3941 dout_tapbin &= 0xf000;
3942 rtl_writephy(tp, 0x1f, 0x0a43);
3943 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003944 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003945 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003946 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003947 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003948 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003949 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003950 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003951
3952 rtl_writephy(tp, 0x1f, 0x0a43);
3953 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003954 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003955 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003956 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003957 rtl_writephy(tp, 0x1f, 0x0000);
3958
3959 /* enable GPHY 10M */
3960 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003961 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003962 rtl_writephy(tp, 0x1f, 0x0000);
3963
3964 /* SAR ADC performance */
3965 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003966 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003967 rtl_writephy(tp, 0x1f, 0x0000);
3968
3969 rtl_writephy(tp, 0x1f, 0x0a43);
3970 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003971 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003972 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003973 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003974 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003975 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003976 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003977 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003978 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003979 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003980 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003981 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003982 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003983 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003984 rtl_writephy(tp, 0x1f, 0x0000);
3985
3986 /* disable phy pfm mode */
3987 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003988 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003989 rtl_writephy(tp, 0x1f, 0x0000);
3990
3991 /* Check ALDPS bit, disable it if enabled */
3992 rtl_writephy(tp, 0x1f, 0x0a43);
3993 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003994 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003995
3996 rtl_writephy(tp, 0x1f, 0x0000);
3997}
3998
3999static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
4000{
4001 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
4002 u16 rlen;
4003 u32 data;
4004
4005 rtl_apply_firmware(tp);
4006
4007 /* CHIN EST parameter update */
4008 rtl_writephy(tp, 0x1f, 0x0a43);
4009 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004010 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004011 rtl_writephy(tp, 0x1f, 0x0000);
4012
4013 /* enable R-tune & PGA-retune function */
4014 rtl_writephy(tp, 0x1f, 0x0a43);
4015 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004016 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004017 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004018 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004019 rtl_writephy(tp, 0x1f, 0x0000);
4020
4021 /* enable GPHY 10M */
4022 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004023 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004024 rtl_writephy(tp, 0x1f, 0x0000);
4025
4026 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
4027 data = r8168_mac_ocp_read(tp, 0xdd02);
4028 ioffset_p3 = ((data & 0x80)>>7);
4029 ioffset_p3 <<= 3;
4030
4031 data = r8168_mac_ocp_read(tp, 0xdd00);
4032 ioffset_p3 |= ((data & (0xe000))>>13);
4033 ioffset_p2 = ((data & (0x1e00))>>9);
4034 ioffset_p1 = ((data & (0x01e0))>>5);
4035 ioffset_p0 = ((data & 0x0010)>>4);
4036 ioffset_p0 <<= 3;
4037 ioffset_p0 |= (data & (0x07));
4038 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
4039
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004040 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08004041 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004042 rtl_writephy(tp, 0x1f, 0x0bcf);
4043 rtl_writephy(tp, 0x16, data);
4044 rtl_writephy(tp, 0x1f, 0x0000);
4045 }
4046
4047 /* Modify rlen (TX LPF corner frequency) level */
4048 rtl_writephy(tp, 0x1f, 0x0bcd);
4049 data = rtl_readphy(tp, 0x16);
4050 data &= 0x000f;
4051 rlen = 0;
4052 if (data > 3)
4053 rlen = data - 3;
4054 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
4055 rtl_writephy(tp, 0x17, data);
4056 rtl_writephy(tp, 0x1f, 0x0bcd);
4057 rtl_writephy(tp, 0x1f, 0x0000);
4058
4059 /* disable phy pfm mode */
4060 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004061 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004062 rtl_writephy(tp, 0x1f, 0x0000);
4063
4064 /* Check ALDPS bit, disable it if enabled */
4065 rtl_writephy(tp, 0x1f, 0x0a43);
4066 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004067 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004068
4069 rtl_writephy(tp, 0x1f, 0x0000);
4070}
4071
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004072static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4073{
4074 /* Enable PHY auto speed down */
4075 rtl_writephy(tp, 0x1f, 0x0a44);
4076 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4077 rtl_writephy(tp, 0x1f, 0x0000);
4078
4079 /* patch 10M & ALDPS */
4080 rtl_writephy(tp, 0x1f, 0x0bcc);
4081 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4082 rtl_writephy(tp, 0x1f, 0x0a44);
4083 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4084 rtl_writephy(tp, 0x1f, 0x0a43);
4085 rtl_writephy(tp, 0x13, 0x8084);
4086 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4087 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4088 rtl_writephy(tp, 0x1f, 0x0000);
4089
4090 /* Enable EEE auto-fallback function */
4091 rtl_writephy(tp, 0x1f, 0x0a4b);
4092 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4093 rtl_writephy(tp, 0x1f, 0x0000);
4094
4095 /* Enable UC LPF tune function */
4096 rtl_writephy(tp, 0x1f, 0x0a43);
4097 rtl_writephy(tp, 0x13, 0x8012);
4098 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4099 rtl_writephy(tp, 0x1f, 0x0000);
4100
4101 /* set rg_sel_sdm_rate */
4102 rtl_writephy(tp, 0x1f, 0x0c42);
4103 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4104 rtl_writephy(tp, 0x1f, 0x0000);
4105
4106 /* Check ALDPS bit, disable it if enabled */
4107 rtl_writephy(tp, 0x1f, 0x0a43);
4108 if (rtl_readphy(tp, 0x10) & 0x0004)
4109 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4110
4111 rtl_writephy(tp, 0x1f, 0x0000);
4112}
4113
4114static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4115{
4116 /* patch 10M & ALDPS */
4117 rtl_writephy(tp, 0x1f, 0x0bcc);
4118 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4119 rtl_writephy(tp, 0x1f, 0x0a44);
4120 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4121 rtl_writephy(tp, 0x1f, 0x0a43);
4122 rtl_writephy(tp, 0x13, 0x8084);
4123 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4124 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4125 rtl_writephy(tp, 0x1f, 0x0000);
4126
4127 /* Enable UC LPF tune function */
4128 rtl_writephy(tp, 0x1f, 0x0a43);
4129 rtl_writephy(tp, 0x13, 0x8012);
4130 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4131 rtl_writephy(tp, 0x1f, 0x0000);
4132
4133 /* Set rg_sel_sdm_rate */
4134 rtl_writephy(tp, 0x1f, 0x0c42);
4135 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4136 rtl_writephy(tp, 0x1f, 0x0000);
4137
4138 /* Channel estimation parameters */
4139 rtl_writephy(tp, 0x1f, 0x0a43);
4140 rtl_writephy(tp, 0x13, 0x80f3);
4141 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4142 rtl_writephy(tp, 0x13, 0x80f0);
4143 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4144 rtl_writephy(tp, 0x13, 0x80ef);
4145 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4146 rtl_writephy(tp, 0x13, 0x80f6);
4147 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4148 rtl_writephy(tp, 0x13, 0x80ec);
4149 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4150 rtl_writephy(tp, 0x13, 0x80ed);
4151 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4152 rtl_writephy(tp, 0x13, 0x80f2);
4153 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4154 rtl_writephy(tp, 0x13, 0x80f4);
4155 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4156 rtl_writephy(tp, 0x1f, 0x0a43);
4157 rtl_writephy(tp, 0x13, 0x8110);
4158 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4159 rtl_writephy(tp, 0x13, 0x810f);
4160 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4161 rtl_writephy(tp, 0x13, 0x8111);
4162 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4163 rtl_writephy(tp, 0x13, 0x8113);
4164 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4165 rtl_writephy(tp, 0x13, 0x8115);
4166 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4167 rtl_writephy(tp, 0x13, 0x810e);
4168 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4169 rtl_writephy(tp, 0x13, 0x810c);
4170 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4171 rtl_writephy(tp, 0x13, 0x810b);
4172 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4173 rtl_writephy(tp, 0x1f, 0x0a43);
4174 rtl_writephy(tp, 0x13, 0x80d1);
4175 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4176 rtl_writephy(tp, 0x13, 0x80cd);
4177 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4178 rtl_writephy(tp, 0x13, 0x80d3);
4179 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4180 rtl_writephy(tp, 0x13, 0x80d5);
4181 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4182 rtl_writephy(tp, 0x13, 0x80d7);
4183 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4184
4185 /* Force PWM-mode */
4186 rtl_writephy(tp, 0x1f, 0x0bcd);
4187 rtl_writephy(tp, 0x14, 0x5065);
4188 rtl_writephy(tp, 0x14, 0xd065);
4189 rtl_writephy(tp, 0x1f, 0x0bc8);
4190 rtl_writephy(tp, 0x12, 0x00ed);
4191 rtl_writephy(tp, 0x1f, 0x0bcd);
4192 rtl_writephy(tp, 0x14, 0x1065);
4193 rtl_writephy(tp, 0x14, 0x9065);
4194 rtl_writephy(tp, 0x14, 0x1065);
4195 rtl_writephy(tp, 0x1f, 0x0000);
4196
4197 /* Check ALDPS bit, disable it if enabled */
4198 rtl_writephy(tp, 0x1f, 0x0a43);
4199 if (rtl_readphy(tp, 0x10) & 0x0004)
4200 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4201
4202 rtl_writephy(tp, 0x1f, 0x0000);
4203}
4204
françois romieu4da19632011-01-03 15:07:55 +00004205static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004206{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004207 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004208 { 0x1f, 0x0003 },
4209 { 0x08, 0x441d },
4210 { 0x01, 0x9100 },
4211 { 0x1f, 0x0000 }
4212 };
4213
françois romieu4da19632011-01-03 15:07:55 +00004214 rtl_writephy(tp, 0x1f, 0x0000);
4215 rtl_patchphy(tp, 0x11, 1 << 12);
4216 rtl_patchphy(tp, 0x19, 1 << 13);
4217 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004218
françois romieu4da19632011-01-03 15:07:55 +00004219 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004220}
4221
Hayes Wang5a5e4442011-02-22 17:26:21 +08004222static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4223{
4224 static const struct phy_reg phy_reg_init[] = {
4225 { 0x1f, 0x0005 },
4226 { 0x1a, 0x0000 },
4227 { 0x1f, 0x0000 },
4228
4229 { 0x1f, 0x0004 },
4230 { 0x1c, 0x0000 },
4231 { 0x1f, 0x0000 },
4232
4233 { 0x1f, 0x0001 },
4234 { 0x15, 0x7701 },
4235 { 0x1f, 0x0000 }
4236 };
4237
4238 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004239 rtl_writephy(tp, 0x1f, 0x0000);
4240 rtl_writephy(tp, 0x18, 0x0310);
4241 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004242
François Romieu953a12c2011-04-24 17:38:48 +02004243 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004244
4245 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4246}
4247
Hayes Wang7e18dca2012-03-30 14:33:02 +08004248static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4249{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004250 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004251 rtl_writephy(tp, 0x1f, 0x0000);
4252 rtl_writephy(tp, 0x18, 0x0310);
4253 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004254
4255 rtl_apply_firmware(tp);
4256
4257 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004258 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004259 rtl_writephy(tp, 0x1f, 0x0004);
4260 rtl_writephy(tp, 0x10, 0x401f);
4261 rtl_writephy(tp, 0x19, 0x7030);
4262 rtl_writephy(tp, 0x1f, 0x0000);
4263}
4264
Hayes Wang5598bfe2012-07-02 17:23:21 +08004265static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4266{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004267 static const struct phy_reg phy_reg_init[] = {
4268 { 0x1f, 0x0004 },
4269 { 0x10, 0xc07f },
4270 { 0x19, 0x7030 },
4271 { 0x1f, 0x0000 }
4272 };
4273
4274 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004275 rtl_writephy(tp, 0x1f, 0x0000);
4276 rtl_writephy(tp, 0x18, 0x0310);
4277 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004278
4279 rtl_apply_firmware(tp);
4280
Francois Romieufdf6fc02012-07-06 22:40:38 +02004281 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004282 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4283
Francois Romieufdf6fc02012-07-06 22:40:38 +02004284 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004285}
4286
Francois Romieu5615d9f2007-08-17 17:50:46 +02004287static void rtl_hw_phy_config(struct net_device *dev)
4288{
4289 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004290
4291 rtl8169_print_mac_version(tp);
4292
4293 switch (tp->mac_version) {
4294 case RTL_GIGA_MAC_VER_01:
4295 break;
4296 case RTL_GIGA_MAC_VER_02:
4297 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004298 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004299 break;
4300 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004301 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004302 break;
françois romieu2e9558562009-08-10 19:44:19 +00004303 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004304 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004305 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004306 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004307 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004308 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004309 case RTL_GIGA_MAC_VER_07:
4310 case RTL_GIGA_MAC_VER_08:
4311 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004312 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004313 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004314 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004315 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004316 break;
4317 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004318 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004319 break;
4320 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004321 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004322 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004323 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004324 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004325 break;
4326 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004327 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004328 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004329 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004330 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004331 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004332 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004333 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004334 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004335 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004336 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004337 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004338 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004339 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004340 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004341 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004342 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004343 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004344 break;
4345 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004346 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004347 break;
4348 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004349 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004350 break;
françois romieue6de30d2011-01-03 15:08:37 +00004351 case RTL_GIGA_MAC_VER_28:
4352 rtl8168d_4_hw_phy_config(tp);
4353 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004354 case RTL_GIGA_MAC_VER_29:
4355 case RTL_GIGA_MAC_VER_30:
4356 rtl8105e_hw_phy_config(tp);
4357 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004358 case RTL_GIGA_MAC_VER_31:
4359 /* None. */
4360 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004361 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004362 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004363 rtl8168e_1_hw_phy_config(tp);
4364 break;
4365 case RTL_GIGA_MAC_VER_34:
4366 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004367 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004368 case RTL_GIGA_MAC_VER_35:
4369 rtl8168f_1_hw_phy_config(tp);
4370 break;
4371 case RTL_GIGA_MAC_VER_36:
4372 rtl8168f_2_hw_phy_config(tp);
4373 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004374
Hayes Wang7e18dca2012-03-30 14:33:02 +08004375 case RTL_GIGA_MAC_VER_37:
4376 rtl8402_hw_phy_config(tp);
4377 break;
4378
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004379 case RTL_GIGA_MAC_VER_38:
4380 rtl8411_hw_phy_config(tp);
4381 break;
4382
Hayes Wang5598bfe2012-07-02 17:23:21 +08004383 case RTL_GIGA_MAC_VER_39:
4384 rtl8106e_hw_phy_config(tp);
4385 break;
4386
Hayes Wangc5583862012-07-02 17:23:22 +08004387 case RTL_GIGA_MAC_VER_40:
4388 rtl8168g_1_hw_phy_config(tp);
4389 break;
hayeswang57538c42013-04-01 22:23:40 +00004390 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004391 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004392 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004393 rtl8168g_2_hw_phy_config(tp);
4394 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004395 case RTL_GIGA_MAC_VER_45:
4396 case RTL_GIGA_MAC_VER_47:
4397 rtl8168h_1_hw_phy_config(tp);
4398 break;
4399 case RTL_GIGA_MAC_VER_46:
4400 case RTL_GIGA_MAC_VER_48:
4401 rtl8168h_2_hw_phy_config(tp);
4402 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004403
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004404 case RTL_GIGA_MAC_VER_49:
4405 rtl8168ep_1_hw_phy_config(tp);
4406 break;
4407 case RTL_GIGA_MAC_VER_50:
4408 case RTL_GIGA_MAC_VER_51:
4409 rtl8168ep_2_hw_phy_config(tp);
4410 break;
4411
Hayes Wangc5583862012-07-02 17:23:22 +08004412 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004413 default:
4414 break;
4415 }
4416}
4417
Francois Romieuda78dbf2012-01-26 14:18:23 +01004418static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 struct timer_list *timer = &tp->timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4422
Francois Romieubcf0bf92006-07-26 23:14:13 +02004423 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
françois romieu4da19632011-01-03 15:07:55 +00004425 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02004426 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 * A busy loop could burn quite a few cycles on nowadays CPU.
4428 * Let's delay the execution of the timer for a few ticks.
4429 */
4430 timeout = HZ/10;
4431 goto out_mod_timer;
4432 }
4433
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004434 if (tp->link_ok(tp))
Francois Romieuda78dbf2012-01-26 14:18:23 +01004435 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02004437 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
françois romieu4da19632011-01-03 15:07:55 +00004439 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
4441out_mod_timer:
4442 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004443}
4444
4445static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4446{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004447 if (!test_and_set_bit(flag, tp->wk.flags))
4448 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004449}
4450
Kees Cook9de36cc2017-10-25 03:53:12 -07004451static void rtl8169_phy_timer(struct timer_list *t)
Francois Romieuda78dbf2012-01-26 14:18:23 +01004452{
Kees Cook9de36cc2017-10-25 03:53:12 -07004453 struct rtl8169_private *tp = from_timer(tp, t, timer);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004454
Francois Romieu98ddf982012-01-31 10:47:34 +01004455 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456}
4457
Francois Romieuffc46952012-07-06 14:19:23 +02004458DECLARE_RTL_COND(rtl_phy_reset_cond)
4459{
4460 return tp->phy_reset_pending(tp);
4461}
4462
Francois Romieubf793292006-11-01 00:53:05 +01004463static void rtl8169_phy_reset(struct net_device *dev,
4464 struct rtl8169_private *tp)
4465{
françois romieu4da19632011-01-03 15:07:55 +00004466 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004467 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004468}
4469
David S. Miller8decf862011-09-22 03:23:13 -04004470static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4471{
David S. Miller8decf862011-09-22 03:23:13 -04004472 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004473 (RTL_R8(tp, PHYstatus) & TBI_Enable);
David S. Miller8decf862011-09-22 03:23:13 -04004474}
4475
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004476static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477{
Francois Romieu5615d9f2007-08-17 17:50:46 +02004478 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004479
Marcus Sundberg773328942008-07-10 21:28:08 +02004480 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4481 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004482 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02004483 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004484
Francois Romieu6dccd162007-02-13 23:38:05 +01004485 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4486
4487 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4488 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004489
Francois Romieubcf0bf92006-07-26 23:14:13 +02004490 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004491 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004492 RTL_W8(tp, 0x82, 0x01);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004493 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004494 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004495 }
4496
Francois Romieubf793292006-11-01 00:53:05 +01004497 rtl8169_phy_reset(dev, tp);
4498
Oliver Neukum54405cd2011-01-06 21:55:13 +01004499 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004500 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4501 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4502 (tp->mii.supports_gmii ?
4503 ADVERTISED_1000baseT_Half |
4504 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004505
David S. Miller8decf862011-09-22 03:23:13 -04004506 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00004507 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004508}
4509
Francois Romieu773d2022007-01-31 23:47:43 +01004510static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4511{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004512 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004513
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004514 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00004515
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004516 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4517 RTL_R32(tp, MAC4);
françois romieu908ba2b2010-04-26 11:42:58 +00004518
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004519 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4520 RTL_R32(tp, MAC0);
françois romieu908ba2b2010-04-26 11:42:58 +00004521
françois romieu9ecb9aa2012-12-07 11:20:21 +00004522 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4523 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004524
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004525 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieu773d2022007-01-31 23:47:43 +01004526
Francois Romieuda78dbf2012-01-26 14:18:23 +01004527 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004528}
4529
4530static int rtl_set_mac_address(struct net_device *dev, void *p)
4531{
4532 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004533 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004534 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004535
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004536 ret = eth_mac_addr(dev, p);
4537 if (ret)
4538 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004539
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004540 pm_runtime_get_noresume(d);
4541
4542 if (pm_runtime_active(d))
4543 rtl_rar_set(tp, dev->dev_addr);
4544
4545 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004546
4547 return 0;
4548}
4549
Francois Romieu5f787a12006-08-17 13:02:36 +02004550static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4551{
4552 struct rtl8169_private *tp = netdev_priv(dev);
4553 struct mii_ioctl_data *data = if_mii(ifr);
4554
Francois Romieu8b4ab282008-11-19 22:05:25 -08004555 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4556}
Francois Romieu5f787a12006-08-17 13:02:36 +02004557
Francois Romieucecb5fd2011-04-01 10:21:07 +02004558static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4559 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004560{
Francois Romieu5f787a12006-08-17 13:02:36 +02004561 switch (cmd) {
4562 case SIOCGMIIPHY:
4563 data->phy_id = 32; /* Internal PHY */
4564 return 0;
4565
4566 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004567 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004568 return 0;
4569
4570 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004571 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004572 return 0;
4573 }
4574 return -EOPNOTSUPP;
4575}
4576
Francois Romieu8b4ab282008-11-19 22:05:25 -08004577static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4578{
4579 return -EOPNOTSUPP;
4580}
4581
Bill Pembertonbaf63292012-12-03 09:23:28 -05004582static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004583{
4584 struct mdio_ops *ops = &tp->mdio_ops;
4585
4586 switch (tp->mac_version) {
4587 case RTL_GIGA_MAC_VER_27:
4588 ops->write = r8168dp_1_mdio_write;
4589 ops->read = r8168dp_1_mdio_read;
4590 break;
françois romieue6de30d2011-01-03 15:08:37 +00004591 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004592 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004593 ops->write = r8168dp_2_mdio_write;
4594 ops->read = r8168dp_2_mdio_read;
4595 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004596 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004597 ops->write = r8168g_mdio_write;
4598 ops->read = r8168g_mdio_read;
4599 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004600 default:
4601 ops->write = r8169_mdio_write;
4602 ops->read = r8169_mdio_read;
4603 break;
4604 }
4605}
4606
hayeswange2409d82013-03-31 17:02:04 +00004607static void rtl_speed_down(struct rtl8169_private *tp)
4608{
4609 u32 adv;
4610 int lpa;
4611
4612 rtl_writephy(tp, 0x1f, 0x0000);
4613 lpa = rtl_readphy(tp, MII_LPA);
4614
4615 if (lpa & (LPA_10HALF | LPA_10FULL))
4616 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4617 else if (lpa & (LPA_100HALF | LPA_100FULL))
4618 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4619 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4620 else
4621 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4622 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4623 (tp->mii.supports_gmii ?
4624 ADVERTISED_1000baseT_Half |
4625 ADVERTISED_1000baseT_Full : 0);
4626
4627 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4628 adv);
4629}
4630
David S. Miller1805b2f2011-10-24 18:18:09 -04004631static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4632{
David S. Miller1805b2f2011-10-24 18:18:09 -04004633 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004634 case RTL_GIGA_MAC_VER_25:
4635 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004636 case RTL_GIGA_MAC_VER_29:
4637 case RTL_GIGA_MAC_VER_30:
4638 case RTL_GIGA_MAC_VER_32:
4639 case RTL_GIGA_MAC_VER_33:
4640 case RTL_GIGA_MAC_VER_34:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004641 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004642 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04004643 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4644 break;
4645 default:
4646 break;
4647 }
4648}
4649
4650static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4651{
4652 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4653 return false;
4654
hayeswange2409d82013-03-31 17:02:04 +00004655 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004656 rtl_wol_suspend_quirk(tp);
4657
4658 return true;
4659}
4660
françois romieu065c27c2011-01-03 15:08:12 +00004661static void r8168_phy_power_up(struct rtl8169_private *tp)
4662{
4663 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004664 switch (tp->mac_version) {
4665 case RTL_GIGA_MAC_VER_11:
4666 case RTL_GIGA_MAC_VER_12:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004667 case RTL_GIGA_MAC_VER_17 ... RTL_GIGA_MAC_VER_28:
hayeswang01dc7fe2011-03-21 01:50:28 +00004668 case RTL_GIGA_MAC_VER_31:
4669 rtl_writephy(tp, 0x0e, 0x0000);
4670 break;
4671 default:
4672 break;
4673 }
françois romieu065c27c2011-01-03 15:08:12 +00004674 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4675}
4676
4677static void r8168_phy_power_down(struct rtl8169_private *tp)
4678{
4679 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004680 switch (tp->mac_version) {
4681 case RTL_GIGA_MAC_VER_32:
4682 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004683 case RTL_GIGA_MAC_VER_40:
4684 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004685 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4686 break;
4687
4688 case RTL_GIGA_MAC_VER_11:
4689 case RTL_GIGA_MAC_VER_12:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004690 case RTL_GIGA_MAC_VER_17 ... RTL_GIGA_MAC_VER_28:
hayeswang01dc7fe2011-03-21 01:50:28 +00004691 case RTL_GIGA_MAC_VER_31:
4692 rtl_writephy(tp, 0x0e, 0x0200);
4693 default:
4694 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4695 break;
4696 }
françois romieu065c27c2011-01-03 15:08:12 +00004697}
4698
4699static void r8168_pll_power_down(struct rtl8169_private *tp)
4700{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01004701 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004702 return;
4703
hayeswang01dc7fe2011-03-21 01:50:28 +00004704 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4705 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004706 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004707
David S. Miller1805b2f2011-10-24 18:18:09 -04004708 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004709 return;
françois romieu065c27c2011-01-03 15:08:12 +00004710
4711 r8168_phy_power_down(tp);
4712
4713 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004714 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004715 case RTL_GIGA_MAC_VER_37:
4716 case RTL_GIGA_MAC_VER_39:
4717 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004718 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004719 case RTL_GIGA_MAC_VER_45:
4720 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004721 case RTL_GIGA_MAC_VER_47:
4722 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004723 case RTL_GIGA_MAC_VER_50:
4724 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004725 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004726 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004727 case RTL_GIGA_MAC_VER_40:
4728 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004729 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004730 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004731 0xfc000000, ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004732 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004733 break;
françois romieu065c27c2011-01-03 15:08:12 +00004734 }
4735}
4736
4737static void r8168_pll_power_up(struct rtl8169_private *tp)
4738{
françois romieu065c27c2011-01-03 15:08:12 +00004739 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004740 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004741 case RTL_GIGA_MAC_VER_37:
4742 case RTL_GIGA_MAC_VER_39:
4743 case RTL_GIGA_MAC_VER_43:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004744 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004745 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004746 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004747 case RTL_GIGA_MAC_VER_45:
4748 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004749 case RTL_GIGA_MAC_VER_47:
4750 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004751 case RTL_GIGA_MAC_VER_50:
4752 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004753 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004754 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004755 case RTL_GIGA_MAC_VER_40:
4756 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004757 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004758 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004759 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004760 0x00000000, ERIAR_EXGMAC);
4761 break;
françois romieu065c27c2011-01-03 15:08:12 +00004762 }
4763
4764 r8168_phy_power_up(tp);
4765}
4766
Francois Romieud58d46b2011-05-03 16:38:29 +02004767static void rtl_generic_op(struct rtl8169_private *tp,
4768 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00004769{
4770 if (op)
4771 op(tp);
4772}
4773
4774static void rtl_pll_power_down(struct rtl8169_private *tp)
4775{
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004776 switch (tp->mac_version) {
4777 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4778 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4779 break;
4780 default:
4781 r8168_pll_power_down(tp);
4782 }
françois romieu065c27c2011-01-03 15:08:12 +00004783}
4784
4785static void rtl_pll_power_up(struct rtl8169_private *tp)
4786{
françois romieu065c27c2011-01-03 15:08:12 +00004787 switch (tp->mac_version) {
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004788 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4789 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
françois romieu065c27c2011-01-03 15:08:12 +00004790 break;
françois romieu065c27c2011-01-03 15:08:12 +00004791 default:
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004792 r8168_pll_power_up(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004793 }
4794}
4795
Hayes Wange542a222011-07-06 15:58:04 +08004796static void rtl_init_rxcfg(struct rtl8169_private *tp)
4797{
Hayes Wange542a222011-07-06 15:58:04 +08004798 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004799 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4800 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004801 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004802 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004803 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004804 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02004805 case RTL_GIGA_MAC_VER_35:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004806 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004807 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004808 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004809 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00004810 break;
Hayes Wange542a222011-07-06 15:58:04 +08004811 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004812 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004813 break;
4814 }
4815}
4816
Hayes Wang92fc43b2011-07-06 15:58:03 +08004817static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4818{
Timo Teräs9fba0812013-01-15 21:01:24 +00004819 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004820}
4821
Francois Romieud58d46b2011-05-03 16:38:29 +02004822static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4823{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004824 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004825 rtl_generic_op(tp, tp->jumbo_ops.enable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004826 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004827}
4828
4829static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4830{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004831 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004832 rtl_generic_op(tp, tp->jumbo_ops.disable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004833 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004834}
4835
4836static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4837{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004838 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4839 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004840 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004841}
4842
4843static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4844{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004845 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4846 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004847 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004848}
4849
4850static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4851{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004852 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004853}
4854
4855static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4856{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004857 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004858}
4859
4860static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4861{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004862 RTL_W8(tp, MaxTxPacketSize, 0x3f);
4863 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4864 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004865 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004866}
4867
4868static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4869{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004870 RTL_W8(tp, MaxTxPacketSize, 0x0c);
4871 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4872 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004873 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004874}
4875
4876static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4877{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004878 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01004879 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004880}
4881
4882static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4883{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004884 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004885 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004886}
4887
4888static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4889{
Francois Romieud58d46b2011-05-03 16:38:29 +02004890 r8168b_0_hw_jumbo_enable(tp);
4891
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004892 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004893}
4894
4895static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4896{
Francois Romieud58d46b2011-05-03 16:38:29 +02004897 r8168b_0_hw_jumbo_disable(tp);
4898
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004899 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004900}
4901
Bill Pembertonbaf63292012-12-03 09:23:28 -05004902static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02004903{
4904 struct jumbo_ops *ops = &tp->jumbo_ops;
4905
4906 switch (tp->mac_version) {
4907 case RTL_GIGA_MAC_VER_11:
4908 ops->disable = r8168b_0_hw_jumbo_disable;
4909 ops->enable = r8168b_0_hw_jumbo_enable;
4910 break;
4911 case RTL_GIGA_MAC_VER_12:
4912 case RTL_GIGA_MAC_VER_17:
4913 ops->disable = r8168b_1_hw_jumbo_disable;
4914 ops->enable = r8168b_1_hw_jumbo_enable;
4915 break;
4916 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4917 case RTL_GIGA_MAC_VER_19:
4918 case RTL_GIGA_MAC_VER_20:
4919 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4920 case RTL_GIGA_MAC_VER_22:
4921 case RTL_GIGA_MAC_VER_23:
4922 case RTL_GIGA_MAC_VER_24:
4923 case RTL_GIGA_MAC_VER_25:
4924 case RTL_GIGA_MAC_VER_26:
4925 ops->disable = r8168c_hw_jumbo_disable;
4926 ops->enable = r8168c_hw_jumbo_enable;
4927 break;
4928 case RTL_GIGA_MAC_VER_27:
4929 case RTL_GIGA_MAC_VER_28:
4930 ops->disable = r8168dp_hw_jumbo_disable;
4931 ops->enable = r8168dp_hw_jumbo_enable;
4932 break;
4933 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4934 case RTL_GIGA_MAC_VER_32:
4935 case RTL_GIGA_MAC_VER_33:
4936 case RTL_GIGA_MAC_VER_34:
4937 ops->disable = r8168e_hw_jumbo_disable;
4938 ops->enable = r8168e_hw_jumbo_enable;
4939 break;
4940
4941 /*
4942 * No action needed for jumbo frames with 8169.
4943 * No jumbo for 810x at all.
4944 */
Heiner Kallweit2a718832018-05-02 21:39:49 +02004945 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02004946 default:
4947 ops->disable = NULL;
4948 ops->enable = NULL;
4949 break;
4950 }
4951}
4952
Francois Romieuffc46952012-07-06 14:19:23 +02004953DECLARE_RTL_COND(rtl_chipcmd_cond)
4954{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004955 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02004956}
4957
Francois Romieu6f43adc2011-04-29 15:05:51 +02004958static void rtl_hw_reset(struct rtl8169_private *tp)
4959{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004960 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004961
Francois Romieuffc46952012-07-06 14:19:23 +02004962 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004963}
4964
Francois Romieub6ffd972011-06-17 17:00:05 +02004965static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4966{
4967 struct rtl_fw *rtl_fw;
4968 const char *name;
4969 int rc = -ENOMEM;
4970
4971 name = rtl_lookup_firmware_name(tp);
4972 if (!name)
4973 goto out_no_firmware;
4974
4975 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4976 if (!rtl_fw)
4977 goto err_warn;
4978
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004979 rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
Francois Romieub6ffd972011-06-17 17:00:05 +02004980 if (rc < 0)
4981 goto err_free;
4982
Francois Romieufd112f22011-06-18 00:10:29 +02004983 rc = rtl_check_firmware(tp, rtl_fw);
4984 if (rc < 0)
4985 goto err_release_firmware;
4986
Francois Romieub6ffd972011-06-17 17:00:05 +02004987 tp->rtl_fw = rtl_fw;
4988out:
4989 return;
4990
Francois Romieufd112f22011-06-18 00:10:29 +02004991err_release_firmware:
4992 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004993err_free:
4994 kfree(rtl_fw);
4995err_warn:
4996 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4997 name, rc);
4998out_no_firmware:
4999 tp->rtl_fw = NULL;
5000 goto out;
5001}
5002
François Romieu953a12c2011-04-24 17:38:48 +02005003static void rtl_request_firmware(struct rtl8169_private *tp)
5004{
Francois Romieub6ffd972011-06-17 17:00:05 +02005005 if (IS_ERR(tp->rtl_fw))
5006 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02005007}
5008
Hayes Wang92fc43b2011-07-06 15:58:03 +08005009static void rtl_rx_close(struct rtl8169_private *tp)
5010{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005011 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005012}
5013
Francois Romieuffc46952012-07-06 14:19:23 +02005014DECLARE_RTL_COND(rtl_npq_cond)
5015{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005016 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02005017}
5018
5019DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5020{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005021 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02005022}
5023
françois romieue6de30d2011-01-03 15:08:37 +00005024static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025{
5026 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00005027 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028
Hayes Wang92fc43b2011-07-06 15:58:03 +08005029 rtl_rx_close(tp);
5030
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02005031 switch (tp->mac_version) {
5032 case RTL_GIGA_MAC_VER_27:
5033 case RTL_GIGA_MAC_VER_28:
5034 case RTL_GIGA_MAC_VER_31:
Francois Romieuffc46952012-07-06 14:19:23 +02005035 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02005036 break;
5037 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
5038 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005039 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02005040 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02005041 break;
5042 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005043 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005044 udelay(100);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02005045 break;
françois romieue6de30d2011-01-03 15:08:37 +00005046 }
5047
Hayes Wang92fc43b2011-07-06 15:58:03 +08005048 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049}
5050
Francois Romieu7f796d832007-06-11 23:04:41 +02005051static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005052{
Francois Romieu9cb427b2006-11-02 00:10:16 +01005053 /* Set DMA burst size and Interframe Gap Time */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005054 RTL_W32(tp, TxConfig, (TX_DMA_BURST << TxDMAShift) |
Francois Romieu9cb427b2006-11-02 00:10:16 +01005055 (InterFrameGap << TxInterFrameGapShift));
5056}
5057
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02005058static void rtl_set_rx_max_size(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02005060 /* Low hurts. Let's disable the filtering. */
5061 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
Francois Romieu07ce4062007-02-23 23:36:39 +01005062}
5063
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005064static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02005065{
5066 /*
5067 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5068 * register to be written before TxDescAddrLow to work.
5069 * Switching from MMIO to I/O access fixes the issue as well.
5070 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005071 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
5072 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
5073 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
5074 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005075}
5076
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02005077static void rtl_hw_start(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02005078{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02005079 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
5080
5081 tp->hw_start(tp);
5082
5083 rtl_set_rx_max_size(tp);
5084 rtl_set_rx_tx_desc_registers(tp);
5085 rtl_set_rx_tx_config_registers(tp);
5086 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
5087
5088 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5089 RTL_R8(tp, IntrMask);
5090 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
5091 /* no early-rx interrupts */
5092 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
5093 rtl_irq_enable_all(tp);
Francois Romieu7f796d832007-06-11 23:04:41 +02005094}
5095
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005096static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01005097{
Francois Romieu37441002011-06-17 22:58:54 +02005098 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01005099 u32 mac_version;
5100 u32 clk;
5101 u32 val;
5102 } cfg2_info [] = {
5103 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5104 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5105 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5106 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02005107 };
5108 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01005109 unsigned int i;
5110 u32 clk;
5111
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005112 clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01005113 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01005114 if ((p->mac_version == mac_version) && (p->clk == clk)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005115 RTL_W32(tp, 0x7c, p->val);
Francois Romieu6dccd162007-02-13 23:38:05 +01005116 break;
5117 }
5118 }
5119}
5120
Francois Romieue6b763e2012-03-08 09:35:39 +01005121static void rtl_set_rx_mode(struct net_device *dev)
5122{
5123 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieue6b763e2012-03-08 09:35:39 +01005124 u32 mc_filter[2]; /* Multicast hash filter */
5125 int rx_mode;
5126 u32 tmp = 0;
5127
5128 if (dev->flags & IFF_PROMISC) {
5129 /* Unconditionally log net taps. */
5130 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5131 rx_mode =
5132 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5133 AcceptAllPhys;
5134 mc_filter[1] = mc_filter[0] = 0xffffffff;
5135 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5136 (dev->flags & IFF_ALLMULTI)) {
5137 /* Too many to filter perfectly -- accept all multicasts. */
5138 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5139 mc_filter[1] = mc_filter[0] = 0xffffffff;
5140 } else {
5141 struct netdev_hw_addr *ha;
5142
5143 rx_mode = AcceptBroadcast | AcceptMyPhys;
5144 mc_filter[1] = mc_filter[0] = 0;
5145 netdev_for_each_mc_addr(ha, dev) {
5146 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5147 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5148 rx_mode |= AcceptMulticast;
5149 }
5150 }
5151
5152 if (dev->features & NETIF_F_RXALL)
5153 rx_mode |= (AcceptErr | AcceptRunt);
5154
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005155 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Francois Romieue6b763e2012-03-08 09:35:39 +01005156
5157 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5158 u32 data = mc_filter[0];
5159
5160 mc_filter[0] = swab32(mc_filter[1]);
5161 mc_filter[1] = swab32(data);
5162 }
5163
Nathan Walp04817762012-11-01 12:08:47 +00005164 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5165 mc_filter[1] = mc_filter[0] = 0xffffffff;
5166
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005167 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
5168 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01005169
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005170 RTL_W32(tp, RxConfig, tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01005171}
5172
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005173static void rtl_hw_start_8169(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005174{
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005175 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005176 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005177
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005178 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005180 tp->cp_cmd |= PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005181
Francois Romieucecb5fd2011-04-01 10:21:07 +02005182 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5183 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005184 dprintk("Set MAC Reg C+CR Offset 0xe0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005186 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 }
5188
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005189 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005190
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005191 rtl8169_set_magic_reg(tp, tp->mac_version);
Francois Romieu6dccd162007-02-13 23:38:05 +01005192
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 /*
5194 * Undocumented corner. Supposedly:
5195 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5196 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005197 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005199 RTL_W32(tp, RxMissed, 0);
Francois Romieu07ce4062007-02-23 23:36:39 +01005200}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005202static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5203{
5204 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02005205 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005206}
5207
5208static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5209{
Francois Romieu52989f02012-07-06 13:37:00 +02005210 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005211}
5212
5213static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02005214{
5215 u32 csi;
5216
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005217 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5218 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00005219}
5220
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005221static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005222{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005223 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00005224}
5225
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005226static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00005227{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005228 rtl_csi_access_enable(tp, 0x27000000);
5229}
5230
Francois Romieuffc46952012-07-06 14:19:23 +02005231DECLARE_RTL_COND(rtl_csiar_cond)
5232{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005233 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02005234}
5235
Francois Romieu52989f02012-07-06 13:37:00 +02005236static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005237{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005238 RTL_W32(tp, CSIDR, value);
5239 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005240 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5241
Francois Romieuffc46952012-07-06 14:19:23 +02005242 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005243}
5244
Francois Romieu52989f02012-07-06 13:37:00 +02005245static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005246{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005247 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) |
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005248 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5249
Francois Romieuffc46952012-07-06 14:19:23 +02005250 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005251 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005252}
5253
Francois Romieu52989f02012-07-06 13:37:00 +02005254static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005255{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005256 RTL_W32(tp, CSIDR, value);
5257 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Hayes Wang7e18dca2012-03-30 14:33:02 +08005258 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5259 CSIAR_FUNC_NIC);
5260
Francois Romieuffc46952012-07-06 14:19:23 +02005261 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005262}
5263
Francois Romieu52989f02012-07-06 13:37:00 +02005264static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005265{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005266 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
Hayes Wang7e18dca2012-03-30 14:33:02 +08005267 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5268
Francois Romieuffc46952012-07-06 14:19:23 +02005269 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005270 RTL_R32(tp, CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005271}
5272
hayeswang45dd95c2013-07-08 17:09:01 +08005273static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5274{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005275 RTL_W32(tp, CSIDR, value);
5276 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
hayeswang45dd95c2013-07-08 17:09:01 +08005277 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5278 CSIAR_FUNC_NIC2);
5279
5280 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5281}
5282
5283static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5284{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005285 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
hayeswang45dd95c2013-07-08 17:09:01 +08005286 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5287
5288 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005289 RTL_R32(tp, CSIDR) : ~0;
hayeswang45dd95c2013-07-08 17:09:01 +08005290}
5291
Bill Pembertonbaf63292012-12-03 09:23:28 -05005292static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005293{
5294 struct csi_ops *ops = &tp->csi_ops;
5295
5296 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02005297 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
5298 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005299 ops->write = NULL;
5300 ops->read = NULL;
5301 break;
5302
Hayes Wang7e18dca2012-03-30 14:33:02 +08005303 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005304 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005305 ops->write = r8402_csi_write;
5306 ops->read = r8402_csi_read;
5307 break;
5308
hayeswang45dd95c2013-07-08 17:09:01 +08005309 case RTL_GIGA_MAC_VER_44:
5310 ops->write = r8411_csi_write;
5311 ops->read = r8411_csi_read;
5312 break;
5313
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005314 default:
5315 ops->write = r8169_csi_write;
5316 ops->read = r8169_csi_read;
5317 break;
5318 }
Francois Romieudacf8152008-08-02 20:44:13 +02005319}
5320
5321struct ephy_info {
5322 unsigned int offset;
5323 u16 mask;
5324 u16 bits;
5325};
5326
Francois Romieufdf6fc02012-07-06 22:40:38 +02005327static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5328 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005329{
5330 u16 w;
5331
5332 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005333 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5334 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005335 e++;
5336 }
5337}
5338
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005339static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02005340{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005341 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005342 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005343}
5344
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005345static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005346{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005347 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005348 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005349}
5350
hayeswangb51ecea2014-07-09 14:52:51 +08005351static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5352{
hayeswangb51ecea2014-07-09 14:52:51 +08005353 u8 data;
5354
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005355 data = RTL_R8(tp, Config3);
hayeswangb51ecea2014-07-09 14:52:51 +08005356
5357 if (enable)
5358 data |= Rdy_to_L23;
5359 else
5360 data &= ~Rdy_to_L23;
5361
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005362 RTL_W8(tp, Config3, data);
hayeswangb51ecea2014-07-09 14:52:51 +08005363}
5364
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005365static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005366{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005367 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005368
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005369 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005370 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieub726e492008-06-28 12:22:59 +02005371
françois romieufaf1e782013-02-27 13:01:57 +00005372 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005373 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00005374 PCI_EXP_DEVCTL_NOSNOOP_EN);
5375 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005376}
5377
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005378static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005379{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005380 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005381
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005382 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005383
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005384 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005385}
5386
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005387static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005388{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005389 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02005390
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005391 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005392
françois romieufaf1e782013-02-27 13:01:57 +00005393 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005394 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02005395
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005396 rtl_disable_clock_request(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005397
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005398 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005399 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu219a1e92008-06-28 11:58:39 +02005400}
5401
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005402static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005403{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005404 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005405 { 0x01, 0, 0x0001 },
5406 { 0x02, 0x0800, 0x1000 },
5407 { 0x03, 0, 0x0042 },
5408 { 0x06, 0x0080, 0x0000 },
5409 { 0x07, 0, 0x2000 }
5410 };
5411
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005412 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005413
Francois Romieufdf6fc02012-07-06 22:40:38 +02005414 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005415
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005416 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005417}
5418
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005419static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005420{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005421 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005422
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005423 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02005424
françois romieufaf1e782013-02-27 13:01:57 +00005425 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005426 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieuef3386f2008-06-29 12:24:30 +02005427
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005428 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005429 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieuef3386f2008-06-29 12:24:30 +02005430}
5431
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005432static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005433{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005434 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005435
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005436 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005437
5438 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005439 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005440
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005441 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005442
françois romieufaf1e782013-02-27 13:01:57 +00005443 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005444 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005445
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005446 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005447 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005448}
5449
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005450static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005451{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005452 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005453 { 0x02, 0x0800, 0x1000 },
5454 { 0x03, 0, 0x0002 },
5455 { 0x06, 0x0080, 0x0000 }
5456 };
5457
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005458 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005459
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005460 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02005461
Francois Romieufdf6fc02012-07-06 22:40:38 +02005462 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005463
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005464 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005465}
5466
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005467static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005468{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005469 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005470 { 0x01, 0, 0x0001 },
5471 { 0x03, 0x0400, 0x0220 }
5472 };
5473
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005474 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005475
Francois Romieufdf6fc02012-07-06 22:40:38 +02005476 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005477
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005478 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005479}
5480
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005481static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005482{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005483 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005484}
5485
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005486static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005487{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005488 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005489
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005490 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005491}
5492
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005493static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005494{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005495 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005496
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005497 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005498
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005499 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005500
françois romieufaf1e782013-02-27 13:01:57 +00005501 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005502 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02005503
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005504 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005505 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu5b538df2008-07-20 16:22:45 +02005506}
5507
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005508static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005509{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005510 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005511
françois romieufaf1e782013-02-27 13:01:57 +00005512 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005513 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang4804b3b2011-03-21 01:50:29 +00005514
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005515 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang4804b3b2011-03-21 01:50:29 +00005516
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005517 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005518}
5519
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005520static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005521{
5522 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005523 { 0x0b, 0x0000, 0x0048 },
5524 { 0x19, 0x0020, 0x0050 },
5525 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00005526 };
françois romieue6de30d2011-01-03 15:08:37 +00005527
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005528 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005529
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005530 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00005531
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005532 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
françois romieue6de30d2011-01-03 15:08:37 +00005533
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005534 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00005535
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005536 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005537}
5538
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005539static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005540{
Hayes Wang70090422011-07-06 15:58:06 +08005541 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005542 { 0x00, 0x0200, 0x0100 },
5543 { 0x00, 0x0000, 0x0004 },
5544 { 0x06, 0x0002, 0x0001 },
5545 { 0x06, 0x0000, 0x0030 },
5546 { 0x07, 0x0000, 0x2000 },
5547 { 0x00, 0x0000, 0x0020 },
5548 { 0x03, 0x5800, 0x2000 },
5549 { 0x03, 0x0000, 0x0001 },
5550 { 0x01, 0x0800, 0x1000 },
5551 { 0x07, 0x0000, 0x4000 },
5552 { 0x1e, 0x0000, 0x2000 },
5553 { 0x19, 0xffff, 0xfe6c },
5554 { 0x0a, 0x0000, 0x0040 }
5555 };
5556
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005557 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005558
Francois Romieufdf6fc02012-07-06 22:40:38 +02005559 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005560
françois romieufaf1e782013-02-27 13:01:57 +00005561 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005562 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00005563
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005564 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang01dc7fe2011-03-21 01:50:28 +00005565
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005566 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005567
5568 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005569 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
5570 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005571
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005572 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005573}
5574
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005575static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005576{
5577 static const struct ephy_info e_info_8168e_2[] = {
5578 { 0x09, 0x0000, 0x0080 },
5579 { 0x19, 0x0000, 0x0224 }
5580 };
5581
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005582 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005583
Francois Romieufdf6fc02012-07-06 22:40:38 +02005584 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005585
françois romieufaf1e782013-02-27 13:01:57 +00005586 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005587 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08005588
Francois Romieufdf6fc02012-07-06 22:40:38 +02005589 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5590 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5591 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5592 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5593 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5594 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005595 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5596 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005597
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005598 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005599
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005600 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005601
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005602 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5603 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08005604
5605 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005606 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang70090422011-07-06 15:58:06 +08005607
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005608 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5609 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5610 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08005611}
5612
Hayes Wang5f886e02012-03-30 14:33:03 +08005613static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005614{
Hayes Wang5f886e02012-03-30 14:33:03 +08005615 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005616
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005617 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08005618
Francois Romieufdf6fc02012-07-06 22:40:38 +02005619 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5620 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5621 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5622 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005623 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5624 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5625 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5626 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005627 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5628 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005629
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005630 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc2218922011-09-06 16:55:18 +08005631
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005632 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005633
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005634 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5635 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5636 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5637 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5638 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005639}
5640
Hayes Wang5f886e02012-03-30 14:33:03 +08005641static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5642{
Hayes Wang5f886e02012-03-30 14:33:03 +08005643 static const struct ephy_info e_info_8168f_1[] = {
5644 { 0x06, 0x00c0, 0x0020 },
5645 { 0x08, 0x0001, 0x0002 },
5646 { 0x09, 0x0000, 0x0080 },
5647 { 0x19, 0x0000, 0x0224 }
5648 };
5649
5650 rtl_hw_start_8168f(tp);
5651
Francois Romieufdf6fc02012-07-06 22:40:38 +02005652 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005653
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005654 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005655
5656 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005657 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang5f886e02012-03-30 14:33:03 +08005658}
5659
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005660static void rtl_hw_start_8411(struct rtl8169_private *tp)
5661{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005662 static const struct ephy_info e_info_8168f_1[] = {
5663 { 0x06, 0x00c0, 0x0020 },
5664 { 0x0f, 0xffff, 0x5200 },
5665 { 0x1e, 0x0000, 0x4000 },
5666 { 0x19, 0x0000, 0x0224 }
5667 };
5668
5669 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08005670 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005671
Francois Romieufdf6fc02012-07-06 22:40:38 +02005672 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005673
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005674 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005675}
5676
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005677static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08005678{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005679 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
hayeswangbeb330a2013-04-01 22:23:39 +00005680
Hayes Wangc5583862012-07-02 17:23:22 +08005681 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5682 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5683 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5684 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5685
5686 rtl_csi_access_enable_1(tp);
5687
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005688 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08005689
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005690 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5691 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00005692 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005693
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005694 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5695 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc5583862012-07-02 17:23:22 +08005696
5697 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5698 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5699
5700 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005701 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wangc5583862012-07-02 17:23:22 +08005702
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005703 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5704 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005705
5706 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08005707}
5708
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005709static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5710{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005711 static const struct ephy_info e_info_8168g_1[] = {
5712 { 0x00, 0x0000, 0x0008 },
5713 { 0x0c, 0x37d0, 0x0820 },
5714 { 0x1e, 0x0000, 0x0001 },
5715 { 0x19, 0x8000, 0x0000 }
5716 };
5717
5718 rtl_hw_start_8168g(tp);
5719
5720 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005721 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5722 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005723 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
5724}
5725
hayeswang57538c42013-04-01 22:23:40 +00005726static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5727{
hayeswang57538c42013-04-01 22:23:40 +00005728 static const struct ephy_info e_info_8168g_2[] = {
5729 { 0x00, 0x0000, 0x0008 },
5730 { 0x0c, 0x3df0, 0x0200 },
5731 { 0x19, 0xffff, 0xfc00 },
5732 { 0x1e, 0xffff, 0x20eb }
5733 };
5734
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005735 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00005736
5737 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005738 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5739 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang57538c42013-04-01 22:23:40 +00005740 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5741}
5742
hayeswang45dd95c2013-07-08 17:09:01 +08005743static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5744{
hayeswang45dd95c2013-07-08 17:09:01 +08005745 static const struct ephy_info e_info_8411_2[] = {
5746 { 0x00, 0x0000, 0x0008 },
5747 { 0x0c, 0x3df0, 0x0200 },
5748 { 0x0f, 0xffff, 0x5200 },
5749 { 0x19, 0x0020, 0x0000 },
5750 { 0x1e, 0x0000, 0x2000 }
5751 };
5752
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005753 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08005754
5755 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005756 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5757 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang45dd95c2013-07-08 17:09:01 +08005758 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
5759}
5760
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005761static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
5762{
Andrzej Hajda72521ea2015-09-24 16:00:24 +02005763 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005764 u32 data;
5765 static const struct ephy_info e_info_8168h_1[] = {
5766 { 0x1e, 0x0800, 0x0001 },
5767 { 0x1d, 0x0000, 0x0800 },
5768 { 0x05, 0xffff, 0x2089 },
5769 { 0x06, 0xffff, 0x5881 },
5770 { 0x04, 0xffff, 0x154a },
5771 { 0x01, 0xffff, 0x068b }
5772 };
5773
5774 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005775 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5776 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005777 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
5778
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005779 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005780
5781 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5782 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5783 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5784 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5785
5786 rtl_csi_access_enable_1(tp);
5787
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005788 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005789
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005790 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5791 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005792
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005793 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005794
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005795 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005796
5797 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5798
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005799 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5800 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005801
5802 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5803 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5804
5805 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005806 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005807
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005808 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5809 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005810
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005811 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005812
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005813 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005814
5815 rtl_pcie_state_l2l3_enable(tp, false);
5816
5817 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08005818 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005819 rtl_writephy(tp, 0x1f, 0x0000);
5820 if (rg_saw_cnt > 0) {
5821 u16 sw_cnt_1ms_ini;
5822
5823 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
5824 sw_cnt_1ms_ini &= 0x0fff;
5825 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005826 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005827 data |= sw_cnt_1ms_ini;
5828 r8168_mac_ocp_write(tp, 0xd412, data);
5829 }
5830
5831 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005832 data &= ~0xf0;
5833 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005834 r8168_mac_ocp_write(tp, 0xe056, data);
5835
5836 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005837 data &= ~0x6000;
5838 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005839 r8168_mac_ocp_write(tp, 0xe052, data);
5840
5841 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005842 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005843 data |= 0x017f;
5844 r8168_mac_ocp_write(tp, 0xe0d6, data);
5845
5846 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005847 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005848 data |= 0x047f;
5849 r8168_mac_ocp_write(tp, 0xd420, data);
5850
5851 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
5852 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
5853 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
5854 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
5855}
5856
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005857static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
5858{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08005859 rtl8168ep_stop_cmac(tp);
5860
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005861 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005862
5863 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5864 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
5865 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
5866 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5867
5868 rtl_csi_access_enable_1(tp);
5869
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005870 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005871
5872 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5873 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5874
5875 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
5876
5877 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5878
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005879 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5880 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005881
5882 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5883 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5884
5885 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005886 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005887
5888 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5889
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005890 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005891
5892 rtl_pcie_state_l2l3_enable(tp, false);
5893}
5894
5895static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
5896{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005897 static const struct ephy_info e_info_8168ep_1[] = {
5898 { 0x00, 0xffff, 0x10ab },
5899 { 0x06, 0xffff, 0xf030 },
5900 { 0x08, 0xffff, 0x2006 },
5901 { 0x0d, 0xffff, 0x1666 },
5902 { 0x0c, 0x3ff0, 0x0000 }
5903 };
5904
5905 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005906 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5907 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005908 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
5909
5910 rtl_hw_start_8168ep(tp);
5911}
5912
5913static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
5914{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005915 static const struct ephy_info e_info_8168ep_2[] = {
5916 { 0x00, 0xffff, 0x10a3 },
5917 { 0x19, 0xffff, 0xfc00 },
5918 { 0x1e, 0xffff, 0x20ea }
5919 };
5920
5921 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005922 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5923 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005924 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
5925
5926 rtl_hw_start_8168ep(tp);
5927
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005928 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5929 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005930}
5931
5932static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
5933{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005934 u32 data;
5935 static const struct ephy_info e_info_8168ep_3[] = {
5936 { 0x00, 0xffff, 0x10a3 },
5937 { 0x19, 0xffff, 0x7c00 },
5938 { 0x1e, 0xffff, 0x20eb },
5939 { 0x0d, 0xffff, 0x1666 }
5940 };
5941
5942 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005943 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5944 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005945 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
5946
5947 rtl_hw_start_8168ep(tp);
5948
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005949 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5950 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005951
5952 data = r8168_mac_ocp_read(tp, 0xd3e2);
5953 data &= 0xf000;
5954 data |= 0x0271;
5955 r8168_mac_ocp_write(tp, 0xd3e2, data);
5956
5957 data = r8168_mac_ocp_read(tp, 0xd3e4);
5958 data &= 0xff00;
5959 r8168_mac_ocp_write(tp, 0xd3e4, data);
5960
5961 data = r8168_mac_ocp_read(tp, 0xe860);
5962 data |= 0x0080;
5963 r8168_mac_ocp_write(tp, 0xe860, data);
5964}
5965
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005966static void rtl_hw_start_8168(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005967{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005968 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02005969
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005970 tp->cp_cmd &= ~INTT_MASK;
5971 tp->cp_cmd |= PktCntrDisable | INTT_1;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005972 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu2dd99532007-06-11 23:22:52 +02005973
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005974 RTL_W16(tp, IntrMitigate, 0x5151);
Francois Romieu0e485152007-02-20 00:00:26 +01005975
5976 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00005977 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005978 tp->event_slow |= RxFIFOOver | PCSTimeout;
5979 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01005980 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005981
Francois Romieu219a1e92008-06-28 11:58:39 +02005982 switch (tp->mac_version) {
5983 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005984 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005985 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005986
5987 case RTL_GIGA_MAC_VER_12:
5988 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005989 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005990 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005991
5992 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005993 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005994 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005995
5996 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005997 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005998 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005999
6000 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006001 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006002 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006003
Francois Romieu197ff762008-06-28 13:16:02 +02006004 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006005 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006006 break;
Francois Romieu197ff762008-06-28 13:16:02 +02006007
Francois Romieu6fb07052008-06-29 11:54:28 +02006008 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006009 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006010 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02006011
Francois Romieuef3386f2008-06-29 12:24:30 +02006012 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006013 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006014 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02006015
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006016 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006017 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006018 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006019
Francois Romieu5b538df2008-07-20 16:22:45 +02006020 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00006021 case RTL_GIGA_MAC_VER_26:
6022 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006023 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006024 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02006025
françois romieue6de30d2011-01-03 15:08:37 +00006026 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006027 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006028 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02006029
hayeswang4804b3b2011-03-21 01:50:29 +00006030 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006031 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006032 break;
6033
hayeswang01dc7fe2011-03-21 01:50:28 +00006034 case RTL_GIGA_MAC_VER_32:
6035 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006036 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006037 break;
6038 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006039 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006040 break;
françois romieue6de30d2011-01-03 15:08:37 +00006041
Hayes Wangc2218922011-09-06 16:55:18 +08006042 case RTL_GIGA_MAC_VER_35:
6043 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006044 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006045 break;
6046
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006047 case RTL_GIGA_MAC_VER_38:
6048 rtl_hw_start_8411(tp);
6049 break;
6050
Hayes Wangc5583862012-07-02 17:23:22 +08006051 case RTL_GIGA_MAC_VER_40:
6052 case RTL_GIGA_MAC_VER_41:
6053 rtl_hw_start_8168g_1(tp);
6054 break;
hayeswang57538c42013-04-01 22:23:40 +00006055 case RTL_GIGA_MAC_VER_42:
6056 rtl_hw_start_8168g_2(tp);
6057 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006058
hayeswang45dd95c2013-07-08 17:09:01 +08006059 case RTL_GIGA_MAC_VER_44:
6060 rtl_hw_start_8411_2(tp);
6061 break;
6062
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006063 case RTL_GIGA_MAC_VER_45:
6064 case RTL_GIGA_MAC_VER_46:
6065 rtl_hw_start_8168h_1(tp);
6066 break;
6067
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006068 case RTL_GIGA_MAC_VER_49:
6069 rtl_hw_start_8168ep_1(tp);
6070 break;
6071
6072 case RTL_GIGA_MAC_VER_50:
6073 rtl_hw_start_8168ep_2(tp);
6074 break;
6075
6076 case RTL_GIGA_MAC_VER_51:
6077 rtl_hw_start_8168ep_3(tp);
6078 break;
6079
Francois Romieu219a1e92008-06-28 11:58:39 +02006080 default:
6081 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006082 tp->dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00006083 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006084 }
Francois Romieu07ce4062007-02-23 23:36:39 +01006085}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006087static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006088{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006089 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02006090 { 0x01, 0, 0x6e65 },
6091 { 0x02, 0, 0x091f },
6092 { 0x03, 0, 0xc2f9 },
6093 { 0x06, 0, 0xafb5 },
6094 { 0x07, 0, 0x0e00 },
6095 { 0x19, 0, 0xec80 },
6096 { 0x01, 0, 0x2e65 },
6097 { 0x01, 0, 0x6e65 }
6098 };
6099 u8 cfg1;
6100
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006101 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006102
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006103 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006104
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006105 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006106
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006107 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02006108 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006109 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006110
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006111 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006112 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006113 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006114
Francois Romieufdf6fc02012-07-06 22:40:38 +02006115 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02006116}
6117
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006118static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006119{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006120 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006121
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006122 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006123
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006124 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
6125 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006126}
6127
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006128static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006129{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006130 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006131
Francois Romieufdf6fc02012-07-06 22:40:38 +02006132 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006133}
6134
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006135static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006136{
6137 static const struct ephy_info e_info_8105e_1[] = {
6138 { 0x07, 0, 0x4000 },
6139 { 0x19, 0, 0x0200 },
6140 { 0x19, 0, 0x0020 },
6141 { 0x1e, 0, 0x2000 },
6142 { 0x03, 0, 0x0001 },
6143 { 0x19, 0, 0x0100 },
6144 { 0x19, 0, 0x0004 },
6145 { 0x0a, 0, 0x0020 }
6146 };
6147
Francois Romieucecb5fd2011-04-01 10:21:07 +02006148 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006149 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006150
Francois Romieucecb5fd2011-04-01 10:21:07 +02006151 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006152 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006153
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006154 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6155 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006156
Francois Romieufdf6fc02012-07-06 22:40:38 +02006157 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08006158
6159 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006160}
6161
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006162static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006163{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006164 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006165 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006166}
6167
Hayes Wang7e18dca2012-03-30 14:33:02 +08006168static void rtl_hw_start_8402(struct rtl8169_private *tp)
6169{
Hayes Wang7e18dca2012-03-30 14:33:02 +08006170 static const struct ephy_info e_info_8402[] = {
6171 { 0x19, 0xffff, 0xff64 },
6172 { 0x1e, 0, 0x4000 }
6173 };
6174
6175 rtl_csi_access_enable_2(tp);
6176
6177 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006178 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006179
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006180 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
6181 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006182
Francois Romieufdf6fc02012-07-06 22:40:38 +02006183 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006184
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006185 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006186
Francois Romieufdf6fc02012-07-06 22:40:38 +02006187 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6188 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006189 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6190 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006191 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6192 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006193 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006194
6195 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006196}
6197
Hayes Wang5598bfe2012-07-02 17:23:21 +08006198static void rtl_hw_start_8106(struct rtl8169_private *tp)
6199{
Hayes Wang5598bfe2012-07-02 17:23:21 +08006200 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006201 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006202
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006203 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
6204 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6205 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006206
6207 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006208}
6209
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006210static void rtl_hw_start_8101(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01006211{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006212 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6213 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006214
Francois Romieucecb5fd2011-04-01 10:21:07 +02006215 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006216 tp->mac_version == RTL_GIGA_MAC_VER_16)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006217 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006218 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006219
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006220 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00006221
Heiner Kallweit12d42c52018-04-28 22:19:30 +02006222 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006223 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
hayeswang1a964642013-04-01 22:23:41 +00006224
Francois Romieu2857ffb2008-08-02 21:08:49 +02006225 switch (tp->mac_version) {
6226 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006227 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006228 break;
6229
6230 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006231 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006232 break;
6233
6234 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006235 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006236 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006237
6238 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006239 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006240 break;
6241 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006242 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006243 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006244
6245 case RTL_GIGA_MAC_VER_37:
6246 rtl_hw_start_8402(tp);
6247 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006248
6249 case RTL_GIGA_MAC_VER_39:
6250 rtl_hw_start_8106(tp);
6251 break;
hayeswang58152cd2013-04-01 22:23:42 +00006252 case RTL_GIGA_MAC_VER_43:
6253 rtl_hw_start_8168g_2(tp);
6254 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006255 case RTL_GIGA_MAC_VER_47:
6256 case RTL_GIGA_MAC_VER_48:
6257 rtl_hw_start_8168h_1(tp);
6258 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006259 }
6260
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006261 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262}
6263
6264static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6265{
Francois Romieud58d46b2011-05-03 16:38:29 +02006266 struct rtl8169_private *tp = netdev_priv(dev);
6267
Francois Romieud58d46b2011-05-03 16:38:29 +02006268 if (new_mtu > ETH_DATA_LEN)
6269 rtl_hw_jumbo_enable(tp);
6270 else
6271 rtl_hw_jumbo_disable(tp);
6272
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006274 netdev_update_features(dev);
6275
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277}
6278
6279static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6280{
Al Viro95e09182007-12-22 18:55:39 +00006281 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6283}
6284
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006285static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6286 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287{
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006288 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
6289 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006290
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006291 kfree(*data_buff);
6292 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 rtl8169_make_unusable_by_asic(desc);
6294}
6295
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006296static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297{
6298 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6299
Alexander Duycka0750132014-12-11 15:02:17 -08006300 /* Force memory writes to complete before releasing descriptor */
6301 dma_wmb();
6302
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006303 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304}
6305
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006306static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006308 return (void *)ALIGN((long)data, 16);
6309}
6310
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006311static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6312 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006313{
6314 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315 dma_addr_t mapping;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006316 struct device *d = tp_to_dev(tp);
Heiner Kallweitd3b404c2018-04-17 23:22:14 +02006317 int node = dev_to_node(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006319 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006320 if (!data)
6321 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006322
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006323 if (rtl8169_align(data) != data) {
6324 kfree(data);
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006325 data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006326 if (!data)
6327 return NULL;
6328 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006329
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006330 mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006331 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006332 if (unlikely(dma_mapping_error(d, mapping))) {
6333 if (net_ratelimit())
6334 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006335 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337
Heiner Kallweitd731af72018-04-17 23:26:41 +02006338 desc->addr = cpu_to_le64(mapping);
6339 rtl8169_mark_to_asic(desc);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006340 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006341
6342err_out:
6343 kfree(data);
6344 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345}
6346
6347static void rtl8169_rx_clear(struct rtl8169_private *tp)
6348{
Francois Romieu07d3f512007-02-21 22:40:46 +01006349 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350
6351 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006352 if (tp->Rx_databuff[i]) {
6353 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 tp->RxDescArray + i);
6355 }
6356 }
6357}
6358
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006359static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006361 desc->opts1 |= cpu_to_le32(RingEnd);
6362}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006363
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006364static int rtl8169_rx_fill(struct rtl8169_private *tp)
6365{
6366 unsigned int i;
6367
6368 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006369 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006370
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006371 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006372 if (!data) {
6373 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006374 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006375 }
6376 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006379 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6380 return 0;
6381
6382err_out:
6383 rtl8169_rx_clear(tp);
6384 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385}
6386
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006387static int rtl8169_init_ring(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389 rtl8169_init_ring_indexes(tp);
6390
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006391 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
6392 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006394 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395}
6396
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006397static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 struct TxDesc *desc)
6399{
6400 unsigned int len = tx_skb->len;
6401
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006402 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6403
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 desc->opts1 = 0x00;
6405 desc->opts2 = 0x00;
6406 desc->addr = 0x00;
6407 tx_skb->len = 0;
6408}
6409
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006410static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6411 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412{
6413 unsigned int i;
6414
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006415 for (i = 0; i < n; i++) {
6416 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 struct ring_info *tx_skb = tp->tx_skb + entry;
6418 unsigned int len = tx_skb->len;
6419
6420 if (len) {
6421 struct sk_buff *skb = tx_skb->skb;
6422
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006423 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 tp->TxDescArray + entry);
6425 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006426 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 tx_skb->skb = NULL;
6428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 }
6430 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006431}
6432
6433static void rtl8169_tx_clear(struct rtl8169_private *tp)
6434{
6435 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 tp->cur_tx = tp->dirty_tx = 0;
6437}
6438
Francois Romieu4422bcd2012-01-26 11:23:32 +01006439static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440{
David Howellsc4028952006-11-22 14:57:56 +00006441 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01006442 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443
Francois Romieuda78dbf2012-01-26 14:18:23 +01006444 napi_disable(&tp->napi);
6445 netif_stop_queue(dev);
6446 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447
françois romieuc7c2c392011-12-04 20:30:52 +00006448 rtl8169_hw_reset(tp);
6449
Francois Romieu56de4142011-03-15 17:29:31 +01006450 for (i = 0; i < NUM_RX_DESC; i++)
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006451 rtl8169_mark_to_asic(tp->RxDescArray + i);
Francois Romieu56de4142011-03-15 17:29:31 +01006452
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00006454 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455
Francois Romieuda78dbf2012-01-26 14:18:23 +01006456 napi_enable(&tp->napi);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006457 rtl_hw_start(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01006458 netif_wake_queue(dev);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006459 rtl8169_check_link_status(dev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460}
6461
6462static void rtl8169_tx_timeout(struct net_device *dev)
6463{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006464 struct rtl8169_private *tp = netdev_priv(dev);
6465
6466 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467}
6468
6469static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07006470 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471{
6472 struct skb_shared_info *info = skb_shinfo(skb);
6473 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006474 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006475 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476
6477 entry = tp->cur_tx;
6478 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006479 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 dma_addr_t mapping;
6481 u32 status, len;
6482 void *addr;
6483
6484 entry = (entry + 1) % NUM_TX_DESC;
6485
6486 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00006487 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00006488 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006489 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006490 if (unlikely(dma_mapping_error(d, mapping))) {
6491 if (net_ratelimit())
6492 netif_err(tp, drv, tp->dev,
6493 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006494 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496
Francois Romieucecb5fd2011-04-01 10:21:07 +02006497 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006498 status = opts[0] | len |
6499 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500
6501 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07006502 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503 txd->addr = cpu_to_le64(mapping);
6504
6505 tp->tx_skb[entry].len = len;
6506 }
6507
6508 if (cur_frag) {
6509 tp->tx_skb[entry].skb = skb;
6510 txd->opts1 |= cpu_to_le32(LastFrag);
6511 }
6512
6513 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006514
6515err_out:
6516 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6517 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518}
6519
françois romieub423e9a2013-05-18 01:24:46 +00006520static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6521{
6522 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6523}
6524
hayeswange9746042014-07-11 16:25:58 +08006525static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6526 struct net_device *dev);
6527/* r8169_csum_workaround()
6528 * The hw limites the value the transport offset. When the offset is out of the
6529 * range, calculate the checksum by sw.
6530 */
6531static void r8169_csum_workaround(struct rtl8169_private *tp,
6532 struct sk_buff *skb)
6533{
6534 if (skb_shinfo(skb)->gso_size) {
6535 netdev_features_t features = tp->dev->features;
6536 struct sk_buff *segs, *nskb;
6537
6538 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6539 segs = skb_gso_segment(skb, features);
6540 if (IS_ERR(segs) || !segs)
6541 goto drop;
6542
6543 do {
6544 nskb = segs;
6545 segs = segs->next;
6546 nskb->next = NULL;
6547 rtl8169_start_xmit(nskb, tp->dev);
6548 } while (segs);
6549
Alexander Duyckeb781392015-05-01 10:34:44 -07006550 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006551 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6552 if (skb_checksum_help(skb) < 0)
6553 goto drop;
6554
6555 rtl8169_start_xmit(skb, tp->dev);
6556 } else {
6557 struct net_device_stats *stats;
6558
6559drop:
6560 stats = &tp->dev->stats;
6561 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07006562 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006563 }
6564}
6565
6566/* msdn_giant_send_check()
6567 * According to the document of microsoft, the TCP Pseudo Header excludes the
6568 * packet length for IPv6 TCP large packets.
6569 */
6570static int msdn_giant_send_check(struct sk_buff *skb)
6571{
6572 const struct ipv6hdr *ipv6h;
6573 struct tcphdr *th;
6574 int ret;
6575
6576 ret = skb_cow_head(skb, 0);
6577 if (ret)
6578 return ret;
6579
6580 ipv6h = ipv6_hdr(skb);
6581 th = tcp_hdr(skb);
6582
6583 th->check = 0;
6584 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6585
6586 return ret;
6587}
6588
6589static inline __be16 get_protocol(struct sk_buff *skb)
6590{
6591 __be16 protocol;
6592
6593 if (skb->protocol == htons(ETH_P_8021Q))
6594 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
6595 else
6596 protocol = skb->protocol;
6597
6598 return protocol;
6599}
6600
hayeswang5888d3f2014-07-11 16:25:56 +08006601static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6602 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603{
Michał Mirosław350fb322011-04-08 06:35:56 +00006604 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605
Francois Romieu2b7b4312011-04-18 22:53:24 -07006606 if (mss) {
6607 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08006608 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6609 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6610 const struct iphdr *ip = ip_hdr(skb);
6611
6612 if (ip->protocol == IPPROTO_TCP)
6613 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6614 else if (ip->protocol == IPPROTO_UDP)
6615 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6616 else
6617 WARN_ON_ONCE(1);
6618 }
6619
6620 return true;
6621}
6622
6623static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6624 struct sk_buff *skb, u32 *opts)
6625{
hayeswangbdfa4ed2014-07-11 16:25:57 +08006626 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08006627 u32 mss = skb_shinfo(skb)->gso_size;
6628
6629 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08006630 if (transport_offset > GTTCPHO_MAX) {
6631 netif_warn(tp, tx_err, tp->dev,
6632 "Invalid transport offset 0x%x for TSO\n",
6633 transport_offset);
6634 return false;
6635 }
6636
6637 switch (get_protocol(skb)) {
6638 case htons(ETH_P_IP):
6639 opts[0] |= TD1_GTSENV4;
6640 break;
6641
6642 case htons(ETH_P_IPV6):
6643 if (msdn_giant_send_check(skb))
6644 return false;
6645
6646 opts[0] |= TD1_GTSENV6;
6647 break;
6648
6649 default:
6650 WARN_ON_ONCE(1);
6651 break;
6652 }
6653
hayeswangbdfa4ed2014-07-11 16:25:57 +08006654 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08006655 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006656 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08006657 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658
françois romieub423e9a2013-05-18 01:24:46 +00006659 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006660 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00006661
hayeswange9746042014-07-11 16:25:58 +08006662 if (transport_offset > TCPHO_MAX) {
6663 netif_warn(tp, tx_err, tp->dev,
6664 "Invalid transport offset 0x%x\n",
6665 transport_offset);
6666 return false;
6667 }
6668
6669 switch (get_protocol(skb)) {
6670 case htons(ETH_P_IP):
6671 opts[1] |= TD1_IPv4_CS;
6672 ip_protocol = ip_hdr(skb)->protocol;
6673 break;
6674
6675 case htons(ETH_P_IPV6):
6676 opts[1] |= TD1_IPv6_CS;
6677 ip_protocol = ipv6_hdr(skb)->nexthdr;
6678 break;
6679
6680 default:
6681 ip_protocol = IPPROTO_RAW;
6682 break;
6683 }
6684
6685 if (ip_protocol == IPPROTO_TCP)
6686 opts[1] |= TD1_TCP_CS;
6687 else if (ip_protocol == IPPROTO_UDP)
6688 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006689 else
6690 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08006691
6692 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00006693 } else {
6694 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006695 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696 }
hayeswang5888d3f2014-07-11 16:25:56 +08006697
françois romieub423e9a2013-05-18 01:24:46 +00006698 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699}
6700
Stephen Hemminger613573252009-08-31 19:50:58 +00006701static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6702 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703{
6704 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006705 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006707 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 dma_addr_t mapping;
6709 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006710 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006711 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02006712
Julien Ducourthial477206a2012-05-09 00:00:06 +02006713 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006714 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006715 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 }
6717
6718 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006719 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720
françois romieub423e9a2013-05-18 01:24:46 +00006721 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6722 opts[0] = DescOwn;
6723
hayeswange9746042014-07-11 16:25:58 +08006724 if (!tp->tso_csum(tp, skb, opts)) {
6725 r8169_csum_workaround(tp, skb);
6726 return NETDEV_TX_OK;
6727 }
françois romieub423e9a2013-05-18 01:24:46 +00006728
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006729 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006730 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006731 if (unlikely(dma_mapping_error(d, mapping))) {
6732 if (net_ratelimit())
6733 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006734 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736
6737 tp->tx_skb[entry].len = len;
6738 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739
Francois Romieu2b7b4312011-04-18 22:53:24 -07006740 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006741 if (frags < 0)
6742 goto err_dma_1;
6743 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07006744 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006745 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07006746 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006747 tp->tx_skb[entry].skb = skb;
6748 }
6749
Francois Romieu2b7b4312011-04-18 22:53:24 -07006750 txd->opts2 = cpu_to_le32(opts[1]);
6751
Richard Cochran5047fb52012-03-10 07:29:42 +00006752 skb_tx_timestamp(skb);
6753
Alexander Duycka0750132014-12-11 15:02:17 -08006754 /* Force memory writes to complete before releasing descriptor */
6755 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756
Francois Romieucecb5fd2011-04-01 10:21:07 +02006757 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006758 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759 txd->opts1 = cpu_to_le32(status);
6760
Alexander Duycka0750132014-12-11 15:02:17 -08006761 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00006762 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763
Alexander Duycka0750132014-12-11 15:02:17 -08006764 tp->cur_tx += frags + 1;
6765
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006766 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767
David S. Miller87cda7c2015-02-22 15:54:29 -05006768 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01006769
David S. Miller87cda7c2015-02-22 15:54:29 -05006770 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01006771 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6772 * not miss a ring update when it notices a stopped queue.
6773 */
6774 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01006776 /* Sync with rtl_tx:
6777 * - publish queue status and cur_tx ring index (write barrier)
6778 * - refresh dirty_tx ring index (read barrier).
6779 * May the current thread have a pessimistic view of the ring
6780 * status and forget to wake up queue, a racing rtl_tx thread
6781 * can't.
6782 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006783 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02006784 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 netif_wake_queue(dev);
6786 }
6787
Stephen Hemminger613573252009-08-31 19:50:58 +00006788 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006790err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006791 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006792err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006793 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006794 dev->stats.tx_dropped++;
6795 return NETDEV_TX_OK;
6796
6797err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006799 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00006800 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801}
6802
6803static void rtl8169_pcierr_interrupt(struct net_device *dev)
6804{
6805 struct rtl8169_private *tp = netdev_priv(dev);
6806 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 u16 pci_status, pci_cmd;
6808
6809 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6810 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6811
Joe Perchesbf82c182010-02-09 11:49:50 +00006812 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6813 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814
6815 /*
6816 * The recovery sequence below admits a very elaborated explanation:
6817 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01006818 * - I did not see what else could be done;
6819 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 *
6821 * Feel free to adjust to your needs.
6822 */
Francois Romieua27993f2006-12-18 00:04:19 +01006823 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01006824 pci_cmd &= ~PCI_COMMAND_PARITY;
6825 else
6826 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6827
6828 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829
6830 pci_write_config_word(pdev, PCI_STATUS,
6831 pci_status & (PCI_STATUS_DETECTED_PARITY |
6832 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6833 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6834
6835 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00006836 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006837 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 tp->cp_cmd &= ~PCIDAC;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006839 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841 }
6842
françois romieue6de30d2011-01-03 15:08:37 +00006843 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01006844
Francois Romieu98ddf982012-01-31 10:47:34 +01006845 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846}
6847
Francois Romieuda78dbf2012-01-26 14:18:23 +01006848static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849{
6850 unsigned int dirty_tx, tx_left;
6851
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852 dirty_tx = tp->dirty_tx;
6853 smp_rmb();
6854 tx_left = tp->cur_tx - dirty_tx;
6855
6856 while (tx_left > 0) {
6857 unsigned int entry = dirty_tx % NUM_TX_DESC;
6858 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859 u32 status;
6860
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6862 if (status & DescOwn)
6863 break;
6864
Alexander Duycka0750132014-12-11 15:02:17 -08006865 /* This barrier is needed to keep us from reading
6866 * any other fields out of the Tx descriptor until
6867 * we know the status of DescOwn
6868 */
6869 dma_rmb();
6870
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006871 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006872 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05006874 u64_stats_update_begin(&tp->tx_stats.syncp);
6875 tp->tx_stats.packets++;
6876 tp->tx_stats.bytes += tx_skb->skb->len;
6877 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006878 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 tx_skb->skb = NULL;
6880 }
6881 dirty_tx++;
6882 tx_left--;
6883 }
6884
6885 if (tp->dirty_tx != dirty_tx) {
6886 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01006887 /* Sync with rtl8169_start_xmit:
6888 * - publish dirty_tx ring index (write barrier)
6889 * - refresh cur_tx ring index and queue status (read barrier)
6890 * May the current thread miss the stopped queue condition,
6891 * a racing xmit thread can only have a right view of the
6892 * ring status.
6893 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006894 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02006896 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897 netif_wake_queue(dev);
6898 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02006899 /*
6900 * 8168 hack: TxPoll requests are lost when the Tx packets are
6901 * too close. Let's kick an extra TxPoll request when a burst
6902 * of start_xmit activity is detected (if it is not detected,
6903 * it is slow enough). -- FR
6904 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006905 if (tp->cur_tx != dirty_tx)
6906 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907 }
6908}
6909
Francois Romieu126fa4b2005-05-12 20:09:17 -04006910static inline int rtl8169_fragmented_frame(u32 status)
6911{
6912 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6913}
6914
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006915static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917 u32 status = opts1 & RxProtoMask;
6918
6919 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00006920 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921 skb->ip_summed = CHECKSUM_UNNECESSARY;
6922 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006923 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924}
6925
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006926static struct sk_buff *rtl8169_try_rx_copy(void *data,
6927 struct rtl8169_private *tp,
6928 int pkt_size,
6929 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02006931 struct sk_buff *skb;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006932 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006934 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006935 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006936 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08006937 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006938 if (skb)
Heiner Kallweit8a67aa82018-04-17 23:19:07 +02006939 skb_copy_to_linear_data(skb, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006940 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6941
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006942 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943}
6944
Francois Romieuda78dbf2012-01-26 14:18:23 +01006945static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946{
6947 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006948 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006949
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951
Timo Teräs9fba0812013-01-15 21:01:24 +00006952 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006954 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955 u32 status;
6956
Heiner Kallweit62028062018-04-17 23:30:29 +02006957 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 if (status & DescOwn)
6959 break;
Alexander Duycka0750132014-12-11 15:02:17 -08006960
6961 /* This barrier is needed to keep us from reading
6962 * any other fields out of the Rx descriptor until
6963 * we know the status of DescOwn
6964 */
6965 dma_rmb();
6966
Richard Dawe4dcb7d32005-05-27 21:12:00 +02006967 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006968 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6969 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006970 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02006972 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02006974 dev->stats.rx_crc_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006975 /* RxFOVF is a reserved bit on later chip versions */
6976 if (tp->mac_version == RTL_GIGA_MAC_VER_01 &&
6977 status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006978 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006979 dev->stats.rx_fifo_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006980 } else if (status & (RxRUNT | RxCRC) &&
6981 !(status & RxRWT) &&
6982 dev->features & NETIF_F_RXALL) {
Ben Greear6bbe0212012-02-10 15:04:33 +00006983 goto process_pkt;
Heiner Kallweit62028062018-04-17 23:30:29 +02006984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006986 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00006987 dma_addr_t addr;
6988 int pkt_size;
6989
6990process_pkt:
6991 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00006992 if (likely(!(dev->features & NETIF_F_RXFCS)))
6993 pkt_size = (status & 0x00003fff) - 4;
6994 else
6995 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996
Francois Romieu126fa4b2005-05-12 20:09:17 -04006997 /*
6998 * The driver does not support incoming fragmented
6999 * frames. They are seen as a symptom of over-mtu
7000 * sized frames.
7001 */
7002 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02007003 dev->stats.rx_dropped++;
7004 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00007005 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007006 }
7007
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007008 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7009 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007010 if (!skb) {
7011 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00007012 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013 }
7014
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007015 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 skb_put(skb, pkt_size);
7017 skb->protocol = eth_type_trans(skb, dev);
7018
Francois Romieu7a8fc772011-03-01 17:18:33 +01007019 rtl8169_rx_vlan_tag(desc, skb);
7020
françois romieu39174292015-11-11 23:35:18 +01007021 if (skb->pkt_type == PACKET_MULTICAST)
7022 dev->stats.multicast++;
7023
Francois Romieu56de4142011-03-15 17:29:31 +01007024 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025
Junchang Wang8027aa22012-03-04 23:30:32 +01007026 u64_stats_update_begin(&tp->rx_stats.syncp);
7027 tp->rx_stats.packets++;
7028 tp->rx_stats.bytes += pkt_size;
7029 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030 }
françois romieuce11ff52013-01-24 13:30:06 +00007031release_descriptor:
7032 desc->opts2 = 0;
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02007033 rtl8169_mark_to_asic(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 }
7035
7036 count = cur_rx - tp->cur_rx;
7037 tp->cur_rx = cur_rx;
7038
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039 return count;
7040}
7041
Francois Romieu07d3f512007-02-21 22:40:46 +01007042static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043{
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02007044 struct rtl8169_private *tp = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007046 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007048 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007049 if (status && status != 0xffff) {
7050 status &= RTL_EVENT_NAPI | tp->event_slow;
7051 if (status) {
7052 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00007053
Francois Romieuda78dbf2012-01-26 14:18:23 +01007054 rtl_irq_disable(tp);
Heiner Kallweit9a899a32018-04-17 23:21:01 +02007055 napi_schedule_irqoff(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058 return IRQ_RETVAL(handled);
7059}
7060
Francois Romieuda78dbf2012-01-26 14:18:23 +01007061/*
7062 * Workqueue context.
7063 */
7064static void rtl_slow_event_work(struct rtl8169_private *tp)
7065{
7066 struct net_device *dev = tp->dev;
7067 u16 status;
7068
7069 status = rtl_get_events(tp) & tp->event_slow;
7070 rtl_ack_events(tp, status);
7071
7072 if (unlikely(status & RxFIFOOver)) {
7073 switch (tp->mac_version) {
7074 /* Work around for rx fifo overflow */
7075 case RTL_GIGA_MAC_VER_11:
7076 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01007077 /* XXX - Hack alert. See rtl_task(). */
7078 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007079 default:
7080 break;
7081 }
7082 }
7083
7084 if (unlikely(status & SYSErr))
7085 rtl8169_pcierr_interrupt(dev);
7086
7087 if (status & LinkChg)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007088 rtl8169_check_link_status(dev, tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007089
françois romieu7dbb4912012-06-09 10:53:16 +00007090 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007091}
7092
Francois Romieu4422bcd2012-01-26 11:23:32 +01007093static void rtl_task(struct work_struct *work)
7094{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007095 static const struct {
7096 int bitnr;
7097 void (*action)(struct rtl8169_private *);
7098 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01007099 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007100 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7101 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7102 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7103 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01007104 struct rtl8169_private *tp =
7105 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007106 struct net_device *dev = tp->dev;
7107 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01007108
Francois Romieuda78dbf2012-01-26 14:18:23 +01007109 rtl_lock_work(tp);
7110
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007111 if (!netif_running(dev) ||
7112 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01007113 goto out_unlock;
7114
7115 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7116 bool pending;
7117
Francois Romieuda78dbf2012-01-26 14:18:23 +01007118 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007119 if (pending)
7120 rtl_work[i].action(tp);
7121 }
7122
7123out_unlock:
7124 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01007125}
7126
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007127static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007129 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7130 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007131 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7132 int work_done= 0;
7133 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134
Francois Romieuda78dbf2012-01-26 14:18:23 +01007135 status = rtl_get_events(tp);
7136 rtl_ack_events(tp, status & ~tp->event_slow);
7137
7138 if (status & RTL_EVENT_NAPI_RX)
7139 work_done = rtl_rx(dev, tp, (u32) budget);
7140
7141 if (status & RTL_EVENT_NAPI_TX)
7142 rtl_tx(dev, tp);
7143
7144 if (status & tp->event_slow) {
7145 enable_mask &= ~tp->event_slow;
7146
7147 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007150 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08007151 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00007152
Francois Romieuda78dbf2012-01-26 14:18:23 +01007153 rtl_irq_enable(tp, enable_mask);
7154 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155 }
7156
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007157 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007160static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02007161{
7162 struct rtl8169_private *tp = netdev_priv(dev);
7163
7164 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7165 return;
7166
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007167 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
7168 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02007169}
7170
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171static void rtl8169_down(struct net_device *dev)
7172{
7173 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174
Francois Romieu4876cc12011-03-11 21:07:11 +01007175 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007177 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007178 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179
Hayes Wang92fc43b2011-07-06 15:58:03 +08007180 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007181 /*
7182 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007183 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7184 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007185 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007186 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007189 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191 rtl8169_tx_clear(tp);
7192
7193 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007194
7195 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196}
7197
7198static int rtl8169_close(struct net_device *dev)
7199{
7200 struct rtl8169_private *tp = netdev_priv(dev);
7201 struct pci_dev *pdev = tp->pci_dev;
7202
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007203 pm_runtime_get_sync(&pdev->dev);
7204
Francois Romieucecb5fd2011-04-01 10:21:07 +02007205 /* Update counters before going down */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007206 rtl8169_update_counters(tp);
Ivan Vecera355423d2009-02-06 21:49:57 -08007207
Francois Romieuda78dbf2012-01-26 14:18:23 +01007208 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007209 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007210
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007212 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213
Lekensteyn4ea72442013-07-22 09:53:30 +02007214 cancel_work_sync(&tp->wk.work);
7215
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02007216 pci_free_irq(pdev, 0, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007218 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7219 tp->RxPhyAddr);
7220 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7221 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222 tp->TxDescArray = NULL;
7223 tp->RxDescArray = NULL;
7224
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007225 pm_runtime_put_sync(&pdev->dev);
7226
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227 return 0;
7228}
7229
Francois Romieudc1c00c2012-03-08 10:06:18 +01007230#ifdef CONFIG_NET_POLL_CONTROLLER
7231static void rtl8169_netpoll(struct net_device *dev)
7232{
7233 struct rtl8169_private *tp = netdev_priv(dev);
7234
Heiner Kallweit29274992018-02-28 20:43:38 +01007235 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), dev);
Francois Romieudc1c00c2012-03-08 10:06:18 +01007236}
7237#endif
7238
Francois Romieudf43ac72012-03-08 09:48:40 +01007239static int rtl_open(struct net_device *dev)
7240{
7241 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007242 struct pci_dev *pdev = tp->pci_dev;
7243 int retval = -ENOMEM;
7244
7245 pm_runtime_get_sync(&pdev->dev);
7246
7247 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007248 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007249 * dma_alloc_coherent provides more.
7250 */
7251 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7252 &tp->TxPhyAddr, GFP_KERNEL);
7253 if (!tp->TxDescArray)
7254 goto err_pm_runtime_put;
7255
7256 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7257 &tp->RxPhyAddr, GFP_KERNEL);
7258 if (!tp->RxDescArray)
7259 goto err_free_tx_0;
7260
Heiner Kallweitb1127e62018-04-17 23:23:35 +02007261 retval = rtl8169_init_ring(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007262 if (retval < 0)
7263 goto err_free_rx_1;
7264
7265 INIT_WORK(&tp->wk.work, rtl_task);
7266
7267 smp_mb();
7268
7269 rtl_request_firmware(tp);
7270
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02007271 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007272 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01007273 if (retval < 0)
7274 goto err_release_fw_2;
7275
7276 rtl_lock_work(tp);
7277
7278 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7279
7280 napi_enable(&tp->napi);
7281
7282 rtl8169_init_phy(dev, tp);
7283
Francois Romieudf43ac72012-03-08 09:48:40 +01007284 rtl_pll_power_up(tp);
7285
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007286 rtl_hw_start(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007287
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007288 if (!rtl8169_init_counter_offsets(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007289 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7290
Francois Romieudf43ac72012-03-08 09:48:40 +01007291 netif_start_queue(dev);
7292
7293 rtl_unlock_work(tp);
7294
7295 tp->saved_wolopts = 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007296 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007297
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007298 rtl8169_check_link_status(dev, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007299out:
7300 return retval;
7301
7302err_release_fw_2:
7303 rtl_release_firmware(tp);
7304 rtl8169_rx_clear(tp);
7305err_free_rx_1:
7306 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7307 tp->RxPhyAddr);
7308 tp->RxDescArray = NULL;
7309err_free_tx_0:
7310 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7311 tp->TxPhyAddr);
7312 tp->TxDescArray = NULL;
7313err_pm_runtime_put:
7314 pm_runtime_put_noidle(&pdev->dev);
7315 goto out;
7316}
7317
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007318static void
Junchang Wang8027aa22012-03-04 23:30:32 +01007319rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007320{
7321 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007322 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02007323 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01007324 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007326 pm_runtime_get_noresume(&pdev->dev);
7327
7328 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007329 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007330
Junchang Wang8027aa22012-03-04 23:30:32 +01007331 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007332 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007333 stats->rx_packets = tp->rx_stats.packets;
7334 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007335 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007336
Junchang Wang8027aa22012-03-04 23:30:32 +01007337 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007338 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007339 stats->tx_packets = tp->tx_stats.packets;
7340 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007341 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007342
7343 stats->rx_dropped = dev->stats.rx_dropped;
7344 stats->tx_dropped = dev->stats.tx_dropped;
7345 stats->rx_length_errors = dev->stats.rx_length_errors;
7346 stats->rx_errors = dev->stats.rx_errors;
7347 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7348 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7349 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02007350 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01007351
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007352 /*
7353 * Fetch additonal counter values missing in stats collected by driver
7354 * from tally counters.
7355 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007356 if (pm_runtime_active(&pdev->dev))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007357 rtl8169_update_counters(tp);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007358
7359 /*
7360 * Subtract values fetched during initalization.
7361 * See rtl8169_init_counter_offsets for a description why we do that.
7362 */
Corinna Vinschen42020322015-09-10 10:47:35 +02007363 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007364 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02007365 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007366 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02007367 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007368 le16_to_cpu(tp->tc_offset.tx_aborted);
7369
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007370 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371}
7372
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007373static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01007374{
françois romieu065c27c2011-01-03 15:08:12 +00007375 struct rtl8169_private *tp = netdev_priv(dev);
7376
Francois Romieu5d06a992006-02-23 00:47:58 +01007377 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007378 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01007379
7380 netif_device_detach(dev);
7381 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007382
7383 rtl_lock_work(tp);
7384 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007385 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007386 rtl_unlock_work(tp);
7387
7388 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007389}
Francois Romieu5d06a992006-02-23 00:47:58 +01007390
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007391#ifdef CONFIG_PM
7392
7393static int rtl8169_suspend(struct device *device)
7394{
7395 struct pci_dev *pdev = to_pci_dev(device);
7396 struct net_device *dev = pci_get_drvdata(pdev);
7397
7398 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02007399
Francois Romieu5d06a992006-02-23 00:47:58 +01007400 return 0;
7401}
7402
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007403static void __rtl8169_resume(struct net_device *dev)
7404{
françois romieu065c27c2011-01-03 15:08:12 +00007405 struct rtl8169_private *tp = netdev_priv(dev);
7406
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007407 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00007408
7409 rtl_pll_power_up(tp);
7410
Artem Savkovcff4c162012-04-03 10:29:11 +00007411 rtl_lock_work(tp);
7412 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007413 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00007414 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007415
Francois Romieu98ddf982012-01-31 10:47:34 +01007416 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007417}
7418
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007419static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01007420{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007421 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01007422 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007423 struct rtl8169_private *tp = netdev_priv(dev);
7424
7425 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01007426
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007427 if (netif_running(dev))
7428 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01007429
Francois Romieu5d06a992006-02-23 00:47:58 +01007430 return 0;
7431}
7432
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007433static int rtl8169_runtime_suspend(struct device *device)
7434{
7435 struct pci_dev *pdev = to_pci_dev(device);
7436 struct net_device *dev = pci_get_drvdata(pdev);
7437 struct rtl8169_private *tp = netdev_priv(dev);
7438
Heiner Kallweita92a0842018-01-08 21:39:13 +01007439 if (!tp->TxDescArray) {
7440 rtl_pll_power_down(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007441 return 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007442 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007443
Francois Romieuda78dbf2012-01-26 14:18:23 +01007444 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007445 tp->saved_wolopts = __rtl8169_get_wol(tp);
7446 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007447 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007448
7449 rtl8169_net_suspend(dev);
7450
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007451 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007452 rtl8169_rx_missed(dev);
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007453 rtl8169_update_counters(tp);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007454
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007455 return 0;
7456}
7457
7458static int rtl8169_runtime_resume(struct device *device)
7459{
7460 struct pci_dev *pdev = to_pci_dev(device);
7461 struct net_device *dev = pci_get_drvdata(pdev);
7462 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08007463 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007464
7465 if (!tp->TxDescArray)
7466 return 0;
7467
Francois Romieuda78dbf2012-01-26 14:18:23 +01007468 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007469 __rtl8169_set_wol(tp, tp->saved_wolopts);
7470 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007471 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007472
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007473 rtl8169_init_phy(dev, tp);
7474
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007475 __rtl8169_resume(dev);
7476
7477 return 0;
7478}
7479
7480static int rtl8169_runtime_idle(struct device *device)
7481{
7482 struct pci_dev *pdev = to_pci_dev(device);
7483 struct net_device *dev = pci_get_drvdata(pdev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007484
Heiner Kallweita92a0842018-01-08 21:39:13 +01007485 if (!netif_running(dev) || !netif_carrier_ok(dev))
7486 pm_schedule_suspend(device, 10000);
7487
7488 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007489}
7490
Alexey Dobriyan47145212009-12-14 18:00:08 -08007491static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02007492 .suspend = rtl8169_suspend,
7493 .resume = rtl8169_resume,
7494 .freeze = rtl8169_suspend,
7495 .thaw = rtl8169_resume,
7496 .poweroff = rtl8169_suspend,
7497 .restore = rtl8169_resume,
7498 .runtime_suspend = rtl8169_runtime_suspend,
7499 .runtime_resume = rtl8169_runtime_resume,
7500 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007501};
7502
7503#define RTL8169_PM_OPS (&rtl8169_pm_ops)
7504
7505#else /* !CONFIG_PM */
7506
7507#define RTL8169_PM_OPS NULL
7508
7509#endif /* !CONFIG_PM */
7510
David S. Miller1805b2f2011-10-24 18:18:09 -04007511static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7512{
David S. Miller1805b2f2011-10-24 18:18:09 -04007513 /* WoL fails with 8168b when the receiver is disabled. */
7514 switch (tp->mac_version) {
7515 case RTL_GIGA_MAC_VER_11:
7516 case RTL_GIGA_MAC_VER_12:
7517 case RTL_GIGA_MAC_VER_17:
7518 pci_clear_master(tp->pci_dev);
7519
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007520 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007521 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007522 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04007523 break;
7524 default:
7525 break;
7526 }
7527}
7528
Francois Romieu1765f952008-09-13 17:21:40 +02007529static void rtl_shutdown(struct pci_dev *pdev)
7530{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007531 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00007532 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02007533
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007534 rtl8169_net_suspend(dev);
7535
Francois Romieucecb5fd2011-04-01 10:21:07 +02007536 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08007537 rtl_rar_set(tp, dev->perm_addr);
7538
Hayes Wang92fc43b2011-07-06 15:58:03 +08007539 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00007540
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007541 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04007542 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
7543 rtl_wol_suspend_quirk(tp);
7544 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00007545 }
7546
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007547 pci_wake_from_d3(pdev, true);
7548 pci_set_power_state(pdev, PCI_D3hot);
7549 }
7550}
Francois Romieu5d06a992006-02-23 00:47:58 +01007551
Bill Pembertonbaf63292012-12-03 09:23:28 -05007552static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01007553{
7554 struct net_device *dev = pci_get_drvdata(pdev);
7555 struct rtl8169_private *tp = netdev_priv(dev);
7556
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007557 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01007558 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01007559
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007560 netif_napi_del(&tp->napi);
7561
Francois Romieue27566e2012-03-08 09:54:01 +01007562 unregister_netdev(dev);
7563
7564 rtl_release_firmware(tp);
7565
7566 if (pci_dev_run_wake(pdev))
7567 pm_runtime_get_noresume(&pdev->dev);
7568
7569 /* restore original MAC address */
7570 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01007571}
7572
Francois Romieufa9c3852012-03-08 10:01:50 +01007573static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01007574 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01007575 .ndo_stop = rtl8169_close,
7576 .ndo_get_stats64 = rtl8169_get_stats64,
7577 .ndo_start_xmit = rtl8169_start_xmit,
7578 .ndo_tx_timeout = rtl8169_tx_timeout,
7579 .ndo_validate_addr = eth_validate_addr,
7580 .ndo_change_mtu = rtl8169_change_mtu,
7581 .ndo_fix_features = rtl8169_fix_features,
7582 .ndo_set_features = rtl8169_set_features,
7583 .ndo_set_mac_address = rtl_set_mac_address,
7584 .ndo_do_ioctl = rtl8169_ioctl,
7585 .ndo_set_rx_mode = rtl_set_rx_mode,
7586#ifdef CONFIG_NET_POLL_CONTROLLER
7587 .ndo_poll_controller = rtl8169_netpoll,
7588#endif
7589
7590};
7591
Francois Romieu31fa8b12012-03-08 10:09:40 +01007592static const struct rtl_cfg_info {
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007593 void (*hw_start)(struct rtl8169_private *tp);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007594 u16 event_slow;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007595 unsigned int has_gmii:1;
Francois Romieu50970832017-10-27 13:24:49 +03007596 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007597 u8 default_ver;
7598} rtl_cfg_infos [] = {
7599 [RTL_CFG_0] = {
7600 .hw_start = rtl_hw_start_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007601 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007602 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007603 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007604 .default_ver = RTL_GIGA_MAC_VER_01,
7605 },
7606 [RTL_CFG_1] = {
7607 .hw_start = rtl_hw_start_8168,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007608 .event_slow = SYSErr | LinkChg | RxOverflow,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007609 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007610 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007611 .default_ver = RTL_GIGA_MAC_VER_11,
7612 },
7613 [RTL_CFG_2] = {
7614 .hw_start = rtl_hw_start_8101,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007615 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
7616 PCSTimeout,
Francois Romieu50970832017-10-27 13:24:49 +03007617 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007618 .default_ver = RTL_GIGA_MAC_VER_13,
7619 }
7620};
7621
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007622static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01007623{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007624 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007625
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007626 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007627 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
7628 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
7629 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007630 flags = PCI_IRQ_LEGACY;
7631 } else {
7632 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007633 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007634
7635 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007636}
7637
Hayes Wangc5583862012-07-02 17:23:22 +08007638DECLARE_RTL_COND(rtl_link_list_ready_cond)
7639{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007640 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08007641}
7642
7643DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7644{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007645 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08007646}
7647
Bill Pembertonbaf63292012-12-03 09:23:28 -05007648static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007649{
Hayes Wangc5583862012-07-02 17:23:22 +08007650 u32 data;
7651
7652 tp->ocp_base = OCP_STD_PHY_BASE;
7653
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007654 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08007655
7656 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7657 return;
7658
7659 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7660 return;
7661
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007662 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08007663 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007664 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08007665
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007666 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007667 data &= ~(1 << 14);
7668 r8168_mac_ocp_write(tp, 0xe8de, data);
7669
7670 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7671 return;
7672
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007673 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007674 data |= (1 << 15);
7675 r8168_mac_ocp_write(tp, 0xe8de, data);
7676
7677 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7678 return;
7679}
7680
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007681static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
7682{
7683 rtl8168ep_stop_cmac(tp);
7684 rtl_hw_init_8168g(tp);
7685}
7686
Bill Pembertonbaf63292012-12-03 09:23:28 -05007687static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007688{
7689 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02007690 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007691 rtl_hw_init_8168g(tp);
7692 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02007693 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007694 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08007695 break;
Hayes Wangc5583862012-07-02 17:23:22 +08007696 default:
7697 break;
7698 }
7699}
7700
hayeswang929a0312014-09-16 11:40:47 +08007701static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01007702{
7703 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007704 struct rtl8169_private *tp;
7705 struct mii_if_info *mii;
7706 struct net_device *dev;
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007707 int chipset, region, i;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007708 int rc;
7709
7710 if (netif_msg_drv(&debug)) {
7711 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
7712 MODULENAME, RTL8169_VERSION);
7713 }
7714
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007715 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
7716 if (!dev)
7717 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007718
7719 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01007720 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007721 tp = netdev_priv(dev);
7722 tp->dev = dev;
7723 tp->pci_dev = pdev;
7724 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
7725
7726 mii = &tp->mii;
7727 mii->dev = dev;
7728 mii->mdio_read = rtl_mdio_read;
7729 mii->mdio_write = rtl_mdio_write;
7730 mii->phy_id_mask = 0x1f;
7731 mii->reg_num_mask = 0x1f;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007732 mii->supports_gmii = cfg->has_gmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007733
7734 /* disable ASPM completely as that cause random device stop working
7735 * problems as well as full system hangs for some PCIe devices users */
7736 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
7737 PCIE_LINK_STATE_CLKPM);
7738
7739 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007740 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007741 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007742 dev_err(&pdev->dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007743 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007744 }
7745
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007746 if (pcim_set_mwi(pdev) < 0)
Heiner Kallweit22148df2018-04-22 17:15:15 +02007747 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007748
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007749 /* use first MMIO region */
7750 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
7751 if (region < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007752 dev_err(&pdev->dev, "no MMIO resource found\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007753 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007754 }
7755
7756 /* check for weird/broken PCI region reporting */
7757 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007758 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007759 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007760 }
7761
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007762 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007763 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007764 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007765 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007766 }
7767
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007768 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01007769
7770 if (!pci_is_pcie(pdev))
Heiner Kallweit22148df2018-04-22 17:15:15 +02007771 dev_info(&pdev->dev, "not PCI Express\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007772
7773 /* Identify chip attached to board */
Heiner Kallweit22148df2018-04-22 17:15:15 +02007774 rtl8169_get_mac_version(tp, cfg->default_ver);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007775
Heiner Kallweit0ae09742018-04-28 22:19:26 +02007776 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007777
7778 if ((sizeof(dma_addr_t) > 4) &&
7779 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
7780 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01007781 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
7782 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007783
7784 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
7785 if (!pci_is_pcie(pdev))
7786 tp->cp_cmd |= PCIDAC;
7787 dev->features |= NETIF_F_HIGHDMA;
7788 } else {
7789 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7790 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007791 dev_err(&pdev->dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007792 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007793 }
7794 }
7795
Francois Romieu3b6cf252012-03-08 09:59:04 +01007796 rtl_init_rxcfg(tp);
7797
7798 rtl_irq_disable(tp);
7799
Hayes Wangc5583862012-07-02 17:23:22 +08007800 rtl_hw_initialize(tp);
7801
Francois Romieu3b6cf252012-03-08 09:59:04 +01007802 rtl_hw_reset(tp);
7803
7804 rtl_ack_events(tp, 0xffff);
7805
7806 pci_set_master(pdev);
7807
Francois Romieu3b6cf252012-03-08 09:59:04 +01007808 rtl_init_mdio_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007809 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08007810 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007811
7812 rtl8169_print_mac_version(tp);
7813
7814 chipset = tp->mac_version;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007815
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007816 rc = rtl_alloc_irq(tp);
7817 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007818 dev_err(&pdev->dev, "Can't allocate interrupt\n");
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007819 return rc;
7820 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007821
Heiner Kallweit7edf6d32018-02-22 21:22:40 +01007822 /* override BIOS settings, use userspace tools to enable WOL */
7823 __rtl8169_set_wol(tp, 0);
7824
Francois Romieu3b6cf252012-03-08 09:59:04 +01007825 if (rtl_tbi_enabled(tp)) {
7826 tp->set_speed = rtl8169_set_speed_tbi;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01007827 tp->get_link_ksettings = rtl8169_get_link_ksettings_tbi;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007828 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
7829 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
7830 tp->link_ok = rtl8169_tbi_link_ok;
7831 tp->do_ioctl = rtl_tbi_ioctl;
7832 } else {
7833 tp->set_speed = rtl8169_set_speed_xmii;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01007834 tp->get_link_ksettings = rtl8169_get_link_ksettings_xmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007835 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
7836 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
7837 tp->link_ok = rtl8169_xmii_link_ok;
7838 tp->do_ioctl = rtl_xmii_ioctl;
7839 }
7840
7841 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05007842 u64_stats_init(&tp->rx_stats.syncp);
7843 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007844
7845 /* Get MAC address */
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007846 switch (tp->mac_version) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007847 u16 mac_addr[3];
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007848 case RTL_GIGA_MAC_VER_35 ... RTL_GIGA_MAC_VER_38:
7849 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08007850 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
7851 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007852
7853 if (is_valid_ether_addr((u8 *)mac_addr))
7854 rtl_rar_set(tp, (u8 *)mac_addr);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007855 break;
7856 default:
7857 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007858 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007859 for (i = 0; i < ETH_ALEN; i++)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007860 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007861
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00007862 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007863 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007864
Heiner Kallweit37621492018-04-17 23:20:03 +02007865 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007866
7867 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7868 * properly for all devices */
7869 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00007870 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007871
7872 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00007873 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7874 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007875 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7876 NETIF_F_HIGHDMA;
7877
hayeswang929a0312014-09-16 11:40:47 +08007878 tp->cp_cmd |= RxChkSum | RxVlan;
7879
7880 /*
7881 * Pretend we are using VLANs; This bypasses a nasty bug where
7882 * Interrupts stop flowing on high load on 8110SCd controllers.
7883 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01007884 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08007885 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00007886 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007887
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007888 switch (rtl_chip_infos[chipset].txd_version) {
7889 case RTL_TD_0:
hayeswang5888d3f2014-07-11 16:25:56 +08007890 tp->tso_csum = rtl8169_tso_csum_v1;
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007891 break;
7892 case RTL_TD_1:
hayeswang5888d3f2014-07-11 16:25:56 +08007893 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08007894 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007895 break;
7896 default:
hayeswang5888d3f2014-07-11 16:25:56 +08007897 WARN_ON_ONCE(1);
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007898 }
hayeswang5888d3f2014-07-11 16:25:56 +08007899
Francois Romieu3b6cf252012-03-08 09:59:04 +01007900 dev->hw_features |= NETIF_F_RXALL;
7901 dev->hw_features |= NETIF_F_RXFCS;
7902
Jarod Wilsonc7315a92016-10-17 15:54:09 -04007903 /* MTU range: 60 - hw-specific max */
7904 dev->min_mtu = ETH_ZLEN;
7905 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
7906
Francois Romieu3b6cf252012-03-08 09:59:04 +01007907 tp->hw_start = cfg->hw_start;
7908 tp->event_slow = cfg->event_slow;
Francois Romieu50970832017-10-27 13:24:49 +03007909 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007910
Kees Cook9de36cc2017-10-25 03:53:12 -07007911 timer_setup(&tp->timer, rtl8169_phy_timer, 0);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007912
7913 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
7914
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007915 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
7916 &tp->counters_phys_addr,
7917 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007918 if (!tp->counters)
7919 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02007920
Heiner Kallweit19c9ea32018-03-26 19:19:30 +02007921 pci_set_drvdata(pdev, dev);
7922
Francois Romieu3b6cf252012-03-08 09:59:04 +01007923 rc = register_netdev(dev);
7924 if (rc < 0)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007925 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007926
Heiner Kallweit2d6c5a62018-04-17 23:31:21 +02007927 netif_info(tp, probe, dev, "%s, %pM, XID %08x, IRQ %d\n",
7928 rtl_chip_infos[chipset].name, dev->dev_addr,
Heiner Kallweit90b989c2018-04-17 23:32:15 +02007929 (u32)(RTL_R32(tp, TxConfig) & 0xfcf0f8ff),
Heiner Kallweit29274992018-02-28 20:43:38 +01007930 pci_irq_vector(pdev, 0));
Francois Romieu3b6cf252012-03-08 09:59:04 +01007931 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
7932 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
7933 "tx checksumming: %s]\n",
7934 rtl_chip_infos[chipset].jumbo_max,
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02007935 tp->mac_version <= RTL_GIGA_MAC_VER_06 ? "ok" : "ko");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007936 }
7937
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007938 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01007939 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007940
Francois Romieu3b6cf252012-03-08 09:59:04 +01007941 netif_carrier_off(dev);
7942
Heiner Kallweita92a0842018-01-08 21:39:13 +01007943 if (pci_dev_run_wake(pdev))
7944 pm_runtime_put_sync(&pdev->dev);
7945
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007946 return 0;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007947}
7948
Linus Torvalds1da177e2005-04-16 15:20:36 -07007949static struct pci_driver rtl8169_pci_driver = {
7950 .name = MODULENAME,
7951 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01007952 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05007953 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02007954 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007955 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007956};
7957
Devendra Naga3eeb7da2012-10-26 09:27:42 +00007958module_pci_driver(rtl8169_pci_driver);