blob: 7675231d9a9d3be408d002b4f3121506ce3bc0a7 [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.
Matt Carlsonb86fb2c2011-01-25 15:58:57 +00007 * Copyright (C) 2005-2011 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>
29#include <linux/ioport.h>
30#include <linux/pci.h>
31#include <linux/netdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/skbuff.h>
34#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000035#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070037#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070038#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/if_vlan.h>
40#include <linux/ip.h>
41#include <linux/tcp.h>
42#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070043#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020044#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080045#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030048#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <asm/system.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000051#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000053#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
David S. Miller49b6e95f2007-03-29 01:38:42 -070055#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070057#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
59
Matt Carlson63532392008-11-03 16:49:57 -080060#define BAR_0 0
61#define BAR_2 2
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include "tg3.h"
64
Joe Perches63c3a662011-04-26 08:12:10 +000065/* Functions & macros to verify TG3_FLAGS types */
66
67static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
68{
69 return test_bit(flag, bits);
70}
71
72static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
73{
74 set_bit(flag, bits);
75}
76
77static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
78{
79 clear_bit(flag, bits);
80}
81
82#define tg3_flag(tp, flag) \
83 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
84#define tg3_flag_set(tp, flag) \
85 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
86#define tg3_flag_clear(tp, flag) \
87 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000090#define TG3_MAJ_NUM 3
Matt Carlson64cad2a2011-04-25 12:42:50 +000091#define TG3_MIN_NUM 118
Matt Carlson6867c842010-07-11 09:31:44 +000092#define DRV_MODULE_VERSION \
93 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlson64cad2a2011-04-25 12:42:50 +000094#define DRV_MODULE_RELDATE "April 22, 2011"
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96#define TG3_DEF_MAC_MODE 0
97#define TG3_DEF_RX_MODE 0
98#define TG3_DEF_TX_MODE 0
99#define TG3_DEF_MSG_ENABLE \
100 (NETIF_MSG_DRV | \
101 NETIF_MSG_PROBE | \
102 NETIF_MSG_LINK | \
103 NETIF_MSG_TIMER | \
104 NETIF_MSG_IFDOWN | \
105 NETIF_MSG_IFUP | \
106 NETIF_MSG_RX_ERR | \
107 NETIF_MSG_TX_ERR)
108
109/* length of time before we decide the hardware is borked,
110 * and dev->tx_timeout() should be called to fix the problem
111 */
Joe Perches63c3a662011-04-26 08:12:10 +0000112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define TG3_TX_TIMEOUT (5 * HZ)
114
115/* hardware minimum and maximum for a single frame's data payload */
116#define TG3_MIN_MTU 60
117#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000118 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/* These numbers seem to be hard coded in the NIC firmware somehow.
121 * You can't change the ring sizes, but you can change where you place
122 * them in the NIC onboard memory.
123 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000124#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000125 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000126 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000128#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000129 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000130 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000132#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/* Do not place this n-ring entries value into the tp struct itself,
135 * we really want to expose these constants to GCC so that modulo et
136 * al. operations are done with shifts and masks instead of with
137 * hw multiply/modulo instructions. Another solution would be to
138 * replace things like '% foo' with '& (foo - 1)'.
139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141#define TG3_TX_RING_SIZE 512
142#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
143
Matt Carlson2c49a442010-09-30 10:34:35 +0000144#define TG3_RX_STD_RING_BYTES(tp) \
145 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
146#define TG3_RX_JMB_RING_BYTES(tp) \
147 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
148#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000149 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
151 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
153
Matt Carlson287be122009-08-28 13:58:46 +0000154#define TG3_DMA_BYTE_ENAB 64
155
156#define TG3_RX_STD_DMA_SZ 1536
157#define TG3_RX_JMB_DMA_SZ 9046
158
159#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
160
161#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
162#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Matt Carlson2c49a442010-09-30 10:34:35 +0000164#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
165 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000166
Matt Carlson2c49a442010-09-30 10:34:35 +0000167#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
168 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000169
Matt Carlsond2757fc2010-04-12 06:58:27 +0000170/* Due to a hardware bug, the 5701 can only DMA to memory addresses
171 * that are at least dword aligned when used in PCIX mode. The driver
172 * works around this bug by double copying the packet. This workaround
173 * is built into the normal double copy length check for efficiency.
174 *
175 * However, the double copy is only necessary on those architectures
176 * where unaligned memory accesses are inefficient. For those architectures
177 * where unaligned memory accesses incur little penalty, we can reintegrate
178 * the 5701 in the normal rx path. Doing so saves a device structure
179 * dereference by hardcoding the double copy threshold in place.
180 */
181#define TG3_RX_COPY_THRESHOLD 256
182#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
183 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
184#else
185 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
186#endif
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000189#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Matt Carlsonad829262008-11-21 17:16:16 -0800191#define TG3_RAW_IP_ALIGN 2
192
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000193#define TG3_FW_UPDATE_TIMEOUT_SEC 5
194
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800195#define FIRMWARE_TG3 "tigon/tg3.bin"
196#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
197#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000200 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
203MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
204MODULE_LICENSE("GPL");
205MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800206MODULE_FIRMWARE(FIRMWARE_TG3);
207MODULE_FIRMWARE(FIRMWARE_TG3TSO);
208MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
211module_param(tg3_debug, int, 0);
212MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
213
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000214static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700288 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
289 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
290 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
291 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
292 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
293 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
294 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
295 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296};
297
298MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
299
Andreas Mohr50da8592006-08-14 23:54:30 -0700300static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000302} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 { "rx_octets" },
304 { "rx_fragments" },
305 { "rx_ucast_packets" },
306 { "rx_mcast_packets" },
307 { "rx_bcast_packets" },
308 { "rx_fcs_errors" },
309 { "rx_align_errors" },
310 { "rx_xon_pause_rcvd" },
311 { "rx_xoff_pause_rcvd" },
312 { "rx_mac_ctrl_rcvd" },
313 { "rx_xoff_entered" },
314 { "rx_frame_too_long_errors" },
315 { "rx_jabbers" },
316 { "rx_undersize_packets" },
317 { "rx_in_length_errors" },
318 { "rx_out_length_errors" },
319 { "rx_64_or_less_octet_packets" },
320 { "rx_65_to_127_octet_packets" },
321 { "rx_128_to_255_octet_packets" },
322 { "rx_256_to_511_octet_packets" },
323 { "rx_512_to_1023_octet_packets" },
324 { "rx_1024_to_1522_octet_packets" },
325 { "rx_1523_to_2047_octet_packets" },
326 { "rx_2048_to_4095_octet_packets" },
327 { "rx_4096_to_8191_octet_packets" },
328 { "rx_8192_to_9022_octet_packets" },
329
330 { "tx_octets" },
331 { "tx_collisions" },
332
333 { "tx_xon_sent" },
334 { "tx_xoff_sent" },
335 { "tx_flow_control" },
336 { "tx_mac_errors" },
337 { "tx_single_collisions" },
338 { "tx_mult_collisions" },
339 { "tx_deferred" },
340 { "tx_excessive_collisions" },
341 { "tx_late_collisions" },
342 { "tx_collide_2times" },
343 { "tx_collide_3times" },
344 { "tx_collide_4times" },
345 { "tx_collide_5times" },
346 { "tx_collide_6times" },
347 { "tx_collide_7times" },
348 { "tx_collide_8times" },
349 { "tx_collide_9times" },
350 { "tx_collide_10times" },
351 { "tx_collide_11times" },
352 { "tx_collide_12times" },
353 { "tx_collide_13times" },
354 { "tx_collide_14times" },
355 { "tx_collide_15times" },
356 { "tx_ucast_packets" },
357 { "tx_mcast_packets" },
358 { "tx_bcast_packets" },
359 { "tx_carrier_sense_errors" },
360 { "tx_discards" },
361 { "tx_errors" },
362
363 { "dma_writeq_full" },
364 { "dma_write_prioq_full" },
365 { "rxbds_empty" },
366 { "rx_discards" },
Matt Carlson4d958472011-04-20 07:57:35 +0000367 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 { "rx_errors" },
369 { "rx_threshold_hit" },
370
371 { "dma_readq_full" },
372 { "dma_read_prioq_full" },
373 { "tx_comp_queue_full" },
374
375 { "ring_set_send_prod_index" },
376 { "ring_status_update" },
377 { "nic_irqs" },
378 { "nic_avoided_irqs" },
379 { "nic_tx_threshold_hit" }
380};
381
Matt Carlson48fa55a2011-04-13 11:05:06 +0000382#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
383
384
Andreas Mohr50da8592006-08-14 23:54:30 -0700385static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700386 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000387} ethtool_test_keys[] = {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700388 { "nvram test (online) " },
389 { "link test (online) " },
390 { "register test (offline)" },
391 { "memory test (offline)" },
392 { "loopback test (offline)" },
393 { "interrupt test (offline)" },
394};
395
Matt Carlson48fa55a2011-04-13 11:05:06 +0000396#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
397
398
Michael Chanb401e9e2005-12-19 16:27:04 -0800399static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
400{
401 writel(val, tp->regs + off);
402}
403
404static u32 tg3_read32(struct tg3 *tp, u32 off)
405{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000406 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800407}
408
Matt Carlson0d3031d2007-10-10 18:02:43 -0700409static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
410{
411 writel(val, tp->aperegs + off);
412}
413
414static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
415{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000416 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
420{
Michael Chan68929142005-08-09 20:17:14 -0700421 unsigned long flags;
422
423 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700424 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
425 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700426 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700427}
428
429static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
430{
431 writel(val, tp->regs + off);
432 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Michael Chan68929142005-08-09 20:17:14 -0700435static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
436{
437 unsigned long flags;
438 u32 val;
439
440 spin_lock_irqsave(&tp->indirect_lock, flags);
441 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
442 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
443 spin_unlock_irqrestore(&tp->indirect_lock, flags);
444 return val;
445}
446
447static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
448{
449 unsigned long flags;
450
451 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
452 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
453 TG3_64BIT_REG_LOW, val);
454 return;
455 }
Matt Carlson66711e662009-11-13 13:03:49 +0000456 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700457 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
458 TG3_64BIT_REG_LOW, val);
459 return;
460 }
461
462 spin_lock_irqsave(&tp->indirect_lock, flags);
463 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
464 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
465 spin_unlock_irqrestore(&tp->indirect_lock, flags);
466
467 /* In indirect mode when disabling interrupts, we also need
468 * to clear the interrupt bit in the GRC local ctrl register.
469 */
470 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
471 (val == 0x1)) {
472 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
473 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
474 }
475}
476
477static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
478{
479 unsigned long flags;
480 u32 val;
481
482 spin_lock_irqsave(&tp->indirect_lock, flags);
483 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
484 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
485 spin_unlock_irqrestore(&tp->indirect_lock, flags);
486 return val;
487}
488
Michael Chanb401e9e2005-12-19 16:27:04 -0800489/* usec_wait specifies the wait time in usec when writing to certain registers
490 * where it is unsafe to read back the register without some delay.
491 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
492 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
493 */
494static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Joe Perches63c3a662011-04-26 08:12:10 +0000496 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800497 /* Non-posted methods */
498 tp->write32(tp, off, val);
499 else {
500 /* Posted method */
501 tg3_write32(tp, off, val);
502 if (usec_wait)
503 udelay(usec_wait);
504 tp->read32(tp, off);
505 }
506 /* Wait again after the read for the posted method to guarantee that
507 * the wait time is met.
508 */
509 if (usec_wait)
510 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Michael Chan09ee9292005-08-09 20:17:00 -0700513static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
514{
515 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000516 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700517 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700518}
519
Michael Chan20094932005-08-09 20:16:32 -0700520static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 void __iomem *mbox = tp->regs + off;
523 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000524 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000526 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 readl(mbox);
528}
529
Michael Chanb5d37722006-09-27 16:06:21 -0700530static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
531{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000532 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700533}
534
535static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
536{
537 writel(val, tp->regs + off + GRCMBOX_BASE);
538}
539
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000540#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700541#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000542#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
543#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
544#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700545
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000546#define tw32(reg, val) tp->write32(tp, reg, val)
547#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
548#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
549#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
552{
Michael Chan68929142005-08-09 20:17:14 -0700553 unsigned long flags;
554
Michael Chanb5d37722006-09-27 16:06:21 -0700555 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
556 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
557 return;
558
Michael Chan68929142005-08-09 20:17:14 -0700559 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000560 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700561 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
562 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Michael Chanbbadf502006-04-06 21:46:34 -0700564 /* Always leave this as zero. */
565 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
566 } else {
567 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
568 tw32_f(TG3PCI_MEM_WIN_DATA, val);
569
570 /* Always leave this as zero. */
571 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
572 }
Michael Chan68929142005-08-09 20:17:14 -0700573 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
577{
Michael Chan68929142005-08-09 20:17:14 -0700578 unsigned long flags;
579
Michael Chanb5d37722006-09-27 16:06:21 -0700580 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
581 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
582 *val = 0;
583 return;
584 }
585
Michael Chan68929142005-08-09 20:17:14 -0700586 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000587 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700588 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
589 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Michael Chanbbadf502006-04-06 21:46:34 -0700591 /* Always leave this as zero. */
592 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
593 } else {
594 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
595 *val = tr32(TG3PCI_MEM_WIN_DATA);
596
597 /* Always leave this as zero. */
598 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
599 }
Michael Chan68929142005-08-09 20:17:14 -0700600 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Matt Carlson0d3031d2007-10-10 18:02:43 -0700603static void tg3_ape_lock_init(struct tg3 *tp)
604{
605 int i;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000606 u32 regbase;
607
608 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
609 regbase = TG3_APE_LOCK_GRANT;
610 else
611 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700612
613 /* Make sure the driver hasn't any stale locks. */
614 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000615 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700616}
617
618static int tg3_ape_lock(struct tg3 *tp, int locknum)
619{
620 int i, off;
621 int ret = 0;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000622 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700623
Joe Perches63c3a662011-04-26 08:12:10 +0000624 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700625 return 0;
626
627 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000628 case TG3_APE_LOCK_GRC:
629 case TG3_APE_LOCK_MEM:
630 break;
631 default:
632 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700633 }
634
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000635 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
636 req = TG3_APE_LOCK_REQ;
637 gnt = TG3_APE_LOCK_GRANT;
638 } else {
639 req = TG3_APE_PER_LOCK_REQ;
640 gnt = TG3_APE_PER_LOCK_GRANT;
641 }
642
Matt Carlson0d3031d2007-10-10 18:02:43 -0700643 off = 4 * locknum;
644
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000645 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700646
647 /* Wait for up to 1 millisecond to acquire lock. */
648 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000649 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700650 if (status == APE_LOCK_GRANT_DRIVER)
651 break;
652 udelay(10);
653 }
654
655 if (status != APE_LOCK_GRANT_DRIVER) {
656 /* Revoke the lock request. */
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000657 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700658 APE_LOCK_GRANT_DRIVER);
659
660 ret = -EBUSY;
661 }
662
663 return ret;
664}
665
666static void tg3_ape_unlock(struct tg3 *tp, int locknum)
667{
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000668 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700669
Joe Perches63c3a662011-04-26 08:12:10 +0000670 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700671 return;
672
673 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000674 case TG3_APE_LOCK_GRC:
675 case TG3_APE_LOCK_MEM:
676 break;
677 default:
678 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700679 }
680
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000681 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
682 gnt = TG3_APE_LOCK_GRANT;
683 else
684 gnt = TG3_APE_PER_LOCK_GRANT;
685
686 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689static void tg3_disable_ints(struct tg3 *tp)
690{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000691 int i;
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 tw32(TG3PCI_MISC_HOST_CTRL,
694 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000695 for (i = 0; i < tp->irq_max; i++)
696 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699static void tg3_enable_ints(struct tg3 *tp)
700{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000701 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000702
Michael Chanbbe832c2005-06-24 20:20:04 -0700703 tp->irq_sync = 0;
704 wmb();
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 tw32(TG3PCI_MISC_HOST_CTRL,
707 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000708
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000709 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000710 for (i = 0; i < tp->irq_cnt; i++) {
711 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000712
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000713 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000714 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000715 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
716
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000717 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000718 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000719
720 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000721 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000722 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
723 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
724 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000725 tw32(HOSTCC_MODE, tp->coal_now);
726
727 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Matt Carlson17375d22009-08-28 14:02:18 +0000730static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700731{
Matt Carlson17375d22009-08-28 14:02:18 +0000732 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000733 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700734 unsigned int work_exists = 0;
735
736 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000737 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700738 if (sblk->status & SD_STATUS_LINK_CHG)
739 work_exists = 1;
740 }
741 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000742 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000743 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700744 work_exists = 1;
745
746 return work_exists;
747}
748
Matt Carlson17375d22009-08-28 14:02:18 +0000749/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700750 * similar to tg3_enable_ints, but it accurately determines whether there
751 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400752 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 */
Matt Carlson17375d22009-08-28 14:02:18 +0000754static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Matt Carlson17375d22009-08-28 14:02:18 +0000756 struct tg3 *tp = tnapi->tp;
757
Matt Carlson898a56f2009-08-28 14:02:40 +0000758 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 mmiowb();
760
David S. Millerfac9b832005-05-18 22:46:34 -0700761 /* When doing tagged status, this work check is unnecessary.
762 * The last_tag we write above tells the chip which piece of
763 * work we've completed.
764 */
Joe Perches63c3a662011-04-26 08:12:10 +0000765 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700766 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000767 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static void tg3_switch_clocks(struct tg3 *tp)
771{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000772 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 u32 orig_clock_ctrl;
774
Joe Perches63c3a662011-04-26 08:12:10 +0000775 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700776 return;
777
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000778 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 orig_clock_ctrl = clock_ctrl;
781 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
782 CLOCK_CTRL_CLKRUN_OENABLE |
783 0x1f);
784 tp->pci_clock_ctrl = clock_ctrl;
785
Joe Perches63c3a662011-04-26 08:12:10 +0000786 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800788 tw32_wait_f(TG3PCI_CLOCK_CTRL,
789 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800792 tw32_wait_f(TG3PCI_CLOCK_CTRL,
793 clock_ctrl |
794 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
795 40);
796 tw32_wait_f(TG3PCI_CLOCK_CTRL,
797 clock_ctrl | (CLOCK_CTRL_ALTCLK),
798 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800800 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
803#define PHY_BUSY_LOOPS 5000
804
805static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
806{
807 u32 frame_val;
808 unsigned int loops;
809 int ret;
810
811 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
812 tw32_f(MAC_MI_MODE,
813 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
814 udelay(80);
815 }
816
817 *val = 0x0;
818
Matt Carlson882e9792009-09-01 13:21:36 +0000819 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 MI_COM_PHY_ADDR_MASK);
821 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
822 MI_COM_REG_ADDR_MASK);
823 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 tw32_f(MAC_MI_COM, frame_val);
826
827 loops = PHY_BUSY_LOOPS;
828 while (loops != 0) {
829 udelay(10);
830 frame_val = tr32(MAC_MI_COM);
831
832 if ((frame_val & MI_COM_BUSY) == 0) {
833 udelay(5);
834 frame_val = tr32(MAC_MI_COM);
835 break;
836 }
837 loops -= 1;
838 }
839
840 ret = -EBUSY;
841 if (loops != 0) {
842 *val = frame_val & MI_COM_DATA_MASK;
843 ret = 0;
844 }
845
846 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
847 tw32_f(MAC_MI_MODE, tp->mi_mode);
848 udelay(80);
849 }
850
851 return ret;
852}
853
854static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
855{
856 u32 frame_val;
857 unsigned int loops;
858 int ret;
859
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000860 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700861 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
862 return 0;
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
865 tw32_f(MAC_MI_MODE,
866 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
867 udelay(80);
868 }
869
Matt Carlson882e9792009-09-01 13:21:36 +0000870 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 MI_COM_PHY_ADDR_MASK);
872 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
873 MI_COM_REG_ADDR_MASK);
874 frame_val |= (val & MI_COM_DATA_MASK);
875 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 tw32_f(MAC_MI_COM, frame_val);
878
879 loops = PHY_BUSY_LOOPS;
880 while (loops != 0) {
881 udelay(10);
882 frame_val = tr32(MAC_MI_COM);
883 if ((frame_val & MI_COM_BUSY) == 0) {
884 udelay(5);
885 frame_val = tr32(MAC_MI_COM);
886 break;
887 }
888 loops -= 1;
889 }
890
891 ret = -EBUSY;
892 if (loops != 0)
893 ret = 0;
894
895 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
896 tw32_f(MAC_MI_MODE, tp->mi_mode);
897 udelay(80);
898 }
899
900 return ret;
901}
902
Matt Carlsonb0988c12011-04-20 07:57:39 +0000903static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
904{
905 int err;
906
907 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
908 if (err)
909 goto done;
910
911 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
912 if (err)
913 goto done;
914
915 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
916 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
917 if (err)
918 goto done;
919
920 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
921
922done:
923 return err;
924}
925
926static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
927{
928 int err;
929
930 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
931 if (err)
932 goto done;
933
934 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
935 if (err)
936 goto done;
937
938 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
939 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
940 if (err)
941 goto done;
942
943 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
944
945done:
946 return err;
947}
948
949static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
950{
951 int err;
952
953 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
954 if (!err)
955 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
956
957 return err;
958}
959
960static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
961{
962 int err;
963
964 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
965 if (!err)
966 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
967
968 return err;
969}
970
Matt Carlson15ee95c2011-04-20 07:57:40 +0000971static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
972{
973 int err;
974
975 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
976 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
977 MII_TG3_AUXCTL_SHDWSEL_MISC);
978 if (!err)
979 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
980
981 return err;
982}
983
Matt Carlsonb4bd2922011-04-20 07:57:41 +0000984static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
985{
986 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
987 set |= MII_TG3_AUXCTL_MISC_WREN;
988
989 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
990}
991
Matt Carlson1d36ba42011-04-20 07:57:42 +0000992#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
993 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
994 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
995 MII_TG3_AUXCTL_ACTL_TX_6DB)
996
997#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
998 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
999 MII_TG3_AUXCTL_ACTL_TX_6DB);
1000
Matt Carlson95e28692008-05-25 23:44:14 -07001001static int tg3_bmcr_reset(struct tg3 *tp)
1002{
1003 u32 phy_control;
1004 int limit, err;
1005
1006 /* OK, reset it, and poll the BMCR_RESET bit until it
1007 * clears or we time out.
1008 */
1009 phy_control = BMCR_RESET;
1010 err = tg3_writephy(tp, MII_BMCR, phy_control);
1011 if (err != 0)
1012 return -EBUSY;
1013
1014 limit = 5000;
1015 while (limit--) {
1016 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1017 if (err != 0)
1018 return -EBUSY;
1019
1020 if ((phy_control & BMCR_RESET) == 0) {
1021 udelay(40);
1022 break;
1023 }
1024 udelay(10);
1025 }
Roel Kluind4675b52009-02-12 16:33:27 -08001026 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001027 return -EBUSY;
1028
1029 return 0;
1030}
1031
Matt Carlson158d7ab2008-05-29 01:37:54 -07001032static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1033{
Francois Romieu3d165432009-01-19 16:56:50 -08001034 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001035 u32 val;
1036
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001037 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001038
1039 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001040 val = -EIO;
1041
1042 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001043
1044 return val;
1045}
1046
1047static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1048{
Francois Romieu3d165432009-01-19 16:56:50 -08001049 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001050 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001051
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001052 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001053
1054 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001055 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001056
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001057 spin_unlock_bh(&tp->lock);
1058
1059 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001060}
1061
1062static int tg3_mdio_reset(struct mii_bus *bp)
1063{
1064 return 0;
1065}
1066
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001067static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001068{
1069 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001070 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001071
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001072 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001073 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001074 case PHY_ID_BCM50610:
1075 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001076 val = MAC_PHYCFG2_50610_LED_MODES;
1077 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001078 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001079 val = MAC_PHYCFG2_AC131_LED_MODES;
1080 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001081 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001082 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1083 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001084 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001085 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1086 break;
1087 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001088 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001089 }
1090
1091 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1092 tw32(MAC_PHYCFG2, val);
1093
1094 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001095 val &= ~(MAC_PHYCFG1_RGMII_INT |
1096 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1097 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001098 tw32(MAC_PHYCFG1, val);
1099
1100 return;
1101 }
1102
Joe Perches63c3a662011-04-26 08:12:10 +00001103 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001104 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1105 MAC_PHYCFG2_FMODE_MASK_MASK |
1106 MAC_PHYCFG2_GMODE_MASK_MASK |
1107 MAC_PHYCFG2_ACT_MASK_MASK |
1108 MAC_PHYCFG2_QUAL_MASK_MASK |
1109 MAC_PHYCFG2_INBAND_ENABLE;
1110
1111 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001112
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001113 val = tr32(MAC_PHYCFG1);
1114 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1115 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001116 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1117 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001118 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001119 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001120 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1121 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001122 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1123 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1124 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001125
Matt Carlsona9daf362008-05-25 23:49:44 -07001126 val = tr32(MAC_EXT_RGMII_MODE);
1127 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1128 MAC_RGMII_MODE_RX_QUALITY |
1129 MAC_RGMII_MODE_RX_ACTIVITY |
1130 MAC_RGMII_MODE_RX_ENG_DET |
1131 MAC_RGMII_MODE_TX_ENABLE |
1132 MAC_RGMII_MODE_TX_LOWPWR |
1133 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001134 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1135 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001136 val |= MAC_RGMII_MODE_RX_INT_B |
1137 MAC_RGMII_MODE_RX_QUALITY |
1138 MAC_RGMII_MODE_RX_ACTIVITY |
1139 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001140 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001141 val |= MAC_RGMII_MODE_TX_ENABLE |
1142 MAC_RGMII_MODE_TX_LOWPWR |
1143 MAC_RGMII_MODE_TX_RESET;
1144 }
1145 tw32(MAC_EXT_RGMII_MODE, val);
1146}
1147
Matt Carlson158d7ab2008-05-29 01:37:54 -07001148static void tg3_mdio_start(struct tg3 *tp)
1149{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001150 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1151 tw32_f(MAC_MI_MODE, tp->mi_mode);
1152 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001153
Joe Perches63c3a662011-04-26 08:12:10 +00001154 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001155 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1156 tg3_mdio_config_5785(tp);
1157}
1158
1159static int tg3_mdio_init(struct tg3 *tp)
1160{
1161 int i;
1162 u32 reg;
1163 struct phy_device *phydev;
1164
Joe Perches63c3a662011-04-26 08:12:10 +00001165 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001166 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001167
Matt Carlson9c7df912010-06-05 17:24:36 +00001168 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001169
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001170 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1171 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1172 else
1173 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1174 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001175 if (is_serdes)
1176 tp->phy_addr += 7;
1177 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001178 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001179
Matt Carlson158d7ab2008-05-29 01:37:54 -07001180 tg3_mdio_start(tp);
1181
Joe Perches63c3a662011-04-26 08:12:10 +00001182 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001183 return 0;
1184
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001185 tp->mdio_bus = mdiobus_alloc();
1186 if (tp->mdio_bus == NULL)
1187 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001188
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001189 tp->mdio_bus->name = "tg3 mdio bus";
1190 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001191 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001192 tp->mdio_bus->priv = tp;
1193 tp->mdio_bus->parent = &tp->pdev->dev;
1194 tp->mdio_bus->read = &tg3_mdio_read;
1195 tp->mdio_bus->write = &tg3_mdio_write;
1196 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001197 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001198 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001199
1200 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001201 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001202
1203 /* The bus registration will look for all the PHYs on the mdio bus.
1204 * Unfortunately, it does not ensure the PHY is powered up before
1205 * accessing the PHY ID registers. A chip reset is the
1206 * quickest way to bring the device back to an operational state..
1207 */
1208 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1209 tg3_bmcr_reset(tp);
1210
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001211 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001212 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001213 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001214 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001215 return i;
1216 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001217
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001218 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001219
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001220 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001221 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001222 mdiobus_unregister(tp->mdio_bus);
1223 mdiobus_free(tp->mdio_bus);
1224 return -ENODEV;
1225 }
1226
1227 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001228 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001229 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001230 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001231 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001232 case PHY_ID_BCM50610:
1233 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001234 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001235 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001236 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001237 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001238 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001239 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001240 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001241 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001242 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001243 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001244 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001245 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001246 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001247 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001248 case PHY_ID_RTL8201E:
1249 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001250 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001251 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001252 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001253 break;
1254 }
1255
Joe Perches63c3a662011-04-26 08:12:10 +00001256 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001257
1258 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1259 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001260
1261 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001262}
1263
1264static void tg3_mdio_fini(struct tg3 *tp)
1265{
Joe Perches63c3a662011-04-26 08:12:10 +00001266 if (tg3_flag(tp, MDIOBUS_INITED)) {
1267 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001268 mdiobus_unregister(tp->mdio_bus);
1269 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001270 }
1271}
1272
Matt Carlson95e28692008-05-25 23:44:14 -07001273/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001274static inline void tg3_generate_fw_event(struct tg3 *tp)
1275{
1276 u32 val;
1277
1278 val = tr32(GRC_RX_CPU_EVENT);
1279 val |= GRC_RX_CPU_DRIVER_EVENT;
1280 tw32_f(GRC_RX_CPU_EVENT, val);
1281
1282 tp->last_event_jiffies = jiffies;
1283}
1284
1285#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1286
1287/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001288static void tg3_wait_for_event_ack(struct tg3 *tp)
1289{
1290 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001291 unsigned int delay_cnt;
1292 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001293
Matt Carlson4ba526c2008-08-15 14:10:04 -07001294 /* If enough time has passed, no wait is necessary. */
1295 time_remain = (long)(tp->last_event_jiffies + 1 +
1296 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1297 (long)jiffies;
1298 if (time_remain < 0)
1299 return;
1300
1301 /* Check if we can shorten the wait time. */
1302 delay_cnt = jiffies_to_usecs(time_remain);
1303 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1304 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1305 delay_cnt = (delay_cnt >> 3) + 1;
1306
1307 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001308 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1309 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001310 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001311 }
1312}
1313
1314/* tp->lock is held. */
1315static void tg3_ump_link_report(struct tg3 *tp)
1316{
1317 u32 reg;
1318 u32 val;
1319
Joe Perches63c3a662011-04-26 08:12:10 +00001320 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson95e28692008-05-25 23:44:14 -07001321 return;
1322
1323 tg3_wait_for_event_ack(tp);
1324
1325 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1326
1327 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1328
1329 val = 0;
1330 if (!tg3_readphy(tp, MII_BMCR, &reg))
1331 val = reg << 16;
1332 if (!tg3_readphy(tp, MII_BMSR, &reg))
1333 val |= (reg & 0xffff);
1334 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1335
1336 val = 0;
1337 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1338 val = reg << 16;
1339 if (!tg3_readphy(tp, MII_LPA, &reg))
1340 val |= (reg & 0xffff);
1341 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1342
1343 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001344 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001345 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1346 val = reg << 16;
1347 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1348 val |= (reg & 0xffff);
1349 }
1350 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1351
1352 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1353 val = reg << 16;
1354 else
1355 val = 0;
1356 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1357
Matt Carlson4ba526c2008-08-15 14:10:04 -07001358 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001359}
1360
1361static void tg3_link_report(struct tg3 *tp)
1362{
1363 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001364 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001365 tg3_ump_link_report(tp);
1366 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001367 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1368 (tp->link_config.active_speed == SPEED_1000 ?
1369 1000 :
1370 (tp->link_config.active_speed == SPEED_100 ?
1371 100 : 10)),
1372 (tp->link_config.active_duplex == DUPLEX_FULL ?
1373 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001374
Joe Perches05dbe002010-02-17 19:44:19 +00001375 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1376 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1377 "on" : "off",
1378 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1379 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001380
1381 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1382 netdev_info(tp->dev, "EEE is %s\n",
1383 tp->setlpicnt ? "enabled" : "disabled");
1384
Matt Carlson95e28692008-05-25 23:44:14 -07001385 tg3_ump_link_report(tp);
1386 }
1387}
1388
1389static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1390{
1391 u16 miireg;
1392
Steve Glendinninge18ce342008-12-16 02:00:00 -08001393 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001394 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001395 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001396 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001397 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001398 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1399 else
1400 miireg = 0;
1401
1402 return miireg;
1403}
1404
1405static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1406{
1407 u16 miireg;
1408
Steve Glendinninge18ce342008-12-16 02:00:00 -08001409 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001410 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001411 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001412 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001413 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001414 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1415 else
1416 miireg = 0;
1417
1418 return miireg;
1419}
1420
Matt Carlson95e28692008-05-25 23:44:14 -07001421static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1422{
1423 u8 cap = 0;
1424
1425 if (lcladv & ADVERTISE_1000XPAUSE) {
1426 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1427 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001428 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001429 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001430 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001431 } else {
1432 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001433 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001434 }
1435 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1436 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001437 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001438 }
1439
1440 return cap;
1441}
1442
Matt Carlsonf51f3562008-05-25 23:45:08 -07001443static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001444{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001445 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001446 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001447 u32 old_rx_mode = tp->rx_mode;
1448 u32 old_tx_mode = tp->tx_mode;
1449
Joe Perches63c3a662011-04-26 08:12:10 +00001450 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001451 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001452 else
1453 autoneg = tp->link_config.autoneg;
1454
Joe Perches63c3a662011-04-26 08:12:10 +00001455 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001456 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001457 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001458 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001459 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001460 } else
1461 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001462
Matt Carlsonf51f3562008-05-25 23:45:08 -07001463 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001464
Steve Glendinninge18ce342008-12-16 02:00:00 -08001465 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001466 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1467 else
1468 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1469
Matt Carlsonf51f3562008-05-25 23:45:08 -07001470 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001471 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001472
Steve Glendinninge18ce342008-12-16 02:00:00 -08001473 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001474 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1475 else
1476 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1477
Matt Carlsonf51f3562008-05-25 23:45:08 -07001478 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001479 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001480}
1481
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001482static void tg3_adjust_link(struct net_device *dev)
1483{
1484 u8 oldflowctrl, linkmesg = 0;
1485 u32 mac_mode, lcl_adv, rmt_adv;
1486 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001487 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001488
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001489 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001490
1491 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1492 MAC_MODE_HALF_DUPLEX);
1493
1494 oldflowctrl = tp->link_config.active_flowctrl;
1495
1496 if (phydev->link) {
1497 lcl_adv = 0;
1498 rmt_adv = 0;
1499
1500 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1501 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001502 else if (phydev->speed == SPEED_1000 ||
1503 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001504 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001505 else
1506 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001507
1508 if (phydev->duplex == DUPLEX_HALF)
1509 mac_mode |= MAC_MODE_HALF_DUPLEX;
1510 else {
1511 lcl_adv = tg3_advert_flowctrl_1000T(
1512 tp->link_config.flowctrl);
1513
1514 if (phydev->pause)
1515 rmt_adv = LPA_PAUSE_CAP;
1516 if (phydev->asym_pause)
1517 rmt_adv |= LPA_PAUSE_ASYM;
1518 }
1519
1520 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1521 } else
1522 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1523
1524 if (mac_mode != tp->mac_mode) {
1525 tp->mac_mode = mac_mode;
1526 tw32_f(MAC_MODE, tp->mac_mode);
1527 udelay(40);
1528 }
1529
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001530 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1531 if (phydev->speed == SPEED_10)
1532 tw32(MAC_MI_STAT,
1533 MAC_MI_STAT_10MBPS_MODE |
1534 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1535 else
1536 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1537 }
1538
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001539 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1540 tw32(MAC_TX_LENGTHS,
1541 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1542 (6 << TX_LENGTHS_IPG_SHIFT) |
1543 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1544 else
1545 tw32(MAC_TX_LENGTHS,
1546 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1547 (6 << TX_LENGTHS_IPG_SHIFT) |
1548 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1549
1550 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1551 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1552 phydev->speed != tp->link_config.active_speed ||
1553 phydev->duplex != tp->link_config.active_duplex ||
1554 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001555 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001556
1557 tp->link_config.active_speed = phydev->speed;
1558 tp->link_config.active_duplex = phydev->duplex;
1559
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001560 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001561
1562 if (linkmesg)
1563 tg3_link_report(tp);
1564}
1565
1566static int tg3_phy_init(struct tg3 *tp)
1567{
1568 struct phy_device *phydev;
1569
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001570 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001571 return 0;
1572
1573 /* Bring the PHY back to a known state. */
1574 tg3_bmcr_reset(tp);
1575
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001576 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001577
1578 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001579 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001580 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001581 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001582 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001583 return PTR_ERR(phydev);
1584 }
1585
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001586 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001587 switch (phydev->interface) {
1588 case PHY_INTERFACE_MODE_GMII:
1589 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001590 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001591 phydev->supported &= (PHY_GBIT_FEATURES |
1592 SUPPORTED_Pause |
1593 SUPPORTED_Asym_Pause);
1594 break;
1595 }
1596 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001597 case PHY_INTERFACE_MODE_MII:
1598 phydev->supported &= (PHY_BASIC_FEATURES |
1599 SUPPORTED_Pause |
1600 SUPPORTED_Asym_Pause);
1601 break;
1602 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001603 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001604 return -EINVAL;
1605 }
1606
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001607 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001608
1609 phydev->advertising = phydev->supported;
1610
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001611 return 0;
1612}
1613
1614static void tg3_phy_start(struct tg3 *tp)
1615{
1616 struct phy_device *phydev;
1617
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001618 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001619 return;
1620
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001621 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001622
Matt Carlson80096062010-08-02 11:26:06 +00001623 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1624 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001625 phydev->speed = tp->link_config.orig_speed;
1626 phydev->duplex = tp->link_config.orig_duplex;
1627 phydev->autoneg = tp->link_config.orig_autoneg;
1628 phydev->advertising = tp->link_config.orig_advertising;
1629 }
1630
1631 phy_start(phydev);
1632
1633 phy_start_aneg(phydev);
1634}
1635
1636static void tg3_phy_stop(struct tg3 *tp)
1637{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001638 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001639 return;
1640
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001641 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001642}
1643
1644static void tg3_phy_fini(struct tg3 *tp)
1645{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001646 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001647 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001648 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001649 }
1650}
1651
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001652static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1653{
1654 u32 phytest;
1655
1656 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1657 u32 phy;
1658
1659 tg3_writephy(tp, MII_TG3_FET_TEST,
1660 phytest | MII_TG3_FET_SHADOW_EN);
1661 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1662 if (enable)
1663 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1664 else
1665 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1666 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1667 }
1668 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1669 }
1670}
1671
Matt Carlson6833c042008-11-21 17:18:59 -08001672static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1673{
1674 u32 reg;
1675
Joe Perches63c3a662011-04-26 08:12:10 +00001676 if (!tg3_flag(tp, 5705_PLUS) ||
1677 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001678 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001679 return;
1680
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001681 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001682 tg3_phy_fet_toggle_apd(tp, enable);
1683 return;
1684 }
1685
Matt Carlson6833c042008-11-21 17:18:59 -08001686 reg = MII_TG3_MISC_SHDW_WREN |
1687 MII_TG3_MISC_SHDW_SCR5_SEL |
1688 MII_TG3_MISC_SHDW_SCR5_LPED |
1689 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1690 MII_TG3_MISC_SHDW_SCR5_SDTL |
1691 MII_TG3_MISC_SHDW_SCR5_C125OE;
1692 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1693 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1694
1695 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1696
1697
1698 reg = MII_TG3_MISC_SHDW_WREN |
1699 MII_TG3_MISC_SHDW_APD_SEL |
1700 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1701 if (enable)
1702 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1703
1704 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1705}
1706
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001707static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1708{
1709 u32 phy;
1710
Joe Perches63c3a662011-04-26 08:12:10 +00001711 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001712 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001713 return;
1714
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001715 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001716 u32 ephy;
1717
Matt Carlson535ef6e2009-08-25 10:09:36 +00001718 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1719 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1720
1721 tg3_writephy(tp, MII_TG3_FET_TEST,
1722 ephy | MII_TG3_FET_SHADOW_EN);
1723 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001724 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001725 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001726 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001727 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1728 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001729 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001730 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001731 }
1732 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00001733 int ret;
1734
1735 ret = tg3_phy_auxctl_read(tp,
1736 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
1737 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001738 if (enable)
1739 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1740 else
1741 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001742 tg3_phy_auxctl_write(tp,
1743 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001744 }
1745 }
1746}
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748static void tg3_phy_set_wirespeed(struct tg3 *tp)
1749{
Matt Carlson15ee95c2011-04-20 07:57:40 +00001750 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 u32 val;
1752
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001753 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return;
1755
Matt Carlson15ee95c2011-04-20 07:57:40 +00001756 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
1757 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001758 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
1759 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001762static void tg3_phy_apply_otp(struct tg3 *tp)
1763{
1764 u32 otp, phy;
1765
1766 if (!tp->phy_otp)
1767 return;
1768
1769 otp = tp->phy_otp;
1770
Matt Carlson1d36ba42011-04-20 07:57:42 +00001771 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
1772 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001773
1774 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1775 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1776 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1777
1778 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1779 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1780 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1781
1782 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1783 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1784 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1785
1786 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1787 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1788
1789 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1790 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1791
1792 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1793 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1794 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1795
Matt Carlson1d36ba42011-04-20 07:57:42 +00001796 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001797}
1798
Matt Carlson52b02d02010-10-14 10:37:41 +00001799static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1800{
1801 u32 val;
1802
1803 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1804 return;
1805
1806 tp->setlpicnt = 0;
1807
1808 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1809 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001810 tp->link_config.active_duplex == DUPLEX_FULL &&
1811 (tp->link_config.active_speed == SPEED_100 ||
1812 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001813 u32 eeectl;
1814
1815 if (tp->link_config.active_speed == SPEED_1000)
1816 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1817 else
1818 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1819
1820 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1821
Matt Carlson3110f5f52010-12-06 08:28:50 +00001822 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1823 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001824
Matt Carlsonb0c59432011-05-19 12:12:48 +00001825 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
1826 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00001827 tp->setlpicnt = 2;
1828 }
1829
1830 if (!tp->setlpicnt) {
1831 val = tr32(TG3_CPMU_EEE_MODE);
1832 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1833 }
1834}
1835
Matt Carlsonb0c59432011-05-19 12:12:48 +00001836static void tg3_phy_eee_enable(struct tg3 *tp)
1837{
1838 u32 val;
1839
1840 if (tp->link_config.active_speed == SPEED_1000 &&
1841 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1842 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
1843 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
1844 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
1845 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0003);
1846 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
1847 }
1848
1849 val = tr32(TG3_CPMU_EEE_MODE);
1850 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
1851}
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853static int tg3_wait_macro_done(struct tg3 *tp)
1854{
1855 int limit = 100;
1856
1857 while (limit--) {
1858 u32 tmp32;
1859
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001860 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if ((tmp32 & 0x1000) == 0)
1862 break;
1863 }
1864 }
Roel Kluind4675b52009-02-12 16:33:27 -08001865 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return -EBUSY;
1867
1868 return 0;
1869}
1870
1871static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1872{
1873 static const u32 test_pat[4][6] = {
1874 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1875 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1876 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1877 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1878 };
1879 int chan;
1880
1881 for (chan = 0; chan < 4; chan++) {
1882 int i;
1883
1884 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1885 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001886 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 for (i = 0; i < 6; i++)
1889 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1890 test_pat[chan][i]);
1891
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001892 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (tg3_wait_macro_done(tp)) {
1894 *resetp = 1;
1895 return -EBUSY;
1896 }
1897
1898 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1899 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001900 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (tg3_wait_macro_done(tp)) {
1902 *resetp = 1;
1903 return -EBUSY;
1904 }
1905
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001906 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (tg3_wait_macro_done(tp)) {
1908 *resetp = 1;
1909 return -EBUSY;
1910 }
1911
1912 for (i = 0; i < 6; i += 2) {
1913 u32 low, high;
1914
1915 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1916 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1917 tg3_wait_macro_done(tp)) {
1918 *resetp = 1;
1919 return -EBUSY;
1920 }
1921 low &= 0x7fff;
1922 high &= 0x000f;
1923 if (low != test_pat[chan][i] ||
1924 high != test_pat[chan][i+1]) {
1925 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1926 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1927 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1928
1929 return -EBUSY;
1930 }
1931 }
1932 }
1933
1934 return 0;
1935}
1936
1937static int tg3_phy_reset_chanpat(struct tg3 *tp)
1938{
1939 int chan;
1940
1941 for (chan = 0; chan < 4; chan++) {
1942 int i;
1943
1944 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1945 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001946 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 for (i = 0; i < 6; i++)
1948 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001949 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (tg3_wait_macro_done(tp))
1951 return -EBUSY;
1952 }
1953
1954 return 0;
1955}
1956
1957static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1958{
1959 u32 reg32, phy9_orig;
1960 int retries, do_phy_reset, err;
1961
1962 retries = 10;
1963 do_phy_reset = 1;
1964 do {
1965 if (do_phy_reset) {
1966 err = tg3_bmcr_reset(tp);
1967 if (err)
1968 return err;
1969 do_phy_reset = 0;
1970 }
1971
1972 /* Disable transmitter and interrupt. */
1973 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1974 continue;
1975
1976 reg32 |= 0x3000;
1977 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1978
1979 /* Set full-duplex, 1000 mbps. */
1980 tg3_writephy(tp, MII_BMCR,
1981 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1982
1983 /* Set to master mode. */
1984 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1985 continue;
1986
1987 tg3_writephy(tp, MII_TG3_CTRL,
1988 (MII_TG3_CTRL_AS_MASTER |
1989 MII_TG3_CTRL_ENABLE_AS_MASTER));
1990
Matt Carlson1d36ba42011-04-20 07:57:42 +00001991 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
1992 if (err)
1993 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001996 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1999 if (!err)
2000 break;
2001 } while (--retries);
2002
2003 err = tg3_phy_reset_chanpat(tp);
2004 if (err)
2005 return err;
2006
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002007 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002010 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Matt Carlson1d36ba42011-04-20 07:57:42 +00002012 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
2015
2016 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2017 reg32 &= ~0x3000;
2018 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2019 } else if (!err)
2020 err = -EBUSY;
2021
2022 return err;
2023}
2024
2025/* This will reset the tigon3 PHY if there is no valid
2026 * link unless the FORCE argument is non-zero.
2027 */
2028static int tg3_phy_reset(struct tg3 *tp)
2029{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002030 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 int err;
2032
Michael Chan60189dd2006-12-17 17:08:07 -08002033 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002034 val = tr32(GRC_MISC_CFG);
2035 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2036 udelay(40);
2037 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002038 err = tg3_readphy(tp, MII_BMSR, &val);
2039 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 if (err != 0)
2041 return -EBUSY;
2042
Michael Chanc8e1e822006-04-29 18:55:17 -07002043 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2044 netif_carrier_off(tp->dev);
2045 tg3_link_report(tp);
2046 }
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2049 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2050 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2051 err = tg3_phy_reset_5703_4_5(tp);
2052 if (err)
2053 return err;
2054 goto out;
2055 }
2056
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002057 cpmuctrl = 0;
2058 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2059 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2060 cpmuctrl = tr32(TG3_CPMU_CTRL);
2061 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2062 tw32(TG3_CPMU_CTRL,
2063 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2064 }
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 err = tg3_bmcr_reset(tp);
2067 if (err)
2068 return err;
2069
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002070 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002071 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2072 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002073
2074 tw32(TG3_CPMU_CTRL, cpmuctrl);
2075 }
2076
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002077 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2078 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002079 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2080 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2081 CPMU_LSPD_1000MB_MACCLK_12_5) {
2082 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2083 udelay(40);
2084 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2085 }
2086 }
2087
Joe Perches63c3a662011-04-26 08:12:10 +00002088 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002089 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002090 return 0;
2091
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002092 tg3_phy_apply_otp(tp);
2093
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002094 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002095 tg3_phy_toggle_apd(tp, true);
2096 else
2097 tg3_phy_toggle_apd(tp, false);
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002100 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2101 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002102 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2103 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002104 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002106
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002107 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002108 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2109 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002111
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002112 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002113 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2114 tg3_phydsp_write(tp, 0x000a, 0x310b);
2115 tg3_phydsp_write(tp, 0x201f, 0x9506);
2116 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2117 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2118 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002119 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002120 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2121 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2122 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2123 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2124 tg3_writephy(tp, MII_TG3_TEST1,
2125 MII_TG3_TEST1_TRIM_EN | 0x4);
2126 } else
2127 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2128
2129 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2130 }
Michael Chanc424cb22006-04-29 18:56:34 -07002131 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 /* Set Extended packet length bit (bit 14) on all chips that */
2134 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002135 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002137 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002138 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002140 err = tg3_phy_auxctl_read(tp,
2141 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2142 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002143 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2144 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 }
2146
2147 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2148 * jumbo frames transmission.
2149 */
Joe Perches63c3a662011-04-26 08:12:10 +00002150 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002151 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002152 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002153 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 }
2155
Michael Chan715116a2006-09-27 16:09:25 -07002156 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002157 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002158 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002159 }
2160
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002161 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 tg3_phy_set_wirespeed(tp);
2163 return 0;
2164}
2165
2166static void tg3_frob_aux_power(struct tg3 *tp)
2167{
Matt Carlson683644b2011-03-09 16:58:23 +00002168 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Matt Carlson334355a2010-01-20 16:58:10 +00002170 /* The GPIOs do something completely different on 57765. */
Joe Perches63c3a662011-04-26 08:12:10 +00002171 if (!tg3_flag(tp, IS_NIC) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002172 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002173 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 return;
2175
Matt Carlson683644b2011-03-09 16:58:23 +00002176 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2177 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +00002178 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2179 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) &&
Matt Carlson683644b2011-03-09 16:58:23 +00002180 tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002181 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002183 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002184
Michael Chanbc1c7562006-03-20 17:48:03 -08002185 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002186 if (dev_peer) {
2187 struct tg3 *tp_peer = netdev_priv(dev_peer);
2188
Joe Perches63c3a662011-04-26 08:12:10 +00002189 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002190 return;
2191
Joe Perches63c3a662011-04-26 08:12:10 +00002192 if (tg3_flag(tp_peer, WOL_ENABLE) ||
2193 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002194 need_vaux = true;
2195 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Joe Perches63c3a662011-04-26 08:12:10 +00002198 if (tg3_flag(tp, WOL_ENABLE) || tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002199 need_vaux = true;
2200
2201 if (need_vaux) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2203 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002204 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2205 (GRC_LCLCTRL_GPIO_OE0 |
2206 GRC_LCLCTRL_GPIO_OE1 |
2207 GRC_LCLCTRL_GPIO_OE2 |
2208 GRC_LCLCTRL_GPIO_OUTPUT0 |
2209 GRC_LCLCTRL_GPIO_OUTPUT1),
2210 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002211 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2212 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002213 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2214 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2215 GRC_LCLCTRL_GPIO_OE1 |
2216 GRC_LCLCTRL_GPIO_OE2 |
2217 GRC_LCLCTRL_GPIO_OUTPUT0 |
2218 GRC_LCLCTRL_GPIO_OUTPUT1 |
2219 tp->grc_local_ctrl;
2220 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2221
2222 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2223 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2224
2225 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2226 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 } else {
2228 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002229 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Michael Chandc56b7d2005-12-19 16:26:28 -08002231 /* Workaround to prevent overdrawing Amps. */
2232 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2233 ASIC_REV_5714) {
2234 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002235 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2236 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002237 }
2238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 /* On 5753 and variants, GPIO2 cannot be used. */
2240 no_gpio2 = tp->nic_sram_data_cfg &
2241 NIC_SRAM_DATA_CFG_NO_GPIO2;
2242
Michael Chandc56b7d2005-12-19 16:26:28 -08002243 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 GRC_LCLCTRL_GPIO_OE1 |
2245 GRC_LCLCTRL_GPIO_OE2 |
2246 GRC_LCLCTRL_GPIO_OUTPUT1 |
2247 GRC_LCLCTRL_GPIO_OUTPUT2;
2248 if (no_gpio2) {
2249 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2250 GRC_LCLCTRL_GPIO_OUTPUT2);
2251 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002252 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2253 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
2255 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2256
Michael Chanb401e9e2005-12-19 16:27:04 -08002257 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2258 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 if (!no_gpio2) {
2261 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002262 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2263 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
2265 }
2266 } else {
2267 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2268 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002269 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2270 (GRC_LCLCTRL_GPIO_OE1 |
2271 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Michael Chanb401e9e2005-12-19 16:27:04 -08002273 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2274 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Michael Chanb401e9e2005-12-19 16:27:04 -08002276 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2277 (GRC_LCLCTRL_GPIO_OE1 |
2278 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
2280 }
2281}
2282
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002283static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2284{
2285 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2286 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002287 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002288 if (speed != SPEED_10)
2289 return 1;
2290 } else if (speed == SPEED_10)
2291 return 1;
2292
2293 return 0;
2294}
2295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296static int tg3_setup_phy(struct tg3 *, int);
2297
2298#define RESET_KIND_SHUTDOWN 0
2299#define RESET_KIND_INIT 1
2300#define RESET_KIND_SUSPEND 2
2301
2302static void tg3_write_sig_post_reset(struct tg3 *, int);
2303static int tg3_halt_cpu(struct tg3 *, u32);
2304
Matt Carlson0a459aa2008-11-03 16:54:15 -08002305static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002306{
Matt Carlsonce057f02007-11-12 21:08:03 -08002307 u32 val;
2308
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002309 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002310 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2311 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2312 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2313
2314 sg_dig_ctrl |=
2315 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2316 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2317 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2318 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002319 return;
Michael Chan51297242007-02-13 12:17:57 -08002320 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002321
Michael Chan60189dd2006-12-17 17:08:07 -08002322 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002323 tg3_bmcr_reset(tp);
2324 val = tr32(GRC_MISC_CFG);
2325 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2326 udelay(40);
2327 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002328 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002329 u32 phytest;
2330 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2331 u32 phy;
2332
2333 tg3_writephy(tp, MII_ADVERTISE, 0);
2334 tg3_writephy(tp, MII_BMCR,
2335 BMCR_ANENABLE | BMCR_ANRESTART);
2336
2337 tg3_writephy(tp, MII_TG3_FET_TEST,
2338 phytest | MII_TG3_FET_SHADOW_EN);
2339 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2340 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2341 tg3_writephy(tp,
2342 MII_TG3_FET_SHDW_AUXMODE4,
2343 phy);
2344 }
2345 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2346 }
2347 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002348 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002349 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2350 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002351
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002352 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2353 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2354 MII_TG3_AUXCTL_PCTL_VREG_11V;
2355 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002356 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002357
Michael Chan15c3b692006-03-22 01:06:52 -08002358 /* The PHY should not be powered down on some chips because
2359 * of bugs.
2360 */
2361 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2362 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2363 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002364 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002365 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002366
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002367 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2368 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002369 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2370 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2371 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2372 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2373 }
2374
Michael Chan15c3b692006-03-22 01:06:52 -08002375 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2376}
2377
Matt Carlson3f007892008-11-03 16:51:36 -08002378/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002379static int tg3_nvram_lock(struct tg3 *tp)
2380{
Joe Perches63c3a662011-04-26 08:12:10 +00002381 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002382 int i;
2383
2384 if (tp->nvram_lock_cnt == 0) {
2385 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2386 for (i = 0; i < 8000; i++) {
2387 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2388 break;
2389 udelay(20);
2390 }
2391 if (i == 8000) {
2392 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2393 return -ENODEV;
2394 }
2395 }
2396 tp->nvram_lock_cnt++;
2397 }
2398 return 0;
2399}
2400
2401/* tp->lock is held. */
2402static void tg3_nvram_unlock(struct tg3 *tp)
2403{
Joe Perches63c3a662011-04-26 08:12:10 +00002404 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002405 if (tp->nvram_lock_cnt > 0)
2406 tp->nvram_lock_cnt--;
2407 if (tp->nvram_lock_cnt == 0)
2408 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2409 }
2410}
2411
2412/* tp->lock is held. */
2413static void tg3_enable_nvram_access(struct tg3 *tp)
2414{
Joe Perches63c3a662011-04-26 08:12:10 +00002415 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002416 u32 nvaccess = tr32(NVRAM_ACCESS);
2417
2418 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2419 }
2420}
2421
2422/* tp->lock is held. */
2423static void tg3_disable_nvram_access(struct tg3 *tp)
2424{
Joe Perches63c3a662011-04-26 08:12:10 +00002425 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002426 u32 nvaccess = tr32(NVRAM_ACCESS);
2427
2428 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2429 }
2430}
2431
2432static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2433 u32 offset, u32 *val)
2434{
2435 u32 tmp;
2436 int i;
2437
2438 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2439 return -EINVAL;
2440
2441 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2442 EEPROM_ADDR_DEVID_MASK |
2443 EEPROM_ADDR_READ);
2444 tw32(GRC_EEPROM_ADDR,
2445 tmp |
2446 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2447 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2448 EEPROM_ADDR_ADDR_MASK) |
2449 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2450
2451 for (i = 0; i < 1000; i++) {
2452 tmp = tr32(GRC_EEPROM_ADDR);
2453
2454 if (tmp & EEPROM_ADDR_COMPLETE)
2455 break;
2456 msleep(1);
2457 }
2458 if (!(tmp & EEPROM_ADDR_COMPLETE))
2459 return -EBUSY;
2460
Matt Carlson62cedd12009-04-20 14:52:29 -07002461 tmp = tr32(GRC_EEPROM_DATA);
2462
2463 /*
2464 * The data will always be opposite the native endian
2465 * format. Perform a blind byteswap to compensate.
2466 */
2467 *val = swab32(tmp);
2468
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002469 return 0;
2470}
2471
2472#define NVRAM_CMD_TIMEOUT 10000
2473
2474static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2475{
2476 int i;
2477
2478 tw32(NVRAM_CMD, nvram_cmd);
2479 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2480 udelay(10);
2481 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2482 udelay(10);
2483 break;
2484 }
2485 }
2486
2487 if (i == NVRAM_CMD_TIMEOUT)
2488 return -EBUSY;
2489
2490 return 0;
2491}
2492
2493static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2494{
Joe Perches63c3a662011-04-26 08:12:10 +00002495 if (tg3_flag(tp, NVRAM) &&
2496 tg3_flag(tp, NVRAM_BUFFERED) &&
2497 tg3_flag(tp, FLASH) &&
2498 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002499 (tp->nvram_jedecnum == JEDEC_ATMEL))
2500
2501 addr = ((addr / tp->nvram_pagesize) <<
2502 ATMEL_AT45DB0X1B_PAGE_POS) +
2503 (addr % tp->nvram_pagesize);
2504
2505 return addr;
2506}
2507
2508static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2509{
Joe Perches63c3a662011-04-26 08:12:10 +00002510 if (tg3_flag(tp, NVRAM) &&
2511 tg3_flag(tp, NVRAM_BUFFERED) &&
2512 tg3_flag(tp, FLASH) &&
2513 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002514 (tp->nvram_jedecnum == JEDEC_ATMEL))
2515
2516 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2517 tp->nvram_pagesize) +
2518 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2519
2520 return addr;
2521}
2522
Matt Carlsone4f34112009-02-25 14:25:00 +00002523/* NOTE: Data read in from NVRAM is byteswapped according to
2524 * the byteswapping settings for all other register accesses.
2525 * tg3 devices are BE devices, so on a BE machine, the data
2526 * returned will be exactly as it is seen in NVRAM. On a LE
2527 * machine, the 32-bit value will be byteswapped.
2528 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002529static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2530{
2531 int ret;
2532
Joe Perches63c3a662011-04-26 08:12:10 +00002533 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002534 return tg3_nvram_read_using_eeprom(tp, offset, val);
2535
2536 offset = tg3_nvram_phys_addr(tp, offset);
2537
2538 if (offset > NVRAM_ADDR_MSK)
2539 return -EINVAL;
2540
2541 ret = tg3_nvram_lock(tp);
2542 if (ret)
2543 return ret;
2544
2545 tg3_enable_nvram_access(tp);
2546
2547 tw32(NVRAM_ADDR, offset);
2548 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2549 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2550
2551 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002552 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002553
2554 tg3_disable_nvram_access(tp);
2555
2556 tg3_nvram_unlock(tp);
2557
2558 return ret;
2559}
2560
Matt Carlsona9dc5292009-02-25 14:25:30 +00002561/* Ensures NVRAM data is in bytestream format. */
2562static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002563{
2564 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002565 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002566 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002567 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002568 return res;
2569}
2570
2571/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002572static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2573{
2574 u32 addr_high, addr_low;
2575 int i;
2576
2577 addr_high = ((tp->dev->dev_addr[0] << 8) |
2578 tp->dev->dev_addr[1]);
2579 addr_low = ((tp->dev->dev_addr[2] << 24) |
2580 (tp->dev->dev_addr[3] << 16) |
2581 (tp->dev->dev_addr[4] << 8) |
2582 (tp->dev->dev_addr[5] << 0));
2583 for (i = 0; i < 4; i++) {
2584 if (i == 1 && skip_mac_1)
2585 continue;
2586 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2587 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2588 }
2589
2590 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2591 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2592 for (i = 0; i < 12; i++) {
2593 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2594 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2595 }
2596 }
2597
2598 addr_high = (tp->dev->dev_addr[0] +
2599 tp->dev->dev_addr[1] +
2600 tp->dev->dev_addr[2] +
2601 tp->dev->dev_addr[3] +
2602 tp->dev->dev_addr[4] +
2603 tp->dev->dev_addr[5]) &
2604 TX_BACKOFF_SEED_MASK;
2605 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2606}
2607
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002608static void tg3_enable_register_access(struct tg3 *tp)
2609{
2610 /*
2611 * Make sure register accesses (indirect or otherwise) will function
2612 * correctly.
2613 */
2614 pci_write_config_dword(tp->pdev,
2615 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2616}
2617
2618static int tg3_power_up(struct tg3 *tp)
2619{
2620 tg3_enable_register_access(tp);
2621
2622 pci_set_power_state(tp->pdev, PCI_D0);
2623
2624 /* Switch out of Vaux if it is a NIC */
Joe Perches63c3a662011-04-26 08:12:10 +00002625 if (tg3_flag(tp, IS_NIC))
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002626 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
2627
2628 return 0;
2629}
2630
2631static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
2633 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002634 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002636 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002637
2638 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00002639 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002640 u16 lnkctl;
2641
2642 pci_read_config_word(tp->pdev,
2643 tp->pcie_cap + PCI_EXP_LNKCTL,
2644 &lnkctl);
2645 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2646 pci_write_config_word(tp->pdev,
2647 tp->pcie_cap + PCI_EXP_LNKCTL,
2648 lnkctl);
2649 }
2650
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2652 tw32(TG3PCI_MISC_HOST_CTRL,
2653 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2654
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002655 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00002656 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002657
Joe Perches63c3a662011-04-26 08:12:10 +00002658 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002659 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002660 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00002661 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002662 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002663 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002664
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002665 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002666
Matt Carlson80096062010-08-02 11:26:06 +00002667 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002668
2669 tp->link_config.orig_speed = phydev->speed;
2670 tp->link_config.orig_duplex = phydev->duplex;
2671 tp->link_config.orig_autoneg = phydev->autoneg;
2672 tp->link_config.orig_advertising = phydev->advertising;
2673
2674 advertising = ADVERTISED_TP |
2675 ADVERTISED_Pause |
2676 ADVERTISED_Autoneg |
2677 ADVERTISED_10baseT_Half;
2678
Joe Perches63c3a662011-04-26 08:12:10 +00002679 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
2680 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002681 advertising |=
2682 ADVERTISED_100baseT_Half |
2683 ADVERTISED_100baseT_Full |
2684 ADVERTISED_10baseT_Full;
2685 else
2686 advertising |= ADVERTISED_10baseT_Full;
2687 }
2688
2689 phydev->advertising = advertising;
2690
2691 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002692
2693 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002694 if (phyid != PHY_ID_BCMAC131) {
2695 phyid &= PHY_BCM_OUI_MASK;
2696 if (phyid == PHY_BCM_OUI_1 ||
2697 phyid == PHY_BCM_OUI_2 ||
2698 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002699 do_low_power = true;
2700 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002701 }
Matt Carlsondd477002008-05-25 23:45:58 -07002702 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002703 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002704
Matt Carlson80096062010-08-02 11:26:06 +00002705 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2706 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002707 tp->link_config.orig_speed = tp->link_config.speed;
2708 tp->link_config.orig_duplex = tp->link_config.duplex;
2709 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002712 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002713 tp->link_config.speed = SPEED_10;
2714 tp->link_config.duplex = DUPLEX_HALF;
2715 tp->link_config.autoneg = AUTONEG_ENABLE;
2716 tg3_setup_phy(tp, 0);
2717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 }
2719
Michael Chanb5d37722006-09-27 16:06:21 -07002720 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2721 u32 val;
2722
2723 val = tr32(GRC_VCPU_EXT_CTRL);
2724 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00002725 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002726 int i;
2727 u32 val;
2728
2729 for (i = 0; i < 200; i++) {
2730 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2731 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2732 break;
2733 msleep(1);
2734 }
2735 }
Joe Perches63c3a662011-04-26 08:12:10 +00002736 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07002737 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2738 WOL_DRV_STATE_SHUTDOWN |
2739 WOL_DRV_WOL |
2740 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002741
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002742 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 u32 mac_mode;
2744
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002745 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002746 if (do_low_power &&
2747 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
2748 tg3_phy_auxctl_write(tp,
2749 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
2750 MII_TG3_AUXCTL_PCTL_WOL_EN |
2751 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2752 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07002753 udelay(40);
2754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002756 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002757 mac_mode = MAC_MODE_PORT_MODE_GMII;
2758 else
2759 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002761 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2762 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2763 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00002764 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002765 SPEED_100 : SPEED_10;
2766 if (tg3_5700_link_polarity(tp, speed))
2767 mac_mode |= MAC_MODE_LINK_POLARITY;
2768 else
2769 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 } else {
2772 mac_mode = MAC_MODE_PORT_MODE_TBI;
2773 }
2774
Joe Perches63c3a662011-04-26 08:12:10 +00002775 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 tw32(MAC_LED_CTRL, tp->led_ctrl);
2777
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002778 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00002779 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
2780 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002781 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Joe Perches63c3a662011-04-26 08:12:10 +00002783 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002784 mac_mode |= MAC_MODE_APE_TX_EN |
2785 MAC_MODE_APE_RX_EN |
2786 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 tw32_f(MAC_MODE, mac_mode);
2789 udelay(100);
2790
2791 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2792 udelay(10);
2793 }
2794
Joe Perches63c3a662011-04-26 08:12:10 +00002795 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2797 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2798 u32 base_val;
2799
2800 base_val = tp->pci_clock_ctrl;
2801 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2802 CLOCK_CTRL_TXCLK_DISABLE);
2803
Michael Chanb401e9e2005-12-19 16:27:04 -08002804 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2805 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00002806 } else if (tg3_flag(tp, 5780_CLASS) ||
2807 tg3_flag(tp, CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002808 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002809 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00002810 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 u32 newbits1, newbits2;
2812
2813 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2814 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2815 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2816 CLOCK_CTRL_TXCLK_DISABLE |
2817 CLOCK_CTRL_ALTCLK);
2818 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00002819 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 newbits1 = CLOCK_CTRL_625_CORE;
2821 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2822 } else {
2823 newbits1 = CLOCK_CTRL_ALTCLK;
2824 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2825 }
2826
Michael Chanb401e9e2005-12-19 16:27:04 -08002827 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2828 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
Michael Chanb401e9e2005-12-19 16:27:04 -08002830 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2831 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Joe Perches63c3a662011-04-26 08:12:10 +00002833 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 u32 newbits3;
2835
2836 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2837 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2838 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2839 CLOCK_CTRL_TXCLK_DISABLE |
2840 CLOCK_CTRL_44MHZ_CORE);
2841 } else {
2842 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2843 }
2844
Michael Chanb401e9e2005-12-19 16:27:04 -08002845 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2846 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 }
2848 }
2849
Joe Perches63c3a662011-04-26 08:12:10 +00002850 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002851 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 tg3_frob_aux_power(tp);
2854
2855 /* Workaround for unstable PLL clock */
2856 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2857 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2858 u32 val = tr32(0x7d00);
2859
2860 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2861 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00002862 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002863 int err;
2864
2865 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002867 if (!err)
2868 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 }
2871
Michael Chanbbadf502006-04-06 21:46:34 -07002872 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 return 0;
2875}
2876
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002877static void tg3_power_down(struct tg3 *tp)
2878{
2879 tg3_power_down_prepare(tp);
2880
Joe Perches63c3a662011-04-26 08:12:10 +00002881 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002882 pci_set_power_state(tp->pdev, PCI_D3hot);
2883}
2884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2886{
2887 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2888 case MII_TG3_AUX_STAT_10HALF:
2889 *speed = SPEED_10;
2890 *duplex = DUPLEX_HALF;
2891 break;
2892
2893 case MII_TG3_AUX_STAT_10FULL:
2894 *speed = SPEED_10;
2895 *duplex = DUPLEX_FULL;
2896 break;
2897
2898 case MII_TG3_AUX_STAT_100HALF:
2899 *speed = SPEED_100;
2900 *duplex = DUPLEX_HALF;
2901 break;
2902
2903 case MII_TG3_AUX_STAT_100FULL:
2904 *speed = SPEED_100;
2905 *duplex = DUPLEX_FULL;
2906 break;
2907
2908 case MII_TG3_AUX_STAT_1000HALF:
2909 *speed = SPEED_1000;
2910 *duplex = DUPLEX_HALF;
2911 break;
2912
2913 case MII_TG3_AUX_STAT_1000FULL:
2914 *speed = SPEED_1000;
2915 *duplex = DUPLEX_FULL;
2916 break;
2917
2918 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002919 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002920 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2921 SPEED_10;
2922 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2923 DUPLEX_HALF;
2924 break;
2925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 *speed = SPEED_INVALID;
2927 *duplex = DUPLEX_INVALID;
2928 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930}
2931
Matt Carlson42b64a42011-05-19 12:12:49 +00002932static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933{
Matt Carlson42b64a42011-05-19 12:12:49 +00002934 int err = 0;
2935 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
Matt Carlson42b64a42011-05-19 12:12:49 +00002937 new_adv = ADVERTISE_CSMA;
2938 if (advertise & ADVERTISED_10baseT_Half)
2939 new_adv |= ADVERTISE_10HALF;
2940 if (advertise & ADVERTISED_10baseT_Full)
2941 new_adv |= ADVERTISE_10FULL;
2942 if (advertise & ADVERTISED_100baseT_Half)
2943 new_adv |= ADVERTISE_100HALF;
2944 if (advertise & ADVERTISED_100baseT_Full)
2945 new_adv |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Matt Carlson42b64a42011-05-19 12:12:49 +00002947 new_adv |= tg3_advert_flowctrl_1000T(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Matt Carlson42b64a42011-05-19 12:12:49 +00002949 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
2950 if (err)
2951 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Matt Carlson42b64a42011-05-19 12:12:49 +00002953 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
2954 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002955
Matt Carlson42b64a42011-05-19 12:12:49 +00002956 new_adv = 0;
2957 if (advertise & ADVERTISED_1000baseT_Half)
2958 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2959 if (advertise & ADVERTISED_1000baseT_Full)
2960 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002961
Matt Carlson42b64a42011-05-19 12:12:49 +00002962 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2963 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2964 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2965 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Matt Carlson42b64a42011-05-19 12:12:49 +00002967 err = tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2968 if (err)
2969 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002970
Matt Carlson42b64a42011-05-19 12:12:49 +00002971 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2972 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Matt Carlson42b64a42011-05-19 12:12:49 +00002974 tw32(TG3_CPMU_EEE_MODE,
2975 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002976
Matt Carlson42b64a42011-05-19 12:12:49 +00002977 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2978 if (!err) {
2979 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00002980
Matt Carlson21a00ab2011-01-25 15:58:55 +00002981 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
2982 case ASIC_REV_5717:
2983 case ASIC_REV_57765:
2984 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
2985 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
2986 MII_TG3_DSP_CH34TP2_HIBW01);
2987 /* Fall through */
2988 case ASIC_REV_5719:
2989 val = MII_TG3_DSP_TAP26_ALNOKO |
2990 MII_TG3_DSP_TAP26_RMRXSTO |
2991 MII_TG3_DSP_TAP26_OPCSINPT;
2992 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
2993 }
Matt Carlson52b02d02010-10-14 10:37:41 +00002994
Matt Carlsona6b68da2010-12-06 08:28:52 +00002995 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00002996 /* Advertise 100-BaseTX EEE ability */
2997 if (advertise & ADVERTISED_100baseT_Full)
2998 val |= MDIO_AN_EEE_ADV_100TX;
2999 /* Advertise 1000-BaseT EEE ability */
3000 if (advertise & ADVERTISED_1000baseT_Full)
3001 val |= MDIO_AN_EEE_ADV_1000T;
3002 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlson52b02d02010-10-14 10:37:41 +00003003
Matt Carlson42b64a42011-05-19 12:12:49 +00003004 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3005 if (!err)
3006 err = err2;
3007 }
3008
3009done:
3010 return err;
3011}
3012
3013static void tg3_phy_copper_begin(struct tg3 *tp)
3014{
3015 u32 new_adv;
3016 int i;
3017
3018 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3019 new_adv = ADVERTISED_10baseT_Half |
3020 ADVERTISED_10baseT_Full;
3021 if (tg3_flag(tp, WOL_SPEED_100MB))
3022 new_adv |= ADVERTISED_100baseT_Half |
3023 ADVERTISED_100baseT_Full;
3024
3025 tg3_phy_autoneg_cfg(tp, new_adv,
3026 FLOW_CTRL_TX | FLOW_CTRL_RX);
3027 } else if (tp->link_config.speed == SPEED_INVALID) {
3028 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3029 tp->link_config.advertising &=
3030 ~(ADVERTISED_1000baseT_Half |
3031 ADVERTISED_1000baseT_Full);
3032
3033 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3034 tp->link_config.flowctrl);
3035 } else {
3036 /* Asking for a specific link mode. */
3037 if (tp->link_config.speed == SPEED_1000) {
3038 if (tp->link_config.duplex == DUPLEX_FULL)
3039 new_adv = ADVERTISED_1000baseT_Full;
3040 else
3041 new_adv = ADVERTISED_1000baseT_Half;
3042 } else if (tp->link_config.speed == SPEED_100) {
3043 if (tp->link_config.duplex == DUPLEX_FULL)
3044 new_adv = ADVERTISED_100baseT_Full;
3045 else
3046 new_adv = ADVERTISED_100baseT_Half;
3047 } else {
3048 if (tp->link_config.duplex == DUPLEX_FULL)
3049 new_adv = ADVERTISED_10baseT_Full;
3050 else
3051 new_adv = ADVERTISED_10baseT_Half;
3052 }
3053
3054 tg3_phy_autoneg_cfg(tp, new_adv,
3055 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003056 }
3057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3059 tp->link_config.speed != SPEED_INVALID) {
3060 u32 bmcr, orig_bmcr;
3061
3062 tp->link_config.active_speed = tp->link_config.speed;
3063 tp->link_config.active_duplex = tp->link_config.duplex;
3064
3065 bmcr = 0;
3066 switch (tp->link_config.speed) {
3067 default:
3068 case SPEED_10:
3069 break;
3070
3071 case SPEED_100:
3072 bmcr |= BMCR_SPEED100;
3073 break;
3074
3075 case SPEED_1000:
3076 bmcr |= TG3_BMCR_SPEED1000;
3077 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
3080 if (tp->link_config.duplex == DUPLEX_FULL)
3081 bmcr |= BMCR_FULLDPLX;
3082
3083 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3084 (bmcr != orig_bmcr)) {
3085 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3086 for (i = 0; i < 1500; i++) {
3087 u32 tmp;
3088
3089 udelay(10);
3090 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3091 tg3_readphy(tp, MII_BMSR, &tmp))
3092 continue;
3093 if (!(tmp & BMSR_LSTATUS)) {
3094 udelay(40);
3095 break;
3096 }
3097 }
3098 tg3_writephy(tp, MII_BMCR, bmcr);
3099 udelay(40);
3100 }
3101 } else {
3102 tg3_writephy(tp, MII_BMCR,
3103 BMCR_ANENABLE | BMCR_ANRESTART);
3104 }
3105}
3106
3107static int tg3_init_5401phy_dsp(struct tg3 *tp)
3108{
3109 int err;
3110
3111 /* Turn off tap power management. */
3112 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003113 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003115 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3116 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3117 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3118 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3119 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
3121 udelay(40);
3122
3123 return err;
3124}
3125
Michael Chan3600d912006-12-07 00:21:48 -08003126static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
Michael Chan3600d912006-12-07 00:21:48 -08003128 u32 adv_reg, all_mask = 0;
3129
3130 if (mask & ADVERTISED_10baseT_Half)
3131 all_mask |= ADVERTISE_10HALF;
3132 if (mask & ADVERTISED_10baseT_Full)
3133 all_mask |= ADVERTISE_10FULL;
3134 if (mask & ADVERTISED_100baseT_Half)
3135 all_mask |= ADVERTISE_100HALF;
3136 if (mask & ADVERTISED_100baseT_Full)
3137 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
3139 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3140 return 0;
3141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 if ((adv_reg & all_mask) != all_mask)
3143 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003144 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 u32 tg3_ctrl;
3146
Michael Chan3600d912006-12-07 00:21:48 -08003147 all_mask = 0;
3148 if (mask & ADVERTISED_1000baseT_Half)
3149 all_mask |= ADVERTISE_1000HALF;
3150 if (mask & ADVERTISED_1000baseT_Full)
3151 all_mask |= ADVERTISE_1000FULL;
3152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3154 return 0;
3155
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 if ((tg3_ctrl & all_mask) != all_mask)
3157 return 0;
3158 }
3159 return 1;
3160}
3161
Matt Carlsonef167e22007-12-20 20:10:01 -08003162static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3163{
3164 u32 curadv, reqadv;
3165
3166 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3167 return 1;
3168
3169 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3170 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3171
3172 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3173 if (curadv != reqadv)
3174 return 0;
3175
Joe Perches63c3a662011-04-26 08:12:10 +00003176 if (tg3_flag(tp, PAUSE_AUTONEG))
Matt Carlsonef167e22007-12-20 20:10:01 -08003177 tg3_readphy(tp, MII_LPA, rmtadv);
3178 } else {
3179 /* Reprogram the advertisement register, even if it
3180 * does not affect the current link. If the link
3181 * gets renegotiated in the future, we can save an
3182 * additional renegotiation cycle by advertising
3183 * it correctly in the first place.
3184 */
3185 if (curadv != reqadv) {
3186 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3187 ADVERTISE_PAUSE_ASYM);
3188 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3189 }
3190 }
3191
3192 return 1;
3193}
3194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3196{
3197 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003198 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003199 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 u16 current_speed;
3201 u8 current_duplex;
3202 int i, err;
3203
3204 tw32(MAC_EVENT, 0);
3205
3206 tw32_f(MAC_STATUS,
3207 (MAC_STATUS_SYNC_CHANGED |
3208 MAC_STATUS_CFG_CHANGED |
3209 MAC_STATUS_MI_COMPLETION |
3210 MAC_STATUS_LNKSTATE_CHANGED));
3211 udelay(40);
3212
Matt Carlson8ef21422008-05-02 16:47:53 -07003213 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3214 tw32_f(MAC_MI_MODE,
3215 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3216 udelay(80);
3217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003219 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
3221 /* Some third-party PHYs need to be reset on link going
3222 * down.
3223 */
3224 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3225 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3226 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3227 netif_carrier_ok(tp->dev)) {
3228 tg3_readphy(tp, MII_BMSR, &bmsr);
3229 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3230 !(bmsr & BMSR_LSTATUS))
3231 force_reset = 1;
3232 }
3233 if (force_reset)
3234 tg3_phy_reset(tp);
3235
Matt Carlson79eb6902010-02-17 15:17:03 +00003236 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 tg3_readphy(tp, MII_BMSR, &bmsr);
3238 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003239 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 bmsr = 0;
3241
3242 if (!(bmsr & BMSR_LSTATUS)) {
3243 err = tg3_init_5401phy_dsp(tp);
3244 if (err)
3245 return err;
3246
3247 tg3_readphy(tp, MII_BMSR, &bmsr);
3248 for (i = 0; i < 1000; i++) {
3249 udelay(10);
3250 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3251 (bmsr & BMSR_LSTATUS)) {
3252 udelay(40);
3253 break;
3254 }
3255 }
3256
Matt Carlson79eb6902010-02-17 15:17:03 +00003257 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3258 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 !(bmsr & BMSR_LSTATUS) &&
3260 tp->link_config.active_speed == SPEED_1000) {
3261 err = tg3_phy_reset(tp);
3262 if (!err)
3263 err = tg3_init_5401phy_dsp(tp);
3264 if (err)
3265 return err;
3266 }
3267 }
3268 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3269 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3270 /* 5701 {A0,B0} CRC bug workaround */
3271 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003272 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3273 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3274 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 }
3276
3277 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003278 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3279 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003281 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003283 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3285
3286 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3287 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3288 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3289 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3290 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3291 else
3292 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3293 }
3294
3295 current_link_up = 0;
3296 current_speed = SPEED_INVALID;
3297 current_duplex = DUPLEX_INVALID;
3298
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003299 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003300 err = tg3_phy_auxctl_read(tp,
3301 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3302 &val);
3303 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003304 tg3_phy_auxctl_write(tp,
3305 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3306 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 goto relink;
3308 }
3309 }
3310
3311 bmsr = 0;
3312 for (i = 0; i < 100; i++) {
3313 tg3_readphy(tp, MII_BMSR, &bmsr);
3314 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3315 (bmsr & BMSR_LSTATUS))
3316 break;
3317 udelay(40);
3318 }
3319
3320 if (bmsr & BMSR_LSTATUS) {
3321 u32 aux_stat, bmcr;
3322
3323 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3324 for (i = 0; i < 2000; i++) {
3325 udelay(10);
3326 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3327 aux_stat)
3328 break;
3329 }
3330
3331 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3332 &current_speed,
3333 &current_duplex);
3334
3335 bmcr = 0;
3336 for (i = 0; i < 200; i++) {
3337 tg3_readphy(tp, MII_BMCR, &bmcr);
3338 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3339 continue;
3340 if (bmcr && bmcr != 0x7fff)
3341 break;
3342 udelay(10);
3343 }
3344
Matt Carlsonef167e22007-12-20 20:10:01 -08003345 lcl_adv = 0;
3346 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
Matt Carlsonef167e22007-12-20 20:10:01 -08003348 tp->link_config.active_speed = current_speed;
3349 tp->link_config.active_duplex = current_duplex;
3350
3351 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3352 if ((bmcr & BMCR_ANENABLE) &&
3353 tg3_copper_is_advertising_all(tp,
3354 tp->link_config.advertising)) {
3355 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3356 &rmt_adv))
3357 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 }
3359 } else {
3360 if (!(bmcr & BMCR_ANENABLE) &&
3361 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003362 tp->link_config.duplex == current_duplex &&
3363 tp->link_config.flowctrl ==
3364 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 }
3367 }
3368
Matt Carlsonef167e22007-12-20 20:10:01 -08003369 if (current_link_up == 1 &&
3370 tp->link_config.active_duplex == DUPLEX_FULL)
3371 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 }
3373
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374relink:
Matt Carlson80096062010-08-02 11:26:06 +00003375 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 tg3_phy_copper_begin(tp);
3377
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003378 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00003379 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
3380 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 current_link_up = 1;
3382 }
3383
3384 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3385 if (current_link_up == 1) {
3386 if (tp->link_config.active_speed == SPEED_100 ||
3387 tp->link_config.active_speed == SPEED_10)
3388 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3389 else
3390 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003391 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003392 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3393 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3395
3396 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3397 if (tp->link_config.active_duplex == DUPLEX_HALF)
3398 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3399
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003401 if (current_link_up == 1 &&
3402 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003404 else
3405 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 }
3407
3408 /* ??? Without this setting Netgear GA302T PHY does not
3409 * ??? send/receive packets...
3410 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003411 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3413 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3414 tw32_f(MAC_MI_MODE, tp->mi_mode);
3415 udelay(80);
3416 }
3417
3418 tw32_f(MAC_MODE, tp->mac_mode);
3419 udelay(40);
3420
Matt Carlson52b02d02010-10-14 10:37:41 +00003421 tg3_phy_eee_adjust(tp, current_link_up);
3422
Joe Perches63c3a662011-04-26 08:12:10 +00003423 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 /* Polled via timer. */
3425 tw32_f(MAC_EVENT, 0);
3426 } else {
3427 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3428 }
3429 udelay(40);
3430
3431 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3432 current_link_up == 1 &&
3433 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00003434 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 udelay(120);
3436 tw32_f(MAC_STATUS,
3437 (MAC_STATUS_SYNC_CHANGED |
3438 MAC_STATUS_CFG_CHANGED));
3439 udelay(40);
3440 tg3_write_mem(tp,
3441 NIC_SRAM_FIRMWARE_MBOX,
3442 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3443 }
3444
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003445 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00003446 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003447 u16 oldlnkctl, newlnkctl;
3448
3449 pci_read_config_word(tp->pdev,
3450 tp->pcie_cap + PCI_EXP_LNKCTL,
3451 &oldlnkctl);
3452 if (tp->link_config.active_speed == SPEED_100 ||
3453 tp->link_config.active_speed == SPEED_10)
3454 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3455 else
3456 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3457 if (newlnkctl != oldlnkctl)
3458 pci_write_config_word(tp->pdev,
3459 tp->pcie_cap + PCI_EXP_LNKCTL,
3460 newlnkctl);
3461 }
3462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 if (current_link_up != netif_carrier_ok(tp->dev)) {
3464 if (current_link_up)
3465 netif_carrier_on(tp->dev);
3466 else
3467 netif_carrier_off(tp->dev);
3468 tg3_link_report(tp);
3469 }
3470
3471 return 0;
3472}
3473
3474struct tg3_fiber_aneginfo {
3475 int state;
3476#define ANEG_STATE_UNKNOWN 0
3477#define ANEG_STATE_AN_ENABLE 1
3478#define ANEG_STATE_RESTART_INIT 2
3479#define ANEG_STATE_RESTART 3
3480#define ANEG_STATE_DISABLE_LINK_OK 4
3481#define ANEG_STATE_ABILITY_DETECT_INIT 5
3482#define ANEG_STATE_ABILITY_DETECT 6
3483#define ANEG_STATE_ACK_DETECT_INIT 7
3484#define ANEG_STATE_ACK_DETECT 8
3485#define ANEG_STATE_COMPLETE_ACK_INIT 9
3486#define ANEG_STATE_COMPLETE_ACK 10
3487#define ANEG_STATE_IDLE_DETECT_INIT 11
3488#define ANEG_STATE_IDLE_DETECT 12
3489#define ANEG_STATE_LINK_OK 13
3490#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3491#define ANEG_STATE_NEXT_PAGE_WAIT 15
3492
3493 u32 flags;
3494#define MR_AN_ENABLE 0x00000001
3495#define MR_RESTART_AN 0x00000002
3496#define MR_AN_COMPLETE 0x00000004
3497#define MR_PAGE_RX 0x00000008
3498#define MR_NP_LOADED 0x00000010
3499#define MR_TOGGLE_TX 0x00000020
3500#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3501#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3502#define MR_LP_ADV_SYM_PAUSE 0x00000100
3503#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3504#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3505#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3506#define MR_LP_ADV_NEXT_PAGE 0x00001000
3507#define MR_TOGGLE_RX 0x00002000
3508#define MR_NP_RX 0x00004000
3509
3510#define MR_LINK_OK 0x80000000
3511
3512 unsigned long link_time, cur_time;
3513
3514 u32 ability_match_cfg;
3515 int ability_match_count;
3516
3517 char ability_match, idle_match, ack_match;
3518
3519 u32 txconfig, rxconfig;
3520#define ANEG_CFG_NP 0x00000080
3521#define ANEG_CFG_ACK 0x00000040
3522#define ANEG_CFG_RF2 0x00000020
3523#define ANEG_CFG_RF1 0x00000010
3524#define ANEG_CFG_PS2 0x00000001
3525#define ANEG_CFG_PS1 0x00008000
3526#define ANEG_CFG_HD 0x00004000
3527#define ANEG_CFG_FD 0x00002000
3528#define ANEG_CFG_INVAL 0x00001f06
3529
3530};
3531#define ANEG_OK 0
3532#define ANEG_DONE 1
3533#define ANEG_TIMER_ENAB 2
3534#define ANEG_FAILED -1
3535
3536#define ANEG_STATE_SETTLE_TIME 10000
3537
3538static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3539 struct tg3_fiber_aneginfo *ap)
3540{
Matt Carlson5be73b42007-12-20 20:09:29 -08003541 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 unsigned long delta;
3543 u32 rx_cfg_reg;
3544 int ret;
3545
3546 if (ap->state == ANEG_STATE_UNKNOWN) {
3547 ap->rxconfig = 0;
3548 ap->link_time = 0;
3549 ap->cur_time = 0;
3550 ap->ability_match_cfg = 0;
3551 ap->ability_match_count = 0;
3552 ap->ability_match = 0;
3553 ap->idle_match = 0;
3554 ap->ack_match = 0;
3555 }
3556 ap->cur_time++;
3557
3558 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3559 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3560
3561 if (rx_cfg_reg != ap->ability_match_cfg) {
3562 ap->ability_match_cfg = rx_cfg_reg;
3563 ap->ability_match = 0;
3564 ap->ability_match_count = 0;
3565 } else {
3566 if (++ap->ability_match_count > 1) {
3567 ap->ability_match = 1;
3568 ap->ability_match_cfg = rx_cfg_reg;
3569 }
3570 }
3571 if (rx_cfg_reg & ANEG_CFG_ACK)
3572 ap->ack_match = 1;
3573 else
3574 ap->ack_match = 0;
3575
3576 ap->idle_match = 0;
3577 } else {
3578 ap->idle_match = 1;
3579 ap->ability_match_cfg = 0;
3580 ap->ability_match_count = 0;
3581 ap->ability_match = 0;
3582 ap->ack_match = 0;
3583
3584 rx_cfg_reg = 0;
3585 }
3586
3587 ap->rxconfig = rx_cfg_reg;
3588 ret = ANEG_OK;
3589
Matt Carlson33f401a2010-04-05 10:19:27 +00003590 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 case ANEG_STATE_UNKNOWN:
3592 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3593 ap->state = ANEG_STATE_AN_ENABLE;
3594
3595 /* fallthru */
3596 case ANEG_STATE_AN_ENABLE:
3597 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3598 if (ap->flags & MR_AN_ENABLE) {
3599 ap->link_time = 0;
3600 ap->cur_time = 0;
3601 ap->ability_match_cfg = 0;
3602 ap->ability_match_count = 0;
3603 ap->ability_match = 0;
3604 ap->idle_match = 0;
3605 ap->ack_match = 0;
3606
3607 ap->state = ANEG_STATE_RESTART_INIT;
3608 } else {
3609 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3610 }
3611 break;
3612
3613 case ANEG_STATE_RESTART_INIT:
3614 ap->link_time = ap->cur_time;
3615 ap->flags &= ~(MR_NP_LOADED);
3616 ap->txconfig = 0;
3617 tw32(MAC_TX_AUTO_NEG, 0);
3618 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3619 tw32_f(MAC_MODE, tp->mac_mode);
3620 udelay(40);
3621
3622 ret = ANEG_TIMER_ENAB;
3623 ap->state = ANEG_STATE_RESTART;
3624
3625 /* fallthru */
3626 case ANEG_STATE_RESTART:
3627 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00003628 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00003630 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 break;
3633
3634 case ANEG_STATE_DISABLE_LINK_OK:
3635 ret = ANEG_DONE;
3636 break;
3637
3638 case ANEG_STATE_ABILITY_DETECT_INIT:
3639 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003640 ap->txconfig = ANEG_CFG_FD;
3641 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3642 if (flowctrl & ADVERTISE_1000XPAUSE)
3643 ap->txconfig |= ANEG_CFG_PS1;
3644 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3645 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3647 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3648 tw32_f(MAC_MODE, tp->mac_mode);
3649 udelay(40);
3650
3651 ap->state = ANEG_STATE_ABILITY_DETECT;
3652 break;
3653
3654 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00003655 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 break;
3658
3659 case ANEG_STATE_ACK_DETECT_INIT:
3660 ap->txconfig |= ANEG_CFG_ACK;
3661 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3662 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3663 tw32_f(MAC_MODE, tp->mac_mode);
3664 udelay(40);
3665
3666 ap->state = ANEG_STATE_ACK_DETECT;
3667
3668 /* fallthru */
3669 case ANEG_STATE_ACK_DETECT:
3670 if (ap->ack_match != 0) {
3671 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3672 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3673 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3674 } else {
3675 ap->state = ANEG_STATE_AN_ENABLE;
3676 }
3677 } else if (ap->ability_match != 0 &&
3678 ap->rxconfig == 0) {
3679 ap->state = ANEG_STATE_AN_ENABLE;
3680 }
3681 break;
3682
3683 case ANEG_STATE_COMPLETE_ACK_INIT:
3684 if (ap->rxconfig & ANEG_CFG_INVAL) {
3685 ret = ANEG_FAILED;
3686 break;
3687 }
3688 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3689 MR_LP_ADV_HALF_DUPLEX |
3690 MR_LP_ADV_SYM_PAUSE |
3691 MR_LP_ADV_ASYM_PAUSE |
3692 MR_LP_ADV_REMOTE_FAULT1 |
3693 MR_LP_ADV_REMOTE_FAULT2 |
3694 MR_LP_ADV_NEXT_PAGE |
3695 MR_TOGGLE_RX |
3696 MR_NP_RX);
3697 if (ap->rxconfig & ANEG_CFG_FD)
3698 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3699 if (ap->rxconfig & ANEG_CFG_HD)
3700 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3701 if (ap->rxconfig & ANEG_CFG_PS1)
3702 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3703 if (ap->rxconfig & ANEG_CFG_PS2)
3704 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3705 if (ap->rxconfig & ANEG_CFG_RF1)
3706 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3707 if (ap->rxconfig & ANEG_CFG_RF2)
3708 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3709 if (ap->rxconfig & ANEG_CFG_NP)
3710 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3711
3712 ap->link_time = ap->cur_time;
3713
3714 ap->flags ^= (MR_TOGGLE_TX);
3715 if (ap->rxconfig & 0x0008)
3716 ap->flags |= MR_TOGGLE_RX;
3717 if (ap->rxconfig & ANEG_CFG_NP)
3718 ap->flags |= MR_NP_RX;
3719 ap->flags |= MR_PAGE_RX;
3720
3721 ap->state = ANEG_STATE_COMPLETE_ACK;
3722 ret = ANEG_TIMER_ENAB;
3723 break;
3724
3725 case ANEG_STATE_COMPLETE_ACK:
3726 if (ap->ability_match != 0 &&
3727 ap->rxconfig == 0) {
3728 ap->state = ANEG_STATE_AN_ENABLE;
3729 break;
3730 }
3731 delta = ap->cur_time - ap->link_time;
3732 if (delta > ANEG_STATE_SETTLE_TIME) {
3733 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3734 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3735 } else {
3736 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3737 !(ap->flags & MR_NP_RX)) {
3738 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3739 } else {
3740 ret = ANEG_FAILED;
3741 }
3742 }
3743 }
3744 break;
3745
3746 case ANEG_STATE_IDLE_DETECT_INIT:
3747 ap->link_time = ap->cur_time;
3748 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3749 tw32_f(MAC_MODE, tp->mac_mode);
3750 udelay(40);
3751
3752 ap->state = ANEG_STATE_IDLE_DETECT;
3753 ret = ANEG_TIMER_ENAB;
3754 break;
3755
3756 case ANEG_STATE_IDLE_DETECT:
3757 if (ap->ability_match != 0 &&
3758 ap->rxconfig == 0) {
3759 ap->state = ANEG_STATE_AN_ENABLE;
3760 break;
3761 }
3762 delta = ap->cur_time - ap->link_time;
3763 if (delta > ANEG_STATE_SETTLE_TIME) {
3764 /* XXX another gem from the Broadcom driver :( */
3765 ap->state = ANEG_STATE_LINK_OK;
3766 }
3767 break;
3768
3769 case ANEG_STATE_LINK_OK:
3770 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3771 ret = ANEG_DONE;
3772 break;
3773
3774 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3775 /* ??? unimplemented */
3776 break;
3777
3778 case ANEG_STATE_NEXT_PAGE_WAIT:
3779 /* ??? unimplemented */
3780 break;
3781
3782 default:
3783 ret = ANEG_FAILED;
3784 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786
3787 return ret;
3788}
3789
Matt Carlson5be73b42007-12-20 20:09:29 -08003790static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791{
3792 int res = 0;
3793 struct tg3_fiber_aneginfo aninfo;
3794 int status = ANEG_FAILED;
3795 unsigned int tick;
3796 u32 tmp;
3797
3798 tw32_f(MAC_TX_AUTO_NEG, 0);
3799
3800 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3801 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3802 udelay(40);
3803
3804 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3805 udelay(40);
3806
3807 memset(&aninfo, 0, sizeof(aninfo));
3808 aninfo.flags |= MR_AN_ENABLE;
3809 aninfo.state = ANEG_STATE_UNKNOWN;
3810 aninfo.cur_time = 0;
3811 tick = 0;
3812 while (++tick < 195000) {
3813 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3814 if (status == ANEG_DONE || status == ANEG_FAILED)
3815 break;
3816
3817 udelay(1);
3818 }
3819
3820 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3821 tw32_f(MAC_MODE, tp->mac_mode);
3822 udelay(40);
3823
Matt Carlson5be73b42007-12-20 20:09:29 -08003824 *txflags = aninfo.txconfig;
3825 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
3827 if (status == ANEG_DONE &&
3828 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3829 MR_LP_ADV_FULL_DUPLEX)))
3830 res = 1;
3831
3832 return res;
3833}
3834
3835static void tg3_init_bcm8002(struct tg3 *tp)
3836{
3837 u32 mac_status = tr32(MAC_STATUS);
3838 int i;
3839
3840 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00003841 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 !(mac_status & MAC_STATUS_PCS_SYNCED))
3843 return;
3844
3845 /* Set PLL lock range. */
3846 tg3_writephy(tp, 0x16, 0x8007);
3847
3848 /* SW reset */
3849 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3850
3851 /* Wait for reset to complete. */
3852 /* XXX schedule_timeout() ... */
3853 for (i = 0; i < 500; i++)
3854 udelay(10);
3855
3856 /* Config mode; select PMA/Ch 1 regs. */
3857 tg3_writephy(tp, 0x10, 0x8411);
3858
3859 /* Enable auto-lock and comdet, select txclk for tx. */
3860 tg3_writephy(tp, 0x11, 0x0a10);
3861
3862 tg3_writephy(tp, 0x18, 0x00a0);
3863 tg3_writephy(tp, 0x16, 0x41ff);
3864
3865 /* Assert and deassert POR. */
3866 tg3_writephy(tp, 0x13, 0x0400);
3867 udelay(40);
3868 tg3_writephy(tp, 0x13, 0x0000);
3869
3870 tg3_writephy(tp, 0x11, 0x0a50);
3871 udelay(40);
3872 tg3_writephy(tp, 0x11, 0x0a10);
3873
3874 /* Wait for signal to stabilize */
3875 /* XXX schedule_timeout() ... */
3876 for (i = 0; i < 15000; i++)
3877 udelay(10);
3878
3879 /* Deselect the channel register so we can read the PHYID
3880 * later.
3881 */
3882 tg3_writephy(tp, 0x10, 0x8011);
3883}
3884
3885static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3886{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003887 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 u32 sg_dig_ctrl, sg_dig_status;
3889 u32 serdes_cfg, expected_sg_dig_ctrl;
3890 int workaround, port_a;
3891 int current_link_up;
3892
3893 serdes_cfg = 0;
3894 expected_sg_dig_ctrl = 0;
3895 workaround = 0;
3896 port_a = 1;
3897 current_link_up = 0;
3898
3899 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3900 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3901 workaround = 1;
3902 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3903 port_a = 0;
3904
3905 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3906 /* preserve bits 20-23 for voltage regulator */
3907 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3908 }
3909
3910 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3911
3912 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003913 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 if (workaround) {
3915 u32 val = serdes_cfg;
3916
3917 if (port_a)
3918 val |= 0xc010000;
3919 else
3920 val |= 0x4010000;
3921 tw32_f(MAC_SERDES_CFG, val);
3922 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003923
3924 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 }
3926 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3927 tg3_setup_flow_control(tp, 0, 0);
3928 current_link_up = 1;
3929 }
3930 goto out;
3931 }
3932
3933 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003934 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
Matt Carlson82cd3d12007-12-20 20:09:00 -08003936 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3937 if (flowctrl & ADVERTISE_1000XPAUSE)
3938 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3939 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3940 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
3942 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003943 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07003944 tp->serdes_counter &&
3945 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3946 MAC_STATUS_RCVD_CFG)) ==
3947 MAC_STATUS_PCS_SYNCED)) {
3948 tp->serdes_counter--;
3949 current_link_up = 1;
3950 goto out;
3951 }
3952restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 if (workaround)
3954 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003955 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 udelay(5);
3957 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3958
Michael Chan3d3ebe72006-09-27 15:59:15 -07003959 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003960 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3962 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003963 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 mac_status = tr32(MAC_STATUS);
3965
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003966 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003968 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969
Matt Carlson82cd3d12007-12-20 20:09:00 -08003970 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3971 local_adv |= ADVERTISE_1000XPAUSE;
3972 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3973 local_adv |= ADVERTISE_1000XPSE_ASYM;
3974
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003975 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003976 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003977 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003978 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
3980 tg3_setup_flow_control(tp, local_adv, remote_adv);
3981 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003982 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003983 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003984 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003985 if (tp->serdes_counter)
3986 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 else {
3988 if (workaround) {
3989 u32 val = serdes_cfg;
3990
3991 if (port_a)
3992 val |= 0xc010000;
3993 else
3994 val |= 0x4010000;
3995
3996 tw32_f(MAC_SERDES_CFG, val);
3997 }
3998
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003999 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 udelay(40);
4001
4002 /* Link parallel detection - link is up */
4003 /* only if we have PCS_SYNC and not */
4004 /* receiving config code words */
4005 mac_status = tr32(MAC_STATUS);
4006 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4007 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4008 tg3_setup_flow_control(tp, 0, 0);
4009 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004010 tp->phy_flags |=
4011 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004012 tp->serdes_counter =
4013 SERDES_PARALLEL_DET_TIMEOUT;
4014 } else
4015 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 }
4017 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004018 } else {
4019 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004020 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 }
4022
4023out:
4024 return current_link_up;
4025}
4026
4027static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4028{
4029 int current_link_up = 0;
4030
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004031 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
4034 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004035 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004037
Matt Carlson5be73b42007-12-20 20:09:29 -08004038 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4039 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
Matt Carlson5be73b42007-12-20 20:09:29 -08004041 if (txflags & ANEG_CFG_PS1)
4042 local_adv |= ADVERTISE_1000XPAUSE;
4043 if (txflags & ANEG_CFG_PS2)
4044 local_adv |= ADVERTISE_1000XPSE_ASYM;
4045
4046 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4047 remote_adv |= LPA_1000XPAUSE;
4048 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4049 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
4051 tg3_setup_flow_control(tp, local_adv, remote_adv);
4052
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 current_link_up = 1;
4054 }
4055 for (i = 0; i < 30; i++) {
4056 udelay(20);
4057 tw32_f(MAC_STATUS,
4058 (MAC_STATUS_SYNC_CHANGED |
4059 MAC_STATUS_CFG_CHANGED));
4060 udelay(40);
4061 if ((tr32(MAC_STATUS) &
4062 (MAC_STATUS_SYNC_CHANGED |
4063 MAC_STATUS_CFG_CHANGED)) == 0)
4064 break;
4065 }
4066
4067 mac_status = tr32(MAC_STATUS);
4068 if (current_link_up == 0 &&
4069 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4070 !(mac_status & MAC_STATUS_RCVD_CFG))
4071 current_link_up = 1;
4072 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004073 tg3_setup_flow_control(tp, 0, 0);
4074
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 /* Forcing 1000FD link up. */
4076 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
4078 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4079 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004080
4081 tw32_f(MAC_MODE, tp->mac_mode);
4082 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 }
4084
4085out:
4086 return current_link_up;
4087}
4088
4089static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4090{
4091 u32 orig_pause_cfg;
4092 u16 orig_active_speed;
4093 u8 orig_active_duplex;
4094 u32 mac_status;
4095 int current_link_up;
4096 int i;
4097
Matt Carlson8d018622007-12-20 20:05:44 -08004098 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 orig_active_speed = tp->link_config.active_speed;
4100 orig_active_duplex = tp->link_config.active_duplex;
4101
Joe Perches63c3a662011-04-26 08:12:10 +00004102 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004104 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 mac_status = tr32(MAC_STATUS);
4106 mac_status &= (MAC_STATUS_PCS_SYNCED |
4107 MAC_STATUS_SIGNAL_DET |
4108 MAC_STATUS_CFG_CHANGED |
4109 MAC_STATUS_RCVD_CFG);
4110 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4111 MAC_STATUS_SIGNAL_DET)) {
4112 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4113 MAC_STATUS_CFG_CHANGED));
4114 return 0;
4115 }
4116 }
4117
4118 tw32_f(MAC_TX_AUTO_NEG, 0);
4119
4120 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4121 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4122 tw32_f(MAC_MODE, tp->mac_mode);
4123 udelay(40);
4124
Matt Carlson79eb6902010-02-17 15:17:03 +00004125 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 tg3_init_bcm8002(tp);
4127
4128 /* Enable link change event even when serdes polling. */
4129 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4130 udelay(40);
4131
4132 current_link_up = 0;
4133 mac_status = tr32(MAC_STATUS);
4134
Joe Perches63c3a662011-04-26 08:12:10 +00004135 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4137 else
4138 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4139
Matt Carlson898a56f2009-08-28 14:02:40 +00004140 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004142 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143
4144 for (i = 0; i < 100; i++) {
4145 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4146 MAC_STATUS_CFG_CHANGED));
4147 udelay(5);
4148 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004149 MAC_STATUS_CFG_CHANGED |
4150 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 break;
4152 }
4153
4154 mac_status = tr32(MAC_STATUS);
4155 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4156 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004157 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4158 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 tw32_f(MAC_MODE, (tp->mac_mode |
4160 MAC_MODE_SEND_CONFIGS));
4161 udelay(1);
4162 tw32_f(MAC_MODE, tp->mac_mode);
4163 }
4164 }
4165
4166 if (current_link_up == 1) {
4167 tp->link_config.active_speed = SPEED_1000;
4168 tp->link_config.active_duplex = DUPLEX_FULL;
4169 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4170 LED_CTRL_LNKLED_OVERRIDE |
4171 LED_CTRL_1000MBPS_ON));
4172 } else {
4173 tp->link_config.active_speed = SPEED_INVALID;
4174 tp->link_config.active_duplex = DUPLEX_INVALID;
4175 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4176 LED_CTRL_LNKLED_OVERRIDE |
4177 LED_CTRL_TRAFFIC_OVERRIDE));
4178 }
4179
4180 if (current_link_up != netif_carrier_ok(tp->dev)) {
4181 if (current_link_up)
4182 netif_carrier_on(tp->dev);
4183 else
4184 netif_carrier_off(tp->dev);
4185 tg3_link_report(tp);
4186 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004187 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 if (orig_pause_cfg != now_pause_cfg ||
4189 orig_active_speed != tp->link_config.active_speed ||
4190 orig_active_duplex != tp->link_config.active_duplex)
4191 tg3_link_report(tp);
4192 }
4193
4194 return 0;
4195}
4196
Michael Chan747e8f82005-07-25 12:33:22 -07004197static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4198{
4199 int current_link_up, err = 0;
4200 u32 bmsr, bmcr;
4201 u16 current_speed;
4202 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004203 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004204
4205 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4206 tw32_f(MAC_MODE, tp->mac_mode);
4207 udelay(40);
4208
4209 tw32(MAC_EVENT, 0);
4210
4211 tw32_f(MAC_STATUS,
4212 (MAC_STATUS_SYNC_CHANGED |
4213 MAC_STATUS_CFG_CHANGED |
4214 MAC_STATUS_MI_COMPLETION |
4215 MAC_STATUS_LNKSTATE_CHANGED));
4216 udelay(40);
4217
4218 if (force_reset)
4219 tg3_phy_reset(tp);
4220
4221 current_link_up = 0;
4222 current_speed = SPEED_INVALID;
4223 current_duplex = DUPLEX_INVALID;
4224
4225 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4226 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004227 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4228 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4229 bmsr |= BMSR_LSTATUS;
4230 else
4231 bmsr &= ~BMSR_LSTATUS;
4232 }
Michael Chan747e8f82005-07-25 12:33:22 -07004233
4234 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4235
4236 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004237 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004238 /* do nothing, just check for link up at the end */
4239 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4240 u32 adv, new_adv;
4241
4242 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4243 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4244 ADVERTISE_1000XPAUSE |
4245 ADVERTISE_1000XPSE_ASYM |
4246 ADVERTISE_SLCT);
4247
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004248 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004249
4250 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4251 new_adv |= ADVERTISE_1000XHALF;
4252 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4253 new_adv |= ADVERTISE_1000XFULL;
4254
4255 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4256 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4257 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4258 tg3_writephy(tp, MII_BMCR, bmcr);
4259
4260 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004261 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004262 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004263
4264 return err;
4265 }
4266 } else {
4267 u32 new_bmcr;
4268
4269 bmcr &= ~BMCR_SPEED1000;
4270 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4271
4272 if (tp->link_config.duplex == DUPLEX_FULL)
4273 new_bmcr |= BMCR_FULLDPLX;
4274
4275 if (new_bmcr != bmcr) {
4276 /* BMCR_SPEED1000 is a reserved bit that needs
4277 * to be set on write.
4278 */
4279 new_bmcr |= BMCR_SPEED1000;
4280
4281 /* Force a linkdown */
4282 if (netif_carrier_ok(tp->dev)) {
4283 u32 adv;
4284
4285 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4286 adv &= ~(ADVERTISE_1000XFULL |
4287 ADVERTISE_1000XHALF |
4288 ADVERTISE_SLCT);
4289 tg3_writephy(tp, MII_ADVERTISE, adv);
4290 tg3_writephy(tp, MII_BMCR, bmcr |
4291 BMCR_ANRESTART |
4292 BMCR_ANENABLE);
4293 udelay(10);
4294 netif_carrier_off(tp->dev);
4295 }
4296 tg3_writephy(tp, MII_BMCR, new_bmcr);
4297 bmcr = new_bmcr;
4298 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4299 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004300 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4301 ASIC_REV_5714) {
4302 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4303 bmsr |= BMSR_LSTATUS;
4304 else
4305 bmsr &= ~BMSR_LSTATUS;
4306 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004307 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004308 }
4309 }
4310
4311 if (bmsr & BMSR_LSTATUS) {
4312 current_speed = SPEED_1000;
4313 current_link_up = 1;
4314 if (bmcr & BMCR_FULLDPLX)
4315 current_duplex = DUPLEX_FULL;
4316 else
4317 current_duplex = DUPLEX_HALF;
4318
Matt Carlsonef167e22007-12-20 20:10:01 -08004319 local_adv = 0;
4320 remote_adv = 0;
4321
Michael Chan747e8f82005-07-25 12:33:22 -07004322 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004323 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004324
4325 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4326 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4327 common = local_adv & remote_adv;
4328 if (common & (ADVERTISE_1000XHALF |
4329 ADVERTISE_1000XFULL)) {
4330 if (common & ADVERTISE_1000XFULL)
4331 current_duplex = DUPLEX_FULL;
4332 else
4333 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004334 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004335 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00004336 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004337 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004338 }
Michael Chan747e8f82005-07-25 12:33:22 -07004339 }
4340 }
4341
Matt Carlsonef167e22007-12-20 20:10:01 -08004342 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4343 tg3_setup_flow_control(tp, local_adv, remote_adv);
4344
Michael Chan747e8f82005-07-25 12:33:22 -07004345 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4346 if (tp->link_config.active_duplex == DUPLEX_HALF)
4347 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4348
4349 tw32_f(MAC_MODE, tp->mac_mode);
4350 udelay(40);
4351
4352 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4353
4354 tp->link_config.active_speed = current_speed;
4355 tp->link_config.active_duplex = current_duplex;
4356
4357 if (current_link_up != netif_carrier_ok(tp->dev)) {
4358 if (current_link_up)
4359 netif_carrier_on(tp->dev);
4360 else {
4361 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004362 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004363 }
4364 tg3_link_report(tp);
4365 }
4366 return err;
4367}
4368
4369static void tg3_serdes_parallel_detect(struct tg3 *tp)
4370{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004371 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004372 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004373 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004374 return;
4375 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004376
Michael Chan747e8f82005-07-25 12:33:22 -07004377 if (!netif_carrier_ok(tp->dev) &&
4378 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4379 u32 bmcr;
4380
4381 tg3_readphy(tp, MII_BMCR, &bmcr);
4382 if (bmcr & BMCR_ANENABLE) {
4383 u32 phy1, phy2;
4384
4385 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004386 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4387 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004388
4389 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004390 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4391 MII_TG3_DSP_EXP1_INT_STAT);
4392 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4393 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004394
4395 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4396 /* We have signal detect and not receiving
4397 * config code words, link is up by parallel
4398 * detection.
4399 */
4400
4401 bmcr &= ~BMCR_ANENABLE;
4402 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4403 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004404 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004405 }
4406 }
Matt Carlson859a588792010-04-05 10:19:28 +00004407 } else if (netif_carrier_ok(tp->dev) &&
4408 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004409 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004410 u32 phy2;
4411
4412 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004413 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4414 MII_TG3_DSP_EXP1_INT_STAT);
4415 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004416 if (phy2 & 0x20) {
4417 u32 bmcr;
4418
4419 /* Config code words received, turn on autoneg. */
4420 tg3_readphy(tp, MII_BMCR, &bmcr);
4421 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4422
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004423 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004424
4425 }
4426 }
4427}
4428
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4430{
Matt Carlsonf2096f92011-04-05 14:22:48 +00004431 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 int err;
4433
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004434 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004436 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004437 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004438 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004441 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004442 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004443
4444 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4445 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4446 scale = 65;
4447 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4448 scale = 6;
4449 else
4450 scale = 12;
4451
4452 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4453 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4454 tw32(GRC_MISC_CFG, val);
4455 }
4456
Matt Carlsonf2096f92011-04-05 14:22:48 +00004457 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4458 (6 << TX_LENGTHS_IPG_SHIFT);
4459 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4460 val |= tr32(MAC_TX_LENGTHS) &
4461 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4462 TX_LENGTHS_CNT_DWN_VAL_MSK);
4463
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 if (tp->link_config.active_speed == SPEED_1000 &&
4465 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00004466 tw32(MAC_TX_LENGTHS, val |
4467 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00004469 tw32(MAC_TX_LENGTHS, val |
4470 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471
Joe Perches63c3a662011-04-26 08:12:10 +00004472 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 if (netif_carrier_ok(tp->dev)) {
4474 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004475 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 } else {
4477 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4478 }
4479 }
4480
Joe Perches63c3a662011-04-26 08:12:10 +00004481 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004482 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07004483 if (!netif_carrier_ok(tp->dev))
4484 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4485 tp->pwrmgmt_thresh;
4486 else
4487 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4488 tw32(PCIE_PWR_MGMT_THRESH, val);
4489 }
4490
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 return err;
4492}
4493
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004494static inline int tg3_irq_sync(struct tg3 *tp)
4495{
4496 return tp->irq_sync;
4497}
4498
Matt Carlson97bd8e42011-04-13 11:05:04 +00004499static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4500{
4501 int i;
4502
4503 dst = (u32 *)((u8 *)dst + off);
4504 for (i = 0; i < len; i += sizeof(u32))
4505 *dst++ = tr32(off + i);
4506}
4507
4508static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4509{
4510 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4511 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4512 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4513 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4514 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4515 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4516 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4517 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4518 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4519 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4520 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4521 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4522 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4523 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4524 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4525 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4526 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4527 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4528 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4529
Joe Perches63c3a662011-04-26 08:12:10 +00004530 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004531 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4532
4533 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4534 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4535 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4536 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4537 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4538 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4539 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4540 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4541
Joe Perches63c3a662011-04-26 08:12:10 +00004542 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004543 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4544 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4545 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4546 }
4547
4548 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4549 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4550 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4551 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4552 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4553
Joe Perches63c3a662011-04-26 08:12:10 +00004554 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004555 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4556}
4557
4558static void tg3_dump_state(struct tg3 *tp)
4559{
4560 int i;
4561 u32 *regs;
4562
4563 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4564 if (!regs) {
4565 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4566 return;
4567 }
4568
Joe Perches63c3a662011-04-26 08:12:10 +00004569 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004570 /* Read up to but not including private PCI registers */
4571 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4572 regs[i / sizeof(u32)] = tr32(i);
4573 } else
4574 tg3_dump_legacy_regs(tp, regs);
4575
4576 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4577 if (!regs[i + 0] && !regs[i + 1] &&
4578 !regs[i + 2] && !regs[i + 3])
4579 continue;
4580
4581 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4582 i * 4,
4583 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4584 }
4585
4586 kfree(regs);
4587
4588 for (i = 0; i < tp->irq_cnt; i++) {
4589 struct tg3_napi *tnapi = &tp->napi[i];
4590
4591 /* SW status block */
4592 netdev_err(tp->dev,
4593 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4594 i,
4595 tnapi->hw_status->status,
4596 tnapi->hw_status->status_tag,
4597 tnapi->hw_status->rx_jumbo_consumer,
4598 tnapi->hw_status->rx_consumer,
4599 tnapi->hw_status->rx_mini_consumer,
4600 tnapi->hw_status->idx[0].rx_producer,
4601 tnapi->hw_status->idx[0].tx_consumer);
4602
4603 netdev_err(tp->dev,
4604 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4605 i,
4606 tnapi->last_tag, tnapi->last_irq_tag,
4607 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4608 tnapi->rx_rcb_ptr,
4609 tnapi->prodring.rx_std_prod_idx,
4610 tnapi->prodring.rx_std_cons_idx,
4611 tnapi->prodring.rx_jmb_prod_idx,
4612 tnapi->prodring.rx_jmb_cons_idx);
4613 }
4614}
4615
Michael Chandf3e6542006-05-26 17:48:07 -07004616/* This is called whenever we suspect that the system chipset is re-
4617 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4618 * is bogus tx completions. We try to recover by setting the
4619 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4620 * in the workqueue.
4621 */
4622static void tg3_tx_recover(struct tg3 *tp)
4623{
Joe Perches63c3a662011-04-26 08:12:10 +00004624 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07004625 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4626
Matt Carlson5129c3a2010-04-05 10:19:23 +00004627 netdev_warn(tp->dev,
4628 "The system may be re-ordering memory-mapped I/O "
4629 "cycles to the network device, attempting to recover. "
4630 "Please report the problem to the driver maintainer "
4631 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004632
4633 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00004634 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07004635 spin_unlock(&tp->lock);
4636}
4637
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004638static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004639{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004640 /* Tell compiler to fetch tx indices from memory. */
4641 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004642 return tnapi->tx_pending -
4643 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004644}
4645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646/* Tigon3 never reports partial packet sends. So we do not
4647 * need special logic to handle SKBs that have not had all
4648 * of their frags sent yet, like SunGEM does.
4649 */
Matt Carlson17375d22009-08-28 14:02:18 +00004650static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651{
Matt Carlson17375d22009-08-28 14:02:18 +00004652 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004653 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004654 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004655 struct netdev_queue *txq;
4656 int index = tnapi - tp->napi;
4657
Joe Perches63c3a662011-04-26 08:12:10 +00004658 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004659 index--;
4660
4661 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
4663 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004664 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004666 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Michael Chandf3e6542006-05-26 17:48:07 -07004668 if (unlikely(skb == NULL)) {
4669 tg3_tx_recover(tp);
4670 return;
4671 }
4672
Alexander Duyckf4188d82009-12-02 16:48:38 +00004673 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004674 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004675 skb_headlen(skb),
4676 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
4678 ri->skb = NULL;
4679
4680 sw_idx = NEXT_TX(sw_idx);
4681
4682 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004683 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004684 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4685 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004686
4687 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004688 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004689 skb_shinfo(skb)->frags[i].size,
4690 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 sw_idx = NEXT_TX(sw_idx);
4692 }
4693
David S. Millerf47c11e2005-06-24 20:18:35 -07004694 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004695
4696 if (unlikely(tx_bug)) {
4697 tg3_tx_recover(tp);
4698 return;
4699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 }
4701
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004702 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
Michael Chan1b2a7202006-08-07 21:46:02 -07004704 /* Need to make the tx_cons update visible to tg3_start_xmit()
4705 * before checking for netif_queue_stopped(). Without the
4706 * memory barrier, there is a small possibility that tg3_start_xmit()
4707 * will miss it and cause the queue to be stopped forever.
4708 */
4709 smp_mb();
4710
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004711 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004712 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004713 __netif_tx_lock(txq, smp_processor_id());
4714 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004715 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004716 netif_tx_wake_queue(txq);
4717 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719}
4720
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004721static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4722{
4723 if (!ri->skb)
4724 return;
4725
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004726 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004727 map_sz, PCI_DMA_FROMDEVICE);
4728 dev_kfree_skb_any(ri->skb);
4729 ri->skb = NULL;
4730}
4731
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732/* Returns size of skb allocated or < 0 on error.
4733 *
4734 * We only need to fill in the address because the other members
4735 * of the RX descriptor are invariant, see tg3_init_rings.
4736 *
4737 * Note the purposeful assymetry of cpu vs. chip accesses. For
4738 * posting buffers we only dirty the first cache line of the RX
4739 * descriptor (containing the address). Whereas for the RX status
4740 * buffers the cpu only reads the last cacheline of the RX descriptor
4741 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4742 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004743static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004744 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745{
4746 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004747 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 struct sk_buff *skb;
4749 dma_addr_t mapping;
4750 int skb_size, dest_idx;
4751
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 switch (opaque_key) {
4753 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004754 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004755 desc = &tpr->rx_std[dest_idx];
4756 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004757 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 break;
4759
4760 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004761 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004762 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004763 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004764 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 break;
4766
4767 default:
4768 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
4771 /* Do not overwrite any of the map or rp information
4772 * until we are sure we can commit to a new buffer.
4773 *
4774 * Callers depend upon this behavior and assume that
4775 * we leave everything unchanged if we fail.
4776 */
Matt Carlson287be122009-08-28 13:58:46 +00004777 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 if (skb == NULL)
4779 return -ENOMEM;
4780
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 skb_reserve(skb, tp->rx_offset);
4782
Matt Carlson287be122009-08-28 13:58:46 +00004783 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004785 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4786 dev_kfree_skb(skb);
4787 return -EIO;
4788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
4790 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004791 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 desc->addr_hi = ((u64)mapping >> 32);
4794 desc->addr_lo = ((u64)mapping & 0xffffffff);
4795
4796 return skb_size;
4797}
4798
4799/* We only need to move over in the address because the other
4800 * members of the RX descriptor are invariant. See notes above
4801 * tg3_alloc_rx_skb for full details.
4802 */
Matt Carlsona3896162009-11-13 13:03:44 +00004803static void tg3_recycle_rx(struct tg3_napi *tnapi,
4804 struct tg3_rx_prodring_set *dpr,
4805 u32 opaque_key, int src_idx,
4806 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807{
Matt Carlson17375d22009-08-28 14:02:18 +00004808 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4810 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004811 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004812 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
4814 switch (opaque_key) {
4815 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004816 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004817 dest_desc = &dpr->rx_std[dest_idx];
4818 dest_map = &dpr->rx_std_buffers[dest_idx];
4819 src_desc = &spr->rx_std[src_idx];
4820 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 break;
4822
4823 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004824 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004825 dest_desc = &dpr->rx_jmb[dest_idx].std;
4826 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4827 src_desc = &spr->rx_jmb[src_idx].std;
4828 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 break;
4830
4831 default:
4832 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834
4835 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004836 dma_unmap_addr_set(dest_map, mapping,
4837 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 dest_desc->addr_hi = src_desc->addr_hi;
4839 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004840
4841 /* Ensure that the update to the skb happens after the physical
4842 * addresses have been transferred to the new BD location.
4843 */
4844 smp_wmb();
4845
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 src_map->skb = NULL;
4847}
4848
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849/* The RX ring scheme is composed of multiple rings which post fresh
4850 * buffers to the chip, and one special ring the chip uses to report
4851 * status back to the host.
4852 *
4853 * The special ring reports the status of received packets to the
4854 * host. The chip does not write into the original descriptor the
4855 * RX buffer was obtained from. The chip simply takes the original
4856 * descriptor as provided by the host, updates the status and length
4857 * field, then writes this into the next status ring entry.
4858 *
4859 * Each ring the host uses to post buffers to the chip is described
4860 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4861 * it is first placed into the on-chip ram. When the packet's length
4862 * is known, it walks down the TG3_BDINFO entries to select the ring.
4863 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4864 * which is within the range of the new packet's length is chosen.
4865 *
4866 * The "separate ring for rx status" scheme may sound queer, but it makes
4867 * sense from a cache coherency perspective. If only the host writes
4868 * to the buffer post rings, and only the chip writes to the rx status
4869 * rings, then cache lines never move beyond shared-modified state.
4870 * If both the host and chip were to write into the same ring, cache line
4871 * eviction could occur since both entities want it in an exclusive state.
4872 */
Matt Carlson17375d22009-08-28 14:02:18 +00004873static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874{
Matt Carlson17375d22009-08-28 14:02:18 +00004875 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004876 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004877 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004878 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004879 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004881 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004883 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 /*
4885 * We need to order the read of hw_idx and the read of
4886 * the opaque cookie.
4887 */
4888 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 work_mask = 0;
4890 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004891 std_prod_idx = tpr->rx_std_prod_idx;
4892 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004894 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004895 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 unsigned int len;
4897 struct sk_buff *skb;
4898 dma_addr_t dma_addr;
4899 u32 opaque_key, desc_idx, *post_ptr;
4900
4901 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4902 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4903 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004904 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004905 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004906 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004907 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004908 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004910 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004911 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004912 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004913 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004914 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916
4917 work_mask |= opaque_key;
4918
4919 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4920 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4921 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004922 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 desc_idx, *post_ptr);
4924 drop_it_no_recycle:
4925 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00004926 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 goto next_pkt;
4928 }
4929
Matt Carlsonad829262008-11-21 17:16:16 -08004930 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4931 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
Matt Carlsond2757fc2010-04-12 06:58:27 +00004933 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 int skb_size;
4935
Matt Carlson86b21e52009-11-13 13:03:45 +00004936 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004937 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 if (skb_size < 0)
4939 goto drop_it;
4940
Matt Carlson287be122009-08-28 13:58:46 +00004941 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 PCI_DMA_FROMDEVICE);
4943
Matt Carlson61e800c2010-02-17 15:16:54 +00004944 /* Ensure that the update to the skb happens
4945 * after the usage of the old DMA mapping.
4946 */
4947 smp_wmb();
4948
4949 ri->skb = NULL;
4950
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 skb_put(skb, len);
4952 } else {
4953 struct sk_buff *copy_skb;
4954
Matt Carlsona3896162009-11-13 13:03:44 +00004955 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 desc_idx, *post_ptr);
4957
Matt Carlsonbf933c82011-01-25 15:58:49 +00004958 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00004959 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 if (copy_skb == NULL)
4961 goto drop_it_no_recycle;
4962
Matt Carlsonbf933c82011-01-25 15:58:49 +00004963 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 skb_put(copy_skb, len);
4965 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004966 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4968
4969 /* We'll reuse the original ring buffer. */
4970 skb = copy_skb;
4971 }
4972
Michał Mirosławdc668912011-04-07 03:35:07 +00004973 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4975 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4976 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4977 skb->ip_summed = CHECKSUM_UNNECESSARY;
4978 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004979 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
4981 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004982
4983 if (len > (tp->dev->mtu + ETH_HLEN) &&
4984 skb->protocol != htons(ETH_P_8021Q)) {
4985 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00004986 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004987 }
4988
Matt Carlson9dc7a112010-04-12 06:58:28 +00004989 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00004990 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
4991 __vlan_hwaccel_put_tag(skb,
4992 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00004993
Matt Carlsonbf933c82011-01-25 15:58:49 +00004994 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 received++;
4997 budget--;
4998
4999next_pkt:
5000 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005001
5002 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005003 tpr->rx_std_prod_idx = std_prod_idx &
5004 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005005 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5006 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005007 work_mask &= ~RXD_OPAQUE_RING_STD;
5008 rx_std_posted = 0;
5009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005011 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005012 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005013
5014 /* Refresh hw_idx to see if there is new work */
5015 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005016 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005017 rmb();
5018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 }
5020
5021 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005022 tnapi->rx_rcb_ptr = sw_idx;
5023 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
5025 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005026 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005027 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005028 tpr->rx_std_prod_idx = std_prod_idx &
5029 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005030 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5031 tpr->rx_std_prod_idx);
5032 }
5033 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005034 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5035 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005036 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5037 tpr->rx_jmb_prod_idx);
5038 }
5039 mmiowb();
5040 } else if (work_mask) {
5041 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5042 * updated before the producer indices can be updated.
5043 */
5044 smp_wmb();
5045
Matt Carlson2c49a442010-09-30 10:34:35 +00005046 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5047 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005048
Matt Carlsone4af1af2010-02-12 14:47:05 +00005049 if (tnapi != &tp->napi[1])
5050 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052
5053 return received;
5054}
5055
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005056static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005059 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005060 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5061
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 if (sblk->status & SD_STATUS_LINK_CHG) {
5063 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005064 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005065 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005066 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005067 tw32_f(MAC_STATUS,
5068 (MAC_STATUS_SYNC_CHANGED |
5069 MAC_STATUS_CFG_CHANGED |
5070 MAC_STATUS_MI_COMPLETION |
5071 MAC_STATUS_LNKSTATE_CHANGED));
5072 udelay(40);
5073 } else
5074 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005075 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 }
5077 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005078}
5079
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005080static int tg3_rx_prodring_xfer(struct tg3 *tp,
5081 struct tg3_rx_prodring_set *dpr,
5082 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005083{
5084 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005085 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005086
5087 while (1) {
5088 src_prod_idx = spr->rx_std_prod_idx;
5089
5090 /* Make sure updates to the rx_std_buffers[] entries and the
5091 * standard producer index are seen in the correct order.
5092 */
5093 smp_rmb();
5094
5095 if (spr->rx_std_cons_idx == src_prod_idx)
5096 break;
5097
5098 if (spr->rx_std_cons_idx < src_prod_idx)
5099 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5100 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005101 cpycnt = tp->rx_std_ring_mask + 1 -
5102 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005103
Matt Carlson2c49a442010-09-30 10:34:35 +00005104 cpycnt = min(cpycnt,
5105 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005106
5107 si = spr->rx_std_cons_idx;
5108 di = dpr->rx_std_prod_idx;
5109
Matt Carlsone92967b2010-02-12 14:47:06 +00005110 for (i = di; i < di + cpycnt; i++) {
5111 if (dpr->rx_std_buffers[i].skb) {
5112 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005113 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005114 break;
5115 }
5116 }
5117
5118 if (!cpycnt)
5119 break;
5120
5121 /* Ensure that updates to the rx_std_buffers ring and the
5122 * shadowed hardware producer ring from tg3_recycle_skb() are
5123 * ordered correctly WRT the skb check above.
5124 */
5125 smp_rmb();
5126
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005127 memcpy(&dpr->rx_std_buffers[di],
5128 &spr->rx_std_buffers[si],
5129 cpycnt * sizeof(struct ring_info));
5130
5131 for (i = 0; i < cpycnt; i++, di++, si++) {
5132 struct tg3_rx_buffer_desc *sbd, *dbd;
5133 sbd = &spr->rx_std[si];
5134 dbd = &dpr->rx_std[di];
5135 dbd->addr_hi = sbd->addr_hi;
5136 dbd->addr_lo = sbd->addr_lo;
5137 }
5138
Matt Carlson2c49a442010-09-30 10:34:35 +00005139 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5140 tp->rx_std_ring_mask;
5141 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5142 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005143 }
5144
5145 while (1) {
5146 src_prod_idx = spr->rx_jmb_prod_idx;
5147
5148 /* Make sure updates to the rx_jmb_buffers[] entries and
5149 * the jumbo producer index are seen in the correct order.
5150 */
5151 smp_rmb();
5152
5153 if (spr->rx_jmb_cons_idx == src_prod_idx)
5154 break;
5155
5156 if (spr->rx_jmb_cons_idx < src_prod_idx)
5157 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5158 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005159 cpycnt = tp->rx_jmb_ring_mask + 1 -
5160 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005161
5162 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005163 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005164
5165 si = spr->rx_jmb_cons_idx;
5166 di = dpr->rx_jmb_prod_idx;
5167
Matt Carlsone92967b2010-02-12 14:47:06 +00005168 for (i = di; i < di + cpycnt; i++) {
5169 if (dpr->rx_jmb_buffers[i].skb) {
5170 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005171 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005172 break;
5173 }
5174 }
5175
5176 if (!cpycnt)
5177 break;
5178
5179 /* Ensure that updates to the rx_jmb_buffers ring and the
5180 * shadowed hardware producer ring from tg3_recycle_skb() are
5181 * ordered correctly WRT the skb check above.
5182 */
5183 smp_rmb();
5184
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005185 memcpy(&dpr->rx_jmb_buffers[di],
5186 &spr->rx_jmb_buffers[si],
5187 cpycnt * sizeof(struct ring_info));
5188
5189 for (i = 0; i < cpycnt; i++, di++, si++) {
5190 struct tg3_rx_buffer_desc *sbd, *dbd;
5191 sbd = &spr->rx_jmb[si].std;
5192 dbd = &dpr->rx_jmb[di].std;
5193 dbd->addr_hi = sbd->addr_hi;
5194 dbd->addr_lo = sbd->addr_lo;
5195 }
5196
Matt Carlson2c49a442010-09-30 10:34:35 +00005197 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5198 tp->rx_jmb_ring_mask;
5199 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5200 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005201 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005202
5203 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005204}
5205
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005206static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5207{
5208 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209
5210 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005211 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005212 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005213 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005214 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 }
5216
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 /* run RX thread, within the bounds set by NAPI.
5218 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005219 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005221 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005222 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223
Joe Perches63c3a662011-04-26 08:12:10 +00005224 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005225 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005226 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005227 u32 std_prod_idx = dpr->rx_std_prod_idx;
5228 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005229
Matt Carlsone4af1af2010-02-12 14:47:05 +00005230 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005231 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005232 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005233
5234 wmb();
5235
Matt Carlsone4af1af2010-02-12 14:47:05 +00005236 if (std_prod_idx != dpr->rx_std_prod_idx)
5237 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5238 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005239
Matt Carlsone4af1af2010-02-12 14:47:05 +00005240 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5241 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5242 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005243
5244 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005245
5246 if (err)
5247 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005248 }
5249
David S. Miller6f535762007-10-11 18:08:29 -07005250 return work_done;
5251}
David S. Millerf7383c22005-05-18 22:50:53 -07005252
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005253static int tg3_poll_msix(struct napi_struct *napi, int budget)
5254{
5255 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5256 struct tg3 *tp = tnapi->tp;
5257 int work_done = 0;
5258 struct tg3_hw_status *sblk = tnapi->hw_status;
5259
5260 while (1) {
5261 work_done = tg3_poll_work(tnapi, work_done, budget);
5262
Joe Perches63c3a662011-04-26 08:12:10 +00005263 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005264 goto tx_recovery;
5265
5266 if (unlikely(work_done >= budget))
5267 break;
5268
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005269 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005270 * to tell the hw how much work has been processed,
5271 * so we must read it before checking for more work.
5272 */
5273 tnapi->last_tag = sblk->status_tag;
5274 tnapi->last_irq_tag = tnapi->last_tag;
5275 rmb();
5276
5277 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005278 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5279 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005280 napi_complete(napi);
5281 /* Reenable interrupts. */
5282 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5283 mmiowb();
5284 break;
5285 }
5286 }
5287
5288 return work_done;
5289
5290tx_recovery:
5291 /* work_done is guaranteed to be less than budget. */
5292 napi_complete(napi);
5293 schedule_work(&tp->reset_task);
5294 return work_done;
5295}
5296
Matt Carlsone64de4e2011-04-13 11:05:05 +00005297static void tg3_process_error(struct tg3 *tp)
5298{
5299 u32 val;
5300 bool real_error = false;
5301
Joe Perches63c3a662011-04-26 08:12:10 +00005302 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005303 return;
5304
5305 /* Check Flow Attention register */
5306 val = tr32(HOSTCC_FLOW_ATTN);
5307 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5308 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5309 real_error = true;
5310 }
5311
5312 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5313 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5314 real_error = true;
5315 }
5316
5317 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5318 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5319 real_error = true;
5320 }
5321
5322 if (!real_error)
5323 return;
5324
5325 tg3_dump_state(tp);
5326
Joe Perches63c3a662011-04-26 08:12:10 +00005327 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsone64de4e2011-04-13 11:05:05 +00005328 schedule_work(&tp->reset_task);
5329}
5330
David S. Miller6f535762007-10-11 18:08:29 -07005331static int tg3_poll(struct napi_struct *napi, int budget)
5332{
Matt Carlson8ef04422009-08-28 14:01:37 +00005333 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5334 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005335 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005336 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005337
5338 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00005339 if (sblk->status & SD_STATUS_ERROR)
5340 tg3_process_error(tp);
5341
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005342 tg3_poll_link(tp);
5343
Matt Carlson17375d22009-08-28 14:02:18 +00005344 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005345
Joe Perches63c3a662011-04-26 08:12:10 +00005346 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07005347 goto tx_recovery;
5348
5349 if (unlikely(work_done >= budget))
5350 break;
5351
Joe Perches63c3a662011-04-26 08:12:10 +00005352 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00005353 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005354 * to tell the hw how much work has been processed,
5355 * so we must read it before checking for more work.
5356 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005357 tnapi->last_tag = sblk->status_tag;
5358 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005359 rmb();
5360 } else
5361 sblk->status &= ~SD_STATUS_UPDATED;
5362
Matt Carlson17375d22009-08-28 14:02:18 +00005363 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005364 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005365 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005366 break;
5367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 }
5369
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005370 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005371
5372tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005373 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005374 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005375 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005376 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377}
5378
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005379static void tg3_napi_disable(struct tg3 *tp)
5380{
5381 int i;
5382
5383 for (i = tp->irq_cnt - 1; i >= 0; i--)
5384 napi_disable(&tp->napi[i].napi);
5385}
5386
5387static void tg3_napi_enable(struct tg3 *tp)
5388{
5389 int i;
5390
5391 for (i = 0; i < tp->irq_cnt; i++)
5392 napi_enable(&tp->napi[i].napi);
5393}
5394
5395static void tg3_napi_init(struct tg3 *tp)
5396{
5397 int i;
5398
5399 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5400 for (i = 1; i < tp->irq_cnt; i++)
5401 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5402}
5403
5404static void tg3_napi_fini(struct tg3 *tp)
5405{
5406 int i;
5407
5408 for (i = 0; i < tp->irq_cnt; i++)
5409 netif_napi_del(&tp->napi[i].napi);
5410}
5411
5412static inline void tg3_netif_stop(struct tg3 *tp)
5413{
5414 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5415 tg3_napi_disable(tp);
5416 netif_tx_disable(tp->dev);
5417}
5418
5419static inline void tg3_netif_start(struct tg3 *tp)
5420{
5421 /* NOTE: unconditional netif_tx_wake_all_queues is only
5422 * appropriate so long as all callers are assured to
5423 * have free tx slots (such as after tg3_init_hw)
5424 */
5425 netif_tx_wake_all_queues(tp->dev);
5426
5427 tg3_napi_enable(tp);
5428 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5429 tg3_enable_ints(tp);
5430}
5431
David S. Millerf47c11e2005-06-24 20:18:35 -07005432static void tg3_irq_quiesce(struct tg3 *tp)
5433{
Matt Carlson4f125f42009-09-01 12:55:02 +00005434 int i;
5435
David S. Millerf47c11e2005-06-24 20:18:35 -07005436 BUG_ON(tp->irq_sync);
5437
5438 tp->irq_sync = 1;
5439 smp_mb();
5440
Matt Carlson4f125f42009-09-01 12:55:02 +00005441 for (i = 0; i < tp->irq_cnt; i++)
5442 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005443}
5444
David S. Millerf47c11e2005-06-24 20:18:35 -07005445/* Fully shutdown all tg3 driver activity elsewhere in the system.
5446 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5447 * with as well. Most of the time, this is not necessary except when
5448 * shutting down the device.
5449 */
5450static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5451{
Michael Chan46966542007-07-11 19:47:19 -07005452 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005453 if (irq_sync)
5454 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005455}
5456
5457static inline void tg3_full_unlock(struct tg3 *tp)
5458{
David S. Millerf47c11e2005-06-24 20:18:35 -07005459 spin_unlock_bh(&tp->lock);
5460}
5461
Michael Chanfcfa0a32006-03-20 22:28:41 -08005462/* One-shot MSI handler - Chip automatically disables interrupt
5463 * after sending MSI so driver doesn't have to do it.
5464 */
David Howells7d12e782006-10-05 14:55:46 +01005465static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005466{
Matt Carlson09943a12009-08-28 14:01:57 +00005467 struct tg3_napi *tnapi = dev_id;
5468 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005469
Matt Carlson898a56f2009-08-28 14:02:40 +00005470 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005471 if (tnapi->rx_rcb)
5472 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005473
5474 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005475 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005476
5477 return IRQ_HANDLED;
5478}
5479
Michael Chan88b06bc22005-04-21 17:13:25 -07005480/* MSI ISR - No need to check for interrupt sharing and no need to
5481 * flush status block and interrupt mailbox. PCI ordering rules
5482 * guarantee that MSI will arrive after the status block.
5483 */
David Howells7d12e782006-10-05 14:55:46 +01005484static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005485{
Matt Carlson09943a12009-08-28 14:01:57 +00005486 struct tg3_napi *tnapi = dev_id;
5487 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005488
Matt Carlson898a56f2009-08-28 14:02:40 +00005489 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005490 if (tnapi->rx_rcb)
5491 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005492 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005493 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005494 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005495 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005496 * NIC to stop sending us irqs, engaging "in-intr-handler"
5497 * event coalescing.
5498 */
5499 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005500 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005501 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005502
Michael Chan88b06bc22005-04-21 17:13:25 -07005503 return IRQ_RETVAL(1);
5504}
5505
David Howells7d12e782006-10-05 14:55:46 +01005506static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507{
Matt Carlson09943a12009-08-28 14:01:57 +00005508 struct tg3_napi *tnapi = dev_id;
5509 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005510 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 unsigned int handled = 1;
5512
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 /* In INTx mode, it is possible for the interrupt to arrive at
5514 * the CPU before the status block posted prior to the interrupt.
5515 * Reading the PCI State register will confirm whether the
5516 * interrupt is ours and will flush the status block.
5517 */
Michael Chand18edcb2007-03-24 20:57:11 -07005518 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00005519 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005520 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5521 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005522 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005523 }
Michael Chand18edcb2007-03-24 20:57:11 -07005524 }
5525
5526 /*
5527 * Writing any value to intr-mbox-0 clears PCI INTA# and
5528 * chip-internal interrupt pending events.
5529 * Writing non-zero to intr-mbox-0 additional tells the
5530 * NIC to stop sending us irqs, engaging "in-intr-handler"
5531 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005532 *
5533 * Flush the mailbox to de-assert the IRQ immediately to prevent
5534 * spurious interrupts. The flush impacts performance but
5535 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005536 */
Michael Chanc04cb342007-05-07 00:26:15 -07005537 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005538 if (tg3_irq_sync(tp))
5539 goto out;
5540 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005541 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005542 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005543 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005544 } else {
5545 /* No work, shared interrupt perhaps? re-enable
5546 * interrupts, and flush that PCI write
5547 */
5548 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5549 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005550 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005551out:
David S. Millerfac9b832005-05-18 22:46:34 -07005552 return IRQ_RETVAL(handled);
5553}
5554
David Howells7d12e782006-10-05 14:55:46 +01005555static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005556{
Matt Carlson09943a12009-08-28 14:01:57 +00005557 struct tg3_napi *tnapi = dev_id;
5558 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005559 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005560 unsigned int handled = 1;
5561
David S. Millerfac9b832005-05-18 22:46:34 -07005562 /* In INTx mode, it is possible for the interrupt to arrive at
5563 * the CPU before the status block posted prior to the interrupt.
5564 * Reading the PCI State register will confirm whether the
5565 * interrupt is ours and will flush the status block.
5566 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005567 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00005568 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005569 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5570 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005571 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 }
Michael Chand18edcb2007-03-24 20:57:11 -07005573 }
5574
5575 /*
5576 * writing any value to intr-mbox-0 clears PCI INTA# and
5577 * chip-internal interrupt pending events.
5578 * writing non-zero to intr-mbox-0 additional tells the
5579 * NIC to stop sending us irqs, engaging "in-intr-handler"
5580 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005581 *
5582 * Flush the mailbox to de-assert the IRQ immediately to prevent
5583 * spurious interrupts. The flush impacts performance but
5584 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005585 */
Michael Chanc04cb342007-05-07 00:26:15 -07005586 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005587
5588 /*
5589 * In a shared interrupt configuration, sometimes other devices'
5590 * interrupts will scream. We record the current status tag here
5591 * so that the above check can report that the screaming interrupts
5592 * are unhandled. Eventually they will be silenced.
5593 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005594 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005595
Michael Chand18edcb2007-03-24 20:57:11 -07005596 if (tg3_irq_sync(tp))
5597 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005598
Matt Carlson72334482009-08-28 14:03:01 +00005599 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005600
Matt Carlson09943a12009-08-28 14:01:57 +00005601 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005602
David S. Millerf47c11e2005-06-24 20:18:35 -07005603out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 return IRQ_RETVAL(handled);
5605}
5606
Michael Chan79381092005-04-21 17:13:59 -07005607/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005608static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005609{
Matt Carlson09943a12009-08-28 14:01:57 +00005610 struct tg3_napi *tnapi = dev_id;
5611 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005612 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005613
Michael Chanf9804dd2005-09-27 12:13:10 -07005614 if ((sblk->status & SD_STATUS_UPDATED) ||
5615 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005616 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005617 return IRQ_RETVAL(1);
5618 }
5619 return IRQ_RETVAL(0);
5620}
5621
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005622static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005623static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624
Michael Chanb9ec6c12006-07-25 16:37:27 -07005625/* Restart hardware after configuration changes, self-test, etc.
5626 * Invoked with tp->lock held.
5627 */
5628static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005629 __releases(tp->lock)
5630 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005631{
5632 int err;
5633
5634 err = tg3_init_hw(tp, reset_phy);
5635 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005636 netdev_err(tp->dev,
5637 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005638 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5639 tg3_full_unlock(tp);
5640 del_timer_sync(&tp->timer);
5641 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005642 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005643 dev_close(tp->dev);
5644 tg3_full_lock(tp, 0);
5645 }
5646 return err;
5647}
5648
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649#ifdef CONFIG_NET_POLL_CONTROLLER
5650static void tg3_poll_controller(struct net_device *dev)
5651{
Matt Carlson4f125f42009-09-01 12:55:02 +00005652 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005653 struct tg3 *tp = netdev_priv(dev);
5654
Matt Carlson4f125f42009-09-01 12:55:02 +00005655 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005656 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657}
5658#endif
5659
David Howellsc4028952006-11-22 14:57:56 +00005660static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661{
David Howellsc4028952006-11-22 14:57:56 +00005662 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005663 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 unsigned int restart_timer;
5665
Michael Chan7faa0062006-02-02 17:29:28 -08005666 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005667
5668 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005669 tg3_full_unlock(tp);
5670 return;
5671 }
5672
5673 tg3_full_unlock(tp);
5674
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005675 tg3_phy_stop(tp);
5676
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 tg3_netif_stop(tp);
5678
David S. Millerf47c11e2005-06-24 20:18:35 -07005679 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680
Joe Perches63c3a662011-04-26 08:12:10 +00005681 restart_timer = tg3_flag(tp, RESTART_TIMER);
5682 tg3_flag_clear(tp, RESTART_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
Joe Perches63c3a662011-04-26 08:12:10 +00005684 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07005685 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5686 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00005687 tg3_flag_set(tp, MBOX_WRITE_REORDER);
5688 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005689 }
5690
Michael Chan944d9802005-05-29 14:57:48 -07005691 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005692 err = tg3_init_hw(tp, 1);
5693 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005694 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
5696 tg3_netif_start(tp);
5697
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 if (restart_timer)
5699 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005700
Michael Chanb9ec6c12006-07-25 16:37:27 -07005701out:
Michael Chan7faa0062006-02-02 17:29:28 -08005702 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005703
5704 if (!err)
5705 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706}
5707
5708static void tg3_tx_timeout(struct net_device *dev)
5709{
5710 struct tg3 *tp = netdev_priv(dev);
5711
Michael Chanb0408752007-02-13 12:18:30 -08005712 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005713 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00005714 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08005715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716
5717 schedule_work(&tp->reset_task);
5718}
5719
Michael Chanc58ec932005-09-17 00:46:27 -07005720/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5721static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5722{
5723 u32 base = (u32) mapping & 0xffffffff;
5724
Eric Dumazet807540b2010-09-23 05:40:09 +00005725 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005726}
5727
Michael Chan72f2afb2006-03-06 19:28:35 -08005728/* Test for DMA addresses > 40-bit */
5729static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5730 int len)
5731{
5732#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00005733 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00005734 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005735 return 0;
5736#else
5737 return 0;
5738#endif
5739}
5740
Matt Carlson2ffcc982011-05-19 12:12:44 +00005741static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
5742 dma_addr_t mapping, int len, u32 flags,
5743 u32 mss_and_is_end)
5744{
5745 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
5746 int is_end = (mss_and_is_end & 0x1);
5747 u32 mss = (mss_and_is_end >> 1);
5748 u32 vlan_tag = 0;
5749
5750 if (is_end)
5751 flags |= TXD_FLAG_END;
5752 if (flags & TXD_FLAG_VLAN) {
5753 vlan_tag = flags >> 16;
5754 flags &= 0xffff;
5755 }
5756 vlan_tag |= (mss << TXD_MSS_SHIFT);
5757
5758 txd->addr_hi = ((u64) mapping >> 32);
5759 txd->addr_lo = ((u64) mapping & 0xffffffff);
5760 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5761 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5762}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763
Matt Carlson432aa7e2011-05-19 12:12:45 +00005764static void tg3_skb_error_unmap(struct tg3_napi *tnapi,
5765 struct sk_buff *skb, int last)
5766{
5767 int i;
5768 u32 entry = tnapi->tx_prod;
5769 struct ring_info *txb = &tnapi->tx_buffers[entry];
5770
5771 pci_unmap_single(tnapi->tp->pdev,
5772 dma_unmap_addr(txb, mapping),
5773 skb_headlen(skb),
5774 PCI_DMA_TODEVICE);
5775 for (i = 0; i <= last; i++) {
5776 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5777
5778 entry = NEXT_TX(entry);
5779 txb = &tnapi->tx_buffers[entry];
5780
5781 pci_unmap_page(tnapi->tp->pdev,
5782 dma_unmap_addr(txb, mapping),
5783 frag->size, PCI_DMA_TODEVICE);
5784 }
5785}
5786
Michael Chan72f2afb2006-03-06 19:28:35 -08005787/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005788static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
Matt Carlson432aa7e2011-05-19 12:12:45 +00005789 struct sk_buff *skb,
5790 u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005792 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005793 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005794 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005795 u32 entry = tnapi->tx_prod;
5796 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797
Matt Carlson41588ba2008-04-19 18:12:33 -07005798 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5799 new_skb = skb_copy(skb, GFP_ATOMIC);
5800 else {
5801 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5802
5803 new_skb = skb_copy_expand(skb,
5804 skb_headroom(skb) + more_headroom,
5805 skb_tailroom(skb), GFP_ATOMIC);
5806 }
5807
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005809 ret = -1;
5810 } else {
5811 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005812 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5813 PCI_DMA_TODEVICE);
5814 /* Make sure the mapping succeeded */
5815 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5816 ret = -1;
5817 dev_kfree_skb(new_skb);
David S. Miller90079ce2008-09-11 04:52:51 -07005818
Michael Chanc58ec932005-09-17 00:46:27 -07005819 /* Make sure new skb does not cross any 4G boundaries.
5820 * Drop the packet if it does.
5821 */
Joe Perches63c3a662011-04-26 08:12:10 +00005822 } else if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
5823 tg3_4g_overflow_test(new_addr, new_skb->len)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005824 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5825 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005826 ret = -1;
5827 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07005828 } else {
Matt Carlson432aa7e2011-05-19 12:12:45 +00005829 tnapi->tx_buffers[entry].skb = new_skb;
5830 dma_unmap_addr_set(&tnapi->tx_buffers[entry],
5831 mapping, new_addr);
5832
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005833 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005834 base_flags, 1 | (mss << 1));
Michael Chanc58ec932005-09-17 00:46:27 -07005835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 }
5837
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 dev_kfree_skb(skb);
5839
Michael Chanc58ec932005-09-17 00:46:27 -07005840 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841}
5842
Matt Carlson2ffcc982011-05-19 12:12:44 +00005843static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005844
5845/* Use GSO to workaround a rare TSO bug that may be triggered when the
5846 * TSO header is greater than 80 bytes.
5847 */
5848static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5849{
5850 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005851 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005852
5853 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005854 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005855 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005856
5857 /* netif_tx_stop_queue() must be done before checking
5858 * checking tx index in tg3_tx_avail() below, because in
5859 * tg3_tx(), we update tx index before checking for
5860 * netif_tx_queue_stopped().
5861 */
5862 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005863 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005864 return NETDEV_TX_BUSY;
5865
5866 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005867 }
5868
5869 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005870 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005871 goto tg3_tso_bug_end;
5872
5873 do {
5874 nskb = segs;
5875 segs = segs->next;
5876 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00005877 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005878 } while (segs);
5879
5880tg3_tso_bug_end:
5881 dev_kfree_skb(skb);
5882
5883 return NETDEV_TX_OK;
5884}
Michael Chan52c0fd82006-06-29 20:15:54 -07005885
Michael Chan5a6f3072006-03-20 22:28:05 -08005886/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00005887 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005888 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00005889static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005890{
5891 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005892 u32 len, entry, base_flags, mss;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005893 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005894 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005895 struct tg3_napi *tnapi;
5896 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005897 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005898
Matt Carlson24f4efd2009-11-13 13:03:35 +00005899 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5900 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00005901 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00005902 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903
Michael Chan00b70502006-06-17 21:58:45 -07005904 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005905 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005906 * interrupt. Furthermore, IRQ processing runs lockless so we have
5907 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005909 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005910 if (!netif_tx_queue_stopped(txq)) {
5911 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005912
5913 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005914 netdev_err(dev,
5915 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 return NETDEV_TX_BUSY;
5918 }
5919
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005920 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005922 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005924
Matt Carlsonbe98da62010-07-11 09:31:46 +00005925 mss = skb_shinfo(skb)->gso_size;
5926 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005927 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00005928 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929
5930 if (skb_header_cloned(skb) &&
5931 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5932 dev_kfree_skb(skb);
5933 goto out_unlock;
5934 }
5935
Matt Carlson34195c32010-07-11 09:31:42 +00005936 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005937 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938
Matt Carlson02e96082010-09-15 08:59:59 +00005939 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00005940 hdr_len = skb_headlen(skb) - ETH_HLEN;
5941 } else {
5942 u32 ip_tcp_len;
5943
5944 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5945 hdr_len = ip_tcp_len + tcp_opt_len;
5946
5947 iph->check = 0;
5948 iph->tot_len = htons(mss + hdr_len);
5949 }
5950
Michael Chan52c0fd82006-06-29 20:15:54 -07005951 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00005952 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00005953 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07005954
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5956 TXD_FLAG_CPU_POST_DMA);
5957
Joe Perches63c3a662011-04-26 08:12:10 +00005958 if (tg3_flag(tp, HW_TSO_1) ||
5959 tg3_flag(tp, HW_TSO_2) ||
5960 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005961 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005963 } else
5964 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5965 iph->daddr, 0,
5966 IPPROTO_TCP,
5967 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968
Joe Perches63c3a662011-04-26 08:12:10 +00005969 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00005970 mss |= (hdr_len & 0xc) << 12;
5971 if (hdr_len & 0x10)
5972 base_flags |= 0x00000010;
5973 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00005974 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005975 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00005976 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005977 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005978 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 int tsflags;
5980
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005981 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 mss |= (tsflags << 11);
5983 }
5984 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005985 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 int tsflags;
5987
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005988 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 base_flags |= tsflags << 12;
5990 }
5991 }
5992 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00005993
Jesse Grosseab6d182010-10-20 13:56:03 +00005994 if (vlan_tx_tag_present(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 base_flags |= (TXD_FLAG_VLAN |
5996 (vlan_tx_tag_get(skb) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997
Joe Perches63c3a662011-04-26 08:12:10 +00005998 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00005999 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006000 base_flags |= TXD_FLAG_JMB_PKT;
6001
Alexander Duyckf4188d82009-12-02 16:48:38 +00006002 len = skb_headlen(skb);
6003
6004 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6005 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006006 dev_kfree_skb(skb);
6007 goto out_unlock;
6008 }
6009
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006010 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006011 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012
6013 would_hit_hwbug = 0;
6014
Joe Perches63c3a662011-04-26 08:12:10 +00006015 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006016 would_hit_hwbug = 1;
6017
Joe Perches63c3a662011-04-26 08:12:10 +00006018 if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006019 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07006020 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00006021
Joe Perches63c3a662011-04-26 08:12:10 +00006022 if (tg3_flag(tp, 40BIT_DMA_LIMIT_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006023 tg3_40bit_overflow_test(tp, mapping, len))
6024 would_hit_hwbug = 1;
6025
Joe Perches63c3a662011-04-26 08:12:10 +00006026 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006027 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006029 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6031
6032 entry = NEXT_TX(entry);
6033
6034 /* Now loop through additional data fragments, and queue them. */
6035 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 last = skb_shinfo(skb)->nr_frags - 1;
6037 for (i = 0; i <= last; i++) {
6038 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6039
6040 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006041 mapping = pci_map_page(tp->pdev,
6042 frag->page,
6043 frag->page_offset,
6044 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006046 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006047 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006048 mapping);
6049 if (pci_dma_mapping_error(tp->pdev, mapping))
6050 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051
Joe Perches63c3a662011-04-26 08:12:10 +00006052 if (tg3_flag(tp, SHORT_DMA_BUG) &&
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006053 len <= 8)
6054 would_hit_hwbug = 1;
6055
Joe Perches63c3a662011-04-26 08:12:10 +00006056 if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006057 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07006058 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059
Joe Perches63c3a662011-04-26 08:12:10 +00006060 if (tg3_flag(tp, 40BIT_DMA_LIMIT_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006061 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08006062 would_hit_hwbug = 1;
6063
Joe Perches63c3a662011-04-26 08:12:10 +00006064 if (tg3_flag(tp, HW_TSO_1) ||
6065 tg3_flag(tp, HW_TSO_2) ||
6066 tg3_flag(tp, HW_TSO_3))
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006067 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 base_flags, (i == last)|(mss << 1));
6069 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006070 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 base_flags, (i == last));
6072
6073 entry = NEXT_TX(entry);
6074 }
6075 }
6076
6077 if (would_hit_hwbug) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00006078 tg3_skb_error_unmap(tnapi, skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
6080 /* If the workaround fails due to memory/mapping
6081 * failure, silently drop this packet.
6082 */
Matt Carlson432aa7e2011-05-19 12:12:45 +00006083 if (tigon3_dma_hwbug_workaround(tnapi, skb, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 goto out_unlock;
6085
Matt Carlson432aa7e2011-05-19 12:12:45 +00006086 entry = NEXT_TX(tnapi->tx_prod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 }
6088
6089 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006090 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006092 tnapi->tx_prod = entry;
6093 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006094 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006095
6096 /* netif_tx_stop_queue() must be done before checking
6097 * checking tx index in tg3_tx_avail() below, because in
6098 * tg3_tx(), we update tx index before checking for
6099 * netif_tx_queue_stopped().
6100 */
6101 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006102 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006103 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105
6106out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006107 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108
6109 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006110
6111dma_error:
Matt Carlson432aa7e2011-05-19 12:12:45 +00006112 tg3_skb_error_unmap(tnapi, skb, i);
Alexander Duyckf4188d82009-12-02 16:48:38 +00006113 dev_kfree_skb(skb);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006114 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006115 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116}
6117
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006118static void tg3_set_loopback(struct net_device *dev, u32 features)
6119{
6120 struct tg3 *tp = netdev_priv(dev);
6121
6122 if (features & NETIF_F_LOOPBACK) {
6123 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6124 return;
6125
6126 /*
6127 * Clear MAC_MODE_HALF_DUPLEX or you won't get packets back in
6128 * loopback mode if Half-Duplex mode was negotiated earlier.
6129 */
6130 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
6131
6132 /* Enable internal MAC loopback mode */
6133 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6134 spin_lock_bh(&tp->lock);
6135 tw32(MAC_MODE, tp->mac_mode);
6136 netif_carrier_on(tp->dev);
6137 spin_unlock_bh(&tp->lock);
6138 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6139 } else {
6140 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6141 return;
6142
6143 /* Disable internal MAC loopback mode */
6144 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6145 spin_lock_bh(&tp->lock);
6146 tw32(MAC_MODE, tp->mac_mode);
6147 /* Force link status check */
6148 tg3_setup_phy(tp, 1);
6149 spin_unlock_bh(&tp->lock);
6150 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6151 }
6152}
6153
Michał Mirosławdc668912011-04-07 03:35:07 +00006154static u32 tg3_fix_features(struct net_device *dev, u32 features)
6155{
6156 struct tg3 *tp = netdev_priv(dev);
6157
Joe Perches63c3a662011-04-26 08:12:10 +00006158 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006159 features &= ~NETIF_F_ALL_TSO;
6160
6161 return features;
6162}
6163
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006164static int tg3_set_features(struct net_device *dev, u32 features)
6165{
6166 u32 changed = dev->features ^ features;
6167
6168 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
6169 tg3_set_loopback(dev, features);
6170
6171 return 0;
6172}
6173
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6175 int new_mtu)
6176{
6177 dev->mtu = new_mtu;
6178
Michael Chanef7f5ec2005-07-25 12:32:25 -07006179 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00006180 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006181 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00006182 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00006183 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006184 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00006185 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006186 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006187 if (tg3_flag(tp, 5780_CLASS)) {
6188 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00006189 netdev_update_features(dev);
6190 }
Joe Perches63c3a662011-04-26 08:12:10 +00006191 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07006192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193}
6194
6195static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6196{
6197 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006198 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199
6200 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6201 return -EINVAL;
6202
6203 if (!netif_running(dev)) {
6204 /* We'll just catch it later when the
6205 * device is up'd.
6206 */
6207 tg3_set_mtu(dev, tp, new_mtu);
6208 return 0;
6209 }
6210
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006211 tg3_phy_stop(tp);
6212
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006214
6215 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216
Michael Chan944d9802005-05-29 14:57:48 -07006217 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218
6219 tg3_set_mtu(dev, tp, new_mtu);
6220
Michael Chanb9ec6c12006-07-25 16:37:27 -07006221 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222
Michael Chanb9ec6c12006-07-25 16:37:27 -07006223 if (!err)
6224 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225
David S. Millerf47c11e2005-06-24 20:18:35 -07006226 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006228 if (!err)
6229 tg3_phy_start(tp);
6230
Michael Chanb9ec6c12006-07-25 16:37:27 -07006231 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232}
6233
Matt Carlson21f581a2009-08-28 14:00:25 +00006234static void tg3_rx_prodring_free(struct tg3 *tp,
6235 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 int i;
6238
Matt Carlson8fea32b2010-09-15 08:59:58 +00006239 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006240 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006241 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006242 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6243 tp->rx_pkt_map_sz);
6244
Joe Perches63c3a662011-04-26 08:12:10 +00006245 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006246 for (i = tpr->rx_jmb_cons_idx;
6247 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006248 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006249 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6250 TG3_RX_JMB_MAP_SZ);
6251 }
6252 }
6253
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006254 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256
Matt Carlson2c49a442010-09-30 10:34:35 +00006257 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006258 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6259 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260
Joe Perches63c3a662011-04-26 08:12:10 +00006261 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006262 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006263 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6264 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 }
6266}
6267
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006268/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 *
6270 * The chip has been shut down and the driver detached from
6271 * the networking, so no interrupts or new tx packets will
6272 * end up in the driver. tp->{tx,}lock are held and thus
6273 * we may not sleep.
6274 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006275static int tg3_rx_prodring_alloc(struct tg3 *tp,
6276 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277{
Matt Carlson287be122009-08-28 13:58:46 +00006278 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006280 tpr->rx_std_cons_idx = 0;
6281 tpr->rx_std_prod_idx = 0;
6282 tpr->rx_jmb_cons_idx = 0;
6283 tpr->rx_jmb_prod_idx = 0;
6284
Matt Carlson8fea32b2010-09-15 08:59:58 +00006285 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006286 memset(&tpr->rx_std_buffers[0], 0,
6287 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006288 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006289 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006290 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006291 goto done;
6292 }
6293
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006295 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296
Matt Carlson287be122009-08-28 13:58:46 +00006297 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00006298 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006299 tp->dev->mtu > ETH_DATA_LEN)
6300 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6301 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006302
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303 /* Initialize invariants of the rings, we only set this
6304 * stuff once. This works because the card does not
6305 * write into the rx buffer posting rings.
6306 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006307 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 struct tg3_rx_buffer_desc *rxd;
6309
Matt Carlson21f581a2009-08-28 14:00:25 +00006310 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006311 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6313 rxd->opaque = (RXD_OPAQUE_RING_STD |
6314 (i << RXD_OPAQUE_INDEX_SHIFT));
6315 }
6316
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006317 /* Now allocate fresh SKBs for each rx ring. */
6318 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006319 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006320 netdev_warn(tp->dev,
6321 "Using a smaller RX standard ring. Only "
6322 "%d out of %d buffers were allocated "
6323 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006324 if (i == 0)
6325 goto initfail;
6326 tp->rx_pending = i;
6327 break;
6328 }
6329 }
6330
Joe Perches63c3a662011-04-26 08:12:10 +00006331 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006332 goto done;
6333
Matt Carlson2c49a442010-09-30 10:34:35 +00006334 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006335
Joe Perches63c3a662011-04-26 08:12:10 +00006336 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00006337 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338
Matt Carlson2c49a442010-09-30 10:34:35 +00006339 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006340 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341
Matt Carlson0d86df82010-02-17 15:17:00 +00006342 rxd = &tpr->rx_jmb[i].std;
6343 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6344 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6345 RXD_FLAG_JUMBO;
6346 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6347 (i << RXD_OPAQUE_INDEX_SHIFT));
6348 }
6349
6350 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6351 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006352 netdev_warn(tp->dev,
6353 "Using a smaller RX jumbo ring. Only %d "
6354 "out of %d buffers were allocated "
6355 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006356 if (i == 0)
6357 goto initfail;
6358 tp->rx_jumbo_pending = i;
6359 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 }
6361 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006362
6363done:
Michael Chan32d8c572006-07-25 16:38:29 -07006364 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006365
6366initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006367 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006368 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369}
6370
Matt Carlson21f581a2009-08-28 14:00:25 +00006371static void tg3_rx_prodring_fini(struct tg3 *tp,
6372 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373{
Matt Carlson21f581a2009-08-28 14:00:25 +00006374 kfree(tpr->rx_std_buffers);
6375 tpr->rx_std_buffers = NULL;
6376 kfree(tpr->rx_jmb_buffers);
6377 tpr->rx_jmb_buffers = NULL;
6378 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006379 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6380 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006381 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006383 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006384 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6385 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006386 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006388}
6389
Matt Carlson21f581a2009-08-28 14:00:25 +00006390static int tg3_rx_prodring_init(struct tg3 *tp,
6391 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006392{
Matt Carlson2c49a442010-09-30 10:34:35 +00006393 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6394 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006395 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006396 return -ENOMEM;
6397
Matt Carlson4bae65c2010-11-24 08:31:52 +00006398 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6399 TG3_RX_STD_RING_BYTES(tp),
6400 &tpr->rx_std_mapping,
6401 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006402 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006403 goto err_out;
6404
Joe Perches63c3a662011-04-26 08:12:10 +00006405 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006406 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006407 GFP_KERNEL);
6408 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006409 goto err_out;
6410
Matt Carlson4bae65c2010-11-24 08:31:52 +00006411 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6412 TG3_RX_JMB_RING_BYTES(tp),
6413 &tpr->rx_jmb_mapping,
6414 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006415 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006416 goto err_out;
6417 }
6418
6419 return 0;
6420
6421err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006422 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006423 return -ENOMEM;
6424}
6425
6426/* Free up pending packets in all rx/tx rings.
6427 *
6428 * The chip has been shut down and the driver detached from
6429 * the networking, so no interrupts or new tx packets will
6430 * end up in the driver. tp->{tx,}lock is not held and we are not
6431 * in an interrupt context and thus may sleep.
6432 */
6433static void tg3_free_rings(struct tg3 *tp)
6434{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006435 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006436
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006437 for (j = 0; j < tp->irq_cnt; j++) {
6438 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006439
Matt Carlson8fea32b2010-09-15 08:59:58 +00006440 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006441
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006442 if (!tnapi->tx_buffers)
6443 continue;
6444
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006445 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006446 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006447 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006448 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006449
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006450 txp = &tnapi->tx_buffers[i];
6451 skb = txp->skb;
6452
6453 if (skb == NULL) {
6454 i++;
6455 continue;
6456 }
6457
Alexander Duyckf4188d82009-12-02 16:48:38 +00006458 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006459 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006460 skb_headlen(skb),
6461 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006462 txp->skb = NULL;
6463
Alexander Duyckf4188d82009-12-02 16:48:38 +00006464 i++;
6465
6466 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6467 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6468 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006469 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006470 skb_shinfo(skb)->frags[k].size,
6471 PCI_DMA_TODEVICE);
6472 i++;
6473 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006474
6475 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006476 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006477 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006478}
6479
6480/* Initialize tx/rx rings for packet processing.
6481 *
6482 * The chip has been shut down and the driver detached from
6483 * the networking, so no interrupts or new tx packets will
6484 * end up in the driver. tp->{tx,}lock are held and thus
6485 * we may not sleep.
6486 */
6487static int tg3_init_rings(struct tg3 *tp)
6488{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006489 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006490
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006491 /* Free up all the SKBs. */
6492 tg3_free_rings(tp);
6493
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006494 for (i = 0; i < tp->irq_cnt; i++) {
6495 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006496
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006497 tnapi->last_tag = 0;
6498 tnapi->last_irq_tag = 0;
6499 tnapi->hw_status->status = 0;
6500 tnapi->hw_status->status_tag = 0;
6501 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6502
6503 tnapi->tx_prod = 0;
6504 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006505 if (tnapi->tx_ring)
6506 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006507
6508 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006509 if (tnapi->rx_rcb)
6510 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006511
Matt Carlson8fea32b2010-09-15 08:59:58 +00006512 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006513 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006514 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006515 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006516 }
Matt Carlson72334482009-08-28 14:03:01 +00006517
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006518 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006519}
6520
6521/*
6522 * Must not be invoked with interrupt sources disabled and
6523 * the hardware shutdown down.
6524 */
6525static void tg3_free_consistent(struct tg3 *tp)
6526{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006527 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006528
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006529 for (i = 0; i < tp->irq_cnt; i++) {
6530 struct tg3_napi *tnapi = &tp->napi[i];
6531
6532 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006533 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006534 tnapi->tx_ring, tnapi->tx_desc_mapping);
6535 tnapi->tx_ring = NULL;
6536 }
6537
6538 kfree(tnapi->tx_buffers);
6539 tnapi->tx_buffers = NULL;
6540
6541 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006542 dma_free_coherent(&tp->pdev->dev,
6543 TG3_RX_RCB_RING_BYTES(tp),
6544 tnapi->rx_rcb,
6545 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006546 tnapi->rx_rcb = NULL;
6547 }
6548
Matt Carlson8fea32b2010-09-15 08:59:58 +00006549 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6550
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006551 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006552 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6553 tnapi->hw_status,
6554 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006555 tnapi->hw_status = NULL;
6556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006558
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006560 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6561 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562 tp->hw_stats = NULL;
6563 }
6564}
6565
6566/*
6567 * Must not be invoked with interrupt sources disabled and
6568 * the hardware shutdown down. Can sleep.
6569 */
6570static int tg3_alloc_consistent(struct tg3 *tp)
6571{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006572 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006573
Matt Carlson4bae65c2010-11-24 08:31:52 +00006574 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6575 sizeof(struct tg3_hw_stats),
6576 &tp->stats_mapping,
6577 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 if (!tp->hw_stats)
6579 goto err_out;
6580
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6582
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006583 for (i = 0; i < tp->irq_cnt; i++) {
6584 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006585 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006586
Matt Carlson4bae65c2010-11-24 08:31:52 +00006587 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6588 TG3_HW_STATUS_SIZE,
6589 &tnapi->status_mapping,
6590 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006591 if (!tnapi->hw_status)
6592 goto err_out;
6593
6594 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006595 sblk = tnapi->hw_status;
6596
Matt Carlson8fea32b2010-09-15 08:59:58 +00006597 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6598 goto err_out;
6599
Matt Carlson19cfaec2009-12-03 08:36:20 +00006600 /* If multivector TSS is enabled, vector 0 does not handle
6601 * tx interrupts. Don't allocate any resources for it.
6602 */
Joe Perches63c3a662011-04-26 08:12:10 +00006603 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
6604 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00006605 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6606 TG3_TX_RING_SIZE,
6607 GFP_KERNEL);
6608 if (!tnapi->tx_buffers)
6609 goto err_out;
6610
Matt Carlson4bae65c2010-11-24 08:31:52 +00006611 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6612 TG3_TX_RING_BYTES,
6613 &tnapi->tx_desc_mapping,
6614 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006615 if (!tnapi->tx_ring)
6616 goto err_out;
6617 }
6618
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006619 /*
6620 * When RSS is enabled, the status block format changes
6621 * slightly. The "rx_jumbo_consumer", "reserved",
6622 * and "rx_mini_consumer" members get mapped to the
6623 * other three rx return ring producer indexes.
6624 */
6625 switch (i) {
6626 default:
6627 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6628 break;
6629 case 2:
6630 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6631 break;
6632 case 3:
6633 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6634 break;
6635 case 4:
6636 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6637 break;
6638 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006639
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006640 /*
6641 * If multivector RSS is enabled, vector 0 does not handle
6642 * rx or tx interrupts. Don't allocate any resources for it.
6643 */
Joe Perches63c3a662011-04-26 08:12:10 +00006644 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006645 continue;
6646
Matt Carlson4bae65c2010-11-24 08:31:52 +00006647 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6648 TG3_RX_RCB_RING_BYTES(tp),
6649 &tnapi->rx_rcb_mapping,
6650 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006651 if (!tnapi->rx_rcb)
6652 goto err_out;
6653
6654 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006655 }
6656
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 return 0;
6658
6659err_out:
6660 tg3_free_consistent(tp);
6661 return -ENOMEM;
6662}
6663
6664#define MAX_WAIT_CNT 1000
6665
6666/* To stop a block, clear the enable bit and poll till it
6667 * clears. tp->lock is held.
6668 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006669static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670{
6671 unsigned int i;
6672 u32 val;
6673
Joe Perches63c3a662011-04-26 08:12:10 +00006674 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675 switch (ofs) {
6676 case RCVLSC_MODE:
6677 case DMAC_MODE:
6678 case MBFREE_MODE:
6679 case BUFMGR_MODE:
6680 case MEMARB_MODE:
6681 /* We can't enable/disable these bits of the
6682 * 5705/5750, just say success.
6683 */
6684 return 0;
6685
6686 default:
6687 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006689 }
6690
6691 val = tr32(ofs);
6692 val &= ~enable_bit;
6693 tw32_f(ofs, val);
6694
6695 for (i = 0; i < MAX_WAIT_CNT; i++) {
6696 udelay(100);
6697 val = tr32(ofs);
6698 if ((val & enable_bit) == 0)
6699 break;
6700 }
6701
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006702 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006703 dev_err(&tp->pdev->dev,
6704 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6705 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 return -ENODEV;
6707 }
6708
6709 return 0;
6710}
6711
6712/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006713static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714{
6715 int i, err;
6716
6717 tg3_disable_ints(tp);
6718
6719 tp->rx_mode &= ~RX_MODE_ENABLE;
6720 tw32_f(MAC_RX_MODE, tp->rx_mode);
6721 udelay(10);
6722
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006723 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6724 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6725 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6726 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6727 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6728 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006730 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6731 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6732 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6733 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6734 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6735 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6736 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737
6738 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6739 tw32_f(MAC_MODE, tp->mac_mode);
6740 udelay(40);
6741
6742 tp->tx_mode &= ~TX_MODE_ENABLE;
6743 tw32_f(MAC_TX_MODE, tp->tx_mode);
6744
6745 for (i = 0; i < MAX_WAIT_CNT; i++) {
6746 udelay(100);
6747 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6748 break;
6749 }
6750 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006751 dev_err(&tp->pdev->dev,
6752 "%s timed out, TX_MODE_ENABLE will not clear "
6753 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006754 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755 }
6756
Michael Chane6de8ad2005-05-05 14:42:41 -07006757 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006758 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6759 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760
6761 tw32(FTQ_RESET, 0xffffffff);
6762 tw32(FTQ_RESET, 0x00000000);
6763
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006764 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6765 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006767 for (i = 0; i < tp->irq_cnt; i++) {
6768 struct tg3_napi *tnapi = &tp->napi[i];
6769 if (tnapi->hw_status)
6770 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 if (tp->hw_stats)
6773 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6774
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775 return err;
6776}
6777
Matt Carlson0d3031d2007-10-10 18:02:43 -07006778static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6779{
6780 int i;
6781 u32 apedata;
6782
Matt Carlsondc6d0742010-09-15 08:59:55 +00006783 /* NCSI does not support APE events */
Joe Perches63c3a662011-04-26 08:12:10 +00006784 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsondc6d0742010-09-15 08:59:55 +00006785 return;
6786
Matt Carlson0d3031d2007-10-10 18:02:43 -07006787 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6788 if (apedata != APE_SEG_SIG_MAGIC)
6789 return;
6790
6791 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006792 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006793 return;
6794
6795 /* Wait for up to 1 millisecond for APE to service previous event. */
6796 for (i = 0; i < 10; i++) {
6797 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6798 return;
6799
6800 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6801
6802 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6803 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6804 event | APE_EVENT_STATUS_EVENT_PENDING);
6805
6806 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6807
6808 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6809 break;
6810
6811 udelay(100);
6812 }
6813
6814 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6815 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6816}
6817
6818static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6819{
6820 u32 event;
6821 u32 apedata;
6822
Joe Perches63c3a662011-04-26 08:12:10 +00006823 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006824 return;
6825
6826 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006827 case RESET_KIND_INIT:
6828 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6829 APE_HOST_SEG_SIG_MAGIC);
6830 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6831 APE_HOST_SEG_LEN_MAGIC);
6832 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6833 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6834 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006835 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006836 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6837 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006838 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6839 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006840
Matt Carlson33f401a2010-04-05 10:19:27 +00006841 event = APE_EVENT_STATUS_STATE_START;
6842 break;
6843 case RESET_KIND_SHUTDOWN:
6844 /* With the interface we are currently using,
6845 * APE does not track driver state. Wiping
6846 * out the HOST SEGMENT SIGNATURE forces
6847 * the APE to assume OS absent status.
6848 */
6849 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006850
Matt Carlsondc6d0742010-09-15 08:59:55 +00006851 if (device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006852 tg3_flag(tp, WOL_ENABLE)) {
Matt Carlsondc6d0742010-09-15 08:59:55 +00006853 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6854 TG3_APE_HOST_WOL_SPEED_AUTO);
6855 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6856 } else
6857 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6858
6859 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6860
Matt Carlson33f401a2010-04-05 10:19:27 +00006861 event = APE_EVENT_STATUS_STATE_UNLOAD;
6862 break;
6863 case RESET_KIND_SUSPEND:
6864 event = APE_EVENT_STATUS_STATE_SUSPEND;
6865 break;
6866 default:
6867 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006868 }
6869
6870 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6871
6872 tg3_ape_send_event(tp, event);
6873}
6874
Michael Chane6af3012005-04-21 17:12:05 -07006875/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6877{
David S. Millerf49639e2006-06-09 11:58:36 -07006878 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6879 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880
Joe Perches63c3a662011-04-26 08:12:10 +00006881 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882 switch (kind) {
6883 case RESET_KIND_INIT:
6884 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6885 DRV_STATE_START);
6886 break;
6887
6888 case RESET_KIND_SHUTDOWN:
6889 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6890 DRV_STATE_UNLOAD);
6891 break;
6892
6893 case RESET_KIND_SUSPEND:
6894 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6895 DRV_STATE_SUSPEND);
6896 break;
6897
6898 default:
6899 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006902
6903 if (kind == RESET_KIND_INIT ||
6904 kind == RESET_KIND_SUSPEND)
6905 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906}
6907
6908/* tp->lock is held. */
6909static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6910{
Joe Perches63c3a662011-04-26 08:12:10 +00006911 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912 switch (kind) {
6913 case RESET_KIND_INIT:
6914 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6915 DRV_STATE_START_DONE);
6916 break;
6917
6918 case RESET_KIND_SHUTDOWN:
6919 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6920 DRV_STATE_UNLOAD_DONE);
6921 break;
6922
6923 default:
6924 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006927
6928 if (kind == RESET_KIND_SHUTDOWN)
6929 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930}
6931
6932/* tp->lock is held. */
6933static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6934{
Joe Perches63c3a662011-04-26 08:12:10 +00006935 if (tg3_flag(tp, ENABLE_ASF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936 switch (kind) {
6937 case RESET_KIND_INIT:
6938 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6939 DRV_STATE_START);
6940 break;
6941
6942 case RESET_KIND_SHUTDOWN:
6943 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6944 DRV_STATE_UNLOAD);
6945 break;
6946
6947 case RESET_KIND_SUSPEND:
6948 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6949 DRV_STATE_SUSPEND);
6950 break;
6951
6952 default:
6953 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955 }
6956}
6957
Michael Chan7a6f4362006-09-27 16:03:31 -07006958static int tg3_poll_fw(struct tg3 *tp)
6959{
6960 int i;
6961 u32 val;
6962
Michael Chanb5d37722006-09-27 16:06:21 -07006963 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006964 /* Wait up to 20ms for init done. */
6965 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006966 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6967 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006968 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006969 }
6970 return -ENODEV;
6971 }
6972
Michael Chan7a6f4362006-09-27 16:03:31 -07006973 /* Wait for firmware initialization to complete. */
6974 for (i = 0; i < 100000; i++) {
6975 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6976 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6977 break;
6978 udelay(10);
6979 }
6980
6981 /* Chip might not be fitted with firmware. Some Sun onboard
6982 * parts are configured like that. So don't signal the timeout
6983 * of the above loop as an error, but do report the lack of
6984 * running firmware once.
6985 */
Joe Perches63c3a662011-04-26 08:12:10 +00006986 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
6987 tg3_flag_set(tp, NO_FWARE_REPORTED);
Michael Chan7a6f4362006-09-27 16:03:31 -07006988
Joe Perches05dbe002010-02-17 19:44:19 +00006989 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07006990 }
6991
Matt Carlson6b10c162010-02-12 14:47:08 +00006992 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
6993 /* The 57765 A0 needs a little more
6994 * time to do some important work.
6995 */
6996 mdelay(10);
6997 }
6998
Michael Chan7a6f4362006-09-27 16:03:31 -07006999 return 0;
7000}
7001
Michael Chanee6a99b2007-07-18 21:49:10 -07007002/* Save PCI command register before chip reset */
7003static void tg3_save_pci_state(struct tg3 *tp)
7004{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007005 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007006}
7007
7008/* Restore PCI state after chip reset */
7009static void tg3_restore_pci_state(struct tg3 *tp)
7010{
7011 u32 val;
7012
7013 /* Re-enable indirect register accesses. */
7014 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7015 tp->misc_host_ctrl);
7016
7017 /* Set MAX PCI retry to zero. */
7018 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7019 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007020 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007021 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007022 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007023 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007024 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007025 PCISTATE_ALLOW_APE_SHMEM_WR |
7026 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007027 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7028
Matt Carlson8a6eac92007-10-21 16:17:55 -07007029 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007030
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007031 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +00007032 if (tg3_flag(tp, PCI_EXPRESS))
Matt Carlsoncf790032010-11-24 08:31:48 +00007033 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007034 else {
7035 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7036 tp->pci_cacheline_sz);
7037 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7038 tp->pci_lat_timer);
7039 }
Michael Chan114342f2007-10-15 02:12:26 -07007040 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007041
Michael Chanee6a99b2007-07-18 21:49:10 -07007042 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007043 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007044 u16 pcix_cmd;
7045
7046 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7047 &pcix_cmd);
7048 pcix_cmd &= ~PCI_X_CMD_ERO;
7049 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7050 pcix_cmd);
7051 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007052
Joe Perches63c3a662011-04-26 08:12:10 +00007053 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007054
7055 /* Chip reset on 5780 will reset MSI enable bit,
7056 * so need to restore it.
7057 */
Joe Perches63c3a662011-04-26 08:12:10 +00007058 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007059 u16 ctrl;
7060
7061 pci_read_config_word(tp->pdev,
7062 tp->msi_cap + PCI_MSI_FLAGS,
7063 &ctrl);
7064 pci_write_config_word(tp->pdev,
7065 tp->msi_cap + PCI_MSI_FLAGS,
7066 ctrl | PCI_MSI_FLAGS_ENABLE);
7067 val = tr32(MSGINT_MODE);
7068 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7069 }
7070 }
7071}
7072
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073static void tg3_stop_fw(struct tg3 *);
7074
7075/* tp->lock is held. */
7076static int tg3_chip_reset(struct tg3 *tp)
7077{
7078 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007079 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007080 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081
David S. Millerf49639e2006-06-09 11:58:36 -07007082 tg3_nvram_lock(tp);
7083
Matt Carlson77b483f2008-08-15 14:07:24 -07007084 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7085
David S. Millerf49639e2006-06-09 11:58:36 -07007086 /* No matching tg3_nvram_unlock() after this because
7087 * chip reset below will undo the nvram lock.
7088 */
7089 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090
Michael Chanee6a99b2007-07-18 21:49:10 -07007091 /* GRC_MISC_CFG core clock reset will clear the memory
7092 * enable bit in PCI register 4 and the MSI enable bit
7093 * on some chips, so we save relevant registers here.
7094 */
7095 tg3_save_pci_state(tp);
7096
Michael Chand9ab5ad12006-03-20 22:27:35 -08007097 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007098 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08007099 tw32(GRC_FASTBOOT_PC, 0);
7100
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101 /*
7102 * We must avoid the readl() that normally takes place.
7103 * It locks machines, causes machine checks, and other
7104 * fun things. So, temporarily disable the 5701
7105 * hardware workaround, while we do the reset.
7106 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007107 write_op = tp->write32;
7108 if (write_op == tg3_write_flush_reg32)
7109 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110
Michael Chand18edcb2007-03-24 20:57:11 -07007111 /* Prevent the irq handler from reading or writing PCI registers
7112 * during chip reset when the memory enable bit in the PCI command
7113 * register may be cleared. The chip does not generate interrupt
7114 * at this time, but the irq handler may still be called due to irq
7115 * sharing or irqpoll.
7116 */
Joe Perches63c3a662011-04-26 08:12:10 +00007117 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007118 for (i = 0; i < tp->irq_cnt; i++) {
7119 struct tg3_napi *tnapi = &tp->napi[i];
7120 if (tnapi->hw_status) {
7121 tnapi->hw_status->status = 0;
7122 tnapi->hw_status->status_tag = 0;
7123 }
7124 tnapi->last_tag = 0;
7125 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007126 }
Michael Chand18edcb2007-03-24 20:57:11 -07007127 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007128
7129 for (i = 0; i < tp->irq_cnt; i++)
7130 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007131
Matt Carlson255ca312009-08-25 10:07:27 +00007132 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7133 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7134 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7135 }
7136
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137 /* do the reset */
7138 val = GRC_MISC_CFG_CORECLK_RESET;
7139
Joe Perches63c3a662011-04-26 08:12:10 +00007140 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007141 /* Force PCIe 1.0a mode */
7142 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007143 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007144 tr32(TG3_PCIE_PHY_TSTCTL) ==
7145 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7146 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7147
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7149 tw32(GRC_MISC_CFG, (1 << 29));
7150 val |= (1 << 29);
7151 }
7152 }
7153
Michael Chanb5d37722006-09-27 16:06:21 -07007154 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7155 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7156 tw32(GRC_VCPU_EXT_CTRL,
7157 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7158 }
7159
Matt Carlsonf37500d2010-08-02 11:25:59 +00007160 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007161 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007163
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164 tw32(GRC_MISC_CFG, val);
7165
Michael Chan1ee582d2005-08-09 20:16:46 -07007166 /* restore 5701 hardware bug workaround write method */
7167 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168
7169 /* Unfortunately, we have to delay before the PCI read back.
7170 * Some 575X chips even will not respond to a PCI cfg access
7171 * when the reset command is given to the chip.
7172 *
7173 * How do these hardware designers expect things to work
7174 * properly if the PCI write is posted for a long period
7175 * of time? It is always necessary to have some method by
7176 * which a register read back can occur to push the write
7177 * out which does the reset.
7178 *
7179 * For most tg3 variants the trick below was working.
7180 * Ho hum...
7181 */
7182 udelay(120);
7183
7184 /* Flush PCI posted writes. The normal MMIO registers
7185 * are inaccessible at this time so this is the only
7186 * way to make this reliably (actually, this is no longer
7187 * the case, see above). I tried to use indirect
7188 * register read/write but this upset some 5701 variants.
7189 */
7190 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7191
7192 udelay(120);
7193
Joe Perches63c3a662011-04-26 08:12:10 +00007194 if (tg3_flag(tp, PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00007195 u16 val16;
7196
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7198 int i;
7199 u32 cfg_val;
7200
7201 /* Wait for link training to complete. */
7202 for (i = 0; i < 5000; i++)
7203 udelay(100);
7204
7205 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7206 pci_write_config_dword(tp->pdev, 0xc4,
7207 cfg_val | (1 << 15));
7208 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007209
Matt Carlsone7126992009-08-25 10:08:16 +00007210 /* Clear the "no snoop" and "relaxed ordering" bits. */
7211 pci_read_config_word(tp->pdev,
7212 tp->pcie_cap + PCI_EXP_DEVCTL,
7213 &val16);
7214 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7215 PCI_EXP_DEVCTL_NOSNOOP_EN);
7216 /*
7217 * Older PCIe devices only support the 128 byte
7218 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007219 */
Joe Perches63c3a662011-04-26 08:12:10 +00007220 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007221 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007222 pci_write_config_word(tp->pdev,
7223 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007224 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007225
Matt Carlsoncf790032010-11-24 08:31:48 +00007226 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007227
7228 /* Clear error status */
7229 pci_write_config_word(tp->pdev,
7230 tp->pcie_cap + PCI_EXP_DEVSTA,
7231 PCI_EXP_DEVSTA_CED |
7232 PCI_EXP_DEVSTA_NFED |
7233 PCI_EXP_DEVSTA_FED |
7234 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235 }
7236
Michael Chanee6a99b2007-07-18 21:49:10 -07007237 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238
Joe Perches63c3a662011-04-26 08:12:10 +00007239 tg3_flag_clear(tp, CHIP_RESETTING);
7240 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007241
Michael Chanee6a99b2007-07-18 21:49:10 -07007242 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007243 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007244 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007245 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246
7247 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7248 tg3_stop_fw(tp);
7249 tw32(0x5000, 0x400);
7250 }
7251
7252 tw32(GRC_MODE, tp->grc_mode);
7253
7254 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007255 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256
7257 tw32(0xc4, val | (1 << 15));
7258 }
7259
7260 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7261 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7262 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7263 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7264 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7265 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7266 }
7267
Joe Perches63c3a662011-04-26 08:12:10 +00007268 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007269 tp->mac_mode = MAC_MODE_APE_TX_EN |
7270 MAC_MODE_APE_RX_EN |
7271 MAC_MODE_TDE_ENABLE;
7272
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007273 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007274 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
7275 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007276 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007277 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7278 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007280 val = 0;
7281
7282 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 udelay(40);
7284
Matt Carlson77b483f2008-08-15 14:07:24 -07007285 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7286
Michael Chan7a6f4362006-09-27 16:03:31 -07007287 err = tg3_poll_fw(tp);
7288 if (err)
7289 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290
Matt Carlson0a9140c2009-08-28 12:27:50 +00007291 tg3_mdio_start(tp);
7292
Joe Perches63c3a662011-04-26 08:12:10 +00007293 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007294 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7295 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007296 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007297 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007298
7299 tw32(0x7c00, val | (1 << 25));
7300 }
7301
Matt Carlsond78b59f2011-04-05 14:22:46 +00007302 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7303 val = tr32(TG3_CPMU_CLCK_ORIDE);
7304 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7305 }
7306
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007308 tg3_flag_clear(tp, ENABLE_ASF);
7309 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7311 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7312 u32 nic_cfg;
7313
7314 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7315 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007316 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007317 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007318 if (tg3_flag(tp, 5750_PLUS))
7319 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007320 }
7321 }
7322
7323 return 0;
7324}
7325
7326/* tp->lock is held. */
7327static void tg3_stop_fw(struct tg3 *tp)
7328{
Joe Perches63c3a662011-04-26 08:12:10 +00007329 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007330 /* Wait for RX cpu to ACK the previous event. */
7331 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332
7333 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007334
7335 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336
Matt Carlson7c5026a2008-05-02 16:49:29 -07007337 /* Wait for RX cpu to ACK this event. */
7338 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339 }
7340}
7341
7342/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007343static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344{
7345 int err;
7346
7347 tg3_stop_fw(tp);
7348
Michael Chan944d9802005-05-29 14:57:48 -07007349 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007351 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352 err = tg3_chip_reset(tp);
7353
Matt Carlsondaba2a62009-04-20 06:58:52 +00007354 __tg3_set_mac_addr(tp, 0);
7355
Michael Chan944d9802005-05-29 14:57:48 -07007356 tg3_write_sig_legacy(tp, kind);
7357 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358
7359 if (err)
7360 return err;
7361
7362 return 0;
7363}
7364
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365#define RX_CPU_SCRATCH_BASE 0x30000
7366#define RX_CPU_SCRATCH_SIZE 0x04000
7367#define TX_CPU_SCRATCH_BASE 0x34000
7368#define TX_CPU_SCRATCH_SIZE 0x04000
7369
7370/* tp->lock is held. */
7371static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7372{
7373 int i;
7374
Joe Perches63c3a662011-04-26 08:12:10 +00007375 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376
Michael Chanb5d37722006-09-27 16:06:21 -07007377 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7378 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7379
7380 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7381 return 0;
7382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383 if (offset == RX_CPU_BASE) {
7384 for (i = 0; i < 10000; i++) {
7385 tw32(offset + CPU_STATE, 0xffffffff);
7386 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7387 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7388 break;
7389 }
7390
7391 tw32(offset + CPU_STATE, 0xffffffff);
7392 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7393 udelay(10);
7394 } else {
7395 for (i = 0; i < 10000; i++) {
7396 tw32(offset + CPU_STATE, 0xffffffff);
7397 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7398 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7399 break;
7400 }
7401 }
7402
7403 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007404 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7405 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406 return -ENODEV;
7407 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007408
7409 /* Clear firmware's nvram arbitration. */
Joe Perches63c3a662011-04-26 08:12:10 +00007410 if (tg3_flag(tp, NVRAM))
Michael Chanec41c7d2006-01-17 02:40:55 -08007411 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412 return 0;
7413}
7414
7415struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007416 unsigned int fw_base;
7417 unsigned int fw_len;
7418 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419};
7420
7421/* tp->lock is held. */
7422static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7423 int cpu_scratch_size, struct fw_info *info)
7424{
Michael Chanec41c7d2006-01-17 02:40:55 -08007425 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426 void (*write_op)(struct tg3 *, u32, u32);
7427
Joe Perches63c3a662011-04-26 08:12:10 +00007428 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007429 netdev_err(tp->dev,
7430 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007431 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432 return -EINVAL;
7433 }
7434
Joe Perches63c3a662011-04-26 08:12:10 +00007435 if (tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436 write_op = tg3_write_mem;
7437 else
7438 write_op = tg3_write_indirect_reg32;
7439
Michael Chan1b628152005-05-29 14:59:49 -07007440 /* It is possible that bootcode is still loading at this point.
7441 * Get the nvram lock first before halting the cpu.
7442 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007443 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007445 if (!lock_err)
7446 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447 if (err)
7448 goto out;
7449
7450 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7451 write_op(tp, cpu_scratch_base + i, 0);
7452 tw32(cpu_base + CPU_STATE, 0xffffffff);
7453 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007454 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007456 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007458 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459
7460 err = 0;
7461
7462out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007463 return err;
7464}
7465
7466/* tp->lock is held. */
7467static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7468{
7469 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007470 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471 int err, i;
7472
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007473 fw_data = (void *)tp->fw->data;
7474
7475 /* Firmware blob starts with version numbers, followed by
7476 start address and length. We are setting complete length.
7477 length = end_address_of_bss - start_address_of_text.
7478 Remainder is the blob to be loaded contiguously
7479 from start address. */
7480
7481 info.fw_base = be32_to_cpu(fw_data[1]);
7482 info.fw_len = tp->fw->size - 12;
7483 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484
7485 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7486 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7487 &info);
7488 if (err)
7489 return err;
7490
7491 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7492 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7493 &info);
7494 if (err)
7495 return err;
7496
7497 /* Now startup only the RX cpu. */
7498 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007499 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500
7501 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007502 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503 break;
7504 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7505 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007506 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007507 udelay(1000);
7508 }
7509 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007510 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7511 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007512 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513 return -ENODEV;
7514 }
7515 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7516 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7517
7518 return 0;
7519}
7520
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521/* tp->lock is held. */
7522static int tg3_load_tso_firmware(struct tg3 *tp)
7523{
7524 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007525 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007526 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7527 int err, i;
7528
Joe Perches63c3a662011-04-26 08:12:10 +00007529 if (tg3_flag(tp, HW_TSO_1) ||
7530 tg3_flag(tp, HW_TSO_2) ||
7531 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532 return 0;
7533
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007534 fw_data = (void *)tp->fw->data;
7535
7536 /* Firmware blob starts with version numbers, followed by
7537 start address and length. We are setting complete length.
7538 length = end_address_of_bss - start_address_of_text.
7539 Remainder is the blob to be loaded contiguously
7540 from start address. */
7541
7542 info.fw_base = be32_to_cpu(fw_data[1]);
7543 cpu_scratch_size = tp->fw_len;
7544 info.fw_len = tp->fw->size - 12;
7545 info.fw_data = &fw_data[3];
7546
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548 cpu_base = RX_CPU_BASE;
7549 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551 cpu_base = TX_CPU_BASE;
7552 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7553 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7554 }
7555
7556 err = tg3_load_firmware_cpu(tp, cpu_base,
7557 cpu_scratch_base, cpu_scratch_size,
7558 &info);
7559 if (err)
7560 return err;
7561
7562 /* Now startup the cpu. */
7563 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007564 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565
7566 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007567 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568 break;
7569 tw32(cpu_base + CPU_STATE, 0xffffffff);
7570 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007571 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572 udelay(1000);
7573 }
7574 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007575 netdev_err(tp->dev,
7576 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007577 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578 return -ENODEV;
7579 }
7580 tw32(cpu_base + CPU_STATE, 0xffffffff);
7581 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7582 return 0;
7583}
7584
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586static int tg3_set_mac_addr(struct net_device *dev, void *p)
7587{
7588 struct tg3 *tp = netdev_priv(dev);
7589 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007590 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591
Michael Chanf9804dd2005-09-27 12:13:10 -07007592 if (!is_valid_ether_addr(addr->sa_data))
7593 return -EINVAL;
7594
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7596
Michael Chane75f7c92006-03-20 21:33:26 -08007597 if (!netif_running(dev))
7598 return 0;
7599
Joe Perches63c3a662011-04-26 08:12:10 +00007600 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007601 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007602
Michael Chan986e0ae2007-05-05 12:10:20 -07007603 addr0_high = tr32(MAC_ADDR_0_HIGH);
7604 addr0_low = tr32(MAC_ADDR_0_LOW);
7605 addr1_high = tr32(MAC_ADDR_1_HIGH);
7606 addr1_low = tr32(MAC_ADDR_1_LOW);
7607
7608 /* Skip MAC addr 1 if ASF is using it. */
7609 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7610 !(addr1_high == 0 && addr1_low == 0))
7611 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007612 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007613 spin_lock_bh(&tp->lock);
7614 __tg3_set_mac_addr(tp, skip_mac_1);
7615 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007616
Michael Chanb9ec6c12006-07-25 16:37:27 -07007617 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007618}
7619
7620/* tp->lock is held. */
7621static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7622 dma_addr_t mapping, u32 maxlen_flags,
7623 u32 nic_addr)
7624{
7625 tg3_write_mem(tp,
7626 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7627 ((u64) mapping >> 32));
7628 tg3_write_mem(tp,
7629 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7630 ((u64) mapping & 0xffffffff));
7631 tg3_write_mem(tp,
7632 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7633 maxlen_flags);
7634
Joe Perches63c3a662011-04-26 08:12:10 +00007635 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636 tg3_write_mem(tp,
7637 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7638 nic_addr);
7639}
7640
7641static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007642static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007643{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007644 int i;
7645
Joe Perches63c3a662011-04-26 08:12:10 +00007646 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007647 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7648 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7649 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007650 } else {
7651 tw32(HOSTCC_TXCOL_TICKS, 0);
7652 tw32(HOSTCC_TXMAX_FRAMES, 0);
7653 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007654 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007655
Joe Perches63c3a662011-04-26 08:12:10 +00007656 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007657 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7658 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7659 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7660 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007661 tw32(HOSTCC_RXCOL_TICKS, 0);
7662 tw32(HOSTCC_RXMAX_FRAMES, 0);
7663 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007664 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007665
Joe Perches63c3a662011-04-26 08:12:10 +00007666 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07007667 u32 val = ec->stats_block_coalesce_usecs;
7668
Matt Carlsonb6080e12009-09-01 13:12:00 +00007669 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7670 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7671
David S. Miller15f98502005-05-18 22:49:26 -07007672 if (!netif_carrier_ok(tp->dev))
7673 val = 0;
7674
7675 tw32(HOSTCC_STAT_COAL_TICKS, val);
7676 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007677
7678 for (i = 0; i < tp->irq_cnt - 1; i++) {
7679 u32 reg;
7680
7681 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7682 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007683 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7684 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007685 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7686 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007687
Joe Perches63c3a662011-04-26 08:12:10 +00007688 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007689 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7690 tw32(reg, ec->tx_coalesce_usecs);
7691 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7692 tw32(reg, ec->tx_max_coalesced_frames);
7693 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7694 tw32(reg, ec->tx_max_coalesced_frames_irq);
7695 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007696 }
7697
7698 for (; i < tp->irq_max - 1; i++) {
7699 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007700 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007701 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007702
Joe Perches63c3a662011-04-26 08:12:10 +00007703 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007704 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7705 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7706 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7707 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007708 }
David S. Miller15f98502005-05-18 22:49:26 -07007709}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007710
7711/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007712static void tg3_rings_reset(struct tg3 *tp)
7713{
7714 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007715 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007716 struct tg3_napi *tnapi = &tp->napi[0];
7717
7718 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007719 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007720 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00007721 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00007722 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007723 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7724 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007725 else
7726 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7727
7728 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7729 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7730 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7731 BDINFO_FLAGS_DISABLED);
7732
7733
7734 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007735 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007736 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00007737 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007738 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007739 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7740 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007741 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7742 else
7743 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7744
7745 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7746 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7747 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7748 BDINFO_FLAGS_DISABLED);
7749
7750 /* Disable interrupts */
7751 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7752
7753 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00007754 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007755 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007756 tp->napi[i].tx_prod = 0;
7757 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007758 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007759 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007760 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7761 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7762 }
Joe Perches63c3a662011-04-26 08:12:10 +00007763 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007764 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007765 } else {
7766 tp->napi[0].tx_prod = 0;
7767 tp->napi[0].tx_cons = 0;
7768 tw32_mailbox(tp->napi[0].prodmbox, 0);
7769 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7770 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007771
7772 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00007773 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00007774 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7775 for (i = 0; i < 16; i++)
7776 tw32_tx_mbox(mbox + i * 8, 0);
7777 }
7778
7779 txrcb = NIC_SRAM_SEND_RCB;
7780 rxrcb = NIC_SRAM_RCV_RET_RCB;
7781
7782 /* Clear status block in ram. */
7783 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7784
7785 /* Set status block DMA address */
7786 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7787 ((u64) tnapi->status_mapping >> 32));
7788 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7789 ((u64) tnapi->status_mapping & 0xffffffff));
7790
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007791 if (tnapi->tx_ring) {
7792 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7793 (TG3_TX_RING_SIZE <<
7794 BDINFO_FLAGS_MAXLEN_SHIFT),
7795 NIC_SRAM_TX_BUFFER_DESC);
7796 txrcb += TG3_BDINFO_SIZE;
7797 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007798
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007799 if (tnapi->rx_rcb) {
7800 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007801 (tp->rx_ret_ring_mask + 1) <<
7802 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007803 rxrcb += TG3_BDINFO_SIZE;
7804 }
7805
7806 stblk = HOSTCC_STATBLCK_RING1;
7807
7808 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7809 u64 mapping = (u64)tnapi->status_mapping;
7810 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7811 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7812
7813 /* Clear status block in ram. */
7814 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7815
Matt Carlson19cfaec2009-12-03 08:36:20 +00007816 if (tnapi->tx_ring) {
7817 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7818 (TG3_TX_RING_SIZE <<
7819 BDINFO_FLAGS_MAXLEN_SHIFT),
7820 NIC_SRAM_TX_BUFFER_DESC);
7821 txrcb += TG3_BDINFO_SIZE;
7822 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007823
7824 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007825 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007826 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7827
7828 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007829 rxrcb += TG3_BDINFO_SIZE;
7830 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007831}
7832
Matt Carlsoneb07a942011-04-20 07:57:36 +00007833static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
7834{
7835 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
7836
Joe Perches63c3a662011-04-26 08:12:10 +00007837 if (!tg3_flag(tp, 5750_PLUS) ||
7838 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00007839 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
7840 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7841 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
7842 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7843 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
7844 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
7845 else
7846 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
7847
7848 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
7849 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
7850
7851 val = min(nic_rep_thresh, host_rep_thresh);
7852 tw32(RCVBDI_STD_THRESH, val);
7853
Joe Perches63c3a662011-04-26 08:12:10 +00007854 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007855 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
7856
Joe Perches63c3a662011-04-26 08:12:10 +00007857 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007858 return;
7859
Joe Perches63c3a662011-04-26 08:12:10 +00007860 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007861 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
7862 else
7863 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
7864
7865 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
7866
7867 val = min(bdcache_maxcnt / 2, host_rep_thresh);
7868 tw32(RCVBDI_JUMBO_THRESH, val);
7869
Joe Perches63c3a662011-04-26 08:12:10 +00007870 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007871 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
7872}
7873
Matt Carlson2d31eca2009-09-01 12:53:31 +00007874/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007875static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007876{
7877 u32 val, rdmac_mode;
7878 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00007879 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007880
7881 tg3_disable_ints(tp);
7882
7883 tg3_stop_fw(tp);
7884
7885 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7886
Joe Perches63c3a662011-04-26 08:12:10 +00007887 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07007888 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889
Matt Carlson699c0192010-12-06 08:28:51 +00007890 /* Enable MAC control of LPI */
7891 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
7892 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
7893 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
7894 TG3_CPMU_EEE_LNKIDL_UART_IDL);
7895
7896 tw32_f(TG3_CPMU_EEE_CTRL,
7897 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
7898
Matt Carlsona386b902010-12-06 08:28:53 +00007899 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
7900 TG3_CPMU_EEEMD_LPI_IN_TX |
7901 TG3_CPMU_EEEMD_LPI_IN_RX |
7902 TG3_CPMU_EEEMD_EEE_ENABLE;
7903
7904 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
7905 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
7906
Joe Perches63c3a662011-04-26 08:12:10 +00007907 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00007908 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
7909
7910 tw32_f(TG3_CPMU_EEE_MODE, val);
7911
7912 tw32_f(TG3_CPMU_EEE_DBTMR1,
7913 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
7914 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
7915
7916 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00007917 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00007918 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00007919 }
7920
Matt Carlson603f1172010-02-12 14:47:10 +00007921 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007922 tg3_phy_reset(tp);
7923
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924 err = tg3_chip_reset(tp);
7925 if (err)
7926 return err;
7927
7928 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7929
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007930 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007931 val = tr32(TG3_CPMU_CTRL);
7932 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7933 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007934
7935 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7936 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7937 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7938 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7939
7940 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7941 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7942 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7943 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7944
7945 val = tr32(TG3_CPMU_HST_ACC);
7946 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7947 val |= CPMU_HST_ACC_MACCLK_6_25;
7948 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007949 }
7950
Matt Carlson33466d932009-04-20 06:57:41 +00007951 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7952 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7953 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7954 PCIE_PWR_MGMT_L1_THRESH_4MS;
7955 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007956
7957 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7958 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7959
7960 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d932009-04-20 06:57:41 +00007961
Matt Carlsonf40386c2009-11-02 14:24:02 +00007962 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7963 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007964 }
7965
Joe Perches63c3a662011-04-26 08:12:10 +00007966 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00007967 u32 grc_mode = tr32(GRC_MODE);
7968
7969 /* Access the lower 1K of PL PCIE block registers. */
7970 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7971 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7972
7973 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7974 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7975 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7976
7977 tw32(GRC_MODE, grc_mode);
7978 }
7979
Matt Carlson5093eed2010-11-24 08:31:45 +00007980 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
7981 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7982 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00007983
Matt Carlson5093eed2010-11-24 08:31:45 +00007984 /* Access the lower 1K of PL PCIE block registers. */
7985 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7986 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00007987
Matt Carlson5093eed2010-11-24 08:31:45 +00007988 val = tr32(TG3_PCIE_TLDLPL_PORT +
7989 TG3_PCIE_PL_LO_PHYCTL5);
7990 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
7991 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00007992
Matt Carlson5093eed2010-11-24 08:31:45 +00007993 tw32(GRC_MODE, grc_mode);
7994 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00007995
Matt Carlson1ff30a52011-05-19 12:12:46 +00007996 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
7997 u32 grc_mode = tr32(GRC_MODE);
7998
7999 /* Access the lower 1K of DL PCIE block registers. */
8000 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8001 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8002
8003 val = tr32(TG3_PCIE_TLDLPL_PORT +
8004 TG3_PCIE_DL_LO_FTSMAX);
8005 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8006 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8007 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8008
8009 tw32(GRC_MODE, grc_mode);
8010 }
8011
Matt Carlsona977dbe2010-04-12 06:58:26 +00008012 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8013 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8014 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8015 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008016 }
8017
Linus Torvalds1da177e2005-04-16 15:20:36 -07008018 /* This works around an issue with Athlon chipsets on
8019 * B3 tigon3 silicon. This bit has no effect on any
8020 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008021 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008022 */
Joe Perches63c3a662011-04-26 08:12:10 +00008023 if (!tg3_flag(tp, CPMU_PRESENT)) {
8024 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008025 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8026 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008028
8029 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008030 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008031 val = tr32(TG3PCI_PCISTATE);
8032 val |= PCISTATE_RETRY_SAME_DMA;
8033 tw32(TG3PCI_PCISTATE, val);
8034 }
8035
Joe Perches63c3a662011-04-26 08:12:10 +00008036 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008037 /* Allow reads and writes to the
8038 * APE register and memory space.
8039 */
8040 val = tr32(TG3PCI_PCISTATE);
8041 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008042 PCISTATE_ALLOW_APE_SHMEM_WR |
8043 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008044 tw32(TG3PCI_PCISTATE, val);
8045 }
8046
Linus Torvalds1da177e2005-04-16 15:20:36 -07008047 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8048 /* Enable some hw fixes. */
8049 val = tr32(TG3PCI_MSI_DATA);
8050 val |= (1 << 26) | (1 << 28) | (1 << 29);
8051 tw32(TG3PCI_MSI_DATA, val);
8052 }
8053
8054 /* Descriptor ring init may make accesses to the
8055 * NIC SRAM area to setup the TX descriptors, so we
8056 * can only do this after the hardware has been
8057 * successfully reset.
8058 */
Michael Chan32d8c572006-07-25 16:38:29 -07008059 err = tg3_init_rings(tp);
8060 if (err)
8061 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008062
Joe Perches63c3a662011-04-26 08:12:10 +00008063 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008064 val = tr32(TG3PCI_DMA_RW_CTRL) &
8065 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008066 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8067 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008068 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8069 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8070 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008071 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8072 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8073 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008074 /* This value is determined during the probe time DMA
8075 * engine test, tg3_test_dma.
8076 */
8077 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008079
8080 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8081 GRC_MODE_4X_NIC_SEND_RINGS |
8082 GRC_MODE_NO_TX_PHDR_CSUM |
8083 GRC_MODE_NO_RX_PHDR_CSUM);
8084 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008085
8086 /* Pseudo-header checksum is done by hardware logic and not
8087 * the offload processers, so make the chip do the pseudo-
8088 * header checksums on receive. For transmit it is more
8089 * convenient to do the pseudo-header checksum in software
8090 * as Linux does that on transmit for us in all cases.
8091 */
8092 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008093
8094 tw32(GRC_MODE,
8095 tp->grc_mode |
8096 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8097
8098 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8099 val = tr32(GRC_MISC_CFG);
8100 val &= ~0xff;
8101 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8102 tw32(GRC_MISC_CFG, val);
8103
8104 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008105 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008106 /* Do nothing. */
8107 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8108 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8109 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8110 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8111 else
8112 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8113 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8114 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008115 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008116 int fw_len;
8117
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008118 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008119 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8120 tw32(BUFMGR_MB_POOL_ADDR,
8121 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8122 tw32(BUFMGR_MB_POOL_SIZE,
8123 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008125
Michael Chan0f893dc2005-07-25 12:30:38 -07008126 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008127 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8128 tp->bufmgr_config.mbuf_read_dma_low_water);
8129 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8130 tp->bufmgr_config.mbuf_mac_rx_low_water);
8131 tw32(BUFMGR_MB_HIGH_WATER,
8132 tp->bufmgr_config.mbuf_high_water);
8133 } else {
8134 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8135 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8136 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8137 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8138 tw32(BUFMGR_MB_HIGH_WATER,
8139 tp->bufmgr_config.mbuf_high_water_jumbo);
8140 }
8141 tw32(BUFMGR_DMA_LOW_WATER,
8142 tp->bufmgr_config.dma_low_water);
8143 tw32(BUFMGR_DMA_HIGH_WATER,
8144 tp->bufmgr_config.dma_high_water);
8145
Matt Carlsond309a462010-09-30 10:34:31 +00008146 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8147 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8148 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008149 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8150 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8151 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8152 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008153 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008154 for (i = 0; i < 2000; i++) {
8155 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8156 break;
8157 udelay(10);
8158 }
8159 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008160 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161 return -ENODEV;
8162 }
8163
Matt Carlsoneb07a942011-04-20 07:57:36 +00008164 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8165 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008166
Matt Carlsoneb07a942011-04-20 07:57:36 +00008167 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008168
8169 /* Initialize TG3_BDINFO's at:
8170 * RCVDBDI_STD_BD: standard eth size rx ring
8171 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8172 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8173 *
8174 * like so:
8175 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8176 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8177 * ring attribute flags
8178 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8179 *
8180 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8181 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8182 *
8183 * The size of each ring is fixed in the firmware, but the location is
8184 * configurable.
8185 */
8186 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008187 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008188 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008189 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008190 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008191 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8192 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008193
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008194 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008195 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008196 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8197 BDINFO_FLAGS_DISABLED);
8198
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008199 /* Program the jumbo buffer descriptor ring control
8200 * blocks on those devices that have them.
8201 */
Matt Carlsonbb18bb92011-03-09 16:58:19 +00008202 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008203 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008204
Joe Perches63c3a662011-04-26 08:12:10 +00008205 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008206 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008207 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008208 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008209 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008210 val = TG3_RX_JMB_RING_SIZE(tp) <<
8211 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008212 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008213 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008214 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008215 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008216 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8217 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008218 } else {
8219 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8220 BDINFO_FLAGS_DISABLED);
8221 }
8222
Joe Perches63c3a662011-04-26 08:12:10 +00008223 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008224 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008225 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008226 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008227 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008228 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8229 val |= (TG3_RX_STD_DMA_SZ << 2);
8230 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008231 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008232 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008233 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008234
8235 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008236
Matt Carlson411da642009-11-13 13:03:46 +00008237 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e662009-11-13 13:03:49 +00008238 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008239
Joe Perches63c3a662011-04-26 08:12:10 +00008240 tpr->rx_jmb_prod_idx =
8241 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e662009-11-13 13:03:49 +00008242 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008243
Matt Carlson2d31eca2009-09-01 12:53:31 +00008244 tg3_rings_reset(tp);
8245
Linus Torvalds1da177e2005-04-16 15:20:36 -07008246 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008247 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248
8249 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008250 tw32(MAC_RX_MTU_SIZE,
8251 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008252
8253 /* The slot time is changed by tg3_setup_phy if we
8254 * run at gigabit with half duplex.
8255 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008256 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8257 (6 << TX_LENGTHS_IPG_SHIFT) |
8258 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8259
8260 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8261 val |= tr32(MAC_TX_LENGTHS) &
8262 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8263 TX_LENGTHS_CNT_DWN_VAL_MSK);
8264
8265 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008266
8267 /* Receive rules. */
8268 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8269 tw32(RCVLPC_CONFIG, 0x0181);
8270
8271 /* Calculate RDMAC_MODE setting early, we need it to determine
8272 * the RCVLPC_STATE_ENABLE mask.
8273 */
8274 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8275 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8276 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8277 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8278 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008279
Matt Carlsondeabaac2010-11-24 08:31:50 +00008280 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008281 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8282
Matt Carlson57e69832008-05-25 23:48:31 -07008283 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008284 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8285 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008286 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8287 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8288 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8289
Matt Carlsonc5908932011-03-09 16:58:25 +00008290 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8291 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008292 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008293 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008294 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8295 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008296 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008297 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8298 }
8299 }
8300
Joe Perches63c3a662011-04-26 08:12:10 +00008301 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008302 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8303
Joe Perches63c3a662011-04-26 08:12:10 +00008304 if (tg3_flag(tp, HW_TSO_1) ||
8305 tg3_flag(tp, HW_TSO_2) ||
8306 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008307 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8308
Matt Carlson108a6c12011-05-19 12:12:47 +00008309 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008310 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008311 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8312 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008313
Matt Carlsonf2096f92011-04-05 14:22:48 +00008314 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8315 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8316
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008317 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8318 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8319 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8320 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008321 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008322 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008323 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8324 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008325 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8326 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8327 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8328 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8329 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8330 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008331 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008332 tw32(TG3_RDMA_RSRVCTRL_REG,
8333 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8334 }
8335
Matt Carlsond78b59f2011-04-05 14:22:46 +00008336 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8337 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008338 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8339 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8340 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8341 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8342 }
8343
Linus Torvalds1da177e2005-04-16 15:20:36 -07008344 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008345 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008346 val = tr32(RCVLPC_STATS_ENABLE);
8347 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8348 tw32(RCVLPC_STATS_ENABLE, val);
8349 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008350 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008351 val = tr32(RCVLPC_STATS_ENABLE);
8352 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8353 tw32(RCVLPC_STATS_ENABLE, val);
8354 } else {
8355 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8356 }
8357 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8358 tw32(SNDDATAI_STATSENAB, 0xffffff);
8359 tw32(SNDDATAI_STATSCTRL,
8360 (SNDDATAI_SCTRL_ENABLE |
8361 SNDDATAI_SCTRL_FASTUPD));
8362
8363 /* Setup host coalescing engine. */
8364 tw32(HOSTCC_MODE, 0);
8365 for (i = 0; i < 2000; i++) {
8366 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8367 break;
8368 udelay(10);
8369 }
8370
Michael Chand244c892005-07-05 14:42:33 -07008371 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008372
Joe Perches63c3a662011-04-26 08:12:10 +00008373 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008374 /* Status/statistics block address. See tg3_timer,
8375 * the tg3_periodic_fetch_stats call there, and
8376 * tg3_get_stats to see how this works for 5705/5750 chips.
8377 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008378 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8379 ((u64) tp->stats_mapping >> 32));
8380 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8381 ((u64) tp->stats_mapping & 0xffffffff));
8382 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008383
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008385
8386 /* Clear statistics and status block memory areas */
8387 for (i = NIC_SRAM_STATS_BLK;
8388 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8389 i += sizeof(u32)) {
8390 tg3_write_mem(tp, i, 0);
8391 udelay(40);
8392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008393 }
8394
8395 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8396
8397 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8398 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008399 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008400 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8401
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008402 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8403 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008404 /* reset to prevent losing 1st rx packet intermittently */
8405 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8406 udelay(10);
8407 }
8408
Joe Perches63c3a662011-04-26 08:12:10 +00008409 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008410 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -07008411 else
8412 tp->mac_mode = 0;
8413 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008415 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008416 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008417 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8418 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008419 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8420 udelay(40);
8421
Michael Chan314fba32005-04-21 17:07:04 -07008422 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008423 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008424 * register to preserve the GPIO settings for LOMs. The GPIOs,
8425 * whether used as inputs or outputs, are set by boot code after
8426 * reset.
8427 */
Joe Perches63c3a662011-04-26 08:12:10 +00008428 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008429 u32 gpio_mask;
8430
Michael Chan9d26e212006-12-07 00:21:14 -08008431 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8432 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8433 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008434
8435 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8436 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8437 GRC_LCLCTRL_GPIO_OUTPUT3;
8438
Michael Chanaf36e6b2006-03-23 01:28:06 -08008439 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8440 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8441
Gary Zambranoaaf84462007-05-05 11:51:45 -07008442 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008443 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8444
8445 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008446 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008447 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8448 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008450 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8451 udelay(100);
8452
Joe Perches63c3a662011-04-26 08:12:10 +00008453 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008454 val = tr32(MSGINT_MODE);
8455 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8456 tw32(MSGINT_MODE, val);
8457 }
8458
Joe Perches63c3a662011-04-26 08:12:10 +00008459 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008460 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8461 udelay(40);
8462 }
8463
8464 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8465 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8466 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8467 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8468 WDMAC_MODE_LNGREAD_ENAB);
8469
Matt Carlsonc5908932011-03-09 16:58:25 +00008470 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8471 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008472 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008473 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8474 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8475 /* nothing */
8476 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008477 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008478 val |= WDMAC_MODE_RX_ACCEL;
8479 }
8480 }
8481
Michael Chand9ab5ad12006-03-20 22:27:35 -08008482 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008483 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008484 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08008485
Matt Carlson788a0352009-11-02 14:26:03 +00008486 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8487 val |= WDMAC_MODE_BURST_ALL_DATA;
8488
Linus Torvalds1da177e2005-04-16 15:20:36 -07008489 tw32_f(WDMAC_MODE, val);
8490 udelay(40);
8491
Joe Perches63c3a662011-04-26 08:12:10 +00008492 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008493 u16 pcix_cmd;
8494
8495 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8496 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008497 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008498 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8499 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008500 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008501 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8502 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008503 }
Matt Carlson9974a352007-10-07 23:27:28 -07008504 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8505 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008506 }
8507
8508 tw32_f(RDMAC_MODE, rdmac_mode);
8509 udelay(40);
8510
8511 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008512 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008513 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008514
8515 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8516 tw32(SNDDATAC_MODE,
8517 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8518 else
8519 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8520
Linus Torvalds1da177e2005-04-16 15:20:36 -07008521 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8522 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008523 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008524 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008525 val |= RCVDBDI_MODE_LRG_RING_SZ;
8526 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008527 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008528 if (tg3_flag(tp, HW_TSO_1) ||
8529 tg3_flag(tp, HW_TSO_2) ||
8530 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008531 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008532 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008533 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008534 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8535 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008536 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8537
8538 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8539 err = tg3_load_5701_a0_firmware_fix(tp);
8540 if (err)
8541 return err;
8542 }
8543
Joe Perches63c3a662011-04-26 08:12:10 +00008544 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008545 err = tg3_load_tso_firmware(tp);
8546 if (err)
8547 return err;
8548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008549
8550 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008551
Joe Perches63c3a662011-04-26 08:12:10 +00008552 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008553 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8554 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008555
8556 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8557 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8558 tp->tx_mode &= ~val;
8559 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8560 }
8561
Linus Torvalds1da177e2005-04-16 15:20:36 -07008562 tw32_f(MAC_TX_MODE, tp->tx_mode);
8563 udelay(100);
8564
Joe Perches63c3a662011-04-26 08:12:10 +00008565 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008566 u32 reg = MAC_RSS_INDIR_TBL_0;
8567 u8 *ent = (u8 *)&val;
8568
8569 /* Setup the indirection table */
8570 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8571 int idx = i % sizeof(val);
8572
Matt Carlson5efeeea2010-07-11 09:31:40 +00008573 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008574 if (idx == sizeof(val) - 1) {
8575 tw32(reg, val);
8576 reg += 4;
8577 }
8578 }
8579
8580 /* Setup the "secret" hash key. */
8581 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8582 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8583 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8584 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8585 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8586 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8587 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8588 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8589 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8590 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8591 }
8592
Linus Torvalds1da177e2005-04-16 15:20:36 -07008593 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008594 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008595 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8596
Joe Perches63c3a662011-04-26 08:12:10 +00008597 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008598 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8599 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8600 RX_MODE_RSS_IPV6_HASH_EN |
8601 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8602 RX_MODE_RSS_IPV4_HASH_EN |
8603 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8604
Linus Torvalds1da177e2005-04-16 15:20:36 -07008605 tw32_f(MAC_RX_MODE, tp->rx_mode);
8606 udelay(10);
8607
Linus Torvalds1da177e2005-04-16 15:20:36 -07008608 tw32(MAC_LED_CTRL, tp->led_ctrl);
8609
8610 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008611 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008612 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8613 udelay(10);
8614 }
8615 tw32_f(MAC_RX_MODE, tp->rx_mode);
8616 udelay(10);
8617
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008618 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008619 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008620 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008621 /* Set drive transmission level to 1.2V */
8622 /* only if the signal pre-emphasis bit is not set */
8623 val = tr32(MAC_SERDES_CFG);
8624 val &= 0xfffff000;
8625 val |= 0x880;
8626 tw32(MAC_SERDES_CFG, val);
8627 }
8628 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8629 tw32(MAC_SERDES_CFG, 0x616000);
8630 }
8631
8632 /* Prevent chip from dropping frames when flow control
8633 * is enabled.
8634 */
Matt Carlson666bc832010-01-20 16:58:03 +00008635 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8636 val = 1;
8637 else
8638 val = 2;
8639 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640
8641 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008642 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008643 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00008644 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008645 }
8646
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008647 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Michael Chand4d2c552006-03-20 17:47:20 -08008648 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8649 u32 tmp;
8650
8651 tmp = tr32(SERDES_RX_CTRL);
8652 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8653 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8654 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8655 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8656 }
8657
Joe Perches63c3a662011-04-26 08:12:10 +00008658 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00008659 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8660 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008661 tp->link_config.speed = tp->link_config.orig_speed;
8662 tp->link_config.duplex = tp->link_config.orig_duplex;
8663 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008665
Matt Carlsondd477002008-05-25 23:45:58 -07008666 err = tg3_setup_phy(tp, 0);
8667 if (err)
8668 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008669
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008670 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8671 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008672 u32 tmp;
8673
8674 /* Clear CRC stats. */
8675 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8676 tg3_writephy(tp, MII_TG3_TEST1,
8677 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008678 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008680 }
8681 }
8682
8683 __tg3_set_rx_mode(tp->dev);
8684
8685 /* Initialize receive rules. */
8686 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8687 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8688 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8689 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8690
Joe Perches63c3a662011-04-26 08:12:10 +00008691 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008692 limit = 8;
8693 else
8694 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008695 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008696 limit -= 4;
8697 switch (limit) {
8698 case 16:
8699 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8700 case 15:
8701 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8702 case 14:
8703 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8704 case 13:
8705 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8706 case 12:
8707 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8708 case 11:
8709 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8710 case 10:
8711 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8712 case 9:
8713 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8714 case 8:
8715 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8716 case 7:
8717 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8718 case 6:
8719 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8720 case 5:
8721 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8722 case 4:
8723 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8724 case 3:
8725 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8726 case 2:
8727 case 1:
8728
8729 default:
8730 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008732
Joe Perches63c3a662011-04-26 08:12:10 +00008733 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07008734 /* Write our heartbeat update interval to APE. */
8735 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8736 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008737
Linus Torvalds1da177e2005-04-16 15:20:36 -07008738 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8739
Linus Torvalds1da177e2005-04-16 15:20:36 -07008740 return 0;
8741}
8742
8743/* Called at device open time to get the chip ready for
8744 * packet processing. Invoked with tp->lock held.
8745 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008746static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008747{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008748 tg3_switch_clocks(tp);
8749
8750 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8751
Matt Carlson2f751b62008-08-04 23:17:34 -07008752 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008753}
8754
8755#define TG3_STAT_ADD32(PSTAT, REG) \
8756do { u32 __val = tr32(REG); \
8757 (PSTAT)->low += __val; \
8758 if ((PSTAT)->low < __val) \
8759 (PSTAT)->high += 1; \
8760} while (0)
8761
8762static void tg3_periodic_fetch_stats(struct tg3 *tp)
8763{
8764 struct tg3_hw_stats *sp = tp->hw_stats;
8765
8766 if (!netif_carrier_ok(tp->dev))
8767 return;
8768
8769 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8770 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8771 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8772 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8773 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8774 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8775 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8776 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8777 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8778 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8779 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8780 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8781 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8782
8783 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8784 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8785 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8786 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8787 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8788 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8789 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8790 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8791 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8792 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8793 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8794 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8795 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8796 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008797
8798 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson4d958472011-04-20 07:57:35 +00008799 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
8800 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8801 } else {
8802 u32 val = tr32(HOSTCC_FLOW_ATTN);
8803 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
8804 if (val) {
8805 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
8806 sp->rx_discards.low += val;
8807 if (sp->rx_discards.low < val)
8808 sp->rx_discards.high += 1;
8809 }
8810 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
8811 }
Michael Chan463d3052006-05-22 16:36:27 -07008812 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008813}
8814
8815static void tg3_timer(unsigned long __opaque)
8816{
8817 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008818
Michael Chanf475f162006-03-27 23:20:14 -08008819 if (tp->irq_sync)
8820 goto restart_timer;
8821
David S. Millerf47c11e2005-06-24 20:18:35 -07008822 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008823
Joe Perches63c3a662011-04-26 08:12:10 +00008824 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07008825 /* All of this garbage is because when using non-tagged
8826 * IRQ status the mailbox/status_block protocol the chip
8827 * uses with the cpu is race prone.
8828 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008829 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008830 tw32(GRC_LOCAL_CTRL,
8831 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8832 } else {
8833 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008834 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008836
David S. Millerfac9b832005-05-18 22:46:34 -07008837 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +00008838 tg3_flag_set(tp, RESTART_TIMER);
David S. Millerf47c11e2005-06-24 20:18:35 -07008839 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008840 schedule_work(&tp->reset_task);
8841 return;
8842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008843 }
8844
Linus Torvalds1da177e2005-04-16 15:20:36 -07008845 /* This part only runs once per second. */
8846 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00008847 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07008848 tg3_periodic_fetch_stats(tp);
8849
Matt Carlsonb0c59432011-05-19 12:12:48 +00008850 if (tp->setlpicnt && !--tp->setlpicnt)
8851 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00008852
Joe Perches63c3a662011-04-26 08:12:10 +00008853 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008854 u32 mac_stat;
8855 int phy_event;
8856
8857 mac_stat = tr32(MAC_STATUS);
8858
8859 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008860 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008861 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8862 phy_event = 1;
8863 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8864 phy_event = 1;
8865
8866 if (phy_event)
8867 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00008868 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008869 u32 mac_stat = tr32(MAC_STATUS);
8870 int need_setup = 0;
8871
8872 if (netif_carrier_ok(tp->dev) &&
8873 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8874 need_setup = 1;
8875 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008876 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008877 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8878 MAC_STATUS_SIGNAL_DET))) {
8879 need_setup = 1;
8880 }
8881 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008882 if (!tp->serdes_counter) {
8883 tw32_f(MAC_MODE,
8884 (tp->mac_mode &
8885 ~MAC_MODE_PORT_MODE_MASK));
8886 udelay(40);
8887 tw32_f(MAC_MODE, tp->mac_mode);
8888 udelay(40);
8889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008890 tg3_setup_phy(tp, 0);
8891 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008892 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008893 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008894 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008896
8897 tp->timer_counter = tp->timer_multiplier;
8898 }
8899
Michael Chan130b8e42006-09-27 16:00:40 -07008900 /* Heartbeat is only sent once every 2 seconds.
8901 *
8902 * The heartbeat is to tell the ASF firmware that the host
8903 * driver is still alive. In the event that the OS crashes,
8904 * ASF needs to reset the hardware to free up the FIFO space
8905 * that may be filled with rx packets destined for the host.
8906 * If the FIFO is full, ASF will no longer function properly.
8907 *
8908 * Unintended resets have been reported on real time kernels
8909 * where the timer doesn't run on time. Netpoll will also have
8910 * same problem.
8911 *
8912 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8913 * to check the ring condition when the heartbeat is expiring
8914 * before doing the reset. This will prevent most unintended
8915 * resets.
8916 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008917 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00008918 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008919 tg3_wait_for_event_ack(tp);
8920
Michael Chanbbadf502006-04-06 21:46:34 -07008921 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008922 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008923 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008924 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8925 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008926
8927 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008928 }
8929 tp->asf_counter = tp->asf_multiplier;
8930 }
8931
David S. Millerf47c11e2005-06-24 20:18:35 -07008932 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008933
Michael Chanf475f162006-03-27 23:20:14 -08008934restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008935 tp->timer.expires = jiffies + tp->timer_offset;
8936 add_timer(&tp->timer);
8937}
8938
Matt Carlson4f125f42009-09-01 12:55:02 +00008939static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008940{
David Howells7d12e782006-10-05 14:55:46 +01008941 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008942 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008943 char *name;
8944 struct tg3_napi *tnapi = &tp->napi[irq_num];
8945
8946 if (tp->irq_cnt == 1)
8947 name = tp->dev->name;
8948 else {
8949 name = &tnapi->irq_lbl[0];
8950 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8951 name[IFNAMSIZ-1] = 0;
8952 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008953
Joe Perches63c3a662011-04-26 08:12:10 +00008954 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008955 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00008956 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08008957 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00008958 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008959 } else {
8960 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00008961 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08008962 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00008963 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008964 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008965
8966 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008967}
8968
Michael Chan79381092005-04-21 17:13:59 -07008969static int tg3_test_interrupt(struct tg3 *tp)
8970{
Matt Carlson09943a12009-08-28 14:01:57 +00008971 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008972 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008973 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008974 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008975
Michael Chand4bc3922005-05-29 14:59:20 -07008976 if (!netif_running(dev))
8977 return -ENODEV;
8978
Michael Chan79381092005-04-21 17:13:59 -07008979 tg3_disable_ints(tp);
8980
Matt Carlson4f125f42009-09-01 12:55:02 +00008981 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008982
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008983 /*
8984 * Turn off MSI one shot mode. Otherwise this test has no
8985 * observable way to know whether the interrupt was delivered.
8986 */
Joe Perches63c3a662011-04-26 08:12:10 +00008987 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008988 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8989 tw32(MSGINT_MODE, val);
8990 }
8991
Matt Carlson4f125f42009-09-01 12:55:02 +00008992 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008993 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008994 if (err)
8995 return err;
8996
Matt Carlson898a56f2009-08-28 14:02:40 +00008997 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008998 tg3_enable_ints(tp);
8999
9000 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009001 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009002
9003 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009004 u32 int_mbox, misc_host_ctrl;
9005
Matt Carlson898a56f2009-08-28 14:02:40 +00009006 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009007 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9008
9009 if ((int_mbox != 0) ||
9010 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9011 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009012 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009013 }
9014
Michael Chan79381092005-04-21 17:13:59 -07009015 msleep(10);
9016 }
9017
9018 tg3_disable_ints(tp);
9019
Matt Carlson4f125f42009-09-01 12:55:02 +00009020 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009021
Matt Carlson4f125f42009-09-01 12:55:02 +00009022 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009023
9024 if (err)
9025 return err;
9026
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009027 if (intr_ok) {
9028 /* Reenable MSI one shot mode. */
Joe Perches63c3a662011-04-26 08:12:10 +00009029 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009030 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9031 tw32(MSGINT_MODE, val);
9032 }
Michael Chan79381092005-04-21 17:13:59 -07009033 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009034 }
Michael Chan79381092005-04-21 17:13:59 -07009035
9036 return -EIO;
9037}
9038
9039/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9040 * successfully restored
9041 */
9042static int tg3_test_msi(struct tg3 *tp)
9043{
Michael Chan79381092005-04-21 17:13:59 -07009044 int err;
9045 u16 pci_cmd;
9046
Joe Perches63c3a662011-04-26 08:12:10 +00009047 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009048 return 0;
9049
9050 /* Turn off SERR reporting in case MSI terminates with Master
9051 * Abort.
9052 */
9053 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9054 pci_write_config_word(tp->pdev, PCI_COMMAND,
9055 pci_cmd & ~PCI_COMMAND_SERR);
9056
9057 err = tg3_test_interrupt(tp);
9058
9059 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9060
9061 if (!err)
9062 return 0;
9063
9064 /* other failures */
9065 if (err != -EIO)
9066 return err;
9067
9068 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009069 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9070 "to INTx mode. Please report this failure to the PCI "
9071 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009072
Matt Carlson4f125f42009-09-01 12:55:02 +00009073 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009074
Michael Chan79381092005-04-21 17:13:59 -07009075 pci_disable_msi(tp->pdev);
9076
Joe Perches63c3a662011-04-26 08:12:10 +00009077 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009078 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009079
Matt Carlson4f125f42009-09-01 12:55:02 +00009080 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009081 if (err)
9082 return err;
9083
9084 /* Need to reset the chip because the MSI cycle may have terminated
9085 * with Master Abort.
9086 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009087 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009088
Michael Chan944d9802005-05-29 14:57:48 -07009089 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009090 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009091
David S. Millerf47c11e2005-06-24 20:18:35 -07009092 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009093
9094 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009095 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009096
9097 return err;
9098}
9099
Matt Carlson9e9fd122009-01-19 16:57:45 -08009100static int tg3_request_firmware(struct tg3 *tp)
9101{
9102 const __be32 *fw_data;
9103
9104 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009105 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9106 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009107 return -ENOENT;
9108 }
9109
9110 fw_data = (void *)tp->fw->data;
9111
9112 /* Firmware blob starts with version numbers, followed by
9113 * start address and _full_ length including BSS sections
9114 * (which must be longer than the actual data, of course
9115 */
9116
9117 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9118 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009119 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9120 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009121 release_firmware(tp->fw);
9122 tp->fw = NULL;
9123 return -EINVAL;
9124 }
9125
9126 /* We no longer need firmware; we have it. */
9127 tp->fw_needed = NULL;
9128 return 0;
9129}
9130
Matt Carlson679563f2009-09-01 12:55:46 +00009131static bool tg3_enable_msix(struct tg3 *tp)
9132{
9133 int i, rc, cpus = num_online_cpus();
9134 struct msix_entry msix_ent[tp->irq_max];
9135
9136 if (cpus == 1)
9137 /* Just fallback to the simpler MSI mode. */
9138 return false;
9139
9140 /*
9141 * We want as many rx rings enabled as there are cpus.
9142 * The first MSIX vector only deals with link interrupts, etc,
9143 * so we add one to the number of vectors we are requesting.
9144 */
9145 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9146
9147 for (i = 0; i < tp->irq_max; i++) {
9148 msix_ent[i].entry = i;
9149 msix_ent[i].vector = 0;
9150 }
9151
9152 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009153 if (rc < 0) {
9154 return false;
9155 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009156 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9157 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009158 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9159 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009160 tp->irq_cnt = rc;
9161 }
9162
9163 for (i = 0; i < tp->irq_max; i++)
9164 tp->napi[i].irq_vec = msix_ent[i].vector;
9165
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009166 netif_set_real_num_tx_queues(tp->dev, 1);
9167 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9168 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9169 pci_disable_msix(tp->pdev);
9170 return false;
9171 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009172
9173 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009174 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009175
9176 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9177 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009178 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009179 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9180 }
9181 }
Matt Carlson2430b032010-06-05 17:24:34 +00009182
Matt Carlson679563f2009-09-01 12:55:46 +00009183 return true;
9184}
9185
Matt Carlson07b01732009-08-28 14:01:15 +00009186static void tg3_ints_init(struct tg3 *tp)
9187{
Joe Perches63c3a662011-04-26 08:12:10 +00009188 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9189 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009190 /* All MSI supporting chips should support tagged
9191 * status. Assert that this is the case.
9192 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009193 netdev_warn(tp->dev,
9194 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009195 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009196 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009197
Joe Perches63c3a662011-04-26 08:12:10 +00009198 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9199 tg3_flag_set(tp, USING_MSIX);
9200 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9201 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009202
Joe Perches63c3a662011-04-26 08:12:10 +00009203 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009204 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009205 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009206 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009207 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9208 }
9209defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009210 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009211 tp->irq_cnt = 1;
9212 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009213 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009214 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009215 }
Matt Carlson07b01732009-08-28 14:01:15 +00009216}
9217
9218static void tg3_ints_fini(struct tg3 *tp)
9219{
Joe Perches63c3a662011-04-26 08:12:10 +00009220 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009221 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009222 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009223 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009224 tg3_flag_clear(tp, USING_MSI);
9225 tg3_flag_clear(tp, USING_MSIX);
9226 tg3_flag_clear(tp, ENABLE_RSS);
9227 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009228}
9229
Linus Torvalds1da177e2005-04-16 15:20:36 -07009230static int tg3_open(struct net_device *dev)
9231{
9232 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009233 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009234
Matt Carlson9e9fd122009-01-19 16:57:45 -08009235 if (tp->fw_needed) {
9236 err = tg3_request_firmware(tp);
9237 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9238 if (err)
9239 return err;
9240 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009241 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009242 tg3_flag_clear(tp, TSO_CAPABLE);
9243 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009244 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009245 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009246 }
9247 }
9248
Michael Chanc49a1562006-12-17 17:07:29 -08009249 netif_carrier_off(tp->dev);
9250
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009251 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009252 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009253 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009254
9255 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009256
Linus Torvalds1da177e2005-04-16 15:20:36 -07009257 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009258 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009259
David S. Millerf47c11e2005-06-24 20:18:35 -07009260 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009261
Matt Carlson679563f2009-09-01 12:55:46 +00009262 /*
9263 * Setup interrupts first so we know how
9264 * many NAPI resources to allocate
9265 */
9266 tg3_ints_init(tp);
9267
Linus Torvalds1da177e2005-04-16 15:20:36 -07009268 /* The placement of this call is tied
9269 * to the setup and use of Host TX descriptors.
9270 */
9271 err = tg3_alloc_consistent(tp);
9272 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009273 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009274
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009275 tg3_napi_init(tp);
9276
Matt Carlsonfed97812009-09-01 13:10:19 +00009277 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009278
Matt Carlson4f125f42009-09-01 12:55:02 +00009279 for (i = 0; i < tp->irq_cnt; i++) {
9280 struct tg3_napi *tnapi = &tp->napi[i];
9281 err = tg3_request_irq(tp, i);
9282 if (err) {
9283 for (i--; i >= 0; i--)
9284 free_irq(tnapi->irq_vec, tnapi);
9285 break;
9286 }
9287 }
Matt Carlson07b01732009-08-28 14:01:15 +00009288
9289 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009290 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009291
David S. Millerf47c11e2005-06-24 20:18:35 -07009292 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009293
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009294 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009295 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009296 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009297 tg3_free_rings(tp);
9298 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00009299 if (tg3_flag(tp, TAGGED_STATUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009300 tp->timer_offset = HZ;
9301 else
9302 tp->timer_offset = HZ / 10;
9303
9304 BUG_ON(tp->timer_offset > HZ);
9305 tp->timer_counter = tp->timer_multiplier =
9306 (HZ / tp->timer_offset);
9307 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009308 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009309
9310 init_timer(&tp->timer);
9311 tp->timer.expires = jiffies + tp->timer_offset;
9312 tp->timer.data = (unsigned long) tp;
9313 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009314 }
9315
David S. Millerf47c11e2005-06-24 20:18:35 -07009316 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009317
Matt Carlson07b01732009-08-28 14:01:15 +00009318 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009319 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009320
Joe Perches63c3a662011-04-26 08:12:10 +00009321 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009322 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009323
Michael Chan79381092005-04-21 17:13:59 -07009324 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009325 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009326 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009327 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009328 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009329
Matt Carlson679563f2009-09-01 12:55:46 +00009330 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009331 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009332
Joe Perches63c3a662011-04-26 08:12:10 +00009333 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009334 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009335
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009336 tw32(PCIE_TRANSACTION_CFG,
9337 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009338 }
Michael Chan79381092005-04-21 17:13:59 -07009339 }
9340
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009341 tg3_phy_start(tp);
9342
David S. Millerf47c11e2005-06-24 20:18:35 -07009343 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009344
Michael Chan79381092005-04-21 17:13:59 -07009345 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009346 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009347 tg3_enable_ints(tp);
9348
David S. Millerf47c11e2005-06-24 20:18:35 -07009349 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009350
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009351 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009352
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009353 /*
9354 * Reset loopback feature if it was turned on while the device was down
9355 * make sure that it's installed properly now.
9356 */
9357 if (dev->features & NETIF_F_LOOPBACK)
9358 tg3_set_loopback(dev, dev->features);
9359
Linus Torvalds1da177e2005-04-16 15:20:36 -07009360 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009361
Matt Carlson679563f2009-09-01 12:55:46 +00009362err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009363 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9364 struct tg3_napi *tnapi = &tp->napi[i];
9365 free_irq(tnapi->irq_vec, tnapi);
9366 }
Matt Carlson07b01732009-08-28 14:01:15 +00009367
Matt Carlson679563f2009-09-01 12:55:46 +00009368err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009369 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009370 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009371 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009372
9373err_out1:
9374 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009375 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009376}
9377
Eric Dumazet511d2222010-07-07 20:44:24 +00009378static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9379 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009380static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9381
9382static int tg3_close(struct net_device *dev)
9383{
Matt Carlson4f125f42009-09-01 12:55:02 +00009384 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009385 struct tg3 *tp = netdev_priv(dev);
9386
Matt Carlsonfed97812009-09-01 13:10:19 +00009387 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009388 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009389
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009390 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009391
9392 del_timer_sync(&tp->timer);
9393
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009394 tg3_phy_stop(tp);
9395
David S. Millerf47c11e2005-06-24 20:18:35 -07009396 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009397
9398 tg3_disable_ints(tp);
9399
Michael Chan944d9802005-05-29 14:57:48 -07009400 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009401 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009402 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009403
David S. Millerf47c11e2005-06-24 20:18:35 -07009404 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009405
Matt Carlson4f125f42009-09-01 12:55:02 +00009406 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9407 struct tg3_napi *tnapi = &tp->napi[i];
9408 free_irq(tnapi->irq_vec, tnapi);
9409 }
Matt Carlson07b01732009-08-28 14:01:15 +00009410
9411 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009412
Eric Dumazet511d2222010-07-07 20:44:24 +00009413 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9414
Linus Torvalds1da177e2005-04-16 15:20:36 -07009415 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9416 sizeof(tp->estats_prev));
9417
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009418 tg3_napi_fini(tp);
9419
Linus Torvalds1da177e2005-04-16 15:20:36 -07009420 tg3_free_consistent(tp);
9421
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009422 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009423
9424 netif_carrier_off(tp->dev);
9425
Linus Torvalds1da177e2005-04-16 15:20:36 -07009426 return 0;
9427}
9428
Eric Dumazet511d2222010-07-07 20:44:24 +00009429static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009430{
9431 return ((u64)val->high << 32) | ((u64)val->low);
9432}
9433
Eric Dumazet511d2222010-07-07 20:44:24 +00009434static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009435{
9436 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9437
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009438 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009439 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9440 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009441 u32 val;
9442
David S. Millerf47c11e2005-06-24 20:18:35 -07009443 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009444 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9445 tg3_writephy(tp, MII_TG3_TEST1,
9446 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009447 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009448 } else
9449 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009450 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009451
9452 tp->phy_crc_errors += val;
9453
9454 return tp->phy_crc_errors;
9455 }
9456
9457 return get_stat64(&hw_stats->rx_fcs_errors);
9458}
9459
9460#define ESTAT_ADD(member) \
9461 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009462 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009463
9464static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9465{
9466 struct tg3_ethtool_stats *estats = &tp->estats;
9467 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9468 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9469
9470 if (!hw_stats)
9471 return old_estats;
9472
9473 ESTAT_ADD(rx_octets);
9474 ESTAT_ADD(rx_fragments);
9475 ESTAT_ADD(rx_ucast_packets);
9476 ESTAT_ADD(rx_mcast_packets);
9477 ESTAT_ADD(rx_bcast_packets);
9478 ESTAT_ADD(rx_fcs_errors);
9479 ESTAT_ADD(rx_align_errors);
9480 ESTAT_ADD(rx_xon_pause_rcvd);
9481 ESTAT_ADD(rx_xoff_pause_rcvd);
9482 ESTAT_ADD(rx_mac_ctrl_rcvd);
9483 ESTAT_ADD(rx_xoff_entered);
9484 ESTAT_ADD(rx_frame_too_long_errors);
9485 ESTAT_ADD(rx_jabbers);
9486 ESTAT_ADD(rx_undersize_packets);
9487 ESTAT_ADD(rx_in_length_errors);
9488 ESTAT_ADD(rx_out_length_errors);
9489 ESTAT_ADD(rx_64_or_less_octet_packets);
9490 ESTAT_ADD(rx_65_to_127_octet_packets);
9491 ESTAT_ADD(rx_128_to_255_octet_packets);
9492 ESTAT_ADD(rx_256_to_511_octet_packets);
9493 ESTAT_ADD(rx_512_to_1023_octet_packets);
9494 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9495 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9496 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9497 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9498 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9499
9500 ESTAT_ADD(tx_octets);
9501 ESTAT_ADD(tx_collisions);
9502 ESTAT_ADD(tx_xon_sent);
9503 ESTAT_ADD(tx_xoff_sent);
9504 ESTAT_ADD(tx_flow_control);
9505 ESTAT_ADD(tx_mac_errors);
9506 ESTAT_ADD(tx_single_collisions);
9507 ESTAT_ADD(tx_mult_collisions);
9508 ESTAT_ADD(tx_deferred);
9509 ESTAT_ADD(tx_excessive_collisions);
9510 ESTAT_ADD(tx_late_collisions);
9511 ESTAT_ADD(tx_collide_2times);
9512 ESTAT_ADD(tx_collide_3times);
9513 ESTAT_ADD(tx_collide_4times);
9514 ESTAT_ADD(tx_collide_5times);
9515 ESTAT_ADD(tx_collide_6times);
9516 ESTAT_ADD(tx_collide_7times);
9517 ESTAT_ADD(tx_collide_8times);
9518 ESTAT_ADD(tx_collide_9times);
9519 ESTAT_ADD(tx_collide_10times);
9520 ESTAT_ADD(tx_collide_11times);
9521 ESTAT_ADD(tx_collide_12times);
9522 ESTAT_ADD(tx_collide_13times);
9523 ESTAT_ADD(tx_collide_14times);
9524 ESTAT_ADD(tx_collide_15times);
9525 ESTAT_ADD(tx_ucast_packets);
9526 ESTAT_ADD(tx_mcast_packets);
9527 ESTAT_ADD(tx_bcast_packets);
9528 ESTAT_ADD(tx_carrier_sense_errors);
9529 ESTAT_ADD(tx_discards);
9530 ESTAT_ADD(tx_errors);
9531
9532 ESTAT_ADD(dma_writeq_full);
9533 ESTAT_ADD(dma_write_prioq_full);
9534 ESTAT_ADD(rxbds_empty);
9535 ESTAT_ADD(rx_discards);
9536 ESTAT_ADD(rx_errors);
9537 ESTAT_ADD(rx_threshold_hit);
9538
9539 ESTAT_ADD(dma_readq_full);
9540 ESTAT_ADD(dma_read_prioq_full);
9541 ESTAT_ADD(tx_comp_queue_full);
9542
9543 ESTAT_ADD(ring_set_send_prod_index);
9544 ESTAT_ADD(ring_status_update);
9545 ESTAT_ADD(nic_irqs);
9546 ESTAT_ADD(nic_avoided_irqs);
9547 ESTAT_ADD(nic_tx_threshold_hit);
9548
9549 return estats;
9550}
9551
Eric Dumazet511d2222010-07-07 20:44:24 +00009552static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9553 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009554{
9555 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009556 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009557 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9558
9559 if (!hw_stats)
9560 return old_stats;
9561
9562 stats->rx_packets = old_stats->rx_packets +
9563 get_stat64(&hw_stats->rx_ucast_packets) +
9564 get_stat64(&hw_stats->rx_mcast_packets) +
9565 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009566
Linus Torvalds1da177e2005-04-16 15:20:36 -07009567 stats->tx_packets = old_stats->tx_packets +
9568 get_stat64(&hw_stats->tx_ucast_packets) +
9569 get_stat64(&hw_stats->tx_mcast_packets) +
9570 get_stat64(&hw_stats->tx_bcast_packets);
9571
9572 stats->rx_bytes = old_stats->rx_bytes +
9573 get_stat64(&hw_stats->rx_octets);
9574 stats->tx_bytes = old_stats->tx_bytes +
9575 get_stat64(&hw_stats->tx_octets);
9576
9577 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009578 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009579 stats->tx_errors = old_stats->tx_errors +
9580 get_stat64(&hw_stats->tx_errors) +
9581 get_stat64(&hw_stats->tx_mac_errors) +
9582 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9583 get_stat64(&hw_stats->tx_discards);
9584
9585 stats->multicast = old_stats->multicast +
9586 get_stat64(&hw_stats->rx_mcast_packets);
9587 stats->collisions = old_stats->collisions +
9588 get_stat64(&hw_stats->tx_collisions);
9589
9590 stats->rx_length_errors = old_stats->rx_length_errors +
9591 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9592 get_stat64(&hw_stats->rx_undersize_packets);
9593
9594 stats->rx_over_errors = old_stats->rx_over_errors +
9595 get_stat64(&hw_stats->rxbds_empty);
9596 stats->rx_frame_errors = old_stats->rx_frame_errors +
9597 get_stat64(&hw_stats->rx_align_errors);
9598 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9599 get_stat64(&hw_stats->tx_discards);
9600 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9601 get_stat64(&hw_stats->tx_carrier_sense_errors);
9602
9603 stats->rx_crc_errors = old_stats->rx_crc_errors +
9604 calc_crc_errors(tp);
9605
John W. Linville4f63b872005-09-12 14:43:18 -07009606 stats->rx_missed_errors = old_stats->rx_missed_errors +
9607 get_stat64(&hw_stats->rx_discards);
9608
Eric Dumazetb0057c52010-10-10 19:55:52 +00009609 stats->rx_dropped = tp->rx_dropped;
9610
Linus Torvalds1da177e2005-04-16 15:20:36 -07009611 return stats;
9612}
9613
9614static inline u32 calc_crc(unsigned char *buf, int len)
9615{
9616 u32 reg;
9617 u32 tmp;
9618 int j, k;
9619
9620 reg = 0xffffffff;
9621
9622 for (j = 0; j < len; j++) {
9623 reg ^= buf[j];
9624
9625 for (k = 0; k < 8; k++) {
9626 tmp = reg & 0x01;
9627
9628 reg >>= 1;
9629
Matt Carlson859a588792010-04-05 10:19:28 +00009630 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009631 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009632 }
9633 }
9634
9635 return ~reg;
9636}
9637
9638static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9639{
9640 /* accept or reject all multicast frames */
9641 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9642 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9643 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9644 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9645}
9646
9647static void __tg3_set_rx_mode(struct net_device *dev)
9648{
9649 struct tg3 *tp = netdev_priv(dev);
9650 u32 rx_mode;
9651
9652 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9653 RX_MODE_KEEP_VLAN_TAG);
9654
Matt Carlsonbf933c82011-01-25 15:58:49 +00009655#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009656 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9657 * flag clear.
9658 */
Joe Perches63c3a662011-04-26 08:12:10 +00009659 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009660 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9661#endif
9662
9663 if (dev->flags & IFF_PROMISC) {
9664 /* Promiscuous mode. */
9665 rx_mode |= RX_MODE_PROMISC;
9666 } else if (dev->flags & IFF_ALLMULTI) {
9667 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009668 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009669 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009670 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009671 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009672 } else {
9673 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +00009674 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009675 u32 mc_filter[4] = { 0, };
9676 u32 regidx;
9677 u32 bit;
9678 u32 crc;
9679
Jiri Pirko22bedad32010-04-01 21:22:57 +00009680 netdev_for_each_mc_addr(ha, dev) {
9681 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009682 bit = ~crc & 0x7f;
9683 regidx = (bit & 0x60) >> 5;
9684 bit &= 0x1f;
9685 mc_filter[regidx] |= (1 << bit);
9686 }
9687
9688 tw32(MAC_HASH_REG_0, mc_filter[0]);
9689 tw32(MAC_HASH_REG_1, mc_filter[1]);
9690 tw32(MAC_HASH_REG_2, mc_filter[2]);
9691 tw32(MAC_HASH_REG_3, mc_filter[3]);
9692 }
9693
9694 if (rx_mode != tp->rx_mode) {
9695 tp->rx_mode = rx_mode;
9696 tw32_f(MAC_RX_MODE, rx_mode);
9697 udelay(10);
9698 }
9699}
9700
9701static void tg3_set_rx_mode(struct net_device *dev)
9702{
9703 struct tg3 *tp = netdev_priv(dev);
9704
Michael Chane75f7c92006-03-20 21:33:26 -08009705 if (!netif_running(dev))
9706 return;
9707
David S. Millerf47c11e2005-06-24 20:18:35 -07009708 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009709 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009710 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009711}
9712
Linus Torvalds1da177e2005-04-16 15:20:36 -07009713static int tg3_get_regs_len(struct net_device *dev)
9714{
Matt Carlson97bd8e42011-04-13 11:05:04 +00009715 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009716}
9717
9718static void tg3_get_regs(struct net_device *dev,
9719 struct ethtool_regs *regs, void *_p)
9720{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009721 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009722
9723 regs->version = 0;
9724
Matt Carlson97bd8e42011-04-13 11:05:04 +00009725 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009726
Matt Carlson80096062010-08-02 11:26:06 +00009727 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009728 return;
9729
David S. Millerf47c11e2005-06-24 20:18:35 -07009730 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009731
Matt Carlson97bd8e42011-04-13 11:05:04 +00009732 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009733
David S. Millerf47c11e2005-06-24 20:18:35 -07009734 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009735}
9736
9737static int tg3_get_eeprom_len(struct net_device *dev)
9738{
9739 struct tg3 *tp = netdev_priv(dev);
9740
9741 return tp->nvram_size;
9742}
9743
Linus Torvalds1da177e2005-04-16 15:20:36 -07009744static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9745{
9746 struct tg3 *tp = netdev_priv(dev);
9747 int ret;
9748 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009749 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009750 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009751
Joe Perches63c3a662011-04-26 08:12:10 +00009752 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +00009753 return -EINVAL;
9754
Matt Carlson80096062010-08-02 11:26:06 +00009755 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009756 return -EAGAIN;
9757
Linus Torvalds1da177e2005-04-16 15:20:36 -07009758 offset = eeprom->offset;
9759 len = eeprom->len;
9760 eeprom->len = 0;
9761
9762 eeprom->magic = TG3_EEPROM_MAGIC;
9763
9764 if (offset & 3) {
9765 /* adjustments to start on required 4 byte boundary */
9766 b_offset = offset & 3;
9767 b_count = 4 - b_offset;
9768 if (b_count > len) {
9769 /* i.e. offset=1 len=2 */
9770 b_count = len;
9771 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009772 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009773 if (ret)
9774 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009775 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776 len -= b_count;
9777 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009778 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009779 }
9780
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009781 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009782 pd = &data[eeprom->len];
9783 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009784 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009785 if (ret) {
9786 eeprom->len += i;
9787 return ret;
9788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009789 memcpy(pd + i, &val, 4);
9790 }
9791 eeprom->len += i;
9792
9793 if (len & 3) {
9794 /* read last bytes not ending on 4 byte boundary */
9795 pd = &data[eeprom->len];
9796 b_count = len & 3;
9797 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009798 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009799 if (ret)
9800 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009801 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009802 eeprom->len += b_count;
9803 }
9804 return 0;
9805}
9806
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009807static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009808
9809static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9810{
9811 struct tg3 *tp = netdev_priv(dev);
9812 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009813 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009814 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009815 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009816
Matt Carlson80096062010-08-02 11:26:06 +00009817 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009818 return -EAGAIN;
9819
Joe Perches63c3a662011-04-26 08:12:10 +00009820 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +00009821 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009822 return -EINVAL;
9823
9824 offset = eeprom->offset;
9825 len = eeprom->len;
9826
9827 if ((b_offset = (offset & 3))) {
9828 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009829 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009830 if (ret)
9831 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009832 len += b_offset;
9833 offset &= ~3;
Michael Chan1c8594b42005-04-21 17:12:46 -07009834 if (len < 4)
9835 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009836 }
9837
9838 odd_len = 0;
Michael Chan1c8594b42005-04-21 17:12:46 -07009839 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840 /* adjustments to end on required 4 byte boundary */
9841 odd_len = 1;
9842 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009843 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009844 if (ret)
9845 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009846 }
9847
9848 buf = data;
9849 if (b_offset || odd_len) {
9850 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009851 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009852 return -ENOMEM;
9853 if (b_offset)
9854 memcpy(buf, &start, 4);
9855 if (odd_len)
9856 memcpy(buf+len-4, &end, 4);
9857 memcpy(buf + b_offset, data, eeprom->len);
9858 }
9859
9860 ret = tg3_nvram_write_block(tp, offset, len, buf);
9861
9862 if (buf != data)
9863 kfree(buf);
9864
9865 return ret;
9866}
9867
9868static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9869{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009870 struct tg3 *tp = netdev_priv(dev);
9871
Joe Perches63c3a662011-04-26 08:12:10 +00009872 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009873 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009874 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009875 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009876 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9877 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009878 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009879
Linus Torvalds1da177e2005-04-16 15:20:36 -07009880 cmd->supported = (SUPPORTED_Autoneg);
9881
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009882 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009883 cmd->supported |= (SUPPORTED_1000baseT_Half |
9884 SUPPORTED_1000baseT_Full);
9885
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009886 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009887 cmd->supported |= (SUPPORTED_100baseT_Half |
9888 SUPPORTED_100baseT_Full |
9889 SUPPORTED_10baseT_Half |
9890 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009891 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009892 cmd->port = PORT_TP;
9893 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009894 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009895 cmd->port = PORT_FIBRE;
9896 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009897
Linus Torvalds1da177e2005-04-16 15:20:36 -07009898 cmd->advertising = tp->link_config.advertising;
9899 if (netif_running(dev)) {
David Decotigny70739492011-04-27 18:32:40 +00009900 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009901 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +00009902 } else {
David Decotigny70739492011-04-27 18:32:40 +00009903 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +00009904 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009905 }
Matt Carlson882e9792009-09-01 13:21:36 +00009906 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009907 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009908 cmd->autoneg = tp->link_config.autoneg;
9909 cmd->maxtxpkt = 0;
9910 cmd->maxrxpkt = 0;
9911 return 0;
9912}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009913
Linus Torvalds1da177e2005-04-16 15:20:36 -07009914static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9915{
9916 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +00009917 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009918
Joe Perches63c3a662011-04-26 08:12:10 +00009919 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009920 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009921 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009922 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009923 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9924 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009925 }
9926
Matt Carlson7e5856b2009-02-25 14:23:01 +00009927 if (cmd->autoneg != AUTONEG_ENABLE &&
9928 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009929 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009930
9931 if (cmd->autoneg == AUTONEG_DISABLE &&
9932 cmd->duplex != DUPLEX_FULL &&
9933 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009934 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009935
Matt Carlson7e5856b2009-02-25 14:23:01 +00009936 if (cmd->autoneg == AUTONEG_ENABLE) {
9937 u32 mask = ADVERTISED_Autoneg |
9938 ADVERTISED_Pause |
9939 ADVERTISED_Asym_Pause;
9940
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009941 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009942 mask |= ADVERTISED_1000baseT_Half |
9943 ADVERTISED_1000baseT_Full;
9944
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009945 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009946 mask |= ADVERTISED_100baseT_Half |
9947 ADVERTISED_100baseT_Full |
9948 ADVERTISED_10baseT_Half |
9949 ADVERTISED_10baseT_Full |
9950 ADVERTISED_TP;
9951 else
9952 mask |= ADVERTISED_FIBRE;
9953
9954 if (cmd->advertising & ~mask)
9955 return -EINVAL;
9956
9957 mask &= (ADVERTISED_1000baseT_Half |
9958 ADVERTISED_1000baseT_Full |
9959 ADVERTISED_100baseT_Half |
9960 ADVERTISED_100baseT_Full |
9961 ADVERTISED_10baseT_Half |
9962 ADVERTISED_10baseT_Full);
9963
9964 cmd->advertising &= mask;
9965 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009966 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +00009967 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +00009968 return -EINVAL;
9969
9970 if (cmd->duplex != DUPLEX_FULL)
9971 return -EINVAL;
9972 } else {
David Decotigny25db0332011-04-27 18:32:39 +00009973 if (speed != SPEED_100 &&
9974 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +00009975 return -EINVAL;
9976 }
9977 }
9978
David S. Millerf47c11e2005-06-24 20:18:35 -07009979 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009980
9981 tp->link_config.autoneg = cmd->autoneg;
9982 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009983 tp->link_config.advertising = (cmd->advertising |
9984 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009985 tp->link_config.speed = SPEED_INVALID;
9986 tp->link_config.duplex = DUPLEX_INVALID;
9987 } else {
9988 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +00009989 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009990 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009991 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009992
Michael Chan24fcad62006-12-17 17:06:46 -08009993 tp->link_config.orig_speed = tp->link_config.speed;
9994 tp->link_config.orig_duplex = tp->link_config.duplex;
9995 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9996
Linus Torvalds1da177e2005-04-16 15:20:36 -07009997 if (netif_running(dev))
9998 tg3_setup_phy(tp, 1);
9999
David S. Millerf47c11e2005-06-24 20:18:35 -070010000 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010001
Linus Torvalds1da177e2005-04-16 15:20:36 -070010002 return 0;
10003}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010004
Linus Torvalds1da177e2005-04-16 15:20:36 -070010005static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10006{
10007 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010008
Linus Torvalds1da177e2005-04-16 15:20:36 -070010009 strcpy(info->driver, DRV_MODULE_NAME);
10010 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010011 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010012 strcpy(info->bus_info, pci_name(tp->pdev));
10013}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010014
Linus Torvalds1da177e2005-04-16 15:20:36 -070010015static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10016{
10017 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010018
Joe Perches63c3a662011-04-26 08:12:10 +000010019 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010020 wol->supported = WAKE_MAGIC;
10021 else
10022 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010023 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010024 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010025 wol->wolopts = WAKE_MAGIC;
10026 memset(&wol->sopass, 0, sizeof(wol->sopass));
10027}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010028
Linus Torvalds1da177e2005-04-16 15:20:36 -070010029static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10030{
10031 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010032 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010033
Linus Torvalds1da177e2005-04-16 15:20:36 -070010034 if (wol->wolopts & ~WAKE_MAGIC)
10035 return -EINVAL;
10036 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010037 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010038 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010039
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010040 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10041
David S. Millerf47c11e2005-06-24 20:18:35 -070010042 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010043 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010044 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010045 else
Joe Perches63c3a662011-04-26 08:12:10 +000010046 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010047 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010048
Linus Torvalds1da177e2005-04-16 15:20:36 -070010049 return 0;
10050}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010051
Linus Torvalds1da177e2005-04-16 15:20:36 -070010052static u32 tg3_get_msglevel(struct net_device *dev)
10053{
10054 struct tg3 *tp = netdev_priv(dev);
10055 return tp->msg_enable;
10056}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010057
Linus Torvalds1da177e2005-04-16 15:20:36 -070010058static void tg3_set_msglevel(struct net_device *dev, u32 value)
10059{
10060 struct tg3 *tp = netdev_priv(dev);
10061 tp->msg_enable = value;
10062}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010063
Linus Torvalds1da177e2005-04-16 15:20:36 -070010064static int tg3_nway_reset(struct net_device *dev)
10065{
10066 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010067 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010068
Linus Torvalds1da177e2005-04-16 15:20:36 -070010069 if (!netif_running(dev))
10070 return -EAGAIN;
10071
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010072 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010073 return -EINVAL;
10074
Joe Perches63c3a662011-04-26 08:12:10 +000010075 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010076 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010077 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010078 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010079 } else {
10080 u32 bmcr;
10081
10082 spin_lock_bh(&tp->lock);
10083 r = -EINVAL;
10084 tg3_readphy(tp, MII_BMCR, &bmcr);
10085 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10086 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010087 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010088 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10089 BMCR_ANENABLE);
10090 r = 0;
10091 }
10092 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010093 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010094
Linus Torvalds1da177e2005-04-16 15:20:36 -070010095 return r;
10096}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010097
Linus Torvalds1da177e2005-04-16 15:20:36 -070010098static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10099{
10100 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010101
Matt Carlson2c49a442010-09-30 10:34:35 +000010102 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010103 ering->rx_mini_max_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010104 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010105 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010106 else
10107 ering->rx_jumbo_max_pending = 0;
10108
10109 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010110
10111 ering->rx_pending = tp->rx_pending;
10112 ering->rx_mini_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010113 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010114 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10115 else
10116 ering->rx_jumbo_pending = 0;
10117
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010118 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010119}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010120
Linus Torvalds1da177e2005-04-16 15:20:36 -070010121static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10122{
10123 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010124 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010125
Matt Carlson2c49a442010-09-30 10:34:35 +000010126 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10127 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010128 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10129 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010130 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010131 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010132 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010133
Michael Chanbbe832c2005-06-24 20:20:04 -070010134 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010135 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010136 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010137 irq_sync = 1;
10138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010139
Michael Chanbbe832c2005-06-24 20:20:04 -070010140 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010141
Linus Torvalds1da177e2005-04-16 15:20:36 -070010142 tp->rx_pending = ering->rx_pending;
10143
Joe Perches63c3a662011-04-26 08:12:10 +000010144 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010145 tp->rx_pending > 63)
10146 tp->rx_pending = 63;
10147 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010148
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010149 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010150 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010151
10152 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010153 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010154 err = tg3_restart_hw(tp, 1);
10155 if (!err)
10156 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010157 }
10158
David S. Millerf47c11e2005-06-24 20:18:35 -070010159 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010160
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010161 if (irq_sync && !err)
10162 tg3_phy_start(tp);
10163
Michael Chanb9ec6c12006-07-25 16:37:27 -070010164 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010165}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010166
Linus Torvalds1da177e2005-04-16 15:20:36 -070010167static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10168{
10169 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010170
Joe Perches63c3a662011-04-26 08:12:10 +000010171 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010172
Steve Glendinninge18ce342008-12-16 02:00:00 -080010173 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010174 epause->rx_pause = 1;
10175 else
10176 epause->rx_pause = 0;
10177
Steve Glendinninge18ce342008-12-16 02:00:00 -080010178 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010179 epause->tx_pause = 1;
10180 else
10181 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010182}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010183
Linus Torvalds1da177e2005-04-16 15:20:36 -070010184static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10185{
10186 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010187 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010188
Joe Perches63c3a662011-04-26 08:12:10 +000010189 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010190 u32 newadv;
10191 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010192
Matt Carlson27121682010-02-17 15:16:57 +000010193 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010194
Matt Carlson27121682010-02-17 15:16:57 +000010195 if (!(phydev->supported & SUPPORTED_Pause) ||
10196 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010197 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010198 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010199
Matt Carlson27121682010-02-17 15:16:57 +000010200 tp->link_config.flowctrl = 0;
10201 if (epause->rx_pause) {
10202 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010203
Matt Carlson27121682010-02-17 15:16:57 +000010204 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010205 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010206 newadv = ADVERTISED_Pause;
10207 } else
10208 newadv = ADVERTISED_Pause |
10209 ADVERTISED_Asym_Pause;
10210 } else if (epause->tx_pause) {
10211 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10212 newadv = ADVERTISED_Asym_Pause;
10213 } else
10214 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010215
Matt Carlson27121682010-02-17 15:16:57 +000010216 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010217 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010218 else
Joe Perches63c3a662011-04-26 08:12:10 +000010219 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010220
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010221 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010222 u32 oldadv = phydev->advertising &
10223 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10224 if (oldadv != newadv) {
10225 phydev->advertising &=
10226 ~(ADVERTISED_Pause |
10227 ADVERTISED_Asym_Pause);
10228 phydev->advertising |= newadv;
10229 if (phydev->autoneg) {
10230 /*
10231 * Always renegotiate the link to
10232 * inform our link partner of our
10233 * flow control settings, even if the
10234 * flow control is forced. Let
10235 * tg3_adjust_link() do the final
10236 * flow control setup.
10237 */
10238 return phy_start_aneg(phydev);
10239 }
10240 }
10241
10242 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010243 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010244 } else {
10245 tp->link_config.orig_advertising &=
10246 ~(ADVERTISED_Pause |
10247 ADVERTISED_Asym_Pause);
10248 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010249 }
10250 } else {
10251 int irq_sync = 0;
10252
10253 if (netif_running(dev)) {
10254 tg3_netif_stop(tp);
10255 irq_sync = 1;
10256 }
10257
10258 tg3_full_lock(tp, irq_sync);
10259
10260 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010261 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010262 else
Joe Perches63c3a662011-04-26 08:12:10 +000010263 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010264 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010265 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010266 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010267 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010268 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010269 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010270 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010271 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010272
10273 if (netif_running(dev)) {
10274 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10275 err = tg3_restart_hw(tp, 1);
10276 if (!err)
10277 tg3_netif_start(tp);
10278 }
10279
10280 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010282
Michael Chanb9ec6c12006-07-25 16:37:27 -070010283 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010284}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010285
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010286static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010287{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010288 switch (sset) {
10289 case ETH_SS_TEST:
10290 return TG3_NUM_TEST;
10291 case ETH_SS_STATS:
10292 return TG3_NUM_STATS;
10293 default:
10294 return -EOPNOTSUPP;
10295 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010296}
10297
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010298static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010299{
10300 switch (stringset) {
10301 case ETH_SS_STATS:
10302 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10303 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010304 case ETH_SS_TEST:
10305 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10306 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010307 default:
10308 WARN_ON(1); /* we need a WARN() */
10309 break;
10310 }
10311}
10312
stephen hemminger81b87092011-04-04 08:43:50 +000010313static int tg3_set_phys_id(struct net_device *dev,
10314 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010315{
10316 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010317
10318 if (!netif_running(tp->dev))
10319 return -EAGAIN;
10320
stephen hemminger81b87092011-04-04 08:43:50 +000010321 switch (state) {
10322 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010323 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010324
stephen hemminger81b87092011-04-04 08:43:50 +000010325 case ETHTOOL_ID_ON:
10326 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10327 LED_CTRL_1000MBPS_ON |
10328 LED_CTRL_100MBPS_ON |
10329 LED_CTRL_10MBPS_ON |
10330 LED_CTRL_TRAFFIC_OVERRIDE |
10331 LED_CTRL_TRAFFIC_BLINK |
10332 LED_CTRL_TRAFFIC_LED);
10333 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010334
stephen hemminger81b87092011-04-04 08:43:50 +000010335 case ETHTOOL_ID_OFF:
10336 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10337 LED_CTRL_TRAFFIC_OVERRIDE);
10338 break;
Michael Chan4009a932005-09-05 17:52:54 -070010339
stephen hemminger81b87092011-04-04 08:43:50 +000010340 case ETHTOOL_ID_INACTIVE:
10341 tw32(MAC_LED_CTRL, tp->led_ctrl);
10342 break;
Michael Chan4009a932005-09-05 17:52:54 -070010343 }
stephen hemminger81b87092011-04-04 08:43:50 +000010344
Michael Chan4009a932005-09-05 17:52:54 -070010345 return 0;
10346}
10347
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010348static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010349 struct ethtool_stats *estats, u64 *tmp_stats)
10350{
10351 struct tg3 *tp = netdev_priv(dev);
10352 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10353}
10354
Matt Carlsonc3e94502011-04-13 11:05:08 +000010355static __be32 * tg3_vpd_readblock(struct tg3 *tp)
10356{
10357 int i;
10358 __be32 *buf;
10359 u32 offset = 0, len = 0;
10360 u32 magic, val;
10361
Joe Perches63c3a662011-04-26 08:12:10 +000010362 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010363 return NULL;
10364
10365 if (magic == TG3_EEPROM_MAGIC) {
10366 for (offset = TG3_NVM_DIR_START;
10367 offset < TG3_NVM_DIR_END;
10368 offset += TG3_NVM_DIRENT_SIZE) {
10369 if (tg3_nvram_read(tp, offset, &val))
10370 return NULL;
10371
10372 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10373 TG3_NVM_DIRTYPE_EXTVPD)
10374 break;
10375 }
10376
10377 if (offset != TG3_NVM_DIR_END) {
10378 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10379 if (tg3_nvram_read(tp, offset + 4, &offset))
10380 return NULL;
10381
10382 offset = tg3_nvram_logical_addr(tp, offset);
10383 }
10384 }
10385
10386 if (!offset || !len) {
10387 offset = TG3_NVM_VPD_OFF;
10388 len = TG3_NVM_VPD_LEN;
10389 }
10390
10391 buf = kmalloc(len, GFP_KERNEL);
10392 if (buf == NULL)
10393 return NULL;
10394
10395 if (magic == TG3_EEPROM_MAGIC) {
10396 for (i = 0; i < len; i += 4) {
10397 /* The data is in little-endian format in NVRAM.
10398 * Use the big-endian read routines to preserve
10399 * the byte order as it exists in NVRAM.
10400 */
10401 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10402 goto error;
10403 }
10404 } else {
10405 u8 *ptr;
10406 ssize_t cnt;
10407 unsigned int pos = 0;
10408
10409 ptr = (u8 *)&buf[0];
10410 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10411 cnt = pci_read_vpd(tp->pdev, pos,
10412 len - pos, ptr);
10413 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10414 cnt = 0;
10415 else if (cnt < 0)
10416 goto error;
10417 }
10418 if (pos != len)
10419 goto error;
10420 }
10421
10422 return buf;
10423
10424error:
10425 kfree(buf);
10426 return NULL;
10427}
10428
Michael Chan566f86a2005-05-29 14:56:58 -070010429#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010430#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10431#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10432#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010433#define NVRAM_SELFBOOT_HW_SIZE 0x20
10434#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010435
10436static int tg3_test_nvram(struct tg3 *tp)
10437{
Al Virob9fc7dc2007-12-17 22:59:57 -080010438 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010439 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010440 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010441
Joe Perches63c3a662011-04-26 08:12:10 +000010442 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010443 return 0;
10444
Matt Carlsone4f34112009-02-25 14:25:00 +000010445 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010446 return -EIO;
10447
Michael Chan1b277772006-03-20 22:27:48 -080010448 if (magic == TG3_EEPROM_MAGIC)
10449 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010450 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010451 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10452 TG3_EEPROM_SB_FORMAT_1) {
10453 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10454 case TG3_EEPROM_SB_REVISION_0:
10455 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10456 break;
10457 case TG3_EEPROM_SB_REVISION_2:
10458 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10459 break;
10460 case TG3_EEPROM_SB_REVISION_3:
10461 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10462 break;
10463 default:
10464 return 0;
10465 }
10466 } else
Michael Chan1b277772006-03-20 22:27:48 -080010467 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010468 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10469 size = NVRAM_SELFBOOT_HW_SIZE;
10470 else
Michael Chan1b277772006-03-20 22:27:48 -080010471 return -EIO;
10472
10473 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010474 if (buf == NULL)
10475 return -ENOMEM;
10476
Michael Chan1b277772006-03-20 22:27:48 -080010477 err = -EIO;
10478 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010479 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10480 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010481 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010482 }
Michael Chan1b277772006-03-20 22:27:48 -080010483 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010484 goto out;
10485
Michael Chan1b277772006-03-20 22:27:48 -080010486 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010487 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010488 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010489 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010490 u8 *buf8 = (u8 *) buf, csum8 = 0;
10491
Al Virob9fc7dc2007-12-17 22:59:57 -080010492 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010493 TG3_EEPROM_SB_REVISION_2) {
10494 /* For rev 2, the csum doesn't include the MBA. */
10495 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10496 csum8 += buf8[i];
10497 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10498 csum8 += buf8[i];
10499 } else {
10500 for (i = 0; i < size; i++)
10501 csum8 += buf8[i];
10502 }
Michael Chan1b277772006-03-20 22:27:48 -080010503
Adrian Bunkad96b482006-04-05 22:21:04 -070010504 if (csum8 == 0) {
10505 err = 0;
10506 goto out;
10507 }
10508
10509 err = -EIO;
10510 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010511 }
Michael Chan566f86a2005-05-29 14:56:58 -070010512
Al Virob9fc7dc2007-12-17 22:59:57 -080010513 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010514 TG3_EEPROM_MAGIC_HW) {
10515 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010516 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010517 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010518
10519 /* Separate the parity bits and the data bytes. */
10520 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10521 if ((i == 0) || (i == 8)) {
10522 int l;
10523 u8 msk;
10524
10525 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10526 parity[k++] = buf8[i] & msk;
10527 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010528 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010529 int l;
10530 u8 msk;
10531
10532 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10533 parity[k++] = buf8[i] & msk;
10534 i++;
10535
10536 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10537 parity[k++] = buf8[i] & msk;
10538 i++;
10539 }
10540 data[j++] = buf8[i];
10541 }
10542
10543 err = -EIO;
10544 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10545 u8 hw8 = hweight8(data[i]);
10546
10547 if ((hw8 & 0x1) && parity[i])
10548 goto out;
10549 else if (!(hw8 & 0x1) && !parity[i])
10550 goto out;
10551 }
10552 err = 0;
10553 goto out;
10554 }
10555
Matt Carlson01c3a392011-03-09 16:58:20 +000010556 err = -EIO;
10557
Michael Chan566f86a2005-05-29 14:56:58 -070010558 /* Bootstrap checksum at offset 0x10 */
10559 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010560 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010561 goto out;
10562
10563 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10564 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010565 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010566 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010567
Matt Carlsonc3e94502011-04-13 11:05:08 +000010568 kfree(buf);
10569
10570 buf = tg3_vpd_readblock(tp);
10571 if (!buf)
10572 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000010573
10574 i = pci_vpd_find_tag((u8 *)buf, 0, TG3_NVM_VPD_LEN,
10575 PCI_VPD_LRDT_RO_DATA);
10576 if (i > 0) {
10577 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10578 if (j < 0)
10579 goto out;
10580
10581 if (i + PCI_VPD_LRDT_TAG_SIZE + j > TG3_NVM_VPD_LEN)
10582 goto out;
10583
10584 i += PCI_VPD_LRDT_TAG_SIZE;
10585 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10586 PCI_VPD_RO_KEYWORD_CHKSUM);
10587 if (j > 0) {
10588 u8 csum8 = 0;
10589
10590 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10591
10592 for (i = 0; i <= j; i++)
10593 csum8 += ((u8 *)buf)[i];
10594
10595 if (csum8)
10596 goto out;
10597 }
10598 }
10599
Michael Chan566f86a2005-05-29 14:56:58 -070010600 err = 0;
10601
10602out:
10603 kfree(buf);
10604 return err;
10605}
10606
Michael Chanca430072005-05-29 14:57:23 -070010607#define TG3_SERDES_TIMEOUT_SEC 2
10608#define TG3_COPPER_TIMEOUT_SEC 6
10609
10610static int tg3_test_link(struct tg3 *tp)
10611{
10612 int i, max;
10613
10614 if (!netif_running(tp->dev))
10615 return -ENODEV;
10616
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010617 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010618 max = TG3_SERDES_TIMEOUT_SEC;
10619 else
10620 max = TG3_COPPER_TIMEOUT_SEC;
10621
10622 for (i = 0; i < max; i++) {
10623 if (netif_carrier_ok(tp->dev))
10624 return 0;
10625
10626 if (msleep_interruptible(1000))
10627 break;
10628 }
10629
10630 return -EIO;
10631}
10632
Michael Chana71116d2005-05-29 14:58:11 -070010633/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010634static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010635{
Michael Chanb16250e2006-09-27 16:10:14 -070010636 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010637 u32 offset, read_mask, write_mask, val, save_val, read_val;
10638 static struct {
10639 u16 offset;
10640 u16 flags;
10641#define TG3_FL_5705 0x1
10642#define TG3_FL_NOT_5705 0x2
10643#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010644#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010645 u32 read_mask;
10646 u32 write_mask;
10647 } reg_tbl[] = {
10648 /* MAC Control Registers */
10649 { MAC_MODE, TG3_FL_NOT_5705,
10650 0x00000000, 0x00ef6f8c },
10651 { MAC_MODE, TG3_FL_5705,
10652 0x00000000, 0x01ef6b8c },
10653 { MAC_STATUS, TG3_FL_NOT_5705,
10654 0x03800107, 0x00000000 },
10655 { MAC_STATUS, TG3_FL_5705,
10656 0x03800100, 0x00000000 },
10657 { MAC_ADDR_0_HIGH, 0x0000,
10658 0x00000000, 0x0000ffff },
10659 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010660 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010661 { MAC_RX_MTU_SIZE, 0x0000,
10662 0x00000000, 0x0000ffff },
10663 { MAC_TX_MODE, 0x0000,
10664 0x00000000, 0x00000070 },
10665 { MAC_TX_LENGTHS, 0x0000,
10666 0x00000000, 0x00003fff },
10667 { MAC_RX_MODE, TG3_FL_NOT_5705,
10668 0x00000000, 0x000007fc },
10669 { MAC_RX_MODE, TG3_FL_5705,
10670 0x00000000, 0x000007dc },
10671 { MAC_HASH_REG_0, 0x0000,
10672 0x00000000, 0xffffffff },
10673 { MAC_HASH_REG_1, 0x0000,
10674 0x00000000, 0xffffffff },
10675 { MAC_HASH_REG_2, 0x0000,
10676 0x00000000, 0xffffffff },
10677 { MAC_HASH_REG_3, 0x0000,
10678 0x00000000, 0xffffffff },
10679
10680 /* Receive Data and Receive BD Initiator Control Registers. */
10681 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10682 0x00000000, 0xffffffff },
10683 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10684 0x00000000, 0xffffffff },
10685 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10686 0x00000000, 0x00000003 },
10687 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10688 0x00000000, 0xffffffff },
10689 { RCVDBDI_STD_BD+0, 0x0000,
10690 0x00000000, 0xffffffff },
10691 { RCVDBDI_STD_BD+4, 0x0000,
10692 0x00000000, 0xffffffff },
10693 { RCVDBDI_STD_BD+8, 0x0000,
10694 0x00000000, 0xffff0002 },
10695 { RCVDBDI_STD_BD+0xc, 0x0000,
10696 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010697
Michael Chana71116d2005-05-29 14:58:11 -070010698 /* Receive BD Initiator Control Registers. */
10699 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10700 0x00000000, 0xffffffff },
10701 { RCVBDI_STD_THRESH, TG3_FL_5705,
10702 0x00000000, 0x000003ff },
10703 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10704 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010705
Michael Chana71116d2005-05-29 14:58:11 -070010706 /* Host Coalescing Control Registers. */
10707 { HOSTCC_MODE, TG3_FL_NOT_5705,
10708 0x00000000, 0x00000004 },
10709 { HOSTCC_MODE, TG3_FL_5705,
10710 0x00000000, 0x000000f6 },
10711 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10712 0x00000000, 0xffffffff },
10713 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10714 0x00000000, 0x000003ff },
10715 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10716 0x00000000, 0xffffffff },
10717 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10718 0x00000000, 0x000003ff },
10719 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10720 0x00000000, 0xffffffff },
10721 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10722 0x00000000, 0x000000ff },
10723 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10724 0x00000000, 0xffffffff },
10725 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10726 0x00000000, 0x000000ff },
10727 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10728 0x00000000, 0xffffffff },
10729 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10730 0x00000000, 0xffffffff },
10731 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10732 0x00000000, 0xffffffff },
10733 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10734 0x00000000, 0x000000ff },
10735 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10736 0x00000000, 0xffffffff },
10737 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10738 0x00000000, 0x000000ff },
10739 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10740 0x00000000, 0xffffffff },
10741 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10742 0x00000000, 0xffffffff },
10743 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10744 0x00000000, 0xffffffff },
10745 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10746 0x00000000, 0xffffffff },
10747 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10748 0x00000000, 0xffffffff },
10749 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10750 0xffffffff, 0x00000000 },
10751 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10752 0xffffffff, 0x00000000 },
10753
10754 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010755 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010756 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010757 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010758 0x00000000, 0x007fffff },
10759 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10760 0x00000000, 0x0000003f },
10761 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10762 0x00000000, 0x000001ff },
10763 { BUFMGR_MB_HIGH_WATER, 0x0000,
10764 0x00000000, 0x000001ff },
10765 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10766 0xffffffff, 0x00000000 },
10767 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10768 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010769
Michael Chana71116d2005-05-29 14:58:11 -070010770 /* Mailbox Registers */
10771 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10772 0x00000000, 0x000001ff },
10773 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10774 0x00000000, 0x000001ff },
10775 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10776 0x00000000, 0x000007ff },
10777 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10778 0x00000000, 0x000001ff },
10779
10780 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10781 };
10782
Michael Chanb16250e2006-09-27 16:10:14 -070010783 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010784 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070010785 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000010786 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070010787 is_5750 = 1;
10788 }
Michael Chana71116d2005-05-29 14:58:11 -070010789
10790 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10791 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10792 continue;
10793
10794 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10795 continue;
10796
Joe Perches63c3a662011-04-26 08:12:10 +000010797 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070010798 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10799 continue;
10800
Michael Chanb16250e2006-09-27 16:10:14 -070010801 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10802 continue;
10803
Michael Chana71116d2005-05-29 14:58:11 -070010804 offset = (u32) reg_tbl[i].offset;
10805 read_mask = reg_tbl[i].read_mask;
10806 write_mask = reg_tbl[i].write_mask;
10807
10808 /* Save the original register content */
10809 save_val = tr32(offset);
10810
10811 /* Determine the read-only value. */
10812 read_val = save_val & read_mask;
10813
10814 /* Write zero to the register, then make sure the read-only bits
10815 * are not changed and the read/write bits are all zeros.
10816 */
10817 tw32(offset, 0);
10818
10819 val = tr32(offset);
10820
10821 /* Test the read-only and read/write bits. */
10822 if (((val & read_mask) != read_val) || (val & write_mask))
10823 goto out;
10824
10825 /* Write ones to all the bits defined by RdMask and WrMask, then
10826 * make sure the read-only bits are not changed and the
10827 * read/write bits are all ones.
10828 */
10829 tw32(offset, read_mask | write_mask);
10830
10831 val = tr32(offset);
10832
10833 /* Test the read-only bits. */
10834 if ((val & read_mask) != read_val)
10835 goto out;
10836
10837 /* Test the read/write bits. */
10838 if ((val & write_mask) != write_mask)
10839 goto out;
10840
10841 tw32(offset, save_val);
10842 }
10843
10844 return 0;
10845
10846out:
Michael Chan9f88f292006-12-07 00:22:54 -080010847 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010848 netdev_err(tp->dev,
10849 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010850 tw32(offset, save_val);
10851 return -EIO;
10852}
10853
Michael Chan7942e1d2005-05-29 14:58:36 -070010854static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10855{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010856 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010857 int i;
10858 u32 j;
10859
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010860 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010861 for (j = 0; j < len; j += 4) {
10862 u32 val;
10863
10864 tg3_write_mem(tp, offset + j, test_pattern[i]);
10865 tg3_read_mem(tp, offset + j, &val);
10866 if (val != test_pattern[i])
10867 return -EIO;
10868 }
10869 }
10870 return 0;
10871}
10872
10873static int tg3_test_memory(struct tg3 *tp)
10874{
10875 static struct mem_entry {
10876 u32 offset;
10877 u32 len;
10878 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010879 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010880 { 0x00002000, 0x1c000},
10881 { 0xffffffff, 0x00000}
10882 }, mem_tbl_5705[] = {
10883 { 0x00000100, 0x0000c},
10884 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010885 { 0x00004000, 0x00800},
10886 { 0x00006000, 0x01000},
10887 { 0x00008000, 0x02000},
10888 { 0x00010000, 0x0e000},
10889 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010890 }, mem_tbl_5755[] = {
10891 { 0x00000200, 0x00008},
10892 { 0x00004000, 0x00800},
10893 { 0x00006000, 0x00800},
10894 { 0x00008000, 0x02000},
10895 { 0x00010000, 0x0c000},
10896 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010897 }, mem_tbl_5906[] = {
10898 { 0x00000200, 0x00008},
10899 { 0x00004000, 0x00400},
10900 { 0x00006000, 0x00400},
10901 { 0x00008000, 0x01000},
10902 { 0x00010000, 0x01000},
10903 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010904 }, mem_tbl_5717[] = {
10905 { 0x00000200, 0x00008},
10906 { 0x00010000, 0x0a000},
10907 { 0x00020000, 0x13c00},
10908 { 0xffffffff, 0x00000}
10909 }, mem_tbl_57765[] = {
10910 { 0x00000200, 0x00008},
10911 { 0x00004000, 0x00800},
10912 { 0x00006000, 0x09800},
10913 { 0x00010000, 0x0a000},
10914 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010915 };
10916 struct mem_entry *mem_tbl;
10917 int err = 0;
10918 int i;
10919
Joe Perches63c3a662011-04-26 08:12:10 +000010920 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010921 mem_tbl = mem_tbl_5717;
10922 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10923 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000010924 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080010925 mem_tbl = mem_tbl_5755;
10926 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10927 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000010928 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080010929 mem_tbl = mem_tbl_5705;
10930 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010931 mem_tbl = mem_tbl_570x;
10932
10933 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000010934 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
10935 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070010936 break;
10937 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010938
Michael Chan7942e1d2005-05-29 14:58:36 -070010939 return err;
10940}
10941
Michael Chan9f40dea2005-09-05 17:53:06 -070010942#define TG3_MAC_LOOPBACK 0
10943#define TG3_PHY_LOOPBACK 1
Matt Carlsonbb158d62011-04-25 12:42:47 +000010944#define TG3_TSO_LOOPBACK 2
10945
10946#define TG3_TSO_MSS 500
10947
10948#define TG3_TSO_IP_HDR_LEN 20
10949#define TG3_TSO_TCP_HDR_LEN 20
10950#define TG3_TSO_TCP_OPT_LEN 12
10951
10952static const u8 tg3_tso_header[] = {
109530x08, 0x00,
109540x45, 0x00, 0x00, 0x00,
109550x00, 0x00, 0x40, 0x00,
109560x40, 0x06, 0x00, 0x00,
109570x0a, 0x00, 0x00, 0x01,
109580x0a, 0x00, 0x00, 0x02,
109590x0d, 0x00, 0xe0, 0x00,
109600x00, 0x00, 0x01, 0x00,
109610x00, 0x00, 0x02, 0x00,
109620x80, 0x10, 0x10, 0x00,
109630x14, 0x09, 0x00, 0x00,
109640x01, 0x01, 0x08, 0x0a,
109650x11, 0x11, 0x11, 0x11,
109660x11, 0x11, 0x11, 0x11,
10967};
Michael Chan9f40dea2005-09-05 17:53:06 -070010968
Matt Carlson4852a862011-04-13 11:05:07 +000010969static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010970{
Michael Chan9f40dea2005-09-05 17:53:06 -070010971 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000010972 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Michael Chanc76949a2005-05-29 14:58:59 -070010973 struct sk_buff *skb, *rx_skb;
10974 u8 *tx_data;
10975 dma_addr_t map;
10976 int num_pkts, tx_len, rx_len, i, err;
10977 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010978 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000010979 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070010980
Matt Carlsonc8873402010-02-12 14:47:11 +000010981 tnapi = &tp->napi[0];
10982 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010983 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000010984 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000010985 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000010986 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000010987 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010988 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010989 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010990
Michael Chan9f40dea2005-09-05 17:53:06 -070010991 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010992 /* HW errata - mac loopback fails in some cases on 5780.
10993 * Normal traffic and PHY loopback are not affected by
Matt Carlsonaba49f22011-01-25 15:58:53 +000010994 * errata. Also, the MAC loopback test is deprecated for
10995 * all newer ASIC revisions.
Michael Chanc94e3942005-09-27 12:12:42 -070010996 */
Matt Carlsonaba49f22011-01-25 15:58:53 +000010997 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010998 tg3_flag(tp, CPMU_PRESENT))
Michael Chanc94e3942005-09-27 12:12:42 -070010999 return 0;
11000
Matt Carlson49692ca2011-01-25 15:58:52 +000011001 mac_mode = tp->mac_mode &
11002 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
11003 mac_mode |= MAC_MODE_PORT_INT_LPBACK;
Joe Perches63c3a662011-04-26 08:12:10 +000011004 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011005 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011006 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070011007 mac_mode |= MAC_MODE_PORT_MODE_MII;
11008 else
11009 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070011010 tw32(MAC_MODE, mac_mode);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011011 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011012 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000011013 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080011014 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
11015 } else
11016 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070011017
Matt Carlson9ef8ca92007-07-11 19:48:29 -070011018 tg3_phy_toggle_automdix(tp, 0);
11019
Michael Chan3f7045c2006-09-27 16:02:29 -070011020 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070011021 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080011022
Matt Carlson49692ca2011-01-25 15:58:52 +000011023 mac_mode = tp->mac_mode &
11024 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011025 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000011026 tg3_writephy(tp, MII_TG3_FET_PTEST,
11027 MII_TG3_FET_PTEST_FRC_TX_LINK |
11028 MII_TG3_FET_PTEST_FRC_TX_LOCK);
11029 /* The write needs to be flushed for the AC131 */
11030 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11031 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080011032 mac_mode |= MAC_MODE_PORT_MODE_MII;
11033 } else
11034 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070011035
Michael Chanc94e3942005-09-27 12:12:42 -070011036 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011037 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070011038 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
11039 udelay(10);
11040 tw32_f(MAC_RX_MODE, tp->rx_mode);
11041 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011042 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000011043 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
11044 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011045 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000011046 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011047 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080011048 tg3_writephy(tp, MII_TG3_EXT_CTRL,
11049 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
11050 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011051 tw32(MAC_MODE, mac_mode);
Matt Carlson49692ca2011-01-25 15:58:52 +000011052
11053 /* Wait for link */
11054 for (i = 0; i < 100; i++) {
11055 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11056 break;
11057 mdelay(1);
11058 }
Matt Carlson859a588792010-04-05 10:19:28 +000011059 }
Michael Chanc76949a2005-05-29 14:58:59 -070011060
11061 err = -EIO;
11062
Matt Carlson4852a862011-04-13 11:05:07 +000011063 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011064 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011065 if (!skb)
11066 return -ENOMEM;
11067
Michael Chanc76949a2005-05-29 14:58:59 -070011068 tx_data = skb_put(skb, tx_len);
11069 memcpy(tx_data, tp->dev->dev_addr, 6);
11070 memset(tx_data + 6, 0x0, 8);
11071
Matt Carlson4852a862011-04-13 11:05:07 +000011072 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011073
Matt Carlsonbb158d62011-04-25 12:42:47 +000011074 if (loopback_mode == TG3_TSO_LOOPBACK) {
11075 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11076
11077 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11078 TG3_TSO_TCP_OPT_LEN;
11079
11080 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11081 sizeof(tg3_tso_header));
11082 mss = TG3_TSO_MSS;
11083
11084 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11085 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11086
11087 /* Set the total length field in the IP header */
11088 iph->tot_len = htons((u16)(mss + hdr_len));
11089
11090 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11091 TXD_FLAG_CPU_POST_DMA);
11092
Joe Perches63c3a662011-04-26 08:12:10 +000011093 if (tg3_flag(tp, HW_TSO_1) ||
11094 tg3_flag(tp, HW_TSO_2) ||
11095 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011096 struct tcphdr *th;
11097 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11098 th = (struct tcphdr *)&tx_data[val];
11099 th->check = 0;
11100 } else
11101 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11102
Joe Perches63c3a662011-04-26 08:12:10 +000011103 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011104 mss |= (hdr_len & 0xc) << 12;
11105 if (hdr_len & 0x10)
11106 base_flags |= 0x00000010;
11107 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011108 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011109 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011110 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011111 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11112 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11113 } else {
11114 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11115 }
11116
11117 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11118 } else {
11119 num_pkts = 1;
11120 data_off = ETH_HLEN;
11121 }
11122
11123 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011124 tx_data[i] = (u8) (i & 0xff);
11125
Alexander Duyckf4188d82009-12-02 16:48:38 +000011126 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11127 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011128 dev_kfree_skb(skb);
11129 return -EIO;
11130 }
Michael Chanc76949a2005-05-29 14:58:59 -070011131
11132 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011133 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011134
11135 udelay(10);
11136
Matt Carlson898a56f2009-08-28 14:02:40 +000011137 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011138
Matt Carlsonbb158d62011-04-25 12:42:47 +000011139 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len,
11140 base_flags, (mss << 1) | 1);
Michael Chanc76949a2005-05-29 14:58:59 -070011141
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011142 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011143
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011144 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11145 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011146
11147 udelay(10);
11148
Matt Carlson303fc922009-11-02 14:27:34 +000011149 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11150 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011151 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011152 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011153
11154 udelay(10);
11155
Matt Carlson898a56f2009-08-28 14:02:40 +000011156 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11157 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011158 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011159 (rx_idx == (rx_start_idx + num_pkts)))
11160 break;
11161 }
11162
Alexander Duyckf4188d82009-12-02 16:48:38 +000011163 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070011164 dev_kfree_skb(skb);
11165
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011166 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011167 goto out;
11168
11169 if (rx_idx != rx_start_idx + num_pkts)
11170 goto out;
11171
Matt Carlsonbb158d62011-04-25 12:42:47 +000011172 val = data_off;
11173 while (rx_idx != rx_start_idx) {
11174 desc = &rnapi->rx_rcb[rx_start_idx++];
11175 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11176 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011177
Matt Carlsonbb158d62011-04-25 12:42:47 +000011178 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11179 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011180 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011181
Matt Carlsonbb158d62011-04-25 12:42:47 +000011182 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11183 - ETH_FCS_LEN;
11184
11185 if (loopback_mode != TG3_TSO_LOOPBACK) {
11186 if (rx_len != tx_len)
11187 goto out;
11188
11189 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11190 if (opaque_key != RXD_OPAQUE_RING_STD)
11191 goto out;
11192 } else {
11193 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11194 goto out;
11195 }
11196 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11197 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
11198 >> RXD_TCPCSUM_SHIFT == 0xffff) {
11199 goto out;
11200 }
11201
11202 if (opaque_key == RXD_OPAQUE_RING_STD) {
11203 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
11204 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11205 mapping);
11206 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
11207 rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
11208 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11209 mapping);
11210 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011211 goto out;
11212
Matt Carlsonbb158d62011-04-25 12:42:47 +000011213 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11214 PCI_DMA_FROMDEVICE);
11215
11216 for (i = data_off; i < rx_len; i++, val++) {
11217 if (*(rx_skb->data + i) != (u8) (val & 0xff))
11218 goto out;
11219 }
Matt Carlson4852a862011-04-13 11:05:07 +000011220 }
11221
Michael Chanc76949a2005-05-29 14:58:59 -070011222 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011223
Michael Chanc76949a2005-05-29 14:58:59 -070011224 /* tg3_free_rings will unmap and free the rx_skb */
11225out:
11226 return err;
11227}
11228
Matt Carlson00c266b2011-04-25 12:42:46 +000011229#define TG3_STD_LOOPBACK_FAILED 1
11230#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011231#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson00c266b2011-04-25 12:42:46 +000011232
11233#define TG3_MAC_LOOPBACK_SHIFT 0
11234#define TG3_PHY_LOOPBACK_SHIFT 4
Matt Carlsonbb158d62011-04-25 12:42:47 +000011235#define TG3_LOOPBACK_FAILED 0x00000077
Michael Chan9f40dea2005-09-05 17:53:06 -070011236
11237static int tg3_test_loopback(struct tg3 *tp)
11238{
11239 int err = 0;
Matt Carlsonab789042011-01-25 15:58:54 +000011240 u32 eee_cap, cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070011241
11242 if (!netif_running(tp->dev))
11243 return TG3_LOOPBACK_FAILED;
11244
Matt Carlsonab789042011-01-25 15:58:54 +000011245 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11246 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11247
Michael Chanb9ec6c12006-07-25 16:37:27 -070011248 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011249 if (err) {
11250 err = TG3_LOOPBACK_FAILED;
11251 goto done;
11252 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011253
Joe Perches63c3a662011-04-26 08:12:10 +000011254 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011255 int i;
11256
11257 /* Reroute all rx packets to the 1st queue */
11258 for (i = MAC_RSS_INDIR_TBL_0;
11259 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11260 tw32(i, 0x0);
11261 }
11262
Matt Carlson6833c042008-11-21 17:18:59 -080011263 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011264 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011265 tg3_phy_toggle_apd(tp, false);
11266
Joe Perches63c3a662011-04-26 08:12:10 +000011267 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011268 int i;
11269 u32 status;
11270
11271 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11272
11273 /* Wait for up to 40 microseconds to acquire lock. */
11274 for (i = 0; i < 4; i++) {
11275 status = tr32(TG3_CPMU_MUTEX_GNT);
11276 if (status == CPMU_MUTEX_GNT_DRIVER)
11277 break;
11278 udelay(10);
11279 }
11280
Matt Carlsonab789042011-01-25 15:58:54 +000011281 if (status != CPMU_MUTEX_GNT_DRIVER) {
11282 err = TG3_LOOPBACK_FAILED;
11283 goto done;
11284 }
Matt Carlson9936bcf2007-10-10 18:03:07 -070011285
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011286 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080011287 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070011288 tw32(TG3_CPMU_CTRL,
11289 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11290 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070011291 }
11292
Matt Carlson4852a862011-04-13 11:05:07 +000011293 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011294 err |= TG3_STD_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011295
Joe Perches63c3a662011-04-26 08:12:10 +000011296 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011297 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011298 err |= TG3_JMB_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson4852a862011-04-13 11:05:07 +000011299
Joe Perches63c3a662011-04-26 08:12:10 +000011300 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011301 tw32(TG3_CPMU_CTRL, cpmuctrl);
11302
11303 /* Release the mutex */
11304 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11305 }
11306
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011307 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011308 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson4852a862011-04-13 11:05:07 +000011309 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011310 err |= TG3_STD_LOOPBACK_FAILED <<
11311 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011312 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonbb158d62011-04-25 12:42:47 +000011313 tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_TSO_LOOPBACK))
11314 err |= TG3_TSO_LOOPBACK_FAILED <<
11315 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011316 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011317 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011318 err |= TG3_JMB_LOOPBACK_FAILED <<
11319 TG3_PHY_LOOPBACK_SHIFT;
Michael Chan9f40dea2005-09-05 17:53:06 -070011320 }
11321
Matt Carlson6833c042008-11-21 17:18:59 -080011322 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011323 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011324 tg3_phy_toggle_apd(tp, true);
11325
Matt Carlsonab789042011-01-25 15:58:54 +000011326done:
11327 tp->phy_flags |= eee_cap;
11328
Michael Chan9f40dea2005-09-05 17:53:06 -070011329 return err;
11330}
11331
Michael Chan4cafd3f2005-05-29 14:56:34 -070011332static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11333 u64 *data)
11334{
Michael Chan566f86a2005-05-29 14:56:58 -070011335 struct tg3 *tp = netdev_priv(dev);
11336
Matt Carlson80096062010-08-02 11:26:06 +000011337 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011338 tg3_power_up(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011339
Michael Chan566f86a2005-05-29 14:56:58 -070011340 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11341
11342 if (tg3_test_nvram(tp) != 0) {
11343 etest->flags |= ETH_TEST_FL_FAILED;
11344 data[0] = 1;
11345 }
Michael Chanca430072005-05-29 14:57:23 -070011346 if (tg3_test_link(tp) != 0) {
11347 etest->flags |= ETH_TEST_FL_FAILED;
11348 data[1] = 1;
11349 }
Michael Chana71116d2005-05-29 14:58:11 -070011350 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011351 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011352
Michael Chanbbe832c2005-06-24 20:20:04 -070011353 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011354 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011355 tg3_netif_stop(tp);
11356 irq_sync = 1;
11357 }
11358
11359 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011360
11361 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011362 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011363 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011364 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011365 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011366 if (!err)
11367 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011368
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011369 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080011370 tg3_phy_reset(tp);
11371
Michael Chana71116d2005-05-29 14:58:11 -070011372 if (tg3_test_registers(tp) != 0) {
11373 etest->flags |= ETH_TEST_FL_FAILED;
11374 data[2] = 1;
11375 }
Michael Chan7942e1d2005-05-29 14:58:36 -070011376 if (tg3_test_memory(tp) != 0) {
11377 etest->flags |= ETH_TEST_FL_FAILED;
11378 data[3] = 1;
11379 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011380 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070011381 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011382
David S. Millerf47c11e2005-06-24 20:18:35 -070011383 tg3_full_unlock(tp);
11384
Michael Chand4bc3922005-05-29 14:59:20 -070011385 if (tg3_test_interrupt(tp) != 0) {
11386 etest->flags |= ETH_TEST_FL_FAILED;
11387 data[5] = 1;
11388 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011389
11390 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011391
Michael Chana71116d2005-05-29 14:58:11 -070011392 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11393 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011394 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011395 err2 = tg3_restart_hw(tp, 1);
11396 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011397 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011398 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011399
11400 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011401
11402 if (irq_sync && !err2)
11403 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011404 }
Matt Carlson80096062010-08-02 11:26:06 +000011405 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011406 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011407
Michael Chan4cafd3f2005-05-29 14:56:34 -070011408}
11409
Linus Torvalds1da177e2005-04-16 15:20:36 -070011410static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11411{
11412 struct mii_ioctl_data *data = if_mii(ifr);
11413 struct tg3 *tp = netdev_priv(dev);
11414 int err;
11415
Joe Perches63c3a662011-04-26 08:12:10 +000011416 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011417 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011418 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011419 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011420 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011421 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011422 }
11423
Matt Carlson33f401a2010-04-05 10:19:27 +000011424 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011425 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011426 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011427
11428 /* fallthru */
11429 case SIOCGMIIREG: {
11430 u32 mii_regval;
11431
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011432 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011433 break; /* We have no PHY */
11434
Matt Carlson34eea5a2011-04-20 07:57:38 +000011435 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011436 return -EAGAIN;
11437
David S. Millerf47c11e2005-06-24 20:18:35 -070011438 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011439 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011440 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011441
11442 data->val_out = mii_regval;
11443
11444 return err;
11445 }
11446
11447 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011448 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011449 break; /* We have no PHY */
11450
Matt Carlson34eea5a2011-04-20 07:57:38 +000011451 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011452 return -EAGAIN;
11453
David S. Millerf47c11e2005-06-24 20:18:35 -070011454 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011455 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011456 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011457
11458 return err;
11459
11460 default:
11461 /* do nothing */
11462 break;
11463 }
11464 return -EOPNOTSUPP;
11465}
11466
David S. Miller15f98502005-05-18 22:49:26 -070011467static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11468{
11469 struct tg3 *tp = netdev_priv(dev);
11470
11471 memcpy(ec, &tp->coal, sizeof(*ec));
11472 return 0;
11473}
11474
Michael Chand244c892005-07-05 14:42:33 -070011475static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11476{
11477 struct tg3 *tp = netdev_priv(dev);
11478 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11479 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11480
Joe Perches63c3a662011-04-26 08:12:10 +000011481 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011482 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11483 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11484 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11485 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11486 }
11487
11488 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11489 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11490 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11491 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11492 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11493 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11494 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11495 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11496 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11497 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11498 return -EINVAL;
11499
11500 /* No rx interrupts will be generated if both are zero */
11501 if ((ec->rx_coalesce_usecs == 0) &&
11502 (ec->rx_max_coalesced_frames == 0))
11503 return -EINVAL;
11504
11505 /* No tx interrupts will be generated if both are zero */
11506 if ((ec->tx_coalesce_usecs == 0) &&
11507 (ec->tx_max_coalesced_frames == 0))
11508 return -EINVAL;
11509
11510 /* Only copy relevant parameters, ignore all others. */
11511 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11512 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11513 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11514 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11515 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11516 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11517 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11518 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11519 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11520
11521 if (netif_running(dev)) {
11522 tg3_full_lock(tp, 0);
11523 __tg3_set_coalesce(tp, &tp->coal);
11524 tg3_full_unlock(tp);
11525 }
11526 return 0;
11527}
11528
Jeff Garzik7282d492006-09-13 14:30:00 -040011529static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011530 .get_settings = tg3_get_settings,
11531 .set_settings = tg3_set_settings,
11532 .get_drvinfo = tg3_get_drvinfo,
11533 .get_regs_len = tg3_get_regs_len,
11534 .get_regs = tg3_get_regs,
11535 .get_wol = tg3_get_wol,
11536 .set_wol = tg3_set_wol,
11537 .get_msglevel = tg3_get_msglevel,
11538 .set_msglevel = tg3_set_msglevel,
11539 .nway_reset = tg3_nway_reset,
11540 .get_link = ethtool_op_get_link,
11541 .get_eeprom_len = tg3_get_eeprom_len,
11542 .get_eeprom = tg3_get_eeprom,
11543 .set_eeprom = tg3_set_eeprom,
11544 .get_ringparam = tg3_get_ringparam,
11545 .set_ringparam = tg3_set_ringparam,
11546 .get_pauseparam = tg3_get_pauseparam,
11547 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011548 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011549 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011550 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011551 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011552 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011553 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011554 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011555};
11556
11557static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11558{
Michael Chan1b277772006-03-20 22:27:48 -080011559 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011560
11561 tp->nvram_size = EEPROM_CHIP_SIZE;
11562
Matt Carlsone4f34112009-02-25 14:25:00 +000011563 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011564 return;
11565
Michael Chanb16250e2006-09-27 16:10:14 -070011566 if ((magic != TG3_EEPROM_MAGIC) &&
11567 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11568 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011569 return;
11570
11571 /*
11572 * Size the chip by reading offsets at increasing powers of two.
11573 * When we encounter our validation signature, we know the addressing
11574 * has wrapped around, and thus have our chip size.
11575 */
Michael Chan1b277772006-03-20 22:27:48 -080011576 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011577
11578 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011579 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011580 return;
11581
Michael Chan18201802006-03-20 22:29:15 -080011582 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011583 break;
11584
11585 cursize <<= 1;
11586 }
11587
11588 tp->nvram_size = cursize;
11589}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011590
Linus Torvalds1da177e2005-04-16 15:20:36 -070011591static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11592{
11593 u32 val;
11594
Joe Perches63c3a662011-04-26 08:12:10 +000011595 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011596 return;
11597
11598 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011599 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011600 tg3_get_eeprom_size(tp);
11601 return;
11602 }
11603
Matt Carlson6d348f22009-02-25 14:25:52 +000011604 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011605 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011606 /* This is confusing. We want to operate on the
11607 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11608 * call will read from NVRAM and byteswap the data
11609 * according to the byteswapping settings for all
11610 * other register accesses. This ensures the data we
11611 * want will always reside in the lower 16-bits.
11612 * However, the data in NVRAM is in LE format, which
11613 * means the data from the NVRAM read will always be
11614 * opposite the endianness of the CPU. The 16-bit
11615 * byteswap then brings the data to CPU endianness.
11616 */
11617 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011618 return;
11619 }
11620 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011621 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011622}
11623
11624static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11625{
11626 u32 nvcfg1;
11627
11628 nvcfg1 = tr32(NVRAM_CFG1);
11629 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000011630 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011631 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011632 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11633 tw32(NVRAM_CFG1, nvcfg1);
11634 }
11635
Michael Chan4c987482005-09-05 17:52:38 -070011636 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Joe Perches63c3a662011-04-26 08:12:10 +000011637 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011638 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011639 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11640 tp->nvram_jedecnum = JEDEC_ATMEL;
11641 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011642 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011643 break;
11644 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11645 tp->nvram_jedecnum = JEDEC_ATMEL;
11646 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11647 break;
11648 case FLASH_VENDOR_ATMEL_EEPROM:
11649 tp->nvram_jedecnum = JEDEC_ATMEL;
11650 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011651 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011652 break;
11653 case FLASH_VENDOR_ST:
11654 tp->nvram_jedecnum = JEDEC_ST;
11655 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011656 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011657 break;
11658 case FLASH_VENDOR_SAIFUN:
11659 tp->nvram_jedecnum = JEDEC_SAIFUN;
11660 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11661 break;
11662 case FLASH_VENDOR_SST_SMALL:
11663 case FLASH_VENDOR_SST_LARGE:
11664 tp->nvram_jedecnum = JEDEC_SST;
11665 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11666 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011667 }
Matt Carlson8590a602009-08-28 12:29:16 +000011668 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011669 tp->nvram_jedecnum = JEDEC_ATMEL;
11670 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011671 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011672 }
11673}
11674
Matt Carlsona1b950d2009-09-01 13:20:17 +000011675static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11676{
11677 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11678 case FLASH_5752PAGE_SIZE_256:
11679 tp->nvram_pagesize = 256;
11680 break;
11681 case FLASH_5752PAGE_SIZE_512:
11682 tp->nvram_pagesize = 512;
11683 break;
11684 case FLASH_5752PAGE_SIZE_1K:
11685 tp->nvram_pagesize = 1024;
11686 break;
11687 case FLASH_5752PAGE_SIZE_2K:
11688 tp->nvram_pagesize = 2048;
11689 break;
11690 case FLASH_5752PAGE_SIZE_4K:
11691 tp->nvram_pagesize = 4096;
11692 break;
11693 case FLASH_5752PAGE_SIZE_264:
11694 tp->nvram_pagesize = 264;
11695 break;
11696 case FLASH_5752PAGE_SIZE_528:
11697 tp->nvram_pagesize = 528;
11698 break;
11699 }
11700}
11701
Michael Chan361b4ac2005-04-21 17:11:21 -070011702static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11703{
11704 u32 nvcfg1;
11705
11706 nvcfg1 = tr32(NVRAM_CFG1);
11707
Michael Chane6af3012005-04-21 17:12:05 -070011708 /* NVRAM protection for TPM */
11709 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000011710 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070011711
Michael Chan361b4ac2005-04-21 17:11:21 -070011712 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011713 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11714 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11715 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011716 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011717 break;
11718 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11719 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011720 tg3_flag_set(tp, NVRAM_BUFFERED);
11721 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011722 break;
11723 case FLASH_5752VENDOR_ST_M45PE10:
11724 case FLASH_5752VENDOR_ST_M45PE20:
11725 case FLASH_5752VENDOR_ST_M45PE40:
11726 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011727 tg3_flag_set(tp, NVRAM_BUFFERED);
11728 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011729 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011730 }
11731
Joe Perches63c3a662011-04-26 08:12:10 +000011732 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011733 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011734 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011735 /* For eeprom, set pagesize to maximum eeprom size */
11736 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11737
11738 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11739 tw32(NVRAM_CFG1, nvcfg1);
11740 }
11741}
11742
Michael Chand3c7b882006-03-23 01:28:25 -080011743static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11744{
Matt Carlson989a9d22007-05-05 11:51:05 -070011745 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011746
11747 nvcfg1 = tr32(NVRAM_CFG1);
11748
11749 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011750 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011751 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070011752 protect = 1;
11753 }
Michael Chand3c7b882006-03-23 01:28:25 -080011754
Matt Carlson989a9d22007-05-05 11:51:05 -070011755 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11756 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011757 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11758 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11759 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11760 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11761 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011762 tg3_flag_set(tp, NVRAM_BUFFERED);
11763 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011764 tp->nvram_pagesize = 264;
11765 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11766 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11767 tp->nvram_size = (protect ? 0x3e200 :
11768 TG3_NVRAM_SIZE_512KB);
11769 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11770 tp->nvram_size = (protect ? 0x1f200 :
11771 TG3_NVRAM_SIZE_256KB);
11772 else
11773 tp->nvram_size = (protect ? 0x1f200 :
11774 TG3_NVRAM_SIZE_128KB);
11775 break;
11776 case FLASH_5752VENDOR_ST_M45PE10:
11777 case FLASH_5752VENDOR_ST_M45PE20:
11778 case FLASH_5752VENDOR_ST_M45PE40:
11779 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011780 tg3_flag_set(tp, NVRAM_BUFFERED);
11781 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011782 tp->nvram_pagesize = 256;
11783 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11784 tp->nvram_size = (protect ?
11785 TG3_NVRAM_SIZE_64KB :
11786 TG3_NVRAM_SIZE_128KB);
11787 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11788 tp->nvram_size = (protect ?
11789 TG3_NVRAM_SIZE_64KB :
11790 TG3_NVRAM_SIZE_256KB);
11791 else
11792 tp->nvram_size = (protect ?
11793 TG3_NVRAM_SIZE_128KB :
11794 TG3_NVRAM_SIZE_512KB);
11795 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011796 }
11797}
11798
Michael Chan1b277772006-03-20 22:27:48 -080011799static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11800{
11801 u32 nvcfg1;
11802
11803 nvcfg1 = tr32(NVRAM_CFG1);
11804
11805 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011806 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11807 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11808 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11809 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11810 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011811 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011812 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011813
Matt Carlson8590a602009-08-28 12:29:16 +000011814 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11815 tw32(NVRAM_CFG1, nvcfg1);
11816 break;
11817 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11818 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11819 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11820 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11821 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011822 tg3_flag_set(tp, NVRAM_BUFFERED);
11823 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011824 tp->nvram_pagesize = 264;
11825 break;
11826 case FLASH_5752VENDOR_ST_M45PE10:
11827 case FLASH_5752VENDOR_ST_M45PE20:
11828 case FLASH_5752VENDOR_ST_M45PE40:
11829 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011830 tg3_flag_set(tp, NVRAM_BUFFERED);
11831 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011832 tp->nvram_pagesize = 256;
11833 break;
Michael Chan1b277772006-03-20 22:27:48 -080011834 }
11835}
11836
Matt Carlson6b91fa02007-10-10 18:01:09 -070011837static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11838{
11839 u32 nvcfg1, protect = 0;
11840
11841 nvcfg1 = tr32(NVRAM_CFG1);
11842
11843 /* NVRAM protection for TPM */
11844 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011845 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011846 protect = 1;
11847 }
11848
11849 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11850 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011851 case FLASH_5761VENDOR_ATMEL_ADB021D:
11852 case FLASH_5761VENDOR_ATMEL_ADB041D:
11853 case FLASH_5761VENDOR_ATMEL_ADB081D:
11854 case FLASH_5761VENDOR_ATMEL_ADB161D:
11855 case FLASH_5761VENDOR_ATMEL_MDB021D:
11856 case FLASH_5761VENDOR_ATMEL_MDB041D:
11857 case FLASH_5761VENDOR_ATMEL_MDB081D:
11858 case FLASH_5761VENDOR_ATMEL_MDB161D:
11859 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011860 tg3_flag_set(tp, NVRAM_BUFFERED);
11861 tg3_flag_set(tp, FLASH);
11862 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000011863 tp->nvram_pagesize = 256;
11864 break;
11865 case FLASH_5761VENDOR_ST_A_M45PE20:
11866 case FLASH_5761VENDOR_ST_A_M45PE40:
11867 case FLASH_5761VENDOR_ST_A_M45PE80:
11868 case FLASH_5761VENDOR_ST_A_M45PE16:
11869 case FLASH_5761VENDOR_ST_M_M45PE20:
11870 case FLASH_5761VENDOR_ST_M_M45PE40:
11871 case FLASH_5761VENDOR_ST_M_M45PE80:
11872 case FLASH_5761VENDOR_ST_M_M45PE16:
11873 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011874 tg3_flag_set(tp, NVRAM_BUFFERED);
11875 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011876 tp->nvram_pagesize = 256;
11877 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011878 }
11879
11880 if (protect) {
11881 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11882 } else {
11883 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011884 case FLASH_5761VENDOR_ATMEL_ADB161D:
11885 case FLASH_5761VENDOR_ATMEL_MDB161D:
11886 case FLASH_5761VENDOR_ST_A_M45PE16:
11887 case FLASH_5761VENDOR_ST_M_M45PE16:
11888 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11889 break;
11890 case FLASH_5761VENDOR_ATMEL_ADB081D:
11891 case FLASH_5761VENDOR_ATMEL_MDB081D:
11892 case FLASH_5761VENDOR_ST_A_M45PE80:
11893 case FLASH_5761VENDOR_ST_M_M45PE80:
11894 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11895 break;
11896 case FLASH_5761VENDOR_ATMEL_ADB041D:
11897 case FLASH_5761VENDOR_ATMEL_MDB041D:
11898 case FLASH_5761VENDOR_ST_A_M45PE40:
11899 case FLASH_5761VENDOR_ST_M_M45PE40:
11900 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11901 break;
11902 case FLASH_5761VENDOR_ATMEL_ADB021D:
11903 case FLASH_5761VENDOR_ATMEL_MDB021D:
11904 case FLASH_5761VENDOR_ST_A_M45PE20:
11905 case FLASH_5761VENDOR_ST_M_M45PE20:
11906 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11907 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011908 }
11909 }
11910}
11911
Michael Chanb5d37722006-09-27 16:06:21 -070011912static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11913{
11914 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011915 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070011916 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11917}
11918
Matt Carlson321d32a2008-11-21 17:22:19 -080011919static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11920{
11921 u32 nvcfg1;
11922
11923 nvcfg1 = tr32(NVRAM_CFG1);
11924
11925 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11926 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11927 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11928 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011929 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080011930 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11931
11932 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11933 tw32(NVRAM_CFG1, nvcfg1);
11934 return;
11935 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11936 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11937 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11938 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11939 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11940 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11941 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11942 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011943 tg3_flag_set(tp, NVRAM_BUFFERED);
11944 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080011945
11946 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11947 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11948 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11949 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11950 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11951 break;
11952 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11953 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11954 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11955 break;
11956 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11957 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11958 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11959 break;
11960 }
11961 break;
11962 case FLASH_5752VENDOR_ST_M45PE10:
11963 case FLASH_5752VENDOR_ST_M45PE20:
11964 case FLASH_5752VENDOR_ST_M45PE40:
11965 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011966 tg3_flag_set(tp, NVRAM_BUFFERED);
11967 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080011968
11969 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11970 case FLASH_5752VENDOR_ST_M45PE10:
11971 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11972 break;
11973 case FLASH_5752VENDOR_ST_M45PE20:
11974 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11975 break;
11976 case FLASH_5752VENDOR_ST_M45PE40:
11977 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11978 break;
11979 }
11980 break;
11981 default:
Joe Perches63c3a662011-04-26 08:12:10 +000011982 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080011983 return;
11984 }
11985
Matt Carlsona1b950d2009-09-01 13:20:17 +000011986 tg3_nvram_get_pagesize(tp, nvcfg1);
11987 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000011988 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000011989}
11990
11991
11992static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11993{
11994 u32 nvcfg1;
11995
11996 nvcfg1 = tr32(NVRAM_CFG1);
11997
11998 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11999 case FLASH_5717VENDOR_ATMEL_EEPROM:
12000 case FLASH_5717VENDOR_MICRO_EEPROM:
12001 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012002 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012003 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12004
12005 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12006 tw32(NVRAM_CFG1, nvcfg1);
12007 return;
12008 case FLASH_5717VENDOR_ATMEL_MDB011D:
12009 case FLASH_5717VENDOR_ATMEL_ADB011B:
12010 case FLASH_5717VENDOR_ATMEL_ADB011D:
12011 case FLASH_5717VENDOR_ATMEL_MDB021D:
12012 case FLASH_5717VENDOR_ATMEL_ADB021B:
12013 case FLASH_5717VENDOR_ATMEL_ADB021D:
12014 case FLASH_5717VENDOR_ATMEL_45USPT:
12015 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012016 tg3_flag_set(tp, NVRAM_BUFFERED);
12017 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012018
12019 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12020 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012021 /* Detect size with tg3_nvram_get_size() */
12022 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012023 case FLASH_5717VENDOR_ATMEL_ADB021B:
12024 case FLASH_5717VENDOR_ATMEL_ADB021D:
12025 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12026 break;
12027 default:
12028 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12029 break;
12030 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012031 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012032 case FLASH_5717VENDOR_ST_M_M25PE10:
12033 case FLASH_5717VENDOR_ST_A_M25PE10:
12034 case FLASH_5717VENDOR_ST_M_M45PE10:
12035 case FLASH_5717VENDOR_ST_A_M45PE10:
12036 case FLASH_5717VENDOR_ST_M_M25PE20:
12037 case FLASH_5717VENDOR_ST_A_M25PE20:
12038 case FLASH_5717VENDOR_ST_M_M45PE20:
12039 case FLASH_5717VENDOR_ST_A_M45PE20:
12040 case FLASH_5717VENDOR_ST_25USPT:
12041 case FLASH_5717VENDOR_ST_45USPT:
12042 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012043 tg3_flag_set(tp, NVRAM_BUFFERED);
12044 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012045
12046 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12047 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012048 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012049 /* Detect size with tg3_nvram_get_size() */
12050 break;
12051 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012052 case FLASH_5717VENDOR_ST_A_M45PE20:
12053 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12054 break;
12055 default:
12056 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12057 break;
12058 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012059 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012060 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012061 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012062 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012063 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012064
12065 tg3_nvram_get_pagesize(tp, nvcfg1);
12066 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012067 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012068}
12069
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012070static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12071{
12072 u32 nvcfg1, nvmpinstrp;
12073
12074 nvcfg1 = tr32(NVRAM_CFG1);
12075 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12076
12077 switch (nvmpinstrp) {
12078 case FLASH_5720_EEPROM_HD:
12079 case FLASH_5720_EEPROM_LD:
12080 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012081 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012082
12083 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12084 tw32(NVRAM_CFG1, nvcfg1);
12085 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12086 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12087 else
12088 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12089 return;
12090 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12091 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12092 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12093 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12094 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12095 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12096 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12097 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12098 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12099 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12100 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12101 case FLASH_5720VENDOR_ATMEL_45USPT:
12102 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012103 tg3_flag_set(tp, NVRAM_BUFFERED);
12104 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012105
12106 switch (nvmpinstrp) {
12107 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12108 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12109 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12110 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12111 break;
12112 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12113 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12114 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12115 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12116 break;
12117 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12118 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12119 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12120 break;
12121 default:
12122 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12123 break;
12124 }
12125 break;
12126 case FLASH_5720VENDOR_M_ST_M25PE10:
12127 case FLASH_5720VENDOR_M_ST_M45PE10:
12128 case FLASH_5720VENDOR_A_ST_M25PE10:
12129 case FLASH_5720VENDOR_A_ST_M45PE10:
12130 case FLASH_5720VENDOR_M_ST_M25PE20:
12131 case FLASH_5720VENDOR_M_ST_M45PE20:
12132 case FLASH_5720VENDOR_A_ST_M25PE20:
12133 case FLASH_5720VENDOR_A_ST_M45PE20:
12134 case FLASH_5720VENDOR_M_ST_M25PE40:
12135 case FLASH_5720VENDOR_M_ST_M45PE40:
12136 case FLASH_5720VENDOR_A_ST_M25PE40:
12137 case FLASH_5720VENDOR_A_ST_M45PE40:
12138 case FLASH_5720VENDOR_M_ST_M25PE80:
12139 case FLASH_5720VENDOR_M_ST_M45PE80:
12140 case FLASH_5720VENDOR_A_ST_M25PE80:
12141 case FLASH_5720VENDOR_A_ST_M45PE80:
12142 case FLASH_5720VENDOR_ST_25USPT:
12143 case FLASH_5720VENDOR_ST_45USPT:
12144 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012145 tg3_flag_set(tp, NVRAM_BUFFERED);
12146 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012147
12148 switch (nvmpinstrp) {
12149 case FLASH_5720VENDOR_M_ST_M25PE20:
12150 case FLASH_5720VENDOR_M_ST_M45PE20:
12151 case FLASH_5720VENDOR_A_ST_M25PE20:
12152 case FLASH_5720VENDOR_A_ST_M45PE20:
12153 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12154 break;
12155 case FLASH_5720VENDOR_M_ST_M25PE40:
12156 case FLASH_5720VENDOR_M_ST_M45PE40:
12157 case FLASH_5720VENDOR_A_ST_M25PE40:
12158 case FLASH_5720VENDOR_A_ST_M45PE40:
12159 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12160 break;
12161 case FLASH_5720VENDOR_M_ST_M25PE80:
12162 case FLASH_5720VENDOR_M_ST_M45PE80:
12163 case FLASH_5720VENDOR_A_ST_M25PE80:
12164 case FLASH_5720VENDOR_A_ST_M45PE80:
12165 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12166 break;
12167 default:
12168 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12169 break;
12170 }
12171 break;
12172 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012173 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012174 return;
12175 }
12176
12177 tg3_nvram_get_pagesize(tp, nvcfg1);
12178 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012179 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012180}
12181
Linus Torvalds1da177e2005-04-16 15:20:36 -070012182/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12183static void __devinit tg3_nvram_init(struct tg3 *tp)
12184{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012185 tw32_f(GRC_EEPROM_ADDR,
12186 (EEPROM_ADDR_FSM_RESET |
12187 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12188 EEPROM_ADDR_CLKPERD_SHIFT)));
12189
Michael Chan9d57f012006-12-07 00:23:25 -080012190 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012191
12192 /* Enable seeprom accesses. */
12193 tw32_f(GRC_LOCAL_CTRL,
12194 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12195 udelay(100);
12196
12197 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12198 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012199 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012200
Michael Chanec41c7d2006-01-17 02:40:55 -080012201 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012202 netdev_warn(tp->dev,
12203 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012204 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012205 return;
12206 }
Michael Chane6af3012005-04-21 17:12:05 -070012207 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012208
Matt Carlson989a9d22007-05-05 11:51:05 -070012209 tp->nvram_size = 0;
12210
Michael Chan361b4ac2005-04-21 17:11:21 -070012211 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12212 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012213 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12214 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012215 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012216 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12217 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012218 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012219 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12220 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012221 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12222 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012223 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12224 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012225 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012226 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12227 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012228 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012229 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12230 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012231 else
12232 tg3_get_nvram_info(tp);
12233
Matt Carlson989a9d22007-05-05 11:51:05 -070012234 if (tp->nvram_size == 0)
12235 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012236
Michael Chane6af3012005-04-21 17:12:05 -070012237 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012238 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012239
12240 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012241 tg3_flag_clear(tp, NVRAM);
12242 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012243
12244 tg3_get_eeprom_size(tp);
12245 }
12246}
12247
Linus Torvalds1da177e2005-04-16 15:20:36 -070012248static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12249 u32 offset, u32 len, u8 *buf)
12250{
12251 int i, j, rc = 0;
12252 u32 val;
12253
12254 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012255 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012256 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012257
12258 addr = offset + i;
12259
12260 memcpy(&data, buf + i, 4);
12261
Matt Carlson62cedd12009-04-20 14:52:29 -070012262 /*
12263 * The SEEPROM interface expects the data to always be opposite
12264 * the native endian format. We accomplish this by reversing
12265 * all the operations that would have been performed on the
12266 * data from a call to tg3_nvram_read_be32().
12267 */
12268 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012269
12270 val = tr32(GRC_EEPROM_ADDR);
12271 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12272
12273 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12274 EEPROM_ADDR_READ);
12275 tw32(GRC_EEPROM_ADDR, val |
12276 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12277 (addr & EEPROM_ADDR_ADDR_MASK) |
12278 EEPROM_ADDR_START |
12279 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012280
Michael Chan9d57f012006-12-07 00:23:25 -080012281 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012282 val = tr32(GRC_EEPROM_ADDR);
12283
12284 if (val & EEPROM_ADDR_COMPLETE)
12285 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012286 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012287 }
12288 if (!(val & EEPROM_ADDR_COMPLETE)) {
12289 rc = -EBUSY;
12290 break;
12291 }
12292 }
12293
12294 return rc;
12295}
12296
12297/* offset and length are dword aligned */
12298static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12299 u8 *buf)
12300{
12301 int ret = 0;
12302 u32 pagesize = tp->nvram_pagesize;
12303 u32 pagemask = pagesize - 1;
12304 u32 nvram_cmd;
12305 u8 *tmp;
12306
12307 tmp = kmalloc(pagesize, GFP_KERNEL);
12308 if (tmp == NULL)
12309 return -ENOMEM;
12310
12311 while (len) {
12312 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012313 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012314
12315 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012316
Linus Torvalds1da177e2005-04-16 15:20:36 -070012317 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012318 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12319 (__be32 *) (tmp + j));
12320 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012321 break;
12322 }
12323 if (ret)
12324 break;
12325
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012326 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012327 size = pagesize;
12328 if (len < size)
12329 size = len;
12330
12331 len -= size;
12332
12333 memcpy(tmp + page_off, buf, size);
12334
12335 offset = offset + (pagesize - page_off);
12336
Michael Chane6af3012005-04-21 17:12:05 -070012337 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012338
12339 /*
12340 * Before we can erase the flash page, we need
12341 * to issue a special "write enable" command.
12342 */
12343 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12344
12345 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12346 break;
12347
12348 /* Erase the target page */
12349 tw32(NVRAM_ADDR, phy_addr);
12350
12351 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12352 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12353
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012354 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012355 break;
12356
12357 /* Issue another write enable to start the write. */
12358 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12359
12360 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12361 break;
12362
12363 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012364 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012365
Al Virob9fc7dc2007-12-17 22:59:57 -080012366 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012367
Al Virob9fc7dc2007-12-17 22:59:57 -080012368 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012369
12370 tw32(NVRAM_ADDR, phy_addr + j);
12371
12372 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12373 NVRAM_CMD_WR;
12374
12375 if (j == 0)
12376 nvram_cmd |= NVRAM_CMD_FIRST;
12377 else if (j == (pagesize - 4))
12378 nvram_cmd |= NVRAM_CMD_LAST;
12379
12380 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12381 break;
12382 }
12383 if (ret)
12384 break;
12385 }
12386
12387 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12388 tg3_nvram_exec_cmd(tp, nvram_cmd);
12389
12390 kfree(tmp);
12391
12392 return ret;
12393}
12394
12395/* offset and length are dword aligned */
12396static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12397 u8 *buf)
12398{
12399 int i, ret = 0;
12400
12401 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012402 u32 page_off, phy_addr, nvram_cmd;
12403 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012404
12405 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012406 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012407
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012408 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012409
Michael Chan18201802006-03-20 22:29:15 -080012410 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012411
12412 tw32(NVRAM_ADDR, phy_addr);
12413
12414 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12415
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012416 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012417 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012418 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012419 nvram_cmd |= NVRAM_CMD_LAST;
12420
12421 if (i == (len - 4))
12422 nvram_cmd |= NVRAM_CMD_LAST;
12423
Matt Carlson321d32a2008-11-21 17:22:19 -080012424 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012425 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012426 (tp->nvram_jedecnum == JEDEC_ST) &&
12427 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012428
12429 if ((ret = tg3_nvram_exec_cmd(tp,
12430 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12431 NVRAM_CMD_DONE)))
12432
12433 break;
12434 }
Joe Perches63c3a662011-04-26 08:12:10 +000012435 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012436 /* We always do complete word writes to eeprom. */
12437 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12438 }
12439
12440 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12441 break;
12442 }
12443 return ret;
12444}
12445
12446/* offset and length are dword aligned */
12447static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12448{
12449 int ret;
12450
Joe Perches63c3a662011-04-26 08:12:10 +000012451 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012452 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12453 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012454 udelay(40);
12455 }
12456
Joe Perches63c3a662011-04-26 08:12:10 +000012457 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012458 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012459 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012460 u32 grc_mode;
12461
Michael Chanec41c7d2006-01-17 02:40:55 -080012462 ret = tg3_nvram_lock(tp);
12463 if (ret)
12464 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012465
Michael Chane6af3012005-04-21 17:12:05 -070012466 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012467 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012468 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012469
12470 grc_mode = tr32(GRC_MODE);
12471 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12472
Joe Perches63c3a662011-04-26 08:12:10 +000012473 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012474 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12475 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012476 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012477 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12478 buf);
12479 }
12480
12481 grc_mode = tr32(GRC_MODE);
12482 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12483
Michael Chane6af3012005-04-21 17:12:05 -070012484 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012485 tg3_nvram_unlock(tp);
12486 }
12487
Joe Perches63c3a662011-04-26 08:12:10 +000012488 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012489 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012490 udelay(40);
12491 }
12492
12493 return ret;
12494}
12495
12496struct subsys_tbl_ent {
12497 u16 subsys_vendor, subsys_devid;
12498 u32 phy_id;
12499};
12500
Matt Carlson24daf2b2010-02-17 15:17:02 +000012501static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012502 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012503 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012504 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012505 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012506 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012507 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012508 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012509 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12510 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12511 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012512 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012513 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012514 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012515 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12516 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12517 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012518 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012519 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012520 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012521 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012522 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012523 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012524 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012525
12526 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012527 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012528 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012529 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012530 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012531 { TG3PCI_SUBVENDOR_ID_3COM,
12532 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12533 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012534 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012535 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012536 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012537
12538 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012539 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012540 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012541 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012542 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012543 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012544 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012545 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012546 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012547
12548 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012549 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012550 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012551 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012552 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012553 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12554 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12555 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012556 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012557 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012558 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012559
12560 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012561 { TG3PCI_SUBVENDOR_ID_IBM,
12562 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012563};
12564
Matt Carlson24daf2b2010-02-17 15:17:02 +000012565static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012566{
12567 int i;
12568
12569 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12570 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12571 tp->pdev->subsystem_vendor) &&
12572 (subsys_id_to_phy_id[i].subsys_devid ==
12573 tp->pdev->subsystem_device))
12574 return &subsys_id_to_phy_id[i];
12575 }
12576 return NULL;
12577}
12578
Michael Chan7d0c41e2005-04-21 17:06:20 -070012579static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012580{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012581 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012582 u16 pmcsr;
12583
12584 /* On some early chips the SRAM cannot be accessed in D3hot state,
12585 * so need make sure we're in D0.
12586 */
12587 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12588 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12589 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12590 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012591
12592 /* Make sure register accesses (indirect or otherwise)
12593 * will function correctly.
12594 */
12595 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12596 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012597
David S. Millerf49639e2006-06-09 11:58:36 -070012598 /* The memory arbiter has to be enabled in order for SRAM accesses
12599 * to succeed. Normally on powerup the tg3 chip firmware will make
12600 * sure it is enabled, but other entities such as system netboot
12601 * code might disable it.
12602 */
12603 val = tr32(MEMARB_MODE);
12604 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12605
Matt Carlson79eb6902010-02-17 15:17:03 +000012606 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012607 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12608
Gary Zambranoa85feb82007-05-05 11:52:19 -070012609 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012610 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12611 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012612
Michael Chanb5d37722006-09-27 16:06:21 -070012613 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012614 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012615 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12616 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012617 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012618 val = tr32(VCPU_CFGSHDW);
12619 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012620 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012621 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012622 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012623 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012624 device_set_wakeup_enable(&tp->pdev->dev, true);
12625 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012626 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012627 }
12628
Linus Torvalds1da177e2005-04-16 15:20:36 -070012629 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12630 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12631 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012632 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012633 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012634
12635 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12636 tp->nic_sram_data_cfg = nic_cfg;
12637
12638 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12639 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12640 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12641 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12642 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12643 (ver > 0) && (ver < 0x100))
12644 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12645
Matt Carlsona9daf362008-05-25 23:49:44 -070012646 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12647 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12648
Linus Torvalds1da177e2005-04-16 15:20:36 -070012649 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12650 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12651 eeprom_phy_serdes = 1;
12652
12653 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12654 if (nic_phy_id != 0) {
12655 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12656 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12657
12658 eeprom_phy_id = (id1 >> 16) << 10;
12659 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12660 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12661 } else
12662 eeprom_phy_id = 0;
12663
Michael Chan7d0c41e2005-04-21 17:06:20 -070012664 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012665 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000012666 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012667 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012668 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012669 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012670 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012671
Joe Perches63c3a662011-04-26 08:12:10 +000012672 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012673 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12674 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012675 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012676 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12677
12678 switch (led_cfg) {
12679 default:
12680 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12681 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12682 break;
12683
12684 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12685 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12686 break;
12687
12688 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12689 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012690
12691 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12692 * read on some older 5700/5701 bootcode.
12693 */
12694 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12695 ASIC_REV_5700 ||
12696 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12697 ASIC_REV_5701)
12698 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12699
Linus Torvalds1da177e2005-04-16 15:20:36 -070012700 break;
12701
12702 case SHASTA_EXT_LED_SHARED:
12703 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12704 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12705 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12706 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12707 LED_CTRL_MODE_PHY_2);
12708 break;
12709
12710 case SHASTA_EXT_LED_MAC:
12711 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12712 break;
12713
12714 case SHASTA_EXT_LED_COMBO:
12715 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12716 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12717 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12718 LED_CTRL_MODE_PHY_2);
12719 break;
12720
Stephen Hemminger855e1112008-04-16 16:37:28 -070012721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012722
12723 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12724 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12725 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12726 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12727
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012728 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12729 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012730
Michael Chan9d26e212006-12-07 00:21:14 -080012731 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000012732 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012733 if ((tp->pdev->subsystem_vendor ==
12734 PCI_VENDOR_ID_ARIMA) &&
12735 (tp->pdev->subsystem_device == 0x205a ||
12736 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000012737 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012738 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012739 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12740 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012742
12743 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000012744 tg3_flag_set(tp, ENABLE_ASF);
12745 if (tg3_flag(tp, 5750_PLUS))
12746 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012747 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012748
12749 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012750 tg3_flag(tp, 5750_PLUS))
12751 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012752
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012753 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012754 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000012755 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012756
Joe Perches63c3a662011-04-26 08:12:10 +000012757 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012758 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012759 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012760 device_set_wakeup_enable(&tp->pdev->dev, true);
12761 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012762
Linus Torvalds1da177e2005-04-16 15:20:36 -070012763 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012764 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012765
12766 /* serdes signal pre-emphasis in register 0x590 set by */
12767 /* bootcode if bit 18 is set */
12768 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012769 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012770
Joe Perches63c3a662011-04-26 08:12:10 +000012771 if ((tg3_flag(tp, 57765_PLUS) ||
12772 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12773 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012774 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012775 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080012776
Joe Perches63c3a662011-04-26 08:12:10 +000012777 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012778 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012779 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012780 u32 cfg3;
12781
12782 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12783 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000012784 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070012785 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012786
Matt Carlson14417062010-02-17 15:16:59 +000012787 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000012788 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070012789 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000012790 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070012791 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000012792 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012793 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012794done:
Joe Perches63c3a662011-04-26 08:12:10 +000012795 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012796 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000012797 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012798 else
12799 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012800}
12801
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012802static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12803{
12804 int i;
12805 u32 val;
12806
12807 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12808 tw32(OTP_CTRL, cmd);
12809
12810 /* Wait for up to 1 ms for command to execute. */
12811 for (i = 0; i < 100; i++) {
12812 val = tr32(OTP_STATUS);
12813 if (val & OTP_STATUS_CMD_DONE)
12814 break;
12815 udelay(10);
12816 }
12817
12818 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12819}
12820
12821/* Read the gphy configuration from the OTP region of the chip. The gphy
12822 * configuration is a 32-bit value that straddles the alignment boundary.
12823 * We do two 32-bit reads and then shift and merge the results.
12824 */
12825static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12826{
12827 u32 bhalf_otp, thalf_otp;
12828
12829 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12830
12831 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12832 return 0;
12833
12834 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12835
12836 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12837 return 0;
12838
12839 thalf_otp = tr32(OTP_READ_DATA);
12840
12841 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12842
12843 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12844 return 0;
12845
12846 bhalf_otp = tr32(OTP_READ_DATA);
12847
12848 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12849}
12850
Matt Carlsone256f8a2011-03-09 16:58:24 +000012851static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
12852{
12853 u32 adv = ADVERTISED_Autoneg |
12854 ADVERTISED_Pause;
12855
12856 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12857 adv |= ADVERTISED_1000baseT_Half |
12858 ADVERTISED_1000baseT_Full;
12859
12860 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
12861 adv |= ADVERTISED_100baseT_Half |
12862 ADVERTISED_100baseT_Full |
12863 ADVERTISED_10baseT_Half |
12864 ADVERTISED_10baseT_Full |
12865 ADVERTISED_TP;
12866 else
12867 adv |= ADVERTISED_FIBRE;
12868
12869 tp->link_config.advertising = adv;
12870 tp->link_config.speed = SPEED_INVALID;
12871 tp->link_config.duplex = DUPLEX_INVALID;
12872 tp->link_config.autoneg = AUTONEG_ENABLE;
12873 tp->link_config.active_speed = SPEED_INVALID;
12874 tp->link_config.active_duplex = DUPLEX_INVALID;
12875 tp->link_config.orig_speed = SPEED_INVALID;
12876 tp->link_config.orig_duplex = DUPLEX_INVALID;
12877 tp->link_config.orig_autoneg = AUTONEG_INVALID;
12878}
12879
Michael Chan7d0c41e2005-04-21 17:06:20 -070012880static int __devinit tg3_phy_probe(struct tg3 *tp)
12881{
12882 u32 hw_phy_id_1, hw_phy_id_2;
12883 u32 hw_phy_id, hw_phy_id_masked;
12884 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012885
Matt Carlsone256f8a2011-03-09 16:58:24 +000012886 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000012887 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000012888 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
12889
Joe Perches63c3a662011-04-26 08:12:10 +000012890 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012891 return tg3_phy_init(tp);
12892
Linus Torvalds1da177e2005-04-16 15:20:36 -070012893 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012894 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012895 */
12896 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012897 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012898 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012899 } else {
12900 /* Now read the physical PHY_ID from the chip and verify
12901 * that it is sane. If it doesn't look good, we fall back
12902 * to either the hard-coded table based PHY_ID and failing
12903 * that the value found in the eeprom area.
12904 */
12905 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12906 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12907
12908 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12909 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12910 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12911
Matt Carlson79eb6902010-02-17 15:17:03 +000012912 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012913 }
12914
Matt Carlson79eb6902010-02-17 15:17:03 +000012915 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012916 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012917 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012918 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012919 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012920 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012921 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012922 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012923 /* Do nothing, phy ID already set up in
12924 * tg3_get_eeprom_hw_cfg().
12925 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012926 } else {
12927 struct subsys_tbl_ent *p;
12928
12929 /* No eeprom signature? Try the hardcoded
12930 * subsys device table.
12931 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012932 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012933 if (!p)
12934 return -ENODEV;
12935
12936 tp->phy_id = p->phy_id;
12937 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012938 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012939 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012940 }
12941 }
12942
Matt Carlsona6b68da2010-12-06 08:28:52 +000012943 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
12944 ((tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
12945 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
12946 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12947 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000012948 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
12949
Matt Carlsone256f8a2011-03-09 16:58:24 +000012950 tg3_phy_init_link_config(tp);
12951
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012952 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012953 !tg3_flag(tp, ENABLE_APE) &&
12954 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000012955 u32 bmsr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012956
12957 tg3_readphy(tp, MII_BMSR, &bmsr);
12958 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12959 (bmsr & BMSR_LSTATUS))
12960 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012961
Linus Torvalds1da177e2005-04-16 15:20:36 -070012962 err = tg3_phy_reset(tp);
12963 if (err)
12964 return err;
12965
Matt Carlson42b64a42011-05-19 12:12:49 +000012966 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012967
Michael Chan3600d912006-12-07 00:21:48 -080012968 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12969 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12970 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12971 if (!tg3_copper_is_advertising_all(tp, mask)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000012972 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
12973 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012974
12975 tg3_writephy(tp, MII_BMCR,
12976 BMCR_ANENABLE | BMCR_ANRESTART);
12977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012978 }
12979
12980skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012981 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012982 err = tg3_init_5401phy_dsp(tp);
12983 if (err)
12984 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012985
Linus Torvalds1da177e2005-04-16 15:20:36 -070012986 err = tg3_init_5401phy_dsp(tp);
12987 }
12988
Linus Torvalds1da177e2005-04-16 15:20:36 -070012989 return err;
12990}
12991
Matt Carlson184b8902010-04-05 10:19:25 +000012992static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012993{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012994 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012995 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000012996 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012997
Matt Carlsonc3e94502011-04-13 11:05:08 +000012998 vpd_data = (u8 *)tg3_vpd_readblock(tp);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012999 if (!vpd_data)
13000 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013001
Matt Carlson4181b2c2010-02-26 14:04:45 +000013002 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
13003 PCI_VPD_LRDT_RO_DATA);
13004 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013005 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013006
13007 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13008 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13009 i += PCI_VPD_LRDT_TAG_SIZE;
13010
13011 if (block_end > TG3_NVM_VPD_LEN)
13012 goto out_not_found;
13013
Matt Carlson184b8902010-04-05 10:19:25 +000013014 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13015 PCI_VPD_RO_KEYWORD_MFR_ID);
13016 if (j > 0) {
13017 len = pci_vpd_info_field_size(&vpd_data[j]);
13018
13019 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13020 if (j + len > block_end || len != 4 ||
13021 memcmp(&vpd_data[j], "1028", 4))
13022 goto partno;
13023
13024 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13025 PCI_VPD_RO_KEYWORD_VENDOR0);
13026 if (j < 0)
13027 goto partno;
13028
13029 len = pci_vpd_info_field_size(&vpd_data[j]);
13030
13031 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13032 if (j + len > block_end)
13033 goto partno;
13034
13035 memcpy(tp->fw_ver, &vpd_data[j], len);
13036 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
13037 }
13038
13039partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013040 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13041 PCI_VPD_RO_KEYWORD_PARTNO);
13042 if (i < 0)
13043 goto out_not_found;
13044
13045 len = pci_vpd_info_field_size(&vpd_data[i]);
13046
13047 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13048 if (len > TG3_BPN_SIZE ||
13049 (len + i) > TG3_NVM_VPD_LEN)
13050 goto out_not_found;
13051
13052 memcpy(tp->board_part_number, &vpd_data[i], len);
13053
Linus Torvalds1da177e2005-04-16 15:20:36 -070013054out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013055 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013056 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013057 return;
13058
13059out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013060 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13061 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13062 strcpy(tp->board_part_number, "BCM5717");
13063 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13064 strcpy(tp->board_part_number, "BCM5718");
13065 else
13066 goto nomatch;
13067 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13068 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13069 strcpy(tp->board_part_number, "BCM57780");
13070 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13071 strcpy(tp->board_part_number, "BCM57760");
13072 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13073 strcpy(tp->board_part_number, "BCM57790");
13074 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13075 strcpy(tp->board_part_number, "BCM57788");
13076 else
13077 goto nomatch;
13078 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13079 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13080 strcpy(tp->board_part_number, "BCM57761");
13081 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13082 strcpy(tp->board_part_number, "BCM57765");
13083 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13084 strcpy(tp->board_part_number, "BCM57781");
13085 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13086 strcpy(tp->board_part_number, "BCM57785");
13087 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13088 strcpy(tp->board_part_number, "BCM57791");
13089 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13090 strcpy(tp->board_part_number, "BCM57795");
13091 else
13092 goto nomatch;
13093 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013094 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013095 } else {
13096nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013097 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013099}
13100
Matt Carlson9c8a6202007-10-21 16:16:08 -070013101static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13102{
13103 u32 val;
13104
Matt Carlsone4f34112009-02-25 14:25:00 +000013105 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013106 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013107 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013108 val != 0)
13109 return 0;
13110
13111 return 1;
13112}
13113
Matt Carlsonacd9c112009-02-25 14:26:33 +000013114static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13115{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013116 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013117 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013118 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013119
13120 if (tg3_nvram_read(tp, 0xc, &offset) ||
13121 tg3_nvram_read(tp, 0x4, &start))
13122 return;
13123
13124 offset = tg3_nvram_logical_addr(tp, offset);
13125
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013126 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013127 return;
13128
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013129 if ((val & 0xfc000000) == 0x0c000000) {
13130 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013131 return;
13132
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013133 if (val == 0)
13134 newver = true;
13135 }
13136
Matt Carlson75f99362010-04-05 10:19:24 +000013137 dst_off = strlen(tp->fw_ver);
13138
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013139 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013140 if (TG3_VER_SIZE - dst_off < 16 ||
13141 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013142 return;
13143
13144 offset = offset + ver_offset - start;
13145 for (i = 0; i < 16; i += 4) {
13146 __be32 v;
13147 if (tg3_nvram_read_be32(tp, offset + i, &v))
13148 return;
13149
Matt Carlson75f99362010-04-05 10:19:24 +000013150 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013151 }
13152 } else {
13153 u32 major, minor;
13154
13155 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13156 return;
13157
13158 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13159 TG3_NVM_BCVER_MAJSFT;
13160 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013161 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13162 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013163 }
13164}
13165
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013166static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13167{
13168 u32 val, major, minor;
13169
13170 /* Use native endian representation */
13171 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13172 return;
13173
13174 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13175 TG3_NVM_HWSB_CFG1_MAJSFT;
13176 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13177 TG3_NVM_HWSB_CFG1_MINSFT;
13178
13179 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13180}
13181
Matt Carlsondfe00d72008-11-21 17:19:41 -080013182static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13183{
13184 u32 offset, major, minor, build;
13185
Matt Carlson75f99362010-04-05 10:19:24 +000013186 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013187
13188 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13189 return;
13190
13191 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13192 case TG3_EEPROM_SB_REVISION_0:
13193 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13194 break;
13195 case TG3_EEPROM_SB_REVISION_2:
13196 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13197 break;
13198 case TG3_EEPROM_SB_REVISION_3:
13199 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13200 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013201 case TG3_EEPROM_SB_REVISION_4:
13202 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13203 break;
13204 case TG3_EEPROM_SB_REVISION_5:
13205 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13206 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013207 case TG3_EEPROM_SB_REVISION_6:
13208 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13209 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013210 default:
13211 return;
13212 }
13213
Matt Carlsone4f34112009-02-25 14:25:00 +000013214 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013215 return;
13216
13217 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13218 TG3_EEPROM_SB_EDH_BLD_SHFT;
13219 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13220 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13221 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13222
13223 if (minor > 99 || build > 26)
13224 return;
13225
Matt Carlson75f99362010-04-05 10:19:24 +000013226 offset = strlen(tp->fw_ver);
13227 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13228 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013229
13230 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013231 offset = strlen(tp->fw_ver);
13232 if (offset < TG3_VER_SIZE - 1)
13233 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013234 }
13235}
13236
Matt Carlsonacd9c112009-02-25 14:26:33 +000013237static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013238{
13239 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013240 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013241
13242 for (offset = TG3_NVM_DIR_START;
13243 offset < TG3_NVM_DIR_END;
13244 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013245 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013246 return;
13247
13248 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13249 break;
13250 }
13251
13252 if (offset == TG3_NVM_DIR_END)
13253 return;
13254
Joe Perches63c3a662011-04-26 08:12:10 +000013255 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013256 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013257 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013258 return;
13259
Matt Carlsone4f34112009-02-25 14:25:00 +000013260 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013261 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013262 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013263 return;
13264
13265 offset += val - start;
13266
Matt Carlsonacd9c112009-02-25 14:26:33 +000013267 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013268
Matt Carlsonacd9c112009-02-25 14:26:33 +000013269 tp->fw_ver[vlen++] = ',';
13270 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013271
13272 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013273 __be32 v;
13274 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013275 return;
13276
Al Virob9fc7dc2007-12-17 22:59:57 -080013277 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013278
Matt Carlsonacd9c112009-02-25 14:26:33 +000013279 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13280 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013281 break;
13282 }
13283
Matt Carlsonacd9c112009-02-25 14:26:33 +000013284 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13285 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013286 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013287}
13288
Matt Carlson7fd76442009-02-25 14:27:20 +000013289static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13290{
13291 int vlen;
13292 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013293 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013294
Joe Perches63c3a662011-04-26 08:12:10 +000013295 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013296 return;
13297
13298 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13299 if (apedata != APE_SEG_SIG_MAGIC)
13300 return;
13301
13302 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13303 if (!(apedata & APE_FW_STATUS_READY))
13304 return;
13305
13306 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13307
Matt Carlsondc6d0742010-09-15 08:59:55 +000013308 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013309 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013310 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013311 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013312 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013313 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013314
Matt Carlson7fd76442009-02-25 14:27:20 +000013315 vlen = strlen(tp->fw_ver);
13316
Matt Carlsonecc79642010-08-02 11:26:01 +000013317 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13318 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013319 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13320 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13321 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13322 (apedata & APE_FW_VERSION_BLDMSK));
13323}
13324
Matt Carlsonacd9c112009-02-25 14:26:33 +000013325static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13326{
13327 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013328 bool vpd_vers = false;
13329
13330 if (tp->fw_ver[0] != 0)
13331 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013332
Joe Perches63c3a662011-04-26 08:12:10 +000013333 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013334 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013335 return;
13336 }
13337
Matt Carlsonacd9c112009-02-25 14:26:33 +000013338 if (tg3_nvram_read(tp, 0, &val))
13339 return;
13340
13341 if (val == TG3_EEPROM_MAGIC)
13342 tg3_read_bc_ver(tp);
13343 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13344 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013345 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13346 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013347 else
13348 return;
13349
Joe Perches63c3a662011-04-26 08:12:10 +000013350 if (!tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013351 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013352
13353 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013354
Matt Carlson75f99362010-04-05 10:19:24 +000013355done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013356 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013357}
13358
Michael Chan7544b092007-05-05 13:08:32 -070013359static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13360
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013361static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13362{
Joe Perches63c3a662011-04-26 08:12:10 +000013363 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013364 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013365 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013366 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013367 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013368 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013369}
13370
Matt Carlson41434702011-03-09 16:58:22 +000013371static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013372 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13373 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13374 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13375 { },
13376};
13377
Linus Torvalds1da177e2005-04-16 15:20:36 -070013378static int __devinit tg3_get_invariants(struct tg3 *tp)
13379{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013380 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013381 u32 pci_state_reg, grc_misc_cfg;
13382 u32 val;
13383 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013384 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013385
Linus Torvalds1da177e2005-04-16 15:20:36 -070013386 /* Force memory write invalidate off. If we leave it on,
13387 * then on 5700_BX chips we have to enable a workaround.
13388 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13389 * to match the cacheline size. The Broadcom driver have this
13390 * workaround but turns MWI off all the times so never uses
13391 * it. This seems to suggest that the workaround is insufficient.
13392 */
13393 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13394 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13395 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13396
13397 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
13398 * has the register indirect write enable bit set before
13399 * we try to access any of the MMIO registers. It is also
13400 * critical that the PCI-X hw workaround situation is decided
13401 * before that as well.
13402 */
13403 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13404 &misc_ctrl_reg);
13405
13406 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13407 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013408 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13409 u32 prod_id_asic_rev;
13410
Matt Carlson5001e2f2009-11-13 13:03:51 +000013411 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13412 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013413 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13414 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013415 pci_read_config_dword(tp->pdev,
13416 TG3PCI_GEN2_PRODID_ASICREV,
13417 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013418 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13419 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13420 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13421 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13422 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13423 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13424 pci_read_config_dword(tp->pdev,
13425 TG3PCI_GEN15_PRODID_ASICREV,
13426 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013427 else
13428 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13429 &prod_id_asic_rev);
13430
Matt Carlson321d32a2008-11-21 17:22:19 -080013431 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013433
Michael Chanff645be2005-04-21 17:09:53 -070013434 /* Wrong chip ID in 5752 A0. This code can be removed later
13435 * as A0 is not in production.
13436 */
13437 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13438 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13439
Michael Chan68929142005-08-09 20:17:14 -070013440 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13441 * we need to disable memory and use config. cycles
13442 * only to access all registers. The 5702/03 chips
13443 * can mistakenly decode the special cycles from the
13444 * ICH chipsets as memory write cycles, causing corruption
13445 * of register and memory space. Only certain ICH bridges
13446 * will drive special cycles with non-zero data during the
13447 * address phase which can fall within the 5703's address
13448 * range. This is not an ICH bug as the PCI spec allows
13449 * non-zero address during special cycles. However, only
13450 * these ICH bridges are known to drive non-zero addresses
13451 * during special cycles.
13452 *
13453 * Since special cycles do not cross PCI bridges, we only
13454 * enable this workaround if the 5703 is on the secondary
13455 * bus of these ICH bridges.
13456 */
13457 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13458 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13459 static struct tg3_dev_id {
13460 u32 vendor;
13461 u32 device;
13462 u32 rev;
13463 } ich_chipsets[] = {
13464 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13465 PCI_ANY_ID },
13466 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13467 PCI_ANY_ID },
13468 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13469 0xa },
13470 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13471 PCI_ANY_ID },
13472 { },
13473 };
13474 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13475 struct pci_dev *bridge = NULL;
13476
13477 while (pci_id->vendor != 0) {
13478 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13479 bridge);
13480 if (!bridge) {
13481 pci_id++;
13482 continue;
13483 }
13484 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013485 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013486 continue;
13487 }
13488 if (bridge->subordinate &&
13489 (bridge->subordinate->number ==
13490 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013491 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013492 pci_dev_put(bridge);
13493 break;
13494 }
13495 }
13496 }
13497
Matt Carlson41588ba2008-04-19 18:12:33 -070013498 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
13499 static struct tg3_dev_id {
13500 u32 vendor;
13501 u32 device;
13502 } bridge_chipsets[] = {
13503 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13504 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13505 { },
13506 };
13507 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13508 struct pci_dev *bridge = NULL;
13509
13510 while (pci_id->vendor != 0) {
13511 bridge = pci_get_device(pci_id->vendor,
13512 pci_id->device,
13513 bridge);
13514 if (!bridge) {
13515 pci_id++;
13516 continue;
13517 }
13518 if (bridge->subordinate &&
13519 (bridge->subordinate->number <=
13520 tp->pdev->bus->number) &&
13521 (bridge->subordinate->subordinate >=
13522 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013523 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013524 pci_dev_put(bridge);
13525 break;
13526 }
13527 }
13528 }
13529
Michael Chan4a29cc22006-03-19 13:21:12 -080013530 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13531 * DMA addresses > 40-bit. This bridge may have other additional
13532 * 57xx devices behind it in some 4-port NIC designs for example.
13533 * Any tg3 device found behind the bridge will also need the 40-bit
13534 * DMA workaround.
13535 */
Michael Chana4e2b342005-10-26 15:46:52 -070013536 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13537 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013538 tg3_flag_set(tp, 5780_CLASS);
13539 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013540 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000013541 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013542 struct pci_dev *bridge = NULL;
13543
13544 do {
13545 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13546 PCI_DEVICE_ID_SERVERWORKS_EPB,
13547 bridge);
13548 if (bridge && bridge->subordinate &&
13549 (bridge->subordinate->number <=
13550 tp->pdev->bus->number) &&
13551 (bridge->subordinate->subordinate >=
13552 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013553 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013554 pci_dev_put(bridge);
13555 break;
13556 }
13557 } while (bridge);
13558 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013559
Linus Torvalds1da177e2005-04-16 15:20:36 -070013560 /* Initialize misc host control in PCI block. */
13561 tp->misc_host_ctrl |= (misc_ctrl_reg &
13562 MISC_HOST_CTRL_CHIPREV);
13563 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13564 tp->misc_host_ctrl);
13565
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013566 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13567 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013568 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13569 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Michael Chan7544b092007-05-05 13:08:32 -070013570 tp->pdev_peer = tg3_find_peer(tp);
13571
Matt Carlsonc885e822010-08-02 11:25:57 +000013572 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013573 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13574 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013575 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013576
13577 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013578 tg3_flag(tp, 5717_PLUS))
13579 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013580
Matt Carlson321d32a2008-11-21 17:22:19 -080013581 /* Intentionally exclude ASIC_REV_5906 */
13582 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad12006-03-20 22:27:35 -080013583 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013584 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013585 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013586 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013587 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013588 tg3_flag(tp, 57765_PLUS))
13589 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013590
13591 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13592 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013593 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013594 tg3_flag(tp, 5755_PLUS) ||
13595 tg3_flag(tp, 5780_CLASS))
13596 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013597
John W. Linville1b440c562005-04-21 17:03:18 -070013598 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
Joe Perches63c3a662011-04-26 08:12:10 +000013599 tg3_flag(tp, 5750_PLUS))
13600 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013601
Matt Carlson027455a2008-12-21 20:19:30 -080013602 /* 5700 B0 chips do not support checksumming correctly due
13603 * to hardware bugs.
13604 */
Michał Mirosławdc668912011-04-07 03:35:07 +000013605 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
13606 u32 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013607
Joe Perches63c3a662011-04-26 08:12:10 +000013608 if (tg3_flag(tp, 5755_PLUS))
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013609 features |= NETIF_F_IPV6_CSUM;
13610 tp->dev->features |= features;
Michał Mirosławdc668912011-04-07 03:35:07 +000013611 tp->dev->hw_features |= features;
13612 tp->dev->vlan_features |= features;
Matt Carlson027455a2008-12-21 20:19:30 -080013613 }
13614
Matt Carlson507399f2009-11-13 13:03:37 +000013615 /* Determine TSO capabilities */
Matt Carlson2866d952011-02-10 20:06:46 -080013616 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson4d163b72011-01-25 15:58:48 +000013617 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013618 else if (tg3_flag(tp, 57765_PLUS))
13619 tg3_flag_set(tp, HW_TSO_3);
13620 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013621 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013622 tg3_flag_set(tp, HW_TSO_2);
13623 else if (tg3_flag(tp, 5750_PLUS)) {
13624 tg3_flag_set(tp, HW_TSO_1);
13625 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013626 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13627 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013628 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013629 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13630 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13631 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013632 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013633 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13634 tp->fw_needed = FIRMWARE_TG3TSO5;
13635 else
13636 tp->fw_needed = FIRMWARE_TG3TSO;
13637 }
13638
13639 tp->irq_max = 1;
13640
Joe Perches63c3a662011-04-26 08:12:10 +000013641 if (tg3_flag(tp, 5750_PLUS)) {
13642 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013643 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13644 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13645 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13646 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13647 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000013648 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013649
Joe Perches63c3a662011-04-26 08:12:10 +000013650 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013651 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013652 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070013653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013654
Joe Perches63c3a662011-04-26 08:12:10 +000013655 if (tg3_flag(tp, 57765_PLUS)) {
13656 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000013657 tp->irq_max = TG3_IRQ_MAX_VECS;
13658 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013659 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013660
Matt Carlson2ffcc982011-05-19 12:12:44 +000013661 /* All chips can get confused if TX buffers
13662 * straddle the 4GB address boundary.
13663 */
13664 tg3_flag_set(tp, 4G_DMA_BNDRY_BUG);
13665
13666 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000013667 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlson2ffcc982011-05-19 12:12:44 +000013668 else
Joe Perches63c3a662011-04-26 08:12:10 +000013669 tg3_flag_set(tp, 40BIT_DMA_LIMIT_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013670
Joe Perches63c3a662011-04-26 08:12:10 +000013671 if (tg3_flag(tp, 5717_PLUS))
13672 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000013673
Joe Perches63c3a662011-04-26 08:12:10 +000013674 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlson2866d952011-02-10 20:06:46 -080013675 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Joe Perches63c3a662011-04-26 08:12:10 +000013676 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000013677
Joe Perches63c3a662011-04-26 08:12:10 +000013678 if (!tg3_flag(tp, 5705_PLUS) ||
13679 tg3_flag(tp, 5780_CLASS) ||
13680 tg3_flag(tp, USE_JUMBO_BDFLAG))
13681 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070013682
Matt Carlson52f44902008-11-21 17:17:04 -080013683 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13684 &pci_state_reg);
13685
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013686 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13687 if (tp->pcie_cap != 0) {
13688 u16 lnkctl;
13689
Joe Perches63c3a662011-04-26 08:12:10 +000013690 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013691
Matt Carlsoncf790032010-11-24 08:31:48 +000013692 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000013693 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13694 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000013695 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000013696
13697 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013698
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013699 pci_read_config_word(tp->pdev,
13700 tp->pcie_cap + PCI_EXP_LNKCTL,
13701 &lnkctl);
13702 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13703 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013704 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013705 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013706 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013707 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13708 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000013709 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000013710 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013711 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080013712 }
Matt Carlson52f44902008-11-21 17:17:04 -080013713 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +000013714 tg3_flag_set(tp, PCI_EXPRESS);
13715 } else if (!tg3_flag(tp, 5705_PLUS) ||
13716 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080013717 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13718 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013719 dev_err(&tp->pdev->dev,
13720 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013721 return -EIO;
13722 }
13723
13724 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000013725 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080013726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013727
Michael Chan399de502005-10-03 14:02:39 -070013728 /* If we have an AMD 762 or VIA K8T800 chipset, write
13729 * reordering to the mailbox registers done by the host
13730 * controller can cause major troubles. We read back from
13731 * every mailbox register write to force the writes to be
13732 * posted to the chip in order.
13733 */
Matt Carlson41434702011-03-09 16:58:22 +000013734 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013735 !tg3_flag(tp, PCI_EXPRESS))
13736 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070013737
Matt Carlson69fc4052008-12-21 20:19:57 -080013738 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13739 &tp->pci_cacheline_sz);
13740 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13741 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013742 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13743 tp->pci_lat_timer < 64) {
13744 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013745 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13746 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013747 }
13748
Matt Carlson52f44902008-11-21 17:17:04 -080013749 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13750 /* 5700 BX chips need to have their TX producer index
13751 * mailboxes written twice to workaround a bug.
13752 */
Joe Perches63c3a662011-04-26 08:12:10 +000013753 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070013754
Matt Carlson52f44902008-11-21 17:17:04 -080013755 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013756 *
13757 * The workaround is to use indirect register accesses
13758 * for all chip writes not to mailbox registers.
13759 */
Joe Perches63c3a662011-04-26 08:12:10 +000013760 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013761 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013762
Joe Perches63c3a662011-04-26 08:12:10 +000013763 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013764
13765 /* The chip can have it's power management PCI config
13766 * space registers clobbered due to this bug.
13767 * So explicitly force the chip into D0 here.
13768 */
Matt Carlson9974a352007-10-07 23:27:28 -070013769 pci_read_config_dword(tp->pdev,
13770 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013771 &pm_reg);
13772 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13773 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013774 pci_write_config_dword(tp->pdev,
13775 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013776 pm_reg);
13777
13778 /* Also, force SERR#/PERR# in PCI command. */
13779 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13780 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13781 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13782 }
13783 }
13784
Linus Torvalds1da177e2005-04-16 15:20:36 -070013785 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013786 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013787 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013788 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013789
13790 /* Chip-specific fixup from Broadcom driver */
13791 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13792 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13793 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13794 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13795 }
13796
Michael Chan1ee582d2005-08-09 20:16:46 -070013797 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013798 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013799 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013800 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013801 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013802 tp->write32_tx_mbox = tg3_write32;
13803 tp->write32_rx_mbox = tg3_write32;
13804
13805 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000013806 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070013807 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013808 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013809 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070013810 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13811 /*
13812 * Back to back register writes can cause problems on these
13813 * chips, the workaround is to read back all reg writes
13814 * except those to mailbox regs.
13815 *
13816 * See tg3_write_indirect_reg32().
13817 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013818 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013819 }
13820
Joe Perches63c3a662011-04-26 08:12:10 +000013821 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070013822 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000013823 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070013824 tp->write32_rx_mbox = tg3_write_flush_reg32;
13825 }
Michael Chan20094932005-08-09 20:16:32 -070013826
Joe Perches63c3a662011-04-26 08:12:10 +000013827 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070013828 tp->read32 = tg3_read_indirect_reg32;
13829 tp->write32 = tg3_write_indirect_reg32;
13830 tp->read32_mbox = tg3_read_indirect_mbox;
13831 tp->write32_mbox = tg3_write_indirect_mbox;
13832 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13833 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13834
13835 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013836 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013837
13838 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13839 pci_cmd &= ~PCI_COMMAND_MEMORY;
13840 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13841 }
Michael Chanb5d37722006-09-27 16:06:21 -070013842 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13843 tp->read32_mbox = tg3_read32_mbox_5906;
13844 tp->write32_mbox = tg3_write32_mbox_5906;
13845 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13846 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13847 }
Michael Chan68929142005-08-09 20:17:14 -070013848
Michael Chanbbadf502006-04-06 21:46:34 -070013849 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013850 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070013851 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013852 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000013853 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070013854
Michael Chan7d0c41e2005-04-21 17:06:20 -070013855 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000013856 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013857 * determined before calling tg3_set_power_state() so that
13858 * we know whether or not to switch out of Vaux power.
13859 * When the flag is set, it means that GPIO1 is used for eeprom
13860 * write protect and also implies that it is a LOM where GPIOs
13861 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013862 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013863 tg3_get_eeprom_hw_cfg(tp);
13864
Joe Perches63c3a662011-04-26 08:12:10 +000013865 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070013866 /* Allow reads and writes to the
13867 * APE register and memory space.
13868 */
13869 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000013870 PCISTATE_ALLOW_APE_SHMEM_WR |
13871 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013872 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13873 pci_state_reg);
13874 }
13875
Matt Carlson9936bcf2007-10-10 18:03:07 -070013876 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013877 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013878 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013879 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013880 tg3_flag(tp, 57765_PLUS))
13881 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070013882
Matt Carlsonbea8a632011-04-25 12:42:49 +000013883 /* Set up tp->grc_local_ctrl before calling tg3_power_up().
Michael Chan314fba32005-04-21 17:07:04 -070013884 * GPIO1 driven high will bring 5700's external PHY out of reset.
13885 * It is also used as eeprom write protect on LOMs.
13886 */
13887 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13888 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
Joe Perches63c3a662011-04-26 08:12:10 +000013889 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070013890 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13891 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013892 /* Unused GPIO3 must be driven as output on 5752 because there
13893 * are no pull-up resistors on unused GPIO pins.
13894 */
13895 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13896 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013897
Matt Carlson321d32a2008-11-21 17:22:19 -080013898 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013899 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13900 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013901 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13902
Matt Carlson8d519ab2009-04-20 06:58:01 +000013903 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13904 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013905 /* Turn off the debug UART. */
13906 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013907 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013908 /* Keep VMain power. */
13909 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13910 GRC_LCLCTRL_GPIO_OUTPUT0;
13911 }
13912
Linus Torvalds1da177e2005-04-16 15:20:36 -070013913 /* Force the chip into D0. */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013914 err = tg3_power_up(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013915 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013916 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013917 return err;
13918 }
13919
Linus Torvalds1da177e2005-04-16 15:20:36 -070013920 /* Derive initial jumbo mode from MTU assigned in
13921 * ether_setup() via the alloc_etherdev() call
13922 */
Joe Perches63c3a662011-04-26 08:12:10 +000013923 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
13924 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013925
13926 /* Determine WakeOnLan speed to use. */
13927 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13928 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13929 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13930 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000013931 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013932 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013933 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013934 }
13935
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013936 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013937 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013938
Linus Torvalds1da177e2005-04-16 15:20:36 -070013939 /* A few boards don't want Ethernet@WireSpeed phy feature */
13940 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13941 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13942 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013943 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013944 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
13945 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13946 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013947
13948 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13949 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013950 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013951 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013952 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013953
Joe Perches63c3a662011-04-26 08:12:10 +000013954 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013955 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013956 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013957 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013958 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070013959 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013960 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013961 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13962 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013963 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13964 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013965 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013966 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013967 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013968 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013969 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070013970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013971
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013972 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13973 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13974 tp->phy_otp = tg3_read_otp_phycfg(tp);
13975 if (tp->phy_otp == 0)
13976 tp->phy_otp = TG3_OTP_DEFAULT;
13977 }
13978
Joe Perches63c3a662011-04-26 08:12:10 +000013979 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070013980 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13981 else
13982 tp->mi_mode = MAC_MI_MODE_BASE;
13983
Linus Torvalds1da177e2005-04-16 15:20:36 -070013984 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013985 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13986 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13987 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13988
Matt Carlson4d958472011-04-20 07:57:35 +000013989 /* Set these bits to enable statistics workaround. */
13990 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13991 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
13992 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
13993 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
13994 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
13995 }
13996
Matt Carlson321d32a2008-11-21 17:22:19 -080013997 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13998 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000013999 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014000
Matt Carlson158d7ab2008-05-29 01:37:54 -070014001 err = tg3_mdio_init(tp);
14002 if (err)
14003 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014004
14005 /* Initialize data/descriptor byte/word swapping. */
14006 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014007 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14008 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14009 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14010 GRC_MODE_B2HRX_ENABLE |
14011 GRC_MODE_HTX2B_ENABLE |
14012 GRC_MODE_HOST_STACKUP);
14013 else
14014 val &= GRC_MODE_HOST_STACKUP;
14015
Linus Torvalds1da177e2005-04-16 15:20:36 -070014016 tw32(GRC_MODE, val | tp->grc_mode);
14017
14018 tg3_switch_clocks(tp);
14019
14020 /* Clear this out for sanity. */
14021 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14022
14023 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14024 &pci_state_reg);
14025 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014026 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014027 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14028
14029 if (chiprevid == CHIPREV_ID_5701_A0 ||
14030 chiprevid == CHIPREV_ID_5701_B0 ||
14031 chiprevid == CHIPREV_ID_5701_B2 ||
14032 chiprevid == CHIPREV_ID_5701_B5) {
14033 void __iomem *sram_base;
14034
14035 /* Write some dummy words into the SRAM status block
14036 * area, see if it reads back correctly. If the return
14037 * value is bad, force enable the PCIX workaround.
14038 */
14039 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14040
14041 writel(0x00000000, sram_base);
14042 writel(0x00000000, sram_base + 4);
14043 writel(0xffffffff, sram_base + 4);
14044 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014045 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014046 }
14047 }
14048
14049 udelay(50);
14050 tg3_nvram_init(tp);
14051
14052 grc_misc_cfg = tr32(GRC_MISC_CFG);
14053 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14054
Linus Torvalds1da177e2005-04-16 15:20:36 -070014055 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14056 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14057 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014058 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014059
Joe Perches63c3a662011-04-26 08:12:10 +000014060 if (!tg3_flag(tp, IS_5788) &&
David S. Millerfac9b832005-05-18 22:46:34 -070014061 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
Joe Perches63c3a662011-04-26 08:12:10 +000014062 tg3_flag_set(tp, TAGGED_STATUS);
14063 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014064 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14065 HOSTCC_MODE_CLRTICK_TXBD);
14066
14067 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14068 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14069 tp->misc_host_ctrl);
14070 }
14071
Matt Carlson3bda1252008-08-15 14:08:22 -070014072 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014073 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014074 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014075 else
14076 tp->mac_mode = TG3_DEF_MAC_MODE;
14077
Linus Torvalds1da177e2005-04-16 15:20:36 -070014078 /* these are limited to 10/100 only */
14079 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14080 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14081 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14082 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14083 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14084 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14085 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14086 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14087 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014088 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14089 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014090 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014091 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14092 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014093 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14094 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014095
14096 err = tg3_phy_probe(tp);
14097 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014098 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014099 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014100 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014101 }
14102
Matt Carlson184b8902010-04-05 10:19:25 +000014103 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014104 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014105
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014106 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14107 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014108 } else {
14109 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014110 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014111 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014112 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014113 }
14114
14115 /* 5700 {AX,BX} chips have a broken status block link
14116 * change bit implementation, so we must use the
14117 * status register in those cases.
14118 */
14119 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014120 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014121 else
Joe Perches63c3a662011-04-26 08:12:10 +000014122 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014123
14124 /* The led_ctrl is set during tg3_phy_probe, here we might
14125 * have to force the link status polling mechanism based
14126 * upon subsystem IDs.
14127 */
14128 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014129 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014130 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14131 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014132 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014133 }
14134
14135 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014136 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014137 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014138 else
Joe Perches63c3a662011-04-26 08:12:10 +000014139 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014140
Matt Carlsonbf933c82011-01-25 15:58:49 +000014141 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014142 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014143 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014144 tg3_flag(tp, PCIX_MODE)) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014145 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014146#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014147 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014148#endif
14149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014150
Matt Carlson2c49a442010-09-30 10:34:35 +000014151 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14152 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014153 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14154
Matt Carlson2c49a442010-09-30 10:34:35 +000014155 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014156
14157 /* Increment the rx prod index on the rx std ring by at most
14158 * 8 for these chips to workaround hw errata.
14159 */
14160 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14161 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14162 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14163 tp->rx_std_max_post = 8;
14164
Joe Perches63c3a662011-04-26 08:12:10 +000014165 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014166 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14167 PCIE_PWR_MGMT_L1_THRESH_MSK;
14168
Linus Torvalds1da177e2005-04-16 15:20:36 -070014169 return err;
14170}
14171
David S. Miller49b6e95f2007-03-29 01:38:42 -070014172#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014173static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14174{
14175 struct net_device *dev = tp->dev;
14176 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014177 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014178 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014179 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014180
David S. Miller49b6e95f2007-03-29 01:38:42 -070014181 addr = of_get_property(dp, "local-mac-address", &len);
14182 if (addr && len == 6) {
14183 memcpy(dev->dev_addr, addr, 6);
14184 memcpy(dev->perm_addr, dev->dev_addr, 6);
14185 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014186 }
14187 return -ENODEV;
14188}
14189
14190static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14191{
14192 struct net_device *dev = tp->dev;
14193
14194 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014195 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014196 return 0;
14197}
14198#endif
14199
14200static int __devinit tg3_get_device_address(struct tg3 *tp)
14201{
14202 struct net_device *dev = tp->dev;
14203 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014204 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014205
David S. Miller49b6e95f2007-03-29 01:38:42 -070014206#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014207 if (!tg3_get_macaddr_sparc(tp))
14208 return 0;
14209#endif
14210
14211 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070014212 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Joe Perches63c3a662011-04-26 08:12:10 +000014213 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014214 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14215 mac_offset = 0xcc;
14216 if (tg3_nvram_lock(tp))
14217 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14218 else
14219 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014220 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlsona50d0792010-06-05 17:24:37 +000014221 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014222 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000014223 if (PCI_FUNC(tp->pdev->devfn) > 1)
14224 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014225 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014226 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014227
14228 /* First try to get it from MAC address mailbox. */
14229 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14230 if ((hi >> 16) == 0x484b) {
14231 dev->dev_addr[0] = (hi >> 8) & 0xff;
14232 dev->dev_addr[1] = (hi >> 0) & 0xff;
14233
14234 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14235 dev->dev_addr[2] = (lo >> 24) & 0xff;
14236 dev->dev_addr[3] = (lo >> 16) & 0xff;
14237 dev->dev_addr[4] = (lo >> 8) & 0xff;
14238 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014239
Michael Chan008652b2006-03-27 23:14:53 -080014240 /* Some old bootcode may report a 0 MAC address in SRAM */
14241 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14242 }
14243 if (!addr_ok) {
14244 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014245 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014246 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014247 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014248 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14249 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014250 }
14251 /* Finally just fetch it out of the MAC control regs. */
14252 else {
14253 hi = tr32(MAC_ADDR_0_HIGH);
14254 lo = tr32(MAC_ADDR_0_LOW);
14255
14256 dev->dev_addr[5] = lo & 0xff;
14257 dev->dev_addr[4] = (lo >> 8) & 0xff;
14258 dev->dev_addr[3] = (lo >> 16) & 0xff;
14259 dev->dev_addr[2] = (lo >> 24) & 0xff;
14260 dev->dev_addr[1] = hi & 0xff;
14261 dev->dev_addr[0] = (hi >> 8) & 0xff;
14262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014263 }
14264
14265 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014266#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014267 if (!tg3_get_default_macaddr_sparc(tp))
14268 return 0;
14269#endif
14270 return -EINVAL;
14271 }
John W. Linville2ff43692005-09-12 14:44:20 -070014272 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014273 return 0;
14274}
14275
David S. Miller59e6b432005-05-18 22:50:10 -070014276#define BOUNDARY_SINGLE_CACHELINE 1
14277#define BOUNDARY_MULTI_CACHELINE 2
14278
14279static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14280{
14281 int cacheline_size;
14282 u8 byte;
14283 int goal;
14284
14285 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14286 if (byte == 0)
14287 cacheline_size = 1024;
14288 else
14289 cacheline_size = (int) byte * 4;
14290
14291 /* On 5703 and later chips, the boundary bits have no
14292 * effect.
14293 */
14294 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14295 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014296 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014297 goto out;
14298
14299#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14300 goal = BOUNDARY_MULTI_CACHELINE;
14301#else
14302#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14303 goal = BOUNDARY_SINGLE_CACHELINE;
14304#else
14305 goal = 0;
14306#endif
14307#endif
14308
Joe Perches63c3a662011-04-26 08:12:10 +000014309 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014310 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14311 goto out;
14312 }
14313
David S. Miller59e6b432005-05-18 22:50:10 -070014314 if (!goal)
14315 goto out;
14316
14317 /* PCI controllers on most RISC systems tend to disconnect
14318 * when a device tries to burst across a cache-line boundary.
14319 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14320 *
14321 * Unfortunately, for PCI-E there are only limited
14322 * write-side controls for this, and thus for reads
14323 * we will still get the disconnects. We'll also waste
14324 * these PCI cycles for both read and write for chips
14325 * other than 5700 and 5701 which do not implement the
14326 * boundary bits.
14327 */
Joe Perches63c3a662011-04-26 08:12:10 +000014328 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014329 switch (cacheline_size) {
14330 case 16:
14331 case 32:
14332 case 64:
14333 case 128:
14334 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14335 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14336 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14337 } else {
14338 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14339 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14340 }
14341 break;
14342
14343 case 256:
14344 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14345 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14346 break;
14347
14348 default:
14349 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14350 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14351 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014352 }
Joe Perches63c3a662011-04-26 08:12:10 +000014353 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014354 switch (cacheline_size) {
14355 case 16:
14356 case 32:
14357 case 64:
14358 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14359 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14360 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14361 break;
14362 }
14363 /* fallthrough */
14364 case 128:
14365 default:
14366 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14367 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14368 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014369 }
David S. Miller59e6b432005-05-18 22:50:10 -070014370 } else {
14371 switch (cacheline_size) {
14372 case 16:
14373 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14374 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14375 DMA_RWCTRL_WRITE_BNDRY_16);
14376 break;
14377 }
14378 /* fallthrough */
14379 case 32:
14380 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14381 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14382 DMA_RWCTRL_WRITE_BNDRY_32);
14383 break;
14384 }
14385 /* fallthrough */
14386 case 64:
14387 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14388 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14389 DMA_RWCTRL_WRITE_BNDRY_64);
14390 break;
14391 }
14392 /* fallthrough */
14393 case 128:
14394 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14395 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14396 DMA_RWCTRL_WRITE_BNDRY_128);
14397 break;
14398 }
14399 /* fallthrough */
14400 case 256:
14401 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14402 DMA_RWCTRL_WRITE_BNDRY_256);
14403 break;
14404 case 512:
14405 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14406 DMA_RWCTRL_WRITE_BNDRY_512);
14407 break;
14408 case 1024:
14409 default:
14410 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14411 DMA_RWCTRL_WRITE_BNDRY_1024);
14412 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014413 }
David S. Miller59e6b432005-05-18 22:50:10 -070014414 }
14415
14416out:
14417 return val;
14418}
14419
Linus Torvalds1da177e2005-04-16 15:20:36 -070014420static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14421{
14422 struct tg3_internal_buffer_desc test_desc;
14423 u32 sram_dma_descs;
14424 int i, ret;
14425
14426 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14427
14428 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14429 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14430 tw32(RDMAC_STATUS, 0);
14431 tw32(WDMAC_STATUS, 0);
14432
14433 tw32(BUFMGR_MODE, 0);
14434 tw32(FTQ_RESET, 0);
14435
14436 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14437 test_desc.addr_lo = buf_dma & 0xffffffff;
14438 test_desc.nic_mbuf = 0x00002100;
14439 test_desc.len = size;
14440
14441 /*
14442 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14443 * the *second* time the tg3 driver was getting loaded after an
14444 * initial scan.
14445 *
14446 * Broadcom tells me:
14447 * ...the DMA engine is connected to the GRC block and a DMA
14448 * reset may affect the GRC block in some unpredictable way...
14449 * The behavior of resets to individual blocks has not been tested.
14450 *
14451 * Broadcom noted the GRC reset will also reset all sub-components.
14452 */
14453 if (to_device) {
14454 test_desc.cqid_sqid = (13 << 8) | 2;
14455
14456 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14457 udelay(40);
14458 } else {
14459 test_desc.cqid_sqid = (16 << 8) | 7;
14460
14461 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14462 udelay(40);
14463 }
14464 test_desc.flags = 0x00000005;
14465
14466 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14467 u32 val;
14468
14469 val = *(((u32 *)&test_desc) + i);
14470 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14471 sram_dma_descs + (i * sizeof(u32)));
14472 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14473 }
14474 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14475
Matt Carlson859a588792010-04-05 10:19:28 +000014476 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014477 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000014478 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014479 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014480
14481 ret = -ENODEV;
14482 for (i = 0; i < 40; i++) {
14483 u32 val;
14484
14485 if (to_device)
14486 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14487 else
14488 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14489 if ((val & 0xffff) == sram_dma_descs) {
14490 ret = 0;
14491 break;
14492 }
14493
14494 udelay(100);
14495 }
14496
14497 return ret;
14498}
14499
David S. Millerded73402005-05-23 13:59:47 -070014500#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014501
Matt Carlson41434702011-03-09 16:58:22 +000014502static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014503 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14504 { },
14505};
14506
Linus Torvalds1da177e2005-04-16 15:20:36 -070014507static int __devinit tg3_test_dma(struct tg3 *tp)
14508{
14509 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014510 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014511 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014512
Matt Carlson4bae65c2010-11-24 08:31:52 +000014513 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14514 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014515 if (!buf) {
14516 ret = -ENOMEM;
14517 goto out_nofree;
14518 }
14519
14520 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14521 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14522
David S. Miller59e6b432005-05-18 22:50:10 -070014523 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014524
Joe Perches63c3a662011-04-26 08:12:10 +000014525 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014526 goto out;
14527
Joe Perches63c3a662011-04-26 08:12:10 +000014528 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014529 /* DMA read watermark not used on PCIE */
14530 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014531 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014532 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14533 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014534 tp->dma_rwctrl |= 0x003f0000;
14535 else
14536 tp->dma_rwctrl |= 0x003f000f;
14537 } else {
14538 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14539 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14540 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014541 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014542
Michael Chan4a29cc22006-03-19 13:21:12 -080014543 /* If the 5704 is behind the EPB bridge, we can
14544 * do the less restrictive ONE_DMA workaround for
14545 * better performance.
14546 */
Joe Perches63c3a662011-04-26 08:12:10 +000014547 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014548 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14549 tp->dma_rwctrl |= 0x8000;
14550 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014551 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14552
Michael Chan49afdeb2007-02-13 12:17:03 -080014553 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14554 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014555 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014556 tp->dma_rwctrl |=
14557 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14558 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14559 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014560 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14561 /* 5780 always in PCIX mode */
14562 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014563 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14564 /* 5714 always in PCIX mode */
14565 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014566 } else {
14567 tp->dma_rwctrl |= 0x001b000f;
14568 }
14569 }
14570
14571 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14572 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14573 tp->dma_rwctrl &= 0xfffffff0;
14574
14575 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14576 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14577 /* Remove this if it causes problems for some boards. */
14578 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14579
14580 /* On 5700/5701 chips, we need to set this bit.
14581 * Otherwise the chip will issue cacheline transactions
14582 * to streamable DMA memory with not all the byte
14583 * enables turned on. This is an error on several
14584 * RISC PCI controllers, in particular sparc64.
14585 *
14586 * On 5703/5704 chips, this bit has been reassigned
14587 * a different meaning. In particular, it is used
14588 * on those chips to enable a PCI-X workaround.
14589 */
14590 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14591 }
14592
14593 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14594
14595#if 0
14596 /* Unneeded, already done by tg3_get_invariants. */
14597 tg3_switch_clocks(tp);
14598#endif
14599
Linus Torvalds1da177e2005-04-16 15:20:36 -070014600 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14601 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14602 goto out;
14603
David S. Miller59e6b432005-05-18 22:50:10 -070014604 /* It is best to perform DMA test with maximum write burst size
14605 * to expose the 5700/5701 write DMA bug.
14606 */
14607 saved_dma_rwctrl = tp->dma_rwctrl;
14608 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14609 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14610
Linus Torvalds1da177e2005-04-16 15:20:36 -070014611 while (1) {
14612 u32 *p = buf, i;
14613
14614 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14615 p[i] = i;
14616
14617 /* Send the buffer to the chip. */
14618 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14619 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014620 dev_err(&tp->pdev->dev,
14621 "%s: Buffer write failed. err = %d\n",
14622 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014623 break;
14624 }
14625
14626#if 0
14627 /* validate data reached card RAM correctly. */
14628 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14629 u32 val;
14630 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14631 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014632 dev_err(&tp->pdev->dev,
14633 "%s: Buffer corrupted on device! "
14634 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014635 /* ret = -ENODEV here? */
14636 }
14637 p[i] = 0;
14638 }
14639#endif
14640 /* Now read it back. */
14641 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14642 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014643 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14644 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014645 break;
14646 }
14647
14648 /* Verify it. */
14649 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14650 if (p[i] == i)
14651 continue;
14652
David S. Miller59e6b432005-05-18 22:50:10 -070014653 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14654 DMA_RWCTRL_WRITE_BNDRY_16) {
14655 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014656 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14657 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14658 break;
14659 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014660 dev_err(&tp->pdev->dev,
14661 "%s: Buffer corrupted on read back! "
14662 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014663 ret = -ENODEV;
14664 goto out;
14665 }
14666 }
14667
14668 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14669 /* Success. */
14670 ret = 0;
14671 break;
14672 }
14673 }
David S. Miller59e6b432005-05-18 22:50:10 -070014674 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14675 DMA_RWCTRL_WRITE_BNDRY_16) {
14676 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014677 * now look for chipsets that are known to expose the
14678 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014679 */
Matt Carlson41434702011-03-09 16:58:22 +000014680 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014681 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14682 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000014683 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014684 /* Safe to use the calculated DMA boundary. */
14685 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000014686 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014687
David S. Miller59e6b432005-05-18 22:50:10 -070014688 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014690
14691out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000014692 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014693out_nofree:
14694 return ret;
14695}
14696
Linus Torvalds1da177e2005-04-16 15:20:36 -070014697static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14698{
Joe Perches63c3a662011-04-26 08:12:10 +000014699 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000014700 tp->bufmgr_config.mbuf_read_dma_low_water =
14701 DEFAULT_MB_RDMA_LOW_WATER_5705;
14702 tp->bufmgr_config.mbuf_mac_rx_low_water =
14703 DEFAULT_MB_MACRX_LOW_WATER_57765;
14704 tp->bufmgr_config.mbuf_high_water =
14705 DEFAULT_MB_HIGH_WATER_57765;
14706
14707 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14708 DEFAULT_MB_RDMA_LOW_WATER_5705;
14709 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14710 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14711 tp->bufmgr_config.mbuf_high_water_jumbo =
14712 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000014713 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014714 tp->bufmgr_config.mbuf_read_dma_low_water =
14715 DEFAULT_MB_RDMA_LOW_WATER_5705;
14716 tp->bufmgr_config.mbuf_mac_rx_low_water =
14717 DEFAULT_MB_MACRX_LOW_WATER_5705;
14718 tp->bufmgr_config.mbuf_high_water =
14719 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014720 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14721 tp->bufmgr_config.mbuf_mac_rx_low_water =
14722 DEFAULT_MB_MACRX_LOW_WATER_5906;
14723 tp->bufmgr_config.mbuf_high_water =
14724 DEFAULT_MB_HIGH_WATER_5906;
14725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014726
Michael Chanfdfec1722005-07-25 12:31:48 -070014727 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14728 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14729 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14730 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14731 tp->bufmgr_config.mbuf_high_water_jumbo =
14732 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14733 } else {
14734 tp->bufmgr_config.mbuf_read_dma_low_water =
14735 DEFAULT_MB_RDMA_LOW_WATER;
14736 tp->bufmgr_config.mbuf_mac_rx_low_water =
14737 DEFAULT_MB_MACRX_LOW_WATER;
14738 tp->bufmgr_config.mbuf_high_water =
14739 DEFAULT_MB_HIGH_WATER;
14740
14741 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14742 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14743 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14744 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14745 tp->bufmgr_config.mbuf_high_water_jumbo =
14746 DEFAULT_MB_HIGH_WATER_JUMBO;
14747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014748
14749 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14750 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14751}
14752
14753static char * __devinit tg3_phy_string(struct tg3 *tp)
14754{
Matt Carlson79eb6902010-02-17 15:17:03 +000014755 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14756 case TG3_PHY_ID_BCM5400: return "5400";
14757 case TG3_PHY_ID_BCM5401: return "5401";
14758 case TG3_PHY_ID_BCM5411: return "5411";
14759 case TG3_PHY_ID_BCM5701: return "5701";
14760 case TG3_PHY_ID_BCM5703: return "5703";
14761 case TG3_PHY_ID_BCM5704: return "5704";
14762 case TG3_PHY_ID_BCM5705: return "5705";
14763 case TG3_PHY_ID_BCM5750: return "5750";
14764 case TG3_PHY_ID_BCM5752: return "5752";
14765 case TG3_PHY_ID_BCM5714: return "5714";
14766 case TG3_PHY_ID_BCM5780: return "5780";
14767 case TG3_PHY_ID_BCM5755: return "5755";
14768 case TG3_PHY_ID_BCM5787: return "5787";
14769 case TG3_PHY_ID_BCM5784: return "5784";
14770 case TG3_PHY_ID_BCM5756: return "5722/5756";
14771 case TG3_PHY_ID_BCM5906: return "5906";
14772 case TG3_PHY_ID_BCM5761: return "5761";
14773 case TG3_PHY_ID_BCM5718C: return "5718C";
14774 case TG3_PHY_ID_BCM5718S: return "5718S";
14775 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014776 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000014777 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014778 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014779 case 0: return "serdes";
14780 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014782}
14783
Michael Chanf9804dd2005-09-27 12:13:10 -070014784static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14785{
Joe Perches63c3a662011-04-26 08:12:10 +000014786 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014787 strcpy(str, "PCI Express");
14788 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000014789 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014790 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14791
14792 strcpy(str, "PCIX:");
14793
14794 if ((clock_ctrl == 7) ||
14795 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14796 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14797 strcat(str, "133MHz");
14798 else if (clock_ctrl == 0)
14799 strcat(str, "33MHz");
14800 else if (clock_ctrl == 2)
14801 strcat(str, "50MHz");
14802 else if (clock_ctrl == 4)
14803 strcat(str, "66MHz");
14804 else if (clock_ctrl == 6)
14805 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014806 } else {
14807 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000014808 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070014809 strcat(str, "66MHz");
14810 else
14811 strcat(str, "33MHz");
14812 }
Joe Perches63c3a662011-04-26 08:12:10 +000014813 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070014814 strcat(str, ":32-bit");
14815 else
14816 strcat(str, ":64-bit");
14817 return str;
14818}
14819
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014820static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014821{
14822 struct pci_dev *peer;
14823 unsigned int func, devnr = tp->pdev->devfn & ~7;
14824
14825 for (func = 0; func < 8; func++) {
14826 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14827 if (peer && peer != tp->pdev)
14828 break;
14829 pci_dev_put(peer);
14830 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014831 /* 5704 can be configured in single-port mode, set peer to
14832 * tp->pdev in that case.
14833 */
14834 if (!peer) {
14835 peer = tp->pdev;
14836 return peer;
14837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014838
14839 /*
14840 * We don't need to keep the refcount elevated; there's no way
14841 * to remove one half of this device without removing the other
14842 */
14843 pci_dev_put(peer);
14844
14845 return peer;
14846}
14847
David S. Miller15f98502005-05-18 22:49:26 -070014848static void __devinit tg3_init_coal(struct tg3 *tp)
14849{
14850 struct ethtool_coalesce *ec = &tp->coal;
14851
14852 memset(ec, 0, sizeof(*ec));
14853 ec->cmd = ETHTOOL_GCOALESCE;
14854 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14855 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14856 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14857 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14858 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14859 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14860 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14861 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14862 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14863
14864 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14865 HOSTCC_MODE_CLRTICK_TXBD)) {
14866 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14867 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14868 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14869 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14870 }
Michael Chand244c892005-07-05 14:42:33 -070014871
Joe Perches63c3a662011-04-26 08:12:10 +000014872 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070014873 ec->rx_coalesce_usecs_irq = 0;
14874 ec->tx_coalesce_usecs_irq = 0;
14875 ec->stats_block_coalesce_usecs = 0;
14876 }
David S. Miller15f98502005-05-18 22:49:26 -070014877}
14878
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014879static const struct net_device_ops tg3_netdev_ops = {
14880 .ndo_open = tg3_open,
14881 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014882 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014883 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014884 .ndo_validate_addr = eth_validate_addr,
14885 .ndo_set_multicast_list = tg3_set_rx_mode,
14886 .ndo_set_mac_address = tg3_set_mac_addr,
14887 .ndo_do_ioctl = tg3_ioctl,
14888 .ndo_tx_timeout = tg3_tx_timeout,
14889 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000014890 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000014891 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080014892#ifdef CONFIG_NET_POLL_CONTROLLER
14893 .ndo_poll_controller = tg3_poll_controller,
14894#endif
14895};
14896
Linus Torvalds1da177e2005-04-16 15:20:36 -070014897static int __devinit tg3_init_one(struct pci_dev *pdev,
14898 const struct pci_device_id *ent)
14899{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014900 struct net_device *dev;
14901 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014902 int i, err, pm_cap;
14903 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014904 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014905 u64 dma_mask, persist_dma_mask;
Michał Mirosławdc668912011-04-07 03:35:07 +000014906 u32 hw_features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014907
Joe Perches05dbe002010-02-17 19:44:19 +000014908 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014909
14910 err = pci_enable_device(pdev);
14911 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014912 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014913 return err;
14914 }
14915
Linus Torvalds1da177e2005-04-16 15:20:36 -070014916 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14917 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014918 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014919 goto err_out_disable_pdev;
14920 }
14921
14922 pci_set_master(pdev);
14923
14924 /* Find power-management capability. */
14925 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14926 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014927 dev_err(&pdev->dev,
14928 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014929 err = -EIO;
14930 goto err_out_free_res;
14931 }
14932
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014933 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014934 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014935 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014936 err = -ENOMEM;
14937 goto err_out_free_res;
14938 }
14939
Linus Torvalds1da177e2005-04-16 15:20:36 -070014940 SET_NETDEV_DEV(dev, &pdev->dev);
14941
Linus Torvalds1da177e2005-04-16 15:20:36 -070014942 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014943
14944 tp = netdev_priv(dev);
14945 tp->pdev = pdev;
14946 tp->dev = dev;
14947 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014948 tp->rx_mode = TG3_DEF_RX_MODE;
14949 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014950
Linus Torvalds1da177e2005-04-16 15:20:36 -070014951 if (tg3_debug > 0)
14952 tp->msg_enable = tg3_debug;
14953 else
14954 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14955
14956 /* The word/byte swap controls here control register access byte
14957 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14958 * setting below.
14959 */
14960 tp->misc_host_ctrl =
14961 MISC_HOST_CTRL_MASK_PCI_INT |
14962 MISC_HOST_CTRL_WORD_SWAP |
14963 MISC_HOST_CTRL_INDIR_ACCESS |
14964 MISC_HOST_CTRL_PCISTATE_RW;
14965
14966 /* The NONFRM (non-frame) byte/word swap controls take effect
14967 * on descriptor entries, anything which isn't packet data.
14968 *
14969 * The StrongARM chips on the board (one for tx, one for rx)
14970 * are running in big-endian mode.
14971 */
14972 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14973 GRC_MODE_WSWAP_NONFRM_DATA);
14974#ifdef __BIG_ENDIAN
14975 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14976#endif
14977 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014978 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014979 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014980
Matt Carlsond5fe4882008-11-21 17:20:32 -080014981 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014982 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014983 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014984 err = -ENOMEM;
14985 goto err_out_free_dev;
14986 }
14987
Linus Torvalds1da177e2005-04-16 15:20:36 -070014988 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14989 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014990
Linus Torvalds1da177e2005-04-16 15:20:36 -070014991 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014992 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000014993 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014994 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014995
14996 err = tg3_get_invariants(tp);
14997 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014998 dev_err(&pdev->dev,
14999 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015000 goto err_out_iounmap;
15001 }
15002
Michael Chan4a29cc22006-03-19 13:21:12 -080015003 /* The EPB bridge inside 5714, 5715, and 5780 and any
15004 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015005 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15006 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15007 * do DMA address check in tg3_start_xmit().
15008 */
Joe Perches63c3a662011-04-26 08:12:10 +000015009 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015010 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015011 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015012 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015013#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015014 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015015#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015016 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015017 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015018
15019 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015020 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015021 err = pci_set_dma_mask(pdev, dma_mask);
15022 if (!err) {
15023 dev->features |= NETIF_F_HIGHDMA;
15024 err = pci_set_consistent_dma_mask(pdev,
15025 persist_dma_mask);
15026 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015027 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15028 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015029 goto err_out_iounmap;
15030 }
15031 }
15032 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015033 if (err || dma_mask == DMA_BIT_MASK(32)) {
15034 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015035 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015036 dev_err(&pdev->dev,
15037 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015038 goto err_out_iounmap;
15039 }
15040 }
15041
Michael Chanfdfec1722005-07-25 12:31:48 -070015042 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015043
Matt Carlson507399f2009-11-13 13:03:37 +000015044 /* Selectively allow TSO based on operating conditions */
Joe Perches63c3a662011-04-26 08:12:10 +000015045 if ((tg3_flag(tp, HW_TSO_1) ||
15046 tg3_flag(tp, HW_TSO_2) ||
15047 tg3_flag(tp, HW_TSO_3)) ||
15048 (tp->fw_needed && !tg3_flag(tp, ENABLE_ASF)))
15049 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson507399f2009-11-13 13:03:37 +000015050 else {
Joe Perches63c3a662011-04-26 08:12:10 +000015051 tg3_flag_clear(tp, TSO_CAPABLE);
15052 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000015053 tp->fw_needed = NULL;
15054 }
15055
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015056 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080015057 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015058
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015059 /* TSO is on by default on chips that support hardware TSO.
15060 * Firmware TSO on older chips gives lower performance, so it
15061 * is off by default, but can be enabled using ethtool.
15062 */
Joe Perches63c3a662011-04-26 08:12:10 +000015063 if ((tg3_flag(tp, HW_TSO_1) ||
15064 tg3_flag(tp, HW_TSO_2) ||
15065 tg3_flag(tp, HW_TSO_3)) &&
Michał Mirosławdc668912011-04-07 03:35:07 +000015066 (dev->features & NETIF_F_IP_CSUM))
15067 hw_features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015068 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Michał Mirosławdc668912011-04-07 03:35:07 +000015069 if (dev->features & NETIF_F_IPV6_CSUM)
15070 hw_features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015071 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015072 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015073 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15074 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015075 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015076 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
15077 hw_features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015079
Michał Mirosławdc668912011-04-07 03:35:07 +000015080 dev->hw_features |= hw_features;
15081 dev->features |= hw_features;
15082 dev->vlan_features |= hw_features;
15083
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015084 /*
15085 * Add loopback capability only for a subset of devices that support
15086 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15087 * loopback for the remaining devices.
15088 */
15089 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15090 !tg3_flag(tp, CPMU_PRESENT))
15091 /* Add the loopback capability */
15092 dev->hw_features |= NETIF_F_LOOPBACK;
15093
Linus Torvalds1da177e2005-04-16 15:20:36 -070015094 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015095 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015096 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015097 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015098 tp->rx_pending = 63;
15099 }
15100
Linus Torvalds1da177e2005-04-16 15:20:36 -070015101 err = tg3_get_device_address(tp);
15102 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015103 dev_err(&pdev->dev,
15104 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000015105 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015106 }
15107
Joe Perches63c3a662011-04-26 08:12:10 +000015108 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson63532392008-11-03 16:49:57 -080015109 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080015110 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015111 dev_err(&pdev->dev,
15112 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015113 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000015114 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015115 }
15116
15117 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000015118
Joe Perches63c3a662011-04-26 08:12:10 +000015119 if (tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000015120 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015121 }
15122
Matt Carlsonc88864d2007-11-12 21:07:01 -080015123 /*
15124 * Reset chip in case UNDI or EFI driver did not shutdown
15125 * DMA self test will enable WDMAC and we'll see (spurious)
15126 * pending DMA on the PCI bus at that point.
15127 */
15128 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15129 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15130 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15131 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15132 }
15133
15134 err = tg3_test_dma(tp);
15135 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015136 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015137 goto err_out_apeunmap;
15138 }
15139
Matt Carlson78f90dc2009-11-13 13:03:42 +000015140 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15141 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15142 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015143 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015144 struct tg3_napi *tnapi = &tp->napi[i];
15145
15146 tnapi->tp = tp;
15147 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15148
15149 tnapi->int_mbox = intmbx;
15150 if (i < 4)
15151 intmbx += 0x8;
15152 else
15153 intmbx += 0x4;
15154
15155 tnapi->consmbox = rcvmbx;
15156 tnapi->prodmbox = sndmbx;
15157
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015158 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015159 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015160 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015161 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015162
Joe Perches63c3a662011-04-26 08:12:10 +000015163 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015164 break;
15165
15166 /*
15167 * If we support MSIX, we'll be using RSS. If we're using
15168 * RSS, the first vector only handles link interrupts and the
15169 * remaining vectors handle rx and tx interrupts. Reuse the
15170 * mailbox values for the next iteration. The values we setup
15171 * above are still useful for the single vectored mode.
15172 */
15173 if (!i)
15174 continue;
15175
15176 rcvmbx += 0x8;
15177
15178 if (sndmbx & 0x4)
15179 sndmbx -= 0x4;
15180 else
15181 sndmbx += 0xc;
15182 }
15183
Matt Carlsonc88864d2007-11-12 21:07:01 -080015184 tg3_init_coal(tp);
15185
Michael Chanc49a1562006-12-17 17:07:29 -080015186 pci_set_drvdata(pdev, dev);
15187
Linus Torvalds1da177e2005-04-16 15:20:36 -070015188 err = register_netdev(dev);
15189 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015190 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015191 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015192 }
15193
Joe Perches05dbe002010-02-17 19:44:19 +000015194 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15195 tp->board_part_number,
15196 tp->pci_chip_rev_id,
15197 tg3_bus_string(tp, str),
15198 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015199
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015200 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015201 struct phy_device *phydev;
15202 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015203 netdev_info(dev,
15204 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015205 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015206 } else {
15207 char *ethtype;
15208
15209 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15210 ethtype = "10/100Base-TX";
15211 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15212 ethtype = "1000Base-SX";
15213 else
15214 ethtype = "10/100/1000Base-T";
15215
Matt Carlson5129c3a2010-04-05 10:19:23 +000015216 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015217 "(WireSpeed[%d], EEE[%d])\n",
15218 tg3_phy_string(tp), ethtype,
15219 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15220 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015221 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015222
Joe Perches05dbe002010-02-17 19:44:19 +000015223 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015224 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015225 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015226 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015227 tg3_flag(tp, ENABLE_ASF) != 0,
15228 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015229 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15230 tp->dma_rwctrl,
15231 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15232 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015233
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015234 pci_save_state(pdev);
15235
Linus Torvalds1da177e2005-04-16 15:20:36 -070015236 return 0;
15237
Matt Carlson0d3031d2007-10-10 18:02:43 -070015238err_out_apeunmap:
15239 if (tp->aperegs) {
15240 iounmap(tp->aperegs);
15241 tp->aperegs = NULL;
15242 }
15243
Linus Torvalds1da177e2005-04-16 15:20:36 -070015244err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015245 if (tp->regs) {
15246 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015247 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015249
15250err_out_free_dev:
15251 free_netdev(dev);
15252
15253err_out_free_res:
15254 pci_release_regions(pdev);
15255
15256err_out_disable_pdev:
15257 pci_disable_device(pdev);
15258 pci_set_drvdata(pdev, NULL);
15259 return err;
15260}
15261
15262static void __devexit tg3_remove_one(struct pci_dev *pdev)
15263{
15264 struct net_device *dev = pci_get_drvdata(pdev);
15265
15266 if (dev) {
15267 struct tg3 *tp = netdev_priv(dev);
15268
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015269 if (tp->fw)
15270 release_firmware(tp->fw);
15271
Tejun Heo23f333a2010-12-12 16:45:14 +010015272 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015273
Joe Perches63c3a662011-04-26 08:12:10 +000015274 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015275 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015276 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015277 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015278
Linus Torvalds1da177e2005-04-16 15:20:36 -070015279 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015280 if (tp->aperegs) {
15281 iounmap(tp->aperegs);
15282 tp->aperegs = NULL;
15283 }
Michael Chan68929142005-08-09 20:17:14 -070015284 if (tp->regs) {
15285 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015286 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015288 free_netdev(dev);
15289 pci_release_regions(pdev);
15290 pci_disable_device(pdev);
15291 pci_set_drvdata(pdev, NULL);
15292 }
15293}
15294
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015295#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015296static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015297{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015298 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015299 struct net_device *dev = pci_get_drvdata(pdev);
15300 struct tg3 *tp = netdev_priv(dev);
15301 int err;
15302
15303 if (!netif_running(dev))
15304 return 0;
15305
Tejun Heo23f333a2010-12-12 16:45:14 +010015306 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015307 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015308 tg3_netif_stop(tp);
15309
15310 del_timer_sync(&tp->timer);
15311
David S. Millerf47c11e2005-06-24 20:18:35 -070015312 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015313 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015314 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015315
15316 netif_device_detach(dev);
15317
David S. Millerf47c11e2005-06-24 20:18:35 -070015318 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015319 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015320 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015321 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015322
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015323 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015324 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015325 int err2;
15326
David S. Millerf47c11e2005-06-24 20:18:35 -070015327 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015328
Joe Perches63c3a662011-04-26 08:12:10 +000015329 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015330 err2 = tg3_restart_hw(tp, 1);
15331 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015332 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015333
15334 tp->timer.expires = jiffies + tp->timer_offset;
15335 add_timer(&tp->timer);
15336
15337 netif_device_attach(dev);
15338 tg3_netif_start(tp);
15339
Michael Chanb9ec6c12006-07-25 16:37:27 -070015340out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015341 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015342
15343 if (!err2)
15344 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015345 }
15346
15347 return err;
15348}
15349
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015350static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015351{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015352 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015353 struct net_device *dev = pci_get_drvdata(pdev);
15354 struct tg3 *tp = netdev_priv(dev);
15355 int err;
15356
15357 if (!netif_running(dev))
15358 return 0;
15359
Linus Torvalds1da177e2005-04-16 15:20:36 -070015360 netif_device_attach(dev);
15361
David S. Millerf47c11e2005-06-24 20:18:35 -070015362 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015363
Joe Perches63c3a662011-04-26 08:12:10 +000015364 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015365 err = tg3_restart_hw(tp, 1);
15366 if (err)
15367 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015368
15369 tp->timer.expires = jiffies + tp->timer_offset;
15370 add_timer(&tp->timer);
15371
Linus Torvalds1da177e2005-04-16 15:20:36 -070015372 tg3_netif_start(tp);
15373
Michael Chanb9ec6c12006-07-25 16:37:27 -070015374out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015375 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015376
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015377 if (!err)
15378 tg3_phy_start(tp);
15379
Michael Chanb9ec6c12006-07-25 16:37:27 -070015380 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015381}
15382
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015383static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015384#define TG3_PM_OPS (&tg3_pm_ops)
15385
15386#else
15387
15388#define TG3_PM_OPS NULL
15389
15390#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015391
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015392/**
15393 * tg3_io_error_detected - called when PCI error is detected
15394 * @pdev: Pointer to PCI device
15395 * @state: The current pci connection state
15396 *
15397 * This function is called after a PCI bus error affecting
15398 * this device has been detected.
15399 */
15400static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15401 pci_channel_state_t state)
15402{
15403 struct net_device *netdev = pci_get_drvdata(pdev);
15404 struct tg3 *tp = netdev_priv(netdev);
15405 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15406
15407 netdev_info(netdev, "PCI I/O error detected\n");
15408
15409 rtnl_lock();
15410
15411 if (!netif_running(netdev))
15412 goto done;
15413
15414 tg3_phy_stop(tp);
15415
15416 tg3_netif_stop(tp);
15417
15418 del_timer_sync(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +000015419 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015420
15421 /* Want to make sure that the reset task doesn't run */
15422 cancel_work_sync(&tp->reset_task);
Joe Perches63c3a662011-04-26 08:12:10 +000015423 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
15424 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015425
15426 netif_device_detach(netdev);
15427
15428 /* Clean up software state, even if MMIO is blocked */
15429 tg3_full_lock(tp, 0);
15430 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15431 tg3_full_unlock(tp);
15432
15433done:
15434 if (state == pci_channel_io_perm_failure)
15435 err = PCI_ERS_RESULT_DISCONNECT;
15436 else
15437 pci_disable_device(pdev);
15438
15439 rtnl_unlock();
15440
15441 return err;
15442}
15443
15444/**
15445 * tg3_io_slot_reset - called after the pci bus has been reset.
15446 * @pdev: Pointer to PCI device
15447 *
15448 * Restart the card from scratch, as if from a cold-boot.
15449 * At this point, the card has exprienced a hard reset,
15450 * followed by fixups by BIOS, and has its config space
15451 * set up identically to what it was at cold boot.
15452 */
15453static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15454{
15455 struct net_device *netdev = pci_get_drvdata(pdev);
15456 struct tg3 *tp = netdev_priv(netdev);
15457 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15458 int err;
15459
15460 rtnl_lock();
15461
15462 if (pci_enable_device(pdev)) {
15463 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15464 goto done;
15465 }
15466
15467 pci_set_master(pdev);
15468 pci_restore_state(pdev);
15469 pci_save_state(pdev);
15470
15471 if (!netif_running(netdev)) {
15472 rc = PCI_ERS_RESULT_RECOVERED;
15473 goto done;
15474 }
15475
15476 err = tg3_power_up(tp);
15477 if (err) {
15478 netdev_err(netdev, "Failed to restore register access.\n");
15479 goto done;
15480 }
15481
15482 rc = PCI_ERS_RESULT_RECOVERED;
15483
15484done:
15485 rtnl_unlock();
15486
15487 return rc;
15488}
15489
15490/**
15491 * tg3_io_resume - called when traffic can start flowing again.
15492 * @pdev: Pointer to PCI device
15493 *
15494 * This callback is called when the error recovery driver tells
15495 * us that its OK to resume normal operation.
15496 */
15497static void tg3_io_resume(struct pci_dev *pdev)
15498{
15499 struct net_device *netdev = pci_get_drvdata(pdev);
15500 struct tg3 *tp = netdev_priv(netdev);
15501 int err;
15502
15503 rtnl_lock();
15504
15505 if (!netif_running(netdev))
15506 goto done;
15507
15508 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015509 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015510 err = tg3_restart_hw(tp, 1);
15511 tg3_full_unlock(tp);
15512 if (err) {
15513 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15514 goto done;
15515 }
15516
15517 netif_device_attach(netdev);
15518
15519 tp->timer.expires = jiffies + tp->timer_offset;
15520 add_timer(&tp->timer);
15521
15522 tg3_netif_start(tp);
15523
15524 tg3_phy_start(tp);
15525
15526done:
15527 rtnl_unlock();
15528}
15529
15530static struct pci_error_handlers tg3_err_handler = {
15531 .error_detected = tg3_io_error_detected,
15532 .slot_reset = tg3_io_slot_reset,
15533 .resume = tg3_io_resume
15534};
15535
Linus Torvalds1da177e2005-04-16 15:20:36 -070015536static struct pci_driver tg3_driver = {
15537 .name = DRV_MODULE_NAME,
15538 .id_table = tg3_pci_tbl,
15539 .probe = tg3_init_one,
15540 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015541 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015542 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015543};
15544
15545static int __init tg3_init(void)
15546{
Jeff Garzik29917622006-08-19 17:48:59 -040015547 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015548}
15549
15550static void __exit tg3_cleanup(void)
15551{
15552 pci_unregister_driver(&tg3_driver);
15553}
15554
15555module_init(tg3_init);
15556module_exit(tg3_cleanup);