blob: 227749107789f5846eed8521df4663da652db38c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
3 *
4 * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5 * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6 * Copyright (C) 2004 Sun Microsystems Inc.
Nithin Nayak Sujirb681b652013-01-06 12:51:10 +00007 * Copyright (C) 2005-2013 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000021#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/compiler.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020027#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000029#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/ioport.h>
31#include <linux/pci.h>
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/skbuff.h>
35#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000036#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070038#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070039#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/if_vlan.h>
41#include <linux/ip.h>
42#include <linux/tcp.h>
43#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070044#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020045#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080046#include <linux/firmware.h>
Michael Chanaed93e02012-07-16 16:24:02 +000047#include <linux/hwmon.h>
48#include <linux/hwmon-sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030051#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000053#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000055#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Matt Carlsonbe947302012-12-03 19:36:57 +000057#include <uapi/linux/net_tstamp.h>
58#include <linux/ptp_clock_kernel.h>
59
David S. Miller49b6e95f2007-03-29 01:38:42 -070060#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070062#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#endif
64
Matt Carlson63532392008-11-03 16:49:57 -080065#define BAR_0 0
66#define BAR_2 2
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include "tg3.h"
69
Joe Perches63c3a662011-04-26 08:12:10 +000070/* Functions & macros to verify TG3_FLAGS types */
71
72static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
73{
74 return test_bit(flag, bits);
75}
76
77static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
78{
79 set_bit(flag, bits);
80}
81
82static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
83{
84 clear_bit(flag, bits);
85}
86
87#define tg3_flag(tp, flag) \
88 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
89#define tg3_flag_set(tp, flag) \
90 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
91#define tg3_flag_clear(tp, flag) \
92 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000095#define TG3_MAJ_NUM 3
Nithin Nayak Sujirb681b652013-01-06 12:51:10 +000096#define TG3_MIN_NUM 129
Matt Carlson6867c842010-07-11 09:31:44 +000097#define DRV_MODULE_VERSION \
98 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Nithin Nayak Sujirb681b652013-01-06 12:51:10 +000099#define DRV_MODULE_RELDATE "January 06, 2013"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000101#define RESET_KIND_SHUTDOWN 0
102#define RESET_KIND_INIT 1
103#define RESET_KIND_SUSPEND 2
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#define TG3_DEF_RX_MODE 0
106#define TG3_DEF_TX_MODE 0
107#define TG3_DEF_MSG_ENABLE \
108 (NETIF_MSG_DRV | \
109 NETIF_MSG_PROBE | \
110 NETIF_MSG_LINK | \
111 NETIF_MSG_TIMER | \
112 NETIF_MSG_IFDOWN | \
113 NETIF_MSG_IFUP | \
114 NETIF_MSG_RX_ERR | \
115 NETIF_MSG_TX_ERR)
116
Matt Carlson520b2752011-06-13 13:39:02 +0000117#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* length of time before we decide the hardware is borked,
120 * and dev->tx_timeout() should be called to fix the problem
121 */
Joe Perches63c3a662011-04-26 08:12:10 +0000122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#define TG3_TX_TIMEOUT (5 * HZ)
124
125/* hardware minimum and maximum for a single frame's data payload */
126#define TG3_MIN_MTU 60
127#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000128 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/* These numbers seem to be hard coded in the NIC firmware somehow.
131 * You can't change the ring sizes, but you can change where you place
132 * them in the NIC onboard memory.
133 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000134#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000135 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000136 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000138#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000139 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000140 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#define TG3_DEF_RX_JUMBO_RING_PENDING 100
142
143/* Do not place this n-ring entries value into the tp struct itself,
144 * we really want to expose these constants to GCC so that modulo et
145 * al. operations are done with shifts and masks instead of with
146 * hw multiply/modulo instructions. Another solution would be to
147 * replace things like '% foo' with '& (foo - 1)'.
148 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150#define TG3_TX_RING_SIZE 512
151#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
152
Matt Carlson2c49a442010-09-30 10:34:35 +0000153#define TG3_RX_STD_RING_BYTES(tp) \
154 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
155#define TG3_RX_JMB_RING_BYTES(tp) \
156 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
157#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000158 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
160 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
162
Matt Carlson287be122009-08-28 13:58:46 +0000163#define TG3_DMA_BYTE_ENAB 64
164
165#define TG3_RX_STD_DMA_SZ 1536
166#define TG3_RX_JMB_DMA_SZ 9046
167
168#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
169
170#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
171#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Matt Carlson2c49a442010-09-30 10:34:35 +0000173#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
174 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000175
Matt Carlson2c49a442010-09-30 10:34:35 +0000176#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
177 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000178
Matt Carlsond2757fc2010-04-12 06:58:27 +0000179/* Due to a hardware bug, the 5701 can only DMA to memory addresses
180 * that are at least dword aligned when used in PCIX mode. The driver
181 * works around this bug by double copying the packet. This workaround
182 * is built into the normal double copy length check for efficiency.
183 *
184 * However, the double copy is only necessary on those architectures
185 * where unaligned memory accesses are inefficient. For those architectures
186 * where unaligned memory accesses incur little penalty, we can reintegrate
187 * the 5701 in the normal rx path. Doing so saves a device structure
188 * dereference by hardcoding the double copy threshold in place.
189 */
190#define TG3_RX_COPY_THRESHOLD 256
191#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
192 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
193#else
194 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
195#endif
196
Matt Carlson81389f52011-08-31 11:44:49 +0000197#if (NET_IP_ALIGN != 0)
198#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
199#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000200#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000201#endif
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000204#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000205#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000206#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Matt Carlsonad829262008-11-21 17:16:16 -0800208#define TG3_RAW_IP_ALIGN 2
209
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000210#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000211#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000212
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800213#define FIRMWARE_TG3 "tigon/tg3.bin"
214#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
215#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
216
Bill Pemberton229b1ad2012-12-03 09:22:59 -0500217static char version[] =
Joe Perches05dbe002010-02-17 19:44:19 +0000218 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
221MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
222MODULE_LICENSE("GPL");
223MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800224MODULE_FIRMWARE(FIRMWARE_TG3);
225MODULE_FIRMWARE(FIRMWARE_TG3TSO);
226MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
229module_param(tg3_debug, int, 0);
230MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
231
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000232#define TG3_DRV_DATA_FLAG_10_100_ONLY 0x0001
233#define TG3_DRV_DATA_FLAG_5705_10_100 0x0002
234
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000235static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901),
255 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
256 TG3_DRV_DATA_FLAG_5705_10_100},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2),
258 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
259 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F),
262 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
263 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F),
269 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F),
275 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000283 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5787M,
284 PCI_VENDOR_ID_LENOVO,
285 TG3PCI_SUBDEVICE_ID_LENOVO_5787M),
286 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F),
289 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
291 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
292 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700302 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
303 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800304 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
305 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000306 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
307 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000308 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
309 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_A),
310 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
311 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
312 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_B),
313 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson321d32a2008-11-21 17:22:19 -0800314 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
315 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000316 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790),
317 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000318 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000319 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
Michael Chan79d49692012-11-05 14:26:29 +0000320 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717_C)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000321 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000322 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
323 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
324 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
325 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000326 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791),
327 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
328 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795),
329 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson302b5002010-06-05 17:24:38 +0000330 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000331 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Greg KH02eca3f2012-07-12 15:39:44 +0000332 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
Michael Chanc86a8562013-01-06 12:51:08 +0000333 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5762)},
334 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5725)},
335 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5727)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700336 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
337 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
338 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
339 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
340 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
341 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
342 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000343 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700344 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345};
346
347MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
348
Andreas Mohr50da8592006-08-14 23:54:30 -0700349static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000351} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 { "rx_octets" },
353 { "rx_fragments" },
354 { "rx_ucast_packets" },
355 { "rx_mcast_packets" },
356 { "rx_bcast_packets" },
357 { "rx_fcs_errors" },
358 { "rx_align_errors" },
359 { "rx_xon_pause_rcvd" },
360 { "rx_xoff_pause_rcvd" },
361 { "rx_mac_ctrl_rcvd" },
362 { "rx_xoff_entered" },
363 { "rx_frame_too_long_errors" },
364 { "rx_jabbers" },
365 { "rx_undersize_packets" },
366 { "rx_in_length_errors" },
367 { "rx_out_length_errors" },
368 { "rx_64_or_less_octet_packets" },
369 { "rx_65_to_127_octet_packets" },
370 { "rx_128_to_255_octet_packets" },
371 { "rx_256_to_511_octet_packets" },
372 { "rx_512_to_1023_octet_packets" },
373 { "rx_1024_to_1522_octet_packets" },
374 { "rx_1523_to_2047_octet_packets" },
375 { "rx_2048_to_4095_octet_packets" },
376 { "rx_4096_to_8191_octet_packets" },
377 { "rx_8192_to_9022_octet_packets" },
378
379 { "tx_octets" },
380 { "tx_collisions" },
381
382 { "tx_xon_sent" },
383 { "tx_xoff_sent" },
384 { "tx_flow_control" },
385 { "tx_mac_errors" },
386 { "tx_single_collisions" },
387 { "tx_mult_collisions" },
388 { "tx_deferred" },
389 { "tx_excessive_collisions" },
390 { "tx_late_collisions" },
391 { "tx_collide_2times" },
392 { "tx_collide_3times" },
393 { "tx_collide_4times" },
394 { "tx_collide_5times" },
395 { "tx_collide_6times" },
396 { "tx_collide_7times" },
397 { "tx_collide_8times" },
398 { "tx_collide_9times" },
399 { "tx_collide_10times" },
400 { "tx_collide_11times" },
401 { "tx_collide_12times" },
402 { "tx_collide_13times" },
403 { "tx_collide_14times" },
404 { "tx_collide_15times" },
405 { "tx_ucast_packets" },
406 { "tx_mcast_packets" },
407 { "tx_bcast_packets" },
408 { "tx_carrier_sense_errors" },
409 { "tx_discards" },
410 { "tx_errors" },
411
412 { "dma_writeq_full" },
413 { "dma_write_prioq_full" },
414 { "rxbds_empty" },
415 { "rx_discards" },
416 { "rx_errors" },
417 { "rx_threshold_hit" },
418
419 { "dma_readq_full" },
420 { "dma_read_prioq_full" },
421 { "tx_comp_queue_full" },
422
423 { "ring_set_send_prod_index" },
424 { "ring_status_update" },
425 { "nic_irqs" },
426 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000427 { "nic_tx_threshold_hit" },
428
429 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430};
431
Matt Carlson48fa55a2011-04-13 11:05:06 +0000432#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000433#define TG3_NVRAM_TEST 0
434#define TG3_LINK_TEST 1
435#define TG3_REGISTER_TEST 2
436#define TG3_MEMORY_TEST 3
437#define TG3_MAC_LOOPB_TEST 4
438#define TG3_PHY_LOOPB_TEST 5
439#define TG3_EXT_LOOPB_TEST 6
440#define TG3_INTERRUPT_TEST 7
Matt Carlson48fa55a2011-04-13 11:05:06 +0000441
442
Andreas Mohr50da8592006-08-14 23:54:30 -0700443static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700444 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000445} ethtool_test_keys[] = {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000446 [TG3_NVRAM_TEST] = { "nvram test (online) " },
447 [TG3_LINK_TEST] = { "link test (online) " },
448 [TG3_REGISTER_TEST] = { "register test (offline)" },
449 [TG3_MEMORY_TEST] = { "memory test (offline)" },
450 [TG3_MAC_LOOPB_TEST] = { "mac loopback test (offline)" },
451 [TG3_PHY_LOOPB_TEST] = { "phy loopback test (offline)" },
452 [TG3_EXT_LOOPB_TEST] = { "ext loopback test (offline)" },
453 [TG3_INTERRUPT_TEST] = { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700454};
455
Matt Carlson48fa55a2011-04-13 11:05:06 +0000456#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
457
458
Michael Chanb401e9e2005-12-19 16:27:04 -0800459static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
460{
461 writel(val, tp->regs + off);
462}
463
464static u32 tg3_read32(struct tg3 *tp, u32 off)
465{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000466 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800467}
468
Matt Carlson0d3031d2007-10-10 18:02:43 -0700469static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
470{
471 writel(val, tp->aperegs + off);
472}
473
474static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
475{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000476 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
480{
Michael Chan68929142005-08-09 20:17:14 -0700481 unsigned long flags;
482
483 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700484 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
485 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700486 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700487}
488
489static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
490{
491 writel(val, tp->regs + off);
492 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Michael Chan68929142005-08-09 20:17:14 -0700495static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
496{
497 unsigned long flags;
498 u32 val;
499
500 spin_lock_irqsave(&tp->indirect_lock, flags);
501 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
502 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
503 spin_unlock_irqrestore(&tp->indirect_lock, flags);
504 return val;
505}
506
507static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
508{
509 unsigned long flags;
510
511 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
512 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
513 TG3_64BIT_REG_LOW, val);
514 return;
515 }
Matt Carlson66711e662009-11-13 13:03:49 +0000516 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700517 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
518 TG3_64BIT_REG_LOW, val);
519 return;
520 }
521
522 spin_lock_irqsave(&tp->indirect_lock, flags);
523 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
524 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
525 spin_unlock_irqrestore(&tp->indirect_lock, flags);
526
527 /* In indirect mode when disabling interrupts, we also need
528 * to clear the interrupt bit in the GRC local ctrl register.
529 */
530 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
531 (val == 0x1)) {
532 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
533 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
534 }
535}
536
537static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
538{
539 unsigned long flags;
540 u32 val;
541
542 spin_lock_irqsave(&tp->indirect_lock, flags);
543 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
544 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
545 spin_unlock_irqrestore(&tp->indirect_lock, flags);
546 return val;
547}
548
Michael Chanb401e9e2005-12-19 16:27:04 -0800549/* usec_wait specifies the wait time in usec when writing to certain registers
550 * where it is unsafe to read back the register without some delay.
551 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
552 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
553 */
554static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Joe Perches63c3a662011-04-26 08:12:10 +0000556 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800557 /* Non-posted methods */
558 tp->write32(tp, off, val);
559 else {
560 /* Posted method */
561 tg3_write32(tp, off, val);
562 if (usec_wait)
563 udelay(usec_wait);
564 tp->read32(tp, off);
565 }
566 /* Wait again after the read for the posted method to guarantee that
567 * the wait time is met.
568 */
569 if (usec_wait)
570 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Michael Chan09ee9292005-08-09 20:17:00 -0700573static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
574{
575 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000576 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700577 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700578}
579
Michael Chan20094932005-08-09 20:16:32 -0700580static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 void __iomem *mbox = tp->regs + off;
583 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000584 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000586 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 readl(mbox);
588}
589
Michael Chanb5d37722006-09-27 16:06:21 -0700590static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
591{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000592 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700593}
594
595static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
596{
597 writel(val, tp->regs + off + GRCMBOX_BASE);
598}
599
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000600#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700601#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000602#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
603#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
604#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700605
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000606#define tw32(reg, val) tp->write32(tp, reg, val)
607#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
608#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
609#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
612{
Michael Chan68929142005-08-09 20:17:14 -0700613 unsigned long flags;
614
Matt Carlson6ff6f812011-05-19 12:12:54 +0000615 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700616 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
617 return;
618
Michael Chan68929142005-08-09 20:17:14 -0700619 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000620 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700621 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
622 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Michael Chanbbadf502006-04-06 21:46:34 -0700624 /* Always leave this as zero. */
625 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
626 } else {
627 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
628 tw32_f(TG3PCI_MEM_WIN_DATA, val);
629
630 /* Always leave this as zero. */
631 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
632 }
Michael Chan68929142005-08-09 20:17:14 -0700633 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
636static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
637{
Michael Chan68929142005-08-09 20:17:14 -0700638 unsigned long flags;
639
Matt Carlson6ff6f812011-05-19 12:12:54 +0000640 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700641 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
642 *val = 0;
643 return;
644 }
645
Michael Chan68929142005-08-09 20:17:14 -0700646 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000647 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700648 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
649 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Michael Chanbbadf502006-04-06 21:46:34 -0700651 /* Always leave this as zero. */
652 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
653 } else {
654 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
655 *val = tr32(TG3PCI_MEM_WIN_DATA);
656
657 /* Always leave this as zero. */
658 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
659 }
Michael Chan68929142005-08-09 20:17:14 -0700660 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Matt Carlson0d3031d2007-10-10 18:02:43 -0700663static void tg3_ape_lock_init(struct tg3 *tp)
664{
665 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000666 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000667
668 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
669 regbase = TG3_APE_LOCK_GRANT;
670 else
671 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700672
673 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000674 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
675 switch (i) {
676 case TG3_APE_LOCK_PHY0:
677 case TG3_APE_LOCK_PHY1:
678 case TG3_APE_LOCK_PHY2:
679 case TG3_APE_LOCK_PHY3:
680 bit = APE_LOCK_GRANT_DRIVER;
681 break;
682 default:
683 if (!tp->pci_fn)
684 bit = APE_LOCK_GRANT_DRIVER;
685 else
686 bit = 1 << tp->pci_fn;
687 }
688 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000689 }
690
Matt Carlson0d3031d2007-10-10 18:02:43 -0700691}
692
693static int tg3_ape_lock(struct tg3 *tp, int locknum)
694{
695 int i, off;
696 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000697 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700698
Joe Perches63c3a662011-04-26 08:12:10 +0000699 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700700 return 0;
701
702 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000703 case TG3_APE_LOCK_GPIO:
704 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
705 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000706 case TG3_APE_LOCK_GRC:
707 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000708 if (!tp->pci_fn)
709 bit = APE_LOCK_REQ_DRIVER;
710 else
711 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000712 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000713 case TG3_APE_LOCK_PHY0:
714 case TG3_APE_LOCK_PHY1:
715 case TG3_APE_LOCK_PHY2:
716 case TG3_APE_LOCK_PHY3:
717 bit = APE_LOCK_REQ_DRIVER;
718 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000719 default:
720 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700721 }
722
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000723 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
724 req = TG3_APE_LOCK_REQ;
725 gnt = TG3_APE_LOCK_GRANT;
726 } else {
727 req = TG3_APE_PER_LOCK_REQ;
728 gnt = TG3_APE_PER_LOCK_GRANT;
729 }
730
Matt Carlson0d3031d2007-10-10 18:02:43 -0700731 off = 4 * locknum;
732
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000733 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700734
735 /* Wait for up to 1 millisecond to acquire lock. */
736 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000737 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000738 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700739 break;
740 udelay(10);
741 }
742
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000743 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700744 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000745 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700746 ret = -EBUSY;
747 }
748
749 return ret;
750}
751
752static void tg3_ape_unlock(struct tg3 *tp, int locknum)
753{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000754 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700755
Joe Perches63c3a662011-04-26 08:12:10 +0000756 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700757 return;
758
759 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000760 case TG3_APE_LOCK_GPIO:
761 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
762 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000763 case TG3_APE_LOCK_GRC:
764 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000765 if (!tp->pci_fn)
766 bit = APE_LOCK_GRANT_DRIVER;
767 else
768 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000769 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000770 case TG3_APE_LOCK_PHY0:
771 case TG3_APE_LOCK_PHY1:
772 case TG3_APE_LOCK_PHY2:
773 case TG3_APE_LOCK_PHY3:
774 bit = APE_LOCK_GRANT_DRIVER;
775 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000776 default:
777 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700778 }
779
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000780 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
781 gnt = TG3_APE_LOCK_GRANT;
782 else
783 gnt = TG3_APE_PER_LOCK_GRANT;
784
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000785 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700786}
787
Matt Carlsonb65a3722012-07-16 16:24:00 +0000788static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000789{
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000790 u32 apedata;
791
Matt Carlsonb65a3722012-07-16 16:24:00 +0000792 while (timeout_us) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000793 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
Matt Carlsonb65a3722012-07-16 16:24:00 +0000794 return -EBUSY;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000795
796 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000797 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
798 break;
799
Matt Carlsonb65a3722012-07-16 16:24:00 +0000800 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
801
802 udelay(10);
803 timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000804 }
805
Matt Carlsonb65a3722012-07-16 16:24:00 +0000806 return timeout_us ? 0 : -EBUSY;
807}
808
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000809static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
810{
811 u32 i, apedata;
812
813 for (i = 0; i < timeout_us / 10; i++) {
814 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
815
816 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
817 break;
818
819 udelay(10);
820 }
821
822 return i == timeout_us / 10;
823}
824
Michael Chan86449942012-10-02 20:31:14 -0700825static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
826 u32 len)
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000827{
828 int err;
829 u32 i, bufoff, msgoff, maxlen, apedata;
830
831 if (!tg3_flag(tp, APE_HAS_NCSI))
832 return 0;
833
834 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
835 if (apedata != APE_SEG_SIG_MAGIC)
836 return -ENODEV;
837
838 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
839 if (!(apedata & APE_FW_STATUS_READY))
840 return -EAGAIN;
841
842 bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
843 TG3_APE_SHMEM_BASE;
844 msgoff = bufoff + 2 * sizeof(u32);
845 maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
846
847 while (len) {
848 u32 length;
849
850 /* Cap xfer sizes to scratchpad limits. */
851 length = (len > maxlen) ? maxlen : len;
852 len -= length;
853
854 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
855 if (!(apedata & APE_FW_STATUS_READY))
856 return -EAGAIN;
857
858 /* Wait for up to 1 msec for APE to service previous event. */
859 err = tg3_ape_event_lock(tp, 1000);
860 if (err)
861 return err;
862
863 apedata = APE_EVENT_STATUS_DRIVER_EVNT |
864 APE_EVENT_STATUS_SCRTCHPD_READ |
865 APE_EVENT_STATUS_EVENT_PENDING;
866 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
867
868 tg3_ape_write32(tp, bufoff, base_off);
869 tg3_ape_write32(tp, bufoff + sizeof(u32), length);
870
871 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
872 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
873
874 base_off += length;
875
876 if (tg3_ape_wait_for_event(tp, 30000))
877 return -EAGAIN;
878
879 for (i = 0; length; i += 4, length -= 4) {
880 u32 val = tg3_ape_read32(tp, msgoff + i);
881 memcpy(data, &val, sizeof(u32));
882 data++;
883 }
884 }
885
886 return 0;
887}
888
Matt Carlsonb65a3722012-07-16 16:24:00 +0000889static int tg3_ape_send_event(struct tg3 *tp, u32 event)
890{
891 int err;
892 u32 apedata;
893
894 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
895 if (apedata != APE_SEG_SIG_MAGIC)
896 return -EAGAIN;
897
898 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
899 if (!(apedata & APE_FW_STATUS_READY))
900 return -EAGAIN;
901
902 /* Wait for up to 1 millisecond for APE to service previous event. */
903 err = tg3_ape_event_lock(tp, 1000);
904 if (err)
905 return err;
906
907 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
908 event | APE_EVENT_STATUS_EVENT_PENDING);
909
910 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
911 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
912
913 return 0;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000914}
915
916static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
917{
918 u32 event;
919 u32 apedata;
920
921 if (!tg3_flag(tp, ENABLE_APE))
922 return;
923
924 switch (kind) {
925 case RESET_KIND_INIT:
926 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
927 APE_HOST_SEG_SIG_MAGIC);
928 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
929 APE_HOST_SEG_LEN_MAGIC);
930 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
931 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
932 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
933 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
934 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
935 APE_HOST_BEHAV_NO_PHYLOCK);
936 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
937 TG3_APE_HOST_DRVR_STATE_START);
938
939 event = APE_EVENT_STATUS_STATE_START;
940 break;
941 case RESET_KIND_SHUTDOWN:
942 /* With the interface we are currently using,
943 * APE does not track driver state. Wiping
944 * out the HOST SEGMENT SIGNATURE forces
945 * the APE to assume OS absent status.
946 */
947 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
948
949 if (device_may_wakeup(&tp->pdev->dev) &&
950 tg3_flag(tp, WOL_ENABLE)) {
951 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
952 TG3_APE_HOST_WOL_SPEED_AUTO);
953 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
954 } else
955 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
956
957 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
958
959 event = APE_EVENT_STATUS_STATE_UNLOAD;
960 break;
961 case RESET_KIND_SUSPEND:
962 event = APE_EVENT_STATUS_STATE_SUSPEND;
963 break;
964 default:
965 return;
966 }
967
968 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
969
970 tg3_ape_send_event(tp, event);
971}
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973static void tg3_disable_ints(struct tg3 *tp)
974{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000975 int i;
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 tw32(TG3PCI_MISC_HOST_CTRL,
978 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000979 for (i = 0; i < tp->irq_max; i++)
980 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983static void tg3_enable_ints(struct tg3 *tp)
984{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000985 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000986
Michael Chanbbe832c2005-06-24 20:20:04 -0700987 tp->irq_sync = 0;
988 wmb();
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 tw32(TG3PCI_MISC_HOST_CTRL,
991 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000992
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000993 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000994 for (i = 0; i < tp->irq_cnt; i++) {
995 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000996
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000997 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000998 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000999 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
1000
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001001 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001002 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001003
1004 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +00001005 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001006 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
1007 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
1008 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001009 tw32(HOSTCC_MODE, tp->coal_now);
1010
1011 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
Matt Carlson17375d22009-08-28 14:02:18 +00001014static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -07001015{
Matt Carlson17375d22009-08-28 14:02:18 +00001016 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00001017 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -07001018 unsigned int work_exists = 0;
1019
1020 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00001021 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -07001022 if (sblk->status & SD_STATUS_LINK_CHG)
1023 work_exists = 1;
1024 }
Matt Carlsonf891ea12012-04-24 13:37:01 +00001025
1026 /* check for TX work to do */
1027 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
1028 work_exists = 1;
1029
1030 /* check for RX work to do */
1031 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00001032 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -07001033 work_exists = 1;
1034
1035 return work_exists;
1036}
1037
Matt Carlson17375d22009-08-28 14:02:18 +00001038/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -07001039 * similar to tg3_enable_ints, but it accurately determines whether there
1040 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001041 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 */
Matt Carlson17375d22009-08-28 14:02:18 +00001043static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Matt Carlson17375d22009-08-28 14:02:18 +00001045 struct tg3 *tp = tnapi->tp;
1046
Matt Carlson898a56f2009-08-28 14:02:40 +00001047 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 mmiowb();
1049
David S. Millerfac9b832005-05-18 22:46:34 -07001050 /* When doing tagged status, this work check is unnecessary.
1051 * The last_tag we write above tells the chip which piece of
1052 * work we've completed.
1053 */
Joe Perches63c3a662011-04-26 08:12:10 +00001054 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -07001055 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00001056 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059static void tg3_switch_clocks(struct tg3 *tp)
1060{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001061 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 u32 orig_clock_ctrl;
1063
Joe Perches63c3a662011-04-26 08:12:10 +00001064 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07001065 return;
1066
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001067 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 orig_clock_ctrl = clock_ctrl;
1070 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
1071 CLOCK_CTRL_CLKRUN_OENABLE |
1072 0x1f);
1073 tp->pci_clock_ctrl = clock_ctrl;
1074
Joe Perches63c3a662011-04-26 08:12:10 +00001075 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001077 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1078 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001081 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1082 clock_ctrl |
1083 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
1084 40);
1085 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1086 clock_ctrl | (CLOCK_CTRL_ALTCLK),
1087 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001089 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
1092#define PHY_BUSY_LOOPS 5000
1093
1094static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1095{
1096 u32 frame_val;
1097 unsigned int loops;
1098 int ret;
1099
1100 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1101 tw32_f(MAC_MI_MODE,
1102 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1103 udelay(80);
1104 }
1105
Michael Chan8151ad52012-07-29 19:15:41 +00001106 tg3_ape_lock(tp, tp->phy_ape_lock);
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 *val = 0x0;
1109
Matt Carlson882e9792009-09-01 13:21:36 +00001110 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 MI_COM_PHY_ADDR_MASK);
1112 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1113 MI_COM_REG_ADDR_MASK);
1114 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 tw32_f(MAC_MI_COM, frame_val);
1117
1118 loops = PHY_BUSY_LOOPS;
1119 while (loops != 0) {
1120 udelay(10);
1121 frame_val = tr32(MAC_MI_COM);
1122
1123 if ((frame_val & MI_COM_BUSY) == 0) {
1124 udelay(5);
1125 frame_val = tr32(MAC_MI_COM);
1126 break;
1127 }
1128 loops -= 1;
1129 }
1130
1131 ret = -EBUSY;
1132 if (loops != 0) {
1133 *val = frame_val & MI_COM_DATA_MASK;
1134 ret = 0;
1135 }
1136
1137 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1138 tw32_f(MAC_MI_MODE, tp->mi_mode);
1139 udelay(80);
1140 }
1141
Michael Chan8151ad52012-07-29 19:15:41 +00001142 tg3_ape_unlock(tp, tp->phy_ape_lock);
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return ret;
1145}
1146
1147static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1148{
1149 u32 frame_val;
1150 unsigned int loops;
1151 int ret;
1152
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001153 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001154 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001155 return 0;
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1158 tw32_f(MAC_MI_MODE,
1159 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1160 udelay(80);
1161 }
1162
Michael Chan8151ad52012-07-29 19:15:41 +00001163 tg3_ape_lock(tp, tp->phy_ape_lock);
1164
Matt Carlson882e9792009-09-01 13:21:36 +00001165 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 MI_COM_PHY_ADDR_MASK);
1167 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1168 MI_COM_REG_ADDR_MASK);
1169 frame_val |= (val & MI_COM_DATA_MASK);
1170 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 tw32_f(MAC_MI_COM, frame_val);
1173
1174 loops = PHY_BUSY_LOOPS;
1175 while (loops != 0) {
1176 udelay(10);
1177 frame_val = tr32(MAC_MI_COM);
1178 if ((frame_val & MI_COM_BUSY) == 0) {
1179 udelay(5);
1180 frame_val = tr32(MAC_MI_COM);
1181 break;
1182 }
1183 loops -= 1;
1184 }
1185
1186 ret = -EBUSY;
1187 if (loops != 0)
1188 ret = 0;
1189
1190 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1191 tw32_f(MAC_MI_MODE, tp->mi_mode);
1192 udelay(80);
1193 }
1194
Michael Chan8151ad52012-07-29 19:15:41 +00001195 tg3_ape_unlock(tp, tp->phy_ape_lock);
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return ret;
1198}
1199
Matt Carlsonb0988c12011-04-20 07:57:39 +00001200static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1201{
1202 int err;
1203
1204 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1205 if (err)
1206 goto done;
1207
1208 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1209 if (err)
1210 goto done;
1211
1212 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1213 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1214 if (err)
1215 goto done;
1216
1217 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1218
1219done:
1220 return err;
1221}
1222
1223static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1224{
1225 int err;
1226
1227 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1228 if (err)
1229 goto done;
1230
1231 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1232 if (err)
1233 goto done;
1234
1235 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1236 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1237 if (err)
1238 goto done;
1239
1240 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1241
1242done:
1243 return err;
1244}
1245
1246static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1247{
1248 int err;
1249
1250 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1251 if (!err)
1252 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1253
1254 return err;
1255}
1256
1257static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1258{
1259 int err;
1260
1261 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1262 if (!err)
1263 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1264
1265 return err;
1266}
1267
Matt Carlson15ee95c2011-04-20 07:57:40 +00001268static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1269{
1270 int err;
1271
1272 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1273 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1274 MII_TG3_AUXCTL_SHDWSEL_MISC);
1275 if (!err)
1276 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1277
1278 return err;
1279}
1280
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001281static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1282{
1283 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1284 set |= MII_TG3_AUXCTL_MISC_WREN;
1285
1286 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1287}
1288
Matt Carlson1d36ba42011-04-20 07:57:42 +00001289#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
1290 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1291 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1292 MII_TG3_AUXCTL_ACTL_TX_6DB)
1293
1294#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1295 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1296 MII_TG3_AUXCTL_ACTL_TX_6DB);
1297
Matt Carlson95e28692008-05-25 23:44:14 -07001298static int tg3_bmcr_reset(struct tg3 *tp)
1299{
1300 u32 phy_control;
1301 int limit, err;
1302
1303 /* OK, reset it, and poll the BMCR_RESET bit until it
1304 * clears or we time out.
1305 */
1306 phy_control = BMCR_RESET;
1307 err = tg3_writephy(tp, MII_BMCR, phy_control);
1308 if (err != 0)
1309 return -EBUSY;
1310
1311 limit = 5000;
1312 while (limit--) {
1313 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1314 if (err != 0)
1315 return -EBUSY;
1316
1317 if ((phy_control & BMCR_RESET) == 0) {
1318 udelay(40);
1319 break;
1320 }
1321 udelay(10);
1322 }
Roel Kluind4675b52009-02-12 16:33:27 -08001323 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001324 return -EBUSY;
1325
1326 return 0;
1327}
1328
Matt Carlson158d7ab2008-05-29 01:37:54 -07001329static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1330{
Francois Romieu3d165432009-01-19 16:56:50 -08001331 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001332 u32 val;
1333
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001334 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001335
1336 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001337 val = -EIO;
1338
1339 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001340
1341 return val;
1342}
1343
1344static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1345{
Francois Romieu3d165432009-01-19 16:56:50 -08001346 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001347 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001348
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001349 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001350
1351 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001352 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001353
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001354 spin_unlock_bh(&tp->lock);
1355
1356 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001357}
1358
1359static int tg3_mdio_reset(struct mii_bus *bp)
1360{
1361 return 0;
1362}
1363
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001364static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001365{
1366 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001367 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001368
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001369 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001370 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001371 case PHY_ID_BCM50610:
1372 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001373 val = MAC_PHYCFG2_50610_LED_MODES;
1374 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001375 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001376 val = MAC_PHYCFG2_AC131_LED_MODES;
1377 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001378 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001379 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1380 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001381 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001382 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1383 break;
1384 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001385 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001386 }
1387
1388 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1389 tw32(MAC_PHYCFG2, val);
1390
1391 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001392 val &= ~(MAC_PHYCFG1_RGMII_INT |
1393 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1394 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001395 tw32(MAC_PHYCFG1, val);
1396
1397 return;
1398 }
1399
Joe Perches63c3a662011-04-26 08:12:10 +00001400 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001401 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1402 MAC_PHYCFG2_FMODE_MASK_MASK |
1403 MAC_PHYCFG2_GMODE_MASK_MASK |
1404 MAC_PHYCFG2_ACT_MASK_MASK |
1405 MAC_PHYCFG2_QUAL_MASK_MASK |
1406 MAC_PHYCFG2_INBAND_ENABLE;
1407
1408 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001409
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001410 val = tr32(MAC_PHYCFG1);
1411 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1412 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001413 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1414 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001415 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001416 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001417 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1418 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001419 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1420 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1421 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001422
Matt Carlsona9daf362008-05-25 23:49:44 -07001423 val = tr32(MAC_EXT_RGMII_MODE);
1424 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1425 MAC_RGMII_MODE_RX_QUALITY |
1426 MAC_RGMII_MODE_RX_ACTIVITY |
1427 MAC_RGMII_MODE_RX_ENG_DET |
1428 MAC_RGMII_MODE_TX_ENABLE |
1429 MAC_RGMII_MODE_TX_LOWPWR |
1430 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001431 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1432 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001433 val |= MAC_RGMII_MODE_RX_INT_B |
1434 MAC_RGMII_MODE_RX_QUALITY |
1435 MAC_RGMII_MODE_RX_ACTIVITY |
1436 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001437 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001438 val |= MAC_RGMII_MODE_TX_ENABLE |
1439 MAC_RGMII_MODE_TX_LOWPWR |
1440 MAC_RGMII_MODE_TX_RESET;
1441 }
1442 tw32(MAC_EXT_RGMII_MODE, val);
1443}
1444
Matt Carlson158d7ab2008-05-29 01:37:54 -07001445static void tg3_mdio_start(struct tg3 *tp)
1446{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001447 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1448 tw32_f(MAC_MI_MODE, tp->mi_mode);
1449 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001450
Joe Perches63c3a662011-04-26 08:12:10 +00001451 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001452 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1453 tg3_mdio_config_5785(tp);
1454}
1455
1456static int tg3_mdio_init(struct tg3 *tp)
1457{
1458 int i;
1459 u32 reg;
1460 struct phy_device *phydev;
1461
Joe Perches63c3a662011-04-26 08:12:10 +00001462 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001463 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001464
Matt Carlson69f11c92011-07-13 09:27:30 +00001465 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001466
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001467 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1468 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1469 else
1470 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1471 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001472 if (is_serdes)
1473 tp->phy_addr += 7;
1474 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001475 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001476
Matt Carlson158d7ab2008-05-29 01:37:54 -07001477 tg3_mdio_start(tp);
1478
Joe Perches63c3a662011-04-26 08:12:10 +00001479 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001480 return 0;
1481
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001482 tp->mdio_bus = mdiobus_alloc();
1483 if (tp->mdio_bus == NULL)
1484 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001485
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001486 tp->mdio_bus->name = "tg3 mdio bus";
1487 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001488 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001489 tp->mdio_bus->priv = tp;
1490 tp->mdio_bus->parent = &tp->pdev->dev;
1491 tp->mdio_bus->read = &tg3_mdio_read;
1492 tp->mdio_bus->write = &tg3_mdio_write;
1493 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001494 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001495 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001496
1497 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001498 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001499
1500 /* The bus registration will look for all the PHYs on the mdio bus.
1501 * Unfortunately, it does not ensure the PHY is powered up before
1502 * accessing the PHY ID registers. A chip reset is the
1503 * quickest way to bring the device back to an operational state..
1504 */
1505 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1506 tg3_bmcr_reset(tp);
1507
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001508 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001509 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001510 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001511 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001512 return i;
1513 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001514
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001515 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001516
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001517 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001518 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001519 mdiobus_unregister(tp->mdio_bus);
1520 mdiobus_free(tp->mdio_bus);
1521 return -ENODEV;
1522 }
1523
1524 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001525 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001526 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001527 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001528 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001529 case PHY_ID_BCM50610:
1530 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001531 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001532 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001533 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001534 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001535 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001536 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001537 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001538 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001539 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001540 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001541 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001542 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001543 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001544 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001545 case PHY_ID_RTL8201E:
1546 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001547 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001548 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001549 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001550 break;
1551 }
1552
Joe Perches63c3a662011-04-26 08:12:10 +00001553 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001554
1555 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1556 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001557
1558 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001559}
1560
1561static void tg3_mdio_fini(struct tg3 *tp)
1562{
Joe Perches63c3a662011-04-26 08:12:10 +00001563 if (tg3_flag(tp, MDIOBUS_INITED)) {
1564 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001565 mdiobus_unregister(tp->mdio_bus);
1566 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001567 }
1568}
1569
Matt Carlson95e28692008-05-25 23:44:14 -07001570/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001571static inline void tg3_generate_fw_event(struct tg3 *tp)
1572{
1573 u32 val;
1574
1575 val = tr32(GRC_RX_CPU_EVENT);
1576 val |= GRC_RX_CPU_DRIVER_EVENT;
1577 tw32_f(GRC_RX_CPU_EVENT, val);
1578
1579 tp->last_event_jiffies = jiffies;
1580}
1581
1582#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1583
1584/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001585static void tg3_wait_for_event_ack(struct tg3 *tp)
1586{
1587 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001588 unsigned int delay_cnt;
1589 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001590
Matt Carlson4ba526c2008-08-15 14:10:04 -07001591 /* If enough time has passed, no wait is necessary. */
1592 time_remain = (long)(tp->last_event_jiffies + 1 +
1593 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1594 (long)jiffies;
1595 if (time_remain < 0)
1596 return;
1597
1598 /* Check if we can shorten the wait time. */
1599 delay_cnt = jiffies_to_usecs(time_remain);
1600 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1601 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1602 delay_cnt = (delay_cnt >> 3) + 1;
1603
1604 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001605 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1606 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001607 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001608 }
1609}
1610
1611/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001612static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001613{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001614 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001615
1616 val = 0;
1617 if (!tg3_readphy(tp, MII_BMCR, &reg))
1618 val = reg << 16;
1619 if (!tg3_readphy(tp, MII_BMSR, &reg))
1620 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001621 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001622
1623 val = 0;
1624 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1625 val = reg << 16;
1626 if (!tg3_readphy(tp, MII_LPA, &reg))
1627 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001628 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001629
1630 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001631 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001632 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1633 val = reg << 16;
1634 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1635 val |= (reg & 0xffff);
1636 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001637 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001638
1639 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1640 val = reg << 16;
1641 else
1642 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001643 *data++ = val;
1644}
1645
1646/* tp->lock is held. */
1647static void tg3_ump_link_report(struct tg3 *tp)
1648{
1649 u32 data[4];
1650
1651 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1652 return;
1653
1654 tg3_phy_gather_ump_data(tp, data);
1655
1656 tg3_wait_for_event_ack(tp);
1657
1658 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1659 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1660 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1661 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1662 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1663 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001664
Matt Carlson4ba526c2008-08-15 14:10:04 -07001665 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001666}
1667
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001668/* tp->lock is held. */
1669static void tg3_stop_fw(struct tg3 *tp)
1670{
1671 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1672 /* Wait for RX cpu to ACK the previous event. */
1673 tg3_wait_for_event_ack(tp);
1674
1675 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1676
1677 tg3_generate_fw_event(tp);
1678
1679 /* Wait for RX cpu to ACK this event. */
1680 tg3_wait_for_event_ack(tp);
1681 }
1682}
1683
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001684/* tp->lock is held. */
1685static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1686{
1687 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1688 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1689
1690 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1691 switch (kind) {
1692 case RESET_KIND_INIT:
1693 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1694 DRV_STATE_START);
1695 break;
1696
1697 case RESET_KIND_SHUTDOWN:
1698 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1699 DRV_STATE_UNLOAD);
1700 break;
1701
1702 case RESET_KIND_SUSPEND:
1703 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1704 DRV_STATE_SUSPEND);
1705 break;
1706
1707 default:
1708 break;
1709 }
1710 }
1711
1712 if (kind == RESET_KIND_INIT ||
1713 kind == RESET_KIND_SUSPEND)
1714 tg3_ape_driver_state_change(tp, kind);
1715}
1716
1717/* tp->lock is held. */
1718static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1719{
1720 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1721 switch (kind) {
1722 case RESET_KIND_INIT:
1723 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1724 DRV_STATE_START_DONE);
1725 break;
1726
1727 case RESET_KIND_SHUTDOWN:
1728 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1729 DRV_STATE_UNLOAD_DONE);
1730 break;
1731
1732 default:
1733 break;
1734 }
1735 }
1736
1737 if (kind == RESET_KIND_SHUTDOWN)
1738 tg3_ape_driver_state_change(tp, kind);
1739}
1740
1741/* tp->lock is held. */
1742static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1743{
1744 if (tg3_flag(tp, ENABLE_ASF)) {
1745 switch (kind) {
1746 case RESET_KIND_INIT:
1747 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1748 DRV_STATE_START);
1749 break;
1750
1751 case RESET_KIND_SHUTDOWN:
1752 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1753 DRV_STATE_UNLOAD);
1754 break;
1755
1756 case RESET_KIND_SUSPEND:
1757 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1758 DRV_STATE_SUSPEND);
1759 break;
1760
1761 default:
1762 break;
1763 }
1764 }
1765}
1766
1767static int tg3_poll_fw(struct tg3 *tp)
1768{
1769 int i;
1770 u32 val;
1771
1772 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1773 /* Wait up to 20ms for init done. */
1774 for (i = 0; i < 200; i++) {
1775 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1776 return 0;
1777 udelay(100);
1778 }
1779 return -ENODEV;
1780 }
1781
1782 /* Wait for firmware initialization to complete. */
1783 for (i = 0; i < 100000; i++) {
1784 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1785 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1786 break;
1787 udelay(10);
1788 }
1789
1790 /* Chip might not be fitted with firmware. Some Sun onboard
1791 * parts are configured like that. So don't signal the timeout
1792 * of the above loop as an error, but do report the lack of
1793 * running firmware once.
1794 */
1795 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1796 tg3_flag_set(tp, NO_FWARE_REPORTED);
1797
1798 netdev_info(tp->dev, "No firmware running\n");
1799 }
1800
1801 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
1802 /* The 57765 A0 needs a little more
1803 * time to do some important work.
1804 */
1805 mdelay(10);
1806 }
1807
1808 return 0;
1809}
1810
Matt Carlson95e28692008-05-25 23:44:14 -07001811static void tg3_link_report(struct tg3 *tp)
1812{
1813 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001814 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001815 tg3_ump_link_report(tp);
1816 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001817 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1818 (tp->link_config.active_speed == SPEED_1000 ?
1819 1000 :
1820 (tp->link_config.active_speed == SPEED_100 ?
1821 100 : 10)),
1822 (tp->link_config.active_duplex == DUPLEX_FULL ?
1823 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001824
Joe Perches05dbe002010-02-17 19:44:19 +00001825 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1826 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1827 "on" : "off",
1828 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1829 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001830
1831 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1832 netdev_info(tp->dev, "EEE is %s\n",
1833 tp->setlpicnt ? "enabled" : "disabled");
1834
Matt Carlson95e28692008-05-25 23:44:14 -07001835 tg3_ump_link_report(tp);
1836 }
1837}
1838
Matt Carlson95e28692008-05-25 23:44:14 -07001839static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1840{
1841 u16 miireg;
1842
Steve Glendinninge18ce342008-12-16 02:00:00 -08001843 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001844 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001845 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001846 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001847 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001848 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1849 else
1850 miireg = 0;
1851
1852 return miireg;
1853}
1854
Matt Carlson95e28692008-05-25 23:44:14 -07001855static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1856{
1857 u8 cap = 0;
1858
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001859 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1860 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1861 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1862 if (lcladv & ADVERTISE_1000XPAUSE)
1863 cap = FLOW_CTRL_RX;
1864 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001865 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001866 }
1867
1868 return cap;
1869}
1870
Matt Carlsonf51f3562008-05-25 23:45:08 -07001871static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001872{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001873 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001874 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001875 u32 old_rx_mode = tp->rx_mode;
1876 u32 old_tx_mode = tp->tx_mode;
1877
Joe Perches63c3a662011-04-26 08:12:10 +00001878 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001879 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001880 else
1881 autoneg = tp->link_config.autoneg;
1882
Joe Perches63c3a662011-04-26 08:12:10 +00001883 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001884 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001885 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001886 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001887 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001888 } else
1889 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001890
Matt Carlsonf51f3562008-05-25 23:45:08 -07001891 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001892
Steve Glendinninge18ce342008-12-16 02:00:00 -08001893 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001894 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1895 else
1896 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1897
Matt Carlsonf51f3562008-05-25 23:45:08 -07001898 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001899 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001900
Steve Glendinninge18ce342008-12-16 02:00:00 -08001901 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001902 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1903 else
1904 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1905
Matt Carlsonf51f3562008-05-25 23:45:08 -07001906 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001907 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001908}
1909
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001910static void tg3_adjust_link(struct net_device *dev)
1911{
1912 u8 oldflowctrl, linkmesg = 0;
1913 u32 mac_mode, lcl_adv, rmt_adv;
1914 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001915 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001916
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001917 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001918
1919 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1920 MAC_MODE_HALF_DUPLEX);
1921
1922 oldflowctrl = tp->link_config.active_flowctrl;
1923
1924 if (phydev->link) {
1925 lcl_adv = 0;
1926 rmt_adv = 0;
1927
1928 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1929 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001930 else if (phydev->speed == SPEED_1000 ||
1931 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001932 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001933 else
1934 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001935
1936 if (phydev->duplex == DUPLEX_HALF)
1937 mac_mode |= MAC_MODE_HALF_DUPLEX;
1938 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00001939 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001940 tp->link_config.flowctrl);
1941
1942 if (phydev->pause)
1943 rmt_adv = LPA_PAUSE_CAP;
1944 if (phydev->asym_pause)
1945 rmt_adv |= LPA_PAUSE_ASYM;
1946 }
1947
1948 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1949 } else
1950 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1951
1952 if (mac_mode != tp->mac_mode) {
1953 tp->mac_mode = mac_mode;
1954 tw32_f(MAC_MODE, tp->mac_mode);
1955 udelay(40);
1956 }
1957
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001958 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1959 if (phydev->speed == SPEED_10)
1960 tw32(MAC_MI_STAT,
1961 MAC_MI_STAT_10MBPS_MODE |
1962 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1963 else
1964 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1965 }
1966
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001967 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1968 tw32(MAC_TX_LENGTHS,
1969 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1970 (6 << TX_LENGTHS_IPG_SHIFT) |
1971 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1972 else
1973 tw32(MAC_TX_LENGTHS,
1974 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1975 (6 << TX_LENGTHS_IPG_SHIFT) |
1976 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1977
Matt Carlson34655ad2012-02-22 12:35:18 +00001978 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001979 phydev->speed != tp->link_config.active_speed ||
1980 phydev->duplex != tp->link_config.active_duplex ||
1981 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001982 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001983
Matt Carlson34655ad2012-02-22 12:35:18 +00001984 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001985 tp->link_config.active_speed = phydev->speed;
1986 tp->link_config.active_duplex = phydev->duplex;
1987
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001988 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001989
1990 if (linkmesg)
1991 tg3_link_report(tp);
1992}
1993
1994static int tg3_phy_init(struct tg3 *tp)
1995{
1996 struct phy_device *phydev;
1997
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001998 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001999 return 0;
2000
2001 /* Bring the PHY back to a known state. */
2002 tg3_bmcr_reset(tp);
2003
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002004 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002005
2006 /* Attach the MAC to the PHY. */
Florian Fainellif9a8f832013-01-14 00:52:52 +00002007 phydev = phy_connect(tp->dev, dev_name(&phydev->dev),
2008 tg3_adjust_link, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002009 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002010 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002011 return PTR_ERR(phydev);
2012 }
2013
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002014 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002015 switch (phydev->interface) {
2016 case PHY_INTERFACE_MODE_GMII:
2017 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002018 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002019 phydev->supported &= (PHY_GBIT_FEATURES |
2020 SUPPORTED_Pause |
2021 SUPPORTED_Asym_Pause);
2022 break;
2023 }
2024 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002025 case PHY_INTERFACE_MODE_MII:
2026 phydev->supported &= (PHY_BASIC_FEATURES |
2027 SUPPORTED_Pause |
2028 SUPPORTED_Asym_Pause);
2029 break;
2030 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002031 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002032 return -EINVAL;
2033 }
2034
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002035 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002036
2037 phydev->advertising = phydev->supported;
2038
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002039 return 0;
2040}
2041
2042static void tg3_phy_start(struct tg3 *tp)
2043{
2044 struct phy_device *phydev;
2045
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002046 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002047 return;
2048
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002049 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002050
Matt Carlson80096062010-08-02 11:26:06 +00002051 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2052 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002053 phydev->speed = tp->link_config.speed;
2054 phydev->duplex = tp->link_config.duplex;
2055 phydev->autoneg = tp->link_config.autoneg;
2056 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002057 }
2058
2059 phy_start(phydev);
2060
2061 phy_start_aneg(phydev);
2062}
2063
2064static void tg3_phy_stop(struct tg3 *tp)
2065{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002066 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002067 return;
2068
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002069 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002070}
2071
2072static void tg3_phy_fini(struct tg3 *tp)
2073{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002074 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002075 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002076 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002077 }
2078}
2079
Matt Carlson941ec902011-08-19 13:58:23 +00002080static int tg3_phy_set_extloopbk(struct tg3 *tp)
2081{
2082 int err;
2083 u32 val;
2084
2085 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2086 return 0;
2087
2088 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2089 /* Cannot do read-modify-write on 5401 */
2090 err = tg3_phy_auxctl_write(tp,
2091 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2092 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2093 0x4c20);
2094 goto done;
2095 }
2096
2097 err = tg3_phy_auxctl_read(tp,
2098 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2099 if (err)
2100 return err;
2101
2102 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2103 err = tg3_phy_auxctl_write(tp,
2104 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2105
2106done:
2107 return err;
2108}
2109
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002110static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2111{
2112 u32 phytest;
2113
2114 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2115 u32 phy;
2116
2117 tg3_writephy(tp, MII_TG3_FET_TEST,
2118 phytest | MII_TG3_FET_SHADOW_EN);
2119 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2120 if (enable)
2121 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2122 else
2123 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2124 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2125 }
2126 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2127 }
2128}
2129
Matt Carlson6833c042008-11-21 17:18:59 -08002130static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2131{
2132 u32 reg;
2133
Joe Perches63c3a662011-04-26 08:12:10 +00002134 if (!tg3_flag(tp, 5705_PLUS) ||
2135 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002136 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002137 return;
2138
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002139 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002140 tg3_phy_fet_toggle_apd(tp, enable);
2141 return;
2142 }
2143
Matt Carlson6833c042008-11-21 17:18:59 -08002144 reg = MII_TG3_MISC_SHDW_WREN |
2145 MII_TG3_MISC_SHDW_SCR5_SEL |
2146 MII_TG3_MISC_SHDW_SCR5_LPED |
2147 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2148 MII_TG3_MISC_SHDW_SCR5_SDTL |
2149 MII_TG3_MISC_SHDW_SCR5_C125OE;
2150 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
2151 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2152
2153 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2154
2155
2156 reg = MII_TG3_MISC_SHDW_WREN |
2157 MII_TG3_MISC_SHDW_APD_SEL |
2158 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2159 if (enable)
2160 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2161
2162 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2163}
2164
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002165static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2166{
2167 u32 phy;
2168
Joe Perches63c3a662011-04-26 08:12:10 +00002169 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002170 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002171 return;
2172
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002173 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002174 u32 ephy;
2175
Matt Carlson535ef6e2009-08-25 10:09:36 +00002176 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2177 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2178
2179 tg3_writephy(tp, MII_TG3_FET_TEST,
2180 ephy | MII_TG3_FET_SHADOW_EN);
2181 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002182 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002183 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002184 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002185 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2186 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002187 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002188 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002189 }
2190 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002191 int ret;
2192
2193 ret = tg3_phy_auxctl_read(tp,
2194 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2195 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002196 if (enable)
2197 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2198 else
2199 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002200 tg3_phy_auxctl_write(tp,
2201 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002202 }
2203 }
2204}
2205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206static void tg3_phy_set_wirespeed(struct tg3 *tp)
2207{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002208 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 u32 val;
2210
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002211 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 return;
2213
Matt Carlson15ee95c2011-04-20 07:57:40 +00002214 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2215 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002216 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2217 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
2219
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002220static void tg3_phy_apply_otp(struct tg3 *tp)
2221{
2222 u32 otp, phy;
2223
2224 if (!tp->phy_otp)
2225 return;
2226
2227 otp = tp->phy_otp;
2228
Matt Carlson1d36ba42011-04-20 07:57:42 +00002229 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
2230 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002231
2232 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2233 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2234 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2235
2236 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2237 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2238 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2239
2240 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2241 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2242 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2243
2244 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2245 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2246
2247 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2248 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2249
2250 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2251 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2252 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2253
Matt Carlson1d36ba42011-04-20 07:57:42 +00002254 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002255}
2256
Matt Carlson52b02d02010-10-14 10:37:41 +00002257static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2258{
2259 u32 val;
2260
2261 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2262 return;
2263
2264 tp->setlpicnt = 0;
2265
2266 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2267 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002268 tp->link_config.active_duplex == DUPLEX_FULL &&
2269 (tp->link_config.active_speed == SPEED_100 ||
2270 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002271 u32 eeectl;
2272
2273 if (tp->link_config.active_speed == SPEED_1000)
2274 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2275 else
2276 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2277
2278 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2279
Matt Carlson3110f5f52010-12-06 08:28:50 +00002280 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2281 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002282
Matt Carlsonb0c59432011-05-19 12:12:48 +00002283 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2284 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002285 tp->setlpicnt = 2;
2286 }
2287
2288 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002289 if (current_link_up == 1 &&
2290 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2291 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
2292 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2293 }
2294
Matt Carlson52b02d02010-10-14 10:37:41 +00002295 val = tr32(TG3_CPMU_EEE_MODE);
2296 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2297 }
2298}
2299
Matt Carlsonb0c59432011-05-19 12:12:48 +00002300static void tg3_phy_eee_enable(struct tg3 *tp)
2301{
2302 u32 val;
2303
2304 if (tp->link_config.active_speed == SPEED_1000 &&
2305 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2306 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002307 tg3_flag(tp, 57765_CLASS)) &&
Matt Carlsonb0c59432011-05-19 12:12:48 +00002308 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002309 val = MII_TG3_DSP_TAP26_ALNOKO |
2310 MII_TG3_DSP_TAP26_RMRXSTO;
2311 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002312 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2313 }
2314
2315 val = tr32(TG3_CPMU_EEE_MODE);
2316 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2317}
2318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319static int tg3_wait_macro_done(struct tg3 *tp)
2320{
2321 int limit = 100;
2322
2323 while (limit--) {
2324 u32 tmp32;
2325
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002326 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 if ((tmp32 & 0x1000) == 0)
2328 break;
2329 }
2330 }
Roel Kluind4675b52009-02-12 16:33:27 -08002331 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return -EBUSY;
2333
2334 return 0;
2335}
2336
2337static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2338{
2339 static const u32 test_pat[4][6] = {
2340 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2341 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2342 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2343 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2344 };
2345 int chan;
2346
2347 for (chan = 0; chan < 4; chan++) {
2348 int i;
2349
2350 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2351 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002352 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354 for (i = 0; i < 6; i++)
2355 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2356 test_pat[chan][i]);
2357
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002358 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (tg3_wait_macro_done(tp)) {
2360 *resetp = 1;
2361 return -EBUSY;
2362 }
2363
2364 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2365 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002366 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (tg3_wait_macro_done(tp)) {
2368 *resetp = 1;
2369 return -EBUSY;
2370 }
2371
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002372 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (tg3_wait_macro_done(tp)) {
2374 *resetp = 1;
2375 return -EBUSY;
2376 }
2377
2378 for (i = 0; i < 6; i += 2) {
2379 u32 low, high;
2380
2381 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2382 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2383 tg3_wait_macro_done(tp)) {
2384 *resetp = 1;
2385 return -EBUSY;
2386 }
2387 low &= 0x7fff;
2388 high &= 0x000f;
2389 if (low != test_pat[chan][i] ||
2390 high != test_pat[chan][i+1]) {
2391 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2392 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2393 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2394
2395 return -EBUSY;
2396 }
2397 }
2398 }
2399
2400 return 0;
2401}
2402
2403static int tg3_phy_reset_chanpat(struct tg3 *tp)
2404{
2405 int chan;
2406
2407 for (chan = 0; chan < 4; chan++) {
2408 int i;
2409
2410 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2411 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002412 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 for (i = 0; i < 6; i++)
2414 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002415 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if (tg3_wait_macro_done(tp))
2417 return -EBUSY;
2418 }
2419
2420 return 0;
2421}
2422
2423static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2424{
2425 u32 reg32, phy9_orig;
2426 int retries, do_phy_reset, err;
2427
2428 retries = 10;
2429 do_phy_reset = 1;
2430 do {
2431 if (do_phy_reset) {
2432 err = tg3_bmcr_reset(tp);
2433 if (err)
2434 return err;
2435 do_phy_reset = 0;
2436 }
2437
2438 /* Disable transmitter and interrupt. */
2439 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2440 continue;
2441
2442 reg32 |= 0x3000;
2443 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2444
2445 /* Set full-duplex, 1000 mbps. */
2446 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002447 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
2449 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002450 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 continue;
2452
Matt Carlson221c5632011-06-13 13:39:01 +00002453 tg3_writephy(tp, MII_CTRL1000,
2454 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Matt Carlson1d36ba42011-04-20 07:57:42 +00002456 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2457 if (err)
2458 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002461 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2464 if (!err)
2465 break;
2466 } while (--retries);
2467
2468 err = tg3_phy_reset_chanpat(tp);
2469 if (err)
2470 return err;
2471
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002472 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
2474 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002475 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Matt Carlson1d36ba42011-04-20 07:57:42 +00002477 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Matt Carlson221c5632011-06-13 13:39:01 +00002479 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2482 reg32 &= ~0x3000;
2483 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2484 } else if (!err)
2485 err = -EBUSY;
2486
2487 return err;
2488}
2489
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002490static void tg3_carrier_on(struct tg3 *tp)
2491{
2492 netif_carrier_on(tp->dev);
2493 tp->link_up = true;
2494}
2495
2496static void tg3_carrier_off(struct tg3 *tp)
2497{
2498 netif_carrier_off(tp->dev);
2499 tp->link_up = false;
2500}
2501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502/* This will reset the tigon3 PHY if there is no valid
2503 * link unless the FORCE argument is non-zero.
2504 */
2505static int tg3_phy_reset(struct tg3 *tp)
2506{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002507 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 int err;
2509
Michael Chan60189dd2006-12-17 17:08:07 -08002510 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002511 val = tr32(GRC_MISC_CFG);
2512 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2513 udelay(40);
2514 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002515 err = tg3_readphy(tp, MII_BMSR, &val);
2516 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 if (err != 0)
2518 return -EBUSY;
2519
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002520 if (netif_running(tp->dev) && tp->link_up) {
2521 tg3_carrier_off(tp);
Michael Chanc8e1e822006-04-29 18:55:17 -07002522 tg3_link_report(tp);
2523 }
2524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2526 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2527 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2528 err = tg3_phy_reset_5703_4_5(tp);
2529 if (err)
2530 return err;
2531 goto out;
2532 }
2533
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002534 cpmuctrl = 0;
2535 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2536 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2537 cpmuctrl = tr32(TG3_CPMU_CTRL);
2538 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2539 tw32(TG3_CPMU_CTRL,
2540 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2541 }
2542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 err = tg3_bmcr_reset(tp);
2544 if (err)
2545 return err;
2546
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002547 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002548 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2549 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002550
2551 tw32(TG3_CPMU_CTRL, cpmuctrl);
2552 }
2553
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002554 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2555 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002556 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2557 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2558 CPMU_LSPD_1000MB_MACCLK_12_5) {
2559 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2560 udelay(40);
2561 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2562 }
2563 }
2564
Joe Perches63c3a662011-04-26 08:12:10 +00002565 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002566 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002567 return 0;
2568
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002569 tg3_phy_apply_otp(tp);
2570
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002571 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002572 tg3_phy_toggle_apd(tp, true);
2573 else
2574 tg3_phy_toggle_apd(tp, false);
2575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002577 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2578 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002579 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2580 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002581 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002583
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002584 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002585 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2586 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002588
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002589 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002590 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2591 tg3_phydsp_write(tp, 0x000a, 0x310b);
2592 tg3_phydsp_write(tp, 0x201f, 0x9506);
2593 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2594 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2595 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002596 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002597 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2598 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2599 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2600 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2601 tg3_writephy(tp, MII_TG3_TEST1,
2602 MII_TG3_TEST1_TRIM_EN | 0x4);
2603 } else
2604 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2605
2606 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2607 }
Michael Chanc424cb22006-04-29 18:56:34 -07002608 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 /* Set Extended packet length bit (bit 14) on all chips that */
2611 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002612 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002614 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002615 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002617 err = tg3_phy_auxctl_read(tp,
2618 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2619 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002620 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2621 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 }
2623
2624 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2625 * jumbo frames transmission.
2626 */
Joe Perches63c3a662011-04-26 08:12:10 +00002627 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002628 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002629 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002630 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
2632
Michael Chan715116a2006-09-27 16:09:25 -07002633 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002634 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002635 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002636 }
2637
Michael Chanc65a17f2013-01-06 12:51:07 +00002638 if (tp->pci_chip_rev_id == CHIPREV_ID_5762_A0)
2639 tg3_phydsp_write(tp, 0xffb, 0x4000);
2640
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002641 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 tg3_phy_set_wirespeed(tp);
2643 return 0;
2644}
2645
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002646#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2647#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2648#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2649 TG3_GPIO_MSG_NEED_VAUX)
2650#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2651 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2652 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2653 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2654 (TG3_GPIO_MSG_DRVR_PRES << 12))
2655
2656#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2657 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2658 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2659 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2660 (TG3_GPIO_MSG_NEED_VAUX << 12))
2661
2662static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2663{
2664 u32 status, shift;
2665
2666 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2667 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2668 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2669 else
2670 status = tr32(TG3_CPMU_DRV_STATUS);
2671
2672 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2673 status &= ~(TG3_GPIO_MSG_MASK << shift);
2674 status |= (newstat << shift);
2675
2676 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2677 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2678 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2679 else
2680 tw32(TG3_CPMU_DRV_STATUS, status);
2681
2682 return status >> TG3_APE_GPIO_MSG_SHIFT;
2683}
2684
Matt Carlson520b2752011-06-13 13:39:02 +00002685static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2686{
2687 if (!tg3_flag(tp, IS_NIC))
2688 return 0;
2689
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002690 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2691 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2692 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2693 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2694 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002695
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002696 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2697
2698 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2699 TG3_GRC_LCLCTL_PWRSW_DELAY);
2700
2701 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2702 } else {
2703 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2704 TG3_GRC_LCLCTL_PWRSW_DELAY);
2705 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002706
Matt Carlson520b2752011-06-13 13:39:02 +00002707 return 0;
2708}
2709
2710static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2711{
2712 u32 grc_local_ctrl;
2713
2714 if (!tg3_flag(tp, IS_NIC) ||
2715 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2716 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2717 return;
2718
2719 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2720
2721 tw32_wait_f(GRC_LOCAL_CTRL,
2722 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2723 TG3_GRC_LCLCTL_PWRSW_DELAY);
2724
2725 tw32_wait_f(GRC_LOCAL_CTRL,
2726 grc_local_ctrl,
2727 TG3_GRC_LCLCTL_PWRSW_DELAY);
2728
2729 tw32_wait_f(GRC_LOCAL_CTRL,
2730 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2731 TG3_GRC_LCLCTL_PWRSW_DELAY);
2732}
2733
2734static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2735{
2736 if (!tg3_flag(tp, IS_NIC))
2737 return;
2738
2739 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2740 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2741 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2742 (GRC_LCLCTRL_GPIO_OE0 |
2743 GRC_LCLCTRL_GPIO_OE1 |
2744 GRC_LCLCTRL_GPIO_OE2 |
2745 GRC_LCLCTRL_GPIO_OUTPUT0 |
2746 GRC_LCLCTRL_GPIO_OUTPUT1),
2747 TG3_GRC_LCLCTL_PWRSW_DELAY);
2748 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2749 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2750 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2751 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2752 GRC_LCLCTRL_GPIO_OE1 |
2753 GRC_LCLCTRL_GPIO_OE2 |
2754 GRC_LCLCTRL_GPIO_OUTPUT0 |
2755 GRC_LCLCTRL_GPIO_OUTPUT1 |
2756 tp->grc_local_ctrl;
2757 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2758 TG3_GRC_LCLCTL_PWRSW_DELAY);
2759
2760 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2761 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2762 TG3_GRC_LCLCTL_PWRSW_DELAY);
2763
2764 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2765 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2766 TG3_GRC_LCLCTL_PWRSW_DELAY);
2767 } else {
2768 u32 no_gpio2;
2769 u32 grc_local_ctrl = 0;
2770
2771 /* Workaround to prevent overdrawing Amps. */
2772 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2773 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2774 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2775 grc_local_ctrl,
2776 TG3_GRC_LCLCTL_PWRSW_DELAY);
2777 }
2778
2779 /* On 5753 and variants, GPIO2 cannot be used. */
2780 no_gpio2 = tp->nic_sram_data_cfg &
2781 NIC_SRAM_DATA_CFG_NO_GPIO2;
2782
2783 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2784 GRC_LCLCTRL_GPIO_OE1 |
2785 GRC_LCLCTRL_GPIO_OE2 |
2786 GRC_LCLCTRL_GPIO_OUTPUT1 |
2787 GRC_LCLCTRL_GPIO_OUTPUT2;
2788 if (no_gpio2) {
2789 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2790 GRC_LCLCTRL_GPIO_OUTPUT2);
2791 }
2792 tw32_wait_f(GRC_LOCAL_CTRL,
2793 tp->grc_local_ctrl | grc_local_ctrl,
2794 TG3_GRC_LCLCTL_PWRSW_DELAY);
2795
2796 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2797
2798 tw32_wait_f(GRC_LOCAL_CTRL,
2799 tp->grc_local_ctrl | grc_local_ctrl,
2800 TG3_GRC_LCLCTL_PWRSW_DELAY);
2801
2802 if (!no_gpio2) {
2803 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2804 tw32_wait_f(GRC_LOCAL_CTRL,
2805 tp->grc_local_ctrl | grc_local_ctrl,
2806 TG3_GRC_LCLCTL_PWRSW_DELAY);
2807 }
2808 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002809}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002810
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002811static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002812{
2813 u32 msg = 0;
2814
2815 /* Serialize power state transitions */
2816 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2817 return;
2818
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002819 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002820 msg = TG3_GPIO_MSG_NEED_VAUX;
2821
2822 msg = tg3_set_function_status(tp, msg);
2823
2824 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2825 goto done;
2826
2827 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2828 tg3_pwrsrc_switch_to_vaux(tp);
2829 else
2830 tg3_pwrsrc_die_with_vmain(tp);
2831
2832done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002833 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002834}
2835
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002836static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
Matt Carlson683644b2011-03-09 16:58:23 +00002838 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Matt Carlson334355a2010-01-20 16:58:10 +00002840 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002841 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return;
2843
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002844 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2845 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2846 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002847 tg3_frob_aux_power_5717(tp, include_wol ?
2848 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002849 return;
2850 }
2851
2852 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002853 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002855 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002856
Michael Chanbc1c7562006-03-20 17:48:03 -08002857 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002858 if (dev_peer) {
2859 struct tg3 *tp_peer = netdev_priv(dev_peer);
2860
Joe Perches63c3a662011-04-26 08:12:10 +00002861 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002862 return;
2863
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002864 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002865 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002866 need_vaux = true;
2867 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002870 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2871 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002872 need_vaux = true;
2873
Matt Carlson520b2752011-06-13 13:39:02 +00002874 if (need_vaux)
2875 tg3_pwrsrc_switch_to_vaux(tp);
2876 else
2877 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878}
2879
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002880static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2881{
2882 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2883 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002884 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002885 if (speed != SPEED_10)
2886 return 1;
2887 } else if (speed == SPEED_10)
2888 return 1;
2889
2890 return 0;
2891}
2892
Matt Carlson0a459aa2008-11-03 16:54:15 -08002893static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002894{
Matt Carlsonce057f02007-11-12 21:08:03 -08002895 u32 val;
2896
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002897 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002898 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2899 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2900 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2901
2902 sg_dig_ctrl |=
2903 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2904 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2905 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2906 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002907 return;
Michael Chan51297242007-02-13 12:17:57 -08002908 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002909
Michael Chan60189dd2006-12-17 17:08:07 -08002910 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002911 tg3_bmcr_reset(tp);
2912 val = tr32(GRC_MISC_CFG);
2913 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2914 udelay(40);
2915 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002916 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002917 u32 phytest;
2918 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2919 u32 phy;
2920
2921 tg3_writephy(tp, MII_ADVERTISE, 0);
2922 tg3_writephy(tp, MII_BMCR,
2923 BMCR_ANENABLE | BMCR_ANRESTART);
2924
2925 tg3_writephy(tp, MII_TG3_FET_TEST,
2926 phytest | MII_TG3_FET_SHADOW_EN);
2927 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2928 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2929 tg3_writephy(tp,
2930 MII_TG3_FET_SHDW_AUXMODE4,
2931 phy);
2932 }
2933 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2934 }
2935 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002936 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002937 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2938 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002939
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002940 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2941 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2942 MII_TG3_AUXCTL_PCTL_VREG_11V;
2943 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002944 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002945
Michael Chan15c3b692006-03-22 01:06:52 -08002946 /* The PHY should not be powered down on some chips because
2947 * of bugs.
2948 */
2949 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2950 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2951 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00002952 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)) ||
2953 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
2954 !tp->pci_fn))
Michael Chan15c3b692006-03-22 01:06:52 -08002955 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002956
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002957 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2958 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002959 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2960 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2961 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2962 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2963 }
2964
Michael Chan15c3b692006-03-22 01:06:52 -08002965 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2966}
2967
Matt Carlson3f007892008-11-03 16:51:36 -08002968/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002969static int tg3_nvram_lock(struct tg3 *tp)
2970{
Joe Perches63c3a662011-04-26 08:12:10 +00002971 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002972 int i;
2973
2974 if (tp->nvram_lock_cnt == 0) {
2975 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2976 for (i = 0; i < 8000; i++) {
2977 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2978 break;
2979 udelay(20);
2980 }
2981 if (i == 8000) {
2982 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2983 return -ENODEV;
2984 }
2985 }
2986 tp->nvram_lock_cnt++;
2987 }
2988 return 0;
2989}
2990
2991/* tp->lock is held. */
2992static void tg3_nvram_unlock(struct tg3 *tp)
2993{
Joe Perches63c3a662011-04-26 08:12:10 +00002994 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002995 if (tp->nvram_lock_cnt > 0)
2996 tp->nvram_lock_cnt--;
2997 if (tp->nvram_lock_cnt == 0)
2998 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2999 }
3000}
3001
3002/* tp->lock is held. */
3003static void tg3_enable_nvram_access(struct tg3 *tp)
3004{
Joe Perches63c3a662011-04-26 08:12:10 +00003005 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003006 u32 nvaccess = tr32(NVRAM_ACCESS);
3007
3008 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3009 }
3010}
3011
3012/* tp->lock is held. */
3013static void tg3_disable_nvram_access(struct tg3 *tp)
3014{
Joe Perches63c3a662011-04-26 08:12:10 +00003015 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003016 u32 nvaccess = tr32(NVRAM_ACCESS);
3017
3018 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3019 }
3020}
3021
3022static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3023 u32 offset, u32 *val)
3024{
3025 u32 tmp;
3026 int i;
3027
3028 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3029 return -EINVAL;
3030
3031 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3032 EEPROM_ADDR_DEVID_MASK |
3033 EEPROM_ADDR_READ);
3034 tw32(GRC_EEPROM_ADDR,
3035 tmp |
3036 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3037 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3038 EEPROM_ADDR_ADDR_MASK) |
3039 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3040
3041 for (i = 0; i < 1000; i++) {
3042 tmp = tr32(GRC_EEPROM_ADDR);
3043
3044 if (tmp & EEPROM_ADDR_COMPLETE)
3045 break;
3046 msleep(1);
3047 }
3048 if (!(tmp & EEPROM_ADDR_COMPLETE))
3049 return -EBUSY;
3050
Matt Carlson62cedd12009-04-20 14:52:29 -07003051 tmp = tr32(GRC_EEPROM_DATA);
3052
3053 /*
3054 * The data will always be opposite the native endian
3055 * format. Perform a blind byteswap to compensate.
3056 */
3057 *val = swab32(tmp);
3058
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003059 return 0;
3060}
3061
3062#define NVRAM_CMD_TIMEOUT 10000
3063
3064static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3065{
3066 int i;
3067
3068 tw32(NVRAM_CMD, nvram_cmd);
3069 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
3070 udelay(10);
3071 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3072 udelay(10);
3073 break;
3074 }
3075 }
3076
3077 if (i == NVRAM_CMD_TIMEOUT)
3078 return -EBUSY;
3079
3080 return 0;
3081}
3082
3083static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3084{
Joe Perches63c3a662011-04-26 08:12:10 +00003085 if (tg3_flag(tp, NVRAM) &&
3086 tg3_flag(tp, NVRAM_BUFFERED) &&
3087 tg3_flag(tp, FLASH) &&
3088 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003089 (tp->nvram_jedecnum == JEDEC_ATMEL))
3090
3091 addr = ((addr / tp->nvram_pagesize) <<
3092 ATMEL_AT45DB0X1B_PAGE_POS) +
3093 (addr % tp->nvram_pagesize);
3094
3095 return addr;
3096}
3097
3098static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3099{
Joe Perches63c3a662011-04-26 08:12:10 +00003100 if (tg3_flag(tp, NVRAM) &&
3101 tg3_flag(tp, NVRAM_BUFFERED) &&
3102 tg3_flag(tp, FLASH) &&
3103 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003104 (tp->nvram_jedecnum == JEDEC_ATMEL))
3105
3106 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3107 tp->nvram_pagesize) +
3108 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3109
3110 return addr;
3111}
3112
Matt Carlsone4f34112009-02-25 14:25:00 +00003113/* NOTE: Data read in from NVRAM is byteswapped according to
3114 * the byteswapping settings for all other register accesses.
3115 * tg3 devices are BE devices, so on a BE machine, the data
3116 * returned will be exactly as it is seen in NVRAM. On a LE
3117 * machine, the 32-bit value will be byteswapped.
3118 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003119static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3120{
3121 int ret;
3122
Joe Perches63c3a662011-04-26 08:12:10 +00003123 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003124 return tg3_nvram_read_using_eeprom(tp, offset, val);
3125
3126 offset = tg3_nvram_phys_addr(tp, offset);
3127
3128 if (offset > NVRAM_ADDR_MSK)
3129 return -EINVAL;
3130
3131 ret = tg3_nvram_lock(tp);
3132 if (ret)
3133 return ret;
3134
3135 tg3_enable_nvram_access(tp);
3136
3137 tw32(NVRAM_ADDR, offset);
3138 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3139 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3140
3141 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003142 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003143
3144 tg3_disable_nvram_access(tp);
3145
3146 tg3_nvram_unlock(tp);
3147
3148 return ret;
3149}
3150
Matt Carlsona9dc5292009-02-25 14:25:30 +00003151/* Ensures NVRAM data is in bytestream format. */
3152static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003153{
3154 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003155 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003156 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003157 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003158 return res;
3159}
3160
Matt Carlsondbe9b922012-02-13 10:20:09 +00003161static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3162 u32 offset, u32 len, u8 *buf)
3163{
3164 int i, j, rc = 0;
3165 u32 val;
3166
3167 for (i = 0; i < len; i += 4) {
3168 u32 addr;
3169 __be32 data;
3170
3171 addr = offset + i;
3172
3173 memcpy(&data, buf + i, 4);
3174
3175 /*
3176 * The SEEPROM interface expects the data to always be opposite
3177 * the native endian format. We accomplish this by reversing
3178 * all the operations that would have been performed on the
3179 * data from a call to tg3_nvram_read_be32().
3180 */
3181 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3182
3183 val = tr32(GRC_EEPROM_ADDR);
3184 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3185
3186 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3187 EEPROM_ADDR_READ);
3188 tw32(GRC_EEPROM_ADDR, val |
3189 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3190 (addr & EEPROM_ADDR_ADDR_MASK) |
3191 EEPROM_ADDR_START |
3192 EEPROM_ADDR_WRITE);
3193
3194 for (j = 0; j < 1000; j++) {
3195 val = tr32(GRC_EEPROM_ADDR);
3196
3197 if (val & EEPROM_ADDR_COMPLETE)
3198 break;
3199 msleep(1);
3200 }
3201 if (!(val & EEPROM_ADDR_COMPLETE)) {
3202 rc = -EBUSY;
3203 break;
3204 }
3205 }
3206
3207 return rc;
3208}
3209
3210/* offset and length are dword aligned */
3211static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3212 u8 *buf)
3213{
3214 int ret = 0;
3215 u32 pagesize = tp->nvram_pagesize;
3216 u32 pagemask = pagesize - 1;
3217 u32 nvram_cmd;
3218 u8 *tmp;
3219
3220 tmp = kmalloc(pagesize, GFP_KERNEL);
3221 if (tmp == NULL)
3222 return -ENOMEM;
3223
3224 while (len) {
3225 int j;
3226 u32 phy_addr, page_off, size;
3227
3228 phy_addr = offset & ~pagemask;
3229
3230 for (j = 0; j < pagesize; j += 4) {
3231 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3232 (__be32 *) (tmp + j));
3233 if (ret)
3234 break;
3235 }
3236 if (ret)
3237 break;
3238
3239 page_off = offset & pagemask;
3240 size = pagesize;
3241 if (len < size)
3242 size = len;
3243
3244 len -= size;
3245
3246 memcpy(tmp + page_off, buf, size);
3247
3248 offset = offset + (pagesize - page_off);
3249
3250 tg3_enable_nvram_access(tp);
3251
3252 /*
3253 * Before we can erase the flash page, we need
3254 * to issue a special "write enable" command.
3255 */
3256 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3257
3258 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3259 break;
3260
3261 /* Erase the target page */
3262 tw32(NVRAM_ADDR, phy_addr);
3263
3264 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3265 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3266
3267 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3268 break;
3269
3270 /* Issue another write enable to start the write. */
3271 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3272
3273 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3274 break;
3275
3276 for (j = 0; j < pagesize; j += 4) {
3277 __be32 data;
3278
3279 data = *((__be32 *) (tmp + j));
3280
3281 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3282
3283 tw32(NVRAM_ADDR, phy_addr + j);
3284
3285 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3286 NVRAM_CMD_WR;
3287
3288 if (j == 0)
3289 nvram_cmd |= NVRAM_CMD_FIRST;
3290 else if (j == (pagesize - 4))
3291 nvram_cmd |= NVRAM_CMD_LAST;
3292
3293 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3294 if (ret)
3295 break;
3296 }
3297 if (ret)
3298 break;
3299 }
3300
3301 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3302 tg3_nvram_exec_cmd(tp, nvram_cmd);
3303
3304 kfree(tmp);
3305
3306 return ret;
3307}
3308
3309/* offset and length are dword aligned */
3310static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3311 u8 *buf)
3312{
3313 int i, ret = 0;
3314
3315 for (i = 0; i < len; i += 4, offset += 4) {
3316 u32 page_off, phy_addr, nvram_cmd;
3317 __be32 data;
3318
3319 memcpy(&data, buf + i, 4);
3320 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3321
3322 page_off = offset % tp->nvram_pagesize;
3323
3324 phy_addr = tg3_nvram_phys_addr(tp, offset);
3325
Matt Carlsondbe9b922012-02-13 10:20:09 +00003326 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3327
3328 if (page_off == 0 || i == 0)
3329 nvram_cmd |= NVRAM_CMD_FIRST;
3330 if (page_off == (tp->nvram_pagesize - 4))
3331 nvram_cmd |= NVRAM_CMD_LAST;
3332
3333 if (i == (len - 4))
3334 nvram_cmd |= NVRAM_CMD_LAST;
3335
Matt Carlson42278222012-02-13 15:20:11 +00003336 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3337 !tg3_flag(tp, FLASH) ||
3338 !tg3_flag(tp, 57765_PLUS))
3339 tw32(NVRAM_ADDR, phy_addr);
3340
Matt Carlsondbe9b922012-02-13 10:20:09 +00003341 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
3342 !tg3_flag(tp, 5755_PLUS) &&
3343 (tp->nvram_jedecnum == JEDEC_ST) &&
3344 (nvram_cmd & NVRAM_CMD_FIRST)) {
3345 u32 cmd;
3346
3347 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3348 ret = tg3_nvram_exec_cmd(tp, cmd);
3349 if (ret)
3350 break;
3351 }
3352 if (!tg3_flag(tp, FLASH)) {
3353 /* We always do complete word writes to eeprom. */
3354 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3355 }
3356
3357 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3358 if (ret)
3359 break;
3360 }
3361 return ret;
3362}
3363
3364/* offset and length are dword aligned */
3365static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3366{
3367 int ret;
3368
3369 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3370 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3371 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3372 udelay(40);
3373 }
3374
3375 if (!tg3_flag(tp, NVRAM)) {
3376 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3377 } else {
3378 u32 grc_mode;
3379
3380 ret = tg3_nvram_lock(tp);
3381 if (ret)
3382 return ret;
3383
3384 tg3_enable_nvram_access(tp);
3385 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3386 tw32(NVRAM_WRITE1, 0x406);
3387
3388 grc_mode = tr32(GRC_MODE);
3389 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3390
3391 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3392 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3393 buf);
3394 } else {
3395 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3396 buf);
3397 }
3398
3399 grc_mode = tr32(GRC_MODE);
3400 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3401
3402 tg3_disable_nvram_access(tp);
3403 tg3_nvram_unlock(tp);
3404 }
3405
3406 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3407 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3408 udelay(40);
3409 }
3410
3411 return ret;
3412}
3413
Matt Carlson997b4f12011-08-31 11:44:53 +00003414#define RX_CPU_SCRATCH_BASE 0x30000
3415#define RX_CPU_SCRATCH_SIZE 0x04000
3416#define TX_CPU_SCRATCH_BASE 0x34000
3417#define TX_CPU_SCRATCH_SIZE 0x04000
3418
3419/* tp->lock is held. */
3420static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
3421{
3422 int i;
3423
3424 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
3425
3426 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3427 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3428
3429 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3430 return 0;
3431 }
3432 if (offset == RX_CPU_BASE) {
3433 for (i = 0; i < 10000; i++) {
3434 tw32(offset + CPU_STATE, 0xffffffff);
3435 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3436 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3437 break;
3438 }
3439
3440 tw32(offset + CPU_STATE, 0xffffffff);
3441 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3442 udelay(10);
3443 } else {
3444 for (i = 0; i < 10000; i++) {
3445 tw32(offset + CPU_STATE, 0xffffffff);
3446 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3447 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3448 break;
3449 }
3450 }
3451
3452 if (i >= 10000) {
3453 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3454 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3455 return -ENODEV;
3456 }
3457
3458 /* Clear firmware's nvram arbitration. */
3459 if (tg3_flag(tp, NVRAM))
3460 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3461 return 0;
3462}
3463
3464struct fw_info {
3465 unsigned int fw_base;
3466 unsigned int fw_len;
3467 const __be32 *fw_data;
3468};
3469
3470/* tp->lock is held. */
3471static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3472 u32 cpu_scratch_base, int cpu_scratch_size,
3473 struct fw_info *info)
3474{
3475 int err, lock_err, i;
3476 void (*write_op)(struct tg3 *, u32, u32);
3477
3478 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3479 netdev_err(tp->dev,
3480 "%s: Trying to load TX cpu firmware which is 5705\n",
3481 __func__);
3482 return -EINVAL;
3483 }
3484
3485 if (tg3_flag(tp, 5705_PLUS))
3486 write_op = tg3_write_mem;
3487 else
3488 write_op = tg3_write_indirect_reg32;
3489
3490 /* It is possible that bootcode is still loading at this point.
3491 * Get the nvram lock first before halting the cpu.
3492 */
3493 lock_err = tg3_nvram_lock(tp);
3494 err = tg3_halt_cpu(tp, cpu_base);
3495 if (!lock_err)
3496 tg3_nvram_unlock(tp);
3497 if (err)
3498 goto out;
3499
3500 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3501 write_op(tp, cpu_scratch_base + i, 0);
3502 tw32(cpu_base + CPU_STATE, 0xffffffff);
3503 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3504 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3505 write_op(tp, (cpu_scratch_base +
3506 (info->fw_base & 0xffff) +
3507 (i * sizeof(u32))),
3508 be32_to_cpu(info->fw_data[i]));
3509
3510 err = 0;
3511
3512out:
3513 return err;
3514}
3515
3516/* tp->lock is held. */
3517static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3518{
3519 struct fw_info info;
3520 const __be32 *fw_data;
3521 int err, i;
3522
3523 fw_data = (void *)tp->fw->data;
3524
3525 /* Firmware blob starts with version numbers, followed by
3526 start address and length. We are setting complete length.
3527 length = end_address_of_bss - start_address_of_text.
3528 Remainder is the blob to be loaded contiguously
3529 from start address. */
3530
3531 info.fw_base = be32_to_cpu(fw_data[1]);
3532 info.fw_len = tp->fw->size - 12;
3533 info.fw_data = &fw_data[3];
3534
3535 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3536 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3537 &info);
3538 if (err)
3539 return err;
3540
3541 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3542 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3543 &info);
3544 if (err)
3545 return err;
3546
3547 /* Now startup only the RX cpu. */
3548 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3549 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3550
3551 for (i = 0; i < 5; i++) {
3552 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3553 break;
3554 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3555 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3556 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3557 udelay(1000);
3558 }
3559 if (i >= 5) {
3560 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3561 "should be %08x\n", __func__,
3562 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3563 return -ENODEV;
3564 }
3565 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3566 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3567
3568 return 0;
3569}
3570
3571/* tp->lock is held. */
3572static int tg3_load_tso_firmware(struct tg3 *tp)
3573{
3574 struct fw_info info;
3575 const __be32 *fw_data;
3576 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3577 int err, i;
3578
3579 if (tg3_flag(tp, HW_TSO_1) ||
3580 tg3_flag(tp, HW_TSO_2) ||
3581 tg3_flag(tp, HW_TSO_3))
3582 return 0;
3583
3584 fw_data = (void *)tp->fw->data;
3585
3586 /* Firmware blob starts with version numbers, followed by
3587 start address and length. We are setting complete length.
3588 length = end_address_of_bss - start_address_of_text.
3589 Remainder is the blob to be loaded contiguously
3590 from start address. */
3591
3592 info.fw_base = be32_to_cpu(fw_data[1]);
3593 cpu_scratch_size = tp->fw_len;
3594 info.fw_len = tp->fw->size - 12;
3595 info.fw_data = &fw_data[3];
3596
3597 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
3598 cpu_base = RX_CPU_BASE;
3599 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3600 } else {
3601 cpu_base = TX_CPU_BASE;
3602 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3603 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3604 }
3605
3606 err = tg3_load_firmware_cpu(tp, cpu_base,
3607 cpu_scratch_base, cpu_scratch_size,
3608 &info);
3609 if (err)
3610 return err;
3611
3612 /* Now startup the cpu. */
3613 tw32(cpu_base + CPU_STATE, 0xffffffff);
3614 tw32_f(cpu_base + CPU_PC, info.fw_base);
3615
3616 for (i = 0; i < 5; i++) {
3617 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3618 break;
3619 tw32(cpu_base + CPU_STATE, 0xffffffff);
3620 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3621 tw32_f(cpu_base + CPU_PC, info.fw_base);
3622 udelay(1000);
3623 }
3624 if (i >= 5) {
3625 netdev_err(tp->dev,
3626 "%s fails to set CPU PC, is %08x should be %08x\n",
3627 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3628 return -ENODEV;
3629 }
3630 tw32(cpu_base + CPU_STATE, 0xffffffff);
3631 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3632 return 0;
3633}
3634
3635
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003636/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003637static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3638{
3639 u32 addr_high, addr_low;
3640 int i;
3641
3642 addr_high = ((tp->dev->dev_addr[0] << 8) |
3643 tp->dev->dev_addr[1]);
3644 addr_low = ((tp->dev->dev_addr[2] << 24) |
3645 (tp->dev->dev_addr[3] << 16) |
3646 (tp->dev->dev_addr[4] << 8) |
3647 (tp->dev->dev_addr[5] << 0));
3648 for (i = 0; i < 4; i++) {
3649 if (i == 1 && skip_mac_1)
3650 continue;
3651 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3652 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3653 }
3654
3655 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3656 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
3657 for (i = 0; i < 12; i++) {
3658 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3659 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3660 }
3661 }
3662
3663 addr_high = (tp->dev->dev_addr[0] +
3664 tp->dev->dev_addr[1] +
3665 tp->dev->dev_addr[2] +
3666 tp->dev->dev_addr[3] +
3667 tp->dev->dev_addr[4] +
3668 tp->dev->dev_addr[5]) &
3669 TX_BACKOFF_SEED_MASK;
3670 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3671}
3672
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003673static void tg3_enable_register_access(struct tg3 *tp)
3674{
3675 /*
3676 * Make sure register accesses (indirect or otherwise) will function
3677 * correctly.
3678 */
3679 pci_write_config_dword(tp->pdev,
3680 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3681}
3682
3683static int tg3_power_up(struct tg3 *tp)
3684{
Matt Carlsonbed98292011-07-13 09:27:29 +00003685 int err;
3686
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003687 tg3_enable_register_access(tp);
3688
Matt Carlsonbed98292011-07-13 09:27:29 +00003689 err = pci_set_power_state(tp->pdev, PCI_D0);
3690 if (!err) {
3691 /* Switch out of Vaux if it is a NIC */
3692 tg3_pwrsrc_switch_to_vmain(tp);
3693 } else {
3694 netdev_err(tp->dev, "Transition to D0 failed\n");
3695 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003696
Matt Carlsonbed98292011-07-13 09:27:29 +00003697 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003698}
3699
Matt Carlson4b409522012-02-13 10:20:11 +00003700static int tg3_setup_phy(struct tg3 *, int);
3701
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003702static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703{
3704 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003705 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003707 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003708
3709 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06003710 if (tg3_flag(tp, CLKREQ_BUG))
3711 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
3712 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003713
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3715 tw32(TG3PCI_MISC_HOST_CTRL,
3716 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3717
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003718 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003719 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003720
Joe Perches63c3a662011-04-26 08:12:10 +00003721 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003722 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003723 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003724 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003725 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003726 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003727
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003728 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003729
Matt Carlson80096062010-08-02 11:26:06 +00003730 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003731
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003732 tp->link_config.speed = phydev->speed;
3733 tp->link_config.duplex = phydev->duplex;
3734 tp->link_config.autoneg = phydev->autoneg;
3735 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003736
3737 advertising = ADVERTISED_TP |
3738 ADVERTISED_Pause |
3739 ADVERTISED_Autoneg |
3740 ADVERTISED_10baseT_Half;
3741
Joe Perches63c3a662011-04-26 08:12:10 +00003742 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3743 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003744 advertising |=
3745 ADVERTISED_100baseT_Half |
3746 ADVERTISED_100baseT_Full |
3747 ADVERTISED_10baseT_Full;
3748 else
3749 advertising |= ADVERTISED_10baseT_Full;
3750 }
3751
3752 phydev->advertising = advertising;
3753
3754 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003755
3756 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003757 if (phyid != PHY_ID_BCMAC131) {
3758 phyid &= PHY_BCM_OUI_MASK;
3759 if (phyid == PHY_BCM_OUI_1 ||
3760 phyid == PHY_BCM_OUI_2 ||
3761 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003762 do_low_power = true;
3763 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003764 }
Matt Carlsondd477002008-05-25 23:45:58 -07003765 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003766 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003767
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003768 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00003769 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Matt Carlson2855b9f2012-02-13 15:20:14 +00003771 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlsondd477002008-05-25 23:45:58 -07003772 tg3_setup_phy(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 }
3774
Michael Chanb5d37722006-09-27 16:06:21 -07003775 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3776 u32 val;
3777
3778 val = tr32(GRC_VCPU_EXT_CTRL);
3779 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003780 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003781 int i;
3782 u32 val;
3783
3784 for (i = 0; i < 200; i++) {
3785 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3786 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3787 break;
3788 msleep(1);
3789 }
3790 }
Joe Perches63c3a662011-04-26 08:12:10 +00003791 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003792 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3793 WOL_DRV_STATE_SHUTDOWN |
3794 WOL_DRV_WOL |
3795 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003796
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003797 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 u32 mac_mode;
3799
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003800 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003801 if (do_low_power &&
3802 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3803 tg3_phy_auxctl_write(tp,
3804 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3805 MII_TG3_AUXCTL_PCTL_WOL_EN |
3806 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3807 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003808 udelay(40);
3809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003811 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003812 mac_mode = MAC_MODE_PORT_MODE_GMII;
3813 else
3814 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003816 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3817 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3818 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003819 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003820 SPEED_100 : SPEED_10;
3821 if (tg3_5700_link_polarity(tp, speed))
3822 mac_mode |= MAC_MODE_LINK_POLARITY;
3823 else
3824 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 } else {
3827 mac_mode = MAC_MODE_PORT_MODE_TBI;
3828 }
3829
Joe Perches63c3a662011-04-26 08:12:10 +00003830 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 tw32(MAC_LED_CTRL, tp->led_ctrl);
3832
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003833 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003834 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3835 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003836 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
Joe Perches63c3a662011-04-26 08:12:10 +00003838 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003839 mac_mode |= MAC_MODE_APE_TX_EN |
3840 MAC_MODE_APE_RX_EN |
3841 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003842
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 tw32_f(MAC_MODE, mac_mode);
3844 udelay(100);
3845
3846 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3847 udelay(10);
3848 }
3849
Joe Perches63c3a662011-04-26 08:12:10 +00003850 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3852 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3853 u32 base_val;
3854
3855 base_val = tp->pci_clock_ctrl;
3856 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3857 CLOCK_CTRL_TXCLK_DISABLE);
3858
Michael Chanb401e9e2005-12-19 16:27:04 -08003859 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3860 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003861 } else if (tg3_flag(tp, 5780_CLASS) ||
3862 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003863 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003864 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003865 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 u32 newbits1, newbits2;
3867
3868 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3869 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3870 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3871 CLOCK_CTRL_TXCLK_DISABLE |
3872 CLOCK_CTRL_ALTCLK);
3873 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003874 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 newbits1 = CLOCK_CTRL_625_CORE;
3876 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3877 } else {
3878 newbits1 = CLOCK_CTRL_ALTCLK;
3879 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3880 }
3881
Michael Chanb401e9e2005-12-19 16:27:04 -08003882 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3883 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
Michael Chanb401e9e2005-12-19 16:27:04 -08003885 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3886 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887
Joe Perches63c3a662011-04-26 08:12:10 +00003888 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 u32 newbits3;
3890
3891 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3892 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3893 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3894 CLOCK_CTRL_TXCLK_DISABLE |
3895 CLOCK_CTRL_44MHZ_CORE);
3896 } else {
3897 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3898 }
3899
Michael Chanb401e9e2005-12-19 16:27:04 -08003900 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3901 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 }
3903 }
3904
Joe Perches63c3a662011-04-26 08:12:10 +00003905 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003906 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003907
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003908 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909
3910 /* Workaround for unstable PLL clock */
3911 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3912 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3913 u32 val = tr32(0x7d00);
3914
3915 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3916 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003917 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003918 int err;
3919
3920 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003922 if (!err)
3923 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 }
3926
Michael Chanbbadf502006-04-06 21:46:34 -07003927 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3928
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 return 0;
3930}
3931
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003932static void tg3_power_down(struct tg3 *tp)
3933{
3934 tg3_power_down_prepare(tp);
3935
Joe Perches63c3a662011-04-26 08:12:10 +00003936 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003937 pci_set_power_state(tp->pdev, PCI_D3hot);
3938}
3939
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3941{
3942 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3943 case MII_TG3_AUX_STAT_10HALF:
3944 *speed = SPEED_10;
3945 *duplex = DUPLEX_HALF;
3946 break;
3947
3948 case MII_TG3_AUX_STAT_10FULL:
3949 *speed = SPEED_10;
3950 *duplex = DUPLEX_FULL;
3951 break;
3952
3953 case MII_TG3_AUX_STAT_100HALF:
3954 *speed = SPEED_100;
3955 *duplex = DUPLEX_HALF;
3956 break;
3957
3958 case MII_TG3_AUX_STAT_100FULL:
3959 *speed = SPEED_100;
3960 *duplex = DUPLEX_FULL;
3961 break;
3962
3963 case MII_TG3_AUX_STAT_1000HALF:
3964 *speed = SPEED_1000;
3965 *duplex = DUPLEX_HALF;
3966 break;
3967
3968 case MII_TG3_AUX_STAT_1000FULL:
3969 *speed = SPEED_1000;
3970 *duplex = DUPLEX_FULL;
3971 break;
3972
3973 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003974 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003975 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3976 SPEED_10;
3977 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3978 DUPLEX_HALF;
3979 break;
3980 }
Matt Carlsone7405222012-02-13 15:20:16 +00003981 *speed = SPEED_UNKNOWN;
3982 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985}
3986
Matt Carlson42b64a42011-05-19 12:12:49 +00003987static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988{
Matt Carlson42b64a42011-05-19 12:12:49 +00003989 int err = 0;
3990 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Matt Carlson42b64a42011-05-19 12:12:49 +00003992 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003993 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00003994 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Matt Carlson42b64a42011-05-19 12:12:49 +00003996 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3997 if (err)
3998 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999
Matt Carlson4f272092011-12-14 11:09:57 +00004000 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4001 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004002
Matt Carlson4f272092011-12-14 11:09:57 +00004003 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4004 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
4005 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004006
Matt Carlson4f272092011-12-14 11:09:57 +00004007 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4008 if (err)
4009 goto done;
4010 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004011
Matt Carlson42b64a42011-05-19 12:12:49 +00004012 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4013 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
Matt Carlson42b64a42011-05-19 12:12:49 +00004015 tw32(TG3_CPMU_EEE_MODE,
4016 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004017
Matt Carlson42b64a42011-05-19 12:12:49 +00004018 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
4019 if (!err) {
4020 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004021
Matt Carlsona6b68da2010-12-06 08:28:52 +00004022 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004023 /* Advertise 100-BaseTX EEE ability */
4024 if (advertise & ADVERTISED_100baseT_Full)
4025 val |= MDIO_AN_EEE_ADV_100TX;
4026 /* Advertise 1000-BaseT EEE ability */
4027 if (advertise & ADVERTISED_1000baseT_Full)
4028 val |= MDIO_AN_EEE_ADV_1000T;
4029 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004030 if (err)
4031 val = 0;
4032
4033 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
4034 case ASIC_REV_5717:
4035 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004036 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004037 case ASIC_REV_5719:
4038 /* If we advertised any eee advertisements above... */
4039 if (val)
4040 val = MII_TG3_DSP_TAP26_ALNOKO |
4041 MII_TG3_DSP_TAP26_RMRXSTO |
4042 MII_TG3_DSP_TAP26_OPCSINPT;
4043 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4044 /* Fall through */
4045 case ASIC_REV_5720:
Michael Chanc65a17f2013-01-06 12:51:07 +00004046 case ASIC_REV_5762:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004047 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4048 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4049 MII_TG3_DSP_CH34TP2_HIBW01);
4050 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004051
Matt Carlson42b64a42011-05-19 12:12:49 +00004052 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
4053 if (!err)
4054 err = err2;
4055 }
4056
4057done:
4058 return err;
4059}
4060
4061static void tg3_phy_copper_begin(struct tg3 *tp)
4062{
Matt Carlsond13ba512012-02-22 12:35:19 +00004063 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4064 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4065 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004066
Matt Carlsond13ba512012-02-22 12:35:19 +00004067 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
4068 adv = ADVERTISED_10baseT_Half |
4069 ADVERTISED_10baseT_Full;
4070 if (tg3_flag(tp, WOL_SPEED_100MB))
4071 adv |= ADVERTISED_100baseT_Half |
4072 ADVERTISED_100baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00004073
Matt Carlsond13ba512012-02-22 12:35:19 +00004074 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004075 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004076 adv = tp->link_config.advertising;
4077 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4078 adv &= ~(ADVERTISED_1000baseT_Half |
4079 ADVERTISED_1000baseT_Full);
4080
4081 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004082 }
4083
Matt Carlsond13ba512012-02-22 12:35:19 +00004084 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004085
Matt Carlsond13ba512012-02-22 12:35:19 +00004086 tg3_writephy(tp, MII_BMCR,
4087 BMCR_ANENABLE | BMCR_ANRESTART);
4088 } else {
4089 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 u32 bmcr, orig_bmcr;
4091
4092 tp->link_config.active_speed = tp->link_config.speed;
4093 tp->link_config.active_duplex = tp->link_config.duplex;
4094
4095 bmcr = 0;
4096 switch (tp->link_config.speed) {
4097 default:
4098 case SPEED_10:
4099 break;
4100
4101 case SPEED_100:
4102 bmcr |= BMCR_SPEED100;
4103 break;
4104
4105 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004106 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
4110 if (tp->link_config.duplex == DUPLEX_FULL)
4111 bmcr |= BMCR_FULLDPLX;
4112
4113 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4114 (bmcr != orig_bmcr)) {
4115 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4116 for (i = 0; i < 1500; i++) {
4117 u32 tmp;
4118
4119 udelay(10);
4120 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4121 tg3_readphy(tp, MII_BMSR, &tmp))
4122 continue;
4123 if (!(tmp & BMSR_LSTATUS)) {
4124 udelay(40);
4125 break;
4126 }
4127 }
4128 tg3_writephy(tp, MII_BMCR, bmcr);
4129 udelay(40);
4130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 }
4132}
4133
4134static int tg3_init_5401phy_dsp(struct tg3 *tp)
4135{
4136 int err;
4137
4138 /* Turn off tap power management. */
4139 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004140 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004142 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4143 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4144 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4145 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4146 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
4148 udelay(40);
4149
4150 return err;
4151}
4152
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004153static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004155 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004156
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004157 advertising = tp->link_config.advertising;
4158 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004160 advmsk = ADVERTISE_ALL;
4161 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004162 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004163 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004166 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4167 return false;
4168
4169 if ((*lcladv & advmsk) != tgtadv)
4170 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004171
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004172 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 u32 tg3_ctrl;
4174
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004175 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004176
Matt Carlson221c5632011-06-13 13:39:01 +00004177 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004178 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
Matt Carlson3198e072012-02-13 15:20:10 +00004180 if (tgtadv &&
4181 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4182 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)) {
4183 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4184 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4185 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4186 } else {
4187 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4188 }
4189
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004190 if (tg3_ctrl != tgtadv)
4191 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004193
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004194 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004195}
4196
Matt Carlson859edb22011-12-08 14:40:16 +00004197static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4198{
4199 u32 lpeth = 0;
4200
4201 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4202 u32 val;
4203
4204 if (tg3_readphy(tp, MII_STAT1000, &val))
4205 return false;
4206
4207 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4208 }
4209
4210 if (tg3_readphy(tp, MII_LPA, rmtadv))
4211 return false;
4212
4213 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4214 tp->link_config.rmt_adv = lpeth;
4215
4216 return true;
4217}
4218
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004219static bool tg3_test_and_report_link_chg(struct tg3 *tp, int curr_link_up)
4220{
4221 if (curr_link_up != tp->link_up) {
4222 if (curr_link_up) {
4223 tg3_carrier_on(tp);
4224 } else {
4225 tg3_carrier_off(tp);
4226 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4227 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4228 }
4229
4230 tg3_link_report(tp);
4231 return true;
4232 }
4233
4234 return false;
4235}
4236
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
4238{
4239 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004240 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004241 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 u16 current_speed;
4243 u8 current_duplex;
4244 int i, err;
4245
4246 tw32(MAC_EVENT, 0);
4247
4248 tw32_f(MAC_STATUS,
4249 (MAC_STATUS_SYNC_CHANGED |
4250 MAC_STATUS_CFG_CHANGED |
4251 MAC_STATUS_MI_COMPLETION |
4252 MAC_STATUS_LNKSTATE_CHANGED));
4253 udelay(40);
4254
Matt Carlson8ef21422008-05-02 16:47:53 -07004255 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4256 tw32_f(MAC_MI_MODE,
4257 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4258 udelay(80);
4259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004261 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262
4263 /* Some third-party PHYs need to be reset on link going
4264 * down.
4265 */
4266 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
4267 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
4268 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004269 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 tg3_readphy(tp, MII_BMSR, &bmsr);
4271 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4272 !(bmsr & BMSR_LSTATUS))
4273 force_reset = 1;
4274 }
4275 if (force_reset)
4276 tg3_phy_reset(tp);
4277
Matt Carlson79eb6902010-02-17 15:17:03 +00004278 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 tg3_readphy(tp, MII_BMSR, &bmsr);
4280 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004281 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 bmsr = 0;
4283
4284 if (!(bmsr & BMSR_LSTATUS)) {
4285 err = tg3_init_5401phy_dsp(tp);
4286 if (err)
4287 return err;
4288
4289 tg3_readphy(tp, MII_BMSR, &bmsr);
4290 for (i = 0; i < 1000; i++) {
4291 udelay(10);
4292 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4293 (bmsr & BMSR_LSTATUS)) {
4294 udelay(40);
4295 break;
4296 }
4297 }
4298
Matt Carlson79eb6902010-02-17 15:17:03 +00004299 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4300 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 !(bmsr & BMSR_LSTATUS) &&
4302 tp->link_config.active_speed == SPEED_1000) {
4303 err = tg3_phy_reset(tp);
4304 if (!err)
4305 err = tg3_init_5401phy_dsp(tp);
4306 if (err)
4307 return err;
4308 }
4309 }
4310 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4311 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
4312 /* 5701 {A0,B0} CRC bug workaround */
4313 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004314 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4315 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4316 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 }
4318
4319 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004320 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4321 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004323 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004325 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4327
4328 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
4329 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
4330 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4331 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4332 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4333 else
4334 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4335 }
4336
4337 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00004338 current_speed = SPEED_UNKNOWN;
4339 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004340 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004341 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004343 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004344 err = tg3_phy_auxctl_read(tp,
4345 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4346 &val);
4347 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004348 tg3_phy_auxctl_write(tp,
4349 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4350 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 goto relink;
4352 }
4353 }
4354
4355 bmsr = 0;
4356 for (i = 0; i < 100; i++) {
4357 tg3_readphy(tp, MII_BMSR, &bmsr);
4358 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4359 (bmsr & BMSR_LSTATUS))
4360 break;
4361 udelay(40);
4362 }
4363
4364 if (bmsr & BMSR_LSTATUS) {
4365 u32 aux_stat, bmcr;
4366
4367 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4368 for (i = 0; i < 2000; i++) {
4369 udelay(10);
4370 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4371 aux_stat)
4372 break;
4373 }
4374
4375 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4376 &current_speed,
4377 &current_duplex);
4378
4379 bmcr = 0;
4380 for (i = 0; i < 200; i++) {
4381 tg3_readphy(tp, MII_BMCR, &bmcr);
4382 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4383 continue;
4384 if (bmcr && bmcr != 0x7fff)
4385 break;
4386 udelay(10);
4387 }
4388
Matt Carlsonef167e22007-12-20 20:10:01 -08004389 lcl_adv = 0;
4390 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391
Matt Carlsonef167e22007-12-20 20:10:01 -08004392 tp->link_config.active_speed = current_speed;
4393 tp->link_config.active_duplex = current_duplex;
4394
4395 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4396 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004397 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004398 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004399 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 } else {
4401 if (!(bmcr & BMCR_ANENABLE) &&
4402 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004403 tp->link_config.duplex == current_duplex &&
4404 tp->link_config.flowctrl ==
4405 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 }
4408 }
4409
Matt Carlsonef167e22007-12-20 20:10:01 -08004410 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004411 tp->link_config.active_duplex == DUPLEX_FULL) {
4412 u32 reg, bit;
4413
4414 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4415 reg = MII_TG3_FET_GEN_STAT;
4416 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4417 } else {
4418 reg = MII_TG3_EXT_STAT;
4419 bit = MII_TG3_EXT_STAT_MDIX;
4420 }
4421
4422 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4423 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4424
Matt Carlsonef167e22007-12-20 20:10:01 -08004425 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 }
4428
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429relink:
Matt Carlson80096062010-08-02 11:26:06 +00004430 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 tg3_phy_copper_begin(tp);
4432
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004433 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004434 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4435 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 current_link_up = 1;
4437 }
4438
4439 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4440 if (current_link_up == 1) {
4441 if (tp->link_config.active_speed == SPEED_100 ||
4442 tp->link_config.active_speed == SPEED_10)
4443 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4444 else
4445 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004446 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004447 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4448 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4450
4451 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4452 if (tp->link_config.active_duplex == DUPLEX_HALF)
4453 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4454
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004456 if (current_link_up == 1 &&
4457 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004459 else
4460 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 }
4462
4463 /* ??? Without this setting Netgear GA302T PHY does not
4464 * ??? send/receive packets...
4465 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004466 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4468 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4469 tw32_f(MAC_MI_MODE, tp->mi_mode);
4470 udelay(80);
4471 }
4472
4473 tw32_f(MAC_MODE, tp->mac_mode);
4474 udelay(40);
4475
Matt Carlson52b02d02010-10-14 10:37:41 +00004476 tg3_phy_eee_adjust(tp, current_link_up);
4477
Joe Perches63c3a662011-04-26 08:12:10 +00004478 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 /* Polled via timer. */
4480 tw32_f(MAC_EVENT, 0);
4481 } else {
4482 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4483 }
4484 udelay(40);
4485
4486 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4487 current_link_up == 1 &&
4488 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004489 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 udelay(120);
4491 tw32_f(MAC_STATUS,
4492 (MAC_STATUS_SYNC_CHANGED |
4493 MAC_STATUS_CFG_CHANGED));
4494 udelay(40);
4495 tg3_write_mem(tp,
4496 NIC_SRAM_FIRMWARE_MBOX,
4497 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4498 }
4499
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004500 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004501 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004502 if (tp->link_config.active_speed == SPEED_100 ||
4503 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004504 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
4505 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004506 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004507 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4508 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004509 }
4510
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004511 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
4513 return 0;
4514}
4515
4516struct tg3_fiber_aneginfo {
4517 int state;
4518#define ANEG_STATE_UNKNOWN 0
4519#define ANEG_STATE_AN_ENABLE 1
4520#define ANEG_STATE_RESTART_INIT 2
4521#define ANEG_STATE_RESTART 3
4522#define ANEG_STATE_DISABLE_LINK_OK 4
4523#define ANEG_STATE_ABILITY_DETECT_INIT 5
4524#define ANEG_STATE_ABILITY_DETECT 6
4525#define ANEG_STATE_ACK_DETECT_INIT 7
4526#define ANEG_STATE_ACK_DETECT 8
4527#define ANEG_STATE_COMPLETE_ACK_INIT 9
4528#define ANEG_STATE_COMPLETE_ACK 10
4529#define ANEG_STATE_IDLE_DETECT_INIT 11
4530#define ANEG_STATE_IDLE_DETECT 12
4531#define ANEG_STATE_LINK_OK 13
4532#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4533#define ANEG_STATE_NEXT_PAGE_WAIT 15
4534
4535 u32 flags;
4536#define MR_AN_ENABLE 0x00000001
4537#define MR_RESTART_AN 0x00000002
4538#define MR_AN_COMPLETE 0x00000004
4539#define MR_PAGE_RX 0x00000008
4540#define MR_NP_LOADED 0x00000010
4541#define MR_TOGGLE_TX 0x00000020
4542#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4543#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4544#define MR_LP_ADV_SYM_PAUSE 0x00000100
4545#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4546#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4547#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4548#define MR_LP_ADV_NEXT_PAGE 0x00001000
4549#define MR_TOGGLE_RX 0x00002000
4550#define MR_NP_RX 0x00004000
4551
4552#define MR_LINK_OK 0x80000000
4553
4554 unsigned long link_time, cur_time;
4555
4556 u32 ability_match_cfg;
4557 int ability_match_count;
4558
4559 char ability_match, idle_match, ack_match;
4560
4561 u32 txconfig, rxconfig;
4562#define ANEG_CFG_NP 0x00000080
4563#define ANEG_CFG_ACK 0x00000040
4564#define ANEG_CFG_RF2 0x00000020
4565#define ANEG_CFG_RF1 0x00000010
4566#define ANEG_CFG_PS2 0x00000001
4567#define ANEG_CFG_PS1 0x00008000
4568#define ANEG_CFG_HD 0x00004000
4569#define ANEG_CFG_FD 0x00002000
4570#define ANEG_CFG_INVAL 0x00001f06
4571
4572};
4573#define ANEG_OK 0
4574#define ANEG_DONE 1
4575#define ANEG_TIMER_ENAB 2
4576#define ANEG_FAILED -1
4577
4578#define ANEG_STATE_SETTLE_TIME 10000
4579
4580static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4581 struct tg3_fiber_aneginfo *ap)
4582{
Matt Carlson5be73b42007-12-20 20:09:29 -08004583 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 unsigned long delta;
4585 u32 rx_cfg_reg;
4586 int ret;
4587
4588 if (ap->state == ANEG_STATE_UNKNOWN) {
4589 ap->rxconfig = 0;
4590 ap->link_time = 0;
4591 ap->cur_time = 0;
4592 ap->ability_match_cfg = 0;
4593 ap->ability_match_count = 0;
4594 ap->ability_match = 0;
4595 ap->idle_match = 0;
4596 ap->ack_match = 0;
4597 }
4598 ap->cur_time++;
4599
4600 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4601 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4602
4603 if (rx_cfg_reg != ap->ability_match_cfg) {
4604 ap->ability_match_cfg = rx_cfg_reg;
4605 ap->ability_match = 0;
4606 ap->ability_match_count = 0;
4607 } else {
4608 if (++ap->ability_match_count > 1) {
4609 ap->ability_match = 1;
4610 ap->ability_match_cfg = rx_cfg_reg;
4611 }
4612 }
4613 if (rx_cfg_reg & ANEG_CFG_ACK)
4614 ap->ack_match = 1;
4615 else
4616 ap->ack_match = 0;
4617
4618 ap->idle_match = 0;
4619 } else {
4620 ap->idle_match = 1;
4621 ap->ability_match_cfg = 0;
4622 ap->ability_match_count = 0;
4623 ap->ability_match = 0;
4624 ap->ack_match = 0;
4625
4626 rx_cfg_reg = 0;
4627 }
4628
4629 ap->rxconfig = rx_cfg_reg;
4630 ret = ANEG_OK;
4631
Matt Carlson33f401a2010-04-05 10:19:27 +00004632 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 case ANEG_STATE_UNKNOWN:
4634 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4635 ap->state = ANEG_STATE_AN_ENABLE;
4636
4637 /* fallthru */
4638 case ANEG_STATE_AN_ENABLE:
4639 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4640 if (ap->flags & MR_AN_ENABLE) {
4641 ap->link_time = 0;
4642 ap->cur_time = 0;
4643 ap->ability_match_cfg = 0;
4644 ap->ability_match_count = 0;
4645 ap->ability_match = 0;
4646 ap->idle_match = 0;
4647 ap->ack_match = 0;
4648
4649 ap->state = ANEG_STATE_RESTART_INIT;
4650 } else {
4651 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4652 }
4653 break;
4654
4655 case ANEG_STATE_RESTART_INIT:
4656 ap->link_time = ap->cur_time;
4657 ap->flags &= ~(MR_NP_LOADED);
4658 ap->txconfig = 0;
4659 tw32(MAC_TX_AUTO_NEG, 0);
4660 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4661 tw32_f(MAC_MODE, tp->mac_mode);
4662 udelay(40);
4663
4664 ret = ANEG_TIMER_ENAB;
4665 ap->state = ANEG_STATE_RESTART;
4666
4667 /* fallthru */
4668 case ANEG_STATE_RESTART:
4669 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004670 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004672 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 break;
4675
4676 case ANEG_STATE_DISABLE_LINK_OK:
4677 ret = ANEG_DONE;
4678 break;
4679
4680 case ANEG_STATE_ABILITY_DETECT_INIT:
4681 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004682 ap->txconfig = ANEG_CFG_FD;
4683 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4684 if (flowctrl & ADVERTISE_1000XPAUSE)
4685 ap->txconfig |= ANEG_CFG_PS1;
4686 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4687 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4689 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4690 tw32_f(MAC_MODE, tp->mac_mode);
4691 udelay(40);
4692
4693 ap->state = ANEG_STATE_ABILITY_DETECT;
4694 break;
4695
4696 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004697 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 break;
4700
4701 case ANEG_STATE_ACK_DETECT_INIT:
4702 ap->txconfig |= ANEG_CFG_ACK;
4703 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4704 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4705 tw32_f(MAC_MODE, tp->mac_mode);
4706 udelay(40);
4707
4708 ap->state = ANEG_STATE_ACK_DETECT;
4709
4710 /* fallthru */
4711 case ANEG_STATE_ACK_DETECT:
4712 if (ap->ack_match != 0) {
4713 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4714 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4715 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4716 } else {
4717 ap->state = ANEG_STATE_AN_ENABLE;
4718 }
4719 } else if (ap->ability_match != 0 &&
4720 ap->rxconfig == 0) {
4721 ap->state = ANEG_STATE_AN_ENABLE;
4722 }
4723 break;
4724
4725 case ANEG_STATE_COMPLETE_ACK_INIT:
4726 if (ap->rxconfig & ANEG_CFG_INVAL) {
4727 ret = ANEG_FAILED;
4728 break;
4729 }
4730 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4731 MR_LP_ADV_HALF_DUPLEX |
4732 MR_LP_ADV_SYM_PAUSE |
4733 MR_LP_ADV_ASYM_PAUSE |
4734 MR_LP_ADV_REMOTE_FAULT1 |
4735 MR_LP_ADV_REMOTE_FAULT2 |
4736 MR_LP_ADV_NEXT_PAGE |
4737 MR_TOGGLE_RX |
4738 MR_NP_RX);
4739 if (ap->rxconfig & ANEG_CFG_FD)
4740 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4741 if (ap->rxconfig & ANEG_CFG_HD)
4742 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4743 if (ap->rxconfig & ANEG_CFG_PS1)
4744 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4745 if (ap->rxconfig & ANEG_CFG_PS2)
4746 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4747 if (ap->rxconfig & ANEG_CFG_RF1)
4748 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4749 if (ap->rxconfig & ANEG_CFG_RF2)
4750 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4751 if (ap->rxconfig & ANEG_CFG_NP)
4752 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4753
4754 ap->link_time = ap->cur_time;
4755
4756 ap->flags ^= (MR_TOGGLE_TX);
4757 if (ap->rxconfig & 0x0008)
4758 ap->flags |= MR_TOGGLE_RX;
4759 if (ap->rxconfig & ANEG_CFG_NP)
4760 ap->flags |= MR_NP_RX;
4761 ap->flags |= MR_PAGE_RX;
4762
4763 ap->state = ANEG_STATE_COMPLETE_ACK;
4764 ret = ANEG_TIMER_ENAB;
4765 break;
4766
4767 case ANEG_STATE_COMPLETE_ACK:
4768 if (ap->ability_match != 0 &&
4769 ap->rxconfig == 0) {
4770 ap->state = ANEG_STATE_AN_ENABLE;
4771 break;
4772 }
4773 delta = ap->cur_time - ap->link_time;
4774 if (delta > ANEG_STATE_SETTLE_TIME) {
4775 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4776 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4777 } else {
4778 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4779 !(ap->flags & MR_NP_RX)) {
4780 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4781 } else {
4782 ret = ANEG_FAILED;
4783 }
4784 }
4785 }
4786 break;
4787
4788 case ANEG_STATE_IDLE_DETECT_INIT:
4789 ap->link_time = ap->cur_time;
4790 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4791 tw32_f(MAC_MODE, tp->mac_mode);
4792 udelay(40);
4793
4794 ap->state = ANEG_STATE_IDLE_DETECT;
4795 ret = ANEG_TIMER_ENAB;
4796 break;
4797
4798 case ANEG_STATE_IDLE_DETECT:
4799 if (ap->ability_match != 0 &&
4800 ap->rxconfig == 0) {
4801 ap->state = ANEG_STATE_AN_ENABLE;
4802 break;
4803 }
4804 delta = ap->cur_time - ap->link_time;
4805 if (delta > ANEG_STATE_SETTLE_TIME) {
4806 /* XXX another gem from the Broadcom driver :( */
4807 ap->state = ANEG_STATE_LINK_OK;
4808 }
4809 break;
4810
4811 case ANEG_STATE_LINK_OK:
4812 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4813 ret = ANEG_DONE;
4814 break;
4815
4816 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4817 /* ??? unimplemented */
4818 break;
4819
4820 case ANEG_STATE_NEXT_PAGE_WAIT:
4821 /* ??? unimplemented */
4822 break;
4823
4824 default:
4825 ret = ANEG_FAILED;
4826 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
4829 return ret;
4830}
4831
Matt Carlson5be73b42007-12-20 20:09:29 -08004832static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833{
4834 int res = 0;
4835 struct tg3_fiber_aneginfo aninfo;
4836 int status = ANEG_FAILED;
4837 unsigned int tick;
4838 u32 tmp;
4839
4840 tw32_f(MAC_TX_AUTO_NEG, 0);
4841
4842 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4843 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4844 udelay(40);
4845
4846 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4847 udelay(40);
4848
4849 memset(&aninfo, 0, sizeof(aninfo));
4850 aninfo.flags |= MR_AN_ENABLE;
4851 aninfo.state = ANEG_STATE_UNKNOWN;
4852 aninfo.cur_time = 0;
4853 tick = 0;
4854 while (++tick < 195000) {
4855 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4856 if (status == ANEG_DONE || status == ANEG_FAILED)
4857 break;
4858
4859 udelay(1);
4860 }
4861
4862 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4863 tw32_f(MAC_MODE, tp->mac_mode);
4864 udelay(40);
4865
Matt Carlson5be73b42007-12-20 20:09:29 -08004866 *txflags = aninfo.txconfig;
4867 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
4869 if (status == ANEG_DONE &&
4870 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4871 MR_LP_ADV_FULL_DUPLEX)))
4872 res = 1;
4873
4874 return res;
4875}
4876
4877static void tg3_init_bcm8002(struct tg3 *tp)
4878{
4879 u32 mac_status = tr32(MAC_STATUS);
4880 int i;
4881
4882 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004883 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 !(mac_status & MAC_STATUS_PCS_SYNCED))
4885 return;
4886
4887 /* Set PLL lock range. */
4888 tg3_writephy(tp, 0x16, 0x8007);
4889
4890 /* SW reset */
4891 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4892
4893 /* Wait for reset to complete. */
4894 /* XXX schedule_timeout() ... */
4895 for (i = 0; i < 500; i++)
4896 udelay(10);
4897
4898 /* Config mode; select PMA/Ch 1 regs. */
4899 tg3_writephy(tp, 0x10, 0x8411);
4900
4901 /* Enable auto-lock and comdet, select txclk for tx. */
4902 tg3_writephy(tp, 0x11, 0x0a10);
4903
4904 tg3_writephy(tp, 0x18, 0x00a0);
4905 tg3_writephy(tp, 0x16, 0x41ff);
4906
4907 /* Assert and deassert POR. */
4908 tg3_writephy(tp, 0x13, 0x0400);
4909 udelay(40);
4910 tg3_writephy(tp, 0x13, 0x0000);
4911
4912 tg3_writephy(tp, 0x11, 0x0a50);
4913 udelay(40);
4914 tg3_writephy(tp, 0x11, 0x0a10);
4915
4916 /* Wait for signal to stabilize */
4917 /* XXX schedule_timeout() ... */
4918 for (i = 0; i < 15000; i++)
4919 udelay(10);
4920
4921 /* Deselect the channel register so we can read the PHYID
4922 * later.
4923 */
4924 tg3_writephy(tp, 0x10, 0x8011);
4925}
4926
4927static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4928{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004929 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 u32 sg_dig_ctrl, sg_dig_status;
4931 u32 serdes_cfg, expected_sg_dig_ctrl;
4932 int workaround, port_a;
4933 int current_link_up;
4934
4935 serdes_cfg = 0;
4936 expected_sg_dig_ctrl = 0;
4937 workaround = 0;
4938 port_a = 1;
4939 current_link_up = 0;
4940
4941 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4942 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4943 workaround = 1;
4944 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4945 port_a = 0;
4946
4947 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4948 /* preserve bits 20-23 for voltage regulator */
4949 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4950 }
4951
4952 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4953
4954 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004955 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 if (workaround) {
4957 u32 val = serdes_cfg;
4958
4959 if (port_a)
4960 val |= 0xc010000;
4961 else
4962 val |= 0x4010000;
4963 tw32_f(MAC_SERDES_CFG, val);
4964 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004965
4966 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 }
4968 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4969 tg3_setup_flow_control(tp, 0, 0);
4970 current_link_up = 1;
4971 }
4972 goto out;
4973 }
4974
4975 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004976 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
Matt Carlson82cd3d12007-12-20 20:09:00 -08004978 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4979 if (flowctrl & ADVERTISE_1000XPAUSE)
4980 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4981 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4982 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
4984 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004985 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004986 tp->serdes_counter &&
4987 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4988 MAC_STATUS_RCVD_CFG)) ==
4989 MAC_STATUS_PCS_SYNCED)) {
4990 tp->serdes_counter--;
4991 current_link_up = 1;
4992 goto out;
4993 }
4994restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 if (workaround)
4996 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004997 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 udelay(5);
4999 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5000
Michael Chan3d3ebe72006-09-27 15:59:15 -07005001 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005002 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5004 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005005 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 mac_status = tr32(MAC_STATUS);
5007
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005008 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005010 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011
Matt Carlson82cd3d12007-12-20 20:09:00 -08005012 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5013 local_adv |= ADVERTISE_1000XPAUSE;
5014 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5015 local_adv |= ADVERTISE_1000XPSE_ASYM;
5016
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005017 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005018 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005019 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005020 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
Matt Carlson859edb22011-12-08 14:40:16 +00005022 tp->link_config.rmt_adv =
5023 mii_adv_to_ethtool_adv_x(remote_adv);
5024
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 tg3_setup_flow_control(tp, local_adv, remote_adv);
5026 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005027 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005028 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005029 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005030 if (tp->serdes_counter)
5031 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 else {
5033 if (workaround) {
5034 u32 val = serdes_cfg;
5035
5036 if (port_a)
5037 val |= 0xc010000;
5038 else
5039 val |= 0x4010000;
5040
5041 tw32_f(MAC_SERDES_CFG, val);
5042 }
5043
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005044 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 udelay(40);
5046
5047 /* Link parallel detection - link is up */
5048 /* only if we have PCS_SYNC and not */
5049 /* receiving config code words */
5050 mac_status = tr32(MAC_STATUS);
5051 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5052 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5053 tg3_setup_flow_control(tp, 0, 0);
5054 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005055 tp->phy_flags |=
5056 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005057 tp->serdes_counter =
5058 SERDES_PARALLEL_DET_TIMEOUT;
5059 } else
5060 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 }
5062 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005063 } else {
5064 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005065 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 }
5067
5068out:
5069 return current_link_up;
5070}
5071
5072static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
5073{
5074 int current_link_up = 0;
5075
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005076 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078
5079 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005080 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005082
Matt Carlson5be73b42007-12-20 20:09:29 -08005083 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5084 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085
Matt Carlson5be73b42007-12-20 20:09:29 -08005086 if (txflags & ANEG_CFG_PS1)
5087 local_adv |= ADVERTISE_1000XPAUSE;
5088 if (txflags & ANEG_CFG_PS2)
5089 local_adv |= ADVERTISE_1000XPSE_ASYM;
5090
5091 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5092 remote_adv |= LPA_1000XPAUSE;
5093 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5094 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
Matt Carlson859edb22011-12-08 14:40:16 +00005096 tp->link_config.rmt_adv =
5097 mii_adv_to_ethtool_adv_x(remote_adv);
5098
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 tg3_setup_flow_control(tp, local_adv, remote_adv);
5100
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 current_link_up = 1;
5102 }
5103 for (i = 0; i < 30; i++) {
5104 udelay(20);
5105 tw32_f(MAC_STATUS,
5106 (MAC_STATUS_SYNC_CHANGED |
5107 MAC_STATUS_CFG_CHANGED));
5108 udelay(40);
5109 if ((tr32(MAC_STATUS) &
5110 (MAC_STATUS_SYNC_CHANGED |
5111 MAC_STATUS_CFG_CHANGED)) == 0)
5112 break;
5113 }
5114
5115 mac_status = tr32(MAC_STATUS);
5116 if (current_link_up == 0 &&
5117 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5118 !(mac_status & MAC_STATUS_RCVD_CFG))
5119 current_link_up = 1;
5120 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005121 tg3_setup_flow_control(tp, 0, 0);
5122
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 /* Forcing 1000FD link up. */
5124 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
5126 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5127 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005128
5129 tw32_f(MAC_MODE, tp->mac_mode);
5130 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 }
5132
5133out:
5134 return current_link_up;
5135}
5136
5137static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
5138{
5139 u32 orig_pause_cfg;
5140 u16 orig_active_speed;
5141 u8 orig_active_duplex;
5142 u32 mac_status;
5143 int current_link_up;
5144 int i;
5145
Matt Carlson8d018622007-12-20 20:05:44 -08005146 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 orig_active_speed = tp->link_config.active_speed;
5148 orig_active_duplex = tp->link_config.active_duplex;
5149
Joe Perches63c3a662011-04-26 08:12:10 +00005150 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005151 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005152 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 mac_status = tr32(MAC_STATUS);
5154 mac_status &= (MAC_STATUS_PCS_SYNCED |
5155 MAC_STATUS_SIGNAL_DET |
5156 MAC_STATUS_CFG_CHANGED |
5157 MAC_STATUS_RCVD_CFG);
5158 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5159 MAC_STATUS_SIGNAL_DET)) {
5160 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5161 MAC_STATUS_CFG_CHANGED));
5162 return 0;
5163 }
5164 }
5165
5166 tw32_f(MAC_TX_AUTO_NEG, 0);
5167
5168 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5169 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5170 tw32_f(MAC_MODE, tp->mac_mode);
5171 udelay(40);
5172
Matt Carlson79eb6902010-02-17 15:17:03 +00005173 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 tg3_init_bcm8002(tp);
5175
5176 /* Enable link change event even when serdes polling. */
5177 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5178 udelay(40);
5179
5180 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00005181 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 mac_status = tr32(MAC_STATUS);
5183
Joe Perches63c3a662011-04-26 08:12:10 +00005184 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5186 else
5187 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5188
Matt Carlson898a56f2009-08-28 14:02:40 +00005189 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005191 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192
5193 for (i = 0; i < 100; i++) {
5194 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5195 MAC_STATUS_CFG_CHANGED));
5196 udelay(5);
5197 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005198 MAC_STATUS_CFG_CHANGED |
5199 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 break;
5201 }
5202
5203 mac_status = tr32(MAC_STATUS);
5204 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
5205 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005206 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5207 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 tw32_f(MAC_MODE, (tp->mac_mode |
5209 MAC_MODE_SEND_CONFIGS));
5210 udelay(1);
5211 tw32_f(MAC_MODE, tp->mac_mode);
5212 }
5213 }
5214
5215 if (current_link_up == 1) {
5216 tp->link_config.active_speed = SPEED_1000;
5217 tp->link_config.active_duplex = DUPLEX_FULL;
5218 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5219 LED_CTRL_LNKLED_OVERRIDE |
5220 LED_CTRL_1000MBPS_ON));
5221 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005222 tp->link_config.active_speed = SPEED_UNKNOWN;
5223 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5225 LED_CTRL_LNKLED_OVERRIDE |
5226 LED_CTRL_TRAFFIC_OVERRIDE));
5227 }
5228
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005229 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005230 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 if (orig_pause_cfg != now_pause_cfg ||
5232 orig_active_speed != tp->link_config.active_speed ||
5233 orig_active_duplex != tp->link_config.active_duplex)
5234 tg3_link_report(tp);
5235 }
5236
5237 return 0;
5238}
5239
Michael Chan747e8f82005-07-25 12:33:22 -07005240static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
5241{
5242 int current_link_up, err = 0;
5243 u32 bmsr, bmcr;
5244 u16 current_speed;
5245 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08005246 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07005247
5248 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5249 tw32_f(MAC_MODE, tp->mac_mode);
5250 udelay(40);
5251
5252 tw32(MAC_EVENT, 0);
5253
5254 tw32_f(MAC_STATUS,
5255 (MAC_STATUS_SYNC_CHANGED |
5256 MAC_STATUS_CFG_CHANGED |
5257 MAC_STATUS_MI_COMPLETION |
5258 MAC_STATUS_LNKSTATE_CHANGED));
5259 udelay(40);
5260
5261 if (force_reset)
5262 tg3_phy_reset(tp);
5263
5264 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00005265 current_speed = SPEED_UNKNOWN;
5266 current_duplex = DUPLEX_UNKNOWN;
Matt Carlson859edb22011-12-08 14:40:16 +00005267 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005268
5269 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5270 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005271 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
5272 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5273 bmsr |= BMSR_LSTATUS;
5274 else
5275 bmsr &= ~BMSR_LSTATUS;
5276 }
Michael Chan747e8f82005-07-25 12:33:22 -07005277
5278 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5279
5280 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005281 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005282 /* do nothing, just check for link up at the end */
5283 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005284 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005285
5286 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005287 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5288 ADVERTISE_1000XPAUSE |
5289 ADVERTISE_1000XPSE_ASYM |
5290 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005291
Matt Carlson28011cf2011-11-16 18:36:59 -05005292 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005293 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005294
Matt Carlson28011cf2011-11-16 18:36:59 -05005295 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5296 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005297 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5298 tg3_writephy(tp, MII_BMCR, bmcr);
5299
5300 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005301 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005302 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005303
5304 return err;
5305 }
5306 } else {
5307 u32 new_bmcr;
5308
5309 bmcr &= ~BMCR_SPEED1000;
5310 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5311
5312 if (tp->link_config.duplex == DUPLEX_FULL)
5313 new_bmcr |= BMCR_FULLDPLX;
5314
5315 if (new_bmcr != bmcr) {
5316 /* BMCR_SPEED1000 is a reserved bit that needs
5317 * to be set on write.
5318 */
5319 new_bmcr |= BMCR_SPEED1000;
5320
5321 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005322 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005323 u32 adv;
5324
5325 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5326 adv &= ~(ADVERTISE_1000XFULL |
5327 ADVERTISE_1000XHALF |
5328 ADVERTISE_SLCT);
5329 tg3_writephy(tp, MII_ADVERTISE, adv);
5330 tg3_writephy(tp, MII_BMCR, bmcr |
5331 BMCR_ANRESTART |
5332 BMCR_ANENABLE);
5333 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005334 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005335 }
5336 tg3_writephy(tp, MII_BMCR, new_bmcr);
5337 bmcr = new_bmcr;
5338 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5339 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005340 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
5341 ASIC_REV_5714) {
5342 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5343 bmsr |= BMSR_LSTATUS;
5344 else
5345 bmsr &= ~BMSR_LSTATUS;
5346 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005347 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005348 }
5349 }
5350
5351 if (bmsr & BMSR_LSTATUS) {
5352 current_speed = SPEED_1000;
5353 current_link_up = 1;
5354 if (bmcr & BMCR_FULLDPLX)
5355 current_duplex = DUPLEX_FULL;
5356 else
5357 current_duplex = DUPLEX_HALF;
5358
Matt Carlsonef167e22007-12-20 20:10:01 -08005359 local_adv = 0;
5360 remote_adv = 0;
5361
Michael Chan747e8f82005-07-25 12:33:22 -07005362 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005363 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005364
5365 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5366 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5367 common = local_adv & remote_adv;
5368 if (common & (ADVERTISE_1000XHALF |
5369 ADVERTISE_1000XFULL)) {
5370 if (common & ADVERTISE_1000XFULL)
5371 current_duplex = DUPLEX_FULL;
5372 else
5373 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005374
5375 tp->link_config.rmt_adv =
5376 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005377 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005378 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005379 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07005380 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00005381 }
Michael Chan747e8f82005-07-25 12:33:22 -07005382 }
5383 }
5384
Matt Carlsonef167e22007-12-20 20:10:01 -08005385 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
5386 tg3_setup_flow_control(tp, local_adv, remote_adv);
5387
Michael Chan747e8f82005-07-25 12:33:22 -07005388 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5389 if (tp->link_config.active_duplex == DUPLEX_HALF)
5390 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5391
5392 tw32_f(MAC_MODE, tp->mac_mode);
5393 udelay(40);
5394
5395 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5396
5397 tp->link_config.active_speed = current_speed;
5398 tp->link_config.active_duplex = current_duplex;
5399
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005400 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07005401 return err;
5402}
5403
5404static void tg3_serdes_parallel_detect(struct tg3 *tp)
5405{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005406 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005407 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005408 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005409 return;
5410 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005411
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005412 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07005413 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5414 u32 bmcr;
5415
5416 tg3_readphy(tp, MII_BMCR, &bmcr);
5417 if (bmcr & BMCR_ANENABLE) {
5418 u32 phy1, phy2;
5419
5420 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005421 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5422 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005423
5424 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005425 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5426 MII_TG3_DSP_EXP1_INT_STAT);
5427 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5428 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005429
5430 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5431 /* We have signal detect and not receiving
5432 * config code words, link is up by parallel
5433 * detection.
5434 */
5435
5436 bmcr &= ~BMCR_ANENABLE;
5437 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5438 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005439 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005440 }
5441 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005442 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00005443 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005444 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005445 u32 phy2;
5446
5447 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005448 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5449 MII_TG3_DSP_EXP1_INT_STAT);
5450 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005451 if (phy2 & 0x20) {
5452 u32 bmcr;
5453
5454 /* Config code words received, turn on autoneg. */
5455 tg3_readphy(tp, MII_BMCR, &bmcr);
5456 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5457
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005458 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005459
5460 }
5461 }
5462}
5463
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5465{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005466 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 int err;
5468
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005469 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005471 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005472 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005473 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005476 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005477 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005478
5479 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5480 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5481 scale = 65;
5482 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5483 scale = 6;
5484 else
5485 scale = 12;
5486
5487 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5488 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5489 tw32(GRC_MISC_CFG, val);
5490 }
5491
Matt Carlsonf2096f92011-04-05 14:22:48 +00005492 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5493 (6 << TX_LENGTHS_IPG_SHIFT);
Michael Chanc65a17f2013-01-06 12:51:07 +00005494 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
5495 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005496 val |= tr32(MAC_TX_LENGTHS) &
5497 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5498 TX_LENGTHS_CNT_DWN_VAL_MSK);
5499
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 if (tp->link_config.active_speed == SPEED_1000 &&
5501 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005502 tw32(MAC_TX_LENGTHS, val |
5503 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005505 tw32(MAC_TX_LENGTHS, val |
5506 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507
Joe Perches63c3a662011-04-26 08:12:10 +00005508 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005509 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005511 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 } else {
5513 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5514 }
5515 }
5516
Joe Perches63c3a662011-04-26 08:12:10 +00005517 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005518 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005519 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07005520 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5521 tp->pwrmgmt_thresh;
5522 else
5523 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5524 tw32(PCIE_PWR_MGMT_THRESH, val);
5525 }
5526
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 return err;
5528}
5529
Matt Carlsonbe947302012-12-03 19:36:57 +00005530/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00005531static u64 tg3_refclk_read(struct tg3 *tp)
5532{
5533 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
5534 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
5535}
5536
5537/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00005538static void tg3_refclk_write(struct tg3 *tp, u64 newval)
5539{
5540 tw32(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_STOP);
5541 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
5542 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
5543 tw32_f(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_RESUME);
5544}
5545
Matt Carlson7d41e492012-12-03 19:36:58 +00005546static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
5547static inline void tg3_full_unlock(struct tg3 *tp);
5548static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
5549{
5550 struct tg3 *tp = netdev_priv(dev);
5551
5552 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
5553 SOF_TIMESTAMPING_RX_SOFTWARE |
5554 SOF_TIMESTAMPING_SOFTWARE |
5555 SOF_TIMESTAMPING_TX_HARDWARE |
5556 SOF_TIMESTAMPING_RX_HARDWARE |
5557 SOF_TIMESTAMPING_RAW_HARDWARE;
5558
5559 if (tp->ptp_clock)
5560 info->phc_index = ptp_clock_index(tp->ptp_clock);
5561 else
5562 info->phc_index = -1;
5563
5564 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
5565
5566 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
5567 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
5568 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
5569 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
5570 return 0;
5571}
5572
5573static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
5574{
5575 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5576 bool neg_adj = false;
5577 u32 correction = 0;
5578
5579 if (ppb < 0) {
5580 neg_adj = true;
5581 ppb = -ppb;
5582 }
5583
5584 /* Frequency adjustment is performed using hardware with a 24 bit
5585 * accumulator and a programmable correction value. On each clk, the
5586 * correction value gets added to the accumulator and when it
5587 * overflows, the time counter is incremented/decremented.
5588 *
5589 * So conversion from ppb to correction value is
5590 * ppb * (1 << 24) / 1000000000
5591 */
5592 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
5593 TG3_EAV_REF_CLK_CORRECT_MASK;
5594
5595 tg3_full_lock(tp, 0);
5596
5597 if (correction)
5598 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
5599 TG3_EAV_REF_CLK_CORRECT_EN |
5600 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
5601 else
5602 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
5603
5604 tg3_full_unlock(tp);
5605
5606 return 0;
5607}
5608
5609static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
5610{
5611 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5612
5613 tg3_full_lock(tp, 0);
5614 tp->ptp_adjust += delta;
5615 tg3_full_unlock(tp);
5616
5617 return 0;
5618}
5619
5620static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
5621{
5622 u64 ns;
5623 u32 remainder;
5624 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5625
5626 tg3_full_lock(tp, 0);
5627 ns = tg3_refclk_read(tp);
5628 ns += tp->ptp_adjust;
5629 tg3_full_unlock(tp);
5630
5631 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
5632 ts->tv_nsec = remainder;
5633
5634 return 0;
5635}
5636
5637static int tg3_ptp_settime(struct ptp_clock_info *ptp,
5638 const struct timespec *ts)
5639{
5640 u64 ns;
5641 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5642
5643 ns = timespec_to_ns(ts);
5644
5645 tg3_full_lock(tp, 0);
5646 tg3_refclk_write(tp, ns);
5647 tp->ptp_adjust = 0;
5648 tg3_full_unlock(tp);
5649
5650 return 0;
5651}
5652
5653static int tg3_ptp_enable(struct ptp_clock_info *ptp,
5654 struct ptp_clock_request *rq, int on)
5655{
5656 return -EOPNOTSUPP;
5657}
5658
5659static const struct ptp_clock_info tg3_ptp_caps = {
5660 .owner = THIS_MODULE,
5661 .name = "tg3 clock",
5662 .max_adj = 250000000,
5663 .n_alarm = 0,
5664 .n_ext_ts = 0,
5665 .n_per_out = 0,
5666 .pps = 0,
5667 .adjfreq = tg3_ptp_adjfreq,
5668 .adjtime = tg3_ptp_adjtime,
5669 .gettime = tg3_ptp_gettime,
5670 .settime = tg3_ptp_settime,
5671 .enable = tg3_ptp_enable,
5672};
5673
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00005674static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
5675 struct skb_shared_hwtstamps *timestamp)
5676{
5677 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
5678 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
5679 tp->ptp_adjust);
5680}
5681
Matt Carlsonbe947302012-12-03 19:36:57 +00005682/* tp->lock must be held */
5683static void tg3_ptp_init(struct tg3 *tp)
5684{
5685 if (!tg3_flag(tp, PTP_CAPABLE))
5686 return;
5687
5688 /* Initialize the hardware clock to the system time. */
5689 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
5690 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00005691 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00005692}
5693
5694/* tp->lock must be held */
5695static void tg3_ptp_resume(struct tg3 *tp)
5696{
5697 if (!tg3_flag(tp, PTP_CAPABLE))
5698 return;
5699
5700 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
5701 tp->ptp_adjust = 0;
5702}
5703
5704static void tg3_ptp_fini(struct tg3 *tp)
5705{
5706 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
5707 return;
5708
Matt Carlson7d41e492012-12-03 19:36:58 +00005709 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00005710 tp->ptp_clock = NULL;
5711 tp->ptp_adjust = 0;
5712}
5713
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005714static inline int tg3_irq_sync(struct tg3 *tp)
5715{
5716 return tp->irq_sync;
5717}
5718
Matt Carlson97bd8e42011-04-13 11:05:04 +00005719static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5720{
5721 int i;
5722
5723 dst = (u32 *)((u8 *)dst + off);
5724 for (i = 0; i < len; i += sizeof(u32))
5725 *dst++ = tr32(off + i);
5726}
5727
5728static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5729{
5730 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5731 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5732 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5733 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5734 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5735 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5736 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5737 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5738 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5739 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5740 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5741 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5742 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5743 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5744 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5745 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5746 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5747 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5748 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5749
Joe Perches63c3a662011-04-26 08:12:10 +00005750 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005751 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5752
5753 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5754 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5755 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5756 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5757 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5758 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5759 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5760 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5761
Joe Perches63c3a662011-04-26 08:12:10 +00005762 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005763 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5764 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5765 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5766 }
5767
5768 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5769 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5770 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5771 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5772 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5773
Joe Perches63c3a662011-04-26 08:12:10 +00005774 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005775 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5776}
5777
5778static void tg3_dump_state(struct tg3 *tp)
5779{
5780 int i;
5781 u32 *regs;
5782
5783 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5784 if (!regs) {
5785 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5786 return;
5787 }
5788
Joe Perches63c3a662011-04-26 08:12:10 +00005789 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005790 /* Read up to but not including private PCI registers */
5791 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5792 regs[i / sizeof(u32)] = tr32(i);
5793 } else
5794 tg3_dump_legacy_regs(tp, regs);
5795
5796 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5797 if (!regs[i + 0] && !regs[i + 1] &&
5798 !regs[i + 2] && !regs[i + 3])
5799 continue;
5800
5801 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5802 i * 4,
5803 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5804 }
5805
5806 kfree(regs);
5807
5808 for (i = 0; i < tp->irq_cnt; i++) {
5809 struct tg3_napi *tnapi = &tp->napi[i];
5810
5811 /* SW status block */
5812 netdev_err(tp->dev,
5813 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5814 i,
5815 tnapi->hw_status->status,
5816 tnapi->hw_status->status_tag,
5817 tnapi->hw_status->rx_jumbo_consumer,
5818 tnapi->hw_status->rx_consumer,
5819 tnapi->hw_status->rx_mini_consumer,
5820 tnapi->hw_status->idx[0].rx_producer,
5821 tnapi->hw_status->idx[0].tx_consumer);
5822
5823 netdev_err(tp->dev,
5824 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5825 i,
5826 tnapi->last_tag, tnapi->last_irq_tag,
5827 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5828 tnapi->rx_rcb_ptr,
5829 tnapi->prodring.rx_std_prod_idx,
5830 tnapi->prodring.rx_std_cons_idx,
5831 tnapi->prodring.rx_jmb_prod_idx,
5832 tnapi->prodring.rx_jmb_cons_idx);
5833 }
5834}
5835
Michael Chandf3e6542006-05-26 17:48:07 -07005836/* This is called whenever we suspect that the system chipset is re-
5837 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5838 * is bogus tx completions. We try to recover by setting the
5839 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5840 * in the workqueue.
5841 */
5842static void tg3_tx_recover(struct tg3 *tp)
5843{
Joe Perches63c3a662011-04-26 08:12:10 +00005844 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005845 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5846
Matt Carlson5129c3a2010-04-05 10:19:23 +00005847 netdev_warn(tp->dev,
5848 "The system may be re-ordering memory-mapped I/O "
5849 "cycles to the network device, attempting to recover. "
5850 "Please report the problem to the driver maintainer "
5851 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005852
5853 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005854 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005855 spin_unlock(&tp->lock);
5856}
5857
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005858static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005859{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005860 /* Tell compiler to fetch tx indices from memory. */
5861 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005862 return tnapi->tx_pending -
5863 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005864}
5865
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866/* Tigon3 never reports partial packet sends. So we do not
5867 * need special logic to handle SKBs that have not had all
5868 * of their frags sent yet, like SunGEM does.
5869 */
Matt Carlson17375d22009-08-28 14:02:18 +00005870static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871{
Matt Carlson17375d22009-08-28 14:02:18 +00005872 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005873 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005874 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005875 struct netdev_queue *txq;
5876 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005877 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005878
Joe Perches63c3a662011-04-26 08:12:10 +00005879 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005880 index--;
5881
5882 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883
5884 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005885 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005887 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888
Michael Chandf3e6542006-05-26 17:48:07 -07005889 if (unlikely(skb == NULL)) {
5890 tg3_tx_recover(tp);
5891 return;
5892 }
5893
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00005894 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
5895 struct skb_shared_hwtstamps timestamp;
5896 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
5897 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
5898
5899 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
5900
5901 skb_tstamp_tx(skb, &timestamp);
5902 }
5903
Alexander Duyckf4188d82009-12-02 16:48:38 +00005904 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005905 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005906 skb_headlen(skb),
5907 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
5909 ri->skb = NULL;
5910
Matt Carlsone01ee142011-07-27 14:20:50 +00005911 while (ri->fragmented) {
5912 ri->fragmented = false;
5913 sw_idx = NEXT_TX(sw_idx);
5914 ri = &tnapi->tx_buffers[sw_idx];
5915 }
5916
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 sw_idx = NEXT_TX(sw_idx);
5918
5919 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005920 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005921 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5922 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005923
5924 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005925 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005926 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005927 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005928
5929 while (ri->fragmented) {
5930 ri->fragmented = false;
5931 sw_idx = NEXT_TX(sw_idx);
5932 ri = &tnapi->tx_buffers[sw_idx];
5933 }
5934
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 sw_idx = NEXT_TX(sw_idx);
5936 }
5937
Tom Herbert298376d2011-11-28 16:33:30 +00005938 pkts_compl++;
5939 bytes_compl += skb->len;
5940
David S. Millerf47c11e2005-06-24 20:18:35 -07005941 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005942
5943 if (unlikely(tx_bug)) {
5944 tg3_tx_recover(tp);
5945 return;
5946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 }
5948
Tom Herbert5cb917b2012-03-05 19:53:50 +00005949 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00005950
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005951 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952
Michael Chan1b2a7202006-08-07 21:46:02 -07005953 /* Need to make the tx_cons update visible to tg3_start_xmit()
5954 * before checking for netif_queue_stopped(). Without the
5955 * memory barrier, there is a small possibility that tg3_start_xmit()
5956 * will miss it and cause the queue to be stopped forever.
5957 */
5958 smp_mb();
5959
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005960 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005961 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005962 __netif_tx_lock(txq, smp_processor_id());
5963 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005964 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005965 netif_tx_wake_queue(txq);
5966 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968}
5969
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005970static void tg3_frag_free(bool is_frag, void *data)
5971{
5972 if (is_frag)
5973 put_page(virt_to_head_page(data));
5974 else
5975 kfree(data);
5976}
5977
Eric Dumazet9205fd92011-11-18 06:47:01 +00005978static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005979{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005980 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
5981 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5982
Eric Dumazet9205fd92011-11-18 06:47:01 +00005983 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005984 return;
5985
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005986 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005987 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00005988 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005989 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005990}
5991
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005992
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993/* Returns size of skb allocated or < 0 on error.
5994 *
5995 * We only need to fill in the address because the other members
5996 * of the RX descriptor are invariant, see tg3_init_rings.
5997 *
5998 * Note the purposeful assymetry of cpu vs. chip accesses. For
5999 * posting buffers we only dirty the first cache line of the RX
6000 * descriptor (containing the address). Whereas for the RX status
6001 * buffers the cpu only reads the last cacheline of the RX descriptor
6002 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6003 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006004static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006005 u32 opaque_key, u32 dest_idx_unmasked,
6006 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007{
6008 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006009 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006010 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006012 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 switch (opaque_key) {
6015 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006016 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006017 desc = &tpr->rx_std[dest_idx];
6018 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006019 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 break;
6021
6022 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006023 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006024 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006025 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006026 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 break;
6028
6029 default:
6030 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032
6033 /* Do not overwrite any of the map or rp information
6034 * until we are sure we can commit to a new buffer.
6035 *
6036 * Callers depend upon this behavior and assume that
6037 * we leave everything unchanged if we fail.
6038 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006039 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6040 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006041 if (skb_size <= PAGE_SIZE) {
6042 data = netdev_alloc_frag(skb_size);
6043 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006044 } else {
6045 data = kmalloc(skb_size, GFP_ATOMIC);
6046 *frag_size = 0;
6047 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006048 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 return -ENOMEM;
6050
Eric Dumazet9205fd92011-11-18 06:47:01 +00006051 mapping = pci_map_single(tp->pdev,
6052 data + TG3_RX_OFFSET(tp),
6053 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006055 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006056 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006057 return -EIO;
6058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059
Eric Dumazet9205fd92011-11-18 06:47:01 +00006060 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006061 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 desc->addr_hi = ((u64)mapping >> 32);
6064 desc->addr_lo = ((u64)mapping & 0xffffffff);
6065
Eric Dumazet9205fd92011-11-18 06:47:01 +00006066 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067}
6068
6069/* We only need to move over in the address because the other
6070 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006071 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 */
Matt Carlsona3896162009-11-13 13:03:44 +00006073static void tg3_recycle_rx(struct tg3_napi *tnapi,
6074 struct tg3_rx_prodring_set *dpr,
6075 u32 opaque_key, int src_idx,
6076 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077{
Matt Carlson17375d22009-08-28 14:02:18 +00006078 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6080 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006081 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006082 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083
6084 switch (opaque_key) {
6085 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006086 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006087 dest_desc = &dpr->rx_std[dest_idx];
6088 dest_map = &dpr->rx_std_buffers[dest_idx];
6089 src_desc = &spr->rx_std[src_idx];
6090 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 break;
6092
6093 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006094 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006095 dest_desc = &dpr->rx_jmb[dest_idx].std;
6096 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6097 src_desc = &spr->rx_jmb[src_idx].std;
6098 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 break;
6100
6101 default:
6102 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104
Eric Dumazet9205fd92011-11-18 06:47:01 +00006105 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006106 dma_unmap_addr_set(dest_map, mapping,
6107 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 dest_desc->addr_hi = src_desc->addr_hi;
6109 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006110
6111 /* Ensure that the update to the skb happens after the physical
6112 * addresses have been transferred to the new BD location.
6113 */
6114 smp_wmb();
6115
Eric Dumazet9205fd92011-11-18 06:47:01 +00006116 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117}
6118
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119/* The RX ring scheme is composed of multiple rings which post fresh
6120 * buffers to the chip, and one special ring the chip uses to report
6121 * status back to the host.
6122 *
6123 * The special ring reports the status of received packets to the
6124 * host. The chip does not write into the original descriptor the
6125 * RX buffer was obtained from. The chip simply takes the original
6126 * descriptor as provided by the host, updates the status and length
6127 * field, then writes this into the next status ring entry.
6128 *
6129 * Each ring the host uses to post buffers to the chip is described
6130 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6131 * it is first placed into the on-chip ram. When the packet's length
6132 * is known, it walks down the TG3_BDINFO entries to select the ring.
6133 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6134 * which is within the range of the new packet's length is chosen.
6135 *
6136 * The "separate ring for rx status" scheme may sound queer, but it makes
6137 * sense from a cache coherency perspective. If only the host writes
6138 * to the buffer post rings, and only the chip writes to the rx status
6139 * rings, then cache lines never move beyond shared-modified state.
6140 * If both the host and chip were to write into the same ring, cache line
6141 * eviction could occur since both entities want it in an exclusive state.
6142 */
Matt Carlson17375d22009-08-28 14:02:18 +00006143static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144{
Matt Carlson17375d22009-08-28 14:02:18 +00006145 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006146 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006147 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006148 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006149 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006151 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006153 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 /*
6155 * We need to order the read of hw_idx and the read of
6156 * the opaque cookie.
6157 */
6158 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 work_mask = 0;
6160 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006161 std_prod_idx = tpr->rx_std_prod_idx;
6162 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006164 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006165 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 unsigned int len;
6167 struct sk_buff *skb;
6168 dma_addr_t dma_addr;
6169 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006170 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006171 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172
6173 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6174 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6175 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006176 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006177 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006178 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006179 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006180 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006182 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006183 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006184 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006185 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006186 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188
6189 work_mask |= opaque_key;
6190
6191 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
6192 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
6193 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006194 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 desc_idx, *post_ptr);
6196 drop_it_no_recycle:
6197 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006198 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 goto next_pkt;
6200 }
6201
Eric Dumazet9205fd92011-11-18 06:47:01 +00006202 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006203 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6204 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006206 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6207 RXD_FLAG_PTPSTAT_PTPV1 ||
6208 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6209 RXD_FLAG_PTPSTAT_PTPV2) {
6210 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6211 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6212 }
6213
Matt Carlsond2757fc2010-04-12 06:58:27 +00006214 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006216 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217
Eric Dumazet9205fd92011-11-18 06:47:01 +00006218 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006219 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 if (skb_size < 0)
6221 goto drop_it;
6222
Matt Carlson287be122009-08-28 13:58:46 +00006223 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 PCI_DMA_FROMDEVICE);
6225
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006226 skb = build_skb(data, frag_size);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006227 if (!skb) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006228 tg3_frag_free(frag_size != 0, data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006229 goto drop_it_no_recycle;
6230 }
6231 skb_reserve(skb, TG3_RX_OFFSET(tp));
6232 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006233 * after the usage of the old DMA mapping.
6234 */
6235 smp_wmb();
6236
Eric Dumazet9205fd92011-11-18 06:47:01 +00006237 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006238
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006240 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 desc_idx, *post_ptr);
6242
Eric Dumazet9205fd92011-11-18 06:47:01 +00006243 skb = netdev_alloc_skb(tp->dev,
6244 len + TG3_RAW_IP_ALIGN);
6245 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 goto drop_it_no_recycle;
6247
Eric Dumazet9205fd92011-11-18 06:47:01 +00006248 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006250 memcpy(skb->data,
6251 data + TG3_RX_OFFSET(tp),
6252 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 }
6255
Eric Dumazet9205fd92011-11-18 06:47:01 +00006256 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006257 if (tstamp)
6258 tg3_hwclock_to_timestamp(tp, tstamp,
6259 skb_hwtstamps(skb));
6260
Michał Mirosławdc668912011-04-07 03:35:07 +00006261 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6263 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6264 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6265 skb->ip_summed = CHECKSUM_UNNECESSARY;
6266 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006267 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268
6269 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006270
6271 if (len > (tp->dev->mtu + ETH_HLEN) &&
6272 skb->protocol != htons(ETH_P_8021Q)) {
6273 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006274 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006275 }
6276
Matt Carlson9dc7a112010-04-12 06:58:28 +00006277 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006278 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
6279 __vlan_hwaccel_put_tag(skb,
6280 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006281
Matt Carlsonbf933c82011-01-25 15:58:49 +00006282 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 received++;
6285 budget--;
6286
6287next_pkt:
6288 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006289
6290 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006291 tpr->rx_std_prod_idx = std_prod_idx &
6292 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006293 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6294 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006295 work_mask &= ~RXD_OPAQUE_RING_STD;
6296 rx_std_posted = 0;
6297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006299 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006300 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006301
6302 /* Refresh hw_idx to see if there is new work */
6303 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006304 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006305 rmb();
6306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 }
6308
6309 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006310 tnapi->rx_rcb_ptr = sw_idx;
6311 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312
6313 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006314 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006315 /* Sync BD data before updating mailbox */
6316 wmb();
6317
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006318 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006319 tpr->rx_std_prod_idx = std_prod_idx &
6320 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006321 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6322 tpr->rx_std_prod_idx);
6323 }
6324 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006325 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6326 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006327 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6328 tpr->rx_jmb_prod_idx);
6329 }
6330 mmiowb();
6331 } else if (work_mask) {
6332 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6333 * updated before the producer indices can be updated.
6334 */
6335 smp_wmb();
6336
Matt Carlson2c49a442010-09-30 10:34:35 +00006337 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6338 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006339
Michael Chan7ae52892012-03-21 15:38:33 +00006340 if (tnapi != &tp->napi[1]) {
6341 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006342 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345
6346 return received;
6347}
6348
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006349static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00006352 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006353 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
6354
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 if (sblk->status & SD_STATUS_LINK_CHG) {
6356 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006357 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07006358 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006359 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07006360 tw32_f(MAC_STATUS,
6361 (MAC_STATUS_SYNC_CHANGED |
6362 MAC_STATUS_CFG_CHANGED |
6363 MAC_STATUS_MI_COMPLETION |
6364 MAC_STATUS_LNKSTATE_CHANGED));
6365 udelay(40);
6366 } else
6367 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07006368 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 }
6370 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006371}
6372
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006373static int tg3_rx_prodring_xfer(struct tg3 *tp,
6374 struct tg3_rx_prodring_set *dpr,
6375 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006376{
6377 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006378 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006379
6380 while (1) {
6381 src_prod_idx = spr->rx_std_prod_idx;
6382
6383 /* Make sure updates to the rx_std_buffers[] entries and the
6384 * standard producer index are seen in the correct order.
6385 */
6386 smp_rmb();
6387
6388 if (spr->rx_std_cons_idx == src_prod_idx)
6389 break;
6390
6391 if (spr->rx_std_cons_idx < src_prod_idx)
6392 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6393 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006394 cpycnt = tp->rx_std_ring_mask + 1 -
6395 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006396
Matt Carlson2c49a442010-09-30 10:34:35 +00006397 cpycnt = min(cpycnt,
6398 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006399
6400 si = spr->rx_std_cons_idx;
6401 di = dpr->rx_std_prod_idx;
6402
Matt Carlsone92967b2010-02-12 14:47:06 +00006403 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006404 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006405 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006406 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006407 break;
6408 }
6409 }
6410
6411 if (!cpycnt)
6412 break;
6413
6414 /* Ensure that updates to the rx_std_buffers ring and the
6415 * shadowed hardware producer ring from tg3_recycle_skb() are
6416 * ordered correctly WRT the skb check above.
6417 */
6418 smp_rmb();
6419
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006420 memcpy(&dpr->rx_std_buffers[di],
6421 &spr->rx_std_buffers[si],
6422 cpycnt * sizeof(struct ring_info));
6423
6424 for (i = 0; i < cpycnt; i++, di++, si++) {
6425 struct tg3_rx_buffer_desc *sbd, *dbd;
6426 sbd = &spr->rx_std[si];
6427 dbd = &dpr->rx_std[di];
6428 dbd->addr_hi = sbd->addr_hi;
6429 dbd->addr_lo = sbd->addr_lo;
6430 }
6431
Matt Carlson2c49a442010-09-30 10:34:35 +00006432 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6433 tp->rx_std_ring_mask;
6434 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6435 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006436 }
6437
6438 while (1) {
6439 src_prod_idx = spr->rx_jmb_prod_idx;
6440
6441 /* Make sure updates to the rx_jmb_buffers[] entries and
6442 * the jumbo producer index are seen in the correct order.
6443 */
6444 smp_rmb();
6445
6446 if (spr->rx_jmb_cons_idx == src_prod_idx)
6447 break;
6448
6449 if (spr->rx_jmb_cons_idx < src_prod_idx)
6450 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6451 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006452 cpycnt = tp->rx_jmb_ring_mask + 1 -
6453 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006454
6455 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006456 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006457
6458 si = spr->rx_jmb_cons_idx;
6459 di = dpr->rx_jmb_prod_idx;
6460
Matt Carlsone92967b2010-02-12 14:47:06 +00006461 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006462 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006463 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006464 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006465 break;
6466 }
6467 }
6468
6469 if (!cpycnt)
6470 break;
6471
6472 /* Ensure that updates to the rx_jmb_buffers ring and the
6473 * shadowed hardware producer ring from tg3_recycle_skb() are
6474 * ordered correctly WRT the skb check above.
6475 */
6476 smp_rmb();
6477
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006478 memcpy(&dpr->rx_jmb_buffers[di],
6479 &spr->rx_jmb_buffers[si],
6480 cpycnt * sizeof(struct ring_info));
6481
6482 for (i = 0; i < cpycnt; i++, di++, si++) {
6483 struct tg3_rx_buffer_desc *sbd, *dbd;
6484 sbd = &spr->rx_jmb[si].std;
6485 dbd = &dpr->rx_jmb[di].std;
6486 dbd->addr_hi = sbd->addr_hi;
6487 dbd->addr_lo = sbd->addr_lo;
6488 }
6489
Matt Carlson2c49a442010-09-30 10:34:35 +00006490 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
6491 tp->rx_jmb_ring_mask;
6492 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
6493 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006494 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006495
6496 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006497}
6498
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006499static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
6500{
6501 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502
6503 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006504 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00006505 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00006506 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07006507 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 }
6509
Matt Carlsonf891ea12012-04-24 13:37:01 +00006510 if (!tnapi->rx_rcb_prod_idx)
6511 return work_done;
6512
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513 /* run RX thread, within the bounds set by NAPI.
6514 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006515 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006517 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00006518 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519
Joe Perches63c3a662011-04-26 08:12:10 +00006520 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006521 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006522 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006523 u32 std_prod_idx = dpr->rx_std_prod_idx;
6524 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006525
Michael Chan7ae52892012-03-21 15:38:33 +00006526 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00006527 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006528 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00006529 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006530
6531 wmb();
6532
Matt Carlsone4af1af2010-02-12 14:47:05 +00006533 if (std_prod_idx != dpr->rx_std_prod_idx)
6534 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6535 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006536
Matt Carlsone4af1af2010-02-12 14:47:05 +00006537 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
6538 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6539 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006540
6541 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006542
6543 if (err)
6544 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006545 }
6546
David S. Miller6f535762007-10-11 18:08:29 -07006547 return work_done;
6548}
David S. Millerf7383c22005-05-18 22:50:53 -07006549
Matt Carlsondb219972011-11-04 09:15:03 +00006550static inline void tg3_reset_task_schedule(struct tg3 *tp)
6551{
6552 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
6553 schedule_work(&tp->reset_task);
6554}
6555
6556static inline void tg3_reset_task_cancel(struct tg3 *tp)
6557{
6558 cancel_work_sync(&tp->reset_task);
6559 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00006560 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00006561}
6562
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006563static int tg3_poll_msix(struct napi_struct *napi, int budget)
6564{
6565 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6566 struct tg3 *tp = tnapi->tp;
6567 int work_done = 0;
6568 struct tg3_hw_status *sblk = tnapi->hw_status;
6569
6570 while (1) {
6571 work_done = tg3_poll_work(tnapi, work_done, budget);
6572
Joe Perches63c3a662011-04-26 08:12:10 +00006573 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006574 goto tx_recovery;
6575
6576 if (unlikely(work_done >= budget))
6577 break;
6578
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006579 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006580 * to tell the hw how much work has been processed,
6581 * so we must read it before checking for more work.
6582 */
6583 tnapi->last_tag = sblk->status_tag;
6584 tnapi->last_irq_tag = tnapi->last_tag;
6585 rmb();
6586
6587 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00006588 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
6589 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00006590
6591 /* This test here is not race free, but will reduce
6592 * the number of interrupts by looping again.
6593 */
6594 if (tnapi == &tp->napi[1] && tp->rx_refill)
6595 continue;
6596
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006597 napi_complete(napi);
6598 /* Reenable interrupts. */
6599 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00006600
6601 /* This test here is synchronized by napi_schedule()
6602 * and napi_complete() to close the race condition.
6603 */
6604 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
6605 tw32(HOSTCC_MODE, tp->coalesce_mode |
6606 HOSTCC_MODE_ENABLE |
6607 tnapi->coal_now);
6608 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006609 mmiowb();
6610 break;
6611 }
6612 }
6613
6614 return work_done;
6615
6616tx_recovery:
6617 /* work_done is guaranteed to be less than budget. */
6618 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006619 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006620 return work_done;
6621}
6622
Matt Carlsone64de4e2011-04-13 11:05:05 +00006623static void tg3_process_error(struct tg3 *tp)
6624{
6625 u32 val;
6626 bool real_error = false;
6627
Joe Perches63c3a662011-04-26 08:12:10 +00006628 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00006629 return;
6630
6631 /* Check Flow Attention register */
6632 val = tr32(HOSTCC_FLOW_ATTN);
6633 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
6634 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
6635 real_error = true;
6636 }
6637
6638 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
6639 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
6640 real_error = true;
6641 }
6642
6643 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6644 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6645 real_error = true;
6646 }
6647
6648 if (!real_error)
6649 return;
6650
6651 tg3_dump_state(tp);
6652
Joe Perches63c3a662011-04-26 08:12:10 +00006653 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006654 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006655}
6656
David S. Miller6f535762007-10-11 18:08:29 -07006657static int tg3_poll(struct napi_struct *napi, int budget)
6658{
Matt Carlson8ef04422009-08-28 14:01:37 +00006659 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6660 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006661 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006662 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006663
6664 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006665 if (sblk->status & SD_STATUS_ERROR)
6666 tg3_process_error(tp);
6667
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006668 tg3_poll_link(tp);
6669
Matt Carlson17375d22009-08-28 14:02:18 +00006670 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006671
Joe Perches63c3a662011-04-26 08:12:10 +00006672 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006673 goto tx_recovery;
6674
6675 if (unlikely(work_done >= budget))
6676 break;
6677
Joe Perches63c3a662011-04-26 08:12:10 +00006678 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006679 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006680 * to tell the hw how much work has been processed,
6681 * so we must read it before checking for more work.
6682 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006683 tnapi->last_tag = sblk->status_tag;
6684 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006685 rmb();
6686 } else
6687 sblk->status &= ~SD_STATUS_UPDATED;
6688
Matt Carlson17375d22009-08-28 14:02:18 +00006689 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006690 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006691 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006692 break;
6693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694 }
6695
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006696 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006697
6698tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006699 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006700 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006701 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006702 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703}
6704
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006705static void tg3_napi_disable(struct tg3 *tp)
6706{
6707 int i;
6708
6709 for (i = tp->irq_cnt - 1; i >= 0; i--)
6710 napi_disable(&tp->napi[i].napi);
6711}
6712
6713static void tg3_napi_enable(struct tg3 *tp)
6714{
6715 int i;
6716
6717 for (i = 0; i < tp->irq_cnt; i++)
6718 napi_enable(&tp->napi[i].napi);
6719}
6720
6721static void tg3_napi_init(struct tg3 *tp)
6722{
6723 int i;
6724
6725 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6726 for (i = 1; i < tp->irq_cnt; i++)
6727 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6728}
6729
6730static void tg3_napi_fini(struct tg3 *tp)
6731{
6732 int i;
6733
6734 for (i = 0; i < tp->irq_cnt; i++)
6735 netif_napi_del(&tp->napi[i].napi);
6736}
6737
6738static inline void tg3_netif_stop(struct tg3 *tp)
6739{
6740 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6741 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006742 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006743 netif_tx_disable(tp->dev);
6744}
6745
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00006746/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006747static inline void tg3_netif_start(struct tg3 *tp)
6748{
Matt Carlsonbe947302012-12-03 19:36:57 +00006749 tg3_ptp_resume(tp);
6750
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006751 /* NOTE: unconditional netif_tx_wake_all_queues is only
6752 * appropriate so long as all callers are assured to
6753 * have free tx slots (such as after tg3_init_hw)
6754 */
6755 netif_tx_wake_all_queues(tp->dev);
6756
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006757 if (tp->link_up)
6758 netif_carrier_on(tp->dev);
6759
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006760 tg3_napi_enable(tp);
6761 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6762 tg3_enable_ints(tp);
6763}
6764
David S. Millerf47c11e2005-06-24 20:18:35 -07006765static void tg3_irq_quiesce(struct tg3 *tp)
6766{
Matt Carlson4f125f42009-09-01 12:55:02 +00006767 int i;
6768
David S. Millerf47c11e2005-06-24 20:18:35 -07006769 BUG_ON(tp->irq_sync);
6770
6771 tp->irq_sync = 1;
6772 smp_mb();
6773
Matt Carlson4f125f42009-09-01 12:55:02 +00006774 for (i = 0; i < tp->irq_cnt; i++)
6775 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006776}
6777
David S. Millerf47c11e2005-06-24 20:18:35 -07006778/* Fully shutdown all tg3 driver activity elsewhere in the system.
6779 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6780 * with as well. Most of the time, this is not necessary except when
6781 * shutting down the device.
6782 */
6783static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6784{
Michael Chan46966542007-07-11 19:47:19 -07006785 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006786 if (irq_sync)
6787 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006788}
6789
6790static inline void tg3_full_unlock(struct tg3 *tp)
6791{
David S. Millerf47c11e2005-06-24 20:18:35 -07006792 spin_unlock_bh(&tp->lock);
6793}
6794
Michael Chanfcfa0a32006-03-20 22:28:41 -08006795/* One-shot MSI handler - Chip automatically disables interrupt
6796 * after sending MSI so driver doesn't have to do it.
6797 */
David Howells7d12e782006-10-05 14:55:46 +01006798static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006799{
Matt Carlson09943a12009-08-28 14:01:57 +00006800 struct tg3_napi *tnapi = dev_id;
6801 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006802
Matt Carlson898a56f2009-08-28 14:02:40 +00006803 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006804 if (tnapi->rx_rcb)
6805 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006806
6807 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006808 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006809
6810 return IRQ_HANDLED;
6811}
6812
Michael Chan88b06bc22005-04-21 17:13:25 -07006813/* MSI ISR - No need to check for interrupt sharing and no need to
6814 * flush status block and interrupt mailbox. PCI ordering rules
6815 * guarantee that MSI will arrive after the status block.
6816 */
David Howells7d12e782006-10-05 14:55:46 +01006817static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006818{
Matt Carlson09943a12009-08-28 14:01:57 +00006819 struct tg3_napi *tnapi = dev_id;
6820 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006821
Matt Carlson898a56f2009-08-28 14:02:40 +00006822 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006823 if (tnapi->rx_rcb)
6824 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006825 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006826 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006827 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006828 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006829 * NIC to stop sending us irqs, engaging "in-intr-handler"
6830 * event coalescing.
6831 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006832 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006833 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006834 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006835
Michael Chan88b06bc22005-04-21 17:13:25 -07006836 return IRQ_RETVAL(1);
6837}
6838
David Howells7d12e782006-10-05 14:55:46 +01006839static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840{
Matt Carlson09943a12009-08-28 14:01:57 +00006841 struct tg3_napi *tnapi = dev_id;
6842 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006843 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844 unsigned int handled = 1;
6845
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846 /* In INTx mode, it is possible for the interrupt to arrive at
6847 * the CPU before the status block posted prior to the interrupt.
6848 * Reading the PCI State register will confirm whether the
6849 * interrupt is ours and will flush the status block.
6850 */
Michael Chand18edcb2007-03-24 20:57:11 -07006851 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006852 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006853 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6854 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006855 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006856 }
Michael Chand18edcb2007-03-24 20:57:11 -07006857 }
6858
6859 /*
6860 * Writing any value to intr-mbox-0 clears PCI INTA# and
6861 * chip-internal interrupt pending events.
6862 * Writing non-zero to intr-mbox-0 additional tells the
6863 * NIC to stop sending us irqs, engaging "in-intr-handler"
6864 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006865 *
6866 * Flush the mailbox to de-assert the IRQ immediately to prevent
6867 * spurious interrupts. The flush impacts performance but
6868 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006869 */
Michael Chanc04cb342007-05-07 00:26:15 -07006870 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006871 if (tg3_irq_sync(tp))
6872 goto out;
6873 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006874 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006875 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006876 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006877 } else {
6878 /* No work, shared interrupt perhaps? re-enable
6879 * interrupts, and flush that PCI write
6880 */
6881 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6882 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006883 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006884out:
David S. Millerfac9b832005-05-18 22:46:34 -07006885 return IRQ_RETVAL(handled);
6886}
6887
David Howells7d12e782006-10-05 14:55:46 +01006888static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006889{
Matt Carlson09943a12009-08-28 14:01:57 +00006890 struct tg3_napi *tnapi = dev_id;
6891 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006892 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006893 unsigned int handled = 1;
6894
David S. Millerfac9b832005-05-18 22:46:34 -07006895 /* In INTx mode, it is possible for the interrupt to arrive at
6896 * the CPU before the status block posted prior to the interrupt.
6897 * Reading the PCI State register will confirm whether the
6898 * interrupt is ours and will flush the status block.
6899 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006900 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006901 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006902 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6903 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006904 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 }
Michael Chand18edcb2007-03-24 20:57:11 -07006906 }
6907
6908 /*
6909 * writing any value to intr-mbox-0 clears PCI INTA# and
6910 * chip-internal interrupt pending events.
6911 * writing non-zero to intr-mbox-0 additional tells the
6912 * NIC to stop sending us irqs, engaging "in-intr-handler"
6913 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006914 *
6915 * Flush the mailbox to de-assert the IRQ immediately to prevent
6916 * spurious interrupts. The flush impacts performance but
6917 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006918 */
Michael Chanc04cb342007-05-07 00:26:15 -07006919 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006920
6921 /*
6922 * In a shared interrupt configuration, sometimes other devices'
6923 * interrupts will scream. We record the current status tag here
6924 * so that the above check can report that the screaming interrupts
6925 * are unhandled. Eventually they will be silenced.
6926 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006927 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006928
Michael Chand18edcb2007-03-24 20:57:11 -07006929 if (tg3_irq_sync(tp))
6930 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006931
Matt Carlson72334482009-08-28 14:03:01 +00006932 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006933
Matt Carlson09943a12009-08-28 14:01:57 +00006934 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006935
David S. Millerf47c11e2005-06-24 20:18:35 -07006936out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937 return IRQ_RETVAL(handled);
6938}
6939
Michael Chan79381092005-04-21 17:13:59 -07006940/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006941static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006942{
Matt Carlson09943a12009-08-28 14:01:57 +00006943 struct tg3_napi *tnapi = dev_id;
6944 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006945 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006946
Michael Chanf9804dd2005-09-27 12:13:10 -07006947 if ((sblk->status & SD_STATUS_UPDATED) ||
6948 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006949 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006950 return IRQ_RETVAL(1);
6951 }
6952 return IRQ_RETVAL(0);
6953}
6954
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955#ifdef CONFIG_NET_POLL_CONTROLLER
6956static void tg3_poll_controller(struct net_device *dev)
6957{
Matt Carlson4f125f42009-09-01 12:55:02 +00006958 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07006959 struct tg3 *tp = netdev_priv(dev);
6960
Matt Carlson4f125f42009-09-01 12:55:02 +00006961 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006962 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963}
6964#endif
6965
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966static void tg3_tx_timeout(struct net_device *dev)
6967{
6968 struct tg3 *tp = netdev_priv(dev);
6969
Michael Chanb0408752007-02-13 12:18:30 -08006970 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006971 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006972 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974
Matt Carlsondb219972011-11-04 09:15:03 +00006975 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976}
6977
Michael Chanc58ec932005-09-17 00:46:27 -07006978/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6979static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6980{
6981 u32 base = (u32) mapping & 0xffffffff;
6982
Eric Dumazet807540b2010-09-23 05:40:09 +00006983 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006984}
6985
Michael Chan72f2afb2006-03-06 19:28:35 -08006986/* Test for DMA addresses > 40-bit */
6987static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6988 int len)
6989{
6990#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006991 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006992 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006993 return 0;
6994#else
6995 return 0;
6996#endif
6997}
6998
Matt Carlsond1a3b732011-07-27 14:20:51 +00006999static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007000 dma_addr_t mapping, u32 len, u32 flags,
7001 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007002{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007003 txbd->addr_hi = ((u64) mapping >> 32);
7004 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7005 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7006 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007007}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008
Matt Carlson84b67b22011-07-27 14:20:52 +00007009static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007010 dma_addr_t map, u32 len, u32 flags,
7011 u32 mss, u32 vlan)
7012{
7013 struct tg3 *tp = tnapi->tp;
7014 bool hwbug = false;
7015
7016 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007017 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007018
7019 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007020 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007021
7022 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007023 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007024
Matt Carlsona4cb4282011-12-14 11:09:58 +00007025 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007026 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007027 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007028 while (len > tp->dma_limit && *budget) {
7029 u32 frag_len = tp->dma_limit;
7030 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007031
Matt Carlsonb9e45482011-11-04 09:14:59 +00007032 /* Avoid the 8byte DMA problem */
7033 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007034 len += tp->dma_limit / 2;
7035 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007036 }
7037
Matt Carlsonb9e45482011-11-04 09:14:59 +00007038 tnapi->tx_buffers[*entry].fragmented = true;
7039
7040 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7041 frag_len, tmp_flag, mss, vlan);
7042 *budget -= 1;
7043 prvidx = *entry;
7044 *entry = NEXT_TX(*entry);
7045
Matt Carlsone31aa982011-07-27 14:20:53 +00007046 map += frag_len;
7047 }
7048
7049 if (len) {
7050 if (*budget) {
7051 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7052 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007053 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007054 *entry = NEXT_TX(*entry);
7055 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007056 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007057 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007058 }
7059 }
7060 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007061 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7062 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007063 *entry = NEXT_TX(*entry);
7064 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007065
7066 return hwbug;
7067}
7068
Matt Carlson0d681b22011-07-27 14:20:49 +00007069static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007070{
7071 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007072 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007073 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007074
Matt Carlson0d681b22011-07-27 14:20:49 +00007075 skb = txb->skb;
7076 txb->skb = NULL;
7077
Matt Carlson432aa7e2011-05-19 12:12:45 +00007078 pci_unmap_single(tnapi->tp->pdev,
7079 dma_unmap_addr(txb, mapping),
7080 skb_headlen(skb),
7081 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007082
7083 while (txb->fragmented) {
7084 txb->fragmented = false;
7085 entry = NEXT_TX(entry);
7086 txb = &tnapi->tx_buffers[entry];
7087 }
7088
Matt Carlsonba1142e2011-11-04 09:15:00 +00007089 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007090 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007091
7092 entry = NEXT_TX(entry);
7093 txb = &tnapi->tx_buffers[entry];
7094
7095 pci_unmap_page(tnapi->tp->pdev,
7096 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007097 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007098
7099 while (txb->fragmented) {
7100 txb->fragmented = false;
7101 entry = NEXT_TX(entry);
7102 txb = &tnapi->tx_buffers[entry];
7103 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007104 }
7105}
7106
Michael Chan72f2afb2006-03-06 19:28:35 -08007107/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007108static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007109 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007110 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007111 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007113 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007114 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007115 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007116 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117
Matt Carlson41588ba2008-04-19 18:12:33 -07007118 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
7119 new_skb = skb_copy(skb, GFP_ATOMIC);
7120 else {
7121 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7122
7123 new_skb = skb_copy_expand(skb,
7124 skb_headroom(skb) + more_headroom,
7125 skb_tailroom(skb), GFP_ATOMIC);
7126 }
7127
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007129 ret = -1;
7130 } else {
7131 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007132 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7133 PCI_DMA_TODEVICE);
7134 /* Make sure the mapping succeeded */
7135 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00007136 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007137 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007138 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007139 u32 save_entry = *entry;
7140
Matt Carlson92cd3a12011-07-27 14:20:47 +00007141 base_flags |= TXD_FLAG_END;
7142
Matt Carlson84b67b22011-07-27 14:20:52 +00007143 tnapi->tx_buffers[*entry].skb = new_skb;
7144 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007145 mapping, new_addr);
7146
Matt Carlson84b67b22011-07-27 14:20:52 +00007147 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007148 new_skb->len, base_flags,
7149 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007150 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007151 dev_kfree_skb(new_skb);
7152 ret = -1;
7153 }
Michael Chanc58ec932005-09-17 00:46:27 -07007154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155 }
7156
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007158 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007159 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160}
7161
Matt Carlson2ffcc982011-05-19 12:12:44 +00007162static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007163
7164/* Use GSO to workaround a rare TSO bug that may be triggered when the
7165 * TSO header is greater than 80 bytes.
7166 */
7167static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
7168{
7169 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007170 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007171
7172 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007173 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07007174 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007175
7176 /* netif_tx_stop_queue() must be done before checking
7177 * checking tx index in tg3_tx_avail() below, because in
7178 * tg3_tx(), we update tx index before checking for
7179 * netif_tx_queue_stopped().
7180 */
7181 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007182 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007183 return NETDEV_TX_BUSY;
7184
7185 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007186 }
7187
7188 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07007189 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07007190 goto tg3_tso_bug_end;
7191
7192 do {
7193 nskb = segs;
7194 segs = segs->next;
7195 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007196 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007197 } while (segs);
7198
7199tg3_tso_bug_end:
7200 dev_kfree_skb(skb);
7201
7202 return NETDEV_TX_OK;
7203}
Michael Chan52c0fd82006-06-29 20:15:54 -07007204
Michael Chan5a6f3072006-03-20 22:28:05 -08007205/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00007206 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08007207 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007208static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007209{
7210 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007211 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007212 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007213 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007214 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007215 struct tg3_napi *tnapi;
7216 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007217 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007218
Matt Carlson24f4efd2009-11-13 13:03:35 +00007219 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7220 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007221 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007222 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223
Matt Carlson84b67b22011-07-27 14:20:52 +00007224 budget = tg3_tx_avail(tnapi);
7225
Michael Chan00b70502006-06-17 21:58:45 -07007226 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007227 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007228 * interrupt. Furthermore, IRQ processing runs lockless so we have
7229 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007231 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007232 if (!netif_tx_queue_stopped(txq)) {
7233 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007234
7235 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007236 netdev_err(dev,
7237 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239 return NETDEV_TX_BUSY;
7240 }
7241
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007242 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07007244 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007246
Matt Carlsonbe98da62010-07-11 09:31:46 +00007247 mss = skb_shinfo(skb)->gso_size;
7248 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007249 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00007250 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251
7252 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00007253 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
7254 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255
Matt Carlson34195c32010-07-11 09:31:42 +00007256 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007257 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258
Eric Dumazeta5a11952012-01-23 01:22:09 +00007259 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007260
Eric Dumazeta5a11952012-01-23 01:22:09 +00007261 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00007262 iph->check = 0;
7263 iph->tot_len = htons(mss + hdr_len);
7264 }
7265
Michael Chan52c0fd82006-06-29 20:15:54 -07007266 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007267 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00007268 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007269
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7271 TXD_FLAG_CPU_POST_DMA);
7272
Joe Perches63c3a662011-04-26 08:12:10 +00007273 if (tg3_flag(tp, HW_TSO_1) ||
7274 tg3_flag(tp, HW_TSO_2) ||
7275 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007276 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007278 } else
7279 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
7280 iph->daddr, 0,
7281 IPPROTO_TCP,
7282 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283
Joe Perches63c3a662011-04-26 08:12:10 +00007284 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007285 mss |= (hdr_len & 0xc) << 12;
7286 if (hdr_len & 0x10)
7287 base_flags |= 0x00000010;
7288 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007289 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007290 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007291 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007292 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007293 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294 int tsflags;
7295
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007296 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297 mss |= (tsflags << 11);
7298 }
7299 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007300 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301 int tsflags;
7302
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007303 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304 base_flags |= tsflags << 12;
7305 }
7306 }
7307 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00007308
Matt Carlson93a700a2011-08-31 11:44:54 +00007309 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
7310 !mss && skb->len > VLAN_ETH_FRAME_LEN)
7311 base_flags |= TXD_FLAG_JMB_PKT;
7312
Matt Carlson92cd3a12011-07-27 14:20:47 +00007313 if (vlan_tx_tag_present(skb)) {
7314 base_flags |= TXD_FLAG_VLAN;
7315 vlan = vlan_tx_tag_get(skb);
7316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00007318 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
7319 tg3_flag(tp, TX_TSTAMP_EN)) {
7320 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
7321 base_flags |= TXD_FLAG_HWTSTAMP;
7322 }
7323
Alexander Duyckf4188d82009-12-02 16:48:38 +00007324 len = skb_headlen(skb);
7325
7326 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00007327 if (pci_dma_mapping_error(tp->pdev, mapping))
7328 goto drop;
7329
David S. Miller90079ce2008-09-11 04:52:51 -07007330
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007331 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007332 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333
7334 would_hit_hwbug = 0;
7335
Joe Perches63c3a662011-04-26 08:12:10 +00007336 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07007337 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338
Matt Carlson84b67b22011-07-27 14:20:52 +00007339 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00007340 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00007341 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00007342 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00007343 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00007344 u32 tmp_mss = mss;
7345
7346 if (!tg3_flag(tp, HW_TSO_1) &&
7347 !tg3_flag(tp, HW_TSO_2) &&
7348 !tg3_flag(tp, HW_TSO_3))
7349 tmp_mss = 0;
7350
Matt Carlsonc5665a52012-02-13 10:20:12 +00007351 /* Now loop through additional data
7352 * fragments, and queue them.
7353 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354 last = skb_shinfo(skb)->nr_frags - 1;
7355 for (i = 0; i <= last; i++) {
7356 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
7357
Eric Dumazet9e903e02011-10-18 21:00:24 +00007358 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00007359 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007360 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007362 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007363 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00007364 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007365 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00007366 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367
Matt Carlsonb9e45482011-11-04 09:14:59 +00007368 if (!budget ||
7369 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00007370 len, base_flags |
7371 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00007372 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007373 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007374 break;
7375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376 }
7377 }
7378
7379 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00007380 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381
7382 /* If the workaround fails due to memory/mapping
7383 * failure, silently drop this packet.
7384 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007385 entry = tnapi->tx_prod;
7386 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04007387 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00007388 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00007389 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007390 }
7391
Richard Cochrand515b452011-06-19 03:31:41 +00007392 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00007393 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00007394
Michael Chan6541b802012-03-04 14:48:14 +00007395 /* Sync BD data before updating mailbox */
7396 wmb();
7397
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007399 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007401 tnapi->tx_prod = entry;
7402 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007403 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007404
7405 /* netif_tx_stop_queue() must be done before checking
7406 * checking tx index in tg3_tx_avail() below, because in
7407 * tg3_tx(), we update tx index before checking for
7408 * netif_tx_queue_stopped().
7409 */
7410 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007411 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007412 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07007413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414
Eric Dumazetcdd0db02009-05-28 00:00:41 +00007415 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007417
7418dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007419 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007420 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007421drop:
7422 dev_kfree_skb(skb);
7423drop_nofree:
7424 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007425 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426}
7427
Matt Carlson6e01b202011-08-19 13:58:20 +00007428static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7429{
7430 if (enable) {
7431 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7432 MAC_MODE_PORT_MODE_MASK);
7433
7434 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7435
7436 if (!tg3_flag(tp, 5705_PLUS))
7437 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7438
7439 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7440 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7441 else
7442 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7443 } else {
7444 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7445
7446 if (tg3_flag(tp, 5705_PLUS) ||
7447 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
7448 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
7449 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7450 }
7451
7452 tw32(MAC_MODE, tp->mac_mode);
7453 udelay(40);
7454}
7455
Matt Carlson941ec902011-08-19 13:58:23 +00007456static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007457{
Matt Carlson941ec902011-08-19 13:58:23 +00007458 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007459
7460 tg3_phy_toggle_apd(tp, false);
7461 tg3_phy_toggle_automdix(tp, 0);
7462
Matt Carlson941ec902011-08-19 13:58:23 +00007463 if (extlpbk && tg3_phy_set_extloopbk(tp))
7464 return -EIO;
7465
7466 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007467 switch (speed) {
7468 case SPEED_10:
7469 break;
7470 case SPEED_100:
7471 bmcr |= BMCR_SPEED100;
7472 break;
7473 case SPEED_1000:
7474 default:
7475 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
7476 speed = SPEED_100;
7477 bmcr |= BMCR_SPEED100;
7478 } else {
7479 speed = SPEED_1000;
7480 bmcr |= BMCR_SPEED1000;
7481 }
7482 }
7483
Matt Carlson941ec902011-08-19 13:58:23 +00007484 if (extlpbk) {
7485 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
7486 tg3_readphy(tp, MII_CTRL1000, &val);
7487 val |= CTL1000_AS_MASTER |
7488 CTL1000_ENABLE_MASTER;
7489 tg3_writephy(tp, MII_CTRL1000, val);
7490 } else {
7491 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
7492 MII_TG3_FET_PTEST_TRIM_2;
7493 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
7494 }
7495 } else
7496 bmcr |= BMCR_LOOPBACK;
7497
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007498 tg3_writephy(tp, MII_BMCR, bmcr);
7499
7500 /* The write needs to be flushed for the FETs */
7501 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
7502 tg3_readphy(tp, MII_BMCR, &bmcr);
7503
7504 udelay(40);
7505
7506 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
7507 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00007508 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007509 MII_TG3_FET_PTEST_FRC_TX_LINK |
7510 MII_TG3_FET_PTEST_FRC_TX_LOCK);
7511
7512 /* The write needs to be flushed for the AC131 */
7513 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
7514 }
7515
7516 /* Reset to prevent losing 1st rx packet intermittently */
7517 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
7518 tg3_flag(tp, 5780_CLASS)) {
7519 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7520 udelay(10);
7521 tw32_f(MAC_RX_MODE, tp->rx_mode);
7522 }
7523
7524 mac_mode = tp->mac_mode &
7525 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
7526 if (speed == SPEED_1000)
7527 mac_mode |= MAC_MODE_PORT_MODE_GMII;
7528 else
7529 mac_mode |= MAC_MODE_PORT_MODE_MII;
7530
7531 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
7532 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
7533
7534 if (masked_phy_id == TG3_PHY_ID_BCM5401)
7535 mac_mode &= ~MAC_MODE_LINK_POLARITY;
7536 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
7537 mac_mode |= MAC_MODE_LINK_POLARITY;
7538
7539 tg3_writephy(tp, MII_TG3_EXT_CTRL,
7540 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
7541 }
7542
7543 tw32(MAC_MODE, mac_mode);
7544 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00007545
7546 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007547}
7548
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007549static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007550{
7551 struct tg3 *tp = netdev_priv(dev);
7552
7553 if (features & NETIF_F_LOOPBACK) {
7554 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
7555 return;
7556
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007557 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007558 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007559 netif_carrier_on(tp->dev);
7560 spin_unlock_bh(&tp->lock);
7561 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
7562 } else {
7563 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
7564 return;
7565
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007566 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007567 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007568 /* Force link status check */
7569 tg3_setup_phy(tp, 1);
7570 spin_unlock_bh(&tp->lock);
7571 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
7572 }
7573}
7574
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007575static netdev_features_t tg3_fix_features(struct net_device *dev,
7576 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00007577{
7578 struct tg3 *tp = netdev_priv(dev);
7579
Joe Perches63c3a662011-04-26 08:12:10 +00007580 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00007581 features &= ~NETIF_F_ALL_TSO;
7582
7583 return features;
7584}
7585
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007586static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007587{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007588 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007589
7590 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7591 tg3_set_loopback(dev, features);
7592
7593 return 0;
7594}
7595
Matt Carlson21f581a2009-08-28 14:00:25 +00007596static void tg3_rx_prodring_free(struct tg3 *tp,
7597 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007598{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599 int i;
7600
Matt Carlson8fea32b2010-09-15 08:59:58 +00007601 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007602 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007603 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007604 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007605 tp->rx_pkt_map_sz);
7606
Joe Perches63c3a662011-04-26 08:12:10 +00007607 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007608 for (i = tpr->rx_jmb_cons_idx;
7609 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007610 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007611 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007612 TG3_RX_JMB_MAP_SZ);
7613 }
7614 }
7615
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007616 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007618
Matt Carlson2c49a442010-09-30 10:34:35 +00007619 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007620 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007621 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622
Joe Perches63c3a662011-04-26 08:12:10 +00007623 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007624 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007625 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007626 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627 }
7628}
7629
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007630/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631 *
7632 * The chip has been shut down and the driver detached from
7633 * the networking, so no interrupts or new tx packets will
7634 * end up in the driver. tp->{tx,}lock are held and thus
7635 * we may not sleep.
7636 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007637static int tg3_rx_prodring_alloc(struct tg3 *tp,
7638 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007639{
Matt Carlson287be122009-08-28 13:58:46 +00007640 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007641
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007642 tpr->rx_std_cons_idx = 0;
7643 tpr->rx_std_prod_idx = 0;
7644 tpr->rx_jmb_cons_idx = 0;
7645 tpr->rx_jmb_prod_idx = 0;
7646
Matt Carlson8fea32b2010-09-15 08:59:58 +00007647 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007648 memset(&tpr->rx_std_buffers[0], 0,
7649 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007650 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007651 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007652 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007653 goto done;
7654 }
7655
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007657 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007658
Matt Carlson287be122009-08-28 13:58:46 +00007659 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007660 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007661 tp->dev->mtu > ETH_DATA_LEN)
7662 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7663 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007664
Linus Torvalds1da177e2005-04-16 15:20:36 -07007665 /* Initialize invariants of the rings, we only set this
7666 * stuff once. This works because the card does not
7667 * write into the rx buffer posting rings.
7668 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007669 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670 struct tg3_rx_buffer_desc *rxd;
7671
Matt Carlson21f581a2009-08-28 14:00:25 +00007672 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007673 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007674 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7675 rxd->opaque = (RXD_OPAQUE_RING_STD |
7676 (i << RXD_OPAQUE_INDEX_SHIFT));
7677 }
7678
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007679 /* Now allocate fresh SKBs for each rx ring. */
7680 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007681 unsigned int frag_size;
7682
7683 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
7684 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007685 netdev_warn(tp->dev,
7686 "Using a smaller RX standard ring. Only "
7687 "%d out of %d buffers were allocated "
7688 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007689 if (i == 0)
7690 goto initfail;
7691 tp->rx_pending = i;
7692 break;
7693 }
7694 }
7695
Joe Perches63c3a662011-04-26 08:12:10 +00007696 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007697 goto done;
7698
Matt Carlson2c49a442010-09-30 10:34:35 +00007699 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007700
Joe Perches63c3a662011-04-26 08:12:10 +00007701 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007702 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007703
Matt Carlson2c49a442010-09-30 10:34:35 +00007704 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007705 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007706
Matt Carlson0d86df82010-02-17 15:17:00 +00007707 rxd = &tpr->rx_jmb[i].std;
7708 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7709 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7710 RXD_FLAG_JUMBO;
7711 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7712 (i << RXD_OPAQUE_INDEX_SHIFT));
7713 }
7714
7715 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007716 unsigned int frag_size;
7717
7718 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
7719 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007720 netdev_warn(tp->dev,
7721 "Using a smaller RX jumbo ring. Only %d "
7722 "out of %d buffers were allocated "
7723 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007724 if (i == 0)
7725 goto initfail;
7726 tp->rx_jumbo_pending = i;
7727 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007728 }
7729 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007730
7731done:
Michael Chan32d8c572006-07-25 16:38:29 -07007732 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007733
7734initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007735 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007736 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007737}
7738
Matt Carlson21f581a2009-08-28 14:00:25 +00007739static void tg3_rx_prodring_fini(struct tg3 *tp,
7740 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007741{
Matt Carlson21f581a2009-08-28 14:00:25 +00007742 kfree(tpr->rx_std_buffers);
7743 tpr->rx_std_buffers = NULL;
7744 kfree(tpr->rx_jmb_buffers);
7745 tpr->rx_jmb_buffers = NULL;
7746 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007747 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7748 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007749 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007750 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007751 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007752 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7753 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007754 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007755 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007756}
7757
Matt Carlson21f581a2009-08-28 14:00:25 +00007758static int tg3_rx_prodring_init(struct tg3 *tp,
7759 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007760{
Matt Carlson2c49a442010-09-30 10:34:35 +00007761 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7762 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007763 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007764 return -ENOMEM;
7765
Matt Carlson4bae65c2010-11-24 08:31:52 +00007766 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7767 TG3_RX_STD_RING_BYTES(tp),
7768 &tpr->rx_std_mapping,
7769 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007770 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007771 goto err_out;
7772
Joe Perches63c3a662011-04-26 08:12:10 +00007773 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007774 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007775 GFP_KERNEL);
7776 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007777 goto err_out;
7778
Matt Carlson4bae65c2010-11-24 08:31:52 +00007779 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7780 TG3_RX_JMB_RING_BYTES(tp),
7781 &tpr->rx_jmb_mapping,
7782 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007783 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007784 goto err_out;
7785 }
7786
7787 return 0;
7788
7789err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007790 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007791 return -ENOMEM;
7792}
7793
7794/* Free up pending packets in all rx/tx rings.
7795 *
7796 * The chip has been shut down and the driver detached from
7797 * the networking, so no interrupts or new tx packets will
7798 * end up in the driver. tp->{tx,}lock is not held and we are not
7799 * in an interrupt context and thus may sleep.
7800 */
7801static void tg3_free_rings(struct tg3 *tp)
7802{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007803 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007804
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007805 for (j = 0; j < tp->irq_cnt; j++) {
7806 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007807
Matt Carlson8fea32b2010-09-15 08:59:58 +00007808 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007809
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007810 if (!tnapi->tx_buffers)
7811 continue;
7812
Matt Carlson0d681b22011-07-27 14:20:49 +00007813 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7814 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007815
Matt Carlson0d681b22011-07-27 14:20:49 +00007816 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007817 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007818
Matt Carlsonba1142e2011-11-04 09:15:00 +00007819 tg3_tx_skb_unmap(tnapi, i,
7820 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007821
7822 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007823 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00007824 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007825 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007826}
7827
7828/* Initialize tx/rx rings for packet processing.
7829 *
7830 * The chip has been shut down and the driver detached from
7831 * the networking, so no interrupts or new tx packets will
7832 * end up in the driver. tp->{tx,}lock are held and thus
7833 * we may not sleep.
7834 */
7835static int tg3_init_rings(struct tg3 *tp)
7836{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007837 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007838
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007839 /* Free up all the SKBs. */
7840 tg3_free_rings(tp);
7841
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007842 for (i = 0; i < tp->irq_cnt; i++) {
7843 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007844
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007845 tnapi->last_tag = 0;
7846 tnapi->last_irq_tag = 0;
7847 tnapi->hw_status->status = 0;
7848 tnapi->hw_status->status_tag = 0;
7849 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7850
7851 tnapi->tx_prod = 0;
7852 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007853 if (tnapi->tx_ring)
7854 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007855
7856 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007857 if (tnapi->rx_rcb)
7858 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007859
Matt Carlson8fea32b2010-09-15 08:59:58 +00007860 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007861 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007862 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007863 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007864 }
Matt Carlson72334482009-08-28 14:03:01 +00007865
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007866 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007867}
7868
Michael Chan49a359e2012-09-28 07:12:37 +00007869static void tg3_mem_tx_release(struct tg3 *tp)
7870{
7871 int i;
7872
7873 for (i = 0; i < tp->irq_max; i++) {
7874 struct tg3_napi *tnapi = &tp->napi[i];
7875
7876 if (tnapi->tx_ring) {
7877 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
7878 tnapi->tx_ring, tnapi->tx_desc_mapping);
7879 tnapi->tx_ring = NULL;
7880 }
7881
7882 kfree(tnapi->tx_buffers);
7883 tnapi->tx_buffers = NULL;
7884 }
7885}
7886
7887static int tg3_mem_tx_acquire(struct tg3 *tp)
7888{
7889 int i;
7890 struct tg3_napi *tnapi = &tp->napi[0];
7891
7892 /* If multivector TSS is enabled, vector 0 does not handle
7893 * tx interrupts. Don't allocate any resources for it.
7894 */
7895 if (tg3_flag(tp, ENABLE_TSS))
7896 tnapi++;
7897
7898 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
7899 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
7900 TG3_TX_RING_SIZE, GFP_KERNEL);
7901 if (!tnapi->tx_buffers)
7902 goto err_out;
7903
7904 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7905 TG3_TX_RING_BYTES,
7906 &tnapi->tx_desc_mapping,
7907 GFP_KERNEL);
7908 if (!tnapi->tx_ring)
7909 goto err_out;
7910 }
7911
7912 return 0;
7913
7914err_out:
7915 tg3_mem_tx_release(tp);
7916 return -ENOMEM;
7917}
7918
7919static void tg3_mem_rx_release(struct tg3 *tp)
7920{
7921 int i;
7922
7923 for (i = 0; i < tp->irq_max; i++) {
7924 struct tg3_napi *tnapi = &tp->napi[i];
7925
7926 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7927
7928 if (!tnapi->rx_rcb)
7929 continue;
7930
7931 dma_free_coherent(&tp->pdev->dev,
7932 TG3_RX_RCB_RING_BYTES(tp),
7933 tnapi->rx_rcb,
7934 tnapi->rx_rcb_mapping);
7935 tnapi->rx_rcb = NULL;
7936 }
7937}
7938
7939static int tg3_mem_rx_acquire(struct tg3 *tp)
7940{
7941 unsigned int i, limit;
7942
7943 limit = tp->rxq_cnt;
7944
7945 /* If RSS is enabled, we need a (dummy) producer ring
7946 * set on vector zero. This is the true hw prodring.
7947 */
7948 if (tg3_flag(tp, ENABLE_RSS))
7949 limit++;
7950
7951 for (i = 0; i < limit; i++) {
7952 struct tg3_napi *tnapi = &tp->napi[i];
7953
7954 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7955 goto err_out;
7956
7957 /* If multivector RSS is enabled, vector 0
7958 * does not handle rx or tx interrupts.
7959 * Don't allocate any resources for it.
7960 */
7961 if (!i && tg3_flag(tp, ENABLE_RSS))
7962 continue;
7963
7964 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7965 TG3_RX_RCB_RING_BYTES(tp),
7966 &tnapi->rx_rcb_mapping,
7967 GFP_KERNEL);
7968 if (!tnapi->rx_rcb)
7969 goto err_out;
7970
7971 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
7972 }
7973
7974 return 0;
7975
7976err_out:
7977 tg3_mem_rx_release(tp);
7978 return -ENOMEM;
7979}
7980
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007981/*
7982 * Must not be invoked with interrupt sources disabled and
7983 * the hardware shutdown down.
7984 */
7985static void tg3_free_consistent(struct tg3 *tp)
7986{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007987 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007988
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007989 for (i = 0; i < tp->irq_cnt; i++) {
7990 struct tg3_napi *tnapi = &tp->napi[i];
7991
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007992 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007993 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7994 tnapi->hw_status,
7995 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007996 tnapi->hw_status = NULL;
7997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007998 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007999
Michael Chan49a359e2012-09-28 07:12:37 +00008000 tg3_mem_rx_release(tp);
8001 tg3_mem_tx_release(tp);
8002
Linus Torvalds1da177e2005-04-16 15:20:36 -07008003 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008004 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8005 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006 tp->hw_stats = NULL;
8007 }
8008}
8009
8010/*
8011 * Must not be invoked with interrupt sources disabled and
8012 * the hardware shutdown down. Can sleep.
8013 */
8014static int tg3_alloc_consistent(struct tg3 *tp)
8015{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008016 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008017
Matt Carlson4bae65c2010-11-24 08:31:52 +00008018 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
8019 sizeof(struct tg3_hw_stats),
8020 &tp->stats_mapping,
8021 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008022 if (!tp->hw_stats)
8023 goto err_out;
8024
Linus Torvalds1da177e2005-04-16 15:20:36 -07008025 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8026
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008027 for (i = 0; i < tp->irq_cnt; i++) {
8028 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008029 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008030
Matt Carlson4bae65c2010-11-24 08:31:52 +00008031 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
8032 TG3_HW_STATUS_SIZE,
8033 &tnapi->status_mapping,
8034 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008035 if (!tnapi->hw_status)
8036 goto err_out;
8037
8038 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008039 sblk = tnapi->hw_status;
8040
Michael Chan49a359e2012-09-28 07:12:37 +00008041 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008042 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008043
Michael Chan49a359e2012-09-28 07:12:37 +00008044 /*
8045 * When RSS is enabled, the status block format changes
8046 * slightly. The "rx_jumbo_consumer", "reserved",
8047 * and "rx_mini_consumer" members get mapped to the
8048 * other three rx return ring producer indexes.
8049 */
8050 switch (i) {
8051 case 1:
8052 prodptr = &sblk->idx[0].rx_producer;
8053 break;
8054 case 2:
8055 prodptr = &sblk->rx_jumbo_consumer;
8056 break;
8057 case 3:
8058 prodptr = &sblk->reserved;
8059 break;
8060 case 4:
8061 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008062 break;
8063 }
Michael Chan49a359e2012-09-28 07:12:37 +00008064 tnapi->rx_rcb_prod_idx = prodptr;
8065 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008066 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008067 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008068 }
8069
Michael Chan49a359e2012-09-28 07:12:37 +00008070 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8071 goto err_out;
8072
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073 return 0;
8074
8075err_out:
8076 tg3_free_consistent(tp);
8077 return -ENOMEM;
8078}
8079
8080#define MAX_WAIT_CNT 1000
8081
8082/* To stop a block, clear the enable bit and poll till it
8083 * clears. tp->lock is held.
8084 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008085static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008086{
8087 unsigned int i;
8088 u32 val;
8089
Joe Perches63c3a662011-04-26 08:12:10 +00008090 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008091 switch (ofs) {
8092 case RCVLSC_MODE:
8093 case DMAC_MODE:
8094 case MBFREE_MODE:
8095 case BUFMGR_MODE:
8096 case MEMARB_MODE:
8097 /* We can't enable/disable these bits of the
8098 * 5705/5750, just say success.
8099 */
8100 return 0;
8101
8102 default:
8103 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008105 }
8106
8107 val = tr32(ofs);
8108 val &= ~enable_bit;
8109 tw32_f(ofs, val);
8110
8111 for (i = 0; i < MAX_WAIT_CNT; i++) {
8112 udelay(100);
8113 val = tr32(ofs);
8114 if ((val & enable_bit) == 0)
8115 break;
8116 }
8117
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008118 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008119 dev_err(&tp->pdev->dev,
8120 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8121 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008122 return -ENODEV;
8123 }
8124
8125 return 0;
8126}
8127
8128/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008129static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008130{
8131 int i, err;
8132
8133 tg3_disable_ints(tp);
8134
8135 tp->rx_mode &= ~RX_MODE_ENABLE;
8136 tw32_f(MAC_RX_MODE, tp->rx_mode);
8137 udelay(10);
8138
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008139 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8140 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8141 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8142 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8143 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8144 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008145
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008146 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8147 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8148 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8149 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8150 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8151 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8152 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008153
8154 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8155 tw32_f(MAC_MODE, tp->mac_mode);
8156 udelay(40);
8157
8158 tp->tx_mode &= ~TX_MODE_ENABLE;
8159 tw32_f(MAC_TX_MODE, tp->tx_mode);
8160
8161 for (i = 0; i < MAX_WAIT_CNT; i++) {
8162 udelay(100);
8163 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8164 break;
8165 }
8166 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008167 dev_err(&tp->pdev->dev,
8168 "%s timed out, TX_MODE_ENABLE will not clear "
8169 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008170 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008171 }
8172
Michael Chane6de8ad2005-05-05 14:42:41 -07008173 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008174 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8175 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008176
8177 tw32(FTQ_RESET, 0xffffffff);
8178 tw32(FTQ_RESET, 0x00000000);
8179
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008180 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8181 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008182
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008183 for (i = 0; i < tp->irq_cnt; i++) {
8184 struct tg3_napi *tnapi = &tp->napi[i];
8185 if (tnapi->hw_status)
8186 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008188
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189 return err;
8190}
8191
Michael Chanee6a99b2007-07-18 21:49:10 -07008192/* Save PCI command register before chip reset */
8193static void tg3_save_pci_state(struct tg3 *tp)
8194{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008195 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008196}
8197
8198/* Restore PCI state after chip reset */
8199static void tg3_restore_pci_state(struct tg3 *tp)
8200{
8201 u32 val;
8202
8203 /* Re-enable indirect register accesses. */
8204 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8205 tp->misc_host_ctrl);
8206
8207 /* Set MAX PCI retry to zero. */
8208 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
8209 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008210 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008211 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008212 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008213 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008214 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008215 PCISTATE_ALLOW_APE_SHMEM_WR |
8216 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008217 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8218
Matt Carlson8a6eac92007-10-21 16:17:55 -07008219 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008220
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008221 if (!tg3_flag(tp, PCI_EXPRESS)) {
8222 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8223 tp->pci_cacheline_sz);
8224 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8225 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008226 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008227
Michael Chanee6a99b2007-07-18 21:49:10 -07008228 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008229 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008230 u16 pcix_cmd;
8231
8232 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8233 &pcix_cmd);
8234 pcix_cmd &= ~PCI_X_CMD_ERO;
8235 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8236 pcix_cmd);
8237 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008238
Joe Perches63c3a662011-04-26 08:12:10 +00008239 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008240
8241 /* Chip reset on 5780 will reset MSI enable bit,
8242 * so need to restore it.
8243 */
Joe Perches63c3a662011-04-26 08:12:10 +00008244 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008245 u16 ctrl;
8246
8247 pci_read_config_word(tp->pdev,
8248 tp->msi_cap + PCI_MSI_FLAGS,
8249 &ctrl);
8250 pci_write_config_word(tp->pdev,
8251 tp->msi_cap + PCI_MSI_FLAGS,
8252 ctrl | PCI_MSI_FLAGS_ENABLE);
8253 val = tr32(MSGINT_MODE);
8254 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8255 }
8256 }
8257}
8258
Linus Torvalds1da177e2005-04-16 15:20:36 -07008259/* tp->lock is held. */
8260static int tg3_chip_reset(struct tg3 *tp)
8261{
8262 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07008263 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00008264 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008265
David S. Millerf49639e2006-06-09 11:58:36 -07008266 tg3_nvram_lock(tp);
8267
Matt Carlson77b483f2008-08-15 14:07:24 -07008268 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
8269
David S. Millerf49639e2006-06-09 11:58:36 -07008270 /* No matching tg3_nvram_unlock() after this because
8271 * chip reset below will undo the nvram lock.
8272 */
8273 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008274
Michael Chanee6a99b2007-07-18 21:49:10 -07008275 /* GRC_MISC_CFG core clock reset will clear the memory
8276 * enable bit in PCI register 4 and the MSI enable bit
8277 * on some chips, so we save relevant registers here.
8278 */
8279 tg3_save_pci_state(tp);
8280
Michael Chand9ab5ad12006-03-20 22:27:35 -08008281 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008282 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08008283 tw32(GRC_FASTBOOT_PC, 0);
8284
Linus Torvalds1da177e2005-04-16 15:20:36 -07008285 /*
8286 * We must avoid the readl() that normally takes place.
8287 * It locks machines, causes machine checks, and other
8288 * fun things. So, temporarily disable the 5701
8289 * hardware workaround, while we do the reset.
8290 */
Michael Chan1ee582d2005-08-09 20:16:46 -07008291 write_op = tp->write32;
8292 if (write_op == tg3_write_flush_reg32)
8293 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008294
Michael Chand18edcb2007-03-24 20:57:11 -07008295 /* Prevent the irq handler from reading or writing PCI registers
8296 * during chip reset when the memory enable bit in the PCI command
8297 * register may be cleared. The chip does not generate interrupt
8298 * at this time, but the irq handler may still be called due to irq
8299 * sharing or irqpoll.
8300 */
Joe Perches63c3a662011-04-26 08:12:10 +00008301 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008302 for (i = 0; i < tp->irq_cnt; i++) {
8303 struct tg3_napi *tnapi = &tp->napi[i];
8304 if (tnapi->hw_status) {
8305 tnapi->hw_status->status = 0;
8306 tnapi->hw_status->status_tag = 0;
8307 }
8308 tnapi->last_tag = 0;
8309 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07008310 }
Michael Chand18edcb2007-03-24 20:57:11 -07008311 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00008312
8313 for (i = 0; i < tp->irq_cnt; i++)
8314 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07008315
Matt Carlson255ca312009-08-25 10:07:27 +00008316 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8317 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8318 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
8319 }
8320
Linus Torvalds1da177e2005-04-16 15:20:36 -07008321 /* do the reset */
8322 val = GRC_MISC_CFG_CORECLK_RESET;
8323
Joe Perches63c3a662011-04-26 08:12:10 +00008324 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00008325 /* Force PCIe 1.0a mode */
8326 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008327 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00008328 tr32(TG3_PCIE_PHY_TSTCTL) ==
8329 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
8330 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
8331
Linus Torvalds1da177e2005-04-16 15:20:36 -07008332 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
8333 tw32(GRC_MISC_CFG, (1 << 29));
8334 val |= (1 << 29);
8335 }
8336 }
8337
Michael Chanb5d37722006-09-27 16:06:21 -07008338 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
8339 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
8340 tw32(GRC_VCPU_EXT_CTRL,
8341 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
8342 }
8343
Matt Carlsonf37500d2010-08-02 11:25:59 +00008344 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00008345 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008346 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00008347
Linus Torvalds1da177e2005-04-16 15:20:36 -07008348 tw32(GRC_MISC_CFG, val);
8349
Michael Chan1ee582d2005-08-09 20:16:46 -07008350 /* restore 5701 hardware bug workaround write method */
8351 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008352
8353 /* Unfortunately, we have to delay before the PCI read back.
8354 * Some 575X chips even will not respond to a PCI cfg access
8355 * when the reset command is given to the chip.
8356 *
8357 * How do these hardware designers expect things to work
8358 * properly if the PCI write is posted for a long period
8359 * of time? It is always necessary to have some method by
8360 * which a register read back can occur to push the write
8361 * out which does the reset.
8362 *
8363 * For most tg3 variants the trick below was working.
8364 * Ho hum...
8365 */
8366 udelay(120);
8367
8368 /* Flush PCI posted writes. The normal MMIO registers
8369 * are inaccessible at this time so this is the only
8370 * way to make this reliably (actually, this is no longer
8371 * the case, see above). I tried to use indirect
8372 * register read/write but this upset some 5701 variants.
8373 */
8374 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
8375
8376 udelay(120);
8377
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008378 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00008379 u16 val16;
8380
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07008382 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008383 u32 cfg_val;
8384
8385 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07008386 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008387 udelay(100);
8388
8389 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
8390 pci_write_config_dword(tp->pdev, 0xc4,
8391 cfg_val | (1 << 15));
8392 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008393
Matt Carlsone7126992009-08-25 10:08:16 +00008394 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008395 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00008396 /*
8397 * Older PCIe devices only support the 128 byte
8398 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008399 */
Joe Perches63c3a662011-04-26 08:12:10 +00008400 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008401 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
8402 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008403
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008404 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008405 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008406 PCI_EXP_DEVSTA_CED |
8407 PCI_EXP_DEVSTA_NFED |
8408 PCI_EXP_DEVSTA_FED |
8409 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008410 }
8411
Michael Chanee6a99b2007-07-18 21:49:10 -07008412 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008413
Joe Perches63c3a662011-04-26 08:12:10 +00008414 tg3_flag_clear(tp, CHIP_RESETTING);
8415 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07008416
Michael Chanee6a99b2007-07-18 21:49:10 -07008417 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008418 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07008419 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07008420 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008421
8422 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
8423 tg3_stop_fw(tp);
8424 tw32(0x5000, 0x400);
8425 }
8426
8427 tw32(GRC_MODE, tp->grc_mode);
8428
8429 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008430 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008431
8432 tw32(0xc4, val | (1 << 15));
8433 }
8434
8435 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
8436 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
8437 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
8438 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
8439 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
8440 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8441 }
8442
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008443 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008444 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008445 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008446 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008447 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008448 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008449 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008450 val = 0;
8451
8452 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008453 udelay(40);
8454
Matt Carlson77b483f2008-08-15 14:07:24 -07008455 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
8456
Michael Chan7a6f4362006-09-27 16:03:31 -07008457 err = tg3_poll_fw(tp);
8458 if (err)
8459 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008460
Matt Carlson0a9140c2009-08-28 12:27:50 +00008461 tg3_mdio_start(tp);
8462
Joe Perches63c3a662011-04-26 08:12:10 +00008463 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008464 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
8465 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008466 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008467 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008468
8469 tw32(0x7c00, val | (1 << 25));
8470 }
8471
Matt Carlsond78b59f2011-04-05 14:22:46 +00008472 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8473 val = tr32(TG3_CPMU_CLCK_ORIDE);
8474 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8475 }
8476
Linus Torvalds1da177e2005-04-16 15:20:36 -07008477 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00008478 tg3_flag_clear(tp, ENABLE_ASF);
8479 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008480 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
8481 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
8482 u32 nic_cfg;
8483
8484 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
8485 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00008486 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008487 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00008488 if (tg3_flag(tp, 5750_PLUS))
8489 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008490 }
8491 }
8492
8493 return 0;
8494}
8495
Matt Carlson65ec6982012-02-28 23:33:37 +00008496static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
8497static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00008498
Linus Torvalds1da177e2005-04-16 15:20:36 -07008499/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07008500static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501{
8502 int err;
8503
8504 tg3_stop_fw(tp);
8505
Michael Chan944d9802005-05-29 14:57:48 -07008506 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008507
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008508 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008509 err = tg3_chip_reset(tp);
8510
Matt Carlsondaba2a62009-04-20 06:58:52 +00008511 __tg3_set_mac_addr(tp, 0);
8512
Michael Chan944d9802005-05-29 14:57:48 -07008513 tg3_write_sig_legacy(tp, kind);
8514 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008515
Matt Carlson92feeab2011-12-08 14:40:14 +00008516 if (tp->hw_stats) {
8517 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05008518 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00008519 tg3_get_estats(tp, &tp->estats_prev);
8520
8521 /* And make sure the next sample is new data */
8522 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8523 }
8524
Linus Torvalds1da177e2005-04-16 15:20:36 -07008525 if (err)
8526 return err;
8527
8528 return 0;
8529}
8530
Linus Torvalds1da177e2005-04-16 15:20:36 -07008531static int tg3_set_mac_addr(struct net_device *dev, void *p)
8532{
8533 struct tg3 *tp = netdev_priv(dev);
8534 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07008535 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008536
Michael Chanf9804dd2005-09-27 12:13:10 -07008537 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00008538 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07008539
Linus Torvalds1da177e2005-04-16 15:20:36 -07008540 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8541
Michael Chane75f7c92006-03-20 21:33:26 -08008542 if (!netif_running(dev))
8543 return 0;
8544
Joe Perches63c3a662011-04-26 08:12:10 +00008545 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07008546 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07008547
Michael Chan986e0ae2007-05-05 12:10:20 -07008548 addr0_high = tr32(MAC_ADDR_0_HIGH);
8549 addr0_low = tr32(MAC_ADDR_0_LOW);
8550 addr1_high = tr32(MAC_ADDR_1_HIGH);
8551 addr1_low = tr32(MAC_ADDR_1_LOW);
8552
8553 /* Skip MAC addr 1 if ASF is using it. */
8554 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
8555 !(addr1_high == 0 && addr1_low == 0))
8556 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07008557 }
Michael Chan986e0ae2007-05-05 12:10:20 -07008558 spin_lock_bh(&tp->lock);
8559 __tg3_set_mac_addr(tp, skip_mac_1);
8560 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008561
Michael Chanb9ec6c12006-07-25 16:37:27 -07008562 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008563}
8564
8565/* tp->lock is held. */
8566static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
8567 dma_addr_t mapping, u32 maxlen_flags,
8568 u32 nic_addr)
8569{
8570 tg3_write_mem(tp,
8571 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
8572 ((u64) mapping >> 32));
8573 tg3_write_mem(tp,
8574 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
8575 ((u64) mapping & 0xffffffff));
8576 tg3_write_mem(tp,
8577 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
8578 maxlen_flags);
8579
Joe Perches63c3a662011-04-26 08:12:10 +00008580 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008581 tg3_write_mem(tp,
8582 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
8583 nic_addr);
8584}
8585
Michael Chana489b6d2012-09-28 07:12:39 +00008586
8587static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008588{
Michael Chana489b6d2012-09-28 07:12:39 +00008589 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00008590
Joe Perches63c3a662011-04-26 08:12:10 +00008591 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008592 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8593 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8594 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008595 } else {
8596 tw32(HOSTCC_TXCOL_TICKS, 0);
8597 tw32(HOSTCC_TXMAX_FRAMES, 0);
8598 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00008599
8600 for (; i < tp->txq_cnt; i++) {
8601 u32 reg;
8602
8603 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8604 tw32(reg, ec->tx_coalesce_usecs);
8605 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8606 tw32(reg, ec->tx_max_coalesced_frames);
8607 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8608 tw32(reg, ec->tx_max_coalesced_frames_irq);
8609 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00008610 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008611
Michael Chana489b6d2012-09-28 07:12:39 +00008612 for (; i < tp->irq_max - 1; i++) {
8613 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8614 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8615 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8616 }
8617}
8618
8619static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
8620{
8621 int i = 0;
8622 u32 limit = tp->rxq_cnt;
8623
Joe Perches63c3a662011-04-26 08:12:10 +00008624 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008625 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8626 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8627 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00008628 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008629 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008630 tw32(HOSTCC_RXCOL_TICKS, 0);
8631 tw32(HOSTCC_RXMAX_FRAMES, 0);
8632 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008633 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008634
Michael Chana489b6d2012-09-28 07:12:39 +00008635 for (; i < limit; i++) {
8636 u32 reg;
8637
8638 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8639 tw32(reg, ec->rx_coalesce_usecs);
8640 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8641 tw32(reg, ec->rx_max_coalesced_frames);
8642 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8643 tw32(reg, ec->rx_max_coalesced_frames_irq);
8644 }
8645
8646 for (; i < tp->irq_max - 1; i++) {
8647 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
8648 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
8649 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8650 }
8651}
8652
8653static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
8654{
8655 tg3_coal_tx_init(tp, ec);
8656 tg3_coal_rx_init(tp, ec);
8657
Joe Perches63c3a662011-04-26 08:12:10 +00008658 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008659 u32 val = ec->stats_block_coalesce_usecs;
8660
Matt Carlsonb6080e12009-09-01 13:12:00 +00008661 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8662 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8663
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00008664 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07008665 val = 0;
8666
8667 tw32(HOSTCC_STAT_COAL_TICKS, val);
8668 }
8669}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008670
8671/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008672static void tg3_rings_reset(struct tg3 *tp)
8673{
8674 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008675 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008676 struct tg3_napi *tnapi = &tp->napi[0];
8677
8678 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008679 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008680 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008681 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008682 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Michael Chanc65a17f2013-01-06 12:51:07 +00008683 else if (tg3_flag(tp, 57765_CLASS) ||
8684 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762)
Matt Carlsonb703df62009-12-03 08:36:21 +00008685 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008686 else
8687 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8688
8689 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8690 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8691 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8692 BDINFO_FLAGS_DISABLED);
8693
8694
8695 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008696 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008697 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008698 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008699 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008700 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chanc65a17f2013-01-06 12:51:07 +00008701 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008702 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008703 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8704 else
8705 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8706
8707 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8708 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8709 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8710 BDINFO_FLAGS_DISABLED);
8711
8712 /* Disable interrupts */
8713 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008714 tp->napi[0].chk_msi_cnt = 0;
8715 tp->napi[0].last_rx_cons = 0;
8716 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008717
8718 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008719 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008720 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008721 tp->napi[i].tx_prod = 0;
8722 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008723 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008724 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008725 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8726 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008727 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008728 tp->napi[i].last_rx_cons = 0;
8729 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008730 }
Joe Perches63c3a662011-04-26 08:12:10 +00008731 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008732 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008733 } else {
8734 tp->napi[0].tx_prod = 0;
8735 tp->napi[0].tx_cons = 0;
8736 tw32_mailbox(tp->napi[0].prodmbox, 0);
8737 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8738 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008739
8740 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008741 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008742 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8743 for (i = 0; i < 16; i++)
8744 tw32_tx_mbox(mbox + i * 8, 0);
8745 }
8746
8747 txrcb = NIC_SRAM_SEND_RCB;
8748 rxrcb = NIC_SRAM_RCV_RET_RCB;
8749
8750 /* Clear status block in ram. */
8751 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8752
8753 /* Set status block DMA address */
8754 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8755 ((u64) tnapi->status_mapping >> 32));
8756 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8757 ((u64) tnapi->status_mapping & 0xffffffff));
8758
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008759 if (tnapi->tx_ring) {
8760 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8761 (TG3_TX_RING_SIZE <<
8762 BDINFO_FLAGS_MAXLEN_SHIFT),
8763 NIC_SRAM_TX_BUFFER_DESC);
8764 txrcb += TG3_BDINFO_SIZE;
8765 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008766
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008767 if (tnapi->rx_rcb) {
8768 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008769 (tp->rx_ret_ring_mask + 1) <<
8770 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008771 rxrcb += TG3_BDINFO_SIZE;
8772 }
8773
8774 stblk = HOSTCC_STATBLCK_RING1;
8775
8776 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8777 u64 mapping = (u64)tnapi->status_mapping;
8778 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8779 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8780
8781 /* Clear status block in ram. */
8782 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8783
Matt Carlson19cfaec2009-12-03 08:36:20 +00008784 if (tnapi->tx_ring) {
8785 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8786 (TG3_TX_RING_SIZE <<
8787 BDINFO_FLAGS_MAXLEN_SHIFT),
8788 NIC_SRAM_TX_BUFFER_DESC);
8789 txrcb += TG3_BDINFO_SIZE;
8790 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008791
8792 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008793 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008794 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8795
8796 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008797 rxrcb += TG3_BDINFO_SIZE;
8798 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008799}
8800
Matt Carlsoneb07a942011-04-20 07:57:36 +00008801static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8802{
8803 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8804
Joe Perches63c3a662011-04-26 08:12:10 +00008805 if (!tg3_flag(tp, 5750_PLUS) ||
8806 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008807 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008808 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8809 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008810 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8811 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8812 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8813 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8814 else
8815 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8816
8817 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8818 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8819
8820 val = min(nic_rep_thresh, host_rep_thresh);
8821 tw32(RCVBDI_STD_THRESH, val);
8822
Joe Perches63c3a662011-04-26 08:12:10 +00008823 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008824 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8825
Joe Perches63c3a662011-04-26 08:12:10 +00008826 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008827 return;
8828
Matt Carlson513aa6e2011-11-21 15:01:18 +00008829 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008830
8831 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8832
8833 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8834 tw32(RCVBDI_JUMBO_THRESH, val);
8835
Joe Perches63c3a662011-04-26 08:12:10 +00008836 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008837 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8838}
8839
Matt Carlsonccd5ba92012-02-13 10:20:08 +00008840static inline u32 calc_crc(unsigned char *buf, int len)
8841{
8842 u32 reg;
8843 u32 tmp;
8844 int j, k;
8845
8846 reg = 0xffffffff;
8847
8848 for (j = 0; j < len; j++) {
8849 reg ^= buf[j];
8850
8851 for (k = 0; k < 8; k++) {
8852 tmp = reg & 0x01;
8853
8854 reg >>= 1;
8855
8856 if (tmp)
8857 reg ^= 0xedb88320;
8858 }
8859 }
8860
8861 return ~reg;
8862}
8863
8864static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8865{
8866 /* accept or reject all multicast frames */
8867 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8868 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8869 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8870 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8871}
8872
8873static void __tg3_set_rx_mode(struct net_device *dev)
8874{
8875 struct tg3 *tp = netdev_priv(dev);
8876 u32 rx_mode;
8877
8878 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8879 RX_MODE_KEEP_VLAN_TAG);
8880
8881#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
8882 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8883 * flag clear.
8884 */
8885 if (!tg3_flag(tp, ENABLE_ASF))
8886 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8887#endif
8888
8889 if (dev->flags & IFF_PROMISC) {
8890 /* Promiscuous mode. */
8891 rx_mode |= RX_MODE_PROMISC;
8892 } else if (dev->flags & IFF_ALLMULTI) {
8893 /* Accept all multicast. */
8894 tg3_set_multi(tp, 1);
8895 } else if (netdev_mc_empty(dev)) {
8896 /* Reject all multicast. */
8897 tg3_set_multi(tp, 0);
8898 } else {
8899 /* Accept one or more multicast(s). */
8900 struct netdev_hw_addr *ha;
8901 u32 mc_filter[4] = { 0, };
8902 u32 regidx;
8903 u32 bit;
8904 u32 crc;
8905
8906 netdev_for_each_mc_addr(ha, dev) {
8907 crc = calc_crc(ha->addr, ETH_ALEN);
8908 bit = ~crc & 0x7f;
8909 regidx = (bit & 0x60) >> 5;
8910 bit &= 0x1f;
8911 mc_filter[regidx] |= (1 << bit);
8912 }
8913
8914 tw32(MAC_HASH_REG_0, mc_filter[0]);
8915 tw32(MAC_HASH_REG_1, mc_filter[1]);
8916 tw32(MAC_HASH_REG_2, mc_filter[2]);
8917 tw32(MAC_HASH_REG_3, mc_filter[3]);
8918 }
8919
8920 if (rx_mode != tp->rx_mode) {
8921 tp->rx_mode = rx_mode;
8922 tw32_f(MAC_RX_MODE, rx_mode);
8923 udelay(10);
8924 }
8925}
8926
Michael Chan91024262012-09-28 07:12:38 +00008927static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00008928{
8929 int i;
8930
8931 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00008932 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00008933}
8934
8935static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008936{
8937 int i;
8938
8939 if (!tg3_flag(tp, SUPPORT_MSIX))
8940 return;
8941
Michael Chan0b3ba052012-11-14 14:44:29 +00008942 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008943 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00008944 return;
8945 }
8946
8947 /* Validate table against current IRQ count */
8948 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00008949 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00008950 break;
8951 }
8952
8953 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00008954 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008955}
8956
Matt Carlson90415472011-12-16 13:33:23 +00008957static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008958{
8959 int i = 0;
8960 u32 reg = MAC_RSS_INDIR_TBL_0;
8961
8962 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8963 u32 val = tp->rss_ind_tbl[i];
8964 i++;
8965 for (; i % 8; i++) {
8966 val <<= 4;
8967 val |= tp->rss_ind_tbl[i];
8968 }
8969 tw32(reg, val);
8970 reg += 4;
8971 }
8972}
8973
Matt Carlson2d31eca2009-09-01 12:53:31 +00008974/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008975static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008976{
8977 u32 val, rdmac_mode;
8978 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008979 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008980
8981 tg3_disable_ints(tp);
8982
8983 tg3_stop_fw(tp);
8984
8985 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8986
Joe Perches63c3a662011-04-26 08:12:10 +00008987 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008988 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008989
Matt Carlson699c0192010-12-06 08:28:51 +00008990 /* Enable MAC control of LPI */
8991 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
Michael Chanc65a17f2013-01-06 12:51:07 +00008992 val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8993 TG3_CPMU_EEE_LNKIDL_UART_IDL;
8994 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8995 val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
8996
8997 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
Matt Carlson699c0192010-12-06 08:28:51 +00008998
8999 tw32_f(TG3_CPMU_EEE_CTRL,
9000 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
9001
Matt Carlsona386b902010-12-06 08:28:53 +00009002 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
9003 TG3_CPMU_EEEMD_LPI_IN_TX |
9004 TG3_CPMU_EEEMD_LPI_IN_RX |
9005 TG3_CPMU_EEEMD_EEE_ENABLE;
9006
9007 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
9008 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
9009
Joe Perches63c3a662011-04-26 08:12:10 +00009010 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00009011 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
9012
9013 tw32_f(TG3_CPMU_EEE_MODE, val);
9014
9015 tw32_f(TG3_CPMU_EEE_DBTMR1,
9016 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
9017 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
9018
9019 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00009020 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00009021 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00009022 }
9023
Matt Carlson603f1172010-02-12 14:47:10 +00009024 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009025 tg3_phy_reset(tp);
9026
Linus Torvalds1da177e2005-04-16 15:20:36 -07009027 err = tg3_chip_reset(tp);
9028 if (err)
9029 return err;
9030
9031 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9032
Matt Carlsonbcb37f62008-11-03 16:52:09 -08009033 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009034 val = tr32(TG3_CPMU_CTRL);
9035 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9036 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009037
9038 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9039 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9040 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9041 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9042
9043 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9044 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9045 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9046 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9047
9048 val = tr32(TG3_CPMU_HST_ACC);
9049 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9050 val |= CPMU_HST_ACC_MACCLK_6_25;
9051 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009052 }
9053
Matt Carlson33466d932009-04-20 06:57:41 +00009054 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
9055 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9056 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9057 PCIE_PWR_MGMT_L1_THRESH_4MS;
9058 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009059
9060 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9061 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9062
9063 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d932009-04-20 06:57:41 +00009064
Matt Carlsonf40386c2009-11-02 14:24:02 +00009065 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9066 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009067 }
9068
Joe Perches63c3a662011-04-26 08:12:10 +00009069 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009070 u32 grc_mode = tr32(GRC_MODE);
9071
9072 /* Access the lower 1K of PL PCIE block registers. */
9073 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9074 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9075
9076 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9077 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9078 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9079
9080 tw32(GRC_MODE, grc_mode);
9081 }
9082
Matt Carlson55086ad2011-12-14 11:09:59 +00009083 if (tg3_flag(tp, 57765_CLASS)) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009084 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
9085 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009086
Matt Carlson5093eed2010-11-24 08:31:45 +00009087 /* Access the lower 1K of PL PCIE block registers. */
9088 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9089 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009090
Matt Carlson5093eed2010-11-24 08:31:45 +00009091 val = tr32(TG3_PCIE_TLDLPL_PORT +
9092 TG3_PCIE_PL_LO_PHYCTL5);
9093 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9094 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009095
Matt Carlson5093eed2010-11-24 08:31:45 +00009096 tw32(GRC_MODE, grc_mode);
9097 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009098
Matt Carlson1ff30a52011-05-19 12:12:46 +00009099 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
9100 u32 grc_mode = tr32(GRC_MODE);
9101
9102 /* Access the lower 1K of DL PCIE block registers. */
9103 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9104 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9105
9106 val = tr32(TG3_PCIE_TLDLPL_PORT +
9107 TG3_PCIE_DL_LO_FTSMAX);
9108 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9109 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9110 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9111
9112 tw32(GRC_MODE, grc_mode);
9113 }
9114
Matt Carlsona977dbe2010-04-12 06:58:26 +00009115 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9116 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9117 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9118 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009119 }
9120
Linus Torvalds1da177e2005-04-16 15:20:36 -07009121 /* This works around an issue with Athlon chipsets on
9122 * B3 tigon3 silicon. This bit has no effect on any
9123 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009124 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009125 */
Joe Perches63c3a662011-04-26 08:12:10 +00009126 if (!tg3_flag(tp, CPMU_PRESENT)) {
9127 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009128 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9129 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009131
9132 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009133 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009134 val = tr32(TG3PCI_PCISTATE);
9135 val |= PCISTATE_RETRY_SAME_DMA;
9136 tw32(TG3PCI_PCISTATE, val);
9137 }
9138
Joe Perches63c3a662011-04-26 08:12:10 +00009139 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009140 /* Allow reads and writes to the
9141 * APE register and memory space.
9142 */
9143 val = tr32(TG3PCI_PCISTATE);
9144 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009145 PCISTATE_ALLOW_APE_SHMEM_WR |
9146 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009147 tw32(TG3PCI_PCISTATE, val);
9148 }
9149
Linus Torvalds1da177e2005-04-16 15:20:36 -07009150 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
9151 /* Enable some hw fixes. */
9152 val = tr32(TG3PCI_MSI_DATA);
9153 val |= (1 << 26) | (1 << 28) | (1 << 29);
9154 tw32(TG3PCI_MSI_DATA, val);
9155 }
9156
9157 /* Descriptor ring init may make accesses to the
9158 * NIC SRAM area to setup the TX descriptors, so we
9159 * can only do this after the hardware has been
9160 * successfully reset.
9161 */
Michael Chan32d8c572006-07-25 16:38:29 -07009162 err = tg3_init_rings(tp);
9163 if (err)
9164 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009165
Joe Perches63c3a662011-04-26 08:12:10 +00009166 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009167 val = tr32(TG3PCI_DMA_RW_CTRL) &
9168 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00009169 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
9170 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00009171 if (!tg3_flag(tp, 57765_CLASS) &&
Michael Chanc65a17f2013-01-06 12:51:07 +00009172 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9173 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +00009174 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009175 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
9176 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
9177 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009178 /* This value is determined during the probe time DMA
9179 * engine test, tg3_test_dma.
9180 */
9181 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
9182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009183
9184 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
9185 GRC_MODE_4X_NIC_SEND_RINGS |
9186 GRC_MODE_NO_TX_PHDR_CSUM |
9187 GRC_MODE_NO_RX_PHDR_CSUM);
9188 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07009189
9190 /* Pseudo-header checksum is done by hardware logic and not
9191 * the offload processers, so make the chip do the pseudo-
9192 * header checksums on receive. For transmit it is more
9193 * convenient to do the pseudo-header checksum in software
9194 * as Linux does that on transmit for us in all cases.
9195 */
9196 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009197
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00009198 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
9199 if (tp->rxptpctl)
9200 tw32(TG3_RX_PTP_CTL,
9201 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
9202
9203 if (tg3_flag(tp, PTP_CAPABLE))
9204 val |= GRC_MODE_TIME_SYNC_ENABLE;
9205
9206 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009207
9208 /* Setup the timer prescalar register. Clock is always 66Mhz. */
9209 val = tr32(GRC_MISC_CFG);
9210 val &= ~0xff;
9211 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
9212 tw32(GRC_MISC_CFG, val);
9213
9214 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00009215 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009216 /* Do nothing. */
9217 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
9218 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
9219 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
9220 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
9221 else
9222 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
9223 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
9224 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00009225 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009226 int fw_len;
9227
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08009228 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009229 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
9230 tw32(BUFMGR_MB_POOL_ADDR,
9231 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
9232 tw32(BUFMGR_MB_POOL_SIZE,
9233 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
9234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009235
Michael Chan0f893dc2005-07-25 12:30:38 -07009236 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009237 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9238 tp->bufmgr_config.mbuf_read_dma_low_water);
9239 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9240 tp->bufmgr_config.mbuf_mac_rx_low_water);
9241 tw32(BUFMGR_MB_HIGH_WATER,
9242 tp->bufmgr_config.mbuf_high_water);
9243 } else {
9244 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9245 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
9246 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9247 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
9248 tw32(BUFMGR_MB_HIGH_WATER,
9249 tp->bufmgr_config.mbuf_high_water_jumbo);
9250 }
9251 tw32(BUFMGR_DMA_LOW_WATER,
9252 tp->bufmgr_config.dma_low_water);
9253 tw32(BUFMGR_DMA_HIGH_WATER,
9254 tp->bufmgr_config.dma_high_water);
9255
Matt Carlsond309a462010-09-30 10:34:31 +00009256 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
9257 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
9258 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00009259 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
9260 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
9261 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
9262 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00009263 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009264 for (i = 0; i < 2000; i++) {
9265 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
9266 break;
9267 udelay(10);
9268 }
9269 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00009270 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009271 return -ENODEV;
9272 }
9273
Matt Carlsoneb07a942011-04-20 07:57:36 +00009274 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
9275 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07009276
Matt Carlsoneb07a942011-04-20 07:57:36 +00009277 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009278
9279 /* Initialize TG3_BDINFO's at:
9280 * RCVDBDI_STD_BD: standard eth size rx ring
9281 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
9282 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
9283 *
9284 * like so:
9285 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
9286 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
9287 * ring attribute flags
9288 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
9289 *
9290 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
9291 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
9292 *
9293 * The size of each ring is fixed in the firmware, but the location is
9294 * configurable.
9295 */
9296 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009297 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009298 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009299 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00009300 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00009301 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
9302 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009303
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009304 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00009305 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009306 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
9307 BDINFO_FLAGS_DISABLED);
9308
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009309 /* Program the jumbo buffer descriptor ring control
9310 * blocks on those devices that have them.
9311 */
Matt Carlsona0512942011-07-27 14:20:54 +00009312 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009313 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009314
Joe Perches63c3a662011-04-26 08:12:10 +00009315 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009316 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009317 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009318 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009319 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00009320 val = TG3_RX_JMB_RING_SIZE(tp) <<
9321 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009322 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00009323 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00009324 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +00009325 tg3_flag(tp, 57765_CLASS) ||
9326 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +00009327 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
9328 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009329 } else {
9330 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
9331 BDINFO_FLAGS_DISABLED);
9332 }
9333
Joe Perches63c3a662011-04-26 08:12:10 +00009334 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00009335 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009336 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
9337 val |= (TG3_RX_STD_DMA_SZ << 2);
9338 } else
Matt Carlson04380d42010-04-12 06:58:29 +00009339 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009340 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00009341 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009342
9343 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009344
Matt Carlson411da642009-11-13 13:03:46 +00009345 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e662009-11-13 13:03:49 +00009346 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009347
Joe Perches63c3a662011-04-26 08:12:10 +00009348 tpr->rx_jmb_prod_idx =
9349 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e662009-11-13 13:03:49 +00009350 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009351
Matt Carlson2d31eca2009-09-01 12:53:31 +00009352 tg3_rings_reset(tp);
9353
Linus Torvalds1da177e2005-04-16 15:20:36 -07009354 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07009355 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009356
9357 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00009358 tw32(MAC_RX_MTU_SIZE,
9359 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009360
9361 /* The slot time is changed by tg3_setup_phy if we
9362 * run at gigabit with half duplex.
9363 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00009364 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
9365 (6 << TX_LENGTHS_IPG_SHIFT) |
9366 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
9367
Michael Chanc65a17f2013-01-06 12:51:07 +00009368 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
9369 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00009370 val |= tr32(MAC_TX_LENGTHS) &
9371 (TX_LENGTHS_JMB_FRM_LEN_MSK |
9372 TX_LENGTHS_CNT_DWN_VAL_MSK);
9373
9374 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009375
9376 /* Receive rules. */
9377 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
9378 tw32(RCVLPC_CONFIG, 0x0181);
9379
9380 /* Calculate RDMAC_MODE setting early, we need it to determine
9381 * the RCVLPC_STATE_ENABLE mask.
9382 */
9383 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
9384 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
9385 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
9386 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
9387 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07009388
Matt Carlsondeabaac2010-11-24 08:31:50 +00009389 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00009390 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
9391
Matt Carlson57e69832008-05-25 23:48:31 -07009392 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009393 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
9394 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07009395 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
9396 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
9397 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
9398
Matt Carlsonc5908932011-03-09 16:58:25 +00009399 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
9400 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009401 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07009402 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009403 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
9404 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009405 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009406 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9407 }
9408 }
9409
Joe Perches63c3a662011-04-26 08:12:10 +00009410 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07009411 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9412
Joe Perches63c3a662011-04-26 08:12:10 +00009413 if (tg3_flag(tp, HW_TSO_1) ||
9414 tg3_flag(tp, HW_TSO_2) ||
9415 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08009416 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
9417
Matt Carlson108a6c12011-05-19 12:12:47 +00009418 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00009419 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08009420 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
9421 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009422
Michael Chanc65a17f2013-01-06 12:51:07 +00009423 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
9424 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00009425 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
9426
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009427 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
9428 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
9429 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
9430 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009431 tg3_flag(tp, 57765_PLUS)) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009432 u32 tgtreg;
9433
9434 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762)
9435 tgtreg = TG3_RDMA_RSRVCTRL_REG2;
9436 else
9437 tgtreg = TG3_RDMA_RSRVCTRL_REG;
9438
9439 val = tr32(tgtreg);
9440 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
9441 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00009442 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
9443 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
9444 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
9445 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
9446 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
9447 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00009448 }
Michael Chanc65a17f2013-01-06 12:51:07 +00009449 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009450 }
9451
Matt Carlsond78b59f2011-04-05 14:22:46 +00009452 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +00009453 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
9454 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762) {
9455 u32 tgtreg;
9456
9457 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762)
9458 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
9459 else
9460 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
9461
9462 val = tr32(tgtreg);
9463 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +00009464 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
9465 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
9466 }
9467
Linus Torvalds1da177e2005-04-16 15:20:36 -07009468 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00009469 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07009470 val = tr32(RCVLPC_STATS_ENABLE);
9471 val &= ~RCVLPC_STATSENAB_DACK_FIX;
9472 tw32(RCVLPC_STATS_ENABLE, val);
9473 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009474 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009475 val = tr32(RCVLPC_STATS_ENABLE);
9476 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
9477 tw32(RCVLPC_STATS_ENABLE, val);
9478 } else {
9479 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
9480 }
9481 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
9482 tw32(SNDDATAI_STATSENAB, 0xffffff);
9483 tw32(SNDDATAI_STATSCTRL,
9484 (SNDDATAI_SCTRL_ENABLE |
9485 SNDDATAI_SCTRL_FASTUPD));
9486
9487 /* Setup host coalescing engine. */
9488 tw32(HOSTCC_MODE, 0);
9489 for (i = 0; i < 2000; i++) {
9490 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
9491 break;
9492 udelay(10);
9493 }
9494
Michael Chand244c892005-07-05 14:42:33 -07009495 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009496
Joe Perches63c3a662011-04-26 08:12:10 +00009497 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009498 /* Status/statistics block address. See tg3_timer,
9499 * the tg3_periodic_fetch_stats call there, and
9500 * tg3_get_stats to see how this works for 5705/5750 chips.
9501 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009502 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9503 ((u64) tp->stats_mapping >> 32));
9504 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9505 ((u64) tp->stats_mapping & 0xffffffff));
9506 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009507
Linus Torvalds1da177e2005-04-16 15:20:36 -07009508 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009509
9510 /* Clear statistics and status block memory areas */
9511 for (i = NIC_SRAM_STATS_BLK;
9512 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
9513 i += sizeof(u32)) {
9514 tg3_write_mem(tp, i, 0);
9515 udelay(40);
9516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009517 }
9518
9519 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
9520
9521 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
9522 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009523 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009524 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
9525
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009526 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
9527 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07009528 /* reset to prevent losing 1st rx packet intermittently */
9529 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9530 udelay(10);
9531 }
9532
Matt Carlson3bda1252008-08-15 14:08:22 -07009533 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00009534 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
9535 MAC_MODE_FHDE_ENABLE;
9536 if (tg3_flag(tp, ENABLE_APE))
9537 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00009538 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009539 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009540 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
9541 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009542 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
9543 udelay(40);
9544
Michael Chan314fba32005-04-21 17:07:04 -07009545 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00009546 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07009547 * register to preserve the GPIO settings for LOMs. The GPIOs,
9548 * whether used as inputs or outputs, are set by boot code after
9549 * reset.
9550 */
Joe Perches63c3a662011-04-26 08:12:10 +00009551 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07009552 u32 gpio_mask;
9553
Michael Chan9d26e212006-12-07 00:21:14 -08009554 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
9555 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
9556 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07009557
9558 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
9559 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
9560 GRC_LCLCTRL_GPIO_OUTPUT3;
9561
Michael Chanaf36e6b2006-03-23 01:28:06 -08009562 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
9563 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
9564
Gary Zambranoaaf84462007-05-05 11:51:45 -07009565 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07009566 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
9567
9568 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00009569 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08009570 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
9571 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07009572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009573 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9574 udelay(100);
9575
Matt Carlsonc3b50032012-01-17 15:27:23 +00009576 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009577 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +00009578 val |= MSGINT_MODE_ENABLE;
9579 if (tp->irq_cnt > 1)
9580 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009581 if (!tg3_flag(tp, 1SHOT_MSI))
9582 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009583 tw32(MSGINT_MODE, val);
9584 }
9585
Joe Perches63c3a662011-04-26 08:12:10 +00009586 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009587 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
9588 udelay(40);
9589 }
9590
9591 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
9592 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
9593 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
9594 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
9595 WDMAC_MODE_LNGREAD_ENAB);
9596
Matt Carlsonc5908932011-03-09 16:58:25 +00009597 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
9598 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009599 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009600 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
9601 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
9602 /* nothing */
9603 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009604 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009605 val |= WDMAC_MODE_RX_ACCEL;
9606 }
9607 }
9608
Michael Chand9ab5ad12006-03-20 22:27:35 -08009609 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00009610 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07009611 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08009612
Matt Carlson788a0352009-11-02 14:26:03 +00009613 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
9614 val |= WDMAC_MODE_BURST_ALL_DATA;
9615
Linus Torvalds1da177e2005-04-16 15:20:36 -07009616 tw32_f(WDMAC_MODE, val);
9617 udelay(40);
9618
Joe Perches63c3a662011-04-26 08:12:10 +00009619 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07009620 u16 pcix_cmd;
9621
9622 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9623 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009624 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07009625 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
9626 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009627 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07009628 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
9629 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009630 }
Matt Carlson9974a352007-10-07 23:27:28 -07009631 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9632 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009633 }
9634
9635 tw32_f(RDMAC_MODE, rdmac_mode);
9636 udelay(40);
9637
Michael Chan091f0ea2012-07-29 19:15:43 +00009638 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
9639 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
9640 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
9641 break;
9642 }
9643 if (i < TG3_NUM_RDMA_CHANNELS) {
9644 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9645 val |= TG3_LSO_RD_DMA_TX_LENGTH_WA;
9646 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
9647 tg3_flag_set(tp, 5719_RDMA_BUG);
9648 }
9649 }
9650
Linus Torvalds1da177e2005-04-16 15:20:36 -07009651 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009652 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009653 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07009654
9655 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
9656 tw32(SNDDATAC_MODE,
9657 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
9658 else
9659 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
9660
Linus Torvalds1da177e2005-04-16 15:20:36 -07009661 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
9662 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009663 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00009664 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009665 val |= RCVDBDI_MODE_LRG_RING_SZ;
9666 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009667 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009668 if (tg3_flag(tp, HW_TSO_1) ||
9669 tg3_flag(tp, HW_TSO_2) ||
9670 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009671 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009672 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009673 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009674 val |= SNDBDI_MODE_MULTI_TXQ_EN;
9675 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009676 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
9677
9678 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9679 err = tg3_load_5701_a0_firmware_fix(tp);
9680 if (err)
9681 return err;
9682 }
9683
Joe Perches63c3a662011-04-26 08:12:10 +00009684 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009685 err = tg3_load_tso_firmware(tp);
9686 if (err)
9687 return err;
9688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009689
9690 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009691
Joe Perches63c3a662011-04-26 08:12:10 +00009692 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00009693 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
9694 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009695
Michael Chanc65a17f2013-01-06 12:51:07 +00009696 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
9697 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00009698 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
9699 tp->tx_mode &= ~val;
9700 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
9701 }
9702
Linus Torvalds1da177e2005-04-16 15:20:36 -07009703 tw32_f(MAC_TX_MODE, tp->tx_mode);
9704 udelay(100);
9705
Joe Perches63c3a662011-04-26 08:12:10 +00009706 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009707 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009708
9709 /* Setup the "secret" hash key. */
9710 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
9711 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
9712 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
9713 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
9714 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
9715 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
9716 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
9717 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
9718 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
9719 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
9720 }
9721
Linus Torvalds1da177e2005-04-16 15:20:36 -07009722 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009723 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08009724 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
9725
Joe Perches63c3a662011-04-26 08:12:10 +00009726 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009727 tp->rx_mode |= RX_MODE_RSS_ENABLE |
9728 RX_MODE_RSS_ITBL_HASH_BITS_7 |
9729 RX_MODE_RSS_IPV6_HASH_EN |
9730 RX_MODE_RSS_TCP_IPV6_HASH_EN |
9731 RX_MODE_RSS_IPV4_HASH_EN |
9732 RX_MODE_RSS_TCP_IPV4_HASH_EN;
9733
Linus Torvalds1da177e2005-04-16 15:20:36 -07009734 tw32_f(MAC_RX_MODE, tp->rx_mode);
9735 udelay(10);
9736
Linus Torvalds1da177e2005-04-16 15:20:36 -07009737 tw32(MAC_LED_CTRL, tp->led_ctrl);
9738
9739 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009740 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009741 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9742 udelay(10);
9743 }
9744 tw32_f(MAC_RX_MODE, tp->rx_mode);
9745 udelay(10);
9746
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009747 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009748 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009749 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009750 /* Set drive transmission level to 1.2V */
9751 /* only if the signal pre-emphasis bit is not set */
9752 val = tr32(MAC_SERDES_CFG);
9753 val &= 0xfffff000;
9754 val |= 0x880;
9755 tw32(MAC_SERDES_CFG, val);
9756 }
9757 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
9758 tw32(MAC_SERDES_CFG, 0x616000);
9759 }
9760
9761 /* Prevent chip from dropping frames when flow control
9762 * is enabled.
9763 */
Matt Carlson55086ad2011-12-14 11:09:59 +00009764 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00009765 val = 1;
9766 else
9767 val = 2;
9768 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009769
9770 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009771 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009772 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009773 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009774 }
9775
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009776 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009777 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009778 u32 tmp;
9779
9780 tmp = tr32(SERDES_RX_CTRL);
9781 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9782 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9783 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9784 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9785 }
9786
Joe Perches63c3a662011-04-26 08:12:10 +00009787 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +00009788 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +00009789 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009790
Matt Carlsondd477002008-05-25 23:45:58 -07009791 err = tg3_setup_phy(tp, 0);
9792 if (err)
9793 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009794
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009795 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9796 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009797 u32 tmp;
9798
9799 /* Clear CRC stats. */
9800 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9801 tg3_writephy(tp, MII_TG3_TEST1,
9802 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009803 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009805 }
9806 }
9807
9808 __tg3_set_rx_mode(tp->dev);
9809
9810 /* Initialize receive rules. */
9811 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9812 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9813 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9814 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9815
Joe Perches63c3a662011-04-26 08:12:10 +00009816 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009817 limit = 8;
9818 else
9819 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009820 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009821 limit -= 4;
9822 switch (limit) {
9823 case 16:
9824 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9825 case 15:
9826 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9827 case 14:
9828 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9829 case 13:
9830 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9831 case 12:
9832 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9833 case 11:
9834 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9835 case 10:
9836 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9837 case 9:
9838 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9839 case 8:
9840 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9841 case 7:
9842 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9843 case 6:
9844 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9845 case 5:
9846 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9847 case 4:
9848 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9849 case 3:
9850 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9851 case 2:
9852 case 1:
9853
9854 default:
9855 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009857
Joe Perches63c3a662011-04-26 08:12:10 +00009858 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009859 /* Write our heartbeat update interval to APE. */
9860 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9861 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009862
Linus Torvalds1da177e2005-04-16 15:20:36 -07009863 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9864
Linus Torvalds1da177e2005-04-16 15:20:36 -07009865 return 0;
9866}
9867
9868/* Called at device open time to get the chip ready for
9869 * packet processing. Invoked with tp->lock held.
9870 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009871static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009872{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009873 tg3_switch_clocks(tp);
9874
9875 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9876
Matt Carlson2f751b62008-08-04 23:17:34 -07009877 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009878}
9879
Michael Chanaed93e02012-07-16 16:24:02 +00009880static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
9881{
9882 int i;
9883
9884 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
9885 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
9886
9887 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
9888 off += len;
9889
9890 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
9891 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
9892 memset(ocir, 0, TG3_OCIR_LEN);
9893 }
9894}
9895
9896/* sysfs attributes for hwmon */
9897static ssize_t tg3_show_temp(struct device *dev,
9898 struct device_attribute *devattr, char *buf)
9899{
9900 struct pci_dev *pdev = to_pci_dev(dev);
9901 struct net_device *netdev = pci_get_drvdata(pdev);
9902 struct tg3 *tp = netdev_priv(netdev);
9903 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
9904 u32 temperature;
9905
9906 spin_lock_bh(&tp->lock);
9907 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
9908 sizeof(temperature));
9909 spin_unlock_bh(&tp->lock);
9910 return sprintf(buf, "%u\n", temperature);
9911}
9912
9913
9914static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
9915 TG3_TEMP_SENSOR_OFFSET);
9916static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
9917 TG3_TEMP_CAUTION_OFFSET);
9918static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
9919 TG3_TEMP_MAX_OFFSET);
9920
9921static struct attribute *tg3_attributes[] = {
9922 &sensor_dev_attr_temp1_input.dev_attr.attr,
9923 &sensor_dev_attr_temp1_crit.dev_attr.attr,
9924 &sensor_dev_attr_temp1_max.dev_attr.attr,
9925 NULL
9926};
9927
9928static const struct attribute_group tg3_group = {
9929 .attrs = tg3_attributes,
9930};
9931
Michael Chanaed93e02012-07-16 16:24:02 +00009932static void tg3_hwmon_close(struct tg3 *tp)
9933{
Michael Chanaed93e02012-07-16 16:24:02 +00009934 if (tp->hwmon_dev) {
9935 hwmon_device_unregister(tp->hwmon_dev);
9936 tp->hwmon_dev = NULL;
9937 sysfs_remove_group(&tp->pdev->dev.kobj, &tg3_group);
9938 }
Michael Chanaed93e02012-07-16 16:24:02 +00009939}
9940
9941static void tg3_hwmon_open(struct tg3 *tp)
9942{
Michael Chanaed93e02012-07-16 16:24:02 +00009943 int i, err;
9944 u32 size = 0;
9945 struct pci_dev *pdev = tp->pdev;
9946 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
9947
9948 tg3_sd_scan_scratchpad(tp, ocirs);
9949
9950 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
9951 if (!ocirs[i].src_data_length)
9952 continue;
9953
9954 size += ocirs[i].src_hdr_length;
9955 size += ocirs[i].src_data_length;
9956 }
9957
9958 if (!size)
9959 return;
9960
9961 /* Register hwmon sysfs hooks */
9962 err = sysfs_create_group(&pdev->dev.kobj, &tg3_group);
9963 if (err) {
9964 dev_err(&pdev->dev, "Cannot create sysfs group, aborting\n");
9965 return;
9966 }
9967
9968 tp->hwmon_dev = hwmon_device_register(&pdev->dev);
9969 if (IS_ERR(tp->hwmon_dev)) {
9970 tp->hwmon_dev = NULL;
9971 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
9972 sysfs_remove_group(&pdev->dev.kobj, &tg3_group);
9973 }
Michael Chanaed93e02012-07-16 16:24:02 +00009974}
9975
9976
Linus Torvalds1da177e2005-04-16 15:20:36 -07009977#define TG3_STAT_ADD32(PSTAT, REG) \
9978do { u32 __val = tr32(REG); \
9979 (PSTAT)->low += __val; \
9980 if ((PSTAT)->low < __val) \
9981 (PSTAT)->high += 1; \
9982} while (0)
9983
9984static void tg3_periodic_fetch_stats(struct tg3 *tp)
9985{
9986 struct tg3_hw_stats *sp = tp->hw_stats;
9987
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009988 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009989 return;
9990
9991 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9992 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9993 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9994 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9995 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9996 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9997 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9998 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9999 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10000 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10001 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10002 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10003 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Michael Chan091f0ea2012-07-29 19:15:43 +000010004 if (unlikely(tg3_flag(tp, 5719_RDMA_BUG) &&
10005 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10006 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10007 u32 val;
10008
10009 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
10010 val &= ~TG3_LSO_RD_DMA_TX_LENGTH_WA;
10011 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
10012 tg3_flag_clear(tp, 5719_RDMA_BUG);
10013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010014
10015 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10016 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10017 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10018 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10019 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10020 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10021 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10022 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10023 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10024 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10025 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10026 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10027 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10028 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010029
10030 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +000010031 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
10032 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
10033 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010034 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10035 } else {
10036 u32 val = tr32(HOSTCC_FLOW_ATTN);
10037 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10038 if (val) {
10039 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10040 sp->rx_discards.low += val;
10041 if (sp->rx_discards.low < val)
10042 sp->rx_discards.high += 1;
10043 }
10044 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10045 }
Michael Chan463d3052006-05-22 16:36:27 -070010046 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010047}
10048
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010049static void tg3_chk_missed_msi(struct tg3 *tp)
10050{
10051 u32 i;
10052
10053 for (i = 0; i < tp->irq_cnt; i++) {
10054 struct tg3_napi *tnapi = &tp->napi[i];
10055
10056 if (tg3_has_work(tnapi)) {
10057 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10058 tnapi->last_tx_cons == tnapi->tx_cons) {
10059 if (tnapi->chk_msi_cnt < 1) {
10060 tnapi->chk_msi_cnt++;
10061 return;
10062 }
Matt Carlson7f230732011-08-31 11:44:48 +000010063 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010064 }
10065 }
10066 tnapi->chk_msi_cnt = 0;
10067 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10068 tnapi->last_tx_cons = tnapi->tx_cons;
10069 }
10070}
10071
Linus Torvalds1da177e2005-04-16 15:20:36 -070010072static void tg3_timer(unsigned long __opaque)
10073{
10074 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010075
Matt Carlson5b190622011-11-04 09:15:04 +000010076 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -080010077 goto restart_timer;
10078
David S. Millerf47c11e2005-06-24 20:18:35 -070010079 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010080
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010081 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010082 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010083 tg3_chk_missed_msi(tp);
10084
Joe Perches63c3a662011-04-26 08:12:10 +000010085 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010086 /* All of this garbage is because when using non-tagged
10087 * IRQ status the mailbox/status_block protocol the chip
10088 * uses with the cpu is race prone.
10089 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010090 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010091 tw32(GRC_LOCAL_CTRL,
10092 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10093 } else {
10094 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010095 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010097
David S. Millerfac9b832005-05-18 22:46:34 -070010098 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010099 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010100 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010101 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010103 }
10104
Linus Torvalds1da177e2005-04-16 15:20:36 -070010105 /* This part only runs once per second. */
10106 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010107 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010108 tg3_periodic_fetch_stats(tp);
10109
Matt Carlsonb0c59432011-05-19 12:12:48 +000010110 if (tp->setlpicnt && !--tp->setlpicnt)
10111 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010112
Joe Perches63c3a662011-04-26 08:12:10 +000010113 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010114 u32 mac_stat;
10115 int phy_event;
10116
10117 mac_stat = tr32(MAC_STATUS);
10118
10119 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010120 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010121 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
10122 phy_event = 1;
10123 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
10124 phy_event = 1;
10125
10126 if (phy_event)
10127 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000010128 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010129 u32 mac_stat = tr32(MAC_STATUS);
10130 int need_setup = 0;
10131
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010132 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010133 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
10134 need_setup = 1;
10135 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010136 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010137 (mac_stat & (MAC_STATUS_PCS_SYNCED |
10138 MAC_STATUS_SIGNAL_DET))) {
10139 need_setup = 1;
10140 }
10141 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070010142 if (!tp->serdes_counter) {
10143 tw32_f(MAC_MODE,
10144 (tp->mac_mode &
10145 ~MAC_MODE_PORT_MODE_MASK));
10146 udelay(40);
10147 tw32_f(MAC_MODE, tp->mac_mode);
10148 udelay(40);
10149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010150 tg3_setup_phy(tp, 0);
10151 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010152 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010153 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070010154 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +000010155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010156
10157 tp->timer_counter = tp->timer_multiplier;
10158 }
10159
Michael Chan130b8e42006-09-27 16:00:40 -070010160 /* Heartbeat is only sent once every 2 seconds.
10161 *
10162 * The heartbeat is to tell the ASF firmware that the host
10163 * driver is still alive. In the event that the OS crashes,
10164 * ASF needs to reset the hardware to free up the FIFO space
10165 * that may be filled with rx packets destined for the host.
10166 * If the FIFO is full, ASF will no longer function properly.
10167 *
10168 * Unintended resets have been reported on real time kernels
10169 * where the timer doesn't run on time. Netpoll will also have
10170 * same problem.
10171 *
10172 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
10173 * to check the ring condition when the heartbeat is expiring
10174 * before doing the reset. This will prevent most unintended
10175 * resets.
10176 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010177 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010178 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070010179 tg3_wait_for_event_ack(tp);
10180
Michael Chanbbadf502006-04-06 21:46:34 -070010181 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070010182 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070010183 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010184 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
10185 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070010186
10187 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010188 }
10189 tp->asf_counter = tp->asf_multiplier;
10190 }
10191
David S. Millerf47c11e2005-06-24 20:18:35 -070010192 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010193
Michael Chanf475f162006-03-27 23:20:14 -080010194restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070010195 tp->timer.expires = jiffies + tp->timer_offset;
10196 add_timer(&tp->timer);
10197}
10198
Bill Pemberton229b1ad2012-12-03 09:22:59 -050010199static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000010200{
10201 if (tg3_flag(tp, TAGGED_STATUS) &&
10202 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
10203 !tg3_flag(tp, 57765_CLASS))
10204 tp->timer_offset = HZ;
10205 else
10206 tp->timer_offset = HZ / 10;
10207
10208 BUG_ON(tp->timer_offset > HZ);
10209
10210 tp->timer_multiplier = (HZ / tp->timer_offset);
10211 tp->asf_multiplier = (HZ / tp->timer_offset) *
10212 TG3_FW_UPDATE_FREQ_SEC;
10213
10214 init_timer(&tp->timer);
10215 tp->timer.data = (unsigned long) tp;
10216 tp->timer.function = tg3_timer;
10217}
10218
10219static void tg3_timer_start(struct tg3 *tp)
10220{
10221 tp->asf_counter = tp->asf_multiplier;
10222 tp->timer_counter = tp->timer_multiplier;
10223
10224 tp->timer.expires = jiffies + tp->timer_offset;
10225 add_timer(&tp->timer);
10226}
10227
10228static void tg3_timer_stop(struct tg3 *tp)
10229{
10230 del_timer_sync(&tp->timer);
10231}
10232
10233/* Restart hardware after configuration changes, self-test, etc.
10234 * Invoked with tp->lock held.
10235 */
10236static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
10237 __releases(tp->lock)
10238 __acquires(tp->lock)
10239{
10240 int err;
10241
10242 err = tg3_init_hw(tp, reset_phy);
10243 if (err) {
10244 netdev_err(tp->dev,
10245 "Failed to re-initialize device, aborting\n");
10246 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10247 tg3_full_unlock(tp);
10248 tg3_timer_stop(tp);
10249 tp->irq_sync = 0;
10250 tg3_napi_enable(tp);
10251 dev_close(tp->dev);
10252 tg3_full_lock(tp, 0);
10253 }
10254 return err;
10255}
10256
10257static void tg3_reset_task(struct work_struct *work)
10258{
10259 struct tg3 *tp = container_of(work, struct tg3, reset_task);
10260 int err;
10261
10262 tg3_full_lock(tp, 0);
10263
10264 if (!netif_running(tp->dev)) {
10265 tg3_flag_clear(tp, RESET_TASK_PENDING);
10266 tg3_full_unlock(tp);
10267 return;
10268 }
10269
10270 tg3_full_unlock(tp);
10271
10272 tg3_phy_stop(tp);
10273
10274 tg3_netif_stop(tp);
10275
10276 tg3_full_lock(tp, 1);
10277
10278 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
10279 tp->write32_tx_mbox = tg3_write32_tx_mbox;
10280 tp->write32_rx_mbox = tg3_write_flush_reg32;
10281 tg3_flag_set(tp, MBOX_WRITE_REORDER);
10282 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
10283 }
10284
10285 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
10286 err = tg3_init_hw(tp, 1);
10287 if (err)
10288 goto out;
10289
10290 tg3_netif_start(tp);
10291
10292out:
10293 tg3_full_unlock(tp);
10294
10295 if (!err)
10296 tg3_phy_start(tp);
10297
10298 tg3_flag_clear(tp, RESET_TASK_PENDING);
10299}
10300
Matt Carlson4f125f42009-09-01 12:55:02 +000010301static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080010302{
David Howells7d12e782006-10-05 14:55:46 +010010303 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010304 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000010305 char *name;
10306 struct tg3_napi *tnapi = &tp->napi[irq_num];
10307
10308 if (tp->irq_cnt == 1)
10309 name = tp->dev->name;
10310 else {
10311 name = &tnapi->irq_lbl[0];
10312 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
10313 name[IFNAMSIZ-1] = 0;
10314 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010315
Joe Perches63c3a662011-04-26 08:12:10 +000010316 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080010317 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000010318 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010319 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010320 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010321 } else {
10322 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000010323 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010324 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010325 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010326 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010327
10328 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010329}
10330
Michael Chan79381092005-04-21 17:13:59 -070010331static int tg3_test_interrupt(struct tg3 *tp)
10332{
Matt Carlson09943a12009-08-28 14:01:57 +000010333 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070010334 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070010335 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010336 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070010337
Michael Chand4bc3922005-05-29 14:59:20 -070010338 if (!netif_running(dev))
10339 return -ENODEV;
10340
Michael Chan79381092005-04-21 17:13:59 -070010341 tg3_disable_ints(tp);
10342
Matt Carlson4f125f42009-09-01 12:55:02 +000010343 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010344
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010345 /*
10346 * Turn off MSI one shot mode. Otherwise this test has no
10347 * observable way to know whether the interrupt was delivered.
10348 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010349 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010350 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
10351 tw32(MSGINT_MODE, val);
10352 }
10353
Matt Carlson4f125f42009-09-01 12:55:02 +000010354 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000010355 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010356 if (err)
10357 return err;
10358
Matt Carlson898a56f2009-08-28 14:02:40 +000010359 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070010360 tg3_enable_ints(tp);
10361
10362 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010363 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070010364
10365 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070010366 u32 int_mbox, misc_host_ctrl;
10367
Matt Carlson898a56f2009-08-28 14:02:40 +000010368 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070010369 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
10370
10371 if ((int_mbox != 0) ||
10372 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
10373 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070010374 break;
Michael Chanb16250e2006-09-27 16:10:14 -070010375 }
10376
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010377 if (tg3_flag(tp, 57765_PLUS) &&
10378 tnapi->hw_status->status_tag != tnapi->last_tag)
10379 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
10380
Michael Chan79381092005-04-21 17:13:59 -070010381 msleep(10);
10382 }
10383
10384 tg3_disable_ints(tp);
10385
Matt Carlson4f125f42009-09-01 12:55:02 +000010386 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010387
Matt Carlson4f125f42009-09-01 12:55:02 +000010388 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010389
10390 if (err)
10391 return err;
10392
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010393 if (intr_ok) {
10394 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000010395 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010396 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
10397 tw32(MSGINT_MODE, val);
10398 }
Michael Chan79381092005-04-21 17:13:59 -070010399 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010400 }
Michael Chan79381092005-04-21 17:13:59 -070010401
10402 return -EIO;
10403}
10404
10405/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
10406 * successfully restored
10407 */
10408static int tg3_test_msi(struct tg3 *tp)
10409{
Michael Chan79381092005-04-21 17:13:59 -070010410 int err;
10411 u16 pci_cmd;
10412
Joe Perches63c3a662011-04-26 08:12:10 +000010413 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070010414 return 0;
10415
10416 /* Turn off SERR reporting in case MSI terminates with Master
10417 * Abort.
10418 */
10419 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
10420 pci_write_config_word(tp->pdev, PCI_COMMAND,
10421 pci_cmd & ~PCI_COMMAND_SERR);
10422
10423 err = tg3_test_interrupt(tp);
10424
10425 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
10426
10427 if (!err)
10428 return 0;
10429
10430 /* other failures */
10431 if (err != -EIO)
10432 return err;
10433
10434 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010435 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
10436 "to INTx mode. Please report this failure to the PCI "
10437 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070010438
Matt Carlson4f125f42009-09-01 12:55:02 +000010439 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000010440
Michael Chan79381092005-04-21 17:13:59 -070010441 pci_disable_msi(tp->pdev);
10442
Joe Perches63c3a662011-04-26 08:12:10 +000010443 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000010444 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070010445
Matt Carlson4f125f42009-09-01 12:55:02 +000010446 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010447 if (err)
10448 return err;
10449
10450 /* Need to reset the chip because the MSI cycle may have terminated
10451 * with Master Abort.
10452 */
David S. Millerf47c11e2005-06-24 20:18:35 -070010453 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010454
Michael Chan944d9802005-05-29 14:57:48 -070010455 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -070010456 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010457
David S. Millerf47c11e2005-06-24 20:18:35 -070010458 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010459
10460 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000010461 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070010462
10463 return err;
10464}
10465
Matt Carlson9e9fd122009-01-19 16:57:45 -080010466static int tg3_request_firmware(struct tg3 *tp)
10467{
10468 const __be32 *fw_data;
10469
10470 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010471 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
10472 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010473 return -ENOENT;
10474 }
10475
10476 fw_data = (void *)tp->fw->data;
10477
10478 /* Firmware blob starts with version numbers, followed by
10479 * start address and _full_ length including BSS sections
10480 * (which must be longer than the actual data, of course
10481 */
10482
10483 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
10484 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010485 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
10486 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010487 release_firmware(tp->fw);
10488 tp->fw = NULL;
10489 return -EINVAL;
10490 }
10491
10492 /* We no longer need firmware; we have it. */
10493 tp->fw_needed = NULL;
10494 return 0;
10495}
10496
Michael Chan91024262012-09-28 07:12:38 +000010497static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000010498{
Michael Chan91024262012-09-28 07:12:38 +000010499 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000010500
Michael Chan91024262012-09-28 07:12:38 +000010501 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000010502 /* We want as many rx rings enabled as there are cpus.
10503 * In multiqueue MSI-X mode, the first MSI-X vector
10504 * only deals with link interrupts, etc, so we add
10505 * one to the number of vectors we are requesting.
10506 */
Michael Chan91024262012-09-28 07:12:38 +000010507 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010508 }
Matt Carlson679563f2009-09-01 12:55:46 +000010509
Michael Chan91024262012-09-28 07:12:38 +000010510 return irq_cnt;
10511}
10512
10513static bool tg3_enable_msix(struct tg3 *tp)
10514{
10515 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070010516 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000010517
Michael Chan09681692012-09-28 07:12:42 +000010518 tp->txq_cnt = tp->txq_req;
10519 tp->rxq_cnt = tp->rxq_req;
10520 if (!tp->rxq_cnt)
10521 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000010522 if (tp->rxq_cnt > tp->rxq_max)
10523 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000010524
10525 /* Disable multiple TX rings by default. Simple round-robin hardware
10526 * scheduling of the TX rings can cause starvation of rings with
10527 * small packets when other rings have TSO or jumbo packets.
10528 */
10529 if (!tp->txq_req)
10530 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000010531
10532 tp->irq_cnt = tg3_irq_count(tp);
10533
Matt Carlson679563f2009-09-01 12:55:46 +000010534 for (i = 0; i < tp->irq_max; i++) {
10535 msix_ent[i].entry = i;
10536 msix_ent[i].vector = 0;
10537 }
10538
10539 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000010540 if (rc < 0) {
10541 return false;
10542 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +000010543 if (pci_enable_msix(tp->pdev, msix_ent, rc))
10544 return false;
Joe Perches05dbe002010-02-17 19:44:19 +000010545 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
10546 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000010547 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000010548 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000010549 if (tp->txq_cnt)
10550 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000010551 }
10552
10553 for (i = 0; i < tp->irq_max; i++)
10554 tp->napi[i].irq_vec = msix_ent[i].vector;
10555
Michael Chan49a359e2012-09-28 07:12:37 +000010556 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010557 pci_disable_msix(tp->pdev);
10558 return false;
10559 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000010560
Michael Chan91024262012-09-28 07:12:38 +000010561 if (tp->irq_cnt == 1)
10562 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000010563
Michael Chan91024262012-09-28 07:12:38 +000010564 tg3_flag_set(tp, ENABLE_RSS);
10565
10566 if (tp->txq_cnt > 1)
10567 tg3_flag_set(tp, ENABLE_TSS);
10568
10569 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000010570
Matt Carlson679563f2009-09-01 12:55:46 +000010571 return true;
10572}
10573
Matt Carlson07b01732009-08-28 14:01:15 +000010574static void tg3_ints_init(struct tg3 *tp)
10575{
Joe Perches63c3a662011-04-26 08:12:10 +000010576 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
10577 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000010578 /* All MSI supporting chips should support tagged
10579 * status. Assert that this is the case.
10580 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010581 netdev_warn(tp->dev,
10582 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000010583 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000010584 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010585
Joe Perches63c3a662011-04-26 08:12:10 +000010586 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
10587 tg3_flag_set(tp, USING_MSIX);
10588 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
10589 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000010590
Joe Perches63c3a662011-04-26 08:12:10 +000010591 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010592 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000010593 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010594 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010595 if (!tg3_flag(tp, 1SHOT_MSI))
10596 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000010597 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
10598 }
10599defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000010600 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010601 tp->irq_cnt = 1;
10602 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000010603 }
10604
10605 if (tp->irq_cnt == 1) {
10606 tp->txq_cnt = 1;
10607 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010608 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070010609 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000010610 }
Matt Carlson07b01732009-08-28 14:01:15 +000010611}
10612
10613static void tg3_ints_fini(struct tg3 *tp)
10614{
Joe Perches63c3a662011-04-26 08:12:10 +000010615 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000010616 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010617 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000010618 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010619 tg3_flag_clear(tp, USING_MSI);
10620 tg3_flag_clear(tp, USING_MSIX);
10621 tg3_flag_clear(tp, ENABLE_RSS);
10622 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000010623}
10624
Matt Carlsonbe947302012-12-03 19:36:57 +000010625static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
10626 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010627{
Michael Chand8f4cd32012-09-28 07:12:40 +000010628 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000010629 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010630
Matt Carlson679563f2009-09-01 12:55:46 +000010631 /*
10632 * Setup interrupts first so we know how
10633 * many NAPI resources to allocate
10634 */
10635 tg3_ints_init(tp);
10636
Matt Carlson90415472011-12-16 13:33:23 +000010637 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010638
Linus Torvalds1da177e2005-04-16 15:20:36 -070010639 /* The placement of this call is tied
10640 * to the setup and use of Host TX descriptors.
10641 */
10642 err = tg3_alloc_consistent(tp);
10643 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010644 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010645
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010646 tg3_napi_init(tp);
10647
Matt Carlsonfed97812009-09-01 13:10:19 +000010648 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070010649
Matt Carlson4f125f42009-09-01 12:55:02 +000010650 for (i = 0; i < tp->irq_cnt; i++) {
10651 struct tg3_napi *tnapi = &tp->napi[i];
10652 err = tg3_request_irq(tp, i);
10653 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000010654 for (i--; i >= 0; i--) {
10655 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000010656 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000010657 }
10658 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000010659 }
10660 }
Matt Carlson07b01732009-08-28 14:01:15 +000010661
David S. Millerf47c11e2005-06-24 20:18:35 -070010662 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010663
Michael Chand8f4cd32012-09-28 07:12:40 +000010664 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010665 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070010666 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010667 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010668 }
10669
David S. Millerf47c11e2005-06-24 20:18:35 -070010670 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010671
Matt Carlson07b01732009-08-28 14:01:15 +000010672 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010673 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010674
Michael Chand8f4cd32012-09-28 07:12:40 +000010675 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070010676 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070010677
Michael Chan79381092005-04-21 17:13:59 -070010678 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010679 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070010680 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070010681 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070010682 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010683
Matt Carlson679563f2009-09-01 12:55:46 +000010684 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070010685 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010686
Joe Perches63c3a662011-04-26 08:12:10 +000010687 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010688 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010689
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010690 tw32(PCIE_TRANSACTION_CFG,
10691 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010692 }
Michael Chan79381092005-04-21 17:13:59 -070010693 }
10694
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010695 tg3_phy_start(tp);
10696
Michael Chanaed93e02012-07-16 16:24:02 +000010697 tg3_hwmon_open(tp);
10698
David S. Millerf47c11e2005-06-24 20:18:35 -070010699 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010700
Matt Carlson21f76382012-02-22 12:35:21 +000010701 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010702 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010703 tg3_enable_ints(tp);
10704
Matt Carlsonbe947302012-12-03 19:36:57 +000010705 if (init)
10706 tg3_ptp_init(tp);
10707 else
10708 tg3_ptp_resume(tp);
10709
10710
David S. Millerf47c11e2005-06-24 20:18:35 -070010711 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010712
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010713 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010714
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000010715 /*
10716 * Reset loopback feature if it was turned on while the device was down
10717 * make sure that it's installed properly now.
10718 */
10719 if (dev->features & NETIF_F_LOOPBACK)
10720 tg3_set_loopback(dev, dev->features);
10721
Linus Torvalds1da177e2005-04-16 15:20:36 -070010722 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000010723
Matt Carlson679563f2009-09-01 12:55:46 +000010724err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000010725 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10726 struct tg3_napi *tnapi = &tp->napi[i];
10727 free_irq(tnapi->irq_vec, tnapi);
10728 }
Matt Carlson07b01732009-08-28 14:01:15 +000010729
Matt Carlson679563f2009-09-01 12:55:46 +000010730err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000010731 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010732 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000010733 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000010734
10735err_out1:
10736 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000010737
Matt Carlson07b01732009-08-28 14:01:15 +000010738 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010739}
10740
Michael Chan65138592012-09-28 07:12:41 +000010741static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010742{
Matt Carlson4f125f42009-09-01 12:55:02 +000010743 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010744
Matt Carlsondb219972011-11-04 09:15:03 +000010745 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000010746 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010747
Matt Carlson21f76382012-02-22 12:35:21 +000010748 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010749
Michael Chanaed93e02012-07-16 16:24:02 +000010750 tg3_hwmon_close(tp);
10751
Matt Carlson24bb4fb2009-10-05 17:55:29 +000010752 tg3_phy_stop(tp);
10753
David S. Millerf47c11e2005-06-24 20:18:35 -070010754 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010755
10756 tg3_disable_ints(tp);
10757
Michael Chan944d9802005-05-29 14:57:48 -070010758 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010759 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010760 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010761
David S. Millerf47c11e2005-06-24 20:18:35 -070010762 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010763
Matt Carlson4f125f42009-09-01 12:55:02 +000010764 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10765 struct tg3_napi *tnapi = &tp->napi[i];
10766 free_irq(tnapi->irq_vec, tnapi);
10767 }
Matt Carlson07b01732009-08-28 14:01:15 +000010768
10769 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010770
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010771 tg3_napi_fini(tp);
10772
Linus Torvalds1da177e2005-04-16 15:20:36 -070010773 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000010774}
10775
Michael Chand8f4cd32012-09-28 07:12:40 +000010776static int tg3_open(struct net_device *dev)
10777{
10778 struct tg3 *tp = netdev_priv(dev);
10779 int err;
10780
10781 if (tp->fw_needed) {
10782 err = tg3_request_firmware(tp);
10783 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
10784 if (err)
10785 return err;
10786 } else if (err) {
10787 netdev_warn(tp->dev, "TSO capability disabled\n");
10788 tg3_flag_clear(tp, TSO_CAPABLE);
10789 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
10790 netdev_notice(tp->dev, "TSO capability restored\n");
10791 tg3_flag_set(tp, TSO_CAPABLE);
10792 }
10793 }
10794
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010795 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000010796
10797 err = tg3_power_up(tp);
10798 if (err)
10799 return err;
10800
10801 tg3_full_lock(tp, 0);
10802
10803 tg3_disable_ints(tp);
10804 tg3_flag_clear(tp, INIT_COMPLETE);
10805
10806 tg3_full_unlock(tp);
10807
Matt Carlsonbe947302012-12-03 19:36:57 +000010808 err = tg3_start(tp, true, true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000010809 if (err) {
10810 tg3_frob_aux_power(tp, false);
10811 pci_set_power_state(tp->pdev, PCI_D3hot);
10812 }
Matt Carlsonbe947302012-12-03 19:36:57 +000010813
Matt Carlson7d41e492012-12-03 19:36:58 +000010814 if (tg3_flag(tp, PTP_CAPABLE)) {
10815 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
10816 &tp->pdev->dev);
10817 if (IS_ERR(tp->ptp_clock))
10818 tp->ptp_clock = NULL;
10819 }
10820
Linus Torvalds1da177e2005-04-16 15:20:36 -070010821 return err;
10822}
10823
10824static int tg3_close(struct net_device *dev)
10825{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010826 struct tg3 *tp = netdev_priv(dev);
10827
Matt Carlsonbe947302012-12-03 19:36:57 +000010828 tg3_ptp_fini(tp);
10829
Michael Chan65138592012-09-28 07:12:41 +000010830 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010831
10832 /* Clear stats across close / open calls */
10833 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
10834 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010835
10836 tg3_power_down(tp);
10837
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010838 tg3_carrier_off(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010839
10840 return 0;
10841}
10842
10843static inline u64 get_stat64(tg3_stat64_t *val)
10844{
10845 return ((u64)val->high << 32) | ((u64)val->low);
10846}
10847
10848static u64 tg3_calc_crc_errors(struct tg3 *tp)
10849{
10850 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10851
10852 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10853 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
10854 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
10855 u32 val;
10856
10857 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
10858 tg3_writephy(tp, MII_TG3_TEST1,
10859 val | MII_TG3_TEST1_CRC_EN);
10860 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
10861 } else
10862 val = 0;
10863
10864 tp->phy_crc_errors += val;
10865
10866 return tp->phy_crc_errors;
10867 }
10868
10869 return get_stat64(&hw_stats->rx_fcs_errors);
10870}
10871
10872#define ESTAT_ADD(member) \
10873 estats->member = old_estats->member + \
10874 get_stat64(&hw_stats->member)
10875
10876static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
10877{
10878 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
10879 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10880
10881 ESTAT_ADD(rx_octets);
10882 ESTAT_ADD(rx_fragments);
10883 ESTAT_ADD(rx_ucast_packets);
10884 ESTAT_ADD(rx_mcast_packets);
10885 ESTAT_ADD(rx_bcast_packets);
10886 ESTAT_ADD(rx_fcs_errors);
10887 ESTAT_ADD(rx_align_errors);
10888 ESTAT_ADD(rx_xon_pause_rcvd);
10889 ESTAT_ADD(rx_xoff_pause_rcvd);
10890 ESTAT_ADD(rx_mac_ctrl_rcvd);
10891 ESTAT_ADD(rx_xoff_entered);
10892 ESTAT_ADD(rx_frame_too_long_errors);
10893 ESTAT_ADD(rx_jabbers);
10894 ESTAT_ADD(rx_undersize_packets);
10895 ESTAT_ADD(rx_in_length_errors);
10896 ESTAT_ADD(rx_out_length_errors);
10897 ESTAT_ADD(rx_64_or_less_octet_packets);
10898 ESTAT_ADD(rx_65_to_127_octet_packets);
10899 ESTAT_ADD(rx_128_to_255_octet_packets);
10900 ESTAT_ADD(rx_256_to_511_octet_packets);
10901 ESTAT_ADD(rx_512_to_1023_octet_packets);
10902 ESTAT_ADD(rx_1024_to_1522_octet_packets);
10903 ESTAT_ADD(rx_1523_to_2047_octet_packets);
10904 ESTAT_ADD(rx_2048_to_4095_octet_packets);
10905 ESTAT_ADD(rx_4096_to_8191_octet_packets);
10906 ESTAT_ADD(rx_8192_to_9022_octet_packets);
10907
10908 ESTAT_ADD(tx_octets);
10909 ESTAT_ADD(tx_collisions);
10910 ESTAT_ADD(tx_xon_sent);
10911 ESTAT_ADD(tx_xoff_sent);
10912 ESTAT_ADD(tx_flow_control);
10913 ESTAT_ADD(tx_mac_errors);
10914 ESTAT_ADD(tx_single_collisions);
10915 ESTAT_ADD(tx_mult_collisions);
10916 ESTAT_ADD(tx_deferred);
10917 ESTAT_ADD(tx_excessive_collisions);
10918 ESTAT_ADD(tx_late_collisions);
10919 ESTAT_ADD(tx_collide_2times);
10920 ESTAT_ADD(tx_collide_3times);
10921 ESTAT_ADD(tx_collide_4times);
10922 ESTAT_ADD(tx_collide_5times);
10923 ESTAT_ADD(tx_collide_6times);
10924 ESTAT_ADD(tx_collide_7times);
10925 ESTAT_ADD(tx_collide_8times);
10926 ESTAT_ADD(tx_collide_9times);
10927 ESTAT_ADD(tx_collide_10times);
10928 ESTAT_ADD(tx_collide_11times);
10929 ESTAT_ADD(tx_collide_12times);
10930 ESTAT_ADD(tx_collide_13times);
10931 ESTAT_ADD(tx_collide_14times);
10932 ESTAT_ADD(tx_collide_15times);
10933 ESTAT_ADD(tx_ucast_packets);
10934 ESTAT_ADD(tx_mcast_packets);
10935 ESTAT_ADD(tx_bcast_packets);
10936 ESTAT_ADD(tx_carrier_sense_errors);
10937 ESTAT_ADD(tx_discards);
10938 ESTAT_ADD(tx_errors);
10939
10940 ESTAT_ADD(dma_writeq_full);
10941 ESTAT_ADD(dma_write_prioq_full);
10942 ESTAT_ADD(rxbds_empty);
10943 ESTAT_ADD(rx_discards);
10944 ESTAT_ADD(rx_errors);
10945 ESTAT_ADD(rx_threshold_hit);
10946
10947 ESTAT_ADD(dma_readq_full);
10948 ESTAT_ADD(dma_read_prioq_full);
10949 ESTAT_ADD(tx_comp_queue_full);
10950
10951 ESTAT_ADD(ring_set_send_prod_index);
10952 ESTAT_ADD(ring_status_update);
10953 ESTAT_ADD(nic_irqs);
10954 ESTAT_ADD(nic_avoided_irqs);
10955 ESTAT_ADD(nic_tx_threshold_hit);
10956
Matt Carlson4452d092011-05-19 12:12:51 +000010957 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010958}
10959
Matt Carlson65ec6982012-02-28 23:33:37 +000010960static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010961{
Eric Dumazet511d2222010-07-07 20:44:24 +000010962 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010963 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10964
Linus Torvalds1da177e2005-04-16 15:20:36 -070010965 stats->rx_packets = old_stats->rx_packets +
10966 get_stat64(&hw_stats->rx_ucast_packets) +
10967 get_stat64(&hw_stats->rx_mcast_packets) +
10968 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010969
Linus Torvalds1da177e2005-04-16 15:20:36 -070010970 stats->tx_packets = old_stats->tx_packets +
10971 get_stat64(&hw_stats->tx_ucast_packets) +
10972 get_stat64(&hw_stats->tx_mcast_packets) +
10973 get_stat64(&hw_stats->tx_bcast_packets);
10974
10975 stats->rx_bytes = old_stats->rx_bytes +
10976 get_stat64(&hw_stats->rx_octets);
10977 stats->tx_bytes = old_stats->tx_bytes +
10978 get_stat64(&hw_stats->tx_octets);
10979
10980 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070010981 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010982 stats->tx_errors = old_stats->tx_errors +
10983 get_stat64(&hw_stats->tx_errors) +
10984 get_stat64(&hw_stats->tx_mac_errors) +
10985 get_stat64(&hw_stats->tx_carrier_sense_errors) +
10986 get_stat64(&hw_stats->tx_discards);
10987
10988 stats->multicast = old_stats->multicast +
10989 get_stat64(&hw_stats->rx_mcast_packets);
10990 stats->collisions = old_stats->collisions +
10991 get_stat64(&hw_stats->tx_collisions);
10992
10993 stats->rx_length_errors = old_stats->rx_length_errors +
10994 get_stat64(&hw_stats->rx_frame_too_long_errors) +
10995 get_stat64(&hw_stats->rx_undersize_packets);
10996
10997 stats->rx_over_errors = old_stats->rx_over_errors +
10998 get_stat64(&hw_stats->rxbds_empty);
10999 stats->rx_frame_errors = old_stats->rx_frame_errors +
11000 get_stat64(&hw_stats->rx_align_errors);
11001 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11002 get_stat64(&hw_stats->tx_discards);
11003 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11004 get_stat64(&hw_stats->tx_carrier_sense_errors);
11005
11006 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011007 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011008
John W. Linville4f63b872005-09-12 14:43:18 -070011009 stats->rx_missed_errors = old_stats->rx_missed_errors +
11010 get_stat64(&hw_stats->rx_discards);
11011
Eric Dumazetb0057c52010-10-10 19:55:52 +000011012 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011013 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011014}
11015
Linus Torvalds1da177e2005-04-16 15:20:36 -070011016static int tg3_get_regs_len(struct net_device *dev)
11017{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011018 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011019}
11020
11021static void tg3_get_regs(struct net_device *dev,
11022 struct ethtool_regs *regs, void *_p)
11023{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011024 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011025
11026 regs->version = 0;
11027
Matt Carlson97bd8e42011-04-13 11:05:04 +000011028 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011029
Matt Carlson80096062010-08-02 11:26:06 +000011030 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011031 return;
11032
David S. Millerf47c11e2005-06-24 20:18:35 -070011033 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011034
Matt Carlson97bd8e42011-04-13 11:05:04 +000011035 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011036
David S. Millerf47c11e2005-06-24 20:18:35 -070011037 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011038}
11039
11040static int tg3_get_eeprom_len(struct net_device *dev)
11041{
11042 struct tg3 *tp = netdev_priv(dev);
11043
11044 return tp->nvram_size;
11045}
11046
Linus Torvalds1da177e2005-04-16 15:20:36 -070011047static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11048{
11049 struct tg3 *tp = netdev_priv(dev);
11050 int ret;
11051 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080011052 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011053 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011054
Joe Perches63c3a662011-04-26 08:12:10 +000011055 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011056 return -EINVAL;
11057
Matt Carlson80096062010-08-02 11:26:06 +000011058 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011059 return -EAGAIN;
11060
Linus Torvalds1da177e2005-04-16 15:20:36 -070011061 offset = eeprom->offset;
11062 len = eeprom->len;
11063 eeprom->len = 0;
11064
11065 eeprom->magic = TG3_EEPROM_MAGIC;
11066
11067 if (offset & 3) {
11068 /* adjustments to start on required 4 byte boundary */
11069 b_offset = offset & 3;
11070 b_count = 4 - b_offset;
11071 if (b_count > len) {
11072 /* i.e. offset=1 len=2 */
11073 b_count = len;
11074 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011075 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011076 if (ret)
11077 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011078 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011079 len -= b_count;
11080 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011081 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011082 }
11083
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011084 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011085 pd = &data[eeprom->len];
11086 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011087 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011088 if (ret) {
11089 eeprom->len += i;
11090 return ret;
11091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011092 memcpy(pd + i, &val, 4);
11093 }
11094 eeprom->len += i;
11095
11096 if (len & 3) {
11097 /* read last bytes not ending on 4 byte boundary */
11098 pd = &data[eeprom->len];
11099 b_count = len & 3;
11100 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011101 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011102 if (ret)
11103 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011104 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011105 eeprom->len += b_count;
11106 }
11107 return 0;
11108}
11109
Linus Torvalds1da177e2005-04-16 15:20:36 -070011110static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11111{
11112 struct tg3 *tp = netdev_priv(dev);
11113 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011114 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011115 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011116 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011117
Matt Carlson80096062010-08-02 11:26:06 +000011118 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011119 return -EAGAIN;
11120
Joe Perches63c3a662011-04-26 08:12:10 +000011121 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000011122 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011123 return -EINVAL;
11124
11125 offset = eeprom->offset;
11126 len = eeprom->len;
11127
11128 if ((b_offset = (offset & 3))) {
11129 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011130 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011131 if (ret)
11132 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011133 len += b_offset;
11134 offset &= ~3;
Michael Chan1c8594b42005-04-21 17:12:46 -070011135 if (len < 4)
11136 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011137 }
11138
11139 odd_len = 0;
Michael Chan1c8594b42005-04-21 17:12:46 -070011140 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011141 /* adjustments to end on required 4 byte boundary */
11142 odd_len = 1;
11143 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011144 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011145 if (ret)
11146 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011147 }
11148
11149 buf = data;
11150 if (b_offset || odd_len) {
11151 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011152 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011153 return -ENOMEM;
11154 if (b_offset)
11155 memcpy(buf, &start, 4);
11156 if (odd_len)
11157 memcpy(buf+len-4, &end, 4);
11158 memcpy(buf + b_offset, data, eeprom->len);
11159 }
11160
11161 ret = tg3_nvram_write_block(tp, offset, len, buf);
11162
11163 if (buf != data)
11164 kfree(buf);
11165
11166 return ret;
11167}
11168
11169static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11170{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011171 struct tg3 *tp = netdev_priv(dev);
11172
Joe Perches63c3a662011-04-26 08:12:10 +000011173 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011174 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011175 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011176 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011177 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11178 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011179 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011180
Linus Torvalds1da177e2005-04-16 15:20:36 -070011181 cmd->supported = (SUPPORTED_Autoneg);
11182
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011183 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011184 cmd->supported |= (SUPPORTED_1000baseT_Half |
11185 SUPPORTED_1000baseT_Full);
11186
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011187 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011188 cmd->supported |= (SUPPORTED_100baseT_Half |
11189 SUPPORTED_100baseT_Full |
11190 SUPPORTED_10baseT_Half |
11191 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080011192 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070011193 cmd->port = PORT_TP;
11194 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011195 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070011196 cmd->port = PORT_FIBRE;
11197 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011198
Linus Torvalds1da177e2005-04-16 15:20:36 -070011199 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000011200 if (tg3_flag(tp, PAUSE_AUTONEG)) {
11201 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
11202 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11203 cmd->advertising |= ADVERTISED_Pause;
11204 } else {
11205 cmd->advertising |= ADVERTISED_Pause |
11206 ADVERTISED_Asym_Pause;
11207 }
11208 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11209 cmd->advertising |= ADVERTISED_Asym_Pause;
11210 }
11211 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011212 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000011213 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011214 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000011215 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011216 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
11217 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
11218 cmd->eth_tp_mdix = ETH_TP_MDI_X;
11219 else
11220 cmd->eth_tp_mdix = ETH_TP_MDI;
11221 }
Matt Carlson64c22182010-10-14 10:37:44 +000011222 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000011223 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
11224 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011225 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011226 }
Matt Carlson882e9792009-09-01 13:21:36 +000011227 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011228 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011229 cmd->autoneg = tp->link_config.autoneg;
11230 cmd->maxtxpkt = 0;
11231 cmd->maxrxpkt = 0;
11232 return 0;
11233}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011234
Linus Torvalds1da177e2005-04-16 15:20:36 -070011235static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11236{
11237 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000011238 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011239
Joe Perches63c3a662011-04-26 08:12:10 +000011240 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011241 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011242 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011243 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011244 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11245 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011246 }
11247
Matt Carlson7e5856b2009-02-25 14:23:01 +000011248 if (cmd->autoneg != AUTONEG_ENABLE &&
11249 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070011250 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011251
11252 if (cmd->autoneg == AUTONEG_DISABLE &&
11253 cmd->duplex != DUPLEX_FULL &&
11254 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070011255 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011256
Matt Carlson7e5856b2009-02-25 14:23:01 +000011257 if (cmd->autoneg == AUTONEG_ENABLE) {
11258 u32 mask = ADVERTISED_Autoneg |
11259 ADVERTISED_Pause |
11260 ADVERTISED_Asym_Pause;
11261
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011262 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011263 mask |= ADVERTISED_1000baseT_Half |
11264 ADVERTISED_1000baseT_Full;
11265
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011266 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011267 mask |= ADVERTISED_100baseT_Half |
11268 ADVERTISED_100baseT_Full |
11269 ADVERTISED_10baseT_Half |
11270 ADVERTISED_10baseT_Full |
11271 ADVERTISED_TP;
11272 else
11273 mask |= ADVERTISED_FIBRE;
11274
11275 if (cmd->advertising & ~mask)
11276 return -EINVAL;
11277
11278 mask &= (ADVERTISED_1000baseT_Half |
11279 ADVERTISED_1000baseT_Full |
11280 ADVERTISED_100baseT_Half |
11281 ADVERTISED_100baseT_Full |
11282 ADVERTISED_10baseT_Half |
11283 ADVERTISED_10baseT_Full);
11284
11285 cmd->advertising &= mask;
11286 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011287 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000011288 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011289 return -EINVAL;
11290
11291 if (cmd->duplex != DUPLEX_FULL)
11292 return -EINVAL;
11293 } else {
David Decotigny25db0332011-04-27 18:32:39 +000011294 if (speed != SPEED_100 &&
11295 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011296 return -EINVAL;
11297 }
11298 }
11299
David S. Millerf47c11e2005-06-24 20:18:35 -070011300 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011301
11302 tp->link_config.autoneg = cmd->autoneg;
11303 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070011304 tp->link_config.advertising = (cmd->advertising |
11305 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000011306 tp->link_config.speed = SPEED_UNKNOWN;
11307 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011308 } else {
11309 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000011310 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011311 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011312 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011313
Linus Torvalds1da177e2005-04-16 15:20:36 -070011314 if (netif_running(dev))
11315 tg3_setup_phy(tp, 1);
11316
David S. Millerf47c11e2005-06-24 20:18:35 -070011317 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011318
Linus Torvalds1da177e2005-04-16 15:20:36 -070011319 return 0;
11320}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011321
Linus Torvalds1da177e2005-04-16 15:20:36 -070011322static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
11323{
11324 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011325
Rick Jones68aad782011-11-07 13:29:27 +000011326 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
11327 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
11328 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
11329 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011330}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011331
Linus Torvalds1da177e2005-04-16 15:20:36 -070011332static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11333{
11334 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011335
Joe Perches63c3a662011-04-26 08:12:10 +000011336 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070011337 wol->supported = WAKE_MAGIC;
11338 else
11339 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011340 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011341 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011342 wol->wolopts = WAKE_MAGIC;
11343 memset(&wol->sopass, 0, sizeof(wol->sopass));
11344}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011345
Linus Torvalds1da177e2005-04-16 15:20:36 -070011346static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11347{
11348 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070011349 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011350
Linus Torvalds1da177e2005-04-16 15:20:36 -070011351 if (wol->wolopts & ~WAKE_MAGIC)
11352 return -EINVAL;
11353 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011354 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011355 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011356
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011357 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
11358
David S. Millerf47c11e2005-06-24 20:18:35 -070011359 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011360 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000011361 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011362 else
Joe Perches63c3a662011-04-26 08:12:10 +000011363 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070011364 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011365
Linus Torvalds1da177e2005-04-16 15:20:36 -070011366 return 0;
11367}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011368
Linus Torvalds1da177e2005-04-16 15:20:36 -070011369static u32 tg3_get_msglevel(struct net_device *dev)
11370{
11371 struct tg3 *tp = netdev_priv(dev);
11372 return tp->msg_enable;
11373}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011374
Linus Torvalds1da177e2005-04-16 15:20:36 -070011375static void tg3_set_msglevel(struct net_device *dev, u32 value)
11376{
11377 struct tg3 *tp = netdev_priv(dev);
11378 tp->msg_enable = value;
11379}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011380
Linus Torvalds1da177e2005-04-16 15:20:36 -070011381static int tg3_nway_reset(struct net_device *dev)
11382{
11383 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011384 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011385
Linus Torvalds1da177e2005-04-16 15:20:36 -070011386 if (!netif_running(dev))
11387 return -EAGAIN;
11388
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011389 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070011390 return -EINVAL;
11391
Joe Perches63c3a662011-04-26 08:12:10 +000011392 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011393 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011394 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011395 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011396 } else {
11397 u32 bmcr;
11398
11399 spin_lock_bh(&tp->lock);
11400 r = -EINVAL;
11401 tg3_readphy(tp, MII_BMCR, &bmcr);
11402 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
11403 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011404 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011405 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
11406 BMCR_ANENABLE);
11407 r = 0;
11408 }
11409 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011410 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011411
Linus Torvalds1da177e2005-04-16 15:20:36 -070011412 return r;
11413}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011414
Linus Torvalds1da177e2005-04-16 15:20:36 -070011415static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11416{
11417 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011418
Matt Carlson2c49a442010-09-30 10:34:35 +000011419 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000011420 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000011421 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080011422 else
11423 ering->rx_jumbo_max_pending = 0;
11424
11425 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011426
11427 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000011428 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080011429 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
11430 else
11431 ering->rx_jumbo_pending = 0;
11432
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011433 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011434}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011435
Linus Torvalds1da177e2005-04-16 15:20:36 -070011436static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11437{
11438 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000011439 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011440
Matt Carlson2c49a442010-09-30 10:34:35 +000011441 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
11442 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070011443 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
11444 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000011445 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070011446 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011447 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011448
Michael Chanbbe832c2005-06-24 20:20:04 -070011449 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011450 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011451 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011452 irq_sync = 1;
11453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011454
Michael Chanbbe832c2005-06-24 20:20:04 -070011455 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011456
Linus Torvalds1da177e2005-04-16 15:20:36 -070011457 tp->rx_pending = ering->rx_pending;
11458
Joe Perches63c3a662011-04-26 08:12:10 +000011459 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011460 tp->rx_pending > 63)
11461 tp->rx_pending = 63;
11462 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000011463
Matt Carlson6fd45cb2010-09-15 08:59:57 +000011464 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000011465 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011466
11467 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070011468 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070011469 err = tg3_restart_hw(tp, 1);
11470 if (!err)
11471 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011472 }
11473
David S. Millerf47c11e2005-06-24 20:18:35 -070011474 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011475
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011476 if (irq_sync && !err)
11477 tg3_phy_start(tp);
11478
Michael Chanb9ec6c12006-07-25 16:37:27 -070011479 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011480}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011481
Linus Torvalds1da177e2005-04-16 15:20:36 -070011482static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
11483{
11484 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011485
Joe Perches63c3a662011-04-26 08:12:10 +000011486 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080011487
Matt Carlson4a2db502011-12-08 14:40:17 +000011488 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080011489 epause->rx_pause = 1;
11490 else
11491 epause->rx_pause = 0;
11492
Matt Carlson4a2db502011-12-08 14:40:17 +000011493 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080011494 epause->tx_pause = 1;
11495 else
11496 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011497}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011498
Linus Torvalds1da177e2005-04-16 15:20:36 -070011499static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
11500{
11501 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011502 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011503
Joe Perches63c3a662011-04-26 08:12:10 +000011504 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000011505 u32 newadv;
11506 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011507
Matt Carlson27121682010-02-17 15:16:57 +000011508 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011509
Matt Carlson27121682010-02-17 15:16:57 +000011510 if (!(phydev->supported & SUPPORTED_Pause) ||
11511 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000011512 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000011513 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011514
Matt Carlson27121682010-02-17 15:16:57 +000011515 tp->link_config.flowctrl = 0;
11516 if (epause->rx_pause) {
11517 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011518
Matt Carlson27121682010-02-17 15:16:57 +000011519 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080011520 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000011521 newadv = ADVERTISED_Pause;
11522 } else
11523 newadv = ADVERTISED_Pause |
11524 ADVERTISED_Asym_Pause;
11525 } else if (epause->tx_pause) {
11526 tp->link_config.flowctrl |= FLOW_CTRL_TX;
11527 newadv = ADVERTISED_Asym_Pause;
11528 } else
11529 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011530
Matt Carlson27121682010-02-17 15:16:57 +000011531 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000011532 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000011533 else
Joe Perches63c3a662011-04-26 08:12:10 +000011534 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000011535
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011536 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000011537 u32 oldadv = phydev->advertising &
11538 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
11539 if (oldadv != newadv) {
11540 phydev->advertising &=
11541 ~(ADVERTISED_Pause |
11542 ADVERTISED_Asym_Pause);
11543 phydev->advertising |= newadv;
11544 if (phydev->autoneg) {
11545 /*
11546 * Always renegotiate the link to
11547 * inform our link partner of our
11548 * flow control settings, even if the
11549 * flow control is forced. Let
11550 * tg3_adjust_link() do the final
11551 * flow control setup.
11552 */
11553 return phy_start_aneg(phydev);
11554 }
11555 }
11556
11557 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011558 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000011559 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000011560 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000011561 ~(ADVERTISED_Pause |
11562 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000011563 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011564 }
11565 } else {
11566 int irq_sync = 0;
11567
11568 if (netif_running(dev)) {
11569 tg3_netif_stop(tp);
11570 irq_sync = 1;
11571 }
11572
11573 tg3_full_lock(tp, irq_sync);
11574
11575 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000011576 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011577 else
Joe Perches63c3a662011-04-26 08:12:10 +000011578 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011579 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080011580 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011581 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080011582 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011583 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080011584 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011585 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080011586 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011587
11588 if (netif_running(dev)) {
11589 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11590 err = tg3_restart_hw(tp, 1);
11591 if (!err)
11592 tg3_netif_start(tp);
11593 }
11594
11595 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011597
Michael Chanb9ec6c12006-07-25 16:37:27 -070011598 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011599}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011600
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011601static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011602{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011603 switch (sset) {
11604 case ETH_SS_TEST:
11605 return TG3_NUM_TEST;
11606 case ETH_SS_STATS:
11607 return TG3_NUM_STATS;
11608 default:
11609 return -EOPNOTSUPP;
11610 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070011611}
11612
Matt Carlson90415472011-12-16 13:33:23 +000011613static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
11614 u32 *rules __always_unused)
11615{
11616 struct tg3 *tp = netdev_priv(dev);
11617
11618 if (!tg3_flag(tp, SUPPORT_MSIX))
11619 return -EOPNOTSUPP;
11620
11621 switch (info->cmd) {
11622 case ETHTOOL_GRXRINGS:
11623 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000011624 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000011625 else {
11626 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000011627 if (info->data > TG3_RSS_MAX_NUM_QS)
11628 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000011629 }
11630
11631 /* The first interrupt vector only
11632 * handles link interrupts.
11633 */
11634 info->data -= 1;
11635 return 0;
11636
11637 default:
11638 return -EOPNOTSUPP;
11639 }
11640}
11641
11642static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
11643{
11644 u32 size = 0;
11645 struct tg3 *tp = netdev_priv(dev);
11646
11647 if (tg3_flag(tp, SUPPORT_MSIX))
11648 size = TG3_RSS_INDIR_TBL_SIZE;
11649
11650 return size;
11651}
11652
11653static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
11654{
11655 struct tg3 *tp = netdev_priv(dev);
11656 int i;
11657
11658 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11659 indir[i] = tp->rss_ind_tbl[i];
11660
11661 return 0;
11662}
11663
11664static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
11665{
11666 struct tg3 *tp = netdev_priv(dev);
11667 size_t i;
11668
11669 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11670 tp->rss_ind_tbl[i] = indir[i];
11671
11672 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
11673 return 0;
11674
11675 /* It is legal to write the indirection
11676 * table while the device is running.
11677 */
11678 tg3_full_lock(tp, 0);
11679 tg3_rss_write_indir_tbl(tp);
11680 tg3_full_unlock(tp);
11681
11682 return 0;
11683}
11684
Michael Chan09681692012-09-28 07:12:42 +000011685static void tg3_get_channels(struct net_device *dev,
11686 struct ethtool_channels *channel)
11687{
11688 struct tg3 *tp = netdev_priv(dev);
11689 u32 deflt_qs = netif_get_num_default_rss_queues();
11690
11691 channel->max_rx = tp->rxq_max;
11692 channel->max_tx = tp->txq_max;
11693
11694 if (netif_running(dev)) {
11695 channel->rx_count = tp->rxq_cnt;
11696 channel->tx_count = tp->txq_cnt;
11697 } else {
11698 if (tp->rxq_req)
11699 channel->rx_count = tp->rxq_req;
11700 else
11701 channel->rx_count = min(deflt_qs, tp->rxq_max);
11702
11703 if (tp->txq_req)
11704 channel->tx_count = tp->txq_req;
11705 else
11706 channel->tx_count = min(deflt_qs, tp->txq_max);
11707 }
11708}
11709
11710static int tg3_set_channels(struct net_device *dev,
11711 struct ethtool_channels *channel)
11712{
11713 struct tg3 *tp = netdev_priv(dev);
11714
11715 if (!tg3_flag(tp, SUPPORT_MSIX))
11716 return -EOPNOTSUPP;
11717
11718 if (channel->rx_count > tp->rxq_max ||
11719 channel->tx_count > tp->txq_max)
11720 return -EINVAL;
11721
11722 tp->rxq_req = channel->rx_count;
11723 tp->txq_req = channel->tx_count;
11724
11725 if (!netif_running(dev))
11726 return 0;
11727
11728 tg3_stop(tp);
11729
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011730 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000011731
Matt Carlsonbe947302012-12-03 19:36:57 +000011732 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000011733
11734 return 0;
11735}
11736
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011737static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011738{
11739 switch (stringset) {
11740 case ETH_SS_STATS:
11741 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
11742 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070011743 case ETH_SS_TEST:
11744 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
11745 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011746 default:
11747 WARN_ON(1); /* we need a WARN() */
11748 break;
11749 }
11750}
11751
stephen hemminger81b87092011-04-04 08:43:50 +000011752static int tg3_set_phys_id(struct net_device *dev,
11753 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070011754{
11755 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070011756
11757 if (!netif_running(tp->dev))
11758 return -EAGAIN;
11759
stephen hemminger81b87092011-04-04 08:43:50 +000011760 switch (state) {
11761 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000011762 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070011763
stephen hemminger81b87092011-04-04 08:43:50 +000011764 case ETHTOOL_ID_ON:
11765 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11766 LED_CTRL_1000MBPS_ON |
11767 LED_CTRL_100MBPS_ON |
11768 LED_CTRL_10MBPS_ON |
11769 LED_CTRL_TRAFFIC_OVERRIDE |
11770 LED_CTRL_TRAFFIC_BLINK |
11771 LED_CTRL_TRAFFIC_LED);
11772 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011773
stephen hemminger81b87092011-04-04 08:43:50 +000011774 case ETHTOOL_ID_OFF:
11775 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11776 LED_CTRL_TRAFFIC_OVERRIDE);
11777 break;
Michael Chan4009a932005-09-05 17:52:54 -070011778
stephen hemminger81b87092011-04-04 08:43:50 +000011779 case ETHTOOL_ID_INACTIVE:
11780 tw32(MAC_LED_CTRL, tp->led_ctrl);
11781 break;
Michael Chan4009a932005-09-05 17:52:54 -070011782 }
stephen hemminger81b87092011-04-04 08:43:50 +000011783
Michael Chan4009a932005-09-05 17:52:54 -070011784 return 0;
11785}
11786
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011787static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011788 struct ethtool_stats *estats, u64 *tmp_stats)
11789{
11790 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000011791
Matt Carlsonb546e462012-02-13 15:20:09 +000011792 if (tp->hw_stats)
11793 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
11794 else
11795 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011796}
11797
Matt Carlson535a4902011-07-20 10:20:56 +000011798static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000011799{
11800 int i;
11801 __be32 *buf;
11802 u32 offset = 0, len = 0;
11803 u32 magic, val;
11804
Joe Perches63c3a662011-04-26 08:12:10 +000011805 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000011806 return NULL;
11807
11808 if (magic == TG3_EEPROM_MAGIC) {
11809 for (offset = TG3_NVM_DIR_START;
11810 offset < TG3_NVM_DIR_END;
11811 offset += TG3_NVM_DIRENT_SIZE) {
11812 if (tg3_nvram_read(tp, offset, &val))
11813 return NULL;
11814
11815 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
11816 TG3_NVM_DIRTYPE_EXTVPD)
11817 break;
11818 }
11819
11820 if (offset != TG3_NVM_DIR_END) {
11821 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
11822 if (tg3_nvram_read(tp, offset + 4, &offset))
11823 return NULL;
11824
11825 offset = tg3_nvram_logical_addr(tp, offset);
11826 }
11827 }
11828
11829 if (!offset || !len) {
11830 offset = TG3_NVM_VPD_OFF;
11831 len = TG3_NVM_VPD_LEN;
11832 }
11833
11834 buf = kmalloc(len, GFP_KERNEL);
11835 if (buf == NULL)
11836 return NULL;
11837
11838 if (magic == TG3_EEPROM_MAGIC) {
11839 for (i = 0; i < len; i += 4) {
11840 /* The data is in little-endian format in NVRAM.
11841 * Use the big-endian read routines to preserve
11842 * the byte order as it exists in NVRAM.
11843 */
11844 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
11845 goto error;
11846 }
11847 } else {
11848 u8 *ptr;
11849 ssize_t cnt;
11850 unsigned int pos = 0;
11851
11852 ptr = (u8 *)&buf[0];
11853 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
11854 cnt = pci_read_vpd(tp->pdev, pos,
11855 len - pos, ptr);
11856 if (cnt == -ETIMEDOUT || cnt == -EINTR)
11857 cnt = 0;
11858 else if (cnt < 0)
11859 goto error;
11860 }
11861 if (pos != len)
11862 goto error;
11863 }
11864
Matt Carlson535a4902011-07-20 10:20:56 +000011865 *vpdlen = len;
11866
Matt Carlsonc3e94502011-04-13 11:05:08 +000011867 return buf;
11868
11869error:
11870 kfree(buf);
11871 return NULL;
11872}
11873
Michael Chan566f86a2005-05-29 14:56:58 -070011874#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080011875#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
11876#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
11877#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000011878#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
11879#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000011880#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070011881#define NVRAM_SELFBOOT_HW_SIZE 0x20
11882#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070011883
11884static int tg3_test_nvram(struct tg3 *tp)
11885{
Matt Carlson535a4902011-07-20 10:20:56 +000011886 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011887 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011888 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070011889
Joe Perches63c3a662011-04-26 08:12:10 +000011890 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011891 return 0;
11892
Matt Carlsone4f34112009-02-25 14:25:00 +000011893 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011894 return -EIO;
11895
Michael Chan1b277772006-03-20 22:27:48 -080011896 if (magic == TG3_EEPROM_MAGIC)
11897 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070011898 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080011899 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
11900 TG3_EEPROM_SB_FORMAT_1) {
11901 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
11902 case TG3_EEPROM_SB_REVISION_0:
11903 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
11904 break;
11905 case TG3_EEPROM_SB_REVISION_2:
11906 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
11907 break;
11908 case TG3_EEPROM_SB_REVISION_3:
11909 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
11910 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000011911 case TG3_EEPROM_SB_REVISION_4:
11912 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
11913 break;
11914 case TG3_EEPROM_SB_REVISION_5:
11915 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
11916 break;
11917 case TG3_EEPROM_SB_REVISION_6:
11918 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
11919 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080011920 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000011921 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080011922 }
11923 } else
Michael Chan1b277772006-03-20 22:27:48 -080011924 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070011925 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
11926 size = NVRAM_SELFBOOT_HW_SIZE;
11927 else
Michael Chan1b277772006-03-20 22:27:48 -080011928 return -EIO;
11929
11930 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070011931 if (buf == NULL)
11932 return -ENOMEM;
11933
Michael Chan1b277772006-03-20 22:27:48 -080011934 err = -EIO;
11935 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011936 err = tg3_nvram_read_be32(tp, i, &buf[j]);
11937 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070011938 break;
Michael Chan566f86a2005-05-29 14:56:58 -070011939 }
Michael Chan1b277772006-03-20 22:27:48 -080011940 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070011941 goto out;
11942
Michael Chan1b277772006-03-20 22:27:48 -080011943 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011944 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080011945 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011946 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080011947 u8 *buf8 = (u8 *) buf, csum8 = 0;
11948
Al Virob9fc7dc2007-12-17 22:59:57 -080011949 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080011950 TG3_EEPROM_SB_REVISION_2) {
11951 /* For rev 2, the csum doesn't include the MBA. */
11952 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
11953 csum8 += buf8[i];
11954 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
11955 csum8 += buf8[i];
11956 } else {
11957 for (i = 0; i < size; i++)
11958 csum8 += buf8[i];
11959 }
Michael Chan1b277772006-03-20 22:27:48 -080011960
Adrian Bunkad96b482006-04-05 22:21:04 -070011961 if (csum8 == 0) {
11962 err = 0;
11963 goto out;
11964 }
11965
11966 err = -EIO;
11967 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080011968 }
Michael Chan566f86a2005-05-29 14:56:58 -070011969
Al Virob9fc7dc2007-12-17 22:59:57 -080011970 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011971 TG3_EEPROM_MAGIC_HW) {
11972 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000011973 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070011974 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070011975
11976 /* Separate the parity bits and the data bytes. */
11977 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
11978 if ((i == 0) || (i == 8)) {
11979 int l;
11980 u8 msk;
11981
11982 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
11983 parity[k++] = buf8[i] & msk;
11984 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000011985 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070011986 int l;
11987 u8 msk;
11988
11989 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
11990 parity[k++] = buf8[i] & msk;
11991 i++;
11992
11993 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
11994 parity[k++] = buf8[i] & msk;
11995 i++;
11996 }
11997 data[j++] = buf8[i];
11998 }
11999
12000 err = -EIO;
12001 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12002 u8 hw8 = hweight8(data[i]);
12003
12004 if ((hw8 & 0x1) && parity[i])
12005 goto out;
12006 else if (!(hw8 & 0x1) && !parity[i])
12007 goto out;
12008 }
12009 err = 0;
12010 goto out;
12011 }
12012
Matt Carlson01c3a392011-03-09 16:58:20 +000012013 err = -EIO;
12014
Michael Chan566f86a2005-05-29 14:56:58 -070012015 /* Bootstrap checksum at offset 0x10 */
12016 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000012017 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012018 goto out;
12019
12020 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
12021 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000012022 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000012023 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070012024
Matt Carlsonc3e94502011-04-13 11:05:08 +000012025 kfree(buf);
12026
Matt Carlson535a4902011-07-20 10:20:56 +000012027 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000012028 if (!buf)
12029 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012030
Matt Carlson535a4902011-07-20 10:20:56 +000012031 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012032 if (i > 0) {
12033 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
12034 if (j < 0)
12035 goto out;
12036
Matt Carlson535a4902011-07-20 10:20:56 +000012037 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000012038 goto out;
12039
12040 i += PCI_VPD_LRDT_TAG_SIZE;
12041 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
12042 PCI_VPD_RO_KEYWORD_CHKSUM);
12043 if (j > 0) {
12044 u8 csum8 = 0;
12045
12046 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12047
12048 for (i = 0; i <= j; i++)
12049 csum8 += ((u8 *)buf)[i];
12050
12051 if (csum8)
12052 goto out;
12053 }
12054 }
12055
Michael Chan566f86a2005-05-29 14:56:58 -070012056 err = 0;
12057
12058out:
12059 kfree(buf);
12060 return err;
12061}
12062
Michael Chanca430072005-05-29 14:57:23 -070012063#define TG3_SERDES_TIMEOUT_SEC 2
12064#define TG3_COPPER_TIMEOUT_SEC 6
12065
12066static int tg3_test_link(struct tg3 *tp)
12067{
12068 int i, max;
12069
12070 if (!netif_running(tp->dev))
12071 return -ENODEV;
12072
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012073 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070012074 max = TG3_SERDES_TIMEOUT_SEC;
12075 else
12076 max = TG3_COPPER_TIMEOUT_SEC;
12077
12078 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012079 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070012080 return 0;
12081
12082 if (msleep_interruptible(1000))
12083 break;
12084 }
12085
12086 return -EIO;
12087}
12088
Michael Chana71116d2005-05-29 14:58:11 -070012089/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080012090static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070012091{
Michael Chanb16250e2006-09-27 16:10:14 -070012092 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070012093 u32 offset, read_mask, write_mask, val, save_val, read_val;
12094 static struct {
12095 u16 offset;
12096 u16 flags;
12097#define TG3_FL_5705 0x1
12098#define TG3_FL_NOT_5705 0x2
12099#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070012100#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070012101 u32 read_mask;
12102 u32 write_mask;
12103 } reg_tbl[] = {
12104 /* MAC Control Registers */
12105 { MAC_MODE, TG3_FL_NOT_5705,
12106 0x00000000, 0x00ef6f8c },
12107 { MAC_MODE, TG3_FL_5705,
12108 0x00000000, 0x01ef6b8c },
12109 { MAC_STATUS, TG3_FL_NOT_5705,
12110 0x03800107, 0x00000000 },
12111 { MAC_STATUS, TG3_FL_5705,
12112 0x03800100, 0x00000000 },
12113 { MAC_ADDR_0_HIGH, 0x0000,
12114 0x00000000, 0x0000ffff },
12115 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012116 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070012117 { MAC_RX_MTU_SIZE, 0x0000,
12118 0x00000000, 0x0000ffff },
12119 { MAC_TX_MODE, 0x0000,
12120 0x00000000, 0x00000070 },
12121 { MAC_TX_LENGTHS, 0x0000,
12122 0x00000000, 0x00003fff },
12123 { MAC_RX_MODE, TG3_FL_NOT_5705,
12124 0x00000000, 0x000007fc },
12125 { MAC_RX_MODE, TG3_FL_5705,
12126 0x00000000, 0x000007dc },
12127 { MAC_HASH_REG_0, 0x0000,
12128 0x00000000, 0xffffffff },
12129 { MAC_HASH_REG_1, 0x0000,
12130 0x00000000, 0xffffffff },
12131 { MAC_HASH_REG_2, 0x0000,
12132 0x00000000, 0xffffffff },
12133 { MAC_HASH_REG_3, 0x0000,
12134 0x00000000, 0xffffffff },
12135
12136 /* Receive Data and Receive BD Initiator Control Registers. */
12137 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
12138 0x00000000, 0xffffffff },
12139 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
12140 0x00000000, 0xffffffff },
12141 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
12142 0x00000000, 0x00000003 },
12143 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
12144 0x00000000, 0xffffffff },
12145 { RCVDBDI_STD_BD+0, 0x0000,
12146 0x00000000, 0xffffffff },
12147 { RCVDBDI_STD_BD+4, 0x0000,
12148 0x00000000, 0xffffffff },
12149 { RCVDBDI_STD_BD+8, 0x0000,
12150 0x00000000, 0xffff0002 },
12151 { RCVDBDI_STD_BD+0xc, 0x0000,
12152 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012153
Michael Chana71116d2005-05-29 14:58:11 -070012154 /* Receive BD Initiator Control Registers. */
12155 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
12156 0x00000000, 0xffffffff },
12157 { RCVBDI_STD_THRESH, TG3_FL_5705,
12158 0x00000000, 0x000003ff },
12159 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
12160 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012161
Michael Chana71116d2005-05-29 14:58:11 -070012162 /* Host Coalescing Control Registers. */
12163 { HOSTCC_MODE, TG3_FL_NOT_5705,
12164 0x00000000, 0x00000004 },
12165 { HOSTCC_MODE, TG3_FL_5705,
12166 0x00000000, 0x000000f6 },
12167 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
12168 0x00000000, 0xffffffff },
12169 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
12170 0x00000000, 0x000003ff },
12171 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
12172 0x00000000, 0xffffffff },
12173 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
12174 0x00000000, 0x000003ff },
12175 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
12176 0x00000000, 0xffffffff },
12177 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12178 0x00000000, 0x000000ff },
12179 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
12180 0x00000000, 0xffffffff },
12181 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12182 0x00000000, 0x000000ff },
12183 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
12184 0x00000000, 0xffffffff },
12185 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
12186 0x00000000, 0xffffffff },
12187 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12188 0x00000000, 0xffffffff },
12189 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12190 0x00000000, 0x000000ff },
12191 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12192 0x00000000, 0xffffffff },
12193 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12194 0x00000000, 0x000000ff },
12195 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
12196 0x00000000, 0xffffffff },
12197 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
12198 0x00000000, 0xffffffff },
12199 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
12200 0x00000000, 0xffffffff },
12201 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
12202 0x00000000, 0xffffffff },
12203 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
12204 0x00000000, 0xffffffff },
12205 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
12206 0xffffffff, 0x00000000 },
12207 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
12208 0xffffffff, 0x00000000 },
12209
12210 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070012211 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012212 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070012213 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012214 0x00000000, 0x007fffff },
12215 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
12216 0x00000000, 0x0000003f },
12217 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
12218 0x00000000, 0x000001ff },
12219 { BUFMGR_MB_HIGH_WATER, 0x0000,
12220 0x00000000, 0x000001ff },
12221 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
12222 0xffffffff, 0x00000000 },
12223 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
12224 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012225
Michael Chana71116d2005-05-29 14:58:11 -070012226 /* Mailbox Registers */
12227 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
12228 0x00000000, 0x000001ff },
12229 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
12230 0x00000000, 0x000001ff },
12231 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
12232 0x00000000, 0x000007ff },
12233 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
12234 0x00000000, 0x000001ff },
12235
12236 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
12237 };
12238
Michael Chanb16250e2006-09-27 16:10:14 -070012239 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012240 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070012241 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000012242 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070012243 is_5750 = 1;
12244 }
Michael Chana71116d2005-05-29 14:58:11 -070012245
12246 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
12247 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
12248 continue;
12249
12250 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
12251 continue;
12252
Joe Perches63c3a662011-04-26 08:12:10 +000012253 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070012254 (reg_tbl[i].flags & TG3_FL_NOT_5788))
12255 continue;
12256
Michael Chanb16250e2006-09-27 16:10:14 -070012257 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
12258 continue;
12259
Michael Chana71116d2005-05-29 14:58:11 -070012260 offset = (u32) reg_tbl[i].offset;
12261 read_mask = reg_tbl[i].read_mask;
12262 write_mask = reg_tbl[i].write_mask;
12263
12264 /* Save the original register content */
12265 save_val = tr32(offset);
12266
12267 /* Determine the read-only value. */
12268 read_val = save_val & read_mask;
12269
12270 /* Write zero to the register, then make sure the read-only bits
12271 * are not changed and the read/write bits are all zeros.
12272 */
12273 tw32(offset, 0);
12274
12275 val = tr32(offset);
12276
12277 /* Test the read-only and read/write bits. */
12278 if (((val & read_mask) != read_val) || (val & write_mask))
12279 goto out;
12280
12281 /* Write ones to all the bits defined by RdMask and WrMask, then
12282 * make sure the read-only bits are not changed and the
12283 * read/write bits are all ones.
12284 */
12285 tw32(offset, read_mask | write_mask);
12286
12287 val = tr32(offset);
12288
12289 /* Test the read-only bits. */
12290 if ((val & read_mask) != read_val)
12291 goto out;
12292
12293 /* Test the read/write bits. */
12294 if ((val & write_mask) != write_mask)
12295 goto out;
12296
12297 tw32(offset, save_val);
12298 }
12299
12300 return 0;
12301
12302out:
Michael Chan9f88f292006-12-07 00:22:54 -080012303 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000012304 netdev_err(tp->dev,
12305 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070012306 tw32(offset, save_val);
12307 return -EIO;
12308}
12309
Michael Chan7942e1d2005-05-29 14:58:36 -070012310static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
12311{
Arjan van de Venf71e1302006-03-03 21:33:57 -050012312 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070012313 int i;
12314 u32 j;
12315
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020012316 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070012317 for (j = 0; j < len; j += 4) {
12318 u32 val;
12319
12320 tg3_write_mem(tp, offset + j, test_pattern[i]);
12321 tg3_read_mem(tp, offset + j, &val);
12322 if (val != test_pattern[i])
12323 return -EIO;
12324 }
12325 }
12326 return 0;
12327}
12328
12329static int tg3_test_memory(struct tg3 *tp)
12330{
12331 static struct mem_entry {
12332 u32 offset;
12333 u32 len;
12334 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080012335 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070012336 { 0x00002000, 0x1c000},
12337 { 0xffffffff, 0x00000}
12338 }, mem_tbl_5705[] = {
12339 { 0x00000100, 0x0000c},
12340 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070012341 { 0x00004000, 0x00800},
12342 { 0x00006000, 0x01000},
12343 { 0x00008000, 0x02000},
12344 { 0x00010000, 0x0e000},
12345 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080012346 }, mem_tbl_5755[] = {
12347 { 0x00000200, 0x00008},
12348 { 0x00004000, 0x00800},
12349 { 0x00006000, 0x00800},
12350 { 0x00008000, 0x02000},
12351 { 0x00010000, 0x0c000},
12352 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070012353 }, mem_tbl_5906[] = {
12354 { 0x00000200, 0x00008},
12355 { 0x00004000, 0x00400},
12356 { 0x00006000, 0x00400},
12357 { 0x00008000, 0x01000},
12358 { 0x00010000, 0x01000},
12359 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012360 }, mem_tbl_5717[] = {
12361 { 0x00000200, 0x00008},
12362 { 0x00010000, 0x0a000},
12363 { 0x00020000, 0x13c00},
12364 { 0xffffffff, 0x00000}
12365 }, mem_tbl_57765[] = {
12366 { 0x00000200, 0x00008},
12367 { 0x00004000, 0x00800},
12368 { 0x00006000, 0x09800},
12369 { 0x00010000, 0x0a000},
12370 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070012371 };
12372 struct mem_entry *mem_tbl;
12373 int err = 0;
12374 int i;
12375
Joe Perches63c3a662011-04-26 08:12:10 +000012376 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012377 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000012378 else if (tg3_flag(tp, 57765_CLASS) ||
12379 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012380 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000012381 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012382 mem_tbl = mem_tbl_5755;
12383 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12384 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000012385 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012386 mem_tbl = mem_tbl_5705;
12387 else
Michael Chan7942e1d2005-05-29 14:58:36 -070012388 mem_tbl = mem_tbl_570x;
12389
12390 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000012391 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
12392 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070012393 break;
12394 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012395
Michael Chan7942e1d2005-05-29 14:58:36 -070012396 return err;
12397}
12398
Matt Carlsonbb158d62011-04-25 12:42:47 +000012399#define TG3_TSO_MSS 500
12400
12401#define TG3_TSO_IP_HDR_LEN 20
12402#define TG3_TSO_TCP_HDR_LEN 20
12403#define TG3_TSO_TCP_OPT_LEN 12
12404
12405static const u8 tg3_tso_header[] = {
124060x08, 0x00,
124070x45, 0x00, 0x00, 0x00,
124080x00, 0x00, 0x40, 0x00,
124090x40, 0x06, 0x00, 0x00,
124100x0a, 0x00, 0x00, 0x01,
124110x0a, 0x00, 0x00, 0x02,
124120x0d, 0x00, 0xe0, 0x00,
124130x00, 0x00, 0x01, 0x00,
124140x00, 0x00, 0x02, 0x00,
124150x80, 0x10, 0x10, 0x00,
124160x14, 0x09, 0x00, 0x00,
124170x01, 0x01, 0x08, 0x0a,
124180x11, 0x11, 0x11, 0x11,
124190x11, 0x11, 0x11, 0x11,
12420};
Michael Chan9f40dea2005-09-05 17:53:06 -070012421
Matt Carlson28a45952011-08-19 13:58:22 +000012422static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070012423{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012424 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012425 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000012426 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000012427 struct sk_buff *skb;
12428 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070012429 dma_addr_t map;
12430 int num_pkts, tx_len, rx_len, i, err;
12431 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000012432 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000012433 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070012434
Matt Carlsonc8873402010-02-12 14:47:11 +000012435 tnapi = &tp->napi[0];
12436 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012437 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000012438 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000012439 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000012440 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000012441 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012442 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012443 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000012444
Michael Chanc76949a2005-05-29 14:58:59 -070012445 err = -EIO;
12446
Matt Carlson4852a862011-04-13 11:05:07 +000012447 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070012448 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070012449 if (!skb)
12450 return -ENOMEM;
12451
Michael Chanc76949a2005-05-29 14:58:59 -070012452 tx_data = skb_put(skb, tx_len);
12453 memcpy(tx_data, tp->dev->dev_addr, 6);
12454 memset(tx_data + 6, 0x0, 8);
12455
Matt Carlson4852a862011-04-13 11:05:07 +000012456 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070012457
Matt Carlson28a45952011-08-19 13:58:22 +000012458 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012459 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
12460
12461 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
12462 TG3_TSO_TCP_OPT_LEN;
12463
12464 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
12465 sizeof(tg3_tso_header));
12466 mss = TG3_TSO_MSS;
12467
12468 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
12469 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
12470
12471 /* Set the total length field in the IP header */
12472 iph->tot_len = htons((u16)(mss + hdr_len));
12473
12474 base_flags = (TXD_FLAG_CPU_PRE_DMA |
12475 TXD_FLAG_CPU_POST_DMA);
12476
Joe Perches63c3a662011-04-26 08:12:10 +000012477 if (tg3_flag(tp, HW_TSO_1) ||
12478 tg3_flag(tp, HW_TSO_2) ||
12479 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012480 struct tcphdr *th;
12481 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
12482 th = (struct tcphdr *)&tx_data[val];
12483 th->check = 0;
12484 } else
12485 base_flags |= TXD_FLAG_TCPUDP_CSUM;
12486
Joe Perches63c3a662011-04-26 08:12:10 +000012487 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012488 mss |= (hdr_len & 0xc) << 12;
12489 if (hdr_len & 0x10)
12490 base_flags |= 0x00000010;
12491 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000012492 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000012493 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000012494 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000012495 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
12496 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
12497 } else {
12498 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
12499 }
12500
12501 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
12502 } else {
12503 num_pkts = 1;
12504 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000012505
12506 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
12507 tx_len > VLAN_ETH_FRAME_LEN)
12508 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012509 }
12510
12511 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070012512 tx_data[i] = (u8) (i & 0xff);
12513
Alexander Duyckf4188d82009-12-02 16:48:38 +000012514 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
12515 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000012516 dev_kfree_skb(skb);
12517 return -EIO;
12518 }
Michael Chanc76949a2005-05-29 14:58:59 -070012519
Matt Carlson0d681b22011-07-27 14:20:49 +000012520 val = tnapi->tx_prod;
12521 tnapi->tx_buffers[val].skb = skb;
12522 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
12523
Michael Chanc76949a2005-05-29 14:58:59 -070012524 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012525 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070012526
12527 udelay(10);
12528
Matt Carlson898a56f2009-08-28 14:02:40 +000012529 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070012530
Matt Carlson84b67b22011-07-27 14:20:52 +000012531 budget = tg3_tx_avail(tnapi);
12532 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000012533 base_flags | TXD_FLAG_END, mss, 0)) {
12534 tnapi->tx_buffers[val].skb = NULL;
12535 dev_kfree_skb(skb);
12536 return -EIO;
12537 }
Michael Chanc76949a2005-05-29 14:58:59 -070012538
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012539 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070012540
Michael Chan6541b802012-03-04 14:48:14 +000012541 /* Sync BD data before updating mailbox */
12542 wmb();
12543
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012544 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
12545 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070012546
12547 udelay(10);
12548
Matt Carlson303fc922009-11-02 14:27:34 +000012549 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
12550 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070012551 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012552 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070012553
12554 udelay(10);
12555
Matt Carlson898a56f2009-08-28 14:02:40 +000012556 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
12557 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012558 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070012559 (rx_idx == (rx_start_idx + num_pkts)))
12560 break;
12561 }
12562
Matt Carlsonba1142e2011-11-04 09:15:00 +000012563 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070012564 dev_kfree_skb(skb);
12565
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012566 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070012567 goto out;
12568
12569 if (rx_idx != rx_start_idx + num_pkts)
12570 goto out;
12571
Matt Carlsonbb158d62011-04-25 12:42:47 +000012572 val = data_off;
12573 while (rx_idx != rx_start_idx) {
12574 desc = &rnapi->rx_rcb[rx_start_idx++];
12575 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
12576 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070012577
Matt Carlsonbb158d62011-04-25 12:42:47 +000012578 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
12579 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000012580 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070012581
Matt Carlsonbb158d62011-04-25 12:42:47 +000012582 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
12583 - ETH_FCS_LEN;
12584
Matt Carlson28a45952011-08-19 13:58:22 +000012585 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012586 if (rx_len != tx_len)
12587 goto out;
12588
12589 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
12590 if (opaque_key != RXD_OPAQUE_RING_STD)
12591 goto out;
12592 } else {
12593 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
12594 goto out;
12595 }
12596 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
12597 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000012598 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012599 goto out;
12600 }
12601
12602 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012603 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012604 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
12605 mapping);
12606 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012607 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012608 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
12609 mapping);
12610 } else
Matt Carlson4852a862011-04-13 11:05:07 +000012611 goto out;
12612
Matt Carlsonbb158d62011-04-25 12:42:47 +000012613 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
12614 PCI_DMA_FROMDEVICE);
12615
Eric Dumazet9205fd92011-11-18 06:47:01 +000012616 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000012617 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012618 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000012619 goto out;
12620 }
Matt Carlson4852a862011-04-13 11:05:07 +000012621 }
12622
Michael Chanc76949a2005-05-29 14:58:59 -070012623 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012624
Eric Dumazet9205fd92011-11-18 06:47:01 +000012625 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070012626out:
12627 return err;
12628}
12629
Matt Carlson00c266b2011-04-25 12:42:46 +000012630#define TG3_STD_LOOPBACK_FAILED 1
12631#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000012632#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000012633#define TG3_LOOPBACK_FAILED \
12634 (TG3_STD_LOOPBACK_FAILED | \
12635 TG3_JMB_LOOPBACK_FAILED | \
12636 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000012637
Matt Carlson941ec902011-08-19 13:58:23 +000012638static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070012639{
Matt Carlson28a45952011-08-19 13:58:22 +000012640 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000012641 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000012642 u32 jmb_pkt_sz = 9000;
12643
12644 if (tp->dma_limit)
12645 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070012646
Matt Carlsonab789042011-01-25 15:58:54 +000012647 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
12648 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
12649
Matt Carlson28a45952011-08-19 13:58:22 +000012650 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012651 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
12652 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012653 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012654 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000012655 goto done;
12656 }
12657
Michael Chanb9ec6c12006-07-25 16:37:27 -070012658 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000012659 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012660 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
12661 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012662 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012663 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000012664 goto done;
12665 }
Michael Chan9f40dea2005-09-05 17:53:06 -070012666
Joe Perches63c3a662011-04-26 08:12:10 +000012667 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000012668 int i;
12669
12670 /* Reroute all rx packets to the 1st queue */
12671 for (i = MAC_RSS_INDIR_TBL_0;
12672 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
12673 tw32(i, 0x0);
12674 }
12675
Matt Carlson6e01b202011-08-19 13:58:20 +000012676 /* HW errata - mac loopback fails in some cases on 5780.
12677 * Normal traffic and PHY loopback are not affected by
12678 * errata. Also, the MAC loopback test is deprecated for
12679 * all newer ASIC revisions.
12680 */
12681 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
12682 !tg3_flag(tp, CPMU_PRESENT)) {
12683 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070012684
Matt Carlson28a45952011-08-19 13:58:22 +000012685 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012686 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012687
12688 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012689 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012690 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012691
12692 tg3_mac_loopback(tp, false);
12693 }
Matt Carlson4852a862011-04-13 11:05:07 +000012694
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012695 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012696 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012697 int i;
12698
Matt Carlson941ec902011-08-19 13:58:23 +000012699 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012700
12701 /* Wait for link */
12702 for (i = 0; i < 100; i++) {
12703 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
12704 break;
12705 mdelay(1);
12706 }
12707
Matt Carlson28a45952011-08-19 13:58:22 +000012708 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012709 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012710 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000012711 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012712 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012713 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012714 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012715 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070012716
Matt Carlson941ec902011-08-19 13:58:23 +000012717 if (do_extlpbk) {
12718 tg3_phy_lpbk_set(tp, 0, true);
12719
12720 /* All link indications report up, but the hardware
12721 * isn't really ready for about 20 msec. Double it
12722 * to be sure.
12723 */
12724 mdelay(40);
12725
12726 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012727 data[TG3_EXT_LOOPB_TEST] |=
12728 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012729 if (tg3_flag(tp, TSO_CAPABLE) &&
12730 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012731 data[TG3_EXT_LOOPB_TEST] |=
12732 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012733 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012734 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012735 data[TG3_EXT_LOOPB_TEST] |=
12736 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012737 }
12738
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012739 /* Re-enable gphy autopowerdown. */
12740 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
12741 tg3_phy_toggle_apd(tp, true);
12742 }
Matt Carlson6833c042008-11-21 17:18:59 -080012743
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012744 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
12745 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000012746
Matt Carlsonab789042011-01-25 15:58:54 +000012747done:
12748 tp->phy_flags |= eee_cap;
12749
Michael Chan9f40dea2005-09-05 17:53:06 -070012750 return err;
12751}
12752
Michael Chan4cafd3f2005-05-29 14:56:34 -070012753static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
12754 u64 *data)
12755{
Michael Chan566f86a2005-05-29 14:56:58 -070012756 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000012757 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070012758
Matt Carlsonbed98292011-07-13 09:27:29 +000012759 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
12760 tg3_power_up(tp)) {
12761 etest->flags |= ETH_TEST_FL_FAILED;
12762 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
12763 return;
12764 }
Michael Chanbc1c7562006-03-20 17:48:03 -080012765
Michael Chan566f86a2005-05-29 14:56:58 -070012766 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
12767
12768 if (tg3_test_nvram(tp) != 0) {
12769 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012770 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070012771 }
Matt Carlson941ec902011-08-19 13:58:23 +000012772 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070012773 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012774 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070012775 }
Michael Chana71116d2005-05-29 14:58:11 -070012776 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012777 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070012778
Michael Chanbbe832c2005-06-24 20:20:04 -070012779 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012780 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012781 tg3_netif_stop(tp);
12782 irq_sync = 1;
12783 }
12784
12785 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070012786 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080012787 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012788 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000012789 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070012790 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080012791 if (!err)
12792 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012793
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012794 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080012795 tg3_phy_reset(tp);
12796
Michael Chana71116d2005-05-29 14:58:11 -070012797 if (tg3_test_registers(tp) != 0) {
12798 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012799 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070012800 }
Matt Carlson28a45952011-08-19 13:58:22 +000012801
Michael Chan7942e1d2005-05-29 14:58:36 -070012802 if (tg3_test_memory(tp) != 0) {
12803 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012804 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070012805 }
Matt Carlson28a45952011-08-19 13:58:22 +000012806
Matt Carlson941ec902011-08-19 13:58:23 +000012807 if (doextlpbk)
12808 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
12809
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012810 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070012811 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070012812
David S. Millerf47c11e2005-06-24 20:18:35 -070012813 tg3_full_unlock(tp);
12814
Michael Chand4bc3922005-05-29 14:59:20 -070012815 if (tg3_test_interrupt(tp) != 0) {
12816 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012817 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070012818 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012819
12820 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070012821
Michael Chana71116d2005-05-29 14:58:11 -070012822 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12823 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012824 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012825 err2 = tg3_restart_hw(tp, 1);
12826 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070012827 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012828 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012829
12830 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012831
12832 if (irq_sync && !err2)
12833 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012834 }
Matt Carlson80096062010-08-02 11:26:06 +000012835 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000012836 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080012837
Michael Chan4cafd3f2005-05-29 14:56:34 -070012838}
12839
Matt Carlson0a633ac2012-12-03 19:36:59 +000012840static int tg3_hwtstamp_ioctl(struct net_device *dev,
12841 struct ifreq *ifr, int cmd)
12842{
12843 struct tg3 *tp = netdev_priv(dev);
12844 struct hwtstamp_config stmpconf;
12845
12846 if (!tg3_flag(tp, PTP_CAPABLE))
12847 return -EINVAL;
12848
12849 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
12850 return -EFAULT;
12851
12852 if (stmpconf.flags)
12853 return -EINVAL;
12854
12855 switch (stmpconf.tx_type) {
12856 case HWTSTAMP_TX_ON:
12857 tg3_flag_set(tp, TX_TSTAMP_EN);
12858 break;
12859 case HWTSTAMP_TX_OFF:
12860 tg3_flag_clear(tp, TX_TSTAMP_EN);
12861 break;
12862 default:
12863 return -ERANGE;
12864 }
12865
12866 switch (stmpconf.rx_filter) {
12867 case HWTSTAMP_FILTER_NONE:
12868 tp->rxptpctl = 0;
12869 break;
12870 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
12871 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
12872 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
12873 break;
12874 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
12875 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
12876 TG3_RX_PTP_CTL_SYNC_EVNT;
12877 break;
12878 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
12879 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
12880 TG3_RX_PTP_CTL_DELAY_REQ;
12881 break;
12882 case HWTSTAMP_FILTER_PTP_V2_EVENT:
12883 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
12884 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
12885 break;
12886 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
12887 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
12888 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
12889 break;
12890 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
12891 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
12892 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
12893 break;
12894 case HWTSTAMP_FILTER_PTP_V2_SYNC:
12895 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
12896 TG3_RX_PTP_CTL_SYNC_EVNT;
12897 break;
12898 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
12899 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
12900 TG3_RX_PTP_CTL_SYNC_EVNT;
12901 break;
12902 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
12903 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
12904 TG3_RX_PTP_CTL_SYNC_EVNT;
12905 break;
12906 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
12907 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
12908 TG3_RX_PTP_CTL_DELAY_REQ;
12909 break;
12910 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
12911 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
12912 TG3_RX_PTP_CTL_DELAY_REQ;
12913 break;
12914 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
12915 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
12916 TG3_RX_PTP_CTL_DELAY_REQ;
12917 break;
12918 default:
12919 return -ERANGE;
12920 }
12921
12922 if (netif_running(dev) && tp->rxptpctl)
12923 tw32(TG3_RX_PTP_CTL,
12924 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
12925
12926 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
12927 -EFAULT : 0;
12928}
12929
Linus Torvalds1da177e2005-04-16 15:20:36 -070012930static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12931{
12932 struct mii_ioctl_data *data = if_mii(ifr);
12933 struct tg3 *tp = netdev_priv(dev);
12934 int err;
12935
Joe Perches63c3a662011-04-26 08:12:10 +000012936 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012937 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012938 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012939 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012940 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000012941 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012942 }
12943
Matt Carlson33f401a2010-04-05 10:19:27 +000012944 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012945 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000012946 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012947
12948 /* fallthru */
12949 case SIOCGMIIREG: {
12950 u32 mii_regval;
12951
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012952 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012953 break; /* We have no PHY */
12954
Matt Carlson34eea5a2011-04-20 07:57:38 +000012955 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012956 return -EAGAIN;
12957
David S. Millerf47c11e2005-06-24 20:18:35 -070012958 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012959 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070012960 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012961
12962 data->val_out = mii_regval;
12963
12964 return err;
12965 }
12966
12967 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012968 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012969 break; /* We have no PHY */
12970
Matt Carlson34eea5a2011-04-20 07:57:38 +000012971 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012972 return -EAGAIN;
12973
David S. Millerf47c11e2005-06-24 20:18:35 -070012974 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012975 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070012976 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012977
12978 return err;
12979
Matt Carlson0a633ac2012-12-03 19:36:59 +000012980 case SIOCSHWTSTAMP:
12981 return tg3_hwtstamp_ioctl(dev, ifr, cmd);
12982
Linus Torvalds1da177e2005-04-16 15:20:36 -070012983 default:
12984 /* do nothing */
12985 break;
12986 }
12987 return -EOPNOTSUPP;
12988}
12989
David S. Miller15f98502005-05-18 22:49:26 -070012990static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12991{
12992 struct tg3 *tp = netdev_priv(dev);
12993
12994 memcpy(ec, &tp->coal, sizeof(*ec));
12995 return 0;
12996}
12997
Michael Chand244c892005-07-05 14:42:33 -070012998static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12999{
13000 struct tg3 *tp = netdev_priv(dev);
13001 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
13002 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
13003
Joe Perches63c3a662011-04-26 08:12:10 +000013004 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070013005 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
13006 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
13007 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
13008 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
13009 }
13010
13011 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
13012 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
13013 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
13014 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
13015 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
13016 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
13017 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
13018 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
13019 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
13020 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
13021 return -EINVAL;
13022
13023 /* No rx interrupts will be generated if both are zero */
13024 if ((ec->rx_coalesce_usecs == 0) &&
13025 (ec->rx_max_coalesced_frames == 0))
13026 return -EINVAL;
13027
13028 /* No tx interrupts will be generated if both are zero */
13029 if ((ec->tx_coalesce_usecs == 0) &&
13030 (ec->tx_max_coalesced_frames == 0))
13031 return -EINVAL;
13032
13033 /* Only copy relevant parameters, ignore all others. */
13034 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
13035 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
13036 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
13037 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
13038 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
13039 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
13040 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
13041 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
13042 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
13043
13044 if (netif_running(dev)) {
13045 tg3_full_lock(tp, 0);
13046 __tg3_set_coalesce(tp, &tp->coal);
13047 tg3_full_unlock(tp);
13048 }
13049 return 0;
13050}
13051
Jeff Garzik7282d492006-09-13 14:30:00 -040013052static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013053 .get_settings = tg3_get_settings,
13054 .set_settings = tg3_set_settings,
13055 .get_drvinfo = tg3_get_drvinfo,
13056 .get_regs_len = tg3_get_regs_len,
13057 .get_regs = tg3_get_regs,
13058 .get_wol = tg3_get_wol,
13059 .set_wol = tg3_set_wol,
13060 .get_msglevel = tg3_get_msglevel,
13061 .set_msglevel = tg3_set_msglevel,
13062 .nway_reset = tg3_nway_reset,
13063 .get_link = ethtool_op_get_link,
13064 .get_eeprom_len = tg3_get_eeprom_len,
13065 .get_eeprom = tg3_get_eeprom,
13066 .set_eeprom = tg3_set_eeprom,
13067 .get_ringparam = tg3_get_ringparam,
13068 .set_ringparam = tg3_set_ringparam,
13069 .get_pauseparam = tg3_get_pauseparam,
13070 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070013071 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013072 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000013073 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013074 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070013075 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070013076 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070013077 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000013078 .get_rxnfc = tg3_get_rxnfc,
13079 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
13080 .get_rxfh_indir = tg3_get_rxfh_indir,
13081 .set_rxfh_indir = tg3_set_rxfh_indir,
Michael Chan09681692012-09-28 07:12:42 +000013082 .get_channels = tg3_get_channels,
13083 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000013084 .get_ts_info = tg3_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013085};
13086
David S. Millerb4017c52012-03-01 17:57:40 -050013087static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
13088 struct rtnl_link_stats64 *stats)
13089{
13090 struct tg3 *tp = netdev_priv(dev);
13091
David S. Millerb4017c52012-03-01 17:57:40 -050013092 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000013093 if (!tp->hw_stats) {
13094 spin_unlock_bh(&tp->lock);
13095 return &tp->net_stats_prev;
13096 }
13097
David S. Millerb4017c52012-03-01 17:57:40 -050013098 tg3_get_nstats(tp, stats);
13099 spin_unlock_bh(&tp->lock);
13100
13101 return stats;
13102}
13103
Matt Carlsonccd5ba92012-02-13 10:20:08 +000013104static void tg3_set_rx_mode(struct net_device *dev)
13105{
13106 struct tg3 *tp = netdev_priv(dev);
13107
13108 if (!netif_running(dev))
13109 return;
13110
13111 tg3_full_lock(tp, 0);
13112 __tg3_set_rx_mode(dev);
13113 tg3_full_unlock(tp);
13114}
13115
Matt Carlsonfaf16272012-02-13 10:20:07 +000013116static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
13117 int new_mtu)
13118{
13119 dev->mtu = new_mtu;
13120
13121 if (new_mtu > ETH_DATA_LEN) {
13122 if (tg3_flag(tp, 5780_CLASS)) {
13123 netdev_update_features(dev);
13124 tg3_flag_clear(tp, TSO_CAPABLE);
13125 } else {
13126 tg3_flag_set(tp, JUMBO_RING_ENABLE);
13127 }
13128 } else {
13129 if (tg3_flag(tp, 5780_CLASS)) {
13130 tg3_flag_set(tp, TSO_CAPABLE);
13131 netdev_update_features(dev);
13132 }
13133 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
13134 }
13135}
13136
13137static int tg3_change_mtu(struct net_device *dev, int new_mtu)
13138{
13139 struct tg3 *tp = netdev_priv(dev);
Michael Chan2fae5e32012-03-04 14:48:15 +000013140 int err, reset_phy = 0;
Matt Carlsonfaf16272012-02-13 10:20:07 +000013141
13142 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
13143 return -EINVAL;
13144
13145 if (!netif_running(dev)) {
13146 /* We'll just catch it later when the
13147 * device is up'd.
13148 */
13149 tg3_set_mtu(dev, tp, new_mtu);
13150 return 0;
13151 }
13152
13153 tg3_phy_stop(tp);
13154
13155 tg3_netif_stop(tp);
13156
13157 tg3_full_lock(tp, 1);
13158
13159 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13160
13161 tg3_set_mtu(dev, tp, new_mtu);
13162
Michael Chan2fae5e32012-03-04 14:48:15 +000013163 /* Reset PHY, otherwise the read DMA engine will be in a mode that
13164 * breaks all requests to 256 bytes.
13165 */
13166 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
13167 reset_phy = 1;
13168
13169 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000013170
13171 if (!err)
13172 tg3_netif_start(tp);
13173
13174 tg3_full_unlock(tp);
13175
13176 if (!err)
13177 tg3_phy_start(tp);
13178
13179 return err;
13180}
13181
13182static const struct net_device_ops tg3_netdev_ops = {
13183 .ndo_open = tg3_open,
13184 .ndo_stop = tg3_close,
13185 .ndo_start_xmit = tg3_start_xmit,
13186 .ndo_get_stats64 = tg3_get_stats64,
13187 .ndo_validate_addr = eth_validate_addr,
13188 .ndo_set_rx_mode = tg3_set_rx_mode,
13189 .ndo_set_mac_address = tg3_set_mac_addr,
13190 .ndo_do_ioctl = tg3_ioctl,
13191 .ndo_tx_timeout = tg3_tx_timeout,
13192 .ndo_change_mtu = tg3_change_mtu,
13193 .ndo_fix_features = tg3_fix_features,
13194 .ndo_set_features = tg3_set_features,
13195#ifdef CONFIG_NET_POLL_CONTROLLER
13196 .ndo_poll_controller = tg3_poll_controller,
13197#endif
13198};
13199
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013200static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013201{
Michael Chan1b277772006-03-20 22:27:48 -080013202 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013203
13204 tp->nvram_size = EEPROM_CHIP_SIZE;
13205
Matt Carlsone4f34112009-02-25 14:25:00 +000013206 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013207 return;
13208
Michael Chanb16250e2006-09-27 16:10:14 -070013209 if ((magic != TG3_EEPROM_MAGIC) &&
13210 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
13211 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013212 return;
13213
13214 /*
13215 * Size the chip by reading offsets at increasing powers of two.
13216 * When we encounter our validation signature, we know the addressing
13217 * has wrapped around, and thus have our chip size.
13218 */
Michael Chan1b277772006-03-20 22:27:48 -080013219 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013220
13221 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013222 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013223 return;
13224
Michael Chan18201802006-03-20 22:29:15 -080013225 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013226 break;
13227
13228 cursize <<= 1;
13229 }
13230
13231 tp->nvram_size = cursize;
13232}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013233
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013234static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013235{
13236 u32 val;
13237
Joe Perches63c3a662011-04-26 08:12:10 +000013238 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080013239 return;
13240
13241 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080013242 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080013243 tg3_get_eeprom_size(tp);
13244 return;
13245 }
13246
Matt Carlson6d348f22009-02-25 14:25:52 +000013247 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013248 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000013249 /* This is confusing. We want to operate on the
13250 * 16-bit value at offset 0xf2. The tg3_nvram_read()
13251 * call will read from NVRAM and byteswap the data
13252 * according to the byteswapping settings for all
13253 * other register accesses. This ensures the data we
13254 * want will always reside in the lower 16-bits.
13255 * However, the data in NVRAM is in LE format, which
13256 * means the data from the NVRAM read will always be
13257 * opposite the endianness of the CPU. The 16-bit
13258 * byteswap then brings the data to CPU endianness.
13259 */
13260 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013261 return;
13262 }
13263 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070013264 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013265}
13266
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013267static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013268{
13269 u32 nvcfg1;
13270
13271 nvcfg1 = tr32(NVRAM_CFG1);
13272 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000013273 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013274 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013275 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13276 tw32(NVRAM_CFG1, nvcfg1);
13277 }
13278
Matt Carlson6ff6f812011-05-19 12:12:54 +000013279 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013280 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013281 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013282 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
13283 tp->nvram_jedecnum = JEDEC_ATMEL;
13284 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013285 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013286 break;
13287 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
13288 tp->nvram_jedecnum = JEDEC_ATMEL;
13289 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
13290 break;
13291 case FLASH_VENDOR_ATMEL_EEPROM:
13292 tp->nvram_jedecnum = JEDEC_ATMEL;
13293 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013294 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013295 break;
13296 case FLASH_VENDOR_ST:
13297 tp->nvram_jedecnum = JEDEC_ST;
13298 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013299 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013300 break;
13301 case FLASH_VENDOR_SAIFUN:
13302 tp->nvram_jedecnum = JEDEC_SAIFUN;
13303 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
13304 break;
13305 case FLASH_VENDOR_SST_SMALL:
13306 case FLASH_VENDOR_SST_LARGE:
13307 tp->nvram_jedecnum = JEDEC_SST;
13308 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
13309 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013310 }
Matt Carlson8590a602009-08-28 12:29:16 +000013311 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013312 tp->nvram_jedecnum = JEDEC_ATMEL;
13313 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013314 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013315 }
13316}
13317
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013318static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013319{
13320 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
13321 case FLASH_5752PAGE_SIZE_256:
13322 tp->nvram_pagesize = 256;
13323 break;
13324 case FLASH_5752PAGE_SIZE_512:
13325 tp->nvram_pagesize = 512;
13326 break;
13327 case FLASH_5752PAGE_SIZE_1K:
13328 tp->nvram_pagesize = 1024;
13329 break;
13330 case FLASH_5752PAGE_SIZE_2K:
13331 tp->nvram_pagesize = 2048;
13332 break;
13333 case FLASH_5752PAGE_SIZE_4K:
13334 tp->nvram_pagesize = 4096;
13335 break;
13336 case FLASH_5752PAGE_SIZE_264:
13337 tp->nvram_pagesize = 264;
13338 break;
13339 case FLASH_5752PAGE_SIZE_528:
13340 tp->nvram_pagesize = 528;
13341 break;
13342 }
13343}
13344
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013345static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070013346{
13347 u32 nvcfg1;
13348
13349 nvcfg1 = tr32(NVRAM_CFG1);
13350
Michael Chane6af3012005-04-21 17:12:05 -070013351 /* NVRAM protection for TPM */
13352 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000013353 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070013354
Michael Chan361b4ac2005-04-21 17:11:21 -070013355 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013356 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
13357 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
13358 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013359 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013360 break;
13361 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13362 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013363 tg3_flag_set(tp, NVRAM_BUFFERED);
13364 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013365 break;
13366 case FLASH_5752VENDOR_ST_M45PE10:
13367 case FLASH_5752VENDOR_ST_M45PE20:
13368 case FLASH_5752VENDOR_ST_M45PE40:
13369 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013370 tg3_flag_set(tp, NVRAM_BUFFERED);
13371 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013372 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070013373 }
13374
Joe Perches63c3a662011-04-26 08:12:10 +000013375 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000013376 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000013377 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070013378 /* For eeprom, set pagesize to maximum eeprom size */
13379 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13380
13381 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13382 tw32(NVRAM_CFG1, nvcfg1);
13383 }
13384}
13385
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013386static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080013387{
Matt Carlson989a9d22007-05-05 11:51:05 -070013388 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080013389
13390 nvcfg1 = tr32(NVRAM_CFG1);
13391
13392 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070013393 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013394 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070013395 protect = 1;
13396 }
Michael Chand3c7b882006-03-23 01:28:25 -080013397
Matt Carlson989a9d22007-05-05 11:51:05 -070013398 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13399 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013400 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13401 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13402 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13403 case FLASH_5755VENDOR_ATMEL_FLASH_5:
13404 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013405 tg3_flag_set(tp, NVRAM_BUFFERED);
13406 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013407 tp->nvram_pagesize = 264;
13408 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
13409 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
13410 tp->nvram_size = (protect ? 0x3e200 :
13411 TG3_NVRAM_SIZE_512KB);
13412 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
13413 tp->nvram_size = (protect ? 0x1f200 :
13414 TG3_NVRAM_SIZE_256KB);
13415 else
13416 tp->nvram_size = (protect ? 0x1f200 :
13417 TG3_NVRAM_SIZE_128KB);
13418 break;
13419 case FLASH_5752VENDOR_ST_M45PE10:
13420 case FLASH_5752VENDOR_ST_M45PE20:
13421 case FLASH_5752VENDOR_ST_M45PE40:
13422 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013423 tg3_flag_set(tp, NVRAM_BUFFERED);
13424 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013425 tp->nvram_pagesize = 256;
13426 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
13427 tp->nvram_size = (protect ?
13428 TG3_NVRAM_SIZE_64KB :
13429 TG3_NVRAM_SIZE_128KB);
13430 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
13431 tp->nvram_size = (protect ?
13432 TG3_NVRAM_SIZE_64KB :
13433 TG3_NVRAM_SIZE_256KB);
13434 else
13435 tp->nvram_size = (protect ?
13436 TG3_NVRAM_SIZE_128KB :
13437 TG3_NVRAM_SIZE_512KB);
13438 break;
Michael Chand3c7b882006-03-23 01:28:25 -080013439 }
13440}
13441
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013442static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080013443{
13444 u32 nvcfg1;
13445
13446 nvcfg1 = tr32(NVRAM_CFG1);
13447
13448 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013449 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
13450 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
13451 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
13452 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
13453 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013454 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013455 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080013456
Matt Carlson8590a602009-08-28 12:29:16 +000013457 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13458 tw32(NVRAM_CFG1, nvcfg1);
13459 break;
13460 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13461 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13462 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13463 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13464 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013465 tg3_flag_set(tp, NVRAM_BUFFERED);
13466 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013467 tp->nvram_pagesize = 264;
13468 break;
13469 case FLASH_5752VENDOR_ST_M45PE10:
13470 case FLASH_5752VENDOR_ST_M45PE20:
13471 case FLASH_5752VENDOR_ST_M45PE40:
13472 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013473 tg3_flag_set(tp, NVRAM_BUFFERED);
13474 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013475 tp->nvram_pagesize = 256;
13476 break;
Michael Chan1b277772006-03-20 22:27:48 -080013477 }
13478}
13479
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013480static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070013481{
13482 u32 nvcfg1, protect = 0;
13483
13484 nvcfg1 = tr32(NVRAM_CFG1);
13485
13486 /* NVRAM protection for TPM */
13487 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013488 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070013489 protect = 1;
13490 }
13491
13492 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13493 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013494 case FLASH_5761VENDOR_ATMEL_ADB021D:
13495 case FLASH_5761VENDOR_ATMEL_ADB041D:
13496 case FLASH_5761VENDOR_ATMEL_ADB081D:
13497 case FLASH_5761VENDOR_ATMEL_ADB161D:
13498 case FLASH_5761VENDOR_ATMEL_MDB021D:
13499 case FLASH_5761VENDOR_ATMEL_MDB041D:
13500 case FLASH_5761VENDOR_ATMEL_MDB081D:
13501 case FLASH_5761VENDOR_ATMEL_MDB161D:
13502 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013503 tg3_flag_set(tp, NVRAM_BUFFERED);
13504 tg3_flag_set(tp, FLASH);
13505 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000013506 tp->nvram_pagesize = 256;
13507 break;
13508 case FLASH_5761VENDOR_ST_A_M45PE20:
13509 case FLASH_5761VENDOR_ST_A_M45PE40:
13510 case FLASH_5761VENDOR_ST_A_M45PE80:
13511 case FLASH_5761VENDOR_ST_A_M45PE16:
13512 case FLASH_5761VENDOR_ST_M_M45PE20:
13513 case FLASH_5761VENDOR_ST_M_M45PE40:
13514 case FLASH_5761VENDOR_ST_M_M45PE80:
13515 case FLASH_5761VENDOR_ST_M_M45PE16:
13516 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013517 tg3_flag_set(tp, NVRAM_BUFFERED);
13518 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013519 tp->nvram_pagesize = 256;
13520 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070013521 }
13522
13523 if (protect) {
13524 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
13525 } else {
13526 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013527 case FLASH_5761VENDOR_ATMEL_ADB161D:
13528 case FLASH_5761VENDOR_ATMEL_MDB161D:
13529 case FLASH_5761VENDOR_ST_A_M45PE16:
13530 case FLASH_5761VENDOR_ST_M_M45PE16:
13531 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
13532 break;
13533 case FLASH_5761VENDOR_ATMEL_ADB081D:
13534 case FLASH_5761VENDOR_ATMEL_MDB081D:
13535 case FLASH_5761VENDOR_ST_A_M45PE80:
13536 case FLASH_5761VENDOR_ST_M_M45PE80:
13537 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13538 break;
13539 case FLASH_5761VENDOR_ATMEL_ADB041D:
13540 case FLASH_5761VENDOR_ATMEL_MDB041D:
13541 case FLASH_5761VENDOR_ST_A_M45PE40:
13542 case FLASH_5761VENDOR_ST_M_M45PE40:
13543 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13544 break;
13545 case FLASH_5761VENDOR_ATMEL_ADB021D:
13546 case FLASH_5761VENDOR_ATMEL_MDB021D:
13547 case FLASH_5761VENDOR_ST_A_M45PE20:
13548 case FLASH_5761VENDOR_ST_M_M45PE20:
13549 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13550 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070013551 }
13552 }
13553}
13554
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013555static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070013556{
13557 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013558 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070013559 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13560}
13561
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013562static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080013563{
13564 u32 nvcfg1;
13565
13566 nvcfg1 = tr32(NVRAM_CFG1);
13567
13568 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13569 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
13570 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
13571 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013572 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080013573 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13574
13575 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13576 tw32(NVRAM_CFG1, nvcfg1);
13577 return;
13578 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13579 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
13580 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
13581 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
13582 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
13583 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
13584 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
13585 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013586 tg3_flag_set(tp, NVRAM_BUFFERED);
13587 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080013588
13589 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13590 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13591 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
13592 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
13593 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13594 break;
13595 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
13596 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
13597 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13598 break;
13599 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
13600 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
13601 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13602 break;
13603 }
13604 break;
13605 case FLASH_5752VENDOR_ST_M45PE10:
13606 case FLASH_5752VENDOR_ST_M45PE20:
13607 case FLASH_5752VENDOR_ST_M45PE40:
13608 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013609 tg3_flag_set(tp, NVRAM_BUFFERED);
13610 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080013611
13612 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13613 case FLASH_5752VENDOR_ST_M45PE10:
13614 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13615 break;
13616 case FLASH_5752VENDOR_ST_M45PE20:
13617 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13618 break;
13619 case FLASH_5752VENDOR_ST_M45PE40:
13620 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13621 break;
13622 }
13623 break;
13624 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013625 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080013626 return;
13627 }
13628
Matt Carlsona1b950d2009-09-01 13:20:17 +000013629 tg3_nvram_get_pagesize(tp, nvcfg1);
13630 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013631 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013632}
13633
13634
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013635static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013636{
13637 u32 nvcfg1;
13638
13639 nvcfg1 = tr32(NVRAM_CFG1);
13640
13641 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13642 case FLASH_5717VENDOR_ATMEL_EEPROM:
13643 case FLASH_5717VENDOR_MICRO_EEPROM:
13644 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013645 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013646 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13647
13648 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13649 tw32(NVRAM_CFG1, nvcfg1);
13650 return;
13651 case FLASH_5717VENDOR_ATMEL_MDB011D:
13652 case FLASH_5717VENDOR_ATMEL_ADB011B:
13653 case FLASH_5717VENDOR_ATMEL_ADB011D:
13654 case FLASH_5717VENDOR_ATMEL_MDB021D:
13655 case FLASH_5717VENDOR_ATMEL_ADB021B:
13656 case FLASH_5717VENDOR_ATMEL_ADB021D:
13657 case FLASH_5717VENDOR_ATMEL_45USPT:
13658 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013659 tg3_flag_set(tp, NVRAM_BUFFERED);
13660 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013661
13662 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13663 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000013664 /* Detect size with tg3_nvram_get_size() */
13665 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013666 case FLASH_5717VENDOR_ATMEL_ADB021B:
13667 case FLASH_5717VENDOR_ATMEL_ADB021D:
13668 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13669 break;
13670 default:
13671 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13672 break;
13673 }
Matt Carlson321d32a2008-11-21 17:22:19 -080013674 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013675 case FLASH_5717VENDOR_ST_M_M25PE10:
13676 case FLASH_5717VENDOR_ST_A_M25PE10:
13677 case FLASH_5717VENDOR_ST_M_M45PE10:
13678 case FLASH_5717VENDOR_ST_A_M45PE10:
13679 case FLASH_5717VENDOR_ST_M_M25PE20:
13680 case FLASH_5717VENDOR_ST_A_M25PE20:
13681 case FLASH_5717VENDOR_ST_M_M45PE20:
13682 case FLASH_5717VENDOR_ST_A_M45PE20:
13683 case FLASH_5717VENDOR_ST_25USPT:
13684 case FLASH_5717VENDOR_ST_45USPT:
13685 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013686 tg3_flag_set(tp, NVRAM_BUFFERED);
13687 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013688
13689 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13690 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000013691 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000013692 /* Detect size with tg3_nvram_get_size() */
13693 break;
13694 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000013695 case FLASH_5717VENDOR_ST_A_M45PE20:
13696 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13697 break;
13698 default:
13699 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13700 break;
13701 }
Matt Carlson321d32a2008-11-21 17:22:19 -080013702 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013703 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013704 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013705 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080013706 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000013707
13708 tg3_nvram_get_pagesize(tp, nvcfg1);
13709 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013710 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013711}
13712
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013713static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013714{
13715 u32 nvcfg1, nvmpinstrp;
13716
13717 nvcfg1 = tr32(NVRAM_CFG1);
13718 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
13719
Michael Chanc86a8562013-01-06 12:51:08 +000013720 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762) {
13721 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
13722 tg3_flag_set(tp, NO_NVRAM);
13723 return;
13724 }
13725
13726 switch (nvmpinstrp) {
13727 case FLASH_5762_EEPROM_HD:
13728 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030013729 break;
Michael Chanc86a8562013-01-06 12:51:08 +000013730 case FLASH_5762_EEPROM_LD:
13731 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030013732 break;
Michael Chanc86a8562013-01-06 12:51:08 +000013733 }
13734 }
13735
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013736 switch (nvmpinstrp) {
13737 case FLASH_5720_EEPROM_HD:
13738 case FLASH_5720_EEPROM_LD:
13739 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013740 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013741
13742 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13743 tw32(NVRAM_CFG1, nvcfg1);
13744 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
13745 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13746 else
13747 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
13748 return;
13749 case FLASH_5720VENDOR_M_ATMEL_DB011D:
13750 case FLASH_5720VENDOR_A_ATMEL_DB011B:
13751 case FLASH_5720VENDOR_A_ATMEL_DB011D:
13752 case FLASH_5720VENDOR_M_ATMEL_DB021D:
13753 case FLASH_5720VENDOR_A_ATMEL_DB021B:
13754 case FLASH_5720VENDOR_A_ATMEL_DB021D:
13755 case FLASH_5720VENDOR_M_ATMEL_DB041D:
13756 case FLASH_5720VENDOR_A_ATMEL_DB041B:
13757 case FLASH_5720VENDOR_A_ATMEL_DB041D:
13758 case FLASH_5720VENDOR_M_ATMEL_DB081D:
13759 case FLASH_5720VENDOR_A_ATMEL_DB081D:
13760 case FLASH_5720VENDOR_ATMEL_45USPT:
13761 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013762 tg3_flag_set(tp, NVRAM_BUFFERED);
13763 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013764
13765 switch (nvmpinstrp) {
13766 case FLASH_5720VENDOR_M_ATMEL_DB021D:
13767 case FLASH_5720VENDOR_A_ATMEL_DB021B:
13768 case FLASH_5720VENDOR_A_ATMEL_DB021D:
13769 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13770 break;
13771 case FLASH_5720VENDOR_M_ATMEL_DB041D:
13772 case FLASH_5720VENDOR_A_ATMEL_DB041B:
13773 case FLASH_5720VENDOR_A_ATMEL_DB041D:
13774 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13775 break;
13776 case FLASH_5720VENDOR_M_ATMEL_DB081D:
13777 case FLASH_5720VENDOR_A_ATMEL_DB081D:
13778 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13779 break;
13780 default:
13781 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13782 break;
13783 }
13784 break;
13785 case FLASH_5720VENDOR_M_ST_M25PE10:
13786 case FLASH_5720VENDOR_M_ST_M45PE10:
13787 case FLASH_5720VENDOR_A_ST_M25PE10:
13788 case FLASH_5720VENDOR_A_ST_M45PE10:
13789 case FLASH_5720VENDOR_M_ST_M25PE20:
13790 case FLASH_5720VENDOR_M_ST_M45PE20:
13791 case FLASH_5720VENDOR_A_ST_M25PE20:
13792 case FLASH_5720VENDOR_A_ST_M45PE20:
13793 case FLASH_5720VENDOR_M_ST_M25PE40:
13794 case FLASH_5720VENDOR_M_ST_M45PE40:
13795 case FLASH_5720VENDOR_A_ST_M25PE40:
13796 case FLASH_5720VENDOR_A_ST_M45PE40:
13797 case FLASH_5720VENDOR_M_ST_M25PE80:
13798 case FLASH_5720VENDOR_M_ST_M45PE80:
13799 case FLASH_5720VENDOR_A_ST_M25PE80:
13800 case FLASH_5720VENDOR_A_ST_M45PE80:
13801 case FLASH_5720VENDOR_ST_25USPT:
13802 case FLASH_5720VENDOR_ST_45USPT:
13803 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013804 tg3_flag_set(tp, NVRAM_BUFFERED);
13805 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013806
13807 switch (nvmpinstrp) {
13808 case FLASH_5720VENDOR_M_ST_M25PE20:
13809 case FLASH_5720VENDOR_M_ST_M45PE20:
13810 case FLASH_5720VENDOR_A_ST_M25PE20:
13811 case FLASH_5720VENDOR_A_ST_M45PE20:
13812 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13813 break;
13814 case FLASH_5720VENDOR_M_ST_M25PE40:
13815 case FLASH_5720VENDOR_M_ST_M45PE40:
13816 case FLASH_5720VENDOR_A_ST_M25PE40:
13817 case FLASH_5720VENDOR_A_ST_M45PE40:
13818 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13819 break;
13820 case FLASH_5720VENDOR_M_ST_M25PE80:
13821 case FLASH_5720VENDOR_M_ST_M45PE80:
13822 case FLASH_5720VENDOR_A_ST_M25PE80:
13823 case FLASH_5720VENDOR_A_ST_M45PE80:
13824 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13825 break;
13826 default:
13827 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13828 break;
13829 }
13830 break;
13831 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013832 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013833 return;
13834 }
13835
13836 tg3_nvram_get_pagesize(tp, nvcfg1);
13837 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013838 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000013839
13840 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762) {
13841 u32 val;
13842
13843 if (tg3_nvram_read(tp, 0, &val))
13844 return;
13845
13846 if (val != TG3_EEPROM_MAGIC &&
13847 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
13848 tg3_flag_set(tp, NO_NVRAM);
13849 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013850}
13851
Linus Torvalds1da177e2005-04-16 15:20:36 -070013852/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013853static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013854{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013855 tw32_f(GRC_EEPROM_ADDR,
13856 (EEPROM_ADDR_FSM_RESET |
13857 (EEPROM_DEFAULT_CLOCK_PERIOD <<
13858 EEPROM_ADDR_CLKPERD_SHIFT)));
13859
Michael Chan9d57f012006-12-07 00:23:25 -080013860 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013861
13862 /* Enable seeprom accesses. */
13863 tw32_f(GRC_LOCAL_CTRL,
13864 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
13865 udelay(100);
13866
13867 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13868 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000013869 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013870
Michael Chanec41c7d2006-01-17 02:40:55 -080013871 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000013872 netdev_warn(tp->dev,
13873 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000013874 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080013875 return;
13876 }
Michael Chane6af3012005-04-21 17:12:05 -070013877 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013878
Matt Carlson989a9d22007-05-05 11:51:05 -070013879 tp->nvram_size = 0;
13880
Michael Chan361b4ac2005-04-21 17:11:21 -070013881 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13882 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080013883 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13884 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070013885 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013886 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13887 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080013888 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070013889 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
13890 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070013891 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13892 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000013893 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000013894 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013895 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013896 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13897 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013898 tg3_get_5717_nvram_info(tp);
Michael Chanc86a8562013-01-06 12:51:08 +000013899 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13900 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013901 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070013902 else
13903 tg3_get_nvram_info(tp);
13904
Matt Carlson989a9d22007-05-05 11:51:05 -070013905 if (tp->nvram_size == 0)
13906 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013907
Michael Chane6af3012005-04-21 17:12:05 -070013908 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080013909 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013910
13911 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013912 tg3_flag_clear(tp, NVRAM);
13913 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013914
13915 tg3_get_eeprom_size(tp);
13916 }
13917}
13918
Linus Torvalds1da177e2005-04-16 15:20:36 -070013919struct subsys_tbl_ent {
13920 u16 subsys_vendor, subsys_devid;
13921 u32 phy_id;
13922};
13923
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013924static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013925 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013926 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013927 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013928 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013929 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013930 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013931 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013932 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13933 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
13934 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013935 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013936 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013937 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013938 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13939 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
13940 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013941 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013942 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013943 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013944 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013945 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013946 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013947 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013948
13949 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013950 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013951 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013952 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013953 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013954 { TG3PCI_SUBVENDOR_ID_3COM,
13955 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
13956 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013957 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013958 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013959 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013960
13961 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013962 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013963 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013964 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013965 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013966 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013967 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013968 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013969 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013970
13971 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013972 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013973 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013974 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013975 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013976 { TG3PCI_SUBVENDOR_ID_COMPAQ,
13977 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
13978 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013979 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013980 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013981 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013982
13983 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013984 { TG3PCI_SUBVENDOR_ID_IBM,
13985 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013986};
13987
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013988static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013989{
13990 int i;
13991
13992 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
13993 if ((subsys_id_to_phy_id[i].subsys_vendor ==
13994 tp->pdev->subsystem_vendor) &&
13995 (subsys_id_to_phy_id[i].subsys_devid ==
13996 tp->pdev->subsystem_device))
13997 return &subsys_id_to_phy_id[i];
13998 }
13999 return NULL;
14000}
14001
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014002static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014003{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014004 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070014005
Matt Carlson79eb6902010-02-17 15:17:03 +000014006 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014007 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14008
Gary Zambranoa85feb82007-05-05 11:52:19 -070014009 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000014010 tg3_flag_set(tp, EEPROM_WRITE_PROT);
14011 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080014012
Michael Chanb5d37722006-09-27 16:06:21 -070014013 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080014014 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014015 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14016 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014017 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014018 val = tr32(VCPU_CFGSHDW);
14019 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000014020 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070014021 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014022 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014023 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014024 device_set_wakeup_enable(&tp->pdev->dev, true);
14025 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014026 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070014027 }
14028
Linus Torvalds1da177e2005-04-16 15:20:36 -070014029 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
14030 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
14031 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070014032 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014033 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014034
14035 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
14036 tp->nic_sram_data_cfg = nic_cfg;
14037
14038 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
14039 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014040 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14041 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14042 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014043 (ver > 0) && (ver < 0x100))
14044 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
14045
Matt Carlsona9daf362008-05-25 23:49:44 -070014046 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
14047 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
14048
Linus Torvalds1da177e2005-04-16 15:20:36 -070014049 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
14050 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
14051 eeprom_phy_serdes = 1;
14052
14053 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
14054 if (nic_phy_id != 0) {
14055 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
14056 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
14057
14058 eeprom_phy_id = (id1 >> 16) << 10;
14059 eeprom_phy_id |= (id2 & 0xfc00) << 16;
14060 eeprom_phy_id |= (id2 & 0x03ff) << 0;
14061 } else
14062 eeprom_phy_id = 0;
14063
Michael Chan7d0c41e2005-04-21 17:06:20 -070014064 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070014065 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000014066 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014067 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000014068 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014069 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070014070 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070014071
Joe Perches63c3a662011-04-26 08:12:10 +000014072 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014073 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
14074 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070014075 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014076 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
14077
14078 switch (led_cfg) {
14079 default:
14080 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
14081 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14082 break;
14083
14084 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
14085 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14086 break;
14087
14088 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
14089 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070014090
14091 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
14092 * read on some older 5700/5701 bootcode.
14093 */
14094 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14095 ASIC_REV_5700 ||
14096 GET_ASIC_REV(tp->pci_chip_rev_id) ==
14097 ASIC_REV_5701)
14098 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14099
Linus Torvalds1da177e2005-04-16 15:20:36 -070014100 break;
14101
14102 case SHASTA_EXT_LED_SHARED:
14103 tp->led_ctrl = LED_CTRL_MODE_SHARED;
14104 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
14105 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
14106 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14107 LED_CTRL_MODE_PHY_2);
14108 break;
14109
14110 case SHASTA_EXT_LED_MAC:
14111 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
14112 break;
14113
14114 case SHASTA_EXT_LED_COMBO:
14115 tp->led_ctrl = LED_CTRL_MODE_COMBO;
14116 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
14117 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14118 LED_CTRL_MODE_PHY_2);
14119 break;
14120
Stephen Hemminger855e1112008-04-16 16:37:28 -070014121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014122
14123 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14124 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
14125 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
14126 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14127
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014128 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
14129 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080014130
Michael Chan9d26e212006-12-07 00:21:14 -080014131 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000014132 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014133 if ((tp->pdev->subsystem_vendor ==
14134 PCI_VENDOR_ID_ARIMA) &&
14135 (tp->pdev->subsystem_device == 0x205a ||
14136 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000014137 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014138 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014139 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14140 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014142
14143 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000014144 tg3_flag_set(tp, ENABLE_ASF);
14145 if (tg3_flag(tp, 5750_PLUS))
14146 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014147 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014148
14149 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014150 tg3_flag(tp, 5750_PLUS))
14151 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014152
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014153 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070014154 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000014155 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014156
Joe Perches63c3a662011-04-26 08:12:10 +000014157 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014158 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014159 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014160 device_set_wakeup_enable(&tp->pdev->dev, true);
14161 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014162
Linus Torvalds1da177e2005-04-16 15:20:36 -070014163 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014164 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014165
14166 /* serdes signal pre-emphasis in register 0x590 set by */
14167 /* bootcode if bit 18 is set */
14168 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014169 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070014170
Joe Perches63c3a662011-04-26 08:12:10 +000014171 if ((tg3_flag(tp, 57765_PLUS) ||
14172 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14173 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080014174 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014175 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080014176
Joe Perches63c3a662011-04-26 08:12:10 +000014177 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000014178 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014179 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070014180 u32 cfg3;
14181
14182 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
14183 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000014184 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070014185 }
Matt Carlsona9daf362008-05-25 23:49:44 -070014186
Matt Carlson14417062010-02-17 15:16:59 +000014187 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000014188 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070014189 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014190 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070014191 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014192 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014193 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014194done:
Joe Perches63c3a662011-04-26 08:12:10 +000014195 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014196 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000014197 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014198 else
14199 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070014200}
14201
Michael Chanc86a8562013-01-06 12:51:08 +000014202static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
14203{
14204 int i, err;
14205 u32 val2, off = offset * 8;
14206
14207 err = tg3_nvram_lock(tp);
14208 if (err)
14209 return err;
14210
14211 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
14212 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
14213 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
14214 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
14215 udelay(10);
14216
14217 for (i = 0; i < 100; i++) {
14218 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
14219 if (val2 & APE_OTP_STATUS_CMD_DONE) {
14220 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
14221 break;
14222 }
14223 udelay(10);
14224 }
14225
14226 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
14227
14228 tg3_nvram_unlock(tp);
14229 if (val2 & APE_OTP_STATUS_CMD_DONE)
14230 return 0;
14231
14232 return -EBUSY;
14233}
14234
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014235static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014236{
14237 int i;
14238 u32 val;
14239
14240 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
14241 tw32(OTP_CTRL, cmd);
14242
14243 /* Wait for up to 1 ms for command to execute. */
14244 for (i = 0; i < 100; i++) {
14245 val = tr32(OTP_STATUS);
14246 if (val & OTP_STATUS_CMD_DONE)
14247 break;
14248 udelay(10);
14249 }
14250
14251 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
14252}
14253
14254/* Read the gphy configuration from the OTP region of the chip. The gphy
14255 * configuration is a 32-bit value that straddles the alignment boundary.
14256 * We do two 32-bit reads and then shift and merge the results.
14257 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014258static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014259{
14260 u32 bhalf_otp, thalf_otp;
14261
14262 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
14263
14264 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
14265 return 0;
14266
14267 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
14268
14269 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14270 return 0;
14271
14272 thalf_otp = tr32(OTP_READ_DATA);
14273
14274 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
14275
14276 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14277 return 0;
14278
14279 bhalf_otp = tr32(OTP_READ_DATA);
14280
14281 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
14282}
14283
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014284static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000014285{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000014286 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014287
14288 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
14289 adv |= ADVERTISED_1000baseT_Half |
14290 ADVERTISED_1000baseT_Full;
14291
14292 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14293 adv |= ADVERTISED_100baseT_Half |
14294 ADVERTISED_100baseT_Full |
14295 ADVERTISED_10baseT_Half |
14296 ADVERTISED_10baseT_Full |
14297 ADVERTISED_TP;
14298 else
14299 adv |= ADVERTISED_FIBRE;
14300
14301 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000014302 tp->link_config.speed = SPEED_UNKNOWN;
14303 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014304 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000014305 tp->link_config.active_speed = SPEED_UNKNOWN;
14306 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000014307
14308 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014309}
14310
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014311static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070014312{
14313 u32 hw_phy_id_1, hw_phy_id_2;
14314 u32 hw_phy_id, hw_phy_id_masked;
14315 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014316
Matt Carlsone256f8a2011-03-09 16:58:24 +000014317 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000014318 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000014319 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
14320
Michael Chan8151ad52012-07-29 19:15:41 +000014321 if (tg3_flag(tp, ENABLE_APE)) {
14322 switch (tp->pci_fn) {
14323 case 0:
14324 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
14325 break;
14326 case 1:
14327 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
14328 break;
14329 case 2:
14330 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
14331 break;
14332 case 3:
14333 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
14334 break;
14335 }
14336 }
14337
Joe Perches63c3a662011-04-26 08:12:10 +000014338 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014339 return tg3_phy_init(tp);
14340
Linus Torvalds1da177e2005-04-16 15:20:36 -070014341 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010014342 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014343 */
14344 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000014345 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000014346 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014347 } else {
14348 /* Now read the physical PHY_ID from the chip and verify
14349 * that it is sane. If it doesn't look good, we fall back
14350 * to either the hard-coded table based PHY_ID and failing
14351 * that the value found in the eeprom area.
14352 */
14353 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
14354 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
14355
14356 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
14357 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
14358 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
14359
Matt Carlson79eb6902010-02-17 15:17:03 +000014360 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014361 }
14362
Matt Carlson79eb6902010-02-17 15:17:03 +000014363 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014364 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000014365 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014366 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070014367 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014368 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014369 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000014370 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070014371 /* Do nothing, phy ID already set up in
14372 * tg3_get_eeprom_hw_cfg().
14373 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014374 } else {
14375 struct subsys_tbl_ent *p;
14376
14377 /* No eeprom signature? Try the hardcoded
14378 * subsys device table.
14379 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014380 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014381 if (!p)
14382 return -ENODEV;
14383
14384 tp->phy_id = p->phy_id;
14385 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000014386 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014387 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014388 }
14389 }
14390
Matt Carlsona6b68da2010-12-06 08:28:52 +000014391 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000014392 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14393 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000014394 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762 ||
Matt Carlson5baa5e92011-07-20 10:20:53 +000014395 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000014396 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
14397 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
14398 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000014399 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
14400
Matt Carlsone256f8a2011-03-09 16:58:24 +000014401 tg3_phy_init_link_config(tp);
14402
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014403 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014404 !tg3_flag(tp, ENABLE_APE) &&
14405 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014406 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014407
14408 tg3_readphy(tp, MII_BMSR, &bmsr);
14409 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
14410 (bmsr & BMSR_LSTATUS))
14411 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014412
Linus Torvalds1da177e2005-04-16 15:20:36 -070014413 err = tg3_phy_reset(tp);
14414 if (err)
14415 return err;
14416
Matt Carlson42b64a42011-05-19 12:12:49 +000014417 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014418
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014419 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000014420 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
14421 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014422
14423 tg3_writephy(tp, MII_BMCR,
14424 BMCR_ANENABLE | BMCR_ANRESTART);
14425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014426 }
14427
14428skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000014429 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014430 err = tg3_init_5401phy_dsp(tp);
14431 if (err)
14432 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014433
Linus Torvalds1da177e2005-04-16 15:20:36 -070014434 err = tg3_init_5401phy_dsp(tp);
14435 }
14436
Linus Torvalds1da177e2005-04-16 15:20:36 -070014437 return err;
14438}
14439
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014440static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014441{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014442 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000014443 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000014444 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000014445 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014446
Matt Carlson535a4902011-07-20 10:20:56 +000014447 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014448 if (!vpd_data)
14449 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014450
Matt Carlson535a4902011-07-20 10:20:56 +000014451 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000014452 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014453 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000014454
14455 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
14456 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
14457 i += PCI_VPD_LRDT_TAG_SIZE;
14458
Matt Carlson535a4902011-07-20 10:20:56 +000014459 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000014460 goto out_not_found;
14461
Matt Carlson184b8902010-04-05 10:19:25 +000014462 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14463 PCI_VPD_RO_KEYWORD_MFR_ID);
14464 if (j > 0) {
14465 len = pci_vpd_info_field_size(&vpd_data[j]);
14466
14467 j += PCI_VPD_INFO_FLD_HDR_SIZE;
14468 if (j + len > block_end || len != 4 ||
14469 memcmp(&vpd_data[j], "1028", 4))
14470 goto partno;
14471
14472 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14473 PCI_VPD_RO_KEYWORD_VENDOR0);
14474 if (j < 0)
14475 goto partno;
14476
14477 len = pci_vpd_info_field_size(&vpd_data[j]);
14478
14479 j += PCI_VPD_INFO_FLD_HDR_SIZE;
14480 if (j + len > block_end)
14481 goto partno;
14482
14483 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000014484 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000014485 }
14486
14487partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000014488 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14489 PCI_VPD_RO_KEYWORD_PARTNO);
14490 if (i < 0)
14491 goto out_not_found;
14492
14493 len = pci_vpd_info_field_size(&vpd_data[i]);
14494
14495 i += PCI_VPD_INFO_FLD_HDR_SIZE;
14496 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000014497 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000014498 goto out_not_found;
14499
14500 memcpy(tp->board_part_number, &vpd_data[i], len);
14501
Linus Torvalds1da177e2005-04-16 15:20:36 -070014502out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014503 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000014504 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014505 return;
14506
14507out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000014508 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000014509 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
14510 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000014511 strcpy(tp->board_part_number, "BCM5717");
14512 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
14513 strcpy(tp->board_part_number, "BCM5718");
14514 else
14515 goto nomatch;
14516 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
14517 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
14518 strcpy(tp->board_part_number, "BCM57780");
14519 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
14520 strcpy(tp->board_part_number, "BCM57760");
14521 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
14522 strcpy(tp->board_part_number, "BCM57790");
14523 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
14524 strcpy(tp->board_part_number, "BCM57788");
14525 else
14526 goto nomatch;
14527 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
14528 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
14529 strcpy(tp->board_part_number, "BCM57761");
14530 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
14531 strcpy(tp->board_part_number, "BCM57765");
14532 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
14533 strcpy(tp->board_part_number, "BCM57781");
14534 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
14535 strcpy(tp->board_part_number, "BCM57785");
14536 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
14537 strcpy(tp->board_part_number, "BCM57791");
14538 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
14539 strcpy(tp->board_part_number, "BCM57795");
14540 else
14541 goto nomatch;
Matt Carlson55086ad2011-12-14 11:09:59 +000014542 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766) {
14543 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
14544 strcpy(tp->board_part_number, "BCM57762");
14545 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
14546 strcpy(tp->board_part_number, "BCM57766");
14547 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
14548 strcpy(tp->board_part_number, "BCM57782");
14549 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
14550 strcpy(tp->board_part_number, "BCM57786");
14551 else
14552 goto nomatch;
Matt Carlson37a949c2010-09-30 10:34:33 +000014553 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070014554 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000014555 } else {
14556nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070014557 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000014558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014559}
14560
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014561static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070014562{
14563 u32 val;
14564
Matt Carlsone4f34112009-02-25 14:25:00 +000014565 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014566 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000014567 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014568 val != 0)
14569 return 0;
14570
14571 return 1;
14572}
14573
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014574static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000014575{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014576 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000014577 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014578 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014579
14580 if (tg3_nvram_read(tp, 0xc, &offset) ||
14581 tg3_nvram_read(tp, 0x4, &start))
14582 return;
14583
14584 offset = tg3_nvram_logical_addr(tp, offset);
14585
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014586 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000014587 return;
14588
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014589 if ((val & 0xfc000000) == 0x0c000000) {
14590 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000014591 return;
14592
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014593 if (val == 0)
14594 newver = true;
14595 }
14596
Matt Carlson75f99362010-04-05 10:19:24 +000014597 dst_off = strlen(tp->fw_ver);
14598
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014599 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000014600 if (TG3_VER_SIZE - dst_off < 16 ||
14601 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014602 return;
14603
14604 offset = offset + ver_offset - start;
14605 for (i = 0; i < 16; i += 4) {
14606 __be32 v;
14607 if (tg3_nvram_read_be32(tp, offset + i, &v))
14608 return;
14609
Matt Carlson75f99362010-04-05 10:19:24 +000014610 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014611 }
14612 } else {
14613 u32 major, minor;
14614
14615 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
14616 return;
14617
14618 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
14619 TG3_NVM_BCVER_MAJSFT;
14620 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000014621 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
14622 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000014623 }
14624}
14625
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014626static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000014627{
14628 u32 val, major, minor;
14629
14630 /* Use native endian representation */
14631 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
14632 return;
14633
14634 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
14635 TG3_NVM_HWSB_CFG1_MAJSFT;
14636 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
14637 TG3_NVM_HWSB_CFG1_MINSFT;
14638
14639 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
14640}
14641
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014642static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080014643{
14644 u32 offset, major, minor, build;
14645
Matt Carlson75f99362010-04-05 10:19:24 +000014646 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080014647
14648 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
14649 return;
14650
14651 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
14652 case TG3_EEPROM_SB_REVISION_0:
14653 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
14654 break;
14655 case TG3_EEPROM_SB_REVISION_2:
14656 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
14657 break;
14658 case TG3_EEPROM_SB_REVISION_3:
14659 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
14660 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000014661 case TG3_EEPROM_SB_REVISION_4:
14662 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
14663 break;
14664 case TG3_EEPROM_SB_REVISION_5:
14665 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
14666 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000014667 case TG3_EEPROM_SB_REVISION_6:
14668 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
14669 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080014670 default:
14671 return;
14672 }
14673
Matt Carlsone4f34112009-02-25 14:25:00 +000014674 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080014675 return;
14676
14677 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
14678 TG3_EEPROM_SB_EDH_BLD_SHFT;
14679 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
14680 TG3_EEPROM_SB_EDH_MAJ_SHFT;
14681 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
14682
14683 if (minor > 99 || build > 26)
14684 return;
14685
Matt Carlson75f99362010-04-05 10:19:24 +000014686 offset = strlen(tp->fw_ver);
14687 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
14688 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080014689
14690 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000014691 offset = strlen(tp->fw_ver);
14692 if (offset < TG3_VER_SIZE - 1)
14693 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080014694 }
14695}
14696
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014697static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080014698{
14699 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014700 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070014701
14702 for (offset = TG3_NVM_DIR_START;
14703 offset < TG3_NVM_DIR_END;
14704 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000014705 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014706 return;
14707
14708 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
14709 break;
14710 }
14711
14712 if (offset == TG3_NVM_DIR_END)
14713 return;
14714
Joe Perches63c3a662011-04-26 08:12:10 +000014715 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014716 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000014717 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014718 return;
14719
Matt Carlsone4f34112009-02-25 14:25:00 +000014720 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014721 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000014722 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014723 return;
14724
14725 offset += val - start;
14726
Matt Carlsonacd9c112009-02-25 14:26:33 +000014727 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014728
Matt Carlsonacd9c112009-02-25 14:26:33 +000014729 tp->fw_ver[vlen++] = ',';
14730 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070014731
14732 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000014733 __be32 v;
14734 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014735 return;
14736
Al Virob9fc7dc2007-12-17 22:59:57 -080014737 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014738
Matt Carlsonacd9c112009-02-25 14:26:33 +000014739 if (vlen > TG3_VER_SIZE - sizeof(v)) {
14740 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014741 break;
14742 }
14743
Matt Carlsonacd9c112009-02-25 14:26:33 +000014744 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
14745 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014746 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000014747}
14748
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014749static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000014750{
Matt Carlson7fd76442009-02-25 14:27:20 +000014751 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000014752
14753 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
14754 if (apedata != APE_SEG_SIG_MAGIC)
14755 return;
14756
14757 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
14758 if (!(apedata & APE_FW_STATUS_READY))
14759 return;
14760
Michael Chan165f4d12012-07-16 16:23:59 +000014761 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
14762 tg3_flag_set(tp, APE_HAS_NCSI);
14763}
14764
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014765static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000014766{
14767 int vlen;
14768 u32 apedata;
14769 char *fwtype;
14770
Matt Carlson7fd76442009-02-25 14:27:20 +000014771 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
14772
Michael Chan165f4d12012-07-16 16:23:59 +000014773 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000014774 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000014775 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
14776 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000014777 else
Matt Carlsonecc79642010-08-02 11:26:01 +000014778 fwtype = "DASH";
14779
Matt Carlson7fd76442009-02-25 14:27:20 +000014780 vlen = strlen(tp->fw_ver);
14781
Matt Carlsonecc79642010-08-02 11:26:01 +000014782 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
14783 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000014784 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
14785 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
14786 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
14787 (apedata & APE_FW_VERSION_BLDMSK));
14788}
14789
Michael Chanc86a8562013-01-06 12:51:08 +000014790static void tg3_read_otp_ver(struct tg3 *tp)
14791{
14792 u32 val, val2;
14793
14794 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5762)
14795 return;
14796
14797 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
14798 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
14799 TG3_OTP_MAGIC0_VALID(val)) {
14800 u64 val64 = (u64) val << 32 | val2;
14801 u32 ver = 0;
14802 int i, vlen;
14803
14804 for (i = 0; i < 7; i++) {
14805 if ((val64 & 0xff) == 0)
14806 break;
14807 ver = val64 & 0xff;
14808 val64 >>= 8;
14809 }
14810 vlen = strlen(tp->fw_ver);
14811 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
14812 }
14813}
14814
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014815static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000014816{
14817 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000014818 bool vpd_vers = false;
14819
14820 if (tp->fw_ver[0] != 0)
14821 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014822
Joe Perches63c3a662011-04-26 08:12:10 +000014823 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000014824 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000014825 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000014826 return;
14827 }
14828
Matt Carlsonacd9c112009-02-25 14:26:33 +000014829 if (tg3_nvram_read(tp, 0, &val))
14830 return;
14831
14832 if (val == TG3_EEPROM_MAGIC)
14833 tg3_read_bc_ver(tp);
14834 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
14835 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000014836 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
14837 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000014838
Michael Chan165f4d12012-07-16 16:23:59 +000014839 if (tg3_flag(tp, ENABLE_ASF)) {
14840 if (tg3_flag(tp, ENABLE_APE)) {
14841 tg3_probe_ncsi(tp);
14842 if (!vpd_vers)
14843 tg3_read_dash_ver(tp);
14844 } else if (!vpd_vers) {
14845 tg3_read_mgmtfw_ver(tp);
14846 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000014847 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070014848
14849 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080014850}
14851
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014852static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
14853{
Joe Perches63c3a662011-04-26 08:12:10 +000014854 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000014855 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000014856 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000014857 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014858 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000014859 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014860}
14861
Matt Carlson41434702011-03-09 16:58:22 +000014862static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014863 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
14864 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
14865 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
14866 { },
14867};
14868
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014869static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000014870{
14871 struct pci_dev *peer;
14872 unsigned int func, devnr = tp->pdev->devfn & ~7;
14873
14874 for (func = 0; func < 8; func++) {
14875 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14876 if (peer && peer != tp->pdev)
14877 break;
14878 pci_dev_put(peer);
14879 }
14880 /* 5704 can be configured in single-port mode, set peer to
14881 * tp->pdev in that case.
14882 */
14883 if (!peer) {
14884 peer = tp->pdev;
14885 return peer;
14886 }
14887
14888 /*
14889 * We don't need to keep the refcount elevated; there's no way
14890 * to remove one half of this device without removing the other
14891 */
14892 pci_dev_put(peer);
14893
14894 return peer;
14895}
14896
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014897static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000014898{
14899 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
14900 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
14901 u32 reg;
14902
14903 /* All devices that use the alternate
14904 * ASIC REV location have a CPMU.
14905 */
14906 tg3_flag_set(tp, CPMU_PRESENT);
14907
14908 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000014909 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000014910 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
14911 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000014912 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
14913 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
14914 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
14915 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727)
Matt Carlson42b123b2012-02-13 15:20:13 +000014916 reg = TG3PCI_GEN2_PRODID_ASICREV;
14917 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
14918 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
14919 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
14920 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
14921 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14922 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
14923 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
14924 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
14925 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
14926 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
14927 reg = TG3PCI_GEN15_PRODID_ASICREV;
14928 else
14929 reg = TG3PCI_PRODID_ASICREV;
14930
14931 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
14932 }
14933
14934 /* Wrong chip ID in 5752 A0. This code can be removed later
14935 * as A0 is not in production.
14936 */
14937 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
14938 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
14939
Michael Chan79d49692012-11-05 14:26:29 +000014940 if (tp->pci_chip_rev_id == CHIPREV_ID_5717_C0)
14941 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
14942
Matt Carlson42b123b2012-02-13 15:20:13 +000014943 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14944 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14945 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14946 tg3_flag_set(tp, 5717_PLUS);
14947
14948 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
14949 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
14950 tg3_flag_set(tp, 57765_CLASS);
14951
Michael Chanc65a17f2013-01-06 12:51:07 +000014952 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
14953 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000014954 tg3_flag_set(tp, 57765_PLUS);
14955
14956 /* Intentionally exclude ASIC_REV_5906 */
14957 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
14958 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
14959 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14960 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
14961 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14962 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14963 tg3_flag(tp, 57765_PLUS))
14964 tg3_flag_set(tp, 5755_PLUS);
14965
14966 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
14967 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
14968 tg3_flag_set(tp, 5780_CLASS);
14969
14970 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14971 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14972 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
14973 tg3_flag(tp, 5755_PLUS) ||
14974 tg3_flag(tp, 5780_CLASS))
14975 tg3_flag_set(tp, 5750_PLUS);
14976
14977 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14978 tg3_flag(tp, 5750_PLUS))
14979 tg3_flag_set(tp, 5705_PLUS);
14980}
14981
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000014982static bool tg3_10_100_only_device(struct tg3 *tp,
14983 const struct pci_device_id *ent)
14984{
14985 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
14986
14987 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14988 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14989 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14990 return true;
14991
14992 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
14993 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
14994 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
14995 return true;
14996 } else {
14997 return true;
14998 }
14999 }
15000
15001 return false;
15002}
15003
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000015004static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015005{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015006 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015007 u32 pci_state_reg, grc_misc_cfg;
15008 u32 val;
15009 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015010 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015011
Linus Torvalds1da177e2005-04-16 15:20:36 -070015012 /* Force memory write invalidate off. If we leave it on,
15013 * then on 5700_BX chips we have to enable a workaround.
15014 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
15015 * to match the cacheline size. The Broadcom driver have this
15016 * workaround but turns MWI off all the times so never uses
15017 * it. This seems to suggest that the workaround is insufficient.
15018 */
15019 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15020 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
15021 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15022
Matt Carlson16821282011-07-13 09:27:28 +000015023 /* Important! -- Make sure register accesses are byteswapped
15024 * correctly. Also, for those chips that require it, make
15025 * sure that indirect register accesses are enabled before
15026 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015027 */
15028 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15029 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000015030 tp->misc_host_ctrl |= (misc_ctrl_reg &
15031 MISC_HOST_CTRL_CHIPREV);
15032 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15033 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015034
Matt Carlson42b123b2012-02-13 15:20:13 +000015035 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070015036
Michael Chan68929142005-08-09 20:17:14 -070015037 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
15038 * we need to disable memory and use config. cycles
15039 * only to access all registers. The 5702/03 chips
15040 * can mistakenly decode the special cycles from the
15041 * ICH chipsets as memory write cycles, causing corruption
15042 * of register and memory space. Only certain ICH bridges
15043 * will drive special cycles with non-zero data during the
15044 * address phase which can fall within the 5703's address
15045 * range. This is not an ICH bug as the PCI spec allows
15046 * non-zero address during special cycles. However, only
15047 * these ICH bridges are known to drive non-zero addresses
15048 * during special cycles.
15049 *
15050 * Since special cycles do not cross PCI bridges, we only
15051 * enable this workaround if the 5703 is on the secondary
15052 * bus of these ICH bridges.
15053 */
15054 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
15055 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
15056 static struct tg3_dev_id {
15057 u32 vendor;
15058 u32 device;
15059 u32 rev;
15060 } ich_chipsets[] = {
15061 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
15062 PCI_ANY_ID },
15063 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
15064 PCI_ANY_ID },
15065 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
15066 0xa },
15067 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
15068 PCI_ANY_ID },
15069 { },
15070 };
15071 struct tg3_dev_id *pci_id = &ich_chipsets[0];
15072 struct pci_dev *bridge = NULL;
15073
15074 while (pci_id->vendor != 0) {
15075 bridge = pci_get_device(pci_id->vendor, pci_id->device,
15076 bridge);
15077 if (!bridge) {
15078 pci_id++;
15079 continue;
15080 }
15081 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070015082 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070015083 continue;
15084 }
15085 if (bridge->subordinate &&
15086 (bridge->subordinate->number ==
15087 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015088 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070015089 pci_dev_put(bridge);
15090 break;
15091 }
15092 }
15093 }
15094
Matt Carlson6ff6f812011-05-19 12:12:54 +000015095 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070015096 static struct tg3_dev_id {
15097 u32 vendor;
15098 u32 device;
15099 } bridge_chipsets[] = {
15100 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
15101 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
15102 { },
15103 };
15104 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
15105 struct pci_dev *bridge = NULL;
15106
15107 while (pci_id->vendor != 0) {
15108 bridge = pci_get_device(pci_id->vendor,
15109 pci_id->device,
15110 bridge);
15111 if (!bridge) {
15112 pci_id++;
15113 continue;
15114 }
15115 if (bridge->subordinate &&
15116 (bridge->subordinate->number <=
15117 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015118 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070015119 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015120 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070015121 pci_dev_put(bridge);
15122 break;
15123 }
15124 }
15125 }
15126
Michael Chan4a29cc22006-03-19 13:21:12 -080015127 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
15128 * DMA addresses > 40-bit. This bridge may have other additional
15129 * 57xx devices behind it in some 4-port NIC designs for example.
15130 * Any tg3 device found behind the bridge will also need the 40-bit
15131 * DMA workaround.
15132 */
Matt Carlson42b123b2012-02-13 15:20:13 +000015133 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015134 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070015135 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000015136 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080015137 struct pci_dev *bridge = NULL;
15138
15139 do {
15140 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
15141 PCI_DEVICE_ID_SERVERWORKS_EPB,
15142 bridge);
15143 if (bridge && bridge->subordinate &&
15144 (bridge->subordinate->number <=
15145 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015146 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080015147 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015148 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080015149 pci_dev_put(bridge);
15150 break;
15151 }
15152 } while (bridge);
15153 }
Michael Chan4cf78e42005-07-25 12:29:19 -070015154
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015155 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000015156 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070015157 tp->pdev_peer = tg3_find_peer(tp);
15158
Matt Carlson507399f2009-11-13 13:03:37 +000015159 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000015160 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000015161 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000015162 else if (tg3_flag(tp, 57765_PLUS))
15163 tg3_flag_set(tp, HW_TSO_3);
15164 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015165 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015166 tg3_flag_set(tp, HW_TSO_2);
15167 else if (tg3_flag(tp, 5750_PLUS)) {
15168 tg3_flag_set(tp, HW_TSO_1);
15169 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000015170 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
15171 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000015172 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000015173 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15174 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
15175 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000015176 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000015177 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
15178 tp->fw_needed = FIRMWARE_TG3TSO5;
15179 else
15180 tp->fw_needed = FIRMWARE_TG3TSO;
15181 }
15182
Matt Carlsondabc5c62011-05-19 12:12:52 +000015183 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000015184 if (tg3_flag(tp, HW_TSO_1) ||
15185 tg3_flag(tp, HW_TSO_2) ||
15186 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015187 tp->fw_needed) {
15188 /* For firmware TSO, assume ASF is disabled.
15189 * We'll disable TSO later if we discover ASF
15190 * is enabled in tg3_get_eeprom_hw_cfg().
15191 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000015192 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015193 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000015194 tg3_flag_clear(tp, TSO_CAPABLE);
15195 tg3_flag_clear(tp, TSO_BUG);
15196 tp->fw_needed = NULL;
15197 }
15198
15199 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
15200 tp->fw_needed = FIRMWARE_TG3;
15201
Matt Carlson507399f2009-11-13 13:03:37 +000015202 tp->irq_max = 1;
15203
Joe Perches63c3a662011-04-26 08:12:10 +000015204 if (tg3_flag(tp, 5750_PLUS)) {
15205 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070015206 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
15207 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
15208 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
15209 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
15210 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000015211 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070015212
Joe Perches63c3a662011-04-26 08:12:10 +000015213 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070015214 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015215 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070015216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015217
Joe Perches63c3a662011-04-26 08:12:10 +000015218 if (tg3_flag(tp, 57765_PLUS)) {
15219 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000015220 tp->irq_max = TG3_IRQ_MAX_VECS;
15221 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015222 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000015223
Michael Chan91024262012-09-28 07:12:38 +000015224 tp->txq_max = 1;
15225 tp->rxq_max = 1;
15226 if (tp->irq_max > 1) {
15227 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
15228 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
15229
15230 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
15231 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
15232 tp->txq_max = tp->irq_max - 1;
15233 }
15234
Matt Carlsonb7abee62012-06-07 12:56:54 +000015235 if (tg3_flag(tp, 5755_PLUS) ||
15236 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015237 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015238
Matt Carlsone31aa982011-07-27 14:20:53 +000015239 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000015240 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000015241
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000015242 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
15243 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000015244 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
15245 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000015246 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000015247
Joe Perches63c3a662011-04-26 08:12:10 +000015248 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000015249 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000015250 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000015251
Joe Perches63c3a662011-04-26 08:12:10 +000015252 if (!tg3_flag(tp, 5705_PLUS) ||
15253 tg3_flag(tp, 5780_CLASS) ||
15254 tg3_flag(tp, USE_JUMBO_BDFLAG))
15255 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070015256
Matt Carlson52f44902008-11-21 17:17:04 -080015257 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15258 &pci_state_reg);
15259
Jon Mason708ebb3a2011-06-27 12:56:50 +000015260 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015261 u16 lnkctl;
15262
Joe Perches63c3a662011-04-26 08:12:10 +000015263 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080015264
Jiang Liu0f49bfb2012-08-20 13:28:20 -060015265 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015266 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000015267 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
15268 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015269 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000015270 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000015271 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015272 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080015273 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000015274 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
15275 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000015276 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000015277 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000015278 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080015279 }
Matt Carlson52f44902008-11-21 17:17:04 -080015280 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000015281 /* BCM5785 devices are effectively PCIe devices, and should
15282 * follow PCIe codepaths, but do not have a PCIe capabilities
15283 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000015284 */
Joe Perches63c3a662011-04-26 08:12:10 +000015285 tg3_flag_set(tp, PCI_EXPRESS);
15286 } else if (!tg3_flag(tp, 5705_PLUS) ||
15287 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080015288 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
15289 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000015290 dev_err(&tp->pdev->dev,
15291 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080015292 return -EIO;
15293 }
15294
15295 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000015296 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080015297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015298
Michael Chan399de502005-10-03 14:02:39 -070015299 /* If we have an AMD 762 or VIA K8T800 chipset, write
15300 * reordering to the mailbox registers done by the host
15301 * controller can cause major troubles. We read back from
15302 * every mailbox register write to force the writes to be
15303 * posted to the chip in order.
15304 */
Matt Carlson41434702011-03-09 16:58:22 +000015305 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015306 !tg3_flag(tp, PCI_EXPRESS))
15307 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070015308
Matt Carlson69fc4052008-12-21 20:19:57 -080015309 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
15310 &tp->pci_cacheline_sz);
15311 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15312 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015313 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
15314 tp->pci_lat_timer < 64) {
15315 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080015316 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15317 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015318 }
15319
Matt Carlson16821282011-07-13 09:27:28 +000015320 /* Important! -- It is critical that the PCI-X hw workaround
15321 * situation is decided before the first MMIO register access.
15322 */
Matt Carlson52f44902008-11-21 17:17:04 -080015323 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
15324 /* 5700 BX chips need to have their TX producer index
15325 * mailboxes written twice to workaround a bug.
15326 */
Joe Perches63c3a662011-04-26 08:12:10 +000015327 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070015328
Matt Carlson52f44902008-11-21 17:17:04 -080015329 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015330 *
15331 * The workaround is to use indirect register accesses
15332 * for all chip writes not to mailbox registers.
15333 */
Joe Perches63c3a662011-04-26 08:12:10 +000015334 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015335 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015336
Joe Perches63c3a662011-04-26 08:12:10 +000015337 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015338
15339 /* The chip can have it's power management PCI config
15340 * space registers clobbered due to this bug.
15341 * So explicitly force the chip into D0 here.
15342 */
Matt Carlson9974a352007-10-07 23:27:28 -070015343 pci_read_config_dword(tp->pdev,
15344 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015345 &pm_reg);
15346 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
15347 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070015348 pci_write_config_dword(tp->pdev,
15349 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015350 pm_reg);
15351
15352 /* Also, force SERR#/PERR# in PCI command. */
15353 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15354 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
15355 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15356 }
15357 }
15358
Linus Torvalds1da177e2005-04-16 15:20:36 -070015359 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000015360 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015361 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000015362 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015363
15364 /* Chip-specific fixup from Broadcom driver */
15365 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
15366 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
15367 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
15368 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
15369 }
15370
Michael Chan1ee582d2005-08-09 20:16:46 -070015371 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070015372 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070015373 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070015374 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070015375 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070015376 tp->write32_tx_mbox = tg3_write32;
15377 tp->write32_rx_mbox = tg3_write32;
15378
15379 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000015380 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070015381 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070015382 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015383 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070015384 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
15385 /*
15386 * Back to back register writes can cause problems on these
15387 * chips, the workaround is to read back all reg writes
15388 * except those to mailbox regs.
15389 *
15390 * See tg3_write_indirect_reg32().
15391 */
Michael Chan1ee582d2005-08-09 20:16:46 -070015392 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070015393 }
15394
Joe Perches63c3a662011-04-26 08:12:10 +000015395 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070015396 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000015397 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070015398 tp->write32_rx_mbox = tg3_write_flush_reg32;
15399 }
Michael Chan20094932005-08-09 20:16:32 -070015400
Joe Perches63c3a662011-04-26 08:12:10 +000015401 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070015402 tp->read32 = tg3_read_indirect_reg32;
15403 tp->write32 = tg3_write_indirect_reg32;
15404 tp->read32_mbox = tg3_read_indirect_mbox;
15405 tp->write32_mbox = tg3_write_indirect_mbox;
15406 tp->write32_tx_mbox = tg3_write_indirect_mbox;
15407 tp->write32_rx_mbox = tg3_write_indirect_mbox;
15408
15409 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015410 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015411
15412 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15413 pci_cmd &= ~PCI_COMMAND_MEMORY;
15414 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15415 }
Michael Chanb5d37722006-09-27 16:06:21 -070015416 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15417 tp->read32_mbox = tg3_read32_mbox_5906;
15418 tp->write32_mbox = tg3_write32_mbox_5906;
15419 tp->write32_tx_mbox = tg3_write32_mbox_5906;
15420 tp->write32_rx_mbox = tg3_write32_mbox_5906;
15421 }
Michael Chan68929142005-08-09 20:17:14 -070015422
Michael Chanbbadf502006-04-06 21:46:34 -070015423 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015424 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070015425 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070015426 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000015427 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070015428
Matt Carlson16821282011-07-13 09:27:28 +000015429 /* The memory arbiter has to be enabled in order for SRAM accesses
15430 * to succeed. Normally on powerup the tg3 chip firmware will make
15431 * sure it is enabled, but other entities such as system netboot
15432 * code might disable it.
15433 */
15434 val = tr32(MEMARB_MODE);
15435 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
15436
Matt Carlson9dc5e342011-11-04 09:15:02 +000015437 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
15438 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
15439 tg3_flag(tp, 5780_CLASS)) {
15440 if (tg3_flag(tp, PCIX_MODE)) {
15441 pci_read_config_dword(tp->pdev,
15442 tp->pcix_cap + PCI_X_STATUS,
15443 &val);
15444 tp->pci_fn = val & 0x7;
15445 }
Michael Chan857001f2013-01-06 12:51:09 +000015446 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
15447 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000015448 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
15449 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000015450 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
15451 val = tr32(TG3_CPMU_STATUS);
15452
15453 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
15454 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
15455 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000015456 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
15457 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000015458 }
15459
Michael Chan7d0c41e2005-04-21 17:06:20 -070015460 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000015461 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070015462 * determined before calling tg3_set_power_state() so that
15463 * we know whether or not to switch out of Vaux power.
15464 * When the flag is set, it means that GPIO1 is used for eeprom
15465 * write protect and also implies that it is a LOM where GPIOs
15466 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015467 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070015468 tg3_get_eeprom_hw_cfg(tp);
15469
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015470 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
15471 tg3_flag_clear(tp, TSO_CAPABLE);
15472 tg3_flag_clear(tp, TSO_BUG);
15473 tp->fw_needed = NULL;
15474 }
15475
Joe Perches63c3a662011-04-26 08:12:10 +000015476 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070015477 /* Allow reads and writes to the
15478 * APE register and memory space.
15479 */
15480 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000015481 PCISTATE_ALLOW_APE_SHMEM_WR |
15482 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015483 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
15484 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000015485
15486 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015487 }
15488
Matt Carlson16821282011-07-13 09:27:28 +000015489 /* Set up tp->grc_local_ctrl before calling
15490 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
15491 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070015492 * It is also used as eeprom write protect on LOMs.
15493 */
15494 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000015495 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015496 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070015497 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
15498 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070015499 /* Unused GPIO3 must be driven as output on 5752 because there
15500 * are no pull-up resistors on unused GPIO pins.
15501 */
15502 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
15503 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070015504
Matt Carlson321d32a2008-11-21 17:22:19 -080015505 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000015506 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000015507 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080015508 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
15509
Matt Carlson8d519ab2009-04-20 06:58:01 +000015510 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15511 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070015512 /* Turn off the debug UART. */
15513 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000015514 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070015515 /* Keep VMain power. */
15516 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
15517 GRC_LCLCTRL_GPIO_OUTPUT0;
15518 }
15519
Michael Chanc86a8562013-01-06 12:51:08 +000015520 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762)
15521 tp->grc_local_ctrl |=
15522 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
15523
Matt Carlson16821282011-07-13 09:27:28 +000015524 /* Switch out of Vaux if it is a NIC */
15525 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015526
Linus Torvalds1da177e2005-04-16 15:20:36 -070015527 /* Derive initial jumbo mode from MTU assigned in
15528 * ether_setup() via the alloc_etherdev() call
15529 */
Joe Perches63c3a662011-04-26 08:12:10 +000015530 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
15531 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015532
15533 /* Determine WakeOnLan speed to use. */
15534 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15535 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
15536 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
15537 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000015538 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015539 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015540 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015541 }
15542
Matt Carlson7f97a4b2009-08-25 10:10:03 +000015543 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015544 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000015545
Linus Torvalds1da177e2005-04-16 15:20:36 -070015546 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000015547 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15548 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015549 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070015550 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015551 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
15552 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15553 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015554
15555 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
15556 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015557 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015558 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015559 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015560
Joe Perches63c3a662011-04-26 08:12:10 +000015561 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015562 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080015563 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015564 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015565 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070015566 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070015567 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070015568 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
15569 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080015570 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
15571 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015572 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080015573 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015574 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080015575 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015576 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070015577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015578
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015579 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15580 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
15581 tp->phy_otp = tg3_read_otp_phycfg(tp);
15582 if (tp->phy_otp == 0)
15583 tp->phy_otp = TG3_OTP_DEFAULT;
15584 }
15585
Joe Perches63c3a662011-04-26 08:12:10 +000015586 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070015587 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
15588 else
15589 tp->mi_mode = MAC_MI_MODE_BASE;
15590
Linus Torvalds1da177e2005-04-16 15:20:36 -070015591 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015592 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
15593 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
15594 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
15595
Matt Carlson4d958472011-04-20 07:57:35 +000015596 /* Set these bits to enable statistics workaround. */
15597 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
15598 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
15599 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
15600 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
15601 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
15602 }
15603
Matt Carlson321d32a2008-11-21 17:22:19 -080015604 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
15605 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000015606 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070015607
Matt Carlson158d7ab2008-05-29 01:37:54 -070015608 err = tg3_mdio_init(tp);
15609 if (err)
15610 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015611
15612 /* Initialize data/descriptor byte/word swapping. */
15613 val = tr32(GRC_MODE);
Michael Chanc65a17f2013-01-06 12:51:07 +000015614 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
15615 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000015616 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
15617 GRC_MODE_WORD_SWAP_B2HRX_DATA |
15618 GRC_MODE_B2HRX_ENABLE |
15619 GRC_MODE_HTX2B_ENABLE |
15620 GRC_MODE_HOST_STACKUP);
15621 else
15622 val &= GRC_MODE_HOST_STACKUP;
15623
Linus Torvalds1da177e2005-04-16 15:20:36 -070015624 tw32(GRC_MODE, val | tp->grc_mode);
15625
15626 tg3_switch_clocks(tp);
15627
15628 /* Clear this out for sanity. */
15629 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
15630
15631 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15632 &pci_state_reg);
15633 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015634 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015635 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
15636
15637 if (chiprevid == CHIPREV_ID_5701_A0 ||
15638 chiprevid == CHIPREV_ID_5701_B0 ||
15639 chiprevid == CHIPREV_ID_5701_B2 ||
15640 chiprevid == CHIPREV_ID_5701_B5) {
15641 void __iomem *sram_base;
15642
15643 /* Write some dummy words into the SRAM status block
15644 * area, see if it reads back correctly. If the return
15645 * value is bad, force enable the PCIX workaround.
15646 */
15647 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
15648
15649 writel(0x00000000, sram_base);
15650 writel(0x00000000, sram_base + 4);
15651 writel(0xffffffff, sram_base + 4);
15652 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000015653 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015654 }
15655 }
15656
15657 udelay(50);
15658 tg3_nvram_init(tp);
15659
15660 grc_misc_cfg = tr32(GRC_MISC_CFG);
15661 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
15662
Linus Torvalds1da177e2005-04-16 15:20:36 -070015663 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
15664 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
15665 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000015666 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015667
Joe Perches63c3a662011-04-26 08:12:10 +000015668 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000015669 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000015670 tg3_flag_set(tp, TAGGED_STATUS);
15671 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070015672 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
15673 HOSTCC_MODE_CLRTICK_TXBD);
15674
15675 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
15676 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15677 tp->misc_host_ctrl);
15678 }
15679
Matt Carlson3bda1252008-08-15 14:08:22 -070015680 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000015681 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000015682 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070015683 else
Matt Carlson6e01b202011-08-19 13:58:20 +000015684 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070015685
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015686 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015687 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015688
15689 err = tg3_phy_probe(tp);
15690 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015691 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015692 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015693 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015694 }
15695
Matt Carlson184b8902010-04-05 10:19:25 +000015696 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080015697 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015698
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015699 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
15700 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015701 } else {
15702 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015703 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015704 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015705 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015706 }
15707
15708 /* 5700 {AX,BX} chips have a broken status block link
15709 * change bit implementation, so we must use the
15710 * status register in those cases.
15711 */
15712 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000015713 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015714 else
Joe Perches63c3a662011-04-26 08:12:10 +000015715 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015716
15717 /* The led_ctrl is set during tg3_phy_probe, here we might
15718 * have to force the link status polling mechanism based
15719 * upon subsystem IDs.
15720 */
15721 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070015722 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015723 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
15724 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000015725 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015726 }
15727
15728 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015729 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000015730 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015731 else
Joe Perches63c3a662011-04-26 08:12:10 +000015732 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015733
Eric Dumazet9205fd92011-11-18 06:47:01 +000015734 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015735 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015736 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015737 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000015738 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015739#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000015740 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015741#endif
15742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015743
Matt Carlson2c49a442010-09-30 10:34:35 +000015744 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
15745 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015746 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
15747
Matt Carlson2c49a442010-09-30 10:34:35 +000015748 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070015749
15750 /* Increment the rx prod index on the rx std ring by at most
15751 * 8 for these chips to workaround hw errata.
15752 */
15753 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
15754 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
15755 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
15756 tp->rx_std_max_post = 8;
15757
Joe Perches63c3a662011-04-26 08:12:10 +000015758 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070015759 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
15760 PCIE_PWR_MGMT_L1_THRESH_MSK;
15761
Linus Torvalds1da177e2005-04-16 15:20:36 -070015762 return err;
15763}
15764
David S. Miller49b6e95f2007-03-29 01:38:42 -070015765#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015766static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015767{
15768 struct net_device *dev = tp->dev;
15769 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070015770 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070015771 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070015772 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015773
David S. Miller49b6e95f2007-03-29 01:38:42 -070015774 addr = of_get_property(dp, "local-mac-address", &len);
15775 if (addr && len == 6) {
15776 memcpy(dev->dev_addr, addr, 6);
David S. Miller49b6e95f2007-03-29 01:38:42 -070015777 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015778 }
15779 return -ENODEV;
15780}
15781
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015782static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015783{
15784 struct net_device *dev = tp->dev;
15785
15786 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
15787 return 0;
15788}
15789#endif
15790
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015791static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015792{
15793 struct net_device *dev = tp->dev;
15794 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080015795 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015796
David S. Miller49b6e95f2007-03-29 01:38:42 -070015797#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070015798 if (!tg3_get_macaddr_sparc(tp))
15799 return 0;
15800#endif
15801
15802 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000015803 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015804 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015805 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
15806 mac_offset = 0xcc;
15807 if (tg3_nvram_lock(tp))
15808 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
15809 else
15810 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000015811 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000015812 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000015813 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000015814 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000015815 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000015816 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070015817 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015818
15819 /* First try to get it from MAC address mailbox. */
15820 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
15821 if ((hi >> 16) == 0x484b) {
15822 dev->dev_addr[0] = (hi >> 8) & 0xff;
15823 dev->dev_addr[1] = (hi >> 0) & 0xff;
15824
15825 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
15826 dev->dev_addr[2] = (lo >> 24) & 0xff;
15827 dev->dev_addr[3] = (lo >> 16) & 0xff;
15828 dev->dev_addr[4] = (lo >> 8) & 0xff;
15829 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015830
Michael Chan008652b2006-03-27 23:14:53 -080015831 /* Some old bootcode may report a 0 MAC address in SRAM */
15832 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
15833 }
15834 if (!addr_ok) {
15835 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000015836 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000015837 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000015838 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070015839 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
15840 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080015841 }
15842 /* Finally just fetch it out of the MAC control regs. */
15843 else {
15844 hi = tr32(MAC_ADDR_0_HIGH);
15845 lo = tr32(MAC_ADDR_0_LOW);
15846
15847 dev->dev_addr[5] = lo & 0xff;
15848 dev->dev_addr[4] = (lo >> 8) & 0xff;
15849 dev->dev_addr[3] = (lo >> 16) & 0xff;
15850 dev->dev_addr[2] = (lo >> 24) & 0xff;
15851 dev->dev_addr[1] = hi & 0xff;
15852 dev->dev_addr[0] = (hi >> 8) & 0xff;
15853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015854 }
15855
15856 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070015857#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070015858 if (!tg3_get_default_macaddr_sparc(tp))
15859 return 0;
15860#endif
15861 return -EINVAL;
15862 }
15863 return 0;
15864}
15865
David S. Miller59e6b432005-05-18 22:50:10 -070015866#define BOUNDARY_SINGLE_CACHELINE 1
15867#define BOUNDARY_MULTI_CACHELINE 2
15868
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015869static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070015870{
15871 int cacheline_size;
15872 u8 byte;
15873 int goal;
15874
15875 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
15876 if (byte == 0)
15877 cacheline_size = 1024;
15878 else
15879 cacheline_size = (int) byte * 4;
15880
15881 /* On 5703 and later chips, the boundary bits have no
15882 * effect.
15883 */
15884 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15885 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015886 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070015887 goto out;
15888
15889#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
15890 goal = BOUNDARY_MULTI_CACHELINE;
15891#else
15892#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
15893 goal = BOUNDARY_SINGLE_CACHELINE;
15894#else
15895 goal = 0;
15896#endif
15897#endif
15898
Joe Perches63c3a662011-04-26 08:12:10 +000015899 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015900 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
15901 goto out;
15902 }
15903
David S. Miller59e6b432005-05-18 22:50:10 -070015904 if (!goal)
15905 goto out;
15906
15907 /* PCI controllers on most RISC systems tend to disconnect
15908 * when a device tries to burst across a cache-line boundary.
15909 * Therefore, letting tg3 do so just wastes PCI bandwidth.
15910 *
15911 * Unfortunately, for PCI-E there are only limited
15912 * write-side controls for this, and thus for reads
15913 * we will still get the disconnects. We'll also waste
15914 * these PCI cycles for both read and write for chips
15915 * other than 5700 and 5701 which do not implement the
15916 * boundary bits.
15917 */
Joe Perches63c3a662011-04-26 08:12:10 +000015918 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070015919 switch (cacheline_size) {
15920 case 16:
15921 case 32:
15922 case 64:
15923 case 128:
15924 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15925 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
15926 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
15927 } else {
15928 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
15929 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
15930 }
15931 break;
15932
15933 case 256:
15934 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
15935 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
15936 break;
15937
15938 default:
15939 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
15940 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
15941 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015942 }
Joe Perches63c3a662011-04-26 08:12:10 +000015943 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070015944 switch (cacheline_size) {
15945 case 16:
15946 case 32:
15947 case 64:
15948 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15949 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
15950 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
15951 break;
15952 }
15953 /* fallthrough */
15954 case 128:
15955 default:
15956 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
15957 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
15958 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015959 }
David S. Miller59e6b432005-05-18 22:50:10 -070015960 } else {
15961 switch (cacheline_size) {
15962 case 16:
15963 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15964 val |= (DMA_RWCTRL_READ_BNDRY_16 |
15965 DMA_RWCTRL_WRITE_BNDRY_16);
15966 break;
15967 }
15968 /* fallthrough */
15969 case 32:
15970 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15971 val |= (DMA_RWCTRL_READ_BNDRY_32 |
15972 DMA_RWCTRL_WRITE_BNDRY_32);
15973 break;
15974 }
15975 /* fallthrough */
15976 case 64:
15977 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15978 val |= (DMA_RWCTRL_READ_BNDRY_64 |
15979 DMA_RWCTRL_WRITE_BNDRY_64);
15980 break;
15981 }
15982 /* fallthrough */
15983 case 128:
15984 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15985 val |= (DMA_RWCTRL_READ_BNDRY_128 |
15986 DMA_RWCTRL_WRITE_BNDRY_128);
15987 break;
15988 }
15989 /* fallthrough */
15990 case 256:
15991 val |= (DMA_RWCTRL_READ_BNDRY_256 |
15992 DMA_RWCTRL_WRITE_BNDRY_256);
15993 break;
15994 case 512:
15995 val |= (DMA_RWCTRL_READ_BNDRY_512 |
15996 DMA_RWCTRL_WRITE_BNDRY_512);
15997 break;
15998 case 1024:
15999 default:
16000 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
16001 DMA_RWCTRL_WRITE_BNDRY_1024);
16002 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016003 }
David S. Miller59e6b432005-05-18 22:50:10 -070016004 }
16005
16006out:
16007 return val;
16008}
16009
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016010static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
16011 int size, int to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016012{
16013 struct tg3_internal_buffer_desc test_desc;
16014 u32 sram_dma_descs;
16015 int i, ret;
16016
16017 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
16018
16019 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
16020 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
16021 tw32(RDMAC_STATUS, 0);
16022 tw32(WDMAC_STATUS, 0);
16023
16024 tw32(BUFMGR_MODE, 0);
16025 tw32(FTQ_RESET, 0);
16026
16027 test_desc.addr_hi = ((u64) buf_dma) >> 32;
16028 test_desc.addr_lo = buf_dma & 0xffffffff;
16029 test_desc.nic_mbuf = 0x00002100;
16030 test_desc.len = size;
16031
16032 /*
16033 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
16034 * the *second* time the tg3 driver was getting loaded after an
16035 * initial scan.
16036 *
16037 * Broadcom tells me:
16038 * ...the DMA engine is connected to the GRC block and a DMA
16039 * reset may affect the GRC block in some unpredictable way...
16040 * The behavior of resets to individual blocks has not been tested.
16041 *
16042 * Broadcom noted the GRC reset will also reset all sub-components.
16043 */
16044 if (to_device) {
16045 test_desc.cqid_sqid = (13 << 8) | 2;
16046
16047 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
16048 udelay(40);
16049 } else {
16050 test_desc.cqid_sqid = (16 << 8) | 7;
16051
16052 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
16053 udelay(40);
16054 }
16055 test_desc.flags = 0x00000005;
16056
16057 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
16058 u32 val;
16059
16060 val = *(((u32 *)&test_desc) + i);
16061 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
16062 sram_dma_descs + (i * sizeof(u32)));
16063 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
16064 }
16065 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
16066
Matt Carlson859a588792010-04-05 10:19:28 +000016067 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016068 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000016069 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070016070 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016071
16072 ret = -ENODEV;
16073 for (i = 0; i < 40; i++) {
16074 u32 val;
16075
16076 if (to_device)
16077 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
16078 else
16079 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
16080 if ((val & 0xffff) == sram_dma_descs) {
16081 ret = 0;
16082 break;
16083 }
16084
16085 udelay(100);
16086 }
16087
16088 return ret;
16089}
16090
David S. Millerded73402005-05-23 13:59:47 -070016091#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070016092
Matt Carlson41434702011-03-09 16:58:22 +000016093static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080016094 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
16095 { },
16096};
16097
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016098static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016099{
16100 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070016101 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016102 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016103
Matt Carlson4bae65c2010-11-24 08:31:52 +000016104 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
16105 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016106 if (!buf) {
16107 ret = -ENOMEM;
16108 goto out_nofree;
16109 }
16110
16111 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
16112 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
16113
David S. Miller59e6b432005-05-18 22:50:10 -070016114 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016115
Joe Perches63c3a662011-04-26 08:12:10 +000016116 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016117 goto out;
16118
Joe Perches63c3a662011-04-26 08:12:10 +000016119 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016120 /* DMA read watermark not used on PCIE */
16121 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000016122 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070016123 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
16124 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016125 tp->dma_rwctrl |= 0x003f0000;
16126 else
16127 tp->dma_rwctrl |= 0x003f000f;
16128 } else {
16129 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
16130 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
16131 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080016132 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016133
Michael Chan4a29cc22006-03-19 13:21:12 -080016134 /* If the 5704 is behind the EPB bridge, we can
16135 * do the less restrictive ONE_DMA workaround for
16136 * better performance.
16137 */
Joe Perches63c3a662011-04-26 08:12:10 +000016138 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080016139 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
16140 tp->dma_rwctrl |= 0x8000;
16141 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016142 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
16143
Michael Chan49afdeb2007-02-13 12:17:03 -080016144 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
16145 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070016146 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080016147 tp->dma_rwctrl |=
16148 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
16149 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
16150 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070016151 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
16152 /* 5780 always in PCIX mode */
16153 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070016154 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
16155 /* 5714 always in PCIX mode */
16156 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016157 } else {
16158 tp->dma_rwctrl |= 0x001b000f;
16159 }
16160 }
16161
16162 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
16163 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
16164 tp->dma_rwctrl &= 0xfffffff0;
16165
16166 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
16167 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
16168 /* Remove this if it causes problems for some boards. */
16169 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
16170
16171 /* On 5700/5701 chips, we need to set this bit.
16172 * Otherwise the chip will issue cacheline transactions
16173 * to streamable DMA memory with not all the byte
16174 * enables turned on. This is an error on several
16175 * RISC PCI controllers, in particular sparc64.
16176 *
16177 * On 5703/5704 chips, this bit has been reassigned
16178 * a different meaning. In particular, it is used
16179 * on those chips to enable a PCI-X workaround.
16180 */
16181 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
16182 }
16183
16184 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16185
16186#if 0
16187 /* Unneeded, already done by tg3_get_invariants. */
16188 tg3_switch_clocks(tp);
16189#endif
16190
Linus Torvalds1da177e2005-04-16 15:20:36 -070016191 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
16192 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
16193 goto out;
16194
David S. Miller59e6b432005-05-18 22:50:10 -070016195 /* It is best to perform DMA test with maximum write burst size
16196 * to expose the 5700/5701 write DMA bug.
16197 */
16198 saved_dma_rwctrl = tp->dma_rwctrl;
16199 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16200 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16201
Linus Torvalds1da177e2005-04-16 15:20:36 -070016202 while (1) {
16203 u32 *p = buf, i;
16204
16205 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
16206 p[i] = i;
16207
16208 /* Send the buffer to the chip. */
16209 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
16210 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000016211 dev_err(&tp->pdev->dev,
16212 "%s: Buffer write failed. err = %d\n",
16213 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016214 break;
16215 }
16216
16217#if 0
16218 /* validate data reached card RAM correctly. */
16219 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16220 u32 val;
16221 tg3_read_mem(tp, 0x2100 + (i*4), &val);
16222 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000016223 dev_err(&tp->pdev->dev,
16224 "%s: Buffer corrupted on device! "
16225 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016226 /* ret = -ENODEV here? */
16227 }
16228 p[i] = 0;
16229 }
16230#endif
16231 /* Now read it back. */
16232 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
16233 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000016234 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
16235 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016236 break;
16237 }
16238
16239 /* Verify it. */
16240 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16241 if (p[i] == i)
16242 continue;
16243
David S. Miller59e6b432005-05-18 22:50:10 -070016244 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16245 DMA_RWCTRL_WRITE_BNDRY_16) {
16246 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016247 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
16248 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16249 break;
16250 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000016251 dev_err(&tp->pdev->dev,
16252 "%s: Buffer corrupted on read back! "
16253 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016254 ret = -ENODEV;
16255 goto out;
16256 }
16257 }
16258
16259 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
16260 /* Success. */
16261 ret = 0;
16262 break;
16263 }
16264 }
David S. Miller59e6b432005-05-18 22:50:10 -070016265 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16266 DMA_RWCTRL_WRITE_BNDRY_16) {
16267 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070016268 * now look for chipsets that are known to expose the
16269 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070016270 */
Matt Carlson41434702011-03-09 16:58:22 +000016271 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016272 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16273 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000016274 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016275 /* Safe to use the calculated DMA boundary. */
16276 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000016277 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070016278
David S. Miller59e6b432005-05-18 22:50:10 -070016279 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016281
16282out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000016283 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016284out_nofree:
16285 return ret;
16286}
16287
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016288static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016289{
Joe Perches63c3a662011-04-26 08:12:10 +000016290 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000016291 tp->bufmgr_config.mbuf_read_dma_low_water =
16292 DEFAULT_MB_RDMA_LOW_WATER_5705;
16293 tp->bufmgr_config.mbuf_mac_rx_low_water =
16294 DEFAULT_MB_MACRX_LOW_WATER_57765;
16295 tp->bufmgr_config.mbuf_high_water =
16296 DEFAULT_MB_HIGH_WATER_57765;
16297
16298 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16299 DEFAULT_MB_RDMA_LOW_WATER_5705;
16300 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16301 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
16302 tp->bufmgr_config.mbuf_high_water_jumbo =
16303 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000016304 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070016305 tp->bufmgr_config.mbuf_read_dma_low_water =
16306 DEFAULT_MB_RDMA_LOW_WATER_5705;
16307 tp->bufmgr_config.mbuf_mac_rx_low_water =
16308 DEFAULT_MB_MACRX_LOW_WATER_5705;
16309 tp->bufmgr_config.mbuf_high_water =
16310 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070016311 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
16312 tp->bufmgr_config.mbuf_mac_rx_low_water =
16313 DEFAULT_MB_MACRX_LOW_WATER_5906;
16314 tp->bufmgr_config.mbuf_high_water =
16315 DEFAULT_MB_HIGH_WATER_5906;
16316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016317
Michael Chanfdfec1722005-07-25 12:31:48 -070016318 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16319 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
16320 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16321 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
16322 tp->bufmgr_config.mbuf_high_water_jumbo =
16323 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
16324 } else {
16325 tp->bufmgr_config.mbuf_read_dma_low_water =
16326 DEFAULT_MB_RDMA_LOW_WATER;
16327 tp->bufmgr_config.mbuf_mac_rx_low_water =
16328 DEFAULT_MB_MACRX_LOW_WATER;
16329 tp->bufmgr_config.mbuf_high_water =
16330 DEFAULT_MB_HIGH_WATER;
16331
16332 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16333 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
16334 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16335 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
16336 tp->bufmgr_config.mbuf_high_water_jumbo =
16337 DEFAULT_MB_HIGH_WATER_JUMBO;
16338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016339
16340 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
16341 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
16342}
16343
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016344static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016345{
Matt Carlson79eb6902010-02-17 15:17:03 +000016346 switch (tp->phy_id & TG3_PHY_ID_MASK) {
16347 case TG3_PHY_ID_BCM5400: return "5400";
16348 case TG3_PHY_ID_BCM5401: return "5401";
16349 case TG3_PHY_ID_BCM5411: return "5411";
16350 case TG3_PHY_ID_BCM5701: return "5701";
16351 case TG3_PHY_ID_BCM5703: return "5703";
16352 case TG3_PHY_ID_BCM5704: return "5704";
16353 case TG3_PHY_ID_BCM5705: return "5705";
16354 case TG3_PHY_ID_BCM5750: return "5750";
16355 case TG3_PHY_ID_BCM5752: return "5752";
16356 case TG3_PHY_ID_BCM5714: return "5714";
16357 case TG3_PHY_ID_BCM5780: return "5780";
16358 case TG3_PHY_ID_BCM5755: return "5755";
16359 case TG3_PHY_ID_BCM5787: return "5787";
16360 case TG3_PHY_ID_BCM5784: return "5784";
16361 case TG3_PHY_ID_BCM5756: return "5722/5756";
16362 case TG3_PHY_ID_BCM5906: return "5906";
16363 case TG3_PHY_ID_BCM5761: return "5761";
16364 case TG3_PHY_ID_BCM5718C: return "5718C";
16365 case TG3_PHY_ID_BCM5718S: return "5718S";
16366 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000016367 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000016368 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000016369 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000016370 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070016371 case 0: return "serdes";
16372 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070016373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016374}
16375
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016376static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070016377{
Joe Perches63c3a662011-04-26 08:12:10 +000016378 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070016379 strcpy(str, "PCI Express");
16380 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000016381 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070016382 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
16383
16384 strcpy(str, "PCIX:");
16385
16386 if ((clock_ctrl == 7) ||
16387 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
16388 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
16389 strcat(str, "133MHz");
16390 else if (clock_ctrl == 0)
16391 strcat(str, "33MHz");
16392 else if (clock_ctrl == 2)
16393 strcat(str, "50MHz");
16394 else if (clock_ctrl == 4)
16395 strcat(str, "66MHz");
16396 else if (clock_ctrl == 6)
16397 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070016398 } else {
16399 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000016400 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070016401 strcat(str, "66MHz");
16402 else
16403 strcat(str, "33MHz");
16404 }
Joe Perches63c3a662011-04-26 08:12:10 +000016405 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070016406 strcat(str, ":32-bit");
16407 else
16408 strcat(str, ":64-bit");
16409 return str;
16410}
16411
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016412static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070016413{
16414 struct ethtool_coalesce *ec = &tp->coal;
16415
16416 memset(ec, 0, sizeof(*ec));
16417 ec->cmd = ETHTOOL_GCOALESCE;
16418 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
16419 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
16420 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
16421 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
16422 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
16423 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
16424 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
16425 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
16426 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
16427
16428 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
16429 HOSTCC_MODE_CLRTICK_TXBD)) {
16430 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
16431 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
16432 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
16433 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
16434 }
Michael Chand244c892005-07-05 14:42:33 -070016435
Joe Perches63c3a662011-04-26 08:12:10 +000016436 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070016437 ec->rx_coalesce_usecs_irq = 0;
16438 ec->tx_coalesce_usecs_irq = 0;
16439 ec->stats_block_coalesce_usecs = 0;
16440 }
David S. Miller15f98502005-05-18 22:49:26 -070016441}
16442
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016443static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016444 const struct pci_device_id *ent)
16445{
Linus Torvalds1da177e2005-04-16 15:20:36 -070016446 struct net_device *dev;
16447 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000016448 int i, err, pm_cap;
16449 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070016450 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080016451 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000016452 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016453
Joe Perches05dbe002010-02-17 19:44:19 +000016454 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016455
16456 err = pci_enable_device(pdev);
16457 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016458 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016459 return err;
16460 }
16461
Linus Torvalds1da177e2005-04-16 15:20:36 -070016462 err = pci_request_regions(pdev, DRV_MODULE_NAME);
16463 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016464 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016465 goto err_out_disable_pdev;
16466 }
16467
16468 pci_set_master(pdev);
16469
16470 /* Find power-management capability. */
16471 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
16472 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000016473 dev_err(&pdev->dev,
16474 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016475 err = -EIO;
16476 goto err_out_free_res;
16477 }
16478
Matt Carlson16821282011-07-13 09:27:28 +000016479 err = pci_set_power_state(pdev, PCI_D0);
16480 if (err) {
16481 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
16482 goto err_out_free_res;
16483 }
16484
Matt Carlsonfe5f5782009-09-01 13:09:39 +000016485 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016486 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016487 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000016488 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016489 }
16490
Linus Torvalds1da177e2005-04-16 15:20:36 -070016491 SET_NETDEV_DEV(dev, &pdev->dev);
16492
Linus Torvalds1da177e2005-04-16 15:20:36 -070016493 tp = netdev_priv(dev);
16494 tp->pdev = pdev;
16495 tp->dev = dev;
16496 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016497 tp->rx_mode = TG3_DEF_RX_MODE;
16498 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070016499
Linus Torvalds1da177e2005-04-16 15:20:36 -070016500 if (tg3_debug > 0)
16501 tp->msg_enable = tg3_debug;
16502 else
16503 tp->msg_enable = TG3_DEF_MSG_ENABLE;
16504
16505 /* The word/byte swap controls here control register access byte
16506 * swapping. DMA data byte swapping is controlled in the GRC_MODE
16507 * setting below.
16508 */
16509 tp->misc_host_ctrl =
16510 MISC_HOST_CTRL_MASK_PCI_INT |
16511 MISC_HOST_CTRL_WORD_SWAP |
16512 MISC_HOST_CTRL_INDIR_ACCESS |
16513 MISC_HOST_CTRL_PCISTATE_RW;
16514
16515 /* The NONFRM (non-frame) byte/word swap controls take effect
16516 * on descriptor entries, anything which isn't packet data.
16517 *
16518 * The StrongARM chips on the board (one for tx, one for rx)
16519 * are running in big-endian mode.
16520 */
16521 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
16522 GRC_MODE_WSWAP_NONFRM_DATA);
16523#ifdef __BIG_ENDIAN
16524 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
16525#endif
16526 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016527 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000016528 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016529
Matt Carlsond5fe4882008-11-21 17:20:32 -080016530 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010016531 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016532 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016533 err = -ENOMEM;
16534 goto err_out_free_dev;
16535 }
16536
Matt Carlsonc9cab242011-07-13 09:27:27 +000016537 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16538 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
16539 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
16540 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
16541 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000016542 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000016543 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
16544 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000016545 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
16546 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
16547 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
16548 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000016549 tg3_flag_set(tp, ENABLE_APE);
16550 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
16551 if (!tp->aperegs) {
16552 dev_err(&pdev->dev,
16553 "Cannot map APE registers, aborting\n");
16554 err = -ENOMEM;
16555 goto err_out_iounmap;
16556 }
16557 }
16558
Linus Torvalds1da177e2005-04-16 15:20:36 -070016559 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
16560 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016561
Linus Torvalds1da177e2005-04-16 15:20:36 -070016562 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016563 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000016564 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016565 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016566
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016567 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016568 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016569 dev_err(&pdev->dev,
16570 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016571 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016572 }
16573
Michael Chan4a29cc22006-03-19 13:21:12 -080016574 /* The EPB bridge inside 5714, 5715, and 5780 and any
16575 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080016576 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
16577 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
16578 * do DMA address check in tg3_start_xmit().
16579 */
Joe Perches63c3a662011-04-26 08:12:10 +000016580 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070016581 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000016582 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070016583 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080016584#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070016585 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080016586#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080016587 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070016588 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080016589
16590 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070016591 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080016592 err = pci_set_dma_mask(pdev, dma_mask);
16593 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000016594 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080016595 err = pci_set_consistent_dma_mask(pdev,
16596 persist_dma_mask);
16597 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016598 dev_err(&pdev->dev, "Unable to obtain 64 bit "
16599 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016600 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080016601 }
16602 }
16603 }
Yang Hongyang284901a2009-04-06 19:01:15 -070016604 if (err || dma_mask == DMA_BIT_MASK(32)) {
16605 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080016606 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016607 dev_err(&pdev->dev,
16608 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016609 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080016610 }
16611 }
16612
Michael Chanfdfec1722005-07-25 12:31:48 -070016613 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016614
Matt Carlson0da06062011-05-19 12:12:53 +000016615 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
16616
16617 /* 5700 B0 chips do not support checksumming correctly due
16618 * to hardware bugs.
16619 */
16620 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
16621 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
16622
16623 if (tg3_flag(tp, 5755_PLUS))
16624 features |= NETIF_F_IPV6_CSUM;
16625 }
16626
Michael Chan4e3a7aa2006-03-20 17:47:44 -080016627 /* TSO is on by default on chips that support hardware TSO.
16628 * Firmware TSO on older chips gives lower performance, so it
16629 * is off by default, but can be enabled using ethtool.
16630 */
Joe Perches63c3a662011-04-26 08:12:10 +000016631 if ((tg3_flag(tp, HW_TSO_1) ||
16632 tg3_flag(tp, HW_TSO_2) ||
16633 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000016634 (features & NETIF_F_IP_CSUM))
16635 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000016636 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000016637 if (features & NETIF_F_IPV6_CSUM)
16638 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000016639 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000016640 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070016641 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
16642 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000016643 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000016644 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000016645 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070016646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016647
Matt Carlsond542fe22011-05-19 16:02:43 +000016648 dev->features |= features;
16649 dev->vlan_features |= features;
16650
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016651 /*
16652 * Add loopback capability only for a subset of devices that support
16653 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
16654 * loopback for the remaining devices.
16655 */
16656 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
16657 !tg3_flag(tp, CPMU_PRESENT))
16658 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000016659 features |= NETIF_F_LOOPBACK;
16660
Matt Carlson0da06062011-05-19 12:12:53 +000016661 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016662
Linus Torvalds1da177e2005-04-16 15:20:36 -070016663 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016664 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016665 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016666 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016667 tp->rx_pending = 63;
16668 }
16669
Linus Torvalds1da177e2005-04-16 15:20:36 -070016670 err = tg3_get_device_address(tp);
16671 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016672 dev_err(&pdev->dev,
16673 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016674 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016675 }
16676
Matt Carlsonc88864d2007-11-12 21:07:01 -080016677 /*
16678 * Reset chip in case UNDI or EFI driver did not shutdown
16679 * DMA self test will enable WDMAC and we'll see (spurious)
16680 * pending DMA on the PCI bus at that point.
16681 */
16682 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
16683 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
16684 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
16685 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
16686 }
16687
16688 err = tg3_test_dma(tp);
16689 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016690 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080016691 goto err_out_apeunmap;
16692 }
16693
Matt Carlson78f90dc2009-11-13 13:03:42 +000016694 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
16695 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
16696 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000016697 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000016698 struct tg3_napi *tnapi = &tp->napi[i];
16699
16700 tnapi->tp = tp;
16701 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
16702
16703 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000016704 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000016705 intmbx += 0x8;
16706 else
16707 intmbx += 0x4;
16708
16709 tnapi->consmbox = rcvmbx;
16710 tnapi->prodmbox = sndmbx;
16711
Matt Carlson66cfd1b2010-09-30 10:34:30 +000016712 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000016713 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000016714 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000016715 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000016716
Joe Perches63c3a662011-04-26 08:12:10 +000016717 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000016718 break;
16719
16720 /*
16721 * If we support MSIX, we'll be using RSS. If we're using
16722 * RSS, the first vector only handles link interrupts and the
16723 * remaining vectors handle rx and tx interrupts. Reuse the
16724 * mailbox values for the next iteration. The values we setup
16725 * above are still useful for the single vectored mode.
16726 */
16727 if (!i)
16728 continue;
16729
16730 rcvmbx += 0x8;
16731
16732 if (sndmbx & 0x4)
16733 sndmbx -= 0x4;
16734 else
16735 sndmbx += 0xc;
16736 }
16737
Matt Carlsonc88864d2007-11-12 21:07:01 -080016738 tg3_init_coal(tp);
16739
Michael Chanc49a1562006-12-17 17:07:29 -080016740 pci_set_drvdata(pdev, dev);
16741
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000016742 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000016743 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
16744 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000016745 tg3_flag_set(tp, PTP_CAPABLE);
16746
Matt Carlsoncd0d7222011-07-13 09:27:33 +000016747 if (tg3_flag(tp, 5717_PLUS)) {
16748 /* Resume a low-power mode */
16749 tg3_frob_aux_power(tp, false);
16750 }
16751
Matt Carlson21f76382012-02-22 12:35:21 +000016752 tg3_timer_init(tp);
16753
Linus Torvalds1da177e2005-04-16 15:20:36 -070016754 err = register_netdev(dev);
16755 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016756 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070016757 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016758 }
16759
Joe Perches05dbe002010-02-17 19:44:19 +000016760 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
16761 tp->board_part_number,
16762 tp->pci_chip_rev_id,
16763 tg3_bus_string(tp, str),
16764 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016765
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016766 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000016767 struct phy_device *phydev;
16768 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000016769 netdev_info(dev,
16770 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000016771 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016772 } else {
16773 char *ethtype;
16774
16775 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
16776 ethtype = "10/100Base-TX";
16777 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
16778 ethtype = "1000Base-SX";
16779 else
16780 ethtype = "10/100/1000Base-T";
16781
Matt Carlson5129c3a2010-04-05 10:19:23 +000016782 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000016783 "(WireSpeed[%d], EEE[%d])\n",
16784 tg3_phy_string(tp), ethtype,
16785 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
16786 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016787 }
Matt Carlsondf59c942008-11-03 16:52:56 -080016788
Joe Perches05dbe002010-02-17 19:44:19 +000016789 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000016790 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000016791 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016792 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000016793 tg3_flag(tp, ENABLE_ASF) != 0,
16794 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000016795 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
16796 tp->dma_rwctrl,
16797 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
16798 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016799
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016800 pci_save_state(pdev);
16801
Linus Torvalds1da177e2005-04-16 15:20:36 -070016802 return 0;
16803
Matt Carlson0d3031d2007-10-10 18:02:43 -070016804err_out_apeunmap:
16805 if (tp->aperegs) {
16806 iounmap(tp->aperegs);
16807 tp->aperegs = NULL;
16808 }
16809
Linus Torvalds1da177e2005-04-16 15:20:36 -070016810err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070016811 if (tp->regs) {
16812 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016813 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016815
16816err_out_free_dev:
16817 free_netdev(dev);
16818
Matt Carlson16821282011-07-13 09:27:28 +000016819err_out_power_down:
16820 pci_set_power_state(pdev, PCI_D3hot);
16821
Linus Torvalds1da177e2005-04-16 15:20:36 -070016822err_out_free_res:
16823 pci_release_regions(pdev);
16824
16825err_out_disable_pdev:
16826 pci_disable_device(pdev);
16827 pci_set_drvdata(pdev, NULL);
16828 return err;
16829}
16830
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016831static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016832{
16833 struct net_device *dev = pci_get_drvdata(pdev);
16834
16835 if (dev) {
16836 struct tg3 *tp = netdev_priv(dev);
16837
Jesper Juhle3c55302012-04-09 22:50:15 +020016838 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080016839
Matt Carlsondb219972011-11-04 09:15:03 +000016840 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070016841
David S. Miller1805b2f2011-10-24 18:18:09 -040016842 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016843 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070016844 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016845 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070016846
Linus Torvalds1da177e2005-04-16 15:20:36 -070016847 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016848 if (tp->aperegs) {
16849 iounmap(tp->aperegs);
16850 tp->aperegs = NULL;
16851 }
Michael Chan68929142005-08-09 20:17:14 -070016852 if (tp->regs) {
16853 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016854 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016856 free_netdev(dev);
16857 pci_release_regions(pdev);
16858 pci_disable_device(pdev);
16859 pci_set_drvdata(pdev, NULL);
16860 }
16861}
16862
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016863#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016864static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016865{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016866 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016867 struct net_device *dev = pci_get_drvdata(pdev);
16868 struct tg3 *tp = netdev_priv(dev);
16869 int err;
16870
16871 if (!netif_running(dev))
16872 return 0;
16873
Matt Carlsondb219972011-11-04 09:15:03 +000016874 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016875 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016876 tg3_netif_stop(tp);
16877
Matt Carlson21f76382012-02-22 12:35:21 +000016878 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016879
David S. Millerf47c11e2005-06-24 20:18:35 -070016880 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016881 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070016882 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016883
16884 netif_device_detach(dev);
16885
David S. Millerf47c11e2005-06-24 20:18:35 -070016886 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070016887 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000016888 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070016889 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016890
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016891 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016892 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016893 int err2;
16894
David S. Millerf47c11e2005-06-24 20:18:35 -070016895 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016896
Joe Perches63c3a662011-04-26 08:12:10 +000016897 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016898 err2 = tg3_restart_hw(tp, 1);
16899 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070016900 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016901
Matt Carlson21f76382012-02-22 12:35:21 +000016902 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016903
16904 netif_device_attach(dev);
16905 tg3_netif_start(tp);
16906
Michael Chanb9ec6c12006-07-25 16:37:27 -070016907out:
David S. Millerf47c11e2005-06-24 20:18:35 -070016908 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016909
16910 if (!err2)
16911 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016912 }
16913
16914 return err;
16915}
16916
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016917static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016918{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016919 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016920 struct net_device *dev = pci_get_drvdata(pdev);
16921 struct tg3 *tp = netdev_priv(dev);
16922 int err;
16923
16924 if (!netif_running(dev))
16925 return 0;
16926
Linus Torvalds1da177e2005-04-16 15:20:36 -070016927 netif_device_attach(dev);
16928
David S. Millerf47c11e2005-06-24 20:18:35 -070016929 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016930
Joe Perches63c3a662011-04-26 08:12:10 +000016931 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070016932 err = tg3_restart_hw(tp, 1);
16933 if (err)
16934 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016935
Matt Carlson21f76382012-02-22 12:35:21 +000016936 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016937
Linus Torvalds1da177e2005-04-16 15:20:36 -070016938 tg3_netif_start(tp);
16939
Michael Chanb9ec6c12006-07-25 16:37:27 -070016940out:
David S. Millerf47c11e2005-06-24 20:18:35 -070016941 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016942
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016943 if (!err)
16944 tg3_phy_start(tp);
16945
Michael Chanb9ec6c12006-07-25 16:37:27 -070016946 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016947}
16948
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016949static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016950#define TG3_PM_OPS (&tg3_pm_ops)
16951
16952#else
16953
16954#define TG3_PM_OPS NULL
16955
16956#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016957
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016958/**
16959 * tg3_io_error_detected - called when PCI error is detected
16960 * @pdev: Pointer to PCI device
16961 * @state: The current pci connection state
16962 *
16963 * This function is called after a PCI bus error affecting
16964 * this device has been detected.
16965 */
16966static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
16967 pci_channel_state_t state)
16968{
16969 struct net_device *netdev = pci_get_drvdata(pdev);
16970 struct tg3 *tp = netdev_priv(netdev);
16971 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
16972
16973 netdev_info(netdev, "PCI I/O error detected\n");
16974
16975 rtnl_lock();
16976
16977 if (!netif_running(netdev))
16978 goto done;
16979
16980 tg3_phy_stop(tp);
16981
16982 tg3_netif_stop(tp);
16983
Matt Carlson21f76382012-02-22 12:35:21 +000016984 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016985
16986 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000016987 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016988
16989 netif_device_detach(netdev);
16990
16991 /* Clean up software state, even if MMIO is blocked */
16992 tg3_full_lock(tp, 0);
16993 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
16994 tg3_full_unlock(tp);
16995
16996done:
16997 if (state == pci_channel_io_perm_failure)
16998 err = PCI_ERS_RESULT_DISCONNECT;
16999 else
17000 pci_disable_device(pdev);
17001
17002 rtnl_unlock();
17003
17004 return err;
17005}
17006
17007/**
17008 * tg3_io_slot_reset - called after the pci bus has been reset.
17009 * @pdev: Pointer to PCI device
17010 *
17011 * Restart the card from scratch, as if from a cold-boot.
17012 * At this point, the card has exprienced a hard reset,
17013 * followed by fixups by BIOS, and has its config space
17014 * set up identically to what it was at cold boot.
17015 */
17016static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
17017{
17018 struct net_device *netdev = pci_get_drvdata(pdev);
17019 struct tg3 *tp = netdev_priv(netdev);
17020 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
17021 int err;
17022
17023 rtnl_lock();
17024
17025 if (pci_enable_device(pdev)) {
17026 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
17027 goto done;
17028 }
17029
17030 pci_set_master(pdev);
17031 pci_restore_state(pdev);
17032 pci_save_state(pdev);
17033
17034 if (!netif_running(netdev)) {
17035 rc = PCI_ERS_RESULT_RECOVERED;
17036 goto done;
17037 }
17038
17039 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000017040 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017041 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017042
17043 rc = PCI_ERS_RESULT_RECOVERED;
17044
17045done:
17046 rtnl_unlock();
17047
17048 return rc;
17049}
17050
17051/**
17052 * tg3_io_resume - called when traffic can start flowing again.
17053 * @pdev: Pointer to PCI device
17054 *
17055 * This callback is called when the error recovery driver tells
17056 * us that its OK to resume normal operation.
17057 */
17058static void tg3_io_resume(struct pci_dev *pdev)
17059{
17060 struct net_device *netdev = pci_get_drvdata(pdev);
17061 struct tg3 *tp = netdev_priv(netdev);
17062 int err;
17063
17064 rtnl_lock();
17065
17066 if (!netif_running(netdev))
17067 goto done;
17068
17069 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000017070 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017071 err = tg3_restart_hw(tp, 1);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017072 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017073 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017074 netdev_err(netdev, "Cannot restart hardware after reset.\n");
17075 goto done;
17076 }
17077
17078 netif_device_attach(netdev);
17079
Matt Carlson21f76382012-02-22 12:35:21 +000017080 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017081
17082 tg3_netif_start(tp);
17083
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017084 tg3_full_unlock(tp);
17085
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017086 tg3_phy_start(tp);
17087
17088done:
17089 rtnl_unlock();
17090}
17091
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070017092static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017093 .error_detected = tg3_io_error_detected,
17094 .slot_reset = tg3_io_slot_reset,
17095 .resume = tg3_io_resume
17096};
17097
Linus Torvalds1da177e2005-04-16 15:20:36 -070017098static struct pci_driver tg3_driver = {
17099 .name = DRV_MODULE_NAME,
17100 .id_table = tg3_pci_tbl,
17101 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017102 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017103 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017104 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017105};
17106
17107static int __init tg3_init(void)
17108{
Jeff Garzik29917622006-08-19 17:48:59 -040017109 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017110}
17111
17112static void __exit tg3_cleanup(void)
17113{
17114 pci_unregister_driver(&tg3_driver);
17115}
17116
17117module_init(tg3_init);
17118module_exit(tg3_cleanup);