blob: 4aa419487ced1fb2d12a65c5b4eeccb262f4f1fd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
24#include <linux/init.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>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000029#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Francois Romieu99f252b2007-04-02 22:59:59 +020032#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
françois romieubca03d52011-01-03 15:07:31 +000040#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000042#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080044#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080045#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
46#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080047#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#ifdef RTL8169_DEBUG
50#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020051 if (!(expr)) { \
52 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070053 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020054 }
Joe Perches06fa7352007-10-18 21:15:00 +020055#define dprintk(fmt, args...) \
56 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#else
58#define assert(expr) do {} while (0)
59#define dprintk(fmt, args...) do {} while (0)
60#endif /* RTL8169_DEBUG */
61
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020062#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070063 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define TX_BUFFS_AVAIL(tp) \
66 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
69 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050070static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Francois Romieu9c14cea2008-07-05 00:21:15 +020072#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
75#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
76
77#define R8169_REGS_SIZE 256
78#define R8169_NAPI_WEIGHT 64
79#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
80#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
81#define RX_BUF_SIZE 1536 /* Rx Buffer size */
82#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
83#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
84
85#define RTL8169_TX_TIMEOUT (6*HZ)
86#define RTL8169_PHY_TIMEOUT (10*HZ)
87
françois romieuea8dbdd2009-03-15 01:10:50 +000088#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
89#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020090#define RTL_EEPROM_SIG_ADDR 0x0000
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* write/read MMIO register */
93#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
94#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
95#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
96#define RTL_R8(reg) readb (ioaddr + (reg))
97#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000098#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200101 RTL_GIGA_MAC_VER_01 = 0,
102 RTL_GIGA_MAC_VER_02,
103 RTL_GIGA_MAC_VER_03,
104 RTL_GIGA_MAC_VER_04,
105 RTL_GIGA_MAC_VER_05,
106 RTL_GIGA_MAC_VER_06,
107 RTL_GIGA_MAC_VER_07,
108 RTL_GIGA_MAC_VER_08,
109 RTL_GIGA_MAC_VER_09,
110 RTL_GIGA_MAC_VER_10,
111 RTL_GIGA_MAC_VER_11,
112 RTL_GIGA_MAC_VER_12,
113 RTL_GIGA_MAC_VER_13,
114 RTL_GIGA_MAC_VER_14,
115 RTL_GIGA_MAC_VER_15,
116 RTL_GIGA_MAC_VER_16,
117 RTL_GIGA_MAC_VER_17,
118 RTL_GIGA_MAC_VER_18,
119 RTL_GIGA_MAC_VER_19,
120 RTL_GIGA_MAC_VER_20,
121 RTL_GIGA_MAC_VER_21,
122 RTL_GIGA_MAC_VER_22,
123 RTL_GIGA_MAC_VER_23,
124 RTL_GIGA_MAC_VER_24,
125 RTL_GIGA_MAC_VER_25,
126 RTL_GIGA_MAC_VER_26,
127 RTL_GIGA_MAC_VER_27,
128 RTL_GIGA_MAC_VER_28,
129 RTL_GIGA_MAC_VER_29,
130 RTL_GIGA_MAC_VER_30,
131 RTL_GIGA_MAC_VER_31,
132 RTL_GIGA_MAC_VER_32,
133 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800134 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800135 RTL_GIGA_MAC_VER_35,
136 RTL_GIGA_MAC_VER_36,
Francois Romieu85bffe62011-04-27 08:22:39 +0200137 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
Francois Romieu2b7b4312011-04-18 22:53:24 -0700140enum rtl_tx_desc_version {
141 RTL_TD_0 = 0,
142 RTL_TD_1 = 1,
143};
144
Francois Romieud58d46b2011-05-03 16:38:29 +0200145#define JUMBO_1K ETH_DATA_LEN
146#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
147#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
148#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
149#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
150
151#define _R(NAME,TD,FW,SZ,B) { \
152 .name = NAME, \
153 .txd_version = TD, \
154 .fw_name = FW, \
155 .jumbo_max = SZ, \
156 .jumbo_tx_csum = B \
157}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800159static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700161 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200162 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200163 u16 jumbo_max;
164 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200165} rtl_chip_infos[] = {
166 /* PCI devices. */
167 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200168 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200169 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200170 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200171 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200172 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200173 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200174 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200175 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200176 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200177 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200178 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200179 /* PCI-E devices. */
180 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200181 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200182 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200183 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200184 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200185 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200186 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200187 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200188 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200189 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200190 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200191 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200192 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200193 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200194 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200195 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200196 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200197 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200198 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200199 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200200 [RTL_GIGA_MAC_VER_17] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200201 _R("RTL8168b/8111b", RTL_TD_1, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200202 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200203 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200204 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200205 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200206 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200207 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200208 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200209 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200210 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200211 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200212 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200213 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200214 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200215 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200216 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200217 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
218 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200219 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200220 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
221 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200222 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200223 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200224 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200225 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200226 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200227 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
228 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200229 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200230 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
231 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200232 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200233 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200234 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200235 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
236 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200237 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200238 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
239 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800240 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200241 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
242 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800243 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200244 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
245 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800246 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200247 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
248 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249};
250#undef _R
251
Francois Romieubcf0bf92006-07-26 23:14:13 +0200252enum cfg_version {
253 RTL_CFG_0 = 0x00,
254 RTL_CFG_1,
255 RTL_CFG_2
256};
257
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000258static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200259 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200260 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200261 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100262 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200263 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
264 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000265 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200266 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200267 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
268 { PCI_VENDOR_ID_LINKSYS, 0x1032,
269 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100270 { 0x0001, 0x8168,
271 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 {0,},
273};
274
275MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
276
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000277static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700278static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200279static struct {
280 u32 msg_enable;
281} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Francois Romieu07d3f512007-02-21 22:40:46 +0100283enum rtl_registers {
284 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100285 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100286 MAR0 = 8, /* Multicast filter. */
287 CounterAddrLow = 0x10,
288 CounterAddrHigh = 0x14,
289 TxDescStartAddrLow = 0x20,
290 TxDescStartAddrHigh = 0x24,
291 TxHDescStartAddrLow = 0x28,
292 TxHDescStartAddrHigh = 0x2c,
293 FLASH = 0x30,
294 ERSR = 0x36,
295 ChipCmd = 0x37,
296 TxPoll = 0x38,
297 IntrMask = 0x3c,
298 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700299
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800300 TxConfig = 0x40,
301#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
302#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
303
304 RxConfig = 0x44,
305#define RX128_INT_EN (1 << 15) /* 8111c and later */
306#define RX_MULTI_EN (1 << 14) /* 8111c only */
307#define RXCFG_FIFO_SHIFT 13
308 /* No threshold before first PCI xfer */
309#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
310#define RXCFG_DMA_SHIFT 8
311 /* Unlimited maximum PCI burst. */
312#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700313
Francois Romieu07d3f512007-02-21 22:40:46 +0100314 RxMissed = 0x4c,
315 Cfg9346 = 0x50,
316 Config0 = 0x51,
317 Config1 = 0x52,
318 Config2 = 0x53,
319 Config3 = 0x54,
320 Config4 = 0x55,
321 Config5 = 0x56,
322 MultiIntr = 0x5c,
323 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100324 PHYstatus = 0x6c,
325 RxMaxSize = 0xda,
326 CPlusCmd = 0xe0,
327 IntrMitigate = 0xe2,
328 RxDescAddrLow = 0xe4,
329 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000330 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
331
332#define NoEarlyTx 0x3f /* Max value : no early transmit. */
333
334 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
335
336#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800337#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000338
Francois Romieu07d3f512007-02-21 22:40:46 +0100339 FuncEvent = 0xf0,
340 FuncEventMask = 0xf4,
341 FuncPresetState = 0xf8,
342 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343};
344
Francois Romieuf162a5d2008-06-01 22:37:49 +0200345enum rtl8110_registers {
346 TBICSR = 0x64,
347 TBI_ANAR = 0x68,
348 TBI_LPAR = 0x6a,
349};
350
351enum rtl8168_8101_registers {
352 CSIDR = 0x64,
353 CSIAR = 0x68,
354#define CSIAR_FLAG 0x80000000
355#define CSIAR_WRITE_CMD 0x80000000
356#define CSIAR_BYTE_ENABLE 0x0f
357#define CSIAR_BYTE_ENABLE_SHIFT 12
358#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000359 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200360 EPHYAR = 0x80,
361#define EPHYAR_FLAG 0x80000000
362#define EPHYAR_WRITE_CMD 0x80000000
363#define EPHYAR_REG_MASK 0x1f
364#define EPHYAR_REG_SHIFT 16
365#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800366 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800367#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200368 DBG_REG = 0xd1,
369#define FIX_NAK_1 (1 << 4)
370#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800371 TWSI = 0xd2,
372 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800373#define NOW_IS_OOB (1 << 7)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800374#define EN_NDP (1 << 3)
375#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000376 EFUSEAR = 0xdc,
377#define EFUSEAR_FLAG 0x80000000
378#define EFUSEAR_WRITE_CMD 0x80000000
379#define EFUSEAR_READ_CMD 0x00000000
380#define EFUSEAR_REG_MASK 0x03ff
381#define EFUSEAR_REG_SHIFT 8
382#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200383};
384
françois romieuc0e45c12011-01-03 15:08:04 +0000385enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800386 LED_FREQ = 0x1a,
387 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000388 ERIDR = 0x70,
389 ERIAR = 0x74,
390#define ERIAR_FLAG 0x80000000
391#define ERIAR_WRITE_CMD 0x80000000
392#define ERIAR_READ_CMD 0x00000000
393#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000394#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800395#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
396#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
397#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
398#define ERIAR_MASK_SHIFT 12
399#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
400#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
401#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000402 EPHY_RXER_NUM = 0x7c,
403 OCPDR = 0xb0, /* OCP GPHY access */
404#define OCPDR_WRITE_CMD 0x80000000
405#define OCPDR_READ_CMD 0x00000000
406#define OCPDR_REG_MASK 0x7f
407#define OCPDR_GPHY_REG_SHIFT 16
408#define OCPDR_DATA_MASK 0xffff
409 OCPAR = 0xb4,
410#define OCPAR_FLAG 0x80000000
411#define OCPAR_GPHY_WRITE_CMD 0x8000f060
412#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000413 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
414 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200415#define TXPLA_RST (1 << 29)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800416#define PWM_EN (1 << 22)
françois romieuc0e45c12011-01-03 15:08:04 +0000417};
418
Francois Romieu07d3f512007-02-21 22:40:46 +0100419enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100421 SYSErr = 0x8000,
422 PCSTimeout = 0x4000,
423 SWInt = 0x0100,
424 TxDescUnavail = 0x0080,
425 RxFIFOOver = 0x0040,
426 LinkChg = 0x0020,
427 RxOverflow = 0x0010,
428 TxErr = 0x0008,
429 TxOK = 0x0004,
430 RxErr = 0x0002,
431 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400434 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200435 RxFOVF = (1 << 23),
436 RxRWT = (1 << 22),
437 RxRES = (1 << 21),
438 RxRUNT = (1 << 20),
439 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800442 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100443 CmdReset = 0x10,
444 CmdRxEnb = 0x08,
445 CmdTxEnb = 0x04,
446 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Francois Romieu275391a2007-02-23 23:50:28 +0100448 /* TXPoll register p.5 */
449 HPQ = 0x80, /* Poll cmd on the high prio queue */
450 NPQ = 0x40, /* Poll cmd on the low prio queue */
451 FSWInt = 0x01, /* Forced software interrupt */
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100454 Cfg9346_Lock = 0x00,
455 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100458 AcceptErr = 0x20,
459 AcceptRunt = 0x10,
460 AcceptBroadcast = 0x08,
461 AcceptMulticast = 0x04,
462 AcceptMyPhys = 0x02,
463 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200464#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /* TxConfigBits */
467 TxInterFrameGapShift = 24,
468 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
469
Francois Romieu5d06a992006-02-23 00:47:58 +0100470 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200471 LEDS1 = (1 << 7),
472 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200473 Speed_down = (1 << 4),
474 MEMMAP = (1 << 3),
475 IOMAP = (1 << 2),
476 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100477 PMEnable = (1 << 0), /* Power Management Enable */
478
Francois Romieu6dccd162007-02-13 23:38:05 +0100479 /* Config2 register p. 25 */
françois romieu2ca6cf02011-12-15 08:37:43 +0000480 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100481 PCI_Clock_66MHz = 0x01,
482 PCI_Clock_33MHz = 0x00,
483
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100484 /* Config3 register p.25 */
485 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
486 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200487 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200488 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100489
Francois Romieud58d46b2011-05-03 16:38:29 +0200490 /* Config4 register */
491 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
492
Francois Romieu5d06a992006-02-23 00:47:58 +0100493 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100494 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
495 MWF = (1 << 5), /* Accept Multicast wakeup frame */
496 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200497 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100498 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100499 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /* TBICSR p.28 */
502 TBIReset = 0x80000000,
503 TBILoopback = 0x40000000,
504 TBINwEnable = 0x20000000,
505 TBINwRestart = 0x10000000,
506 TBILinkOk = 0x02000000,
507 TBINwComplete = 0x01000000,
508
509 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200510 EnableBist = (1 << 15), // 8168 8101
511 Mac_dbgo_oe = (1 << 14), // 8168 8101
512 Normal_mode = (1 << 13), // unused
513 Force_half_dup = (1 << 12), // 8168 8101
514 Force_rxflow_en = (1 << 11), // 8168 8101
515 Force_txflow_en = (1 << 10), // 8168 8101
516 Cxpl_dbg_sel = (1 << 9), // 8168 8101
517 ASF = (1 << 8), // 8168 8101
518 PktCntrDisable = (1 << 7), // 8168 8101
519 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 RxVlan = (1 << 6),
521 RxChkSum = (1 << 5),
522 PCIDAC = (1 << 4),
523 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100524 INTT_0 = 0x0000, // 8168
525 INTT_1 = 0x0001, // 8168
526 INTT_2 = 0x0002, // 8168
527 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100530 TBI_Enable = 0x80,
531 TxFlowCtrl = 0x40,
532 RxFlowCtrl = 0x20,
533 _1000bpsF = 0x10,
534 _100bps = 0x08,
535 _10bps = 0x04,
536 LinkStatus = 0x02,
537 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100540 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200541
542 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100543 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544};
545
Francois Romieu2b7b4312011-04-18 22:53:24 -0700546enum rtl_desc_bit {
547 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
549 RingEnd = (1 << 30), /* End of descriptor ring */
550 FirstFrag = (1 << 29), /* First segment of a packet */
551 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700552};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Francois Romieu2b7b4312011-04-18 22:53:24 -0700554/* Generic case. */
555enum rtl_tx_desc_bit {
556 /* First doubleword. */
557 TD_LSO = (1 << 27), /* Large Send Offload */
558#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Francois Romieu2b7b4312011-04-18 22:53:24 -0700560 /* Second doubleword. */
561 TxVlanTag = (1 << 17), /* Add VLAN tag */
562};
563
564/* 8169, 8168b and 810x except 8102e. */
565enum rtl_tx_desc_bit_0 {
566 /* First doubleword. */
567#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
568 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
569 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
570 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
571};
572
573/* 8102e, 8168c and beyond. */
574enum rtl_tx_desc_bit_1 {
575 /* Second doubleword. */
576#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
577 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
578 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
579 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
580};
581
582static const struct rtl_tx_desc_info {
583 struct {
584 u32 udp;
585 u32 tcp;
586 } checksum;
587 u16 mss_shift;
588 u16 opts_offset;
589} tx_desc_info [] = {
590 [RTL_TD_0] = {
591 .checksum = {
592 .udp = TD0_IP_CS | TD0_UDP_CS,
593 .tcp = TD0_IP_CS | TD0_TCP_CS
594 },
595 .mss_shift = TD0_MSS_SHIFT,
596 .opts_offset = 0
597 },
598 [RTL_TD_1] = {
599 .checksum = {
600 .udp = TD1_IP_CS | TD1_UDP_CS,
601 .tcp = TD1_IP_CS | TD1_TCP_CS
602 },
603 .mss_shift = TD1_MSS_SHIFT,
604 .opts_offset = 1
605 }
606};
607
608enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* Rx private */
610 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
611 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
612
613#define RxProtoUDP (PID1)
614#define RxProtoTCP (PID0)
615#define RxProtoIP (PID1 | PID0)
616#define RxProtoMask RxProtoIP
617
618 IPFail = (1 << 16), /* IP checksum failed */
619 UDPFail = (1 << 15), /* UDP/IP checksum failed */
620 TCPFail = (1 << 14), /* TCP/IP checksum failed */
621 RxVlanTag = (1 << 16), /* VLAN tag available */
622};
623
624#define RsvdMask 0x3fffc000
625
626struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200627 __le32 opts1;
628 __le32 opts2;
629 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630};
631
632struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200633 __le32 opts1;
634 __le32 opts2;
635 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636};
637
638struct ring_info {
639 struct sk_buff *skb;
640 u32 len;
641 u8 __pad[sizeof(void *) - sizeof(u32)];
642};
643
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200644enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200645 RTL_FEATURE_WOL = (1 << 0),
646 RTL_FEATURE_MSI = (1 << 1),
647 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200648};
649
Ivan Vecera355423d2009-02-06 21:49:57 -0800650struct rtl8169_counters {
651 __le64 tx_packets;
652 __le64 rx_packets;
653 __le64 tx_errors;
654 __le32 rx_errors;
655 __le16 rx_missed;
656 __le16 align_errors;
657 __le32 tx_one_collision;
658 __le32 tx_multi_collision;
659 __le64 rx_unicast;
660 __le64 rx_broadcast;
661 __le32 rx_multicast;
662 __le16 tx_aborted;
663 __le16 tx_underun;
664};
665
Francois Romieuda78dbf2012-01-26 14:18:23 +0100666enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100667 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100668 RTL_FLAG_TASK_SLOW_PENDING,
669 RTL_FLAG_TASK_RESET_PENDING,
670 RTL_FLAG_TASK_PHY_PENDING,
671 RTL_FLAG_MAX
672};
673
Junchang Wang8027aa22012-03-04 23:30:32 +0100674struct rtl8169_stats {
675 u64 packets;
676 u64 bytes;
677 struct u64_stats_sync syncp;
678};
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680struct rtl8169_private {
681 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200682 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000683 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700684 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200685 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700686 u16 txd_version;
687 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
689 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
690 u32 dirty_rx;
691 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100692 struct rtl8169_stats rx_stats;
693 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
695 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
696 dma_addr_t TxPhyAddr;
697 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000698 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct timer_list timer;
701 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100702
703 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000704
705 struct mdio_ops {
706 void (*write)(void __iomem *, int, int);
707 int (*read)(void __iomem *, int);
708 } mdio_ops;
709
françois romieu065c27c2011-01-03 15:08:12 +0000710 struct pll_power_ops {
711 void (*down)(struct rtl8169_private *);
712 void (*up)(struct rtl8169_private *);
713 } pll_power_ops;
714
Francois Romieud58d46b2011-05-03 16:38:29 +0200715 struct jumbo_ops {
716 void (*enable)(struct rtl8169_private *);
717 void (*disable)(struct rtl8169_private *);
718 } jumbo_ops;
719
Oliver Neukum54405cd2011-01-06 21:55:13 +0100720 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200721 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000722 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100723 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000724 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800726 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100727
728 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100729 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
730 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100731 struct work_struct work;
732 } wk;
733
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200734 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200735
736 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800737 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000738 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400739 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000740
Francois Romieub6ffd972011-06-17 17:00:05 +0200741 struct rtl_fw {
742 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200743
744#define RTL_VER_SIZE 32
745
746 char version[RTL_VER_SIZE];
747
748 struct rtl_fw_phy_action {
749 __le32 *code;
750 size_t size;
751 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200752 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300753#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754};
755
Ralf Baechle979b6c12005-06-13 14:30:40 -0700756MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700759MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200760module_param_named(debug, debug.msg_enable, int, 0);
761MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762MODULE_LICENSE("GPL");
763MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000764MODULE_FIRMWARE(FIRMWARE_8168D_1);
765MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000766MODULE_FIRMWARE(FIRMWARE_8168E_1);
767MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400768MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800769MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800770MODULE_FIRMWARE(FIRMWARE_8168F_1);
771MODULE_FIRMWARE(FIRMWARE_8168F_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Francois Romieuda78dbf2012-01-26 14:18:23 +0100773static void rtl_lock_work(struct rtl8169_private *tp)
774{
775 mutex_lock(&tp->wk.mutex);
776}
777
778static void rtl_unlock_work(struct rtl8169_private *tp)
779{
780 mutex_unlock(&tp->wk.mutex);
781}
782
Francois Romieud58d46b2011-05-03 16:38:29 +0200783static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
784{
785 int cap = pci_pcie_cap(pdev);
786
787 if (cap) {
788 u16 ctl;
789
790 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
791 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
792 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
793 }
794}
795
françois romieub646d902011-01-03 15:08:21 +0000796static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
797{
798 void __iomem *ioaddr = tp->mmio_addr;
799 int i;
800
801 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
802 for (i = 0; i < 20; i++) {
803 udelay(100);
804 if (RTL_R32(OCPAR) & OCPAR_FLAG)
805 break;
806 }
807 return RTL_R32(OCPDR);
808}
809
810static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
811{
812 void __iomem *ioaddr = tp->mmio_addr;
813 int i;
814
815 RTL_W32(OCPDR, data);
816 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
817 for (i = 0; i < 20; i++) {
818 udelay(100);
819 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
820 break;
821 }
822}
823
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800824static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000825{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800826 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000827 int i;
828
829 RTL_W8(ERIDR, cmd);
830 RTL_W32(ERIAR, 0x800010e8);
831 msleep(2);
832 for (i = 0; i < 5; i++) {
833 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200834 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000835 break;
836 }
837
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800838 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000839}
840
841#define OOB_CMD_RESET 0x00
842#define OOB_CMD_DRIVER_START 0x05
843#define OOB_CMD_DRIVER_STOP 0x06
844
Francois Romieucecb5fd2011-04-01 10:21:07 +0200845static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
846{
847 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
848}
849
françois romieub646d902011-01-03 15:08:21 +0000850static void rtl8168_driver_start(struct rtl8169_private *tp)
851{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200852 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000853 int i;
854
855 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
856
Francois Romieucecb5fd2011-04-01 10:21:07 +0200857 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000858
françois romieub646d902011-01-03 15:08:21 +0000859 for (i = 0; i < 10; i++) {
860 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000861 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000862 break;
863 }
864}
865
866static void rtl8168_driver_stop(struct rtl8169_private *tp)
867{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200868 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000869 int i;
870
871 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
872
Francois Romieucecb5fd2011-04-01 10:21:07 +0200873 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000874
françois romieub646d902011-01-03 15:08:21 +0000875 for (i = 0; i < 10; i++) {
876 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000877 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000878 break;
879 }
880}
881
hayeswang4804b3b2011-03-21 01:50:29 +0000882static int r8168dp_check_dash(struct rtl8169_private *tp)
883{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200884 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000885
Francois Romieucecb5fd2011-04-01 10:21:07 +0200886 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000887}
françois romieub646d902011-01-03 15:08:21 +0000888
françois romieu4da19632011-01-03 15:07:55 +0000889static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 int i;
892
Francois Romieua6baf3a2007-11-08 23:23:21 +0100893 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Francois Romieu23714082006-01-29 00:49:09 +0100895 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100896 /*
897 * Check if the RTL8169 has completed writing to the specified
898 * MII register.
899 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200900 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 break;
Francois Romieu23714082006-01-29 00:49:09 +0100902 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700904 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700905 * According to hardware specs a 20us delay is required after write
906 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700907 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700908 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
françois romieu4da19632011-01-03 15:07:55 +0000911static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
913 int i, value = -1;
914
Francois Romieua6baf3a2007-11-08 23:23:21 +0100915 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Francois Romieu23714082006-01-29 00:49:09 +0100917 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100918 /*
919 * Check if the RTL8169 has completed retrieving data from
920 * the specified MII register.
921 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100923 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 break;
925 }
Francois Romieu23714082006-01-29 00:49:09 +0100926 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700928 /*
929 * According to hardware specs a 20us delay is required after read
930 * complete indication, but before sending next command.
931 */
932 udelay(20);
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return value;
935}
936
françois romieuc0e45c12011-01-03 15:08:04 +0000937static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
938{
939 int i;
940
941 RTL_W32(OCPDR, data |
942 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
943 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
944 RTL_W32(EPHY_RXER_NUM, 0);
945
946 for (i = 0; i < 100; i++) {
947 mdelay(1);
948 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
949 break;
950 }
951}
952
953static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
954{
955 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
956 (value & OCPDR_DATA_MASK));
957}
958
959static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
960{
961 int i;
962
963 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
964
965 mdelay(1);
966 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
967 RTL_W32(EPHY_RXER_NUM, 0);
968
969 for (i = 0; i < 100; i++) {
970 mdelay(1);
971 if (RTL_R32(OCPAR) & OCPAR_FLAG)
972 break;
973 }
974
975 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
976}
977
françois romieue6de30d2011-01-03 15:08:37 +0000978#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
979
980static void r8168dp_2_mdio_start(void __iomem *ioaddr)
981{
982 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
983}
984
985static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
986{
987 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
988}
989
990static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
991{
992 r8168dp_2_mdio_start(ioaddr);
993
994 r8169_mdio_write(ioaddr, reg_addr, value);
995
996 r8168dp_2_mdio_stop(ioaddr);
997}
998
999static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
1000{
1001 int value;
1002
1003 r8168dp_2_mdio_start(ioaddr);
1004
1005 value = r8169_mdio_read(ioaddr, reg_addr);
1006
1007 r8168dp_2_mdio_stop(ioaddr);
1008
1009 return value;
1010}
1011
françois romieu4da19632011-01-03 15:07:55 +00001012static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001013{
françois romieuc0e45c12011-01-03 15:08:04 +00001014 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001015}
1016
françois romieu4da19632011-01-03 15:07:55 +00001017static int rtl_readphy(struct rtl8169_private *tp, int location)
1018{
françois romieuc0e45c12011-01-03 15:08:04 +00001019 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +00001020}
1021
1022static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1023{
1024 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1025}
1026
1027static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001028{
1029 int val;
1030
françois romieu4da19632011-01-03 15:07:55 +00001031 val = rtl_readphy(tp, reg_addr);
1032 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001033}
1034
Francois Romieuccdffb92008-07-26 14:26:06 +02001035static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1036 int val)
1037{
1038 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001039
françois romieu4da19632011-01-03 15:07:55 +00001040 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001041}
1042
1043static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1044{
1045 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001046
françois romieu4da19632011-01-03 15:07:55 +00001047 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001048}
1049
Francois Romieudacf8152008-08-02 20:44:13 +02001050static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1051{
1052 unsigned int i;
1053
1054 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1055 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1056
1057 for (i = 0; i < 100; i++) {
1058 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1059 break;
1060 udelay(10);
1061 }
1062}
1063
1064static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1065{
1066 u16 value = 0xffff;
1067 unsigned int i;
1068
1069 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1070
1071 for (i = 0; i < 100; i++) {
1072 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1073 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1074 break;
1075 }
1076 udelay(10);
1077 }
1078
1079 return value;
1080}
1081
1082static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1083{
1084 unsigned int i;
1085
1086 RTL_W32(CSIDR, value);
1087 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1088 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1089
1090 for (i = 0; i < 100; i++) {
1091 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1092 break;
1093 udelay(10);
1094 }
1095}
1096
1097static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1098{
1099 u32 value = ~0x00;
1100 unsigned int i;
1101
1102 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1103 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1104
1105 for (i = 0; i < 100; i++) {
1106 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1107 value = RTL_R32(CSIDR);
1108 break;
1109 }
1110 udelay(10);
1111 }
1112
1113 return value;
1114}
1115
Hayes Wang133ac402011-07-06 15:58:05 +08001116static
1117void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1118{
1119 unsigned int i;
1120
1121 BUG_ON((addr & 3) || (mask == 0));
1122 RTL_W32(ERIDR, val);
1123 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1124
1125 for (i = 0; i < 100; i++) {
1126 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1127 break;
1128 udelay(100);
1129 }
1130}
1131
1132static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1133{
1134 u32 value = ~0x00;
1135 unsigned int i;
1136
1137 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1138
1139 for (i = 0; i < 100; i++) {
1140 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1141 value = RTL_R32(ERIDR);
1142 break;
1143 }
1144 udelay(100);
1145 }
1146
1147 return value;
1148}
1149
1150static void
1151rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1152{
1153 u32 val;
1154
1155 val = rtl_eri_read(ioaddr, addr, type);
1156 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1157}
1158
françois romieuc28aa382011-08-02 03:53:43 +00001159struct exgmac_reg {
1160 u16 addr;
1161 u16 mask;
1162 u32 val;
1163};
1164
1165static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1166 const struct exgmac_reg *r, int len)
1167{
1168 while (len-- > 0) {
1169 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1170 r++;
1171 }
1172}
1173
françois romieudaf9df62009-10-07 12:44:20 +00001174static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1175{
1176 u8 value = 0xff;
1177 unsigned int i;
1178
1179 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1180
1181 for (i = 0; i < 300; i++) {
1182 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1183 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1184 break;
1185 }
1186 udelay(100);
1187 }
1188
1189 return value;
1190}
1191
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001192static u16 rtl_get_events(struct rtl8169_private *tp)
1193{
1194 void __iomem *ioaddr = tp->mmio_addr;
1195
1196 return RTL_R16(IntrStatus);
1197}
1198
1199static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1200{
1201 void __iomem *ioaddr = tp->mmio_addr;
1202
1203 RTL_W16(IntrStatus, bits);
1204 mmiowb();
1205}
1206
1207static void rtl_irq_disable(struct rtl8169_private *tp)
1208{
1209 void __iomem *ioaddr = tp->mmio_addr;
1210
1211 RTL_W16(IntrMask, 0);
1212 mmiowb();
1213}
1214
Francois Romieu3e990ff2012-01-26 12:50:01 +01001215static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1216{
1217 void __iomem *ioaddr = tp->mmio_addr;
1218
1219 RTL_W16(IntrMask, bits);
1220}
1221
Francois Romieuda78dbf2012-01-26 14:18:23 +01001222#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1223#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1224#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1225
1226static void rtl_irq_enable_all(struct rtl8169_private *tp)
1227{
1228 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1229}
1230
françois romieu811fd302011-12-04 20:30:45 +00001231static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
françois romieu811fd302011-12-04 20:30:45 +00001233 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001235 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001236 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001237 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
françois romieu4da19632011-01-03 15:07:55 +00001240static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
françois romieu4da19632011-01-03 15:07:55 +00001242 void __iomem *ioaddr = tp->mmio_addr;
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return RTL_R32(TBICSR) & TBIReset;
1245}
1246
françois romieu4da19632011-01-03 15:07:55 +00001247static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
françois romieu4da19632011-01-03 15:07:55 +00001249 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
1252static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1253{
1254 return RTL_R32(TBICSR) & TBILinkOk;
1255}
1256
1257static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1258{
1259 return RTL_R8(PHYstatus) & LinkStatus;
1260}
1261
françois romieu4da19632011-01-03 15:07:55 +00001262static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
françois romieu4da19632011-01-03 15:07:55 +00001264 void __iomem *ioaddr = tp->mmio_addr;
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1267}
1268
françois romieu4da19632011-01-03 15:07:55 +00001269static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271 unsigned int val;
1272
françois romieu4da19632011-01-03 15:07:55 +00001273 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1274 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Hayes Wang70090422011-07-06 15:58:06 +08001277static void rtl_link_chg_patch(struct rtl8169_private *tp)
1278{
1279 void __iomem *ioaddr = tp->mmio_addr;
1280 struct net_device *dev = tp->dev;
1281
1282 if (!netif_running(dev))
1283 return;
1284
1285 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1286 if (RTL_R8(PHYstatus) & _1000bpsF) {
1287 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1288 0x00000011, ERIAR_EXGMAC);
1289 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1290 0x00000005, ERIAR_EXGMAC);
1291 } else if (RTL_R8(PHYstatus) & _100bps) {
1292 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1293 0x0000001f, ERIAR_EXGMAC);
1294 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1295 0x00000005, ERIAR_EXGMAC);
1296 } else {
1297 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1298 0x0000001f, ERIAR_EXGMAC);
1299 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1300 0x0000003f, ERIAR_EXGMAC);
1301 }
1302 /* Reset packet filter */
1303 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1304 ERIAR_EXGMAC);
1305 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1306 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001307 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1308 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1309 if (RTL_R8(PHYstatus) & _1000bpsF) {
1310 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1311 0x00000011, ERIAR_EXGMAC);
1312 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1313 0x00000005, ERIAR_EXGMAC);
1314 } else {
1315 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1316 0x0000001f, ERIAR_EXGMAC);
1317 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1318 0x0000003f, ERIAR_EXGMAC);
1319 }
Hayes Wang70090422011-07-06 15:58:06 +08001320 }
1321}
1322
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001323static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001324 struct rtl8169_private *tp,
1325 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001328 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001329 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001330 if (pm)
1331 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001333 if (net_ratelimit())
1334 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001335 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001337 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001338 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001339 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001343static void rtl8169_check_link_status(struct net_device *dev,
1344 struct rtl8169_private *tp,
1345 void __iomem *ioaddr)
1346{
1347 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1348}
1349
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001350#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1351
1352static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1353{
1354 void __iomem *ioaddr = tp->mmio_addr;
1355 u8 options;
1356 u32 wolopts = 0;
1357
1358 options = RTL_R8(Config1);
1359 if (!(options & PMEnable))
1360 return 0;
1361
1362 options = RTL_R8(Config3);
1363 if (options & LinkUp)
1364 wolopts |= WAKE_PHY;
1365 if (options & MagicPacket)
1366 wolopts |= WAKE_MAGIC;
1367
1368 options = RTL_R8(Config5);
1369 if (options & UWF)
1370 wolopts |= WAKE_UCAST;
1371 if (options & BWF)
1372 wolopts |= WAKE_BCAST;
1373 if (options & MWF)
1374 wolopts |= WAKE_MCAST;
1375
1376 return wolopts;
1377}
1378
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001379static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1380{
1381 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001382
Francois Romieuda78dbf2012-01-26 14:18:23 +01001383 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001384
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001385 wol->supported = WAKE_ANY;
1386 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001387
Francois Romieuda78dbf2012-01-26 14:18:23 +01001388 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001389}
1390
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001391static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001392{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001393 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001394 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001395 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001396 u32 opt;
1397 u16 reg;
1398 u8 mask;
1399 } cfg[] = {
1400 { WAKE_ANY, Config1, PMEnable },
1401 { WAKE_PHY, Config3, LinkUp },
1402 { WAKE_MAGIC, Config3, MagicPacket },
1403 { WAKE_UCAST, Config5, UWF },
1404 { WAKE_BCAST, Config5, BWF },
1405 { WAKE_MCAST, Config5, MWF },
1406 { WAKE_ANY, Config5, LanWake }
1407 };
1408
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001409 RTL_W8(Cfg9346, Cfg9346_Unlock);
1410
1411 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1412 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001413 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001414 options |= cfg[i].mask;
1415 RTL_W8(cfg[i].reg, options);
1416 }
1417
1418 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001419}
1420
1421static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1422{
1423 struct rtl8169_private *tp = netdev_priv(dev);
1424
Francois Romieuda78dbf2012-01-26 14:18:23 +01001425 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001426
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001427 if (wol->wolopts)
1428 tp->features |= RTL_FEATURE_WOL;
1429 else
1430 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001431 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001432
1433 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001434
françois romieuea809072010-11-08 13:23:58 +00001435 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1436
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001437 return 0;
1438}
1439
Francois Romieu31bd2042011-04-26 18:58:59 +02001440static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1441{
Francois Romieu85bffe62011-04-27 08:22:39 +02001442 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001443}
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445static void rtl8169_get_drvinfo(struct net_device *dev,
1446 struct ethtool_drvinfo *info)
1447{
1448 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001449 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Rick Jones68aad782011-11-07 13:29:27 +00001451 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1452 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1453 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001454 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001455 if (!IS_ERR_OR_NULL(rtl_fw))
1456 strlcpy(info->fw_version, rtl_fw->version,
1457 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
1460static int rtl8169_get_regs_len(struct net_device *dev)
1461{
1462 return R8169_REGS_SIZE;
1463}
1464
1465static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001466 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 struct rtl8169_private *tp = netdev_priv(dev);
1469 void __iomem *ioaddr = tp->mmio_addr;
1470 int ret = 0;
1471 u32 reg;
1472
1473 reg = RTL_R32(TBICSR);
1474 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1475 (duplex == DUPLEX_FULL)) {
1476 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1477 } else if (autoneg == AUTONEG_ENABLE)
1478 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1479 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001480 netif_warn(tp, link, dev,
1481 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 ret = -EOPNOTSUPP;
1483 }
1484
1485 return ret;
1486}
1487
1488static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001489 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001492 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001493 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Hayes Wang716b50a2011-02-22 17:26:18 +08001495 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001498 int auto_nego;
1499
françois romieu4da19632011-01-03 15:07:55 +00001500 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001501 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1502 ADVERTISE_100HALF | ADVERTISE_100FULL);
1503
1504 if (adv & ADVERTISED_10baseT_Half)
1505 auto_nego |= ADVERTISE_10HALF;
1506 if (adv & ADVERTISED_10baseT_Full)
1507 auto_nego |= ADVERTISE_10FULL;
1508 if (adv & ADVERTISED_100baseT_Half)
1509 auto_nego |= ADVERTISE_100HALF;
1510 if (adv & ADVERTISED_100baseT_Full)
1511 auto_nego |= ADVERTISE_100FULL;
1512
françois romieu3577aa12009-05-19 10:46:48 +00001513 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1514
françois romieu4da19632011-01-03 15:07:55 +00001515 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001516 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1517
1518 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001519 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001520 if (adv & ADVERTISED_1000baseT_Half)
1521 giga_ctrl |= ADVERTISE_1000HALF;
1522 if (adv & ADVERTISED_1000baseT_Full)
1523 giga_ctrl |= ADVERTISE_1000FULL;
1524 } else if (adv & (ADVERTISED_1000baseT_Half |
1525 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001526 netif_info(tp, link, dev,
1527 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001528 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
françois romieu3577aa12009-05-19 10:46:48 +00001531 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001532
françois romieu4da19632011-01-03 15:07:55 +00001533 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1534 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001535 } else {
1536 giga_ctrl = 0;
1537
1538 if (speed == SPEED_10)
1539 bmcr = 0;
1540 else if (speed == SPEED_100)
1541 bmcr = BMCR_SPEED100;
1542 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001543 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001544
1545 if (duplex == DUPLEX_FULL)
1546 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001547 }
1548
françois romieu4da19632011-01-03 15:07:55 +00001549 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001550
Francois Romieucecb5fd2011-04-01 10:21:07 +02001551 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1552 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001553 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001554 rtl_writephy(tp, 0x17, 0x2138);
1555 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001556 } else {
françois romieu4da19632011-01-03 15:07:55 +00001557 rtl_writephy(tp, 0x17, 0x2108);
1558 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001559 }
1560 }
1561
Oliver Neukum54405cd2011-01-06 21:55:13 +01001562 rc = 0;
1563out:
1564 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
1567static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001568 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 struct rtl8169_private *tp = netdev_priv(dev);
1571 int ret;
1572
Oliver Neukum54405cd2011-01-06 21:55:13 +01001573 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001574 if (ret < 0)
1575 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Francois Romieu4876cc12011-03-11 21:07:11 +01001577 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1578 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001580 }
1581out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return ret;
1583}
1584
1585static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1586{
1587 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 int ret;
1589
Francois Romieu4876cc12011-03-11 21:07:11 +01001590 del_timer_sync(&tp->timer);
1591
Francois Romieuda78dbf2012-01-26 14:18:23 +01001592 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001593 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001594 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001595 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 return ret;
1598}
1599
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001600static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1601 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Francois Romieud58d46b2011-05-03 16:38:29 +02001603 struct rtl8169_private *tp = netdev_priv(dev);
1604
Francois Romieu2b7b4312011-04-18 22:53:24 -07001605 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001606 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Francois Romieud58d46b2011-05-03 16:38:29 +02001608 if (dev->mtu > JUMBO_1K &&
1609 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1610 features &= ~NETIF_F_IP_CSUM;
1611
Michał Mirosław350fb322011-04-08 06:35:56 +00001612 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
Francois Romieuda78dbf2012-01-26 14:18:23 +01001615static void __rtl8169_set_features(struct net_device *dev,
1616 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
1618 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001619 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001620 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Ben Greear6bbe0212012-02-10 15:04:33 +00001622 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1623 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Ben Greear6bbe0212012-02-10 15:04:33 +00001625 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1626 if (features & NETIF_F_RXCSUM)
1627 tp->cp_cmd |= RxChkSum;
1628 else
1629 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001630
Ben Greear6bbe0212012-02-10 15:04:33 +00001631 if (dev->features & NETIF_F_HW_VLAN_RX)
1632 tp->cp_cmd |= RxVlan;
1633 else
1634 tp->cp_cmd &= ~RxVlan;
1635
1636 RTL_W16(CPlusCmd, tp->cp_cmd);
1637 RTL_R16(CPlusCmd);
1638 }
1639 if (changed & NETIF_F_RXALL) {
1640 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1641 if (features & NETIF_F_RXALL)
1642 tmp |= (AcceptErr | AcceptRunt);
1643 RTL_W32(RxConfig, tmp);
1644 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001645}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Francois Romieuda78dbf2012-01-26 14:18:23 +01001647static int rtl8169_set_features(struct net_device *dev,
1648 netdev_features_t features)
1649{
1650 struct rtl8169_private *tp = netdev_priv(dev);
1651
1652 rtl_lock_work(tp);
1653 __rtl8169_set_features(dev, features);
1654 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 return 0;
1657}
1658
Francois Romieuda78dbf2012-01-26 14:18:23 +01001659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1661 struct sk_buff *skb)
1662{
Jesse Grosseab6d182010-10-20 13:56:03 +00001663 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1665}
1666
Francois Romieu7a8fc772011-03-01 17:18:33 +01001667static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
1669 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Francois Romieu7a8fc772011-03-01 17:18:33 +01001671 if (opts2 & RxVlanTag)
1672 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
Francois Romieuccdffb92008-07-26 14:26:06 +02001677static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 struct rtl8169_private *tp = netdev_priv(dev);
1680 void __iomem *ioaddr = tp->mmio_addr;
1681 u32 status;
1682
1683 cmd->supported =
1684 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1685 cmd->port = PORT_FIBRE;
1686 cmd->transceiver = XCVR_INTERNAL;
1687
1688 status = RTL_R32(TBICSR);
1689 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1690 cmd->autoneg = !!(status & TBINwEnable);
1691
David Decotigny70739492011-04-27 18:32:40 +00001692 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001694
1695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
Francois Romieuccdffb92008-07-26 14:26:06 +02001698static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
1700 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Francois Romieuccdffb92008-07-26 14:26:06 +02001702 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
1705static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1706{
1707 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001708 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Francois Romieuda78dbf2012-01-26 14:18:23 +01001710 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001711 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001712 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Francois Romieuccdffb92008-07-26 14:26:06 +02001714 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
1717static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1718 void *p)
1719{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001720 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Francois Romieu5b0384f2006-08-16 16:00:01 +02001722 if (regs->len > R8169_REGS_SIZE)
1723 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Francois Romieuda78dbf2012-01-26 14:18:23 +01001725 rtl_lock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001726 memcpy_fromio(p, tp->mmio_addr, regs->len);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001727 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001730static u32 rtl8169_get_msglevel(struct net_device *dev)
1731{
1732 struct rtl8169_private *tp = netdev_priv(dev);
1733
1734 return tp->msg_enable;
1735}
1736
1737static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1738{
1739 struct rtl8169_private *tp = netdev_priv(dev);
1740
1741 tp->msg_enable = value;
1742}
1743
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001744static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1745 "tx_packets",
1746 "rx_packets",
1747 "tx_errors",
1748 "rx_errors",
1749 "rx_missed",
1750 "align_errors",
1751 "tx_single_collisions",
1752 "tx_multi_collisions",
1753 "unicast",
1754 "broadcast",
1755 "multicast",
1756 "tx_aborted",
1757 "tx_underrun",
1758};
1759
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001760static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001761{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001762 switch (sset) {
1763 case ETH_SS_STATS:
1764 return ARRAY_SIZE(rtl8169_gstrings);
1765 default:
1766 return -EOPNOTSUPP;
1767 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001768}
1769
Ivan Vecera355423d2009-02-06 21:49:57 -08001770static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001771{
1772 struct rtl8169_private *tp = netdev_priv(dev);
1773 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001774 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001775 struct rtl8169_counters *counters;
1776 dma_addr_t paddr;
1777 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001778 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001779
Ivan Vecera355423d2009-02-06 21:49:57 -08001780 /*
1781 * Some chips are unable to dump tally counters when the receiver
1782 * is disabled.
1783 */
1784 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1785 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001786
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001787 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001788 if (!counters)
1789 return;
1790
1791 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001792 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001793 RTL_W32(CounterAddrLow, cmd);
1794 RTL_W32(CounterAddrLow, cmd | CounterDump);
1795
Ivan Vecera355423d2009-02-06 21:49:57 -08001796 while (wait--) {
1797 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001798 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001799 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001800 }
1801 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001802 }
1803
1804 RTL_W32(CounterAddrLow, 0);
1805 RTL_W32(CounterAddrHigh, 0);
1806
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001807 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001808}
1809
Ivan Vecera355423d2009-02-06 21:49:57 -08001810static void rtl8169_get_ethtool_stats(struct net_device *dev,
1811 struct ethtool_stats *stats, u64 *data)
1812{
1813 struct rtl8169_private *tp = netdev_priv(dev);
1814
1815 ASSERT_RTNL();
1816
1817 rtl8169_update_counters(dev);
1818
1819 data[0] = le64_to_cpu(tp->counters.tx_packets);
1820 data[1] = le64_to_cpu(tp->counters.rx_packets);
1821 data[2] = le64_to_cpu(tp->counters.tx_errors);
1822 data[3] = le32_to_cpu(tp->counters.rx_errors);
1823 data[4] = le16_to_cpu(tp->counters.rx_missed);
1824 data[5] = le16_to_cpu(tp->counters.align_errors);
1825 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1826 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1827 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1828 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1829 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1830 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1831 data[12] = le16_to_cpu(tp->counters.tx_underun);
1832}
1833
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001834static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1835{
1836 switch(stringset) {
1837 case ETH_SS_STATS:
1838 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1839 break;
1840 }
1841}
1842
Jeff Garzik7282d492006-09-13 14:30:00 -04001843static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 .get_drvinfo = rtl8169_get_drvinfo,
1845 .get_regs_len = rtl8169_get_regs_len,
1846 .get_link = ethtool_op_get_link,
1847 .get_settings = rtl8169_get_settings,
1848 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001849 .get_msglevel = rtl8169_get_msglevel,
1850 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001852 .get_wol = rtl8169_get_wol,
1853 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001854 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001855 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001856 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857};
1858
Francois Romieu07d3f512007-02-21 22:40:46 +01001859static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001860 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Francois Romieu5d320a22011-05-08 17:47:36 +02001862 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001863 /*
1864 * The driver currently handles the 8168Bf and the 8168Be identically
1865 * but they can be identified more specifically through the test below
1866 * if needed:
1867 *
1868 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001869 *
1870 * Same thing for the 8101Eb and the 8101Ec:
1871 *
1872 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001873 */
Francois Romieu37441002011-06-17 22:58:54 +02001874 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001876 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 int mac_version;
1878 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001879 /* 8168F family. */
1880 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1881 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1882
hayeswang01dc7fe2011-03-21 01:50:28 +00001883 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001884 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001885 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1886 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1887 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1888
Francois Romieu5b538df2008-07-20 16:22:45 +02001889 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001890 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1891 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001892 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001893
françois romieue6de30d2011-01-03 15:08:37 +00001894 /* 8168DP family. */
1895 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1896 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001897 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001898
Francois Romieuef808d52008-06-29 13:10:54 +02001899 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001900 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001901 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001902 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001903 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001904 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1905 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001906 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001907 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001908 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001909
1910 /* 8168B family. */
1911 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1912 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1913 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1914 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1915
1916 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001917 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001918 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1919 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1920 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001921 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1922 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1923 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1924 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1925 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1926 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001927 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001928 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001929 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001930 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1931 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001932 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1933 /* FIXME: where did these entries come from ? -- FR */
1934 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1935 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1936
1937 /* 8110 family. */
1938 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1939 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1940 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1941 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1942 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1943 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1944
Jean Delvaref21b75e2009-05-26 20:54:48 -07001945 /* Catch-all */
1946 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001947 };
1948 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 u32 reg;
1950
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001951 reg = RTL_R32(TxConfig);
1952 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 p++;
1954 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001955
1956 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1957 netif_notice(tp, probe, dev,
1958 "unknown MAC, using family default\n");
1959 tp->mac_version = default_version;
1960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
1963static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1964{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001965 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Francois Romieu867763c2007-08-17 18:21:58 +02001968struct phy_reg {
1969 u16 reg;
1970 u16 val;
1971};
1972
françois romieu4da19632011-01-03 15:07:55 +00001973static void rtl_writephy_batch(struct rtl8169_private *tp,
1974 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001975{
1976 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001977 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001978 regs++;
1979 }
1980}
1981
françois romieubca03d52011-01-03 15:07:31 +00001982#define PHY_READ 0x00000000
1983#define PHY_DATA_OR 0x10000000
1984#define PHY_DATA_AND 0x20000000
1985#define PHY_BJMPN 0x30000000
1986#define PHY_READ_EFUSE 0x40000000
1987#define PHY_READ_MAC_BYTE 0x50000000
1988#define PHY_WRITE_MAC_BYTE 0x60000000
1989#define PHY_CLEAR_READCOUNT 0x70000000
1990#define PHY_WRITE 0x80000000
1991#define PHY_READCOUNT_EQ_SKIP 0x90000000
1992#define PHY_COMP_EQ_SKIPN 0xa0000000
1993#define PHY_COMP_NEQ_SKIPN 0xb0000000
1994#define PHY_WRITE_PREVIOUS 0xc0000000
1995#define PHY_SKIPN 0xd0000000
1996#define PHY_DELAY_MS 0xe0000000
1997#define PHY_WRITE_ERI_WORD 0xf0000000
1998
Hayes Wang960aee62011-06-18 11:37:48 +02001999struct fw_info {
2000 u32 magic;
2001 char version[RTL_VER_SIZE];
2002 __le32 fw_start;
2003 __le32 fw_len;
2004 u8 chksum;
2005} __packed;
2006
Francois Romieu1c361ef2011-06-17 17:16:24 +02002007#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2008
2009static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002010{
Francois Romieub6ffd972011-06-17 17:00:05 +02002011 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002012 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002013 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2014 char *version = rtl_fw->version;
2015 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002016
Francois Romieu1c361ef2011-06-17 17:16:24 +02002017 if (fw->size < FW_OPCODE_SIZE)
2018 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002019
2020 if (!fw_info->magic) {
2021 size_t i, size, start;
2022 u8 checksum = 0;
2023
2024 if (fw->size < sizeof(*fw_info))
2025 goto out;
2026
2027 for (i = 0; i < fw->size; i++)
2028 checksum += fw->data[i];
2029 if (checksum != 0)
2030 goto out;
2031
2032 start = le32_to_cpu(fw_info->fw_start);
2033 if (start > fw->size)
2034 goto out;
2035
2036 size = le32_to_cpu(fw_info->fw_len);
2037 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2038 goto out;
2039
2040 memcpy(version, fw_info->version, RTL_VER_SIZE);
2041
2042 pa->code = (__le32 *)(fw->data + start);
2043 pa->size = size;
2044 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002045 if (fw->size % FW_OPCODE_SIZE)
2046 goto out;
2047
2048 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2049
2050 pa->code = (__le32 *)fw->data;
2051 pa->size = fw->size / FW_OPCODE_SIZE;
2052 }
2053 version[RTL_VER_SIZE - 1] = 0;
2054
2055 rc = true;
2056out:
2057 return rc;
2058}
2059
Francois Romieufd112f22011-06-18 00:10:29 +02002060static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2061 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002062{
Francois Romieufd112f22011-06-18 00:10:29 +02002063 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002064 size_t index;
2065
Francois Romieu1c361ef2011-06-17 17:16:24 +02002066 for (index = 0; index < pa->size; index++) {
2067 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002068 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002069
hayeswang42b82dc2011-01-10 02:07:25 +00002070 switch(action & 0xf0000000) {
2071 case PHY_READ:
2072 case PHY_DATA_OR:
2073 case PHY_DATA_AND:
2074 case PHY_READ_EFUSE:
2075 case PHY_CLEAR_READCOUNT:
2076 case PHY_WRITE:
2077 case PHY_WRITE_PREVIOUS:
2078 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002079 break;
2080
hayeswang42b82dc2011-01-10 02:07:25 +00002081 case PHY_BJMPN:
2082 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002083 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002084 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002085 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002086 }
2087 break;
2088 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002089 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002090 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002091 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002092 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002093 }
2094 break;
2095 case PHY_COMP_EQ_SKIPN:
2096 case PHY_COMP_NEQ_SKIPN:
2097 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002098 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002099 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002100 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002101 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002102 }
2103 break;
2104
2105 case PHY_READ_MAC_BYTE:
2106 case PHY_WRITE_MAC_BYTE:
2107 case PHY_WRITE_ERI_WORD:
2108 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002109 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002110 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002111 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002112 }
2113 }
Francois Romieufd112f22011-06-18 00:10:29 +02002114 rc = true;
2115out:
2116 return rc;
2117}
françois romieubca03d52011-01-03 15:07:31 +00002118
Francois Romieufd112f22011-06-18 00:10:29 +02002119static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2120{
2121 struct net_device *dev = tp->dev;
2122 int rc = -EINVAL;
2123
2124 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2125 netif_err(tp, ifup, dev, "invalid firwmare\n");
2126 goto out;
2127 }
2128
2129 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2130 rc = 0;
2131out:
2132 return rc;
2133}
2134
2135static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2136{
2137 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2138 u32 predata, count;
2139 size_t index;
2140
2141 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002142
Francois Romieu1c361ef2011-06-17 17:16:24 +02002143 for (index = 0; index < pa->size; ) {
2144 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002145 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002146 u32 regno = (action & 0x0fff0000) >> 16;
2147
2148 if (!action)
2149 break;
françois romieubca03d52011-01-03 15:07:31 +00002150
2151 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002152 case PHY_READ:
2153 predata = rtl_readphy(tp, regno);
2154 count++;
2155 index++;
françois romieubca03d52011-01-03 15:07:31 +00002156 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002157 case PHY_DATA_OR:
2158 predata |= data;
2159 index++;
2160 break;
2161 case PHY_DATA_AND:
2162 predata &= data;
2163 index++;
2164 break;
2165 case PHY_BJMPN:
2166 index -= regno;
2167 break;
2168 case PHY_READ_EFUSE:
2169 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2170 index++;
2171 break;
2172 case PHY_CLEAR_READCOUNT:
2173 count = 0;
2174 index++;
2175 break;
2176 case PHY_WRITE:
2177 rtl_writephy(tp, regno, data);
2178 index++;
2179 break;
2180 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002181 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002182 break;
2183 case PHY_COMP_EQ_SKIPN:
2184 if (predata == data)
2185 index += regno;
2186 index++;
2187 break;
2188 case PHY_COMP_NEQ_SKIPN:
2189 if (predata != data)
2190 index += regno;
2191 index++;
2192 break;
2193 case PHY_WRITE_PREVIOUS:
2194 rtl_writephy(tp, regno, predata);
2195 index++;
2196 break;
2197 case PHY_SKIPN:
2198 index += regno + 1;
2199 break;
2200 case PHY_DELAY_MS:
2201 mdelay(data);
2202 index++;
2203 break;
2204
2205 case PHY_READ_MAC_BYTE:
2206 case PHY_WRITE_MAC_BYTE:
2207 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002208 default:
2209 BUG();
2210 }
2211 }
2212}
2213
françois romieuf1e02ed2011-01-13 13:07:53 +00002214static void rtl_release_firmware(struct rtl8169_private *tp)
2215{
Francois Romieub6ffd972011-06-17 17:00:05 +02002216 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2217 release_firmware(tp->rtl_fw->fw);
2218 kfree(tp->rtl_fw);
2219 }
2220 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002221}
2222
François Romieu953a12c2011-04-24 17:38:48 +02002223static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002224{
Francois Romieub6ffd972011-06-17 17:00:05 +02002225 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002226
2227 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002228 if (!IS_ERR_OR_NULL(rtl_fw))
2229 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002230}
2231
2232static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2233{
2234 if (rtl_readphy(tp, reg) != val)
2235 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2236 else
2237 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002238}
2239
françois romieu4da19632011-01-03 15:07:55 +00002240static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002242 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002243 { 0x1f, 0x0001 },
2244 { 0x06, 0x006e },
2245 { 0x08, 0x0708 },
2246 { 0x15, 0x4000 },
2247 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
françois romieu0b9b5712009-08-10 19:44:56 +00002249 { 0x1f, 0x0001 },
2250 { 0x03, 0x00a1 },
2251 { 0x02, 0x0008 },
2252 { 0x01, 0x0120 },
2253 { 0x00, 0x1000 },
2254 { 0x04, 0x0800 },
2255 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
françois romieu0b9b5712009-08-10 19:44:56 +00002257 { 0x03, 0xff41 },
2258 { 0x02, 0xdf60 },
2259 { 0x01, 0x0140 },
2260 { 0x00, 0x0077 },
2261 { 0x04, 0x7800 },
2262 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
françois romieu0b9b5712009-08-10 19:44:56 +00002264 { 0x03, 0x802f },
2265 { 0x02, 0x4f02 },
2266 { 0x01, 0x0409 },
2267 { 0x00, 0xf0f9 },
2268 { 0x04, 0x9800 },
2269 { 0x04, 0x9000 },
2270
2271 { 0x03, 0xdf01 },
2272 { 0x02, 0xdf20 },
2273 { 0x01, 0xff95 },
2274 { 0x00, 0xba00 },
2275 { 0x04, 0xa800 },
2276 { 0x04, 0xa000 },
2277
2278 { 0x03, 0xff41 },
2279 { 0x02, 0xdf20 },
2280 { 0x01, 0x0140 },
2281 { 0x00, 0x00bb },
2282 { 0x04, 0xb800 },
2283 { 0x04, 0xb000 },
2284
2285 { 0x03, 0xdf41 },
2286 { 0x02, 0xdc60 },
2287 { 0x01, 0x6340 },
2288 { 0x00, 0x007d },
2289 { 0x04, 0xd800 },
2290 { 0x04, 0xd000 },
2291
2292 { 0x03, 0xdf01 },
2293 { 0x02, 0xdf20 },
2294 { 0x01, 0x100a },
2295 { 0x00, 0xa0ff },
2296 { 0x04, 0xf800 },
2297 { 0x04, 0xf000 },
2298
2299 { 0x1f, 0x0000 },
2300 { 0x0b, 0x0000 },
2301 { 0x00, 0x9200 }
2302 };
2303
françois romieu4da19632011-01-03 15:07:55 +00002304 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
2306
françois romieu4da19632011-01-03 15:07:55 +00002307static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002308{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002309 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002310 { 0x1f, 0x0002 },
2311 { 0x01, 0x90d0 },
2312 { 0x1f, 0x0000 }
2313 };
2314
françois romieu4da19632011-01-03 15:07:55 +00002315 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002316}
2317
françois romieu4da19632011-01-03 15:07:55 +00002318static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002319{
2320 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002321
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002322 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2323 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002324 return;
2325
françois romieu4da19632011-01-03 15:07:55 +00002326 rtl_writephy(tp, 0x1f, 0x0001);
2327 rtl_writephy(tp, 0x10, 0xf01b);
2328 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002329}
2330
françois romieu4da19632011-01-03 15:07:55 +00002331static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002332{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002333 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002334 { 0x1f, 0x0001 },
2335 { 0x04, 0x0000 },
2336 { 0x03, 0x00a1 },
2337 { 0x02, 0x0008 },
2338 { 0x01, 0x0120 },
2339 { 0x00, 0x1000 },
2340 { 0x04, 0x0800 },
2341 { 0x04, 0x9000 },
2342 { 0x03, 0x802f },
2343 { 0x02, 0x4f02 },
2344 { 0x01, 0x0409 },
2345 { 0x00, 0xf099 },
2346 { 0x04, 0x9800 },
2347 { 0x04, 0xa000 },
2348 { 0x03, 0xdf01 },
2349 { 0x02, 0xdf20 },
2350 { 0x01, 0xff95 },
2351 { 0x00, 0xba00 },
2352 { 0x04, 0xa800 },
2353 { 0x04, 0xf000 },
2354 { 0x03, 0xdf01 },
2355 { 0x02, 0xdf20 },
2356 { 0x01, 0x101a },
2357 { 0x00, 0xa0ff },
2358 { 0x04, 0xf800 },
2359 { 0x04, 0x0000 },
2360 { 0x1f, 0x0000 },
2361
2362 { 0x1f, 0x0001 },
2363 { 0x10, 0xf41b },
2364 { 0x14, 0xfb54 },
2365 { 0x18, 0xf5c7 },
2366 { 0x1f, 0x0000 },
2367
2368 { 0x1f, 0x0001 },
2369 { 0x17, 0x0cc0 },
2370 { 0x1f, 0x0000 }
2371 };
2372
françois romieu4da19632011-01-03 15:07:55 +00002373 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002374
françois romieu4da19632011-01-03 15:07:55 +00002375 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002376}
2377
françois romieu4da19632011-01-03 15:07:55 +00002378static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002379{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002380 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002381 { 0x1f, 0x0001 },
2382 { 0x04, 0x0000 },
2383 { 0x03, 0x00a1 },
2384 { 0x02, 0x0008 },
2385 { 0x01, 0x0120 },
2386 { 0x00, 0x1000 },
2387 { 0x04, 0x0800 },
2388 { 0x04, 0x9000 },
2389 { 0x03, 0x802f },
2390 { 0x02, 0x4f02 },
2391 { 0x01, 0x0409 },
2392 { 0x00, 0xf099 },
2393 { 0x04, 0x9800 },
2394 { 0x04, 0xa000 },
2395 { 0x03, 0xdf01 },
2396 { 0x02, 0xdf20 },
2397 { 0x01, 0xff95 },
2398 { 0x00, 0xba00 },
2399 { 0x04, 0xa800 },
2400 { 0x04, 0xf000 },
2401 { 0x03, 0xdf01 },
2402 { 0x02, 0xdf20 },
2403 { 0x01, 0x101a },
2404 { 0x00, 0xa0ff },
2405 { 0x04, 0xf800 },
2406 { 0x04, 0x0000 },
2407 { 0x1f, 0x0000 },
2408
2409 { 0x1f, 0x0001 },
2410 { 0x0b, 0x8480 },
2411 { 0x1f, 0x0000 },
2412
2413 { 0x1f, 0x0001 },
2414 { 0x18, 0x67c7 },
2415 { 0x04, 0x2000 },
2416 { 0x03, 0x002f },
2417 { 0x02, 0x4360 },
2418 { 0x01, 0x0109 },
2419 { 0x00, 0x3022 },
2420 { 0x04, 0x2800 },
2421 { 0x1f, 0x0000 },
2422
2423 { 0x1f, 0x0001 },
2424 { 0x17, 0x0cc0 },
2425 { 0x1f, 0x0000 }
2426 };
2427
françois romieu4da19632011-01-03 15:07:55 +00002428 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002429}
2430
françois romieu4da19632011-01-03 15:07:55 +00002431static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002432{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002433 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002434 { 0x10, 0xf41b },
2435 { 0x1f, 0x0000 }
2436 };
2437
françois romieu4da19632011-01-03 15:07:55 +00002438 rtl_writephy(tp, 0x1f, 0x0001);
2439 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002440
françois romieu4da19632011-01-03 15:07:55 +00002441 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002442}
2443
françois romieu4da19632011-01-03 15:07:55 +00002444static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002445{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002446 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002447 { 0x1f, 0x0001 },
2448 { 0x10, 0xf41b },
2449 { 0x1f, 0x0000 }
2450 };
2451
françois romieu4da19632011-01-03 15:07:55 +00002452 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002453}
2454
françois romieu4da19632011-01-03 15:07:55 +00002455static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002456{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002457 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002458 { 0x1f, 0x0000 },
2459 { 0x1d, 0x0f00 },
2460 { 0x1f, 0x0002 },
2461 { 0x0c, 0x1ec8 },
2462 { 0x1f, 0x0000 }
2463 };
2464
françois romieu4da19632011-01-03 15:07:55 +00002465 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002466}
2467
françois romieu4da19632011-01-03 15:07:55 +00002468static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002469{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002470 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002471 { 0x1f, 0x0001 },
2472 { 0x1d, 0x3d98 },
2473 { 0x1f, 0x0000 }
2474 };
2475
françois romieu4da19632011-01-03 15:07:55 +00002476 rtl_writephy(tp, 0x1f, 0x0000);
2477 rtl_patchphy(tp, 0x14, 1 << 5);
2478 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002479
françois romieu4da19632011-01-03 15:07:55 +00002480 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002481}
2482
françois romieu4da19632011-01-03 15:07:55 +00002483static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002484{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002485 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002486 { 0x1f, 0x0001 },
2487 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002488 { 0x1f, 0x0002 },
2489 { 0x00, 0x88d4 },
2490 { 0x01, 0x82b1 },
2491 { 0x03, 0x7002 },
2492 { 0x08, 0x9e30 },
2493 { 0x09, 0x01f0 },
2494 { 0x0a, 0x5500 },
2495 { 0x0c, 0x00c8 },
2496 { 0x1f, 0x0003 },
2497 { 0x12, 0xc096 },
2498 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002499 { 0x1f, 0x0000 },
2500 { 0x1f, 0x0000 },
2501 { 0x09, 0x2000 },
2502 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002503 };
2504
françois romieu4da19632011-01-03 15:07:55 +00002505 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002506
françois romieu4da19632011-01-03 15:07:55 +00002507 rtl_patchphy(tp, 0x14, 1 << 5);
2508 rtl_patchphy(tp, 0x0d, 1 << 5);
2509 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002510}
2511
françois romieu4da19632011-01-03 15:07:55 +00002512static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002513{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002514 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002515 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002516 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002517 { 0x03, 0x802f },
2518 { 0x02, 0x4f02 },
2519 { 0x01, 0x0409 },
2520 { 0x00, 0xf099 },
2521 { 0x04, 0x9800 },
2522 { 0x04, 0x9000 },
2523 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002524 { 0x1f, 0x0002 },
2525 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002526 { 0x06, 0x0761 },
2527 { 0x1f, 0x0003 },
2528 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002529 { 0x1f, 0x0000 }
2530 };
2531
françois romieu4da19632011-01-03 15:07:55 +00002532 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002533
françois romieu4da19632011-01-03 15:07:55 +00002534 rtl_patchphy(tp, 0x16, 1 << 0);
2535 rtl_patchphy(tp, 0x14, 1 << 5);
2536 rtl_patchphy(tp, 0x0d, 1 << 5);
2537 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002538}
2539
françois romieu4da19632011-01-03 15:07:55 +00002540static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002541{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002542 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002543 { 0x1f, 0x0001 },
2544 { 0x12, 0x2300 },
2545 { 0x1d, 0x3d98 },
2546 { 0x1f, 0x0002 },
2547 { 0x0c, 0x7eb8 },
2548 { 0x06, 0x5461 },
2549 { 0x1f, 0x0003 },
2550 { 0x16, 0x0f0a },
2551 { 0x1f, 0x0000 }
2552 };
2553
françois romieu4da19632011-01-03 15:07:55 +00002554 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002555
françois romieu4da19632011-01-03 15:07:55 +00002556 rtl_patchphy(tp, 0x16, 1 << 0);
2557 rtl_patchphy(tp, 0x14, 1 << 5);
2558 rtl_patchphy(tp, 0x0d, 1 << 5);
2559 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002560}
2561
françois romieu4da19632011-01-03 15:07:55 +00002562static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002563{
françois romieu4da19632011-01-03 15:07:55 +00002564 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002565}
2566
françois romieubca03d52011-01-03 15:07:31 +00002567static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002568{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002569 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002570 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002571 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002572 { 0x06, 0x4064 },
2573 { 0x07, 0x2863 },
2574 { 0x08, 0x059c },
2575 { 0x09, 0x26b4 },
2576 { 0x0a, 0x6a19 },
2577 { 0x0b, 0xdcc8 },
2578 { 0x10, 0xf06d },
2579 { 0x14, 0x7f68 },
2580 { 0x18, 0x7fd9 },
2581 { 0x1c, 0xf0ff },
2582 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002583 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002584 { 0x12, 0xf49f },
2585 { 0x13, 0x070b },
2586 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002587 { 0x14, 0x94c0 },
2588
2589 /*
2590 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002591 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002592 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002593 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002594 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002595 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002596 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002597 { 0x06, 0x5561 },
2598
2599 /*
2600 * Can not link to 1Gbps with bad cable
2601 * Decrease SNR threshold form 21.07dB to 19.04dB
2602 */
2603 { 0x1f, 0x0001 },
2604 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002605
2606 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002607 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002608 };
françois romieubca03d52011-01-03 15:07:31 +00002609 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002610
françois romieu4da19632011-01-03 15:07:55 +00002611 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002612
françois romieubca03d52011-01-03 15:07:31 +00002613 /*
2614 * Rx Error Issue
2615 * Fine Tune Switching regulator parameter
2616 */
françois romieu4da19632011-01-03 15:07:55 +00002617 rtl_writephy(tp, 0x1f, 0x0002);
2618 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2619 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002620
françois romieudaf9df62009-10-07 12:44:20 +00002621 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002622 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002623 { 0x1f, 0x0002 },
2624 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002625 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002626 { 0x05, 0x8330 },
2627 { 0x06, 0x669a },
2628 { 0x1f, 0x0002 }
2629 };
2630 int val;
2631
françois romieu4da19632011-01-03 15:07:55 +00002632 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002633
françois romieu4da19632011-01-03 15:07:55 +00002634 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002635
2636 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002637 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002638 0x0065, 0x0066, 0x0067, 0x0068,
2639 0x0069, 0x006a, 0x006b, 0x006c
2640 };
2641 int i;
2642
françois romieu4da19632011-01-03 15:07:55 +00002643 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002644
2645 val &= 0xff00;
2646 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002647 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002648 }
2649 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002650 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002651 { 0x1f, 0x0002 },
2652 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002653 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002654 { 0x05, 0x8330 },
2655 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002656 };
2657
françois romieu4da19632011-01-03 15:07:55 +00002658 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002659 }
2660
françois romieubca03d52011-01-03 15:07:31 +00002661 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002662 rtl_writephy(tp, 0x1f, 0x0002);
2663 rtl_patchphy(tp, 0x0d, 0x0300);
2664 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002665
françois romieubca03d52011-01-03 15:07:31 +00002666 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002667 rtl_writephy(tp, 0x1f, 0x0002);
2668 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2669 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002670
françois romieu4da19632011-01-03 15:07:55 +00002671 rtl_writephy(tp, 0x1f, 0x0005);
2672 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002673
2674 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002675
françois romieu4da19632011-01-03 15:07:55 +00002676 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002677}
2678
françois romieubca03d52011-01-03 15:07:31 +00002679static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002680{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002681 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002682 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002683 { 0x1f, 0x0001 },
2684 { 0x06, 0x4064 },
2685 { 0x07, 0x2863 },
2686 { 0x08, 0x059c },
2687 { 0x09, 0x26b4 },
2688 { 0x0a, 0x6a19 },
2689 { 0x0b, 0xdcc8 },
2690 { 0x10, 0xf06d },
2691 { 0x14, 0x7f68 },
2692 { 0x18, 0x7fd9 },
2693 { 0x1c, 0xf0ff },
2694 { 0x1d, 0x3d9c },
2695 { 0x1f, 0x0003 },
2696 { 0x12, 0xf49f },
2697 { 0x13, 0x070b },
2698 { 0x1a, 0x05ad },
2699 { 0x14, 0x94c0 },
2700
françois romieubca03d52011-01-03 15:07:31 +00002701 /*
2702 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002703 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002704 */
françois romieudaf9df62009-10-07 12:44:20 +00002705 { 0x1f, 0x0002 },
2706 { 0x06, 0x5561 },
2707 { 0x1f, 0x0005 },
2708 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002709 { 0x06, 0x5561 },
2710
2711 /*
2712 * Can not link to 1Gbps with bad cable
2713 * Decrease SNR threshold form 21.07dB to 19.04dB
2714 */
2715 { 0x1f, 0x0001 },
2716 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002717
2718 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002719 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002720 };
françois romieubca03d52011-01-03 15:07:31 +00002721 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002722
françois romieu4da19632011-01-03 15:07:55 +00002723 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002724
2725 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002726 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002727 { 0x1f, 0x0002 },
2728 { 0x05, 0x669a },
2729 { 0x1f, 0x0005 },
2730 { 0x05, 0x8330 },
2731 { 0x06, 0x669a },
2732
2733 { 0x1f, 0x0002 }
2734 };
2735 int val;
2736
françois romieu4da19632011-01-03 15:07:55 +00002737 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002738
françois romieu4da19632011-01-03 15:07:55 +00002739 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002740 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002741 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002742 0x0065, 0x0066, 0x0067, 0x0068,
2743 0x0069, 0x006a, 0x006b, 0x006c
2744 };
2745 int i;
2746
françois romieu4da19632011-01-03 15:07:55 +00002747 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002748
2749 val &= 0xff00;
2750 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002751 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002752 }
2753 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002754 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002755 { 0x1f, 0x0002 },
2756 { 0x05, 0x2642 },
2757 { 0x1f, 0x0005 },
2758 { 0x05, 0x8330 },
2759 { 0x06, 0x2642 }
2760 };
2761
françois romieu4da19632011-01-03 15:07:55 +00002762 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002763 }
2764
françois romieubca03d52011-01-03 15:07:31 +00002765 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002766 rtl_writephy(tp, 0x1f, 0x0002);
2767 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2768 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002769
françois romieubca03d52011-01-03 15:07:31 +00002770 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002771 rtl_writephy(tp, 0x1f, 0x0002);
2772 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002773
françois romieu4da19632011-01-03 15:07:55 +00002774 rtl_writephy(tp, 0x1f, 0x0005);
2775 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002776
2777 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002778
françois romieu4da19632011-01-03 15:07:55 +00002779 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002780}
2781
françois romieu4da19632011-01-03 15:07:55 +00002782static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002783{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002784 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002785 { 0x1f, 0x0002 },
2786 { 0x10, 0x0008 },
2787 { 0x0d, 0x006c },
2788
2789 { 0x1f, 0x0000 },
2790 { 0x0d, 0xf880 },
2791
2792 { 0x1f, 0x0001 },
2793 { 0x17, 0x0cc0 },
2794
2795 { 0x1f, 0x0001 },
2796 { 0x0b, 0xa4d8 },
2797 { 0x09, 0x281c },
2798 { 0x07, 0x2883 },
2799 { 0x0a, 0x6b35 },
2800 { 0x1d, 0x3da4 },
2801 { 0x1c, 0xeffd },
2802 { 0x14, 0x7f52 },
2803 { 0x18, 0x7fc6 },
2804 { 0x08, 0x0601 },
2805 { 0x06, 0x4063 },
2806 { 0x10, 0xf074 },
2807 { 0x1f, 0x0003 },
2808 { 0x13, 0x0789 },
2809 { 0x12, 0xf4bd },
2810 { 0x1a, 0x04fd },
2811 { 0x14, 0x84b0 },
2812 { 0x1f, 0x0000 },
2813 { 0x00, 0x9200 },
2814
2815 { 0x1f, 0x0005 },
2816 { 0x01, 0x0340 },
2817 { 0x1f, 0x0001 },
2818 { 0x04, 0x4000 },
2819 { 0x03, 0x1d21 },
2820 { 0x02, 0x0c32 },
2821 { 0x01, 0x0200 },
2822 { 0x00, 0x5554 },
2823 { 0x04, 0x4800 },
2824 { 0x04, 0x4000 },
2825 { 0x04, 0xf000 },
2826 { 0x03, 0xdf01 },
2827 { 0x02, 0xdf20 },
2828 { 0x01, 0x101a },
2829 { 0x00, 0xa0ff },
2830 { 0x04, 0xf800 },
2831 { 0x04, 0xf000 },
2832 { 0x1f, 0x0000 },
2833
2834 { 0x1f, 0x0007 },
2835 { 0x1e, 0x0023 },
2836 { 0x16, 0x0000 },
2837 { 0x1f, 0x0000 }
2838 };
2839
françois romieu4da19632011-01-03 15:07:55 +00002840 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002841}
2842
françois romieue6de30d2011-01-03 15:08:37 +00002843static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2844{
2845 static const struct phy_reg phy_reg_init[] = {
2846 { 0x1f, 0x0001 },
2847 { 0x17, 0x0cc0 },
2848
2849 { 0x1f, 0x0007 },
2850 { 0x1e, 0x002d },
2851 { 0x18, 0x0040 },
2852 { 0x1f, 0x0000 }
2853 };
2854
2855 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2856 rtl_patchphy(tp, 0x0d, 1 << 5);
2857}
2858
Hayes Wang70090422011-07-06 15:58:06 +08002859static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002860{
2861 static const struct phy_reg phy_reg_init[] = {
2862 /* Enable Delay cap */
2863 { 0x1f, 0x0005 },
2864 { 0x05, 0x8b80 },
2865 { 0x06, 0xc896 },
2866 { 0x1f, 0x0000 },
2867
2868 /* Channel estimation fine tune */
2869 { 0x1f, 0x0001 },
2870 { 0x0b, 0x6c20 },
2871 { 0x07, 0x2872 },
2872 { 0x1c, 0xefff },
2873 { 0x1f, 0x0003 },
2874 { 0x14, 0x6420 },
2875 { 0x1f, 0x0000 },
2876
2877 /* Update PFM & 10M TX idle timer */
2878 { 0x1f, 0x0007 },
2879 { 0x1e, 0x002f },
2880 { 0x15, 0x1919 },
2881 { 0x1f, 0x0000 },
2882
2883 { 0x1f, 0x0007 },
2884 { 0x1e, 0x00ac },
2885 { 0x18, 0x0006 },
2886 { 0x1f, 0x0000 }
2887 };
2888
Francois Romieu15ecd032011-04-27 13:52:22 -07002889 rtl_apply_firmware(tp);
2890
hayeswang01dc7fe2011-03-21 01:50:28 +00002891 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2892
2893 /* DCO enable for 10M IDLE Power */
2894 rtl_writephy(tp, 0x1f, 0x0007);
2895 rtl_writephy(tp, 0x1e, 0x0023);
2896 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2897 rtl_writephy(tp, 0x1f, 0x0000);
2898
2899 /* For impedance matching */
2900 rtl_writephy(tp, 0x1f, 0x0002);
2901 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002902 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002903
2904 /* PHY auto speed down */
2905 rtl_writephy(tp, 0x1f, 0x0007);
2906 rtl_writephy(tp, 0x1e, 0x002d);
2907 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2908 rtl_writephy(tp, 0x1f, 0x0000);
2909 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2910
2911 rtl_writephy(tp, 0x1f, 0x0005);
2912 rtl_writephy(tp, 0x05, 0x8b86);
2913 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2914 rtl_writephy(tp, 0x1f, 0x0000);
2915
2916 rtl_writephy(tp, 0x1f, 0x0005);
2917 rtl_writephy(tp, 0x05, 0x8b85);
2918 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2919 rtl_writephy(tp, 0x1f, 0x0007);
2920 rtl_writephy(tp, 0x1e, 0x0020);
2921 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2922 rtl_writephy(tp, 0x1f, 0x0006);
2923 rtl_writephy(tp, 0x00, 0x5a00);
2924 rtl_writephy(tp, 0x1f, 0x0000);
2925 rtl_writephy(tp, 0x0d, 0x0007);
2926 rtl_writephy(tp, 0x0e, 0x003c);
2927 rtl_writephy(tp, 0x0d, 0x4007);
2928 rtl_writephy(tp, 0x0e, 0x0000);
2929 rtl_writephy(tp, 0x0d, 0x0000);
2930}
2931
Hayes Wang70090422011-07-06 15:58:06 +08002932static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2933{
2934 static const struct phy_reg phy_reg_init[] = {
2935 /* Enable Delay cap */
2936 { 0x1f, 0x0004 },
2937 { 0x1f, 0x0007 },
2938 { 0x1e, 0x00ac },
2939 { 0x18, 0x0006 },
2940 { 0x1f, 0x0002 },
2941 { 0x1f, 0x0000 },
2942 { 0x1f, 0x0000 },
2943
2944 /* Channel estimation fine tune */
2945 { 0x1f, 0x0003 },
2946 { 0x09, 0xa20f },
2947 { 0x1f, 0x0000 },
2948 { 0x1f, 0x0000 },
2949
2950 /* Green Setting */
2951 { 0x1f, 0x0005 },
2952 { 0x05, 0x8b5b },
2953 { 0x06, 0x9222 },
2954 { 0x05, 0x8b6d },
2955 { 0x06, 0x8000 },
2956 { 0x05, 0x8b76 },
2957 { 0x06, 0x8000 },
2958 { 0x1f, 0x0000 }
2959 };
2960
2961 rtl_apply_firmware(tp);
2962
2963 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2964
2965 /* For 4-corner performance improve */
2966 rtl_writephy(tp, 0x1f, 0x0005);
2967 rtl_writephy(tp, 0x05, 0x8b80);
2968 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2969 rtl_writephy(tp, 0x1f, 0x0000);
2970
2971 /* PHY auto speed down */
2972 rtl_writephy(tp, 0x1f, 0x0004);
2973 rtl_writephy(tp, 0x1f, 0x0007);
2974 rtl_writephy(tp, 0x1e, 0x002d);
2975 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2976 rtl_writephy(tp, 0x1f, 0x0002);
2977 rtl_writephy(tp, 0x1f, 0x0000);
2978 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2979
2980 /* improve 10M EEE waveform */
2981 rtl_writephy(tp, 0x1f, 0x0005);
2982 rtl_writephy(tp, 0x05, 0x8b86);
2983 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2984 rtl_writephy(tp, 0x1f, 0x0000);
2985
2986 /* Improve 2-pair detection performance */
2987 rtl_writephy(tp, 0x1f, 0x0005);
2988 rtl_writephy(tp, 0x05, 0x8b85);
2989 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
2990 rtl_writephy(tp, 0x1f, 0x0000);
2991
2992 /* EEE setting */
2993 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
2994 ERIAR_EXGMAC);
2995 rtl_writephy(tp, 0x1f, 0x0005);
2996 rtl_writephy(tp, 0x05, 0x8b85);
2997 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2998 rtl_writephy(tp, 0x1f, 0x0004);
2999 rtl_writephy(tp, 0x1f, 0x0007);
3000 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003001 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003002 rtl_writephy(tp, 0x1f, 0x0002);
3003 rtl_writephy(tp, 0x1f, 0x0000);
3004 rtl_writephy(tp, 0x0d, 0x0007);
3005 rtl_writephy(tp, 0x0e, 0x003c);
3006 rtl_writephy(tp, 0x0d, 0x4007);
3007 rtl_writephy(tp, 0x0e, 0x0000);
3008 rtl_writephy(tp, 0x0d, 0x0000);
3009
3010 /* Green feature */
3011 rtl_writephy(tp, 0x1f, 0x0003);
3012 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3013 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3014 rtl_writephy(tp, 0x1f, 0x0000);
3015}
3016
Hayes Wangc2218922011-09-06 16:55:18 +08003017static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3018{
3019 static const struct phy_reg phy_reg_init[] = {
3020 /* Channel estimation fine tune */
3021 { 0x1f, 0x0003 },
3022 { 0x09, 0xa20f },
3023 { 0x1f, 0x0000 },
3024
3025 /* Modify green table for giga & fnet */
3026 { 0x1f, 0x0005 },
3027 { 0x05, 0x8b55 },
3028 { 0x06, 0x0000 },
3029 { 0x05, 0x8b5e },
3030 { 0x06, 0x0000 },
3031 { 0x05, 0x8b67 },
3032 { 0x06, 0x0000 },
3033 { 0x05, 0x8b70 },
3034 { 0x06, 0x0000 },
3035 { 0x1f, 0x0000 },
3036 { 0x1f, 0x0007 },
3037 { 0x1e, 0x0078 },
3038 { 0x17, 0x0000 },
3039 { 0x19, 0x00fb },
3040 { 0x1f, 0x0000 },
3041
3042 /* Modify green table for 10M */
3043 { 0x1f, 0x0005 },
3044 { 0x05, 0x8b79 },
3045 { 0x06, 0xaa00 },
3046 { 0x1f, 0x0000 },
3047
3048 /* Disable hiimpedance detection (RTCT) */
3049 { 0x1f, 0x0003 },
3050 { 0x01, 0x328a },
3051 { 0x1f, 0x0000 }
3052 };
3053
3054 rtl_apply_firmware(tp);
3055
3056 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3057
3058 /* For 4-corner performance improve */
3059 rtl_writephy(tp, 0x1f, 0x0005);
3060 rtl_writephy(tp, 0x05, 0x8b80);
3061 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3062 rtl_writephy(tp, 0x1f, 0x0000);
3063
3064 /* PHY auto speed down */
3065 rtl_writephy(tp, 0x1f, 0x0007);
3066 rtl_writephy(tp, 0x1e, 0x002d);
3067 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3068 rtl_writephy(tp, 0x1f, 0x0000);
3069 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3070
3071 /* Improve 10M EEE waveform */
3072 rtl_writephy(tp, 0x1f, 0x0005);
3073 rtl_writephy(tp, 0x05, 0x8b86);
3074 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3075 rtl_writephy(tp, 0x1f, 0x0000);
3076
3077 /* Improve 2-pair detection performance */
3078 rtl_writephy(tp, 0x1f, 0x0005);
3079 rtl_writephy(tp, 0x05, 0x8b85);
3080 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3081 rtl_writephy(tp, 0x1f, 0x0000);
3082}
3083
3084static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3085{
3086 rtl_apply_firmware(tp);
3087
3088 /* For 4-corner performance improve */
3089 rtl_writephy(tp, 0x1f, 0x0005);
3090 rtl_writephy(tp, 0x05, 0x8b80);
3091 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3092 rtl_writephy(tp, 0x1f, 0x0000);
3093
3094 /* PHY auto speed down */
3095 rtl_writephy(tp, 0x1f, 0x0007);
3096 rtl_writephy(tp, 0x1e, 0x002d);
3097 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3098 rtl_writephy(tp, 0x1f, 0x0000);
3099 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3100
3101 /* Improve 10M EEE waveform */
3102 rtl_writephy(tp, 0x1f, 0x0005);
3103 rtl_writephy(tp, 0x05, 0x8b86);
3104 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3105 rtl_writephy(tp, 0x1f, 0x0000);
3106}
3107
françois romieu4da19632011-01-03 15:07:55 +00003108static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003109{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003110 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003111 { 0x1f, 0x0003 },
3112 { 0x08, 0x441d },
3113 { 0x01, 0x9100 },
3114 { 0x1f, 0x0000 }
3115 };
3116
françois romieu4da19632011-01-03 15:07:55 +00003117 rtl_writephy(tp, 0x1f, 0x0000);
3118 rtl_patchphy(tp, 0x11, 1 << 12);
3119 rtl_patchphy(tp, 0x19, 1 << 13);
3120 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003121
françois romieu4da19632011-01-03 15:07:55 +00003122 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003123}
3124
Hayes Wang5a5e4442011-02-22 17:26:21 +08003125static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3126{
3127 static const struct phy_reg phy_reg_init[] = {
3128 { 0x1f, 0x0005 },
3129 { 0x1a, 0x0000 },
3130 { 0x1f, 0x0000 },
3131
3132 { 0x1f, 0x0004 },
3133 { 0x1c, 0x0000 },
3134 { 0x1f, 0x0000 },
3135
3136 { 0x1f, 0x0001 },
3137 { 0x15, 0x7701 },
3138 { 0x1f, 0x0000 }
3139 };
3140
3141 /* Disable ALDPS before ram code */
3142 rtl_writephy(tp, 0x1f, 0x0000);
3143 rtl_writephy(tp, 0x18, 0x0310);
3144 msleep(100);
3145
François Romieu953a12c2011-04-24 17:38:48 +02003146 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003147
3148 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3149}
3150
Francois Romieu5615d9f2007-08-17 17:50:46 +02003151static void rtl_hw_phy_config(struct net_device *dev)
3152{
3153 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003154
3155 rtl8169_print_mac_version(tp);
3156
3157 switch (tp->mac_version) {
3158 case RTL_GIGA_MAC_VER_01:
3159 break;
3160 case RTL_GIGA_MAC_VER_02:
3161 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003162 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003163 break;
3164 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003165 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003166 break;
françois romieu2e9558562009-08-10 19:44:19 +00003167 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003168 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003169 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003170 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003171 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003172 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003173 case RTL_GIGA_MAC_VER_07:
3174 case RTL_GIGA_MAC_VER_08:
3175 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003176 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003177 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003178 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003179 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003180 break;
3181 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003182 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003183 break;
3184 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003185 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003186 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003187 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003188 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003189 break;
3190 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003191 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003192 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003193 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003194 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003195 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003196 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003197 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003198 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003199 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003200 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003201 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003202 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003203 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003204 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003205 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003206 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003207 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003208 break;
3209 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003210 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003211 break;
3212 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003213 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003214 break;
françois romieue6de30d2011-01-03 15:08:37 +00003215 case RTL_GIGA_MAC_VER_28:
3216 rtl8168d_4_hw_phy_config(tp);
3217 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003218 case RTL_GIGA_MAC_VER_29:
3219 case RTL_GIGA_MAC_VER_30:
3220 rtl8105e_hw_phy_config(tp);
3221 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003222 case RTL_GIGA_MAC_VER_31:
3223 /* None. */
3224 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003225 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003226 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003227 rtl8168e_1_hw_phy_config(tp);
3228 break;
3229 case RTL_GIGA_MAC_VER_34:
3230 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003231 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003232 case RTL_GIGA_MAC_VER_35:
3233 rtl8168f_1_hw_phy_config(tp);
3234 break;
3235 case RTL_GIGA_MAC_VER_36:
3236 rtl8168f_2_hw_phy_config(tp);
3237 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003238
Francois Romieu5615d9f2007-08-17 17:50:46 +02003239 default:
3240 break;
3241 }
3242}
3243
Francois Romieuda78dbf2012-01-26 14:18:23 +01003244static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 struct timer_list *timer = &tp->timer;
3247 void __iomem *ioaddr = tp->mmio_addr;
3248 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3249
Francois Romieubcf0bf92006-07-26 23:14:13 +02003250 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
françois romieu4da19632011-01-03 15:07:55 +00003252 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003253 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 * A busy loop could burn quite a few cycles on nowadays CPU.
3255 * Let's delay the execution of the timer for a few ticks.
3256 */
3257 timeout = HZ/10;
3258 goto out_mod_timer;
3259 }
3260
3261 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003262 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Francois Romieuda78dbf2012-01-26 14:18:23 +01003264 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
françois romieu4da19632011-01-03 15:07:55 +00003266 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
3268out_mod_timer:
3269 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003270}
3271
3272static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3273{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003274 if (!test_and_set_bit(flag, tp->wk.flags))
3275 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003276}
3277
3278static void rtl8169_phy_timer(unsigned long __opaque)
3279{
3280 struct net_device *dev = (struct net_device *)__opaque;
3281 struct rtl8169_private *tp = netdev_priv(dev);
3282
Francois Romieu98ddf982012-01-31 10:47:34 +01003283 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284}
3285
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3287 void __iomem *ioaddr)
3288{
3289 iounmap(ioaddr);
3290 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003291 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 pci_disable_device(pdev);
3293 free_netdev(dev);
3294}
3295
Francois Romieubf793292006-11-01 00:53:05 +01003296static void rtl8169_phy_reset(struct net_device *dev,
3297 struct rtl8169_private *tp)
3298{
Francois Romieu07d3f512007-02-21 22:40:46 +01003299 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003300
françois romieu4da19632011-01-03 15:07:55 +00003301 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003302 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003303 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003304 return;
3305 msleep(1);
3306 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003307 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003308}
3309
David S. Miller8decf862011-09-22 03:23:13 -04003310static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3311{
3312 void __iomem *ioaddr = tp->mmio_addr;
3313
3314 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3315 (RTL_R8(PHYstatus) & TBI_Enable);
3316}
3317
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003318static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003320 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003321
Francois Romieu5615d9f2007-08-17 17:50:46 +02003322 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003323
Marcus Sundberg773328942008-07-10 21:28:08 +02003324 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3325 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3326 RTL_W8(0x82, 0x01);
3327 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003328
Francois Romieu6dccd162007-02-13 23:38:05 +01003329 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3330
3331 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3332 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003333
Francois Romieubcf0bf92006-07-26 23:14:13 +02003334 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003335 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3336 RTL_W8(0x82, 0x01);
3337 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003338 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003339 }
3340
Francois Romieubf793292006-11-01 00:53:05 +01003341 rtl8169_phy_reset(dev, tp);
3342
Oliver Neukum54405cd2011-01-06 21:55:13 +01003343 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003344 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3345 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3346 (tp->mii.supports_gmii ?
3347 ADVERTISED_1000baseT_Half |
3348 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003349
David S. Miller8decf862011-09-22 03:23:13 -04003350 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003351 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003352}
3353
Francois Romieu773d2022007-01-31 23:47:43 +01003354static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3355{
3356 void __iomem *ioaddr = tp->mmio_addr;
3357 u32 high;
3358 u32 low;
3359
3360 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3361 high = addr[4] | (addr[5] << 8);
3362
Francois Romieuda78dbf2012-01-26 14:18:23 +01003363 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003364
3365 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003366
Francois Romieu773d2022007-01-31 23:47:43 +01003367 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003368 RTL_R32(MAC4);
3369
Francois Romieu78f1cd02010-03-27 19:35:46 -07003370 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003371 RTL_R32(MAC0);
3372
françois romieuc28aa382011-08-02 03:53:43 +00003373 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3374 const struct exgmac_reg e[] = {
3375 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3376 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3377 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3378 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3379 low >> 16 },
3380 };
3381
3382 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3383 }
3384
Francois Romieu773d2022007-01-31 23:47:43 +01003385 RTL_W8(Cfg9346, Cfg9346_Lock);
3386
Francois Romieuda78dbf2012-01-26 14:18:23 +01003387 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003388}
3389
3390static int rtl_set_mac_address(struct net_device *dev, void *p)
3391{
3392 struct rtl8169_private *tp = netdev_priv(dev);
3393 struct sockaddr *addr = p;
3394
3395 if (!is_valid_ether_addr(addr->sa_data))
3396 return -EADDRNOTAVAIL;
3397
3398 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3399
3400 rtl_rar_set(tp, dev->dev_addr);
3401
3402 return 0;
3403}
3404
Francois Romieu5f787a12006-08-17 13:02:36 +02003405static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3406{
3407 struct rtl8169_private *tp = netdev_priv(dev);
3408 struct mii_ioctl_data *data = if_mii(ifr);
3409
Francois Romieu8b4ab282008-11-19 22:05:25 -08003410 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3411}
Francois Romieu5f787a12006-08-17 13:02:36 +02003412
Francois Romieucecb5fd2011-04-01 10:21:07 +02003413static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3414 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003415{
Francois Romieu5f787a12006-08-17 13:02:36 +02003416 switch (cmd) {
3417 case SIOCGMIIPHY:
3418 data->phy_id = 32; /* Internal PHY */
3419 return 0;
3420
3421 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003422 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003423 return 0;
3424
3425 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003426 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003427 return 0;
3428 }
3429 return -EOPNOTSUPP;
3430}
3431
Francois Romieu8b4ab282008-11-19 22:05:25 -08003432static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3433{
3434 return -EOPNOTSUPP;
3435}
3436
Francois Romieufbac58f2007-10-04 22:51:38 +02003437static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3438{
3439 if (tp->features & RTL_FEATURE_MSI) {
3440 pci_disable_msi(pdev);
3441 tp->features &= ~RTL_FEATURE_MSI;
3442 }
3443}
3444
françois romieuc0e45c12011-01-03 15:08:04 +00003445static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3446{
3447 struct mdio_ops *ops = &tp->mdio_ops;
3448
3449 switch (tp->mac_version) {
3450 case RTL_GIGA_MAC_VER_27:
3451 ops->write = r8168dp_1_mdio_write;
3452 ops->read = r8168dp_1_mdio_read;
3453 break;
françois romieue6de30d2011-01-03 15:08:37 +00003454 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003455 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003456 ops->write = r8168dp_2_mdio_write;
3457 ops->read = r8168dp_2_mdio_read;
3458 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003459 default:
3460 ops->write = r8169_mdio_write;
3461 ops->read = r8169_mdio_read;
3462 break;
3463 }
3464}
3465
David S. Miller1805b2f2011-10-24 18:18:09 -04003466static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3467{
3468 void __iomem *ioaddr = tp->mmio_addr;
3469
3470 switch (tp->mac_version) {
3471 case RTL_GIGA_MAC_VER_29:
3472 case RTL_GIGA_MAC_VER_30:
3473 case RTL_GIGA_MAC_VER_32:
3474 case RTL_GIGA_MAC_VER_33:
3475 case RTL_GIGA_MAC_VER_34:
3476 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3477 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3478 break;
3479 default:
3480 break;
3481 }
3482}
3483
3484static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3485{
3486 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3487 return false;
3488
3489 rtl_writephy(tp, 0x1f, 0x0000);
3490 rtl_writephy(tp, MII_BMCR, 0x0000);
3491
3492 rtl_wol_suspend_quirk(tp);
3493
3494 return true;
3495}
3496
françois romieu065c27c2011-01-03 15:08:12 +00003497static void r810x_phy_power_down(struct rtl8169_private *tp)
3498{
3499 rtl_writephy(tp, 0x1f, 0x0000);
3500 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3501}
3502
3503static void r810x_phy_power_up(struct rtl8169_private *tp)
3504{
3505 rtl_writephy(tp, 0x1f, 0x0000);
3506 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3507}
3508
3509static void r810x_pll_power_down(struct rtl8169_private *tp)
3510{
David S. Miller1805b2f2011-10-24 18:18:09 -04003511 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003512 return;
françois romieu065c27c2011-01-03 15:08:12 +00003513
3514 r810x_phy_power_down(tp);
3515}
3516
3517static void r810x_pll_power_up(struct rtl8169_private *tp)
3518{
3519 r810x_phy_power_up(tp);
3520}
3521
3522static void r8168_phy_power_up(struct rtl8169_private *tp)
3523{
3524 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003525 switch (tp->mac_version) {
3526 case RTL_GIGA_MAC_VER_11:
3527 case RTL_GIGA_MAC_VER_12:
3528 case RTL_GIGA_MAC_VER_17:
3529 case RTL_GIGA_MAC_VER_18:
3530 case RTL_GIGA_MAC_VER_19:
3531 case RTL_GIGA_MAC_VER_20:
3532 case RTL_GIGA_MAC_VER_21:
3533 case RTL_GIGA_MAC_VER_22:
3534 case RTL_GIGA_MAC_VER_23:
3535 case RTL_GIGA_MAC_VER_24:
3536 case RTL_GIGA_MAC_VER_25:
3537 case RTL_GIGA_MAC_VER_26:
3538 case RTL_GIGA_MAC_VER_27:
3539 case RTL_GIGA_MAC_VER_28:
3540 case RTL_GIGA_MAC_VER_31:
3541 rtl_writephy(tp, 0x0e, 0x0000);
3542 break;
3543 default:
3544 break;
3545 }
françois romieu065c27c2011-01-03 15:08:12 +00003546 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3547}
3548
3549static void r8168_phy_power_down(struct rtl8169_private *tp)
3550{
3551 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003552 switch (tp->mac_version) {
3553 case RTL_GIGA_MAC_VER_32:
3554 case RTL_GIGA_MAC_VER_33:
3555 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3556 break;
3557
3558 case RTL_GIGA_MAC_VER_11:
3559 case RTL_GIGA_MAC_VER_12:
3560 case RTL_GIGA_MAC_VER_17:
3561 case RTL_GIGA_MAC_VER_18:
3562 case RTL_GIGA_MAC_VER_19:
3563 case RTL_GIGA_MAC_VER_20:
3564 case RTL_GIGA_MAC_VER_21:
3565 case RTL_GIGA_MAC_VER_22:
3566 case RTL_GIGA_MAC_VER_23:
3567 case RTL_GIGA_MAC_VER_24:
3568 case RTL_GIGA_MAC_VER_25:
3569 case RTL_GIGA_MAC_VER_26:
3570 case RTL_GIGA_MAC_VER_27:
3571 case RTL_GIGA_MAC_VER_28:
3572 case RTL_GIGA_MAC_VER_31:
3573 rtl_writephy(tp, 0x0e, 0x0200);
3574 default:
3575 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3576 break;
3577 }
françois romieu065c27c2011-01-03 15:08:12 +00003578}
3579
3580static void r8168_pll_power_down(struct rtl8169_private *tp)
3581{
3582 void __iomem *ioaddr = tp->mmio_addr;
3583
Francois Romieucecb5fd2011-04-01 10:21:07 +02003584 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3585 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3586 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003587 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003588 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003589 }
françois romieu065c27c2011-01-03 15:08:12 +00003590
Francois Romieucecb5fd2011-04-01 10:21:07 +02003591 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3592 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003593 (RTL_R16(CPlusCmd) & ASF)) {
3594 return;
3595 }
3596
hayeswang01dc7fe2011-03-21 01:50:28 +00003597 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3598 tp->mac_version == RTL_GIGA_MAC_VER_33)
3599 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3600
David S. Miller1805b2f2011-10-24 18:18:09 -04003601 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003602 return;
françois romieu065c27c2011-01-03 15:08:12 +00003603
3604 r8168_phy_power_down(tp);
3605
3606 switch (tp->mac_version) {
3607 case RTL_GIGA_MAC_VER_25:
3608 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003609 case RTL_GIGA_MAC_VER_27:
3610 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003611 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003612 case RTL_GIGA_MAC_VER_32:
3613 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003614 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3615 break;
3616 }
3617}
3618
3619static void r8168_pll_power_up(struct rtl8169_private *tp)
3620{
3621 void __iomem *ioaddr = tp->mmio_addr;
3622
Francois Romieucecb5fd2011-04-01 10:21:07 +02003623 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3624 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3625 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003626 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003627 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003628 }
françois romieu065c27c2011-01-03 15:08:12 +00003629
3630 switch (tp->mac_version) {
3631 case RTL_GIGA_MAC_VER_25:
3632 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003633 case RTL_GIGA_MAC_VER_27:
3634 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003635 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003636 case RTL_GIGA_MAC_VER_32:
3637 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003638 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3639 break;
3640 }
3641
3642 r8168_phy_power_up(tp);
3643}
3644
Francois Romieud58d46b2011-05-03 16:38:29 +02003645static void rtl_generic_op(struct rtl8169_private *tp,
3646 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003647{
3648 if (op)
3649 op(tp);
3650}
3651
3652static void rtl_pll_power_down(struct rtl8169_private *tp)
3653{
Francois Romieud58d46b2011-05-03 16:38:29 +02003654 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003655}
3656
3657static void rtl_pll_power_up(struct rtl8169_private *tp)
3658{
Francois Romieud58d46b2011-05-03 16:38:29 +02003659 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003660}
3661
3662static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3663{
3664 struct pll_power_ops *ops = &tp->pll_power_ops;
3665
3666 switch (tp->mac_version) {
3667 case RTL_GIGA_MAC_VER_07:
3668 case RTL_GIGA_MAC_VER_08:
3669 case RTL_GIGA_MAC_VER_09:
3670 case RTL_GIGA_MAC_VER_10:
3671 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003672 case RTL_GIGA_MAC_VER_29:
3673 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003674 ops->down = r810x_pll_power_down;
3675 ops->up = r810x_pll_power_up;
3676 break;
3677
3678 case RTL_GIGA_MAC_VER_11:
3679 case RTL_GIGA_MAC_VER_12:
3680 case RTL_GIGA_MAC_VER_17:
3681 case RTL_GIGA_MAC_VER_18:
3682 case RTL_GIGA_MAC_VER_19:
3683 case RTL_GIGA_MAC_VER_20:
3684 case RTL_GIGA_MAC_VER_21:
3685 case RTL_GIGA_MAC_VER_22:
3686 case RTL_GIGA_MAC_VER_23:
3687 case RTL_GIGA_MAC_VER_24:
3688 case RTL_GIGA_MAC_VER_25:
3689 case RTL_GIGA_MAC_VER_26:
3690 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003691 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003692 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003693 case RTL_GIGA_MAC_VER_32:
3694 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003695 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003696 case RTL_GIGA_MAC_VER_35:
3697 case RTL_GIGA_MAC_VER_36:
françois romieu065c27c2011-01-03 15:08:12 +00003698 ops->down = r8168_pll_power_down;
3699 ops->up = r8168_pll_power_up;
3700 break;
3701
3702 default:
3703 ops->down = NULL;
3704 ops->up = NULL;
3705 break;
3706 }
3707}
3708
Hayes Wange542a222011-07-06 15:58:04 +08003709static void rtl_init_rxcfg(struct rtl8169_private *tp)
3710{
3711 void __iomem *ioaddr = tp->mmio_addr;
3712
3713 switch (tp->mac_version) {
3714 case RTL_GIGA_MAC_VER_01:
3715 case RTL_GIGA_MAC_VER_02:
3716 case RTL_GIGA_MAC_VER_03:
3717 case RTL_GIGA_MAC_VER_04:
3718 case RTL_GIGA_MAC_VER_05:
3719 case RTL_GIGA_MAC_VER_06:
3720 case RTL_GIGA_MAC_VER_10:
3721 case RTL_GIGA_MAC_VER_11:
3722 case RTL_GIGA_MAC_VER_12:
3723 case RTL_GIGA_MAC_VER_13:
3724 case RTL_GIGA_MAC_VER_14:
3725 case RTL_GIGA_MAC_VER_15:
3726 case RTL_GIGA_MAC_VER_16:
3727 case RTL_GIGA_MAC_VER_17:
3728 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3729 break;
3730 case RTL_GIGA_MAC_VER_18:
3731 case RTL_GIGA_MAC_VER_19:
3732 case RTL_GIGA_MAC_VER_20:
3733 case RTL_GIGA_MAC_VER_21:
3734 case RTL_GIGA_MAC_VER_22:
3735 case RTL_GIGA_MAC_VER_23:
3736 case RTL_GIGA_MAC_VER_24:
3737 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3738 break;
3739 default:
3740 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3741 break;
3742 }
3743}
3744
Hayes Wang92fc43b2011-07-06 15:58:03 +08003745static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3746{
3747 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3748}
3749
Francois Romieud58d46b2011-05-03 16:38:29 +02003750static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3751{
françois romieu9c5028e2012-03-02 04:43:14 +00003752 void __iomem *ioaddr = tp->mmio_addr;
3753
3754 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003755 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00003756 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003757}
3758
3759static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3760{
françois romieu9c5028e2012-03-02 04:43:14 +00003761 void __iomem *ioaddr = tp->mmio_addr;
3762
3763 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003764 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00003765 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003766}
3767
3768static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3769{
3770 void __iomem *ioaddr = tp->mmio_addr;
3771
3772 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3773 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3774 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3775}
3776
3777static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3778{
3779 void __iomem *ioaddr = tp->mmio_addr;
3780
3781 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3782 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3783 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3784}
3785
3786static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3787{
3788 void __iomem *ioaddr = tp->mmio_addr;
3789
3790 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3791}
3792
3793static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3794{
3795 void __iomem *ioaddr = tp->mmio_addr;
3796
3797 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3798}
3799
3800static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3801{
3802 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003803
3804 RTL_W8(MaxTxPacketSize, 0x3f);
3805 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3806 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003807 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003808}
3809
3810static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3811{
3812 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003813
3814 RTL_W8(MaxTxPacketSize, 0x0c);
3815 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3816 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003817 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003818}
3819
3820static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3821{
3822 rtl_tx_performance_tweak(tp->pci_dev,
3823 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3824}
3825
3826static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3827{
3828 rtl_tx_performance_tweak(tp->pci_dev,
3829 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3830}
3831
3832static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3833{
3834 void __iomem *ioaddr = tp->mmio_addr;
3835
3836 r8168b_0_hw_jumbo_enable(tp);
3837
3838 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3839}
3840
3841static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3842{
3843 void __iomem *ioaddr = tp->mmio_addr;
3844
3845 r8168b_0_hw_jumbo_disable(tp);
3846
3847 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3848}
3849
3850static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3851{
3852 struct jumbo_ops *ops = &tp->jumbo_ops;
3853
3854 switch (tp->mac_version) {
3855 case RTL_GIGA_MAC_VER_11:
3856 ops->disable = r8168b_0_hw_jumbo_disable;
3857 ops->enable = r8168b_0_hw_jumbo_enable;
3858 break;
3859 case RTL_GIGA_MAC_VER_12:
3860 case RTL_GIGA_MAC_VER_17:
3861 ops->disable = r8168b_1_hw_jumbo_disable;
3862 ops->enable = r8168b_1_hw_jumbo_enable;
3863 break;
3864 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3865 case RTL_GIGA_MAC_VER_19:
3866 case RTL_GIGA_MAC_VER_20:
3867 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3868 case RTL_GIGA_MAC_VER_22:
3869 case RTL_GIGA_MAC_VER_23:
3870 case RTL_GIGA_MAC_VER_24:
3871 case RTL_GIGA_MAC_VER_25:
3872 case RTL_GIGA_MAC_VER_26:
3873 ops->disable = r8168c_hw_jumbo_disable;
3874 ops->enable = r8168c_hw_jumbo_enable;
3875 break;
3876 case RTL_GIGA_MAC_VER_27:
3877 case RTL_GIGA_MAC_VER_28:
3878 ops->disable = r8168dp_hw_jumbo_disable;
3879 ops->enable = r8168dp_hw_jumbo_enable;
3880 break;
3881 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3882 case RTL_GIGA_MAC_VER_32:
3883 case RTL_GIGA_MAC_VER_33:
3884 case RTL_GIGA_MAC_VER_34:
3885 ops->disable = r8168e_hw_jumbo_disable;
3886 ops->enable = r8168e_hw_jumbo_enable;
3887 break;
3888
3889 /*
3890 * No action needed for jumbo frames with 8169.
3891 * No jumbo for 810x at all.
3892 */
3893 default:
3894 ops->disable = NULL;
3895 ops->enable = NULL;
3896 break;
3897 }
3898}
3899
Francois Romieu6f43adc2011-04-29 15:05:51 +02003900static void rtl_hw_reset(struct rtl8169_private *tp)
3901{
3902 void __iomem *ioaddr = tp->mmio_addr;
3903 int i;
3904
3905 /* Soft reset the chip. */
3906 RTL_W8(ChipCmd, CmdReset);
3907
3908 /* Check that the chip has finished the reset. */
3909 for (i = 0; i < 100; i++) {
3910 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3911 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003912 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003913 }
3914}
3915
Francois Romieub6ffd972011-06-17 17:00:05 +02003916static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3917{
3918 struct rtl_fw *rtl_fw;
3919 const char *name;
3920 int rc = -ENOMEM;
3921
3922 name = rtl_lookup_firmware_name(tp);
3923 if (!name)
3924 goto out_no_firmware;
3925
3926 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3927 if (!rtl_fw)
3928 goto err_warn;
3929
3930 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3931 if (rc < 0)
3932 goto err_free;
3933
Francois Romieufd112f22011-06-18 00:10:29 +02003934 rc = rtl_check_firmware(tp, rtl_fw);
3935 if (rc < 0)
3936 goto err_release_firmware;
3937
Francois Romieub6ffd972011-06-17 17:00:05 +02003938 tp->rtl_fw = rtl_fw;
3939out:
3940 return;
3941
Francois Romieufd112f22011-06-18 00:10:29 +02003942err_release_firmware:
3943 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02003944err_free:
3945 kfree(rtl_fw);
3946err_warn:
3947 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3948 name, rc);
3949out_no_firmware:
3950 tp->rtl_fw = NULL;
3951 goto out;
3952}
3953
François Romieu953a12c2011-04-24 17:38:48 +02003954static void rtl_request_firmware(struct rtl8169_private *tp)
3955{
Francois Romieub6ffd972011-06-17 17:00:05 +02003956 if (IS_ERR(tp->rtl_fw))
3957 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02003958}
3959
Hayes Wang92fc43b2011-07-06 15:58:03 +08003960static void rtl_rx_close(struct rtl8169_private *tp)
3961{
3962 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003963
Francois Romieu1687b562011-07-19 17:21:29 +02003964 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08003965}
3966
françois romieue6de30d2011-01-03 15:08:37 +00003967static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968{
françois romieue6de30d2011-01-03 15:08:37 +00003969 void __iomem *ioaddr = tp->mmio_addr;
3970
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00003972 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
Hayes Wang92fc43b2011-07-06 15:58:03 +08003974 rtl_rx_close(tp);
3975
Hayes Wang5d2e1952011-02-22 17:26:22 +08003976 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003977 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3978 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003979 while (RTL_R8(TxPoll) & NPQ)
3980 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08003981 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
3982 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
3983 tp->mac_version == RTL_GIGA_MAC_VER_36) {
David S. Miller8decf862011-09-22 03:23:13 -04003984 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08003985 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
3986 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08003987 } else {
3988 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
3989 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00003990 }
3991
Hayes Wang92fc43b2011-07-06 15:58:03 +08003992 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993}
3994
Francois Romieu7f796d832007-06-11 23:04:41 +02003995static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003996{
3997 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01003998
3999 /* Set DMA burst size and Interframe Gap Time */
4000 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4001 (InterFrameGap << TxInterFrameGapShift));
4002}
4003
Francois Romieu07ce4062007-02-23 23:36:39 +01004004static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005{
4006 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
Francois Romieu07ce4062007-02-23 23:36:39 +01004008 tp->hw_start(dev);
4009
Francois Romieuda78dbf2012-01-26 14:18:23 +01004010 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004011}
4012
Francois Romieu7f796d832007-06-11 23:04:41 +02004013static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4014 void __iomem *ioaddr)
4015{
4016 /*
4017 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4018 * register to be written before TxDescAddrLow to work.
4019 * Switching from MMIO to I/O access fixes the issue as well.
4020 */
4021 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004022 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004023 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004024 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004025}
4026
4027static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4028{
4029 u16 cmd;
4030
4031 cmd = RTL_R16(CPlusCmd);
4032 RTL_W16(CPlusCmd, cmd);
4033 return cmd;
4034}
4035
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004036static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004037{
4038 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004039 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004040}
4041
Francois Romieu6dccd162007-02-13 23:38:05 +01004042static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4043{
Francois Romieu37441002011-06-17 22:58:54 +02004044 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004045 u32 mac_version;
4046 u32 clk;
4047 u32 val;
4048 } cfg2_info [] = {
4049 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4050 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4051 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4052 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004053 };
4054 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004055 unsigned int i;
4056 u32 clk;
4057
4058 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004059 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004060 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4061 RTL_W32(0x7c, p->val);
4062 break;
4063 }
4064 }
4065}
4066
Francois Romieue6b763e2012-03-08 09:35:39 +01004067static void rtl_set_rx_mode(struct net_device *dev)
4068{
4069 struct rtl8169_private *tp = netdev_priv(dev);
4070 void __iomem *ioaddr = tp->mmio_addr;
4071 u32 mc_filter[2]; /* Multicast hash filter */
4072 int rx_mode;
4073 u32 tmp = 0;
4074
4075 if (dev->flags & IFF_PROMISC) {
4076 /* Unconditionally log net taps. */
4077 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4078 rx_mode =
4079 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4080 AcceptAllPhys;
4081 mc_filter[1] = mc_filter[0] = 0xffffffff;
4082 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4083 (dev->flags & IFF_ALLMULTI)) {
4084 /* Too many to filter perfectly -- accept all multicasts. */
4085 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4086 mc_filter[1] = mc_filter[0] = 0xffffffff;
4087 } else {
4088 struct netdev_hw_addr *ha;
4089
4090 rx_mode = AcceptBroadcast | AcceptMyPhys;
4091 mc_filter[1] = mc_filter[0] = 0;
4092 netdev_for_each_mc_addr(ha, dev) {
4093 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4094 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4095 rx_mode |= AcceptMulticast;
4096 }
4097 }
4098
4099 if (dev->features & NETIF_F_RXALL)
4100 rx_mode |= (AcceptErr | AcceptRunt);
4101
4102 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4103
4104 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4105 u32 data = mc_filter[0];
4106
4107 mc_filter[0] = swab32(mc_filter[1]);
4108 mc_filter[1] = swab32(data);
4109 }
4110
4111 RTL_W32(MAR0 + 4, mc_filter[1]);
4112 RTL_W32(MAR0 + 0, mc_filter[0]);
4113
4114 RTL_W32(RxConfig, tmp);
4115}
4116
Francois Romieu07ce4062007-02-23 23:36:39 +01004117static void rtl_hw_start_8169(struct net_device *dev)
4118{
4119 struct rtl8169_private *tp = netdev_priv(dev);
4120 void __iomem *ioaddr = tp->mmio_addr;
4121 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004122
Francois Romieu9cb427b2006-11-02 00:10:16 +01004123 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4124 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4125 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4126 }
4127
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004129 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4130 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4131 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4132 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004133 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4134
Hayes Wange542a222011-07-06 15:58:04 +08004135 rtl_init_rxcfg(tp);
4136
françois romieuf0298f82011-01-03 15:07:42 +00004137 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004139 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
Francois Romieucecb5fd2011-04-01 10:21:07 +02004141 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4142 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4143 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4144 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004145 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
Francois Romieu7f796d832007-06-11 23:04:41 +02004147 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004148
Francois Romieucecb5fd2011-04-01 10:21:07 +02004149 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4150 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004151 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004153 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 }
4155
Francois Romieubcf0bf92006-07-26 23:14:13 +02004156 RTL_W16(CPlusCmd, tp->cp_cmd);
4157
Francois Romieu6dccd162007-02-13 23:38:05 +01004158 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4159
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 /*
4161 * Undocumented corner. Supposedly:
4162 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4163 */
4164 RTL_W16(IntrMitigate, 0x0000);
4165
Francois Romieu7f796d832007-06-11 23:04:41 +02004166 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004167
Francois Romieucecb5fd2011-04-01 10:21:07 +02004168 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4169 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4170 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4171 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004172 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4173 rtl_set_rx_tx_config_registers(tp);
4174 }
4175
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004177
4178 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4179 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
4181 RTL_W32(RxMissed, 0);
4182
Francois Romieu07ce4062007-02-23 23:36:39 +01004183 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
4185 /* no early-rx interrupts */
4186 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004187}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
françois romieu650e8d52011-01-03 15:08:29 +00004189static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004190{
4191 u32 csi;
4192
4193 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004194 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4195}
4196
françois romieue6de30d2011-01-03 15:08:37 +00004197static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4198{
4199 rtl_csi_access_enable(ioaddr, 0x17000000);
4200}
4201
françois romieu650e8d52011-01-03 15:08:29 +00004202static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4203{
4204 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004205}
4206
4207struct ephy_info {
4208 unsigned int offset;
4209 u16 mask;
4210 u16 bits;
4211};
4212
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004213static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004214{
4215 u16 w;
4216
4217 while (len-- > 0) {
4218 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4219 rtl_ephy_write(ioaddr, e->offset, w);
4220 e++;
4221 }
4222}
4223
Francois Romieub726e492008-06-28 12:22:59 +02004224static void rtl_disable_clock_request(struct pci_dev *pdev)
4225{
Jon Masone44daad2011-06-27 07:46:31 +00004226 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004227
4228 if (cap) {
4229 u16 ctl;
4230
4231 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4232 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4233 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4234 }
4235}
4236
françois romieue6de30d2011-01-03 15:08:37 +00004237static void rtl_enable_clock_request(struct pci_dev *pdev)
4238{
Jon Masone44daad2011-06-27 07:46:31 +00004239 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004240
4241 if (cap) {
4242 u16 ctl;
4243
4244 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4245 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4246 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4247 }
4248}
4249
Francois Romieub726e492008-06-28 12:22:59 +02004250#define R8168_CPCMD_QUIRK_MASK (\
4251 EnableBist | \
4252 Mac_dbgo_oe | \
4253 Force_half_dup | \
4254 Force_rxflow_en | \
4255 Force_txflow_en | \
4256 Cxpl_dbg_sel | \
4257 ASF | \
4258 PktCntrDisable | \
4259 Mac_dbgo_sel)
4260
Francois Romieu219a1e92008-06-28 11:58:39 +02004261static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4262{
Francois Romieub726e492008-06-28 12:22:59 +02004263 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4264
4265 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4266
Francois Romieu2e68ae42008-06-28 12:00:55 +02004267 rtl_tx_performance_tweak(pdev,
4268 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004269}
4270
4271static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4272{
4273 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004274
françois romieuf0298f82011-01-03 15:07:42 +00004275 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004276
4277 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004278}
4279
4280static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4281{
Francois Romieub726e492008-06-28 12:22:59 +02004282 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4283
4284 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4285
Francois Romieu219a1e92008-06-28 11:58:39 +02004286 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004287
4288 rtl_disable_clock_request(pdev);
4289
4290 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004291}
4292
Francois Romieuef3386f2008-06-29 12:24:30 +02004293static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004294{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004295 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004296 { 0x01, 0, 0x0001 },
4297 { 0x02, 0x0800, 0x1000 },
4298 { 0x03, 0, 0x0042 },
4299 { 0x06, 0x0080, 0x0000 },
4300 { 0x07, 0, 0x2000 }
4301 };
4302
françois romieu650e8d52011-01-03 15:08:29 +00004303 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004304
4305 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4306
Francois Romieu219a1e92008-06-28 11:58:39 +02004307 __rtl_hw_start_8168cp(ioaddr, pdev);
4308}
4309
Francois Romieuef3386f2008-06-29 12:24:30 +02004310static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4311{
françois romieu650e8d52011-01-03 15:08:29 +00004312 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004313
4314 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4315
4316 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4317
4318 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4319}
4320
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004321static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4322{
françois romieu650e8d52011-01-03 15:08:29 +00004323 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004324
4325 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4326
4327 /* Magic. */
4328 RTL_W8(DBG_REG, 0x20);
4329
françois romieuf0298f82011-01-03 15:07:42 +00004330 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004331
4332 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4333
4334 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4335}
4336
Francois Romieu219a1e92008-06-28 11:58:39 +02004337static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4338{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004339 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004340 { 0x02, 0x0800, 0x1000 },
4341 { 0x03, 0, 0x0002 },
4342 { 0x06, 0x0080, 0x0000 }
4343 };
4344
françois romieu650e8d52011-01-03 15:08:29 +00004345 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004346
4347 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4348
4349 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4350
Francois Romieu219a1e92008-06-28 11:58:39 +02004351 __rtl_hw_start_8168cp(ioaddr, pdev);
4352}
4353
4354static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4355{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004356 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004357 { 0x01, 0, 0x0001 },
4358 { 0x03, 0x0400, 0x0220 }
4359 };
4360
françois romieu650e8d52011-01-03 15:08:29 +00004361 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004362
4363 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4364
Francois Romieu219a1e92008-06-28 11:58:39 +02004365 __rtl_hw_start_8168cp(ioaddr, pdev);
4366}
4367
Francois Romieu197ff762008-06-28 13:16:02 +02004368static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4369{
4370 rtl_hw_start_8168c_2(ioaddr, pdev);
4371}
4372
Francois Romieu6fb07052008-06-29 11:54:28 +02004373static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4374{
françois romieu650e8d52011-01-03 15:08:29 +00004375 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004376
4377 __rtl_hw_start_8168cp(ioaddr, pdev);
4378}
4379
Francois Romieu5b538df2008-07-20 16:22:45 +02004380static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4381{
françois romieu650e8d52011-01-03 15:08:29 +00004382 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004383
4384 rtl_disable_clock_request(pdev);
4385
françois romieuf0298f82011-01-03 15:07:42 +00004386 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004387
4388 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4389
4390 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4391}
4392
hayeswang4804b3b2011-03-21 01:50:29 +00004393static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4394{
4395 rtl_csi_access_enable_1(ioaddr);
4396
4397 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4398
4399 RTL_W8(MaxTxPacketSize, TxPacketMax);
4400
4401 rtl_disable_clock_request(pdev);
4402}
4403
françois romieue6de30d2011-01-03 15:08:37 +00004404static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4405{
4406 static const struct ephy_info e_info_8168d_4[] = {
4407 { 0x0b, ~0, 0x48 },
4408 { 0x19, 0x20, 0x50 },
4409 { 0x0c, ~0, 0x20 }
4410 };
4411 int i;
4412
4413 rtl_csi_access_enable_1(ioaddr);
4414
4415 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4416
4417 RTL_W8(MaxTxPacketSize, TxPacketMax);
4418
4419 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4420 const struct ephy_info *e = e_info_8168d_4 + i;
4421 u16 w;
4422
4423 w = rtl_ephy_read(ioaddr, e->offset);
4424 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4425 }
4426
4427 rtl_enable_clock_request(pdev);
4428}
4429
Hayes Wang70090422011-07-06 15:58:06 +08004430static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004431{
Hayes Wang70090422011-07-06 15:58:06 +08004432 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004433 { 0x00, 0x0200, 0x0100 },
4434 { 0x00, 0x0000, 0x0004 },
4435 { 0x06, 0x0002, 0x0001 },
4436 { 0x06, 0x0000, 0x0030 },
4437 { 0x07, 0x0000, 0x2000 },
4438 { 0x00, 0x0000, 0x0020 },
4439 { 0x03, 0x5800, 0x2000 },
4440 { 0x03, 0x0000, 0x0001 },
4441 { 0x01, 0x0800, 0x1000 },
4442 { 0x07, 0x0000, 0x4000 },
4443 { 0x1e, 0x0000, 0x2000 },
4444 { 0x19, 0xffff, 0xfe6c },
4445 { 0x0a, 0x0000, 0x0040 }
4446 };
4447
4448 rtl_csi_access_enable_2(ioaddr);
4449
Hayes Wang70090422011-07-06 15:58:06 +08004450 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004451
4452 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4453
4454 RTL_W8(MaxTxPacketSize, TxPacketMax);
4455
4456 rtl_disable_clock_request(pdev);
4457
4458 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004459 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4460 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004461
Francois Romieucecb5fd2011-04-01 10:21:07 +02004462 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004463}
4464
Hayes Wang70090422011-07-06 15:58:06 +08004465static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4466{
4467 static const struct ephy_info e_info_8168e_2[] = {
4468 { 0x09, 0x0000, 0x0080 },
4469 { 0x19, 0x0000, 0x0224 }
4470 };
4471
4472 rtl_csi_access_enable_1(ioaddr);
4473
4474 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4475
4476 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4477
4478 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4479 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4480 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4481 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4482 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4483 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4484 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4485 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4486 ERIAR_EXGMAC);
4487
Hayes Wang3090bd92011-09-06 16:55:15 +08004488 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004489
4490 rtl_disable_clock_request(pdev);
4491
4492 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4493 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4494
4495 /* Adjust EEE LED frequency */
4496 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4497
4498 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4499 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4500 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4501}
4502
Hayes Wangc2218922011-09-06 16:55:18 +08004503static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4504{
4505 static const struct ephy_info e_info_8168f_1[] = {
4506 { 0x06, 0x00c0, 0x0020 },
4507 { 0x08, 0x0001, 0x0002 },
4508 { 0x09, 0x0000, 0x0080 },
4509 { 0x19, 0x0000, 0x0224 }
4510 };
4511
4512 rtl_csi_access_enable_1(ioaddr);
4513
4514 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4515
4516 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4517
4518 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4519 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4520 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4521 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4522 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4523 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4524 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4525 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4526 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4527 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4528 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4529 ERIAR_EXGMAC);
4530
4531 RTL_W8(MaxTxPacketSize, EarlySize);
4532
4533 rtl_disable_clock_request(pdev);
4534
4535 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4536 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4537
4538 /* Adjust EEE LED frequency */
4539 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4540
4541 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4542 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4543 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4544}
4545
Francois Romieu07ce4062007-02-23 23:36:39 +01004546static void rtl_hw_start_8168(struct net_device *dev)
4547{
Francois Romieu2dd99532007-06-11 23:22:52 +02004548 struct rtl8169_private *tp = netdev_priv(dev);
4549 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004550 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004551
4552 RTL_W8(Cfg9346, Cfg9346_Unlock);
4553
françois romieuf0298f82011-01-03 15:07:42 +00004554 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004555
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004556 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004557
Francois Romieu0e485152007-02-20 00:00:26 +01004558 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004559
4560 RTL_W16(CPlusCmd, tp->cp_cmd);
4561
Francois Romieu0e485152007-02-20 00:00:26 +01004562 RTL_W16(IntrMitigate, 0x5151);
4563
4564 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004565 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01004566 tp->event_slow |= RxFIFOOver | PCSTimeout;
4567 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01004568 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004569
4570 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4571
Francois Romieub8363902008-06-01 12:31:57 +02004572 rtl_set_rx_mode(dev);
4573
4574 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4575 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004576
4577 RTL_R8(IntrMask);
4578
Francois Romieu219a1e92008-06-28 11:58:39 +02004579 switch (tp->mac_version) {
4580 case RTL_GIGA_MAC_VER_11:
4581 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004582 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004583
4584 case RTL_GIGA_MAC_VER_12:
4585 case RTL_GIGA_MAC_VER_17:
4586 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004587 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004588
4589 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004590 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004591 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004592
4593 case RTL_GIGA_MAC_VER_19:
4594 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004595 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004596
4597 case RTL_GIGA_MAC_VER_20:
4598 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004599 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004600
Francois Romieu197ff762008-06-28 13:16:02 +02004601 case RTL_GIGA_MAC_VER_21:
4602 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004603 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004604
Francois Romieu6fb07052008-06-29 11:54:28 +02004605 case RTL_GIGA_MAC_VER_22:
4606 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004607 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004608
Francois Romieuef3386f2008-06-29 12:24:30 +02004609 case RTL_GIGA_MAC_VER_23:
4610 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004611 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004612
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004613 case RTL_GIGA_MAC_VER_24:
4614 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004615 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004616
Francois Romieu5b538df2008-07-20 16:22:45 +02004617 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004618 case RTL_GIGA_MAC_VER_26:
4619 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004620 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004621 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004622
françois romieue6de30d2011-01-03 15:08:37 +00004623 case RTL_GIGA_MAC_VER_28:
4624 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004625 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004626
hayeswang4804b3b2011-03-21 01:50:29 +00004627 case RTL_GIGA_MAC_VER_31:
4628 rtl_hw_start_8168dp(ioaddr, pdev);
4629 break;
4630
hayeswang01dc7fe2011-03-21 01:50:28 +00004631 case RTL_GIGA_MAC_VER_32:
4632 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004633 rtl_hw_start_8168e_1(ioaddr, pdev);
4634 break;
4635 case RTL_GIGA_MAC_VER_34:
4636 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004637 break;
françois romieue6de30d2011-01-03 15:08:37 +00004638
Hayes Wangc2218922011-09-06 16:55:18 +08004639 case RTL_GIGA_MAC_VER_35:
4640 case RTL_GIGA_MAC_VER_36:
4641 rtl_hw_start_8168f_1(ioaddr, pdev);
4642 break;
4643
Francois Romieu219a1e92008-06-28 11:58:39 +02004644 default:
4645 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4646 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004647 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004648 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004649
Francois Romieu0e485152007-02-20 00:00:26 +01004650 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4651
Francois Romieub8363902008-06-01 12:31:57 +02004652 RTL_W8(Cfg9346, Cfg9346_Lock);
4653
Francois Romieu2dd99532007-06-11 23:22:52 +02004654 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004655}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656
Francois Romieu2857ffb2008-08-02 21:08:49 +02004657#define R810X_CPCMD_QUIRK_MASK (\
4658 EnableBist | \
4659 Mac_dbgo_oe | \
4660 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004661 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004662 Force_txflow_en | \
4663 Cxpl_dbg_sel | \
4664 ASF | \
4665 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004666 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004667
4668static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4669{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004670 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004671 { 0x01, 0, 0x6e65 },
4672 { 0x02, 0, 0x091f },
4673 { 0x03, 0, 0xc2f9 },
4674 { 0x06, 0, 0xafb5 },
4675 { 0x07, 0, 0x0e00 },
4676 { 0x19, 0, 0xec80 },
4677 { 0x01, 0, 0x2e65 },
4678 { 0x01, 0, 0x6e65 }
4679 };
4680 u8 cfg1;
4681
françois romieu650e8d52011-01-03 15:08:29 +00004682 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004683
4684 RTL_W8(DBG_REG, FIX_NAK_1);
4685
4686 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4687
4688 RTL_W8(Config1,
4689 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4690 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4691
4692 cfg1 = RTL_R8(Config1);
4693 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4694 RTL_W8(Config1, cfg1 & ~LEDS0);
4695
Francois Romieu2857ffb2008-08-02 21:08:49 +02004696 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4697}
4698
4699static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4700{
françois romieu650e8d52011-01-03 15:08:29 +00004701 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004702
4703 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4704
4705 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4706 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004707}
4708
4709static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4710{
4711 rtl_hw_start_8102e_2(ioaddr, pdev);
4712
4713 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4714}
4715
Hayes Wang5a5e4442011-02-22 17:26:21 +08004716static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4717{
4718 static const struct ephy_info e_info_8105e_1[] = {
4719 { 0x07, 0, 0x4000 },
4720 { 0x19, 0, 0x0200 },
4721 { 0x19, 0, 0x0020 },
4722 { 0x1e, 0, 0x2000 },
4723 { 0x03, 0, 0x0001 },
4724 { 0x19, 0, 0x0100 },
4725 { 0x19, 0, 0x0004 },
4726 { 0x0a, 0, 0x0020 }
4727 };
4728
Francois Romieucecb5fd2011-04-01 10:21:07 +02004729 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004730 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4731
Francois Romieucecb5fd2011-04-01 10:21:07 +02004732 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004733 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4734
4735 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08004736 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004737
4738 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4739}
4740
4741static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4742{
4743 rtl_hw_start_8105e_1(ioaddr, pdev);
4744 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4745}
4746
Francois Romieu07ce4062007-02-23 23:36:39 +01004747static void rtl_hw_start_8101(struct net_device *dev)
4748{
Francois Romieucdf1a602007-06-11 23:29:50 +02004749 struct rtl8169_private *tp = netdev_priv(dev);
4750 void __iomem *ioaddr = tp->mmio_addr;
4751 struct pci_dev *pdev = tp->pci_dev;
4752
Francois Romieuda78dbf2012-01-26 14:18:23 +01004753 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
4754 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00004755
Francois Romieucecb5fd2011-04-01 10:21:07 +02004756 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4757 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00004758 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02004759
4760 if (cap) {
4761 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4762 PCI_EXP_DEVCTL_NOSNOOP_EN);
4763 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004764 }
4765
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004766 RTL_W8(Cfg9346, Cfg9346_Unlock);
4767
Francois Romieu2857ffb2008-08-02 21:08:49 +02004768 switch (tp->mac_version) {
4769 case RTL_GIGA_MAC_VER_07:
4770 rtl_hw_start_8102e_1(ioaddr, pdev);
4771 break;
4772
4773 case RTL_GIGA_MAC_VER_08:
4774 rtl_hw_start_8102e_3(ioaddr, pdev);
4775 break;
4776
4777 case RTL_GIGA_MAC_VER_09:
4778 rtl_hw_start_8102e_2(ioaddr, pdev);
4779 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004780
4781 case RTL_GIGA_MAC_VER_29:
4782 rtl_hw_start_8105e_1(ioaddr, pdev);
4783 break;
4784 case RTL_GIGA_MAC_VER_30:
4785 rtl_hw_start_8105e_2(ioaddr, pdev);
4786 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004787 }
4788
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004789 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004790
françois romieuf0298f82011-01-03 15:07:42 +00004791 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004792
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004793 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004794
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004795 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004796 RTL_W16(CPlusCmd, tp->cp_cmd);
4797
4798 RTL_W16(IntrMitigate, 0x0000);
4799
4800 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4801
4802 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4803 rtl_set_rx_tx_config_registers(tp);
4804
Francois Romieucdf1a602007-06-11 23:29:50 +02004805 RTL_R8(IntrMask);
4806
Francois Romieucdf1a602007-06-11 23:29:50 +02004807 rtl_set_rx_mode(dev);
4808
4809 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810}
4811
4812static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4813{
Francois Romieud58d46b2011-05-03 16:38:29 +02004814 struct rtl8169_private *tp = netdev_priv(dev);
4815
4816 if (new_mtu < ETH_ZLEN ||
4817 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 return -EINVAL;
4819
Francois Romieud58d46b2011-05-03 16:38:29 +02004820 if (new_mtu > ETH_DATA_LEN)
4821 rtl_hw_jumbo_enable(tp);
4822 else
4823 rtl_hw_jumbo_disable(tp);
4824
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004826 netdev_update_features(dev);
4827
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004828 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829}
4830
4831static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4832{
Al Viro95e09182007-12-22 18:55:39 +00004833 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4835}
4836
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004837static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4838 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004840 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004841 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004842
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004843 kfree(*data_buff);
4844 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 rtl8169_make_unusable_by_asic(desc);
4846}
4847
4848static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4849{
4850 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4851
4852 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4853}
4854
4855static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4856 u32 rx_buf_sz)
4857{
4858 desc->addr = cpu_to_le64(mapping);
4859 wmb();
4860 rtl8169_mark_to_asic(desc, rx_buf_sz);
4861}
4862
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004863static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004865 return (void *)ALIGN((long)data, 16);
4866}
4867
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004868static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4869 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004870{
4871 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004873 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004874 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004875 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004877 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4878 if (!data)
4879 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004880
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004881 if (rtl8169_align(data) != data) {
4882 kfree(data);
4883 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4884 if (!data)
4885 return NULL;
4886 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004887
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004888 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004889 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004890 if (unlikely(dma_mapping_error(d, mapping))) {
4891 if (net_ratelimit())
4892 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004893 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
4896 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004897 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004898
4899err_out:
4900 kfree(data);
4901 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902}
4903
4904static void rtl8169_rx_clear(struct rtl8169_private *tp)
4905{
Francois Romieu07d3f512007-02-21 22:40:46 +01004906 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
4908 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004909 if (tp->Rx_databuff[i]) {
4910 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 tp->RxDescArray + i);
4912 }
4913 }
4914}
4915
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004916static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004918 desc->opts1 |= cpu_to_le32(RingEnd);
4919}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004920
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004921static int rtl8169_rx_fill(struct rtl8169_private *tp)
4922{
4923 unsigned int i;
4924
4925 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004926 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004927
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004928 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004930
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004931 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004932 if (!data) {
4933 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004934 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004935 }
4936 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004939 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4940 return 0;
4941
4942err_out:
4943 rtl8169_rx_clear(tp);
4944 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945}
4946
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947static int rtl8169_init_ring(struct net_device *dev)
4948{
4949 struct rtl8169_private *tp = netdev_priv(dev);
4950
4951 rtl8169_init_ring_indexes(tp);
4952
4953 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004954 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004956 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957}
4958
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004959static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 struct TxDesc *desc)
4961{
4962 unsigned int len = tx_skb->len;
4963
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004964 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4965
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 desc->opts1 = 0x00;
4967 desc->opts2 = 0x00;
4968 desc->addr = 0x00;
4969 tx_skb->len = 0;
4970}
4971
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004972static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4973 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974{
4975 unsigned int i;
4976
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004977 for (i = 0; i < n; i++) {
4978 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 struct ring_info *tx_skb = tp->tx_skb + entry;
4980 unsigned int len = tx_skb->len;
4981
4982 if (len) {
4983 struct sk_buff *skb = tx_skb->skb;
4984
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004985 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 tp->TxDescArray + entry);
4987 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004988 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 dev_kfree_skb(skb);
4990 tx_skb->skb = NULL;
4991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 }
4993 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004994}
4995
4996static void rtl8169_tx_clear(struct rtl8169_private *tp)
4997{
4998 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 tp->cur_tx = tp->dirty_tx = 0;
Igor Maravic036dafa2012-03-05 00:01:25 +01005000 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001}
5002
Francois Romieu4422bcd2012-01-26 11:23:32 +01005003static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004{
David Howellsc4028952006-11-22 14:57:56 +00005005 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005006 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007
Francois Romieuda78dbf2012-01-26 14:18:23 +01005008 napi_disable(&tp->napi);
5009 netif_stop_queue(dev);
5010 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011
françois romieuc7c2c392011-12-04 20:30:52 +00005012 rtl8169_hw_reset(tp);
5013
Francois Romieu56de4142011-03-15 17:29:31 +01005014 for (i = 0; i < NUM_RX_DESC; i++)
5015 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5016
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005018 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019
Francois Romieuda78dbf2012-01-26 14:18:23 +01005020 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005021 rtl_hw_start(dev);
5022 netif_wake_queue(dev);
5023 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024}
5025
5026static void rtl8169_tx_timeout(struct net_device *dev)
5027{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005028 struct rtl8169_private *tp = netdev_priv(dev);
5029
5030 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031}
5032
5033static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005034 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035{
5036 struct skb_shared_info *info = skb_shinfo(skb);
5037 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005038 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005039 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040
5041 entry = tp->cur_tx;
5042 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005043 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 dma_addr_t mapping;
5045 u32 status, len;
5046 void *addr;
5047
5048 entry = (entry + 1) % NUM_TX_DESC;
5049
5050 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005051 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005052 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005053 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005054 if (unlikely(dma_mapping_error(d, mapping))) {
5055 if (net_ratelimit())
5056 netif_err(tp, drv, tp->dev,
5057 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005058 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
Francois Romieucecb5fd2011-04-01 10:21:07 +02005061 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005062 status = opts[0] | len |
5063 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064
5065 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005066 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 txd->addr = cpu_to_le64(mapping);
5068
5069 tp->tx_skb[entry].len = len;
5070 }
5071
5072 if (cur_frag) {
5073 tp->tx_skb[entry].skb = skb;
5074 txd->opts1 |= cpu_to_le32(LastFrag);
5075 }
5076
5077 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005078
5079err_out:
5080 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5081 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082}
5083
Francois Romieu2b7b4312011-04-18 22:53:24 -07005084static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5085 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005087 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005088 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005089 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
Francois Romieu2b7b4312011-04-18 22:53:24 -07005091 if (mss) {
5092 opts[0] |= TD_LSO;
5093 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5094 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005095 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096
5097 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005098 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005100 opts[offset] |= info->checksum.udp;
5101 else
5102 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104}
5105
Stephen Hemminger613573252009-08-31 19:50:58 +00005106static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5107 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108{
5109 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005110 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 struct TxDesc *txd = tp->TxDescArray + entry;
5112 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005113 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 dma_addr_t mapping;
5115 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005116 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005117 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005118
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005120 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005121 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 }
5123
5124 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005125 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005127 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005128 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005129 if (unlikely(dma_mapping_error(d, mapping))) {
5130 if (net_ratelimit())
5131 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005132 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134
5135 tp->tx_skb[entry].len = len;
5136 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137
Francois Romieu2b7b4312011-04-18 22:53:24 -07005138 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5139 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005140
Francois Romieu2b7b4312011-04-18 22:53:24 -07005141 rtl8169_tso_csum(tp, skb, opts);
5142
5143 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005144 if (frags < 0)
5145 goto err_dma_1;
5146 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005147 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005148 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005149 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005150 tp->tx_skb[entry].skb = skb;
5151 }
5152
Francois Romieu2b7b4312011-04-18 22:53:24 -07005153 txd->opts2 = cpu_to_le32(opts[1]);
5154
Igor Maravic036dafa2012-03-05 00:01:25 +01005155 netdev_sent_queue(dev, skb->len);
5156
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 wmb();
5158
Francois Romieucecb5fd2011-04-01 10:21:07 +02005159 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005160 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 txd->opts1 = cpu_to_le32(status);
5162
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 tp->cur_tx += frags + 1;
5164
David Dillow4c020a92010-03-03 16:33:10 +00005165 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166
Francois Romieucecb5fd2011-04-01 10:21:07 +02005167 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168
Francois Romieuda78dbf2012-01-26 14:18:23 +01005169 mmiowb();
5170
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005172 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5173 * not miss a ring update when it notices a stopped queue.
5174 */
5175 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005177 /* Sync with rtl_tx:
5178 * - publish queue status and cur_tx ring index (write barrier)
5179 * - refresh dirty_tx ring index (read barrier).
5180 * May the current thread have a pessimistic view of the ring
5181 * status and forget to wake up queue, a racing rtl_tx thread
5182 * can't.
5183 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005184 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5186 netif_wake_queue(dev);
5187 }
5188
Stephen Hemminger613573252009-08-31 19:50:58 +00005189 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005191err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005192 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005193err_dma_0:
5194 dev_kfree_skb(skb);
5195 dev->stats.tx_dropped++;
5196 return NETDEV_TX_OK;
5197
5198err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005200 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005201 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202}
5203
5204static void rtl8169_pcierr_interrupt(struct net_device *dev)
5205{
5206 struct rtl8169_private *tp = netdev_priv(dev);
5207 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 u16 pci_status, pci_cmd;
5209
5210 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5211 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5212
Joe Perchesbf82c182010-02-09 11:49:50 +00005213 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5214 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215
5216 /*
5217 * The recovery sequence below admits a very elaborated explanation:
5218 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005219 * - I did not see what else could be done;
5220 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 *
5222 * Feel free to adjust to your needs.
5223 */
Francois Romieua27993f2006-12-18 00:04:19 +01005224 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005225 pci_cmd &= ~PCI_COMMAND_PARITY;
5226 else
5227 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5228
5229 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230
5231 pci_write_config_word(pdev, PCI_STATUS,
5232 pci_status & (PCI_STATUS_DETECTED_PARITY |
5233 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5234 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5235
5236 /* The infamous DAC f*ckup only happens at boot time */
5237 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005238 void __iomem *ioaddr = tp->mmio_addr;
5239
Joe Perchesbf82c182010-02-09 11:49:50 +00005240 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 tp->cp_cmd &= ~PCIDAC;
5242 RTL_W16(CPlusCmd, tp->cp_cmd);
5243 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 }
5245
françois romieue6de30d2011-01-03 15:08:37 +00005246 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005247
Francois Romieu98ddf982012-01-31 10:47:34 +01005248 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249}
5250
Igor Maravic036dafa2012-03-05 00:01:25 +01005251struct rtl_txc {
5252 int packets;
5253 int bytes;
5254};
5255
Francois Romieuda78dbf2012-01-26 14:18:23 +01005256static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257{
Igor Maravic036dafa2012-03-05 00:01:25 +01005258 struct rtl8169_stats *tx_stats = &tp->tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 unsigned int dirty_tx, tx_left;
Igor Maravic036dafa2012-03-05 00:01:25 +01005260 struct rtl_txc txc = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 dirty_tx = tp->dirty_tx;
5263 smp_rmb();
5264 tx_left = tp->cur_tx - dirty_tx;
5265
5266 while (tx_left > 0) {
5267 unsigned int entry = dirty_tx % NUM_TX_DESC;
5268 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 u32 status;
5270
5271 rmb();
5272 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5273 if (status & DescOwn)
5274 break;
5275
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005276 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5277 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 if (status & LastFrag) {
Igor Maravic036dafa2012-03-05 00:01:25 +01005279 struct sk_buff *skb = tx_skb->skb;
5280
5281 txc.packets++;
5282 txc.bytes += skb->len;
5283 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 tx_skb->skb = NULL;
5285 }
5286 dirty_tx++;
5287 tx_left--;
5288 }
5289
Igor Maravic036dafa2012-03-05 00:01:25 +01005290 u64_stats_update_begin(&tx_stats->syncp);
5291 tx_stats->packets += txc.packets;
5292 tx_stats->bytes += txc.bytes;
5293 u64_stats_update_end(&tx_stats->syncp);
5294
5295 netdev_completed_queue(dev, txc.packets, txc.bytes);
5296
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 if (tp->dirty_tx != dirty_tx) {
5298 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005299 /* Sync with rtl8169_start_xmit:
5300 * - publish dirty_tx ring index (write barrier)
5301 * - refresh cur_tx ring index and queue status (read barrier)
5302 * May the current thread miss the stopped queue condition,
5303 * a racing xmit thread can only have a right view of the
5304 * ring status.
5305 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005306 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 if (netif_queue_stopped(dev) &&
5308 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5309 netif_wake_queue(dev);
5310 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005311 /*
5312 * 8168 hack: TxPoll requests are lost when the Tx packets are
5313 * too close. Let's kick an extra TxPoll request when a burst
5314 * of start_xmit activity is detected (if it is not detected,
5315 * it is slow enough). -- FR
5316 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005317 if (tp->cur_tx != dirty_tx) {
5318 void __iomem *ioaddr = tp->mmio_addr;
5319
Francois Romieud78ae2d2007-08-26 20:08:19 +02005320 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 }
5323}
5324
Francois Romieu126fa4b2005-05-12 20:09:17 -04005325static inline int rtl8169_fragmented_frame(u32 status)
5326{
5327 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5328}
5329
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005330static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 u32 status = opts1 & RxProtoMask;
5333
5334 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005335 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 skb->ip_summed = CHECKSUM_UNNECESSARY;
5337 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005338 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339}
5340
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005341static struct sk_buff *rtl8169_try_rx_copy(void *data,
5342 struct rtl8169_private *tp,
5343 int pkt_size,
5344 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005346 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005347 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005349 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005350 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005351 prefetch(data);
5352 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5353 if (skb)
5354 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005355 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5356
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005357 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358}
5359
Francois Romieuda78dbf2012-01-26 14:18:23 +01005360static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361{
5362 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005363 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 cur_rx = tp->cur_rx;
5366 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005367 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005369 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005371 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 u32 status;
5373
5374 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005375 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376
5377 if (status & DescOwn)
5378 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005379 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005380 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5381 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005382 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005384 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005386 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005387 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005388 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005389 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005390 }
Ben Greear6bbe0212012-02-10 15:04:33 +00005391 if ((status & (RxRUNT | RxCRC)) &&
5392 !(status & (RxRWT | RxFOVF)) &&
5393 (dev->features & NETIF_F_RXALL))
5394 goto process_pkt;
5395
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005396 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005398 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005399 dma_addr_t addr;
5400 int pkt_size;
5401
5402process_pkt:
5403 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005404 if (likely(!(dev->features & NETIF_F_RXFCS)))
5405 pkt_size = (status & 0x00003fff) - 4;
5406 else
5407 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408
Francois Romieu126fa4b2005-05-12 20:09:17 -04005409 /*
5410 * The driver does not support incoming fragmented
5411 * frames. They are seen as a symptom of over-mtu
5412 * sized frames.
5413 */
5414 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005415 dev->stats.rx_dropped++;
5416 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005417 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005418 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005419 }
5420
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005421 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5422 tp, pkt_size, addr);
5423 rtl8169_mark_to_asic(desc, rx_buf_sz);
5424 if (!skb) {
5425 dev->stats.rx_dropped++;
5426 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 }
5428
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005429 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 skb_put(skb, pkt_size);
5431 skb->protocol = eth_type_trans(skb, dev);
5432
Francois Romieu7a8fc772011-03-01 17:18:33 +01005433 rtl8169_rx_vlan_tag(desc, skb);
5434
Francois Romieu56de4142011-03-15 17:29:31 +01005435 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436
Junchang Wang8027aa22012-03-04 23:30:32 +01005437 u64_stats_update_begin(&tp->rx_stats.syncp);
5438 tp->rx_stats.packets++;
5439 tp->rx_stats.bytes += pkt_size;
5440 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005442
5443 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005444 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005445 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5446 desc->opts2 = 0;
5447 cur_rx++;
5448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 }
5450
5451 count = cur_rx - tp->cur_rx;
5452 tp->cur_rx = cur_rx;
5453
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005454 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
5456 return count;
5457}
5458
Francois Romieu07d3f512007-02-21 22:40:46 +01005459static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460{
Francois Romieu07d3f512007-02-21 22:40:46 +01005461 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005464 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005466 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005467 if (status && status != 0xffff) {
5468 status &= RTL_EVENT_NAPI | tp->event_slow;
5469 if (status) {
5470 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00005471
Francois Romieuda78dbf2012-01-26 14:18:23 +01005472 rtl_irq_disable(tp);
5473 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 return IRQ_RETVAL(handled);
5477}
5478
Francois Romieuda78dbf2012-01-26 14:18:23 +01005479/*
5480 * Workqueue context.
5481 */
5482static void rtl_slow_event_work(struct rtl8169_private *tp)
5483{
5484 struct net_device *dev = tp->dev;
5485 u16 status;
5486
5487 status = rtl_get_events(tp) & tp->event_slow;
5488 rtl_ack_events(tp, status);
5489
5490 if (unlikely(status & RxFIFOOver)) {
5491 switch (tp->mac_version) {
5492 /* Work around for rx fifo overflow */
5493 case RTL_GIGA_MAC_VER_11:
5494 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01005495 /* XXX - Hack alert. See rtl_task(). */
5496 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005497 default:
5498 break;
5499 }
5500 }
5501
5502 if (unlikely(status & SYSErr))
5503 rtl8169_pcierr_interrupt(dev);
5504
5505 if (status & LinkChg)
5506 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5507
5508 napi_disable(&tp->napi);
5509 rtl_irq_disable(tp);
5510
5511 napi_enable(&tp->napi);
5512 napi_schedule(&tp->napi);
5513}
5514
Francois Romieu4422bcd2012-01-26 11:23:32 +01005515static void rtl_task(struct work_struct *work)
5516{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005517 static const struct {
5518 int bitnr;
5519 void (*action)(struct rtl8169_private *);
5520 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01005521 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005522 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5523 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5524 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5525 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005526 struct rtl8169_private *tp =
5527 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005528 struct net_device *dev = tp->dev;
5529 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005530
Francois Romieuda78dbf2012-01-26 14:18:23 +01005531 rtl_lock_work(tp);
5532
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005533 if (!netif_running(dev) ||
5534 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005535 goto out_unlock;
5536
5537 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5538 bool pending;
5539
Francois Romieuda78dbf2012-01-26 14:18:23 +01005540 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005541 if (pending)
5542 rtl_work[i].action(tp);
5543 }
5544
5545out_unlock:
5546 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005547}
5548
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005549static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005551 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5552 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005553 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5554 int work_done= 0;
5555 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556
Francois Romieuda78dbf2012-01-26 14:18:23 +01005557 status = rtl_get_events(tp);
5558 rtl_ack_events(tp, status & ~tp->event_slow);
5559
5560 if (status & RTL_EVENT_NAPI_RX)
5561 work_done = rtl_rx(dev, tp, (u32) budget);
5562
5563 if (status & RTL_EVENT_NAPI_TX)
5564 rtl_tx(dev, tp);
5565
5566 if (status & tp->event_slow) {
5567 enable_mask &= ~tp->event_slow;
5568
5569 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
5570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005572 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005573 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005574
Francois Romieuda78dbf2012-01-26 14:18:23 +01005575 rtl_irq_enable(tp, enable_mask);
5576 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 }
5578
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005579 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581
Francois Romieu523a6092008-09-10 22:28:56 +02005582static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5583{
5584 struct rtl8169_private *tp = netdev_priv(dev);
5585
5586 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5587 return;
5588
5589 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5590 RTL_W32(RxMissed, 0);
5591}
5592
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593static void rtl8169_down(struct net_device *dev)
5594{
5595 struct rtl8169_private *tp = netdev_priv(dev);
5596 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
Francois Romieu4876cc12011-03-11 21:07:11 +01005598 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005600 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005601 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602
Hayes Wang92fc43b2011-07-06 15:58:03 +08005603 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005604 /*
5605 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01005606 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
5607 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005608 */
Francois Romieu523a6092008-09-10 22:28:56 +02005609 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005612 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 rtl8169_tx_clear(tp);
5615
5616 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005617
5618 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619}
5620
5621static int rtl8169_close(struct net_device *dev)
5622{
5623 struct rtl8169_private *tp = netdev_priv(dev);
5624 struct pci_dev *pdev = tp->pci_dev;
5625
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005626 pm_runtime_get_sync(&pdev->dev);
5627
Francois Romieucecb5fd2011-04-01 10:21:07 +02005628 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005629 rtl8169_update_counters(dev);
5630
Francois Romieuda78dbf2012-01-26 14:18:23 +01005631 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005632 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005633
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005635 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636
Francois Romieu92a7c4e2012-03-10 10:42:12 +01005637 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005639 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5640 tp->RxPhyAddr);
5641 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5642 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 tp->TxDescArray = NULL;
5644 tp->RxDescArray = NULL;
5645
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005646 pm_runtime_put_sync(&pdev->dev);
5647
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 return 0;
5649}
5650
Francois Romieudc1c00c2012-03-08 10:06:18 +01005651#ifdef CONFIG_NET_POLL_CONTROLLER
5652static void rtl8169_netpoll(struct net_device *dev)
5653{
5654 struct rtl8169_private *tp = netdev_priv(dev);
5655
5656 rtl8169_interrupt(tp->pci_dev->irq, dev);
5657}
5658#endif
5659
Francois Romieudf43ac72012-03-08 09:48:40 +01005660static int rtl_open(struct net_device *dev)
5661{
5662 struct rtl8169_private *tp = netdev_priv(dev);
5663 void __iomem *ioaddr = tp->mmio_addr;
5664 struct pci_dev *pdev = tp->pci_dev;
5665 int retval = -ENOMEM;
5666
5667 pm_runtime_get_sync(&pdev->dev);
5668
5669 /*
5670 * Rx and Tx desscriptors needs 256 bytes alignment.
5671 * dma_alloc_coherent provides more.
5672 */
5673 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
5674 &tp->TxPhyAddr, GFP_KERNEL);
5675 if (!tp->TxDescArray)
5676 goto err_pm_runtime_put;
5677
5678 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
5679 &tp->RxPhyAddr, GFP_KERNEL);
5680 if (!tp->RxDescArray)
5681 goto err_free_tx_0;
5682
5683 retval = rtl8169_init_ring(dev);
5684 if (retval < 0)
5685 goto err_free_rx_1;
5686
5687 INIT_WORK(&tp->wk.work, rtl_task);
5688
5689 smp_mb();
5690
5691 rtl_request_firmware(tp);
5692
Francois Romieu92a7c4e2012-03-10 10:42:12 +01005693 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01005694 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
5695 dev->name, dev);
5696 if (retval < 0)
5697 goto err_release_fw_2;
5698
5699 rtl_lock_work(tp);
5700
5701 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
5702
5703 napi_enable(&tp->napi);
5704
5705 rtl8169_init_phy(dev, tp);
5706
5707 __rtl8169_set_features(dev, dev->features);
5708
5709 rtl_pll_power_up(tp);
5710
5711 rtl_hw_start(dev);
5712
5713 netif_start_queue(dev);
5714
5715 rtl_unlock_work(tp);
5716
5717 tp->saved_wolopts = 0;
5718 pm_runtime_put_noidle(&pdev->dev);
5719
5720 rtl8169_check_link_status(dev, tp, ioaddr);
5721out:
5722 return retval;
5723
5724err_release_fw_2:
5725 rtl_release_firmware(tp);
5726 rtl8169_rx_clear(tp);
5727err_free_rx_1:
5728 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5729 tp->RxPhyAddr);
5730 tp->RxDescArray = NULL;
5731err_free_tx_0:
5732 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5733 tp->TxPhyAddr);
5734 tp->TxDescArray = NULL;
5735err_pm_runtime_put:
5736 pm_runtime_put_noidle(&pdev->dev);
5737 goto out;
5738}
5739
Junchang Wang8027aa22012-03-04 23:30:32 +01005740static struct rtnl_link_stats64 *
5741rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742{
5743 struct rtl8169_private *tp = netdev_priv(dev);
5744 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01005745 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746
Francois Romieuda78dbf2012-01-26 14:18:23 +01005747 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02005748 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02005749
Junchang Wang8027aa22012-03-04 23:30:32 +01005750 do {
5751 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
5752 stats->rx_packets = tp->rx_stats.packets;
5753 stats->rx_bytes = tp->rx_stats.bytes;
5754 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
5755
5756
5757 do {
5758 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
5759 stats->tx_packets = tp->tx_stats.packets;
5760 stats->tx_bytes = tp->tx_stats.bytes;
5761 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
5762
5763 stats->rx_dropped = dev->stats.rx_dropped;
5764 stats->tx_dropped = dev->stats.tx_dropped;
5765 stats->rx_length_errors = dev->stats.rx_length_errors;
5766 stats->rx_errors = dev->stats.rx_errors;
5767 stats->rx_crc_errors = dev->stats.rx_crc_errors;
5768 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
5769 stats->rx_missed_errors = dev->stats.rx_missed_errors;
5770
5771 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772}
5773
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005774static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005775{
françois romieu065c27c2011-01-03 15:08:12 +00005776 struct rtl8169_private *tp = netdev_priv(dev);
5777
Francois Romieu5d06a992006-02-23 00:47:58 +01005778 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005779 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005780
5781 netif_device_detach(dev);
5782 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005783
5784 rtl_lock_work(tp);
5785 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005786 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005787 rtl_unlock_work(tp);
5788
5789 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005790}
Francois Romieu5d06a992006-02-23 00:47:58 +01005791
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005792#ifdef CONFIG_PM
5793
5794static int rtl8169_suspend(struct device *device)
5795{
5796 struct pci_dev *pdev = to_pci_dev(device);
5797 struct net_device *dev = pci_get_drvdata(pdev);
5798
5799 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005800
Francois Romieu5d06a992006-02-23 00:47:58 +01005801 return 0;
5802}
5803
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005804static void __rtl8169_resume(struct net_device *dev)
5805{
françois romieu065c27c2011-01-03 15:08:12 +00005806 struct rtl8169_private *tp = netdev_priv(dev);
5807
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005808 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005809
5810 rtl_pll_power_up(tp);
5811
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005812 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005813
Francois Romieu98ddf982012-01-31 10:47:34 +01005814 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005815}
5816
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005817static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005818{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005819 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005820 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005821 struct rtl8169_private *tp = netdev_priv(dev);
5822
5823 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005824
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005825 if (netif_running(dev))
5826 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005827
Francois Romieu5d06a992006-02-23 00:47:58 +01005828 return 0;
5829}
5830
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005831static int rtl8169_runtime_suspend(struct device *device)
5832{
5833 struct pci_dev *pdev = to_pci_dev(device);
5834 struct net_device *dev = pci_get_drvdata(pdev);
5835 struct rtl8169_private *tp = netdev_priv(dev);
5836
5837 if (!tp->TxDescArray)
5838 return 0;
5839
Francois Romieuda78dbf2012-01-26 14:18:23 +01005840 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005841 tp->saved_wolopts = __rtl8169_get_wol(tp);
5842 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005843 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005844
5845 rtl8169_net_suspend(dev);
5846
5847 return 0;
5848}
5849
5850static int rtl8169_runtime_resume(struct device *device)
5851{
5852 struct pci_dev *pdev = to_pci_dev(device);
5853 struct net_device *dev = pci_get_drvdata(pdev);
5854 struct rtl8169_private *tp = netdev_priv(dev);
5855
5856 if (!tp->TxDescArray)
5857 return 0;
5858
Francois Romieuda78dbf2012-01-26 14:18:23 +01005859 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005860 __rtl8169_set_wol(tp, tp->saved_wolopts);
5861 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005862 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005863
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005864 rtl8169_init_phy(dev, tp);
5865
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005866 __rtl8169_resume(dev);
5867
5868 return 0;
5869}
5870
5871static int rtl8169_runtime_idle(struct device *device)
5872{
5873 struct pci_dev *pdev = to_pci_dev(device);
5874 struct net_device *dev = pci_get_drvdata(pdev);
5875 struct rtl8169_private *tp = netdev_priv(dev);
5876
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005877 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005878}
5879
Alexey Dobriyan47145212009-12-14 18:00:08 -08005880static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005881 .suspend = rtl8169_suspend,
5882 .resume = rtl8169_resume,
5883 .freeze = rtl8169_suspend,
5884 .thaw = rtl8169_resume,
5885 .poweroff = rtl8169_suspend,
5886 .restore = rtl8169_resume,
5887 .runtime_suspend = rtl8169_runtime_suspend,
5888 .runtime_resume = rtl8169_runtime_resume,
5889 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005890};
5891
5892#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5893
5894#else /* !CONFIG_PM */
5895
5896#define RTL8169_PM_OPS NULL
5897
5898#endif /* !CONFIG_PM */
5899
David S. Miller1805b2f2011-10-24 18:18:09 -04005900static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
5901{
5902 void __iomem *ioaddr = tp->mmio_addr;
5903
5904 /* WoL fails with 8168b when the receiver is disabled. */
5905 switch (tp->mac_version) {
5906 case RTL_GIGA_MAC_VER_11:
5907 case RTL_GIGA_MAC_VER_12:
5908 case RTL_GIGA_MAC_VER_17:
5909 pci_clear_master(tp->pci_dev);
5910
5911 RTL_W8(ChipCmd, CmdRxEnb);
5912 /* PCI commit */
5913 RTL_R8(ChipCmd);
5914 break;
5915 default:
5916 break;
5917 }
5918}
5919
Francois Romieu1765f952008-09-13 17:21:40 +02005920static void rtl_shutdown(struct pci_dev *pdev)
5921{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005922 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005923 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00005924 struct device *d = &pdev->dev;
5925
5926 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02005927
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005928 rtl8169_net_suspend(dev);
5929
Francois Romieucecb5fd2011-04-01 10:21:07 +02005930 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005931 rtl_rar_set(tp, dev->perm_addr);
5932
Hayes Wang92fc43b2011-07-06 15:58:03 +08005933 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00005934
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005935 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04005936 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
5937 rtl_wol_suspend_quirk(tp);
5938 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00005939 }
5940
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005941 pci_wake_from_d3(pdev, true);
5942 pci_set_power_state(pdev, PCI_D3hot);
5943 }
françois romieu2a15cd22012-03-06 01:14:12 +00005944
5945 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005946}
Francois Romieu5d06a992006-02-23 00:47:58 +01005947
Francois Romieue27566e2012-03-08 09:54:01 +01005948static void __devexit rtl_remove_one(struct pci_dev *pdev)
5949{
5950 struct net_device *dev = pci_get_drvdata(pdev);
5951 struct rtl8169_private *tp = netdev_priv(dev);
5952
5953 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
5954 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5955 tp->mac_version == RTL_GIGA_MAC_VER_31) {
5956 rtl8168_driver_stop(tp);
5957 }
5958
5959 cancel_work_sync(&tp->wk.work);
5960
5961 unregister_netdev(dev);
5962
5963 rtl_release_firmware(tp);
5964
5965 if (pci_dev_run_wake(pdev))
5966 pm_runtime_get_noresume(&pdev->dev);
5967
5968 /* restore original MAC address */
5969 rtl_rar_set(tp, dev->perm_addr);
5970
5971 rtl_disable_msi(pdev, tp);
5972 rtl8169_release_board(pdev, dev, tp->mmio_addr);
5973 pci_set_drvdata(pdev, NULL);
5974}
5975
Francois Romieufa9c3852012-03-08 10:01:50 +01005976static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01005977 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01005978 .ndo_stop = rtl8169_close,
5979 .ndo_get_stats64 = rtl8169_get_stats64,
5980 .ndo_start_xmit = rtl8169_start_xmit,
5981 .ndo_tx_timeout = rtl8169_tx_timeout,
5982 .ndo_validate_addr = eth_validate_addr,
5983 .ndo_change_mtu = rtl8169_change_mtu,
5984 .ndo_fix_features = rtl8169_fix_features,
5985 .ndo_set_features = rtl8169_set_features,
5986 .ndo_set_mac_address = rtl_set_mac_address,
5987 .ndo_do_ioctl = rtl8169_ioctl,
5988 .ndo_set_rx_mode = rtl_set_rx_mode,
5989#ifdef CONFIG_NET_POLL_CONTROLLER
5990 .ndo_poll_controller = rtl8169_netpoll,
5991#endif
5992
5993};
5994
Francois Romieu31fa8b12012-03-08 10:09:40 +01005995static const struct rtl_cfg_info {
5996 void (*hw_start)(struct net_device *);
5997 unsigned int region;
5998 unsigned int align;
5999 u16 event_slow;
6000 unsigned features;
6001 u8 default_ver;
6002} rtl_cfg_infos [] = {
6003 [RTL_CFG_0] = {
6004 .hw_start = rtl_hw_start_8169,
6005 .region = 1,
6006 .align = 0,
6007 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6008 .features = RTL_FEATURE_GMII,
6009 .default_ver = RTL_GIGA_MAC_VER_01,
6010 },
6011 [RTL_CFG_1] = {
6012 .hw_start = rtl_hw_start_8168,
6013 .region = 2,
6014 .align = 8,
6015 .event_slow = SYSErr | LinkChg | RxOverflow,
6016 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6017 .default_ver = RTL_GIGA_MAC_VER_11,
6018 },
6019 [RTL_CFG_2] = {
6020 .hw_start = rtl_hw_start_8101,
6021 .region = 2,
6022 .align = 8,
6023 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6024 PCSTimeout,
6025 .features = RTL_FEATURE_MSI,
6026 .default_ver = RTL_GIGA_MAC_VER_13,
6027 }
6028};
6029
6030/* Cfg9346_Unlock assumed. */
6031static unsigned rtl_try_msi(struct rtl8169_private *tp,
6032 const struct rtl_cfg_info *cfg)
6033{
6034 void __iomem *ioaddr = tp->mmio_addr;
6035 unsigned msi = 0;
6036 u8 cfg2;
6037
6038 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6039 if (cfg->features & RTL_FEATURE_MSI) {
6040 if (pci_enable_msi(tp->pci_dev)) {
6041 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6042 } else {
6043 cfg2 |= MSIEnable;
6044 msi = RTL_FEATURE_MSI;
6045 }
6046 }
6047 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6048 RTL_W8(Config2, cfg2);
6049 return msi;
6050}
6051
Francois Romieu3b6cf252012-03-08 09:59:04 +01006052static int __devinit
6053rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6054{
6055 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6056 const unsigned int region = cfg->region;
6057 struct rtl8169_private *tp;
6058 struct mii_if_info *mii;
6059 struct net_device *dev;
6060 void __iomem *ioaddr;
6061 int chipset, i;
6062 int rc;
6063
6064 if (netif_msg_drv(&debug)) {
6065 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6066 MODULENAME, RTL8169_VERSION);
6067 }
6068
6069 dev = alloc_etherdev(sizeof (*tp));
6070 if (!dev) {
6071 rc = -ENOMEM;
6072 goto out;
6073 }
6074
6075 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006076 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006077 tp = netdev_priv(dev);
6078 tp->dev = dev;
6079 tp->pci_dev = pdev;
6080 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6081
6082 mii = &tp->mii;
6083 mii->dev = dev;
6084 mii->mdio_read = rtl_mdio_read;
6085 mii->mdio_write = rtl_mdio_write;
6086 mii->phy_id_mask = 0x1f;
6087 mii->reg_num_mask = 0x1f;
6088 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6089
6090 /* disable ASPM completely as that cause random device stop working
6091 * problems as well as full system hangs for some PCIe devices users */
6092 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6093 PCIE_LINK_STATE_CLKPM);
6094
6095 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6096 rc = pci_enable_device(pdev);
6097 if (rc < 0) {
6098 netif_err(tp, probe, dev, "enable failure\n");
6099 goto err_out_free_dev_1;
6100 }
6101
6102 if (pci_set_mwi(pdev) < 0)
6103 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6104
6105 /* make sure PCI base addr 1 is MMIO */
6106 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6107 netif_err(tp, probe, dev,
6108 "region #%d not an MMIO resource, aborting\n",
6109 region);
6110 rc = -ENODEV;
6111 goto err_out_mwi_2;
6112 }
6113
6114 /* check for weird/broken PCI region reporting */
6115 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6116 netif_err(tp, probe, dev,
6117 "Invalid PCI region size(s), aborting\n");
6118 rc = -ENODEV;
6119 goto err_out_mwi_2;
6120 }
6121
6122 rc = pci_request_regions(pdev, MODULENAME);
6123 if (rc < 0) {
6124 netif_err(tp, probe, dev, "could not request regions\n");
6125 goto err_out_mwi_2;
6126 }
6127
6128 tp->cp_cmd = RxChkSum;
6129
6130 if ((sizeof(dma_addr_t) > 4) &&
6131 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6132 tp->cp_cmd |= PCIDAC;
6133 dev->features |= NETIF_F_HIGHDMA;
6134 } else {
6135 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6136 if (rc < 0) {
6137 netif_err(tp, probe, dev, "DMA configuration failed\n");
6138 goto err_out_free_res_3;
6139 }
6140 }
6141
6142 /* ioremap MMIO region */
6143 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6144 if (!ioaddr) {
6145 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6146 rc = -EIO;
6147 goto err_out_free_res_3;
6148 }
6149 tp->mmio_addr = ioaddr;
6150
6151 if (!pci_is_pcie(pdev))
6152 netif_info(tp, probe, dev, "not PCI Express\n");
6153
6154 /* Identify chip attached to board */
6155 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6156
6157 rtl_init_rxcfg(tp);
6158
6159 rtl_irq_disable(tp);
6160
6161 rtl_hw_reset(tp);
6162
6163 rtl_ack_events(tp, 0xffff);
6164
6165 pci_set_master(pdev);
6166
6167 /*
6168 * Pretend we are using VLANs; This bypasses a nasty bug where
6169 * Interrupts stop flowing on high load on 8110SCd controllers.
6170 */
6171 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6172 tp->cp_cmd |= RxVlan;
6173
6174 rtl_init_mdio_ops(tp);
6175 rtl_init_pll_power_ops(tp);
6176 rtl_init_jumbo_ops(tp);
6177
6178 rtl8169_print_mac_version(tp);
6179
6180 chipset = tp->mac_version;
6181 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6182
6183 RTL_W8(Cfg9346, Cfg9346_Unlock);
6184 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6185 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6186 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6187 tp->features |= RTL_FEATURE_WOL;
6188 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6189 tp->features |= RTL_FEATURE_WOL;
6190 tp->features |= rtl_try_msi(tp, cfg);
6191 RTL_W8(Cfg9346, Cfg9346_Lock);
6192
6193 if (rtl_tbi_enabled(tp)) {
6194 tp->set_speed = rtl8169_set_speed_tbi;
6195 tp->get_settings = rtl8169_gset_tbi;
6196 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6197 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6198 tp->link_ok = rtl8169_tbi_link_ok;
6199 tp->do_ioctl = rtl_tbi_ioctl;
6200 } else {
6201 tp->set_speed = rtl8169_set_speed_xmii;
6202 tp->get_settings = rtl8169_gset_xmii;
6203 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6204 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6205 tp->link_ok = rtl8169_xmii_link_ok;
6206 tp->do_ioctl = rtl_xmii_ioctl;
6207 }
6208
6209 mutex_init(&tp->wk.mutex);
6210
6211 /* Get MAC address */
6212 for (i = 0; i < ETH_ALEN; i++)
6213 dev->dev_addr[i] = RTL_R8(MAC0 + i);
6214 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
6215
6216 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6217 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006218
6219 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6220
6221 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6222 * properly for all devices */
6223 dev->features |= NETIF_F_RXCSUM |
6224 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6225
6226 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6227 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6228 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6229 NETIF_F_HIGHDMA;
6230
6231 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6232 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6233 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6234
6235 dev->hw_features |= NETIF_F_RXALL;
6236 dev->hw_features |= NETIF_F_RXFCS;
6237
6238 tp->hw_start = cfg->hw_start;
6239 tp->event_slow = cfg->event_slow;
6240
6241 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6242 ~(RxBOVF | RxFOVF) : ~0;
6243
6244 init_timer(&tp->timer);
6245 tp->timer.data = (unsigned long) dev;
6246 tp->timer.function = rtl8169_phy_timer;
6247
6248 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6249
6250 rc = register_netdev(dev);
6251 if (rc < 0)
6252 goto err_out_msi_4;
6253
6254 pci_set_drvdata(pdev, dev);
6255
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006256 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
6257 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
6258 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006259 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6260 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6261 "tx checksumming: %s]\n",
6262 rtl_chip_infos[chipset].jumbo_max,
6263 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6264 }
6265
6266 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6267 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6268 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6269 rtl8168_driver_start(tp);
6270 }
6271
6272 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6273
6274 if (pci_dev_run_wake(pdev))
6275 pm_runtime_put_noidle(&pdev->dev);
6276
6277 netif_carrier_off(dev);
6278
6279out:
6280 return rc;
6281
6282err_out_msi_4:
6283 rtl_disable_msi(pdev, tp);
6284 iounmap(ioaddr);
6285err_out_free_res_3:
6286 pci_release_regions(pdev);
6287err_out_mwi_2:
6288 pci_clear_mwi(pdev);
6289 pci_disable_device(pdev);
6290err_out_free_dev_1:
6291 free_netdev(dev);
6292 goto out;
6293}
6294
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295static struct pci_driver rtl8169_pci_driver = {
6296 .name = MODULENAME,
6297 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006298 .probe = rtl_init_one,
Francois Romieue27566e2012-03-08 09:54:01 +01006299 .remove = __devexit_p(rtl_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006300 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006301 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302};
6303
Francois Romieu07d3f512007-02-21 22:40:46 +01006304static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305{
Jeff Garzik29917622006-08-19 17:48:59 -04006306 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307}
6308
Francois Romieu07d3f512007-02-21 22:40:46 +01006309static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310{
6311 pci_unregister_driver(&rtl8169_pci_driver);
6312}
6313
6314module_init(rtl8169_init_module);
6315module_exit(rtl8169_cleanup_module);