blob: 96eb28a7065ea2d1f47ecbacdebb02681e23c41d [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>
Heiner Kallweitf1e911d2018-07-17 22:51:26 +020019#include <linux/phy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/if_vlan.h>
21#include <linux/crc32.h>
22#include <linux/in.h>
23#include <linux/ip.h>
24#include <linux/tcp.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000025#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000027#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000028#include <linux/firmware.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040029#include <linux/prefetch.h>
hayeswange9746042014-07-11 16:25:58 +080030#include <linux/ipv6.h>
31#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
françois romieubca03d52011-01-03 15:07:31 +000039#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
40#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000041#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
42#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080043#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080044#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
45#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080046#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080047#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080048#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080049#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080050#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000051#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000052#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000053#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +080054#define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
55#define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
56#define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
57#define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
françois romieubca03d52011-01-03 15:07:31 +000058
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020059#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d92005-09-30 16:54:02 -070060 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020061
Julien Ducourthial477206a2012-05-09 00:00:06 +020062#define TX_SLOTS_AVAIL(tp) \
63 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
64
65/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
66#define TX_FRAGS_READY_FOR(tp,nr_frags) \
67 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
70 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050071static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Michal Schmidtaee77e42012-09-09 13:55:26 +000073#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
75
76#define R8169_REGS_SIZE 256
Heiner Kallweit1d0254d2018-04-17 23:25:46 +020077#define R8169_RX_BUF_SIZE (SZ_16K - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000079#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
81#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
82
83#define RTL8169_TX_TIMEOUT (6*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/* write/read MMIO register */
Andy Shevchenko1ef72862018-03-01 13:27:34 +020086#define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
87#define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
88#define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
89#define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
90#define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
91#define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +020094 RTL_GIGA_MAC_VER_01 = 0,
95 RTL_GIGA_MAC_VER_02,
96 RTL_GIGA_MAC_VER_03,
97 RTL_GIGA_MAC_VER_04,
98 RTL_GIGA_MAC_VER_05,
99 RTL_GIGA_MAC_VER_06,
100 RTL_GIGA_MAC_VER_07,
101 RTL_GIGA_MAC_VER_08,
102 RTL_GIGA_MAC_VER_09,
103 RTL_GIGA_MAC_VER_10,
104 RTL_GIGA_MAC_VER_11,
105 RTL_GIGA_MAC_VER_12,
106 RTL_GIGA_MAC_VER_13,
107 RTL_GIGA_MAC_VER_14,
108 RTL_GIGA_MAC_VER_15,
109 RTL_GIGA_MAC_VER_16,
110 RTL_GIGA_MAC_VER_17,
111 RTL_GIGA_MAC_VER_18,
112 RTL_GIGA_MAC_VER_19,
113 RTL_GIGA_MAC_VER_20,
114 RTL_GIGA_MAC_VER_21,
115 RTL_GIGA_MAC_VER_22,
116 RTL_GIGA_MAC_VER_23,
117 RTL_GIGA_MAC_VER_24,
118 RTL_GIGA_MAC_VER_25,
119 RTL_GIGA_MAC_VER_26,
120 RTL_GIGA_MAC_VER_27,
121 RTL_GIGA_MAC_VER_28,
122 RTL_GIGA_MAC_VER_29,
123 RTL_GIGA_MAC_VER_30,
124 RTL_GIGA_MAC_VER_31,
125 RTL_GIGA_MAC_VER_32,
126 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800127 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800128 RTL_GIGA_MAC_VER_35,
129 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800130 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800131 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800132 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800133 RTL_GIGA_MAC_VER_40,
134 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000135 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000136 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800137 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800138 RTL_GIGA_MAC_VER_45,
139 RTL_GIGA_MAC_VER_46,
140 RTL_GIGA_MAC_VER_47,
141 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800142 RTL_GIGA_MAC_VER_49,
143 RTL_GIGA_MAC_VER_50,
144 RTL_GIGA_MAC_VER_51,
Francois Romieu85bffe62011-04-27 08:22:39 +0200145 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
Francois Romieu2b7b4312011-04-18 22:53:24 -0700148enum rtl_tx_desc_version {
149 RTL_TD_0 = 0,
150 RTL_TD_1 = 1,
151};
152
Francois Romieud58d46b2011-05-03 16:38:29 +0200153#define JUMBO_1K ETH_DATA_LEN
154#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
155#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
156#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
157#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
158
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200159#define _R(NAME,TD,FW,SZ) { \
Francois Romieud58d46b2011-05-03 16:38:29 +0200160 .name = NAME, \
161 .txd_version = TD, \
162 .fw_name = FW, \
163 .jumbo_max = SZ, \
Francois Romieud58d46b2011-05-03 16:38:29 +0200164}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800166static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700168 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200169 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200170 u16 jumbo_max;
Francois Romieu85bffe62011-04-27 08:22:39 +0200171} rtl_chip_infos[] = {
172 /* PCI devices. */
173 [RTL_GIGA_MAC_VER_01] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200174 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200175 [RTL_GIGA_MAC_VER_02] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200176 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200177 [RTL_GIGA_MAC_VER_03] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200178 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200179 [RTL_GIGA_MAC_VER_04] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200180 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200181 [RTL_GIGA_MAC_VER_05] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200182 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200183 [RTL_GIGA_MAC_VER_06] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200184 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200185 /* PCI-E devices. */
186 [RTL_GIGA_MAC_VER_07] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200187 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200188 [RTL_GIGA_MAC_VER_08] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200189 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200190 [RTL_GIGA_MAC_VER_09] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200191 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200192 [RTL_GIGA_MAC_VER_10] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200193 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200194 [RTL_GIGA_MAC_VER_11] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200195 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200196 [RTL_GIGA_MAC_VER_12] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200197 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200198 [RTL_GIGA_MAC_VER_13] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200199 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200200 [RTL_GIGA_MAC_VER_14] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200201 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200202 [RTL_GIGA_MAC_VER_15] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200203 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200204 [RTL_GIGA_MAC_VER_16] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200205 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200206 [RTL_GIGA_MAC_VER_17] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200207 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200208 [RTL_GIGA_MAC_VER_18] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200209 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200210 [RTL_GIGA_MAC_VER_19] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200211 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200212 [RTL_GIGA_MAC_VER_20] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200213 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200214 [RTL_GIGA_MAC_VER_21] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200215 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200216 [RTL_GIGA_MAC_VER_22] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200217 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200218 [RTL_GIGA_MAC_VER_23] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200219 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200220 [RTL_GIGA_MAC_VER_24] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200221 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200222 [RTL_GIGA_MAC_VER_25] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200223 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200224 [RTL_GIGA_MAC_VER_26] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200225 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200226 [RTL_GIGA_MAC_VER_27] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200227 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200228 [RTL_GIGA_MAC_VER_28] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200229 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200230 [RTL_GIGA_MAC_VER_29] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200231 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200232 [RTL_GIGA_MAC_VER_30] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200233 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200234 [RTL_GIGA_MAC_VER_31] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200235 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200236 [RTL_GIGA_MAC_VER_32] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200237 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200238 [RTL_GIGA_MAC_VER_33] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200239 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2, JUMBO_9K),
Hayes Wang70090422011-07-06 15:58:06 +0800240 [RTL_GIGA_MAC_VER_34] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200241 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3, JUMBO_9K),
Hayes Wangc2218922011-09-06 16:55:18 +0800242 [RTL_GIGA_MAC_VER_35] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200243 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1, JUMBO_9K),
Hayes Wangc2218922011-09-06 16:55:18 +0800244 [RTL_GIGA_MAC_VER_36] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200245 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2, JUMBO_9K),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800246 [RTL_GIGA_MAC_VER_37] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200247 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1, JUMBO_1K),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800248 [RTL_GIGA_MAC_VER_38] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200249 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1, JUMBO_9K),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800250 [RTL_GIGA_MAC_VER_39] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200251 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1, JUMBO_1K),
Hayes Wangc5583862012-07-02 17:23:22 +0800252 [RTL_GIGA_MAC_VER_40] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200253 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2, JUMBO_9K),
Hayes Wangc5583862012-07-02 17:23:22 +0800254 [RTL_GIGA_MAC_VER_41] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200255 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K),
hayeswang57538c42013-04-01 22:23:40 +0000256 [RTL_GIGA_MAC_VER_42] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200257 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3, JUMBO_9K),
hayeswang58152cd2013-04-01 22:23:42 +0000258 [RTL_GIGA_MAC_VER_43] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200259 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2, JUMBO_1K),
hayeswang45dd95c2013-07-08 17:09:01 +0800260 [RTL_GIGA_MAC_VER_44] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200261 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2, JUMBO_9K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800262 [RTL_GIGA_MAC_VER_45] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200263 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1, JUMBO_9K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800264 [RTL_GIGA_MAC_VER_46] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200265 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2, JUMBO_9K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800266 [RTL_GIGA_MAC_VER_47] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200267 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1, JUMBO_1K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800268 [RTL_GIGA_MAC_VER_48] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200269 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2, JUMBO_1K),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800270 [RTL_GIGA_MAC_VER_49] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200271 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800272 [RTL_GIGA_MAC_VER_50] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200273 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800274 [RTL_GIGA_MAC_VER_51] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200275 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276};
277#undef _R
278
Francois Romieubcf0bf92006-07-26 23:14:13 +0200279enum cfg_version {
280 RTL_CFG_0 = 0x00,
281 RTL_CFG_1,
282 RTL_CFG_2
283};
284
Benoit Taine9baa3c32014-08-08 15:56:03 +0200285static const struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200286 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200287 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Chun-Hao Lin610c9082016-12-27 16:29:43 +0800288 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
Francois Romieud81bf552006-09-20 21:31:20 +0200289 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100290 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200291 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200292 { PCI_VENDOR_ID_DLINK, 0x4300,
293 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200294 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000295 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200296 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200297 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
298 { PCI_VENDOR_ID_LINKSYS, 0x1032,
299 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100300 { 0x0001, 0x8168,
301 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 {0,},
303};
304
305MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
306
Ard Biesheuvel27896c82016-05-14 22:40:15 +0200307static int use_dac = -1;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200308static struct {
309 u32 msg_enable;
310} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Francois Romieu07d3f512007-02-21 22:40:46 +0100312enum rtl_registers {
313 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100314 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100315 MAR0 = 8, /* Multicast filter. */
316 CounterAddrLow = 0x10,
317 CounterAddrHigh = 0x14,
318 TxDescStartAddrLow = 0x20,
319 TxDescStartAddrHigh = 0x24,
320 TxHDescStartAddrLow = 0x28,
321 TxHDescStartAddrHigh = 0x2c,
322 FLASH = 0x30,
323 ERSR = 0x36,
324 ChipCmd = 0x37,
325 TxPoll = 0x38,
326 IntrMask = 0x3c,
327 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700328
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800329 TxConfig = 0x40,
330#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
331#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
332
333 RxConfig = 0x44,
334#define RX128_INT_EN (1 << 15) /* 8111c and later */
335#define RX_MULTI_EN (1 << 14) /* 8111c only */
336#define RXCFG_FIFO_SHIFT 13
337 /* No threshold before first PCI xfer */
338#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000339#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800340#define RXCFG_DMA_SHIFT 8
341 /* Unlimited maximum PCI burst. */
342#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700343
Francois Romieu07d3f512007-02-21 22:40:46 +0100344 RxMissed = 0x4c,
345 Cfg9346 = 0x50,
346 Config0 = 0x51,
347 Config1 = 0x52,
348 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200349#define PME_SIGNAL (1 << 5) /* 8168c and later */
350
Francois Romieu07d3f512007-02-21 22:40:46 +0100351 Config3 = 0x54,
352 Config4 = 0x55,
353 Config5 = 0x56,
354 MultiIntr = 0x5c,
355 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100356 PHYstatus = 0x6c,
357 RxMaxSize = 0xda,
358 CPlusCmd = 0xe0,
359 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300360
361#define RTL_COALESCE_MASK 0x0f
362#define RTL_COALESCE_SHIFT 4
363#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
364#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
365
Francois Romieu07d3f512007-02-21 22:40:46 +0100366 RxDescAddrLow = 0xe4,
367 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000368 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
369
370#define NoEarlyTx 0x3f /* Max value : no early transmit. */
371
372 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
373
374#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800375#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000376
Francois Romieu07d3f512007-02-21 22:40:46 +0100377 FuncEvent = 0xf0,
378 FuncEventMask = 0xf4,
379 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800380 IBCR0 = 0xf8,
381 IBCR2 = 0xf9,
382 IBIMR0 = 0xfa,
383 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100384 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385};
386
Francois Romieuf162a5d2008-06-01 22:37:49 +0200387enum rtl8168_8101_registers {
388 CSIDR = 0x64,
389 CSIAR = 0x68,
390#define CSIAR_FLAG 0x80000000
391#define CSIAR_WRITE_CMD 0x80000000
Heiner Kallweitff1d7332018-05-02 21:39:56 +0200392#define CSIAR_BYTE_ENABLE 0x0000f000
393#define CSIAR_ADDR_MASK 0x00000fff
françois romieu065c27c2011-01-03 15:08:12 +0000394 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200395 EPHYAR = 0x80,
396#define EPHYAR_FLAG 0x80000000
397#define EPHYAR_WRITE_CMD 0x80000000
398#define EPHYAR_REG_MASK 0x1f
399#define EPHYAR_REG_SHIFT 16
400#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800401 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800402#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800403#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200404 DBG_REG = 0xd1,
405#define FIX_NAK_1 (1 << 4)
406#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800407 TWSI = 0xd2,
408 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800409#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800410#define TX_EMPTY (1 << 5)
411#define RX_EMPTY (1 << 4)
412#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800413#define EN_NDP (1 << 3)
414#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800415#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000416 EFUSEAR = 0xdc,
417#define EFUSEAR_FLAG 0x80000000
418#define EFUSEAR_WRITE_CMD 0x80000000
419#define EFUSEAR_READ_CMD 0x00000000
420#define EFUSEAR_REG_MASK 0x03ff
421#define EFUSEAR_REG_SHIFT 8
422#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800423 MISC_1 = 0xf2,
424#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200425};
426
françois romieuc0e45c12011-01-03 15:08:04 +0000427enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800428 LED_FREQ = 0x1a,
429 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000430 ERIDR = 0x70,
431 ERIAR = 0x74,
432#define ERIAR_FLAG 0x80000000
433#define ERIAR_WRITE_CMD 0x80000000
434#define ERIAR_READ_CMD 0x00000000
435#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000436#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800437#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
438#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
439#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800440#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800441#define ERIAR_MASK_SHIFT 12
442#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
443#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800444#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800445#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800446#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000447 EPHY_RXER_NUM = 0x7c,
448 OCPDR = 0xb0, /* OCP GPHY access */
449#define OCPDR_WRITE_CMD 0x80000000
450#define OCPDR_READ_CMD 0x00000000
451#define OCPDR_REG_MASK 0x7f
452#define OCPDR_GPHY_REG_SHIFT 16
453#define OCPDR_DATA_MASK 0xffff
454 OCPAR = 0xb4,
455#define OCPAR_FLAG 0x80000000
456#define OCPAR_GPHY_WRITE_CMD 0x8000f060
457#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800458 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000459 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
460 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200461#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800462#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800463#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800464#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800465#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000466};
467
Francois Romieu07d3f512007-02-21 22:40:46 +0100468enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100470 SYSErr = 0x8000,
471 PCSTimeout = 0x4000,
472 SWInt = 0x0100,
473 TxDescUnavail = 0x0080,
474 RxFIFOOver = 0x0040,
475 LinkChg = 0x0020,
476 RxOverflow = 0x0010,
477 TxErr = 0x0008,
478 TxOK = 0x0004,
479 RxErr = 0x0002,
480 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400483 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200484 RxFOVF = (1 << 23),
485 RxRWT = (1 << 22),
486 RxRES = (1 << 21),
487 RxRUNT = (1 << 20),
488 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800491 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100492 CmdReset = 0x10,
493 CmdRxEnb = 0x08,
494 CmdTxEnb = 0x04,
495 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Francois Romieu275391a2007-02-23 23:50:28 +0100497 /* TXPoll register p.5 */
498 HPQ = 0x80, /* Poll cmd on the high prio queue */
499 NPQ = 0x40, /* Poll cmd on the low prio queue */
500 FSWInt = 0x01, /* Forced software interrupt */
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100503 Cfg9346_Lock = 0x00,
504 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100507 AcceptErr = 0x20,
508 AcceptRunt = 0x10,
509 AcceptBroadcast = 0x08,
510 AcceptMulticast = 0x04,
511 AcceptMyPhys = 0x02,
512 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200513#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* TxConfigBits */
516 TxInterFrameGapShift = 24,
517 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
518
Francois Romieu5d06a992006-02-23 00:47:58 +0100519 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200520 LEDS1 = (1 << 7),
521 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200522 Speed_down = (1 << 4),
523 MEMMAP = (1 << 3),
524 IOMAP = (1 << 2),
525 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100526 PMEnable = (1 << 0), /* Power Management Enable */
527
Francois Romieu6dccd162007-02-13 23:38:05 +0100528 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000529 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000530 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100531 PCI_Clock_66MHz = 0x01,
532 PCI_Clock_33MHz = 0x00,
533
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100534 /* Config3 register p.25 */
535 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
536 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200537 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800538 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200539 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100540
Francois Romieud58d46b2011-05-03 16:38:29 +0200541 /* Config4 register */
542 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
543
Francois Romieu5d06a992006-02-23 00:47:58 +0100544 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100545 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
546 MWF = (1 << 5), /* Accept Multicast wakeup frame */
547 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200548 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100549 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100550 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000551 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200554 EnableBist = (1 << 15), // 8168 8101
555 Mac_dbgo_oe = (1 << 14), // 8168 8101
556 Normal_mode = (1 << 13), // unused
557 Force_half_dup = (1 << 12), // 8168 8101
558 Force_rxflow_en = (1 << 11), // 8168 8101
559 Force_txflow_en = (1 << 10), // 8168 8101
560 Cxpl_dbg_sel = (1 << 9), // 8168 8101
561 ASF = (1 << 8), // 8168 8101
562 PktCntrDisable = (1 << 7), // 8168 8101
563 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 RxVlan = (1 << 6),
565 RxChkSum = (1 << 5),
566 PCIDAC = (1 << 4),
567 PCIMulRW = (1 << 3),
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +0200568#define INTT_MASK GENMASK(1, 0)
Francois Romieu0e485152007-02-20 00:00:26 +0100569 INTT_0 = 0x0000, // 8168
570 INTT_1 = 0x0001, // 8168
571 INTT_2 = 0x0002, // 8168
572 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100575 TBI_Enable = 0x80,
576 TxFlowCtrl = 0x40,
577 RxFlowCtrl = 0x20,
578 _1000bpsF = 0x10,
579 _100bps = 0x08,
580 _10bps = 0x04,
581 LinkStatus = 0x02,
582 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100585 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200586
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200587 /* ResetCounterCommand */
588 CounterReset = 0x1,
589
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200590 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100591 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800592
593 /* magic enable v2 */
594 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595};
596
Francois Romieu2b7b4312011-04-18 22:53:24 -0700597enum rtl_desc_bit {
598 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
600 RingEnd = (1 << 30), /* End of descriptor ring */
601 FirstFrag = (1 << 29), /* First segment of a packet */
602 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700603};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Francois Romieu2b7b4312011-04-18 22:53:24 -0700605/* Generic case. */
606enum rtl_tx_desc_bit {
607 /* First doubleword. */
608 TD_LSO = (1 << 27), /* Large Send Offload */
609#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Francois Romieu2b7b4312011-04-18 22:53:24 -0700611 /* Second doubleword. */
612 TxVlanTag = (1 << 17), /* Add VLAN tag */
613};
614
615/* 8169, 8168b and 810x except 8102e. */
616enum rtl_tx_desc_bit_0 {
617 /* First doubleword. */
618#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
619 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
620 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
621 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
622};
623
624/* 8102e, 8168c and beyond. */
625enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800626 /* First doubleword. */
627 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800628 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800629#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800630#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800631
Francois Romieu2b7b4312011-04-18 22:53:24 -0700632 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800633#define TCPHO_SHIFT 18
634#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700635#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800636 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
637 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700638 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
639 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
640};
641
Francois Romieu2b7b4312011-04-18 22:53:24 -0700642enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /* Rx private */
644 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500645 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647#define RxProtoUDP (PID1)
648#define RxProtoTCP (PID0)
649#define RxProtoIP (PID1 | PID0)
650#define RxProtoMask RxProtoIP
651
652 IPFail = (1 << 16), /* IP checksum failed */
653 UDPFail = (1 << 15), /* UDP/IP checksum failed */
654 TCPFail = (1 << 14), /* TCP/IP checksum failed */
655 RxVlanTag = (1 << 16), /* VLAN tag available */
656};
657
658#define RsvdMask 0x3fffc000
Heiner Kallweit12d42c52018-04-28 22:19:30 +0200659#define CPCMD_QUIRK_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200662 __le32 opts1;
663 __le32 opts2;
664 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665};
666
667struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200668 __le32 opts1;
669 __le32 opts2;
670 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671};
672
673struct ring_info {
674 struct sk_buff *skb;
675 u32 len;
676 u8 __pad[sizeof(void *) - sizeof(u32)];
677};
678
Ivan Vecera355423d2009-02-06 21:49:57 -0800679struct rtl8169_counters {
680 __le64 tx_packets;
681 __le64 rx_packets;
682 __le64 tx_errors;
683 __le32 rx_errors;
684 __le16 rx_missed;
685 __le16 align_errors;
686 __le32 tx_one_collision;
687 __le32 tx_multi_collision;
688 __le64 rx_unicast;
689 __le64 rx_broadcast;
690 __le32 rx_multicast;
691 __le16 tx_aborted;
692 __le16 tx_underun;
693};
694
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200695struct rtl8169_tc_offsets {
696 bool inited;
697 __le64 tx_errors;
698 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200699 __le16 tx_aborted;
700};
701
Francois Romieuda78dbf2012-01-26 14:18:23 +0100702enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100703 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100704 RTL_FLAG_TASK_SLOW_PENDING,
705 RTL_FLAG_TASK_RESET_PENDING,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100706 RTL_FLAG_MAX
707};
708
Junchang Wang8027aa22012-03-04 23:30:32 +0100709struct rtl8169_stats {
710 u64 packets;
711 u64 bytes;
712 struct u64_stats_sync syncp;
713};
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715struct rtl8169_private {
716 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200717 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000718 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700719 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200720 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700721 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
723 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100725 struct rtl8169_stats rx_stats;
726 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
728 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
729 dma_addr_t TxPhyAddr;
730 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000731 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100734
735 u16 event_slow;
Francois Romieu50970832017-10-27 13:24:49 +0300736 const struct rtl_coalesce_info *coalesce_info;
françois romieuc0e45c12011-01-03 15:08:04 +0000737
738 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200739 void (*write)(struct rtl8169_private *, int, int);
740 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000741 } mdio_ops;
742
Francois Romieud58d46b2011-05-03 16:38:29 +0200743 struct jumbo_ops {
744 void (*enable)(struct rtl8169_private *);
745 void (*disable)(struct rtl8169_private *);
746 } jumbo_ops;
747
Heiner Kallweit61cb5322018-04-17 23:27:38 +0200748 void (*hw_start)(struct rtl8169_private *tp);
hayeswang5888d3f2014-07-11 16:25:56 +0800749 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100750
751 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100752 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
753 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100754 struct work_struct work;
755 } wk;
756
Francois Romieuccdffb92008-07-26 14:26:06 +0200757 struct mii_if_info mii;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +0200758 struct mii_bus *mii_bus;
Corinna Vinschen42020322015-09-10 10:47:35 +0200759 dma_addr_t counters_phys_addr;
760 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200761 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000762 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000763
Francois Romieub6ffd972011-06-17 17:00:05 +0200764 struct rtl_fw {
765 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200766
767#define RTL_VER_SIZE 32
768
769 char version[RTL_VER_SIZE];
770
771 struct rtl_fw_phy_action {
772 __le32 *code;
773 size_t size;
774 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200775 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300776#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800777
778 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779};
780
Ralf Baechle979b6c12005-06-13 14:30:40 -0700781MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700784MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200785module_param_named(debug, debug.msg_enable, int, 0);
786MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787MODULE_LICENSE("GPL");
788MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000789MODULE_FIRMWARE(FIRMWARE_8168D_1);
790MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000791MODULE_FIRMWARE(FIRMWARE_8168E_1);
792MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400793MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800794MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800795MODULE_FIRMWARE(FIRMWARE_8168F_1);
796MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800797MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800798MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800799MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800800MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000801MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000802MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000803MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800804MODULE_FIRMWARE(FIRMWARE_8168H_1);
805MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200806MODULE_FIRMWARE(FIRMWARE_8107E_1);
807MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Heiner Kallweit1e1205b2018-03-20 07:45:42 +0100809static inline struct device *tp_to_dev(struct rtl8169_private *tp)
810{
811 return &tp->pci_dev->dev;
812}
813
Francois Romieuda78dbf2012-01-26 14:18:23 +0100814static void rtl_lock_work(struct rtl8169_private *tp)
815{
816 mutex_lock(&tp->wk.mutex);
817}
818
819static void rtl_unlock_work(struct rtl8169_private *tp)
820{
821 mutex_unlock(&tp->wk.mutex);
822}
823
Heiner Kallweitcb732002018-03-20 07:45:35 +0100824static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
Francois Romieud58d46b2011-05-03 16:38:29 +0200825{
Heiner Kallweitcb732002018-03-20 07:45:35 +0100826 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +0800827 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200828}
829
Francois Romieuffc46952012-07-06 14:19:23 +0200830struct rtl_cond {
831 bool (*check)(struct rtl8169_private *);
832 const char *msg;
833};
834
835static void rtl_udelay(unsigned int d)
836{
837 udelay(d);
838}
839
840static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
841 void (*delay)(unsigned int), unsigned int d, int n,
842 bool high)
843{
844 int i;
845
846 for (i = 0; i < n; i++) {
847 delay(d);
848 if (c->check(tp) == high)
849 return true;
850 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200851 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
852 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200853 return false;
854}
855
856static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
857 const struct rtl_cond *c,
858 unsigned int d, int n)
859{
860 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
861}
862
863static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
864 const struct rtl_cond *c,
865 unsigned int d, int n)
866{
867 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
868}
869
870static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
871 const struct rtl_cond *c,
872 unsigned int d, int n)
873{
874 return rtl_loop_wait(tp, c, msleep, d, n, true);
875}
876
877static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
878 const struct rtl_cond *c,
879 unsigned int d, int n)
880{
881 return rtl_loop_wait(tp, c, msleep, d, n, false);
882}
883
884#define DECLARE_RTL_COND(name) \
885static bool name ## _check(struct rtl8169_private *); \
886 \
887static const struct rtl_cond name = { \
888 .check = name ## _check, \
889 .msg = #name \
890}; \
891 \
892static bool name ## _check(struct rtl8169_private *tp)
893
Hayes Wangc5583862012-07-02 17:23:22 +0800894static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
895{
896 if (reg & 0xffff0001) {
897 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
898 return true;
899 }
900 return false;
901}
902
903DECLARE_RTL_COND(rtl_ocp_gphy_cond)
904{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200905 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
Hayes Wangc5583862012-07-02 17:23:22 +0800906}
907
908static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
909{
Hayes Wangc5583862012-07-02 17:23:22 +0800910 if (rtl_ocp_reg_failure(tp, reg))
911 return;
912
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200913 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800914
915 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
916}
917
918static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
919{
Hayes Wangc5583862012-07-02 17:23:22 +0800920 if (rtl_ocp_reg_failure(tp, reg))
921 return 0;
922
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200923 RTL_W32(tp, GPHY_OCP, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800924
925 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200926 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
Hayes Wangc5583862012-07-02 17:23:22 +0800927}
928
Hayes Wangc5583862012-07-02 17:23:22 +0800929static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
930{
Hayes Wangc5583862012-07-02 17:23:22 +0800931 if (rtl_ocp_reg_failure(tp, reg))
932 return;
933
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200934 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800935}
936
937static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
938{
Hayes Wangc5583862012-07-02 17:23:22 +0800939 if (rtl_ocp_reg_failure(tp, reg))
940 return 0;
941
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200942 RTL_W32(tp, OCPDR, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800943
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200944 return RTL_R32(tp, OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +0800945}
946
947#define OCP_STD_PHY_BASE 0xa400
948
949static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
950{
951 if (reg == 0x1f) {
952 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
953 return;
954 }
955
956 if (tp->ocp_base != OCP_STD_PHY_BASE)
957 reg -= 0x10;
958
959 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
960}
961
962static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
963{
964 if (tp->ocp_base != OCP_STD_PHY_BASE)
965 reg -= 0x10;
966
967 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
968}
969
hayeswangeee37862013-04-01 22:23:38 +0000970static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
971{
972 if (reg == 0x1f) {
973 tp->ocp_base = value << 4;
974 return;
975 }
976
977 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
978}
979
980static int mac_mcu_read(struct rtl8169_private *tp, int reg)
981{
982 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
983}
984
Francois Romieuffc46952012-07-06 14:19:23 +0200985DECLARE_RTL_COND(rtl_phyar_cond)
986{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200987 return RTL_R32(tp, PHYAR) & 0x80000000;
Francois Romieuffc46952012-07-06 14:19:23 +0200988}
989
Francois Romieu24192212012-07-06 20:19:42 +0200990static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200992 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Francois Romieuffc46952012-07-06 14:19:23 +0200994 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -0700995 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700996 * According to hardware specs a 20us delay is required after write
997 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700998 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700999 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
Francois Romieu24192212012-07-06 20:19:42 +02001002static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Francois Romieuffc46952012-07-06 14:19:23 +02001004 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001006 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Francois Romieuffc46952012-07-06 14:19:23 +02001008 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001009 RTL_R32(tp, PHYAR) & 0xffff : ~0;
Francois Romieuffc46952012-07-06 14:19:23 +02001010
Timo Teräs81a95f02010-06-09 17:31:48 -07001011 /*
1012 * According to hardware specs a 20us delay is required after read
1013 * complete indication, but before sending next command.
1014 */
1015 udelay(20);
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return value;
1018}
1019
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001020DECLARE_RTL_COND(rtl_ocpar_cond)
1021{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001022 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001023}
1024
Francois Romieu24192212012-07-06 20:19:42 +02001025static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001026{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001027 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1028 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
1029 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001030
Francois Romieuffc46952012-07-06 14:19:23 +02001031 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001032}
1033
Francois Romieu24192212012-07-06 20:19:42 +02001034static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001035{
Francois Romieu24192212012-07-06 20:19:42 +02001036 r8168dp_1_mdio_access(tp, reg,
1037 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001038}
1039
Francois Romieu24192212012-07-06 20:19:42 +02001040static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001041{
Francois Romieu24192212012-07-06 20:19:42 +02001042 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001043
1044 mdelay(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001045 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
1046 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001047
Francois Romieuffc46952012-07-06 14:19:23 +02001048 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001049 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001050}
1051
françois romieue6de30d2011-01-03 15:08:37 +00001052#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1053
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001054static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001055{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001056 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001057}
1058
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001059static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001060{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001061 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001062}
1063
Francois Romieu24192212012-07-06 20:19:42 +02001064static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001065{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001066 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001067
Francois Romieu24192212012-07-06 20:19:42 +02001068 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001069
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001070 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001071}
1072
Francois Romieu24192212012-07-06 20:19:42 +02001073static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001074{
1075 int value;
1076
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001077 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001078
Francois Romieu24192212012-07-06 20:19:42 +02001079 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001080
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001081 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001082
1083 return value;
1084}
1085
françois romieu4da19632011-01-03 15:07:55 +00001086static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001087{
Francois Romieu24192212012-07-06 20:19:42 +02001088 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001089}
1090
françois romieu4da19632011-01-03 15:07:55 +00001091static int rtl_readphy(struct rtl8169_private *tp, int location)
1092{
Francois Romieu24192212012-07-06 20:19:42 +02001093 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001094}
1095
1096static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1097{
1098 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1099}
1100
Chun-Hao Lin76564422014-10-01 23:17:17 +08001101static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001102{
1103 int val;
1104
françois romieu4da19632011-01-03 15:07:55 +00001105 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001106 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001107}
1108
Francois Romieuccdffb92008-07-26 14:26:06 +02001109static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1110 int val)
1111{
1112 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001113
françois romieu4da19632011-01-03 15:07:55 +00001114 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001115}
1116
1117static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1118{
1119 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001120
françois romieu4da19632011-01-03 15:07:55 +00001121 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001122}
1123
Francois Romieuffc46952012-07-06 14:19:23 +02001124DECLARE_RTL_COND(rtl_ephyar_cond)
1125{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001126 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001127}
1128
Francois Romieufdf6fc02012-07-06 22:40:38 +02001129static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001130{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001131 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
Francois Romieudacf8152008-08-02 20:44:13 +02001132 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1133
Francois Romieuffc46952012-07-06 14:19:23 +02001134 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1135
1136 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001137}
1138
Francois Romieufdf6fc02012-07-06 22:40:38 +02001139static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001140{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001141 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
Francois Romieudacf8152008-08-02 20:44:13 +02001142
Francois Romieuffc46952012-07-06 14:19:23 +02001143 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001144 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001145}
1146
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001147DECLARE_RTL_COND(rtl_eriar_cond)
1148{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001149 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001150}
1151
Francois Romieufdf6fc02012-07-06 22:40:38 +02001152static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1153 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001154{
Hayes Wang133ac402011-07-06 15:58:05 +08001155 BUG_ON((addr & 3) || (mask == 0));
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001156 RTL_W32(tp, ERIDR, val);
1157 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001158
Francois Romieuffc46952012-07-06 14:19:23 +02001159 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001160}
1161
Francois Romieufdf6fc02012-07-06 22:40:38 +02001162static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001163{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001164 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001165
Francois Romieuffc46952012-07-06 14:19:23 +02001166 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001167 RTL_R32(tp, ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001168}
1169
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001170static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001171 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001172{
1173 u32 val;
1174
Francois Romieufdf6fc02012-07-06 22:40:38 +02001175 val = rtl_eri_read(tp, addr, type);
1176 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001177}
1178
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001179static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1180{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001181 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001182 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001183 RTL_R32(tp, OCPDR) : ~0;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001184}
1185
1186static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1187{
1188 return rtl_eri_read(tp, reg, ERIAR_OOB);
1189}
1190
1191static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1192{
1193 switch (tp->mac_version) {
1194 case RTL_GIGA_MAC_VER_27:
1195 case RTL_GIGA_MAC_VER_28:
1196 case RTL_GIGA_MAC_VER_31:
1197 return r8168dp_ocp_read(tp, mask, reg);
1198 case RTL_GIGA_MAC_VER_49:
1199 case RTL_GIGA_MAC_VER_50:
1200 case RTL_GIGA_MAC_VER_51:
1201 return r8168ep_ocp_read(tp, mask, reg);
1202 default:
1203 BUG();
1204 return ~0;
1205 }
1206}
1207
1208static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1209 u32 data)
1210{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001211 RTL_W32(tp, OCPDR, data);
1212 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001213 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1214}
1215
1216static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1217 u32 data)
1218{
1219 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1220 data, ERIAR_OOB);
1221}
1222
1223static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1224{
1225 switch (tp->mac_version) {
1226 case RTL_GIGA_MAC_VER_27:
1227 case RTL_GIGA_MAC_VER_28:
1228 case RTL_GIGA_MAC_VER_31:
1229 r8168dp_ocp_write(tp, mask, reg, data);
1230 break;
1231 case RTL_GIGA_MAC_VER_49:
1232 case RTL_GIGA_MAC_VER_50:
1233 case RTL_GIGA_MAC_VER_51:
1234 r8168ep_ocp_write(tp, mask, reg, data);
1235 break;
1236 default:
1237 BUG();
1238 break;
1239 }
1240}
1241
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001242static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1243{
1244 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1245
1246 ocp_write(tp, 0x1, 0x30, 0x00000001);
1247}
1248
1249#define OOB_CMD_RESET 0x00
1250#define OOB_CMD_DRIVER_START 0x05
1251#define OOB_CMD_DRIVER_STOP 0x06
1252
1253static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1254{
1255 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1256}
1257
1258DECLARE_RTL_COND(rtl_ocp_read_cond)
1259{
1260 u16 reg;
1261
1262 reg = rtl8168_get_ocp_reg(tp);
1263
1264 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1265}
1266
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001267DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1268{
1269 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1270}
1271
1272DECLARE_RTL_COND(rtl_ocp_tx_cond)
1273{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001274 return RTL_R8(tp, IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001275}
1276
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001277static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1278{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001279 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001280 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001281 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1282 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001283}
1284
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001285static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001286{
1287 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001288 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1289}
1290
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001291static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1292{
1293 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1294 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1295 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1296}
1297
1298static void rtl8168_driver_start(struct rtl8169_private *tp)
1299{
1300 switch (tp->mac_version) {
1301 case RTL_GIGA_MAC_VER_27:
1302 case RTL_GIGA_MAC_VER_28:
1303 case RTL_GIGA_MAC_VER_31:
1304 rtl8168dp_driver_start(tp);
1305 break;
1306 case RTL_GIGA_MAC_VER_49:
1307 case RTL_GIGA_MAC_VER_50:
1308 case RTL_GIGA_MAC_VER_51:
1309 rtl8168ep_driver_start(tp);
1310 break;
1311 default:
1312 BUG();
1313 break;
1314 }
1315}
1316
1317static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1318{
1319 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1320 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1321}
1322
1323static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1324{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001325 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001326 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1327 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1328 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1329}
1330
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001331static void rtl8168_driver_stop(struct rtl8169_private *tp)
1332{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001333 switch (tp->mac_version) {
1334 case RTL_GIGA_MAC_VER_27:
1335 case RTL_GIGA_MAC_VER_28:
1336 case RTL_GIGA_MAC_VER_31:
1337 rtl8168dp_driver_stop(tp);
1338 break;
1339 case RTL_GIGA_MAC_VER_49:
1340 case RTL_GIGA_MAC_VER_50:
1341 case RTL_GIGA_MAC_VER_51:
1342 rtl8168ep_driver_stop(tp);
1343 break;
1344 default:
1345 BUG();
1346 break;
1347 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001348}
1349
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001350static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001351{
1352 u16 reg = rtl8168_get_ocp_reg(tp);
1353
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001354 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001355}
1356
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001357static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001358{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001359 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001360}
1361
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001362static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001363{
1364 switch (tp->mac_version) {
1365 case RTL_GIGA_MAC_VER_27:
1366 case RTL_GIGA_MAC_VER_28:
1367 case RTL_GIGA_MAC_VER_31:
1368 return r8168dp_check_dash(tp);
1369 case RTL_GIGA_MAC_VER_49:
1370 case RTL_GIGA_MAC_VER_50:
1371 case RTL_GIGA_MAC_VER_51:
1372 return r8168ep_check_dash(tp);
1373 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001374 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001375 }
1376}
1377
françois romieuc28aa382011-08-02 03:53:43 +00001378struct exgmac_reg {
1379 u16 addr;
1380 u16 mask;
1381 u32 val;
1382};
1383
Francois Romieufdf6fc02012-07-06 22:40:38 +02001384static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001385 const struct exgmac_reg *r, int len)
1386{
1387 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001388 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001389 r++;
1390 }
1391}
1392
Francois Romieuffc46952012-07-06 14:19:23 +02001393DECLARE_RTL_COND(rtl_efusear_cond)
1394{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001395 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001396}
1397
Francois Romieufdf6fc02012-07-06 22:40:38 +02001398static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001399{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001400 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
françois romieudaf9df62009-10-07 12:44:20 +00001401
Francois Romieuffc46952012-07-06 14:19:23 +02001402 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001403 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001404}
1405
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001406static u16 rtl_get_events(struct rtl8169_private *tp)
1407{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001408 return RTL_R16(tp, IntrStatus);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001409}
1410
1411static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1412{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001413 RTL_W16(tp, IntrStatus, bits);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001414 mmiowb();
1415}
1416
1417static void rtl_irq_disable(struct rtl8169_private *tp)
1418{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001419 RTL_W16(tp, IntrMask, 0);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001420 mmiowb();
1421}
1422
Francois Romieu3e990ff2012-01-26 12:50:01 +01001423static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1424{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001425 RTL_W16(tp, IntrMask, bits);
Francois Romieu3e990ff2012-01-26 12:50:01 +01001426}
1427
Francois Romieuda78dbf2012-01-26 14:18:23 +01001428#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1429#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1430#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1431
1432static void rtl_irq_enable_all(struct rtl8169_private *tp)
1433{
1434 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1435}
1436
françois romieu811fd302011-12-04 20:30:45 +00001437static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001439 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001440 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001441 RTL_R8(tp, ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
françois romieu4da19632011-01-03 15:07:55 +00001444static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
françois romieu4da19632011-01-03 15:07:55 +00001446 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
françois romieu4da19632011-01-03 15:07:55 +00001449static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 unsigned int val;
1452
françois romieu4da19632011-01-03 15:07:55 +00001453 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1454 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
Hayes Wang70090422011-07-06 15:58:06 +08001457static void rtl_link_chg_patch(struct rtl8169_private *tp)
1458{
Hayes Wang70090422011-07-06 15:58:06 +08001459 struct net_device *dev = tp->dev;
1460
1461 if (!netif_running(dev))
1462 return;
1463
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001464 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1465 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001466 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001467 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1468 ERIAR_EXGMAC);
1469 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1470 ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001471 } else if (RTL_R8(tp, PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001472 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1473 ERIAR_EXGMAC);
1474 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1475 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001476 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001477 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1478 ERIAR_EXGMAC);
1479 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1480 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001481 }
1482 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001483 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001484 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001485 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001486 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001487 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1488 tp->mac_version == RTL_GIGA_MAC_VER_36) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001489 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001490 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1491 ERIAR_EXGMAC);
1492 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1493 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001494 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001495 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1496 ERIAR_EXGMAC);
1497 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1498 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001499 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001500 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001501 if (RTL_R8(tp, PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001502 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1503 ERIAR_EXGMAC);
1504 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1505 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001506 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001507 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1508 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001509 }
Hayes Wang70090422011-07-06 15:58:06 +08001510 }
1511}
1512
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001513#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1514
1515static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1516{
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001517 u8 options;
1518 u32 wolopts = 0;
1519
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001520 options = RTL_R8(tp, Config1);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001521 if (!(options & PMEnable))
1522 return 0;
1523
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001524 options = RTL_R8(tp, Config3);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001525 if (options & LinkUp)
1526 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001527 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001528 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1529 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001530 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1531 wolopts |= WAKE_MAGIC;
1532 break;
1533 default:
1534 if (options & MagicPacket)
1535 wolopts |= WAKE_MAGIC;
1536 break;
1537 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001538
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001539 options = RTL_R8(tp, Config5);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001540 if (options & UWF)
1541 wolopts |= WAKE_UCAST;
1542 if (options & BWF)
1543 wolopts |= WAKE_BCAST;
1544 if (options & MWF)
1545 wolopts |= WAKE_MCAST;
1546
1547 return wolopts;
1548}
1549
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001550static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1551{
1552 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001553
Francois Romieuda78dbf2012-01-26 14:18:23 +01001554 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001555 wol->supported = WAKE_ANY;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001556 wol->wolopts = tp->saved_wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001557 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001558}
1559
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001560static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001561{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001562 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001563 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001564 u32 opt;
1565 u16 reg;
1566 u8 mask;
1567 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001568 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001569 { WAKE_UCAST, Config5, UWF },
1570 { WAKE_BCAST, Config5, BWF },
1571 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001572 { WAKE_ANY, Config5, LanWake },
1573 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001574 };
Francois Romieu851e6022012-04-17 11:10:11 +02001575 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001576
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001577 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001578
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001579 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001580 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1581 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001582 tmp = ARRAY_SIZE(cfg) - 1;
1583 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001584 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001585 0x0dc,
1586 ERIAR_MASK_0100,
1587 MagicPacket_v2,
1588 0x0000,
1589 ERIAR_EXGMAC);
1590 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001591 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001592 0x0dc,
1593 ERIAR_MASK_0100,
1594 0x0000,
1595 MagicPacket_v2,
1596 ERIAR_EXGMAC);
1597 break;
1598 default:
1599 tmp = ARRAY_SIZE(cfg);
1600 break;
1601 }
1602
1603 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001604 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001605 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001606 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001607 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001608 }
1609
Francois Romieu851e6022012-04-17 11:10:11 +02001610 switch (tp->mac_version) {
1611 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001612 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001613 if (wolopts)
1614 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001615 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001616 break;
1617 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001618 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001619 if (wolopts)
1620 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001621 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001622 break;
1623 }
1624
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001625 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001626}
1627
1628static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1629{
1630 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001631 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001632
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001633 if (wol->wolopts & ~WAKE_ANY)
1634 return -EINVAL;
1635
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001636 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001637
Francois Romieuda78dbf2012-01-26 14:18:23 +01001638 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001639
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001640 tp->saved_wolopts = wol->wolopts;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001641
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001642 if (pm_runtime_active(d))
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001643 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001644
1645 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001646
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001647 device_set_wakeup_enable(d, tp->saved_wolopts);
françois romieuea809072010-11-08 13:23:58 +00001648
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001649 pm_runtime_put_noidle(d);
1650
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001651 return 0;
1652}
1653
Francois Romieu31bd2042011-04-26 18:58:59 +02001654static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1655{
Francois Romieu85bffe62011-04-27 08:22:39 +02001656 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001657}
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659static void rtl8169_get_drvinfo(struct net_device *dev,
1660 struct ethtool_drvinfo *info)
1661{
1662 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001663 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Rick Jones68aad782011-11-07 13:29:27 +00001665 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1666 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1667 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001668 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001669 if (!IS_ERR_OR_NULL(rtl_fw))
1670 strlcpy(info->fw_version, rtl_fw->version,
1671 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
1674static int rtl8169_get_regs_len(struct net_device *dev)
1675{
1676 return R8169_REGS_SIZE;
1677}
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001680 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
1682 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001683 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001684 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Hayes Wang716b50a2011-02-22 17:26:18 +08001686 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001689 int auto_nego;
1690
françois romieu4da19632011-01-03 15:07:55 +00001691 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001692 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1693 ADVERTISE_100HALF | ADVERTISE_100FULL);
1694
1695 if (adv & ADVERTISED_10baseT_Half)
1696 auto_nego |= ADVERTISE_10HALF;
1697 if (adv & ADVERTISED_10baseT_Full)
1698 auto_nego |= ADVERTISE_10FULL;
1699 if (adv & ADVERTISED_100baseT_Half)
1700 auto_nego |= ADVERTISE_100HALF;
1701 if (adv & ADVERTISED_100baseT_Full)
1702 auto_nego |= ADVERTISE_100FULL;
1703
françois romieu3577aa12009-05-19 10:46:48 +00001704 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1705
françois romieu4da19632011-01-03 15:07:55 +00001706 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001707 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1708
1709 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001710 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001711 if (adv & ADVERTISED_1000baseT_Half)
1712 giga_ctrl |= ADVERTISE_1000HALF;
1713 if (adv & ADVERTISED_1000baseT_Full)
1714 giga_ctrl |= ADVERTISE_1000FULL;
1715 } else if (adv & (ADVERTISED_1000baseT_Half |
1716 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001717 netif_info(tp, link, dev,
1718 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001719 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
françois romieu3577aa12009-05-19 10:46:48 +00001722 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001723
françois romieu4da19632011-01-03 15:07:55 +00001724 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1725 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001726 } else {
françois romieu3577aa12009-05-19 10:46:48 +00001727 if (speed == SPEED_10)
1728 bmcr = 0;
1729 else if (speed == SPEED_100)
1730 bmcr = BMCR_SPEED100;
1731 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001732 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001733
1734 if (duplex == DUPLEX_FULL)
1735 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001736 }
1737
françois romieu4da19632011-01-03 15:07:55 +00001738 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001739
Francois Romieucecb5fd2011-04-01 10:21:07 +02001740 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1741 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001742 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001743 rtl_writephy(tp, 0x17, 0x2138);
1744 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001745 } else {
françois romieu4da19632011-01-03 15:07:55 +00001746 rtl_writephy(tp, 0x17, 0x2108);
1747 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001748 }
1749 }
1750
Oliver Neukum54405cd2011-01-06 21:55:13 +01001751 rc = 0;
1752out:
1753 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
1756static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001757 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Heiner Kallweit335c9972018-07-01 00:25:19 +02001759 return rtl8169_set_speed_xmii(dev, autoneg, speed, duplex, advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001762static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1763 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Francois Romieud58d46b2011-05-03 16:38:29 +02001765 struct rtl8169_private *tp = netdev_priv(dev);
1766
Francois Romieu2b7b4312011-04-18 22:53:24 -07001767 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001768 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Francois Romieud58d46b2011-05-03 16:38:29 +02001770 if (dev->mtu > JUMBO_1K &&
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02001771 tp->mac_version > RTL_GIGA_MAC_VER_06)
Francois Romieud58d46b2011-05-03 16:38:29 +02001772 features &= ~NETIF_F_IP_CSUM;
1773
Michał Mirosław350fb322011-04-08 06:35:56 +00001774 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
Heiner Kallweita3984572018-04-28 22:19:15 +02001777static int rtl8169_set_features(struct net_device *dev,
1778 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
1780 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001781 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Heiner Kallweita3984572018-04-28 22:19:15 +02001783 rtl_lock_work(tp);
1784
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001785 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001786 if (features & NETIF_F_RXALL)
1787 rx_config |= (AcceptErr | AcceptRunt);
1788 else
1789 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001791 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001792
hayeswang929a0312014-09-16 11:40:47 +08001793 if (features & NETIF_F_RXCSUM)
1794 tp->cp_cmd |= RxChkSum;
1795 else
1796 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001797
hayeswang929a0312014-09-16 11:40:47 +08001798 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1799 tp->cp_cmd |= RxVlan;
1800 else
1801 tp->cp_cmd &= ~RxVlan;
1802
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001803 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1804 RTL_R16(tp, CPlusCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Francois Romieuda78dbf2012-01-26 14:18:23 +01001806 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 return 0;
1809}
1810
Kirill Smelkov810f4892012-11-10 21:11:02 +04001811static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001813 return (skb_vlan_tag_present(skb)) ?
1814 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
Francois Romieu7a8fc772011-03-01 17:18:33 +01001817static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
1819 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Francois Romieu7a8fc772011-03-01 17:18:33 +01001821 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001822 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
Heiner Kallweite3972862018-06-29 08:07:04 +02001825static int rtl8169_get_link_ksettings(struct net_device *dev,
1826 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
1828 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03001830 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
1831
1832 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01001835static int rtl8169_set_link_ksettings(struct net_device *dev,
1836 const struct ethtool_link_ksettings *cmd)
1837{
1838 struct rtl8169_private *tp = netdev_priv(dev);
1839 int rc;
1840 u32 advertising;
1841
1842 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
1843 cmd->link_modes.advertising))
1844 return -EINVAL;
1845
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01001846 rtl_lock_work(tp);
1847 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
1848 cmd->base.duplex, advertising);
1849 rtl_unlock_work(tp);
1850
1851 return rc;
1852}
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1855 void *p)
1856{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001857 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02001858 u32 __iomem *data = tp->mmio_addr;
1859 u32 *dw = p;
1860 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Francois Romieuda78dbf2012-01-26 14:18:23 +01001862 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02001863 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1864 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001865 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001868static u32 rtl8169_get_msglevel(struct net_device *dev)
1869{
1870 struct rtl8169_private *tp = netdev_priv(dev);
1871
1872 return tp->msg_enable;
1873}
1874
1875static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1876{
1877 struct rtl8169_private *tp = netdev_priv(dev);
1878
1879 tp->msg_enable = value;
1880}
1881
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001882static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1883 "tx_packets",
1884 "rx_packets",
1885 "tx_errors",
1886 "rx_errors",
1887 "rx_missed",
1888 "align_errors",
1889 "tx_single_collisions",
1890 "tx_multi_collisions",
1891 "unicast",
1892 "broadcast",
1893 "multicast",
1894 "tx_aborted",
1895 "tx_underrun",
1896};
1897
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001898static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001899{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001900 switch (sset) {
1901 case ETH_SS_STATS:
1902 return ARRAY_SIZE(rtl8169_gstrings);
1903 default:
1904 return -EOPNOTSUPP;
1905 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001906}
1907
Corinna Vinschen42020322015-09-10 10:47:35 +02001908DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001909{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001910 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001911}
1912
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001913static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001914{
Corinna Vinschen42020322015-09-10 10:47:35 +02001915 dma_addr_t paddr = tp->counters_phys_addr;
1916 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02001917
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001918 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
1919 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02001920 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001921 RTL_W32(tp, CounterAddrLow, cmd);
1922 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02001923
Francois Romieua78e9362018-01-26 01:53:26 +01001924 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001925}
1926
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001927static bool rtl8169_reset_counters(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001928{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001929 /*
1930 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
1931 * tally counters.
1932 */
1933 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
1934 return true;
1935
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001936 return rtl8169_do_counters(tp, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02001937}
1938
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001939static bool rtl8169_update_counters(struct rtl8169_private *tp)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001940{
Ivan Vecera355423d2009-02-06 21:49:57 -08001941 /*
1942 * Some chips are unable to dump tally counters when the receiver
1943 * is disabled.
1944 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001945 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001946 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001947
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001948 return rtl8169_do_counters(tp, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001949}
1950
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001951static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001952{
Corinna Vinschen42020322015-09-10 10:47:35 +02001953 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001954 bool ret = false;
1955
1956 /*
1957 * rtl8169_init_counter_offsets is called from rtl_open. On chip
1958 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
1959 * reset by a power cycle, while the counter values collected by the
1960 * driver are reset at every driver unload/load cycle.
1961 *
1962 * To make sure the HW values returned by @get_stats64 match the SW
1963 * values, we collect the initial values at first open(*) and use them
1964 * as offsets to normalize the values returned by @get_stats64.
1965 *
1966 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
1967 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
1968 * set at open time by rtl_hw_start.
1969 */
1970
1971 if (tp->tc_offset.inited)
1972 return true;
1973
1974 /* If both, reset and update fail, propagate to caller. */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001975 if (rtl8169_reset_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001976 ret = true;
1977
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001978 if (rtl8169_update_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001979 ret = true;
1980
Corinna Vinschen42020322015-09-10 10:47:35 +02001981 tp->tc_offset.tx_errors = counters->tx_errors;
1982 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
1983 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001984 tp->tc_offset.inited = true;
1985
1986 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001987}
1988
Ivan Vecera355423d2009-02-06 21:49:57 -08001989static void rtl8169_get_ethtool_stats(struct net_device *dev,
1990 struct ethtool_stats *stats, u64 *data)
1991{
1992 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001993 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02001994 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08001995
1996 ASSERT_RTNL();
1997
Chun-Hao Line0636232016-07-29 16:37:55 +08001998 pm_runtime_get_noresume(d);
1999
2000 if (pm_runtime_active(d))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002001 rtl8169_update_counters(tp);
Chun-Hao Line0636232016-07-29 16:37:55 +08002002
2003 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08002004
Corinna Vinschen42020322015-09-10 10:47:35 +02002005 data[0] = le64_to_cpu(counters->tx_packets);
2006 data[1] = le64_to_cpu(counters->rx_packets);
2007 data[2] = le64_to_cpu(counters->tx_errors);
2008 data[3] = le32_to_cpu(counters->rx_errors);
2009 data[4] = le16_to_cpu(counters->rx_missed);
2010 data[5] = le16_to_cpu(counters->align_errors);
2011 data[6] = le32_to_cpu(counters->tx_one_collision);
2012 data[7] = le32_to_cpu(counters->tx_multi_collision);
2013 data[8] = le64_to_cpu(counters->rx_unicast);
2014 data[9] = le64_to_cpu(counters->rx_broadcast);
2015 data[10] = le32_to_cpu(counters->rx_multicast);
2016 data[11] = le16_to_cpu(counters->tx_aborted);
2017 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08002018}
2019
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002020static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2021{
2022 switch(stringset) {
2023 case ETH_SS_STATS:
2024 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2025 break;
2026 }
2027}
2028
Florian Fainellif0903ea2016-12-03 12:01:19 -08002029static int rtl8169_nway_reset(struct net_device *dev)
2030{
2031 struct rtl8169_private *tp = netdev_priv(dev);
2032
2033 return mii_nway_restart(&tp->mii);
2034}
2035
Francois Romieu50970832017-10-27 13:24:49 +03002036/*
2037 * Interrupt coalescing
2038 *
2039 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2040 * > 8169, 8168 and 810x line of chipsets
2041 *
2042 * 8169, 8168, and 8136(810x) serial chipsets support it.
2043 *
2044 * > 2 - the Tx timer unit at gigabit speed
2045 *
2046 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2047 * (0xe0) bit 1 and bit 0.
2048 *
2049 * For 8169
2050 * bit[1:0] \ speed 1000M 100M 10M
2051 * 0 0 320ns 2.56us 40.96us
2052 * 0 1 2.56us 20.48us 327.7us
2053 * 1 0 5.12us 40.96us 655.4us
2054 * 1 1 10.24us 81.92us 1.31ms
2055 *
2056 * For the other
2057 * bit[1:0] \ speed 1000M 100M 10M
2058 * 0 0 5us 2.56us 40.96us
2059 * 0 1 40us 20.48us 327.7us
2060 * 1 0 80us 40.96us 655.4us
2061 * 1 1 160us 81.92us 1.31ms
2062 */
2063
2064/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2065struct rtl_coalesce_scale {
2066 /* Rx / Tx */
2067 u32 nsecs[2];
2068};
2069
2070/* rx/tx scale factors for all CPlusCmd[0:1] cases */
2071struct rtl_coalesce_info {
2072 u32 speed;
2073 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2074};
2075
2076/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2077#define rxtx_x1822(r, t) { \
2078 {{(r), (t)}}, \
2079 {{(r)*8, (t)*8}}, \
2080 {{(r)*8*2, (t)*8*2}}, \
2081 {{(r)*8*2*2, (t)*8*2*2}}, \
2082}
2083static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2084 /* speed delays: rx00 tx00 */
2085 { SPEED_10, rxtx_x1822(40960, 40960) },
2086 { SPEED_100, rxtx_x1822( 2560, 2560) },
2087 { SPEED_1000, rxtx_x1822( 320, 320) },
2088 { 0 },
2089};
2090
2091static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2092 /* speed delays: rx00 tx00 */
2093 { SPEED_10, rxtx_x1822(40960, 40960) },
2094 { SPEED_100, rxtx_x1822( 2560, 2560) },
2095 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2096 { 0 },
2097};
2098#undef rxtx_x1822
2099
2100/* get rx/tx scale vector corresponding to current speed */
2101static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2102{
2103 struct rtl8169_private *tp = netdev_priv(dev);
2104 struct ethtool_link_ksettings ecmd;
2105 const struct rtl_coalesce_info *ci;
2106 int rc;
2107
2108 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2109 if (rc < 0)
2110 return ERR_PTR(rc);
2111
2112 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2113 if (ecmd.base.speed == ci->speed) {
2114 return ci;
2115 }
2116 }
2117
2118 return ERR_PTR(-ELNRNG);
2119}
2120
2121static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2122{
2123 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002124 const struct rtl_coalesce_info *ci;
2125 const struct rtl_coalesce_scale *scale;
2126 struct {
2127 u32 *max_frames;
2128 u32 *usecs;
2129 } coal_settings [] = {
2130 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2131 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2132 }, *p = coal_settings;
2133 int i;
2134 u16 w;
2135
2136 memset(ec, 0, sizeof(*ec));
2137
2138 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2139 ci = rtl_coalesce_info(dev);
2140 if (IS_ERR(ci))
2141 return PTR_ERR(ci);
2142
Heiner Kallweit0ae09742018-04-28 22:19:26 +02002143 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
Francois Romieu50970832017-10-27 13:24:49 +03002144
2145 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002146 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03002147 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2148 w >>= RTL_COALESCE_SHIFT;
2149 *p->usecs = w & RTL_COALESCE_MASK;
2150 }
2151
2152 for (i = 0; i < 2; i++) {
2153 p = coal_settings + i;
2154 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2155
2156 /*
2157 * ethtool_coalesce says it is illegal to set both usecs and
2158 * max_frames to 0.
2159 */
2160 if (!*p->usecs && !*p->max_frames)
2161 *p->max_frames = 1;
2162 }
2163
2164 return 0;
2165}
2166
2167/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2168static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2169 struct net_device *dev, u32 nsec, u16 *cp01)
2170{
2171 const struct rtl_coalesce_info *ci;
2172 u16 i;
2173
2174 ci = rtl_coalesce_info(dev);
2175 if (IS_ERR(ci))
2176 return ERR_CAST(ci);
2177
2178 for (i = 0; i < 4; i++) {
2179 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2180 ci->scalev[i].nsecs[1]);
2181 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2182 *cp01 = i;
2183 return &ci->scalev[i];
2184 }
2185 }
2186
2187 return ERR_PTR(-EINVAL);
2188}
2189
2190static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2191{
2192 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002193 const struct rtl_coalesce_scale *scale;
2194 struct {
2195 u32 frames;
2196 u32 usecs;
2197 } coal_settings [] = {
2198 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2199 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2200 }, *p = coal_settings;
2201 u16 w = 0, cp01;
2202 int i;
2203
2204 scale = rtl_coalesce_choose_scale(dev,
2205 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2206 if (IS_ERR(scale))
2207 return PTR_ERR(scale);
2208
2209 for (i = 0; i < 2; i++, p++) {
2210 u32 units;
2211
2212 /*
2213 * accept max_frames=1 we returned in rtl_get_coalesce.
2214 * accept it not only when usecs=0 because of e.g. the following scenario:
2215 *
2216 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2217 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2218 * - then user does `ethtool -C eth0 rx-usecs 100`
2219 *
2220 * since ethtool sends to kernel whole ethtool_coalesce
2221 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2222 * we'll reject it below in `frames % 4 != 0`.
2223 */
2224 if (p->frames == 1) {
2225 p->frames = 0;
2226 }
2227
2228 units = p->usecs * 1000 / scale->nsecs[i];
2229 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2230 return -EINVAL;
2231
2232 w <<= RTL_COALESCE_SHIFT;
2233 w |= units;
2234 w <<= RTL_COALESCE_SHIFT;
2235 w |= p->frames >> 2;
2236 }
2237
2238 rtl_lock_work(tp);
2239
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002240 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03002241
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +02002242 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002243 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2244 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03002245
2246 rtl_unlock_work(tp);
2247
2248 return 0;
2249}
2250
Jeff Garzik7282d492006-09-13 14:30:00 -04002251static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 .get_drvinfo = rtl8169_get_drvinfo,
2253 .get_regs_len = rtl8169_get_regs_len,
2254 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002255 .get_coalesce = rtl_get_coalesce,
2256 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002257 .get_msglevel = rtl8169_get_msglevel,
2258 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002260 .get_wol = rtl8169_get_wol,
2261 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002262 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002263 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002264 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002265 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainellif0903ea2016-12-03 12:01:19 -08002266 .nway_reset = rtl8169_nway_reset,
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002267 .get_link_ksettings = rtl8169_get_link_ksettings,
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002268 .set_link_ksettings = rtl8169_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269};
2270
Francois Romieu07d3f512007-02-21 22:40:46 +01002271static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Heiner Kallweit22148df2018-04-22 17:15:15 +02002272 u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
Francois Romieu0e485152007-02-20 00:00:26 +01002274 /*
2275 * The driver currently handles the 8168Bf and the 8168Be identically
2276 * but they can be identified more specifically through the test below
2277 * if needed:
2278 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002279 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002280 *
2281 * Same thing for the 8101Eb and the 8101Ec:
2282 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002283 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002284 */
Francois Romieu37441002011-06-17 22:58:54 +02002285 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002287 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 int mac_version;
2289 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002290 /* 8168EP family. */
2291 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2292 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2293 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2294
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002295 /* 8168H family. */
2296 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2297 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2298
Hayes Wangc5583862012-07-02 17:23:22 +08002299 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002300 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002301 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002302 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2303 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2304
Hayes Wangc2218922011-09-06 16:55:18 +08002305 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002306 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002307 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2308 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2309
hayeswang01dc7fe2011-03-21 01:50:28 +00002310 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002311 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002312 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2313 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2314
Francois Romieu5b538df2008-07-20 16:22:45 +02002315 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002316 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002317 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002318
françois romieue6de30d2011-01-03 15:08:37 +00002319 /* 8168DP family. */
2320 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2321 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002322 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002323
Francois Romieuef808d52008-06-29 13:10:54 +02002324 /* 8168C family. */
Francois Romieuef3386f2008-06-29 12:24:30 +02002325 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002326 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002327 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002328 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2329 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002330 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieuef808d52008-06-29 13:10:54 +02002331 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002332
2333 /* 8168B family. */
2334 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002335 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2336 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2337
2338 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002339 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002340 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002341 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2342 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002343 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2344 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2345 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2346 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002347 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002348 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002349 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002350 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2351 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002352 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2353 /* FIXME: where did these entries come from ? -- FR */
2354 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2355 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2356
2357 /* 8110 family. */
2358 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2359 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2360 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2361 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2362 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2363 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2364
Jean Delvaref21b75e2009-05-26 20:54:48 -07002365 /* Catch-all */
2366 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002367 };
2368 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 u32 reg;
2370
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002371 reg = RTL_R32(tp, TxConfig);
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002372 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 p++;
2374 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002375
2376 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02002377 dev_notice(tp_to_dev(tp),
2378 "unknown MAC, using family default\n");
Francois Romieu5d320a22011-05-08 17:47:36 +02002379 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002380 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2381 tp->mac_version = tp->mii.supports_gmii ?
2382 RTL_GIGA_MAC_VER_42 :
2383 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002384 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2385 tp->mac_version = tp->mii.supports_gmii ?
2386 RTL_GIGA_MAC_VER_45 :
2387 RTL_GIGA_MAC_VER_47;
2388 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2389 tp->mac_version = tp->mii.supports_gmii ?
2390 RTL_GIGA_MAC_VER_46 :
2391 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393}
2394
2395static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2396{
Heiner Kallweit49d17512018-06-28 20:36:15 +02002397 netif_dbg(tp, drv, tp->dev, "mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398}
2399
Francois Romieu867763c2007-08-17 18:21:58 +02002400struct phy_reg {
2401 u16 reg;
2402 u16 val;
2403};
2404
françois romieu4da19632011-01-03 15:07:55 +00002405static void rtl_writephy_batch(struct rtl8169_private *tp,
2406 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002407{
2408 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002409 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002410 regs++;
2411 }
2412}
2413
françois romieubca03d52011-01-03 15:07:31 +00002414#define PHY_READ 0x00000000
2415#define PHY_DATA_OR 0x10000000
2416#define PHY_DATA_AND 0x20000000
2417#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002418#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002419#define PHY_CLEAR_READCOUNT 0x70000000
2420#define PHY_WRITE 0x80000000
2421#define PHY_READCOUNT_EQ_SKIP 0x90000000
2422#define PHY_COMP_EQ_SKIPN 0xa0000000
2423#define PHY_COMP_NEQ_SKIPN 0xb0000000
2424#define PHY_WRITE_PREVIOUS 0xc0000000
2425#define PHY_SKIPN 0xd0000000
2426#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002427
Hayes Wang960aee62011-06-18 11:37:48 +02002428struct fw_info {
2429 u32 magic;
2430 char version[RTL_VER_SIZE];
2431 __le32 fw_start;
2432 __le32 fw_len;
2433 u8 chksum;
2434} __packed;
2435
Francois Romieu1c361ef2011-06-17 17:16:24 +02002436#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2437
2438static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002439{
Francois Romieub6ffd972011-06-17 17:00:05 +02002440 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002441 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002442 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2443 char *version = rtl_fw->version;
2444 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002445
Francois Romieu1c361ef2011-06-17 17:16:24 +02002446 if (fw->size < FW_OPCODE_SIZE)
2447 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002448
2449 if (!fw_info->magic) {
2450 size_t i, size, start;
2451 u8 checksum = 0;
2452
2453 if (fw->size < sizeof(*fw_info))
2454 goto out;
2455
2456 for (i = 0; i < fw->size; i++)
2457 checksum += fw->data[i];
2458 if (checksum != 0)
2459 goto out;
2460
2461 start = le32_to_cpu(fw_info->fw_start);
2462 if (start > fw->size)
2463 goto out;
2464
2465 size = le32_to_cpu(fw_info->fw_len);
2466 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2467 goto out;
2468
2469 memcpy(version, fw_info->version, RTL_VER_SIZE);
2470
2471 pa->code = (__le32 *)(fw->data + start);
2472 pa->size = size;
2473 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002474 if (fw->size % FW_OPCODE_SIZE)
2475 goto out;
2476
2477 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2478
2479 pa->code = (__le32 *)fw->data;
2480 pa->size = fw->size / FW_OPCODE_SIZE;
2481 }
2482 version[RTL_VER_SIZE - 1] = 0;
2483
2484 rc = true;
2485out:
2486 return rc;
2487}
2488
Francois Romieufd112f22011-06-18 00:10:29 +02002489static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2490 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002491{
Francois Romieufd112f22011-06-18 00:10:29 +02002492 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002493 size_t index;
2494
Francois Romieu1c361ef2011-06-17 17:16:24 +02002495 for (index = 0; index < pa->size; index++) {
2496 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002497 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002498
hayeswang42b82dc2011-01-10 02:07:25 +00002499 switch(action & 0xf0000000) {
2500 case PHY_READ:
2501 case PHY_DATA_OR:
2502 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002503 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002504 case PHY_CLEAR_READCOUNT:
2505 case PHY_WRITE:
2506 case PHY_WRITE_PREVIOUS:
2507 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002508 break;
2509
hayeswang42b82dc2011-01-10 02:07:25 +00002510 case PHY_BJMPN:
2511 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002512 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002513 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002514 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002515 }
2516 break;
2517 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002518 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002519 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002520 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002521 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002522 }
2523 break;
2524 case PHY_COMP_EQ_SKIPN:
2525 case PHY_COMP_NEQ_SKIPN:
2526 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002527 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002528 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002529 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002530 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002531 }
2532 break;
2533
hayeswang42b82dc2011-01-10 02:07:25 +00002534 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002535 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002536 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002537 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002538 }
2539 }
Francois Romieufd112f22011-06-18 00:10:29 +02002540 rc = true;
2541out:
2542 return rc;
2543}
françois romieubca03d52011-01-03 15:07:31 +00002544
Francois Romieufd112f22011-06-18 00:10:29 +02002545static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2546{
2547 struct net_device *dev = tp->dev;
2548 int rc = -EINVAL;
2549
2550 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002551 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002552 goto out;
2553 }
2554
2555 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2556 rc = 0;
2557out:
2558 return rc;
2559}
2560
2561static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2562{
2563 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002564 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002565 u32 predata, count;
2566 size_t index;
2567
2568 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002569 org.write = ops->write;
2570 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002571
Francois Romieu1c361ef2011-06-17 17:16:24 +02002572 for (index = 0; index < pa->size; ) {
2573 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002574 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002575 u32 regno = (action & 0x0fff0000) >> 16;
2576
2577 if (!action)
2578 break;
françois romieubca03d52011-01-03 15:07:31 +00002579
2580 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002581 case PHY_READ:
2582 predata = rtl_readphy(tp, regno);
2583 count++;
2584 index++;
françois romieubca03d52011-01-03 15:07:31 +00002585 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002586 case PHY_DATA_OR:
2587 predata |= data;
2588 index++;
2589 break;
2590 case PHY_DATA_AND:
2591 predata &= data;
2592 index++;
2593 break;
2594 case PHY_BJMPN:
2595 index -= regno;
2596 break;
hayeswangeee37862013-04-01 22:23:38 +00002597 case PHY_MDIO_CHG:
2598 if (data == 0) {
2599 ops->write = org.write;
2600 ops->read = org.read;
2601 } else if (data == 1) {
2602 ops->write = mac_mcu_write;
2603 ops->read = mac_mcu_read;
2604 }
2605
hayeswang42b82dc2011-01-10 02:07:25 +00002606 index++;
2607 break;
2608 case PHY_CLEAR_READCOUNT:
2609 count = 0;
2610 index++;
2611 break;
2612 case PHY_WRITE:
2613 rtl_writephy(tp, regno, data);
2614 index++;
2615 break;
2616 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002617 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002618 break;
2619 case PHY_COMP_EQ_SKIPN:
2620 if (predata == data)
2621 index += regno;
2622 index++;
2623 break;
2624 case PHY_COMP_NEQ_SKIPN:
2625 if (predata != data)
2626 index += regno;
2627 index++;
2628 break;
2629 case PHY_WRITE_PREVIOUS:
2630 rtl_writephy(tp, regno, predata);
2631 index++;
2632 break;
2633 case PHY_SKIPN:
2634 index += regno + 1;
2635 break;
2636 case PHY_DELAY_MS:
2637 mdelay(data);
2638 index++;
2639 break;
2640
françois romieubca03d52011-01-03 15:07:31 +00002641 default:
2642 BUG();
2643 }
2644 }
hayeswangeee37862013-04-01 22:23:38 +00002645
2646 ops->write = org.write;
2647 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002648}
2649
françois romieuf1e02ed2011-01-13 13:07:53 +00002650static void rtl_release_firmware(struct rtl8169_private *tp)
2651{
Francois Romieub6ffd972011-06-17 17:00:05 +02002652 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2653 release_firmware(tp->rtl_fw->fw);
2654 kfree(tp->rtl_fw);
2655 }
2656 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002657}
2658
François Romieu953a12c2011-04-24 17:38:48 +02002659static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002660{
Francois Romieub6ffd972011-06-17 17:00:05 +02002661 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002662
2663 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002664 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002665 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002666}
2667
2668static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2669{
2670 if (rtl_readphy(tp, reg) != val)
2671 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2672 else
2673 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002674}
2675
françois romieu4da19632011-01-03 15:07:55 +00002676static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002678 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002679 { 0x1f, 0x0001 },
2680 { 0x06, 0x006e },
2681 { 0x08, 0x0708 },
2682 { 0x15, 0x4000 },
2683 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
françois romieu0b9b5712009-08-10 19:44:56 +00002685 { 0x1f, 0x0001 },
2686 { 0x03, 0x00a1 },
2687 { 0x02, 0x0008 },
2688 { 0x01, 0x0120 },
2689 { 0x00, 0x1000 },
2690 { 0x04, 0x0800 },
2691 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
françois romieu0b9b5712009-08-10 19:44:56 +00002693 { 0x03, 0xff41 },
2694 { 0x02, 0xdf60 },
2695 { 0x01, 0x0140 },
2696 { 0x00, 0x0077 },
2697 { 0x04, 0x7800 },
2698 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
françois romieu0b9b5712009-08-10 19:44:56 +00002700 { 0x03, 0x802f },
2701 { 0x02, 0x4f02 },
2702 { 0x01, 0x0409 },
2703 { 0x00, 0xf0f9 },
2704 { 0x04, 0x9800 },
2705 { 0x04, 0x9000 },
2706
2707 { 0x03, 0xdf01 },
2708 { 0x02, 0xdf20 },
2709 { 0x01, 0xff95 },
2710 { 0x00, 0xba00 },
2711 { 0x04, 0xa800 },
2712 { 0x04, 0xa000 },
2713
2714 { 0x03, 0xff41 },
2715 { 0x02, 0xdf20 },
2716 { 0x01, 0x0140 },
2717 { 0x00, 0x00bb },
2718 { 0x04, 0xb800 },
2719 { 0x04, 0xb000 },
2720
2721 { 0x03, 0xdf41 },
2722 { 0x02, 0xdc60 },
2723 { 0x01, 0x6340 },
2724 { 0x00, 0x007d },
2725 { 0x04, 0xd800 },
2726 { 0x04, 0xd000 },
2727
2728 { 0x03, 0xdf01 },
2729 { 0x02, 0xdf20 },
2730 { 0x01, 0x100a },
2731 { 0x00, 0xa0ff },
2732 { 0x04, 0xf800 },
2733 { 0x04, 0xf000 },
2734
2735 { 0x1f, 0x0000 },
2736 { 0x0b, 0x0000 },
2737 { 0x00, 0x9200 }
2738 };
2739
françois romieu4da19632011-01-03 15:07:55 +00002740 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
2742
françois romieu4da19632011-01-03 15:07:55 +00002743static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002744{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002745 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002746 { 0x1f, 0x0002 },
2747 { 0x01, 0x90d0 },
2748 { 0x1f, 0x0000 }
2749 };
2750
françois romieu4da19632011-01-03 15:07:55 +00002751 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002752}
2753
françois romieu4da19632011-01-03 15:07:55 +00002754static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002755{
2756 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002757
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002758 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2759 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002760 return;
2761
françois romieu4da19632011-01-03 15:07:55 +00002762 rtl_writephy(tp, 0x1f, 0x0001);
2763 rtl_writephy(tp, 0x10, 0xf01b);
2764 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002765}
2766
françois romieu4da19632011-01-03 15:07:55 +00002767static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002768{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002769 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002770 { 0x1f, 0x0001 },
2771 { 0x04, 0x0000 },
2772 { 0x03, 0x00a1 },
2773 { 0x02, 0x0008 },
2774 { 0x01, 0x0120 },
2775 { 0x00, 0x1000 },
2776 { 0x04, 0x0800 },
2777 { 0x04, 0x9000 },
2778 { 0x03, 0x802f },
2779 { 0x02, 0x4f02 },
2780 { 0x01, 0x0409 },
2781 { 0x00, 0xf099 },
2782 { 0x04, 0x9800 },
2783 { 0x04, 0xa000 },
2784 { 0x03, 0xdf01 },
2785 { 0x02, 0xdf20 },
2786 { 0x01, 0xff95 },
2787 { 0x00, 0xba00 },
2788 { 0x04, 0xa800 },
2789 { 0x04, 0xf000 },
2790 { 0x03, 0xdf01 },
2791 { 0x02, 0xdf20 },
2792 { 0x01, 0x101a },
2793 { 0x00, 0xa0ff },
2794 { 0x04, 0xf800 },
2795 { 0x04, 0x0000 },
2796 { 0x1f, 0x0000 },
2797
2798 { 0x1f, 0x0001 },
2799 { 0x10, 0xf41b },
2800 { 0x14, 0xfb54 },
2801 { 0x18, 0xf5c7 },
2802 { 0x1f, 0x0000 },
2803
2804 { 0x1f, 0x0001 },
2805 { 0x17, 0x0cc0 },
2806 { 0x1f, 0x0000 }
2807 };
2808
françois romieu4da19632011-01-03 15:07:55 +00002809 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002810
françois romieu4da19632011-01-03 15:07:55 +00002811 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002812}
2813
françois romieu4da19632011-01-03 15:07:55 +00002814static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002815{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002816 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002817 { 0x1f, 0x0001 },
2818 { 0x04, 0x0000 },
2819 { 0x03, 0x00a1 },
2820 { 0x02, 0x0008 },
2821 { 0x01, 0x0120 },
2822 { 0x00, 0x1000 },
2823 { 0x04, 0x0800 },
2824 { 0x04, 0x9000 },
2825 { 0x03, 0x802f },
2826 { 0x02, 0x4f02 },
2827 { 0x01, 0x0409 },
2828 { 0x00, 0xf099 },
2829 { 0x04, 0x9800 },
2830 { 0x04, 0xa000 },
2831 { 0x03, 0xdf01 },
2832 { 0x02, 0xdf20 },
2833 { 0x01, 0xff95 },
2834 { 0x00, 0xba00 },
2835 { 0x04, 0xa800 },
2836 { 0x04, 0xf000 },
2837 { 0x03, 0xdf01 },
2838 { 0x02, 0xdf20 },
2839 { 0x01, 0x101a },
2840 { 0x00, 0xa0ff },
2841 { 0x04, 0xf800 },
2842 { 0x04, 0x0000 },
2843 { 0x1f, 0x0000 },
2844
2845 { 0x1f, 0x0001 },
2846 { 0x0b, 0x8480 },
2847 { 0x1f, 0x0000 },
2848
2849 { 0x1f, 0x0001 },
2850 { 0x18, 0x67c7 },
2851 { 0x04, 0x2000 },
2852 { 0x03, 0x002f },
2853 { 0x02, 0x4360 },
2854 { 0x01, 0x0109 },
2855 { 0x00, 0x3022 },
2856 { 0x04, 0x2800 },
2857 { 0x1f, 0x0000 },
2858
2859 { 0x1f, 0x0001 },
2860 { 0x17, 0x0cc0 },
2861 { 0x1f, 0x0000 }
2862 };
2863
françois romieu4da19632011-01-03 15:07:55 +00002864 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002865}
2866
françois romieu4da19632011-01-03 15:07:55 +00002867static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002868{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002869 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002870 { 0x10, 0xf41b },
2871 { 0x1f, 0x0000 }
2872 };
2873
françois romieu4da19632011-01-03 15:07:55 +00002874 rtl_writephy(tp, 0x1f, 0x0001);
2875 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002876
françois romieu4da19632011-01-03 15:07:55 +00002877 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002878}
2879
françois romieu4da19632011-01-03 15:07:55 +00002880static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002881{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002882 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002883 { 0x1f, 0x0001 },
2884 { 0x10, 0xf41b },
2885 { 0x1f, 0x0000 }
2886 };
2887
françois romieu4da19632011-01-03 15:07:55 +00002888 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002889}
2890
françois romieu4da19632011-01-03 15:07:55 +00002891static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002892{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002893 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002894 { 0x1f, 0x0000 },
2895 { 0x1d, 0x0f00 },
2896 { 0x1f, 0x0002 },
2897 { 0x0c, 0x1ec8 },
2898 { 0x1f, 0x0000 }
2899 };
2900
françois romieu4da19632011-01-03 15:07:55 +00002901 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002902}
2903
françois romieu4da19632011-01-03 15:07:55 +00002904static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002905{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002906 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002907 { 0x1f, 0x0001 },
2908 { 0x1d, 0x3d98 },
2909 { 0x1f, 0x0000 }
2910 };
2911
françois romieu4da19632011-01-03 15:07:55 +00002912 rtl_writephy(tp, 0x1f, 0x0000);
2913 rtl_patchphy(tp, 0x14, 1 << 5);
2914 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002915
françois romieu4da19632011-01-03 15:07:55 +00002916 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002917}
2918
françois romieu4da19632011-01-03 15:07:55 +00002919static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002920{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002921 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002922 { 0x1f, 0x0001 },
2923 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002924 { 0x1f, 0x0002 },
2925 { 0x00, 0x88d4 },
2926 { 0x01, 0x82b1 },
2927 { 0x03, 0x7002 },
2928 { 0x08, 0x9e30 },
2929 { 0x09, 0x01f0 },
2930 { 0x0a, 0x5500 },
2931 { 0x0c, 0x00c8 },
2932 { 0x1f, 0x0003 },
2933 { 0x12, 0xc096 },
2934 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002935 { 0x1f, 0x0000 },
2936 { 0x1f, 0x0000 },
2937 { 0x09, 0x2000 },
2938 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002939 };
2940
françois romieu4da19632011-01-03 15:07:55 +00002941 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002942
françois romieu4da19632011-01-03 15:07:55 +00002943 rtl_patchphy(tp, 0x14, 1 << 5);
2944 rtl_patchphy(tp, 0x0d, 1 << 5);
2945 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002946}
2947
françois romieu4da19632011-01-03 15:07:55 +00002948static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002949{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002950 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002951 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002952 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002953 { 0x03, 0x802f },
2954 { 0x02, 0x4f02 },
2955 { 0x01, 0x0409 },
2956 { 0x00, 0xf099 },
2957 { 0x04, 0x9800 },
2958 { 0x04, 0x9000 },
2959 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002960 { 0x1f, 0x0002 },
2961 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002962 { 0x06, 0x0761 },
2963 { 0x1f, 0x0003 },
2964 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002965 { 0x1f, 0x0000 }
2966 };
2967
françois romieu4da19632011-01-03 15:07:55 +00002968 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002969
françois romieu4da19632011-01-03 15:07:55 +00002970 rtl_patchphy(tp, 0x16, 1 << 0);
2971 rtl_patchphy(tp, 0x14, 1 << 5);
2972 rtl_patchphy(tp, 0x0d, 1 << 5);
2973 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002974}
2975
françois romieu4da19632011-01-03 15:07:55 +00002976static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002977{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002978 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002979 { 0x1f, 0x0001 },
2980 { 0x12, 0x2300 },
2981 { 0x1d, 0x3d98 },
2982 { 0x1f, 0x0002 },
2983 { 0x0c, 0x7eb8 },
2984 { 0x06, 0x5461 },
2985 { 0x1f, 0x0003 },
2986 { 0x16, 0x0f0a },
2987 { 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 Romieu197ff762008-06-28 13:16:02 +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 Romieu197ff762008-06-28 13:16:02 +02002996}
2997
françois romieu4da19632011-01-03 15:07:55 +00002998static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002999{
françois romieu4da19632011-01-03 15:07:55 +00003000 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003001}
3002
françois romieubca03d52011-01-03 15:07:31 +00003003static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003004{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003005 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003006 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003007 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003008 { 0x06, 0x4064 },
3009 { 0x07, 0x2863 },
3010 { 0x08, 0x059c },
3011 { 0x09, 0x26b4 },
3012 { 0x0a, 0x6a19 },
3013 { 0x0b, 0xdcc8 },
3014 { 0x10, 0xf06d },
3015 { 0x14, 0x7f68 },
3016 { 0x18, 0x7fd9 },
3017 { 0x1c, 0xf0ff },
3018 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003019 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003020 { 0x12, 0xf49f },
3021 { 0x13, 0x070b },
3022 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003023 { 0x14, 0x94c0 },
3024
3025 /*
3026 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003027 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003028 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003029 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003030 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003031 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003032 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003033 { 0x06, 0x5561 },
3034
3035 /*
3036 * Can not link to 1Gbps with bad cable
3037 * Decrease SNR threshold form 21.07dB to 19.04dB
3038 */
3039 { 0x1f, 0x0001 },
3040 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003041
3042 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003043 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003044 };
3045
françois romieu4da19632011-01-03 15:07:55 +00003046 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003047
françois romieubca03d52011-01-03 15:07:31 +00003048 /*
3049 * Rx Error Issue
3050 * Fine Tune Switching regulator parameter
3051 */
françois romieu4da19632011-01-03 15:07:55 +00003052 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003053 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3054 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003055
Francois Romieufdf6fc02012-07-06 22:40:38 +02003056 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003057 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003058 { 0x1f, 0x0002 },
3059 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003060 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003061 { 0x05, 0x8330 },
3062 { 0x06, 0x669a },
3063 { 0x1f, 0x0002 }
3064 };
3065 int val;
3066
françois romieu4da19632011-01-03 15:07:55 +00003067 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003068
françois romieu4da19632011-01-03 15:07:55 +00003069 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003070
3071 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003072 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003073 0x0065, 0x0066, 0x0067, 0x0068,
3074 0x0069, 0x006a, 0x006b, 0x006c
3075 };
3076 int i;
3077
françois romieu4da19632011-01-03 15:07:55 +00003078 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003079
3080 val &= 0xff00;
3081 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003082 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003083 }
3084 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003085 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003086 { 0x1f, 0x0002 },
3087 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003088 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003089 { 0x05, 0x8330 },
3090 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003091 };
3092
françois romieu4da19632011-01-03 15:07:55 +00003093 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003094 }
3095
françois romieubca03d52011-01-03 15:07:31 +00003096 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003097 rtl_writephy(tp, 0x1f, 0x0002);
3098 rtl_patchphy(tp, 0x0d, 0x0300);
3099 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003100
françois romieubca03d52011-01-03 15:07:31 +00003101 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003102 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003103 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3104 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003105
françois romieu4da19632011-01-03 15:07:55 +00003106 rtl_writephy(tp, 0x1f, 0x0005);
3107 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003108
3109 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003110
françois romieu4da19632011-01-03 15:07:55 +00003111 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003112}
3113
françois romieubca03d52011-01-03 15:07:31 +00003114static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003115{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003116 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003117 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003118 { 0x1f, 0x0001 },
3119 { 0x06, 0x4064 },
3120 { 0x07, 0x2863 },
3121 { 0x08, 0x059c },
3122 { 0x09, 0x26b4 },
3123 { 0x0a, 0x6a19 },
3124 { 0x0b, 0xdcc8 },
3125 { 0x10, 0xf06d },
3126 { 0x14, 0x7f68 },
3127 { 0x18, 0x7fd9 },
3128 { 0x1c, 0xf0ff },
3129 { 0x1d, 0x3d9c },
3130 { 0x1f, 0x0003 },
3131 { 0x12, 0xf49f },
3132 { 0x13, 0x070b },
3133 { 0x1a, 0x05ad },
3134 { 0x14, 0x94c0 },
3135
françois romieubca03d52011-01-03 15:07:31 +00003136 /*
3137 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003138 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003139 */
françois romieudaf9df62009-10-07 12:44:20 +00003140 { 0x1f, 0x0002 },
3141 { 0x06, 0x5561 },
3142 { 0x1f, 0x0005 },
3143 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003144 { 0x06, 0x5561 },
3145
3146 /*
3147 * Can not link to 1Gbps with bad cable
3148 * Decrease SNR threshold form 21.07dB to 19.04dB
3149 */
3150 { 0x1f, 0x0001 },
3151 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003152
3153 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003154 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003155 };
3156
françois romieu4da19632011-01-03 15:07:55 +00003157 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003158
Francois Romieufdf6fc02012-07-06 22:40:38 +02003159 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003160 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003161 { 0x1f, 0x0002 },
3162 { 0x05, 0x669a },
3163 { 0x1f, 0x0005 },
3164 { 0x05, 0x8330 },
3165 { 0x06, 0x669a },
3166
3167 { 0x1f, 0x0002 }
3168 };
3169 int val;
3170
françois romieu4da19632011-01-03 15:07:55 +00003171 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003172
françois romieu4da19632011-01-03 15:07:55 +00003173 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003174 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003175 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003176 0x0065, 0x0066, 0x0067, 0x0068,
3177 0x0069, 0x006a, 0x006b, 0x006c
3178 };
3179 int i;
3180
françois romieu4da19632011-01-03 15:07:55 +00003181 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003182
3183 val &= 0xff00;
3184 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003185 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003186 }
3187 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003188 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003189 { 0x1f, 0x0002 },
3190 { 0x05, 0x2642 },
3191 { 0x1f, 0x0005 },
3192 { 0x05, 0x8330 },
3193 { 0x06, 0x2642 }
3194 };
3195
françois romieu4da19632011-01-03 15:07:55 +00003196 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003197 }
3198
françois romieubca03d52011-01-03 15:07:31 +00003199 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003200 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003201 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3202 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003203
françois romieubca03d52011-01-03 15:07:31 +00003204 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003205 rtl_writephy(tp, 0x1f, 0x0002);
3206 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003207
françois romieu4da19632011-01-03 15:07:55 +00003208 rtl_writephy(tp, 0x1f, 0x0005);
3209 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003210
3211 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003212
françois romieu4da19632011-01-03 15:07:55 +00003213 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003214}
3215
françois romieu4da19632011-01-03 15:07:55 +00003216static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003217{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003218 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003219 { 0x1f, 0x0002 },
3220 { 0x10, 0x0008 },
3221 { 0x0d, 0x006c },
3222
3223 { 0x1f, 0x0000 },
3224 { 0x0d, 0xf880 },
3225
3226 { 0x1f, 0x0001 },
3227 { 0x17, 0x0cc0 },
3228
3229 { 0x1f, 0x0001 },
3230 { 0x0b, 0xa4d8 },
3231 { 0x09, 0x281c },
3232 { 0x07, 0x2883 },
3233 { 0x0a, 0x6b35 },
3234 { 0x1d, 0x3da4 },
3235 { 0x1c, 0xeffd },
3236 { 0x14, 0x7f52 },
3237 { 0x18, 0x7fc6 },
3238 { 0x08, 0x0601 },
3239 { 0x06, 0x4063 },
3240 { 0x10, 0xf074 },
3241 { 0x1f, 0x0003 },
3242 { 0x13, 0x0789 },
3243 { 0x12, 0xf4bd },
3244 { 0x1a, 0x04fd },
3245 { 0x14, 0x84b0 },
3246 { 0x1f, 0x0000 },
3247 { 0x00, 0x9200 },
3248
3249 { 0x1f, 0x0005 },
3250 { 0x01, 0x0340 },
3251 { 0x1f, 0x0001 },
3252 { 0x04, 0x4000 },
3253 { 0x03, 0x1d21 },
3254 { 0x02, 0x0c32 },
3255 { 0x01, 0x0200 },
3256 { 0x00, 0x5554 },
3257 { 0x04, 0x4800 },
3258 { 0x04, 0x4000 },
3259 { 0x04, 0xf000 },
3260 { 0x03, 0xdf01 },
3261 { 0x02, 0xdf20 },
3262 { 0x01, 0x101a },
3263 { 0x00, 0xa0ff },
3264 { 0x04, 0xf800 },
3265 { 0x04, 0xf000 },
3266 { 0x1f, 0x0000 },
3267
3268 { 0x1f, 0x0007 },
3269 { 0x1e, 0x0023 },
3270 { 0x16, 0x0000 },
3271 { 0x1f, 0x0000 }
3272 };
3273
françois romieu4da19632011-01-03 15:07:55 +00003274 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003275}
3276
françois romieue6de30d2011-01-03 15:08:37 +00003277static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3278{
3279 static const struct phy_reg phy_reg_init[] = {
3280 { 0x1f, 0x0001 },
3281 { 0x17, 0x0cc0 },
3282
3283 { 0x1f, 0x0007 },
3284 { 0x1e, 0x002d },
3285 { 0x18, 0x0040 },
3286 { 0x1f, 0x0000 }
3287 };
3288
3289 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3290 rtl_patchphy(tp, 0x0d, 1 << 5);
3291}
3292
Hayes Wang70090422011-07-06 15:58:06 +08003293static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003294{
3295 static const struct phy_reg phy_reg_init[] = {
3296 /* Enable Delay cap */
3297 { 0x1f, 0x0005 },
3298 { 0x05, 0x8b80 },
3299 { 0x06, 0xc896 },
3300 { 0x1f, 0x0000 },
3301
3302 /* Channel estimation fine tune */
3303 { 0x1f, 0x0001 },
3304 { 0x0b, 0x6c20 },
3305 { 0x07, 0x2872 },
3306 { 0x1c, 0xefff },
3307 { 0x1f, 0x0003 },
3308 { 0x14, 0x6420 },
3309 { 0x1f, 0x0000 },
3310
3311 /* Update PFM & 10M TX idle timer */
3312 { 0x1f, 0x0007 },
3313 { 0x1e, 0x002f },
3314 { 0x15, 0x1919 },
3315 { 0x1f, 0x0000 },
3316
3317 { 0x1f, 0x0007 },
3318 { 0x1e, 0x00ac },
3319 { 0x18, 0x0006 },
3320 { 0x1f, 0x0000 }
3321 };
3322
Francois Romieu15ecd032011-04-27 13:52:22 -07003323 rtl_apply_firmware(tp);
3324
hayeswang01dc7fe2011-03-21 01:50:28 +00003325 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3326
3327 /* DCO enable for 10M IDLE Power */
3328 rtl_writephy(tp, 0x1f, 0x0007);
3329 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003330 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003331 rtl_writephy(tp, 0x1f, 0x0000);
3332
3333 /* For impedance matching */
3334 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003335 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003336 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003337
3338 /* PHY auto speed down */
3339 rtl_writephy(tp, 0x1f, 0x0007);
3340 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003341 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003342 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003343 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003344
3345 rtl_writephy(tp, 0x1f, 0x0005);
3346 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003347 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003348 rtl_writephy(tp, 0x1f, 0x0000);
3349
3350 rtl_writephy(tp, 0x1f, 0x0005);
3351 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003352 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003353 rtl_writephy(tp, 0x1f, 0x0007);
3354 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003355 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003356 rtl_writephy(tp, 0x1f, 0x0006);
3357 rtl_writephy(tp, 0x00, 0x5a00);
3358 rtl_writephy(tp, 0x1f, 0x0000);
3359 rtl_writephy(tp, 0x0d, 0x0007);
3360 rtl_writephy(tp, 0x0e, 0x003c);
3361 rtl_writephy(tp, 0x0d, 0x4007);
3362 rtl_writephy(tp, 0x0e, 0x0000);
3363 rtl_writephy(tp, 0x0d, 0x0000);
3364}
3365
françois romieu9ecb9aa2012-12-07 11:20:21 +00003366static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3367{
3368 const u16 w[] = {
3369 addr[0] | (addr[1] << 8),
3370 addr[2] | (addr[3] << 8),
3371 addr[4] | (addr[5] << 8)
3372 };
3373 const struct exgmac_reg e[] = {
3374 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3375 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3376 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3377 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3378 };
3379
3380 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3381}
3382
Hayes Wang70090422011-07-06 15:58:06 +08003383static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3384{
3385 static const struct phy_reg phy_reg_init[] = {
3386 /* Enable Delay cap */
3387 { 0x1f, 0x0004 },
3388 { 0x1f, 0x0007 },
3389 { 0x1e, 0x00ac },
3390 { 0x18, 0x0006 },
3391 { 0x1f, 0x0002 },
3392 { 0x1f, 0x0000 },
3393 { 0x1f, 0x0000 },
3394
3395 /* Channel estimation fine tune */
3396 { 0x1f, 0x0003 },
3397 { 0x09, 0xa20f },
3398 { 0x1f, 0x0000 },
3399 { 0x1f, 0x0000 },
3400
3401 /* Green Setting */
3402 { 0x1f, 0x0005 },
3403 { 0x05, 0x8b5b },
3404 { 0x06, 0x9222 },
3405 { 0x05, 0x8b6d },
3406 { 0x06, 0x8000 },
3407 { 0x05, 0x8b76 },
3408 { 0x06, 0x8000 },
3409 { 0x1f, 0x0000 }
3410 };
3411
3412 rtl_apply_firmware(tp);
3413
3414 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3415
3416 /* For 4-corner performance improve */
3417 rtl_writephy(tp, 0x1f, 0x0005);
3418 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003419 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003420 rtl_writephy(tp, 0x1f, 0x0000);
3421
3422 /* PHY auto speed down */
3423 rtl_writephy(tp, 0x1f, 0x0004);
3424 rtl_writephy(tp, 0x1f, 0x0007);
3425 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003426 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003427 rtl_writephy(tp, 0x1f, 0x0002);
3428 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003429 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003430
3431 /* improve 10M EEE waveform */
3432 rtl_writephy(tp, 0x1f, 0x0005);
3433 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003434 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003435 rtl_writephy(tp, 0x1f, 0x0000);
3436
3437 /* Improve 2-pair detection performance */
3438 rtl_writephy(tp, 0x1f, 0x0005);
3439 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003440 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003441 rtl_writephy(tp, 0x1f, 0x0000);
3442
3443 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003444 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003445 rtl_writephy(tp, 0x1f, 0x0005);
3446 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003447 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003448 rtl_writephy(tp, 0x1f, 0x0004);
3449 rtl_writephy(tp, 0x1f, 0x0007);
3450 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003451 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003452 rtl_writephy(tp, 0x1f, 0x0002);
3453 rtl_writephy(tp, 0x1f, 0x0000);
3454 rtl_writephy(tp, 0x0d, 0x0007);
3455 rtl_writephy(tp, 0x0e, 0x003c);
3456 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003457 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003458 rtl_writephy(tp, 0x0d, 0x0000);
3459
3460 /* Green feature */
3461 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003462 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3463 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003464 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003465 rtl_writephy(tp, 0x1f, 0x0005);
3466 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3467 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003468
françois romieu9ecb9aa2012-12-07 11:20:21 +00003469 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3470 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003471}
3472
Hayes Wang5f886e02012-03-30 14:33:03 +08003473static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3474{
3475 /* For 4-corner performance improve */
3476 rtl_writephy(tp, 0x1f, 0x0005);
3477 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003478 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003479 rtl_writephy(tp, 0x1f, 0x0000);
3480
3481 /* PHY auto speed down */
3482 rtl_writephy(tp, 0x1f, 0x0007);
3483 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003484 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003485 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003486 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003487
3488 /* Improve 10M EEE waveform */
3489 rtl_writephy(tp, 0x1f, 0x0005);
3490 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003491 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003492 rtl_writephy(tp, 0x1f, 0x0000);
3493}
3494
Hayes Wangc2218922011-09-06 16:55:18 +08003495static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3496{
3497 static const struct phy_reg phy_reg_init[] = {
3498 /* Channel estimation fine tune */
3499 { 0x1f, 0x0003 },
3500 { 0x09, 0xa20f },
3501 { 0x1f, 0x0000 },
3502
3503 /* Modify green table for giga & fnet */
3504 { 0x1f, 0x0005 },
3505 { 0x05, 0x8b55 },
3506 { 0x06, 0x0000 },
3507 { 0x05, 0x8b5e },
3508 { 0x06, 0x0000 },
3509 { 0x05, 0x8b67 },
3510 { 0x06, 0x0000 },
3511 { 0x05, 0x8b70 },
3512 { 0x06, 0x0000 },
3513 { 0x1f, 0x0000 },
3514 { 0x1f, 0x0007 },
3515 { 0x1e, 0x0078 },
3516 { 0x17, 0x0000 },
3517 { 0x19, 0x00fb },
3518 { 0x1f, 0x0000 },
3519
3520 /* Modify green table for 10M */
3521 { 0x1f, 0x0005 },
3522 { 0x05, 0x8b79 },
3523 { 0x06, 0xaa00 },
3524 { 0x1f, 0x0000 },
3525
3526 /* Disable hiimpedance detection (RTCT) */
3527 { 0x1f, 0x0003 },
3528 { 0x01, 0x328a },
3529 { 0x1f, 0x0000 }
3530 };
3531
3532 rtl_apply_firmware(tp);
3533
3534 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3535
Hayes Wang5f886e02012-03-30 14:33:03 +08003536 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003537
3538 /* Improve 2-pair detection performance */
3539 rtl_writephy(tp, 0x1f, 0x0005);
3540 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003541 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003542 rtl_writephy(tp, 0x1f, 0x0000);
3543}
3544
3545static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3546{
3547 rtl_apply_firmware(tp);
3548
Hayes Wang5f886e02012-03-30 14:33:03 +08003549 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003550}
3551
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003552static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3553{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003554 static const struct phy_reg phy_reg_init[] = {
3555 /* Channel estimation fine tune */
3556 { 0x1f, 0x0003 },
3557 { 0x09, 0xa20f },
3558 { 0x1f, 0x0000 },
3559
3560 /* Modify green table for giga & fnet */
3561 { 0x1f, 0x0005 },
3562 { 0x05, 0x8b55 },
3563 { 0x06, 0x0000 },
3564 { 0x05, 0x8b5e },
3565 { 0x06, 0x0000 },
3566 { 0x05, 0x8b67 },
3567 { 0x06, 0x0000 },
3568 { 0x05, 0x8b70 },
3569 { 0x06, 0x0000 },
3570 { 0x1f, 0x0000 },
3571 { 0x1f, 0x0007 },
3572 { 0x1e, 0x0078 },
3573 { 0x17, 0x0000 },
3574 { 0x19, 0x00aa },
3575 { 0x1f, 0x0000 },
3576
3577 /* Modify green table for 10M */
3578 { 0x1f, 0x0005 },
3579 { 0x05, 0x8b79 },
3580 { 0x06, 0xaa00 },
3581 { 0x1f, 0x0000 },
3582
3583 /* Disable hiimpedance detection (RTCT) */
3584 { 0x1f, 0x0003 },
3585 { 0x01, 0x328a },
3586 { 0x1f, 0x0000 }
3587 };
3588
3589
3590 rtl_apply_firmware(tp);
3591
3592 rtl8168f_hw_phy_config(tp);
3593
3594 /* Improve 2-pair detection performance */
3595 rtl_writephy(tp, 0x1f, 0x0005);
3596 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003597 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003598 rtl_writephy(tp, 0x1f, 0x0000);
3599
3600 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3601
3602 /* Modify green table for giga */
3603 rtl_writephy(tp, 0x1f, 0x0005);
3604 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003605 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003606 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003607 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003608 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003609 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003610 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003611 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003612 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003613 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003614 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003615 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003616 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003617 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003618 rtl_writephy(tp, 0x1f, 0x0000);
3619
3620 /* uc same-seed solution */
3621 rtl_writephy(tp, 0x1f, 0x0005);
3622 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003623 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003624 rtl_writephy(tp, 0x1f, 0x0000);
3625
3626 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003627 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003628 rtl_writephy(tp, 0x1f, 0x0005);
3629 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003630 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003631 rtl_writephy(tp, 0x1f, 0x0004);
3632 rtl_writephy(tp, 0x1f, 0x0007);
3633 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003634 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003635 rtl_writephy(tp, 0x1f, 0x0000);
3636 rtl_writephy(tp, 0x0d, 0x0007);
3637 rtl_writephy(tp, 0x0e, 0x003c);
3638 rtl_writephy(tp, 0x0d, 0x4007);
3639 rtl_writephy(tp, 0x0e, 0x0000);
3640 rtl_writephy(tp, 0x0d, 0x0000);
3641
3642 /* Green feature */
3643 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003644 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3645 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003646 rtl_writephy(tp, 0x1f, 0x0000);
3647}
3648
Hayes Wangc5583862012-07-02 17:23:22 +08003649static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3650{
Hayes Wangc5583862012-07-02 17:23:22 +08003651 rtl_apply_firmware(tp);
3652
hayeswang41f44d12013-04-01 22:23:36 +00003653 rtl_writephy(tp, 0x1f, 0x0a46);
3654 if (rtl_readphy(tp, 0x10) & 0x0100) {
3655 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003656 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003657 } else {
3658 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003659 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003660 }
Hayes Wangc5583862012-07-02 17:23:22 +08003661
hayeswang41f44d12013-04-01 22:23:36 +00003662 rtl_writephy(tp, 0x1f, 0x0a46);
3663 if (rtl_readphy(tp, 0x13) & 0x0100) {
3664 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003665 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003666 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003667 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003668 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003669 }
Hayes Wangc5583862012-07-02 17:23:22 +08003670
hayeswang41f44d12013-04-01 22:23:36 +00003671 /* Enable PHY auto speed down */
3672 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003673 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003674
hayeswangfe7524c2013-04-01 22:23:37 +00003675 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003676 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003677 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003678 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003679 rtl_writephy(tp, 0x1f, 0x0a43);
3680 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003681 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3682 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003683
hayeswang41f44d12013-04-01 22:23:36 +00003684 /* EEE auto-fallback function */
3685 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003686 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003687
hayeswang41f44d12013-04-01 22:23:36 +00003688 /* Enable UC LPF tune function */
3689 rtl_writephy(tp, 0x1f, 0x0a43);
3690 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003691 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003692
3693 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003694 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003695
hayeswangfe7524c2013-04-01 22:23:37 +00003696 /* Improve SWR Efficiency */
3697 rtl_writephy(tp, 0x1f, 0x0bcd);
3698 rtl_writephy(tp, 0x14, 0x5065);
3699 rtl_writephy(tp, 0x14, 0xd065);
3700 rtl_writephy(tp, 0x1f, 0x0bc8);
3701 rtl_writephy(tp, 0x11, 0x5655);
3702 rtl_writephy(tp, 0x1f, 0x0bcd);
3703 rtl_writephy(tp, 0x14, 0x1065);
3704 rtl_writephy(tp, 0x14, 0x9065);
3705 rtl_writephy(tp, 0x14, 0x1065);
3706
David Chang1bac1072013-11-27 15:48:36 +08003707 /* Check ALDPS bit, disable it if enabled */
3708 rtl_writephy(tp, 0x1f, 0x0a43);
3709 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003710 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08003711
hayeswang41f44d12013-04-01 22:23:36 +00003712 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003713}
3714
hayeswang57538c42013-04-01 22:23:40 +00003715static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3716{
3717 rtl_apply_firmware(tp);
3718}
3719
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003720static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3721{
3722 u16 dout_tapbin;
3723 u32 data;
3724
3725 rtl_apply_firmware(tp);
3726
3727 /* CHN EST parameters adjust - giga master */
3728 rtl_writephy(tp, 0x1f, 0x0a43);
3729 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003730 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003731 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003732 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003733 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003734 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003735 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003736 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003737 rtl_writephy(tp, 0x1f, 0x0000);
3738
3739 /* CHN EST parameters adjust - giga slave */
3740 rtl_writephy(tp, 0x1f, 0x0a43);
3741 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003742 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003743 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003744 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003745 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003746 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003747 rtl_writephy(tp, 0x1f, 0x0000);
3748
3749 /* CHN EST parameters adjust - fnet */
3750 rtl_writephy(tp, 0x1f, 0x0a43);
3751 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003752 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003753 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003754 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003755 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003756 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003757 rtl_writephy(tp, 0x1f, 0x0000);
3758
3759 /* enable R-tune & PGA-retune function */
3760 dout_tapbin = 0;
3761 rtl_writephy(tp, 0x1f, 0x0a46);
3762 data = rtl_readphy(tp, 0x13);
3763 data &= 3;
3764 data <<= 2;
3765 dout_tapbin |= data;
3766 data = rtl_readphy(tp, 0x12);
3767 data &= 0xc000;
3768 data >>= 14;
3769 dout_tapbin |= data;
3770 dout_tapbin = ~(dout_tapbin^0x08);
3771 dout_tapbin <<= 12;
3772 dout_tapbin &= 0xf000;
3773 rtl_writephy(tp, 0x1f, 0x0a43);
3774 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003775 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003776 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003777 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003778 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003779 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003780 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003781 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003782
3783 rtl_writephy(tp, 0x1f, 0x0a43);
3784 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003785 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003786 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003787 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003788 rtl_writephy(tp, 0x1f, 0x0000);
3789
3790 /* enable GPHY 10M */
3791 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003792 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003793 rtl_writephy(tp, 0x1f, 0x0000);
3794
3795 /* SAR ADC performance */
3796 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003797 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003798 rtl_writephy(tp, 0x1f, 0x0000);
3799
3800 rtl_writephy(tp, 0x1f, 0x0a43);
3801 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003802 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003803 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003804 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003805 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003806 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003807 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003808 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003809 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003810 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003811 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003812 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003813 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003814 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003815 rtl_writephy(tp, 0x1f, 0x0000);
3816
3817 /* disable phy pfm mode */
3818 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003819 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003820 rtl_writephy(tp, 0x1f, 0x0000);
3821
3822 /* Check ALDPS bit, disable it if enabled */
3823 rtl_writephy(tp, 0x1f, 0x0a43);
3824 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003825 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003826
3827 rtl_writephy(tp, 0x1f, 0x0000);
3828}
3829
3830static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3831{
3832 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3833 u16 rlen;
3834 u32 data;
3835
3836 rtl_apply_firmware(tp);
3837
3838 /* CHIN EST parameter update */
3839 rtl_writephy(tp, 0x1f, 0x0a43);
3840 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003841 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003842 rtl_writephy(tp, 0x1f, 0x0000);
3843
3844 /* enable R-tune & PGA-retune function */
3845 rtl_writephy(tp, 0x1f, 0x0a43);
3846 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003847 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003848 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003849 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003850 rtl_writephy(tp, 0x1f, 0x0000);
3851
3852 /* enable GPHY 10M */
3853 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003854 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003855 rtl_writephy(tp, 0x1f, 0x0000);
3856
3857 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3858 data = r8168_mac_ocp_read(tp, 0xdd02);
3859 ioffset_p3 = ((data & 0x80)>>7);
3860 ioffset_p3 <<= 3;
3861
3862 data = r8168_mac_ocp_read(tp, 0xdd00);
3863 ioffset_p3 |= ((data & (0xe000))>>13);
3864 ioffset_p2 = ((data & (0x1e00))>>9);
3865 ioffset_p1 = ((data & (0x01e0))>>5);
3866 ioffset_p0 = ((data & 0x0010)>>4);
3867 ioffset_p0 <<= 3;
3868 ioffset_p0 |= (data & (0x07));
3869 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3870
Chun-Hao Lin05b96872014-10-01 23:17:12 +08003871 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08003872 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003873 rtl_writephy(tp, 0x1f, 0x0bcf);
3874 rtl_writephy(tp, 0x16, data);
3875 rtl_writephy(tp, 0x1f, 0x0000);
3876 }
3877
3878 /* Modify rlen (TX LPF corner frequency) level */
3879 rtl_writephy(tp, 0x1f, 0x0bcd);
3880 data = rtl_readphy(tp, 0x16);
3881 data &= 0x000f;
3882 rlen = 0;
3883 if (data > 3)
3884 rlen = data - 3;
3885 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3886 rtl_writephy(tp, 0x17, data);
3887 rtl_writephy(tp, 0x1f, 0x0bcd);
3888 rtl_writephy(tp, 0x1f, 0x0000);
3889
3890 /* disable phy pfm mode */
3891 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003892 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003893 rtl_writephy(tp, 0x1f, 0x0000);
3894
3895 /* Check ALDPS bit, disable it if enabled */
3896 rtl_writephy(tp, 0x1f, 0x0a43);
3897 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003898 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003899
3900 rtl_writephy(tp, 0x1f, 0x0000);
3901}
3902
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003903static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3904{
3905 /* Enable PHY auto speed down */
3906 rtl_writephy(tp, 0x1f, 0x0a44);
3907 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3908 rtl_writephy(tp, 0x1f, 0x0000);
3909
3910 /* patch 10M & ALDPS */
3911 rtl_writephy(tp, 0x1f, 0x0bcc);
3912 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3913 rtl_writephy(tp, 0x1f, 0x0a44);
3914 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3915 rtl_writephy(tp, 0x1f, 0x0a43);
3916 rtl_writephy(tp, 0x13, 0x8084);
3917 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3918 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3919 rtl_writephy(tp, 0x1f, 0x0000);
3920
3921 /* Enable EEE auto-fallback function */
3922 rtl_writephy(tp, 0x1f, 0x0a4b);
3923 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3924 rtl_writephy(tp, 0x1f, 0x0000);
3925
3926 /* Enable UC LPF tune function */
3927 rtl_writephy(tp, 0x1f, 0x0a43);
3928 rtl_writephy(tp, 0x13, 0x8012);
3929 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3930 rtl_writephy(tp, 0x1f, 0x0000);
3931
3932 /* set rg_sel_sdm_rate */
3933 rtl_writephy(tp, 0x1f, 0x0c42);
3934 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3935 rtl_writephy(tp, 0x1f, 0x0000);
3936
3937 /* Check ALDPS bit, disable it if enabled */
3938 rtl_writephy(tp, 0x1f, 0x0a43);
3939 if (rtl_readphy(tp, 0x10) & 0x0004)
3940 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3941
3942 rtl_writephy(tp, 0x1f, 0x0000);
3943}
3944
3945static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3946{
3947 /* patch 10M & ALDPS */
3948 rtl_writephy(tp, 0x1f, 0x0bcc);
3949 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3950 rtl_writephy(tp, 0x1f, 0x0a44);
3951 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3952 rtl_writephy(tp, 0x1f, 0x0a43);
3953 rtl_writephy(tp, 0x13, 0x8084);
3954 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3955 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3956 rtl_writephy(tp, 0x1f, 0x0000);
3957
3958 /* Enable UC LPF tune function */
3959 rtl_writephy(tp, 0x1f, 0x0a43);
3960 rtl_writephy(tp, 0x13, 0x8012);
3961 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3962 rtl_writephy(tp, 0x1f, 0x0000);
3963
3964 /* Set rg_sel_sdm_rate */
3965 rtl_writephy(tp, 0x1f, 0x0c42);
3966 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3967 rtl_writephy(tp, 0x1f, 0x0000);
3968
3969 /* Channel estimation parameters */
3970 rtl_writephy(tp, 0x1f, 0x0a43);
3971 rtl_writephy(tp, 0x13, 0x80f3);
3972 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
3973 rtl_writephy(tp, 0x13, 0x80f0);
3974 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
3975 rtl_writephy(tp, 0x13, 0x80ef);
3976 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
3977 rtl_writephy(tp, 0x13, 0x80f6);
3978 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
3979 rtl_writephy(tp, 0x13, 0x80ec);
3980 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
3981 rtl_writephy(tp, 0x13, 0x80ed);
3982 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3983 rtl_writephy(tp, 0x13, 0x80f2);
3984 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
3985 rtl_writephy(tp, 0x13, 0x80f4);
3986 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
3987 rtl_writephy(tp, 0x1f, 0x0a43);
3988 rtl_writephy(tp, 0x13, 0x8110);
3989 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
3990 rtl_writephy(tp, 0x13, 0x810f);
3991 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
3992 rtl_writephy(tp, 0x13, 0x8111);
3993 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
3994 rtl_writephy(tp, 0x13, 0x8113);
3995 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
3996 rtl_writephy(tp, 0x13, 0x8115);
3997 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
3998 rtl_writephy(tp, 0x13, 0x810e);
3999 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4000 rtl_writephy(tp, 0x13, 0x810c);
4001 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4002 rtl_writephy(tp, 0x13, 0x810b);
4003 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4004 rtl_writephy(tp, 0x1f, 0x0a43);
4005 rtl_writephy(tp, 0x13, 0x80d1);
4006 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4007 rtl_writephy(tp, 0x13, 0x80cd);
4008 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4009 rtl_writephy(tp, 0x13, 0x80d3);
4010 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4011 rtl_writephy(tp, 0x13, 0x80d5);
4012 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4013 rtl_writephy(tp, 0x13, 0x80d7);
4014 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4015
4016 /* Force PWM-mode */
4017 rtl_writephy(tp, 0x1f, 0x0bcd);
4018 rtl_writephy(tp, 0x14, 0x5065);
4019 rtl_writephy(tp, 0x14, 0xd065);
4020 rtl_writephy(tp, 0x1f, 0x0bc8);
4021 rtl_writephy(tp, 0x12, 0x00ed);
4022 rtl_writephy(tp, 0x1f, 0x0bcd);
4023 rtl_writephy(tp, 0x14, 0x1065);
4024 rtl_writephy(tp, 0x14, 0x9065);
4025 rtl_writephy(tp, 0x14, 0x1065);
4026 rtl_writephy(tp, 0x1f, 0x0000);
4027
4028 /* Check ALDPS bit, disable it if enabled */
4029 rtl_writephy(tp, 0x1f, 0x0a43);
4030 if (rtl_readphy(tp, 0x10) & 0x0004)
4031 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4032
4033 rtl_writephy(tp, 0x1f, 0x0000);
4034}
4035
françois romieu4da19632011-01-03 15:07:55 +00004036static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004037{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004038 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004039 { 0x1f, 0x0003 },
4040 { 0x08, 0x441d },
4041 { 0x01, 0x9100 },
4042 { 0x1f, 0x0000 }
4043 };
4044
françois romieu4da19632011-01-03 15:07:55 +00004045 rtl_writephy(tp, 0x1f, 0x0000);
4046 rtl_patchphy(tp, 0x11, 1 << 12);
4047 rtl_patchphy(tp, 0x19, 1 << 13);
4048 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004049
françois romieu4da19632011-01-03 15:07:55 +00004050 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004051}
4052
Hayes Wang5a5e4442011-02-22 17:26:21 +08004053static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4054{
4055 static const struct phy_reg phy_reg_init[] = {
4056 { 0x1f, 0x0005 },
4057 { 0x1a, 0x0000 },
4058 { 0x1f, 0x0000 },
4059
4060 { 0x1f, 0x0004 },
4061 { 0x1c, 0x0000 },
4062 { 0x1f, 0x0000 },
4063
4064 { 0x1f, 0x0001 },
4065 { 0x15, 0x7701 },
4066 { 0x1f, 0x0000 }
4067 };
4068
4069 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004070 rtl_writephy(tp, 0x1f, 0x0000);
4071 rtl_writephy(tp, 0x18, 0x0310);
4072 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004073
François Romieu953a12c2011-04-24 17:38:48 +02004074 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004075
4076 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4077}
4078
Hayes Wang7e18dca2012-03-30 14:33:02 +08004079static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4080{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004081 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004082 rtl_writephy(tp, 0x1f, 0x0000);
4083 rtl_writephy(tp, 0x18, 0x0310);
4084 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004085
4086 rtl_apply_firmware(tp);
4087
4088 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004089 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004090 rtl_writephy(tp, 0x1f, 0x0004);
4091 rtl_writephy(tp, 0x10, 0x401f);
4092 rtl_writephy(tp, 0x19, 0x7030);
4093 rtl_writephy(tp, 0x1f, 0x0000);
4094}
4095
Hayes Wang5598bfe2012-07-02 17:23:21 +08004096static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4097{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004098 static const struct phy_reg phy_reg_init[] = {
4099 { 0x1f, 0x0004 },
4100 { 0x10, 0xc07f },
4101 { 0x19, 0x7030 },
4102 { 0x1f, 0x0000 }
4103 };
4104
4105 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004106 rtl_writephy(tp, 0x1f, 0x0000);
4107 rtl_writephy(tp, 0x18, 0x0310);
4108 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004109
4110 rtl_apply_firmware(tp);
4111
Francois Romieufdf6fc02012-07-06 22:40:38 +02004112 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004113 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4114
Francois Romieufdf6fc02012-07-06 22:40:38 +02004115 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004116}
4117
Francois Romieu5615d9f2007-08-17 17:50:46 +02004118static void rtl_hw_phy_config(struct net_device *dev)
4119{
4120 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004121
4122 rtl8169_print_mac_version(tp);
4123
4124 switch (tp->mac_version) {
4125 case RTL_GIGA_MAC_VER_01:
4126 break;
4127 case RTL_GIGA_MAC_VER_02:
4128 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004129 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004130 break;
4131 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004132 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004133 break;
françois romieu2e9558562009-08-10 19:44:19 +00004134 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004135 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004136 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004137 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004138 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004139 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004140 case RTL_GIGA_MAC_VER_07:
4141 case RTL_GIGA_MAC_VER_08:
4142 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004143 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004144 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004145 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004146 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004147 break;
4148 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004149 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004150 break;
4151 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004152 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004153 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004154 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004155 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004156 break;
4157 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004158 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004159 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004160 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004161 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004162 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004163 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004164 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004165 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004166 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004167 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004168 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004169 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004170 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004171 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004172 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004173 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004174 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004175 break;
4176 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004177 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004178 break;
4179 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004180 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004181 break;
françois romieue6de30d2011-01-03 15:08:37 +00004182 case RTL_GIGA_MAC_VER_28:
4183 rtl8168d_4_hw_phy_config(tp);
4184 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004185 case RTL_GIGA_MAC_VER_29:
4186 case RTL_GIGA_MAC_VER_30:
4187 rtl8105e_hw_phy_config(tp);
4188 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004189 case RTL_GIGA_MAC_VER_31:
4190 /* None. */
4191 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004192 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004193 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004194 rtl8168e_1_hw_phy_config(tp);
4195 break;
4196 case RTL_GIGA_MAC_VER_34:
4197 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004198 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004199 case RTL_GIGA_MAC_VER_35:
4200 rtl8168f_1_hw_phy_config(tp);
4201 break;
4202 case RTL_GIGA_MAC_VER_36:
4203 rtl8168f_2_hw_phy_config(tp);
4204 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004205
Hayes Wang7e18dca2012-03-30 14:33:02 +08004206 case RTL_GIGA_MAC_VER_37:
4207 rtl8402_hw_phy_config(tp);
4208 break;
4209
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004210 case RTL_GIGA_MAC_VER_38:
4211 rtl8411_hw_phy_config(tp);
4212 break;
4213
Hayes Wang5598bfe2012-07-02 17:23:21 +08004214 case RTL_GIGA_MAC_VER_39:
4215 rtl8106e_hw_phy_config(tp);
4216 break;
4217
Hayes Wangc5583862012-07-02 17:23:22 +08004218 case RTL_GIGA_MAC_VER_40:
4219 rtl8168g_1_hw_phy_config(tp);
4220 break;
hayeswang57538c42013-04-01 22:23:40 +00004221 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004222 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004223 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004224 rtl8168g_2_hw_phy_config(tp);
4225 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004226 case RTL_GIGA_MAC_VER_45:
4227 case RTL_GIGA_MAC_VER_47:
4228 rtl8168h_1_hw_phy_config(tp);
4229 break;
4230 case RTL_GIGA_MAC_VER_46:
4231 case RTL_GIGA_MAC_VER_48:
4232 rtl8168h_2_hw_phy_config(tp);
4233 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004234
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004235 case RTL_GIGA_MAC_VER_49:
4236 rtl8168ep_1_hw_phy_config(tp);
4237 break;
4238 case RTL_GIGA_MAC_VER_50:
4239 case RTL_GIGA_MAC_VER_51:
4240 rtl8168ep_2_hw_phy_config(tp);
4241 break;
4242
Hayes Wangc5583862012-07-02 17:23:22 +08004243 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004244 default:
4245 break;
4246 }
4247}
4248
Francois Romieuda78dbf2012-01-26 14:18:23 +01004249static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4250{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004251 if (!test_and_set_bit(flag, tp->wk.flags))
4252 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004253}
4254
Francois Romieuffc46952012-07-06 14:19:23 +02004255DECLARE_RTL_COND(rtl_phy_reset_cond)
4256{
Heiner Kallweite3972862018-06-29 08:07:04 +02004257 return rtl8169_xmii_reset_pending(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004258}
4259
Francois Romieubf793292006-11-01 00:53:05 +01004260static void rtl8169_phy_reset(struct net_device *dev,
4261 struct rtl8169_private *tp)
4262{
Heiner Kallweite3972862018-06-29 08:07:04 +02004263 rtl8169_xmii_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004264 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004265}
4266
David S. Miller8decf862011-09-22 03:23:13 -04004267static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4268{
David S. Miller8decf862011-09-22 03:23:13 -04004269 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
Heiner Kallweite3972862018-06-29 08:07:04 +02004270 (RTL_R8(tp, PHYstatus) & TBI_Enable);
David S. Miller8decf862011-09-22 03:23:13 -04004271}
4272
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004273static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274{
Francois Romieu5615d9f2007-08-17 17:50:46 +02004275 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004276
Marcus Sundberg773328942008-07-10 21:28:08 +02004277 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Heiner Kallweit49d17512018-06-28 20:36:15 +02004278 netif_dbg(tp, drv, dev,
4279 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004280 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02004281 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004282
Francois Romieu6dccd162007-02-13 23:38:05 +01004283 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4284
4285 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4286 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004287
Francois Romieubcf0bf92006-07-26 23:14:13 +02004288 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Heiner Kallweit49d17512018-06-28 20:36:15 +02004289 netif_dbg(tp, drv, dev,
4290 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004291 RTL_W8(tp, 0x82, 0x01);
Heiner Kallweit49d17512018-06-28 20:36:15 +02004292 netif_dbg(tp, drv, dev,
4293 "Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004294 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004295 }
4296
Francois Romieubf793292006-11-01 00:53:05 +01004297 rtl8169_phy_reset(dev, tp);
4298
Oliver Neukum54405cd2011-01-06 21:55:13 +01004299 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004300 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4301 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4302 (tp->mii.supports_gmii ?
4303 ADVERTISED_1000baseT_Half |
4304 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004305}
4306
Francois Romieu773d2022007-01-31 23:47:43 +01004307static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4308{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004309 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004310
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004311 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00004312
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004313 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4314 RTL_R32(tp, MAC4);
françois romieu908ba2bf2010-04-26 11:42:58 +00004315
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004316 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4317 RTL_R32(tp, MAC0);
françois romieu908ba2bf2010-04-26 11:42:58 +00004318
françois romieu9ecb9aa2012-12-07 11:20:21 +00004319 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4320 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004321
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004322 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieu773d2022007-01-31 23:47:43 +01004323
Francois Romieuda78dbf2012-01-26 14:18:23 +01004324 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004325}
4326
4327static int rtl_set_mac_address(struct net_device *dev, void *p)
4328{
4329 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004330 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004331 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004332
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004333 ret = eth_mac_addr(dev, p);
4334 if (ret)
4335 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004336
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004337 pm_runtime_get_noresume(d);
4338
4339 if (pm_runtime_active(d))
4340 rtl_rar_set(tp, dev->dev_addr);
4341
4342 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004343
4344 return 0;
4345}
4346
Francois Romieucecb5fd2011-04-01 10:21:07 +02004347static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4348 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004349{
Francois Romieu5f787a12006-08-17 13:02:36 +02004350 switch (cmd) {
4351 case SIOCGMIIPHY:
4352 data->phy_id = 32; /* Internal PHY */
4353 return 0;
4354
4355 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004356 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004357 return 0;
4358
4359 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004360 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004361 return 0;
4362 }
4363 return -EOPNOTSUPP;
4364}
4365
Heiner Kallweite3972862018-06-29 08:07:04 +02004366static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004367{
Heiner Kallweite3972862018-06-29 08:07:04 +02004368 struct rtl8169_private *tp = netdev_priv(dev);
4369 struct mii_ioctl_data *data = if_mii(ifr);
4370
4371 return netif_running(dev) ? rtl_xmii_ioctl(tp, data, cmd) : -ENODEV;
Francois Romieu8b4ab282008-11-19 22:05:25 -08004372}
4373
Bill Pembertonbaf63292012-12-03 09:23:28 -05004374static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004375{
4376 struct mdio_ops *ops = &tp->mdio_ops;
4377
4378 switch (tp->mac_version) {
4379 case RTL_GIGA_MAC_VER_27:
4380 ops->write = r8168dp_1_mdio_write;
4381 ops->read = r8168dp_1_mdio_read;
4382 break;
françois romieue6de30d2011-01-03 15:08:37 +00004383 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004384 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004385 ops->write = r8168dp_2_mdio_write;
4386 ops->read = r8168dp_2_mdio_read;
4387 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004388 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004389 ops->write = r8168g_mdio_write;
4390 ops->read = r8168g_mdio_read;
4391 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004392 default:
4393 ops->write = r8169_mdio_write;
4394 ops->read = r8169_mdio_read;
4395 break;
4396 }
4397}
4398
hayeswange2409d82013-03-31 17:02:04 +00004399static void rtl_speed_down(struct rtl8169_private *tp)
4400{
4401 u32 adv;
4402 int lpa;
4403
4404 rtl_writephy(tp, 0x1f, 0x0000);
4405 lpa = rtl_readphy(tp, MII_LPA);
4406
4407 if (lpa & (LPA_10HALF | LPA_10FULL))
4408 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4409 else if (lpa & (LPA_100HALF | LPA_100FULL))
4410 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4411 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4412 else
4413 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4414 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4415 (tp->mii.supports_gmii ?
4416 ADVERTISED_1000baseT_Half |
4417 ADVERTISED_1000baseT_Full : 0);
4418
4419 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4420 adv);
4421}
4422
David S. Miller1805b2f2011-10-24 18:18:09 -04004423static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4424{
David S. Miller1805b2f2011-10-24 18:18:09 -04004425 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004426 case RTL_GIGA_MAC_VER_25:
4427 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004428 case RTL_GIGA_MAC_VER_29:
4429 case RTL_GIGA_MAC_VER_30:
4430 case RTL_GIGA_MAC_VER_32:
4431 case RTL_GIGA_MAC_VER_33:
4432 case RTL_GIGA_MAC_VER_34:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004433 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004434 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04004435 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4436 break;
4437 default:
4438 break;
4439 }
4440}
4441
4442static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4443{
Heiner Kallweit6fcf9b12018-07-04 21:11:29 +02004444 if (!netif_running(tp->dev) || !__rtl8169_get_wol(tp))
David S. Miller1805b2f2011-10-24 18:18:09 -04004445 return false;
4446
hayeswange2409d82013-03-31 17:02:04 +00004447 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004448 rtl_wol_suspend_quirk(tp);
4449
4450 return true;
4451}
4452
françois romieu065c27c2011-01-03 15:08:12 +00004453static void r8168_phy_power_up(struct rtl8169_private *tp)
4454{
4455 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004456 switch (tp->mac_version) {
4457 case RTL_GIGA_MAC_VER_11:
4458 case RTL_GIGA_MAC_VER_12:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004459 case RTL_GIGA_MAC_VER_17 ... RTL_GIGA_MAC_VER_28:
hayeswang01dc7fe2011-03-21 01:50:28 +00004460 case RTL_GIGA_MAC_VER_31:
4461 rtl_writephy(tp, 0x0e, 0x0000);
4462 break;
4463 default:
4464 break;
4465 }
françois romieu065c27c2011-01-03 15:08:12 +00004466 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
David S. Millerb2d6cee2018-05-11 20:53:22 -04004467
4468 /* give MAC/PHY some time to resume */
4469 msleep(20);
françois romieu065c27c2011-01-03 15:08:12 +00004470}
4471
4472static void r8168_phy_power_down(struct rtl8169_private *tp)
4473{
4474 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004475 switch (tp->mac_version) {
4476 case RTL_GIGA_MAC_VER_32:
4477 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004478 case RTL_GIGA_MAC_VER_40:
4479 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004480 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4481 break;
4482
4483 case RTL_GIGA_MAC_VER_11:
4484 case RTL_GIGA_MAC_VER_12:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004485 case RTL_GIGA_MAC_VER_17 ... RTL_GIGA_MAC_VER_28:
hayeswang01dc7fe2011-03-21 01:50:28 +00004486 case RTL_GIGA_MAC_VER_31:
4487 rtl_writephy(tp, 0x0e, 0x0200);
4488 default:
4489 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4490 break;
4491 }
françois romieu065c27c2011-01-03 15:08:12 +00004492}
4493
4494static void r8168_pll_power_down(struct rtl8169_private *tp)
4495{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01004496 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004497 return;
4498
hayeswang01dc7fe2011-03-21 01:50:28 +00004499 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4500 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004501 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004502
David S. Miller1805b2f2011-10-24 18:18:09 -04004503 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004504 return;
françois romieu065c27c2011-01-03 15:08:12 +00004505
4506 r8168_phy_power_down(tp);
4507
4508 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004509 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004510 case RTL_GIGA_MAC_VER_37:
4511 case RTL_GIGA_MAC_VER_39:
4512 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004513 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004514 case RTL_GIGA_MAC_VER_45:
4515 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004516 case RTL_GIGA_MAC_VER_47:
4517 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004518 case RTL_GIGA_MAC_VER_50:
4519 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004520 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004521 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004522 case RTL_GIGA_MAC_VER_40:
4523 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004524 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004525 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004526 0xfc000000, ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004527 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004528 break;
françois romieu065c27c2011-01-03 15:08:12 +00004529 }
4530}
4531
4532static void r8168_pll_power_up(struct rtl8169_private *tp)
4533{
françois romieu065c27c2011-01-03 15:08:12 +00004534 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004535 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004536 case RTL_GIGA_MAC_VER_37:
4537 case RTL_GIGA_MAC_VER_39:
4538 case RTL_GIGA_MAC_VER_43:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004539 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004540 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004541 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004542 case RTL_GIGA_MAC_VER_45:
4543 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004544 case RTL_GIGA_MAC_VER_47:
4545 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004546 case RTL_GIGA_MAC_VER_50:
4547 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004548 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004549 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004550 case RTL_GIGA_MAC_VER_40:
4551 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004552 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004553 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004554 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004555 0x00000000, ERIAR_EXGMAC);
4556 break;
françois romieu065c27c2011-01-03 15:08:12 +00004557 }
4558
4559 r8168_phy_power_up(tp);
4560}
4561
françois romieu065c27c2011-01-03 15:08:12 +00004562static void rtl_pll_power_down(struct rtl8169_private *tp)
4563{
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004564 switch (tp->mac_version) {
4565 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4566 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4567 break;
4568 default:
4569 r8168_pll_power_down(tp);
4570 }
françois romieu065c27c2011-01-03 15:08:12 +00004571}
4572
4573static void rtl_pll_power_up(struct rtl8169_private *tp)
4574{
françois romieu065c27c2011-01-03 15:08:12 +00004575 switch (tp->mac_version) {
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004576 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4577 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
françois romieu065c27c2011-01-03 15:08:12 +00004578 break;
françois romieu065c27c2011-01-03 15:08:12 +00004579 default:
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004580 r8168_pll_power_up(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004581 }
4582}
4583
Hayes Wange542a222011-07-06 15:58:04 +08004584static void rtl_init_rxcfg(struct rtl8169_private *tp)
4585{
Hayes Wange542a222011-07-06 15:58:04 +08004586 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004587 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4588 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004589 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004590 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004591 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004592 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02004593 case RTL_GIGA_MAC_VER_35:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004594 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004595 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004596 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004597 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00004598 break;
Hayes Wange542a222011-07-06 15:58:04 +08004599 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004600 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004601 break;
4602 }
4603}
4604
Hayes Wang92fc43b2011-07-06 15:58:03 +08004605static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4606{
Timo Teräs9fba0812013-01-15 21:01:24 +00004607 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004608}
4609
Francois Romieud58d46b2011-05-03 16:38:29 +02004610static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4611{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004612 if (tp->jumbo_ops.enable) {
4613 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4614 tp->jumbo_ops.enable(tp);
4615 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4616 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004617}
4618
4619static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4620{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004621 if (tp->jumbo_ops.disable) {
4622 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4623 tp->jumbo_ops.disable(tp);
4624 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4625 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004626}
4627
4628static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4629{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004630 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4631 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004632 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004633}
4634
4635static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4636{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004637 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4638 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004639 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004640}
4641
4642static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4643{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004644 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004645}
4646
4647static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4648{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004649 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004650}
4651
4652static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4653{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004654 RTL_W8(tp, MaxTxPacketSize, 0x3f);
4655 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4656 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004657 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004658}
4659
4660static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4661{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004662 RTL_W8(tp, MaxTxPacketSize, 0x0c);
4663 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4664 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004665 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004666}
4667
4668static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4669{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004670 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01004671 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004672}
4673
4674static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4675{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004676 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004677 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004678}
4679
4680static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4681{
Francois Romieud58d46b2011-05-03 16:38:29 +02004682 r8168b_0_hw_jumbo_enable(tp);
4683
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004684 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004685}
4686
4687static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4688{
Francois Romieud58d46b2011-05-03 16:38:29 +02004689 r8168b_0_hw_jumbo_disable(tp);
4690
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004691 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004692}
4693
Bill Pembertonbaf63292012-12-03 09:23:28 -05004694static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02004695{
4696 struct jumbo_ops *ops = &tp->jumbo_ops;
4697
4698 switch (tp->mac_version) {
4699 case RTL_GIGA_MAC_VER_11:
4700 ops->disable = r8168b_0_hw_jumbo_disable;
4701 ops->enable = r8168b_0_hw_jumbo_enable;
4702 break;
4703 case RTL_GIGA_MAC_VER_12:
4704 case RTL_GIGA_MAC_VER_17:
4705 ops->disable = r8168b_1_hw_jumbo_disable;
4706 ops->enable = r8168b_1_hw_jumbo_enable;
4707 break;
4708 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4709 case RTL_GIGA_MAC_VER_19:
4710 case RTL_GIGA_MAC_VER_20:
4711 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4712 case RTL_GIGA_MAC_VER_22:
4713 case RTL_GIGA_MAC_VER_23:
4714 case RTL_GIGA_MAC_VER_24:
4715 case RTL_GIGA_MAC_VER_25:
4716 case RTL_GIGA_MAC_VER_26:
4717 ops->disable = r8168c_hw_jumbo_disable;
4718 ops->enable = r8168c_hw_jumbo_enable;
4719 break;
4720 case RTL_GIGA_MAC_VER_27:
4721 case RTL_GIGA_MAC_VER_28:
4722 ops->disable = r8168dp_hw_jumbo_disable;
4723 ops->enable = r8168dp_hw_jumbo_enable;
4724 break;
4725 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4726 case RTL_GIGA_MAC_VER_32:
4727 case RTL_GIGA_MAC_VER_33:
4728 case RTL_GIGA_MAC_VER_34:
4729 ops->disable = r8168e_hw_jumbo_disable;
4730 ops->enable = r8168e_hw_jumbo_enable;
4731 break;
4732
4733 /*
4734 * No action needed for jumbo frames with 8169.
4735 * No jumbo for 810x at all.
4736 */
Heiner Kallweit2a718832018-05-02 21:39:49 +02004737 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02004738 default:
4739 ops->disable = NULL;
4740 ops->enable = NULL;
4741 break;
4742 }
4743}
4744
Francois Romieuffc46952012-07-06 14:19:23 +02004745DECLARE_RTL_COND(rtl_chipcmd_cond)
4746{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004747 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02004748}
4749
Francois Romieu6f43adc2011-04-29 15:05:51 +02004750static void rtl_hw_reset(struct rtl8169_private *tp)
4751{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004752 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004753
Francois Romieuffc46952012-07-06 14:19:23 +02004754 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004755}
4756
Francois Romieub6ffd972011-06-17 17:00:05 +02004757static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4758{
4759 struct rtl_fw *rtl_fw;
4760 const char *name;
4761 int rc = -ENOMEM;
4762
4763 name = rtl_lookup_firmware_name(tp);
4764 if (!name)
4765 goto out_no_firmware;
4766
4767 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4768 if (!rtl_fw)
4769 goto err_warn;
4770
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004771 rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
Francois Romieub6ffd972011-06-17 17:00:05 +02004772 if (rc < 0)
4773 goto err_free;
4774
Francois Romieufd112f22011-06-18 00:10:29 +02004775 rc = rtl_check_firmware(tp, rtl_fw);
4776 if (rc < 0)
4777 goto err_release_firmware;
4778
Francois Romieub6ffd972011-06-17 17:00:05 +02004779 tp->rtl_fw = rtl_fw;
4780out:
4781 return;
4782
Francois Romieufd112f22011-06-18 00:10:29 +02004783err_release_firmware:
4784 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004785err_free:
4786 kfree(rtl_fw);
4787err_warn:
4788 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4789 name, rc);
4790out_no_firmware:
4791 tp->rtl_fw = NULL;
4792 goto out;
4793}
4794
François Romieu953a12c2011-04-24 17:38:48 +02004795static void rtl_request_firmware(struct rtl8169_private *tp)
4796{
Francois Romieub6ffd972011-06-17 17:00:05 +02004797 if (IS_ERR(tp->rtl_fw))
4798 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004799}
4800
Hayes Wang92fc43b2011-07-06 15:58:03 +08004801static void rtl_rx_close(struct rtl8169_private *tp)
4802{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004803 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004804}
4805
Francois Romieuffc46952012-07-06 14:19:23 +02004806DECLARE_RTL_COND(rtl_npq_cond)
4807{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004808 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02004809}
4810
4811DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4812{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004813 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02004814}
4815
françois romieue6de30d2011-01-03 15:08:37 +00004816static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817{
4818 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004819 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
Hayes Wang92fc43b2011-07-06 15:58:03 +08004821 rtl_rx_close(tp);
4822
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004823 switch (tp->mac_version) {
4824 case RTL_GIGA_MAC_VER_27:
4825 case RTL_GIGA_MAC_VER_28:
4826 case RTL_GIGA_MAC_VER_31:
Francois Romieuffc46952012-07-06 14:19:23 +02004827 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004828 break;
4829 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
4830 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004831 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02004832 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004833 break;
4834 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004835 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004836 udelay(100);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004837 break;
françois romieue6de30d2011-01-03 15:08:37 +00004838 }
4839
Hayes Wang92fc43b2011-07-06 15:58:03 +08004840 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841}
4842
Francois Romieu7f796d832007-06-11 23:04:41 +02004843static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004844{
Francois Romieu9cb427b2006-11-02 00:10:16 +01004845 /* Set DMA burst size and Interframe Gap Time */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004846 RTL_W32(tp, TxConfig, (TX_DMA_BURST << TxDMAShift) |
Francois Romieu9cb427b2006-11-02 00:10:16 +01004847 (InterFrameGap << TxInterFrameGapShift));
4848}
4849
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004850static void rtl_set_rx_max_size(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004852 /* Low hurts. Let's disable the filtering. */
4853 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
Francois Romieu07ce4062007-02-23 23:36:39 +01004854}
4855
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004856static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02004857{
4858 /*
4859 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4860 * register to be written before TxDescAddrLow to work.
4861 * Switching from MMIO to I/O access fixes the issue as well.
4862 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004863 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4864 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4865 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4866 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004867}
4868
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004869static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01004870{
Francois Romieu37441002011-06-17 22:58:54 +02004871 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004872 u32 mac_version;
4873 u32 clk;
4874 u32 val;
4875 } cfg2_info [] = {
4876 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4877 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4878 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4879 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004880 };
4881 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004882 unsigned int i;
4883 u32 clk;
4884
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004885 clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004886 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004887 if ((p->mac_version == mac_version) && (p->clk == clk)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004888 RTL_W32(tp, 0x7c, p->val);
Francois Romieu6dccd162007-02-13 23:38:05 +01004889 break;
4890 }
4891 }
4892}
4893
Francois Romieue6b763e2012-03-08 09:35:39 +01004894static void rtl_set_rx_mode(struct net_device *dev)
4895{
4896 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieue6b763e2012-03-08 09:35:39 +01004897 u32 mc_filter[2]; /* Multicast hash filter */
4898 int rx_mode;
4899 u32 tmp = 0;
4900
4901 if (dev->flags & IFF_PROMISC) {
4902 /* Unconditionally log net taps. */
4903 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4904 rx_mode =
4905 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4906 AcceptAllPhys;
4907 mc_filter[1] = mc_filter[0] = 0xffffffff;
4908 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4909 (dev->flags & IFF_ALLMULTI)) {
4910 /* Too many to filter perfectly -- accept all multicasts. */
4911 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4912 mc_filter[1] = mc_filter[0] = 0xffffffff;
4913 } else {
4914 struct netdev_hw_addr *ha;
4915
4916 rx_mode = AcceptBroadcast | AcceptMyPhys;
4917 mc_filter[1] = mc_filter[0] = 0;
4918 netdev_for_each_mc_addr(ha, dev) {
4919 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4920 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4921 rx_mode |= AcceptMulticast;
4922 }
4923 }
4924
4925 if (dev->features & NETIF_F_RXALL)
4926 rx_mode |= (AcceptErr | AcceptRunt);
4927
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004928 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Francois Romieue6b763e2012-03-08 09:35:39 +01004929
4930 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4931 u32 data = mc_filter[0];
4932
4933 mc_filter[0] = swab32(mc_filter[1]);
4934 mc_filter[1] = swab32(data);
4935 }
4936
Nathan Walp04817762012-11-01 12:08:47 +00004937 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4938 mc_filter[1] = mc_filter[0] = 0xffffffff;
4939
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004940 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
4941 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01004942
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004943 RTL_W32(tp, RxConfig, tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01004944}
4945
Heiner Kallweit52f85602018-05-19 10:29:33 +02004946static void rtl_hw_start(struct rtl8169_private *tp)
4947{
4948 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4949
4950 tp->hw_start(tp);
4951
4952 rtl_set_rx_max_size(tp);
4953 rtl_set_rx_tx_desc_registers(tp);
4954 rtl_set_rx_tx_config_registers(tp);
4955 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4956
4957 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4958 RTL_R8(tp, IntrMask);
4959 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
4960 rtl_set_rx_mode(tp->dev);
4961 /* no early-rx interrupts */
4962 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
4963 rtl_irq_enable_all(tp);
4964}
4965
Heiner Kallweit61cb5322018-04-17 23:27:38 +02004966static void rtl_hw_start_8169(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01004967{
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004968 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02004969 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004970
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004971 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004973 tp->cp_cmd |= PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004974
Francois Romieucecb5fd2011-04-01 10:21:07 +02004975 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4976 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Heiner Kallweit49d17512018-06-28 20:36:15 +02004977 netif_dbg(tp, drv, tp->dev,
4978 "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004979 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 }
4981
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004982 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieubcf0bf92006-07-26 23:14:13 +02004983
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004984 rtl8169_set_magic_reg(tp, tp->mac_version);
Francois Romieu6dccd162007-02-13 23:38:05 +01004985
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 /*
4987 * Undocumented corner. Supposedly:
4988 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4989 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004990 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004992 RTL_W32(tp, RxMissed, 0);
Francois Romieu07ce4062007-02-23 23:36:39 +01004993}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
Francois Romieuffc46952012-07-06 14:19:23 +02004995DECLARE_RTL_COND(rtl_csiar_cond)
4996{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004997 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02004998}
4999
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005000static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005001{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005002 u32 func = PCI_FUNC(tp->pci_dev->devfn);
5003
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005004 RTL_W32(tp, CSIDR, value);
5005 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005006 CSIAR_BYTE_ENABLE | func << 16);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005007
Francois Romieuffc46952012-07-06 14:19:23 +02005008 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005009}
5010
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005011static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005012{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005013 u32 func = PCI_FUNC(tp->pci_dev->devfn);
5014
5015 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
5016 CSIAR_BYTE_ENABLE);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005017
Francois Romieuffc46952012-07-06 14:19:23 +02005018 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005019 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005020}
5021
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005022static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005023{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005024 struct pci_dev *pdev = tp->pci_dev;
5025 u32 csi;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005026
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005027 /* According to Realtek the value at config space address 0x070f
5028 * controls the L0s/L1 entrance latency. We try standard ECAM access
5029 * first and if it fails fall back to CSI.
5030 */
5031 if (pdev->cfg_size > 0x070f &&
5032 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
5033 return;
5034
5035 netdev_notice_once(tp->dev,
5036 "No native access to PCI extended config space, falling back to CSI\n");
5037 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5038 rtl_csi_write(tp, 0x070c, csi | val << 24);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005039}
5040
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005041static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
hayeswang45dd95c2013-07-08 17:09:01 +08005042{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005043 rtl_csi_access_enable(tp, 0x27);
Francois Romieudacf8152008-08-02 20:44:13 +02005044}
5045
5046struct ephy_info {
5047 unsigned int offset;
5048 u16 mask;
5049 u16 bits;
5050};
5051
Francois Romieufdf6fc02012-07-06 22:40:38 +02005052static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5053 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005054{
5055 u16 w;
5056
5057 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005058 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5059 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005060 e++;
5061 }
5062}
5063
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005064static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02005065{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005066 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005067 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005068}
5069
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005070static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005071{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005072 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005073 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005074}
5075
hayeswangb51ecea2014-07-09 14:52:51 +08005076static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5077{
hayeswangb51ecea2014-07-09 14:52:51 +08005078 u8 data;
5079
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005080 data = RTL_R8(tp, Config3);
hayeswangb51ecea2014-07-09 14:52:51 +08005081
5082 if (enable)
5083 data |= Rdy_to_L23;
5084 else
5085 data &= ~Rdy_to_L23;
5086
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005087 RTL_W8(tp, Config3, data);
hayeswangb51ecea2014-07-09 14:52:51 +08005088}
5089
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005090static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
5091{
5092 if (enable) {
5093 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
5094 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
5095 } else {
5096 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5097 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
5098 }
5099}
5100
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005101static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005102{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005103 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005104
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005105 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005106 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieub726e492008-06-28 12:22:59 +02005107
françois romieufaf1e782013-02-27 13:01:57 +00005108 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005109 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00005110 PCI_EXP_DEVCTL_NOSNOOP_EN);
5111 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005112}
5113
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005114static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005115{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005116 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005117
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005118 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005119
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005120 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005121}
5122
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005123static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005124{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005125 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02005126
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005127 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005128
françois romieufaf1e782013-02-27 13:01:57 +00005129 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005130 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02005131
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005132 rtl_disable_clock_request(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005133
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005134 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005135 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu219a1e92008-06-28 11:58:39 +02005136}
5137
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005138static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005139{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005140 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005141 { 0x01, 0, 0x0001 },
5142 { 0x02, 0x0800, 0x1000 },
5143 { 0x03, 0, 0x0042 },
5144 { 0x06, 0x0080, 0x0000 },
5145 { 0x07, 0, 0x2000 }
5146 };
5147
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005148 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005149
Francois Romieufdf6fc02012-07-06 22:40:38 +02005150 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005151
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005152 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005153}
5154
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005155static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005156{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005157 rtl_set_def_aspm_entry_latency(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005158
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005159 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02005160
françois romieufaf1e782013-02-27 13:01:57 +00005161 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005162 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieuef3386f2008-06-29 12:24:30 +02005163
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005164 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005165 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieuef3386f2008-06-29 12:24:30 +02005166}
5167
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005168static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005169{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005170 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005171
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005172 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005173
5174 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005175 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005176
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005177 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005178
françois romieufaf1e782013-02-27 13:01:57 +00005179 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005180 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005181
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005182 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005183 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005184}
5185
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005186static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005187{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005188 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005189 { 0x02, 0x0800, 0x1000 },
5190 { 0x03, 0, 0x0002 },
5191 { 0x06, 0x0080, 0x0000 }
5192 };
5193
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005194 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005195
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005196 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02005197
Francois Romieufdf6fc02012-07-06 22:40:38 +02005198 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005199
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005200 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005201}
5202
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005203static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005204{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005205 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005206 { 0x01, 0, 0x0001 },
5207 { 0x03, 0x0400, 0x0220 }
5208 };
5209
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005210 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005211
Francois Romieufdf6fc02012-07-06 22:40:38 +02005212 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005213
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005214 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005215}
5216
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005217static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005218{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005219 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005220}
5221
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005222static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005223{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005224 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005225
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005226 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005227}
5228
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005229static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005230{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005231 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005232
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005233 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005234
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005235 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005236
françois romieufaf1e782013-02-27 13:01:57 +00005237 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005238 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02005239
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005240 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005241 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu5b538df2008-07-20 16:22:45 +02005242}
5243
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005244static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005245{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005246 rtl_set_def_aspm_entry_latency(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005247
françois romieufaf1e782013-02-27 13:01:57 +00005248 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005249 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang4804b3b2011-03-21 01:50:29 +00005250
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005251 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang4804b3b2011-03-21 01:50:29 +00005252
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005253 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005254}
5255
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005256static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005257{
5258 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005259 { 0x0b, 0x0000, 0x0048 },
5260 { 0x19, 0x0020, 0x0050 },
5261 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00005262 };
françois romieue6de30d2011-01-03 15:08:37 +00005263
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005264 rtl_set_def_aspm_entry_latency(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005265
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005266 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00005267
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005268 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
françois romieue6de30d2011-01-03 15:08:37 +00005269
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005270 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00005271
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005272 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005273}
5274
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005275static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005276{
Hayes Wang70090422011-07-06 15:58:06 +08005277 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005278 { 0x00, 0x0200, 0x0100 },
5279 { 0x00, 0x0000, 0x0004 },
5280 { 0x06, 0x0002, 0x0001 },
5281 { 0x06, 0x0000, 0x0030 },
5282 { 0x07, 0x0000, 0x2000 },
5283 { 0x00, 0x0000, 0x0020 },
5284 { 0x03, 0x5800, 0x2000 },
5285 { 0x03, 0x0000, 0x0001 },
5286 { 0x01, 0x0800, 0x1000 },
5287 { 0x07, 0x0000, 0x4000 },
5288 { 0x1e, 0x0000, 0x2000 },
5289 { 0x19, 0xffff, 0xfe6c },
5290 { 0x0a, 0x0000, 0x0040 }
5291 };
5292
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005293 rtl_set_def_aspm_entry_latency(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005294
Francois Romieufdf6fc02012-07-06 22:40:38 +02005295 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005296
françois romieufaf1e782013-02-27 13:01:57 +00005297 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005298 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00005299
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005300 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang01dc7fe2011-03-21 01:50:28 +00005301
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005302 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005303
5304 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005305 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
5306 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005307
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005308 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005309}
5310
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005311static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005312{
5313 static const struct ephy_info e_info_8168e_2[] = {
5314 { 0x09, 0x0000, 0x0080 },
5315 { 0x19, 0x0000, 0x0224 }
5316 };
5317
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005318 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005319
Francois Romieufdf6fc02012-07-06 22:40:38 +02005320 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005321
françois romieufaf1e782013-02-27 13:01:57 +00005322 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005323 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08005324
Francois Romieufdf6fc02012-07-06 22:40:38 +02005325 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5326 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5327 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5328 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5329 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5330 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005331 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5332 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005333
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005334 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005335
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005336 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005337
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005338 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5339 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08005340
5341 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005342 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang70090422011-07-06 15:58:06 +08005343
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005344 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5345 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5346 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Heiner Kallweitaa1e7d22018-06-23 09:53:00 +02005347
5348 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang70090422011-07-06 15:58:06 +08005349}
5350
Hayes Wang5f886e02012-03-30 14:33:03 +08005351static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005352{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005353 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005354
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005355 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08005356
Francois Romieufdf6fc02012-07-06 22:40:38 +02005357 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5358 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5359 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5360 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005361 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5362 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5363 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5364 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005365 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5366 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005367
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005368 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc2218922011-09-06 16:55:18 +08005369
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005370 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005371
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005372 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5373 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5374 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5375 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5376 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005377}
5378
Hayes Wang5f886e02012-03-30 14:33:03 +08005379static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5380{
Hayes Wang5f886e02012-03-30 14:33:03 +08005381 static const struct ephy_info e_info_8168f_1[] = {
5382 { 0x06, 0x00c0, 0x0020 },
5383 { 0x08, 0x0001, 0x0002 },
5384 { 0x09, 0x0000, 0x0080 },
5385 { 0x19, 0x0000, 0x0224 }
5386 };
5387
5388 rtl_hw_start_8168f(tp);
5389
Francois Romieufdf6fc02012-07-06 22:40:38 +02005390 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005391
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005392 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005393
5394 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005395 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang5f886e02012-03-30 14:33:03 +08005396}
5397
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005398static void rtl_hw_start_8411(struct rtl8169_private *tp)
5399{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005400 static const struct ephy_info e_info_8168f_1[] = {
5401 { 0x06, 0x00c0, 0x0020 },
5402 { 0x0f, 0xffff, 0x5200 },
5403 { 0x1e, 0x0000, 0x4000 },
5404 { 0x19, 0x0000, 0x0224 }
5405 };
5406
5407 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08005408 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005409
Francois Romieufdf6fc02012-07-06 22:40:38 +02005410 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005411
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005412 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005413}
5414
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005415static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08005416{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005417 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
hayeswangbeb330a2013-04-01 22:23:39 +00005418
Hayes Wangc5583862012-07-02 17:23:22 +08005419 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5420 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5421 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5422 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5423
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005424 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08005425
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005426 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08005427
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005428 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5429 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00005430 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005431
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005432 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5433 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc5583862012-07-02 17:23:22 +08005434
5435 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5436 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5437
5438 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005439 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wangc5583862012-07-02 17:23:22 +08005440
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005441 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5442 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005443
5444 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08005445}
5446
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005447static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5448{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005449 static const struct ephy_info e_info_8168g_1[] = {
5450 { 0x00, 0x0000, 0x0008 },
5451 { 0x0c, 0x37d0, 0x0820 },
5452 { 0x1e, 0x0000, 0x0001 },
5453 { 0x19, 0x8000, 0x0000 }
5454 };
5455
5456 rtl_hw_start_8168g(tp);
5457
5458 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005459 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005460 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005461 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005462}
5463
hayeswang57538c42013-04-01 22:23:40 +00005464static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5465{
hayeswang57538c42013-04-01 22:23:40 +00005466 static const struct ephy_info e_info_8168g_2[] = {
5467 { 0x00, 0x0000, 0x0008 },
5468 { 0x0c, 0x3df0, 0x0200 },
5469 { 0x19, 0xffff, 0xfc00 },
5470 { 0x1e, 0xffff, 0x20eb }
5471 };
5472
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005473 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00005474
5475 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005476 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5477 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang57538c42013-04-01 22:23:40 +00005478 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5479}
5480
hayeswang45dd95c2013-07-08 17:09:01 +08005481static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5482{
hayeswang45dd95c2013-07-08 17:09:01 +08005483 static const struct ephy_info e_info_8411_2[] = {
5484 { 0x00, 0x0000, 0x0008 },
5485 { 0x0c, 0x3df0, 0x0200 },
5486 { 0x0f, 0xffff, 0x5200 },
5487 { 0x19, 0x0020, 0x0000 },
5488 { 0x1e, 0x0000, 0x2000 }
5489 };
5490
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005491 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08005492
5493 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005494 rtl_hw_aspm_clkreq_enable(tp, false);
hayeswang45dd95c2013-07-08 17:09:01 +08005495 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005496 rtl_hw_aspm_clkreq_enable(tp, true);
hayeswang45dd95c2013-07-08 17:09:01 +08005497}
5498
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005499static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
5500{
Andrzej Hajda72521ea2015-09-24 16:00:24 +02005501 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005502 u32 data;
5503 static const struct ephy_info e_info_8168h_1[] = {
5504 { 0x1e, 0x0800, 0x0001 },
5505 { 0x1d, 0x0000, 0x0800 },
5506 { 0x05, 0xffff, 0x2089 },
5507 { 0x06, 0xffff, 0x5881 },
5508 { 0x04, 0xffff, 0x154a },
5509 { 0x01, 0xffff, 0x068b }
5510 };
5511
5512 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005513 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005514 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
5515
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005516 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005517
5518 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5519 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5520 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5521 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5522
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005523 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005524
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005525 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005526
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005527 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5528 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005529
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005530 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005531
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005532 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005533
5534 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5535
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005536 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5537 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005538
5539 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5540 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5541
5542 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005543 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005544
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005545 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5546 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005547
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005548 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005549
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005550 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005551
5552 rtl_pcie_state_l2l3_enable(tp, false);
5553
5554 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08005555 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005556 rtl_writephy(tp, 0x1f, 0x0000);
5557 if (rg_saw_cnt > 0) {
5558 u16 sw_cnt_1ms_ini;
5559
5560 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
5561 sw_cnt_1ms_ini &= 0x0fff;
5562 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005563 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005564 data |= sw_cnt_1ms_ini;
5565 r8168_mac_ocp_write(tp, 0xd412, data);
5566 }
5567
5568 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005569 data &= ~0xf0;
5570 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005571 r8168_mac_ocp_write(tp, 0xe056, data);
5572
5573 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005574 data &= ~0x6000;
5575 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005576 r8168_mac_ocp_write(tp, 0xe052, data);
5577
5578 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005579 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005580 data |= 0x017f;
5581 r8168_mac_ocp_write(tp, 0xe0d6, data);
5582
5583 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005584 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005585 data |= 0x047f;
5586 r8168_mac_ocp_write(tp, 0xd420, data);
5587
5588 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
5589 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
5590 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
5591 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005592
5593 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005594}
5595
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005596static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
5597{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08005598 rtl8168ep_stop_cmac(tp);
5599
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005600 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005601
5602 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5603 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
5604 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
5605 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5606
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005607 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005608
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005609 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005610
5611 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5612 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5613
5614 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
5615
5616 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5617
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005618 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5619 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005620
5621 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5622 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5623
5624 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005625 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005626
5627 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5628
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005629 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005630
5631 rtl_pcie_state_l2l3_enable(tp, false);
5632}
5633
5634static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
5635{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005636 static const struct ephy_info e_info_8168ep_1[] = {
5637 { 0x00, 0xffff, 0x10ab },
5638 { 0x06, 0xffff, 0xf030 },
5639 { 0x08, 0xffff, 0x2006 },
5640 { 0x0d, 0xffff, 0x1666 },
5641 { 0x0c, 0x3ff0, 0x0000 }
5642 };
5643
5644 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005645 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005646 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
5647
5648 rtl_hw_start_8168ep(tp);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005649
5650 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005651}
5652
5653static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
5654{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005655 static const struct ephy_info e_info_8168ep_2[] = {
5656 { 0x00, 0xffff, 0x10a3 },
5657 { 0x19, 0xffff, 0xfc00 },
5658 { 0x1e, 0xffff, 0x20ea }
5659 };
5660
5661 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005662 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005663 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
5664
5665 rtl_hw_start_8168ep(tp);
5666
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005667 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5668 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005669
5670 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005671}
5672
5673static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
5674{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005675 u32 data;
5676 static const struct ephy_info e_info_8168ep_3[] = {
5677 { 0x00, 0xffff, 0x10a3 },
5678 { 0x19, 0xffff, 0x7c00 },
5679 { 0x1e, 0xffff, 0x20eb },
5680 { 0x0d, 0xffff, 0x1666 }
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_3, ARRAY_SIZE(e_info_8168ep_3));
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);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005691
5692 data = r8168_mac_ocp_read(tp, 0xd3e2);
5693 data &= 0xf000;
5694 data |= 0x0271;
5695 r8168_mac_ocp_write(tp, 0xd3e2, data);
5696
5697 data = r8168_mac_ocp_read(tp, 0xd3e4);
5698 data &= 0xff00;
5699 r8168_mac_ocp_write(tp, 0xd3e4, data);
5700
5701 data = r8168_mac_ocp_read(tp, 0xe860);
5702 data |= 0x0080;
5703 r8168_mac_ocp_write(tp, 0xe860, data);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005704
5705 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005706}
5707
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005708static void rtl_hw_start_8168(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005709{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005710 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02005711
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005712 tp->cp_cmd &= ~INTT_MASK;
5713 tp->cp_cmd |= PktCntrDisable | INTT_1;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005714 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu2dd99532007-06-11 23:22:52 +02005715
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005716 RTL_W16(tp, IntrMitigate, 0x5151);
Francois Romieu0e485152007-02-20 00:00:26 +01005717
5718 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00005719 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005720 tp->event_slow |= RxFIFOOver | PCSTimeout;
5721 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01005722 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005723
Francois Romieu219a1e92008-06-28 11:58:39 +02005724 switch (tp->mac_version) {
5725 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005726 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005727 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005728
5729 case RTL_GIGA_MAC_VER_12:
5730 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005731 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005732 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005733
5734 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005735 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005736 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005737
5738 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005739 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005740 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005741
5742 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005743 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005744 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005745
Francois Romieu197ff762008-06-28 13:16:02 +02005746 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005747 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005748 break;
Francois Romieu197ff762008-06-28 13:16:02 +02005749
Francois Romieu6fb07052008-06-29 11:54:28 +02005750 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005751 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005752 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02005753
Francois Romieuef3386f2008-06-29 12:24:30 +02005754 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005755 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005756 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02005757
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005758 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005759 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005760 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005761
Francois Romieu5b538df2008-07-20 16:22:45 +02005762 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00005763 case RTL_GIGA_MAC_VER_26:
5764 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005765 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005766 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02005767
françois romieue6de30d2011-01-03 15:08:37 +00005768 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005769 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005770 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005771
hayeswang4804b3b2011-03-21 01:50:29 +00005772 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005773 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005774 break;
5775
hayeswang01dc7fe2011-03-21 01:50:28 +00005776 case RTL_GIGA_MAC_VER_32:
5777 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005778 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005779 break;
5780 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005781 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005782 break;
françois romieue6de30d2011-01-03 15:08:37 +00005783
Hayes Wangc2218922011-09-06 16:55:18 +08005784 case RTL_GIGA_MAC_VER_35:
5785 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005786 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005787 break;
5788
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005789 case RTL_GIGA_MAC_VER_38:
5790 rtl_hw_start_8411(tp);
5791 break;
5792
Hayes Wangc5583862012-07-02 17:23:22 +08005793 case RTL_GIGA_MAC_VER_40:
5794 case RTL_GIGA_MAC_VER_41:
5795 rtl_hw_start_8168g_1(tp);
5796 break;
hayeswang57538c42013-04-01 22:23:40 +00005797 case RTL_GIGA_MAC_VER_42:
5798 rtl_hw_start_8168g_2(tp);
5799 break;
Hayes Wangc5583862012-07-02 17:23:22 +08005800
hayeswang45dd95c2013-07-08 17:09:01 +08005801 case RTL_GIGA_MAC_VER_44:
5802 rtl_hw_start_8411_2(tp);
5803 break;
5804
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005805 case RTL_GIGA_MAC_VER_45:
5806 case RTL_GIGA_MAC_VER_46:
5807 rtl_hw_start_8168h_1(tp);
5808 break;
5809
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005810 case RTL_GIGA_MAC_VER_49:
5811 rtl_hw_start_8168ep_1(tp);
5812 break;
5813
5814 case RTL_GIGA_MAC_VER_50:
5815 rtl_hw_start_8168ep_2(tp);
5816 break;
5817
5818 case RTL_GIGA_MAC_VER_51:
5819 rtl_hw_start_8168ep_3(tp);
5820 break;
5821
Francois Romieu219a1e92008-06-28 11:58:39 +02005822 default:
Heiner Kallweit49d17512018-06-28 20:36:15 +02005823 netif_err(tp, drv, tp->dev,
5824 "unknown chipset (mac_version = %d)\n",
5825 tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00005826 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005827 }
Francois Romieu07ce4062007-02-23 23:36:39 +01005828}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005830static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005831{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005832 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005833 { 0x01, 0, 0x6e65 },
5834 { 0x02, 0, 0x091f },
5835 { 0x03, 0, 0xc2f9 },
5836 { 0x06, 0, 0xafb5 },
5837 { 0x07, 0, 0x0e00 },
5838 { 0x19, 0, 0xec80 },
5839 { 0x01, 0, 0x2e65 },
5840 { 0x01, 0, 0x6e65 }
5841 };
5842 u8 cfg1;
5843
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005844 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005845
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005846 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005847
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005848 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005849
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005850 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02005851 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005852 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005853
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005854 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005855 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005856 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005857
Francois Romieufdf6fc02012-07-06 22:40:38 +02005858 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02005859}
5860
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005861static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005862{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005863 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005864
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005865 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005866
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005867 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
5868 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005869}
5870
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005871static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005872{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005873 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005874
Francois Romieufdf6fc02012-07-06 22:40:38 +02005875 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005876}
5877
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005878static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005879{
5880 static const struct ephy_info e_info_8105e_1[] = {
5881 { 0x07, 0, 0x4000 },
5882 { 0x19, 0, 0x0200 },
5883 { 0x19, 0, 0x0020 },
5884 { 0x1e, 0, 0x2000 },
5885 { 0x03, 0, 0x0001 },
5886 { 0x19, 0, 0x0100 },
5887 { 0x19, 0, 0x0004 },
5888 { 0x0a, 0, 0x0020 }
5889 };
5890
Francois Romieucecb5fd2011-04-01 10:21:07 +02005891 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005892 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005893
Francois Romieucecb5fd2011-04-01 10:21:07 +02005894 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005895 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005896
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005897 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5898 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005899
Francois Romieufdf6fc02012-07-06 22:40:38 +02005900 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08005901
5902 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005903}
5904
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005905static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005906{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005907 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005908 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005909}
5910
Hayes Wang7e18dca2012-03-30 14:33:02 +08005911static void rtl_hw_start_8402(struct rtl8169_private *tp)
5912{
Hayes Wang7e18dca2012-03-30 14:33:02 +08005913 static const struct ephy_info e_info_8402[] = {
5914 { 0x19, 0xffff, 0xff64 },
5915 { 0x1e, 0, 0x4000 }
5916 };
5917
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005918 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005919
5920 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005921 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005922
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005923 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5924 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005925
Francois Romieufdf6fc02012-07-06 22:40:38 +02005926 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08005927
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005928 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005929
Francois Romieufdf6fc02012-07-06 22:40:38 +02005930 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5931 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005932 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5933 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005934 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5935 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005936 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005937
5938 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005939}
5940
Hayes Wang5598bfe2012-07-02 17:23:21 +08005941static void rtl_hw_start_8106(struct rtl8169_private *tp)
5942{
Hayes Wang5598bfe2012-07-02 17:23:21 +08005943 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005944 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005945
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005946 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5947 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5948 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08005949
5950 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005951}
5952
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005953static void rtl_hw_start_8101(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005954{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005955 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5956 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00005957
Francois Romieucecb5fd2011-04-01 10:21:07 +02005958 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08005959 tp->mac_version == RTL_GIGA_MAC_VER_16)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005960 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06005961 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02005962
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005963 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00005964
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005965 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005966 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
hayeswang1a964642013-04-01 22:23:41 +00005967
Francois Romieu2857ffb2008-08-02 21:08:49 +02005968 switch (tp->mac_version) {
5969 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005970 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005971 break;
5972
5973 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005974 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005975 break;
5976
5977 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005978 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005979 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005980
5981 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005982 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005983 break;
5984 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005985 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005986 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005987
5988 case RTL_GIGA_MAC_VER_37:
5989 rtl_hw_start_8402(tp);
5990 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08005991
5992 case RTL_GIGA_MAC_VER_39:
5993 rtl_hw_start_8106(tp);
5994 break;
hayeswang58152cd2013-04-01 22:23:42 +00005995 case RTL_GIGA_MAC_VER_43:
5996 rtl_hw_start_8168g_2(tp);
5997 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005998 case RTL_GIGA_MAC_VER_47:
5999 case RTL_GIGA_MAC_VER_48:
6000 rtl_hw_start_8168h_1(tp);
6001 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006002 }
6003
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006004 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005}
6006
6007static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6008{
Francois Romieud58d46b2011-05-03 16:38:29 +02006009 struct rtl8169_private *tp = netdev_priv(dev);
6010
Francois Romieud58d46b2011-05-03 16:38:29 +02006011 if (new_mtu > ETH_DATA_LEN)
6012 rtl_hw_jumbo_enable(tp);
6013 else
6014 rtl_hw_jumbo_disable(tp);
6015
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006017 netdev_update_features(dev);
6018
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006019 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020}
6021
6022static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6023{
Al Viro95e09182007-12-22 18:55:39 +00006024 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6026}
6027
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006028static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6029 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030{
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006031 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
6032 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006033
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006034 kfree(*data_buff);
6035 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 rtl8169_make_unusable_by_asic(desc);
6037}
6038
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006039static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040{
6041 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6042
Alexander Duycka0750132014-12-11 15:02:17 -08006043 /* Force memory writes to complete before releasing descriptor */
6044 dma_wmb();
6045
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006046 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047}
6048
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006049static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006051 return (void *)ALIGN((long)data, 16);
6052}
6053
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006054static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6055 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006056{
6057 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 dma_addr_t mapping;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006059 struct device *d = tp_to_dev(tp);
Heiner Kallweitd3b404c2018-04-17 23:22:14 +02006060 int node = dev_to_node(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006062 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006063 if (!data)
6064 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006065
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006066 if (rtl8169_align(data) != data) {
6067 kfree(data);
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006068 data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006069 if (!data)
6070 return NULL;
6071 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006072
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006073 mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006074 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006075 if (unlikely(dma_mapping_error(d, mapping))) {
6076 if (net_ratelimit())
6077 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006078 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
Heiner Kallweitd731af72018-04-17 23:26:41 +02006081 desc->addr = cpu_to_le64(mapping);
6082 rtl8169_mark_to_asic(desc);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006083 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006084
6085err_out:
6086 kfree(data);
6087 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088}
6089
6090static void rtl8169_rx_clear(struct rtl8169_private *tp)
6091{
Francois Romieu07d3f512007-02-21 22:40:46 +01006092 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
6094 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006095 if (tp->Rx_databuff[i]) {
6096 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 tp->RxDescArray + i);
6098 }
6099 }
6100}
6101
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006102static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006104 desc->opts1 |= cpu_to_le32(RingEnd);
6105}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006106
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006107static int rtl8169_rx_fill(struct rtl8169_private *tp)
6108{
6109 unsigned int i;
6110
6111 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006112 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006113
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006114 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006115 if (!data) {
6116 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006117 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006118 }
6119 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006122 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6123 return 0;
6124
6125err_out:
6126 rtl8169_rx_clear(tp);
6127 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128}
6129
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006130static int rtl8169_init_ring(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 rtl8169_init_ring_indexes(tp);
6133
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006134 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
6135 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006137 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138}
6139
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006140static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 struct TxDesc *desc)
6142{
6143 unsigned int len = tx_skb->len;
6144
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006145 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6146
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 desc->opts1 = 0x00;
6148 desc->opts2 = 0x00;
6149 desc->addr = 0x00;
6150 tx_skb->len = 0;
6151}
6152
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006153static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6154 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155{
6156 unsigned int i;
6157
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006158 for (i = 0; i < n; i++) {
6159 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160 struct ring_info *tx_skb = tp->tx_skb + entry;
6161 unsigned int len = tx_skb->len;
6162
6163 if (len) {
6164 struct sk_buff *skb = tx_skb->skb;
6165
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006166 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 tp->TxDescArray + entry);
6168 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006169 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 tx_skb->skb = NULL;
6171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 }
6173 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006174}
6175
6176static void rtl8169_tx_clear(struct rtl8169_private *tp)
6177{
6178 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 tp->cur_tx = tp->dirty_tx = 0;
6180}
6181
Francois Romieu4422bcd2012-01-26 11:23:32 +01006182static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183{
David Howellsc4028952006-11-22 14:57:56 +00006184 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01006185 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186
Francois Romieuda78dbf2012-01-26 14:18:23 +01006187 napi_disable(&tp->napi);
6188 netif_stop_queue(dev);
6189 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190
françois romieuc7c2c392011-12-04 20:30:52 +00006191 rtl8169_hw_reset(tp);
6192
Francois Romieu56de4142011-03-15 17:29:31 +01006193 for (i = 0; i < NUM_RX_DESC; i++)
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006194 rtl8169_mark_to_asic(tp->RxDescArray + i);
Francois Romieu56de4142011-03-15 17:29:31 +01006195
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00006197 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198
Francois Romieuda78dbf2012-01-26 14:18:23 +01006199 napi_enable(&tp->napi);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006200 rtl_hw_start(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01006201 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202}
6203
6204static void rtl8169_tx_timeout(struct net_device *dev)
6205{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006206 struct rtl8169_private *tp = netdev_priv(dev);
6207
6208 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209}
6210
6211static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07006212 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213{
6214 struct skb_shared_info *info = skb_shinfo(skb);
6215 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006216 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006217 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218
6219 entry = tp->cur_tx;
6220 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006221 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 dma_addr_t mapping;
6223 u32 status, len;
6224 void *addr;
6225
6226 entry = (entry + 1) % NUM_TX_DESC;
6227
6228 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00006229 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00006230 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006231 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006232 if (unlikely(dma_mapping_error(d, mapping))) {
6233 if (net_ratelimit())
6234 netif_err(tp, drv, tp->dev,
6235 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006236 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238
Francois Romieucecb5fd2011-04-01 10:21:07 +02006239 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006240 status = opts[0] | len |
6241 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242
6243 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07006244 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 txd->addr = cpu_to_le64(mapping);
6246
6247 tp->tx_skb[entry].len = len;
6248 }
6249
6250 if (cur_frag) {
6251 tp->tx_skb[entry].skb = skb;
6252 txd->opts1 |= cpu_to_le32(LastFrag);
6253 }
6254
6255 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006256
6257err_out:
6258 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6259 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260}
6261
françois romieub423e9a2013-05-18 01:24:46 +00006262static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6263{
6264 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6265}
6266
hayeswange9746042014-07-11 16:25:58 +08006267static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6268 struct net_device *dev);
6269/* r8169_csum_workaround()
6270 * The hw limites the value the transport offset. When the offset is out of the
6271 * range, calculate the checksum by sw.
6272 */
6273static void r8169_csum_workaround(struct rtl8169_private *tp,
6274 struct sk_buff *skb)
6275{
6276 if (skb_shinfo(skb)->gso_size) {
6277 netdev_features_t features = tp->dev->features;
6278 struct sk_buff *segs, *nskb;
6279
6280 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6281 segs = skb_gso_segment(skb, features);
6282 if (IS_ERR(segs) || !segs)
6283 goto drop;
6284
6285 do {
6286 nskb = segs;
6287 segs = segs->next;
6288 nskb->next = NULL;
6289 rtl8169_start_xmit(nskb, tp->dev);
6290 } while (segs);
6291
Alexander Duyckeb781392015-05-01 10:34:44 -07006292 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006293 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6294 if (skb_checksum_help(skb) < 0)
6295 goto drop;
6296
6297 rtl8169_start_xmit(skb, tp->dev);
6298 } else {
6299 struct net_device_stats *stats;
6300
6301drop:
6302 stats = &tp->dev->stats;
6303 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07006304 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006305 }
6306}
6307
6308/* msdn_giant_send_check()
6309 * According to the document of microsoft, the TCP Pseudo Header excludes the
6310 * packet length for IPv6 TCP large packets.
6311 */
6312static int msdn_giant_send_check(struct sk_buff *skb)
6313{
6314 const struct ipv6hdr *ipv6h;
6315 struct tcphdr *th;
6316 int ret;
6317
6318 ret = skb_cow_head(skb, 0);
6319 if (ret)
6320 return ret;
6321
6322 ipv6h = ipv6_hdr(skb);
6323 th = tcp_hdr(skb);
6324
6325 th->check = 0;
6326 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6327
6328 return ret;
6329}
6330
hayeswang5888d3f2014-07-11 16:25:56 +08006331static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6332 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333{
Michał Mirosław350fb322011-04-08 06:35:56 +00006334 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335
Francois Romieu2b7b4312011-04-18 22:53:24 -07006336 if (mss) {
6337 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08006338 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6339 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6340 const struct iphdr *ip = ip_hdr(skb);
6341
6342 if (ip->protocol == IPPROTO_TCP)
6343 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6344 else if (ip->protocol == IPPROTO_UDP)
6345 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6346 else
6347 WARN_ON_ONCE(1);
6348 }
6349
6350 return true;
6351}
6352
6353static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6354 struct sk_buff *skb, u32 *opts)
6355{
hayeswangbdfa4ed2014-07-11 16:25:57 +08006356 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08006357 u32 mss = skb_shinfo(skb)->gso_size;
6358
6359 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08006360 if (transport_offset > GTTCPHO_MAX) {
6361 netif_warn(tp, tx_err, tp->dev,
6362 "Invalid transport offset 0x%x for TSO\n",
6363 transport_offset);
6364 return false;
6365 }
6366
Heiner Kallweit4ff36462018-05-02 21:40:02 +02006367 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08006368 case htons(ETH_P_IP):
6369 opts[0] |= TD1_GTSENV4;
6370 break;
6371
6372 case htons(ETH_P_IPV6):
6373 if (msdn_giant_send_check(skb))
6374 return false;
6375
6376 opts[0] |= TD1_GTSENV6;
6377 break;
6378
6379 default:
6380 WARN_ON_ONCE(1);
6381 break;
6382 }
6383
hayeswangbdfa4ed2014-07-11 16:25:57 +08006384 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08006385 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006386 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08006387 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388
françois romieub423e9a2013-05-18 01:24:46 +00006389 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006390 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00006391
hayeswange9746042014-07-11 16:25:58 +08006392 if (transport_offset > TCPHO_MAX) {
6393 netif_warn(tp, tx_err, tp->dev,
6394 "Invalid transport offset 0x%x\n",
6395 transport_offset);
6396 return false;
6397 }
6398
Heiner Kallweit4ff36462018-05-02 21:40:02 +02006399 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08006400 case htons(ETH_P_IP):
6401 opts[1] |= TD1_IPv4_CS;
6402 ip_protocol = ip_hdr(skb)->protocol;
6403 break;
6404
6405 case htons(ETH_P_IPV6):
6406 opts[1] |= TD1_IPv6_CS;
6407 ip_protocol = ipv6_hdr(skb)->nexthdr;
6408 break;
6409
6410 default:
6411 ip_protocol = IPPROTO_RAW;
6412 break;
6413 }
6414
6415 if (ip_protocol == IPPROTO_TCP)
6416 opts[1] |= TD1_TCP_CS;
6417 else if (ip_protocol == IPPROTO_UDP)
6418 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006419 else
6420 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08006421
6422 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00006423 } else {
6424 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006425 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 }
hayeswang5888d3f2014-07-11 16:25:56 +08006427
françois romieub423e9a2013-05-18 01:24:46 +00006428 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429}
6430
Stephen Hemminger613573252009-08-31 19:50:58 +00006431static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6432 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433{
6434 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006435 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006437 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438 dma_addr_t mapping;
6439 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006440 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006441 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02006442
Julien Ducourthial477206a2012-05-09 00:00:06 +02006443 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006444 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006445 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446 }
6447
6448 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006449 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450
françois romieub423e9a2013-05-18 01:24:46 +00006451 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6452 opts[0] = DescOwn;
6453
hayeswange9746042014-07-11 16:25:58 +08006454 if (!tp->tso_csum(tp, skb, opts)) {
6455 r8169_csum_workaround(tp, skb);
6456 return NETDEV_TX_OK;
6457 }
françois romieub423e9a2013-05-18 01:24:46 +00006458
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006459 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006460 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006461 if (unlikely(dma_mapping_error(d, mapping))) {
6462 if (net_ratelimit())
6463 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006464 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466
6467 tp->tx_skb[entry].len = len;
6468 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469
Francois Romieu2b7b4312011-04-18 22:53:24 -07006470 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006471 if (frags < 0)
6472 goto err_dma_1;
6473 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07006474 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006475 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07006476 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006477 tp->tx_skb[entry].skb = skb;
6478 }
6479
Francois Romieu2b7b4312011-04-18 22:53:24 -07006480 txd->opts2 = cpu_to_le32(opts[1]);
6481
Richard Cochran5047fb52012-03-10 07:29:42 +00006482 skb_tx_timestamp(skb);
6483
Alexander Duycka0750132014-12-11 15:02:17 -08006484 /* Force memory writes to complete before releasing descriptor */
6485 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486
Francois Romieucecb5fd2011-04-01 10:21:07 +02006487 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006488 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489 txd->opts1 = cpu_to_le32(status);
6490
Alexander Duycka0750132014-12-11 15:02:17 -08006491 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00006492 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493
Alexander Duycka0750132014-12-11 15:02:17 -08006494 tp->cur_tx += frags + 1;
6495
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006496 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497
David S. Miller87cda7c2015-02-22 15:54:29 -05006498 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01006499
David S. Miller87cda7c2015-02-22 15:54:29 -05006500 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01006501 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6502 * not miss a ring update when it notices a stopped queue.
6503 */
6504 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01006506 /* Sync with rtl_tx:
6507 * - publish queue status and cur_tx ring index (write barrier)
6508 * - refresh dirty_tx ring index (read barrier).
6509 * May the current thread have a pessimistic view of the ring
6510 * status and forget to wake up queue, a racing rtl_tx thread
6511 * can't.
6512 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006513 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02006514 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515 netif_wake_queue(dev);
6516 }
6517
Stephen Hemminger613573252009-08-31 19:50:58 +00006518 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006520err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006521 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006522err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006523 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006524 dev->stats.tx_dropped++;
6525 return NETDEV_TX_OK;
6526
6527err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006529 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00006530 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531}
6532
6533static void rtl8169_pcierr_interrupt(struct net_device *dev)
6534{
6535 struct rtl8169_private *tp = netdev_priv(dev);
6536 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 u16 pci_status, pci_cmd;
6538
6539 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6540 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6541
Joe Perchesbf82c182010-02-09 11:49:50 +00006542 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6543 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544
6545 /*
6546 * The recovery sequence below admits a very elaborated explanation:
6547 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01006548 * - I did not see what else could be done;
6549 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 *
6551 * Feel free to adjust to your needs.
6552 */
Francois Romieua27993f2006-12-18 00:04:19 +01006553 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01006554 pci_cmd &= ~PCI_COMMAND_PARITY;
6555 else
6556 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6557
6558 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559
6560 pci_write_config_word(pdev, PCI_STATUS,
6561 pci_status & (PCI_STATUS_DETECTED_PARITY |
6562 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6563 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6564
6565 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00006566 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006567 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 tp->cp_cmd &= ~PCIDAC;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006569 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571 }
6572
françois romieue6de30d2011-01-03 15:08:37 +00006573 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01006574
Francois Romieu98ddf982012-01-31 10:47:34 +01006575 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576}
6577
Francois Romieuda78dbf2012-01-26 14:18:23 +01006578static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579{
6580 unsigned int dirty_tx, tx_left;
6581
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582 dirty_tx = tp->dirty_tx;
6583 smp_rmb();
6584 tx_left = tp->cur_tx - dirty_tx;
6585
6586 while (tx_left > 0) {
6587 unsigned int entry = dirty_tx % NUM_TX_DESC;
6588 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 u32 status;
6590
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6592 if (status & DescOwn)
6593 break;
6594
Alexander Duycka0750132014-12-11 15:02:17 -08006595 /* This barrier is needed to keep us from reading
6596 * any other fields out of the Tx descriptor until
6597 * we know the status of DescOwn
6598 */
6599 dma_rmb();
6600
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006601 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006602 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05006604 u64_stats_update_begin(&tp->tx_stats.syncp);
6605 tp->tx_stats.packets++;
6606 tp->tx_stats.bytes += tx_skb->skb->len;
6607 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006608 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 tx_skb->skb = NULL;
6610 }
6611 dirty_tx++;
6612 tx_left--;
6613 }
6614
6615 if (tp->dirty_tx != dirty_tx) {
6616 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01006617 /* Sync with rtl8169_start_xmit:
6618 * - publish dirty_tx ring index (write barrier)
6619 * - refresh cur_tx ring index and queue status (read barrier)
6620 * May the current thread miss the stopped queue condition,
6621 * a racing xmit thread can only have a right view of the
6622 * ring status.
6623 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006624 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02006626 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627 netif_wake_queue(dev);
6628 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02006629 /*
6630 * 8168 hack: TxPoll requests are lost when the Tx packets are
6631 * too close. Let's kick an extra TxPoll request when a burst
6632 * of start_xmit activity is detected (if it is not detected,
6633 * it is slow enough). -- FR
6634 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006635 if (tp->cur_tx != dirty_tx)
6636 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 }
6638}
6639
Francois Romieu126fa4b2005-05-12 20:09:17 -04006640static inline int rtl8169_fragmented_frame(u32 status)
6641{
6642 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6643}
6644
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006645static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647 u32 status = opts1 & RxProtoMask;
6648
6649 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00006650 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 skb->ip_summed = CHECKSUM_UNNECESSARY;
6652 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006653 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654}
6655
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006656static struct sk_buff *rtl8169_try_rx_copy(void *data,
6657 struct rtl8169_private *tp,
6658 int pkt_size,
6659 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02006661 struct sk_buff *skb;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006662 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006664 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006665 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006666 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08006667 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006668 if (skb)
Heiner Kallweit8a67aa82018-04-17 23:19:07 +02006669 skb_copy_to_linear_data(skb, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006670 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6671
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006672 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673}
6674
Francois Romieuda78dbf2012-01-26 14:18:23 +01006675static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676{
6677 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006678 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681
Timo Teräs9fba0812013-01-15 21:01:24 +00006682 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006684 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 u32 status;
6686
Heiner Kallweit62028062018-04-17 23:30:29 +02006687 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 if (status & DescOwn)
6689 break;
Alexander Duycka0750132014-12-11 15:02:17 -08006690
6691 /* This barrier is needed to keep us from reading
6692 * any other fields out of the Rx descriptor until
6693 * we know the status of DescOwn
6694 */
6695 dma_rmb();
6696
Richard Dawe4dcb7d32005-05-27 21:12:00 +02006697 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006698 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6699 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006700 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02006702 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02006704 dev->stats.rx_crc_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006705 /* RxFOVF is a reserved bit on later chip versions */
6706 if (tp->mac_version == RTL_GIGA_MAC_VER_01 &&
6707 status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006708 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006709 dev->stats.rx_fifo_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006710 } else if (status & (RxRUNT | RxCRC) &&
6711 !(status & RxRWT) &&
6712 dev->features & NETIF_F_RXALL) {
Ben Greear6bbe0212012-02-10 15:04:33 +00006713 goto process_pkt;
Heiner Kallweit62028062018-04-17 23:30:29 +02006714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006716 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00006717 dma_addr_t addr;
6718 int pkt_size;
6719
6720process_pkt:
6721 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00006722 if (likely(!(dev->features & NETIF_F_RXFCS)))
6723 pkt_size = (status & 0x00003fff) - 4;
6724 else
6725 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726
Francois Romieu126fa4b2005-05-12 20:09:17 -04006727 /*
6728 * The driver does not support incoming fragmented
6729 * frames. They are seen as a symptom of over-mtu
6730 * sized frames.
6731 */
6732 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02006733 dev->stats.rx_dropped++;
6734 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00006735 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006736 }
6737
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006738 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6739 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006740 if (!skb) {
6741 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00006742 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743 }
6744
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006745 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 skb_put(skb, pkt_size);
6747 skb->protocol = eth_type_trans(skb, dev);
6748
Francois Romieu7a8fc772011-03-01 17:18:33 +01006749 rtl8169_rx_vlan_tag(desc, skb);
6750
françois romieu39174292015-11-11 23:35:18 +01006751 if (skb->pkt_type == PACKET_MULTICAST)
6752 dev->stats.multicast++;
6753
Francois Romieu56de4142011-03-15 17:29:31 +01006754 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755
Junchang Wang8027aa22012-03-04 23:30:32 +01006756 u64_stats_update_begin(&tp->rx_stats.syncp);
6757 tp->rx_stats.packets++;
6758 tp->rx_stats.bytes += pkt_size;
6759 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 }
françois romieuce11ff52013-01-24 13:30:06 +00006761release_descriptor:
6762 desc->opts2 = 0;
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006763 rtl8169_mark_to_asic(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 }
6765
6766 count = cur_rx - tp->cur_rx;
6767 tp->cur_rx = cur_rx;
6768
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769 return count;
6770}
6771
Francois Romieu07d3f512007-02-21 22:40:46 +01006772static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773{
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006774 struct rtl8169_private *tp = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006776 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006778 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006779 if (status && status != 0xffff) {
6780 status &= RTL_EVENT_NAPI | tp->event_slow;
6781 if (status) {
6782 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00006783
Francois Romieuda78dbf2012-01-26 14:18:23 +01006784 rtl_irq_disable(tp);
Heiner Kallweit9a899a32018-04-17 23:21:01 +02006785 napi_schedule_irqoff(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 return IRQ_RETVAL(handled);
6789}
6790
Francois Romieuda78dbf2012-01-26 14:18:23 +01006791/*
6792 * Workqueue context.
6793 */
6794static void rtl_slow_event_work(struct rtl8169_private *tp)
6795{
6796 struct net_device *dev = tp->dev;
6797 u16 status;
6798
6799 status = rtl_get_events(tp) & tp->event_slow;
6800 rtl_ack_events(tp, status);
6801
6802 if (unlikely(status & RxFIFOOver)) {
6803 switch (tp->mac_version) {
6804 /* Work around for rx fifo overflow */
6805 case RTL_GIGA_MAC_VER_11:
6806 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01006807 /* XXX - Hack alert. See rtl_task(). */
6808 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006809 default:
6810 break;
6811 }
6812 }
6813
6814 if (unlikely(status & SYSErr))
6815 rtl8169_pcierr_interrupt(dev);
6816
6817 if (status & LinkChg)
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006818 phy_mac_interrupt(dev->phydev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006819
françois romieu7dbb4912012-06-09 10:53:16 +00006820 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006821}
6822
Francois Romieu4422bcd2012-01-26 11:23:32 +01006823static void rtl_task(struct work_struct *work)
6824{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006825 static const struct {
6826 int bitnr;
6827 void (*action)(struct rtl8169_private *);
6828 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01006829 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006830 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
6831 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
Francois Romieuda78dbf2012-01-26 14:18:23 +01006832 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01006833 struct rtl8169_private *tp =
6834 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006835 struct net_device *dev = tp->dev;
6836 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01006837
Francois Romieuda78dbf2012-01-26 14:18:23 +01006838 rtl_lock_work(tp);
6839
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006840 if (!netif_running(dev) ||
6841 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01006842 goto out_unlock;
6843
6844 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6845 bool pending;
6846
Francois Romieuda78dbf2012-01-26 14:18:23 +01006847 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006848 if (pending)
6849 rtl_work[i].action(tp);
6850 }
6851
6852out_unlock:
6853 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01006854}
6855
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006856static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006858 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6859 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006860 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
6861 int work_done= 0;
6862 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863
Francois Romieuda78dbf2012-01-26 14:18:23 +01006864 status = rtl_get_events(tp);
6865 rtl_ack_events(tp, status & ~tp->event_slow);
6866
6867 if (status & RTL_EVENT_NAPI_RX)
6868 work_done = rtl_rx(dev, tp, (u32) budget);
6869
6870 if (status & RTL_EVENT_NAPI_TX)
6871 rtl_tx(dev, tp);
6872
6873 if (status & tp->event_slow) {
6874 enable_mask &= ~tp->event_slow;
6875
6876 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006879 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08006880 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00006881
Francois Romieuda78dbf2012-01-26 14:18:23 +01006882 rtl_irq_enable(tp, enable_mask);
6883 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 }
6885
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006886 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006889static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02006890{
6891 struct rtl8169_private *tp = netdev_priv(dev);
6892
6893 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6894 return;
6895
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006896 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
6897 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02006898}
6899
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006900static void r8169_phylink_handler(struct net_device *ndev)
6901{
6902 struct rtl8169_private *tp = netdev_priv(ndev);
6903
6904 if (netif_carrier_ok(ndev)) {
6905 rtl_link_chg_patch(tp);
6906 pm_request_resume(&tp->pci_dev->dev);
6907 } else {
6908 pm_runtime_idle(&tp->pci_dev->dev);
6909 }
6910
6911 if (net_ratelimit())
6912 phy_print_status(ndev->phydev);
6913}
6914
6915static int r8169_phy_connect(struct rtl8169_private *tp)
6916{
6917 struct phy_device *phydev = mdiobus_get_phy(tp->mii_bus, 0);
6918 phy_interface_t phy_mode;
6919 int ret;
6920
6921 phy_mode = tp->mii.supports_gmii ? PHY_INTERFACE_MODE_GMII :
6922 PHY_INTERFACE_MODE_MII;
6923
6924 ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
6925 phy_mode);
6926 if (ret)
6927 return ret;
6928
6929 if (!tp->mii.supports_gmii)
6930 phy_set_max_speed(phydev, SPEED_100);
6931
6932 /* Ensure to advertise everything, incl. pause */
6933 phydev->advertising = phydev->supported;
6934
6935 phy_attached_info(phydev);
6936
6937 return 0;
6938}
6939
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940static void rtl8169_down(struct net_device *dev)
6941{
6942 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006944 phy_stop(dev->phydev);
6945
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006946 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006947 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948
Hayes Wang92fc43b2011-07-06 15:58:03 +08006949 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006950 /*
6951 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006952 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6953 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006954 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006955 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006958 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 rtl8169_tx_clear(tp);
6961
6962 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006963
6964 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965}
6966
6967static int rtl8169_close(struct net_device *dev)
6968{
6969 struct rtl8169_private *tp = netdev_priv(dev);
6970 struct pci_dev *pdev = tp->pci_dev;
6971
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006972 pm_runtime_get_sync(&pdev->dev);
6973
Francois Romieucecb5fd2011-04-01 10:21:07 +02006974 /* Update counters before going down */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006975 rtl8169_update_counters(tp);
Ivan Vecera355423d2009-02-06 21:49:57 -08006976
Francois Romieuda78dbf2012-01-26 14:18:23 +01006977 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006978 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006979
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006981 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982
Lekensteyn4ea72442013-07-22 09:53:30 +02006983 cancel_work_sync(&tp->wk.work);
6984
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006985 phy_disconnect(dev->phydev);
6986
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006987 pci_free_irq(pdev, 0, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006989 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6990 tp->RxPhyAddr);
6991 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6992 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993 tp->TxDescArray = NULL;
6994 tp->RxDescArray = NULL;
6995
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006996 pm_runtime_put_sync(&pdev->dev);
6997
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 return 0;
6999}
7000
Francois Romieudc1c00c2012-03-08 10:06:18 +01007001#ifdef CONFIG_NET_POLL_CONTROLLER
7002static void rtl8169_netpoll(struct net_device *dev)
7003{
7004 struct rtl8169_private *tp = netdev_priv(dev);
7005
Ville Syrjälä6d8b8342018-06-20 15:01:53 +03007006 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
Francois Romieudc1c00c2012-03-08 10:06:18 +01007007}
7008#endif
7009
Francois Romieudf43ac72012-03-08 09:48:40 +01007010static int rtl_open(struct net_device *dev)
7011{
7012 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007013 struct pci_dev *pdev = tp->pci_dev;
7014 int retval = -ENOMEM;
7015
7016 pm_runtime_get_sync(&pdev->dev);
7017
7018 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007019 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007020 * dma_alloc_coherent provides more.
7021 */
7022 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7023 &tp->TxPhyAddr, GFP_KERNEL);
7024 if (!tp->TxDescArray)
7025 goto err_pm_runtime_put;
7026
7027 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7028 &tp->RxPhyAddr, GFP_KERNEL);
7029 if (!tp->RxDescArray)
7030 goto err_free_tx_0;
7031
Heiner Kallweitb1127e62018-04-17 23:23:35 +02007032 retval = rtl8169_init_ring(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007033 if (retval < 0)
7034 goto err_free_rx_1;
7035
7036 INIT_WORK(&tp->wk.work, rtl_task);
7037
7038 smp_mb();
7039
7040 rtl_request_firmware(tp);
7041
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02007042 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007043 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01007044 if (retval < 0)
7045 goto err_release_fw_2;
7046
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007047 retval = r8169_phy_connect(tp);
7048 if (retval)
7049 goto err_free_irq;
7050
Francois Romieudf43ac72012-03-08 09:48:40 +01007051 rtl_lock_work(tp);
7052
7053 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7054
7055 napi_enable(&tp->napi);
7056
7057 rtl8169_init_phy(dev, tp);
7058
Francois Romieudf43ac72012-03-08 09:48:40 +01007059 rtl_pll_power_up(tp);
7060
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007061 rtl_hw_start(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007062
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007063 if (!rtl8169_init_counter_offsets(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007064 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7065
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007066 phy_start(dev->phydev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007067 netif_start_queue(dev);
7068
7069 rtl_unlock_work(tp);
7070
Heiner Kallweita92a0842018-01-08 21:39:13 +01007071 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007072out:
7073 return retval;
7074
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007075err_free_irq:
7076 pci_free_irq(pdev, 0, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007077err_release_fw_2:
7078 rtl_release_firmware(tp);
7079 rtl8169_rx_clear(tp);
7080err_free_rx_1:
7081 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7082 tp->RxPhyAddr);
7083 tp->RxDescArray = NULL;
7084err_free_tx_0:
7085 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7086 tp->TxPhyAddr);
7087 tp->TxDescArray = NULL;
7088err_pm_runtime_put:
7089 pm_runtime_put_noidle(&pdev->dev);
7090 goto out;
7091}
7092
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007093static void
Junchang Wang8027aa22012-03-04 23:30:32 +01007094rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095{
7096 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007097 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02007098 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01007099 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007101 pm_runtime_get_noresume(&pdev->dev);
7102
7103 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007104 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007105
Junchang Wang8027aa22012-03-04 23:30:32 +01007106 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007107 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007108 stats->rx_packets = tp->rx_stats.packets;
7109 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007110 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007111
Junchang Wang8027aa22012-03-04 23:30:32 +01007112 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007113 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007114 stats->tx_packets = tp->tx_stats.packets;
7115 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007116 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007117
7118 stats->rx_dropped = dev->stats.rx_dropped;
7119 stats->tx_dropped = dev->stats.tx_dropped;
7120 stats->rx_length_errors = dev->stats.rx_length_errors;
7121 stats->rx_errors = dev->stats.rx_errors;
7122 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7123 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7124 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02007125 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01007126
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007127 /*
7128 * Fetch additonal counter values missing in stats collected by driver
7129 * from tally counters.
7130 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007131 if (pm_runtime_active(&pdev->dev))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007132 rtl8169_update_counters(tp);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007133
7134 /*
7135 * Subtract values fetched during initalization.
7136 * See rtl8169_init_counter_offsets for a description why we do that.
7137 */
Corinna Vinschen42020322015-09-10 10:47:35 +02007138 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007139 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02007140 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007141 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02007142 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007143 le16_to_cpu(tp->tc_offset.tx_aborted);
7144
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007145 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146}
7147
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007148static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01007149{
françois romieu065c27c2011-01-03 15:08:12 +00007150 struct rtl8169_private *tp = netdev_priv(dev);
7151
Francois Romieu5d06a992006-02-23 00:47:58 +01007152 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007153 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01007154
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007155 phy_stop(dev->phydev);
Francois Romieu5d06a992006-02-23 00:47:58 +01007156 netif_device_detach(dev);
7157 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007158
7159 rtl_lock_work(tp);
7160 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007161 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007162 rtl_unlock_work(tp);
7163
7164 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007165}
Francois Romieu5d06a992006-02-23 00:47:58 +01007166
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007167#ifdef CONFIG_PM
7168
7169static int rtl8169_suspend(struct device *device)
7170{
7171 struct pci_dev *pdev = to_pci_dev(device);
7172 struct net_device *dev = pci_get_drvdata(pdev);
7173
7174 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02007175
Francois Romieu5d06a992006-02-23 00:47:58 +01007176 return 0;
7177}
7178
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007179static void __rtl8169_resume(struct net_device *dev)
7180{
françois romieu065c27c2011-01-03 15:08:12 +00007181 struct rtl8169_private *tp = netdev_priv(dev);
7182
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007183 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00007184
7185 rtl_pll_power_up(tp);
Heiner Kallweit92bad852018-06-24 18:37:36 +02007186 rtl8169_init_phy(dev, tp);
françois romieu065c27c2011-01-03 15:08:12 +00007187
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007188 phy_start(tp->dev->phydev);
7189
Artem Savkovcff4c162012-04-03 10:29:11 +00007190 rtl_lock_work(tp);
7191 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007192 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00007193 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007194
Francois Romieu98ddf982012-01-31 10:47:34 +01007195 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007196}
7197
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007198static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01007199{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007200 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01007201 struct net_device *dev = pci_get_drvdata(pdev);
7202
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007203 if (netif_running(dev))
7204 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01007205
Francois Romieu5d06a992006-02-23 00:47:58 +01007206 return 0;
7207}
7208
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007209static int rtl8169_runtime_suspend(struct device *device)
7210{
7211 struct pci_dev *pdev = to_pci_dev(device);
7212 struct net_device *dev = pci_get_drvdata(pdev);
7213 struct rtl8169_private *tp = netdev_priv(dev);
7214
Heiner Kallweita92a0842018-01-08 21:39:13 +01007215 if (!tp->TxDescArray) {
7216 rtl_pll_power_down(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007217 return 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007218 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007219
Francois Romieuda78dbf2012-01-26 14:18:23 +01007220 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007221 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007222 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007223
7224 rtl8169_net_suspend(dev);
7225
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007226 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007227 rtl8169_rx_missed(dev);
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007228 rtl8169_update_counters(tp);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007229
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007230 return 0;
7231}
7232
7233static int rtl8169_runtime_resume(struct device *device)
7234{
7235 struct pci_dev *pdev = to_pci_dev(device);
7236 struct net_device *dev = pci_get_drvdata(pdev);
7237 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08007238 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007239
7240 if (!tp->TxDescArray)
7241 return 0;
7242
Francois Romieuda78dbf2012-01-26 14:18:23 +01007243 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007244 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007245 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007246
7247 __rtl8169_resume(dev);
7248
7249 return 0;
7250}
7251
7252static int rtl8169_runtime_idle(struct device *device)
7253{
7254 struct pci_dev *pdev = to_pci_dev(device);
7255 struct net_device *dev = pci_get_drvdata(pdev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007256
Heiner Kallweita92a0842018-01-08 21:39:13 +01007257 if (!netif_running(dev) || !netif_carrier_ok(dev))
7258 pm_schedule_suspend(device, 10000);
7259
7260 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007261}
7262
Alexey Dobriyan47145212009-12-14 18:00:08 -08007263static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02007264 .suspend = rtl8169_suspend,
7265 .resume = rtl8169_resume,
7266 .freeze = rtl8169_suspend,
7267 .thaw = rtl8169_resume,
7268 .poweroff = rtl8169_suspend,
7269 .restore = rtl8169_resume,
7270 .runtime_suspend = rtl8169_runtime_suspend,
7271 .runtime_resume = rtl8169_runtime_resume,
7272 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007273};
7274
7275#define RTL8169_PM_OPS (&rtl8169_pm_ops)
7276
7277#else /* !CONFIG_PM */
7278
7279#define RTL8169_PM_OPS NULL
7280
7281#endif /* !CONFIG_PM */
7282
David S. Miller1805b2f2011-10-24 18:18:09 -04007283static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7284{
David S. Miller1805b2f2011-10-24 18:18:09 -04007285 /* WoL fails with 8168b when the receiver is disabled. */
7286 switch (tp->mac_version) {
7287 case RTL_GIGA_MAC_VER_11:
7288 case RTL_GIGA_MAC_VER_12:
7289 case RTL_GIGA_MAC_VER_17:
7290 pci_clear_master(tp->pci_dev);
7291
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007292 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007293 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007294 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04007295 break;
7296 default:
7297 break;
7298 }
7299}
7300
Francois Romieu1765f952008-09-13 17:21:40 +02007301static void rtl_shutdown(struct pci_dev *pdev)
7302{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007303 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00007304 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02007305
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007306 rtl8169_net_suspend(dev);
7307
Francois Romieucecb5fd2011-04-01 10:21:07 +02007308 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08007309 rtl_rar_set(tp, dev->perm_addr);
7310
Hayes Wang92fc43b2011-07-06 15:58:03 +08007311 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00007312
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007313 if (system_state == SYSTEM_POWER_OFF) {
Heiner Kallweit433f9d02018-06-24 18:39:06 +02007314 if (tp->saved_wolopts) {
David S. Miller1805b2f2011-10-24 18:18:09 -04007315 rtl_wol_suspend_quirk(tp);
7316 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00007317 }
7318
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007319 pci_wake_from_d3(pdev, true);
7320 pci_set_power_state(pdev, PCI_D3hot);
7321 }
7322}
Francois Romieu5d06a992006-02-23 00:47:58 +01007323
Bill Pembertonbaf63292012-12-03 09:23:28 -05007324static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01007325{
7326 struct net_device *dev = pci_get_drvdata(pdev);
7327 struct rtl8169_private *tp = netdev_priv(dev);
7328
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007329 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01007330 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01007331
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007332 netif_napi_del(&tp->napi);
7333
Francois Romieue27566e2012-03-08 09:54:01 +01007334 unregister_netdev(dev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007335 mdiobus_unregister(tp->mii_bus);
Francois Romieue27566e2012-03-08 09:54:01 +01007336
7337 rtl_release_firmware(tp);
7338
7339 if (pci_dev_run_wake(pdev))
7340 pm_runtime_get_noresume(&pdev->dev);
7341
7342 /* restore original MAC address */
7343 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01007344}
7345
Francois Romieufa9c3852012-03-08 10:01:50 +01007346static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01007347 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01007348 .ndo_stop = rtl8169_close,
7349 .ndo_get_stats64 = rtl8169_get_stats64,
7350 .ndo_start_xmit = rtl8169_start_xmit,
7351 .ndo_tx_timeout = rtl8169_tx_timeout,
7352 .ndo_validate_addr = eth_validate_addr,
7353 .ndo_change_mtu = rtl8169_change_mtu,
7354 .ndo_fix_features = rtl8169_fix_features,
7355 .ndo_set_features = rtl8169_set_features,
7356 .ndo_set_mac_address = rtl_set_mac_address,
7357 .ndo_do_ioctl = rtl8169_ioctl,
7358 .ndo_set_rx_mode = rtl_set_rx_mode,
7359#ifdef CONFIG_NET_POLL_CONTROLLER
7360 .ndo_poll_controller = rtl8169_netpoll,
7361#endif
7362
7363};
7364
Francois Romieu31fa8b12012-03-08 10:09:40 +01007365static const struct rtl_cfg_info {
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007366 void (*hw_start)(struct rtl8169_private *tp);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007367 u16 event_slow;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007368 unsigned int has_gmii:1;
Francois Romieu50970832017-10-27 13:24:49 +03007369 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007370 u8 default_ver;
7371} rtl_cfg_infos [] = {
7372 [RTL_CFG_0] = {
7373 .hw_start = rtl_hw_start_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007374 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007375 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007376 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007377 .default_ver = RTL_GIGA_MAC_VER_01,
7378 },
7379 [RTL_CFG_1] = {
7380 .hw_start = rtl_hw_start_8168,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007381 .event_slow = SYSErr | LinkChg | RxOverflow,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007382 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007383 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007384 .default_ver = RTL_GIGA_MAC_VER_11,
7385 },
7386 [RTL_CFG_2] = {
7387 .hw_start = rtl_hw_start_8101,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007388 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
7389 PCSTimeout,
Francois Romieu50970832017-10-27 13:24:49 +03007390 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007391 .default_ver = RTL_GIGA_MAC_VER_13,
7392 }
7393};
7394
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007395static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01007396{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007397 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007398
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007399 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007400 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
7401 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
7402 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007403 flags = PCI_IRQ_LEGACY;
7404 } else {
7405 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007406 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007407
7408 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007409}
7410
Hayes Wangc5583862012-07-02 17:23:22 +08007411DECLARE_RTL_COND(rtl_link_list_ready_cond)
7412{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007413 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08007414}
7415
7416DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7417{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007418 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08007419}
7420
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007421static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
7422{
7423 struct rtl8169_private *tp = mii_bus->priv;
7424
7425 if (phyaddr > 0)
7426 return -ENODEV;
7427
7428 return rtl_readphy(tp, phyreg);
7429}
7430
7431static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
7432 int phyreg, u16 val)
7433{
7434 struct rtl8169_private *tp = mii_bus->priv;
7435
7436 if (phyaddr > 0)
7437 return -ENODEV;
7438
7439 rtl_writephy(tp, phyreg, val);
7440
7441 return 0;
7442}
7443
7444static int r8169_mdio_register(struct rtl8169_private *tp)
7445{
7446 struct pci_dev *pdev = tp->pci_dev;
7447 struct phy_device *phydev;
7448 struct mii_bus *new_bus;
7449 int ret;
7450
7451 new_bus = devm_mdiobus_alloc(&pdev->dev);
7452 if (!new_bus)
7453 return -ENOMEM;
7454
7455 new_bus->name = "r8169";
7456 new_bus->priv = tp;
7457 new_bus->parent = &pdev->dev;
7458 new_bus->irq[0] = PHY_IGNORE_INTERRUPT;
7459 snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x",
7460 PCI_DEVID(pdev->bus->number, pdev->devfn));
7461
7462 new_bus->read = r8169_mdio_read_reg;
7463 new_bus->write = r8169_mdio_write_reg;
7464
7465 ret = mdiobus_register(new_bus);
7466 if (ret)
7467 return ret;
7468
7469 phydev = mdiobus_get_phy(new_bus, 0);
7470 if (!phydev) {
7471 mdiobus_unregister(new_bus);
7472 return -ENODEV;
7473 }
7474
7475 tp->mii_bus = new_bus;
7476
7477 return 0;
7478}
7479
Bill Pembertonbaf63292012-12-03 09:23:28 -05007480static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007481{
Hayes Wangc5583862012-07-02 17:23:22 +08007482 u32 data;
7483
7484 tp->ocp_base = OCP_STD_PHY_BASE;
7485
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007486 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08007487
7488 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7489 return;
7490
7491 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7492 return;
7493
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007494 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08007495 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007496 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08007497
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007498 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007499 data &= ~(1 << 14);
7500 r8168_mac_ocp_write(tp, 0xe8de, data);
7501
7502 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7503 return;
7504
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007505 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007506 data |= (1 << 15);
7507 r8168_mac_ocp_write(tp, 0xe8de, data);
7508
7509 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7510 return;
7511}
7512
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007513static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
7514{
7515 rtl8168ep_stop_cmac(tp);
7516 rtl_hw_init_8168g(tp);
7517}
7518
Bill Pembertonbaf63292012-12-03 09:23:28 -05007519static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007520{
7521 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02007522 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007523 rtl_hw_init_8168g(tp);
7524 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02007525 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007526 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08007527 break;
Hayes Wangc5583862012-07-02 17:23:22 +08007528 default:
7529 break;
7530 }
7531}
7532
hayeswang929a0312014-09-16 11:40:47 +08007533static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01007534{
7535 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007536 struct rtl8169_private *tp;
7537 struct mii_if_info *mii;
7538 struct net_device *dev;
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007539 int chipset, region, i;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007540 int rc;
7541
7542 if (netif_msg_drv(&debug)) {
7543 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
7544 MODULENAME, RTL8169_VERSION);
7545 }
7546
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007547 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
7548 if (!dev)
7549 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007550
7551 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01007552 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007553 tp = netdev_priv(dev);
7554 tp->dev = dev;
7555 tp->pci_dev = pdev;
7556 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
7557
7558 mii = &tp->mii;
7559 mii->dev = dev;
7560 mii->mdio_read = rtl_mdio_read;
7561 mii->mdio_write = rtl_mdio_write;
7562 mii->phy_id_mask = 0x1f;
7563 mii->reg_num_mask = 0x1f;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007564 mii->supports_gmii = cfg->has_gmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007565
Francois Romieu3b6cf252012-03-08 09:59:04 +01007566 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007567 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007568 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007569 dev_err(&pdev->dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007570 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007571 }
7572
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007573 if (pcim_set_mwi(pdev) < 0)
Heiner Kallweit22148df2018-04-22 17:15:15 +02007574 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007575
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007576 /* use first MMIO region */
7577 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
7578 if (region < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007579 dev_err(&pdev->dev, "no MMIO resource found\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007580 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007581 }
7582
7583 /* check for weird/broken PCI region reporting */
7584 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007585 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007586 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007587 }
7588
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007589 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007590 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007591 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007592 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007593 }
7594
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007595 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01007596
7597 if (!pci_is_pcie(pdev))
Heiner Kallweit22148df2018-04-22 17:15:15 +02007598 dev_info(&pdev->dev, "not PCI Express\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007599
7600 /* Identify chip attached to board */
Heiner Kallweit22148df2018-04-22 17:15:15 +02007601 rtl8169_get_mac_version(tp, cfg->default_ver);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007602
Heiner Kallweite3972862018-06-29 08:07:04 +02007603 if (rtl_tbi_enabled(tp)) {
7604 dev_err(&pdev->dev, "TBI fiber mode not supported\n");
7605 return -ENODEV;
7606 }
7607
Heiner Kallweit0ae09742018-04-28 22:19:26 +02007608 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007609
7610 if ((sizeof(dma_addr_t) > 4) &&
7611 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
7612 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01007613 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
7614 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007615
7616 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
7617 if (!pci_is_pcie(pdev))
7618 tp->cp_cmd |= PCIDAC;
7619 dev->features |= NETIF_F_HIGHDMA;
7620 } else {
7621 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7622 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007623 dev_err(&pdev->dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007624 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007625 }
7626 }
7627
Francois Romieu3b6cf252012-03-08 09:59:04 +01007628 rtl_init_rxcfg(tp);
7629
7630 rtl_irq_disable(tp);
7631
Hayes Wangc5583862012-07-02 17:23:22 +08007632 rtl_hw_initialize(tp);
7633
Francois Romieu3b6cf252012-03-08 09:59:04 +01007634 rtl_hw_reset(tp);
7635
7636 rtl_ack_events(tp, 0xffff);
7637
7638 pci_set_master(pdev);
7639
Francois Romieu3b6cf252012-03-08 09:59:04 +01007640 rtl_init_mdio_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007641 rtl_init_jumbo_ops(tp);
7642
7643 rtl8169_print_mac_version(tp);
7644
7645 chipset = tp->mac_version;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007646
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007647 rc = rtl_alloc_irq(tp);
7648 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007649 dev_err(&pdev->dev, "Can't allocate interrupt\n");
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007650 return rc;
7651 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007652
Heiner Kallweit7edf6d32018-02-22 21:22:40 +01007653 /* override BIOS settings, use userspace tools to enable WOL */
7654 __rtl8169_set_wol(tp, 0);
7655
Francois Romieu3b6cf252012-03-08 09:59:04 +01007656 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05007657 u64_stats_init(&tp->rx_stats.syncp);
7658 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007659
7660 /* Get MAC address */
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007661 switch (tp->mac_version) {
Heiner Kallweit353af852018-05-02 21:39:59 +02007662 u8 mac_addr[ETH_ALEN] __aligned(4);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007663 case RTL_GIGA_MAC_VER_35 ... RTL_GIGA_MAC_VER_38:
7664 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08007665 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
Heiner Kallweit353af852018-05-02 21:39:59 +02007666 *(u16 *)&mac_addr[4] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007667
Heiner Kallweit353af852018-05-02 21:39:59 +02007668 if (is_valid_ether_addr(mac_addr))
7669 rtl_rar_set(tp, mac_addr);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007670 break;
7671 default:
7672 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007673 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007674 for (i = 0; i < ETH_ALEN; i++)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007675 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007676
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00007677 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007678 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007679
Heiner Kallweit37621492018-04-17 23:20:03 +02007680 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007681
7682 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7683 * properly for all devices */
7684 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00007685 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007686
7687 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00007688 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7689 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007690 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7691 NETIF_F_HIGHDMA;
7692
hayeswang929a0312014-09-16 11:40:47 +08007693 tp->cp_cmd |= RxChkSum | RxVlan;
7694
7695 /*
7696 * Pretend we are using VLANs; This bypasses a nasty bug where
7697 * Interrupts stop flowing on high load on 8110SCd controllers.
7698 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01007699 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08007700 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00007701 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007702
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007703 switch (rtl_chip_infos[chipset].txd_version) {
7704 case RTL_TD_0:
hayeswang5888d3f2014-07-11 16:25:56 +08007705 tp->tso_csum = rtl8169_tso_csum_v1;
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007706 break;
7707 case RTL_TD_1:
hayeswang5888d3f2014-07-11 16:25:56 +08007708 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08007709 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007710 break;
7711 default:
hayeswang5888d3f2014-07-11 16:25:56 +08007712 WARN_ON_ONCE(1);
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007713 }
hayeswang5888d3f2014-07-11 16:25:56 +08007714
Francois Romieu3b6cf252012-03-08 09:59:04 +01007715 dev->hw_features |= NETIF_F_RXALL;
7716 dev->hw_features |= NETIF_F_RXFCS;
7717
Jarod Wilsonc7315a92016-10-17 15:54:09 -04007718 /* MTU range: 60 - hw-specific max */
7719 dev->min_mtu = ETH_ZLEN;
7720 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
7721
Francois Romieu3b6cf252012-03-08 09:59:04 +01007722 tp->hw_start = cfg->hw_start;
7723 tp->event_slow = cfg->event_slow;
Francois Romieu50970832017-10-27 13:24:49 +03007724 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007725
Francois Romieu3b6cf252012-03-08 09:59:04 +01007726 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
7727
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007728 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
7729 &tp->counters_phys_addr,
7730 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007731 if (!tp->counters)
7732 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02007733
Heiner Kallweit19c9ea32018-03-26 19:19:30 +02007734 pci_set_drvdata(pdev, dev);
7735
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007736 rc = r8169_mdio_register(tp);
7737 if (rc)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007738 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007739
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007740 rc = register_netdev(dev);
7741 if (rc)
7742 goto err_mdio_unregister;
7743
Heiner Kallweit2d6c5a62018-04-17 23:31:21 +02007744 netif_info(tp, probe, dev, "%s, %pM, XID %08x, IRQ %d\n",
7745 rtl_chip_infos[chipset].name, dev->dev_addr,
Heiner Kallweit90b989c2018-04-17 23:32:15 +02007746 (u32)(RTL_R32(tp, TxConfig) & 0xfcf0f8ff),
Heiner Kallweit29274992018-02-28 20:43:38 +01007747 pci_irq_vector(pdev, 0));
Francois Romieu3b6cf252012-03-08 09:59:04 +01007748 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
7749 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
7750 "tx checksumming: %s]\n",
7751 rtl_chip_infos[chipset].jumbo_max,
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02007752 tp->mac_version <= RTL_GIGA_MAC_VER_06 ? "ok" : "ko");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007753 }
7754
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007755 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01007756 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007757
Heiner Kallweita92a0842018-01-08 21:39:13 +01007758 if (pci_dev_run_wake(pdev))
7759 pm_runtime_put_sync(&pdev->dev);
7760
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007761 return 0;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007762
7763err_mdio_unregister:
7764 mdiobus_unregister(tp->mii_bus);
7765 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007766}
7767
Linus Torvalds1da177e2005-04-16 15:20:36 -07007768static struct pci_driver rtl8169_pci_driver = {
7769 .name = MODULENAME,
7770 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01007771 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05007772 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02007773 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007774 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007775};
7776
Devendra Naga3eeb7da2012-10-26 09:27:42 +00007777module_pci_driver(rtl8169_pci_driver);