blob: d598fdf0470cfcf505706c592494018779a51eb1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000024#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000026#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000027#include <linux/firmware.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040028#include <linux/prefetch.h>
hayeswange9746042014-07-11 16:25:58 +080029#include <linux/ipv6.h>
30#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/io.h>
33#include <asm/irq.h>
34
Francois Romieu865c6522008-05-11 14:51:00 +020035#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define MODULENAME "r8169"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
françois romieubca03d52011-01-03 15:07:31 +000038#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
39#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000040#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
41#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080042#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080043#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
44#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080045#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080046#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080047#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080048#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080049#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000050#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000051#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000052#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +080053#define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
54#define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
55#define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
56#define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
françois romieubca03d52011-01-03 15:07:31 +000057
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020058#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070059 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020060
Julien Ducourthial477206a2012-05-09 00:00:06 +020061#define TX_SLOTS_AVAIL(tp) \
62 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
63
64/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
65#define TX_FRAGS_READY_FOR(tp,nr_frags) \
66 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
69 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050070static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Michal Schmidtaee77e42012-09-09 13:55:26 +000072#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
74
75#define R8169_REGS_SIZE 256
Heiner Kallweit1d0254d2018-04-17 23:25:46 +020076#define R8169_RX_BUF_SIZE (SZ_16K - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000078#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
80#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
81
82#define RTL8169_TX_TIMEOUT (6*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/* write/read MMIO register */
Andy Shevchenko1ef72862018-03-01 13:27:34 +020085#define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
86#define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
87#define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
88#define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
89#define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
90#define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +020093 RTL_GIGA_MAC_VER_01 = 0,
94 RTL_GIGA_MAC_VER_02,
95 RTL_GIGA_MAC_VER_03,
96 RTL_GIGA_MAC_VER_04,
97 RTL_GIGA_MAC_VER_05,
98 RTL_GIGA_MAC_VER_06,
99 RTL_GIGA_MAC_VER_07,
100 RTL_GIGA_MAC_VER_08,
101 RTL_GIGA_MAC_VER_09,
102 RTL_GIGA_MAC_VER_10,
103 RTL_GIGA_MAC_VER_11,
104 RTL_GIGA_MAC_VER_12,
105 RTL_GIGA_MAC_VER_13,
106 RTL_GIGA_MAC_VER_14,
107 RTL_GIGA_MAC_VER_15,
108 RTL_GIGA_MAC_VER_16,
109 RTL_GIGA_MAC_VER_17,
110 RTL_GIGA_MAC_VER_18,
111 RTL_GIGA_MAC_VER_19,
112 RTL_GIGA_MAC_VER_20,
113 RTL_GIGA_MAC_VER_21,
114 RTL_GIGA_MAC_VER_22,
115 RTL_GIGA_MAC_VER_23,
116 RTL_GIGA_MAC_VER_24,
117 RTL_GIGA_MAC_VER_25,
118 RTL_GIGA_MAC_VER_26,
119 RTL_GIGA_MAC_VER_27,
120 RTL_GIGA_MAC_VER_28,
121 RTL_GIGA_MAC_VER_29,
122 RTL_GIGA_MAC_VER_30,
123 RTL_GIGA_MAC_VER_31,
124 RTL_GIGA_MAC_VER_32,
125 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800126 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800127 RTL_GIGA_MAC_VER_35,
128 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800129 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800130 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800131 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800132 RTL_GIGA_MAC_VER_40,
133 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000134 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000135 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800136 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800137 RTL_GIGA_MAC_VER_45,
138 RTL_GIGA_MAC_VER_46,
139 RTL_GIGA_MAC_VER_47,
140 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800141 RTL_GIGA_MAC_VER_49,
142 RTL_GIGA_MAC_VER_50,
143 RTL_GIGA_MAC_VER_51,
Francois Romieu85bffe62011-04-27 08:22:39 +0200144 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
Francois Romieu2b7b4312011-04-18 22:53:24 -0700147enum rtl_tx_desc_version {
148 RTL_TD_0 = 0,
149 RTL_TD_1 = 1,
150};
151
Francois Romieud58d46b2011-05-03 16:38:29 +0200152#define JUMBO_1K ETH_DATA_LEN
153#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
154#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
155#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
156#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
157
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200158#define _R(NAME,TD,FW,SZ) { \
Francois Romieud58d46b2011-05-03 16:38:29 +0200159 .name = NAME, \
160 .txd_version = TD, \
161 .fw_name = FW, \
162 .jumbo_max = SZ, \
Francois Romieud58d46b2011-05-03 16:38:29 +0200163}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800165static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700167 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200168 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200169 u16 jumbo_max;
Francois Romieu85bffe62011-04-27 08:22:39 +0200170} rtl_chip_infos[] = {
171 /* PCI devices. */
172 [RTL_GIGA_MAC_VER_01] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200173 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200174 [RTL_GIGA_MAC_VER_02] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200175 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200176 [RTL_GIGA_MAC_VER_03] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200177 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200178 [RTL_GIGA_MAC_VER_04] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200179 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200180 [RTL_GIGA_MAC_VER_05] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200181 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200182 [RTL_GIGA_MAC_VER_06] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200183 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200184 /* PCI-E devices. */
185 [RTL_GIGA_MAC_VER_07] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200186 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200187 [RTL_GIGA_MAC_VER_08] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200188 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200189 [RTL_GIGA_MAC_VER_09] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200190 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200191 [RTL_GIGA_MAC_VER_10] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200192 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200193 [RTL_GIGA_MAC_VER_11] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200194 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200195 [RTL_GIGA_MAC_VER_12] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200196 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200197 [RTL_GIGA_MAC_VER_13] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200198 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200199 [RTL_GIGA_MAC_VER_14] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200200 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200201 [RTL_GIGA_MAC_VER_15] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200202 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200203 [RTL_GIGA_MAC_VER_16] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200204 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200205 [RTL_GIGA_MAC_VER_17] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200206 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200207 [RTL_GIGA_MAC_VER_18] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200208 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200209 [RTL_GIGA_MAC_VER_19] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200210 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200211 [RTL_GIGA_MAC_VER_20] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200212 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200213 [RTL_GIGA_MAC_VER_21] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200214 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200215 [RTL_GIGA_MAC_VER_22] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200216 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200217 [RTL_GIGA_MAC_VER_23] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200218 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200219 [RTL_GIGA_MAC_VER_24] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200220 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200221 [RTL_GIGA_MAC_VER_25] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200222 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200223 [RTL_GIGA_MAC_VER_26] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200224 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200225 [RTL_GIGA_MAC_VER_27] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200226 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200227 [RTL_GIGA_MAC_VER_28] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200228 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200229 [RTL_GIGA_MAC_VER_29] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200230 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200231 [RTL_GIGA_MAC_VER_30] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200232 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200233 [RTL_GIGA_MAC_VER_31] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200234 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200235 [RTL_GIGA_MAC_VER_32] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200236 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200237 [RTL_GIGA_MAC_VER_33] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200238 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2, JUMBO_9K),
Hayes Wang70090422011-07-06 15:58:06 +0800239 [RTL_GIGA_MAC_VER_34] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200240 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3, JUMBO_9K),
Hayes Wangc2218922011-09-06 16:55:18 +0800241 [RTL_GIGA_MAC_VER_35] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200242 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1, JUMBO_9K),
Hayes Wangc2218922011-09-06 16:55:18 +0800243 [RTL_GIGA_MAC_VER_36] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200244 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2, JUMBO_9K),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800245 [RTL_GIGA_MAC_VER_37] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200246 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1, JUMBO_1K),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800247 [RTL_GIGA_MAC_VER_38] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200248 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1, JUMBO_9K),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800249 [RTL_GIGA_MAC_VER_39] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200250 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1, JUMBO_1K),
Hayes Wangc5583862012-07-02 17:23:22 +0800251 [RTL_GIGA_MAC_VER_40] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200252 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2, JUMBO_9K),
Hayes Wangc5583862012-07-02 17:23:22 +0800253 [RTL_GIGA_MAC_VER_41] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200254 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K),
hayeswang57538c42013-04-01 22:23:40 +0000255 [RTL_GIGA_MAC_VER_42] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200256 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3, JUMBO_9K),
hayeswang58152cd2013-04-01 22:23:42 +0000257 [RTL_GIGA_MAC_VER_43] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200258 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2, JUMBO_1K),
hayeswang45dd95c2013-07-08 17:09:01 +0800259 [RTL_GIGA_MAC_VER_44] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200260 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2, JUMBO_9K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800261 [RTL_GIGA_MAC_VER_45] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200262 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1, JUMBO_9K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800263 [RTL_GIGA_MAC_VER_46] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200264 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2, JUMBO_9K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800265 [RTL_GIGA_MAC_VER_47] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200266 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1, JUMBO_1K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800267 [RTL_GIGA_MAC_VER_48] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200268 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2, JUMBO_1K),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800269 [RTL_GIGA_MAC_VER_49] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200270 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800271 [RTL_GIGA_MAC_VER_50] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200272 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800273 [RTL_GIGA_MAC_VER_51] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200274 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275};
276#undef _R
277
Francois Romieubcf0bf92006-07-26 23:14:13 +0200278enum cfg_version {
279 RTL_CFG_0 = 0x00,
280 RTL_CFG_1,
281 RTL_CFG_2
282};
283
Benoit Taine9baa3c32014-08-08 15:56:03 +0200284static const struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200285 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200286 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Chun-Hao Lin610c9082016-12-27 16:29:43 +0800287 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
Francois Romieud81bf552006-09-20 21:31:20 +0200288 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100289 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200290 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200291 { PCI_VENDOR_ID_DLINK, 0x4300,
292 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200293 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000294 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200295 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200296 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
297 { PCI_VENDOR_ID_LINKSYS, 0x1032,
298 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100299 { 0x0001, 0x8168,
300 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 {0,},
302};
303
304MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
305
Ard Biesheuvel27896c82016-05-14 22:40:15 +0200306static int use_dac = -1;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200307static struct {
308 u32 msg_enable;
309} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Francois Romieu07d3f512007-02-21 22:40:46 +0100311enum rtl_registers {
312 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100313 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100314 MAR0 = 8, /* Multicast filter. */
315 CounterAddrLow = 0x10,
316 CounterAddrHigh = 0x14,
317 TxDescStartAddrLow = 0x20,
318 TxDescStartAddrHigh = 0x24,
319 TxHDescStartAddrLow = 0x28,
320 TxHDescStartAddrHigh = 0x2c,
321 FLASH = 0x30,
322 ERSR = 0x36,
323 ChipCmd = 0x37,
324 TxPoll = 0x38,
325 IntrMask = 0x3c,
326 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700327
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800328 TxConfig = 0x40,
329#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
330#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
331
332 RxConfig = 0x44,
333#define RX128_INT_EN (1 << 15) /* 8111c and later */
334#define RX_MULTI_EN (1 << 14) /* 8111c only */
335#define RXCFG_FIFO_SHIFT 13
336 /* No threshold before first PCI xfer */
337#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000338#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800339#define RXCFG_DMA_SHIFT 8
340 /* Unlimited maximum PCI burst. */
341#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700342
Francois Romieu07d3f512007-02-21 22:40:46 +0100343 RxMissed = 0x4c,
344 Cfg9346 = 0x50,
345 Config0 = 0x51,
346 Config1 = 0x52,
347 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200348#define PME_SIGNAL (1 << 5) /* 8168c and later */
349
Francois Romieu07d3f512007-02-21 22:40:46 +0100350 Config3 = 0x54,
351 Config4 = 0x55,
352 Config5 = 0x56,
353 MultiIntr = 0x5c,
354 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100355 PHYstatus = 0x6c,
356 RxMaxSize = 0xda,
357 CPlusCmd = 0xe0,
358 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300359
360#define RTL_COALESCE_MASK 0x0f
361#define RTL_COALESCE_SHIFT 4
362#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
363#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
364
Francois Romieu07d3f512007-02-21 22:40:46 +0100365 RxDescAddrLow = 0xe4,
366 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000367 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
368
369#define NoEarlyTx 0x3f /* Max value : no early transmit. */
370
371 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
372
373#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800374#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000375
Francois Romieu07d3f512007-02-21 22:40:46 +0100376 FuncEvent = 0xf0,
377 FuncEventMask = 0xf4,
378 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800379 IBCR0 = 0xf8,
380 IBCR2 = 0xf9,
381 IBIMR0 = 0xfa,
382 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100383 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384};
385
Francois Romieuf162a5d2008-06-01 22:37:49 +0200386enum rtl8168_8101_registers {
387 CSIDR = 0x64,
388 CSIAR = 0x68,
389#define CSIAR_FLAG 0x80000000
390#define CSIAR_WRITE_CMD 0x80000000
Heiner Kallweitff1d7332018-05-02 21:39:56 +0200391#define CSIAR_BYTE_ENABLE 0x0000f000
392#define CSIAR_ADDR_MASK 0x00000fff
françois romieu065c27c2011-01-03 15:08:12 +0000393 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200394 EPHYAR = 0x80,
395#define EPHYAR_FLAG 0x80000000
396#define EPHYAR_WRITE_CMD 0x80000000
397#define EPHYAR_REG_MASK 0x1f
398#define EPHYAR_REG_SHIFT 16
399#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800400 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800401#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800402#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200403 DBG_REG = 0xd1,
404#define FIX_NAK_1 (1 << 4)
405#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800406 TWSI = 0xd2,
407 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800408#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800409#define TX_EMPTY (1 << 5)
410#define RX_EMPTY (1 << 4)
411#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800412#define EN_NDP (1 << 3)
413#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800414#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000415 EFUSEAR = 0xdc,
416#define EFUSEAR_FLAG 0x80000000
417#define EFUSEAR_WRITE_CMD 0x80000000
418#define EFUSEAR_READ_CMD 0x00000000
419#define EFUSEAR_REG_MASK 0x03ff
420#define EFUSEAR_REG_SHIFT 8
421#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800422 MISC_1 = 0xf2,
423#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200424};
425
françois romieuc0e45c12011-01-03 15:08:04 +0000426enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800427 LED_FREQ = 0x1a,
428 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000429 ERIDR = 0x70,
430 ERIAR = 0x74,
431#define ERIAR_FLAG 0x80000000
432#define ERIAR_WRITE_CMD 0x80000000
433#define ERIAR_READ_CMD 0x00000000
434#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000435#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800436#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
437#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
438#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800439#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800440#define ERIAR_MASK_SHIFT 12
441#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
442#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800443#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800444#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800445#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000446 EPHY_RXER_NUM = 0x7c,
447 OCPDR = 0xb0, /* OCP GPHY access */
448#define OCPDR_WRITE_CMD 0x80000000
449#define OCPDR_READ_CMD 0x00000000
450#define OCPDR_REG_MASK 0x7f
451#define OCPDR_GPHY_REG_SHIFT 16
452#define OCPDR_DATA_MASK 0xffff
453 OCPAR = 0xb4,
454#define OCPAR_FLAG 0x80000000
455#define OCPAR_GPHY_WRITE_CMD 0x8000f060
456#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800457 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000458 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
459 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200460#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800461#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800462#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800463#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800464#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000465};
466
Francois Romieu07d3f512007-02-21 22:40:46 +0100467enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100469 SYSErr = 0x8000,
470 PCSTimeout = 0x4000,
471 SWInt = 0x0100,
472 TxDescUnavail = 0x0080,
473 RxFIFOOver = 0x0040,
474 LinkChg = 0x0020,
475 RxOverflow = 0x0010,
476 TxErr = 0x0008,
477 TxOK = 0x0004,
478 RxErr = 0x0002,
479 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400482 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200483 RxFOVF = (1 << 23),
484 RxRWT = (1 << 22),
485 RxRES = (1 << 21),
486 RxRUNT = (1 << 20),
487 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800490 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100491 CmdReset = 0x10,
492 CmdRxEnb = 0x08,
493 CmdTxEnb = 0x04,
494 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Francois Romieu275391a2007-02-23 23:50:28 +0100496 /* TXPoll register p.5 */
497 HPQ = 0x80, /* Poll cmd on the high prio queue */
498 NPQ = 0x40, /* Poll cmd on the low prio queue */
499 FSWInt = 0x01, /* Forced software interrupt */
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100502 Cfg9346_Lock = 0x00,
503 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100506 AcceptErr = 0x20,
507 AcceptRunt = 0x10,
508 AcceptBroadcast = 0x08,
509 AcceptMulticast = 0x04,
510 AcceptMyPhys = 0x02,
511 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200512#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* TxConfigBits */
515 TxInterFrameGapShift = 24,
516 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
517
Francois Romieu5d06a992006-02-23 00:47:58 +0100518 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200519 LEDS1 = (1 << 7),
520 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200521 Speed_down = (1 << 4),
522 MEMMAP = (1 << 3),
523 IOMAP = (1 << 2),
524 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100525 PMEnable = (1 << 0), /* Power Management Enable */
526
Francois Romieu6dccd162007-02-13 23:38:05 +0100527 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000528 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000529 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100530 PCI_Clock_66MHz = 0x01,
531 PCI_Clock_33MHz = 0x00,
532
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100533 /* Config3 register p.25 */
534 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
535 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200536 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800537 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200538 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100539
Francois Romieud58d46b2011-05-03 16:38:29 +0200540 /* Config4 register */
541 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
542
Francois Romieu5d06a992006-02-23 00:47:58 +0100543 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100544 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
545 MWF = (1 << 5), /* Accept Multicast wakeup frame */
546 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200547 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100548 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100549 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000550 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200553 EnableBist = (1 << 15), // 8168 8101
554 Mac_dbgo_oe = (1 << 14), // 8168 8101
555 Normal_mode = (1 << 13), // unused
556 Force_half_dup = (1 << 12), // 8168 8101
557 Force_rxflow_en = (1 << 11), // 8168 8101
558 Force_txflow_en = (1 << 10), // 8168 8101
559 Cxpl_dbg_sel = (1 << 9), // 8168 8101
560 ASF = (1 << 8), // 8168 8101
561 PktCntrDisable = (1 << 7), // 8168 8101
562 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 RxVlan = (1 << 6),
564 RxChkSum = (1 << 5),
565 PCIDAC = (1 << 4),
566 PCIMulRW = (1 << 3),
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +0200567#define INTT_MASK GENMASK(1, 0)
Francois Romieu0e485152007-02-20 00:00:26 +0100568 INTT_0 = 0x0000, // 8168
569 INTT_1 = 0x0001, // 8168
570 INTT_2 = 0x0002, // 8168
571 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100574 TBI_Enable = 0x80,
575 TxFlowCtrl = 0x40,
576 RxFlowCtrl = 0x20,
577 _1000bpsF = 0x10,
578 _100bps = 0x08,
579 _10bps = 0x04,
580 LinkStatus = 0x02,
581 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100584 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200585
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200586 /* ResetCounterCommand */
587 CounterReset = 0x1,
588
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200589 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100590 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800591
592 /* magic enable v2 */
593 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594};
595
Francois Romieu2b7b4312011-04-18 22:53:24 -0700596enum rtl_desc_bit {
597 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
599 RingEnd = (1 << 30), /* End of descriptor ring */
600 FirstFrag = (1 << 29), /* First segment of a packet */
601 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700602};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Francois Romieu2b7b4312011-04-18 22:53:24 -0700604/* Generic case. */
605enum rtl_tx_desc_bit {
606 /* First doubleword. */
607 TD_LSO = (1 << 27), /* Large Send Offload */
608#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Francois Romieu2b7b4312011-04-18 22:53:24 -0700610 /* Second doubleword. */
611 TxVlanTag = (1 << 17), /* Add VLAN tag */
612};
613
614/* 8169, 8168b and 810x except 8102e. */
615enum rtl_tx_desc_bit_0 {
616 /* First doubleword. */
617#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
618 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
619 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
620 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
621};
622
623/* 8102e, 8168c and beyond. */
624enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800625 /* First doubleword. */
626 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800627 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800628#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800629#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800630
Francois Romieu2b7b4312011-04-18 22:53:24 -0700631 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800632#define TCPHO_SHIFT 18
633#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700634#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800635 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
636 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700637 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
638 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
639};
640
Francois Romieu2b7b4312011-04-18 22:53:24 -0700641enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /* Rx private */
643 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500644 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646#define RxProtoUDP (PID1)
647#define RxProtoTCP (PID0)
648#define RxProtoIP (PID1 | PID0)
649#define RxProtoMask RxProtoIP
650
651 IPFail = (1 << 16), /* IP checksum failed */
652 UDPFail = (1 << 15), /* UDP/IP checksum failed */
653 TCPFail = (1 << 14), /* TCP/IP checksum failed */
654 RxVlanTag = (1 << 16), /* VLAN tag available */
655};
656
657#define RsvdMask 0x3fffc000
Heiner Kallweit12d42c52018-04-28 22:19:30 +0200658#define CPCMD_QUIRK_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200661 __le32 opts1;
662 __le32 opts2;
663 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664};
665
666struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200667 __le32 opts1;
668 __le32 opts2;
669 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670};
671
672struct ring_info {
673 struct sk_buff *skb;
674 u32 len;
675 u8 __pad[sizeof(void *) - sizeof(u32)];
676};
677
Ivan Vecera355423d2009-02-06 21:49:57 -0800678struct rtl8169_counters {
679 __le64 tx_packets;
680 __le64 rx_packets;
681 __le64 tx_errors;
682 __le32 rx_errors;
683 __le16 rx_missed;
684 __le16 align_errors;
685 __le32 tx_one_collision;
686 __le32 tx_multi_collision;
687 __le64 rx_unicast;
688 __le64 rx_broadcast;
689 __le32 rx_multicast;
690 __le16 tx_aborted;
691 __le16 tx_underun;
692};
693
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200694struct rtl8169_tc_offsets {
695 bool inited;
696 __le64 tx_errors;
697 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200698 __le16 tx_aborted;
699};
700
Francois Romieuda78dbf2012-01-26 14:18:23 +0100701enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100702 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100703 RTL_FLAG_TASK_SLOW_PENDING,
704 RTL_FLAG_TASK_RESET_PENDING,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100705 RTL_FLAG_MAX
706};
707
Junchang Wang8027aa22012-03-04 23:30:32 +0100708struct rtl8169_stats {
709 u64 packets;
710 u64 bytes;
711 struct u64_stats_sync syncp;
712};
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714struct rtl8169_private {
715 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200716 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000717 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700718 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200719 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700720 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
722 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100724 struct rtl8169_stats rx_stats;
725 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
727 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
728 dma_addr_t TxPhyAddr;
729 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000730 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100733
734 u16 event_slow;
Francois Romieu50970832017-10-27 13:24:49 +0300735 const struct rtl_coalesce_info *coalesce_info;
françois romieuc0e45c12011-01-03 15:08:04 +0000736
737 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200738 void (*write)(struct rtl8169_private *, int, int);
739 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000740 } mdio_ops;
741
Francois Romieud58d46b2011-05-03 16:38:29 +0200742 struct jumbo_ops {
743 void (*enable)(struct rtl8169_private *);
744 void (*disable)(struct rtl8169_private *);
745 } jumbo_ops;
746
Heiner Kallweit61cb5322018-04-17 23:27:38 +0200747 void (*hw_start)(struct rtl8169_private *tp);
hayeswang5888d3f2014-07-11 16:25:56 +0800748 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100749
750 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100751 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
752 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100753 struct work_struct work;
754 } wk;
755
Francois Romieuccdffb92008-07-26 14:26:06 +0200756 struct mii_if_info mii;
Corinna Vinschen42020322015-09-10 10:47:35 +0200757 dma_addr_t counters_phys_addr;
758 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200759 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000760 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000761
Francois Romieub6ffd972011-06-17 17:00:05 +0200762 struct rtl_fw {
763 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200764
765#define RTL_VER_SIZE 32
766
767 char version[RTL_VER_SIZE];
768
769 struct rtl_fw_phy_action {
770 __le32 *code;
771 size_t size;
772 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200773 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300774#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800775
776 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777};
778
Ralf Baechle979b6c12005-06-13 14:30:40 -0700779MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700782MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200783module_param_named(debug, debug.msg_enable, int, 0);
784MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785MODULE_LICENSE("GPL");
786MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000787MODULE_FIRMWARE(FIRMWARE_8168D_1);
788MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000789MODULE_FIRMWARE(FIRMWARE_8168E_1);
790MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400791MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800792MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800793MODULE_FIRMWARE(FIRMWARE_8168F_1);
794MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800795MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800796MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800797MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800798MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000799MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000800MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000801MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800802MODULE_FIRMWARE(FIRMWARE_8168H_1);
803MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200804MODULE_FIRMWARE(FIRMWARE_8107E_1);
805MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Heiner Kallweit1e1205b2018-03-20 07:45:42 +0100807static inline struct device *tp_to_dev(struct rtl8169_private *tp)
808{
809 return &tp->pci_dev->dev;
810}
811
Francois Romieuda78dbf2012-01-26 14:18:23 +0100812static void rtl_lock_work(struct rtl8169_private *tp)
813{
814 mutex_lock(&tp->wk.mutex);
815}
816
817static void rtl_unlock_work(struct rtl8169_private *tp)
818{
819 mutex_unlock(&tp->wk.mutex);
820}
821
Heiner Kallweitcb732002018-03-20 07:45:35 +0100822static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
Francois Romieud58d46b2011-05-03 16:38:29 +0200823{
Heiner Kallweitcb732002018-03-20 07:45:35 +0100824 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +0800825 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200826}
827
Francois Romieuffc46952012-07-06 14:19:23 +0200828struct rtl_cond {
829 bool (*check)(struct rtl8169_private *);
830 const char *msg;
831};
832
833static void rtl_udelay(unsigned int d)
834{
835 udelay(d);
836}
837
838static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
839 void (*delay)(unsigned int), unsigned int d, int n,
840 bool high)
841{
842 int i;
843
844 for (i = 0; i < n; i++) {
845 delay(d);
846 if (c->check(tp) == high)
847 return true;
848 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200849 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
850 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200851 return false;
852}
853
854static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
855 const struct rtl_cond *c,
856 unsigned int d, int n)
857{
858 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
859}
860
861static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
862 const struct rtl_cond *c,
863 unsigned int d, int n)
864{
865 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
866}
867
868static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
869 const struct rtl_cond *c,
870 unsigned int d, int n)
871{
872 return rtl_loop_wait(tp, c, msleep, d, n, true);
873}
874
875static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
876 const struct rtl_cond *c,
877 unsigned int d, int n)
878{
879 return rtl_loop_wait(tp, c, msleep, d, n, false);
880}
881
882#define DECLARE_RTL_COND(name) \
883static bool name ## _check(struct rtl8169_private *); \
884 \
885static const struct rtl_cond name = { \
886 .check = name ## _check, \
887 .msg = #name \
888}; \
889 \
890static bool name ## _check(struct rtl8169_private *tp)
891
Hayes Wangc5583862012-07-02 17:23:22 +0800892static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
893{
894 if (reg & 0xffff0001) {
895 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
896 return true;
897 }
898 return false;
899}
900
901DECLARE_RTL_COND(rtl_ocp_gphy_cond)
902{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200903 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
Hayes Wangc5583862012-07-02 17:23:22 +0800904}
905
906static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
907{
Hayes Wangc5583862012-07-02 17:23:22 +0800908 if (rtl_ocp_reg_failure(tp, reg))
909 return;
910
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200911 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800912
913 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
914}
915
916static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
917{
Hayes Wangc5583862012-07-02 17:23:22 +0800918 if (rtl_ocp_reg_failure(tp, reg))
919 return 0;
920
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200921 RTL_W32(tp, GPHY_OCP, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800922
923 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200924 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
Hayes Wangc5583862012-07-02 17:23:22 +0800925}
926
Hayes Wangc5583862012-07-02 17:23:22 +0800927static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
928{
Hayes Wangc5583862012-07-02 17:23:22 +0800929 if (rtl_ocp_reg_failure(tp, reg))
930 return;
931
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200932 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800933}
934
935static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
936{
Hayes Wangc5583862012-07-02 17:23:22 +0800937 if (rtl_ocp_reg_failure(tp, reg))
938 return 0;
939
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200940 RTL_W32(tp, OCPDR, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800941
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200942 return RTL_R32(tp, OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +0800943}
944
945#define OCP_STD_PHY_BASE 0xa400
946
947static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
948{
949 if (reg == 0x1f) {
950 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
951 return;
952 }
953
954 if (tp->ocp_base != OCP_STD_PHY_BASE)
955 reg -= 0x10;
956
957 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
958}
959
960static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
961{
962 if (tp->ocp_base != OCP_STD_PHY_BASE)
963 reg -= 0x10;
964
965 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
966}
967
hayeswangeee37862013-04-01 22:23:38 +0000968static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
969{
970 if (reg == 0x1f) {
971 tp->ocp_base = value << 4;
972 return;
973 }
974
975 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
976}
977
978static int mac_mcu_read(struct rtl8169_private *tp, int reg)
979{
980 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
981}
982
Francois Romieuffc46952012-07-06 14:19:23 +0200983DECLARE_RTL_COND(rtl_phyar_cond)
984{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200985 return RTL_R32(tp, PHYAR) & 0x80000000;
Francois Romieuffc46952012-07-06 14:19:23 +0200986}
987
Francois Romieu24192212012-07-06 20:19:42 +0200988static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200990 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Francois Romieuffc46952012-07-06 14:19:23 +0200992 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -0700993 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700994 * According to hardware specs a 20us delay is required after write
995 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700996 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700997 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
Francois Romieu24192212012-07-06 20:19:42 +02001000static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Francois Romieuffc46952012-07-06 14:19:23 +02001002 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001004 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Francois Romieuffc46952012-07-06 14:19:23 +02001006 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001007 RTL_R32(tp, PHYAR) & 0xffff : ~0;
Francois Romieuffc46952012-07-06 14:19:23 +02001008
Timo Teräs81a95f02010-06-09 17:31:48 -07001009 /*
1010 * According to hardware specs a 20us delay is required after read
1011 * complete indication, but before sending next command.
1012 */
1013 udelay(20);
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return value;
1016}
1017
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001018DECLARE_RTL_COND(rtl_ocpar_cond)
1019{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001020 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001021}
1022
Francois Romieu24192212012-07-06 20:19:42 +02001023static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001024{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001025 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1026 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
1027 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001028
Francois Romieuffc46952012-07-06 14:19:23 +02001029 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001030}
1031
Francois Romieu24192212012-07-06 20:19:42 +02001032static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001033{
Francois Romieu24192212012-07-06 20:19:42 +02001034 r8168dp_1_mdio_access(tp, reg,
1035 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001036}
1037
Francois Romieu24192212012-07-06 20:19:42 +02001038static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001039{
Francois Romieu24192212012-07-06 20:19:42 +02001040 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001041
1042 mdelay(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001043 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
1044 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001045
Francois Romieuffc46952012-07-06 14:19:23 +02001046 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001047 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001048}
1049
françois romieue6de30d2011-01-03 15:08:37 +00001050#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1051
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001052static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001053{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001054 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001055}
1056
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001057static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001058{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001059 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001060}
1061
Francois Romieu24192212012-07-06 20:19:42 +02001062static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001063{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001064 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001065
Francois Romieu24192212012-07-06 20:19:42 +02001066 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001067
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001068 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001069}
1070
Francois Romieu24192212012-07-06 20:19:42 +02001071static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001072{
1073 int value;
1074
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001075 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001076
Francois Romieu24192212012-07-06 20:19:42 +02001077 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001078
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001079 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001080
1081 return value;
1082}
1083
françois romieu4da19632011-01-03 15:07:55 +00001084static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001085{
Francois Romieu24192212012-07-06 20:19:42 +02001086 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001087}
1088
françois romieu4da19632011-01-03 15:07:55 +00001089static int rtl_readphy(struct rtl8169_private *tp, int location)
1090{
Francois Romieu24192212012-07-06 20:19:42 +02001091 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001092}
1093
1094static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1095{
1096 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1097}
1098
Chun-Hao Lin76564422014-10-01 23:17:17 +08001099static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001100{
1101 int val;
1102
françois romieu4da19632011-01-03 15:07:55 +00001103 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001104 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001105}
1106
Francois Romieuccdffb92008-07-26 14:26:06 +02001107static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1108 int val)
1109{
1110 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001111
françois romieu4da19632011-01-03 15:07:55 +00001112 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001113}
1114
1115static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1116{
1117 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001118
françois romieu4da19632011-01-03 15:07:55 +00001119 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001120}
1121
Francois Romieuffc46952012-07-06 14:19:23 +02001122DECLARE_RTL_COND(rtl_ephyar_cond)
1123{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001124 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001125}
1126
Francois Romieufdf6fc02012-07-06 22:40:38 +02001127static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001128{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001129 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
Francois Romieudacf8152008-08-02 20:44:13 +02001130 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1131
Francois Romieuffc46952012-07-06 14:19:23 +02001132 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1133
1134 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001135}
1136
Francois Romieufdf6fc02012-07-06 22:40:38 +02001137static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001138{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001139 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
Francois Romieudacf8152008-08-02 20:44:13 +02001140
Francois Romieuffc46952012-07-06 14:19:23 +02001141 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001142 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001143}
1144
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001145DECLARE_RTL_COND(rtl_eriar_cond)
1146{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001147 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001148}
1149
Francois Romieufdf6fc02012-07-06 22:40:38 +02001150static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1151 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001152{
Hayes Wang133ac402011-07-06 15:58:05 +08001153 BUG_ON((addr & 3) || (mask == 0));
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001154 RTL_W32(tp, ERIDR, val);
1155 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001156
Francois Romieuffc46952012-07-06 14:19:23 +02001157 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001158}
1159
Francois Romieufdf6fc02012-07-06 22:40:38 +02001160static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001161{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001162 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001163
Francois Romieuffc46952012-07-06 14:19:23 +02001164 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001165 RTL_R32(tp, ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001166}
1167
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001168static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001169 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001170{
1171 u32 val;
1172
Francois Romieufdf6fc02012-07-06 22:40:38 +02001173 val = rtl_eri_read(tp, addr, type);
1174 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001175}
1176
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001177static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1178{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001179 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001180 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001181 RTL_R32(tp, OCPDR) : ~0;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001182}
1183
1184static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1185{
1186 return rtl_eri_read(tp, reg, ERIAR_OOB);
1187}
1188
1189static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1190{
1191 switch (tp->mac_version) {
1192 case RTL_GIGA_MAC_VER_27:
1193 case RTL_GIGA_MAC_VER_28:
1194 case RTL_GIGA_MAC_VER_31:
1195 return r8168dp_ocp_read(tp, mask, reg);
1196 case RTL_GIGA_MAC_VER_49:
1197 case RTL_GIGA_MAC_VER_50:
1198 case RTL_GIGA_MAC_VER_51:
1199 return r8168ep_ocp_read(tp, mask, reg);
1200 default:
1201 BUG();
1202 return ~0;
1203 }
1204}
1205
1206static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1207 u32 data)
1208{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001209 RTL_W32(tp, OCPDR, data);
1210 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001211 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1212}
1213
1214static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1215 u32 data)
1216{
1217 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1218 data, ERIAR_OOB);
1219}
1220
1221static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1222{
1223 switch (tp->mac_version) {
1224 case RTL_GIGA_MAC_VER_27:
1225 case RTL_GIGA_MAC_VER_28:
1226 case RTL_GIGA_MAC_VER_31:
1227 r8168dp_ocp_write(tp, mask, reg, data);
1228 break;
1229 case RTL_GIGA_MAC_VER_49:
1230 case RTL_GIGA_MAC_VER_50:
1231 case RTL_GIGA_MAC_VER_51:
1232 r8168ep_ocp_write(tp, mask, reg, data);
1233 break;
1234 default:
1235 BUG();
1236 break;
1237 }
1238}
1239
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001240static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1241{
1242 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1243
1244 ocp_write(tp, 0x1, 0x30, 0x00000001);
1245}
1246
1247#define OOB_CMD_RESET 0x00
1248#define OOB_CMD_DRIVER_START 0x05
1249#define OOB_CMD_DRIVER_STOP 0x06
1250
1251static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1252{
1253 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1254}
1255
1256DECLARE_RTL_COND(rtl_ocp_read_cond)
1257{
1258 u16 reg;
1259
1260 reg = rtl8168_get_ocp_reg(tp);
1261
1262 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1263}
1264
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001265DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1266{
1267 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1268}
1269
1270DECLARE_RTL_COND(rtl_ocp_tx_cond)
1271{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001272 return RTL_R8(tp, IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001273}
1274
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001275static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1276{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001277 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001278 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001279 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1280 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001281}
1282
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001283static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001284{
1285 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001286 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1287}
1288
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001289static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1290{
1291 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1292 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1293 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1294}
1295
1296static void rtl8168_driver_start(struct rtl8169_private *tp)
1297{
1298 switch (tp->mac_version) {
1299 case RTL_GIGA_MAC_VER_27:
1300 case RTL_GIGA_MAC_VER_28:
1301 case RTL_GIGA_MAC_VER_31:
1302 rtl8168dp_driver_start(tp);
1303 break;
1304 case RTL_GIGA_MAC_VER_49:
1305 case RTL_GIGA_MAC_VER_50:
1306 case RTL_GIGA_MAC_VER_51:
1307 rtl8168ep_driver_start(tp);
1308 break;
1309 default:
1310 BUG();
1311 break;
1312 }
1313}
1314
1315static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1316{
1317 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1318 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1319}
1320
1321static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1322{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001323 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001324 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1325 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1326 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1327}
1328
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001329static void rtl8168_driver_stop(struct rtl8169_private *tp)
1330{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001331 switch (tp->mac_version) {
1332 case RTL_GIGA_MAC_VER_27:
1333 case RTL_GIGA_MAC_VER_28:
1334 case RTL_GIGA_MAC_VER_31:
1335 rtl8168dp_driver_stop(tp);
1336 break;
1337 case RTL_GIGA_MAC_VER_49:
1338 case RTL_GIGA_MAC_VER_50:
1339 case RTL_GIGA_MAC_VER_51:
1340 rtl8168ep_driver_stop(tp);
1341 break;
1342 default:
1343 BUG();
1344 break;
1345 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001346}
1347
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001348static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001349{
1350 u16 reg = rtl8168_get_ocp_reg(tp);
1351
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001352 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001353}
1354
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001355static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001356{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001357 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001358}
1359
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001360static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001361{
1362 switch (tp->mac_version) {
1363 case RTL_GIGA_MAC_VER_27:
1364 case RTL_GIGA_MAC_VER_28:
1365 case RTL_GIGA_MAC_VER_31:
1366 return r8168dp_check_dash(tp);
1367 case RTL_GIGA_MAC_VER_49:
1368 case RTL_GIGA_MAC_VER_50:
1369 case RTL_GIGA_MAC_VER_51:
1370 return r8168ep_check_dash(tp);
1371 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001372 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001373 }
1374}
1375
françois romieuc28aa382011-08-02 03:53:43 +00001376struct exgmac_reg {
1377 u16 addr;
1378 u16 mask;
1379 u32 val;
1380};
1381
Francois Romieufdf6fc02012-07-06 22:40:38 +02001382static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001383 const struct exgmac_reg *r, int len)
1384{
1385 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001386 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001387 r++;
1388 }
1389}
1390
Francois Romieuffc46952012-07-06 14:19:23 +02001391DECLARE_RTL_COND(rtl_efusear_cond)
1392{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001393 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001394}
1395
Francois Romieufdf6fc02012-07-06 22:40:38 +02001396static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001397{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001398 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
françois romieudaf9df62009-10-07 12:44:20 +00001399
Francois Romieuffc46952012-07-06 14:19:23 +02001400 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001401 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001402}
1403
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001404static u16 rtl_get_events(struct rtl8169_private *tp)
1405{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001406 return RTL_R16(tp, IntrStatus);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001407}
1408
1409static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1410{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001411 RTL_W16(tp, IntrStatus, bits);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001412 mmiowb();
1413}
1414
1415static void rtl_irq_disable(struct rtl8169_private *tp)
1416{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001417 RTL_W16(tp, IntrMask, 0);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001418 mmiowb();
1419}
1420
Francois Romieu3e990ff2012-01-26 12:50:01 +01001421static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1422{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001423 RTL_W16(tp, IntrMask, bits);
Francois Romieu3e990ff2012-01-26 12:50:01 +01001424}
1425
Francois Romieuda78dbf2012-01-26 14:18:23 +01001426#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1427#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1428#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1429
1430static void rtl_irq_enable_all(struct rtl8169_private *tp)
1431{
1432 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1433}
1434
françois romieu811fd302011-12-04 20:30:45 +00001435static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001437 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001438 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001439 RTL_R8(tp, ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
françois romieu4da19632011-01-03 15:07:55 +00001442static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
françois romieu4da19632011-01-03 15:07:55 +00001444 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001447static unsigned int rtl8169_xmii_link_ok(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001449 return RTL_R8(tp, PHYstatus) & LinkStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
françois romieu4da19632011-01-03 15:07:55 +00001452static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
1454 unsigned int val;
1455
françois romieu4da19632011-01-03 15:07:55 +00001456 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1457 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
Hayes Wang70090422011-07-06 15:58:06 +08001460static void rtl_link_chg_patch(struct rtl8169_private *tp)
1461{
Hayes Wang70090422011-07-06 15:58:06 +08001462 struct net_device *dev = tp->dev;
1463
1464 if (!netif_running(dev))
1465 return;
1466
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001467 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1468 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001469 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001470 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1471 ERIAR_EXGMAC);
1472 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1473 ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001474 } else if (RTL_R8(tp, PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001475 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1476 ERIAR_EXGMAC);
1477 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1478 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001479 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001480 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1481 ERIAR_EXGMAC);
1482 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1483 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001484 }
1485 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001486 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001487 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001488 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001489 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001490 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1491 tp->mac_version == RTL_GIGA_MAC_VER_36) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001492 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001493 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1494 ERIAR_EXGMAC);
1495 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1496 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001497 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001498 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1499 ERIAR_EXGMAC);
1500 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1501 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001502 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001503 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001504 if (RTL_R8(tp, PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001505 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1506 ERIAR_EXGMAC);
1507 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1508 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001509 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001510 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1511 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001512 }
Hayes Wang70090422011-07-06 15:58:06 +08001513 }
1514}
1515
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01001516static void rtl8169_check_link_status(struct net_device *dev,
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001517 struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001519 struct device *d = tp_to_dev(tp);
1520
Heiner Kallweite3972862018-06-29 08:07:04 +02001521 if (rtl8169_xmii_link_ok(tp)) {
Hayes Wang70090422011-07-06 15:58:06 +08001522 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001523 /* This is to cancel a scheduled suspend if there's one. */
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001524 pm_request_resume(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001526 if (net_ratelimit())
1527 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001528 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001530 netif_info(tp, ifdown, dev, "link down\n");
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001531 pm_runtime_idle(d);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001535#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1536
1537static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1538{
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001539 u8 options;
1540 u32 wolopts = 0;
1541
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001542 options = RTL_R8(tp, Config1);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001543 if (!(options & PMEnable))
1544 return 0;
1545
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001546 options = RTL_R8(tp, Config3);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001547 if (options & LinkUp)
1548 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001549 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001550 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1551 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001552 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1553 wolopts |= WAKE_MAGIC;
1554 break;
1555 default:
1556 if (options & MagicPacket)
1557 wolopts |= WAKE_MAGIC;
1558 break;
1559 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001560
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001561 options = RTL_R8(tp, Config5);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001562 if (options & UWF)
1563 wolopts |= WAKE_UCAST;
1564 if (options & BWF)
1565 wolopts |= WAKE_BCAST;
1566 if (options & MWF)
1567 wolopts |= WAKE_MCAST;
1568
1569 return wolopts;
1570}
1571
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001572static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1573{
1574 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001575
Francois Romieuda78dbf2012-01-26 14:18:23 +01001576 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001577 wol->supported = WAKE_ANY;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001578 wol->wolopts = tp->saved_wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001579 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001580}
1581
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001582static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001583{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001584 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001585 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001586 u32 opt;
1587 u16 reg;
1588 u8 mask;
1589 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001590 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001591 { WAKE_UCAST, Config5, UWF },
1592 { WAKE_BCAST, Config5, BWF },
1593 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001594 { WAKE_ANY, Config5, LanWake },
1595 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001596 };
Francois Romieu851e6022012-04-17 11:10:11 +02001597 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001598
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001599 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001600
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001601 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001602 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1603 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001604 tmp = ARRAY_SIZE(cfg) - 1;
1605 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001606 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001607 0x0dc,
1608 ERIAR_MASK_0100,
1609 MagicPacket_v2,
1610 0x0000,
1611 ERIAR_EXGMAC);
1612 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001613 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001614 0x0dc,
1615 ERIAR_MASK_0100,
1616 0x0000,
1617 MagicPacket_v2,
1618 ERIAR_EXGMAC);
1619 break;
1620 default:
1621 tmp = ARRAY_SIZE(cfg);
1622 break;
1623 }
1624
1625 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001626 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001627 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001628 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001629 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001630 }
1631
Francois Romieu851e6022012-04-17 11:10:11 +02001632 switch (tp->mac_version) {
1633 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001634 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001635 if (wolopts)
1636 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001637 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001638 break;
1639 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001640 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001641 if (wolopts)
1642 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001643 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001644 break;
1645 }
1646
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001647 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001648}
1649
1650static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1651{
1652 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001653 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001654
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001655 if (wol->wolopts & ~WAKE_ANY)
1656 return -EINVAL;
1657
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001658 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001659
Francois Romieuda78dbf2012-01-26 14:18:23 +01001660 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001661
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001662 tp->saved_wolopts = wol->wolopts;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001663
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001664 if (pm_runtime_active(d))
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001665 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001666
1667 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001668
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001669 device_set_wakeup_enable(d, tp->saved_wolopts);
françois romieuea809072010-11-08 13:23:58 +00001670
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001671 pm_runtime_put_noidle(d);
1672
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001673 return 0;
1674}
1675
Francois Romieu31bd2042011-04-26 18:58:59 +02001676static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1677{
Francois Romieu85bffe62011-04-27 08:22:39 +02001678 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001679}
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681static void rtl8169_get_drvinfo(struct net_device *dev,
1682 struct ethtool_drvinfo *info)
1683{
1684 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001685 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Rick Jones68aad782011-11-07 13:29:27 +00001687 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1688 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1689 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001690 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001691 if (!IS_ERR_OR_NULL(rtl_fw))
1692 strlcpy(info->fw_version, rtl_fw->version,
1693 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
1696static int rtl8169_get_regs_len(struct net_device *dev)
1697{
1698 return R8169_REGS_SIZE;
1699}
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001702 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
1704 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001705 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001706 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Hayes Wang716b50a2011-02-22 17:26:18 +08001708 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001711 int auto_nego;
1712
françois romieu4da19632011-01-03 15:07:55 +00001713 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001714 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1715 ADVERTISE_100HALF | ADVERTISE_100FULL);
1716
1717 if (adv & ADVERTISED_10baseT_Half)
1718 auto_nego |= ADVERTISE_10HALF;
1719 if (adv & ADVERTISED_10baseT_Full)
1720 auto_nego |= ADVERTISE_10FULL;
1721 if (adv & ADVERTISED_100baseT_Half)
1722 auto_nego |= ADVERTISE_100HALF;
1723 if (adv & ADVERTISED_100baseT_Full)
1724 auto_nego |= ADVERTISE_100FULL;
1725
françois romieu3577aa12009-05-19 10:46:48 +00001726 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1727
françois romieu4da19632011-01-03 15:07:55 +00001728 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001729 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1730
1731 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001732 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001733 if (adv & ADVERTISED_1000baseT_Half)
1734 giga_ctrl |= ADVERTISE_1000HALF;
1735 if (adv & ADVERTISED_1000baseT_Full)
1736 giga_ctrl |= ADVERTISE_1000FULL;
1737 } else if (adv & (ADVERTISED_1000baseT_Half |
1738 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001739 netif_info(tp, link, dev,
1740 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001741 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
françois romieu3577aa12009-05-19 10:46:48 +00001744 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001745
françois romieu4da19632011-01-03 15:07:55 +00001746 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1747 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001748 } else {
françois romieu3577aa12009-05-19 10:46:48 +00001749 if (speed == SPEED_10)
1750 bmcr = 0;
1751 else if (speed == SPEED_100)
1752 bmcr = BMCR_SPEED100;
1753 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001754 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001755
1756 if (duplex == DUPLEX_FULL)
1757 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001758 }
1759
françois romieu4da19632011-01-03 15:07:55 +00001760 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001761
Francois Romieucecb5fd2011-04-01 10:21:07 +02001762 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1763 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001764 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001765 rtl_writephy(tp, 0x17, 0x2138);
1766 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001767 } else {
françois romieu4da19632011-01-03 15:07:55 +00001768 rtl_writephy(tp, 0x17, 0x2108);
1769 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001770 }
1771 }
1772
Oliver Neukum54405cd2011-01-06 21:55:13 +01001773 rc = 0;
1774out:
1775 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
1778static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001779 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
Heiner Kallweit335c9972018-07-01 00:25:19 +02001781 return rtl8169_set_speed_xmii(dev, autoneg, speed, duplex, advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001784static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1785 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Francois Romieud58d46b2011-05-03 16:38:29 +02001787 struct rtl8169_private *tp = netdev_priv(dev);
1788
Francois Romieu2b7b4312011-04-18 22:53:24 -07001789 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001790 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Francois Romieud58d46b2011-05-03 16:38:29 +02001792 if (dev->mtu > JUMBO_1K &&
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02001793 tp->mac_version > RTL_GIGA_MAC_VER_06)
Francois Romieud58d46b2011-05-03 16:38:29 +02001794 features &= ~NETIF_F_IP_CSUM;
1795
Michał Mirosław350fb322011-04-08 06:35:56 +00001796 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
Heiner Kallweita3984572018-04-28 22:19:15 +02001799static int rtl8169_set_features(struct net_device *dev,
1800 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
1802 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001803 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Heiner Kallweita3984572018-04-28 22:19:15 +02001805 rtl_lock_work(tp);
1806
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001807 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001808 if (features & NETIF_F_RXALL)
1809 rx_config |= (AcceptErr | AcceptRunt);
1810 else
1811 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001813 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001814
hayeswang929a0312014-09-16 11:40:47 +08001815 if (features & NETIF_F_RXCSUM)
1816 tp->cp_cmd |= RxChkSum;
1817 else
1818 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001819
hayeswang929a0312014-09-16 11:40:47 +08001820 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1821 tp->cp_cmd |= RxVlan;
1822 else
1823 tp->cp_cmd &= ~RxVlan;
1824
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001825 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1826 RTL_R16(tp, CPlusCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Francois Romieuda78dbf2012-01-26 14:18:23 +01001828 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 return 0;
1831}
1832
Kirill Smelkov810f4892012-11-10 21:11:02 +04001833static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001835 return (skb_vlan_tag_present(skb)) ?
1836 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
1838
Francois Romieu7a8fc772011-03-01 17:18:33 +01001839static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
1841 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Francois Romieu7a8fc772011-03-01 17:18:33 +01001843 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001844 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
Heiner Kallweite3972862018-06-29 08:07:04 +02001847static int rtl8169_get_link_ksettings(struct net_device *dev,
1848 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
1850 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03001852 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
1853
1854 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01001857static int rtl8169_set_link_ksettings(struct net_device *dev,
1858 const struct ethtool_link_ksettings *cmd)
1859{
1860 struct rtl8169_private *tp = netdev_priv(dev);
1861 int rc;
1862 u32 advertising;
1863
1864 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
1865 cmd->link_modes.advertising))
1866 return -EINVAL;
1867
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01001868 rtl_lock_work(tp);
1869 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
1870 cmd->base.duplex, advertising);
1871 rtl_unlock_work(tp);
1872
1873 return rc;
1874}
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1877 void *p)
1878{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001879 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02001880 u32 __iomem *data = tp->mmio_addr;
1881 u32 *dw = p;
1882 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Francois Romieuda78dbf2012-01-26 14:18:23 +01001884 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02001885 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1886 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001887 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
1889
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001890static u32 rtl8169_get_msglevel(struct net_device *dev)
1891{
1892 struct rtl8169_private *tp = netdev_priv(dev);
1893
1894 return tp->msg_enable;
1895}
1896
1897static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1898{
1899 struct rtl8169_private *tp = netdev_priv(dev);
1900
1901 tp->msg_enable = value;
1902}
1903
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001904static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1905 "tx_packets",
1906 "rx_packets",
1907 "tx_errors",
1908 "rx_errors",
1909 "rx_missed",
1910 "align_errors",
1911 "tx_single_collisions",
1912 "tx_multi_collisions",
1913 "unicast",
1914 "broadcast",
1915 "multicast",
1916 "tx_aborted",
1917 "tx_underrun",
1918};
1919
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001920static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001921{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001922 switch (sset) {
1923 case ETH_SS_STATS:
1924 return ARRAY_SIZE(rtl8169_gstrings);
1925 default:
1926 return -EOPNOTSUPP;
1927 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001928}
1929
Corinna Vinschen42020322015-09-10 10:47:35 +02001930DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001931{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001932 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001933}
1934
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001935static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001936{
Corinna Vinschen42020322015-09-10 10:47:35 +02001937 dma_addr_t paddr = tp->counters_phys_addr;
1938 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02001939
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001940 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
1941 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02001942 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001943 RTL_W32(tp, CounterAddrLow, cmd);
1944 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02001945
Francois Romieua78e9362018-01-26 01:53:26 +01001946 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001947}
1948
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001949static bool rtl8169_reset_counters(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001950{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001951 /*
1952 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
1953 * tally counters.
1954 */
1955 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
1956 return true;
1957
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001958 return rtl8169_do_counters(tp, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02001959}
1960
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001961static bool rtl8169_update_counters(struct rtl8169_private *tp)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001962{
Ivan Vecera355423d2009-02-06 21:49:57 -08001963 /*
1964 * Some chips are unable to dump tally counters when the receiver
1965 * is disabled.
1966 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001967 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001968 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001969
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001970 return rtl8169_do_counters(tp, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001971}
1972
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001973static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001974{
Corinna Vinschen42020322015-09-10 10:47:35 +02001975 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001976 bool ret = false;
1977
1978 /*
1979 * rtl8169_init_counter_offsets is called from rtl_open. On chip
1980 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
1981 * reset by a power cycle, while the counter values collected by the
1982 * driver are reset at every driver unload/load cycle.
1983 *
1984 * To make sure the HW values returned by @get_stats64 match the SW
1985 * values, we collect the initial values at first open(*) and use them
1986 * as offsets to normalize the values returned by @get_stats64.
1987 *
1988 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
1989 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
1990 * set at open time by rtl_hw_start.
1991 */
1992
1993 if (tp->tc_offset.inited)
1994 return true;
1995
1996 /* If both, reset and update fail, propagate to caller. */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001997 if (rtl8169_reset_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001998 ret = true;
1999
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002000 if (rtl8169_update_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002001 ret = true;
2002
Corinna Vinschen42020322015-09-10 10:47:35 +02002003 tp->tc_offset.tx_errors = counters->tx_errors;
2004 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2005 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002006 tp->tc_offset.inited = true;
2007
2008 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002009}
2010
Ivan Vecera355423d2009-02-06 21:49:57 -08002011static void rtl8169_get_ethtool_stats(struct net_device *dev,
2012 struct ethtool_stats *stats, u64 *data)
2013{
2014 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01002015 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02002016 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08002017
2018 ASSERT_RTNL();
2019
Chun-Hao Line0636232016-07-29 16:37:55 +08002020 pm_runtime_get_noresume(d);
2021
2022 if (pm_runtime_active(d))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002023 rtl8169_update_counters(tp);
Chun-Hao Line0636232016-07-29 16:37:55 +08002024
2025 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08002026
Corinna Vinschen42020322015-09-10 10:47:35 +02002027 data[0] = le64_to_cpu(counters->tx_packets);
2028 data[1] = le64_to_cpu(counters->rx_packets);
2029 data[2] = le64_to_cpu(counters->tx_errors);
2030 data[3] = le32_to_cpu(counters->rx_errors);
2031 data[4] = le16_to_cpu(counters->rx_missed);
2032 data[5] = le16_to_cpu(counters->align_errors);
2033 data[6] = le32_to_cpu(counters->tx_one_collision);
2034 data[7] = le32_to_cpu(counters->tx_multi_collision);
2035 data[8] = le64_to_cpu(counters->rx_unicast);
2036 data[9] = le64_to_cpu(counters->rx_broadcast);
2037 data[10] = le32_to_cpu(counters->rx_multicast);
2038 data[11] = le16_to_cpu(counters->tx_aborted);
2039 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08002040}
2041
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002042static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2043{
2044 switch(stringset) {
2045 case ETH_SS_STATS:
2046 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2047 break;
2048 }
2049}
2050
Florian Fainellif0903ea2016-12-03 12:01:19 -08002051static int rtl8169_nway_reset(struct net_device *dev)
2052{
2053 struct rtl8169_private *tp = netdev_priv(dev);
2054
2055 return mii_nway_restart(&tp->mii);
2056}
2057
Francois Romieu50970832017-10-27 13:24:49 +03002058/*
2059 * Interrupt coalescing
2060 *
2061 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2062 * > 8169, 8168 and 810x line of chipsets
2063 *
2064 * 8169, 8168, and 8136(810x) serial chipsets support it.
2065 *
2066 * > 2 - the Tx timer unit at gigabit speed
2067 *
2068 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2069 * (0xe0) bit 1 and bit 0.
2070 *
2071 * For 8169
2072 * bit[1:0] \ speed 1000M 100M 10M
2073 * 0 0 320ns 2.56us 40.96us
2074 * 0 1 2.56us 20.48us 327.7us
2075 * 1 0 5.12us 40.96us 655.4us
2076 * 1 1 10.24us 81.92us 1.31ms
2077 *
2078 * For the other
2079 * bit[1:0] \ speed 1000M 100M 10M
2080 * 0 0 5us 2.56us 40.96us
2081 * 0 1 40us 20.48us 327.7us
2082 * 1 0 80us 40.96us 655.4us
2083 * 1 1 160us 81.92us 1.31ms
2084 */
2085
2086/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2087struct rtl_coalesce_scale {
2088 /* Rx / Tx */
2089 u32 nsecs[2];
2090};
2091
2092/* rx/tx scale factors for all CPlusCmd[0:1] cases */
2093struct rtl_coalesce_info {
2094 u32 speed;
2095 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2096};
2097
2098/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2099#define rxtx_x1822(r, t) { \
2100 {{(r), (t)}}, \
2101 {{(r)*8, (t)*8}}, \
2102 {{(r)*8*2, (t)*8*2}}, \
2103 {{(r)*8*2*2, (t)*8*2*2}}, \
2104}
2105static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2106 /* speed delays: rx00 tx00 */
2107 { SPEED_10, rxtx_x1822(40960, 40960) },
2108 { SPEED_100, rxtx_x1822( 2560, 2560) },
2109 { SPEED_1000, rxtx_x1822( 320, 320) },
2110 { 0 },
2111};
2112
2113static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2114 /* speed delays: rx00 tx00 */
2115 { SPEED_10, rxtx_x1822(40960, 40960) },
2116 { SPEED_100, rxtx_x1822( 2560, 2560) },
2117 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2118 { 0 },
2119};
2120#undef rxtx_x1822
2121
2122/* get rx/tx scale vector corresponding to current speed */
2123static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2124{
2125 struct rtl8169_private *tp = netdev_priv(dev);
2126 struct ethtool_link_ksettings ecmd;
2127 const struct rtl_coalesce_info *ci;
2128 int rc;
2129
2130 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2131 if (rc < 0)
2132 return ERR_PTR(rc);
2133
2134 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2135 if (ecmd.base.speed == ci->speed) {
2136 return ci;
2137 }
2138 }
2139
2140 return ERR_PTR(-ELNRNG);
2141}
2142
2143static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2144{
2145 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002146 const struct rtl_coalesce_info *ci;
2147 const struct rtl_coalesce_scale *scale;
2148 struct {
2149 u32 *max_frames;
2150 u32 *usecs;
2151 } coal_settings [] = {
2152 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2153 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2154 }, *p = coal_settings;
2155 int i;
2156 u16 w;
2157
2158 memset(ec, 0, sizeof(*ec));
2159
2160 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2161 ci = rtl_coalesce_info(dev);
2162 if (IS_ERR(ci))
2163 return PTR_ERR(ci);
2164
Heiner Kallweit0ae09742018-04-28 22:19:26 +02002165 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
Francois Romieu50970832017-10-27 13:24:49 +03002166
2167 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002168 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03002169 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2170 w >>= RTL_COALESCE_SHIFT;
2171 *p->usecs = w & RTL_COALESCE_MASK;
2172 }
2173
2174 for (i = 0; i < 2; i++) {
2175 p = coal_settings + i;
2176 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2177
2178 /*
2179 * ethtool_coalesce says it is illegal to set both usecs and
2180 * max_frames to 0.
2181 */
2182 if (!*p->usecs && !*p->max_frames)
2183 *p->max_frames = 1;
2184 }
2185
2186 return 0;
2187}
2188
2189/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2190static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2191 struct net_device *dev, u32 nsec, u16 *cp01)
2192{
2193 const struct rtl_coalesce_info *ci;
2194 u16 i;
2195
2196 ci = rtl_coalesce_info(dev);
2197 if (IS_ERR(ci))
2198 return ERR_CAST(ci);
2199
2200 for (i = 0; i < 4; i++) {
2201 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2202 ci->scalev[i].nsecs[1]);
2203 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2204 *cp01 = i;
2205 return &ci->scalev[i];
2206 }
2207 }
2208
2209 return ERR_PTR(-EINVAL);
2210}
2211
2212static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2213{
2214 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002215 const struct rtl_coalesce_scale *scale;
2216 struct {
2217 u32 frames;
2218 u32 usecs;
2219 } coal_settings [] = {
2220 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2221 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2222 }, *p = coal_settings;
2223 u16 w = 0, cp01;
2224 int i;
2225
2226 scale = rtl_coalesce_choose_scale(dev,
2227 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2228 if (IS_ERR(scale))
2229 return PTR_ERR(scale);
2230
2231 for (i = 0; i < 2; i++, p++) {
2232 u32 units;
2233
2234 /*
2235 * accept max_frames=1 we returned in rtl_get_coalesce.
2236 * accept it not only when usecs=0 because of e.g. the following scenario:
2237 *
2238 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2239 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2240 * - then user does `ethtool -C eth0 rx-usecs 100`
2241 *
2242 * since ethtool sends to kernel whole ethtool_coalesce
2243 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2244 * we'll reject it below in `frames % 4 != 0`.
2245 */
2246 if (p->frames == 1) {
2247 p->frames = 0;
2248 }
2249
2250 units = p->usecs * 1000 / scale->nsecs[i];
2251 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2252 return -EINVAL;
2253
2254 w <<= RTL_COALESCE_SHIFT;
2255 w |= units;
2256 w <<= RTL_COALESCE_SHIFT;
2257 w |= p->frames >> 2;
2258 }
2259
2260 rtl_lock_work(tp);
2261
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002262 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03002263
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +02002264 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002265 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2266 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03002267
2268 rtl_unlock_work(tp);
2269
2270 return 0;
2271}
2272
Jeff Garzik7282d492006-09-13 14:30:00 -04002273static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 .get_drvinfo = rtl8169_get_drvinfo,
2275 .get_regs_len = rtl8169_get_regs_len,
2276 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002277 .get_coalesce = rtl_get_coalesce,
2278 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002279 .get_msglevel = rtl8169_get_msglevel,
2280 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002282 .get_wol = rtl8169_get_wol,
2283 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002284 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002285 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002286 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002287 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainellif0903ea2016-12-03 12:01:19 -08002288 .nway_reset = rtl8169_nway_reset,
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002289 .get_link_ksettings = rtl8169_get_link_ksettings,
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002290 .set_link_ksettings = rtl8169_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291};
2292
Francois Romieu07d3f512007-02-21 22:40:46 +01002293static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Heiner Kallweit22148df2018-04-22 17:15:15 +02002294 u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
Francois Romieu0e485152007-02-20 00:00:26 +01002296 /*
2297 * The driver currently handles the 8168Bf and the 8168Be identically
2298 * but they can be identified more specifically through the test below
2299 * if needed:
2300 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002301 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002302 *
2303 * Same thing for the 8101Eb and the 8101Ec:
2304 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002305 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002306 */
Francois Romieu37441002011-06-17 22:58:54 +02002307 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002309 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 int mac_version;
2311 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002312 /* 8168EP family. */
2313 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2314 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2315 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2316
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002317 /* 8168H family. */
2318 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2319 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2320
Hayes Wangc5583862012-07-02 17:23:22 +08002321 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002322 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002323 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002324 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2325 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2326
Hayes Wangc2218922011-09-06 16:55:18 +08002327 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002328 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002329 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2330 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2331
hayeswang01dc7fe2011-03-21 01:50:28 +00002332 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002333 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002334 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2335 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2336
Francois Romieu5b538df2008-07-20 16:22:45 +02002337 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002338 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002339 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002340
françois romieue6de30d2011-01-03 15:08:37 +00002341 /* 8168DP family. */
2342 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2343 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002344 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002345
Francois Romieuef808d52008-06-29 13:10:54 +02002346 /* 8168C family. */
Francois Romieuef3386f2008-06-29 12:24:30 +02002347 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002348 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002349 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002350 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2351 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002352 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieuef808d52008-06-29 13:10:54 +02002353 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002354
2355 /* 8168B family. */
2356 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002357 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2358 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2359
2360 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002361 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002362 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002363 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2364 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002365 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2366 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2367 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2368 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002369 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002370 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002371 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002372 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2373 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002374 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2375 /* FIXME: where did these entries come from ? -- FR */
2376 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2377 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2378
2379 /* 8110 family. */
2380 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2381 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2382 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2383 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2384 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2385 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2386
Jean Delvaref21b75e2009-05-26 20:54:48 -07002387 /* Catch-all */
2388 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002389 };
2390 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 u32 reg;
2392
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002393 reg = RTL_R32(tp, TxConfig);
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002394 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 p++;
2396 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002397
2398 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02002399 dev_notice(tp_to_dev(tp),
2400 "unknown MAC, using family default\n");
Francois Romieu5d320a22011-05-08 17:47:36 +02002401 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002402 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2403 tp->mac_version = tp->mii.supports_gmii ?
2404 RTL_GIGA_MAC_VER_42 :
2405 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002406 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2407 tp->mac_version = tp->mii.supports_gmii ?
2408 RTL_GIGA_MAC_VER_45 :
2409 RTL_GIGA_MAC_VER_47;
2410 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2411 tp->mac_version = tp->mii.supports_gmii ?
2412 RTL_GIGA_MAC_VER_46 :
2413 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
2417static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2418{
Heiner Kallweit49d17512018-06-28 20:36:15 +02002419 netif_dbg(tp, drv, tp->dev, "mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
Francois Romieu867763c2007-08-17 18:21:58 +02002422struct phy_reg {
2423 u16 reg;
2424 u16 val;
2425};
2426
françois romieu4da19632011-01-03 15:07:55 +00002427static void rtl_writephy_batch(struct rtl8169_private *tp,
2428 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002429{
2430 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002431 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002432 regs++;
2433 }
2434}
2435
françois romieubca03d52011-01-03 15:07:31 +00002436#define PHY_READ 0x00000000
2437#define PHY_DATA_OR 0x10000000
2438#define PHY_DATA_AND 0x20000000
2439#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002440#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002441#define PHY_CLEAR_READCOUNT 0x70000000
2442#define PHY_WRITE 0x80000000
2443#define PHY_READCOUNT_EQ_SKIP 0x90000000
2444#define PHY_COMP_EQ_SKIPN 0xa0000000
2445#define PHY_COMP_NEQ_SKIPN 0xb0000000
2446#define PHY_WRITE_PREVIOUS 0xc0000000
2447#define PHY_SKIPN 0xd0000000
2448#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002449
Hayes Wang960aee62011-06-18 11:37:48 +02002450struct fw_info {
2451 u32 magic;
2452 char version[RTL_VER_SIZE];
2453 __le32 fw_start;
2454 __le32 fw_len;
2455 u8 chksum;
2456} __packed;
2457
Francois Romieu1c361ef2011-06-17 17:16:24 +02002458#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2459
2460static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002461{
Francois Romieub6ffd972011-06-17 17:00:05 +02002462 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002463 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002464 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2465 char *version = rtl_fw->version;
2466 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002467
Francois Romieu1c361ef2011-06-17 17:16:24 +02002468 if (fw->size < FW_OPCODE_SIZE)
2469 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002470
2471 if (!fw_info->magic) {
2472 size_t i, size, start;
2473 u8 checksum = 0;
2474
2475 if (fw->size < sizeof(*fw_info))
2476 goto out;
2477
2478 for (i = 0; i < fw->size; i++)
2479 checksum += fw->data[i];
2480 if (checksum != 0)
2481 goto out;
2482
2483 start = le32_to_cpu(fw_info->fw_start);
2484 if (start > fw->size)
2485 goto out;
2486
2487 size = le32_to_cpu(fw_info->fw_len);
2488 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2489 goto out;
2490
2491 memcpy(version, fw_info->version, RTL_VER_SIZE);
2492
2493 pa->code = (__le32 *)(fw->data + start);
2494 pa->size = size;
2495 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002496 if (fw->size % FW_OPCODE_SIZE)
2497 goto out;
2498
2499 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2500
2501 pa->code = (__le32 *)fw->data;
2502 pa->size = fw->size / FW_OPCODE_SIZE;
2503 }
2504 version[RTL_VER_SIZE - 1] = 0;
2505
2506 rc = true;
2507out:
2508 return rc;
2509}
2510
Francois Romieufd112f22011-06-18 00:10:29 +02002511static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2512 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002513{
Francois Romieufd112f22011-06-18 00:10:29 +02002514 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002515 size_t index;
2516
Francois Romieu1c361ef2011-06-17 17:16:24 +02002517 for (index = 0; index < pa->size; index++) {
2518 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002519 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002520
hayeswang42b82dc2011-01-10 02:07:25 +00002521 switch(action & 0xf0000000) {
2522 case PHY_READ:
2523 case PHY_DATA_OR:
2524 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002525 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002526 case PHY_CLEAR_READCOUNT:
2527 case PHY_WRITE:
2528 case PHY_WRITE_PREVIOUS:
2529 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002530 break;
2531
hayeswang42b82dc2011-01-10 02:07:25 +00002532 case PHY_BJMPN:
2533 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002534 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002535 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002536 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002537 }
2538 break;
2539 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002540 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002541 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002542 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002543 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002544 }
2545 break;
2546 case PHY_COMP_EQ_SKIPN:
2547 case PHY_COMP_NEQ_SKIPN:
2548 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002549 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002550 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002551 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002552 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002553 }
2554 break;
2555
hayeswang42b82dc2011-01-10 02:07:25 +00002556 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002557 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002558 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002559 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002560 }
2561 }
Francois Romieufd112f22011-06-18 00:10:29 +02002562 rc = true;
2563out:
2564 return rc;
2565}
françois romieubca03d52011-01-03 15:07:31 +00002566
Francois Romieufd112f22011-06-18 00:10:29 +02002567static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2568{
2569 struct net_device *dev = tp->dev;
2570 int rc = -EINVAL;
2571
2572 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002573 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002574 goto out;
2575 }
2576
2577 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2578 rc = 0;
2579out:
2580 return rc;
2581}
2582
2583static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2584{
2585 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002586 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002587 u32 predata, count;
2588 size_t index;
2589
2590 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002591 org.write = ops->write;
2592 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002593
Francois Romieu1c361ef2011-06-17 17:16:24 +02002594 for (index = 0; index < pa->size; ) {
2595 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002596 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002597 u32 regno = (action & 0x0fff0000) >> 16;
2598
2599 if (!action)
2600 break;
françois romieubca03d52011-01-03 15:07:31 +00002601
2602 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002603 case PHY_READ:
2604 predata = rtl_readphy(tp, regno);
2605 count++;
2606 index++;
françois romieubca03d52011-01-03 15:07:31 +00002607 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002608 case PHY_DATA_OR:
2609 predata |= data;
2610 index++;
2611 break;
2612 case PHY_DATA_AND:
2613 predata &= data;
2614 index++;
2615 break;
2616 case PHY_BJMPN:
2617 index -= regno;
2618 break;
hayeswangeee37862013-04-01 22:23:38 +00002619 case PHY_MDIO_CHG:
2620 if (data == 0) {
2621 ops->write = org.write;
2622 ops->read = org.read;
2623 } else if (data == 1) {
2624 ops->write = mac_mcu_write;
2625 ops->read = mac_mcu_read;
2626 }
2627
hayeswang42b82dc2011-01-10 02:07:25 +00002628 index++;
2629 break;
2630 case PHY_CLEAR_READCOUNT:
2631 count = 0;
2632 index++;
2633 break;
2634 case PHY_WRITE:
2635 rtl_writephy(tp, regno, data);
2636 index++;
2637 break;
2638 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002639 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002640 break;
2641 case PHY_COMP_EQ_SKIPN:
2642 if (predata == data)
2643 index += regno;
2644 index++;
2645 break;
2646 case PHY_COMP_NEQ_SKIPN:
2647 if (predata != data)
2648 index += regno;
2649 index++;
2650 break;
2651 case PHY_WRITE_PREVIOUS:
2652 rtl_writephy(tp, regno, predata);
2653 index++;
2654 break;
2655 case PHY_SKIPN:
2656 index += regno + 1;
2657 break;
2658 case PHY_DELAY_MS:
2659 mdelay(data);
2660 index++;
2661 break;
2662
françois romieubca03d52011-01-03 15:07:31 +00002663 default:
2664 BUG();
2665 }
2666 }
hayeswangeee37862013-04-01 22:23:38 +00002667
2668 ops->write = org.write;
2669 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002670}
2671
françois romieuf1e02ed2011-01-13 13:07:53 +00002672static void rtl_release_firmware(struct rtl8169_private *tp)
2673{
Francois Romieub6ffd972011-06-17 17:00:05 +02002674 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2675 release_firmware(tp->rtl_fw->fw);
2676 kfree(tp->rtl_fw);
2677 }
2678 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002679}
2680
François Romieu953a12c2011-04-24 17:38:48 +02002681static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002682{
Francois Romieub6ffd972011-06-17 17:00:05 +02002683 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002684
2685 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002686 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002687 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002688}
2689
2690static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2691{
2692 if (rtl_readphy(tp, reg) != val)
2693 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2694 else
2695 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002696}
2697
françois romieu4da19632011-01-03 15:07:55 +00002698static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002700 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002701 { 0x1f, 0x0001 },
2702 { 0x06, 0x006e },
2703 { 0x08, 0x0708 },
2704 { 0x15, 0x4000 },
2705 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
françois romieu0b9b5712009-08-10 19:44:56 +00002707 { 0x1f, 0x0001 },
2708 { 0x03, 0x00a1 },
2709 { 0x02, 0x0008 },
2710 { 0x01, 0x0120 },
2711 { 0x00, 0x1000 },
2712 { 0x04, 0x0800 },
2713 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
françois romieu0b9b5712009-08-10 19:44:56 +00002715 { 0x03, 0xff41 },
2716 { 0x02, 0xdf60 },
2717 { 0x01, 0x0140 },
2718 { 0x00, 0x0077 },
2719 { 0x04, 0x7800 },
2720 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
françois romieu0b9b5712009-08-10 19:44:56 +00002722 { 0x03, 0x802f },
2723 { 0x02, 0x4f02 },
2724 { 0x01, 0x0409 },
2725 { 0x00, 0xf0f9 },
2726 { 0x04, 0x9800 },
2727 { 0x04, 0x9000 },
2728
2729 { 0x03, 0xdf01 },
2730 { 0x02, 0xdf20 },
2731 { 0x01, 0xff95 },
2732 { 0x00, 0xba00 },
2733 { 0x04, 0xa800 },
2734 { 0x04, 0xa000 },
2735
2736 { 0x03, 0xff41 },
2737 { 0x02, 0xdf20 },
2738 { 0x01, 0x0140 },
2739 { 0x00, 0x00bb },
2740 { 0x04, 0xb800 },
2741 { 0x04, 0xb000 },
2742
2743 { 0x03, 0xdf41 },
2744 { 0x02, 0xdc60 },
2745 { 0x01, 0x6340 },
2746 { 0x00, 0x007d },
2747 { 0x04, 0xd800 },
2748 { 0x04, 0xd000 },
2749
2750 { 0x03, 0xdf01 },
2751 { 0x02, 0xdf20 },
2752 { 0x01, 0x100a },
2753 { 0x00, 0xa0ff },
2754 { 0x04, 0xf800 },
2755 { 0x04, 0xf000 },
2756
2757 { 0x1f, 0x0000 },
2758 { 0x0b, 0x0000 },
2759 { 0x00, 0x9200 }
2760 };
2761
françois romieu4da19632011-01-03 15:07:55 +00002762 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
2764
françois romieu4da19632011-01-03 15:07:55 +00002765static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002766{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002767 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002768 { 0x1f, 0x0002 },
2769 { 0x01, 0x90d0 },
2770 { 0x1f, 0x0000 }
2771 };
2772
françois romieu4da19632011-01-03 15:07:55 +00002773 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002774}
2775
françois romieu4da19632011-01-03 15:07:55 +00002776static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002777{
2778 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002779
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002780 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2781 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002782 return;
2783
françois romieu4da19632011-01-03 15:07:55 +00002784 rtl_writephy(tp, 0x1f, 0x0001);
2785 rtl_writephy(tp, 0x10, 0xf01b);
2786 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002787}
2788
françois romieu4da19632011-01-03 15:07:55 +00002789static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002790{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002791 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002792 { 0x1f, 0x0001 },
2793 { 0x04, 0x0000 },
2794 { 0x03, 0x00a1 },
2795 { 0x02, 0x0008 },
2796 { 0x01, 0x0120 },
2797 { 0x00, 0x1000 },
2798 { 0x04, 0x0800 },
2799 { 0x04, 0x9000 },
2800 { 0x03, 0x802f },
2801 { 0x02, 0x4f02 },
2802 { 0x01, 0x0409 },
2803 { 0x00, 0xf099 },
2804 { 0x04, 0x9800 },
2805 { 0x04, 0xa000 },
2806 { 0x03, 0xdf01 },
2807 { 0x02, 0xdf20 },
2808 { 0x01, 0xff95 },
2809 { 0x00, 0xba00 },
2810 { 0x04, 0xa800 },
2811 { 0x04, 0xf000 },
2812 { 0x03, 0xdf01 },
2813 { 0x02, 0xdf20 },
2814 { 0x01, 0x101a },
2815 { 0x00, 0xa0ff },
2816 { 0x04, 0xf800 },
2817 { 0x04, 0x0000 },
2818 { 0x1f, 0x0000 },
2819
2820 { 0x1f, 0x0001 },
2821 { 0x10, 0xf41b },
2822 { 0x14, 0xfb54 },
2823 { 0x18, 0xf5c7 },
2824 { 0x1f, 0x0000 },
2825
2826 { 0x1f, 0x0001 },
2827 { 0x17, 0x0cc0 },
2828 { 0x1f, 0x0000 }
2829 };
2830
françois romieu4da19632011-01-03 15:07:55 +00002831 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002832
françois romieu4da19632011-01-03 15:07:55 +00002833 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002834}
2835
françois romieu4da19632011-01-03 15:07:55 +00002836static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002837{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002838 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002839 { 0x1f, 0x0001 },
2840 { 0x04, 0x0000 },
2841 { 0x03, 0x00a1 },
2842 { 0x02, 0x0008 },
2843 { 0x01, 0x0120 },
2844 { 0x00, 0x1000 },
2845 { 0x04, 0x0800 },
2846 { 0x04, 0x9000 },
2847 { 0x03, 0x802f },
2848 { 0x02, 0x4f02 },
2849 { 0x01, 0x0409 },
2850 { 0x00, 0xf099 },
2851 { 0x04, 0x9800 },
2852 { 0x04, 0xa000 },
2853 { 0x03, 0xdf01 },
2854 { 0x02, 0xdf20 },
2855 { 0x01, 0xff95 },
2856 { 0x00, 0xba00 },
2857 { 0x04, 0xa800 },
2858 { 0x04, 0xf000 },
2859 { 0x03, 0xdf01 },
2860 { 0x02, 0xdf20 },
2861 { 0x01, 0x101a },
2862 { 0x00, 0xa0ff },
2863 { 0x04, 0xf800 },
2864 { 0x04, 0x0000 },
2865 { 0x1f, 0x0000 },
2866
2867 { 0x1f, 0x0001 },
2868 { 0x0b, 0x8480 },
2869 { 0x1f, 0x0000 },
2870
2871 { 0x1f, 0x0001 },
2872 { 0x18, 0x67c7 },
2873 { 0x04, 0x2000 },
2874 { 0x03, 0x002f },
2875 { 0x02, 0x4360 },
2876 { 0x01, 0x0109 },
2877 { 0x00, 0x3022 },
2878 { 0x04, 0x2800 },
2879 { 0x1f, 0x0000 },
2880
2881 { 0x1f, 0x0001 },
2882 { 0x17, 0x0cc0 },
2883 { 0x1f, 0x0000 }
2884 };
2885
françois romieu4da19632011-01-03 15:07:55 +00002886 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002887}
2888
françois romieu4da19632011-01-03 15:07:55 +00002889static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002890{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002891 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002892 { 0x10, 0xf41b },
2893 { 0x1f, 0x0000 }
2894 };
2895
françois romieu4da19632011-01-03 15:07:55 +00002896 rtl_writephy(tp, 0x1f, 0x0001);
2897 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002898
françois romieu4da19632011-01-03 15:07:55 +00002899 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002900}
2901
françois romieu4da19632011-01-03 15:07:55 +00002902static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002903{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002904 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002905 { 0x1f, 0x0001 },
2906 { 0x10, 0xf41b },
2907 { 0x1f, 0x0000 }
2908 };
2909
françois romieu4da19632011-01-03 15:07:55 +00002910 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002911}
2912
françois romieu4da19632011-01-03 15:07:55 +00002913static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002914{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002915 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002916 { 0x1f, 0x0000 },
2917 { 0x1d, 0x0f00 },
2918 { 0x1f, 0x0002 },
2919 { 0x0c, 0x1ec8 },
2920 { 0x1f, 0x0000 }
2921 };
2922
françois romieu4da19632011-01-03 15:07:55 +00002923 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002924}
2925
françois romieu4da19632011-01-03 15:07:55 +00002926static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002927{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002928 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002929 { 0x1f, 0x0001 },
2930 { 0x1d, 0x3d98 },
2931 { 0x1f, 0x0000 }
2932 };
2933
françois romieu4da19632011-01-03 15:07:55 +00002934 rtl_writephy(tp, 0x1f, 0x0000);
2935 rtl_patchphy(tp, 0x14, 1 << 5);
2936 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002937
françois romieu4da19632011-01-03 15:07:55 +00002938 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002939}
2940
françois romieu4da19632011-01-03 15:07:55 +00002941static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002942{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002943 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002944 { 0x1f, 0x0001 },
2945 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002946 { 0x1f, 0x0002 },
2947 { 0x00, 0x88d4 },
2948 { 0x01, 0x82b1 },
2949 { 0x03, 0x7002 },
2950 { 0x08, 0x9e30 },
2951 { 0x09, 0x01f0 },
2952 { 0x0a, 0x5500 },
2953 { 0x0c, 0x00c8 },
2954 { 0x1f, 0x0003 },
2955 { 0x12, 0xc096 },
2956 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002957 { 0x1f, 0x0000 },
2958 { 0x1f, 0x0000 },
2959 { 0x09, 0x2000 },
2960 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002961 };
2962
françois romieu4da19632011-01-03 15:07:55 +00002963 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002964
françois romieu4da19632011-01-03 15:07:55 +00002965 rtl_patchphy(tp, 0x14, 1 << 5);
2966 rtl_patchphy(tp, 0x0d, 1 << 5);
2967 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002968}
2969
françois romieu4da19632011-01-03 15:07:55 +00002970static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002971{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002972 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002973 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002974 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002975 { 0x03, 0x802f },
2976 { 0x02, 0x4f02 },
2977 { 0x01, 0x0409 },
2978 { 0x00, 0xf099 },
2979 { 0x04, 0x9800 },
2980 { 0x04, 0x9000 },
2981 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002982 { 0x1f, 0x0002 },
2983 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002984 { 0x06, 0x0761 },
2985 { 0x1f, 0x0003 },
2986 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002987 { 0x1f, 0x0000 }
2988 };
2989
françois romieu4da19632011-01-03 15:07:55 +00002990 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002991
françois romieu4da19632011-01-03 15:07:55 +00002992 rtl_patchphy(tp, 0x16, 1 << 0);
2993 rtl_patchphy(tp, 0x14, 1 << 5);
2994 rtl_patchphy(tp, 0x0d, 1 << 5);
2995 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002996}
2997
françois romieu4da19632011-01-03 15:07:55 +00002998static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002999{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003000 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02003001 { 0x1f, 0x0001 },
3002 { 0x12, 0x2300 },
3003 { 0x1d, 0x3d98 },
3004 { 0x1f, 0x0002 },
3005 { 0x0c, 0x7eb8 },
3006 { 0x06, 0x5461 },
3007 { 0x1f, 0x0003 },
3008 { 0x16, 0x0f0a },
3009 { 0x1f, 0x0000 }
3010 };
3011
françois romieu4da19632011-01-03 15:07:55 +00003012 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02003013
françois romieu4da19632011-01-03 15:07:55 +00003014 rtl_patchphy(tp, 0x16, 1 << 0);
3015 rtl_patchphy(tp, 0x14, 1 << 5);
3016 rtl_patchphy(tp, 0x0d, 1 << 5);
3017 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003018}
3019
françois romieu4da19632011-01-03 15:07:55 +00003020static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003021{
françois romieu4da19632011-01-03 15:07:55 +00003022 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003023}
3024
françois romieubca03d52011-01-03 15:07:31 +00003025static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003026{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003027 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003028 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003029 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003030 { 0x06, 0x4064 },
3031 { 0x07, 0x2863 },
3032 { 0x08, 0x059c },
3033 { 0x09, 0x26b4 },
3034 { 0x0a, 0x6a19 },
3035 { 0x0b, 0xdcc8 },
3036 { 0x10, 0xf06d },
3037 { 0x14, 0x7f68 },
3038 { 0x18, 0x7fd9 },
3039 { 0x1c, 0xf0ff },
3040 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003041 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003042 { 0x12, 0xf49f },
3043 { 0x13, 0x070b },
3044 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003045 { 0x14, 0x94c0 },
3046
3047 /*
3048 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003049 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003050 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003051 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003052 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003053 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003054 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003055 { 0x06, 0x5561 },
3056
3057 /*
3058 * Can not link to 1Gbps with bad cable
3059 * Decrease SNR threshold form 21.07dB to 19.04dB
3060 */
3061 { 0x1f, 0x0001 },
3062 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003063
3064 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003065 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003066 };
3067
françois romieu4da19632011-01-03 15:07:55 +00003068 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003069
françois romieubca03d52011-01-03 15:07:31 +00003070 /*
3071 * Rx Error Issue
3072 * Fine Tune Switching regulator parameter
3073 */
françois romieu4da19632011-01-03 15:07:55 +00003074 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003075 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3076 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003077
Francois Romieufdf6fc02012-07-06 22:40:38 +02003078 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003079 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003080 { 0x1f, 0x0002 },
3081 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003082 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003083 { 0x05, 0x8330 },
3084 { 0x06, 0x669a },
3085 { 0x1f, 0x0002 }
3086 };
3087 int val;
3088
françois romieu4da19632011-01-03 15:07:55 +00003089 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003090
françois romieu4da19632011-01-03 15:07:55 +00003091 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003092
3093 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003094 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003095 0x0065, 0x0066, 0x0067, 0x0068,
3096 0x0069, 0x006a, 0x006b, 0x006c
3097 };
3098 int i;
3099
françois romieu4da19632011-01-03 15:07:55 +00003100 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003101
3102 val &= 0xff00;
3103 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003104 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003105 }
3106 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003107 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003108 { 0x1f, 0x0002 },
3109 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003110 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003111 { 0x05, 0x8330 },
3112 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003113 };
3114
françois romieu4da19632011-01-03 15:07:55 +00003115 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003116 }
3117
françois romieubca03d52011-01-03 15:07:31 +00003118 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003119 rtl_writephy(tp, 0x1f, 0x0002);
3120 rtl_patchphy(tp, 0x0d, 0x0300);
3121 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003122
françois romieubca03d52011-01-03 15:07:31 +00003123 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003124 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003125 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3126 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003127
françois romieu4da19632011-01-03 15:07:55 +00003128 rtl_writephy(tp, 0x1f, 0x0005);
3129 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003130
3131 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003132
françois romieu4da19632011-01-03 15:07:55 +00003133 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003134}
3135
françois romieubca03d52011-01-03 15:07:31 +00003136static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003137{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003138 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003139 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003140 { 0x1f, 0x0001 },
3141 { 0x06, 0x4064 },
3142 { 0x07, 0x2863 },
3143 { 0x08, 0x059c },
3144 { 0x09, 0x26b4 },
3145 { 0x0a, 0x6a19 },
3146 { 0x0b, 0xdcc8 },
3147 { 0x10, 0xf06d },
3148 { 0x14, 0x7f68 },
3149 { 0x18, 0x7fd9 },
3150 { 0x1c, 0xf0ff },
3151 { 0x1d, 0x3d9c },
3152 { 0x1f, 0x0003 },
3153 { 0x12, 0xf49f },
3154 { 0x13, 0x070b },
3155 { 0x1a, 0x05ad },
3156 { 0x14, 0x94c0 },
3157
françois romieubca03d52011-01-03 15:07:31 +00003158 /*
3159 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003160 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003161 */
françois romieudaf9df62009-10-07 12:44:20 +00003162 { 0x1f, 0x0002 },
3163 { 0x06, 0x5561 },
3164 { 0x1f, 0x0005 },
3165 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003166 { 0x06, 0x5561 },
3167
3168 /*
3169 * Can not link to 1Gbps with bad cable
3170 * Decrease SNR threshold form 21.07dB to 19.04dB
3171 */
3172 { 0x1f, 0x0001 },
3173 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003174
3175 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003176 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003177 };
3178
françois romieu4da19632011-01-03 15:07:55 +00003179 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003180
Francois Romieufdf6fc02012-07-06 22:40:38 +02003181 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003182 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003183 { 0x1f, 0x0002 },
3184 { 0x05, 0x669a },
3185 { 0x1f, 0x0005 },
3186 { 0x05, 0x8330 },
3187 { 0x06, 0x669a },
3188
3189 { 0x1f, 0x0002 }
3190 };
3191 int val;
3192
françois romieu4da19632011-01-03 15:07:55 +00003193 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003194
françois romieu4da19632011-01-03 15:07:55 +00003195 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003196 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003197 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003198 0x0065, 0x0066, 0x0067, 0x0068,
3199 0x0069, 0x006a, 0x006b, 0x006c
3200 };
3201 int i;
3202
françois romieu4da19632011-01-03 15:07:55 +00003203 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003204
3205 val &= 0xff00;
3206 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003207 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003208 }
3209 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003210 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003211 { 0x1f, 0x0002 },
3212 { 0x05, 0x2642 },
3213 { 0x1f, 0x0005 },
3214 { 0x05, 0x8330 },
3215 { 0x06, 0x2642 }
3216 };
3217
françois romieu4da19632011-01-03 15:07:55 +00003218 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003219 }
3220
françois romieubca03d52011-01-03 15:07:31 +00003221 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003222 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003223 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3224 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003225
françois romieubca03d52011-01-03 15:07:31 +00003226 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003227 rtl_writephy(tp, 0x1f, 0x0002);
3228 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003229
françois romieu4da19632011-01-03 15:07:55 +00003230 rtl_writephy(tp, 0x1f, 0x0005);
3231 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003232
3233 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003234
françois romieu4da19632011-01-03 15:07:55 +00003235 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003236}
3237
françois romieu4da19632011-01-03 15:07:55 +00003238static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003239{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003240 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003241 { 0x1f, 0x0002 },
3242 { 0x10, 0x0008 },
3243 { 0x0d, 0x006c },
3244
3245 { 0x1f, 0x0000 },
3246 { 0x0d, 0xf880 },
3247
3248 { 0x1f, 0x0001 },
3249 { 0x17, 0x0cc0 },
3250
3251 { 0x1f, 0x0001 },
3252 { 0x0b, 0xa4d8 },
3253 { 0x09, 0x281c },
3254 { 0x07, 0x2883 },
3255 { 0x0a, 0x6b35 },
3256 { 0x1d, 0x3da4 },
3257 { 0x1c, 0xeffd },
3258 { 0x14, 0x7f52 },
3259 { 0x18, 0x7fc6 },
3260 { 0x08, 0x0601 },
3261 { 0x06, 0x4063 },
3262 { 0x10, 0xf074 },
3263 { 0x1f, 0x0003 },
3264 { 0x13, 0x0789 },
3265 { 0x12, 0xf4bd },
3266 { 0x1a, 0x04fd },
3267 { 0x14, 0x84b0 },
3268 { 0x1f, 0x0000 },
3269 { 0x00, 0x9200 },
3270
3271 { 0x1f, 0x0005 },
3272 { 0x01, 0x0340 },
3273 { 0x1f, 0x0001 },
3274 { 0x04, 0x4000 },
3275 { 0x03, 0x1d21 },
3276 { 0x02, 0x0c32 },
3277 { 0x01, 0x0200 },
3278 { 0x00, 0x5554 },
3279 { 0x04, 0x4800 },
3280 { 0x04, 0x4000 },
3281 { 0x04, 0xf000 },
3282 { 0x03, 0xdf01 },
3283 { 0x02, 0xdf20 },
3284 { 0x01, 0x101a },
3285 { 0x00, 0xa0ff },
3286 { 0x04, 0xf800 },
3287 { 0x04, 0xf000 },
3288 { 0x1f, 0x0000 },
3289
3290 { 0x1f, 0x0007 },
3291 { 0x1e, 0x0023 },
3292 { 0x16, 0x0000 },
3293 { 0x1f, 0x0000 }
3294 };
3295
françois romieu4da19632011-01-03 15:07:55 +00003296 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003297}
3298
françois romieue6de30d2011-01-03 15:08:37 +00003299static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3300{
3301 static const struct phy_reg phy_reg_init[] = {
3302 { 0x1f, 0x0001 },
3303 { 0x17, 0x0cc0 },
3304
3305 { 0x1f, 0x0007 },
3306 { 0x1e, 0x002d },
3307 { 0x18, 0x0040 },
3308 { 0x1f, 0x0000 }
3309 };
3310
3311 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3312 rtl_patchphy(tp, 0x0d, 1 << 5);
3313}
3314
Hayes Wang70090422011-07-06 15:58:06 +08003315static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003316{
3317 static const struct phy_reg phy_reg_init[] = {
3318 /* Enable Delay cap */
3319 { 0x1f, 0x0005 },
3320 { 0x05, 0x8b80 },
3321 { 0x06, 0xc896 },
3322 { 0x1f, 0x0000 },
3323
3324 /* Channel estimation fine tune */
3325 { 0x1f, 0x0001 },
3326 { 0x0b, 0x6c20 },
3327 { 0x07, 0x2872 },
3328 { 0x1c, 0xefff },
3329 { 0x1f, 0x0003 },
3330 { 0x14, 0x6420 },
3331 { 0x1f, 0x0000 },
3332
3333 /* Update PFM & 10M TX idle timer */
3334 { 0x1f, 0x0007 },
3335 { 0x1e, 0x002f },
3336 { 0x15, 0x1919 },
3337 { 0x1f, 0x0000 },
3338
3339 { 0x1f, 0x0007 },
3340 { 0x1e, 0x00ac },
3341 { 0x18, 0x0006 },
3342 { 0x1f, 0x0000 }
3343 };
3344
Francois Romieu15ecd032011-04-27 13:52:22 -07003345 rtl_apply_firmware(tp);
3346
hayeswang01dc7fe2011-03-21 01:50:28 +00003347 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3348
3349 /* DCO enable for 10M IDLE Power */
3350 rtl_writephy(tp, 0x1f, 0x0007);
3351 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003352 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003353 rtl_writephy(tp, 0x1f, 0x0000);
3354
3355 /* For impedance matching */
3356 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003357 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003358 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003359
3360 /* PHY auto speed down */
3361 rtl_writephy(tp, 0x1f, 0x0007);
3362 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003363 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003364 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003365 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003366
3367 rtl_writephy(tp, 0x1f, 0x0005);
3368 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003369 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003370 rtl_writephy(tp, 0x1f, 0x0000);
3371
3372 rtl_writephy(tp, 0x1f, 0x0005);
3373 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003374 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003375 rtl_writephy(tp, 0x1f, 0x0007);
3376 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003377 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003378 rtl_writephy(tp, 0x1f, 0x0006);
3379 rtl_writephy(tp, 0x00, 0x5a00);
3380 rtl_writephy(tp, 0x1f, 0x0000);
3381 rtl_writephy(tp, 0x0d, 0x0007);
3382 rtl_writephy(tp, 0x0e, 0x003c);
3383 rtl_writephy(tp, 0x0d, 0x4007);
3384 rtl_writephy(tp, 0x0e, 0x0000);
3385 rtl_writephy(tp, 0x0d, 0x0000);
3386}
3387
françois romieu9ecb9aa2012-12-07 11:20:21 +00003388static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3389{
3390 const u16 w[] = {
3391 addr[0] | (addr[1] << 8),
3392 addr[2] | (addr[3] << 8),
3393 addr[4] | (addr[5] << 8)
3394 };
3395 const struct exgmac_reg e[] = {
3396 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3397 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3398 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3399 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3400 };
3401
3402 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3403}
3404
Hayes Wang70090422011-07-06 15:58:06 +08003405static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3406{
3407 static const struct phy_reg phy_reg_init[] = {
3408 /* Enable Delay cap */
3409 { 0x1f, 0x0004 },
3410 { 0x1f, 0x0007 },
3411 { 0x1e, 0x00ac },
3412 { 0x18, 0x0006 },
3413 { 0x1f, 0x0002 },
3414 { 0x1f, 0x0000 },
3415 { 0x1f, 0x0000 },
3416
3417 /* Channel estimation fine tune */
3418 { 0x1f, 0x0003 },
3419 { 0x09, 0xa20f },
3420 { 0x1f, 0x0000 },
3421 { 0x1f, 0x0000 },
3422
3423 /* Green Setting */
3424 { 0x1f, 0x0005 },
3425 { 0x05, 0x8b5b },
3426 { 0x06, 0x9222 },
3427 { 0x05, 0x8b6d },
3428 { 0x06, 0x8000 },
3429 { 0x05, 0x8b76 },
3430 { 0x06, 0x8000 },
3431 { 0x1f, 0x0000 }
3432 };
3433
3434 rtl_apply_firmware(tp);
3435
3436 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3437
3438 /* For 4-corner performance improve */
3439 rtl_writephy(tp, 0x1f, 0x0005);
3440 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003441 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003442 rtl_writephy(tp, 0x1f, 0x0000);
3443
3444 /* PHY auto speed down */
3445 rtl_writephy(tp, 0x1f, 0x0004);
3446 rtl_writephy(tp, 0x1f, 0x0007);
3447 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003448 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003449 rtl_writephy(tp, 0x1f, 0x0002);
3450 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003451 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003452
3453 /* improve 10M EEE waveform */
3454 rtl_writephy(tp, 0x1f, 0x0005);
3455 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003456 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003457 rtl_writephy(tp, 0x1f, 0x0000);
3458
3459 /* Improve 2-pair detection performance */
3460 rtl_writephy(tp, 0x1f, 0x0005);
3461 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003462 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003463 rtl_writephy(tp, 0x1f, 0x0000);
3464
3465 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003466 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003467 rtl_writephy(tp, 0x1f, 0x0005);
3468 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003469 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003470 rtl_writephy(tp, 0x1f, 0x0004);
3471 rtl_writephy(tp, 0x1f, 0x0007);
3472 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003473 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003474 rtl_writephy(tp, 0x1f, 0x0002);
3475 rtl_writephy(tp, 0x1f, 0x0000);
3476 rtl_writephy(tp, 0x0d, 0x0007);
3477 rtl_writephy(tp, 0x0e, 0x003c);
3478 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003479 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003480 rtl_writephy(tp, 0x0d, 0x0000);
3481
3482 /* Green feature */
3483 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003484 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3485 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003486 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003487 rtl_writephy(tp, 0x1f, 0x0005);
3488 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3489 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003490
françois romieu9ecb9aa2012-12-07 11:20:21 +00003491 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3492 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003493}
3494
Hayes Wang5f886e02012-03-30 14:33:03 +08003495static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3496{
3497 /* For 4-corner performance improve */
3498 rtl_writephy(tp, 0x1f, 0x0005);
3499 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003500 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003501 rtl_writephy(tp, 0x1f, 0x0000);
3502
3503 /* PHY auto speed down */
3504 rtl_writephy(tp, 0x1f, 0x0007);
3505 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003506 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003507 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003508 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003509
3510 /* Improve 10M EEE waveform */
3511 rtl_writephy(tp, 0x1f, 0x0005);
3512 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003513 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003514 rtl_writephy(tp, 0x1f, 0x0000);
3515}
3516
Hayes Wangc2218922011-09-06 16:55:18 +08003517static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3518{
3519 static const struct phy_reg phy_reg_init[] = {
3520 /* Channel estimation fine tune */
3521 { 0x1f, 0x0003 },
3522 { 0x09, 0xa20f },
3523 { 0x1f, 0x0000 },
3524
3525 /* Modify green table for giga & fnet */
3526 { 0x1f, 0x0005 },
3527 { 0x05, 0x8b55 },
3528 { 0x06, 0x0000 },
3529 { 0x05, 0x8b5e },
3530 { 0x06, 0x0000 },
3531 { 0x05, 0x8b67 },
3532 { 0x06, 0x0000 },
3533 { 0x05, 0x8b70 },
3534 { 0x06, 0x0000 },
3535 { 0x1f, 0x0000 },
3536 { 0x1f, 0x0007 },
3537 { 0x1e, 0x0078 },
3538 { 0x17, 0x0000 },
3539 { 0x19, 0x00fb },
3540 { 0x1f, 0x0000 },
3541
3542 /* Modify green table for 10M */
3543 { 0x1f, 0x0005 },
3544 { 0x05, 0x8b79 },
3545 { 0x06, 0xaa00 },
3546 { 0x1f, 0x0000 },
3547
3548 /* Disable hiimpedance detection (RTCT) */
3549 { 0x1f, 0x0003 },
3550 { 0x01, 0x328a },
3551 { 0x1f, 0x0000 }
3552 };
3553
3554 rtl_apply_firmware(tp);
3555
3556 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3557
Hayes Wang5f886e02012-03-30 14:33:03 +08003558 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003559
3560 /* Improve 2-pair detection performance */
3561 rtl_writephy(tp, 0x1f, 0x0005);
3562 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003563 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003564 rtl_writephy(tp, 0x1f, 0x0000);
3565}
3566
3567static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3568{
3569 rtl_apply_firmware(tp);
3570
Hayes Wang5f886e02012-03-30 14:33:03 +08003571 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003572}
3573
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003574static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3575{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003576 static const struct phy_reg phy_reg_init[] = {
3577 /* Channel estimation fine tune */
3578 { 0x1f, 0x0003 },
3579 { 0x09, 0xa20f },
3580 { 0x1f, 0x0000 },
3581
3582 /* Modify green table for giga & fnet */
3583 { 0x1f, 0x0005 },
3584 { 0x05, 0x8b55 },
3585 { 0x06, 0x0000 },
3586 { 0x05, 0x8b5e },
3587 { 0x06, 0x0000 },
3588 { 0x05, 0x8b67 },
3589 { 0x06, 0x0000 },
3590 { 0x05, 0x8b70 },
3591 { 0x06, 0x0000 },
3592 { 0x1f, 0x0000 },
3593 { 0x1f, 0x0007 },
3594 { 0x1e, 0x0078 },
3595 { 0x17, 0x0000 },
3596 { 0x19, 0x00aa },
3597 { 0x1f, 0x0000 },
3598
3599 /* Modify green table for 10M */
3600 { 0x1f, 0x0005 },
3601 { 0x05, 0x8b79 },
3602 { 0x06, 0xaa00 },
3603 { 0x1f, 0x0000 },
3604
3605 /* Disable hiimpedance detection (RTCT) */
3606 { 0x1f, 0x0003 },
3607 { 0x01, 0x328a },
3608 { 0x1f, 0x0000 }
3609 };
3610
3611
3612 rtl_apply_firmware(tp);
3613
3614 rtl8168f_hw_phy_config(tp);
3615
3616 /* Improve 2-pair detection performance */
3617 rtl_writephy(tp, 0x1f, 0x0005);
3618 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003619 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003620 rtl_writephy(tp, 0x1f, 0x0000);
3621
3622 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3623
3624 /* Modify green table for giga */
3625 rtl_writephy(tp, 0x1f, 0x0005);
3626 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003627 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003628 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003629 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003630 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003631 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003632 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003633 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003634 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003635 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003636 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003637 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003638 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003639 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003640 rtl_writephy(tp, 0x1f, 0x0000);
3641
3642 /* uc same-seed solution */
3643 rtl_writephy(tp, 0x1f, 0x0005);
3644 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003645 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003646 rtl_writephy(tp, 0x1f, 0x0000);
3647
3648 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003649 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003650 rtl_writephy(tp, 0x1f, 0x0005);
3651 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003652 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003653 rtl_writephy(tp, 0x1f, 0x0004);
3654 rtl_writephy(tp, 0x1f, 0x0007);
3655 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003656 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003657 rtl_writephy(tp, 0x1f, 0x0000);
3658 rtl_writephy(tp, 0x0d, 0x0007);
3659 rtl_writephy(tp, 0x0e, 0x003c);
3660 rtl_writephy(tp, 0x0d, 0x4007);
3661 rtl_writephy(tp, 0x0e, 0x0000);
3662 rtl_writephy(tp, 0x0d, 0x0000);
3663
3664 /* Green feature */
3665 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003666 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3667 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003668 rtl_writephy(tp, 0x1f, 0x0000);
3669}
3670
Hayes Wangc5583862012-07-02 17:23:22 +08003671static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3672{
Hayes Wangc5583862012-07-02 17:23:22 +08003673 rtl_apply_firmware(tp);
3674
hayeswang41f44d12013-04-01 22:23:36 +00003675 rtl_writephy(tp, 0x1f, 0x0a46);
3676 if (rtl_readphy(tp, 0x10) & 0x0100) {
3677 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003678 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003679 } else {
3680 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003681 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003682 }
Hayes Wangc5583862012-07-02 17:23:22 +08003683
hayeswang41f44d12013-04-01 22:23:36 +00003684 rtl_writephy(tp, 0x1f, 0x0a46);
3685 if (rtl_readphy(tp, 0x13) & 0x0100) {
3686 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003687 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003688 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003689 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003690 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003691 }
Hayes Wangc5583862012-07-02 17:23:22 +08003692
hayeswang41f44d12013-04-01 22:23:36 +00003693 /* Enable PHY auto speed down */
3694 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003695 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003696
hayeswangfe7524c2013-04-01 22:23:37 +00003697 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003698 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003699 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003700 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003701 rtl_writephy(tp, 0x1f, 0x0a43);
3702 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003703 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3704 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003705
hayeswang41f44d12013-04-01 22:23:36 +00003706 /* EEE auto-fallback function */
3707 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003708 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003709
hayeswang41f44d12013-04-01 22:23:36 +00003710 /* Enable UC LPF tune function */
3711 rtl_writephy(tp, 0x1f, 0x0a43);
3712 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003713 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003714
3715 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003716 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003717
hayeswangfe7524c2013-04-01 22:23:37 +00003718 /* Improve SWR Efficiency */
3719 rtl_writephy(tp, 0x1f, 0x0bcd);
3720 rtl_writephy(tp, 0x14, 0x5065);
3721 rtl_writephy(tp, 0x14, 0xd065);
3722 rtl_writephy(tp, 0x1f, 0x0bc8);
3723 rtl_writephy(tp, 0x11, 0x5655);
3724 rtl_writephy(tp, 0x1f, 0x0bcd);
3725 rtl_writephy(tp, 0x14, 0x1065);
3726 rtl_writephy(tp, 0x14, 0x9065);
3727 rtl_writephy(tp, 0x14, 0x1065);
3728
David Chang1bac1072013-11-27 15:48:36 +08003729 /* Check ALDPS bit, disable it if enabled */
3730 rtl_writephy(tp, 0x1f, 0x0a43);
3731 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003732 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08003733
hayeswang41f44d12013-04-01 22:23:36 +00003734 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003735}
3736
hayeswang57538c42013-04-01 22:23:40 +00003737static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3738{
3739 rtl_apply_firmware(tp);
3740}
3741
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003742static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3743{
3744 u16 dout_tapbin;
3745 u32 data;
3746
3747 rtl_apply_firmware(tp);
3748
3749 /* CHN EST parameters adjust - giga master */
3750 rtl_writephy(tp, 0x1f, 0x0a43);
3751 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003752 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003753 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003754 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003755 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003756 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003757 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003758 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003759 rtl_writephy(tp, 0x1f, 0x0000);
3760
3761 /* CHN EST parameters adjust - giga slave */
3762 rtl_writephy(tp, 0x1f, 0x0a43);
3763 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003764 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003765 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003766 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003767 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003768 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003769 rtl_writephy(tp, 0x1f, 0x0000);
3770
3771 /* CHN EST parameters adjust - fnet */
3772 rtl_writephy(tp, 0x1f, 0x0a43);
3773 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003774 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003775 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003776 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003777 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003778 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003779 rtl_writephy(tp, 0x1f, 0x0000);
3780
3781 /* enable R-tune & PGA-retune function */
3782 dout_tapbin = 0;
3783 rtl_writephy(tp, 0x1f, 0x0a46);
3784 data = rtl_readphy(tp, 0x13);
3785 data &= 3;
3786 data <<= 2;
3787 dout_tapbin |= data;
3788 data = rtl_readphy(tp, 0x12);
3789 data &= 0xc000;
3790 data >>= 14;
3791 dout_tapbin |= data;
3792 dout_tapbin = ~(dout_tapbin^0x08);
3793 dout_tapbin <<= 12;
3794 dout_tapbin &= 0xf000;
3795 rtl_writephy(tp, 0x1f, 0x0a43);
3796 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003797 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003798 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003799 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003800 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003801 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003802 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003803 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003804
3805 rtl_writephy(tp, 0x1f, 0x0a43);
3806 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003807 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003808 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003809 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003810 rtl_writephy(tp, 0x1f, 0x0000);
3811
3812 /* enable GPHY 10M */
3813 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003814 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003815 rtl_writephy(tp, 0x1f, 0x0000);
3816
3817 /* SAR ADC performance */
3818 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003819 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003820 rtl_writephy(tp, 0x1f, 0x0000);
3821
3822 rtl_writephy(tp, 0x1f, 0x0a43);
3823 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003824 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003825 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003826 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003827 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003828 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003829 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003830 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003831 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003832 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003833 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003834 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003835 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003836 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003837 rtl_writephy(tp, 0x1f, 0x0000);
3838
3839 /* disable phy pfm mode */
3840 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003841 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003842 rtl_writephy(tp, 0x1f, 0x0000);
3843
3844 /* Check ALDPS bit, disable it if enabled */
3845 rtl_writephy(tp, 0x1f, 0x0a43);
3846 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003847 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003848
3849 rtl_writephy(tp, 0x1f, 0x0000);
3850}
3851
3852static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3853{
3854 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3855 u16 rlen;
3856 u32 data;
3857
3858 rtl_apply_firmware(tp);
3859
3860 /* CHIN EST parameter update */
3861 rtl_writephy(tp, 0x1f, 0x0a43);
3862 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003863 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003864 rtl_writephy(tp, 0x1f, 0x0000);
3865
3866 /* enable R-tune & PGA-retune function */
3867 rtl_writephy(tp, 0x1f, 0x0a43);
3868 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003869 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003870 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003871 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003872 rtl_writephy(tp, 0x1f, 0x0000);
3873
3874 /* enable GPHY 10M */
3875 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003876 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003877 rtl_writephy(tp, 0x1f, 0x0000);
3878
3879 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3880 data = r8168_mac_ocp_read(tp, 0xdd02);
3881 ioffset_p3 = ((data & 0x80)>>7);
3882 ioffset_p3 <<= 3;
3883
3884 data = r8168_mac_ocp_read(tp, 0xdd00);
3885 ioffset_p3 |= ((data & (0xe000))>>13);
3886 ioffset_p2 = ((data & (0x1e00))>>9);
3887 ioffset_p1 = ((data & (0x01e0))>>5);
3888 ioffset_p0 = ((data & 0x0010)>>4);
3889 ioffset_p0 <<= 3;
3890 ioffset_p0 |= (data & (0x07));
3891 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3892
Chun-Hao Lin05b96872014-10-01 23:17:12 +08003893 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08003894 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003895 rtl_writephy(tp, 0x1f, 0x0bcf);
3896 rtl_writephy(tp, 0x16, data);
3897 rtl_writephy(tp, 0x1f, 0x0000);
3898 }
3899
3900 /* Modify rlen (TX LPF corner frequency) level */
3901 rtl_writephy(tp, 0x1f, 0x0bcd);
3902 data = rtl_readphy(tp, 0x16);
3903 data &= 0x000f;
3904 rlen = 0;
3905 if (data > 3)
3906 rlen = data - 3;
3907 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3908 rtl_writephy(tp, 0x17, data);
3909 rtl_writephy(tp, 0x1f, 0x0bcd);
3910 rtl_writephy(tp, 0x1f, 0x0000);
3911
3912 /* disable phy pfm mode */
3913 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003914 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003915 rtl_writephy(tp, 0x1f, 0x0000);
3916
3917 /* Check ALDPS bit, disable it if enabled */
3918 rtl_writephy(tp, 0x1f, 0x0a43);
3919 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003920 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003921
3922 rtl_writephy(tp, 0x1f, 0x0000);
3923}
3924
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003925static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3926{
3927 /* Enable PHY auto speed down */
3928 rtl_writephy(tp, 0x1f, 0x0a44);
3929 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3930 rtl_writephy(tp, 0x1f, 0x0000);
3931
3932 /* patch 10M & ALDPS */
3933 rtl_writephy(tp, 0x1f, 0x0bcc);
3934 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3935 rtl_writephy(tp, 0x1f, 0x0a44);
3936 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3937 rtl_writephy(tp, 0x1f, 0x0a43);
3938 rtl_writephy(tp, 0x13, 0x8084);
3939 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3940 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3941 rtl_writephy(tp, 0x1f, 0x0000);
3942
3943 /* Enable EEE auto-fallback function */
3944 rtl_writephy(tp, 0x1f, 0x0a4b);
3945 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3946 rtl_writephy(tp, 0x1f, 0x0000);
3947
3948 /* Enable UC LPF tune function */
3949 rtl_writephy(tp, 0x1f, 0x0a43);
3950 rtl_writephy(tp, 0x13, 0x8012);
3951 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3952 rtl_writephy(tp, 0x1f, 0x0000);
3953
3954 /* set rg_sel_sdm_rate */
3955 rtl_writephy(tp, 0x1f, 0x0c42);
3956 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3957 rtl_writephy(tp, 0x1f, 0x0000);
3958
3959 /* Check ALDPS bit, disable it if enabled */
3960 rtl_writephy(tp, 0x1f, 0x0a43);
3961 if (rtl_readphy(tp, 0x10) & 0x0004)
3962 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3963
3964 rtl_writephy(tp, 0x1f, 0x0000);
3965}
3966
3967static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3968{
3969 /* patch 10M & ALDPS */
3970 rtl_writephy(tp, 0x1f, 0x0bcc);
3971 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3972 rtl_writephy(tp, 0x1f, 0x0a44);
3973 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3974 rtl_writephy(tp, 0x1f, 0x0a43);
3975 rtl_writephy(tp, 0x13, 0x8084);
3976 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3977 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3978 rtl_writephy(tp, 0x1f, 0x0000);
3979
3980 /* Enable UC LPF tune function */
3981 rtl_writephy(tp, 0x1f, 0x0a43);
3982 rtl_writephy(tp, 0x13, 0x8012);
3983 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3984 rtl_writephy(tp, 0x1f, 0x0000);
3985
3986 /* Set rg_sel_sdm_rate */
3987 rtl_writephy(tp, 0x1f, 0x0c42);
3988 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3989 rtl_writephy(tp, 0x1f, 0x0000);
3990
3991 /* Channel estimation parameters */
3992 rtl_writephy(tp, 0x1f, 0x0a43);
3993 rtl_writephy(tp, 0x13, 0x80f3);
3994 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
3995 rtl_writephy(tp, 0x13, 0x80f0);
3996 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
3997 rtl_writephy(tp, 0x13, 0x80ef);
3998 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
3999 rtl_writephy(tp, 0x13, 0x80f6);
4000 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4001 rtl_writephy(tp, 0x13, 0x80ec);
4002 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4003 rtl_writephy(tp, 0x13, 0x80ed);
4004 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4005 rtl_writephy(tp, 0x13, 0x80f2);
4006 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4007 rtl_writephy(tp, 0x13, 0x80f4);
4008 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4009 rtl_writephy(tp, 0x1f, 0x0a43);
4010 rtl_writephy(tp, 0x13, 0x8110);
4011 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4012 rtl_writephy(tp, 0x13, 0x810f);
4013 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4014 rtl_writephy(tp, 0x13, 0x8111);
4015 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4016 rtl_writephy(tp, 0x13, 0x8113);
4017 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4018 rtl_writephy(tp, 0x13, 0x8115);
4019 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4020 rtl_writephy(tp, 0x13, 0x810e);
4021 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4022 rtl_writephy(tp, 0x13, 0x810c);
4023 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4024 rtl_writephy(tp, 0x13, 0x810b);
4025 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4026 rtl_writephy(tp, 0x1f, 0x0a43);
4027 rtl_writephy(tp, 0x13, 0x80d1);
4028 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4029 rtl_writephy(tp, 0x13, 0x80cd);
4030 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4031 rtl_writephy(tp, 0x13, 0x80d3);
4032 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4033 rtl_writephy(tp, 0x13, 0x80d5);
4034 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4035 rtl_writephy(tp, 0x13, 0x80d7);
4036 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4037
4038 /* Force PWM-mode */
4039 rtl_writephy(tp, 0x1f, 0x0bcd);
4040 rtl_writephy(tp, 0x14, 0x5065);
4041 rtl_writephy(tp, 0x14, 0xd065);
4042 rtl_writephy(tp, 0x1f, 0x0bc8);
4043 rtl_writephy(tp, 0x12, 0x00ed);
4044 rtl_writephy(tp, 0x1f, 0x0bcd);
4045 rtl_writephy(tp, 0x14, 0x1065);
4046 rtl_writephy(tp, 0x14, 0x9065);
4047 rtl_writephy(tp, 0x14, 0x1065);
4048 rtl_writephy(tp, 0x1f, 0x0000);
4049
4050 /* Check ALDPS bit, disable it if enabled */
4051 rtl_writephy(tp, 0x1f, 0x0a43);
4052 if (rtl_readphy(tp, 0x10) & 0x0004)
4053 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4054
4055 rtl_writephy(tp, 0x1f, 0x0000);
4056}
4057
françois romieu4da19632011-01-03 15:07:55 +00004058static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004059{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004060 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004061 { 0x1f, 0x0003 },
4062 { 0x08, 0x441d },
4063 { 0x01, 0x9100 },
4064 { 0x1f, 0x0000 }
4065 };
4066
françois romieu4da19632011-01-03 15:07:55 +00004067 rtl_writephy(tp, 0x1f, 0x0000);
4068 rtl_patchphy(tp, 0x11, 1 << 12);
4069 rtl_patchphy(tp, 0x19, 1 << 13);
4070 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004071
françois romieu4da19632011-01-03 15:07:55 +00004072 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004073}
4074
Hayes Wang5a5e4442011-02-22 17:26:21 +08004075static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4076{
4077 static const struct phy_reg phy_reg_init[] = {
4078 { 0x1f, 0x0005 },
4079 { 0x1a, 0x0000 },
4080 { 0x1f, 0x0000 },
4081
4082 { 0x1f, 0x0004 },
4083 { 0x1c, 0x0000 },
4084 { 0x1f, 0x0000 },
4085
4086 { 0x1f, 0x0001 },
4087 { 0x15, 0x7701 },
4088 { 0x1f, 0x0000 }
4089 };
4090
4091 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004092 rtl_writephy(tp, 0x1f, 0x0000);
4093 rtl_writephy(tp, 0x18, 0x0310);
4094 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004095
François Romieu953a12c2011-04-24 17:38:48 +02004096 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004097
4098 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4099}
4100
Hayes Wang7e18dca2012-03-30 14:33:02 +08004101static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4102{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004103 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004104 rtl_writephy(tp, 0x1f, 0x0000);
4105 rtl_writephy(tp, 0x18, 0x0310);
4106 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004107
4108 rtl_apply_firmware(tp);
4109
4110 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004111 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004112 rtl_writephy(tp, 0x1f, 0x0004);
4113 rtl_writephy(tp, 0x10, 0x401f);
4114 rtl_writephy(tp, 0x19, 0x7030);
4115 rtl_writephy(tp, 0x1f, 0x0000);
4116}
4117
Hayes Wang5598bfe2012-07-02 17:23:21 +08004118static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4119{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004120 static const struct phy_reg phy_reg_init[] = {
4121 { 0x1f, 0x0004 },
4122 { 0x10, 0xc07f },
4123 { 0x19, 0x7030 },
4124 { 0x1f, 0x0000 }
4125 };
4126
4127 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004128 rtl_writephy(tp, 0x1f, 0x0000);
4129 rtl_writephy(tp, 0x18, 0x0310);
4130 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004131
4132 rtl_apply_firmware(tp);
4133
Francois Romieufdf6fc02012-07-06 22:40:38 +02004134 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004135 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4136
Francois Romieufdf6fc02012-07-06 22:40:38 +02004137 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004138}
4139
Francois Romieu5615d9f2007-08-17 17:50:46 +02004140static void rtl_hw_phy_config(struct net_device *dev)
4141{
4142 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004143
4144 rtl8169_print_mac_version(tp);
4145
4146 switch (tp->mac_version) {
4147 case RTL_GIGA_MAC_VER_01:
4148 break;
4149 case RTL_GIGA_MAC_VER_02:
4150 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004151 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004152 break;
4153 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004154 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004155 break;
françois romieu2e9558562009-08-10 19:44:19 +00004156 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004157 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004158 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004159 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004160 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004161 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004162 case RTL_GIGA_MAC_VER_07:
4163 case RTL_GIGA_MAC_VER_08:
4164 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004165 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004166 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004167 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004168 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004169 break;
4170 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004171 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004172 break;
4173 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004174 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004175 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004176 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004177 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004178 break;
4179 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004180 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004181 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004182 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004183 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004184 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004185 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004186 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004187 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004188 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004189 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004190 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004191 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004192 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004193 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004194 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004195 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004196 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004197 break;
4198 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004199 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004200 break;
4201 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004202 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004203 break;
françois romieue6de30d2011-01-03 15:08:37 +00004204 case RTL_GIGA_MAC_VER_28:
4205 rtl8168d_4_hw_phy_config(tp);
4206 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004207 case RTL_GIGA_MAC_VER_29:
4208 case RTL_GIGA_MAC_VER_30:
4209 rtl8105e_hw_phy_config(tp);
4210 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004211 case RTL_GIGA_MAC_VER_31:
4212 /* None. */
4213 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004214 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004215 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004216 rtl8168e_1_hw_phy_config(tp);
4217 break;
4218 case RTL_GIGA_MAC_VER_34:
4219 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004220 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004221 case RTL_GIGA_MAC_VER_35:
4222 rtl8168f_1_hw_phy_config(tp);
4223 break;
4224 case RTL_GIGA_MAC_VER_36:
4225 rtl8168f_2_hw_phy_config(tp);
4226 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004227
Hayes Wang7e18dca2012-03-30 14:33:02 +08004228 case RTL_GIGA_MAC_VER_37:
4229 rtl8402_hw_phy_config(tp);
4230 break;
4231
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004232 case RTL_GIGA_MAC_VER_38:
4233 rtl8411_hw_phy_config(tp);
4234 break;
4235
Hayes Wang5598bfe2012-07-02 17:23:21 +08004236 case RTL_GIGA_MAC_VER_39:
4237 rtl8106e_hw_phy_config(tp);
4238 break;
4239
Hayes Wangc5583862012-07-02 17:23:22 +08004240 case RTL_GIGA_MAC_VER_40:
4241 rtl8168g_1_hw_phy_config(tp);
4242 break;
hayeswang57538c42013-04-01 22:23:40 +00004243 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004244 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004245 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004246 rtl8168g_2_hw_phy_config(tp);
4247 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004248 case RTL_GIGA_MAC_VER_45:
4249 case RTL_GIGA_MAC_VER_47:
4250 rtl8168h_1_hw_phy_config(tp);
4251 break;
4252 case RTL_GIGA_MAC_VER_46:
4253 case RTL_GIGA_MAC_VER_48:
4254 rtl8168h_2_hw_phy_config(tp);
4255 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004256
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004257 case RTL_GIGA_MAC_VER_49:
4258 rtl8168ep_1_hw_phy_config(tp);
4259 break;
4260 case RTL_GIGA_MAC_VER_50:
4261 case RTL_GIGA_MAC_VER_51:
4262 rtl8168ep_2_hw_phy_config(tp);
4263 break;
4264
Hayes Wangc5583862012-07-02 17:23:22 +08004265 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004266 default:
4267 break;
4268 }
4269}
4270
Francois Romieuda78dbf2012-01-26 14:18:23 +01004271static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4272{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004273 if (!test_and_set_bit(flag, tp->wk.flags))
4274 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004275}
4276
Francois Romieuffc46952012-07-06 14:19:23 +02004277DECLARE_RTL_COND(rtl_phy_reset_cond)
4278{
Heiner Kallweite3972862018-06-29 08:07:04 +02004279 return rtl8169_xmii_reset_pending(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004280}
4281
Francois Romieubf793292006-11-01 00:53:05 +01004282static void rtl8169_phy_reset(struct net_device *dev,
4283 struct rtl8169_private *tp)
4284{
Heiner Kallweite3972862018-06-29 08:07:04 +02004285 rtl8169_xmii_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004286 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004287}
4288
David S. Miller8decf862011-09-22 03:23:13 -04004289static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4290{
David S. Miller8decf862011-09-22 03:23:13 -04004291 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
Heiner Kallweite3972862018-06-29 08:07:04 +02004292 (RTL_R8(tp, PHYstatus) & TBI_Enable);
David S. Miller8decf862011-09-22 03:23:13 -04004293}
4294
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004295static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296{
Francois Romieu5615d9f2007-08-17 17:50:46 +02004297 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004298
Marcus Sundberg773328942008-07-10 21:28:08 +02004299 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Heiner Kallweit49d17512018-06-28 20:36:15 +02004300 netif_dbg(tp, drv, dev,
4301 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004302 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02004303 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004304
Francois Romieu6dccd162007-02-13 23:38:05 +01004305 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4306
4307 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4308 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004309
Francois Romieubcf0bf92006-07-26 23:14:13 +02004310 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Heiner Kallweit49d17512018-06-28 20:36:15 +02004311 netif_dbg(tp, drv, dev,
4312 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004313 RTL_W8(tp, 0x82, 0x01);
Heiner Kallweit49d17512018-06-28 20:36:15 +02004314 netif_dbg(tp, drv, dev,
4315 "Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004316 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004317 }
4318
Francois Romieubf793292006-11-01 00:53:05 +01004319 rtl8169_phy_reset(dev, tp);
4320
Oliver Neukum54405cd2011-01-06 21:55:13 +01004321 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004322 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4323 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4324 (tp->mii.supports_gmii ?
4325 ADVERTISED_1000baseT_Half |
4326 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004327}
4328
Francois Romieu773d2022007-01-31 23:47:43 +01004329static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4330{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004331 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004332
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004333 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00004334
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004335 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4336 RTL_R32(tp, MAC4);
françois romieu908ba2b2010-04-26 11:42:58 +00004337
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004338 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4339 RTL_R32(tp, MAC0);
françois romieu908ba2b2010-04-26 11:42:58 +00004340
françois romieu9ecb9aa2012-12-07 11:20:21 +00004341 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4342 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004343
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004344 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieu773d2022007-01-31 23:47:43 +01004345
Francois Romieuda78dbf2012-01-26 14:18:23 +01004346 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004347}
4348
4349static int rtl_set_mac_address(struct net_device *dev, void *p)
4350{
4351 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004352 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004353 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004354
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004355 ret = eth_mac_addr(dev, p);
4356 if (ret)
4357 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004358
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004359 pm_runtime_get_noresume(d);
4360
4361 if (pm_runtime_active(d))
4362 rtl_rar_set(tp, dev->dev_addr);
4363
4364 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004365
4366 return 0;
4367}
4368
Francois Romieucecb5fd2011-04-01 10:21:07 +02004369static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4370 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004371{
Francois Romieu5f787a12006-08-17 13:02:36 +02004372 switch (cmd) {
4373 case SIOCGMIIPHY:
4374 data->phy_id = 32; /* Internal PHY */
4375 return 0;
4376
4377 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004378 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004379 return 0;
4380
4381 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004382 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004383 return 0;
4384 }
4385 return -EOPNOTSUPP;
4386}
4387
Heiner Kallweite3972862018-06-29 08:07:04 +02004388static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004389{
Heiner Kallweite3972862018-06-29 08:07:04 +02004390 struct rtl8169_private *tp = netdev_priv(dev);
4391 struct mii_ioctl_data *data = if_mii(ifr);
4392
4393 return netif_running(dev) ? rtl_xmii_ioctl(tp, data, cmd) : -ENODEV;
Francois Romieu8b4ab282008-11-19 22:05:25 -08004394}
4395
Bill Pembertonbaf63292012-12-03 09:23:28 -05004396static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004397{
4398 struct mdio_ops *ops = &tp->mdio_ops;
4399
4400 switch (tp->mac_version) {
4401 case RTL_GIGA_MAC_VER_27:
4402 ops->write = r8168dp_1_mdio_write;
4403 ops->read = r8168dp_1_mdio_read;
4404 break;
françois romieue6de30d2011-01-03 15:08:37 +00004405 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004406 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004407 ops->write = r8168dp_2_mdio_write;
4408 ops->read = r8168dp_2_mdio_read;
4409 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004410 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004411 ops->write = r8168g_mdio_write;
4412 ops->read = r8168g_mdio_read;
4413 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004414 default:
4415 ops->write = r8169_mdio_write;
4416 ops->read = r8169_mdio_read;
4417 break;
4418 }
4419}
4420
hayeswange2409d82013-03-31 17:02:04 +00004421static void rtl_speed_down(struct rtl8169_private *tp)
4422{
4423 u32 adv;
4424 int lpa;
4425
4426 rtl_writephy(tp, 0x1f, 0x0000);
4427 lpa = rtl_readphy(tp, MII_LPA);
4428
4429 if (lpa & (LPA_10HALF | LPA_10FULL))
4430 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4431 else if (lpa & (LPA_100HALF | LPA_100FULL))
4432 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4433 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4434 else
4435 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4436 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4437 (tp->mii.supports_gmii ?
4438 ADVERTISED_1000baseT_Half |
4439 ADVERTISED_1000baseT_Full : 0);
4440
4441 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4442 adv);
4443}
4444
David S. Miller1805b2f2011-10-24 18:18:09 -04004445static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4446{
David S. Miller1805b2f2011-10-24 18:18:09 -04004447 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004448 case RTL_GIGA_MAC_VER_25:
4449 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004450 case RTL_GIGA_MAC_VER_29:
4451 case RTL_GIGA_MAC_VER_30:
4452 case RTL_GIGA_MAC_VER_32:
4453 case RTL_GIGA_MAC_VER_33:
4454 case RTL_GIGA_MAC_VER_34:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004455 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004456 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04004457 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4458 break;
4459 default:
4460 break;
4461 }
4462}
4463
4464static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4465{
Heiner Kallweit6fcf9b12018-07-04 21:11:29 +02004466 if (!netif_running(tp->dev) || !__rtl8169_get_wol(tp))
David S. Miller1805b2f2011-10-24 18:18:09 -04004467 return false;
4468
hayeswange2409d82013-03-31 17:02:04 +00004469 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004470 rtl_wol_suspend_quirk(tp);
4471
4472 return true;
4473}
4474
françois romieu065c27c2011-01-03 15:08:12 +00004475static void r8168_phy_power_up(struct rtl8169_private *tp)
4476{
4477 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004478 switch (tp->mac_version) {
4479 case RTL_GIGA_MAC_VER_11:
4480 case RTL_GIGA_MAC_VER_12:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004481 case RTL_GIGA_MAC_VER_17 ... RTL_GIGA_MAC_VER_28:
hayeswang01dc7fe2011-03-21 01:50:28 +00004482 case RTL_GIGA_MAC_VER_31:
4483 rtl_writephy(tp, 0x0e, 0x0000);
4484 break;
4485 default:
4486 break;
4487 }
françois romieu065c27c2011-01-03 15:08:12 +00004488 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
David S. Millerb2d6cee2018-05-11 20:53:22 -04004489
4490 /* give MAC/PHY some time to resume */
4491 msleep(20);
françois romieu065c27c2011-01-03 15:08:12 +00004492}
4493
4494static void r8168_phy_power_down(struct rtl8169_private *tp)
4495{
4496 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004497 switch (tp->mac_version) {
4498 case RTL_GIGA_MAC_VER_32:
4499 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004500 case RTL_GIGA_MAC_VER_40:
4501 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004502 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4503 break;
4504
4505 case RTL_GIGA_MAC_VER_11:
4506 case RTL_GIGA_MAC_VER_12:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004507 case RTL_GIGA_MAC_VER_17 ... RTL_GIGA_MAC_VER_28:
hayeswang01dc7fe2011-03-21 01:50:28 +00004508 case RTL_GIGA_MAC_VER_31:
4509 rtl_writephy(tp, 0x0e, 0x0200);
4510 default:
4511 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4512 break;
4513 }
françois romieu065c27c2011-01-03 15:08:12 +00004514}
4515
4516static void r8168_pll_power_down(struct rtl8169_private *tp)
4517{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01004518 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004519 return;
4520
hayeswang01dc7fe2011-03-21 01:50:28 +00004521 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4522 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004523 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004524
David S. Miller1805b2f2011-10-24 18:18:09 -04004525 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004526 return;
françois romieu065c27c2011-01-03 15:08:12 +00004527
4528 r8168_phy_power_down(tp);
4529
4530 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004531 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004532 case RTL_GIGA_MAC_VER_37:
4533 case RTL_GIGA_MAC_VER_39:
4534 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004535 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004536 case RTL_GIGA_MAC_VER_45:
4537 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004538 case RTL_GIGA_MAC_VER_47:
4539 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004540 case RTL_GIGA_MAC_VER_50:
4541 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004542 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004543 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004544 case RTL_GIGA_MAC_VER_40:
4545 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004546 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004547 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004548 0xfc000000, ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004549 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004550 break;
françois romieu065c27c2011-01-03 15:08:12 +00004551 }
4552}
4553
4554static void r8168_pll_power_up(struct rtl8169_private *tp)
4555{
françois romieu065c27c2011-01-03 15:08:12 +00004556 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004557 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004558 case RTL_GIGA_MAC_VER_37:
4559 case RTL_GIGA_MAC_VER_39:
4560 case RTL_GIGA_MAC_VER_43:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004561 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004562 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004563 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004564 case RTL_GIGA_MAC_VER_45:
4565 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004566 case RTL_GIGA_MAC_VER_47:
4567 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004568 case RTL_GIGA_MAC_VER_50:
4569 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004570 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004571 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004572 case RTL_GIGA_MAC_VER_40:
4573 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004574 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004575 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004576 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004577 0x00000000, ERIAR_EXGMAC);
4578 break;
françois romieu065c27c2011-01-03 15:08:12 +00004579 }
4580
4581 r8168_phy_power_up(tp);
4582}
4583
françois romieu065c27c2011-01-03 15:08:12 +00004584static void rtl_pll_power_down(struct rtl8169_private *tp)
4585{
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004586 switch (tp->mac_version) {
4587 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4588 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4589 break;
4590 default:
4591 r8168_pll_power_down(tp);
4592 }
françois romieu065c27c2011-01-03 15:08:12 +00004593}
4594
4595static void rtl_pll_power_up(struct rtl8169_private *tp)
4596{
françois romieu065c27c2011-01-03 15:08:12 +00004597 switch (tp->mac_version) {
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004598 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4599 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
françois romieu065c27c2011-01-03 15:08:12 +00004600 break;
françois romieu065c27c2011-01-03 15:08:12 +00004601 default:
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004602 r8168_pll_power_up(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004603 }
4604}
4605
Hayes Wange542a222011-07-06 15:58:04 +08004606static void rtl_init_rxcfg(struct rtl8169_private *tp)
4607{
Hayes Wange542a222011-07-06 15:58:04 +08004608 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004609 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4610 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004611 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004612 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004613 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004614 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02004615 case RTL_GIGA_MAC_VER_35:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004616 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004617 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004618 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004619 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00004620 break;
Hayes Wange542a222011-07-06 15:58:04 +08004621 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004622 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004623 break;
4624 }
4625}
4626
Hayes Wang92fc43b2011-07-06 15:58:03 +08004627static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4628{
Timo Teräs9fba0812013-01-15 21:01:24 +00004629 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004630}
4631
Francois Romieud58d46b2011-05-03 16:38:29 +02004632static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4633{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004634 if (tp->jumbo_ops.enable) {
4635 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4636 tp->jumbo_ops.enable(tp);
4637 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4638 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004639}
4640
4641static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4642{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004643 if (tp->jumbo_ops.disable) {
4644 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4645 tp->jumbo_ops.disable(tp);
4646 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4647 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004648}
4649
4650static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4651{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004652 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4653 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004654 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004655}
4656
4657static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4658{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004659 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4660 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004661 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004662}
4663
4664static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4665{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004666 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004667}
4668
4669static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4670{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004671 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004672}
4673
4674static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4675{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004676 RTL_W8(tp, MaxTxPacketSize, 0x3f);
4677 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4678 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004679 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004680}
4681
4682static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4683{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004684 RTL_W8(tp, MaxTxPacketSize, 0x0c);
4685 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4686 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004687 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004688}
4689
4690static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4691{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004692 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01004693 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004694}
4695
4696static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4697{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004698 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004699 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004700}
4701
4702static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4703{
Francois Romieud58d46b2011-05-03 16:38:29 +02004704 r8168b_0_hw_jumbo_enable(tp);
4705
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004706 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004707}
4708
4709static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4710{
Francois Romieud58d46b2011-05-03 16:38:29 +02004711 r8168b_0_hw_jumbo_disable(tp);
4712
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004713 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004714}
4715
Bill Pembertonbaf63292012-12-03 09:23:28 -05004716static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02004717{
4718 struct jumbo_ops *ops = &tp->jumbo_ops;
4719
4720 switch (tp->mac_version) {
4721 case RTL_GIGA_MAC_VER_11:
4722 ops->disable = r8168b_0_hw_jumbo_disable;
4723 ops->enable = r8168b_0_hw_jumbo_enable;
4724 break;
4725 case RTL_GIGA_MAC_VER_12:
4726 case RTL_GIGA_MAC_VER_17:
4727 ops->disable = r8168b_1_hw_jumbo_disable;
4728 ops->enable = r8168b_1_hw_jumbo_enable;
4729 break;
4730 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4731 case RTL_GIGA_MAC_VER_19:
4732 case RTL_GIGA_MAC_VER_20:
4733 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4734 case RTL_GIGA_MAC_VER_22:
4735 case RTL_GIGA_MAC_VER_23:
4736 case RTL_GIGA_MAC_VER_24:
4737 case RTL_GIGA_MAC_VER_25:
4738 case RTL_GIGA_MAC_VER_26:
4739 ops->disable = r8168c_hw_jumbo_disable;
4740 ops->enable = r8168c_hw_jumbo_enable;
4741 break;
4742 case RTL_GIGA_MAC_VER_27:
4743 case RTL_GIGA_MAC_VER_28:
4744 ops->disable = r8168dp_hw_jumbo_disable;
4745 ops->enable = r8168dp_hw_jumbo_enable;
4746 break;
4747 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4748 case RTL_GIGA_MAC_VER_32:
4749 case RTL_GIGA_MAC_VER_33:
4750 case RTL_GIGA_MAC_VER_34:
4751 ops->disable = r8168e_hw_jumbo_disable;
4752 ops->enable = r8168e_hw_jumbo_enable;
4753 break;
4754
4755 /*
4756 * No action needed for jumbo frames with 8169.
4757 * No jumbo for 810x at all.
4758 */
Heiner Kallweit2a718832018-05-02 21:39:49 +02004759 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02004760 default:
4761 ops->disable = NULL;
4762 ops->enable = NULL;
4763 break;
4764 }
4765}
4766
Francois Romieuffc46952012-07-06 14:19:23 +02004767DECLARE_RTL_COND(rtl_chipcmd_cond)
4768{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004769 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02004770}
4771
Francois Romieu6f43adc2011-04-29 15:05:51 +02004772static void rtl_hw_reset(struct rtl8169_private *tp)
4773{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004774 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004775
Francois Romieuffc46952012-07-06 14:19:23 +02004776 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004777}
4778
Francois Romieub6ffd972011-06-17 17:00:05 +02004779static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4780{
4781 struct rtl_fw *rtl_fw;
4782 const char *name;
4783 int rc = -ENOMEM;
4784
4785 name = rtl_lookup_firmware_name(tp);
4786 if (!name)
4787 goto out_no_firmware;
4788
4789 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4790 if (!rtl_fw)
4791 goto err_warn;
4792
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004793 rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
Francois Romieub6ffd972011-06-17 17:00:05 +02004794 if (rc < 0)
4795 goto err_free;
4796
Francois Romieufd112f22011-06-18 00:10:29 +02004797 rc = rtl_check_firmware(tp, rtl_fw);
4798 if (rc < 0)
4799 goto err_release_firmware;
4800
Francois Romieub6ffd972011-06-17 17:00:05 +02004801 tp->rtl_fw = rtl_fw;
4802out:
4803 return;
4804
Francois Romieufd112f22011-06-18 00:10:29 +02004805err_release_firmware:
4806 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004807err_free:
4808 kfree(rtl_fw);
4809err_warn:
4810 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4811 name, rc);
4812out_no_firmware:
4813 tp->rtl_fw = NULL;
4814 goto out;
4815}
4816
François Romieu953a12c2011-04-24 17:38:48 +02004817static void rtl_request_firmware(struct rtl8169_private *tp)
4818{
Francois Romieub6ffd972011-06-17 17:00:05 +02004819 if (IS_ERR(tp->rtl_fw))
4820 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004821}
4822
Hayes Wang92fc43b2011-07-06 15:58:03 +08004823static void rtl_rx_close(struct rtl8169_private *tp)
4824{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004825 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004826}
4827
Francois Romieuffc46952012-07-06 14:19:23 +02004828DECLARE_RTL_COND(rtl_npq_cond)
4829{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004830 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02004831}
4832
4833DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4834{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004835 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02004836}
4837
françois romieue6de30d2011-01-03 15:08:37 +00004838static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839{
4840 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004841 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
Hayes Wang92fc43b2011-07-06 15:58:03 +08004843 rtl_rx_close(tp);
4844
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004845 switch (tp->mac_version) {
4846 case RTL_GIGA_MAC_VER_27:
4847 case RTL_GIGA_MAC_VER_28:
4848 case RTL_GIGA_MAC_VER_31:
Francois Romieuffc46952012-07-06 14:19:23 +02004849 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004850 break;
4851 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
4852 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004853 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02004854 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004855 break;
4856 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004857 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004858 udelay(100);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004859 break;
françois romieue6de30d2011-01-03 15:08:37 +00004860 }
4861
Hayes Wang92fc43b2011-07-06 15:58:03 +08004862 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863}
4864
Francois Romieu7f796d832007-06-11 23:04:41 +02004865static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004866{
Francois Romieu9cb427b2006-11-02 00:10:16 +01004867 /* Set DMA burst size and Interframe Gap Time */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004868 RTL_W32(tp, TxConfig, (TX_DMA_BURST << TxDMAShift) |
Francois Romieu9cb427b2006-11-02 00:10:16 +01004869 (InterFrameGap << TxInterFrameGapShift));
4870}
4871
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004872static void rtl_set_rx_max_size(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004874 /* Low hurts. Let's disable the filtering. */
4875 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
Francois Romieu07ce4062007-02-23 23:36:39 +01004876}
4877
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004878static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02004879{
4880 /*
4881 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4882 * register to be written before TxDescAddrLow to work.
4883 * Switching from MMIO to I/O access fixes the issue as well.
4884 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004885 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4886 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4887 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4888 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004889}
4890
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004891static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01004892{
Francois Romieu37441002011-06-17 22:58:54 +02004893 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004894 u32 mac_version;
4895 u32 clk;
4896 u32 val;
4897 } cfg2_info [] = {
4898 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4899 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4900 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4901 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004902 };
4903 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004904 unsigned int i;
4905 u32 clk;
4906
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004907 clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004908 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004909 if ((p->mac_version == mac_version) && (p->clk == clk)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004910 RTL_W32(tp, 0x7c, p->val);
Francois Romieu6dccd162007-02-13 23:38:05 +01004911 break;
4912 }
4913 }
4914}
4915
Francois Romieue6b763e2012-03-08 09:35:39 +01004916static void rtl_set_rx_mode(struct net_device *dev)
4917{
4918 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieue6b763e2012-03-08 09:35:39 +01004919 u32 mc_filter[2]; /* Multicast hash filter */
4920 int rx_mode;
4921 u32 tmp = 0;
4922
4923 if (dev->flags & IFF_PROMISC) {
4924 /* Unconditionally log net taps. */
4925 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4926 rx_mode =
4927 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4928 AcceptAllPhys;
4929 mc_filter[1] = mc_filter[0] = 0xffffffff;
4930 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4931 (dev->flags & IFF_ALLMULTI)) {
4932 /* Too many to filter perfectly -- accept all multicasts. */
4933 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4934 mc_filter[1] = mc_filter[0] = 0xffffffff;
4935 } else {
4936 struct netdev_hw_addr *ha;
4937
4938 rx_mode = AcceptBroadcast | AcceptMyPhys;
4939 mc_filter[1] = mc_filter[0] = 0;
4940 netdev_for_each_mc_addr(ha, dev) {
4941 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4942 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4943 rx_mode |= AcceptMulticast;
4944 }
4945 }
4946
4947 if (dev->features & NETIF_F_RXALL)
4948 rx_mode |= (AcceptErr | AcceptRunt);
4949
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004950 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Francois Romieue6b763e2012-03-08 09:35:39 +01004951
4952 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4953 u32 data = mc_filter[0];
4954
4955 mc_filter[0] = swab32(mc_filter[1]);
4956 mc_filter[1] = swab32(data);
4957 }
4958
Nathan Walp04817762012-11-01 12:08:47 +00004959 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4960 mc_filter[1] = mc_filter[0] = 0xffffffff;
4961
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004962 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
4963 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01004964
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004965 RTL_W32(tp, RxConfig, tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01004966}
4967
Heiner Kallweit52f85602018-05-19 10:29:33 +02004968static void rtl_hw_start(struct rtl8169_private *tp)
4969{
4970 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4971
4972 tp->hw_start(tp);
4973
4974 rtl_set_rx_max_size(tp);
4975 rtl_set_rx_tx_desc_registers(tp);
4976 rtl_set_rx_tx_config_registers(tp);
4977 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4978
4979 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4980 RTL_R8(tp, IntrMask);
4981 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
4982 rtl_set_rx_mode(tp->dev);
4983 /* no early-rx interrupts */
4984 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
4985 rtl_irq_enable_all(tp);
4986}
4987
Heiner Kallweit61cb5322018-04-17 23:27:38 +02004988static void rtl_hw_start_8169(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01004989{
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004990 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02004991 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004992
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004993 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004995 tp->cp_cmd |= PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004996
Francois Romieucecb5fd2011-04-01 10:21:07 +02004997 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4998 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Heiner Kallweit49d17512018-06-28 20:36:15 +02004999 netif_dbg(tp, drv, tp->dev,
5000 "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005001 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 }
5003
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005004 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005005
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005006 rtl8169_set_magic_reg(tp, tp->mac_version);
Francois Romieu6dccd162007-02-13 23:38:05 +01005007
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 /*
5009 * Undocumented corner. Supposedly:
5010 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5011 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005012 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005014 RTL_W32(tp, RxMissed, 0);
Francois Romieu07ce4062007-02-23 23:36:39 +01005015}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
Francois Romieuffc46952012-07-06 14:19:23 +02005017DECLARE_RTL_COND(rtl_csiar_cond)
5018{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005019 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02005020}
5021
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005022static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005023{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005024 u32 func = PCI_FUNC(tp->pci_dev->devfn);
5025
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005026 RTL_W32(tp, CSIDR, value);
5027 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005028 CSIAR_BYTE_ENABLE | func << 16);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005029
Francois Romieuffc46952012-07-06 14:19:23 +02005030 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005031}
5032
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005033static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005034{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005035 u32 func = PCI_FUNC(tp->pci_dev->devfn);
5036
5037 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
5038 CSIAR_BYTE_ENABLE);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005039
Francois Romieuffc46952012-07-06 14:19:23 +02005040 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005041 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005042}
5043
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005044static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005045{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005046 struct pci_dev *pdev = tp->pci_dev;
5047 u32 csi;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005048
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005049 /* According to Realtek the value at config space address 0x070f
5050 * controls the L0s/L1 entrance latency. We try standard ECAM access
5051 * first and if it fails fall back to CSI.
5052 */
5053 if (pdev->cfg_size > 0x070f &&
5054 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
5055 return;
5056
5057 netdev_notice_once(tp->dev,
5058 "No native access to PCI extended config space, falling back to CSI\n");
5059 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5060 rtl_csi_write(tp, 0x070c, csi | val << 24);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005061}
5062
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005063static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
hayeswang45dd95c2013-07-08 17:09:01 +08005064{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005065 rtl_csi_access_enable(tp, 0x27);
Francois Romieudacf8152008-08-02 20:44:13 +02005066}
5067
5068struct ephy_info {
5069 unsigned int offset;
5070 u16 mask;
5071 u16 bits;
5072};
5073
Francois Romieufdf6fc02012-07-06 22:40:38 +02005074static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5075 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005076{
5077 u16 w;
5078
5079 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005080 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5081 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005082 e++;
5083 }
5084}
5085
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005086static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02005087{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005088 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005089 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005090}
5091
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005092static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005093{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005094 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005095 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005096}
5097
hayeswangb51ecea2014-07-09 14:52:51 +08005098static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5099{
hayeswangb51ecea2014-07-09 14:52:51 +08005100 u8 data;
5101
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005102 data = RTL_R8(tp, Config3);
hayeswangb51ecea2014-07-09 14:52:51 +08005103
5104 if (enable)
5105 data |= Rdy_to_L23;
5106 else
5107 data &= ~Rdy_to_L23;
5108
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005109 RTL_W8(tp, Config3, data);
hayeswangb51ecea2014-07-09 14:52:51 +08005110}
5111
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005112static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
5113{
5114 if (enable) {
5115 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
5116 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
5117 } else {
5118 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5119 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
5120 }
5121}
5122
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005123static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005124{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005125 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005126
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005127 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005128 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieub726e492008-06-28 12:22:59 +02005129
françois romieufaf1e782013-02-27 13:01:57 +00005130 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005131 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00005132 PCI_EXP_DEVCTL_NOSNOOP_EN);
5133 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005134}
5135
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005136static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005137{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005138 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005139
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005140 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005141
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005142 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005143}
5144
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005145static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005146{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005147 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02005148
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005149 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005150
françois romieufaf1e782013-02-27 13:01:57 +00005151 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005152 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02005153
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005154 rtl_disable_clock_request(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005155
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005156 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005157 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu219a1e92008-06-28 11:58:39 +02005158}
5159
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005160static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005161{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005162 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005163 { 0x01, 0, 0x0001 },
5164 { 0x02, 0x0800, 0x1000 },
5165 { 0x03, 0, 0x0042 },
5166 { 0x06, 0x0080, 0x0000 },
5167 { 0x07, 0, 0x2000 }
5168 };
5169
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005170 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005171
Francois Romieufdf6fc02012-07-06 22:40:38 +02005172 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005173
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005174 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005175}
5176
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005177static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005178{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005179 rtl_set_def_aspm_entry_latency(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005180
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005181 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02005182
françois romieufaf1e782013-02-27 13:01:57 +00005183 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005184 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieuef3386f2008-06-29 12:24:30 +02005185
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005186 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005187 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieuef3386f2008-06-29 12:24:30 +02005188}
5189
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005190static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005191{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005192 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005193
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005194 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005195
5196 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005197 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005198
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005199 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005200
françois romieufaf1e782013-02-27 13:01:57 +00005201 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005202 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005203
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005204 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005205 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005206}
5207
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005208static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005209{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005210 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005211 { 0x02, 0x0800, 0x1000 },
5212 { 0x03, 0, 0x0002 },
5213 { 0x06, 0x0080, 0x0000 }
5214 };
5215
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005216 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005217
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005218 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02005219
Francois Romieufdf6fc02012-07-06 22:40:38 +02005220 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005221
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005222 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005223}
5224
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005225static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005226{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005227 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005228 { 0x01, 0, 0x0001 },
5229 { 0x03, 0x0400, 0x0220 }
5230 };
5231
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005232 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005233
Francois Romieufdf6fc02012-07-06 22:40:38 +02005234 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005235
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005236 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005237}
5238
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005239static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005240{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005241 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005242}
5243
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005244static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005245{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005246 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005247
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005248 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005249}
5250
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005251static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005252{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005253 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005254
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005255 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005256
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005257 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005258
françois romieufaf1e782013-02-27 13:01:57 +00005259 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005260 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02005261
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005262 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005263 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu5b538df2008-07-20 16:22:45 +02005264}
5265
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005266static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005267{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005268 rtl_set_def_aspm_entry_latency(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005269
françois romieufaf1e782013-02-27 13:01:57 +00005270 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005271 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang4804b3b2011-03-21 01:50:29 +00005272
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005273 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang4804b3b2011-03-21 01:50:29 +00005274
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005275 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005276}
5277
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005278static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005279{
5280 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005281 { 0x0b, 0x0000, 0x0048 },
5282 { 0x19, 0x0020, 0x0050 },
5283 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00005284 };
françois romieue6de30d2011-01-03 15:08:37 +00005285
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005286 rtl_set_def_aspm_entry_latency(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005287
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005288 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00005289
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005290 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
françois romieue6de30d2011-01-03 15:08:37 +00005291
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005292 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00005293
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005294 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005295}
5296
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005297static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005298{
Hayes Wang70090422011-07-06 15:58:06 +08005299 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005300 { 0x00, 0x0200, 0x0100 },
5301 { 0x00, 0x0000, 0x0004 },
5302 { 0x06, 0x0002, 0x0001 },
5303 { 0x06, 0x0000, 0x0030 },
5304 { 0x07, 0x0000, 0x2000 },
5305 { 0x00, 0x0000, 0x0020 },
5306 { 0x03, 0x5800, 0x2000 },
5307 { 0x03, 0x0000, 0x0001 },
5308 { 0x01, 0x0800, 0x1000 },
5309 { 0x07, 0x0000, 0x4000 },
5310 { 0x1e, 0x0000, 0x2000 },
5311 { 0x19, 0xffff, 0xfe6c },
5312 { 0x0a, 0x0000, 0x0040 }
5313 };
5314
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005315 rtl_set_def_aspm_entry_latency(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005316
Francois Romieufdf6fc02012-07-06 22:40:38 +02005317 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005318
françois romieufaf1e782013-02-27 13:01:57 +00005319 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005320 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00005321
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005322 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang01dc7fe2011-03-21 01:50:28 +00005323
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005324 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005325
5326 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005327 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
5328 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005329
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005330 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005331}
5332
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005333static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005334{
5335 static const struct ephy_info e_info_8168e_2[] = {
5336 { 0x09, 0x0000, 0x0080 },
5337 { 0x19, 0x0000, 0x0224 }
5338 };
5339
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005340 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005341
Francois Romieufdf6fc02012-07-06 22:40:38 +02005342 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005343
françois romieufaf1e782013-02-27 13:01:57 +00005344 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005345 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08005346
Francois Romieufdf6fc02012-07-06 22:40:38 +02005347 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5348 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5349 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5350 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5351 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5352 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005353 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5354 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005355
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005356 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005357
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005358 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005359
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005360 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5361 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08005362
5363 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005364 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang70090422011-07-06 15:58:06 +08005365
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005366 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5367 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5368 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Heiner Kallweitaa1e7d22018-06-23 09:53:00 +02005369
5370 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang70090422011-07-06 15:58:06 +08005371}
5372
Hayes Wang5f886e02012-03-30 14:33:03 +08005373static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005374{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005375 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005376
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005377 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08005378
Francois Romieufdf6fc02012-07-06 22:40:38 +02005379 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5380 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5381 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5382 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005383 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5384 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5385 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5386 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005387 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5388 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005389
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005390 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc2218922011-09-06 16:55:18 +08005391
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005392 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005393
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005394 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5395 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5396 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5397 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5398 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005399}
5400
Hayes Wang5f886e02012-03-30 14:33:03 +08005401static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5402{
Hayes Wang5f886e02012-03-30 14:33:03 +08005403 static const struct ephy_info e_info_8168f_1[] = {
5404 { 0x06, 0x00c0, 0x0020 },
5405 { 0x08, 0x0001, 0x0002 },
5406 { 0x09, 0x0000, 0x0080 },
5407 { 0x19, 0x0000, 0x0224 }
5408 };
5409
5410 rtl_hw_start_8168f(tp);
5411
Francois Romieufdf6fc02012-07-06 22:40:38 +02005412 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005413
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005414 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005415
5416 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005417 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang5f886e02012-03-30 14:33:03 +08005418}
5419
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005420static void rtl_hw_start_8411(struct rtl8169_private *tp)
5421{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005422 static const struct ephy_info e_info_8168f_1[] = {
5423 { 0x06, 0x00c0, 0x0020 },
5424 { 0x0f, 0xffff, 0x5200 },
5425 { 0x1e, 0x0000, 0x4000 },
5426 { 0x19, 0x0000, 0x0224 }
5427 };
5428
5429 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08005430 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005431
Francois Romieufdf6fc02012-07-06 22:40:38 +02005432 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005433
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005434 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005435}
5436
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005437static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08005438{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005439 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
hayeswangbeb330a2013-04-01 22:23:39 +00005440
Hayes Wangc5583862012-07-02 17:23:22 +08005441 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5442 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5443 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5444 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5445
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005446 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08005447
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005448 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08005449
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005450 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5451 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00005452 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005453
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005454 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5455 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc5583862012-07-02 17:23:22 +08005456
5457 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5458 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5459
5460 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005461 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wangc5583862012-07-02 17:23:22 +08005462
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005463 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5464 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005465
5466 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08005467}
5468
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005469static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5470{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005471 static const struct ephy_info e_info_8168g_1[] = {
5472 { 0x00, 0x0000, 0x0008 },
5473 { 0x0c, 0x37d0, 0x0820 },
5474 { 0x1e, 0x0000, 0x0001 },
5475 { 0x19, 0x8000, 0x0000 }
5476 };
5477
5478 rtl_hw_start_8168g(tp);
5479
5480 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005481 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005482 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005483 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005484}
5485
hayeswang57538c42013-04-01 22:23:40 +00005486static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5487{
hayeswang57538c42013-04-01 22:23:40 +00005488 static const struct ephy_info e_info_8168g_2[] = {
5489 { 0x00, 0x0000, 0x0008 },
5490 { 0x0c, 0x3df0, 0x0200 },
5491 { 0x19, 0xffff, 0xfc00 },
5492 { 0x1e, 0xffff, 0x20eb }
5493 };
5494
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005495 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00005496
5497 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005498 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5499 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang57538c42013-04-01 22:23:40 +00005500 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5501}
5502
hayeswang45dd95c2013-07-08 17:09:01 +08005503static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5504{
hayeswang45dd95c2013-07-08 17:09:01 +08005505 static const struct ephy_info e_info_8411_2[] = {
5506 { 0x00, 0x0000, 0x0008 },
5507 { 0x0c, 0x3df0, 0x0200 },
5508 { 0x0f, 0xffff, 0x5200 },
5509 { 0x19, 0x0020, 0x0000 },
5510 { 0x1e, 0x0000, 0x2000 }
5511 };
5512
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005513 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08005514
5515 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005516 rtl_hw_aspm_clkreq_enable(tp, false);
hayeswang45dd95c2013-07-08 17:09:01 +08005517 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005518 rtl_hw_aspm_clkreq_enable(tp, true);
hayeswang45dd95c2013-07-08 17:09:01 +08005519}
5520
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005521static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
5522{
Andrzej Hajda72521ea2015-09-24 16:00:24 +02005523 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005524 u32 data;
5525 static const struct ephy_info e_info_8168h_1[] = {
5526 { 0x1e, 0x0800, 0x0001 },
5527 { 0x1d, 0x0000, 0x0800 },
5528 { 0x05, 0xffff, 0x2089 },
5529 { 0x06, 0xffff, 0x5881 },
5530 { 0x04, 0xffff, 0x154a },
5531 { 0x01, 0xffff, 0x068b }
5532 };
5533
5534 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005535 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005536 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
5537
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005538 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005539
5540 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5541 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5542 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5543 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5544
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005545 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005546
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005547 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005548
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005549 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5550 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005551
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005552 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005553
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005554 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005555
5556 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5557
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005558 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5559 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005560
5561 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5562 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5563
5564 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005565 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005566
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005567 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5568 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005569
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005570 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005571
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005572 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005573
5574 rtl_pcie_state_l2l3_enable(tp, false);
5575
5576 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08005577 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005578 rtl_writephy(tp, 0x1f, 0x0000);
5579 if (rg_saw_cnt > 0) {
5580 u16 sw_cnt_1ms_ini;
5581
5582 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
5583 sw_cnt_1ms_ini &= 0x0fff;
5584 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005585 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005586 data |= sw_cnt_1ms_ini;
5587 r8168_mac_ocp_write(tp, 0xd412, data);
5588 }
5589
5590 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005591 data &= ~0xf0;
5592 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005593 r8168_mac_ocp_write(tp, 0xe056, data);
5594
5595 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005596 data &= ~0x6000;
5597 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005598 r8168_mac_ocp_write(tp, 0xe052, data);
5599
5600 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005601 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005602 data |= 0x017f;
5603 r8168_mac_ocp_write(tp, 0xe0d6, data);
5604
5605 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005606 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005607 data |= 0x047f;
5608 r8168_mac_ocp_write(tp, 0xd420, data);
5609
5610 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
5611 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
5612 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
5613 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005614
5615 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005616}
5617
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005618static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
5619{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08005620 rtl8168ep_stop_cmac(tp);
5621
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005622 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005623
5624 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5625 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
5626 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
5627 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5628
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005629 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005630
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005631 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005632
5633 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5634 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5635
5636 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
5637
5638 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5639
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005640 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5641 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005642
5643 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5644 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5645
5646 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005647 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005648
5649 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5650
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005651 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005652
5653 rtl_pcie_state_l2l3_enable(tp, false);
5654}
5655
5656static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
5657{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005658 static const struct ephy_info e_info_8168ep_1[] = {
5659 { 0x00, 0xffff, 0x10ab },
5660 { 0x06, 0xffff, 0xf030 },
5661 { 0x08, 0xffff, 0x2006 },
5662 { 0x0d, 0xffff, 0x1666 },
5663 { 0x0c, 0x3ff0, 0x0000 }
5664 };
5665
5666 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005667 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005668 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
5669
5670 rtl_hw_start_8168ep(tp);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005671
5672 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005673}
5674
5675static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
5676{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005677 static const struct ephy_info e_info_8168ep_2[] = {
5678 { 0x00, 0xffff, 0x10a3 },
5679 { 0x19, 0xffff, 0xfc00 },
5680 { 0x1e, 0xffff, 0x20ea }
5681 };
5682
5683 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005684 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005685 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
5686
5687 rtl_hw_start_8168ep(tp);
5688
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005689 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5690 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005691
5692 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005693}
5694
5695static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
5696{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005697 u32 data;
5698 static const struct ephy_info e_info_8168ep_3[] = {
5699 { 0x00, 0xffff, 0x10a3 },
5700 { 0x19, 0xffff, 0x7c00 },
5701 { 0x1e, 0xffff, 0x20eb },
5702 { 0x0d, 0xffff, 0x1666 }
5703 };
5704
5705 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005706 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005707 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
5708
5709 rtl_hw_start_8168ep(tp);
5710
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005711 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5712 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005713
5714 data = r8168_mac_ocp_read(tp, 0xd3e2);
5715 data &= 0xf000;
5716 data |= 0x0271;
5717 r8168_mac_ocp_write(tp, 0xd3e2, data);
5718
5719 data = r8168_mac_ocp_read(tp, 0xd3e4);
5720 data &= 0xff00;
5721 r8168_mac_ocp_write(tp, 0xd3e4, data);
5722
5723 data = r8168_mac_ocp_read(tp, 0xe860);
5724 data |= 0x0080;
5725 r8168_mac_ocp_write(tp, 0xe860, data);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005726
5727 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005728}
5729
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005730static void rtl_hw_start_8168(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005731{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005732 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02005733
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005734 tp->cp_cmd &= ~INTT_MASK;
5735 tp->cp_cmd |= PktCntrDisable | INTT_1;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005736 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu2dd99532007-06-11 23:22:52 +02005737
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005738 RTL_W16(tp, IntrMitigate, 0x5151);
Francois Romieu0e485152007-02-20 00:00:26 +01005739
5740 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00005741 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005742 tp->event_slow |= RxFIFOOver | PCSTimeout;
5743 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01005744 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005745
Francois Romieu219a1e92008-06-28 11:58:39 +02005746 switch (tp->mac_version) {
5747 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005748 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005749 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005750
5751 case RTL_GIGA_MAC_VER_12:
5752 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005753 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005754 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005755
5756 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005757 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005758 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005759
5760 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005761 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005762 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005763
5764 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005765 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005766 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005767
Francois Romieu197ff762008-06-28 13:16:02 +02005768 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005769 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005770 break;
Francois Romieu197ff762008-06-28 13:16:02 +02005771
Francois Romieu6fb07052008-06-29 11:54:28 +02005772 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005773 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005774 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02005775
Francois Romieuef3386f2008-06-29 12:24:30 +02005776 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005777 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005778 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02005779
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005780 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005781 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005782 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005783
Francois Romieu5b538df2008-07-20 16:22:45 +02005784 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00005785 case RTL_GIGA_MAC_VER_26:
5786 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005787 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005788 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02005789
françois romieue6de30d2011-01-03 15:08:37 +00005790 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005791 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005792 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005793
hayeswang4804b3b2011-03-21 01:50:29 +00005794 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005795 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005796 break;
5797
hayeswang01dc7fe2011-03-21 01:50:28 +00005798 case RTL_GIGA_MAC_VER_32:
5799 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005800 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005801 break;
5802 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005803 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005804 break;
françois romieue6de30d2011-01-03 15:08:37 +00005805
Hayes Wangc2218922011-09-06 16:55:18 +08005806 case RTL_GIGA_MAC_VER_35:
5807 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005808 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005809 break;
5810
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005811 case RTL_GIGA_MAC_VER_38:
5812 rtl_hw_start_8411(tp);
5813 break;
5814
Hayes Wangc5583862012-07-02 17:23:22 +08005815 case RTL_GIGA_MAC_VER_40:
5816 case RTL_GIGA_MAC_VER_41:
5817 rtl_hw_start_8168g_1(tp);
5818 break;
hayeswang57538c42013-04-01 22:23:40 +00005819 case RTL_GIGA_MAC_VER_42:
5820 rtl_hw_start_8168g_2(tp);
5821 break;
Hayes Wangc5583862012-07-02 17:23:22 +08005822
hayeswang45dd95c2013-07-08 17:09:01 +08005823 case RTL_GIGA_MAC_VER_44:
5824 rtl_hw_start_8411_2(tp);
5825 break;
5826
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005827 case RTL_GIGA_MAC_VER_45:
5828 case RTL_GIGA_MAC_VER_46:
5829 rtl_hw_start_8168h_1(tp);
5830 break;
5831
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005832 case RTL_GIGA_MAC_VER_49:
5833 rtl_hw_start_8168ep_1(tp);
5834 break;
5835
5836 case RTL_GIGA_MAC_VER_50:
5837 rtl_hw_start_8168ep_2(tp);
5838 break;
5839
5840 case RTL_GIGA_MAC_VER_51:
5841 rtl_hw_start_8168ep_3(tp);
5842 break;
5843
Francois Romieu219a1e92008-06-28 11:58:39 +02005844 default:
Heiner Kallweit49d17512018-06-28 20:36:15 +02005845 netif_err(tp, drv, tp->dev,
5846 "unknown chipset (mac_version = %d)\n",
5847 tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00005848 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005849 }
Francois Romieu07ce4062007-02-23 23:36:39 +01005850}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005852static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005853{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005854 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005855 { 0x01, 0, 0x6e65 },
5856 { 0x02, 0, 0x091f },
5857 { 0x03, 0, 0xc2f9 },
5858 { 0x06, 0, 0xafb5 },
5859 { 0x07, 0, 0x0e00 },
5860 { 0x19, 0, 0xec80 },
5861 { 0x01, 0, 0x2e65 },
5862 { 0x01, 0, 0x6e65 }
5863 };
5864 u8 cfg1;
5865
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005866 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005867
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005868 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005869
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005870 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005871
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005872 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02005873 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005874 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005875
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005876 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005877 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005878 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005879
Francois Romieufdf6fc02012-07-06 22:40:38 +02005880 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02005881}
5882
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005883static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005884{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005885 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005886
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005887 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005888
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005889 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
5890 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005891}
5892
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005893static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005894{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005895 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005896
Francois Romieufdf6fc02012-07-06 22:40:38 +02005897 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005898}
5899
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005900static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005901{
5902 static const struct ephy_info e_info_8105e_1[] = {
5903 { 0x07, 0, 0x4000 },
5904 { 0x19, 0, 0x0200 },
5905 { 0x19, 0, 0x0020 },
5906 { 0x1e, 0, 0x2000 },
5907 { 0x03, 0, 0x0001 },
5908 { 0x19, 0, 0x0100 },
5909 { 0x19, 0, 0x0004 },
5910 { 0x0a, 0, 0x0020 }
5911 };
5912
Francois Romieucecb5fd2011-04-01 10:21:07 +02005913 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005914 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005915
Francois Romieucecb5fd2011-04-01 10:21:07 +02005916 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005917 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005918
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005919 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5920 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005921
Francois Romieufdf6fc02012-07-06 22:40:38 +02005922 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08005923
5924 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005925}
5926
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005927static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005928{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005929 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005930 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005931}
5932
Hayes Wang7e18dca2012-03-30 14:33:02 +08005933static void rtl_hw_start_8402(struct rtl8169_private *tp)
5934{
Hayes Wang7e18dca2012-03-30 14:33:02 +08005935 static const struct ephy_info e_info_8402[] = {
5936 { 0x19, 0xffff, 0xff64 },
5937 { 0x1e, 0, 0x4000 }
5938 };
5939
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005940 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005941
5942 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005943 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005944
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005945 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5946 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005947
Francois Romieufdf6fc02012-07-06 22:40:38 +02005948 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08005949
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005950 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005951
Francois Romieufdf6fc02012-07-06 22:40:38 +02005952 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5953 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005954 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5955 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005956 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5957 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005958 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005959
5960 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005961}
5962
Hayes Wang5598bfe2012-07-02 17:23:21 +08005963static void rtl_hw_start_8106(struct rtl8169_private *tp)
5964{
Hayes Wang5598bfe2012-07-02 17:23:21 +08005965 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005966 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005967
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005968 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5969 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5970 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08005971
5972 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005973}
5974
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005975static void rtl_hw_start_8101(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005976{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005977 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5978 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00005979
Francois Romieucecb5fd2011-04-01 10:21:07 +02005980 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08005981 tp->mac_version == RTL_GIGA_MAC_VER_16)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005982 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06005983 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02005984
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005985 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00005986
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005987 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005988 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
hayeswang1a964642013-04-01 22:23:41 +00005989
Francois Romieu2857ffb2008-08-02 21:08:49 +02005990 switch (tp->mac_version) {
5991 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005992 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005993 break;
5994
5995 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005996 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005997 break;
5998
5999 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006000 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006001 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006002
6003 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006004 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006005 break;
6006 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006007 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006008 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006009
6010 case RTL_GIGA_MAC_VER_37:
6011 rtl_hw_start_8402(tp);
6012 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006013
6014 case RTL_GIGA_MAC_VER_39:
6015 rtl_hw_start_8106(tp);
6016 break;
hayeswang58152cd2013-04-01 22:23:42 +00006017 case RTL_GIGA_MAC_VER_43:
6018 rtl_hw_start_8168g_2(tp);
6019 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006020 case RTL_GIGA_MAC_VER_47:
6021 case RTL_GIGA_MAC_VER_48:
6022 rtl_hw_start_8168h_1(tp);
6023 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006024 }
6025
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006026 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027}
6028
6029static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6030{
Francois Romieud58d46b2011-05-03 16:38:29 +02006031 struct rtl8169_private *tp = netdev_priv(dev);
6032
Francois Romieud58d46b2011-05-03 16:38:29 +02006033 if (new_mtu > ETH_DATA_LEN)
6034 rtl_hw_jumbo_enable(tp);
6035 else
6036 rtl_hw_jumbo_disable(tp);
6037
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006039 netdev_update_features(dev);
6040
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006041 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042}
6043
6044static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6045{
Al Viro95e09182007-12-22 18:55:39 +00006046 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6048}
6049
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006050static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6051 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052{
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006053 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
6054 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006055
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006056 kfree(*data_buff);
6057 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 rtl8169_make_unusable_by_asic(desc);
6059}
6060
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006061static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062{
6063 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6064
Alexander Duycka0750132014-12-11 15:02:17 -08006065 /* Force memory writes to complete before releasing descriptor */
6066 dma_wmb();
6067
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006068 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069}
6070
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006071static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006073 return (void *)ALIGN((long)data, 16);
6074}
6075
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006076static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6077 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006078{
6079 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 dma_addr_t mapping;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006081 struct device *d = tp_to_dev(tp);
Heiner Kallweitd3b404c2018-04-17 23:22:14 +02006082 int node = dev_to_node(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006084 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006085 if (!data)
6086 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006087
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006088 if (rtl8169_align(data) != data) {
6089 kfree(data);
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006090 data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006091 if (!data)
6092 return NULL;
6093 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006094
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006095 mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006096 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006097 if (unlikely(dma_mapping_error(d, mapping))) {
6098 if (net_ratelimit())
6099 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006100 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102
Heiner Kallweitd731af72018-04-17 23:26:41 +02006103 desc->addr = cpu_to_le64(mapping);
6104 rtl8169_mark_to_asic(desc);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006105 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006106
6107err_out:
6108 kfree(data);
6109 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110}
6111
6112static void rtl8169_rx_clear(struct rtl8169_private *tp)
6113{
Francois Romieu07d3f512007-02-21 22:40:46 +01006114 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115
6116 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006117 if (tp->Rx_databuff[i]) {
6118 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 tp->RxDescArray + i);
6120 }
6121 }
6122}
6123
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006124static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006126 desc->opts1 |= cpu_to_le32(RingEnd);
6127}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006128
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006129static int rtl8169_rx_fill(struct rtl8169_private *tp)
6130{
6131 unsigned int i;
6132
6133 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006134 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006135
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006136 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006137 if (!data) {
6138 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006139 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006140 }
6141 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006144 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6145 return 0;
6146
6147err_out:
6148 rtl8169_rx_clear(tp);
6149 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150}
6151
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006152static int rtl8169_init_ring(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 rtl8169_init_ring_indexes(tp);
6155
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006156 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
6157 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006159 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160}
6161
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006162static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 struct TxDesc *desc)
6164{
6165 unsigned int len = tx_skb->len;
6166
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006167 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6168
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 desc->opts1 = 0x00;
6170 desc->opts2 = 0x00;
6171 desc->addr = 0x00;
6172 tx_skb->len = 0;
6173}
6174
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006175static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6176 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177{
6178 unsigned int i;
6179
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006180 for (i = 0; i < n; i++) {
6181 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 struct ring_info *tx_skb = tp->tx_skb + entry;
6183 unsigned int len = tx_skb->len;
6184
6185 if (len) {
6186 struct sk_buff *skb = tx_skb->skb;
6187
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006188 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 tp->TxDescArray + entry);
6190 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006191 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 tx_skb->skb = NULL;
6193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 }
6195 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006196}
6197
6198static void rtl8169_tx_clear(struct rtl8169_private *tp)
6199{
6200 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 tp->cur_tx = tp->dirty_tx = 0;
6202}
6203
Francois Romieu4422bcd2012-01-26 11:23:32 +01006204static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205{
David Howellsc4028952006-11-22 14:57:56 +00006206 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01006207 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208
Francois Romieuda78dbf2012-01-26 14:18:23 +01006209 napi_disable(&tp->napi);
6210 netif_stop_queue(dev);
6211 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212
françois romieuc7c2c392011-12-04 20:30:52 +00006213 rtl8169_hw_reset(tp);
6214
Francois Romieu56de4142011-03-15 17:29:31 +01006215 for (i = 0; i < NUM_RX_DESC; i++)
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006216 rtl8169_mark_to_asic(tp->RxDescArray + i);
Francois Romieu56de4142011-03-15 17:29:31 +01006217
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00006219 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
Francois Romieuda78dbf2012-01-26 14:18:23 +01006221 napi_enable(&tp->napi);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006222 rtl_hw_start(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01006223 netif_wake_queue(dev);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006224 rtl8169_check_link_status(dev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225}
6226
6227static void rtl8169_tx_timeout(struct net_device *dev)
6228{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006229 struct rtl8169_private *tp = netdev_priv(dev);
6230
6231 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232}
6233
6234static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07006235 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236{
6237 struct skb_shared_info *info = skb_shinfo(skb);
6238 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006239 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006240 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241
6242 entry = tp->cur_tx;
6243 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006244 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 dma_addr_t mapping;
6246 u32 status, len;
6247 void *addr;
6248
6249 entry = (entry + 1) % NUM_TX_DESC;
6250
6251 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00006252 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00006253 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006254 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006255 if (unlikely(dma_mapping_error(d, mapping))) {
6256 if (net_ratelimit())
6257 netif_err(tp, drv, tp->dev,
6258 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006259 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261
Francois Romieucecb5fd2011-04-01 10:21:07 +02006262 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006263 status = opts[0] | len |
6264 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265
6266 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07006267 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 txd->addr = cpu_to_le64(mapping);
6269
6270 tp->tx_skb[entry].len = len;
6271 }
6272
6273 if (cur_frag) {
6274 tp->tx_skb[entry].skb = skb;
6275 txd->opts1 |= cpu_to_le32(LastFrag);
6276 }
6277
6278 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006279
6280err_out:
6281 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6282 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283}
6284
françois romieub423e9a2013-05-18 01:24:46 +00006285static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6286{
6287 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6288}
6289
hayeswange9746042014-07-11 16:25:58 +08006290static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6291 struct net_device *dev);
6292/* r8169_csum_workaround()
6293 * The hw limites the value the transport offset. When the offset is out of the
6294 * range, calculate the checksum by sw.
6295 */
6296static void r8169_csum_workaround(struct rtl8169_private *tp,
6297 struct sk_buff *skb)
6298{
6299 if (skb_shinfo(skb)->gso_size) {
6300 netdev_features_t features = tp->dev->features;
6301 struct sk_buff *segs, *nskb;
6302
6303 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6304 segs = skb_gso_segment(skb, features);
6305 if (IS_ERR(segs) || !segs)
6306 goto drop;
6307
6308 do {
6309 nskb = segs;
6310 segs = segs->next;
6311 nskb->next = NULL;
6312 rtl8169_start_xmit(nskb, tp->dev);
6313 } while (segs);
6314
Alexander Duyckeb781392015-05-01 10:34:44 -07006315 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006316 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6317 if (skb_checksum_help(skb) < 0)
6318 goto drop;
6319
6320 rtl8169_start_xmit(skb, tp->dev);
6321 } else {
6322 struct net_device_stats *stats;
6323
6324drop:
6325 stats = &tp->dev->stats;
6326 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07006327 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006328 }
6329}
6330
6331/* msdn_giant_send_check()
6332 * According to the document of microsoft, the TCP Pseudo Header excludes the
6333 * packet length for IPv6 TCP large packets.
6334 */
6335static int msdn_giant_send_check(struct sk_buff *skb)
6336{
6337 const struct ipv6hdr *ipv6h;
6338 struct tcphdr *th;
6339 int ret;
6340
6341 ret = skb_cow_head(skb, 0);
6342 if (ret)
6343 return ret;
6344
6345 ipv6h = ipv6_hdr(skb);
6346 th = tcp_hdr(skb);
6347
6348 th->check = 0;
6349 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6350
6351 return ret;
6352}
6353
hayeswang5888d3f2014-07-11 16:25:56 +08006354static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6355 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356{
Michał Mirosław350fb322011-04-08 06:35:56 +00006357 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358
Francois Romieu2b7b4312011-04-18 22:53:24 -07006359 if (mss) {
6360 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08006361 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6362 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6363 const struct iphdr *ip = ip_hdr(skb);
6364
6365 if (ip->protocol == IPPROTO_TCP)
6366 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6367 else if (ip->protocol == IPPROTO_UDP)
6368 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6369 else
6370 WARN_ON_ONCE(1);
6371 }
6372
6373 return true;
6374}
6375
6376static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6377 struct sk_buff *skb, u32 *opts)
6378{
hayeswangbdfa4ed2014-07-11 16:25:57 +08006379 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08006380 u32 mss = skb_shinfo(skb)->gso_size;
6381
6382 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08006383 if (transport_offset > GTTCPHO_MAX) {
6384 netif_warn(tp, tx_err, tp->dev,
6385 "Invalid transport offset 0x%x for TSO\n",
6386 transport_offset);
6387 return false;
6388 }
6389
Heiner Kallweit4ff36462018-05-02 21:40:02 +02006390 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08006391 case htons(ETH_P_IP):
6392 opts[0] |= TD1_GTSENV4;
6393 break;
6394
6395 case htons(ETH_P_IPV6):
6396 if (msdn_giant_send_check(skb))
6397 return false;
6398
6399 opts[0] |= TD1_GTSENV6;
6400 break;
6401
6402 default:
6403 WARN_ON_ONCE(1);
6404 break;
6405 }
6406
hayeswangbdfa4ed2014-07-11 16:25:57 +08006407 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08006408 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006409 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08006410 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411
françois romieub423e9a2013-05-18 01:24:46 +00006412 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006413 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00006414
hayeswange9746042014-07-11 16:25:58 +08006415 if (transport_offset > TCPHO_MAX) {
6416 netif_warn(tp, tx_err, tp->dev,
6417 "Invalid transport offset 0x%x\n",
6418 transport_offset);
6419 return false;
6420 }
6421
Heiner Kallweit4ff36462018-05-02 21:40:02 +02006422 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08006423 case htons(ETH_P_IP):
6424 opts[1] |= TD1_IPv4_CS;
6425 ip_protocol = ip_hdr(skb)->protocol;
6426 break;
6427
6428 case htons(ETH_P_IPV6):
6429 opts[1] |= TD1_IPv6_CS;
6430 ip_protocol = ipv6_hdr(skb)->nexthdr;
6431 break;
6432
6433 default:
6434 ip_protocol = IPPROTO_RAW;
6435 break;
6436 }
6437
6438 if (ip_protocol == IPPROTO_TCP)
6439 opts[1] |= TD1_TCP_CS;
6440 else if (ip_protocol == IPPROTO_UDP)
6441 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006442 else
6443 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08006444
6445 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00006446 } else {
6447 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006448 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449 }
hayeswang5888d3f2014-07-11 16:25:56 +08006450
françois romieub423e9a2013-05-18 01:24:46 +00006451 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452}
6453
Stephen Hemminger613573252009-08-31 19:50:58 +00006454static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6455 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456{
6457 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006458 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006460 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461 dma_addr_t mapping;
6462 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006463 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006464 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02006465
Julien Ducourthial477206a2012-05-09 00:00:06 +02006466 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006467 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006468 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469 }
6470
6471 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006472 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473
françois romieub423e9a2013-05-18 01:24:46 +00006474 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6475 opts[0] = DescOwn;
6476
hayeswange9746042014-07-11 16:25:58 +08006477 if (!tp->tso_csum(tp, skb, opts)) {
6478 r8169_csum_workaround(tp, skb);
6479 return NETDEV_TX_OK;
6480 }
françois romieub423e9a2013-05-18 01:24:46 +00006481
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006482 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006483 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006484 if (unlikely(dma_mapping_error(d, mapping))) {
6485 if (net_ratelimit())
6486 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006487 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489
6490 tp->tx_skb[entry].len = len;
6491 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492
Francois Romieu2b7b4312011-04-18 22:53:24 -07006493 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006494 if (frags < 0)
6495 goto err_dma_1;
6496 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07006497 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006498 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07006499 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006500 tp->tx_skb[entry].skb = skb;
6501 }
6502
Francois Romieu2b7b4312011-04-18 22:53:24 -07006503 txd->opts2 = cpu_to_le32(opts[1]);
6504
Richard Cochran5047fb52012-03-10 07:29:42 +00006505 skb_tx_timestamp(skb);
6506
Alexander Duycka0750132014-12-11 15:02:17 -08006507 /* Force memory writes to complete before releasing descriptor */
6508 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509
Francois Romieucecb5fd2011-04-01 10:21:07 +02006510 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006511 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512 txd->opts1 = cpu_to_le32(status);
6513
Alexander Duycka0750132014-12-11 15:02:17 -08006514 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00006515 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516
Alexander Duycka0750132014-12-11 15:02:17 -08006517 tp->cur_tx += frags + 1;
6518
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006519 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520
David S. Miller87cda7c2015-02-22 15:54:29 -05006521 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01006522
David S. Miller87cda7c2015-02-22 15:54:29 -05006523 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01006524 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6525 * not miss a ring update when it notices a stopped queue.
6526 */
6527 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01006529 /* Sync with rtl_tx:
6530 * - publish queue status and cur_tx ring index (write barrier)
6531 * - refresh dirty_tx ring index (read barrier).
6532 * May the current thread have a pessimistic view of the ring
6533 * status and forget to wake up queue, a racing rtl_tx thread
6534 * can't.
6535 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006536 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02006537 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538 netif_wake_queue(dev);
6539 }
6540
Stephen Hemminger613573252009-08-31 19:50:58 +00006541 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006543err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006544 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006545err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006546 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006547 dev->stats.tx_dropped++;
6548 return NETDEV_TX_OK;
6549
6550err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006552 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00006553 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554}
6555
6556static void rtl8169_pcierr_interrupt(struct net_device *dev)
6557{
6558 struct rtl8169_private *tp = netdev_priv(dev);
6559 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 u16 pci_status, pci_cmd;
6561
6562 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6563 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6564
Joe Perchesbf82c182010-02-09 11:49:50 +00006565 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6566 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567
6568 /*
6569 * The recovery sequence below admits a very elaborated explanation:
6570 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01006571 * - I did not see what else could be done;
6572 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 *
6574 * Feel free to adjust to your needs.
6575 */
Francois Romieua27993f2006-12-18 00:04:19 +01006576 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01006577 pci_cmd &= ~PCI_COMMAND_PARITY;
6578 else
6579 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6580
6581 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582
6583 pci_write_config_word(pdev, PCI_STATUS,
6584 pci_status & (PCI_STATUS_DETECTED_PARITY |
6585 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6586 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6587
6588 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00006589 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006590 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591 tp->cp_cmd &= ~PCIDAC;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006592 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 }
6595
françois romieue6de30d2011-01-03 15:08:37 +00006596 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01006597
Francois Romieu98ddf982012-01-31 10:47:34 +01006598 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599}
6600
Francois Romieuda78dbf2012-01-26 14:18:23 +01006601static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602{
6603 unsigned int dirty_tx, tx_left;
6604
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 dirty_tx = tp->dirty_tx;
6606 smp_rmb();
6607 tx_left = tp->cur_tx - dirty_tx;
6608
6609 while (tx_left > 0) {
6610 unsigned int entry = dirty_tx % NUM_TX_DESC;
6611 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 u32 status;
6613
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6615 if (status & DescOwn)
6616 break;
6617
Alexander Duycka0750132014-12-11 15:02:17 -08006618 /* This barrier is needed to keep us from reading
6619 * any other fields out of the Tx descriptor until
6620 * we know the status of DescOwn
6621 */
6622 dma_rmb();
6623
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006624 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006625 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05006627 u64_stats_update_begin(&tp->tx_stats.syncp);
6628 tp->tx_stats.packets++;
6629 tp->tx_stats.bytes += tx_skb->skb->len;
6630 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006631 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 tx_skb->skb = NULL;
6633 }
6634 dirty_tx++;
6635 tx_left--;
6636 }
6637
6638 if (tp->dirty_tx != dirty_tx) {
6639 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01006640 /* Sync with rtl8169_start_xmit:
6641 * - publish dirty_tx ring index (write barrier)
6642 * - refresh cur_tx ring index and queue status (read barrier)
6643 * May the current thread miss the stopped queue condition,
6644 * a racing xmit thread can only have a right view of the
6645 * ring status.
6646 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006647 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02006649 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 netif_wake_queue(dev);
6651 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02006652 /*
6653 * 8168 hack: TxPoll requests are lost when the Tx packets are
6654 * too close. Let's kick an extra TxPoll request when a burst
6655 * of start_xmit activity is detected (if it is not detected,
6656 * it is slow enough). -- FR
6657 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006658 if (tp->cur_tx != dirty_tx)
6659 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 }
6661}
6662
Francois Romieu126fa4b2005-05-12 20:09:17 -04006663static inline int rtl8169_fragmented_frame(u32 status)
6664{
6665 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6666}
6667
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006668static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670 u32 status = opts1 & RxProtoMask;
6671
6672 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00006673 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674 skb->ip_summed = CHECKSUM_UNNECESSARY;
6675 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006676 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677}
6678
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006679static struct sk_buff *rtl8169_try_rx_copy(void *data,
6680 struct rtl8169_private *tp,
6681 int pkt_size,
6682 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02006684 struct sk_buff *skb;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006685 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006687 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006688 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006689 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08006690 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006691 if (skb)
Heiner Kallweit8a67aa82018-04-17 23:19:07 +02006692 skb_copy_to_linear_data(skb, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006693 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6694
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006695 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696}
6697
Francois Romieuda78dbf2012-01-26 14:18:23 +01006698static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699{
6700 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006701 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704
Timo Teräs9fba0812013-01-15 21:01:24 +00006705 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006707 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 u32 status;
6709
Heiner Kallweit62028062018-04-17 23:30:29 +02006710 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 if (status & DescOwn)
6712 break;
Alexander Duycka0750132014-12-11 15:02:17 -08006713
6714 /* This barrier is needed to keep us from reading
6715 * any other fields out of the Rx descriptor until
6716 * we know the status of DescOwn
6717 */
6718 dma_rmb();
6719
Richard Dawe4dcb7d32005-05-27 21:12:00 +02006720 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006721 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6722 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006723 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02006725 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02006727 dev->stats.rx_crc_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006728 /* RxFOVF is a reserved bit on later chip versions */
6729 if (tp->mac_version == RTL_GIGA_MAC_VER_01 &&
6730 status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006731 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006732 dev->stats.rx_fifo_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006733 } else if (status & (RxRUNT | RxCRC) &&
6734 !(status & RxRWT) &&
6735 dev->features & NETIF_F_RXALL) {
Ben Greear6bbe0212012-02-10 15:04:33 +00006736 goto process_pkt;
Heiner Kallweit62028062018-04-17 23:30:29 +02006737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006739 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00006740 dma_addr_t addr;
6741 int pkt_size;
6742
6743process_pkt:
6744 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00006745 if (likely(!(dev->features & NETIF_F_RXFCS)))
6746 pkt_size = (status & 0x00003fff) - 4;
6747 else
6748 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749
Francois Romieu126fa4b2005-05-12 20:09:17 -04006750 /*
6751 * The driver does not support incoming fragmented
6752 * frames. They are seen as a symptom of over-mtu
6753 * sized frames.
6754 */
6755 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02006756 dev->stats.rx_dropped++;
6757 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00006758 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006759 }
6760
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006761 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6762 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006763 if (!skb) {
6764 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00006765 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766 }
6767
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006768 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769 skb_put(skb, pkt_size);
6770 skb->protocol = eth_type_trans(skb, dev);
6771
Francois Romieu7a8fc772011-03-01 17:18:33 +01006772 rtl8169_rx_vlan_tag(desc, skb);
6773
françois romieu39174292015-11-11 23:35:18 +01006774 if (skb->pkt_type == PACKET_MULTICAST)
6775 dev->stats.multicast++;
6776
Francois Romieu56de4142011-03-15 17:29:31 +01006777 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778
Junchang Wang8027aa22012-03-04 23:30:32 +01006779 u64_stats_update_begin(&tp->rx_stats.syncp);
6780 tp->rx_stats.packets++;
6781 tp->rx_stats.bytes += pkt_size;
6782 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 }
françois romieuce11ff52013-01-24 13:30:06 +00006784release_descriptor:
6785 desc->opts2 = 0;
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006786 rtl8169_mark_to_asic(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 }
6788
6789 count = cur_rx - tp->cur_rx;
6790 tp->cur_rx = cur_rx;
6791
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 return count;
6793}
6794
Francois Romieu07d3f512007-02-21 22:40:46 +01006795static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796{
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006797 struct rtl8169_private *tp = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006799 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006801 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006802 if (status && status != 0xffff) {
6803 status &= RTL_EVENT_NAPI | tp->event_slow;
6804 if (status) {
6805 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00006806
Francois Romieuda78dbf2012-01-26 14:18:23 +01006807 rtl_irq_disable(tp);
Heiner Kallweit9a899a32018-04-17 23:21:01 +02006808 napi_schedule_irqoff(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 return IRQ_RETVAL(handled);
6812}
6813
Francois Romieuda78dbf2012-01-26 14:18:23 +01006814/*
6815 * Workqueue context.
6816 */
6817static void rtl_slow_event_work(struct rtl8169_private *tp)
6818{
6819 struct net_device *dev = tp->dev;
6820 u16 status;
6821
6822 status = rtl_get_events(tp) & tp->event_slow;
6823 rtl_ack_events(tp, status);
6824
6825 if (unlikely(status & RxFIFOOver)) {
6826 switch (tp->mac_version) {
6827 /* Work around for rx fifo overflow */
6828 case RTL_GIGA_MAC_VER_11:
6829 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01006830 /* XXX - Hack alert. See rtl_task(). */
6831 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006832 default:
6833 break;
6834 }
6835 }
6836
6837 if (unlikely(status & SYSErr))
6838 rtl8169_pcierr_interrupt(dev);
6839
6840 if (status & LinkChg)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006841 rtl8169_check_link_status(dev, tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006842
françois romieu7dbb4912012-06-09 10:53:16 +00006843 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006844}
6845
Francois Romieu4422bcd2012-01-26 11:23:32 +01006846static void rtl_task(struct work_struct *work)
6847{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006848 static const struct {
6849 int bitnr;
6850 void (*action)(struct rtl8169_private *);
6851 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01006852 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006853 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
6854 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
Francois Romieuda78dbf2012-01-26 14:18:23 +01006855 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01006856 struct rtl8169_private *tp =
6857 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006858 struct net_device *dev = tp->dev;
6859 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01006860
Francois Romieuda78dbf2012-01-26 14:18:23 +01006861 rtl_lock_work(tp);
6862
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006863 if (!netif_running(dev) ||
6864 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01006865 goto out_unlock;
6866
6867 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6868 bool pending;
6869
Francois Romieuda78dbf2012-01-26 14:18:23 +01006870 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006871 if (pending)
6872 rtl_work[i].action(tp);
6873 }
6874
6875out_unlock:
6876 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01006877}
6878
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006879static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006881 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6882 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006883 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
6884 int work_done= 0;
6885 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886
Francois Romieuda78dbf2012-01-26 14:18:23 +01006887 status = rtl_get_events(tp);
6888 rtl_ack_events(tp, status & ~tp->event_slow);
6889
6890 if (status & RTL_EVENT_NAPI_RX)
6891 work_done = rtl_rx(dev, tp, (u32) budget);
6892
6893 if (status & RTL_EVENT_NAPI_TX)
6894 rtl_tx(dev, tp);
6895
6896 if (status & tp->event_slow) {
6897 enable_mask &= ~tp->event_slow;
6898
6899 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006902 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08006903 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00006904
Francois Romieuda78dbf2012-01-26 14:18:23 +01006905 rtl_irq_enable(tp, enable_mask);
6906 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907 }
6908
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006909 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006912static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02006913{
6914 struct rtl8169_private *tp = netdev_priv(dev);
6915
6916 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6917 return;
6918
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006919 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
6920 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02006921}
6922
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923static void rtl8169_down(struct net_device *dev)
6924{
6925 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006927 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006928 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929
Hayes Wang92fc43b2011-07-06 15:58:03 +08006930 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006931 /*
6932 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006933 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6934 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006935 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006936 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006939 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 rtl8169_tx_clear(tp);
6942
6943 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006944
6945 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946}
6947
6948static int rtl8169_close(struct net_device *dev)
6949{
6950 struct rtl8169_private *tp = netdev_priv(dev);
6951 struct pci_dev *pdev = tp->pci_dev;
6952
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006953 pm_runtime_get_sync(&pdev->dev);
6954
Francois Romieucecb5fd2011-04-01 10:21:07 +02006955 /* Update counters before going down */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006956 rtl8169_update_counters(tp);
Ivan Vecera355423d2009-02-06 21:49:57 -08006957
Francois Romieuda78dbf2012-01-26 14:18:23 +01006958 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006959 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006960
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006962 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963
Lekensteyn4ea72442013-07-22 09:53:30 +02006964 cancel_work_sync(&tp->wk.work);
6965
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006966 pci_free_irq(pdev, 0, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006968 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6969 tp->RxPhyAddr);
6970 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6971 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972 tp->TxDescArray = NULL;
6973 tp->RxDescArray = NULL;
6974
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006975 pm_runtime_put_sync(&pdev->dev);
6976
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977 return 0;
6978}
6979
Francois Romieudc1c00c2012-03-08 10:06:18 +01006980#ifdef CONFIG_NET_POLL_CONTROLLER
6981static void rtl8169_netpoll(struct net_device *dev)
6982{
6983 struct rtl8169_private *tp = netdev_priv(dev);
6984
Ville Syrjälä6d8b8342018-06-20 15:01:53 +03006985 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
Francois Romieudc1c00c2012-03-08 10:06:18 +01006986}
6987#endif
6988
Francois Romieudf43ac72012-03-08 09:48:40 +01006989static int rtl_open(struct net_device *dev)
6990{
6991 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006992 struct pci_dev *pdev = tp->pci_dev;
6993 int retval = -ENOMEM;
6994
6995 pm_runtime_get_sync(&pdev->dev);
6996
6997 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02006998 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01006999 * dma_alloc_coherent provides more.
7000 */
7001 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7002 &tp->TxPhyAddr, GFP_KERNEL);
7003 if (!tp->TxDescArray)
7004 goto err_pm_runtime_put;
7005
7006 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7007 &tp->RxPhyAddr, GFP_KERNEL);
7008 if (!tp->RxDescArray)
7009 goto err_free_tx_0;
7010
Heiner Kallweitb1127e62018-04-17 23:23:35 +02007011 retval = rtl8169_init_ring(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007012 if (retval < 0)
7013 goto err_free_rx_1;
7014
7015 INIT_WORK(&tp->wk.work, rtl_task);
7016
7017 smp_mb();
7018
7019 rtl_request_firmware(tp);
7020
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02007021 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007022 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01007023 if (retval < 0)
7024 goto err_release_fw_2;
7025
7026 rtl_lock_work(tp);
7027
7028 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7029
7030 napi_enable(&tp->napi);
7031
7032 rtl8169_init_phy(dev, tp);
7033
Francois Romieudf43ac72012-03-08 09:48:40 +01007034 rtl_pll_power_up(tp);
7035
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007036 rtl_hw_start(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007037
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007038 if (!rtl8169_init_counter_offsets(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007039 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7040
Francois Romieudf43ac72012-03-08 09:48:40 +01007041 netif_start_queue(dev);
7042
7043 rtl_unlock_work(tp);
7044
Heiner Kallweita92a0842018-01-08 21:39:13 +01007045 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007046
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007047 rtl8169_check_link_status(dev, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007048out:
7049 return retval;
7050
7051err_release_fw_2:
7052 rtl_release_firmware(tp);
7053 rtl8169_rx_clear(tp);
7054err_free_rx_1:
7055 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7056 tp->RxPhyAddr);
7057 tp->RxDescArray = NULL;
7058err_free_tx_0:
7059 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7060 tp->TxPhyAddr);
7061 tp->TxDescArray = NULL;
7062err_pm_runtime_put:
7063 pm_runtime_put_noidle(&pdev->dev);
7064 goto out;
7065}
7066
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007067static void
Junchang Wang8027aa22012-03-04 23:30:32 +01007068rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069{
7070 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007071 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02007072 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01007073 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007075 pm_runtime_get_noresume(&pdev->dev);
7076
7077 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007078 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007079
Junchang Wang8027aa22012-03-04 23:30:32 +01007080 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007081 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007082 stats->rx_packets = tp->rx_stats.packets;
7083 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007084 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007085
Junchang Wang8027aa22012-03-04 23:30:32 +01007086 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007087 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007088 stats->tx_packets = tp->tx_stats.packets;
7089 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007090 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007091
7092 stats->rx_dropped = dev->stats.rx_dropped;
7093 stats->tx_dropped = dev->stats.tx_dropped;
7094 stats->rx_length_errors = dev->stats.rx_length_errors;
7095 stats->rx_errors = dev->stats.rx_errors;
7096 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7097 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7098 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02007099 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01007100
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007101 /*
7102 * Fetch additonal counter values missing in stats collected by driver
7103 * from tally counters.
7104 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007105 if (pm_runtime_active(&pdev->dev))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007106 rtl8169_update_counters(tp);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007107
7108 /*
7109 * Subtract values fetched during initalization.
7110 * See rtl8169_init_counter_offsets for a description why we do that.
7111 */
Corinna Vinschen42020322015-09-10 10:47:35 +02007112 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007113 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02007114 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007115 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02007116 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007117 le16_to_cpu(tp->tc_offset.tx_aborted);
7118
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007119 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120}
7121
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007122static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01007123{
françois romieu065c27c2011-01-03 15:08:12 +00007124 struct rtl8169_private *tp = netdev_priv(dev);
7125
Francois Romieu5d06a992006-02-23 00:47:58 +01007126 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007127 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01007128
7129 netif_device_detach(dev);
7130 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007131
7132 rtl_lock_work(tp);
7133 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007134 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007135 rtl_unlock_work(tp);
7136
7137 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007138}
Francois Romieu5d06a992006-02-23 00:47:58 +01007139
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007140#ifdef CONFIG_PM
7141
7142static int rtl8169_suspend(struct device *device)
7143{
7144 struct pci_dev *pdev = to_pci_dev(device);
7145 struct net_device *dev = pci_get_drvdata(pdev);
7146
7147 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02007148
Francois Romieu5d06a992006-02-23 00:47:58 +01007149 return 0;
7150}
7151
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007152static void __rtl8169_resume(struct net_device *dev)
7153{
françois romieu065c27c2011-01-03 15:08:12 +00007154 struct rtl8169_private *tp = netdev_priv(dev);
7155
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007156 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00007157
7158 rtl_pll_power_up(tp);
Heiner Kallweit92bad852018-06-24 18:37:36 +02007159 rtl8169_init_phy(dev, tp);
françois romieu065c27c2011-01-03 15:08:12 +00007160
Artem Savkovcff4c162012-04-03 10:29:11 +00007161 rtl_lock_work(tp);
7162 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007163 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00007164 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007165
Francois Romieu98ddf982012-01-31 10:47:34 +01007166 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007167}
7168
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007169static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01007170{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007171 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01007172 struct net_device *dev = pci_get_drvdata(pdev);
7173
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007174 if (netif_running(dev))
7175 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01007176
Francois Romieu5d06a992006-02-23 00:47:58 +01007177 return 0;
7178}
7179
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007180static int rtl8169_runtime_suspend(struct device *device)
7181{
7182 struct pci_dev *pdev = to_pci_dev(device);
7183 struct net_device *dev = pci_get_drvdata(pdev);
7184 struct rtl8169_private *tp = netdev_priv(dev);
7185
Heiner Kallweita92a0842018-01-08 21:39:13 +01007186 if (!tp->TxDescArray) {
7187 rtl_pll_power_down(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007188 return 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007189 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007190
Francois Romieuda78dbf2012-01-26 14:18:23 +01007191 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007192 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007193 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007194
7195 rtl8169_net_suspend(dev);
7196
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007197 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007198 rtl8169_rx_missed(dev);
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007199 rtl8169_update_counters(tp);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007200
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007201 return 0;
7202}
7203
7204static int rtl8169_runtime_resume(struct device *device)
7205{
7206 struct pci_dev *pdev = to_pci_dev(device);
7207 struct net_device *dev = pci_get_drvdata(pdev);
7208 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08007209 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007210
7211 if (!tp->TxDescArray)
7212 return 0;
7213
Francois Romieuda78dbf2012-01-26 14:18:23 +01007214 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007215 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007216 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007217
7218 __rtl8169_resume(dev);
7219
7220 return 0;
7221}
7222
7223static int rtl8169_runtime_idle(struct device *device)
7224{
7225 struct pci_dev *pdev = to_pci_dev(device);
7226 struct net_device *dev = pci_get_drvdata(pdev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007227
Heiner Kallweita92a0842018-01-08 21:39:13 +01007228 if (!netif_running(dev) || !netif_carrier_ok(dev))
7229 pm_schedule_suspend(device, 10000);
7230
7231 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007232}
7233
Alexey Dobriyan47145212009-12-14 18:00:08 -08007234static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02007235 .suspend = rtl8169_suspend,
7236 .resume = rtl8169_resume,
7237 .freeze = rtl8169_suspend,
7238 .thaw = rtl8169_resume,
7239 .poweroff = rtl8169_suspend,
7240 .restore = rtl8169_resume,
7241 .runtime_suspend = rtl8169_runtime_suspend,
7242 .runtime_resume = rtl8169_runtime_resume,
7243 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007244};
7245
7246#define RTL8169_PM_OPS (&rtl8169_pm_ops)
7247
7248#else /* !CONFIG_PM */
7249
7250#define RTL8169_PM_OPS NULL
7251
7252#endif /* !CONFIG_PM */
7253
David S. Miller1805b2f2011-10-24 18:18:09 -04007254static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7255{
David S. Miller1805b2f2011-10-24 18:18:09 -04007256 /* WoL fails with 8168b when the receiver is disabled. */
7257 switch (tp->mac_version) {
7258 case RTL_GIGA_MAC_VER_11:
7259 case RTL_GIGA_MAC_VER_12:
7260 case RTL_GIGA_MAC_VER_17:
7261 pci_clear_master(tp->pci_dev);
7262
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007263 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007264 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007265 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04007266 break;
7267 default:
7268 break;
7269 }
7270}
7271
Francois Romieu1765f952008-09-13 17:21:40 +02007272static void rtl_shutdown(struct pci_dev *pdev)
7273{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007274 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00007275 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02007276
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007277 rtl8169_net_suspend(dev);
7278
Francois Romieucecb5fd2011-04-01 10:21:07 +02007279 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08007280 rtl_rar_set(tp, dev->perm_addr);
7281
Hayes Wang92fc43b2011-07-06 15:58:03 +08007282 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00007283
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007284 if (system_state == SYSTEM_POWER_OFF) {
Heiner Kallweit433f9d02018-06-24 18:39:06 +02007285 if (tp->saved_wolopts) {
David S. Miller1805b2f2011-10-24 18:18:09 -04007286 rtl_wol_suspend_quirk(tp);
7287 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00007288 }
7289
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007290 pci_wake_from_d3(pdev, true);
7291 pci_set_power_state(pdev, PCI_D3hot);
7292 }
7293}
Francois Romieu5d06a992006-02-23 00:47:58 +01007294
Bill Pembertonbaf63292012-12-03 09:23:28 -05007295static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01007296{
7297 struct net_device *dev = pci_get_drvdata(pdev);
7298 struct rtl8169_private *tp = netdev_priv(dev);
7299
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007300 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01007301 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01007302
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007303 netif_napi_del(&tp->napi);
7304
Francois Romieue27566e2012-03-08 09:54:01 +01007305 unregister_netdev(dev);
7306
7307 rtl_release_firmware(tp);
7308
7309 if (pci_dev_run_wake(pdev))
7310 pm_runtime_get_noresume(&pdev->dev);
7311
7312 /* restore original MAC address */
7313 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01007314}
7315
Francois Romieufa9c3852012-03-08 10:01:50 +01007316static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01007317 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01007318 .ndo_stop = rtl8169_close,
7319 .ndo_get_stats64 = rtl8169_get_stats64,
7320 .ndo_start_xmit = rtl8169_start_xmit,
7321 .ndo_tx_timeout = rtl8169_tx_timeout,
7322 .ndo_validate_addr = eth_validate_addr,
7323 .ndo_change_mtu = rtl8169_change_mtu,
7324 .ndo_fix_features = rtl8169_fix_features,
7325 .ndo_set_features = rtl8169_set_features,
7326 .ndo_set_mac_address = rtl_set_mac_address,
7327 .ndo_do_ioctl = rtl8169_ioctl,
7328 .ndo_set_rx_mode = rtl_set_rx_mode,
7329#ifdef CONFIG_NET_POLL_CONTROLLER
7330 .ndo_poll_controller = rtl8169_netpoll,
7331#endif
7332
7333};
7334
Francois Romieu31fa8b12012-03-08 10:09:40 +01007335static const struct rtl_cfg_info {
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007336 void (*hw_start)(struct rtl8169_private *tp);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007337 u16 event_slow;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007338 unsigned int has_gmii:1;
Francois Romieu50970832017-10-27 13:24:49 +03007339 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007340 u8 default_ver;
7341} rtl_cfg_infos [] = {
7342 [RTL_CFG_0] = {
7343 .hw_start = rtl_hw_start_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007344 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007345 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007346 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007347 .default_ver = RTL_GIGA_MAC_VER_01,
7348 },
7349 [RTL_CFG_1] = {
7350 .hw_start = rtl_hw_start_8168,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007351 .event_slow = SYSErr | LinkChg | RxOverflow,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007352 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007353 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007354 .default_ver = RTL_GIGA_MAC_VER_11,
7355 },
7356 [RTL_CFG_2] = {
7357 .hw_start = rtl_hw_start_8101,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007358 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
7359 PCSTimeout,
Francois Romieu50970832017-10-27 13:24:49 +03007360 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007361 .default_ver = RTL_GIGA_MAC_VER_13,
7362 }
7363};
7364
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007365static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01007366{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007367 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007368
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007369 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007370 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
7371 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
7372 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007373 flags = PCI_IRQ_LEGACY;
7374 } else {
7375 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007376 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007377
7378 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007379}
7380
Hayes Wangc5583862012-07-02 17:23:22 +08007381DECLARE_RTL_COND(rtl_link_list_ready_cond)
7382{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007383 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08007384}
7385
7386DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7387{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007388 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08007389}
7390
Bill Pembertonbaf63292012-12-03 09:23:28 -05007391static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007392{
Hayes Wangc5583862012-07-02 17:23:22 +08007393 u32 data;
7394
7395 tp->ocp_base = OCP_STD_PHY_BASE;
7396
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007397 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08007398
7399 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7400 return;
7401
7402 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7403 return;
7404
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007405 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08007406 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007407 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08007408
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007409 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007410 data &= ~(1 << 14);
7411 r8168_mac_ocp_write(tp, 0xe8de, data);
7412
7413 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7414 return;
7415
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007416 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007417 data |= (1 << 15);
7418 r8168_mac_ocp_write(tp, 0xe8de, data);
7419
7420 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7421 return;
7422}
7423
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007424static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
7425{
7426 rtl8168ep_stop_cmac(tp);
7427 rtl_hw_init_8168g(tp);
7428}
7429
Bill Pembertonbaf63292012-12-03 09:23:28 -05007430static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007431{
7432 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02007433 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007434 rtl_hw_init_8168g(tp);
7435 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02007436 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007437 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08007438 break;
Hayes Wangc5583862012-07-02 17:23:22 +08007439 default:
7440 break;
7441 }
7442}
7443
hayeswang929a0312014-09-16 11:40:47 +08007444static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01007445{
7446 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007447 struct rtl8169_private *tp;
7448 struct mii_if_info *mii;
7449 struct net_device *dev;
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007450 int chipset, region, i;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007451 int rc;
7452
7453 if (netif_msg_drv(&debug)) {
7454 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
7455 MODULENAME, RTL8169_VERSION);
7456 }
7457
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007458 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
7459 if (!dev)
7460 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007461
7462 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01007463 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007464 tp = netdev_priv(dev);
7465 tp->dev = dev;
7466 tp->pci_dev = pdev;
7467 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
7468
7469 mii = &tp->mii;
7470 mii->dev = dev;
7471 mii->mdio_read = rtl_mdio_read;
7472 mii->mdio_write = rtl_mdio_write;
7473 mii->phy_id_mask = 0x1f;
7474 mii->reg_num_mask = 0x1f;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007475 mii->supports_gmii = cfg->has_gmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007476
Francois Romieu3b6cf252012-03-08 09:59:04 +01007477 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007478 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007479 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007480 dev_err(&pdev->dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007481 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007482 }
7483
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007484 if (pcim_set_mwi(pdev) < 0)
Heiner Kallweit22148df2018-04-22 17:15:15 +02007485 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007486
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007487 /* use first MMIO region */
7488 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
7489 if (region < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007490 dev_err(&pdev->dev, "no MMIO resource found\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007491 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007492 }
7493
7494 /* check for weird/broken PCI region reporting */
7495 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007496 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007497 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007498 }
7499
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007500 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007501 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007502 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007503 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007504 }
7505
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007506 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01007507
7508 if (!pci_is_pcie(pdev))
Heiner Kallweit22148df2018-04-22 17:15:15 +02007509 dev_info(&pdev->dev, "not PCI Express\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007510
7511 /* Identify chip attached to board */
Heiner Kallweit22148df2018-04-22 17:15:15 +02007512 rtl8169_get_mac_version(tp, cfg->default_ver);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007513
Heiner Kallweite3972862018-06-29 08:07:04 +02007514 if (rtl_tbi_enabled(tp)) {
7515 dev_err(&pdev->dev, "TBI fiber mode not supported\n");
7516 return -ENODEV;
7517 }
7518
Heiner Kallweit0ae09742018-04-28 22:19:26 +02007519 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007520
7521 if ((sizeof(dma_addr_t) > 4) &&
7522 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
7523 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01007524 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
7525 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007526
7527 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
7528 if (!pci_is_pcie(pdev))
7529 tp->cp_cmd |= PCIDAC;
7530 dev->features |= NETIF_F_HIGHDMA;
7531 } else {
7532 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7533 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007534 dev_err(&pdev->dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007535 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007536 }
7537 }
7538
Francois Romieu3b6cf252012-03-08 09:59:04 +01007539 rtl_init_rxcfg(tp);
7540
7541 rtl_irq_disable(tp);
7542
Hayes Wangc5583862012-07-02 17:23:22 +08007543 rtl_hw_initialize(tp);
7544
Francois Romieu3b6cf252012-03-08 09:59:04 +01007545 rtl_hw_reset(tp);
7546
7547 rtl_ack_events(tp, 0xffff);
7548
7549 pci_set_master(pdev);
7550
Francois Romieu3b6cf252012-03-08 09:59:04 +01007551 rtl_init_mdio_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007552 rtl_init_jumbo_ops(tp);
7553
7554 rtl8169_print_mac_version(tp);
7555
7556 chipset = tp->mac_version;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007557
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007558 rc = rtl_alloc_irq(tp);
7559 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007560 dev_err(&pdev->dev, "Can't allocate interrupt\n");
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007561 return rc;
7562 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007563
Heiner Kallweit7edf6d32018-02-22 21:22:40 +01007564 /* override BIOS settings, use userspace tools to enable WOL */
7565 __rtl8169_set_wol(tp, 0);
7566
Francois Romieu3b6cf252012-03-08 09:59:04 +01007567 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05007568 u64_stats_init(&tp->rx_stats.syncp);
7569 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007570
7571 /* Get MAC address */
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007572 switch (tp->mac_version) {
Heiner Kallweit353af852018-05-02 21:39:59 +02007573 u8 mac_addr[ETH_ALEN] __aligned(4);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007574 case RTL_GIGA_MAC_VER_35 ... RTL_GIGA_MAC_VER_38:
7575 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08007576 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
Heiner Kallweit353af852018-05-02 21:39:59 +02007577 *(u16 *)&mac_addr[4] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007578
Heiner Kallweit353af852018-05-02 21:39:59 +02007579 if (is_valid_ether_addr(mac_addr))
7580 rtl_rar_set(tp, mac_addr);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007581 break;
7582 default:
7583 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007584 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007585 for (i = 0; i < ETH_ALEN; i++)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007586 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007587
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00007588 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007589 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007590
Heiner Kallweit37621492018-04-17 23:20:03 +02007591 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007592
7593 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7594 * properly for all devices */
7595 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00007596 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007597
7598 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00007599 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7600 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007601 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7602 NETIF_F_HIGHDMA;
7603
hayeswang929a0312014-09-16 11:40:47 +08007604 tp->cp_cmd |= RxChkSum | RxVlan;
7605
7606 /*
7607 * Pretend we are using VLANs; This bypasses a nasty bug where
7608 * Interrupts stop flowing on high load on 8110SCd controllers.
7609 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01007610 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08007611 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00007612 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007613
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007614 switch (rtl_chip_infos[chipset].txd_version) {
7615 case RTL_TD_0:
hayeswang5888d3f2014-07-11 16:25:56 +08007616 tp->tso_csum = rtl8169_tso_csum_v1;
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007617 break;
7618 case RTL_TD_1:
hayeswang5888d3f2014-07-11 16:25:56 +08007619 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08007620 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007621 break;
7622 default:
hayeswang5888d3f2014-07-11 16:25:56 +08007623 WARN_ON_ONCE(1);
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007624 }
hayeswang5888d3f2014-07-11 16:25:56 +08007625
Francois Romieu3b6cf252012-03-08 09:59:04 +01007626 dev->hw_features |= NETIF_F_RXALL;
7627 dev->hw_features |= NETIF_F_RXFCS;
7628
Jarod Wilsonc7315a92016-10-17 15:54:09 -04007629 /* MTU range: 60 - hw-specific max */
7630 dev->min_mtu = ETH_ZLEN;
7631 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
7632
Francois Romieu3b6cf252012-03-08 09:59:04 +01007633 tp->hw_start = cfg->hw_start;
7634 tp->event_slow = cfg->event_slow;
Francois Romieu50970832017-10-27 13:24:49 +03007635 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007636
Francois Romieu3b6cf252012-03-08 09:59:04 +01007637 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
7638
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007639 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
7640 &tp->counters_phys_addr,
7641 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007642 if (!tp->counters)
7643 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02007644
Heiner Kallweit19c9ea32018-03-26 19:19:30 +02007645 pci_set_drvdata(pdev, dev);
7646
Francois Romieu3b6cf252012-03-08 09:59:04 +01007647 rc = register_netdev(dev);
7648 if (rc < 0)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007649 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007650
Heiner Kallweit2d6c5a62018-04-17 23:31:21 +02007651 netif_info(tp, probe, dev, "%s, %pM, XID %08x, IRQ %d\n",
7652 rtl_chip_infos[chipset].name, dev->dev_addr,
Heiner Kallweit90b989c2018-04-17 23:32:15 +02007653 (u32)(RTL_R32(tp, TxConfig) & 0xfcf0f8ff),
Heiner Kallweit29274992018-02-28 20:43:38 +01007654 pci_irq_vector(pdev, 0));
Francois Romieu3b6cf252012-03-08 09:59:04 +01007655 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
7656 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
7657 "tx checksumming: %s]\n",
7658 rtl_chip_infos[chipset].jumbo_max,
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02007659 tp->mac_version <= RTL_GIGA_MAC_VER_06 ? "ok" : "ko");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007660 }
7661
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007662 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01007663 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007664
Francois Romieu3b6cf252012-03-08 09:59:04 +01007665 netif_carrier_off(dev);
7666
Heiner Kallweita92a0842018-01-08 21:39:13 +01007667 if (pci_dev_run_wake(pdev))
7668 pm_runtime_put_sync(&pdev->dev);
7669
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007670 return 0;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007671}
7672
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673static struct pci_driver rtl8169_pci_driver = {
7674 .name = MODULENAME,
7675 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01007676 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05007677 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02007678 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007679 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680};
7681
Devendra Naga3eeb7da2012-10-26 09:27:42 +00007682module_pci_driver(rtl8169_pci_driver);