blob: 122bde5bb5a491677756b1976999bb062a1294a2 [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 Carlson43a5f002011-05-19 12:12:56 +000091#define TG3_MIN_NUM 119
Matt Carlson6867c842010-07-11 09:31:44 +000092#define DRV_MODULE_VERSION \
93 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlson43a5f002011-05-19 12:12:56 +000094#define DRV_MODULE_RELDATE "May 18, 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)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000295 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700296 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297};
298
299MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
300
Andreas Mohr50da8592006-08-14 23:54:30 -0700301static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000303} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 { "rx_octets" },
305 { "rx_fragments" },
306 { "rx_ucast_packets" },
307 { "rx_mcast_packets" },
308 { "rx_bcast_packets" },
309 { "rx_fcs_errors" },
310 { "rx_align_errors" },
311 { "rx_xon_pause_rcvd" },
312 { "rx_xoff_pause_rcvd" },
313 { "rx_mac_ctrl_rcvd" },
314 { "rx_xoff_entered" },
315 { "rx_frame_too_long_errors" },
316 { "rx_jabbers" },
317 { "rx_undersize_packets" },
318 { "rx_in_length_errors" },
319 { "rx_out_length_errors" },
320 { "rx_64_or_less_octet_packets" },
321 { "rx_65_to_127_octet_packets" },
322 { "rx_128_to_255_octet_packets" },
323 { "rx_256_to_511_octet_packets" },
324 { "rx_512_to_1023_octet_packets" },
325 { "rx_1024_to_1522_octet_packets" },
326 { "rx_1523_to_2047_octet_packets" },
327 { "rx_2048_to_4095_octet_packets" },
328 { "rx_4096_to_8191_octet_packets" },
329 { "rx_8192_to_9022_octet_packets" },
330
331 { "tx_octets" },
332 { "tx_collisions" },
333
334 { "tx_xon_sent" },
335 { "tx_xoff_sent" },
336 { "tx_flow_control" },
337 { "tx_mac_errors" },
338 { "tx_single_collisions" },
339 { "tx_mult_collisions" },
340 { "tx_deferred" },
341 { "tx_excessive_collisions" },
342 { "tx_late_collisions" },
343 { "tx_collide_2times" },
344 { "tx_collide_3times" },
345 { "tx_collide_4times" },
346 { "tx_collide_5times" },
347 { "tx_collide_6times" },
348 { "tx_collide_7times" },
349 { "tx_collide_8times" },
350 { "tx_collide_9times" },
351 { "tx_collide_10times" },
352 { "tx_collide_11times" },
353 { "tx_collide_12times" },
354 { "tx_collide_13times" },
355 { "tx_collide_14times" },
356 { "tx_collide_15times" },
357 { "tx_ucast_packets" },
358 { "tx_mcast_packets" },
359 { "tx_bcast_packets" },
360 { "tx_carrier_sense_errors" },
361 { "tx_discards" },
362 { "tx_errors" },
363
364 { "dma_writeq_full" },
365 { "dma_write_prioq_full" },
366 { "rxbds_empty" },
367 { "rx_discards" },
368 { "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" },
Matt Carlson4452d092011-05-19 12:12:51 +0000379 { "nic_tx_threshold_hit" },
380
381 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382};
383
Matt Carlson48fa55a2011-04-13 11:05:06 +0000384#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
385
386
Andreas Mohr50da8592006-08-14 23:54:30 -0700387static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700388 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000389} ethtool_test_keys[] = {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700390 { "nvram test (online) " },
391 { "link test (online) " },
392 { "register test (offline)" },
393 { "memory test (offline)" },
394 { "loopback test (offline)" },
395 { "interrupt test (offline)" },
396};
397
Matt Carlson48fa55a2011-04-13 11:05:06 +0000398#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
399
400
Michael Chanb401e9e2005-12-19 16:27:04 -0800401static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
402{
403 writel(val, tp->regs + off);
404}
405
406static u32 tg3_read32(struct tg3 *tp, u32 off)
407{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000408 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800409}
410
Matt Carlson0d3031d2007-10-10 18:02:43 -0700411static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
412{
413 writel(val, tp->aperegs + off);
414}
415
416static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
417{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000418 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
422{
Michael Chan68929142005-08-09 20:17:14 -0700423 unsigned long flags;
424
425 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700426 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
427 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700428 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700429}
430
431static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
432{
433 writel(val, tp->regs + off);
434 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Michael Chan68929142005-08-09 20:17:14 -0700437static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
438{
439 unsigned long flags;
440 u32 val;
441
442 spin_lock_irqsave(&tp->indirect_lock, flags);
443 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
444 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
445 spin_unlock_irqrestore(&tp->indirect_lock, flags);
446 return val;
447}
448
449static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
450{
451 unsigned long flags;
452
453 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
454 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
455 TG3_64BIT_REG_LOW, val);
456 return;
457 }
Matt Carlson66711e62009-11-13 13:03:49 +0000458 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700459 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
460 TG3_64BIT_REG_LOW, val);
461 return;
462 }
463
464 spin_lock_irqsave(&tp->indirect_lock, flags);
465 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
466 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
467 spin_unlock_irqrestore(&tp->indirect_lock, flags);
468
469 /* In indirect mode when disabling interrupts, we also need
470 * to clear the interrupt bit in the GRC local ctrl register.
471 */
472 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
473 (val == 0x1)) {
474 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
475 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
476 }
477}
478
479static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
480{
481 unsigned long flags;
482 u32 val;
483
484 spin_lock_irqsave(&tp->indirect_lock, flags);
485 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
486 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
487 spin_unlock_irqrestore(&tp->indirect_lock, flags);
488 return val;
489}
490
Michael Chanb401e9e2005-12-19 16:27:04 -0800491/* usec_wait specifies the wait time in usec when writing to certain registers
492 * where it is unsafe to read back the register without some delay.
493 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
494 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
495 */
496static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Joe Perches63c3a662011-04-26 08:12:10 +0000498 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800499 /* Non-posted methods */
500 tp->write32(tp, off, val);
501 else {
502 /* Posted method */
503 tg3_write32(tp, off, val);
504 if (usec_wait)
505 udelay(usec_wait);
506 tp->read32(tp, off);
507 }
508 /* Wait again after the read for the posted method to guarantee that
509 * the wait time is met.
510 */
511 if (usec_wait)
512 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Michael Chan09ee9292005-08-09 20:17:00 -0700515static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
516{
517 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000518 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700519 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700520}
521
Michael Chan20094932005-08-09 20:16:32 -0700522static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 void __iomem *mbox = tp->regs + off;
525 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000526 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000528 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 readl(mbox);
530}
531
Michael Chanb5d37722006-09-27 16:06:21 -0700532static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
533{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000534 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700535}
536
537static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
538{
539 writel(val, tp->regs + off + GRCMBOX_BASE);
540}
541
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000542#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700543#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000544#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
545#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
546#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700547
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000548#define tw32(reg, val) tp->write32(tp, reg, val)
549#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
550#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
551#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
554{
Michael Chan68929142005-08-09 20:17:14 -0700555 unsigned long flags;
556
Matt Carlson6ff6f812011-05-19 12:12:54 +0000557 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700558 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
559 return;
560
Michael Chan68929142005-08-09 20:17:14 -0700561 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000562 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700563 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
564 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Michael Chanbbadf502006-04-06 21:46:34 -0700566 /* Always leave this as zero. */
567 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
568 } else {
569 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
570 tw32_f(TG3PCI_MEM_WIN_DATA, val);
571
572 /* Always leave this as zero. */
573 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
574 }
Michael Chan68929142005-08-09 20:17:14 -0700575 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
578static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
579{
Michael Chan68929142005-08-09 20:17:14 -0700580 unsigned long flags;
581
Matt Carlson6ff6f812011-05-19 12:12:54 +0000582 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700583 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
584 *val = 0;
585 return;
586 }
587
Michael Chan68929142005-08-09 20:17:14 -0700588 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000589 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700590 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
591 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Michael Chanbbadf502006-04-06 21:46:34 -0700593 /* Always leave this as zero. */
594 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
595 } else {
596 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
597 *val = tr32(TG3PCI_MEM_WIN_DATA);
598
599 /* Always leave this as zero. */
600 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
601 }
Michael Chan68929142005-08-09 20:17:14 -0700602 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
Matt Carlson0d3031d2007-10-10 18:02:43 -0700605static void tg3_ape_lock_init(struct tg3 *tp)
606{
607 int i;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000608 u32 regbase;
609
610 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
611 regbase = TG3_APE_LOCK_GRANT;
612 else
613 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700614
615 /* Make sure the driver hasn't any stale locks. */
616 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000617 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700618}
619
620static int tg3_ape_lock(struct tg3 *tp, int locknum)
621{
622 int i, off;
623 int ret = 0;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000624 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700625
Joe Perches63c3a662011-04-26 08:12:10 +0000626 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700627 return 0;
628
629 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000630 case TG3_APE_LOCK_GRC:
631 case TG3_APE_LOCK_MEM:
632 break;
633 default:
634 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700635 }
636
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000637 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
638 req = TG3_APE_LOCK_REQ;
639 gnt = TG3_APE_LOCK_GRANT;
640 } else {
641 req = TG3_APE_PER_LOCK_REQ;
642 gnt = TG3_APE_PER_LOCK_GRANT;
643 }
644
Matt Carlson0d3031d2007-10-10 18:02:43 -0700645 off = 4 * locknum;
646
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000647 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700648
649 /* Wait for up to 1 millisecond to acquire lock. */
650 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000651 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700652 if (status == APE_LOCK_GRANT_DRIVER)
653 break;
654 udelay(10);
655 }
656
657 if (status != APE_LOCK_GRANT_DRIVER) {
658 /* Revoke the lock request. */
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000659 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700660 APE_LOCK_GRANT_DRIVER);
661
662 ret = -EBUSY;
663 }
664
665 return ret;
666}
667
668static void tg3_ape_unlock(struct tg3 *tp, int locknum)
669{
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000670 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700671
Joe Perches63c3a662011-04-26 08:12:10 +0000672 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700673 return;
674
675 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000676 case TG3_APE_LOCK_GRC:
677 case TG3_APE_LOCK_MEM:
678 break;
679 default:
680 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700681 }
682
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000683 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
684 gnt = TG3_APE_LOCK_GRANT;
685 else
686 gnt = TG3_APE_PER_LOCK_GRANT;
687
688 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700689}
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691static void tg3_disable_ints(struct tg3 *tp)
692{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000693 int i;
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 tw32(TG3PCI_MISC_HOST_CTRL,
696 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000697 for (i = 0; i < tp->irq_max; i++)
698 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701static void tg3_enable_ints(struct tg3 *tp)
702{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000703 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000704
Michael Chanbbe832c2005-06-24 20:20:04 -0700705 tp->irq_sync = 0;
706 wmb();
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 tw32(TG3PCI_MISC_HOST_CTRL,
709 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000710
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000711 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000712 for (i = 0; i < tp->irq_cnt; i++) {
713 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000714
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000715 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000716 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000717 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
718
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000719 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000720 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000721
722 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000723 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000724 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
725 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
726 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000727 tw32(HOSTCC_MODE, tp->coal_now);
728
729 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Matt Carlson17375d22009-08-28 14:02:18 +0000732static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700733{
Matt Carlson17375d22009-08-28 14:02:18 +0000734 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000735 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700736 unsigned int work_exists = 0;
737
738 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000739 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700740 if (sblk->status & SD_STATUS_LINK_CHG)
741 work_exists = 1;
742 }
743 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000744 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000745 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700746 work_exists = 1;
747
748 return work_exists;
749}
750
Matt Carlson17375d22009-08-28 14:02:18 +0000751/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700752 * similar to tg3_enable_ints, but it accurately determines whether there
753 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400754 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 */
Matt Carlson17375d22009-08-28 14:02:18 +0000756static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Matt Carlson17375d22009-08-28 14:02:18 +0000758 struct tg3 *tp = tnapi->tp;
759
Matt Carlson898a56f2009-08-28 14:02:40 +0000760 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 mmiowb();
762
David S. Millerfac9b832005-05-18 22:46:34 -0700763 /* When doing tagged status, this work check is unnecessary.
764 * The last_tag we write above tells the chip which piece of
765 * work we've completed.
766 */
Joe Perches63c3a662011-04-26 08:12:10 +0000767 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700768 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000769 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772static void tg3_switch_clocks(struct tg3 *tp)
773{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000774 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 u32 orig_clock_ctrl;
776
Joe Perches63c3a662011-04-26 08:12:10 +0000777 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700778 return;
779
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000780 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 orig_clock_ctrl = clock_ctrl;
783 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
784 CLOCK_CTRL_CLKRUN_OENABLE |
785 0x1f);
786 tp->pci_clock_ctrl = clock_ctrl;
787
Joe Perches63c3a662011-04-26 08:12:10 +0000788 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800790 tw32_wait_f(TG3PCI_CLOCK_CTRL,
791 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800794 tw32_wait_f(TG3PCI_CLOCK_CTRL,
795 clock_ctrl |
796 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
797 40);
798 tw32_wait_f(TG3PCI_CLOCK_CTRL,
799 clock_ctrl | (CLOCK_CTRL_ALTCLK),
800 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800802 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
805#define PHY_BUSY_LOOPS 5000
806
807static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
808{
809 u32 frame_val;
810 unsigned int loops;
811 int ret;
812
813 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
814 tw32_f(MAC_MI_MODE,
815 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
816 udelay(80);
817 }
818
819 *val = 0x0;
820
Matt Carlson882e9792009-09-01 13:21:36 +0000821 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 MI_COM_PHY_ADDR_MASK);
823 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
824 MI_COM_REG_ADDR_MASK);
825 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 tw32_f(MAC_MI_COM, frame_val);
828
829 loops = PHY_BUSY_LOOPS;
830 while (loops != 0) {
831 udelay(10);
832 frame_val = tr32(MAC_MI_COM);
833
834 if ((frame_val & MI_COM_BUSY) == 0) {
835 udelay(5);
836 frame_val = tr32(MAC_MI_COM);
837 break;
838 }
839 loops -= 1;
840 }
841
842 ret = -EBUSY;
843 if (loops != 0) {
844 *val = frame_val & MI_COM_DATA_MASK;
845 ret = 0;
846 }
847
848 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
849 tw32_f(MAC_MI_MODE, tp->mi_mode);
850 udelay(80);
851 }
852
853 return ret;
854}
855
856static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
857{
858 u32 frame_val;
859 unsigned int loops;
860 int ret;
861
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000862 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700863 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
864 return 0;
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
867 tw32_f(MAC_MI_MODE,
868 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
869 udelay(80);
870 }
871
Matt Carlson882e9792009-09-01 13:21:36 +0000872 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 MI_COM_PHY_ADDR_MASK);
874 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
875 MI_COM_REG_ADDR_MASK);
876 frame_val |= (val & MI_COM_DATA_MASK);
877 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 tw32_f(MAC_MI_COM, frame_val);
880
881 loops = PHY_BUSY_LOOPS;
882 while (loops != 0) {
883 udelay(10);
884 frame_val = tr32(MAC_MI_COM);
885 if ((frame_val & MI_COM_BUSY) == 0) {
886 udelay(5);
887 frame_val = tr32(MAC_MI_COM);
888 break;
889 }
890 loops -= 1;
891 }
892
893 ret = -EBUSY;
894 if (loops != 0)
895 ret = 0;
896
897 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
898 tw32_f(MAC_MI_MODE, tp->mi_mode);
899 udelay(80);
900 }
901
902 return ret;
903}
904
Matt Carlsonb0988c12011-04-20 07:57:39 +0000905static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
906{
907 int err;
908
909 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
910 if (err)
911 goto done;
912
913 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
914 if (err)
915 goto done;
916
917 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
918 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
919 if (err)
920 goto done;
921
922 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
923
924done:
925 return err;
926}
927
928static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
929{
930 int err;
931
932 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
933 if (err)
934 goto done;
935
936 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
937 if (err)
938 goto done;
939
940 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
941 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
942 if (err)
943 goto done;
944
945 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
946
947done:
948 return err;
949}
950
951static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
952{
953 int err;
954
955 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
956 if (!err)
957 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
958
959 return err;
960}
961
962static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
963{
964 int err;
965
966 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
967 if (!err)
968 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
969
970 return err;
971}
972
Matt Carlson15ee95c2011-04-20 07:57:40 +0000973static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
974{
975 int err;
976
977 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
978 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
979 MII_TG3_AUXCTL_SHDWSEL_MISC);
980 if (!err)
981 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
982
983 return err;
984}
985
Matt Carlsonb4bd2922011-04-20 07:57:41 +0000986static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
987{
988 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
989 set |= MII_TG3_AUXCTL_MISC_WREN;
990
991 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
992}
993
Matt Carlson1d36ba42011-04-20 07:57:42 +0000994#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
995 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
996 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
997 MII_TG3_AUXCTL_ACTL_TX_6DB)
998
999#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1000 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1001 MII_TG3_AUXCTL_ACTL_TX_6DB);
1002
Matt Carlson95e28692008-05-25 23:44:14 -07001003static int tg3_bmcr_reset(struct tg3 *tp)
1004{
1005 u32 phy_control;
1006 int limit, err;
1007
1008 /* OK, reset it, and poll the BMCR_RESET bit until it
1009 * clears or we time out.
1010 */
1011 phy_control = BMCR_RESET;
1012 err = tg3_writephy(tp, MII_BMCR, phy_control);
1013 if (err != 0)
1014 return -EBUSY;
1015
1016 limit = 5000;
1017 while (limit--) {
1018 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1019 if (err != 0)
1020 return -EBUSY;
1021
1022 if ((phy_control & BMCR_RESET) == 0) {
1023 udelay(40);
1024 break;
1025 }
1026 udelay(10);
1027 }
Roel Kluind4675b52009-02-12 16:33:27 -08001028 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001029 return -EBUSY;
1030
1031 return 0;
1032}
1033
Matt Carlson158d7ab2008-05-29 01:37:54 -07001034static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1035{
Francois Romieu3d165432009-01-19 16:56:50 -08001036 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001037 u32 val;
1038
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001039 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001040
1041 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001042 val = -EIO;
1043
1044 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001045
1046 return val;
1047}
1048
1049static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1050{
Francois Romieu3d165432009-01-19 16:56:50 -08001051 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001052 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001053
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001054 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001055
1056 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001057 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001058
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001059 spin_unlock_bh(&tp->lock);
1060
1061 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001062}
1063
1064static int tg3_mdio_reset(struct mii_bus *bp)
1065{
1066 return 0;
1067}
1068
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001069static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001070{
1071 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001072 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001073
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001074 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001075 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001076 case PHY_ID_BCM50610:
1077 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001078 val = MAC_PHYCFG2_50610_LED_MODES;
1079 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001080 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001081 val = MAC_PHYCFG2_AC131_LED_MODES;
1082 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001083 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001084 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1085 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001086 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001087 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1088 break;
1089 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001090 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001091 }
1092
1093 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1094 tw32(MAC_PHYCFG2, val);
1095
1096 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001097 val &= ~(MAC_PHYCFG1_RGMII_INT |
1098 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1099 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001100 tw32(MAC_PHYCFG1, val);
1101
1102 return;
1103 }
1104
Joe Perches63c3a662011-04-26 08:12:10 +00001105 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001106 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1107 MAC_PHYCFG2_FMODE_MASK_MASK |
1108 MAC_PHYCFG2_GMODE_MASK_MASK |
1109 MAC_PHYCFG2_ACT_MASK_MASK |
1110 MAC_PHYCFG2_QUAL_MASK_MASK |
1111 MAC_PHYCFG2_INBAND_ENABLE;
1112
1113 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001114
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001115 val = tr32(MAC_PHYCFG1);
1116 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1117 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001118 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1119 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001120 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001121 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001122 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1123 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001124 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1125 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1126 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001127
Matt Carlsona9daf362008-05-25 23:49:44 -07001128 val = tr32(MAC_EXT_RGMII_MODE);
1129 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1130 MAC_RGMII_MODE_RX_QUALITY |
1131 MAC_RGMII_MODE_RX_ACTIVITY |
1132 MAC_RGMII_MODE_RX_ENG_DET |
1133 MAC_RGMII_MODE_TX_ENABLE |
1134 MAC_RGMII_MODE_TX_LOWPWR |
1135 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001136 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1137 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001138 val |= MAC_RGMII_MODE_RX_INT_B |
1139 MAC_RGMII_MODE_RX_QUALITY |
1140 MAC_RGMII_MODE_RX_ACTIVITY |
1141 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001142 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001143 val |= MAC_RGMII_MODE_TX_ENABLE |
1144 MAC_RGMII_MODE_TX_LOWPWR |
1145 MAC_RGMII_MODE_TX_RESET;
1146 }
1147 tw32(MAC_EXT_RGMII_MODE, val);
1148}
1149
Matt Carlson158d7ab2008-05-29 01:37:54 -07001150static void tg3_mdio_start(struct tg3 *tp)
1151{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001152 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1153 tw32_f(MAC_MI_MODE, tp->mi_mode);
1154 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001155
Joe Perches63c3a662011-04-26 08:12:10 +00001156 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001157 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1158 tg3_mdio_config_5785(tp);
1159}
1160
1161static int tg3_mdio_init(struct tg3 *tp)
1162{
1163 int i;
1164 u32 reg;
1165 struct phy_device *phydev;
1166
Joe Perches63c3a662011-04-26 08:12:10 +00001167 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001168 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001169
Matt Carlson9c7df912010-06-05 17:24:36 +00001170 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001171
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001172 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1173 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1174 else
1175 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1176 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001177 if (is_serdes)
1178 tp->phy_addr += 7;
1179 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001180 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001181
Matt Carlson158d7ab2008-05-29 01:37:54 -07001182 tg3_mdio_start(tp);
1183
Joe Perches63c3a662011-04-26 08:12:10 +00001184 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001185 return 0;
1186
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001187 tp->mdio_bus = mdiobus_alloc();
1188 if (tp->mdio_bus == NULL)
1189 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001190
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001191 tp->mdio_bus->name = "tg3 mdio bus";
1192 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001193 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001194 tp->mdio_bus->priv = tp;
1195 tp->mdio_bus->parent = &tp->pdev->dev;
1196 tp->mdio_bus->read = &tg3_mdio_read;
1197 tp->mdio_bus->write = &tg3_mdio_write;
1198 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001199 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001200 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001201
1202 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001203 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001204
1205 /* The bus registration will look for all the PHYs on the mdio bus.
1206 * Unfortunately, it does not ensure the PHY is powered up before
1207 * accessing the PHY ID registers. A chip reset is the
1208 * quickest way to bring the device back to an operational state..
1209 */
1210 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1211 tg3_bmcr_reset(tp);
1212
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001213 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001214 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001215 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001216 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001217 return i;
1218 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001219
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001220 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001221
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001222 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001223 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001224 mdiobus_unregister(tp->mdio_bus);
1225 mdiobus_free(tp->mdio_bus);
1226 return -ENODEV;
1227 }
1228
1229 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001230 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001231 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001232 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001233 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001234 case PHY_ID_BCM50610:
1235 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001236 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001237 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001238 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001239 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001240 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001241 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001242 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001243 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001244 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001245 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001246 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001247 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001248 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001249 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001250 case PHY_ID_RTL8201E:
1251 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001252 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e092009-11-02 14:31:11 +00001253 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001254 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001255 break;
1256 }
1257
Joe Perches63c3a662011-04-26 08:12:10 +00001258 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001259
1260 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1261 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001262
1263 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001264}
1265
1266static void tg3_mdio_fini(struct tg3 *tp)
1267{
Joe Perches63c3a662011-04-26 08:12:10 +00001268 if (tg3_flag(tp, MDIOBUS_INITED)) {
1269 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001270 mdiobus_unregister(tp->mdio_bus);
1271 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001272 }
1273}
1274
Matt Carlson95e28692008-05-25 23:44:14 -07001275/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001276static inline void tg3_generate_fw_event(struct tg3 *tp)
1277{
1278 u32 val;
1279
1280 val = tr32(GRC_RX_CPU_EVENT);
1281 val |= GRC_RX_CPU_DRIVER_EVENT;
1282 tw32_f(GRC_RX_CPU_EVENT, val);
1283
1284 tp->last_event_jiffies = jiffies;
1285}
1286
1287#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1288
1289/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001290static void tg3_wait_for_event_ack(struct tg3 *tp)
1291{
1292 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001293 unsigned int delay_cnt;
1294 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001295
Matt Carlson4ba526c2008-08-15 14:10:04 -07001296 /* If enough time has passed, no wait is necessary. */
1297 time_remain = (long)(tp->last_event_jiffies + 1 +
1298 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1299 (long)jiffies;
1300 if (time_remain < 0)
1301 return;
1302
1303 /* Check if we can shorten the wait time. */
1304 delay_cnt = jiffies_to_usecs(time_remain);
1305 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1306 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1307 delay_cnt = (delay_cnt >> 3) + 1;
1308
1309 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001310 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1311 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001312 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001313 }
1314}
1315
1316/* tp->lock is held. */
1317static void tg3_ump_link_report(struct tg3 *tp)
1318{
1319 u32 reg;
1320 u32 val;
1321
Joe Perches63c3a662011-04-26 08:12:10 +00001322 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson95e28692008-05-25 23:44:14 -07001323 return;
1324
1325 tg3_wait_for_event_ack(tp);
1326
1327 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1328
1329 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1330
1331 val = 0;
1332 if (!tg3_readphy(tp, MII_BMCR, &reg))
1333 val = reg << 16;
1334 if (!tg3_readphy(tp, MII_BMSR, &reg))
1335 val |= (reg & 0xffff);
1336 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1337
1338 val = 0;
1339 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1340 val = reg << 16;
1341 if (!tg3_readphy(tp, MII_LPA, &reg))
1342 val |= (reg & 0xffff);
1343 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1344
1345 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001346 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001347 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1348 val = reg << 16;
1349 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1350 val |= (reg & 0xffff);
1351 }
1352 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1353
1354 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1355 val = reg << 16;
1356 else
1357 val = 0;
1358 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1359
Matt Carlson4ba526c2008-08-15 14:10:04 -07001360 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001361}
1362
1363static void tg3_link_report(struct tg3 *tp)
1364{
1365 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001366 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001367 tg3_ump_link_report(tp);
1368 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001369 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1370 (tp->link_config.active_speed == SPEED_1000 ?
1371 1000 :
1372 (tp->link_config.active_speed == SPEED_100 ?
1373 100 : 10)),
1374 (tp->link_config.active_duplex == DUPLEX_FULL ?
1375 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001376
Joe Perches05dbe002010-02-17 19:44:19 +00001377 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1378 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1379 "on" : "off",
1380 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1381 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001382
1383 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1384 netdev_info(tp->dev, "EEE is %s\n",
1385 tp->setlpicnt ? "enabled" : "disabled");
1386
Matt Carlson95e28692008-05-25 23:44:14 -07001387 tg3_ump_link_report(tp);
1388 }
1389}
1390
1391static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1392{
1393 u16 miireg;
1394
Steve Glendinninge18ce342008-12-16 02:00:00 -08001395 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001396 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001397 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001398 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001399 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001400 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1401 else
1402 miireg = 0;
1403
1404 return miireg;
1405}
1406
1407static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1408{
1409 u16 miireg;
1410
Steve Glendinninge18ce342008-12-16 02:00:00 -08001411 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001412 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001413 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001414 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001415 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001416 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1417 else
1418 miireg = 0;
1419
1420 return miireg;
1421}
1422
Matt Carlson95e28692008-05-25 23:44:14 -07001423static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1424{
1425 u8 cap = 0;
1426
1427 if (lcladv & ADVERTISE_1000XPAUSE) {
1428 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1429 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001430 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001431 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001432 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001433 } else {
1434 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001435 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001436 }
1437 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1438 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001439 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001440 }
1441
1442 return cap;
1443}
1444
Matt Carlsonf51f3562008-05-25 23:45:08 -07001445static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001446{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001447 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001448 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001449 u32 old_rx_mode = tp->rx_mode;
1450 u32 old_tx_mode = tp->tx_mode;
1451
Joe Perches63c3a662011-04-26 08:12:10 +00001452 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001453 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001454 else
1455 autoneg = tp->link_config.autoneg;
1456
Joe Perches63c3a662011-04-26 08:12:10 +00001457 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001458 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001459 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001460 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001461 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001462 } else
1463 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001464
Matt Carlsonf51f3562008-05-25 23:45:08 -07001465 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001466
Steve Glendinninge18ce342008-12-16 02:00:00 -08001467 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001468 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1469 else
1470 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1471
Matt Carlsonf51f3562008-05-25 23:45:08 -07001472 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001473 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001474
Steve Glendinninge18ce342008-12-16 02:00:00 -08001475 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001476 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1477 else
1478 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1479
Matt Carlsonf51f3562008-05-25 23:45:08 -07001480 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001481 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001482}
1483
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001484static void tg3_adjust_link(struct net_device *dev)
1485{
1486 u8 oldflowctrl, linkmesg = 0;
1487 u32 mac_mode, lcl_adv, rmt_adv;
1488 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001489 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001490
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001491 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001492
1493 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1494 MAC_MODE_HALF_DUPLEX);
1495
1496 oldflowctrl = tp->link_config.active_flowctrl;
1497
1498 if (phydev->link) {
1499 lcl_adv = 0;
1500 rmt_adv = 0;
1501
1502 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1503 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001504 else if (phydev->speed == SPEED_1000 ||
1505 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001506 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001507 else
1508 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001509
1510 if (phydev->duplex == DUPLEX_HALF)
1511 mac_mode |= MAC_MODE_HALF_DUPLEX;
1512 else {
1513 lcl_adv = tg3_advert_flowctrl_1000T(
1514 tp->link_config.flowctrl);
1515
1516 if (phydev->pause)
1517 rmt_adv = LPA_PAUSE_CAP;
1518 if (phydev->asym_pause)
1519 rmt_adv |= LPA_PAUSE_ASYM;
1520 }
1521
1522 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1523 } else
1524 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1525
1526 if (mac_mode != tp->mac_mode) {
1527 tp->mac_mode = mac_mode;
1528 tw32_f(MAC_MODE, tp->mac_mode);
1529 udelay(40);
1530 }
1531
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001532 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1533 if (phydev->speed == SPEED_10)
1534 tw32(MAC_MI_STAT,
1535 MAC_MI_STAT_10MBPS_MODE |
1536 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1537 else
1538 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1539 }
1540
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001541 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1542 tw32(MAC_TX_LENGTHS,
1543 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1544 (6 << TX_LENGTHS_IPG_SHIFT) |
1545 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1546 else
1547 tw32(MAC_TX_LENGTHS,
1548 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1549 (6 << TX_LENGTHS_IPG_SHIFT) |
1550 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1551
1552 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1553 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1554 phydev->speed != tp->link_config.active_speed ||
1555 phydev->duplex != tp->link_config.active_duplex ||
1556 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001557 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001558
1559 tp->link_config.active_speed = phydev->speed;
1560 tp->link_config.active_duplex = phydev->duplex;
1561
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001562 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001563
1564 if (linkmesg)
1565 tg3_link_report(tp);
1566}
1567
1568static int tg3_phy_init(struct tg3 *tp)
1569{
1570 struct phy_device *phydev;
1571
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001572 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001573 return 0;
1574
1575 /* Bring the PHY back to a known state. */
1576 tg3_bmcr_reset(tp);
1577
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001578 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001579
1580 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001581 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001582 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001583 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001584 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001585 return PTR_ERR(phydev);
1586 }
1587
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001588 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001589 switch (phydev->interface) {
1590 case PHY_INTERFACE_MODE_GMII:
1591 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001592 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001593 phydev->supported &= (PHY_GBIT_FEATURES |
1594 SUPPORTED_Pause |
1595 SUPPORTED_Asym_Pause);
1596 break;
1597 }
1598 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001599 case PHY_INTERFACE_MODE_MII:
1600 phydev->supported &= (PHY_BASIC_FEATURES |
1601 SUPPORTED_Pause |
1602 SUPPORTED_Asym_Pause);
1603 break;
1604 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001605 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001606 return -EINVAL;
1607 }
1608
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001609 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001610
1611 phydev->advertising = phydev->supported;
1612
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001613 return 0;
1614}
1615
1616static void tg3_phy_start(struct tg3 *tp)
1617{
1618 struct phy_device *phydev;
1619
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001620 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001621 return;
1622
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001623 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001624
Matt Carlson80096062010-08-02 11:26:06 +00001625 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1626 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001627 phydev->speed = tp->link_config.orig_speed;
1628 phydev->duplex = tp->link_config.orig_duplex;
1629 phydev->autoneg = tp->link_config.orig_autoneg;
1630 phydev->advertising = tp->link_config.orig_advertising;
1631 }
1632
1633 phy_start(phydev);
1634
1635 phy_start_aneg(phydev);
1636}
1637
1638static void tg3_phy_stop(struct tg3 *tp)
1639{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001640 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001641 return;
1642
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001643 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001644}
1645
1646static void tg3_phy_fini(struct tg3 *tp)
1647{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001648 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001649 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001650 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001651 }
1652}
1653
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001654static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1655{
1656 u32 phytest;
1657
1658 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1659 u32 phy;
1660
1661 tg3_writephy(tp, MII_TG3_FET_TEST,
1662 phytest | MII_TG3_FET_SHADOW_EN);
1663 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1664 if (enable)
1665 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1666 else
1667 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1668 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1669 }
1670 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1671 }
1672}
1673
Matt Carlson6833c042008-11-21 17:18:59 -08001674static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1675{
1676 u32 reg;
1677
Joe Perches63c3a662011-04-26 08:12:10 +00001678 if (!tg3_flag(tp, 5705_PLUS) ||
1679 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001680 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001681 return;
1682
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001683 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001684 tg3_phy_fet_toggle_apd(tp, enable);
1685 return;
1686 }
1687
Matt Carlson6833c042008-11-21 17:18:59 -08001688 reg = MII_TG3_MISC_SHDW_WREN |
1689 MII_TG3_MISC_SHDW_SCR5_SEL |
1690 MII_TG3_MISC_SHDW_SCR5_LPED |
1691 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1692 MII_TG3_MISC_SHDW_SCR5_SDTL |
1693 MII_TG3_MISC_SHDW_SCR5_C125OE;
1694 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1695 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1696
1697 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1698
1699
1700 reg = MII_TG3_MISC_SHDW_WREN |
1701 MII_TG3_MISC_SHDW_APD_SEL |
1702 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1703 if (enable)
1704 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1705
1706 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1707}
1708
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001709static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1710{
1711 u32 phy;
1712
Joe Perches63c3a662011-04-26 08:12:10 +00001713 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001714 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001715 return;
1716
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001717 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001718 u32 ephy;
1719
Matt Carlson535ef6e2009-08-25 10:09:36 +00001720 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1721 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1722
1723 tg3_writephy(tp, MII_TG3_FET_TEST,
1724 ephy | MII_TG3_FET_SHADOW_EN);
1725 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001726 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001727 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001728 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001729 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1730 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001731 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001732 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001733 }
1734 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00001735 int ret;
1736
1737 ret = tg3_phy_auxctl_read(tp,
1738 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
1739 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001740 if (enable)
1741 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1742 else
1743 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001744 tg3_phy_auxctl_write(tp,
1745 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001746 }
1747 }
1748}
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750static void tg3_phy_set_wirespeed(struct tg3 *tp)
1751{
Matt Carlson15ee95c2011-04-20 07:57:40 +00001752 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 u32 val;
1754
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001755 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return;
1757
Matt Carlson15ee95c2011-04-20 07:57:40 +00001758 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
1759 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001760 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
1761 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001764static void tg3_phy_apply_otp(struct tg3 *tp)
1765{
1766 u32 otp, phy;
1767
1768 if (!tp->phy_otp)
1769 return;
1770
1771 otp = tp->phy_otp;
1772
Matt Carlson1d36ba42011-04-20 07:57:42 +00001773 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
1774 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001775
1776 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1777 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1778 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1779
1780 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1781 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1782 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1783
1784 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1785 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1786 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1787
1788 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1789 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1790
1791 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1792 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1793
1794 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1795 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1796 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1797
Matt Carlson1d36ba42011-04-20 07:57:42 +00001798 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001799}
1800
Matt Carlson52b02d02010-10-14 10:37:41 +00001801static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1802{
1803 u32 val;
1804
1805 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1806 return;
1807
1808 tp->setlpicnt = 0;
1809
1810 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1811 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001812 tp->link_config.active_duplex == DUPLEX_FULL &&
1813 (tp->link_config.active_speed == SPEED_100 ||
1814 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001815 u32 eeectl;
1816
1817 if (tp->link_config.active_speed == SPEED_1000)
1818 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1819 else
1820 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1821
1822 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1823
Matt Carlson3110f5f52010-12-06 08:28:50 +00001824 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1825 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001826
Matt Carlsonb0c59432011-05-19 12:12:48 +00001827 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
1828 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00001829 tp->setlpicnt = 2;
1830 }
1831
1832 if (!tp->setlpicnt) {
1833 val = tr32(TG3_CPMU_EEE_MODE);
1834 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1835 }
1836}
1837
Matt Carlsonb0c59432011-05-19 12:12:48 +00001838static void tg3_phy_eee_enable(struct tg3 *tp)
1839{
1840 u32 val;
1841
1842 if (tp->link_config.active_speed == SPEED_1000 &&
1843 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1844 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
1845 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
1846 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
1847 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0003);
1848 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
1849 }
1850
1851 val = tr32(TG3_CPMU_EEE_MODE);
1852 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
1853}
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855static int tg3_wait_macro_done(struct tg3 *tp)
1856{
1857 int limit = 100;
1858
1859 while (limit--) {
1860 u32 tmp32;
1861
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001862 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if ((tmp32 & 0x1000) == 0)
1864 break;
1865 }
1866 }
Roel Kluind4675b52009-02-12 16:33:27 -08001867 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return -EBUSY;
1869
1870 return 0;
1871}
1872
1873static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1874{
1875 static const u32 test_pat[4][6] = {
1876 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1877 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1878 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1879 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1880 };
1881 int chan;
1882
1883 for (chan = 0; chan < 4; chan++) {
1884 int i;
1885
1886 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1887 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001888 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 for (i = 0; i < 6; i++)
1891 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1892 test_pat[chan][i]);
1893
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001894 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (tg3_wait_macro_done(tp)) {
1896 *resetp = 1;
1897 return -EBUSY;
1898 }
1899
1900 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1901 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001902 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (tg3_wait_macro_done(tp)) {
1904 *resetp = 1;
1905 return -EBUSY;
1906 }
1907
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001908 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (tg3_wait_macro_done(tp)) {
1910 *resetp = 1;
1911 return -EBUSY;
1912 }
1913
1914 for (i = 0; i < 6; i += 2) {
1915 u32 low, high;
1916
1917 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1918 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1919 tg3_wait_macro_done(tp)) {
1920 *resetp = 1;
1921 return -EBUSY;
1922 }
1923 low &= 0x7fff;
1924 high &= 0x000f;
1925 if (low != test_pat[chan][i] ||
1926 high != test_pat[chan][i+1]) {
1927 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1928 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1929 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1930
1931 return -EBUSY;
1932 }
1933 }
1934 }
1935
1936 return 0;
1937}
1938
1939static int tg3_phy_reset_chanpat(struct tg3 *tp)
1940{
1941 int chan;
1942
1943 for (chan = 0; chan < 4; chan++) {
1944 int i;
1945
1946 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1947 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001948 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 for (i = 0; i < 6; i++)
1950 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001951 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (tg3_wait_macro_done(tp))
1953 return -EBUSY;
1954 }
1955
1956 return 0;
1957}
1958
1959static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1960{
1961 u32 reg32, phy9_orig;
1962 int retries, do_phy_reset, err;
1963
1964 retries = 10;
1965 do_phy_reset = 1;
1966 do {
1967 if (do_phy_reset) {
1968 err = tg3_bmcr_reset(tp);
1969 if (err)
1970 return err;
1971 do_phy_reset = 0;
1972 }
1973
1974 /* Disable transmitter and interrupt. */
1975 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1976 continue;
1977
1978 reg32 |= 0x3000;
1979 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1980
1981 /* Set full-duplex, 1000 mbps. */
1982 tg3_writephy(tp, MII_BMCR,
1983 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1984
1985 /* Set to master mode. */
1986 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1987 continue;
1988
1989 tg3_writephy(tp, MII_TG3_CTRL,
1990 (MII_TG3_CTRL_AS_MASTER |
1991 MII_TG3_CTRL_ENABLE_AS_MASTER));
1992
Matt Carlson1d36ba42011-04-20 07:57:42 +00001993 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
1994 if (err)
1995 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001998 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2001 if (!err)
2002 break;
2003 } while (--retries);
2004
2005 err = tg3_phy_reset_chanpat(tp);
2006 if (err)
2007 return err;
2008
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002009 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002012 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Matt Carlson1d36ba42011-04-20 07:57:42 +00002014 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
2017
2018 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2019 reg32 &= ~0x3000;
2020 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2021 } else if (!err)
2022 err = -EBUSY;
2023
2024 return err;
2025}
2026
2027/* This will reset the tigon3 PHY if there is no valid
2028 * link unless the FORCE argument is non-zero.
2029 */
2030static int tg3_phy_reset(struct tg3 *tp)
2031{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002032 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 int err;
2034
Michael Chan60189dd2006-12-17 17:08:07 -08002035 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002036 val = tr32(GRC_MISC_CFG);
2037 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2038 udelay(40);
2039 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002040 err = tg3_readphy(tp, MII_BMSR, &val);
2041 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (err != 0)
2043 return -EBUSY;
2044
Michael Chanc8e1e822006-04-29 18:55:17 -07002045 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2046 netif_carrier_off(tp->dev);
2047 tg3_link_report(tp);
2048 }
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2051 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2052 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2053 err = tg3_phy_reset_5703_4_5(tp);
2054 if (err)
2055 return err;
2056 goto out;
2057 }
2058
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002059 cpmuctrl = 0;
2060 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2061 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2062 cpmuctrl = tr32(TG3_CPMU_CTRL);
2063 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2064 tw32(TG3_CPMU_CTRL,
2065 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2066 }
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 err = tg3_bmcr_reset(tp);
2069 if (err)
2070 return err;
2071
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002072 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002073 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2074 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002075
2076 tw32(TG3_CPMU_CTRL, cpmuctrl);
2077 }
2078
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002079 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2080 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002081 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2082 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2083 CPMU_LSPD_1000MB_MACCLK_12_5) {
2084 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2085 udelay(40);
2086 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2087 }
2088 }
2089
Joe Perches63c3a662011-04-26 08:12:10 +00002090 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002091 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002092 return 0;
2093
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002094 tg3_phy_apply_otp(tp);
2095
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002096 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002097 tg3_phy_toggle_apd(tp, true);
2098 else
2099 tg3_phy_toggle_apd(tp, false);
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002102 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2103 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002104 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2105 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002106 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002108
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002109 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002110 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2111 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002113
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002114 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002115 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2116 tg3_phydsp_write(tp, 0x000a, 0x310b);
2117 tg3_phydsp_write(tp, 0x201f, 0x9506);
2118 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2119 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2120 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002121 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002122 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2123 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2124 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2125 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2126 tg3_writephy(tp, MII_TG3_TEST1,
2127 MII_TG3_TEST1_TRIM_EN | 0x4);
2128 } else
2129 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2130
2131 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2132 }
Michael Chanc424cb22006-04-29 18:56:34 -07002133 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 /* Set Extended packet length bit (bit 14) on all chips that */
2136 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002137 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002139 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002140 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002142 err = tg3_phy_auxctl_read(tp,
2143 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2144 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002145 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2146 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
2148
2149 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2150 * jumbo frames transmission.
2151 */
Joe Perches63c3a662011-04-26 08:12:10 +00002152 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002153 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002154 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002155 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 }
2157
Michael Chan715116a2006-09-27 16:09:25 -07002158 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002159 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002160 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002161 }
2162
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002163 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 tg3_phy_set_wirespeed(tp);
2165 return 0;
2166}
2167
2168static void tg3_frob_aux_power(struct tg3 *tp)
2169{
Matt Carlson683644b2011-03-09 16:58:23 +00002170 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Matt Carlson334355a2010-01-20 16:58:10 +00002172 /* The GPIOs do something completely different on 57765. */
Joe Perches63c3a662011-04-26 08:12:10 +00002173 if (!tg3_flag(tp, IS_NIC) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002174 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002175 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return;
2177
Matt Carlson683644b2011-03-09 16:58:23 +00002178 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2179 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +00002180 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2181 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) &&
Matt Carlson683644b2011-03-09 16:58:23 +00002182 tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002183 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002185 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002186
Michael Chanbc1c7562006-03-20 17:48:03 -08002187 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002188 if (dev_peer) {
2189 struct tg3 *tp_peer = netdev_priv(dev_peer);
2190
Joe Perches63c3a662011-04-26 08:12:10 +00002191 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002192 return;
2193
Joe Perches63c3a662011-04-26 08:12:10 +00002194 if (tg3_flag(tp_peer, WOL_ENABLE) ||
2195 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002196 need_vaux = true;
2197 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Joe Perches63c3a662011-04-26 08:12:10 +00002200 if (tg3_flag(tp, WOL_ENABLE) || tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002201 need_vaux = true;
2202
2203 if (need_vaux) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2205 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002206 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2207 (GRC_LCLCTRL_GPIO_OE0 |
2208 GRC_LCLCTRL_GPIO_OE1 |
2209 GRC_LCLCTRL_GPIO_OE2 |
2210 GRC_LCLCTRL_GPIO_OUTPUT0 |
2211 GRC_LCLCTRL_GPIO_OUTPUT1),
2212 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002213 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2214 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002215 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2216 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2217 GRC_LCLCTRL_GPIO_OE1 |
2218 GRC_LCLCTRL_GPIO_OE2 |
2219 GRC_LCLCTRL_GPIO_OUTPUT0 |
2220 GRC_LCLCTRL_GPIO_OUTPUT1 |
2221 tp->grc_local_ctrl;
2222 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2223
2224 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2225 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2226
2227 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2228 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 } else {
2230 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002231 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Michael Chandc56b7d2005-12-19 16:26:28 -08002233 /* Workaround to prevent overdrawing Amps. */
2234 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2235 ASIC_REV_5714) {
2236 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002237 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2238 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002239 }
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 /* On 5753 and variants, GPIO2 cannot be used. */
2242 no_gpio2 = tp->nic_sram_data_cfg &
2243 NIC_SRAM_DATA_CFG_NO_GPIO2;
2244
Michael Chandc56b7d2005-12-19 16:26:28 -08002245 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 GRC_LCLCTRL_GPIO_OE1 |
2247 GRC_LCLCTRL_GPIO_OE2 |
2248 GRC_LCLCTRL_GPIO_OUTPUT1 |
2249 GRC_LCLCTRL_GPIO_OUTPUT2;
2250 if (no_gpio2) {
2251 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2252 GRC_LCLCTRL_GPIO_OUTPUT2);
2253 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002254 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2255 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2258
Michael Chanb401e9e2005-12-19 16:27:04 -08002259 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2260 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262 if (!no_gpio2) {
2263 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002264 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2265 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
2267 }
2268 } else {
2269 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2270 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002271 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2272 (GRC_LCLCTRL_GPIO_OE1 |
2273 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Michael Chanb401e9e2005-12-19 16:27:04 -08002275 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2276 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Michael Chanb401e9e2005-12-19 16:27:04 -08002278 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2279 (GRC_LCLCTRL_GPIO_OE1 |
2280 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 }
2282 }
2283}
2284
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002285static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2286{
2287 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2288 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002289 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002290 if (speed != SPEED_10)
2291 return 1;
2292 } else if (speed == SPEED_10)
2293 return 1;
2294
2295 return 0;
2296}
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298static int tg3_setup_phy(struct tg3 *, int);
2299
2300#define RESET_KIND_SHUTDOWN 0
2301#define RESET_KIND_INIT 1
2302#define RESET_KIND_SUSPEND 2
2303
2304static void tg3_write_sig_post_reset(struct tg3 *, int);
2305static int tg3_halt_cpu(struct tg3 *, u32);
2306
Matt Carlson0a459aa2008-11-03 16:54:15 -08002307static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002308{
Matt Carlsonce057f02007-11-12 21:08:03 -08002309 u32 val;
2310
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002311 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002312 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2313 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2314 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2315
2316 sg_dig_ctrl |=
2317 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2318 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2319 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2320 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002321 return;
Michael Chan51297242007-02-13 12:17:57 -08002322 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002323
Michael Chan60189dd2006-12-17 17:08:07 -08002324 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002325 tg3_bmcr_reset(tp);
2326 val = tr32(GRC_MISC_CFG);
2327 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2328 udelay(40);
2329 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002330 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002331 u32 phytest;
2332 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2333 u32 phy;
2334
2335 tg3_writephy(tp, MII_ADVERTISE, 0);
2336 tg3_writephy(tp, MII_BMCR,
2337 BMCR_ANENABLE | BMCR_ANRESTART);
2338
2339 tg3_writephy(tp, MII_TG3_FET_TEST,
2340 phytest | MII_TG3_FET_SHADOW_EN);
2341 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2342 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2343 tg3_writephy(tp,
2344 MII_TG3_FET_SHDW_AUXMODE4,
2345 phy);
2346 }
2347 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2348 }
2349 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002350 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002351 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2352 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002353
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002354 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2355 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2356 MII_TG3_AUXCTL_PCTL_VREG_11V;
2357 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002358 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002359
Michael Chan15c3b692006-03-22 01:06:52 -08002360 /* The PHY should not be powered down on some chips because
2361 * of bugs.
2362 */
2363 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2364 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2365 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002366 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002367 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002368
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002369 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2370 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002371 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2372 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2373 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2374 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2375 }
2376
Michael Chan15c3b692006-03-22 01:06:52 -08002377 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2378}
2379
Matt Carlson3f007892008-11-03 16:51:36 -08002380/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002381static int tg3_nvram_lock(struct tg3 *tp)
2382{
Joe Perches63c3a662011-04-26 08:12:10 +00002383 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002384 int i;
2385
2386 if (tp->nvram_lock_cnt == 0) {
2387 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2388 for (i = 0; i < 8000; i++) {
2389 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2390 break;
2391 udelay(20);
2392 }
2393 if (i == 8000) {
2394 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2395 return -ENODEV;
2396 }
2397 }
2398 tp->nvram_lock_cnt++;
2399 }
2400 return 0;
2401}
2402
2403/* tp->lock is held. */
2404static void tg3_nvram_unlock(struct tg3 *tp)
2405{
Joe Perches63c3a662011-04-26 08:12:10 +00002406 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002407 if (tp->nvram_lock_cnt > 0)
2408 tp->nvram_lock_cnt--;
2409 if (tp->nvram_lock_cnt == 0)
2410 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2411 }
2412}
2413
2414/* tp->lock is held. */
2415static void tg3_enable_nvram_access(struct tg3 *tp)
2416{
Joe Perches63c3a662011-04-26 08:12:10 +00002417 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002418 u32 nvaccess = tr32(NVRAM_ACCESS);
2419
2420 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2421 }
2422}
2423
2424/* tp->lock is held. */
2425static void tg3_disable_nvram_access(struct tg3 *tp)
2426{
Joe Perches63c3a662011-04-26 08:12:10 +00002427 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002428 u32 nvaccess = tr32(NVRAM_ACCESS);
2429
2430 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2431 }
2432}
2433
2434static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2435 u32 offset, u32 *val)
2436{
2437 u32 tmp;
2438 int i;
2439
2440 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2441 return -EINVAL;
2442
2443 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2444 EEPROM_ADDR_DEVID_MASK |
2445 EEPROM_ADDR_READ);
2446 tw32(GRC_EEPROM_ADDR,
2447 tmp |
2448 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2449 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2450 EEPROM_ADDR_ADDR_MASK) |
2451 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2452
2453 for (i = 0; i < 1000; i++) {
2454 tmp = tr32(GRC_EEPROM_ADDR);
2455
2456 if (tmp & EEPROM_ADDR_COMPLETE)
2457 break;
2458 msleep(1);
2459 }
2460 if (!(tmp & EEPROM_ADDR_COMPLETE))
2461 return -EBUSY;
2462
Matt Carlson62cedd12009-04-20 14:52:29 -07002463 tmp = tr32(GRC_EEPROM_DATA);
2464
2465 /*
2466 * The data will always be opposite the native endian
2467 * format. Perform a blind byteswap to compensate.
2468 */
2469 *val = swab32(tmp);
2470
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002471 return 0;
2472}
2473
2474#define NVRAM_CMD_TIMEOUT 10000
2475
2476static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2477{
2478 int i;
2479
2480 tw32(NVRAM_CMD, nvram_cmd);
2481 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2482 udelay(10);
2483 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2484 udelay(10);
2485 break;
2486 }
2487 }
2488
2489 if (i == NVRAM_CMD_TIMEOUT)
2490 return -EBUSY;
2491
2492 return 0;
2493}
2494
2495static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2496{
Joe Perches63c3a662011-04-26 08:12:10 +00002497 if (tg3_flag(tp, NVRAM) &&
2498 tg3_flag(tp, NVRAM_BUFFERED) &&
2499 tg3_flag(tp, FLASH) &&
2500 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002501 (tp->nvram_jedecnum == JEDEC_ATMEL))
2502
2503 addr = ((addr / tp->nvram_pagesize) <<
2504 ATMEL_AT45DB0X1B_PAGE_POS) +
2505 (addr % tp->nvram_pagesize);
2506
2507 return addr;
2508}
2509
2510static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2511{
Joe Perches63c3a662011-04-26 08:12:10 +00002512 if (tg3_flag(tp, NVRAM) &&
2513 tg3_flag(tp, NVRAM_BUFFERED) &&
2514 tg3_flag(tp, FLASH) &&
2515 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002516 (tp->nvram_jedecnum == JEDEC_ATMEL))
2517
2518 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2519 tp->nvram_pagesize) +
2520 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2521
2522 return addr;
2523}
2524
Matt Carlsone4f34112009-02-25 14:25:00 +00002525/* NOTE: Data read in from NVRAM is byteswapped according to
2526 * the byteswapping settings for all other register accesses.
2527 * tg3 devices are BE devices, so on a BE machine, the data
2528 * returned will be exactly as it is seen in NVRAM. On a LE
2529 * machine, the 32-bit value will be byteswapped.
2530 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002531static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2532{
2533 int ret;
2534
Joe Perches63c3a662011-04-26 08:12:10 +00002535 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002536 return tg3_nvram_read_using_eeprom(tp, offset, val);
2537
2538 offset = tg3_nvram_phys_addr(tp, offset);
2539
2540 if (offset > NVRAM_ADDR_MSK)
2541 return -EINVAL;
2542
2543 ret = tg3_nvram_lock(tp);
2544 if (ret)
2545 return ret;
2546
2547 tg3_enable_nvram_access(tp);
2548
2549 tw32(NVRAM_ADDR, offset);
2550 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2551 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2552
2553 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002554 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002555
2556 tg3_disable_nvram_access(tp);
2557
2558 tg3_nvram_unlock(tp);
2559
2560 return ret;
2561}
2562
Matt Carlsona9dc5292009-02-25 14:25:30 +00002563/* Ensures NVRAM data is in bytestream format. */
2564static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002565{
2566 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002567 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002568 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002569 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002570 return res;
2571}
2572
2573/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002574static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2575{
2576 u32 addr_high, addr_low;
2577 int i;
2578
2579 addr_high = ((tp->dev->dev_addr[0] << 8) |
2580 tp->dev->dev_addr[1]);
2581 addr_low = ((tp->dev->dev_addr[2] << 24) |
2582 (tp->dev->dev_addr[3] << 16) |
2583 (tp->dev->dev_addr[4] << 8) |
2584 (tp->dev->dev_addr[5] << 0));
2585 for (i = 0; i < 4; i++) {
2586 if (i == 1 && skip_mac_1)
2587 continue;
2588 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2589 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2590 }
2591
2592 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2593 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2594 for (i = 0; i < 12; i++) {
2595 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2596 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2597 }
2598 }
2599
2600 addr_high = (tp->dev->dev_addr[0] +
2601 tp->dev->dev_addr[1] +
2602 tp->dev->dev_addr[2] +
2603 tp->dev->dev_addr[3] +
2604 tp->dev->dev_addr[4] +
2605 tp->dev->dev_addr[5]) &
2606 TX_BACKOFF_SEED_MASK;
2607 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2608}
2609
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002610static void tg3_enable_register_access(struct tg3 *tp)
2611{
2612 /*
2613 * Make sure register accesses (indirect or otherwise) will function
2614 * correctly.
2615 */
2616 pci_write_config_dword(tp->pdev,
2617 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2618}
2619
2620static int tg3_power_up(struct tg3 *tp)
2621{
2622 tg3_enable_register_access(tp);
2623
2624 pci_set_power_state(tp->pdev, PCI_D0);
2625
2626 /* Switch out of Vaux if it is a NIC */
Joe Perches63c3a662011-04-26 08:12:10 +00002627 if (tg3_flag(tp, IS_NIC))
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002628 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
2629
2630 return 0;
2631}
2632
2633static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
2635 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002636 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002638 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002639
2640 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00002641 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002642 u16 lnkctl;
2643
2644 pci_read_config_word(tp->pdev,
2645 tp->pcie_cap + PCI_EXP_LNKCTL,
2646 &lnkctl);
2647 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2648 pci_write_config_word(tp->pdev,
2649 tp->pcie_cap + PCI_EXP_LNKCTL,
2650 lnkctl);
2651 }
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2654 tw32(TG3PCI_MISC_HOST_CTRL,
2655 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2656
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002657 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00002658 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002659
Joe Perches63c3a662011-04-26 08:12:10 +00002660 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002661 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002662 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00002663 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002664 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002665 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002666
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002667 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002668
Matt Carlson80096062010-08-02 11:26:06 +00002669 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002670
2671 tp->link_config.orig_speed = phydev->speed;
2672 tp->link_config.orig_duplex = phydev->duplex;
2673 tp->link_config.orig_autoneg = phydev->autoneg;
2674 tp->link_config.orig_advertising = phydev->advertising;
2675
2676 advertising = ADVERTISED_TP |
2677 ADVERTISED_Pause |
2678 ADVERTISED_Autoneg |
2679 ADVERTISED_10baseT_Half;
2680
Joe Perches63c3a662011-04-26 08:12:10 +00002681 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
2682 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002683 advertising |=
2684 ADVERTISED_100baseT_Half |
2685 ADVERTISED_100baseT_Full |
2686 ADVERTISED_10baseT_Full;
2687 else
2688 advertising |= ADVERTISED_10baseT_Full;
2689 }
2690
2691 phydev->advertising = advertising;
2692
2693 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002694
2695 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002696 if (phyid != PHY_ID_BCMAC131) {
2697 phyid &= PHY_BCM_OUI_MASK;
2698 if (phyid == PHY_BCM_OUI_1 ||
2699 phyid == PHY_BCM_OUI_2 ||
2700 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002701 do_low_power = true;
2702 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002703 }
Matt Carlsondd477002008-05-25 23:45:58 -07002704 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002705 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002706
Matt Carlson80096062010-08-02 11:26:06 +00002707 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2708 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002709 tp->link_config.orig_speed = tp->link_config.speed;
2710 tp->link_config.orig_duplex = tp->link_config.duplex;
2711 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002714 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002715 tp->link_config.speed = SPEED_10;
2716 tp->link_config.duplex = DUPLEX_HALF;
2717 tp->link_config.autoneg = AUTONEG_ENABLE;
2718 tg3_setup_phy(tp, 0);
2719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 }
2721
Michael Chanb5d37722006-09-27 16:06:21 -07002722 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2723 u32 val;
2724
2725 val = tr32(GRC_VCPU_EXT_CTRL);
2726 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00002727 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002728 int i;
2729 u32 val;
2730
2731 for (i = 0; i < 200; i++) {
2732 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2733 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2734 break;
2735 msleep(1);
2736 }
2737 }
Joe Perches63c3a662011-04-26 08:12:10 +00002738 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07002739 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2740 WOL_DRV_STATE_SHUTDOWN |
2741 WOL_DRV_WOL |
2742 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002743
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002744 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 u32 mac_mode;
2746
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002747 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002748 if (do_low_power &&
2749 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
2750 tg3_phy_auxctl_write(tp,
2751 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
2752 MII_TG3_AUXCTL_PCTL_WOL_EN |
2753 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2754 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07002755 udelay(40);
2756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002758 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002759 mac_mode = MAC_MODE_PORT_MODE_GMII;
2760 else
2761 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002763 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2764 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2765 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00002766 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002767 SPEED_100 : SPEED_10;
2768 if (tg3_5700_link_polarity(tp, speed))
2769 mac_mode |= MAC_MODE_LINK_POLARITY;
2770 else
2771 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 } else {
2774 mac_mode = MAC_MODE_PORT_MODE_TBI;
2775 }
2776
Joe Perches63c3a662011-04-26 08:12:10 +00002777 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 tw32(MAC_LED_CTRL, tp->led_ctrl);
2779
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002780 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00002781 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
2782 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002783 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Joe Perches63c3a662011-04-26 08:12:10 +00002785 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002786 mac_mode |= MAC_MODE_APE_TX_EN |
2787 MAC_MODE_APE_RX_EN |
2788 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002789
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 tw32_f(MAC_MODE, mac_mode);
2791 udelay(100);
2792
2793 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2794 udelay(10);
2795 }
2796
Joe Perches63c3a662011-04-26 08:12:10 +00002797 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2799 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2800 u32 base_val;
2801
2802 base_val = tp->pci_clock_ctrl;
2803 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2804 CLOCK_CTRL_TXCLK_DISABLE);
2805
Michael Chanb401e9e2005-12-19 16:27:04 -08002806 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2807 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00002808 } else if (tg3_flag(tp, 5780_CLASS) ||
2809 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00002810 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002811 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00002812 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 u32 newbits1, newbits2;
2814
2815 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2816 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2817 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2818 CLOCK_CTRL_TXCLK_DISABLE |
2819 CLOCK_CTRL_ALTCLK);
2820 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00002821 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 newbits1 = CLOCK_CTRL_625_CORE;
2823 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2824 } else {
2825 newbits1 = CLOCK_CTRL_ALTCLK;
2826 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2827 }
2828
Michael Chanb401e9e2005-12-19 16:27:04 -08002829 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2830 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Michael Chanb401e9e2005-12-19 16:27:04 -08002832 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2833 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Joe Perches63c3a662011-04-26 08:12:10 +00002835 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 u32 newbits3;
2837
2838 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2839 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2840 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2841 CLOCK_CTRL_TXCLK_DISABLE |
2842 CLOCK_CTRL_44MHZ_CORE);
2843 } else {
2844 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2845 }
2846
Michael Chanb401e9e2005-12-19 16:27:04 -08002847 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2848 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 }
2850 }
2851
Joe Perches63c3a662011-04-26 08:12:10 +00002852 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002853 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 tg3_frob_aux_power(tp);
2856
2857 /* Workaround for unstable PLL clock */
2858 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2859 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2860 u32 val = tr32(0x7d00);
2861
2862 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2863 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00002864 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002865 int err;
2866
2867 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002869 if (!err)
2870 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 }
2873
Michael Chanbbadf502006-04-06 21:46:34 -07002874 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 return 0;
2877}
2878
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002879static void tg3_power_down(struct tg3 *tp)
2880{
2881 tg3_power_down_prepare(tp);
2882
Joe Perches63c3a662011-04-26 08:12:10 +00002883 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002884 pci_set_power_state(tp->pdev, PCI_D3hot);
2885}
2886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2888{
2889 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2890 case MII_TG3_AUX_STAT_10HALF:
2891 *speed = SPEED_10;
2892 *duplex = DUPLEX_HALF;
2893 break;
2894
2895 case MII_TG3_AUX_STAT_10FULL:
2896 *speed = SPEED_10;
2897 *duplex = DUPLEX_FULL;
2898 break;
2899
2900 case MII_TG3_AUX_STAT_100HALF:
2901 *speed = SPEED_100;
2902 *duplex = DUPLEX_HALF;
2903 break;
2904
2905 case MII_TG3_AUX_STAT_100FULL:
2906 *speed = SPEED_100;
2907 *duplex = DUPLEX_FULL;
2908 break;
2909
2910 case MII_TG3_AUX_STAT_1000HALF:
2911 *speed = SPEED_1000;
2912 *duplex = DUPLEX_HALF;
2913 break;
2914
2915 case MII_TG3_AUX_STAT_1000FULL:
2916 *speed = SPEED_1000;
2917 *duplex = DUPLEX_FULL;
2918 break;
2919
2920 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002921 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002922 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2923 SPEED_10;
2924 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2925 DUPLEX_HALF;
2926 break;
2927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 *speed = SPEED_INVALID;
2929 *duplex = DUPLEX_INVALID;
2930 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932}
2933
Matt Carlson42b64a42011-05-19 12:12:49 +00002934static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
Matt Carlson42b64a42011-05-19 12:12:49 +00002936 int err = 0;
2937 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Matt Carlson42b64a42011-05-19 12:12:49 +00002939 new_adv = ADVERTISE_CSMA;
2940 if (advertise & ADVERTISED_10baseT_Half)
2941 new_adv |= ADVERTISE_10HALF;
2942 if (advertise & ADVERTISED_10baseT_Full)
2943 new_adv |= ADVERTISE_10FULL;
2944 if (advertise & ADVERTISED_100baseT_Half)
2945 new_adv |= ADVERTISE_100HALF;
2946 if (advertise & ADVERTISED_100baseT_Full)
2947 new_adv |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Matt Carlson42b64a42011-05-19 12:12:49 +00002949 new_adv |= tg3_advert_flowctrl_1000T(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Matt Carlson42b64a42011-05-19 12:12:49 +00002951 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
2952 if (err)
2953 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Matt Carlson42b64a42011-05-19 12:12:49 +00002955 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
2956 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002957
Matt Carlson42b64a42011-05-19 12:12:49 +00002958 new_adv = 0;
2959 if (advertise & ADVERTISED_1000baseT_Half)
2960 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2961 if (advertise & ADVERTISED_1000baseT_Full)
2962 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002963
Matt Carlson42b64a42011-05-19 12:12:49 +00002964 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2965 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2966 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2967 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
Matt Carlson42b64a42011-05-19 12:12:49 +00002969 err = tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2970 if (err)
2971 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002972
Matt Carlson42b64a42011-05-19 12:12:49 +00002973 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2974 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Matt Carlson42b64a42011-05-19 12:12:49 +00002976 tw32(TG3_CPMU_EEE_MODE,
2977 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002978
Matt Carlson42b64a42011-05-19 12:12:49 +00002979 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2980 if (!err) {
2981 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00002982
Matt Carlson21a00ab2011-01-25 15:58:55 +00002983 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
2984 case ASIC_REV_5717:
2985 case ASIC_REV_57765:
2986 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
2987 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
2988 MII_TG3_DSP_CH34TP2_HIBW01);
2989 /* Fall through */
2990 case ASIC_REV_5719:
2991 val = MII_TG3_DSP_TAP26_ALNOKO |
2992 MII_TG3_DSP_TAP26_RMRXSTO |
2993 MII_TG3_DSP_TAP26_OPCSINPT;
2994 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
2995 }
Matt Carlson52b02d02010-10-14 10:37:41 +00002996
Matt Carlsona6b68da2010-12-06 08:28:52 +00002997 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00002998 /* Advertise 100-BaseTX EEE ability */
2999 if (advertise & ADVERTISED_100baseT_Full)
3000 val |= MDIO_AN_EEE_ADV_100TX;
3001 /* Advertise 1000-BaseT EEE ability */
3002 if (advertise & ADVERTISED_1000baseT_Full)
3003 val |= MDIO_AN_EEE_ADV_1000T;
3004 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlson52b02d02010-10-14 10:37:41 +00003005
Matt Carlson42b64a42011-05-19 12:12:49 +00003006 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3007 if (!err)
3008 err = err2;
3009 }
3010
3011done:
3012 return err;
3013}
3014
3015static void tg3_phy_copper_begin(struct tg3 *tp)
3016{
3017 u32 new_adv;
3018 int i;
3019
3020 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3021 new_adv = ADVERTISED_10baseT_Half |
3022 ADVERTISED_10baseT_Full;
3023 if (tg3_flag(tp, WOL_SPEED_100MB))
3024 new_adv |= ADVERTISED_100baseT_Half |
3025 ADVERTISED_100baseT_Full;
3026
3027 tg3_phy_autoneg_cfg(tp, new_adv,
3028 FLOW_CTRL_TX | FLOW_CTRL_RX);
3029 } else if (tp->link_config.speed == SPEED_INVALID) {
3030 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3031 tp->link_config.advertising &=
3032 ~(ADVERTISED_1000baseT_Half |
3033 ADVERTISED_1000baseT_Full);
3034
3035 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3036 tp->link_config.flowctrl);
3037 } else {
3038 /* Asking for a specific link mode. */
3039 if (tp->link_config.speed == SPEED_1000) {
3040 if (tp->link_config.duplex == DUPLEX_FULL)
3041 new_adv = ADVERTISED_1000baseT_Full;
3042 else
3043 new_adv = ADVERTISED_1000baseT_Half;
3044 } else if (tp->link_config.speed == SPEED_100) {
3045 if (tp->link_config.duplex == DUPLEX_FULL)
3046 new_adv = ADVERTISED_100baseT_Full;
3047 else
3048 new_adv = ADVERTISED_100baseT_Half;
3049 } else {
3050 if (tp->link_config.duplex == DUPLEX_FULL)
3051 new_adv = ADVERTISED_10baseT_Full;
3052 else
3053 new_adv = ADVERTISED_10baseT_Half;
3054 }
3055
3056 tg3_phy_autoneg_cfg(tp, new_adv,
3057 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003058 }
3059
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3061 tp->link_config.speed != SPEED_INVALID) {
3062 u32 bmcr, orig_bmcr;
3063
3064 tp->link_config.active_speed = tp->link_config.speed;
3065 tp->link_config.active_duplex = tp->link_config.duplex;
3066
3067 bmcr = 0;
3068 switch (tp->link_config.speed) {
3069 default:
3070 case SPEED_10:
3071 break;
3072
3073 case SPEED_100:
3074 bmcr |= BMCR_SPEED100;
3075 break;
3076
3077 case SPEED_1000:
3078 bmcr |= TG3_BMCR_SPEED1000;
3079 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
3082 if (tp->link_config.duplex == DUPLEX_FULL)
3083 bmcr |= BMCR_FULLDPLX;
3084
3085 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3086 (bmcr != orig_bmcr)) {
3087 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3088 for (i = 0; i < 1500; i++) {
3089 u32 tmp;
3090
3091 udelay(10);
3092 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3093 tg3_readphy(tp, MII_BMSR, &tmp))
3094 continue;
3095 if (!(tmp & BMSR_LSTATUS)) {
3096 udelay(40);
3097 break;
3098 }
3099 }
3100 tg3_writephy(tp, MII_BMCR, bmcr);
3101 udelay(40);
3102 }
3103 } else {
3104 tg3_writephy(tp, MII_BMCR,
3105 BMCR_ANENABLE | BMCR_ANRESTART);
3106 }
3107}
3108
3109static int tg3_init_5401phy_dsp(struct tg3 *tp)
3110{
3111 int err;
3112
3113 /* Turn off tap power management. */
3114 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003115 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003117 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3118 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3119 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3120 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3121 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
3123 udelay(40);
3124
3125 return err;
3126}
3127
Michael Chan3600d912006-12-07 00:21:48 -08003128static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
Michael Chan3600d912006-12-07 00:21:48 -08003130 u32 adv_reg, all_mask = 0;
3131
3132 if (mask & ADVERTISED_10baseT_Half)
3133 all_mask |= ADVERTISE_10HALF;
3134 if (mask & ADVERTISED_10baseT_Full)
3135 all_mask |= ADVERTISE_10FULL;
3136 if (mask & ADVERTISED_100baseT_Half)
3137 all_mask |= ADVERTISE_100HALF;
3138 if (mask & ADVERTISED_100baseT_Full)
3139 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
3141 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3142 return 0;
3143
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 if ((adv_reg & all_mask) != all_mask)
3145 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003146 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 u32 tg3_ctrl;
3148
Michael Chan3600d912006-12-07 00:21:48 -08003149 all_mask = 0;
3150 if (mask & ADVERTISED_1000baseT_Half)
3151 all_mask |= ADVERTISE_1000HALF;
3152 if (mask & ADVERTISED_1000baseT_Full)
3153 all_mask |= ADVERTISE_1000FULL;
3154
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3156 return 0;
3157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 if ((tg3_ctrl & all_mask) != all_mask)
3159 return 0;
3160 }
3161 return 1;
3162}
3163
Matt Carlsonef167e22007-12-20 20:10:01 -08003164static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3165{
3166 u32 curadv, reqadv;
3167
3168 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3169 return 1;
3170
3171 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3172 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3173
3174 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3175 if (curadv != reqadv)
3176 return 0;
3177
Joe Perches63c3a662011-04-26 08:12:10 +00003178 if (tg3_flag(tp, PAUSE_AUTONEG))
Matt Carlsonef167e22007-12-20 20:10:01 -08003179 tg3_readphy(tp, MII_LPA, rmtadv);
3180 } else {
3181 /* Reprogram the advertisement register, even if it
3182 * does not affect the current link. If the link
3183 * gets renegotiated in the future, we can save an
3184 * additional renegotiation cycle by advertising
3185 * it correctly in the first place.
3186 */
3187 if (curadv != reqadv) {
3188 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3189 ADVERTISE_PAUSE_ASYM);
3190 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3191 }
3192 }
3193
3194 return 1;
3195}
3196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3198{
3199 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003200 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003201 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 u16 current_speed;
3203 u8 current_duplex;
3204 int i, err;
3205
3206 tw32(MAC_EVENT, 0);
3207
3208 tw32_f(MAC_STATUS,
3209 (MAC_STATUS_SYNC_CHANGED |
3210 MAC_STATUS_CFG_CHANGED |
3211 MAC_STATUS_MI_COMPLETION |
3212 MAC_STATUS_LNKSTATE_CHANGED));
3213 udelay(40);
3214
Matt Carlson8ef21422008-05-02 16:47:53 -07003215 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3216 tw32_f(MAC_MI_MODE,
3217 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3218 udelay(80);
3219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003221 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
3223 /* Some third-party PHYs need to be reset on link going
3224 * down.
3225 */
3226 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3227 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3228 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3229 netif_carrier_ok(tp->dev)) {
3230 tg3_readphy(tp, MII_BMSR, &bmsr);
3231 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3232 !(bmsr & BMSR_LSTATUS))
3233 force_reset = 1;
3234 }
3235 if (force_reset)
3236 tg3_phy_reset(tp);
3237
Matt Carlson79eb6902010-02-17 15:17:03 +00003238 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 tg3_readphy(tp, MII_BMSR, &bmsr);
3240 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003241 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 bmsr = 0;
3243
3244 if (!(bmsr & BMSR_LSTATUS)) {
3245 err = tg3_init_5401phy_dsp(tp);
3246 if (err)
3247 return err;
3248
3249 tg3_readphy(tp, MII_BMSR, &bmsr);
3250 for (i = 0; i < 1000; i++) {
3251 udelay(10);
3252 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3253 (bmsr & BMSR_LSTATUS)) {
3254 udelay(40);
3255 break;
3256 }
3257 }
3258
Matt Carlson79eb6902010-02-17 15:17:03 +00003259 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3260 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 !(bmsr & BMSR_LSTATUS) &&
3262 tp->link_config.active_speed == SPEED_1000) {
3263 err = tg3_phy_reset(tp);
3264 if (!err)
3265 err = tg3_init_5401phy_dsp(tp);
3266 if (err)
3267 return err;
3268 }
3269 }
3270 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3271 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3272 /* 5701 {A0,B0} CRC bug workaround */
3273 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003274 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3275 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3276 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 }
3278
3279 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003280 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3281 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003283 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003285 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3287
3288 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3289 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3290 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3291 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3292 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3293 else
3294 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3295 }
3296
3297 current_link_up = 0;
3298 current_speed = SPEED_INVALID;
3299 current_duplex = DUPLEX_INVALID;
3300
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003301 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003302 err = tg3_phy_auxctl_read(tp,
3303 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3304 &val);
3305 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003306 tg3_phy_auxctl_write(tp,
3307 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3308 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 goto relink;
3310 }
3311 }
3312
3313 bmsr = 0;
3314 for (i = 0; i < 100; i++) {
3315 tg3_readphy(tp, MII_BMSR, &bmsr);
3316 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3317 (bmsr & BMSR_LSTATUS))
3318 break;
3319 udelay(40);
3320 }
3321
3322 if (bmsr & BMSR_LSTATUS) {
3323 u32 aux_stat, bmcr;
3324
3325 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3326 for (i = 0; i < 2000; i++) {
3327 udelay(10);
3328 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3329 aux_stat)
3330 break;
3331 }
3332
3333 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3334 &current_speed,
3335 &current_duplex);
3336
3337 bmcr = 0;
3338 for (i = 0; i < 200; i++) {
3339 tg3_readphy(tp, MII_BMCR, &bmcr);
3340 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3341 continue;
3342 if (bmcr && bmcr != 0x7fff)
3343 break;
3344 udelay(10);
3345 }
3346
Matt Carlsonef167e22007-12-20 20:10:01 -08003347 lcl_adv = 0;
3348 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Matt Carlsonef167e22007-12-20 20:10:01 -08003350 tp->link_config.active_speed = current_speed;
3351 tp->link_config.active_duplex = current_duplex;
3352
3353 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3354 if ((bmcr & BMCR_ANENABLE) &&
3355 tg3_copper_is_advertising_all(tp,
3356 tp->link_config.advertising)) {
3357 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3358 &rmt_adv))
3359 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 }
3361 } else {
3362 if (!(bmcr & BMCR_ANENABLE) &&
3363 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003364 tp->link_config.duplex == current_duplex &&
3365 tp->link_config.flowctrl ==
3366 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 }
3369 }
3370
Matt Carlsonef167e22007-12-20 20:10:01 -08003371 if (current_link_up == 1 &&
3372 tp->link_config.active_duplex == DUPLEX_FULL)
3373 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 }
3375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376relink:
Matt Carlson80096062010-08-02 11:26:06 +00003377 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 tg3_phy_copper_begin(tp);
3379
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003380 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00003381 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
3382 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 current_link_up = 1;
3384 }
3385
3386 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3387 if (current_link_up == 1) {
3388 if (tp->link_config.active_speed == SPEED_100 ||
3389 tp->link_config.active_speed == SPEED_10)
3390 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3391 else
3392 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003393 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003394 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3395 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3397
3398 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3399 if (tp->link_config.active_duplex == DUPLEX_HALF)
3400 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3401
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003403 if (current_link_up == 1 &&
3404 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003406 else
3407 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 }
3409
3410 /* ??? Without this setting Netgear GA302T PHY does not
3411 * ??? send/receive packets...
3412 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003413 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3415 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3416 tw32_f(MAC_MI_MODE, tp->mi_mode);
3417 udelay(80);
3418 }
3419
3420 tw32_f(MAC_MODE, tp->mac_mode);
3421 udelay(40);
3422
Matt Carlson52b02d02010-10-14 10:37:41 +00003423 tg3_phy_eee_adjust(tp, current_link_up);
3424
Joe Perches63c3a662011-04-26 08:12:10 +00003425 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 /* Polled via timer. */
3427 tw32_f(MAC_EVENT, 0);
3428 } else {
3429 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3430 }
3431 udelay(40);
3432
3433 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3434 current_link_up == 1 &&
3435 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00003436 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 udelay(120);
3438 tw32_f(MAC_STATUS,
3439 (MAC_STATUS_SYNC_CHANGED |
3440 MAC_STATUS_CFG_CHANGED));
3441 udelay(40);
3442 tg3_write_mem(tp,
3443 NIC_SRAM_FIRMWARE_MBOX,
3444 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3445 }
3446
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003447 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00003448 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003449 u16 oldlnkctl, newlnkctl;
3450
3451 pci_read_config_word(tp->pdev,
3452 tp->pcie_cap + PCI_EXP_LNKCTL,
3453 &oldlnkctl);
3454 if (tp->link_config.active_speed == SPEED_100 ||
3455 tp->link_config.active_speed == SPEED_10)
3456 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3457 else
3458 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3459 if (newlnkctl != oldlnkctl)
3460 pci_write_config_word(tp->pdev,
3461 tp->pcie_cap + PCI_EXP_LNKCTL,
3462 newlnkctl);
3463 }
3464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 if (current_link_up != netif_carrier_ok(tp->dev)) {
3466 if (current_link_up)
3467 netif_carrier_on(tp->dev);
3468 else
3469 netif_carrier_off(tp->dev);
3470 tg3_link_report(tp);
3471 }
3472
3473 return 0;
3474}
3475
3476struct tg3_fiber_aneginfo {
3477 int state;
3478#define ANEG_STATE_UNKNOWN 0
3479#define ANEG_STATE_AN_ENABLE 1
3480#define ANEG_STATE_RESTART_INIT 2
3481#define ANEG_STATE_RESTART 3
3482#define ANEG_STATE_DISABLE_LINK_OK 4
3483#define ANEG_STATE_ABILITY_DETECT_INIT 5
3484#define ANEG_STATE_ABILITY_DETECT 6
3485#define ANEG_STATE_ACK_DETECT_INIT 7
3486#define ANEG_STATE_ACK_DETECT 8
3487#define ANEG_STATE_COMPLETE_ACK_INIT 9
3488#define ANEG_STATE_COMPLETE_ACK 10
3489#define ANEG_STATE_IDLE_DETECT_INIT 11
3490#define ANEG_STATE_IDLE_DETECT 12
3491#define ANEG_STATE_LINK_OK 13
3492#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3493#define ANEG_STATE_NEXT_PAGE_WAIT 15
3494
3495 u32 flags;
3496#define MR_AN_ENABLE 0x00000001
3497#define MR_RESTART_AN 0x00000002
3498#define MR_AN_COMPLETE 0x00000004
3499#define MR_PAGE_RX 0x00000008
3500#define MR_NP_LOADED 0x00000010
3501#define MR_TOGGLE_TX 0x00000020
3502#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3503#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3504#define MR_LP_ADV_SYM_PAUSE 0x00000100
3505#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3506#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3507#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3508#define MR_LP_ADV_NEXT_PAGE 0x00001000
3509#define MR_TOGGLE_RX 0x00002000
3510#define MR_NP_RX 0x00004000
3511
3512#define MR_LINK_OK 0x80000000
3513
3514 unsigned long link_time, cur_time;
3515
3516 u32 ability_match_cfg;
3517 int ability_match_count;
3518
3519 char ability_match, idle_match, ack_match;
3520
3521 u32 txconfig, rxconfig;
3522#define ANEG_CFG_NP 0x00000080
3523#define ANEG_CFG_ACK 0x00000040
3524#define ANEG_CFG_RF2 0x00000020
3525#define ANEG_CFG_RF1 0x00000010
3526#define ANEG_CFG_PS2 0x00000001
3527#define ANEG_CFG_PS1 0x00008000
3528#define ANEG_CFG_HD 0x00004000
3529#define ANEG_CFG_FD 0x00002000
3530#define ANEG_CFG_INVAL 0x00001f06
3531
3532};
3533#define ANEG_OK 0
3534#define ANEG_DONE 1
3535#define ANEG_TIMER_ENAB 2
3536#define ANEG_FAILED -1
3537
3538#define ANEG_STATE_SETTLE_TIME 10000
3539
3540static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3541 struct tg3_fiber_aneginfo *ap)
3542{
Matt Carlson5be73b42007-12-20 20:09:29 -08003543 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 unsigned long delta;
3545 u32 rx_cfg_reg;
3546 int ret;
3547
3548 if (ap->state == ANEG_STATE_UNKNOWN) {
3549 ap->rxconfig = 0;
3550 ap->link_time = 0;
3551 ap->cur_time = 0;
3552 ap->ability_match_cfg = 0;
3553 ap->ability_match_count = 0;
3554 ap->ability_match = 0;
3555 ap->idle_match = 0;
3556 ap->ack_match = 0;
3557 }
3558 ap->cur_time++;
3559
3560 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3561 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3562
3563 if (rx_cfg_reg != ap->ability_match_cfg) {
3564 ap->ability_match_cfg = rx_cfg_reg;
3565 ap->ability_match = 0;
3566 ap->ability_match_count = 0;
3567 } else {
3568 if (++ap->ability_match_count > 1) {
3569 ap->ability_match = 1;
3570 ap->ability_match_cfg = rx_cfg_reg;
3571 }
3572 }
3573 if (rx_cfg_reg & ANEG_CFG_ACK)
3574 ap->ack_match = 1;
3575 else
3576 ap->ack_match = 0;
3577
3578 ap->idle_match = 0;
3579 } else {
3580 ap->idle_match = 1;
3581 ap->ability_match_cfg = 0;
3582 ap->ability_match_count = 0;
3583 ap->ability_match = 0;
3584 ap->ack_match = 0;
3585
3586 rx_cfg_reg = 0;
3587 }
3588
3589 ap->rxconfig = rx_cfg_reg;
3590 ret = ANEG_OK;
3591
Matt Carlson33f401a2010-04-05 10:19:27 +00003592 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 case ANEG_STATE_UNKNOWN:
3594 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3595 ap->state = ANEG_STATE_AN_ENABLE;
3596
3597 /* fallthru */
3598 case ANEG_STATE_AN_ENABLE:
3599 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3600 if (ap->flags & MR_AN_ENABLE) {
3601 ap->link_time = 0;
3602 ap->cur_time = 0;
3603 ap->ability_match_cfg = 0;
3604 ap->ability_match_count = 0;
3605 ap->ability_match = 0;
3606 ap->idle_match = 0;
3607 ap->ack_match = 0;
3608
3609 ap->state = ANEG_STATE_RESTART_INIT;
3610 } else {
3611 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3612 }
3613 break;
3614
3615 case ANEG_STATE_RESTART_INIT:
3616 ap->link_time = ap->cur_time;
3617 ap->flags &= ~(MR_NP_LOADED);
3618 ap->txconfig = 0;
3619 tw32(MAC_TX_AUTO_NEG, 0);
3620 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3621 tw32_f(MAC_MODE, tp->mac_mode);
3622 udelay(40);
3623
3624 ret = ANEG_TIMER_ENAB;
3625 ap->state = ANEG_STATE_RESTART;
3626
3627 /* fallthru */
3628 case ANEG_STATE_RESTART:
3629 delta = ap->cur_time - ap->link_time;
Matt Carlson859a5882010-04-05 10:19:28 +00003630 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a5882010-04-05 10:19:28 +00003632 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 break;
3635
3636 case ANEG_STATE_DISABLE_LINK_OK:
3637 ret = ANEG_DONE;
3638 break;
3639
3640 case ANEG_STATE_ABILITY_DETECT_INIT:
3641 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003642 ap->txconfig = ANEG_CFG_FD;
3643 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3644 if (flowctrl & ADVERTISE_1000XPAUSE)
3645 ap->txconfig |= ANEG_CFG_PS1;
3646 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3647 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3649 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3650 tw32_f(MAC_MODE, tp->mac_mode);
3651 udelay(40);
3652
3653 ap->state = ANEG_STATE_ABILITY_DETECT;
3654 break;
3655
3656 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a5882010-04-05 10:19:28 +00003657 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 break;
3660
3661 case ANEG_STATE_ACK_DETECT_INIT:
3662 ap->txconfig |= ANEG_CFG_ACK;
3663 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3664 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3665 tw32_f(MAC_MODE, tp->mac_mode);
3666 udelay(40);
3667
3668 ap->state = ANEG_STATE_ACK_DETECT;
3669
3670 /* fallthru */
3671 case ANEG_STATE_ACK_DETECT:
3672 if (ap->ack_match != 0) {
3673 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3674 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3675 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3676 } else {
3677 ap->state = ANEG_STATE_AN_ENABLE;
3678 }
3679 } else if (ap->ability_match != 0 &&
3680 ap->rxconfig == 0) {
3681 ap->state = ANEG_STATE_AN_ENABLE;
3682 }
3683 break;
3684
3685 case ANEG_STATE_COMPLETE_ACK_INIT:
3686 if (ap->rxconfig & ANEG_CFG_INVAL) {
3687 ret = ANEG_FAILED;
3688 break;
3689 }
3690 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3691 MR_LP_ADV_HALF_DUPLEX |
3692 MR_LP_ADV_SYM_PAUSE |
3693 MR_LP_ADV_ASYM_PAUSE |
3694 MR_LP_ADV_REMOTE_FAULT1 |
3695 MR_LP_ADV_REMOTE_FAULT2 |
3696 MR_LP_ADV_NEXT_PAGE |
3697 MR_TOGGLE_RX |
3698 MR_NP_RX);
3699 if (ap->rxconfig & ANEG_CFG_FD)
3700 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3701 if (ap->rxconfig & ANEG_CFG_HD)
3702 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3703 if (ap->rxconfig & ANEG_CFG_PS1)
3704 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3705 if (ap->rxconfig & ANEG_CFG_PS2)
3706 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3707 if (ap->rxconfig & ANEG_CFG_RF1)
3708 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3709 if (ap->rxconfig & ANEG_CFG_RF2)
3710 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3711 if (ap->rxconfig & ANEG_CFG_NP)
3712 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3713
3714 ap->link_time = ap->cur_time;
3715
3716 ap->flags ^= (MR_TOGGLE_TX);
3717 if (ap->rxconfig & 0x0008)
3718 ap->flags |= MR_TOGGLE_RX;
3719 if (ap->rxconfig & ANEG_CFG_NP)
3720 ap->flags |= MR_NP_RX;
3721 ap->flags |= MR_PAGE_RX;
3722
3723 ap->state = ANEG_STATE_COMPLETE_ACK;
3724 ret = ANEG_TIMER_ENAB;
3725 break;
3726
3727 case ANEG_STATE_COMPLETE_ACK:
3728 if (ap->ability_match != 0 &&
3729 ap->rxconfig == 0) {
3730 ap->state = ANEG_STATE_AN_ENABLE;
3731 break;
3732 }
3733 delta = ap->cur_time - ap->link_time;
3734 if (delta > ANEG_STATE_SETTLE_TIME) {
3735 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3736 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3737 } else {
3738 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3739 !(ap->flags & MR_NP_RX)) {
3740 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3741 } else {
3742 ret = ANEG_FAILED;
3743 }
3744 }
3745 }
3746 break;
3747
3748 case ANEG_STATE_IDLE_DETECT_INIT:
3749 ap->link_time = ap->cur_time;
3750 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3751 tw32_f(MAC_MODE, tp->mac_mode);
3752 udelay(40);
3753
3754 ap->state = ANEG_STATE_IDLE_DETECT;
3755 ret = ANEG_TIMER_ENAB;
3756 break;
3757
3758 case ANEG_STATE_IDLE_DETECT:
3759 if (ap->ability_match != 0 &&
3760 ap->rxconfig == 0) {
3761 ap->state = ANEG_STATE_AN_ENABLE;
3762 break;
3763 }
3764 delta = ap->cur_time - ap->link_time;
3765 if (delta > ANEG_STATE_SETTLE_TIME) {
3766 /* XXX another gem from the Broadcom driver :( */
3767 ap->state = ANEG_STATE_LINK_OK;
3768 }
3769 break;
3770
3771 case ANEG_STATE_LINK_OK:
3772 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3773 ret = ANEG_DONE;
3774 break;
3775
3776 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3777 /* ??? unimplemented */
3778 break;
3779
3780 case ANEG_STATE_NEXT_PAGE_WAIT:
3781 /* ??? unimplemented */
3782 break;
3783
3784 default:
3785 ret = ANEG_FAILED;
3786 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
3789 return ret;
3790}
3791
Matt Carlson5be73b42007-12-20 20:09:29 -08003792static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793{
3794 int res = 0;
3795 struct tg3_fiber_aneginfo aninfo;
3796 int status = ANEG_FAILED;
3797 unsigned int tick;
3798 u32 tmp;
3799
3800 tw32_f(MAC_TX_AUTO_NEG, 0);
3801
3802 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3803 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3804 udelay(40);
3805
3806 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3807 udelay(40);
3808
3809 memset(&aninfo, 0, sizeof(aninfo));
3810 aninfo.flags |= MR_AN_ENABLE;
3811 aninfo.state = ANEG_STATE_UNKNOWN;
3812 aninfo.cur_time = 0;
3813 tick = 0;
3814 while (++tick < 195000) {
3815 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3816 if (status == ANEG_DONE || status == ANEG_FAILED)
3817 break;
3818
3819 udelay(1);
3820 }
3821
3822 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3823 tw32_f(MAC_MODE, tp->mac_mode);
3824 udelay(40);
3825
Matt Carlson5be73b42007-12-20 20:09:29 -08003826 *txflags = aninfo.txconfig;
3827 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
3829 if (status == ANEG_DONE &&
3830 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3831 MR_LP_ADV_FULL_DUPLEX)))
3832 res = 1;
3833
3834 return res;
3835}
3836
3837static void tg3_init_bcm8002(struct tg3 *tp)
3838{
3839 u32 mac_status = tr32(MAC_STATUS);
3840 int i;
3841
3842 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00003843 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 !(mac_status & MAC_STATUS_PCS_SYNCED))
3845 return;
3846
3847 /* Set PLL lock range. */
3848 tg3_writephy(tp, 0x16, 0x8007);
3849
3850 /* SW reset */
3851 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3852
3853 /* Wait for reset to complete. */
3854 /* XXX schedule_timeout() ... */
3855 for (i = 0; i < 500; i++)
3856 udelay(10);
3857
3858 /* Config mode; select PMA/Ch 1 regs. */
3859 tg3_writephy(tp, 0x10, 0x8411);
3860
3861 /* Enable auto-lock and comdet, select txclk for tx. */
3862 tg3_writephy(tp, 0x11, 0x0a10);
3863
3864 tg3_writephy(tp, 0x18, 0x00a0);
3865 tg3_writephy(tp, 0x16, 0x41ff);
3866
3867 /* Assert and deassert POR. */
3868 tg3_writephy(tp, 0x13, 0x0400);
3869 udelay(40);
3870 tg3_writephy(tp, 0x13, 0x0000);
3871
3872 tg3_writephy(tp, 0x11, 0x0a50);
3873 udelay(40);
3874 tg3_writephy(tp, 0x11, 0x0a10);
3875
3876 /* Wait for signal to stabilize */
3877 /* XXX schedule_timeout() ... */
3878 for (i = 0; i < 15000; i++)
3879 udelay(10);
3880
3881 /* Deselect the channel register so we can read the PHYID
3882 * later.
3883 */
3884 tg3_writephy(tp, 0x10, 0x8011);
3885}
3886
3887static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3888{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003889 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 u32 sg_dig_ctrl, sg_dig_status;
3891 u32 serdes_cfg, expected_sg_dig_ctrl;
3892 int workaround, port_a;
3893 int current_link_up;
3894
3895 serdes_cfg = 0;
3896 expected_sg_dig_ctrl = 0;
3897 workaround = 0;
3898 port_a = 1;
3899 current_link_up = 0;
3900
3901 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3902 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3903 workaround = 1;
3904 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3905 port_a = 0;
3906
3907 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3908 /* preserve bits 20-23 for voltage regulator */
3909 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3910 }
3911
3912 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3913
3914 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003915 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 if (workaround) {
3917 u32 val = serdes_cfg;
3918
3919 if (port_a)
3920 val |= 0xc010000;
3921 else
3922 val |= 0x4010000;
3923 tw32_f(MAC_SERDES_CFG, val);
3924 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003925
3926 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 }
3928 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3929 tg3_setup_flow_control(tp, 0, 0);
3930 current_link_up = 1;
3931 }
3932 goto out;
3933 }
3934
3935 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003936 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
Matt Carlson82cd3d12007-12-20 20:09:00 -08003938 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3939 if (flowctrl & ADVERTISE_1000XPAUSE)
3940 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3941 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3942 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943
3944 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003945 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07003946 tp->serdes_counter &&
3947 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3948 MAC_STATUS_RCVD_CFG)) ==
3949 MAC_STATUS_PCS_SYNCED)) {
3950 tp->serdes_counter--;
3951 current_link_up = 1;
3952 goto out;
3953 }
3954restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 if (workaround)
3956 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003957 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 udelay(5);
3959 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3960
Michael Chan3d3ebe72006-09-27 15:59:15 -07003961 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003962 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3964 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003965 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 mac_status = tr32(MAC_STATUS);
3967
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003968 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003970 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
Matt Carlson82cd3d12007-12-20 20:09:00 -08003972 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3973 local_adv |= ADVERTISE_1000XPAUSE;
3974 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3975 local_adv |= ADVERTISE_1000XPSE_ASYM;
3976
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003977 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003978 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003979 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003980 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
3982 tg3_setup_flow_control(tp, local_adv, remote_adv);
3983 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003984 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003985 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003986 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003987 if (tp->serdes_counter)
3988 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 else {
3990 if (workaround) {
3991 u32 val = serdes_cfg;
3992
3993 if (port_a)
3994 val |= 0xc010000;
3995 else
3996 val |= 0x4010000;
3997
3998 tw32_f(MAC_SERDES_CFG, val);
3999 }
4000
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004001 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 udelay(40);
4003
4004 /* Link parallel detection - link is up */
4005 /* only if we have PCS_SYNC and not */
4006 /* receiving config code words */
4007 mac_status = tr32(MAC_STATUS);
4008 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4009 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4010 tg3_setup_flow_control(tp, 0, 0);
4011 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004012 tp->phy_flags |=
4013 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004014 tp->serdes_counter =
4015 SERDES_PARALLEL_DET_TIMEOUT;
4016 } else
4017 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 }
4019 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004020 } else {
4021 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004022 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 }
4024
4025out:
4026 return current_link_up;
4027}
4028
4029static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4030{
4031 int current_link_up = 0;
4032
Michael Chan5cf64b82007-05-05 12:11:21 -07004033 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
4036 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004037 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004039
Matt Carlson5be73b42007-12-20 20:09:29 -08004040 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4041 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Matt Carlson5be73b42007-12-20 20:09:29 -08004043 if (txflags & ANEG_CFG_PS1)
4044 local_adv |= ADVERTISE_1000XPAUSE;
4045 if (txflags & ANEG_CFG_PS2)
4046 local_adv |= ADVERTISE_1000XPSE_ASYM;
4047
4048 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4049 remote_adv |= LPA_1000XPAUSE;
4050 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4051 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
4053 tg3_setup_flow_control(tp, local_adv, remote_adv);
4054
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 current_link_up = 1;
4056 }
4057 for (i = 0; i < 30; i++) {
4058 udelay(20);
4059 tw32_f(MAC_STATUS,
4060 (MAC_STATUS_SYNC_CHANGED |
4061 MAC_STATUS_CFG_CHANGED));
4062 udelay(40);
4063 if ((tr32(MAC_STATUS) &
4064 (MAC_STATUS_SYNC_CHANGED |
4065 MAC_STATUS_CFG_CHANGED)) == 0)
4066 break;
4067 }
4068
4069 mac_status = tr32(MAC_STATUS);
4070 if (current_link_up == 0 &&
4071 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4072 !(mac_status & MAC_STATUS_RCVD_CFG))
4073 current_link_up = 1;
4074 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004075 tg3_setup_flow_control(tp, 0, 0);
4076
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 /* Forcing 1000FD link up. */
4078 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
4080 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4081 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004082
4083 tw32_f(MAC_MODE, tp->mac_mode);
4084 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 }
4086
4087out:
4088 return current_link_up;
4089}
4090
4091static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4092{
4093 u32 orig_pause_cfg;
4094 u16 orig_active_speed;
4095 u8 orig_active_duplex;
4096 u32 mac_status;
4097 int current_link_up;
4098 int i;
4099
Matt Carlson8d018622007-12-20 20:05:44 -08004100 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 orig_active_speed = tp->link_config.active_speed;
4102 orig_active_duplex = tp->link_config.active_duplex;
4103
Joe Perches63c3a662011-04-26 08:12:10 +00004104 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004106 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 mac_status = tr32(MAC_STATUS);
4108 mac_status &= (MAC_STATUS_PCS_SYNCED |
4109 MAC_STATUS_SIGNAL_DET |
4110 MAC_STATUS_CFG_CHANGED |
4111 MAC_STATUS_RCVD_CFG);
4112 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4113 MAC_STATUS_SIGNAL_DET)) {
4114 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4115 MAC_STATUS_CFG_CHANGED));
4116 return 0;
4117 }
4118 }
4119
4120 tw32_f(MAC_TX_AUTO_NEG, 0);
4121
4122 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4123 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4124 tw32_f(MAC_MODE, tp->mac_mode);
4125 udelay(40);
4126
Matt Carlson79eb6902010-02-17 15:17:03 +00004127 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 tg3_init_bcm8002(tp);
4129
4130 /* Enable link change event even when serdes polling. */
4131 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4132 udelay(40);
4133
4134 current_link_up = 0;
4135 mac_status = tr32(MAC_STATUS);
4136
Joe Perches63c3a662011-04-26 08:12:10 +00004137 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4139 else
4140 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4141
Matt Carlson898a56f2009-08-28 14:02:40 +00004142 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004144 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
4146 for (i = 0; i < 100; i++) {
4147 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4148 MAC_STATUS_CFG_CHANGED));
4149 udelay(5);
4150 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004151 MAC_STATUS_CFG_CHANGED |
4152 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 break;
4154 }
4155
4156 mac_status = tr32(MAC_STATUS);
4157 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4158 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004159 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4160 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 tw32_f(MAC_MODE, (tp->mac_mode |
4162 MAC_MODE_SEND_CONFIGS));
4163 udelay(1);
4164 tw32_f(MAC_MODE, tp->mac_mode);
4165 }
4166 }
4167
4168 if (current_link_up == 1) {
4169 tp->link_config.active_speed = SPEED_1000;
4170 tp->link_config.active_duplex = DUPLEX_FULL;
4171 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4172 LED_CTRL_LNKLED_OVERRIDE |
4173 LED_CTRL_1000MBPS_ON));
4174 } else {
4175 tp->link_config.active_speed = SPEED_INVALID;
4176 tp->link_config.active_duplex = DUPLEX_INVALID;
4177 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4178 LED_CTRL_LNKLED_OVERRIDE |
4179 LED_CTRL_TRAFFIC_OVERRIDE));
4180 }
4181
4182 if (current_link_up != netif_carrier_ok(tp->dev)) {
4183 if (current_link_up)
4184 netif_carrier_on(tp->dev);
4185 else
4186 netif_carrier_off(tp->dev);
4187 tg3_link_report(tp);
4188 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004189 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 if (orig_pause_cfg != now_pause_cfg ||
4191 orig_active_speed != tp->link_config.active_speed ||
4192 orig_active_duplex != tp->link_config.active_duplex)
4193 tg3_link_report(tp);
4194 }
4195
4196 return 0;
4197}
4198
Michael Chan747e8f82005-07-25 12:33:22 -07004199static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4200{
4201 int current_link_up, err = 0;
4202 u32 bmsr, bmcr;
4203 u16 current_speed;
4204 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004205 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004206
4207 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4208 tw32_f(MAC_MODE, tp->mac_mode);
4209 udelay(40);
4210
4211 tw32(MAC_EVENT, 0);
4212
4213 tw32_f(MAC_STATUS,
4214 (MAC_STATUS_SYNC_CHANGED |
4215 MAC_STATUS_CFG_CHANGED |
4216 MAC_STATUS_MI_COMPLETION |
4217 MAC_STATUS_LNKSTATE_CHANGED));
4218 udelay(40);
4219
4220 if (force_reset)
4221 tg3_phy_reset(tp);
4222
4223 current_link_up = 0;
4224 current_speed = SPEED_INVALID;
4225 current_duplex = DUPLEX_INVALID;
4226
4227 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4228 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004229 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4230 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4231 bmsr |= BMSR_LSTATUS;
4232 else
4233 bmsr &= ~BMSR_LSTATUS;
4234 }
Michael Chan747e8f82005-07-25 12:33:22 -07004235
4236 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4237
4238 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004239 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004240 /* do nothing, just check for link up at the end */
4241 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4242 u32 adv, new_adv;
4243
4244 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4245 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4246 ADVERTISE_1000XPAUSE |
4247 ADVERTISE_1000XPSE_ASYM |
4248 ADVERTISE_SLCT);
4249
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004250 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004251
4252 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4253 new_adv |= ADVERTISE_1000XHALF;
4254 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4255 new_adv |= ADVERTISE_1000XFULL;
4256
4257 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4258 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4259 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4260 tg3_writephy(tp, MII_BMCR, bmcr);
4261
4262 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004263 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004264 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004265
4266 return err;
4267 }
4268 } else {
4269 u32 new_bmcr;
4270
4271 bmcr &= ~BMCR_SPEED1000;
4272 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4273
4274 if (tp->link_config.duplex == DUPLEX_FULL)
4275 new_bmcr |= BMCR_FULLDPLX;
4276
4277 if (new_bmcr != bmcr) {
4278 /* BMCR_SPEED1000 is a reserved bit that needs
4279 * to be set on write.
4280 */
4281 new_bmcr |= BMCR_SPEED1000;
4282
4283 /* Force a linkdown */
4284 if (netif_carrier_ok(tp->dev)) {
4285 u32 adv;
4286
4287 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4288 adv &= ~(ADVERTISE_1000XFULL |
4289 ADVERTISE_1000XHALF |
4290 ADVERTISE_SLCT);
4291 tg3_writephy(tp, MII_ADVERTISE, adv);
4292 tg3_writephy(tp, MII_BMCR, bmcr |
4293 BMCR_ANRESTART |
4294 BMCR_ANENABLE);
4295 udelay(10);
4296 netif_carrier_off(tp->dev);
4297 }
4298 tg3_writephy(tp, MII_BMCR, new_bmcr);
4299 bmcr = new_bmcr;
4300 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4301 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004302 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4303 ASIC_REV_5714) {
4304 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4305 bmsr |= BMSR_LSTATUS;
4306 else
4307 bmsr &= ~BMSR_LSTATUS;
4308 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004309 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004310 }
4311 }
4312
4313 if (bmsr & BMSR_LSTATUS) {
4314 current_speed = SPEED_1000;
4315 current_link_up = 1;
4316 if (bmcr & BMCR_FULLDPLX)
4317 current_duplex = DUPLEX_FULL;
4318 else
4319 current_duplex = DUPLEX_HALF;
4320
Matt Carlsonef167e22007-12-20 20:10:01 -08004321 local_adv = 0;
4322 remote_adv = 0;
4323
Michael Chan747e8f82005-07-25 12:33:22 -07004324 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004325 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004326
4327 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4328 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4329 common = local_adv & remote_adv;
4330 if (common & (ADVERTISE_1000XHALF |
4331 ADVERTISE_1000XFULL)) {
4332 if (common & ADVERTISE_1000XFULL)
4333 current_duplex = DUPLEX_FULL;
4334 else
4335 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004336 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004337 /* Link is up via parallel detect */
Matt Carlson859a5882010-04-05 10:19:28 +00004338 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004339 current_link_up = 0;
Matt Carlson859a5882010-04-05 10:19:28 +00004340 }
Michael Chan747e8f82005-07-25 12:33:22 -07004341 }
4342 }
4343
Matt Carlsonef167e22007-12-20 20:10:01 -08004344 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4345 tg3_setup_flow_control(tp, local_adv, remote_adv);
4346
Michael Chan747e8f82005-07-25 12:33:22 -07004347 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4348 if (tp->link_config.active_duplex == DUPLEX_HALF)
4349 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4350
4351 tw32_f(MAC_MODE, tp->mac_mode);
4352 udelay(40);
4353
4354 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4355
4356 tp->link_config.active_speed = current_speed;
4357 tp->link_config.active_duplex = current_duplex;
4358
4359 if (current_link_up != netif_carrier_ok(tp->dev)) {
4360 if (current_link_up)
4361 netif_carrier_on(tp->dev);
4362 else {
4363 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004364 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004365 }
4366 tg3_link_report(tp);
4367 }
4368 return err;
4369}
4370
4371static void tg3_serdes_parallel_detect(struct tg3 *tp)
4372{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004373 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004374 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004375 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004376 return;
4377 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004378
Michael Chan747e8f82005-07-25 12:33:22 -07004379 if (!netif_carrier_ok(tp->dev) &&
4380 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4381 u32 bmcr;
4382
4383 tg3_readphy(tp, MII_BMCR, &bmcr);
4384 if (bmcr & BMCR_ANENABLE) {
4385 u32 phy1, phy2;
4386
4387 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004388 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4389 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004390
4391 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004392 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4393 MII_TG3_DSP_EXP1_INT_STAT);
4394 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4395 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004396
4397 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4398 /* We have signal detect and not receiving
4399 * config code words, link is up by parallel
4400 * detection.
4401 */
4402
4403 bmcr &= ~BMCR_ANENABLE;
4404 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4405 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004406 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004407 }
4408 }
Matt Carlson859a5882010-04-05 10:19:28 +00004409 } else if (netif_carrier_ok(tp->dev) &&
4410 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004411 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004412 u32 phy2;
4413
4414 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004415 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4416 MII_TG3_DSP_EXP1_INT_STAT);
4417 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004418 if (phy2 & 0x20) {
4419 u32 bmcr;
4420
4421 /* Config code words received, turn on autoneg. */
4422 tg3_readphy(tp, MII_BMCR, &bmcr);
4423 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4424
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004425 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004426
4427 }
4428 }
4429}
4430
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4432{
Matt Carlsonf2096f92011-04-05 14:22:48 +00004433 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 int err;
4435
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004436 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004438 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004439 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a5882010-04-05 10:19:28 +00004440 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004443 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004444 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004445
4446 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4447 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4448 scale = 65;
4449 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4450 scale = 6;
4451 else
4452 scale = 12;
4453
4454 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4455 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4456 tw32(GRC_MISC_CFG, val);
4457 }
4458
Matt Carlsonf2096f92011-04-05 14:22:48 +00004459 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4460 (6 << TX_LENGTHS_IPG_SHIFT);
4461 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4462 val |= tr32(MAC_TX_LENGTHS) &
4463 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4464 TX_LENGTHS_CNT_DWN_VAL_MSK);
4465
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 if (tp->link_config.active_speed == SPEED_1000 &&
4467 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00004468 tw32(MAC_TX_LENGTHS, val |
4469 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00004471 tw32(MAC_TX_LENGTHS, val |
4472 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
Joe Perches63c3a662011-04-26 08:12:10 +00004474 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 if (netif_carrier_ok(tp->dev)) {
4476 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004477 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 } else {
4479 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4480 }
4481 }
4482
Joe Perches63c3a662011-04-26 08:12:10 +00004483 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004484 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07004485 if (!netif_carrier_ok(tp->dev))
4486 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4487 tp->pwrmgmt_thresh;
4488 else
4489 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4490 tw32(PCIE_PWR_MGMT_THRESH, val);
4491 }
4492
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 return err;
4494}
4495
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004496static inline int tg3_irq_sync(struct tg3 *tp)
4497{
4498 return tp->irq_sync;
4499}
4500
Matt Carlson97bd8e42011-04-13 11:05:04 +00004501static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4502{
4503 int i;
4504
4505 dst = (u32 *)((u8 *)dst + off);
4506 for (i = 0; i < len; i += sizeof(u32))
4507 *dst++ = tr32(off + i);
4508}
4509
4510static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4511{
4512 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4513 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4514 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4515 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4516 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4517 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4518 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4519 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4520 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4521 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4522 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4523 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4524 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4525 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4526 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4527 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4528 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4529 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4530 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4531
Joe Perches63c3a662011-04-26 08:12:10 +00004532 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004533 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4534
4535 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4536 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4537 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4538 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4539 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4540 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4541 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4542 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4543
Joe Perches63c3a662011-04-26 08:12:10 +00004544 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004545 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4546 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4547 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4548 }
4549
4550 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4551 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4552 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4553 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4554 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4555
Joe Perches63c3a662011-04-26 08:12:10 +00004556 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004557 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4558}
4559
4560static void tg3_dump_state(struct tg3 *tp)
4561{
4562 int i;
4563 u32 *regs;
4564
4565 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4566 if (!regs) {
4567 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4568 return;
4569 }
4570
Joe Perches63c3a662011-04-26 08:12:10 +00004571 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004572 /* Read up to but not including private PCI registers */
4573 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4574 regs[i / sizeof(u32)] = tr32(i);
4575 } else
4576 tg3_dump_legacy_regs(tp, regs);
4577
4578 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4579 if (!regs[i + 0] && !regs[i + 1] &&
4580 !regs[i + 2] && !regs[i + 3])
4581 continue;
4582
4583 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4584 i * 4,
4585 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4586 }
4587
4588 kfree(regs);
4589
4590 for (i = 0; i < tp->irq_cnt; i++) {
4591 struct tg3_napi *tnapi = &tp->napi[i];
4592
4593 /* SW status block */
4594 netdev_err(tp->dev,
4595 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4596 i,
4597 tnapi->hw_status->status,
4598 tnapi->hw_status->status_tag,
4599 tnapi->hw_status->rx_jumbo_consumer,
4600 tnapi->hw_status->rx_consumer,
4601 tnapi->hw_status->rx_mini_consumer,
4602 tnapi->hw_status->idx[0].rx_producer,
4603 tnapi->hw_status->idx[0].tx_consumer);
4604
4605 netdev_err(tp->dev,
4606 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4607 i,
4608 tnapi->last_tag, tnapi->last_irq_tag,
4609 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4610 tnapi->rx_rcb_ptr,
4611 tnapi->prodring.rx_std_prod_idx,
4612 tnapi->prodring.rx_std_cons_idx,
4613 tnapi->prodring.rx_jmb_prod_idx,
4614 tnapi->prodring.rx_jmb_cons_idx);
4615 }
4616}
4617
Michael Chandf3e6542006-05-26 17:48:07 -07004618/* This is called whenever we suspect that the system chipset is re-
4619 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4620 * is bogus tx completions. We try to recover by setting the
4621 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4622 * in the workqueue.
4623 */
4624static void tg3_tx_recover(struct tg3 *tp)
4625{
Joe Perches63c3a662011-04-26 08:12:10 +00004626 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07004627 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4628
Matt Carlson5129c3a2010-04-05 10:19:23 +00004629 netdev_warn(tp->dev,
4630 "The system may be re-ordering memory-mapped I/O "
4631 "cycles to the network device, attempting to recover. "
4632 "Please report the problem to the driver maintainer "
4633 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004634
4635 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00004636 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07004637 spin_unlock(&tp->lock);
4638}
4639
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004640static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004641{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004642 /* Tell compiler to fetch tx indices from memory. */
4643 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004644 return tnapi->tx_pending -
4645 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004646}
4647
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648/* Tigon3 never reports partial packet sends. So we do not
4649 * need special logic to handle SKBs that have not had all
4650 * of their frags sent yet, like SunGEM does.
4651 */
Matt Carlson17375d22009-08-28 14:02:18 +00004652static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653{
Matt Carlson17375d22009-08-28 14:02:18 +00004654 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004655 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004656 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004657 struct netdev_queue *txq;
4658 int index = tnapi - tp->napi;
4659
Joe Perches63c3a662011-04-26 08:12:10 +00004660 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004661 index--;
4662
4663 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
4665 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004666 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004668 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
Michael Chandf3e6542006-05-26 17:48:07 -07004670 if (unlikely(skb == NULL)) {
4671 tg3_tx_recover(tp);
4672 return;
4673 }
4674
Alexander Duyckf4188d82009-12-02 16:48:38 +00004675 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004676 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004677 skb_headlen(skb),
4678 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679
4680 ri->skb = NULL;
4681
4682 sw_idx = NEXT_TX(sw_idx);
4683
4684 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004685 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004686 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4687 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004688
4689 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004690 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004691 skb_shinfo(skb)->frags[i].size,
4692 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 sw_idx = NEXT_TX(sw_idx);
4694 }
4695
David S. Millerf47c11e2005-06-24 20:18:35 -07004696 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004697
4698 if (unlikely(tx_bug)) {
4699 tg3_tx_recover(tp);
4700 return;
4701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 }
4703
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004704 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
Michael Chan1b2a7202006-08-07 21:46:02 -07004706 /* Need to make the tx_cons update visible to tg3_start_xmit()
4707 * before checking for netif_queue_stopped(). Without the
4708 * memory barrier, there is a small possibility that tg3_start_xmit()
4709 * will miss it and cause the queue to be stopped forever.
4710 */
4711 smp_mb();
4712
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004713 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004714 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004715 __netif_tx_lock(txq, smp_processor_id());
4716 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004717 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004718 netif_tx_wake_queue(txq);
4719 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721}
4722
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004723static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4724{
4725 if (!ri->skb)
4726 return;
4727
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004728 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004729 map_sz, PCI_DMA_FROMDEVICE);
4730 dev_kfree_skb_any(ri->skb);
4731 ri->skb = NULL;
4732}
4733
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734/* Returns size of skb allocated or < 0 on error.
4735 *
4736 * We only need to fill in the address because the other members
4737 * of the RX descriptor are invariant, see tg3_init_rings.
4738 *
4739 * Note the purposeful assymetry of cpu vs. chip accesses. For
4740 * posting buffers we only dirty the first cache line of the RX
4741 * descriptor (containing the address). Whereas for the RX status
4742 * buffers the cpu only reads the last cacheline of the RX descriptor
4743 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4744 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004745static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004746 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747{
4748 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004749 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 struct sk_buff *skb;
4751 dma_addr_t mapping;
4752 int skb_size, dest_idx;
4753
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 switch (opaque_key) {
4755 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004756 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004757 desc = &tpr->rx_std[dest_idx];
4758 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004759 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 break;
4761
4762 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004763 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004764 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004765 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004766 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 break;
4768
4769 default:
4770 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
4773 /* Do not overwrite any of the map or rp information
4774 * until we are sure we can commit to a new buffer.
4775 *
4776 * Callers depend upon this behavior and assume that
4777 * we leave everything unchanged if we fail.
4778 */
Matt Carlson287be122009-08-28 13:58:46 +00004779 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 if (skb == NULL)
4781 return -ENOMEM;
4782
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 skb_reserve(skb, tp->rx_offset);
4784
Matt Carlson287be122009-08-28 13:58:46 +00004785 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004787 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4788 dev_kfree_skb(skb);
4789 return -EIO;
4790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
4792 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004793 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 desc->addr_hi = ((u64)mapping >> 32);
4796 desc->addr_lo = ((u64)mapping & 0xffffffff);
4797
4798 return skb_size;
4799}
4800
4801/* We only need to move over in the address because the other
4802 * members of the RX descriptor are invariant. See notes above
4803 * tg3_alloc_rx_skb for full details.
4804 */
Matt Carlsona3896162009-11-13 13:03:44 +00004805static void tg3_recycle_rx(struct tg3_napi *tnapi,
4806 struct tg3_rx_prodring_set *dpr,
4807 u32 opaque_key, int src_idx,
4808 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809{
Matt Carlson17375d22009-08-28 14:02:18 +00004810 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4812 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004813 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004814 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
4816 switch (opaque_key) {
4817 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004818 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004819 dest_desc = &dpr->rx_std[dest_idx];
4820 dest_map = &dpr->rx_std_buffers[dest_idx];
4821 src_desc = &spr->rx_std[src_idx];
4822 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 break;
4824
4825 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004826 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004827 dest_desc = &dpr->rx_jmb[dest_idx].std;
4828 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4829 src_desc = &spr->rx_jmb[src_idx].std;
4830 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 break;
4832
4833 default:
4834 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
4837 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004838 dma_unmap_addr_set(dest_map, mapping,
4839 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 dest_desc->addr_hi = src_desc->addr_hi;
4841 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004842
4843 /* Ensure that the update to the skb happens after the physical
4844 * addresses have been transferred to the new BD location.
4845 */
4846 smp_wmb();
4847
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 src_map->skb = NULL;
4849}
4850
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851/* The RX ring scheme is composed of multiple rings which post fresh
4852 * buffers to the chip, and one special ring the chip uses to report
4853 * status back to the host.
4854 *
4855 * The special ring reports the status of received packets to the
4856 * host. The chip does not write into the original descriptor the
4857 * RX buffer was obtained from. The chip simply takes the original
4858 * descriptor as provided by the host, updates the status and length
4859 * field, then writes this into the next status ring entry.
4860 *
4861 * Each ring the host uses to post buffers to the chip is described
4862 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4863 * it is first placed into the on-chip ram. When the packet's length
4864 * is known, it walks down the TG3_BDINFO entries to select the ring.
4865 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4866 * which is within the range of the new packet's length is chosen.
4867 *
4868 * The "separate ring for rx status" scheme may sound queer, but it makes
4869 * sense from a cache coherency perspective. If only the host writes
4870 * to the buffer post rings, and only the chip writes to the rx status
4871 * rings, then cache lines never move beyond shared-modified state.
4872 * If both the host and chip were to write into the same ring, cache line
4873 * eviction could occur since both entities want it in an exclusive state.
4874 */
Matt Carlson17375d22009-08-28 14:02:18 +00004875static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876{
Matt Carlson17375d22009-08-28 14:02:18 +00004877 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004878 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004879 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004880 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004881 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004883 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004885 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 /*
4887 * We need to order the read of hw_idx and the read of
4888 * the opaque cookie.
4889 */
4890 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 work_mask = 0;
4892 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004893 std_prod_idx = tpr->rx_std_prod_idx;
4894 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004896 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004897 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 unsigned int len;
4899 struct sk_buff *skb;
4900 dma_addr_t dma_addr;
4901 u32 opaque_key, desc_idx, *post_ptr;
4902
4903 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4904 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4905 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004906 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004907 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004908 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004909 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004910 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004912 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004913 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004914 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004915 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004916 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918
4919 work_mask |= opaque_key;
4920
4921 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4922 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4923 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004924 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 desc_idx, *post_ptr);
4926 drop_it_no_recycle:
4927 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00004928 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 goto next_pkt;
4930 }
4931
Matt Carlsonad829262008-11-21 17:16:16 -08004932 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4933 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934
Matt Carlsond2757fc2010-04-12 06:58:27 +00004935 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 int skb_size;
4937
Matt Carlson86b21e52009-11-13 13:03:45 +00004938 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004939 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 if (skb_size < 0)
4941 goto drop_it;
4942
Matt Carlson287be122009-08-28 13:58:46 +00004943 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 PCI_DMA_FROMDEVICE);
4945
Matt Carlson61e800c2010-02-17 15:16:54 +00004946 /* Ensure that the update to the skb happens
4947 * after the usage of the old DMA mapping.
4948 */
4949 smp_wmb();
4950
4951 ri->skb = NULL;
4952
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 skb_put(skb, len);
4954 } else {
4955 struct sk_buff *copy_skb;
4956
Matt Carlsona3896162009-11-13 13:03:44 +00004957 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 desc_idx, *post_ptr);
4959
Matt Carlsonbf933c82011-01-25 15:58:49 +00004960 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00004961 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 if (copy_skb == NULL)
4963 goto drop_it_no_recycle;
4964
Matt Carlsonbf933c82011-01-25 15:58:49 +00004965 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 skb_put(copy_skb, len);
4967 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004968 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4970
4971 /* We'll reuse the original ring buffer. */
4972 skb = copy_skb;
4973 }
4974
Michał Mirosławdc668912011-04-07 03:35:07 +00004975 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4977 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4978 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4979 skb->ip_summed = CHECKSUM_UNNECESSARY;
4980 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004981 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
4983 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004984
4985 if (len > (tp->dev->mtu + ETH_HLEN) &&
4986 skb->protocol != htons(ETH_P_8021Q)) {
4987 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00004988 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004989 }
4990
Matt Carlson9dc7a112010-04-12 06:58:28 +00004991 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00004992 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
4993 __vlan_hwaccel_put_tag(skb,
4994 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00004995
Matt Carlsonbf933c82011-01-25 15:58:49 +00004996 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 received++;
4999 budget--;
5000
5001next_pkt:
5002 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005003
5004 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005005 tpr->rx_std_prod_idx = std_prod_idx &
5006 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005007 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5008 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005009 work_mask &= ~RXD_OPAQUE_RING_STD;
5010 rx_std_posted = 0;
5011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005013 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005014 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005015
5016 /* Refresh hw_idx to see if there is new work */
5017 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005018 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005019 rmb();
5020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 }
5022
5023 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005024 tnapi->rx_rcb_ptr = sw_idx;
5025 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
5027 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005028 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005029 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005030 tpr->rx_std_prod_idx = std_prod_idx &
5031 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005032 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5033 tpr->rx_std_prod_idx);
5034 }
5035 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005036 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5037 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005038 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5039 tpr->rx_jmb_prod_idx);
5040 }
5041 mmiowb();
5042 } else if (work_mask) {
5043 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5044 * updated before the producer indices can be updated.
5045 */
5046 smp_wmb();
5047
Matt Carlson2c49a442010-09-30 10:34:35 +00005048 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5049 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005050
Matt Carlsone4af1af2010-02-12 14:47:05 +00005051 if (tnapi != &tp->napi[1])
5052 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054
5055 return received;
5056}
5057
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005058static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005061 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005062 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5063
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 if (sblk->status & SD_STATUS_LINK_CHG) {
5065 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005066 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005067 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005068 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005069 tw32_f(MAC_STATUS,
5070 (MAC_STATUS_SYNC_CHANGED |
5071 MAC_STATUS_CFG_CHANGED |
5072 MAC_STATUS_MI_COMPLETION |
5073 MAC_STATUS_LNKSTATE_CHANGED));
5074 udelay(40);
5075 } else
5076 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005077 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 }
5079 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005080}
5081
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005082static int tg3_rx_prodring_xfer(struct tg3 *tp,
5083 struct tg3_rx_prodring_set *dpr,
5084 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005085{
5086 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005087 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005088
5089 while (1) {
5090 src_prod_idx = spr->rx_std_prod_idx;
5091
5092 /* Make sure updates to the rx_std_buffers[] entries and the
5093 * standard producer index are seen in the correct order.
5094 */
5095 smp_rmb();
5096
5097 if (spr->rx_std_cons_idx == src_prod_idx)
5098 break;
5099
5100 if (spr->rx_std_cons_idx < src_prod_idx)
5101 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5102 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005103 cpycnt = tp->rx_std_ring_mask + 1 -
5104 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005105
Matt Carlson2c49a442010-09-30 10:34:35 +00005106 cpycnt = min(cpycnt,
5107 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005108
5109 si = spr->rx_std_cons_idx;
5110 di = dpr->rx_std_prod_idx;
5111
Matt Carlsone92967b2010-02-12 14:47:06 +00005112 for (i = di; i < di + cpycnt; i++) {
5113 if (dpr->rx_std_buffers[i].skb) {
5114 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005115 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005116 break;
5117 }
5118 }
5119
5120 if (!cpycnt)
5121 break;
5122
5123 /* Ensure that updates to the rx_std_buffers ring and the
5124 * shadowed hardware producer ring from tg3_recycle_skb() are
5125 * ordered correctly WRT the skb check above.
5126 */
5127 smp_rmb();
5128
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005129 memcpy(&dpr->rx_std_buffers[di],
5130 &spr->rx_std_buffers[si],
5131 cpycnt * sizeof(struct ring_info));
5132
5133 for (i = 0; i < cpycnt; i++, di++, si++) {
5134 struct tg3_rx_buffer_desc *sbd, *dbd;
5135 sbd = &spr->rx_std[si];
5136 dbd = &dpr->rx_std[di];
5137 dbd->addr_hi = sbd->addr_hi;
5138 dbd->addr_lo = sbd->addr_lo;
5139 }
5140
Matt Carlson2c49a442010-09-30 10:34:35 +00005141 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5142 tp->rx_std_ring_mask;
5143 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5144 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005145 }
5146
5147 while (1) {
5148 src_prod_idx = spr->rx_jmb_prod_idx;
5149
5150 /* Make sure updates to the rx_jmb_buffers[] entries and
5151 * the jumbo producer index are seen in the correct order.
5152 */
5153 smp_rmb();
5154
5155 if (spr->rx_jmb_cons_idx == src_prod_idx)
5156 break;
5157
5158 if (spr->rx_jmb_cons_idx < src_prod_idx)
5159 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5160 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005161 cpycnt = tp->rx_jmb_ring_mask + 1 -
5162 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005163
5164 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005165 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005166
5167 si = spr->rx_jmb_cons_idx;
5168 di = dpr->rx_jmb_prod_idx;
5169
Matt Carlsone92967b2010-02-12 14:47:06 +00005170 for (i = di; i < di + cpycnt; i++) {
5171 if (dpr->rx_jmb_buffers[i].skb) {
5172 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005173 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005174 break;
5175 }
5176 }
5177
5178 if (!cpycnt)
5179 break;
5180
5181 /* Ensure that updates to the rx_jmb_buffers ring and the
5182 * shadowed hardware producer ring from tg3_recycle_skb() are
5183 * ordered correctly WRT the skb check above.
5184 */
5185 smp_rmb();
5186
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005187 memcpy(&dpr->rx_jmb_buffers[di],
5188 &spr->rx_jmb_buffers[si],
5189 cpycnt * sizeof(struct ring_info));
5190
5191 for (i = 0; i < cpycnt; i++, di++, si++) {
5192 struct tg3_rx_buffer_desc *sbd, *dbd;
5193 sbd = &spr->rx_jmb[si].std;
5194 dbd = &dpr->rx_jmb[di].std;
5195 dbd->addr_hi = sbd->addr_hi;
5196 dbd->addr_lo = sbd->addr_lo;
5197 }
5198
Matt Carlson2c49a442010-09-30 10:34:35 +00005199 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5200 tp->rx_jmb_ring_mask;
5201 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5202 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005203 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005204
5205 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005206}
5207
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005208static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5209{
5210 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211
5212 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005213 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005214 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005215 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005216 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 }
5218
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 /* run RX thread, within the bounds set by NAPI.
5220 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005221 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005223 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005224 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
Joe Perches63c3a662011-04-26 08:12:10 +00005226 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005227 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005228 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005229 u32 std_prod_idx = dpr->rx_std_prod_idx;
5230 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005231
Matt Carlsone4af1af2010-02-12 14:47:05 +00005232 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005233 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005234 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005235
5236 wmb();
5237
Matt Carlsone4af1af2010-02-12 14:47:05 +00005238 if (std_prod_idx != dpr->rx_std_prod_idx)
5239 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5240 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005241
Matt Carlsone4af1af2010-02-12 14:47:05 +00005242 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5243 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5244 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005245
5246 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005247
5248 if (err)
5249 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005250 }
5251
David S. Miller6f535762007-10-11 18:08:29 -07005252 return work_done;
5253}
David S. Millerf7383c22005-05-18 22:50:53 -07005254
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005255static int tg3_poll_msix(struct napi_struct *napi, int budget)
5256{
5257 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5258 struct tg3 *tp = tnapi->tp;
5259 int work_done = 0;
5260 struct tg3_hw_status *sblk = tnapi->hw_status;
5261
5262 while (1) {
5263 work_done = tg3_poll_work(tnapi, work_done, budget);
5264
Joe Perches63c3a662011-04-26 08:12:10 +00005265 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005266 goto tx_recovery;
5267
5268 if (unlikely(work_done >= budget))
5269 break;
5270
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005271 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005272 * to tell the hw how much work has been processed,
5273 * so we must read it before checking for more work.
5274 */
5275 tnapi->last_tag = sblk->status_tag;
5276 tnapi->last_irq_tag = tnapi->last_tag;
5277 rmb();
5278
5279 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005280 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5281 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005282 napi_complete(napi);
5283 /* Reenable interrupts. */
5284 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5285 mmiowb();
5286 break;
5287 }
5288 }
5289
5290 return work_done;
5291
5292tx_recovery:
5293 /* work_done is guaranteed to be less than budget. */
5294 napi_complete(napi);
5295 schedule_work(&tp->reset_task);
5296 return work_done;
5297}
5298
Matt Carlsone64de4e2011-04-13 11:05:05 +00005299static void tg3_process_error(struct tg3 *tp)
5300{
5301 u32 val;
5302 bool real_error = false;
5303
Joe Perches63c3a662011-04-26 08:12:10 +00005304 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005305 return;
5306
5307 /* Check Flow Attention register */
5308 val = tr32(HOSTCC_FLOW_ATTN);
5309 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5310 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5311 real_error = true;
5312 }
5313
5314 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5315 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5316 real_error = true;
5317 }
5318
5319 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5320 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5321 real_error = true;
5322 }
5323
5324 if (!real_error)
5325 return;
5326
5327 tg3_dump_state(tp);
5328
Joe Perches63c3a662011-04-26 08:12:10 +00005329 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsone64de4e2011-04-13 11:05:05 +00005330 schedule_work(&tp->reset_task);
5331}
5332
David S. Miller6f535762007-10-11 18:08:29 -07005333static int tg3_poll(struct napi_struct *napi, int budget)
5334{
Matt Carlson8ef04422009-08-28 14:01:37 +00005335 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5336 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005337 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005338 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005339
5340 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00005341 if (sblk->status & SD_STATUS_ERROR)
5342 tg3_process_error(tp);
5343
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005344 tg3_poll_link(tp);
5345
Matt Carlson17375d22009-08-28 14:02:18 +00005346 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005347
Joe Perches63c3a662011-04-26 08:12:10 +00005348 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07005349 goto tx_recovery;
5350
5351 if (unlikely(work_done >= budget))
5352 break;
5353
Joe Perches63c3a662011-04-26 08:12:10 +00005354 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00005355 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005356 * to tell the hw how much work has been processed,
5357 * so we must read it before checking for more work.
5358 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005359 tnapi->last_tag = sblk->status_tag;
5360 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005361 rmb();
5362 } else
5363 sblk->status &= ~SD_STATUS_UPDATED;
5364
Matt Carlson17375d22009-08-28 14:02:18 +00005365 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005366 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005367 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005368 break;
5369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 }
5371
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005372 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005373
5374tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005375 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005376 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005377 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005378 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379}
5380
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005381static void tg3_napi_disable(struct tg3 *tp)
5382{
5383 int i;
5384
5385 for (i = tp->irq_cnt - 1; i >= 0; i--)
5386 napi_disable(&tp->napi[i].napi);
5387}
5388
5389static void tg3_napi_enable(struct tg3 *tp)
5390{
5391 int i;
5392
5393 for (i = 0; i < tp->irq_cnt; i++)
5394 napi_enable(&tp->napi[i].napi);
5395}
5396
5397static void tg3_napi_init(struct tg3 *tp)
5398{
5399 int i;
5400
5401 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5402 for (i = 1; i < tp->irq_cnt; i++)
5403 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5404}
5405
5406static void tg3_napi_fini(struct tg3 *tp)
5407{
5408 int i;
5409
5410 for (i = 0; i < tp->irq_cnt; i++)
5411 netif_napi_del(&tp->napi[i].napi);
5412}
5413
5414static inline void tg3_netif_stop(struct tg3 *tp)
5415{
5416 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5417 tg3_napi_disable(tp);
5418 netif_tx_disable(tp->dev);
5419}
5420
5421static inline void tg3_netif_start(struct tg3 *tp)
5422{
5423 /* NOTE: unconditional netif_tx_wake_all_queues is only
5424 * appropriate so long as all callers are assured to
5425 * have free tx slots (such as after tg3_init_hw)
5426 */
5427 netif_tx_wake_all_queues(tp->dev);
5428
5429 tg3_napi_enable(tp);
5430 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5431 tg3_enable_ints(tp);
5432}
5433
David S. Millerf47c11e2005-06-24 20:18:35 -07005434static void tg3_irq_quiesce(struct tg3 *tp)
5435{
Matt Carlson4f125f42009-09-01 12:55:02 +00005436 int i;
5437
David S. Millerf47c11e2005-06-24 20:18:35 -07005438 BUG_ON(tp->irq_sync);
5439
5440 tp->irq_sync = 1;
5441 smp_mb();
5442
Matt Carlson4f125f42009-09-01 12:55:02 +00005443 for (i = 0; i < tp->irq_cnt; i++)
5444 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005445}
5446
David S. Millerf47c11e2005-06-24 20:18:35 -07005447/* Fully shutdown all tg3 driver activity elsewhere in the system.
5448 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5449 * with as well. Most of the time, this is not necessary except when
5450 * shutting down the device.
5451 */
5452static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5453{
Michael Chan46966542007-07-11 19:47:19 -07005454 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005455 if (irq_sync)
5456 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005457}
5458
5459static inline void tg3_full_unlock(struct tg3 *tp)
5460{
David S. Millerf47c11e2005-06-24 20:18:35 -07005461 spin_unlock_bh(&tp->lock);
5462}
5463
Michael Chanfcfa0a32006-03-20 22:28:41 -08005464/* One-shot MSI handler - Chip automatically disables interrupt
5465 * after sending MSI so driver doesn't have to do it.
5466 */
David Howells7d12e782006-10-05 14:55:46 +01005467static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005468{
Matt Carlson09943a12009-08-28 14:01:57 +00005469 struct tg3_napi *tnapi = dev_id;
5470 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005471
Matt Carlson898a56f2009-08-28 14:02:40 +00005472 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005473 if (tnapi->rx_rcb)
5474 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005475
5476 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005477 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005478
5479 return IRQ_HANDLED;
5480}
5481
Michael Chan88b06bc2005-04-21 17:13:25 -07005482/* MSI ISR - No need to check for interrupt sharing and no need to
5483 * flush status block and interrupt mailbox. PCI ordering rules
5484 * guarantee that MSI will arrive after the status block.
5485 */
David Howells7d12e782006-10-05 14:55:46 +01005486static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc2005-04-21 17:13:25 -07005487{
Matt Carlson09943a12009-08-28 14:01:57 +00005488 struct tg3_napi *tnapi = dev_id;
5489 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc2005-04-21 17:13:25 -07005490
Matt Carlson898a56f2009-08-28 14:02:40 +00005491 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005492 if (tnapi->rx_rcb)
5493 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc2005-04-21 17:13:25 -07005494 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005495 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc2005-04-21 17:13:25 -07005496 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005497 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc2005-04-21 17:13:25 -07005498 * NIC to stop sending us irqs, engaging "in-intr-handler"
5499 * event coalescing.
5500 */
5501 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005502 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005503 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005504
Michael Chan88b06bc2005-04-21 17:13:25 -07005505 return IRQ_RETVAL(1);
5506}
5507
David Howells7d12e782006-10-05 14:55:46 +01005508static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509{
Matt Carlson09943a12009-08-28 14:01:57 +00005510 struct tg3_napi *tnapi = dev_id;
5511 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005512 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 unsigned int handled = 1;
5514
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 /* In INTx mode, it is possible for the interrupt to arrive at
5516 * the CPU before the status block posted prior to the interrupt.
5517 * Reading the PCI State register will confirm whether the
5518 * interrupt is ours and will flush the status block.
5519 */
Michael Chand18edcb2007-03-24 20:57:11 -07005520 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00005521 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005522 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5523 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005524 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005525 }
Michael Chand18edcb2007-03-24 20:57:11 -07005526 }
5527
5528 /*
5529 * Writing any value to intr-mbox-0 clears PCI INTA# and
5530 * chip-internal interrupt pending events.
5531 * Writing non-zero to intr-mbox-0 additional tells the
5532 * NIC to stop sending us irqs, engaging "in-intr-handler"
5533 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005534 *
5535 * Flush the mailbox to de-assert the IRQ immediately to prevent
5536 * spurious interrupts. The flush impacts performance but
5537 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005538 */
Michael Chanc04cb342007-05-07 00:26:15 -07005539 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005540 if (tg3_irq_sync(tp))
5541 goto out;
5542 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005543 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005544 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005545 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005546 } else {
5547 /* No work, shared interrupt perhaps? re-enable
5548 * interrupts, and flush that PCI write
5549 */
5550 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5551 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005552 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005553out:
David S. Millerfac9b832005-05-18 22:46:34 -07005554 return IRQ_RETVAL(handled);
5555}
5556
David Howells7d12e782006-10-05 14:55:46 +01005557static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005558{
Matt Carlson09943a12009-08-28 14:01:57 +00005559 struct tg3_napi *tnapi = dev_id;
5560 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005561 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005562 unsigned int handled = 1;
5563
David S. Millerfac9b832005-05-18 22:46:34 -07005564 /* In INTx mode, it is possible for the interrupt to arrive at
5565 * the CPU before the status block posted prior to the interrupt.
5566 * Reading the PCI State register will confirm whether the
5567 * interrupt is ours and will flush the status block.
5568 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005569 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00005570 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005571 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5572 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005573 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 }
Michael Chand18edcb2007-03-24 20:57:11 -07005575 }
5576
5577 /*
5578 * writing any value to intr-mbox-0 clears PCI INTA# and
5579 * chip-internal interrupt pending events.
5580 * writing non-zero to intr-mbox-0 additional tells the
5581 * NIC to stop sending us irqs, engaging "in-intr-handler"
5582 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005583 *
5584 * Flush the mailbox to de-assert the IRQ immediately to prevent
5585 * spurious interrupts. The flush impacts performance but
5586 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005587 */
Michael Chanc04cb342007-05-07 00:26:15 -07005588 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005589
5590 /*
5591 * In a shared interrupt configuration, sometimes other devices'
5592 * interrupts will scream. We record the current status tag here
5593 * so that the above check can report that the screaming interrupts
5594 * are unhandled. Eventually they will be silenced.
5595 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005596 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005597
Michael Chand18edcb2007-03-24 20:57:11 -07005598 if (tg3_irq_sync(tp))
5599 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005600
Matt Carlson72334482009-08-28 14:03:01 +00005601 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005602
Matt Carlson09943a12009-08-28 14:01:57 +00005603 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005604
David S. Millerf47c11e2005-06-24 20:18:35 -07005605out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 return IRQ_RETVAL(handled);
5607}
5608
Michael Chan79381092005-04-21 17:13:59 -07005609/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005610static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005611{
Matt Carlson09943a12009-08-28 14:01:57 +00005612 struct tg3_napi *tnapi = dev_id;
5613 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005614 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005615
Michael Chanf9804dd2005-09-27 12:13:10 -07005616 if ((sblk->status & SD_STATUS_UPDATED) ||
5617 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005618 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005619 return IRQ_RETVAL(1);
5620 }
5621 return IRQ_RETVAL(0);
5622}
5623
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005624static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005625static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626
Michael Chanb9ec6c12006-07-25 16:37:27 -07005627/* Restart hardware after configuration changes, self-test, etc.
5628 * Invoked with tp->lock held.
5629 */
5630static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005631 __releases(tp->lock)
5632 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005633{
5634 int err;
5635
5636 err = tg3_init_hw(tp, reset_phy);
5637 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005638 netdev_err(tp->dev,
5639 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005640 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5641 tg3_full_unlock(tp);
5642 del_timer_sync(&tp->timer);
5643 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005644 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005645 dev_close(tp->dev);
5646 tg3_full_lock(tp, 0);
5647 }
5648 return err;
5649}
5650
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651#ifdef CONFIG_NET_POLL_CONTROLLER
5652static void tg3_poll_controller(struct net_device *dev)
5653{
Matt Carlson4f125f42009-09-01 12:55:02 +00005654 int i;
Michael Chan88b06bc2005-04-21 17:13:25 -07005655 struct tg3 *tp = netdev_priv(dev);
5656
Matt Carlson4f125f42009-09-01 12:55:02 +00005657 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005658 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659}
5660#endif
5661
David Howellsc4028952006-11-22 14:57:56 +00005662static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663{
David Howellsc4028952006-11-22 14:57:56 +00005664 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005665 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 unsigned int restart_timer;
5667
Michael Chan7faa0062006-02-02 17:29:28 -08005668 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005669
5670 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005671 tg3_full_unlock(tp);
5672 return;
5673 }
5674
5675 tg3_full_unlock(tp);
5676
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005677 tg3_phy_stop(tp);
5678
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 tg3_netif_stop(tp);
5680
David S. Millerf47c11e2005-06-24 20:18:35 -07005681 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682
Joe Perches63c3a662011-04-26 08:12:10 +00005683 restart_timer = tg3_flag(tp, RESTART_TIMER);
5684 tg3_flag_clear(tp, RESTART_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685
Joe Perches63c3a662011-04-26 08:12:10 +00005686 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07005687 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5688 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00005689 tg3_flag_set(tp, MBOX_WRITE_REORDER);
5690 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005691 }
5692
Michael Chan944d9802005-05-29 14:57:48 -07005693 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005694 err = tg3_init_hw(tp, 1);
5695 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005696 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
5698 tg3_netif_start(tp);
5699
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 if (restart_timer)
5701 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005702
Michael Chanb9ec6c12006-07-25 16:37:27 -07005703out:
Michael Chan7faa0062006-02-02 17:29:28 -08005704 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005705
5706 if (!err)
5707 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708}
5709
5710static void tg3_tx_timeout(struct net_device *dev)
5711{
5712 struct tg3 *tp = netdev_priv(dev);
5713
Michael Chanb0408752007-02-13 12:18:30 -08005714 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005715 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00005716 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08005717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718
5719 schedule_work(&tp->reset_task);
5720}
5721
Michael Chanc58ec932005-09-17 00:46:27 -07005722/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5723static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5724{
5725 u32 base = (u32) mapping & 0xffffffff;
5726
Eric Dumazet807540b2010-09-23 05:40:09 +00005727 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005728}
5729
Michael Chan72f2afb2006-03-06 19:28:35 -08005730/* Test for DMA addresses > 40-bit */
5731static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5732 int len)
5733{
5734#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00005735 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00005736 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005737 return 0;
5738#else
5739 return 0;
5740#endif
5741}
5742
Matt Carlson2ffcc982011-05-19 12:12:44 +00005743static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
5744 dma_addr_t mapping, int len, u32 flags,
5745 u32 mss_and_is_end)
5746{
5747 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
5748 int is_end = (mss_and_is_end & 0x1);
5749 u32 mss = (mss_and_is_end >> 1);
5750 u32 vlan_tag = 0;
5751
5752 if (is_end)
5753 flags |= TXD_FLAG_END;
5754 if (flags & TXD_FLAG_VLAN) {
5755 vlan_tag = flags >> 16;
5756 flags &= 0xffff;
5757 }
5758 vlan_tag |= (mss << TXD_MSS_SHIFT);
5759
5760 txd->addr_hi = ((u64) mapping >> 32);
5761 txd->addr_lo = ((u64) mapping & 0xffffffff);
5762 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5763 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5764}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765
Matt Carlson432aa7e2011-05-19 12:12:45 +00005766static void tg3_skb_error_unmap(struct tg3_napi *tnapi,
5767 struct sk_buff *skb, int last)
5768{
5769 int i;
5770 u32 entry = tnapi->tx_prod;
5771 struct ring_info *txb = &tnapi->tx_buffers[entry];
5772
5773 pci_unmap_single(tnapi->tp->pdev,
5774 dma_unmap_addr(txb, mapping),
5775 skb_headlen(skb),
5776 PCI_DMA_TODEVICE);
Matt Carlson9a2e0fb2011-06-02 13:01:39 +00005777 for (i = 0; i < last; i++) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00005778 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5779
5780 entry = NEXT_TX(entry);
5781 txb = &tnapi->tx_buffers[entry];
5782
5783 pci_unmap_page(tnapi->tp->pdev,
5784 dma_unmap_addr(txb, mapping),
5785 frag->size, PCI_DMA_TODEVICE);
5786 }
5787}
5788
Michael Chan72f2afb2006-03-06 19:28:35 -08005789/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005790static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
Matt Carlson432aa7e2011-05-19 12:12:45 +00005791 struct sk_buff *skb,
5792 u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005794 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005795 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005796 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005797 u32 entry = tnapi->tx_prod;
5798 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799
Matt Carlson41588ba2008-04-19 18:12:33 -07005800 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5801 new_skb = skb_copy(skb, GFP_ATOMIC);
5802 else {
5803 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5804
5805 new_skb = skb_copy_expand(skb,
5806 skb_headroom(skb) + more_headroom,
5807 skb_tailroom(skb), GFP_ATOMIC);
5808 }
5809
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005811 ret = -1;
5812 } else {
5813 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005814 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5815 PCI_DMA_TODEVICE);
5816 /* Make sure the mapping succeeded */
5817 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5818 ret = -1;
5819 dev_kfree_skb(new_skb);
David S. Miller90079ce2008-09-11 04:52:51 -07005820
Michael Chanc58ec932005-09-17 00:46:27 -07005821 /* Make sure new skb does not cross any 4G boundaries.
5822 * Drop the packet if it does.
5823 */
Joe Perches63c3a662011-04-26 08:12:10 +00005824 } else if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
5825 tg3_4g_overflow_test(new_addr, new_skb->len)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005826 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5827 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005828 ret = -1;
5829 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07005830 } else {
Matt Carlson432aa7e2011-05-19 12:12:45 +00005831 tnapi->tx_buffers[entry].skb = new_skb;
5832 dma_unmap_addr_set(&tnapi->tx_buffers[entry],
5833 mapping, new_addr);
5834
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005835 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005836 base_flags, 1 | (mss << 1));
Michael Chanc58ec932005-09-17 00:46:27 -07005837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 }
5839
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 dev_kfree_skb(skb);
5841
Michael Chanc58ec932005-09-17 00:46:27 -07005842 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843}
5844
Matt Carlson2ffcc982011-05-19 12:12:44 +00005845static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005846
5847/* Use GSO to workaround a rare TSO bug that may be triggered when the
5848 * TSO header is greater than 80 bytes.
5849 */
5850static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5851{
5852 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005853 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005854
5855 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005856 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005857 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005858
5859 /* netif_tx_stop_queue() must be done before checking
5860 * checking tx index in tg3_tx_avail() below, because in
5861 * tg3_tx(), we update tx index before checking for
5862 * netif_tx_queue_stopped().
5863 */
5864 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005865 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005866 return NETDEV_TX_BUSY;
5867
5868 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005869 }
5870
5871 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005872 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005873 goto tg3_tso_bug_end;
5874
5875 do {
5876 nskb = segs;
5877 segs = segs->next;
5878 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00005879 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005880 } while (segs);
5881
5882tg3_tso_bug_end:
5883 dev_kfree_skb(skb);
5884
5885 return NETDEV_TX_OK;
5886}
Michael Chan52c0fd82006-06-29 20:15:54 -07005887
Michael Chan5a6f3072006-03-20 22:28:05 -08005888/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00005889 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005890 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00005891static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005892{
5893 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005894 u32 len, entry, base_flags, mss;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005895 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005896 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005897 struct tg3_napi *tnapi;
5898 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005899 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005900
Matt Carlson24f4efd2009-11-13 13:03:35 +00005901 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5902 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00005903 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00005904 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905
Michael Chan00b70502006-06-17 21:58:45 -07005906 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005907 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005908 * interrupt. Furthermore, IRQ processing runs lockless so we have
5909 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005911 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005912 if (!netif_tx_queue_stopped(txq)) {
5913 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005914
5915 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005916 netdev_err(dev,
5917 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 return NETDEV_TX_BUSY;
5920 }
5921
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005922 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005924 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005926
Matt Carlsonbe98da62010-07-11 09:31:46 +00005927 mss = skb_shinfo(skb)->gso_size;
5928 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005929 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00005930 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931
5932 if (skb_header_cloned(skb) &&
5933 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5934 dev_kfree_skb(skb);
5935 goto out_unlock;
5936 }
5937
Matt Carlson34195c32010-07-11 09:31:42 +00005938 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005939 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
Matt Carlson02e96082010-09-15 08:59:59 +00005941 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00005942 hdr_len = skb_headlen(skb) - ETH_HLEN;
5943 } else {
5944 u32 ip_tcp_len;
5945
5946 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5947 hdr_len = ip_tcp_len + tcp_opt_len;
5948
5949 iph->check = 0;
5950 iph->tot_len = htons(mss + hdr_len);
5951 }
5952
Michael Chan52c0fd82006-06-29 20:15:54 -07005953 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00005954 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00005955 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07005956
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5958 TXD_FLAG_CPU_POST_DMA);
5959
Joe Perches63c3a662011-04-26 08:12:10 +00005960 if (tg3_flag(tp, HW_TSO_1) ||
5961 tg3_flag(tp, HW_TSO_2) ||
5962 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005963 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005965 } else
5966 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5967 iph->daddr, 0,
5968 IPPROTO_TCP,
5969 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970
Joe Perches63c3a662011-04-26 08:12:10 +00005971 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00005972 mss |= (hdr_len & 0xc) << 12;
5973 if (hdr_len & 0x10)
5974 base_flags |= 0x00000010;
5975 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00005976 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005977 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00005978 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005979 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005980 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 int tsflags;
5982
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005983 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 mss |= (tsflags << 11);
5985 }
5986 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005987 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 int tsflags;
5989
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005990 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 base_flags |= tsflags << 12;
5992 }
5993 }
5994 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00005995
Jesse Grosseab6d182010-10-20 13:56:03 +00005996 if (vlan_tx_tag_present(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 base_flags |= (TXD_FLAG_VLAN |
5998 (vlan_tx_tag_get(skb) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999
Joe Perches63c3a662011-04-26 08:12:10 +00006000 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00006001 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006002 base_flags |= TXD_FLAG_JMB_PKT;
6003
Alexander Duyckf4188d82009-12-02 16:48:38 +00006004 len = skb_headlen(skb);
6005
6006 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6007 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006008 dev_kfree_skb(skb);
6009 goto out_unlock;
6010 }
6011
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006012 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006013 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014
6015 would_hit_hwbug = 0;
6016
Joe Perches63c3a662011-04-26 08:12:10 +00006017 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006018 would_hit_hwbug = 1;
6019
Joe Perches63c3a662011-04-26 08:12:10 +00006020 if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006021 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07006022 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00006023
Joe Perches63c3a662011-04-26 08:12:10 +00006024 if (tg3_flag(tp, 40BIT_DMA_LIMIT_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006025 tg3_40bit_overflow_test(tp, mapping, len))
6026 would_hit_hwbug = 1;
6027
Joe Perches63c3a662011-04-26 08:12:10 +00006028 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006029 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006031 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6033
6034 entry = NEXT_TX(entry);
6035
6036 /* Now loop through additional data fragments, and queue them. */
6037 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 last = skb_shinfo(skb)->nr_frags - 1;
6039 for (i = 0; i <= last; i++) {
6040 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6041
6042 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006043 mapping = pci_map_page(tp->pdev,
6044 frag->page,
6045 frag->page_offset,
6046 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006048 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006049 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006050 mapping);
6051 if (pci_dma_mapping_error(tp->pdev, mapping))
6052 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053
Joe Perches63c3a662011-04-26 08:12:10 +00006054 if (tg3_flag(tp, SHORT_DMA_BUG) &&
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006055 len <= 8)
6056 would_hit_hwbug = 1;
6057
Joe Perches63c3a662011-04-26 08:12:10 +00006058 if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006059 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07006060 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
Joe Perches63c3a662011-04-26 08:12:10 +00006062 if (tg3_flag(tp, 40BIT_DMA_LIMIT_BUG) &&
Matt Carlson0e1406d2009-11-02 12:33:33 +00006063 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08006064 would_hit_hwbug = 1;
6065
Joe Perches63c3a662011-04-26 08:12:10 +00006066 if (tg3_flag(tp, HW_TSO_1) ||
6067 tg3_flag(tp, HW_TSO_2) ||
6068 tg3_flag(tp, HW_TSO_3))
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006069 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 base_flags, (i == last)|(mss << 1));
6071 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006072 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 base_flags, (i == last));
6074
6075 entry = NEXT_TX(entry);
6076 }
6077 }
6078
6079 if (would_hit_hwbug) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00006080 tg3_skb_error_unmap(tnapi, skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
6082 /* If the workaround fails due to memory/mapping
6083 * failure, silently drop this packet.
6084 */
Matt Carlson432aa7e2011-05-19 12:12:45 +00006085 if (tigon3_dma_hwbug_workaround(tnapi, skb, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 goto out_unlock;
6087
Matt Carlson432aa7e2011-05-19 12:12:45 +00006088 entry = NEXT_TX(tnapi->tx_prod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 }
6090
6091 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006092 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006094 tnapi->tx_prod = entry;
6095 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006096 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006097
6098 /* netif_tx_stop_queue() must be done before checking
6099 * checking tx index in tg3_tx_avail() below, because in
6100 * tg3_tx(), we update tx index before checking for
6101 * netif_tx_queue_stopped().
6102 */
6103 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006104 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006105 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
6108out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006109 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110
6111 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006112
6113dma_error:
Matt Carlson432aa7e2011-05-19 12:12:45 +00006114 tg3_skb_error_unmap(tnapi, skb, i);
Alexander Duyckf4188d82009-12-02 16:48:38 +00006115 dev_kfree_skb(skb);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006116 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006117 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118}
6119
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006120static void tg3_set_loopback(struct net_device *dev, u32 features)
6121{
6122 struct tg3 *tp = netdev_priv(dev);
6123
6124 if (features & NETIF_F_LOOPBACK) {
6125 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6126 return;
6127
6128 /*
6129 * Clear MAC_MODE_HALF_DUPLEX or you won't get packets back in
6130 * loopback mode if Half-Duplex mode was negotiated earlier.
6131 */
6132 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
6133
6134 /* Enable internal MAC loopback mode */
6135 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6136 spin_lock_bh(&tp->lock);
6137 tw32(MAC_MODE, tp->mac_mode);
6138 netif_carrier_on(tp->dev);
6139 spin_unlock_bh(&tp->lock);
6140 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6141 } else {
6142 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6143 return;
6144
6145 /* Disable internal MAC loopback mode */
6146 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6147 spin_lock_bh(&tp->lock);
6148 tw32(MAC_MODE, tp->mac_mode);
6149 /* Force link status check */
6150 tg3_setup_phy(tp, 1);
6151 spin_unlock_bh(&tp->lock);
6152 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6153 }
6154}
6155
Michał Mirosławdc668912011-04-07 03:35:07 +00006156static u32 tg3_fix_features(struct net_device *dev, u32 features)
6157{
6158 struct tg3 *tp = netdev_priv(dev);
6159
Joe Perches63c3a662011-04-26 08:12:10 +00006160 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006161 features &= ~NETIF_F_ALL_TSO;
6162
6163 return features;
6164}
6165
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006166static int tg3_set_features(struct net_device *dev, u32 features)
6167{
6168 u32 changed = dev->features ^ features;
6169
6170 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
6171 tg3_set_loopback(dev, features);
6172
6173 return 0;
6174}
6175
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6177 int new_mtu)
6178{
6179 dev->mtu = new_mtu;
6180
Michael Chanef7f5ec2005-07-25 12:32:25 -07006181 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00006182 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006183 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00006184 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00006185 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006186 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00006187 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006188 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006189 if (tg3_flag(tp, 5780_CLASS)) {
6190 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00006191 netdev_update_features(dev);
6192 }
Joe Perches63c3a662011-04-26 08:12:10 +00006193 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07006194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195}
6196
6197static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6198{
6199 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006200 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201
6202 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6203 return -EINVAL;
6204
6205 if (!netif_running(dev)) {
6206 /* We'll just catch it later when the
6207 * device is up'd.
6208 */
6209 tg3_set_mtu(dev, tp, new_mtu);
6210 return 0;
6211 }
6212
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006213 tg3_phy_stop(tp);
6214
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006216
6217 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218
Michael Chan944d9802005-05-29 14:57:48 -07006219 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
6221 tg3_set_mtu(dev, tp, new_mtu);
6222
Michael Chanb9ec6c12006-07-25 16:37:27 -07006223 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224
Michael Chanb9ec6c12006-07-25 16:37:27 -07006225 if (!err)
6226 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227
David S. Millerf47c11e2005-06-24 20:18:35 -07006228 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006230 if (!err)
6231 tg3_phy_start(tp);
6232
Michael Chanb9ec6c12006-07-25 16:37:27 -07006233 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234}
6235
Matt Carlson21f581a2009-08-28 14:00:25 +00006236static void tg3_rx_prodring_free(struct tg3 *tp,
6237 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 int i;
6240
Matt Carlson8fea32b2010-09-15 08:59:58 +00006241 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006242 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006243 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006244 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6245 tp->rx_pkt_map_sz);
6246
Joe Perches63c3a662011-04-26 08:12:10 +00006247 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006248 for (i = tpr->rx_jmb_cons_idx;
6249 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006250 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006251 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6252 TG3_RX_JMB_MAP_SZ);
6253 }
6254 }
6255
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006256 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258
Matt Carlson2c49a442010-09-30 10:34:35 +00006259 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006260 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6261 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262
Joe Perches63c3a662011-04-26 08:12:10 +00006263 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006264 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006265 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6266 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 }
6268}
6269
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006270/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 *
6272 * The chip has been shut down and the driver detached from
6273 * the networking, so no interrupts or new tx packets will
6274 * end up in the driver. tp->{tx,}lock are held and thus
6275 * we may not sleep.
6276 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006277static int tg3_rx_prodring_alloc(struct tg3 *tp,
6278 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279{
Matt Carlson287be122009-08-28 13:58:46 +00006280 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006282 tpr->rx_std_cons_idx = 0;
6283 tpr->rx_std_prod_idx = 0;
6284 tpr->rx_jmb_cons_idx = 0;
6285 tpr->rx_jmb_prod_idx = 0;
6286
Matt Carlson8fea32b2010-09-15 08:59:58 +00006287 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006288 memset(&tpr->rx_std_buffers[0], 0,
6289 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006290 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006291 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006292 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006293 goto done;
6294 }
6295
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006297 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298
Matt Carlson287be122009-08-28 13:58:46 +00006299 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00006300 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006301 tp->dev->mtu > ETH_DATA_LEN)
6302 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6303 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006304
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 /* Initialize invariants of the rings, we only set this
6306 * stuff once. This works because the card does not
6307 * write into the rx buffer posting rings.
6308 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006309 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310 struct tg3_rx_buffer_desc *rxd;
6311
Matt Carlson21f581a2009-08-28 14:00:25 +00006312 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006313 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6315 rxd->opaque = (RXD_OPAQUE_RING_STD |
6316 (i << RXD_OPAQUE_INDEX_SHIFT));
6317 }
6318
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006319 /* Now allocate fresh SKBs for each rx ring. */
6320 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006321 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006322 netdev_warn(tp->dev,
6323 "Using a smaller RX standard ring. Only "
6324 "%d out of %d buffers were allocated "
6325 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006326 if (i == 0)
6327 goto initfail;
6328 tp->rx_pending = i;
6329 break;
6330 }
6331 }
6332
Joe Perches63c3a662011-04-26 08:12:10 +00006333 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006334 goto done;
6335
Matt Carlson2c49a442010-09-30 10:34:35 +00006336 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006337
Joe Perches63c3a662011-04-26 08:12:10 +00006338 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00006339 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340
Matt Carlson2c49a442010-09-30 10:34:35 +00006341 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006342 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343
Matt Carlson0d86df82010-02-17 15:17:00 +00006344 rxd = &tpr->rx_jmb[i].std;
6345 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6346 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6347 RXD_FLAG_JUMBO;
6348 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6349 (i << RXD_OPAQUE_INDEX_SHIFT));
6350 }
6351
6352 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6353 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006354 netdev_warn(tp->dev,
6355 "Using a smaller RX jumbo ring. Only %d "
6356 "out of %d buffers were allocated "
6357 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006358 if (i == 0)
6359 goto initfail;
6360 tp->rx_jumbo_pending = i;
6361 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 }
6363 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006364
6365done:
Michael Chan32d8c572006-07-25 16:38:29 -07006366 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006367
6368initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006369 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006370 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371}
6372
Matt Carlson21f581a2009-08-28 14:00:25 +00006373static void tg3_rx_prodring_fini(struct tg3 *tp,
6374 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375{
Matt Carlson21f581a2009-08-28 14:00:25 +00006376 kfree(tpr->rx_std_buffers);
6377 tpr->rx_std_buffers = NULL;
6378 kfree(tpr->rx_jmb_buffers);
6379 tpr->rx_jmb_buffers = NULL;
6380 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006381 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6382 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006383 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006385 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006386 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6387 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006388 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006390}
6391
Matt Carlson21f581a2009-08-28 14:00:25 +00006392static int tg3_rx_prodring_init(struct tg3 *tp,
6393 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006394{
Matt Carlson2c49a442010-09-30 10:34:35 +00006395 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6396 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006397 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006398 return -ENOMEM;
6399
Matt Carlson4bae65c2010-11-24 08:31:52 +00006400 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6401 TG3_RX_STD_RING_BYTES(tp),
6402 &tpr->rx_std_mapping,
6403 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006404 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006405 goto err_out;
6406
Joe Perches63c3a662011-04-26 08:12:10 +00006407 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006408 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006409 GFP_KERNEL);
6410 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006411 goto err_out;
6412
Matt Carlson4bae65c2010-11-24 08:31:52 +00006413 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6414 TG3_RX_JMB_RING_BYTES(tp),
6415 &tpr->rx_jmb_mapping,
6416 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006417 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006418 goto err_out;
6419 }
6420
6421 return 0;
6422
6423err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006424 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006425 return -ENOMEM;
6426}
6427
6428/* Free up pending packets in all rx/tx rings.
6429 *
6430 * The chip has been shut down and the driver detached from
6431 * the networking, so no interrupts or new tx packets will
6432 * end up in the driver. tp->{tx,}lock is not held and we are not
6433 * in an interrupt context and thus may sleep.
6434 */
6435static void tg3_free_rings(struct tg3 *tp)
6436{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006437 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006438
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006439 for (j = 0; j < tp->irq_cnt; j++) {
6440 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006441
Matt Carlson8fea32b2010-09-15 08:59:58 +00006442 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006443
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006444 if (!tnapi->tx_buffers)
6445 continue;
6446
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006447 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006448 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006449 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006450 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006451
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006452 txp = &tnapi->tx_buffers[i];
6453 skb = txp->skb;
6454
6455 if (skb == NULL) {
6456 i++;
6457 continue;
6458 }
6459
Alexander Duyckf4188d82009-12-02 16:48:38 +00006460 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006461 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006462 skb_headlen(skb),
6463 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006464 txp->skb = NULL;
6465
Alexander Duyckf4188d82009-12-02 16:48:38 +00006466 i++;
6467
6468 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6469 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6470 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006471 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006472 skb_shinfo(skb)->frags[k].size,
6473 PCI_DMA_TODEVICE);
6474 i++;
6475 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006476
6477 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006478 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006479 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006480}
6481
6482/* Initialize tx/rx rings for packet processing.
6483 *
6484 * The chip has been shut down and the driver detached from
6485 * the networking, so no interrupts or new tx packets will
6486 * end up in the driver. tp->{tx,}lock are held and thus
6487 * we may not sleep.
6488 */
6489static int tg3_init_rings(struct tg3 *tp)
6490{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006491 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006492
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006493 /* Free up all the SKBs. */
6494 tg3_free_rings(tp);
6495
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006496 for (i = 0; i < tp->irq_cnt; i++) {
6497 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006498
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006499 tnapi->last_tag = 0;
6500 tnapi->last_irq_tag = 0;
6501 tnapi->hw_status->status = 0;
6502 tnapi->hw_status->status_tag = 0;
6503 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6504
6505 tnapi->tx_prod = 0;
6506 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006507 if (tnapi->tx_ring)
6508 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006509
6510 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006511 if (tnapi->rx_rcb)
6512 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006513
Matt Carlson8fea32b2010-09-15 08:59:58 +00006514 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006515 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006516 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006517 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006518 }
Matt Carlson72334482009-08-28 14:03:01 +00006519
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006520 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006521}
6522
6523/*
6524 * Must not be invoked with interrupt sources disabled and
6525 * the hardware shutdown down.
6526 */
6527static void tg3_free_consistent(struct tg3 *tp)
6528{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006529 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006530
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006531 for (i = 0; i < tp->irq_cnt; i++) {
6532 struct tg3_napi *tnapi = &tp->napi[i];
6533
6534 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006535 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006536 tnapi->tx_ring, tnapi->tx_desc_mapping);
6537 tnapi->tx_ring = NULL;
6538 }
6539
6540 kfree(tnapi->tx_buffers);
6541 tnapi->tx_buffers = NULL;
6542
6543 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006544 dma_free_coherent(&tp->pdev->dev,
6545 TG3_RX_RCB_RING_BYTES(tp),
6546 tnapi->rx_rcb,
6547 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006548 tnapi->rx_rcb = NULL;
6549 }
6550
Matt Carlson8fea32b2010-09-15 08:59:58 +00006551 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6552
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006553 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006554 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6555 tnapi->hw_status,
6556 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006557 tnapi->hw_status = NULL;
6558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006560
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006562 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6563 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564 tp->hw_stats = NULL;
6565 }
6566}
6567
6568/*
6569 * Must not be invoked with interrupt sources disabled and
6570 * the hardware shutdown down. Can sleep.
6571 */
6572static int tg3_alloc_consistent(struct tg3 *tp)
6573{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006574 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006575
Matt Carlson4bae65c2010-11-24 08:31:52 +00006576 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6577 sizeof(struct tg3_hw_stats),
6578 &tp->stats_mapping,
6579 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 if (!tp->hw_stats)
6581 goto err_out;
6582
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6584
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006585 for (i = 0; i < tp->irq_cnt; i++) {
6586 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006587 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006588
Matt Carlson4bae65c2010-11-24 08:31:52 +00006589 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6590 TG3_HW_STATUS_SIZE,
6591 &tnapi->status_mapping,
6592 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006593 if (!tnapi->hw_status)
6594 goto err_out;
6595
6596 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006597 sblk = tnapi->hw_status;
6598
Matt Carlson8fea32b2010-09-15 08:59:58 +00006599 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6600 goto err_out;
6601
Matt Carlson19cfaec2009-12-03 08:36:20 +00006602 /* If multivector TSS is enabled, vector 0 does not handle
6603 * tx interrupts. Don't allocate any resources for it.
6604 */
Joe Perches63c3a662011-04-26 08:12:10 +00006605 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
6606 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00006607 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6608 TG3_TX_RING_SIZE,
6609 GFP_KERNEL);
6610 if (!tnapi->tx_buffers)
6611 goto err_out;
6612
Matt Carlson4bae65c2010-11-24 08:31:52 +00006613 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6614 TG3_TX_RING_BYTES,
6615 &tnapi->tx_desc_mapping,
6616 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006617 if (!tnapi->tx_ring)
6618 goto err_out;
6619 }
6620
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006621 /*
6622 * When RSS is enabled, the status block format changes
6623 * slightly. The "rx_jumbo_consumer", "reserved",
6624 * and "rx_mini_consumer" members get mapped to the
6625 * other three rx return ring producer indexes.
6626 */
6627 switch (i) {
6628 default:
6629 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6630 break;
6631 case 2:
6632 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6633 break;
6634 case 3:
6635 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6636 break;
6637 case 4:
6638 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6639 break;
6640 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006641
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006642 /*
6643 * If multivector RSS is enabled, vector 0 does not handle
6644 * rx or tx interrupts. Don't allocate any resources for it.
6645 */
Joe Perches63c3a662011-04-26 08:12:10 +00006646 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006647 continue;
6648
Matt Carlson4bae65c2010-11-24 08:31:52 +00006649 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6650 TG3_RX_RCB_RING_BYTES(tp),
6651 &tnapi->rx_rcb_mapping,
6652 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006653 if (!tnapi->rx_rcb)
6654 goto err_out;
6655
6656 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006657 }
6658
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 return 0;
6660
6661err_out:
6662 tg3_free_consistent(tp);
6663 return -ENOMEM;
6664}
6665
6666#define MAX_WAIT_CNT 1000
6667
6668/* To stop a block, clear the enable bit and poll till it
6669 * clears. tp->lock is held.
6670 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006671static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672{
6673 unsigned int i;
6674 u32 val;
6675
Joe Perches63c3a662011-04-26 08:12:10 +00006676 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 switch (ofs) {
6678 case RCVLSC_MODE:
6679 case DMAC_MODE:
6680 case MBFREE_MODE:
6681 case BUFMGR_MODE:
6682 case MEMARB_MODE:
6683 /* We can't enable/disable these bits of the
6684 * 5705/5750, just say success.
6685 */
6686 return 0;
6687
6688 default:
6689 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691 }
6692
6693 val = tr32(ofs);
6694 val &= ~enable_bit;
6695 tw32_f(ofs, val);
6696
6697 for (i = 0; i < MAX_WAIT_CNT; i++) {
6698 udelay(100);
6699 val = tr32(ofs);
6700 if ((val & enable_bit) == 0)
6701 break;
6702 }
6703
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006704 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006705 dev_err(&tp->pdev->dev,
6706 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6707 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 return -ENODEV;
6709 }
6710
6711 return 0;
6712}
6713
6714/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006715static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716{
6717 int i, err;
6718
6719 tg3_disable_ints(tp);
6720
6721 tp->rx_mode &= ~RX_MODE_ENABLE;
6722 tw32_f(MAC_RX_MODE, tp->rx_mode);
6723 udelay(10);
6724
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006725 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6726 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6727 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6728 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6729 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6730 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006732 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6733 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6734 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6735 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6736 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6737 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6738 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739
6740 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6741 tw32_f(MAC_MODE, tp->mac_mode);
6742 udelay(40);
6743
6744 tp->tx_mode &= ~TX_MODE_ENABLE;
6745 tw32_f(MAC_TX_MODE, tp->tx_mode);
6746
6747 for (i = 0; i < MAX_WAIT_CNT; i++) {
6748 udelay(100);
6749 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6750 break;
6751 }
6752 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006753 dev_err(&tp->pdev->dev,
6754 "%s timed out, TX_MODE_ENABLE will not clear "
6755 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006756 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 }
6758
Michael Chane6de8ad2005-05-05 14:42:41 -07006759 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006760 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6761 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762
6763 tw32(FTQ_RESET, 0xffffffff);
6764 tw32(FTQ_RESET, 0x00000000);
6765
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006766 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6767 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006769 for (i = 0; i < tp->irq_cnt; i++) {
6770 struct tg3_napi *tnapi = &tp->napi[i];
6771 if (tnapi->hw_status)
6772 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 if (tp->hw_stats)
6775 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6776
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 return err;
6778}
6779
Matt Carlson0d3031d2007-10-10 18:02:43 -07006780static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6781{
6782 int i;
6783 u32 apedata;
6784
Matt Carlsondc6d0742010-09-15 08:59:55 +00006785 /* NCSI does not support APE events */
Joe Perches63c3a662011-04-26 08:12:10 +00006786 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsondc6d0742010-09-15 08:59:55 +00006787 return;
6788
Matt Carlson0d3031d2007-10-10 18:02:43 -07006789 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6790 if (apedata != APE_SEG_SIG_MAGIC)
6791 return;
6792
6793 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006794 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006795 return;
6796
6797 /* Wait for up to 1 millisecond for APE to service previous event. */
6798 for (i = 0; i < 10; i++) {
6799 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6800 return;
6801
6802 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6803
6804 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6805 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6806 event | APE_EVENT_STATUS_EVENT_PENDING);
6807
6808 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6809
6810 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6811 break;
6812
6813 udelay(100);
6814 }
6815
6816 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6817 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6818}
6819
6820static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6821{
6822 u32 event;
6823 u32 apedata;
6824
Joe Perches63c3a662011-04-26 08:12:10 +00006825 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006826 return;
6827
6828 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006829 case RESET_KIND_INIT:
6830 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6831 APE_HOST_SEG_SIG_MAGIC);
6832 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6833 APE_HOST_SEG_LEN_MAGIC);
6834 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6835 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6836 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006837 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006838 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6839 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006840 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6841 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006842
Matt Carlson33f401a2010-04-05 10:19:27 +00006843 event = APE_EVENT_STATUS_STATE_START;
6844 break;
6845 case RESET_KIND_SHUTDOWN:
6846 /* With the interface we are currently using,
6847 * APE does not track driver state. Wiping
6848 * out the HOST SEGMENT SIGNATURE forces
6849 * the APE to assume OS absent status.
6850 */
6851 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006852
Matt Carlsondc6d0742010-09-15 08:59:55 +00006853 if (device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006854 tg3_flag(tp, WOL_ENABLE)) {
Matt Carlsondc6d0742010-09-15 08:59:55 +00006855 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6856 TG3_APE_HOST_WOL_SPEED_AUTO);
6857 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6858 } else
6859 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6860
6861 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6862
Matt Carlson33f401a2010-04-05 10:19:27 +00006863 event = APE_EVENT_STATUS_STATE_UNLOAD;
6864 break;
6865 case RESET_KIND_SUSPEND:
6866 event = APE_EVENT_STATUS_STATE_SUSPEND;
6867 break;
6868 default:
6869 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006870 }
6871
6872 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6873
6874 tg3_ape_send_event(tp, event);
6875}
6876
Michael Chane6af3012005-04-21 17:12:05 -07006877/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6879{
David S. Millerf49639e2006-06-09 11:58:36 -07006880 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6881 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882
Joe Perches63c3a662011-04-26 08:12:10 +00006883 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 switch (kind) {
6885 case RESET_KIND_INIT:
6886 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6887 DRV_STATE_START);
6888 break;
6889
6890 case RESET_KIND_SHUTDOWN:
6891 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6892 DRV_STATE_UNLOAD);
6893 break;
6894
6895 case RESET_KIND_SUSPEND:
6896 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6897 DRV_STATE_SUSPEND);
6898 break;
6899
6900 default:
6901 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006904
6905 if (kind == RESET_KIND_INIT ||
6906 kind == RESET_KIND_SUSPEND)
6907 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908}
6909
6910/* tp->lock is held. */
6911static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6912{
Joe Perches63c3a662011-04-26 08:12:10 +00006913 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 switch (kind) {
6915 case RESET_KIND_INIT:
6916 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6917 DRV_STATE_START_DONE);
6918 break;
6919
6920 case RESET_KIND_SHUTDOWN:
6921 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6922 DRV_STATE_UNLOAD_DONE);
6923 break;
6924
6925 default:
6926 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006929
6930 if (kind == RESET_KIND_SHUTDOWN)
6931 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932}
6933
6934/* tp->lock is held. */
6935static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6936{
Joe Perches63c3a662011-04-26 08:12:10 +00006937 if (tg3_flag(tp, ENABLE_ASF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 switch (kind) {
6939 case RESET_KIND_INIT:
6940 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6941 DRV_STATE_START);
6942 break;
6943
6944 case RESET_KIND_SHUTDOWN:
6945 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6946 DRV_STATE_UNLOAD);
6947 break;
6948
6949 case RESET_KIND_SUSPEND:
6950 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6951 DRV_STATE_SUSPEND);
6952 break;
6953
6954 default:
6955 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957 }
6958}
6959
Michael Chan7a6f4362006-09-27 16:03:31 -07006960static int tg3_poll_fw(struct tg3 *tp)
6961{
6962 int i;
6963 u32 val;
6964
Michael Chanb5d37722006-09-27 16:06:21 -07006965 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006966 /* Wait up to 20ms for init done. */
6967 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006968 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6969 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006970 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006971 }
6972 return -ENODEV;
6973 }
6974
Michael Chan7a6f4362006-09-27 16:03:31 -07006975 /* Wait for firmware initialization to complete. */
6976 for (i = 0; i < 100000; i++) {
6977 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6978 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6979 break;
6980 udelay(10);
6981 }
6982
6983 /* Chip might not be fitted with firmware. Some Sun onboard
6984 * parts are configured like that. So don't signal the timeout
6985 * of the above loop as an error, but do report the lack of
6986 * running firmware once.
6987 */
Joe Perches63c3a662011-04-26 08:12:10 +00006988 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
6989 tg3_flag_set(tp, NO_FWARE_REPORTED);
Michael Chan7a6f4362006-09-27 16:03:31 -07006990
Joe Perches05dbe002010-02-17 19:44:19 +00006991 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07006992 }
6993
Matt Carlson6b10c162010-02-12 14:47:08 +00006994 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
6995 /* The 57765 A0 needs a little more
6996 * time to do some important work.
6997 */
6998 mdelay(10);
6999 }
7000
Michael Chan7a6f4362006-09-27 16:03:31 -07007001 return 0;
7002}
7003
Michael Chanee6a99b2007-07-18 21:49:10 -07007004/* Save PCI command register before chip reset */
7005static void tg3_save_pci_state(struct tg3 *tp)
7006{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007007 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007008}
7009
7010/* Restore PCI state after chip reset */
7011static void tg3_restore_pci_state(struct tg3 *tp)
7012{
7013 u32 val;
7014
7015 /* Re-enable indirect register accesses. */
7016 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7017 tp->misc_host_ctrl);
7018
7019 /* Set MAX PCI retry to zero. */
7020 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7021 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007022 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007023 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007024 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007025 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007026 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00007027 PCISTATE_ALLOW_APE_SHMEM_WR |
7028 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007029 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7030
Matt Carlson8a6eac92007-10-21 16:17:55 -07007031 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007032
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007033 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +00007034 if (tg3_flag(tp, PCI_EXPRESS))
Matt Carlsoncf790032010-11-24 08:31:48 +00007035 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007036 else {
7037 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7038 tp->pci_cacheline_sz);
7039 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7040 tp->pci_lat_timer);
7041 }
Michael Chan114342f2007-10-15 02:12:26 -07007042 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007043
Michael Chanee6a99b2007-07-18 21:49:10 -07007044 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007045 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007046 u16 pcix_cmd;
7047
7048 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7049 &pcix_cmd);
7050 pcix_cmd &= ~PCI_X_CMD_ERO;
7051 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7052 pcix_cmd);
7053 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007054
Joe Perches63c3a662011-04-26 08:12:10 +00007055 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007056
7057 /* Chip reset on 5780 will reset MSI enable bit,
7058 * so need to restore it.
7059 */
Joe Perches63c3a662011-04-26 08:12:10 +00007060 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007061 u16 ctrl;
7062
7063 pci_read_config_word(tp->pdev,
7064 tp->msi_cap + PCI_MSI_FLAGS,
7065 &ctrl);
7066 pci_write_config_word(tp->pdev,
7067 tp->msi_cap + PCI_MSI_FLAGS,
7068 ctrl | PCI_MSI_FLAGS_ENABLE);
7069 val = tr32(MSGINT_MODE);
7070 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7071 }
7072 }
7073}
7074
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075static void tg3_stop_fw(struct tg3 *);
7076
7077/* tp->lock is held. */
7078static int tg3_chip_reset(struct tg3 *tp)
7079{
7080 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007081 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007082 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083
David S. Millerf49639e2006-06-09 11:58:36 -07007084 tg3_nvram_lock(tp);
7085
Matt Carlson77b483f2008-08-15 14:07:24 -07007086 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7087
David S. Millerf49639e2006-06-09 11:58:36 -07007088 /* No matching tg3_nvram_unlock() after this because
7089 * chip reset below will undo the nvram lock.
7090 */
7091 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092
Michael Chanee6a99b2007-07-18 21:49:10 -07007093 /* GRC_MISC_CFG core clock reset will clear the memory
7094 * enable bit in PCI register 4 and the MSI enable bit
7095 * on some chips, so we save relevant registers here.
7096 */
7097 tg3_save_pci_state(tp);
7098
Michael Chand9ab5ad2006-03-20 22:27:35 -08007099 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007100 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007101 tw32(GRC_FASTBOOT_PC, 0);
7102
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 /*
7104 * We must avoid the readl() that normally takes place.
7105 * It locks machines, causes machine checks, and other
7106 * fun things. So, temporarily disable the 5701
7107 * hardware workaround, while we do the reset.
7108 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007109 write_op = tp->write32;
7110 if (write_op == tg3_write_flush_reg32)
7111 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112
Michael Chand18edcb2007-03-24 20:57:11 -07007113 /* Prevent the irq handler from reading or writing PCI registers
7114 * during chip reset when the memory enable bit in the PCI command
7115 * register may be cleared. The chip does not generate interrupt
7116 * at this time, but the irq handler may still be called due to irq
7117 * sharing or irqpoll.
7118 */
Joe Perches63c3a662011-04-26 08:12:10 +00007119 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007120 for (i = 0; i < tp->irq_cnt; i++) {
7121 struct tg3_napi *tnapi = &tp->napi[i];
7122 if (tnapi->hw_status) {
7123 tnapi->hw_status->status = 0;
7124 tnapi->hw_status->status_tag = 0;
7125 }
7126 tnapi->last_tag = 0;
7127 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007128 }
Michael Chand18edcb2007-03-24 20:57:11 -07007129 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007130
7131 for (i = 0; i < tp->irq_cnt; i++)
7132 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007133
Matt Carlson255ca312009-08-25 10:07:27 +00007134 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7135 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7136 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7137 }
7138
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 /* do the reset */
7140 val = GRC_MISC_CFG_CORECLK_RESET;
7141
Joe Perches63c3a662011-04-26 08:12:10 +00007142 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007143 /* Force PCIe 1.0a mode */
7144 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007145 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007146 tr32(TG3_PCIE_PHY_TSTCTL) ==
7147 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7148 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7149
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7151 tw32(GRC_MISC_CFG, (1 << 29));
7152 val |= (1 << 29);
7153 }
7154 }
7155
Michael Chanb5d37722006-09-27 16:06:21 -07007156 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7157 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7158 tw32(GRC_VCPU_EXT_CTRL,
7159 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7160 }
7161
Matt Carlsonf37500d2010-08-02 11:25:59 +00007162 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007163 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007165
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166 tw32(GRC_MISC_CFG, val);
7167
Michael Chan1ee582d2005-08-09 20:16:46 -07007168 /* restore 5701 hardware bug workaround write method */
7169 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170
7171 /* Unfortunately, we have to delay before the PCI read back.
7172 * Some 575X chips even will not respond to a PCI cfg access
7173 * when the reset command is given to the chip.
7174 *
7175 * How do these hardware designers expect things to work
7176 * properly if the PCI write is posted for a long period
7177 * of time? It is always necessary to have some method by
7178 * which a register read back can occur to push the write
7179 * out which does the reset.
7180 *
7181 * For most tg3 variants the trick below was working.
7182 * Ho hum...
7183 */
7184 udelay(120);
7185
7186 /* Flush PCI posted writes. The normal MMIO registers
7187 * are inaccessible at this time so this is the only
7188 * way to make this reliably (actually, this is no longer
7189 * the case, see above). I tried to use indirect
7190 * register read/write but this upset some 5701 variants.
7191 */
7192 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7193
7194 udelay(120);
7195
Joe Perches63c3a662011-04-26 08:12:10 +00007196 if (tg3_flag(tp, PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00007197 u16 val16;
7198
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7200 int i;
7201 u32 cfg_val;
7202
7203 /* Wait for link training to complete. */
7204 for (i = 0; i < 5000; i++)
7205 udelay(100);
7206
7207 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7208 pci_write_config_dword(tp->pdev, 0xc4,
7209 cfg_val | (1 << 15));
7210 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007211
Matt Carlsone7126992009-08-25 10:08:16 +00007212 /* Clear the "no snoop" and "relaxed ordering" bits. */
7213 pci_read_config_word(tp->pdev,
7214 tp->pcie_cap + PCI_EXP_DEVCTL,
7215 &val16);
7216 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7217 PCI_EXP_DEVCTL_NOSNOOP_EN);
7218 /*
7219 * Older PCIe devices only support the 128 byte
7220 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007221 */
Joe Perches63c3a662011-04-26 08:12:10 +00007222 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007223 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007224 pci_write_config_word(tp->pdev,
7225 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007226 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007227
Matt Carlsoncf790032010-11-24 08:31:48 +00007228 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007229
7230 /* Clear error status */
7231 pci_write_config_word(tp->pdev,
7232 tp->pcie_cap + PCI_EXP_DEVSTA,
7233 PCI_EXP_DEVSTA_CED |
7234 PCI_EXP_DEVSTA_NFED |
7235 PCI_EXP_DEVSTA_FED |
7236 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 }
7238
Michael Chanee6a99b2007-07-18 21:49:10 -07007239 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240
Joe Perches63c3a662011-04-26 08:12:10 +00007241 tg3_flag_clear(tp, CHIP_RESETTING);
7242 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007243
Michael Chanee6a99b2007-07-18 21:49:10 -07007244 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007245 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007246 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007247 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248
7249 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7250 tg3_stop_fw(tp);
7251 tw32(0x5000, 0x400);
7252 }
7253
7254 tw32(GRC_MODE, tp->grc_mode);
7255
7256 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007257 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258
7259 tw32(0xc4, val | (1 << 15));
7260 }
7261
7262 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7263 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7264 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7265 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7266 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7267 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7268 }
7269
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007270 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlsonb7d4d462011-07-20 10:20:50 +00007271 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007272 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007273 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlsonb7d4d462011-07-20 10:20:50 +00007274 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007275 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007277 val = 0;
7278
7279 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280 udelay(40);
7281
Matt Carlson77b483f2008-08-15 14:07:24 -07007282 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7283
Michael Chan7a6f4362006-09-27 16:03:31 -07007284 err = tg3_poll_fw(tp);
7285 if (err)
7286 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287
Matt Carlson0a9140c2009-08-28 12:27:50 +00007288 tg3_mdio_start(tp);
7289
Joe Perches63c3a662011-04-26 08:12:10 +00007290 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007291 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7292 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007293 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007294 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295
7296 tw32(0x7c00, val | (1 << 25));
7297 }
7298
Matt Carlsond78b59f2011-04-05 14:22:46 +00007299 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7300 val = tr32(TG3_CPMU_CLCK_ORIDE);
7301 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7302 }
7303
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007305 tg3_flag_clear(tp, ENABLE_ASF);
7306 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7308 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7309 u32 nic_cfg;
7310
7311 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7312 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007313 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007314 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007315 if (tg3_flag(tp, 5750_PLUS))
7316 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317 }
7318 }
7319
7320 return 0;
7321}
7322
7323/* tp->lock is held. */
7324static void tg3_stop_fw(struct tg3 *tp)
7325{
Joe Perches63c3a662011-04-26 08:12:10 +00007326 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007327 /* Wait for RX cpu to ACK the previous event. */
7328 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007329
7330 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007331
7332 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333
Matt Carlson7c5026a2008-05-02 16:49:29 -07007334 /* Wait for RX cpu to ACK this event. */
7335 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336 }
7337}
7338
7339/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007340static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341{
7342 int err;
7343
7344 tg3_stop_fw(tp);
7345
Michael Chan944d9802005-05-29 14:57:48 -07007346 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007348 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349 err = tg3_chip_reset(tp);
7350
Matt Carlsondaba2a62009-04-20 06:58:52 +00007351 __tg3_set_mac_addr(tp, 0);
7352
Michael Chan944d9802005-05-29 14:57:48 -07007353 tg3_write_sig_legacy(tp, kind);
7354 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355
7356 if (err)
7357 return err;
7358
7359 return 0;
7360}
7361
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362#define RX_CPU_SCRATCH_BASE 0x30000
7363#define RX_CPU_SCRATCH_SIZE 0x04000
7364#define TX_CPU_SCRATCH_BASE 0x34000
7365#define TX_CPU_SCRATCH_SIZE 0x04000
7366
7367/* tp->lock is held. */
7368static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7369{
7370 int i;
7371
Joe Perches63c3a662011-04-26 08:12:10 +00007372 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373
Michael Chanb5d37722006-09-27 16:06:21 -07007374 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7375 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7376
7377 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7378 return 0;
7379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380 if (offset == RX_CPU_BASE) {
7381 for (i = 0; i < 10000; i++) {
7382 tw32(offset + CPU_STATE, 0xffffffff);
7383 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7384 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7385 break;
7386 }
7387
7388 tw32(offset + CPU_STATE, 0xffffffff);
7389 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7390 udelay(10);
7391 } else {
7392 for (i = 0; i < 10000; i++) {
7393 tw32(offset + CPU_STATE, 0xffffffff);
7394 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7395 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7396 break;
7397 }
7398 }
7399
7400 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007401 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7402 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403 return -ENODEV;
7404 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007405
7406 /* Clear firmware's nvram arbitration. */
Joe Perches63c3a662011-04-26 08:12:10 +00007407 if (tg3_flag(tp, NVRAM))
Michael Chanec41c7d2006-01-17 02:40:55 -08007408 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409 return 0;
7410}
7411
7412struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007413 unsigned int fw_base;
7414 unsigned int fw_len;
7415 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416};
7417
7418/* tp->lock is held. */
7419static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7420 int cpu_scratch_size, struct fw_info *info)
7421{
Michael Chanec41c7d2006-01-17 02:40:55 -08007422 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423 void (*write_op)(struct tg3 *, u32, u32);
7424
Joe Perches63c3a662011-04-26 08:12:10 +00007425 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007426 netdev_err(tp->dev,
7427 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007428 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429 return -EINVAL;
7430 }
7431
Joe Perches63c3a662011-04-26 08:12:10 +00007432 if (tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 write_op = tg3_write_mem;
7434 else
7435 write_op = tg3_write_indirect_reg32;
7436
Michael Chan1b628152005-05-29 14:59:49 -07007437 /* It is possible that bootcode is still loading at this point.
7438 * Get the nvram lock first before halting the cpu.
7439 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007440 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007442 if (!lock_err)
7443 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444 if (err)
7445 goto out;
7446
7447 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7448 write_op(tp, cpu_scratch_base + i, 0);
7449 tw32(cpu_base + CPU_STATE, 0xffffffff);
7450 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007451 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007453 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007455 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456
7457 err = 0;
7458
7459out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460 return err;
7461}
7462
7463/* tp->lock is held. */
7464static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7465{
7466 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007467 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468 int err, i;
7469
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007470 fw_data = (void *)tp->fw->data;
7471
7472 /* Firmware blob starts with version numbers, followed by
7473 start address and length. We are setting complete length.
7474 length = end_address_of_bss - start_address_of_text.
7475 Remainder is the blob to be loaded contiguously
7476 from start address. */
7477
7478 info.fw_base = be32_to_cpu(fw_data[1]);
7479 info.fw_len = tp->fw->size - 12;
7480 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481
7482 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7483 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7484 &info);
7485 if (err)
7486 return err;
7487
7488 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7489 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7490 &info);
7491 if (err)
7492 return err;
7493
7494 /* Now startup only the RX cpu. */
7495 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007496 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497
7498 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007499 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500 break;
7501 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7502 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007503 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 udelay(1000);
7505 }
7506 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007507 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7508 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007509 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510 return -ENODEV;
7511 }
7512 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7513 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7514
7515 return 0;
7516}
7517
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518/* tp->lock is held. */
7519static int tg3_load_tso_firmware(struct tg3 *tp)
7520{
7521 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007522 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7524 int err, i;
7525
Joe Perches63c3a662011-04-26 08:12:10 +00007526 if (tg3_flag(tp, HW_TSO_1) ||
7527 tg3_flag(tp, HW_TSO_2) ||
7528 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007529 return 0;
7530
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007531 fw_data = (void *)tp->fw->data;
7532
7533 /* Firmware blob starts with version numbers, followed by
7534 start address and length. We are setting complete length.
7535 length = end_address_of_bss - start_address_of_text.
7536 Remainder is the blob to be loaded contiguously
7537 from start address. */
7538
7539 info.fw_base = be32_to_cpu(fw_data[1]);
7540 cpu_scratch_size = tp->fw_len;
7541 info.fw_len = tp->fw->size - 12;
7542 info.fw_data = &fw_data[3];
7543
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545 cpu_base = RX_CPU_BASE;
7546 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548 cpu_base = TX_CPU_BASE;
7549 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7550 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7551 }
7552
7553 err = tg3_load_firmware_cpu(tp, cpu_base,
7554 cpu_scratch_base, cpu_scratch_size,
7555 &info);
7556 if (err)
7557 return err;
7558
7559 /* Now startup the cpu. */
7560 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007561 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562
7563 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007564 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565 break;
7566 tw32(cpu_base + CPU_STATE, 0xffffffff);
7567 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007568 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 udelay(1000);
7570 }
7571 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007572 netdev_err(tp->dev,
7573 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007574 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575 return -ENODEV;
7576 }
7577 tw32(cpu_base + CPU_STATE, 0xffffffff);
7578 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7579 return 0;
7580}
7581
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583static int tg3_set_mac_addr(struct net_device *dev, void *p)
7584{
7585 struct tg3 *tp = netdev_priv(dev);
7586 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007587 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588
Michael Chanf9804dd2005-09-27 12:13:10 -07007589 if (!is_valid_ether_addr(addr->sa_data))
7590 return -EINVAL;
7591
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7593
Michael Chane75f7c92006-03-20 21:33:26 -08007594 if (!netif_running(dev))
7595 return 0;
7596
Joe Perches63c3a662011-04-26 08:12:10 +00007597 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007598 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007599
Michael Chan986e0ae2007-05-05 12:10:20 -07007600 addr0_high = tr32(MAC_ADDR_0_HIGH);
7601 addr0_low = tr32(MAC_ADDR_0_LOW);
7602 addr1_high = tr32(MAC_ADDR_1_HIGH);
7603 addr1_low = tr32(MAC_ADDR_1_LOW);
7604
7605 /* Skip MAC addr 1 if ASF is using it. */
7606 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7607 !(addr1_high == 0 && addr1_low == 0))
7608 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007609 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007610 spin_lock_bh(&tp->lock);
7611 __tg3_set_mac_addr(tp, skip_mac_1);
7612 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613
Michael Chanb9ec6c12006-07-25 16:37:27 -07007614 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615}
7616
7617/* tp->lock is held. */
7618static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7619 dma_addr_t mapping, u32 maxlen_flags,
7620 u32 nic_addr)
7621{
7622 tg3_write_mem(tp,
7623 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7624 ((u64) mapping >> 32));
7625 tg3_write_mem(tp,
7626 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7627 ((u64) mapping & 0xffffffff));
7628 tg3_write_mem(tp,
7629 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7630 maxlen_flags);
7631
Joe Perches63c3a662011-04-26 08:12:10 +00007632 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633 tg3_write_mem(tp,
7634 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7635 nic_addr);
7636}
7637
7638static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007639static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007640{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007641 int i;
7642
Joe Perches63c3a662011-04-26 08:12:10 +00007643 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007644 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7645 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7646 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007647 } else {
7648 tw32(HOSTCC_TXCOL_TICKS, 0);
7649 tw32(HOSTCC_TXMAX_FRAMES, 0);
7650 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007651 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007652
Joe Perches63c3a662011-04-26 08:12:10 +00007653 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007654 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7655 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7656 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7657 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007658 tw32(HOSTCC_RXCOL_TICKS, 0);
7659 tw32(HOSTCC_RXMAX_FRAMES, 0);
7660 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007661 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007662
Joe Perches63c3a662011-04-26 08:12:10 +00007663 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07007664 u32 val = ec->stats_block_coalesce_usecs;
7665
Matt Carlsonb6080e12009-09-01 13:12:00 +00007666 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7667 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7668
David S. Miller15f98502005-05-18 22:49:26 -07007669 if (!netif_carrier_ok(tp->dev))
7670 val = 0;
7671
7672 tw32(HOSTCC_STAT_COAL_TICKS, val);
7673 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007674
7675 for (i = 0; i < tp->irq_cnt - 1; i++) {
7676 u32 reg;
7677
7678 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7679 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007680 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7681 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007682 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7683 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007684
Joe Perches63c3a662011-04-26 08:12:10 +00007685 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007686 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7687 tw32(reg, ec->tx_coalesce_usecs);
7688 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7689 tw32(reg, ec->tx_max_coalesced_frames);
7690 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7691 tw32(reg, ec->tx_max_coalesced_frames_irq);
7692 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007693 }
7694
7695 for (; i < tp->irq_max - 1; i++) {
7696 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007697 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007698 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007699
Joe Perches63c3a662011-04-26 08:12:10 +00007700 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007701 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7702 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7703 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7704 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007705 }
David S. Miller15f98502005-05-18 22:49:26 -07007706}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007707
7708/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007709static void tg3_rings_reset(struct tg3 *tp)
7710{
7711 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007712 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007713 struct tg3_napi *tnapi = &tp->napi[0];
7714
7715 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007716 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007717 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00007718 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00007719 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007720 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7721 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007722 else
7723 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7724
7725 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7726 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7727 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7728 BDINFO_FLAGS_DISABLED);
7729
7730
7731 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007732 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007733 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00007734 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007735 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007736 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7737 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007738 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7739 else
7740 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7741
7742 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7743 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7744 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7745 BDINFO_FLAGS_DISABLED);
7746
7747 /* Disable interrupts */
7748 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7749
7750 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00007751 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007752 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007753 tp->napi[i].tx_prod = 0;
7754 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007755 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007756 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007757 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7758 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7759 }
Joe Perches63c3a662011-04-26 08:12:10 +00007760 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007761 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007762 } else {
7763 tp->napi[0].tx_prod = 0;
7764 tp->napi[0].tx_cons = 0;
7765 tw32_mailbox(tp->napi[0].prodmbox, 0);
7766 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7767 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007768
7769 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00007770 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00007771 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7772 for (i = 0; i < 16; i++)
7773 tw32_tx_mbox(mbox + i * 8, 0);
7774 }
7775
7776 txrcb = NIC_SRAM_SEND_RCB;
7777 rxrcb = NIC_SRAM_RCV_RET_RCB;
7778
7779 /* Clear status block in ram. */
7780 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7781
7782 /* Set status block DMA address */
7783 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7784 ((u64) tnapi->status_mapping >> 32));
7785 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7786 ((u64) tnapi->status_mapping & 0xffffffff));
7787
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007788 if (tnapi->tx_ring) {
7789 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7790 (TG3_TX_RING_SIZE <<
7791 BDINFO_FLAGS_MAXLEN_SHIFT),
7792 NIC_SRAM_TX_BUFFER_DESC);
7793 txrcb += TG3_BDINFO_SIZE;
7794 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007795
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007796 if (tnapi->rx_rcb) {
7797 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007798 (tp->rx_ret_ring_mask + 1) <<
7799 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007800 rxrcb += TG3_BDINFO_SIZE;
7801 }
7802
7803 stblk = HOSTCC_STATBLCK_RING1;
7804
7805 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7806 u64 mapping = (u64)tnapi->status_mapping;
7807 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7808 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7809
7810 /* Clear status block in ram. */
7811 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7812
Matt Carlson19cfaec2009-12-03 08:36:20 +00007813 if (tnapi->tx_ring) {
7814 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7815 (TG3_TX_RING_SIZE <<
7816 BDINFO_FLAGS_MAXLEN_SHIFT),
7817 NIC_SRAM_TX_BUFFER_DESC);
7818 txrcb += TG3_BDINFO_SIZE;
7819 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007820
7821 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007822 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007823 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7824
7825 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007826 rxrcb += TG3_BDINFO_SIZE;
7827 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007828}
7829
Matt Carlsoneb07a942011-04-20 07:57:36 +00007830static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
7831{
7832 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
7833
Joe Perches63c3a662011-04-26 08:12:10 +00007834 if (!tg3_flag(tp, 5750_PLUS) ||
7835 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00007836 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
7837 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7838 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
7839 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7840 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
7841 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
7842 else
7843 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
7844
7845 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
7846 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
7847
7848 val = min(nic_rep_thresh, host_rep_thresh);
7849 tw32(RCVBDI_STD_THRESH, val);
7850
Joe Perches63c3a662011-04-26 08:12:10 +00007851 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007852 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
7853
Joe Perches63c3a662011-04-26 08:12:10 +00007854 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007855 return;
7856
Joe Perches63c3a662011-04-26 08:12:10 +00007857 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007858 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
7859 else
7860 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
7861
7862 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
7863
7864 val = min(bdcache_maxcnt / 2, host_rep_thresh);
7865 tw32(RCVBDI_JUMBO_THRESH, val);
7866
Joe Perches63c3a662011-04-26 08:12:10 +00007867 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007868 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
7869}
7870
Matt Carlson2d31eca2009-09-01 12:53:31 +00007871/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007872static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873{
7874 u32 val, rdmac_mode;
7875 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00007876 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877
7878 tg3_disable_ints(tp);
7879
7880 tg3_stop_fw(tp);
7881
7882 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7883
Joe Perches63c3a662011-04-26 08:12:10 +00007884 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07007885 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007886
Matt Carlson699c0192010-12-06 08:28:51 +00007887 /* Enable MAC control of LPI */
7888 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
7889 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
7890 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
7891 TG3_CPMU_EEE_LNKIDL_UART_IDL);
7892
7893 tw32_f(TG3_CPMU_EEE_CTRL,
7894 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
7895
Matt Carlsona386b902010-12-06 08:28:53 +00007896 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
7897 TG3_CPMU_EEEMD_LPI_IN_TX |
7898 TG3_CPMU_EEEMD_LPI_IN_RX |
7899 TG3_CPMU_EEEMD_EEE_ENABLE;
7900
7901 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
7902 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
7903
Joe Perches63c3a662011-04-26 08:12:10 +00007904 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00007905 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
7906
7907 tw32_f(TG3_CPMU_EEE_MODE, val);
7908
7909 tw32_f(TG3_CPMU_EEE_DBTMR1,
7910 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
7911 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
7912
7913 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00007914 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00007915 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00007916 }
7917
Matt Carlson603f1172010-02-12 14:47:10 +00007918 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007919 tg3_phy_reset(tp);
7920
Linus Torvalds1da177e2005-04-16 15:20:36 -07007921 err = tg3_chip_reset(tp);
7922 if (err)
7923 return err;
7924
7925 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7926
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007927 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007928 val = tr32(TG3_CPMU_CTRL);
7929 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7930 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007931
7932 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7933 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7934 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7935 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7936
7937 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7938 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7939 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7940 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7941
7942 val = tr32(TG3_CPMU_HST_ACC);
7943 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7944 val |= CPMU_HST_ACC_MACCLK_6_25;
7945 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007946 }
7947
Matt Carlson33466d92009-04-20 06:57:41 +00007948 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7949 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7950 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7951 PCIE_PWR_MGMT_L1_THRESH_4MS;
7952 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007953
7954 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7955 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7956
7957 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007958
Matt Carlsonf40386c2009-11-02 14:24:02 +00007959 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7960 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007961 }
7962
Joe Perches63c3a662011-04-26 08:12:10 +00007963 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00007964 u32 grc_mode = tr32(GRC_MODE);
7965
7966 /* Access the lower 1K of PL PCIE block registers. */
7967 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7968 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7969
7970 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7971 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7972 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7973
7974 tw32(GRC_MODE, grc_mode);
7975 }
7976
Matt Carlson5093eed2010-11-24 08:31:45 +00007977 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
7978 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7979 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00007980
Matt Carlson5093eed2010-11-24 08:31:45 +00007981 /* Access the lower 1K of PL PCIE block registers. */
7982 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7983 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00007984
Matt Carlson5093eed2010-11-24 08:31:45 +00007985 val = tr32(TG3_PCIE_TLDLPL_PORT +
7986 TG3_PCIE_PL_LO_PHYCTL5);
7987 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
7988 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00007989
Matt Carlson5093eed2010-11-24 08:31:45 +00007990 tw32(GRC_MODE, grc_mode);
7991 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00007992
Matt Carlson1ff30a52011-05-19 12:12:46 +00007993 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
7994 u32 grc_mode = tr32(GRC_MODE);
7995
7996 /* Access the lower 1K of DL PCIE block registers. */
7997 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7998 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
7999
8000 val = tr32(TG3_PCIE_TLDLPL_PORT +
8001 TG3_PCIE_DL_LO_FTSMAX);
8002 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8003 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8004 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8005
8006 tw32(GRC_MODE, grc_mode);
8007 }
8008
Matt Carlsona977dbe2010-04-12 06:58:26 +00008009 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8010 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8011 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8012 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008013 }
8014
Linus Torvalds1da177e2005-04-16 15:20:36 -07008015 /* This works around an issue with Athlon chipsets on
8016 * B3 tigon3 silicon. This bit has no effect on any
8017 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008018 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008019 */
Joe Perches63c3a662011-04-26 08:12:10 +00008020 if (!tg3_flag(tp, CPMU_PRESENT)) {
8021 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008022 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8023 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008025
8026 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008027 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008028 val = tr32(TG3PCI_PCISTATE);
8029 val |= PCISTATE_RETRY_SAME_DMA;
8030 tw32(TG3PCI_PCISTATE, val);
8031 }
8032
Joe Perches63c3a662011-04-26 08:12:10 +00008033 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008034 /* Allow reads and writes to the
8035 * APE register and memory space.
8036 */
8037 val = tr32(TG3PCI_PCISTATE);
8038 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00008039 PCISTATE_ALLOW_APE_SHMEM_WR |
8040 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008041 tw32(TG3PCI_PCISTATE, val);
8042 }
8043
Linus Torvalds1da177e2005-04-16 15:20:36 -07008044 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8045 /* Enable some hw fixes. */
8046 val = tr32(TG3PCI_MSI_DATA);
8047 val |= (1 << 26) | (1 << 28) | (1 << 29);
8048 tw32(TG3PCI_MSI_DATA, val);
8049 }
8050
8051 /* Descriptor ring init may make accesses to the
8052 * NIC SRAM area to setup the TX descriptors, so we
8053 * can only do this after the hardware has been
8054 * successfully reset.
8055 */
Michael Chan32d8c572006-07-25 16:38:29 -07008056 err = tg3_init_rings(tp);
8057 if (err)
8058 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059
Joe Perches63c3a662011-04-26 08:12:10 +00008060 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008061 val = tr32(TG3PCI_DMA_RW_CTRL) &
8062 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008063 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8064 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008065 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8066 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8067 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008068 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8069 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8070 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008071 /* This value is determined during the probe time DMA
8072 * engine test, tg3_test_dma.
8073 */
8074 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008076
8077 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8078 GRC_MODE_4X_NIC_SEND_RINGS |
8079 GRC_MODE_NO_TX_PHDR_CSUM |
8080 GRC_MODE_NO_RX_PHDR_CSUM);
8081 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008082
8083 /* Pseudo-header checksum is done by hardware logic and not
8084 * the offload processers, so make the chip do the pseudo-
8085 * header checksums on receive. For transmit it is more
8086 * convenient to do the pseudo-header checksum in software
8087 * as Linux does that on transmit for us in all cases.
8088 */
8089 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008090
8091 tw32(GRC_MODE,
8092 tp->grc_mode |
8093 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8094
8095 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8096 val = tr32(GRC_MISC_CFG);
8097 val &= ~0xff;
8098 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8099 tw32(GRC_MISC_CFG, val);
8100
8101 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008102 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008103 /* Do nothing. */
8104 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8105 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8106 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8107 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8108 else
8109 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8110 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8111 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008112 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008113 int fw_len;
8114
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008115 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008116 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8117 tw32(BUFMGR_MB_POOL_ADDR,
8118 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8119 tw32(BUFMGR_MB_POOL_SIZE,
8120 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008122
Michael Chan0f893dc2005-07-25 12:30:38 -07008123 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008124 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8125 tp->bufmgr_config.mbuf_read_dma_low_water);
8126 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8127 tp->bufmgr_config.mbuf_mac_rx_low_water);
8128 tw32(BUFMGR_MB_HIGH_WATER,
8129 tp->bufmgr_config.mbuf_high_water);
8130 } else {
8131 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8132 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8133 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8134 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8135 tw32(BUFMGR_MB_HIGH_WATER,
8136 tp->bufmgr_config.mbuf_high_water_jumbo);
8137 }
8138 tw32(BUFMGR_DMA_LOW_WATER,
8139 tp->bufmgr_config.dma_low_water);
8140 tw32(BUFMGR_DMA_HIGH_WATER,
8141 tp->bufmgr_config.dma_high_water);
8142
Matt Carlsond309a462010-09-30 10:34:31 +00008143 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8144 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8145 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008146 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8147 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8148 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8149 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008150 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008151 for (i = 0; i < 2000; i++) {
8152 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8153 break;
8154 udelay(10);
8155 }
8156 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008157 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008158 return -ENODEV;
8159 }
8160
Matt Carlsoneb07a942011-04-20 07:57:36 +00008161 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8162 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008163
Matt Carlsoneb07a942011-04-20 07:57:36 +00008164 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008165
8166 /* Initialize TG3_BDINFO's at:
8167 * RCVDBDI_STD_BD: standard eth size rx ring
8168 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8169 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8170 *
8171 * like so:
8172 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8173 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8174 * ring attribute flags
8175 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8176 *
8177 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8178 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8179 *
8180 * The size of each ring is fixed in the firmware, but the location is
8181 * configurable.
8182 */
8183 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008184 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008185 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008186 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008187 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008188 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8189 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008190
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008191 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008192 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008193 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8194 BDINFO_FLAGS_DISABLED);
8195
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008196 /* Program the jumbo buffer descriptor ring control
8197 * blocks on those devices that have them.
8198 */
Matt Carlsonbb18bb92011-03-09 16:58:19 +00008199 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008200 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008201
Joe Perches63c3a662011-04-26 08:12:10 +00008202 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008203 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008204 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008205 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008206 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008207 val = TG3_RX_JMB_RING_SIZE(tp) <<
8208 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008209 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008210 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008211 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008212 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008213 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8214 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008215 } else {
8216 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8217 BDINFO_FLAGS_DISABLED);
8218 }
8219
Joe Perches63c3a662011-04-26 08:12:10 +00008220 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008221 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008222 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008223 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008224 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008225 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8226 val |= (TG3_RX_STD_DMA_SZ << 2);
8227 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008228 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008229 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008230 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008231
8232 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008233
Matt Carlson411da642009-11-13 13:03:46 +00008234 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008235 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008236
Joe Perches63c3a662011-04-26 08:12:10 +00008237 tpr->rx_jmb_prod_idx =
8238 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008239 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008240
Matt Carlson2d31eca2009-09-01 12:53:31 +00008241 tg3_rings_reset(tp);
8242
Linus Torvalds1da177e2005-04-16 15:20:36 -07008243 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008244 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008245
8246 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008247 tw32(MAC_RX_MTU_SIZE,
8248 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008249
8250 /* The slot time is changed by tg3_setup_phy if we
8251 * run at gigabit with half duplex.
8252 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008253 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8254 (6 << TX_LENGTHS_IPG_SHIFT) |
8255 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8256
8257 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8258 val |= tr32(MAC_TX_LENGTHS) &
8259 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8260 TX_LENGTHS_CNT_DWN_VAL_MSK);
8261
8262 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008263
8264 /* Receive rules. */
8265 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8266 tw32(RCVLPC_CONFIG, 0x0181);
8267
8268 /* Calculate RDMAC_MODE setting early, we need it to determine
8269 * the RCVLPC_STATE_ENABLE mask.
8270 */
8271 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8272 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8273 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8274 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8275 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008276
Matt Carlsondeabaac2010-11-24 08:31:50 +00008277 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008278 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8279
Matt Carlson57e69832008-05-25 23:48:31 -07008280 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008281 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8282 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008283 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8284 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8285 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8286
Matt Carlsonc5908932011-03-09 16:58:25 +00008287 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8288 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008289 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008290 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008291 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8292 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008293 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008294 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8295 }
8296 }
8297
Joe Perches63c3a662011-04-26 08:12:10 +00008298 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008299 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8300
Joe Perches63c3a662011-04-26 08:12:10 +00008301 if (tg3_flag(tp, HW_TSO_1) ||
8302 tg3_flag(tp, HW_TSO_2) ||
8303 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008304 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8305
Matt Carlson108a6c12011-05-19 12:12:47 +00008306 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008307 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008308 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8309 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008310
Matt Carlsonf2096f92011-04-05 14:22:48 +00008311 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8312 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8313
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008314 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8315 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8316 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8317 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008318 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008319 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008320 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8321 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008322 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8323 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8324 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8325 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8326 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8327 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008328 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008329 tw32(TG3_RDMA_RSRVCTRL_REG,
8330 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8331 }
8332
Matt Carlsond78b59f2011-04-05 14:22:46 +00008333 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8334 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008335 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8336 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8337 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8338 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8339 }
8340
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008342 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008343 val = tr32(RCVLPC_STATS_ENABLE);
8344 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8345 tw32(RCVLPC_STATS_ENABLE, val);
8346 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008347 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008348 val = tr32(RCVLPC_STATS_ENABLE);
8349 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8350 tw32(RCVLPC_STATS_ENABLE, val);
8351 } else {
8352 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8353 }
8354 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8355 tw32(SNDDATAI_STATSENAB, 0xffffff);
8356 tw32(SNDDATAI_STATSCTRL,
8357 (SNDDATAI_SCTRL_ENABLE |
8358 SNDDATAI_SCTRL_FASTUPD));
8359
8360 /* Setup host coalescing engine. */
8361 tw32(HOSTCC_MODE, 0);
8362 for (i = 0; i < 2000; i++) {
8363 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8364 break;
8365 udelay(10);
8366 }
8367
Michael Chand244c892005-07-05 14:42:33 -07008368 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008369
Joe Perches63c3a662011-04-26 08:12:10 +00008370 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008371 /* Status/statistics block address. See tg3_timer,
8372 * the tg3_periodic_fetch_stats call there, and
8373 * tg3_get_stats to see how this works for 5705/5750 chips.
8374 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008375 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8376 ((u64) tp->stats_mapping >> 32));
8377 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8378 ((u64) tp->stats_mapping & 0xffffffff));
8379 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008380
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008382
8383 /* Clear statistics and status block memory areas */
8384 for (i = NIC_SRAM_STATS_BLK;
8385 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8386 i += sizeof(u32)) {
8387 tg3_write_mem(tp, i, 0);
8388 udelay(40);
8389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008390 }
8391
8392 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8393
8394 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8395 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008396 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008397 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8398
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008399 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8400 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008401 /* reset to prevent losing 1st rx packet intermittently */
8402 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8403 udelay(10);
8404 }
8405
Matt Carlson3bda1252008-08-15 14:08:22 -07008406 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlsonb7d4d462011-07-20 10:20:50 +00008407 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
8408 MAC_MODE_FHDE_ENABLE;
8409 if (tg3_flag(tp, ENABLE_APE))
8410 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00008411 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008412 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008413 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8414 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8416 udelay(40);
8417
Michael Chan314fba32005-04-21 17:07:04 -07008418 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008419 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008420 * register to preserve the GPIO settings for LOMs. The GPIOs,
8421 * whether used as inputs or outputs, are set by boot code after
8422 * reset.
8423 */
Joe Perches63c3a662011-04-26 08:12:10 +00008424 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008425 u32 gpio_mask;
8426
Michael Chan9d26e212006-12-07 00:21:14 -08008427 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8428 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8429 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008430
8431 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8432 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8433 GRC_LCLCTRL_GPIO_OUTPUT3;
8434
Michael Chanaf36e6b2006-03-23 01:28:06 -08008435 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8436 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8437
Gary Zambranoaaf84462007-05-05 11:51:45 -07008438 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008439 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8440
8441 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008442 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008443 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8444 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008446 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8447 udelay(100);
8448
Joe Perches63c3a662011-04-26 08:12:10 +00008449 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008450 val = tr32(MSGINT_MODE);
8451 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8452 tw32(MSGINT_MODE, val);
8453 }
8454
Joe Perches63c3a662011-04-26 08:12:10 +00008455 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008456 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8457 udelay(40);
8458 }
8459
8460 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8461 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8462 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8463 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8464 WDMAC_MODE_LNGREAD_ENAB);
8465
Matt Carlsonc5908932011-03-09 16:58:25 +00008466 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8467 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008468 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008469 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8470 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8471 /* nothing */
8472 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008473 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008474 val |= WDMAC_MODE_RX_ACCEL;
8475 }
8476 }
8477
Michael Chand9ab5ad2006-03-20 22:27:35 -08008478 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008479 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008480 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008481
Matt Carlson788a0352009-11-02 14:26:03 +00008482 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8483 val |= WDMAC_MODE_BURST_ALL_DATA;
8484
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485 tw32_f(WDMAC_MODE, val);
8486 udelay(40);
8487
Joe Perches63c3a662011-04-26 08:12:10 +00008488 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008489 u16 pcix_cmd;
8490
8491 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8492 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008493 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008494 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8495 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008496 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008497 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8498 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008499 }
Matt Carlson9974a352007-10-07 23:27:28 -07008500 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8501 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008502 }
8503
8504 tw32_f(RDMAC_MODE, rdmac_mode);
8505 udelay(40);
8506
8507 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008508 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008509 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008510
8511 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8512 tw32(SNDDATAC_MODE,
8513 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8514 else
8515 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8516
Linus Torvalds1da177e2005-04-16 15:20:36 -07008517 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8518 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008519 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008520 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008521 val |= RCVDBDI_MODE_LRG_RING_SZ;
8522 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008524 if (tg3_flag(tp, HW_TSO_1) ||
8525 tg3_flag(tp, HW_TSO_2) ||
8526 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008527 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008528 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008529 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008530 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8531 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008532 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8533
8534 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8535 err = tg3_load_5701_a0_firmware_fix(tp);
8536 if (err)
8537 return err;
8538 }
8539
Joe Perches63c3a662011-04-26 08:12:10 +00008540 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008541 err = tg3_load_tso_firmware(tp);
8542 if (err)
8543 return err;
8544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008545
8546 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008547
Joe Perches63c3a662011-04-26 08:12:10 +00008548 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008549 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8550 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008551
8552 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8553 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8554 tp->tx_mode &= ~val;
8555 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8556 }
8557
Linus Torvalds1da177e2005-04-16 15:20:36 -07008558 tw32_f(MAC_TX_MODE, tp->tx_mode);
8559 udelay(100);
8560
Joe Perches63c3a662011-04-26 08:12:10 +00008561 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008562 u32 reg = MAC_RSS_INDIR_TBL_0;
8563 u8 *ent = (u8 *)&val;
8564
8565 /* Setup the indirection table */
8566 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8567 int idx = i % sizeof(val);
8568
Matt Carlson5efeeea2010-07-11 09:31:40 +00008569 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008570 if (idx == sizeof(val) - 1) {
8571 tw32(reg, val);
8572 reg += 4;
8573 }
8574 }
8575
8576 /* Setup the "secret" hash key. */
8577 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8578 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8579 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8580 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8581 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8582 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8583 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8584 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8585 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8586 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8587 }
8588
Linus Torvalds1da177e2005-04-16 15:20:36 -07008589 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008590 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008591 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8592
Joe Perches63c3a662011-04-26 08:12:10 +00008593 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008594 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8595 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8596 RX_MODE_RSS_IPV6_HASH_EN |
8597 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8598 RX_MODE_RSS_IPV4_HASH_EN |
8599 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8600
Linus Torvalds1da177e2005-04-16 15:20:36 -07008601 tw32_f(MAC_RX_MODE, tp->rx_mode);
8602 udelay(10);
8603
Linus Torvalds1da177e2005-04-16 15:20:36 -07008604 tw32(MAC_LED_CTRL, tp->led_ctrl);
8605
8606 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008607 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008608 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8609 udelay(10);
8610 }
8611 tw32_f(MAC_RX_MODE, tp->rx_mode);
8612 udelay(10);
8613
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008614 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008615 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008616 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008617 /* Set drive transmission level to 1.2V */
8618 /* only if the signal pre-emphasis bit is not set */
8619 val = tr32(MAC_SERDES_CFG);
8620 val &= 0xfffff000;
8621 val |= 0x880;
8622 tw32(MAC_SERDES_CFG, val);
8623 }
8624 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8625 tw32(MAC_SERDES_CFG, 0x616000);
8626 }
8627
8628 /* Prevent chip from dropping frames when flow control
8629 * is enabled.
8630 */
Matt Carlson666bc832010-01-20 16:58:03 +00008631 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8632 val = 1;
8633 else
8634 val = 2;
8635 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008636
8637 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008638 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008639 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00008640 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008641 }
8642
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008643 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00008644 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08008645 u32 tmp;
8646
8647 tmp = tr32(SERDES_RX_CTRL);
8648 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8649 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8650 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8651 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8652 }
8653
Joe Perches63c3a662011-04-26 08:12:10 +00008654 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00008655 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8656 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008657 tp->link_config.speed = tp->link_config.orig_speed;
8658 tp->link_config.duplex = tp->link_config.orig_duplex;
8659 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008661
Matt Carlsondd477002008-05-25 23:45:58 -07008662 err = tg3_setup_phy(tp, 0);
8663 if (err)
8664 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008665
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008666 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8667 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008668 u32 tmp;
8669
8670 /* Clear CRC stats. */
8671 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8672 tg3_writephy(tp, MII_TG3_TEST1,
8673 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008674 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008676 }
8677 }
8678
8679 __tg3_set_rx_mode(tp->dev);
8680
8681 /* Initialize receive rules. */
8682 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8683 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8684 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8685 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8686
Joe Perches63c3a662011-04-26 08:12:10 +00008687 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008688 limit = 8;
8689 else
8690 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008691 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008692 limit -= 4;
8693 switch (limit) {
8694 case 16:
8695 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8696 case 15:
8697 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8698 case 14:
8699 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8700 case 13:
8701 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8702 case 12:
8703 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8704 case 11:
8705 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8706 case 10:
8707 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8708 case 9:
8709 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8710 case 8:
8711 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8712 case 7:
8713 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8714 case 6:
8715 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8716 case 5:
8717 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8718 case 4:
8719 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8720 case 3:
8721 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8722 case 2:
8723 case 1:
8724
8725 default:
8726 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008728
Joe Perches63c3a662011-04-26 08:12:10 +00008729 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07008730 /* Write our heartbeat update interval to APE. */
8731 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8732 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008733
Linus Torvalds1da177e2005-04-16 15:20:36 -07008734 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8735
Linus Torvalds1da177e2005-04-16 15:20:36 -07008736 return 0;
8737}
8738
8739/* Called at device open time to get the chip ready for
8740 * packet processing. Invoked with tp->lock held.
8741 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008742static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008743{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008744 tg3_switch_clocks(tp);
8745
8746 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8747
Matt Carlson2f751b62008-08-04 23:17:34 -07008748 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008749}
8750
8751#define TG3_STAT_ADD32(PSTAT, REG) \
8752do { u32 __val = tr32(REG); \
8753 (PSTAT)->low += __val; \
8754 if ((PSTAT)->low < __val) \
8755 (PSTAT)->high += 1; \
8756} while (0)
8757
8758static void tg3_periodic_fetch_stats(struct tg3 *tp)
8759{
8760 struct tg3_hw_stats *sp = tp->hw_stats;
8761
8762 if (!netif_carrier_ok(tp->dev))
8763 return;
8764
8765 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8766 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8767 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8768 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8769 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8770 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8771 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8772 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8773 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8774 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8775 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8776 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8777 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8778
8779 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8780 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8781 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8782 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8783 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8784 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8785 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8786 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8787 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8788 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8789 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8790 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8791 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8792 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008793
8794 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00008795 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
8796 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
8797 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00008798 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8799 } else {
8800 u32 val = tr32(HOSTCC_FLOW_ATTN);
8801 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
8802 if (val) {
8803 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
8804 sp->rx_discards.low += val;
8805 if (sp->rx_discards.low < val)
8806 sp->rx_discards.high += 1;
8807 }
8808 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
8809 }
Michael Chan463d3052006-05-22 16:36:27 -07008810 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008811}
8812
8813static void tg3_timer(unsigned long __opaque)
8814{
8815 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008816
Michael Chanf475f162006-03-27 23:20:14 -08008817 if (tp->irq_sync)
8818 goto restart_timer;
8819
David S. Millerf47c11e2005-06-24 20:18:35 -07008820 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008821
Joe Perches63c3a662011-04-26 08:12:10 +00008822 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07008823 /* All of this garbage is because when using non-tagged
8824 * IRQ status the mailbox/status_block protocol the chip
8825 * uses with the cpu is race prone.
8826 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008827 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008828 tw32(GRC_LOCAL_CTRL,
8829 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8830 } else {
8831 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008832 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008834
David S. Millerfac9b832005-05-18 22:46:34 -07008835 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +00008836 tg3_flag_set(tp, RESTART_TIMER);
David S. Millerf47c11e2005-06-24 20:18:35 -07008837 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008838 schedule_work(&tp->reset_task);
8839 return;
8840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008841 }
8842
Linus Torvalds1da177e2005-04-16 15:20:36 -07008843 /* This part only runs once per second. */
8844 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00008845 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07008846 tg3_periodic_fetch_stats(tp);
8847
Matt Carlsonb0c59432011-05-19 12:12:48 +00008848 if (tp->setlpicnt && !--tp->setlpicnt)
8849 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00008850
Joe Perches63c3a662011-04-26 08:12:10 +00008851 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008852 u32 mac_stat;
8853 int phy_event;
8854
8855 mac_stat = tr32(MAC_STATUS);
8856
8857 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008858 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008859 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8860 phy_event = 1;
8861 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8862 phy_event = 1;
8863
8864 if (phy_event)
8865 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00008866 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008867 u32 mac_stat = tr32(MAC_STATUS);
8868 int need_setup = 0;
8869
8870 if (netif_carrier_ok(tp->dev) &&
8871 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8872 need_setup = 1;
8873 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008874 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008875 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8876 MAC_STATUS_SIGNAL_DET))) {
8877 need_setup = 1;
8878 }
8879 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008880 if (!tp->serdes_counter) {
8881 tw32_f(MAC_MODE,
8882 (tp->mac_mode &
8883 ~MAC_MODE_PORT_MODE_MASK));
8884 udelay(40);
8885 tw32_f(MAC_MODE, tp->mac_mode);
8886 udelay(40);
8887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008888 tg3_setup_phy(tp, 0);
8889 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008890 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008891 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008892 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008894
8895 tp->timer_counter = tp->timer_multiplier;
8896 }
8897
Michael Chan130b8e42006-09-27 16:00:40 -07008898 /* Heartbeat is only sent once every 2 seconds.
8899 *
8900 * The heartbeat is to tell the ASF firmware that the host
8901 * driver is still alive. In the event that the OS crashes,
8902 * ASF needs to reset the hardware to free up the FIFO space
8903 * that may be filled with rx packets destined for the host.
8904 * If the FIFO is full, ASF will no longer function properly.
8905 *
8906 * Unintended resets have been reported on real time kernels
8907 * where the timer doesn't run on time. Netpoll will also have
8908 * same problem.
8909 *
8910 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8911 * to check the ring condition when the heartbeat is expiring
8912 * before doing the reset. This will prevent most unintended
8913 * resets.
8914 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008915 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00008916 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008917 tg3_wait_for_event_ack(tp);
8918
Michael Chanbbadf502006-04-06 21:46:34 -07008919 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008920 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008921 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008922 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8923 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008924
8925 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008926 }
8927 tp->asf_counter = tp->asf_multiplier;
8928 }
8929
David S. Millerf47c11e2005-06-24 20:18:35 -07008930 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008931
Michael Chanf475f162006-03-27 23:20:14 -08008932restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008933 tp->timer.expires = jiffies + tp->timer_offset;
8934 add_timer(&tp->timer);
8935}
8936
Matt Carlson4f125f42009-09-01 12:55:02 +00008937static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008938{
David Howells7d12e782006-10-05 14:55:46 +01008939 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008940 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008941 char *name;
8942 struct tg3_napi *tnapi = &tp->napi[irq_num];
8943
8944 if (tp->irq_cnt == 1)
8945 name = tp->dev->name;
8946 else {
8947 name = &tnapi->irq_lbl[0];
8948 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8949 name[IFNAMSIZ-1] = 0;
8950 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008951
Joe Perches63c3a662011-04-26 08:12:10 +00008952 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008953 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00008954 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08008955 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00008956 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008957 } else {
8958 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00008959 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08008960 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00008961 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008962 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008963
8964 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008965}
8966
Michael Chan79381092005-04-21 17:13:59 -07008967static int tg3_test_interrupt(struct tg3 *tp)
8968{
Matt Carlson09943a12009-08-28 14:01:57 +00008969 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008970 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008971 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008972 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008973
Michael Chand4bc3922005-05-29 14:59:20 -07008974 if (!netif_running(dev))
8975 return -ENODEV;
8976
Michael Chan79381092005-04-21 17:13:59 -07008977 tg3_disable_ints(tp);
8978
Matt Carlson4f125f42009-09-01 12:55:02 +00008979 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008980
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008981 /*
8982 * Turn off MSI one shot mode. Otherwise this test has no
8983 * observable way to know whether the interrupt was delivered.
8984 */
Joe Perches63c3a662011-04-26 08:12:10 +00008985 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008986 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8987 tw32(MSGINT_MODE, val);
8988 }
8989
Matt Carlson4f125f42009-09-01 12:55:02 +00008990 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008991 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008992 if (err)
8993 return err;
8994
Matt Carlson898a56f2009-08-28 14:02:40 +00008995 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008996 tg3_enable_ints(tp);
8997
8998 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008999 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009000
9001 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009002 u32 int_mbox, misc_host_ctrl;
9003
Matt Carlson898a56f2009-08-28 14:02:40 +00009004 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009005 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9006
9007 if ((int_mbox != 0) ||
9008 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9009 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009010 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009011 }
9012
Michael Chan79381092005-04-21 17:13:59 -07009013 msleep(10);
9014 }
9015
9016 tg3_disable_ints(tp);
9017
Matt Carlson4f125f42009-09-01 12:55:02 +00009018 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009019
Matt Carlson4f125f42009-09-01 12:55:02 +00009020 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009021
9022 if (err)
9023 return err;
9024
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009025 if (intr_ok) {
9026 /* Reenable MSI one shot mode. */
Joe Perches63c3a662011-04-26 08:12:10 +00009027 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009028 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9029 tw32(MSGINT_MODE, val);
9030 }
Michael Chan79381092005-04-21 17:13:59 -07009031 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009032 }
Michael Chan79381092005-04-21 17:13:59 -07009033
9034 return -EIO;
9035}
9036
9037/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9038 * successfully restored
9039 */
9040static int tg3_test_msi(struct tg3 *tp)
9041{
Michael Chan79381092005-04-21 17:13:59 -07009042 int err;
9043 u16 pci_cmd;
9044
Joe Perches63c3a662011-04-26 08:12:10 +00009045 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009046 return 0;
9047
9048 /* Turn off SERR reporting in case MSI terminates with Master
9049 * Abort.
9050 */
9051 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9052 pci_write_config_word(tp->pdev, PCI_COMMAND,
9053 pci_cmd & ~PCI_COMMAND_SERR);
9054
9055 err = tg3_test_interrupt(tp);
9056
9057 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9058
9059 if (!err)
9060 return 0;
9061
9062 /* other failures */
9063 if (err != -EIO)
9064 return err;
9065
9066 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009067 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9068 "to INTx mode. Please report this failure to the PCI "
9069 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009070
Matt Carlson4f125f42009-09-01 12:55:02 +00009071 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009072
Michael Chan79381092005-04-21 17:13:59 -07009073 pci_disable_msi(tp->pdev);
9074
Joe Perches63c3a662011-04-26 08:12:10 +00009075 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009076 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009077
Matt Carlson4f125f42009-09-01 12:55:02 +00009078 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009079 if (err)
9080 return err;
9081
9082 /* Need to reset the chip because the MSI cycle may have terminated
9083 * with Master Abort.
9084 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009085 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009086
Michael Chan944d9802005-05-29 14:57:48 -07009087 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009088 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009089
David S. Millerf47c11e2005-06-24 20:18:35 -07009090 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009091
9092 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009093 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009094
9095 return err;
9096}
9097
Matt Carlson9e9fd122009-01-19 16:57:45 -08009098static int tg3_request_firmware(struct tg3 *tp)
9099{
9100 const __be32 *fw_data;
9101
9102 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009103 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9104 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009105 return -ENOENT;
9106 }
9107
9108 fw_data = (void *)tp->fw->data;
9109
9110 /* Firmware blob starts with version numbers, followed by
9111 * start address and _full_ length including BSS sections
9112 * (which must be longer than the actual data, of course
9113 */
9114
9115 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9116 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009117 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9118 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009119 release_firmware(tp->fw);
9120 tp->fw = NULL;
9121 return -EINVAL;
9122 }
9123
9124 /* We no longer need firmware; we have it. */
9125 tp->fw_needed = NULL;
9126 return 0;
9127}
9128
Matt Carlson679563f2009-09-01 12:55:46 +00009129static bool tg3_enable_msix(struct tg3 *tp)
9130{
9131 int i, rc, cpus = num_online_cpus();
9132 struct msix_entry msix_ent[tp->irq_max];
9133
9134 if (cpus == 1)
9135 /* Just fallback to the simpler MSI mode. */
9136 return false;
9137
9138 /*
9139 * We want as many rx rings enabled as there are cpus.
9140 * The first MSIX vector only deals with link interrupts, etc,
9141 * so we add one to the number of vectors we are requesting.
9142 */
9143 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9144
9145 for (i = 0; i < tp->irq_max; i++) {
9146 msix_ent[i].entry = i;
9147 msix_ent[i].vector = 0;
9148 }
9149
9150 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009151 if (rc < 0) {
9152 return false;
9153 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009154 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9155 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009156 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9157 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009158 tp->irq_cnt = rc;
9159 }
9160
9161 for (i = 0; i < tp->irq_max; i++)
9162 tp->napi[i].irq_vec = msix_ent[i].vector;
9163
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009164 netif_set_real_num_tx_queues(tp->dev, 1);
9165 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9166 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9167 pci_disable_msix(tp->pdev);
9168 return false;
9169 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009170
9171 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009172 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009173
9174 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9175 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009176 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009177 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9178 }
9179 }
Matt Carlson2430b032010-06-05 17:24:34 +00009180
Matt Carlson679563f2009-09-01 12:55:46 +00009181 return true;
9182}
9183
Matt Carlson07b01732009-08-28 14:01:15 +00009184static void tg3_ints_init(struct tg3 *tp)
9185{
Joe Perches63c3a662011-04-26 08:12:10 +00009186 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9187 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009188 /* All MSI supporting chips should support tagged
9189 * status. Assert that this is the case.
9190 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009191 netdev_warn(tp->dev,
9192 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009193 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009194 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009195
Joe Perches63c3a662011-04-26 08:12:10 +00009196 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9197 tg3_flag_set(tp, USING_MSIX);
9198 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9199 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009200
Joe Perches63c3a662011-04-26 08:12:10 +00009201 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009202 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009203 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009204 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009205 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9206 }
9207defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009208 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009209 tp->irq_cnt = 1;
9210 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009211 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009212 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009213 }
Matt Carlson07b01732009-08-28 14:01:15 +00009214}
9215
9216static void tg3_ints_fini(struct tg3 *tp)
9217{
Joe Perches63c3a662011-04-26 08:12:10 +00009218 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009219 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009220 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009221 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009222 tg3_flag_clear(tp, USING_MSI);
9223 tg3_flag_clear(tp, USING_MSIX);
9224 tg3_flag_clear(tp, ENABLE_RSS);
9225 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009226}
9227
Linus Torvalds1da177e2005-04-16 15:20:36 -07009228static int tg3_open(struct net_device *dev)
9229{
9230 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009231 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009232
Matt Carlson9e9fd122009-01-19 16:57:45 -08009233 if (tp->fw_needed) {
9234 err = tg3_request_firmware(tp);
9235 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9236 if (err)
9237 return err;
9238 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009239 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009240 tg3_flag_clear(tp, TSO_CAPABLE);
9241 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009242 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009243 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009244 }
9245 }
9246
Michael Chanc49a1562006-12-17 17:07:29 -08009247 netif_carrier_off(tp->dev);
9248
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009249 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009250 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009251 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009252
9253 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009254
Linus Torvalds1da177e2005-04-16 15:20:36 -07009255 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009256 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009257
David S. Millerf47c11e2005-06-24 20:18:35 -07009258 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009259
Matt Carlson679563f2009-09-01 12:55:46 +00009260 /*
9261 * Setup interrupts first so we know how
9262 * many NAPI resources to allocate
9263 */
9264 tg3_ints_init(tp);
9265
Linus Torvalds1da177e2005-04-16 15:20:36 -07009266 /* The placement of this call is tied
9267 * to the setup and use of Host TX descriptors.
9268 */
9269 err = tg3_alloc_consistent(tp);
9270 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009271 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009272
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009273 tg3_napi_init(tp);
9274
Matt Carlsonfed97812009-09-01 13:10:19 +00009275 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009276
Matt Carlson4f125f42009-09-01 12:55:02 +00009277 for (i = 0; i < tp->irq_cnt; i++) {
9278 struct tg3_napi *tnapi = &tp->napi[i];
9279 err = tg3_request_irq(tp, i);
9280 if (err) {
9281 for (i--; i >= 0; i--)
9282 free_irq(tnapi->irq_vec, tnapi);
9283 break;
9284 }
9285 }
Matt Carlson07b01732009-08-28 14:01:15 +00009286
9287 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009288 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009289
David S. Millerf47c11e2005-06-24 20:18:35 -07009290 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009291
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009292 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009293 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009294 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009295 tg3_free_rings(tp);
9296 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00009297 if (tg3_flag(tp, TAGGED_STATUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009298 tp->timer_offset = HZ;
9299 else
9300 tp->timer_offset = HZ / 10;
9301
9302 BUG_ON(tp->timer_offset > HZ);
9303 tp->timer_counter = tp->timer_multiplier =
9304 (HZ / tp->timer_offset);
9305 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009306 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009307
9308 init_timer(&tp->timer);
9309 tp->timer.expires = jiffies + tp->timer_offset;
9310 tp->timer.data = (unsigned long) tp;
9311 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312 }
9313
David S. Millerf47c11e2005-06-24 20:18:35 -07009314 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009315
Matt Carlson07b01732009-08-28 14:01:15 +00009316 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009317 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009318
Joe Perches63c3a662011-04-26 08:12:10 +00009319 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009320 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009321
Michael Chan79381092005-04-21 17:13:59 -07009322 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009323 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009324 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009325 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009326 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009327
Matt Carlson679563f2009-09-01 12:55:46 +00009328 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009329 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009330
Joe Perches63c3a662011-04-26 08:12:10 +00009331 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009332 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009333
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009334 tw32(PCIE_TRANSACTION_CFG,
9335 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009336 }
Michael Chan79381092005-04-21 17:13:59 -07009337 }
9338
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009339 tg3_phy_start(tp);
9340
David S. Millerf47c11e2005-06-24 20:18:35 -07009341 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009342
Michael Chan79381092005-04-21 17:13:59 -07009343 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009344 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009345 tg3_enable_ints(tp);
9346
David S. Millerf47c11e2005-06-24 20:18:35 -07009347 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009348
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009349 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009350
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009351 /*
9352 * Reset loopback feature if it was turned on while the device was down
9353 * make sure that it's installed properly now.
9354 */
9355 if (dev->features & NETIF_F_LOOPBACK)
9356 tg3_set_loopback(dev, dev->features);
9357
Linus Torvalds1da177e2005-04-16 15:20:36 -07009358 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009359
Matt Carlson679563f2009-09-01 12:55:46 +00009360err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009361 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9362 struct tg3_napi *tnapi = &tp->napi[i];
9363 free_irq(tnapi->irq_vec, tnapi);
9364 }
Matt Carlson07b01732009-08-28 14:01:15 +00009365
Matt Carlson679563f2009-09-01 12:55:46 +00009366err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009367 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009368 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009369 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009370
9371err_out1:
9372 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009373 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009374}
9375
Eric Dumazet511d2222010-07-07 20:44:24 +00009376static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9377 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009378static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9379
9380static int tg3_close(struct net_device *dev)
9381{
Matt Carlson4f125f42009-09-01 12:55:02 +00009382 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009383 struct tg3 *tp = netdev_priv(dev);
9384
Matt Carlsonfed97812009-09-01 13:10:19 +00009385 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009386 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009387
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009388 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009389
9390 del_timer_sync(&tp->timer);
9391
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009392 tg3_phy_stop(tp);
9393
David S. Millerf47c11e2005-06-24 20:18:35 -07009394 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009395
9396 tg3_disable_ints(tp);
9397
Michael Chan944d9802005-05-29 14:57:48 -07009398 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009399 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009400 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009401
David S. Millerf47c11e2005-06-24 20:18:35 -07009402 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009403
Matt Carlson4f125f42009-09-01 12:55:02 +00009404 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9405 struct tg3_napi *tnapi = &tp->napi[i];
9406 free_irq(tnapi->irq_vec, tnapi);
9407 }
Matt Carlson07b01732009-08-28 14:01:15 +00009408
9409 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009410
Eric Dumazet511d2222010-07-07 20:44:24 +00009411 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9412
Linus Torvalds1da177e2005-04-16 15:20:36 -07009413 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9414 sizeof(tp->estats_prev));
9415
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009416 tg3_napi_fini(tp);
9417
Linus Torvalds1da177e2005-04-16 15:20:36 -07009418 tg3_free_consistent(tp);
9419
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009420 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009421
9422 netif_carrier_off(tp->dev);
9423
Linus Torvalds1da177e2005-04-16 15:20:36 -07009424 return 0;
9425}
9426
Eric Dumazet511d2222010-07-07 20:44:24 +00009427static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009428{
9429 return ((u64)val->high << 32) | ((u64)val->low);
9430}
9431
Eric Dumazet511d2222010-07-07 20:44:24 +00009432static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009433{
9434 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9435
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009436 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009437 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9438 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009439 u32 val;
9440
David S. Millerf47c11e2005-06-24 20:18:35 -07009441 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009442 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9443 tg3_writephy(tp, MII_TG3_TEST1,
9444 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009445 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009446 } else
9447 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009448 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009449
9450 tp->phy_crc_errors += val;
9451
9452 return tp->phy_crc_errors;
9453 }
9454
9455 return get_stat64(&hw_stats->rx_fcs_errors);
9456}
9457
9458#define ESTAT_ADD(member) \
9459 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009460 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009461
9462static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9463{
9464 struct tg3_ethtool_stats *estats = &tp->estats;
9465 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9466 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9467
9468 if (!hw_stats)
9469 return old_estats;
9470
9471 ESTAT_ADD(rx_octets);
9472 ESTAT_ADD(rx_fragments);
9473 ESTAT_ADD(rx_ucast_packets);
9474 ESTAT_ADD(rx_mcast_packets);
9475 ESTAT_ADD(rx_bcast_packets);
9476 ESTAT_ADD(rx_fcs_errors);
9477 ESTAT_ADD(rx_align_errors);
9478 ESTAT_ADD(rx_xon_pause_rcvd);
9479 ESTAT_ADD(rx_xoff_pause_rcvd);
9480 ESTAT_ADD(rx_mac_ctrl_rcvd);
9481 ESTAT_ADD(rx_xoff_entered);
9482 ESTAT_ADD(rx_frame_too_long_errors);
9483 ESTAT_ADD(rx_jabbers);
9484 ESTAT_ADD(rx_undersize_packets);
9485 ESTAT_ADD(rx_in_length_errors);
9486 ESTAT_ADD(rx_out_length_errors);
9487 ESTAT_ADD(rx_64_or_less_octet_packets);
9488 ESTAT_ADD(rx_65_to_127_octet_packets);
9489 ESTAT_ADD(rx_128_to_255_octet_packets);
9490 ESTAT_ADD(rx_256_to_511_octet_packets);
9491 ESTAT_ADD(rx_512_to_1023_octet_packets);
9492 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9493 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9494 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9495 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9496 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9497
9498 ESTAT_ADD(tx_octets);
9499 ESTAT_ADD(tx_collisions);
9500 ESTAT_ADD(tx_xon_sent);
9501 ESTAT_ADD(tx_xoff_sent);
9502 ESTAT_ADD(tx_flow_control);
9503 ESTAT_ADD(tx_mac_errors);
9504 ESTAT_ADD(tx_single_collisions);
9505 ESTAT_ADD(tx_mult_collisions);
9506 ESTAT_ADD(tx_deferred);
9507 ESTAT_ADD(tx_excessive_collisions);
9508 ESTAT_ADD(tx_late_collisions);
9509 ESTAT_ADD(tx_collide_2times);
9510 ESTAT_ADD(tx_collide_3times);
9511 ESTAT_ADD(tx_collide_4times);
9512 ESTAT_ADD(tx_collide_5times);
9513 ESTAT_ADD(tx_collide_6times);
9514 ESTAT_ADD(tx_collide_7times);
9515 ESTAT_ADD(tx_collide_8times);
9516 ESTAT_ADD(tx_collide_9times);
9517 ESTAT_ADD(tx_collide_10times);
9518 ESTAT_ADD(tx_collide_11times);
9519 ESTAT_ADD(tx_collide_12times);
9520 ESTAT_ADD(tx_collide_13times);
9521 ESTAT_ADD(tx_collide_14times);
9522 ESTAT_ADD(tx_collide_15times);
9523 ESTAT_ADD(tx_ucast_packets);
9524 ESTAT_ADD(tx_mcast_packets);
9525 ESTAT_ADD(tx_bcast_packets);
9526 ESTAT_ADD(tx_carrier_sense_errors);
9527 ESTAT_ADD(tx_discards);
9528 ESTAT_ADD(tx_errors);
9529
9530 ESTAT_ADD(dma_writeq_full);
9531 ESTAT_ADD(dma_write_prioq_full);
9532 ESTAT_ADD(rxbds_empty);
9533 ESTAT_ADD(rx_discards);
9534 ESTAT_ADD(rx_errors);
9535 ESTAT_ADD(rx_threshold_hit);
9536
9537 ESTAT_ADD(dma_readq_full);
9538 ESTAT_ADD(dma_read_prioq_full);
9539 ESTAT_ADD(tx_comp_queue_full);
9540
9541 ESTAT_ADD(ring_set_send_prod_index);
9542 ESTAT_ADD(ring_status_update);
9543 ESTAT_ADD(nic_irqs);
9544 ESTAT_ADD(nic_avoided_irqs);
9545 ESTAT_ADD(nic_tx_threshold_hit);
9546
Matt Carlson4452d092011-05-19 12:12:51 +00009547 ESTAT_ADD(mbuf_lwm_thresh_hit);
9548
Linus Torvalds1da177e2005-04-16 15:20:36 -07009549 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 Carlson859a5882010-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 Pirko22bedad2010-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 Pirko22bedad2010-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 Chan1c8594b2005-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 Chan1c8594b2005-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 Carlson859a5882010-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 Carlson859a5882010-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)
Matt Carlson54e0a672011-05-19 12:12:50 +000011198 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011199 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 Chand9ab5ad2006-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
Matt Carlson6ff6f812011-05-19 12:12:54 +000011636 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 Carlson859a5882010-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 Carlson859a5882010-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;
Matt Carlson6ff6f812011-05-19 12:12:54 +000012640 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 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012643 (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) &&
Matt Carlsonf7838b52011-07-20 10:20:53 +000012944 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
12945 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
12946 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000012947 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
12948 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12949 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000012950 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
12951
Matt Carlsone256f8a2011-03-09 16:58:24 +000012952 tg3_phy_init_link_config(tp);
12953
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012954 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012955 !tg3_flag(tp, ENABLE_APE) &&
12956 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000012957 u32 bmsr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012958
12959 tg3_readphy(tp, MII_BMSR, &bmsr);
12960 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12961 (bmsr & BMSR_LSTATUS))
12962 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012963
Linus Torvalds1da177e2005-04-16 15:20:36 -070012964 err = tg3_phy_reset(tp);
12965 if (err)
12966 return err;
12967
Matt Carlson42b64a42011-05-19 12:12:49 +000012968 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012969
Michael Chan3600d912006-12-07 00:21:48 -080012970 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12971 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12972 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12973 if (!tg3_copper_is_advertising_all(tp, mask)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000012974 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
12975 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012976
12977 tg3_writephy(tp, MII_BMCR,
12978 BMCR_ANENABLE | BMCR_ANRESTART);
12979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012980 }
12981
12982skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012983 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012984 err = tg3_init_5401phy_dsp(tp);
12985 if (err)
12986 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012987
Linus Torvalds1da177e2005-04-16 15:20:36 -070012988 err = tg3_init_5401phy_dsp(tp);
12989 }
12990
Linus Torvalds1da177e2005-04-16 15:20:36 -070012991 return err;
12992}
12993
Matt Carlson184b8902010-04-05 10:19:25 +000012994static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012995{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012996 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012997 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000012998 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012999
Matt Carlsonc3e94502011-04-13 11:05:08 +000013000 vpd_data = (u8 *)tg3_vpd_readblock(tp);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013001 if (!vpd_data)
13002 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013003
Matt Carlson4181b2c2010-02-26 14:04:45 +000013004 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
13005 PCI_VPD_LRDT_RO_DATA);
13006 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013007 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013008
13009 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13010 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13011 i += PCI_VPD_LRDT_TAG_SIZE;
13012
13013 if (block_end > TG3_NVM_VPD_LEN)
13014 goto out_not_found;
13015
Matt Carlson184b8902010-04-05 10:19:25 +000013016 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13017 PCI_VPD_RO_KEYWORD_MFR_ID);
13018 if (j > 0) {
13019 len = pci_vpd_info_field_size(&vpd_data[j]);
13020
13021 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13022 if (j + len > block_end || len != 4 ||
13023 memcmp(&vpd_data[j], "1028", 4))
13024 goto partno;
13025
13026 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13027 PCI_VPD_RO_KEYWORD_VENDOR0);
13028 if (j < 0)
13029 goto partno;
13030
13031 len = pci_vpd_info_field_size(&vpd_data[j]);
13032
13033 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13034 if (j + len > block_end)
13035 goto partno;
13036
13037 memcpy(tp->fw_ver, &vpd_data[j], len);
13038 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
13039 }
13040
13041partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013042 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13043 PCI_VPD_RO_KEYWORD_PARTNO);
13044 if (i < 0)
13045 goto out_not_found;
13046
13047 len = pci_vpd_info_field_size(&vpd_data[i]);
13048
13049 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13050 if (len > TG3_BPN_SIZE ||
13051 (len + i) > TG3_NVM_VPD_LEN)
13052 goto out_not_found;
13053
13054 memcpy(tp->board_part_number, &vpd_data[i], len);
13055
Linus Torvalds1da177e2005-04-16 15:20:36 -070013056out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013057 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013058 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013059 return;
13060
13061out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013062 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13063 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13064 strcpy(tp->board_part_number, "BCM5717");
13065 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13066 strcpy(tp->board_part_number, "BCM5718");
13067 else
13068 goto nomatch;
13069 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13070 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13071 strcpy(tp->board_part_number, "BCM57780");
13072 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13073 strcpy(tp->board_part_number, "BCM57760");
13074 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13075 strcpy(tp->board_part_number, "BCM57790");
13076 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13077 strcpy(tp->board_part_number, "BCM57788");
13078 else
13079 goto nomatch;
13080 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13081 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13082 strcpy(tp->board_part_number, "BCM57761");
13083 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13084 strcpy(tp->board_part_number, "BCM57765");
13085 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13086 strcpy(tp->board_part_number, "BCM57781");
13087 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13088 strcpy(tp->board_part_number, "BCM57785");
13089 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13090 strcpy(tp->board_part_number, "BCM57791");
13091 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13092 strcpy(tp->board_part_number, "BCM57795");
13093 else
13094 goto nomatch;
13095 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013096 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013097 } else {
13098nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013099 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013101}
13102
Matt Carlson9c8a6202007-10-21 16:16:08 -070013103static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13104{
13105 u32 val;
13106
Matt Carlsone4f34112009-02-25 14:25:00 +000013107 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013108 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013109 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013110 val != 0)
13111 return 0;
13112
13113 return 1;
13114}
13115
Matt Carlsonacd9c112009-02-25 14:26:33 +000013116static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13117{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013118 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013119 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013120 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013121
13122 if (tg3_nvram_read(tp, 0xc, &offset) ||
13123 tg3_nvram_read(tp, 0x4, &start))
13124 return;
13125
13126 offset = tg3_nvram_logical_addr(tp, offset);
13127
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013128 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013129 return;
13130
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013131 if ((val & 0xfc000000) == 0x0c000000) {
13132 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013133 return;
13134
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013135 if (val == 0)
13136 newver = true;
13137 }
13138
Matt Carlson75f99362010-04-05 10:19:24 +000013139 dst_off = strlen(tp->fw_ver);
13140
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013141 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013142 if (TG3_VER_SIZE - dst_off < 16 ||
13143 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013144 return;
13145
13146 offset = offset + ver_offset - start;
13147 for (i = 0; i < 16; i += 4) {
13148 __be32 v;
13149 if (tg3_nvram_read_be32(tp, offset + i, &v))
13150 return;
13151
Matt Carlson75f99362010-04-05 10:19:24 +000013152 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013153 }
13154 } else {
13155 u32 major, minor;
13156
13157 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13158 return;
13159
13160 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13161 TG3_NVM_BCVER_MAJSFT;
13162 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013163 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13164 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013165 }
13166}
13167
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013168static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13169{
13170 u32 val, major, minor;
13171
13172 /* Use native endian representation */
13173 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13174 return;
13175
13176 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13177 TG3_NVM_HWSB_CFG1_MAJSFT;
13178 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13179 TG3_NVM_HWSB_CFG1_MINSFT;
13180
13181 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13182}
13183
Matt Carlsondfe00d72008-11-21 17:19:41 -080013184static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13185{
13186 u32 offset, major, minor, build;
13187
Matt Carlson75f99362010-04-05 10:19:24 +000013188 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013189
13190 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13191 return;
13192
13193 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13194 case TG3_EEPROM_SB_REVISION_0:
13195 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13196 break;
13197 case TG3_EEPROM_SB_REVISION_2:
13198 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13199 break;
13200 case TG3_EEPROM_SB_REVISION_3:
13201 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13202 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013203 case TG3_EEPROM_SB_REVISION_4:
13204 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13205 break;
13206 case TG3_EEPROM_SB_REVISION_5:
13207 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13208 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013209 case TG3_EEPROM_SB_REVISION_6:
13210 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13211 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013212 default:
13213 return;
13214 }
13215
Matt Carlsone4f34112009-02-25 14:25:00 +000013216 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013217 return;
13218
13219 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13220 TG3_EEPROM_SB_EDH_BLD_SHFT;
13221 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13222 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13223 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13224
13225 if (minor > 99 || build > 26)
13226 return;
13227
Matt Carlson75f99362010-04-05 10:19:24 +000013228 offset = strlen(tp->fw_ver);
13229 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13230 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013231
13232 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013233 offset = strlen(tp->fw_ver);
13234 if (offset < TG3_VER_SIZE - 1)
13235 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013236 }
13237}
13238
Matt Carlsonacd9c112009-02-25 14:26:33 +000013239static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013240{
13241 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013242 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013243
13244 for (offset = TG3_NVM_DIR_START;
13245 offset < TG3_NVM_DIR_END;
13246 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013247 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013248 return;
13249
13250 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13251 break;
13252 }
13253
13254 if (offset == TG3_NVM_DIR_END)
13255 return;
13256
Joe Perches63c3a662011-04-26 08:12:10 +000013257 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013258 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013259 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013260 return;
13261
Matt Carlsone4f34112009-02-25 14:25:00 +000013262 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013263 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013264 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013265 return;
13266
13267 offset += val - start;
13268
Matt Carlsonacd9c112009-02-25 14:26:33 +000013269 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013270
Matt Carlsonacd9c112009-02-25 14:26:33 +000013271 tp->fw_ver[vlen++] = ',';
13272 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013273
13274 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013275 __be32 v;
13276 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013277 return;
13278
Al Virob9fc7dc2007-12-17 22:59:57 -080013279 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013280
Matt Carlsonacd9c112009-02-25 14:26:33 +000013281 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13282 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013283 break;
13284 }
13285
Matt Carlsonacd9c112009-02-25 14:26:33 +000013286 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13287 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013288 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013289}
13290
Matt Carlson7fd76442009-02-25 14:27:20 +000013291static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13292{
13293 int vlen;
13294 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013295 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013296
Joe Perches63c3a662011-04-26 08:12:10 +000013297 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013298 return;
13299
13300 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13301 if (apedata != APE_SEG_SIG_MAGIC)
13302 return;
13303
13304 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13305 if (!(apedata & APE_FW_STATUS_READY))
13306 return;
13307
13308 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13309
Matt Carlsondc6d0742010-09-15 08:59:55 +000013310 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013311 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013312 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013313 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013314 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013315 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013316
Matt Carlson7fd76442009-02-25 14:27:20 +000013317 vlen = strlen(tp->fw_ver);
13318
Matt Carlsonecc79642010-08-02 11:26:01 +000013319 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13320 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013321 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13322 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13323 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13324 (apedata & APE_FW_VERSION_BLDMSK));
13325}
13326
Matt Carlsonacd9c112009-02-25 14:26:33 +000013327static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13328{
13329 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013330 bool vpd_vers = false;
13331
13332 if (tp->fw_ver[0] != 0)
13333 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013334
Joe Perches63c3a662011-04-26 08:12:10 +000013335 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013336 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013337 return;
13338 }
13339
Matt Carlsonacd9c112009-02-25 14:26:33 +000013340 if (tg3_nvram_read(tp, 0, &val))
13341 return;
13342
13343 if (val == TG3_EEPROM_MAGIC)
13344 tg3_read_bc_ver(tp);
13345 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13346 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013347 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13348 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013349 else
13350 return;
13351
Joe Perches63c3a662011-04-26 08:12:10 +000013352 if (!tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013353 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013354
13355 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013356
Matt Carlson75f99362010-04-05 10:19:24 +000013357done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013358 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013359}
13360
Michael Chan7544b092007-05-05 13:08:32 -070013361static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13362
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013363static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13364{
Joe Perches63c3a662011-04-26 08:12:10 +000013365 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013366 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013367 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013368 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013369 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013370 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013371}
13372
Matt Carlson41434702011-03-09 16:58:22 +000013373static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013374 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13375 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13376 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13377 { },
13378};
13379
Linus Torvalds1da177e2005-04-16 15:20:36 -070013380static int __devinit tg3_get_invariants(struct tg3 *tp)
13381{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013382 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013383 u32 pci_state_reg, grc_misc_cfg;
13384 u32 val;
13385 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013386 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013387
Linus Torvalds1da177e2005-04-16 15:20:36 -070013388 /* Force memory write invalidate off. If we leave it on,
13389 * then on 5700_BX chips we have to enable a workaround.
13390 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13391 * to match the cacheline size. The Broadcom driver have this
13392 * workaround but turns MWI off all the times so never uses
13393 * it. This seems to suggest that the workaround is insufficient.
13394 */
13395 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13396 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13397 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13398
13399 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
13400 * has the register indirect write enable bit set before
13401 * we try to access any of the MMIO registers. It is also
13402 * critical that the PCI-X hw workaround situation is decided
13403 * before that as well.
13404 */
13405 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13406 &misc_ctrl_reg);
13407
13408 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13409 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013410 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13411 u32 prod_id_asic_rev;
13412
Matt Carlson5001e2f2009-11-13 13:03:51 +000013413 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13414 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013415 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13416 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013417 pci_read_config_dword(tp->pdev,
13418 TG3PCI_GEN2_PRODID_ASICREV,
13419 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013420 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13421 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13422 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13423 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13424 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13425 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13426 pci_read_config_dword(tp->pdev,
13427 TG3PCI_GEN15_PRODID_ASICREV,
13428 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013429 else
13430 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13431 &prod_id_asic_rev);
13432
Matt Carlson321d32a2008-11-21 17:22:19 -080013433 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013435
Michael Chanff645be2005-04-21 17:09:53 -070013436 /* Wrong chip ID in 5752 A0. This code can be removed later
13437 * as A0 is not in production.
13438 */
13439 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13440 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13441
Michael Chan68929142005-08-09 20:17:14 -070013442 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13443 * we need to disable memory and use config. cycles
13444 * only to access all registers. The 5702/03 chips
13445 * can mistakenly decode the special cycles from the
13446 * ICH chipsets as memory write cycles, causing corruption
13447 * of register and memory space. Only certain ICH bridges
13448 * will drive special cycles with non-zero data during the
13449 * address phase which can fall within the 5703's address
13450 * range. This is not an ICH bug as the PCI spec allows
13451 * non-zero address during special cycles. However, only
13452 * these ICH bridges are known to drive non-zero addresses
13453 * during special cycles.
13454 *
13455 * Since special cycles do not cross PCI bridges, we only
13456 * enable this workaround if the 5703 is on the secondary
13457 * bus of these ICH bridges.
13458 */
13459 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13460 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13461 static struct tg3_dev_id {
13462 u32 vendor;
13463 u32 device;
13464 u32 rev;
13465 } ich_chipsets[] = {
13466 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13467 PCI_ANY_ID },
13468 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13469 PCI_ANY_ID },
13470 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13471 0xa },
13472 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13473 PCI_ANY_ID },
13474 { },
13475 };
13476 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13477 struct pci_dev *bridge = NULL;
13478
13479 while (pci_id->vendor != 0) {
13480 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13481 bridge);
13482 if (!bridge) {
13483 pci_id++;
13484 continue;
13485 }
13486 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013487 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013488 continue;
13489 }
13490 if (bridge->subordinate &&
13491 (bridge->subordinate->number ==
13492 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013493 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013494 pci_dev_put(bridge);
13495 break;
13496 }
13497 }
13498 }
13499
Matt Carlson6ff6f812011-05-19 12:12:54 +000013500 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070013501 static struct tg3_dev_id {
13502 u32 vendor;
13503 u32 device;
13504 } bridge_chipsets[] = {
13505 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13506 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13507 { },
13508 };
13509 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13510 struct pci_dev *bridge = NULL;
13511
13512 while (pci_id->vendor != 0) {
13513 bridge = pci_get_device(pci_id->vendor,
13514 pci_id->device,
13515 bridge);
13516 if (!bridge) {
13517 pci_id++;
13518 continue;
13519 }
13520 if (bridge->subordinate &&
13521 (bridge->subordinate->number <=
13522 tp->pdev->bus->number) &&
13523 (bridge->subordinate->subordinate >=
13524 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013525 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013526 pci_dev_put(bridge);
13527 break;
13528 }
13529 }
13530 }
13531
Michael Chan4a29cc22006-03-19 13:21:12 -080013532 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13533 * DMA addresses > 40-bit. This bridge may have other additional
13534 * 57xx devices behind it in some 4-port NIC designs for example.
13535 * Any tg3 device found behind the bridge will also need the 40-bit
13536 * DMA workaround.
13537 */
Michael Chana4e2b342005-10-26 15:46:52 -070013538 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13539 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013540 tg3_flag_set(tp, 5780_CLASS);
13541 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013542 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a5882010-04-05 10:19:28 +000013543 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013544 struct pci_dev *bridge = NULL;
13545
13546 do {
13547 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13548 PCI_DEVICE_ID_SERVERWORKS_EPB,
13549 bridge);
13550 if (bridge && bridge->subordinate &&
13551 (bridge->subordinate->number <=
13552 tp->pdev->bus->number) &&
13553 (bridge->subordinate->subordinate >=
13554 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013555 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013556 pci_dev_put(bridge);
13557 break;
13558 }
13559 } while (bridge);
13560 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013561
Linus Torvalds1da177e2005-04-16 15:20:36 -070013562 /* Initialize misc host control in PCI block. */
13563 tp->misc_host_ctrl |= (misc_ctrl_reg &
13564 MISC_HOST_CTRL_CHIPREV);
13565 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13566 tp->misc_host_ctrl);
13567
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013568 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13569 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013570 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13571 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Michael Chan7544b092007-05-05 13:08:32 -070013572 tp->pdev_peer = tg3_find_peer(tp);
13573
Matt Carlsonc885e822010-08-02 11:25:57 +000013574 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013575 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13576 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013577 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013578
13579 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013580 tg3_flag(tp, 5717_PLUS))
13581 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013582
Matt Carlson321d32a2008-11-21 17:22:19 -080013583 /* Intentionally exclude ASIC_REV_5906 */
13584 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013585 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013586 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013587 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013588 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013589 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013590 tg3_flag(tp, 57765_PLUS))
13591 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013592
13593 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13594 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013595 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013596 tg3_flag(tp, 5755_PLUS) ||
13597 tg3_flag(tp, 5780_CLASS))
13598 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013599
Matt Carlson6ff6f812011-05-19 12:12:54 +000013600 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013601 tg3_flag(tp, 5750_PLUS))
13602 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013603
Matt Carlson507399f2009-11-13 13:03:37 +000013604 /* Determine TSO capabilities */
Matt Carlson2866d952011-02-10 20:06:46 -080013605 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson4d163b72011-01-25 15:58:48 +000013606 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013607 else if (tg3_flag(tp, 57765_PLUS))
13608 tg3_flag_set(tp, HW_TSO_3);
13609 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013610 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013611 tg3_flag_set(tp, HW_TSO_2);
13612 else if (tg3_flag(tp, 5750_PLUS)) {
13613 tg3_flag_set(tp, HW_TSO_1);
13614 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013615 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13616 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013617 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013618 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13619 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13620 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013621 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013622 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13623 tp->fw_needed = FIRMWARE_TG3TSO5;
13624 else
13625 tp->fw_needed = FIRMWARE_TG3TSO;
13626 }
13627
Matt Carlsondabc5c62011-05-19 12:12:52 +000013628 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013629 if (tg3_flag(tp, HW_TSO_1) ||
13630 tg3_flag(tp, HW_TSO_2) ||
13631 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsondabc5c62011-05-19 12:12:52 +000013632 (tp->fw_needed && !tg3_flag(tp, ENABLE_ASF)))
13633 tg3_flag_set(tp, TSO_CAPABLE);
13634 else {
13635 tg3_flag_clear(tp, TSO_CAPABLE);
13636 tg3_flag_clear(tp, TSO_BUG);
13637 tp->fw_needed = NULL;
13638 }
13639
13640 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
13641 tp->fw_needed = FIRMWARE_TG3;
13642
Matt Carlson507399f2009-11-13 13:03:37 +000013643 tp->irq_max = 1;
13644
Joe Perches63c3a662011-04-26 08:12:10 +000013645 if (tg3_flag(tp, 5750_PLUS)) {
13646 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013647 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13648 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13649 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13650 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13651 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000013652 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013653
Joe Perches63c3a662011-04-26 08:12:10 +000013654 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013655 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013656 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070013657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013658
Joe Perches63c3a662011-04-26 08:12:10 +000013659 if (tg3_flag(tp, 57765_PLUS)) {
13660 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000013661 tp->irq_max = TG3_IRQ_MAX_VECS;
13662 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013663 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013664
Matt Carlson2ffcc982011-05-19 12:12:44 +000013665 /* All chips can get confused if TX buffers
13666 * straddle the 4GB address boundary.
13667 */
13668 tg3_flag_set(tp, 4G_DMA_BNDRY_BUG);
13669
13670 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000013671 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlson2ffcc982011-05-19 12:12:44 +000013672 else
Joe Perches63c3a662011-04-26 08:12:10 +000013673 tg3_flag_set(tp, 40BIT_DMA_LIMIT_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013674
Joe Perches63c3a662011-04-26 08:12:10 +000013675 if (tg3_flag(tp, 5717_PLUS))
13676 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000013677
Joe Perches63c3a662011-04-26 08:12:10 +000013678 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlson2866d952011-02-10 20:06:46 -080013679 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Joe Perches63c3a662011-04-26 08:12:10 +000013680 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000013681
Joe Perches63c3a662011-04-26 08:12:10 +000013682 if (!tg3_flag(tp, 5705_PLUS) ||
13683 tg3_flag(tp, 5780_CLASS) ||
13684 tg3_flag(tp, USE_JUMBO_BDFLAG))
13685 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070013686
Matt Carlson52f44902008-11-21 17:17:04 -080013687 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13688 &pci_state_reg);
13689
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013690 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13691 if (tp->pcie_cap != 0) {
13692 u16 lnkctl;
13693
Joe Perches63c3a662011-04-26 08:12:10 +000013694 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013695
Matt Carlsoncf790032010-11-24 08:31:48 +000013696 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000013697 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13698 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000013699 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000013700
13701 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013702
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013703 pci_read_config_word(tp->pdev,
13704 tp->pcie_cap + PCI_EXP_LNKCTL,
13705 &lnkctl);
13706 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000013707 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13708 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013709 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000013710 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000013711 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013712 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013713 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013714 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13715 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000013716 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000013717 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013718 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080013719 }
Matt Carlson52f44902008-11-21 17:17:04 -080013720 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +000013721 tg3_flag_set(tp, PCI_EXPRESS);
13722 } else if (!tg3_flag(tp, 5705_PLUS) ||
13723 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080013724 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13725 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013726 dev_err(&tp->pdev->dev,
13727 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013728 return -EIO;
13729 }
13730
13731 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000013732 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080013733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013734
Michael Chan399de502005-10-03 14:02:39 -070013735 /* If we have an AMD 762 or VIA K8T800 chipset, write
13736 * reordering to the mailbox registers done by the host
13737 * controller can cause major troubles. We read back from
13738 * every mailbox register write to force the writes to be
13739 * posted to the chip in order.
13740 */
Matt Carlson41434702011-03-09 16:58:22 +000013741 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013742 !tg3_flag(tp, PCI_EXPRESS))
13743 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070013744
Matt Carlson69fc4052008-12-21 20:19:57 -080013745 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13746 &tp->pci_cacheline_sz);
13747 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13748 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013749 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13750 tp->pci_lat_timer < 64) {
13751 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013752 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13753 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013754 }
13755
Matt Carlson52f44902008-11-21 17:17:04 -080013756 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13757 /* 5700 BX chips need to have their TX producer index
13758 * mailboxes written twice to workaround a bug.
13759 */
Joe Perches63c3a662011-04-26 08:12:10 +000013760 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070013761
Matt Carlson52f44902008-11-21 17:17:04 -080013762 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013763 *
13764 * The workaround is to use indirect register accesses
13765 * for all chip writes not to mailbox registers.
13766 */
Joe Perches63c3a662011-04-26 08:12:10 +000013767 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013768 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013769
Joe Perches63c3a662011-04-26 08:12:10 +000013770 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013771
13772 /* The chip can have it's power management PCI config
13773 * space registers clobbered due to this bug.
13774 * So explicitly force the chip into D0 here.
13775 */
Matt Carlson9974a352007-10-07 23:27:28 -070013776 pci_read_config_dword(tp->pdev,
13777 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013778 &pm_reg);
13779 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13780 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013781 pci_write_config_dword(tp->pdev,
13782 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013783 pm_reg);
13784
13785 /* Also, force SERR#/PERR# in PCI command. */
13786 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13787 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13788 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13789 }
13790 }
13791
Linus Torvalds1da177e2005-04-16 15:20:36 -070013792 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013793 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013794 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013795 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013796
13797 /* Chip-specific fixup from Broadcom driver */
13798 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13799 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13800 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13801 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13802 }
13803
Michael Chan1ee582d2005-08-09 20:16:46 -070013804 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013805 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013806 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013807 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013808 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013809 tp->write32_tx_mbox = tg3_write32;
13810 tp->write32_rx_mbox = tg3_write32;
13811
13812 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000013813 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070013814 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013815 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013816 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070013817 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13818 /*
13819 * Back to back register writes can cause problems on these
13820 * chips, the workaround is to read back all reg writes
13821 * except those to mailbox regs.
13822 *
13823 * See tg3_write_indirect_reg32().
13824 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013825 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013826 }
13827
Joe Perches63c3a662011-04-26 08:12:10 +000013828 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070013829 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000013830 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070013831 tp->write32_rx_mbox = tg3_write_flush_reg32;
13832 }
Michael Chan20094932005-08-09 20:16:32 -070013833
Joe Perches63c3a662011-04-26 08:12:10 +000013834 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070013835 tp->read32 = tg3_read_indirect_reg32;
13836 tp->write32 = tg3_write_indirect_reg32;
13837 tp->read32_mbox = tg3_read_indirect_mbox;
13838 tp->write32_mbox = tg3_write_indirect_mbox;
13839 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13840 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13841
13842 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013843 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013844
13845 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13846 pci_cmd &= ~PCI_COMMAND_MEMORY;
13847 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13848 }
Michael Chanb5d37722006-09-27 16:06:21 -070013849 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13850 tp->read32_mbox = tg3_read32_mbox_5906;
13851 tp->write32_mbox = tg3_write32_mbox_5906;
13852 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13853 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13854 }
Michael Chan68929142005-08-09 20:17:14 -070013855
Michael Chanbbadf502006-04-06 21:46:34 -070013856 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013857 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070013858 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013859 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000013860 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070013861
Michael Chan7d0c41e2005-04-21 17:06:20 -070013862 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000013863 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013864 * determined before calling tg3_set_power_state() so that
13865 * we know whether or not to switch out of Vaux power.
13866 * When the flag is set, it means that GPIO1 is used for eeprom
13867 * write protect and also implies that it is a LOM where GPIOs
13868 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013869 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013870 tg3_get_eeprom_hw_cfg(tp);
13871
Joe Perches63c3a662011-04-26 08:12:10 +000013872 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070013873 /* Allow reads and writes to the
13874 * APE register and memory space.
13875 */
13876 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +000013877 PCISTATE_ALLOW_APE_SHMEM_WR |
13878 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013879 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13880 pci_state_reg);
13881 }
13882
Matt Carlson9936bcf2007-10-10 18:03:07 -070013883 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013884 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013885 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013886 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013887 tg3_flag(tp, 57765_PLUS))
13888 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070013889
Matt Carlsonbea8a632011-04-25 12:42:49 +000013890 /* Set up tp->grc_local_ctrl before calling tg3_power_up().
Michael Chan314fba32005-04-21 17:07:04 -070013891 * GPIO1 driven high will bring 5700's external PHY out of reset.
13892 * It is also used as eeprom write protect on LOMs.
13893 */
13894 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013895 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013896 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070013897 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13898 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013899 /* Unused GPIO3 must be driven as output on 5752 because there
13900 * are no pull-up resistors on unused GPIO pins.
13901 */
13902 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13903 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013904
Matt Carlson321d32a2008-11-21 17:22:19 -080013905 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013906 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13907 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013908 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13909
Matt Carlson8d519ab2009-04-20 06:58:01 +000013910 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13911 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013912 /* Turn off the debug UART. */
13913 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013914 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013915 /* Keep VMain power. */
13916 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13917 GRC_LCLCTRL_GPIO_OUTPUT0;
13918 }
13919
Linus Torvalds1da177e2005-04-16 15:20:36 -070013920 /* Force the chip into D0. */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013921 err = tg3_power_up(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013922 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013923 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013924 return err;
13925 }
13926
Linus Torvalds1da177e2005-04-16 15:20:36 -070013927 /* Derive initial jumbo mode from MTU assigned in
13928 * ether_setup() via the alloc_etherdev() call
13929 */
Joe Perches63c3a662011-04-26 08:12:10 +000013930 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
13931 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013932
13933 /* Determine WakeOnLan speed to use. */
13934 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13935 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13936 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13937 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000013938 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013939 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013940 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013941 }
13942
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013943 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013944 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013945
Linus Torvalds1da177e2005-04-16 15:20:36 -070013946 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013947 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13948 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013949 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013950 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013951 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
13952 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13953 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013954
13955 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13956 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013957 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013958 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013959 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013960
Joe Perches63c3a662011-04-26 08:12:10 +000013961 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013962 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013963 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013964 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013965 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070013966 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013967 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013968 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13969 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013970 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13971 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013972 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013973 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013974 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013975 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013976 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070013977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013978
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013979 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13980 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13981 tp->phy_otp = tg3_read_otp_phycfg(tp);
13982 if (tp->phy_otp == 0)
13983 tp->phy_otp = TG3_OTP_DEFAULT;
13984 }
13985
Joe Perches63c3a662011-04-26 08:12:10 +000013986 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070013987 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13988 else
13989 tp->mi_mode = MAC_MI_MODE_BASE;
13990
Linus Torvalds1da177e2005-04-16 15:20:36 -070013991 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013992 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13993 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13994 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13995
Matt Carlson4d958472011-04-20 07:57:35 +000013996 /* Set these bits to enable statistics workaround. */
13997 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13998 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
13999 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14000 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14001 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14002 }
14003
Matt Carlson321d32a2008-11-21 17:22:19 -080014004 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14005 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014006 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014007
Matt Carlson158d7ab2008-05-29 01:37:54 -070014008 err = tg3_mdio_init(tp);
14009 if (err)
14010 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014011
14012 /* Initialize data/descriptor byte/word swapping. */
14013 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014014 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14015 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14016 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14017 GRC_MODE_B2HRX_ENABLE |
14018 GRC_MODE_HTX2B_ENABLE |
14019 GRC_MODE_HOST_STACKUP);
14020 else
14021 val &= GRC_MODE_HOST_STACKUP;
14022
Linus Torvalds1da177e2005-04-16 15:20:36 -070014023 tw32(GRC_MODE, val | tp->grc_mode);
14024
14025 tg3_switch_clocks(tp);
14026
14027 /* Clear this out for sanity. */
14028 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14029
14030 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14031 &pci_state_reg);
14032 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014033 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014034 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14035
14036 if (chiprevid == CHIPREV_ID_5701_A0 ||
14037 chiprevid == CHIPREV_ID_5701_B0 ||
14038 chiprevid == CHIPREV_ID_5701_B2 ||
14039 chiprevid == CHIPREV_ID_5701_B5) {
14040 void __iomem *sram_base;
14041
14042 /* Write some dummy words into the SRAM status block
14043 * area, see if it reads back correctly. If the return
14044 * value is bad, force enable the PCIX workaround.
14045 */
14046 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14047
14048 writel(0x00000000, sram_base);
14049 writel(0x00000000, sram_base + 4);
14050 writel(0xffffffff, sram_base + 4);
14051 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014052 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014053 }
14054 }
14055
14056 udelay(50);
14057 tg3_nvram_init(tp);
14058
14059 grc_misc_cfg = tr32(GRC_MISC_CFG);
14060 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14061
Linus Torvalds1da177e2005-04-16 15:20:36 -070014062 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14063 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14064 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014065 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014066
Joe Perches63c3a662011-04-26 08:12:10 +000014067 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014068 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014069 tg3_flag_set(tp, TAGGED_STATUS);
14070 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014071 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14072 HOSTCC_MODE_CLRTICK_TXBD);
14073
14074 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14075 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14076 tp->misc_host_ctrl);
14077 }
14078
Matt Carlson3bda1252008-08-15 14:08:22 -070014079 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014080 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014081 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014082 else
14083 tp->mac_mode = TG3_DEF_MAC_MODE;
14084
Linus Torvalds1da177e2005-04-16 15:20:36 -070014085 /* these are limited to 10/100 only */
14086 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14087 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14088 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14089 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14090 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14091 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14092 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14093 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14094 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014095 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14096 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014097 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014098 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14099 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014100 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14101 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014102
14103 err = tg3_phy_probe(tp);
14104 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014105 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014106 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014107 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014108 }
14109
Matt Carlson184b8902010-04-05 10:19:25 +000014110 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014111 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014112
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014113 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14114 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014115 } else {
14116 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014117 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014118 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014119 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014120 }
14121
14122 /* 5700 {AX,BX} chips have a broken status block link
14123 * change bit implementation, so we must use the
14124 * status register in those cases.
14125 */
14126 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014127 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014128 else
Joe Perches63c3a662011-04-26 08:12:10 +000014129 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014130
14131 /* The led_ctrl is set during tg3_phy_probe, here we might
14132 * have to force the link status polling mechanism based
14133 * upon subsystem IDs.
14134 */
14135 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014136 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014137 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14138 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014139 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014140 }
14141
14142 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014143 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014144 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014145 else
Joe Perches63c3a662011-04-26 08:12:10 +000014146 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014147
Matt Carlsonbf933c82011-01-25 15:58:49 +000014148 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014149 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014150 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014151 tg3_flag(tp, PCIX_MODE)) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014152 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014153#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014154 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014155#endif
14156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014157
Matt Carlson2c49a442010-09-30 10:34:35 +000014158 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14159 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014160 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14161
Matt Carlson2c49a442010-09-30 10:34:35 +000014162 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014163
14164 /* Increment the rx prod index on the rx std ring by at most
14165 * 8 for these chips to workaround hw errata.
14166 */
14167 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14168 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14169 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14170 tp->rx_std_max_post = 8;
14171
Joe Perches63c3a662011-04-26 08:12:10 +000014172 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014173 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14174 PCIE_PWR_MGMT_L1_THRESH_MSK;
14175
Linus Torvalds1da177e2005-04-16 15:20:36 -070014176 return err;
14177}
14178
David S. Miller49b6e95f2007-03-29 01:38:42 -070014179#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014180static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14181{
14182 struct net_device *dev = tp->dev;
14183 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014184 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014185 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014186 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014187
David S. Miller49b6e95f2007-03-29 01:38:42 -070014188 addr = of_get_property(dp, "local-mac-address", &len);
14189 if (addr && len == 6) {
14190 memcpy(dev->dev_addr, addr, 6);
14191 memcpy(dev->perm_addr, dev->dev_addr, 6);
14192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014193 }
14194 return -ENODEV;
14195}
14196
14197static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14198{
14199 struct net_device *dev = tp->dev;
14200
14201 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014202 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014203 return 0;
14204}
14205#endif
14206
14207static int __devinit tg3_get_device_address(struct tg3 *tp)
14208{
14209 struct net_device *dev = tp->dev;
14210 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014211 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014212
David S. Miller49b6e95f2007-03-29 01:38:42 -070014213#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014214 if (!tg3_get_macaddr_sparc(tp))
14215 return 0;
14216#endif
14217
14218 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014219 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014220 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014221 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14222 mac_offset = 0xcc;
14223 if (tg3_nvram_lock(tp))
14224 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14225 else
14226 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014227 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlsona50d0792010-06-05 17:24:37 +000014228 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014229 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000014230 if (PCI_FUNC(tp->pdev->devfn) > 1)
14231 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014232 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014233 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014234
14235 /* First try to get it from MAC address mailbox. */
14236 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14237 if ((hi >> 16) == 0x484b) {
14238 dev->dev_addr[0] = (hi >> 8) & 0xff;
14239 dev->dev_addr[1] = (hi >> 0) & 0xff;
14240
14241 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14242 dev->dev_addr[2] = (lo >> 24) & 0xff;
14243 dev->dev_addr[3] = (lo >> 16) & 0xff;
14244 dev->dev_addr[4] = (lo >> 8) & 0xff;
14245 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014246
Michael Chan008652b2006-03-27 23:14:53 -080014247 /* Some old bootcode may report a 0 MAC address in SRAM */
14248 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14249 }
14250 if (!addr_ok) {
14251 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014252 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014253 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014254 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014255 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14256 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014257 }
14258 /* Finally just fetch it out of the MAC control regs. */
14259 else {
14260 hi = tr32(MAC_ADDR_0_HIGH);
14261 lo = tr32(MAC_ADDR_0_LOW);
14262
14263 dev->dev_addr[5] = lo & 0xff;
14264 dev->dev_addr[4] = (lo >> 8) & 0xff;
14265 dev->dev_addr[3] = (lo >> 16) & 0xff;
14266 dev->dev_addr[2] = (lo >> 24) & 0xff;
14267 dev->dev_addr[1] = hi & 0xff;
14268 dev->dev_addr[0] = (hi >> 8) & 0xff;
14269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014270 }
14271
14272 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014273#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014274 if (!tg3_get_default_macaddr_sparc(tp))
14275 return 0;
14276#endif
14277 return -EINVAL;
14278 }
John W. Linville2ff43692005-09-12 14:44:20 -070014279 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014280 return 0;
14281}
14282
David S. Miller59e6b432005-05-18 22:50:10 -070014283#define BOUNDARY_SINGLE_CACHELINE 1
14284#define BOUNDARY_MULTI_CACHELINE 2
14285
14286static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14287{
14288 int cacheline_size;
14289 u8 byte;
14290 int goal;
14291
14292 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14293 if (byte == 0)
14294 cacheline_size = 1024;
14295 else
14296 cacheline_size = (int) byte * 4;
14297
14298 /* On 5703 and later chips, the boundary bits have no
14299 * effect.
14300 */
14301 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14302 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014303 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014304 goto out;
14305
14306#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14307 goal = BOUNDARY_MULTI_CACHELINE;
14308#else
14309#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14310 goal = BOUNDARY_SINGLE_CACHELINE;
14311#else
14312 goal = 0;
14313#endif
14314#endif
14315
Joe Perches63c3a662011-04-26 08:12:10 +000014316 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014317 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14318 goto out;
14319 }
14320
David S. Miller59e6b432005-05-18 22:50:10 -070014321 if (!goal)
14322 goto out;
14323
14324 /* PCI controllers on most RISC systems tend to disconnect
14325 * when a device tries to burst across a cache-line boundary.
14326 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14327 *
14328 * Unfortunately, for PCI-E there are only limited
14329 * write-side controls for this, and thus for reads
14330 * we will still get the disconnects. We'll also waste
14331 * these PCI cycles for both read and write for chips
14332 * other than 5700 and 5701 which do not implement the
14333 * boundary bits.
14334 */
Joe Perches63c3a662011-04-26 08:12:10 +000014335 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014336 switch (cacheline_size) {
14337 case 16:
14338 case 32:
14339 case 64:
14340 case 128:
14341 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14342 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14343 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14344 } else {
14345 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14346 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14347 }
14348 break;
14349
14350 case 256:
14351 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14352 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14353 break;
14354
14355 default:
14356 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14357 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14358 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014359 }
Joe Perches63c3a662011-04-26 08:12:10 +000014360 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014361 switch (cacheline_size) {
14362 case 16:
14363 case 32:
14364 case 64:
14365 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14366 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14367 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14368 break;
14369 }
14370 /* fallthrough */
14371 case 128:
14372 default:
14373 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14374 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14375 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014376 }
David S. Miller59e6b432005-05-18 22:50:10 -070014377 } else {
14378 switch (cacheline_size) {
14379 case 16:
14380 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14381 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14382 DMA_RWCTRL_WRITE_BNDRY_16);
14383 break;
14384 }
14385 /* fallthrough */
14386 case 32:
14387 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14388 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14389 DMA_RWCTRL_WRITE_BNDRY_32);
14390 break;
14391 }
14392 /* fallthrough */
14393 case 64:
14394 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14395 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14396 DMA_RWCTRL_WRITE_BNDRY_64);
14397 break;
14398 }
14399 /* fallthrough */
14400 case 128:
14401 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14402 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14403 DMA_RWCTRL_WRITE_BNDRY_128);
14404 break;
14405 }
14406 /* fallthrough */
14407 case 256:
14408 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14409 DMA_RWCTRL_WRITE_BNDRY_256);
14410 break;
14411 case 512:
14412 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14413 DMA_RWCTRL_WRITE_BNDRY_512);
14414 break;
14415 case 1024:
14416 default:
14417 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14418 DMA_RWCTRL_WRITE_BNDRY_1024);
14419 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014420 }
David S. Miller59e6b432005-05-18 22:50:10 -070014421 }
14422
14423out:
14424 return val;
14425}
14426
Linus Torvalds1da177e2005-04-16 15:20:36 -070014427static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14428{
14429 struct tg3_internal_buffer_desc test_desc;
14430 u32 sram_dma_descs;
14431 int i, ret;
14432
14433 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14434
14435 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14436 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14437 tw32(RDMAC_STATUS, 0);
14438 tw32(WDMAC_STATUS, 0);
14439
14440 tw32(BUFMGR_MODE, 0);
14441 tw32(FTQ_RESET, 0);
14442
14443 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14444 test_desc.addr_lo = buf_dma & 0xffffffff;
14445 test_desc.nic_mbuf = 0x00002100;
14446 test_desc.len = size;
14447
14448 /*
14449 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14450 * the *second* time the tg3 driver was getting loaded after an
14451 * initial scan.
14452 *
14453 * Broadcom tells me:
14454 * ...the DMA engine is connected to the GRC block and a DMA
14455 * reset may affect the GRC block in some unpredictable way...
14456 * The behavior of resets to individual blocks has not been tested.
14457 *
14458 * Broadcom noted the GRC reset will also reset all sub-components.
14459 */
14460 if (to_device) {
14461 test_desc.cqid_sqid = (13 << 8) | 2;
14462
14463 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14464 udelay(40);
14465 } else {
14466 test_desc.cqid_sqid = (16 << 8) | 7;
14467
14468 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14469 udelay(40);
14470 }
14471 test_desc.flags = 0x00000005;
14472
14473 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14474 u32 val;
14475
14476 val = *(((u32 *)&test_desc) + i);
14477 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14478 sram_dma_descs + (i * sizeof(u32)));
14479 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14480 }
14481 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14482
Matt Carlson859a5882010-04-05 10:19:28 +000014483 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014484 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a5882010-04-05 10:19:28 +000014485 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014486 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014487
14488 ret = -ENODEV;
14489 for (i = 0; i < 40; i++) {
14490 u32 val;
14491
14492 if (to_device)
14493 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14494 else
14495 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14496 if ((val & 0xffff) == sram_dma_descs) {
14497 ret = 0;
14498 break;
14499 }
14500
14501 udelay(100);
14502 }
14503
14504 return ret;
14505}
14506
David S. Millerded73402005-05-23 13:59:47 -070014507#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014508
Matt Carlson41434702011-03-09 16:58:22 +000014509static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014510 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14511 { },
14512};
14513
Linus Torvalds1da177e2005-04-16 15:20:36 -070014514static int __devinit tg3_test_dma(struct tg3 *tp)
14515{
14516 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014517 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014518 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014519
Matt Carlson4bae65c2010-11-24 08:31:52 +000014520 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14521 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014522 if (!buf) {
14523 ret = -ENOMEM;
14524 goto out_nofree;
14525 }
14526
14527 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14528 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14529
David S. Miller59e6b432005-05-18 22:50:10 -070014530 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014531
Joe Perches63c3a662011-04-26 08:12:10 +000014532 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014533 goto out;
14534
Joe Perches63c3a662011-04-26 08:12:10 +000014535 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014536 /* DMA read watermark not used on PCIE */
14537 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014538 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014539 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14540 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014541 tp->dma_rwctrl |= 0x003f0000;
14542 else
14543 tp->dma_rwctrl |= 0x003f000f;
14544 } else {
14545 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14546 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14547 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014548 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014549
Michael Chan4a29cc22006-03-19 13:21:12 -080014550 /* If the 5704 is behind the EPB bridge, we can
14551 * do the less restrictive ONE_DMA workaround for
14552 * better performance.
14553 */
Joe Perches63c3a662011-04-26 08:12:10 +000014554 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014555 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14556 tp->dma_rwctrl |= 0x8000;
14557 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014558 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14559
Michael Chan49afdeb2007-02-13 12:17:03 -080014560 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14561 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014562 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014563 tp->dma_rwctrl |=
14564 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14565 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14566 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014567 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14568 /* 5780 always in PCIX mode */
14569 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014570 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14571 /* 5714 always in PCIX mode */
14572 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014573 } else {
14574 tp->dma_rwctrl |= 0x001b000f;
14575 }
14576 }
14577
14578 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14579 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14580 tp->dma_rwctrl &= 0xfffffff0;
14581
14582 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14583 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14584 /* Remove this if it causes problems for some boards. */
14585 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14586
14587 /* On 5700/5701 chips, we need to set this bit.
14588 * Otherwise the chip will issue cacheline transactions
14589 * to streamable DMA memory with not all the byte
14590 * enables turned on. This is an error on several
14591 * RISC PCI controllers, in particular sparc64.
14592 *
14593 * On 5703/5704 chips, this bit has been reassigned
14594 * a different meaning. In particular, it is used
14595 * on those chips to enable a PCI-X workaround.
14596 */
14597 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14598 }
14599
14600 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14601
14602#if 0
14603 /* Unneeded, already done by tg3_get_invariants. */
14604 tg3_switch_clocks(tp);
14605#endif
14606
Linus Torvalds1da177e2005-04-16 15:20:36 -070014607 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14608 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14609 goto out;
14610
David S. Miller59e6b432005-05-18 22:50:10 -070014611 /* It is best to perform DMA test with maximum write burst size
14612 * to expose the 5700/5701 write DMA bug.
14613 */
14614 saved_dma_rwctrl = tp->dma_rwctrl;
14615 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14616 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14617
Linus Torvalds1da177e2005-04-16 15:20:36 -070014618 while (1) {
14619 u32 *p = buf, i;
14620
14621 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14622 p[i] = i;
14623
14624 /* Send the buffer to the chip. */
14625 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14626 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014627 dev_err(&tp->pdev->dev,
14628 "%s: Buffer write failed. err = %d\n",
14629 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014630 break;
14631 }
14632
14633#if 0
14634 /* validate data reached card RAM correctly. */
14635 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14636 u32 val;
14637 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14638 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014639 dev_err(&tp->pdev->dev,
14640 "%s: Buffer corrupted on device! "
14641 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014642 /* ret = -ENODEV here? */
14643 }
14644 p[i] = 0;
14645 }
14646#endif
14647 /* Now read it back. */
14648 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14649 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014650 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14651 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014652 break;
14653 }
14654
14655 /* Verify it. */
14656 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14657 if (p[i] == i)
14658 continue;
14659
David S. Miller59e6b432005-05-18 22:50:10 -070014660 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14661 DMA_RWCTRL_WRITE_BNDRY_16) {
14662 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014663 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14664 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14665 break;
14666 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014667 dev_err(&tp->pdev->dev,
14668 "%s: Buffer corrupted on read back! "
14669 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014670 ret = -ENODEV;
14671 goto out;
14672 }
14673 }
14674
14675 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14676 /* Success. */
14677 ret = 0;
14678 break;
14679 }
14680 }
David S. Miller59e6b432005-05-18 22:50:10 -070014681 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14682 DMA_RWCTRL_WRITE_BNDRY_16) {
14683 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014684 * now look for chipsets that are known to expose the
14685 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014686 */
Matt Carlson41434702011-03-09 16:58:22 +000014687 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014688 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14689 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a5882010-04-05 10:19:28 +000014690 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014691 /* Safe to use the calculated DMA boundary. */
14692 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a5882010-04-05 10:19:28 +000014693 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014694
David S. Miller59e6b432005-05-18 22:50:10 -070014695 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014697
14698out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000014699 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014700out_nofree:
14701 return ret;
14702}
14703
Linus Torvalds1da177e2005-04-16 15:20:36 -070014704static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14705{
Joe Perches63c3a662011-04-26 08:12:10 +000014706 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000014707 tp->bufmgr_config.mbuf_read_dma_low_water =
14708 DEFAULT_MB_RDMA_LOW_WATER_5705;
14709 tp->bufmgr_config.mbuf_mac_rx_low_water =
14710 DEFAULT_MB_MACRX_LOW_WATER_57765;
14711 tp->bufmgr_config.mbuf_high_water =
14712 DEFAULT_MB_HIGH_WATER_57765;
14713
14714 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14715 DEFAULT_MB_RDMA_LOW_WATER_5705;
14716 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14717 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14718 tp->bufmgr_config.mbuf_high_water_jumbo =
14719 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000014720 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec172005-07-25 12:31:48 -070014721 tp->bufmgr_config.mbuf_read_dma_low_water =
14722 DEFAULT_MB_RDMA_LOW_WATER_5705;
14723 tp->bufmgr_config.mbuf_mac_rx_low_water =
14724 DEFAULT_MB_MACRX_LOW_WATER_5705;
14725 tp->bufmgr_config.mbuf_high_water =
14726 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014727 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14728 tp->bufmgr_config.mbuf_mac_rx_low_water =
14729 DEFAULT_MB_MACRX_LOW_WATER_5906;
14730 tp->bufmgr_config.mbuf_high_water =
14731 DEFAULT_MB_HIGH_WATER_5906;
14732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014733
Michael Chanfdfec172005-07-25 12:31:48 -070014734 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14735 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14736 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14737 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14738 tp->bufmgr_config.mbuf_high_water_jumbo =
14739 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14740 } else {
14741 tp->bufmgr_config.mbuf_read_dma_low_water =
14742 DEFAULT_MB_RDMA_LOW_WATER;
14743 tp->bufmgr_config.mbuf_mac_rx_low_water =
14744 DEFAULT_MB_MACRX_LOW_WATER;
14745 tp->bufmgr_config.mbuf_high_water =
14746 DEFAULT_MB_HIGH_WATER;
14747
14748 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14749 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14750 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14751 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14752 tp->bufmgr_config.mbuf_high_water_jumbo =
14753 DEFAULT_MB_HIGH_WATER_JUMBO;
14754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014755
14756 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14757 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14758}
14759
14760static char * __devinit tg3_phy_string(struct tg3 *tp)
14761{
Matt Carlson79eb6902010-02-17 15:17:03 +000014762 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14763 case TG3_PHY_ID_BCM5400: return "5400";
14764 case TG3_PHY_ID_BCM5401: return "5401";
14765 case TG3_PHY_ID_BCM5411: return "5411";
14766 case TG3_PHY_ID_BCM5701: return "5701";
14767 case TG3_PHY_ID_BCM5703: return "5703";
14768 case TG3_PHY_ID_BCM5704: return "5704";
14769 case TG3_PHY_ID_BCM5705: return "5705";
14770 case TG3_PHY_ID_BCM5750: return "5750";
14771 case TG3_PHY_ID_BCM5752: return "5752";
14772 case TG3_PHY_ID_BCM5714: return "5714";
14773 case TG3_PHY_ID_BCM5780: return "5780";
14774 case TG3_PHY_ID_BCM5755: return "5755";
14775 case TG3_PHY_ID_BCM5787: return "5787";
14776 case TG3_PHY_ID_BCM5784: return "5784";
14777 case TG3_PHY_ID_BCM5756: return "5722/5756";
14778 case TG3_PHY_ID_BCM5906: return "5906";
14779 case TG3_PHY_ID_BCM5761: return "5761";
14780 case TG3_PHY_ID_BCM5718C: return "5718C";
14781 case TG3_PHY_ID_BCM5718S: return "5718S";
14782 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014783 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000014784 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014785 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014786 case 0: return "serdes";
14787 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014789}
14790
Michael Chanf9804dd2005-09-27 12:13:10 -070014791static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14792{
Joe Perches63c3a662011-04-26 08:12:10 +000014793 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014794 strcpy(str, "PCI Express");
14795 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000014796 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014797 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14798
14799 strcpy(str, "PCIX:");
14800
14801 if ((clock_ctrl == 7) ||
14802 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14803 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14804 strcat(str, "133MHz");
14805 else if (clock_ctrl == 0)
14806 strcat(str, "33MHz");
14807 else if (clock_ctrl == 2)
14808 strcat(str, "50MHz");
14809 else if (clock_ctrl == 4)
14810 strcat(str, "66MHz");
14811 else if (clock_ctrl == 6)
14812 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014813 } else {
14814 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000014815 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070014816 strcat(str, "66MHz");
14817 else
14818 strcat(str, "33MHz");
14819 }
Joe Perches63c3a662011-04-26 08:12:10 +000014820 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070014821 strcat(str, ":32-bit");
14822 else
14823 strcat(str, ":64-bit");
14824 return str;
14825}
14826
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014827static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014828{
14829 struct pci_dev *peer;
14830 unsigned int func, devnr = tp->pdev->devfn & ~7;
14831
14832 for (func = 0; func < 8; func++) {
14833 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14834 if (peer && peer != tp->pdev)
14835 break;
14836 pci_dev_put(peer);
14837 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014838 /* 5704 can be configured in single-port mode, set peer to
14839 * tp->pdev in that case.
14840 */
14841 if (!peer) {
14842 peer = tp->pdev;
14843 return peer;
14844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014845
14846 /*
14847 * We don't need to keep the refcount elevated; there's no way
14848 * to remove one half of this device without removing the other
14849 */
14850 pci_dev_put(peer);
14851
14852 return peer;
14853}
14854
David S. Miller15f98502005-05-18 22:49:26 -070014855static void __devinit tg3_init_coal(struct tg3 *tp)
14856{
14857 struct ethtool_coalesce *ec = &tp->coal;
14858
14859 memset(ec, 0, sizeof(*ec));
14860 ec->cmd = ETHTOOL_GCOALESCE;
14861 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14862 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14863 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14864 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14865 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14866 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14867 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14868 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14869 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14870
14871 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14872 HOSTCC_MODE_CLRTICK_TXBD)) {
14873 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14874 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14875 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14876 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14877 }
Michael Chand244c892005-07-05 14:42:33 -070014878
Joe Perches63c3a662011-04-26 08:12:10 +000014879 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070014880 ec->rx_coalesce_usecs_irq = 0;
14881 ec->tx_coalesce_usecs_irq = 0;
14882 ec->stats_block_coalesce_usecs = 0;
14883 }
David S. Miller15f98502005-05-18 22:49:26 -070014884}
14885
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014886static const struct net_device_ops tg3_netdev_ops = {
14887 .ndo_open = tg3_open,
14888 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014889 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014890 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014891 .ndo_validate_addr = eth_validate_addr,
14892 .ndo_set_multicast_list = tg3_set_rx_mode,
14893 .ndo_set_mac_address = tg3_set_mac_addr,
14894 .ndo_do_ioctl = tg3_ioctl,
14895 .ndo_tx_timeout = tg3_tx_timeout,
14896 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000014897 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000014898 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080014899#ifdef CONFIG_NET_POLL_CONTROLLER
14900 .ndo_poll_controller = tg3_poll_controller,
14901#endif
14902};
14903
Linus Torvalds1da177e2005-04-16 15:20:36 -070014904static int __devinit tg3_init_one(struct pci_dev *pdev,
14905 const struct pci_device_id *ent)
14906{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014907 struct net_device *dev;
14908 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014909 int i, err, pm_cap;
14910 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014911 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014912 u64 dma_mask, persist_dma_mask;
Matt Carlson0da06062011-05-19 12:12:53 +000014913 u32 features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014914
Joe Perches05dbe002010-02-17 19:44:19 +000014915 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014916
14917 err = pci_enable_device(pdev);
14918 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014919 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014920 return err;
14921 }
14922
Linus Torvalds1da177e2005-04-16 15:20:36 -070014923 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14924 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014925 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014926 goto err_out_disable_pdev;
14927 }
14928
14929 pci_set_master(pdev);
14930
14931 /* Find power-management capability. */
14932 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14933 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014934 dev_err(&pdev->dev,
14935 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014936 err = -EIO;
14937 goto err_out_free_res;
14938 }
14939
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014940 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014941 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014942 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014943 err = -ENOMEM;
14944 goto err_out_free_res;
14945 }
14946
Linus Torvalds1da177e2005-04-16 15:20:36 -070014947 SET_NETDEV_DEV(dev, &pdev->dev);
14948
Linus Torvalds1da177e2005-04-16 15:20:36 -070014949 tp = netdev_priv(dev);
14950 tp->pdev = pdev;
14951 tp->dev = dev;
14952 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014953 tp->rx_mode = TG3_DEF_RX_MODE;
14954 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014955
Linus Torvalds1da177e2005-04-16 15:20:36 -070014956 if (tg3_debug > 0)
14957 tp->msg_enable = tg3_debug;
14958 else
14959 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14960
14961 /* The word/byte swap controls here control register access byte
14962 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14963 * setting below.
14964 */
14965 tp->misc_host_ctrl =
14966 MISC_HOST_CTRL_MASK_PCI_INT |
14967 MISC_HOST_CTRL_WORD_SWAP |
14968 MISC_HOST_CTRL_INDIR_ACCESS |
14969 MISC_HOST_CTRL_PCISTATE_RW;
14970
14971 /* The NONFRM (non-frame) byte/word swap controls take effect
14972 * on descriptor entries, anything which isn't packet data.
14973 *
14974 * The StrongARM chips on the board (one for tx, one for rx)
14975 * are running in big-endian mode.
14976 */
14977 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14978 GRC_MODE_WSWAP_NONFRM_DATA);
14979#ifdef __BIG_ENDIAN
14980 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14981#endif
14982 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014983 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014984 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014985
Matt Carlsond5fe4882008-11-21 17:20:32 -080014986 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014987 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014988 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014989 err = -ENOMEM;
14990 goto err_out_free_dev;
14991 }
14992
Linus Torvalds1da177e2005-04-16 15:20:36 -070014993 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14994 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014995
Linus Torvalds1da177e2005-04-16 15:20:36 -070014996 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014997 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000014998 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014999 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015000
15001 err = tg3_get_invariants(tp);
15002 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015003 dev_err(&pdev->dev,
15004 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015005 goto err_out_iounmap;
15006 }
15007
Michael Chan4a29cc22006-03-19 13:21:12 -080015008 /* The EPB bridge inside 5714, 5715, and 5780 and any
15009 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015010 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15011 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15012 * do DMA address check in tg3_start_xmit().
15013 */
Joe Perches63c3a662011-04-26 08:12:10 +000015014 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015015 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015016 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015017 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015018#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015019 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015020#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015021 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015022 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015023
15024 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015025 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015026 err = pci_set_dma_mask(pdev, dma_mask);
15027 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015028 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015029 err = pci_set_consistent_dma_mask(pdev,
15030 persist_dma_mask);
15031 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015032 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15033 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015034 goto err_out_iounmap;
15035 }
15036 }
15037 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015038 if (err || dma_mask == DMA_BIT_MASK(32)) {
15039 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015040 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015041 dev_err(&pdev->dev,
15042 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015043 goto err_out_iounmap;
15044 }
15045 }
15046
Michael Chanfdfec172005-07-25 12:31:48 -070015047 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015048
Matt Carlson0da06062011-05-19 12:12:53 +000015049 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15050
15051 /* 5700 B0 chips do not support checksumming correctly due
15052 * to hardware bugs.
15053 */
15054 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15055 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15056
15057 if (tg3_flag(tp, 5755_PLUS))
15058 features |= NETIF_F_IPV6_CSUM;
15059 }
15060
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015061 /* TSO is on by default on chips that support hardware TSO.
15062 * Firmware TSO on older chips gives lower performance, so it
15063 * is off by default, but can be enabled using ethtool.
15064 */
Joe Perches63c3a662011-04-26 08:12:10 +000015065 if ((tg3_flag(tp, HW_TSO_1) ||
15066 tg3_flag(tp, HW_TSO_2) ||
15067 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015068 (features & NETIF_F_IP_CSUM))
15069 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015070 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015071 if (features & NETIF_F_IPV6_CSUM)
15072 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015073 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015074 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015075 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15076 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015077 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015078 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015079 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015081
Matt Carlsond542fe22011-05-19 16:02:43 +000015082 dev->features |= features;
15083 dev->vlan_features |= features;
15084
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015085 /*
15086 * Add loopback capability only for a subset of devices that support
15087 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15088 * loopback for the remaining devices.
15089 */
15090 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15091 !tg3_flag(tp, CPMU_PRESENT))
15092 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015093 features |= NETIF_F_LOOPBACK;
15094
Matt Carlson0da06062011-05-19 12:12:53 +000015095 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015096
Linus Torvalds1da177e2005-04-16 15:20:36 -070015097 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015098 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015099 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015100 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015101 tp->rx_pending = 63;
15102 }
15103
Linus Torvalds1da177e2005-04-16 15:20:36 -070015104 err = tg3_get_device_address(tp);
15105 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015106 dev_err(&pdev->dev,
15107 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000015108 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015109 }
15110
Joe Perches63c3a662011-04-26 08:12:10 +000015111 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson63532392008-11-03 16:49:57 -080015112 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080015113 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015114 dev_err(&pdev->dev,
15115 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015116 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000015117 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015118 }
15119
15120 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000015121
Joe Perches63c3a662011-04-26 08:12:10 +000015122 if (tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000015123 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015124 }
15125
Matt Carlsonc88864d2007-11-12 21:07:01 -080015126 /*
15127 * Reset chip in case UNDI or EFI driver did not shutdown
15128 * DMA self test will enable WDMAC and we'll see (spurious)
15129 * pending DMA on the PCI bus at that point.
15130 */
15131 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15132 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15133 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15134 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15135 }
15136
15137 err = tg3_test_dma(tp);
15138 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015139 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015140 goto err_out_apeunmap;
15141 }
15142
Matt Carlson78f90dc2009-11-13 13:03:42 +000015143 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15144 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15145 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015146 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015147 struct tg3_napi *tnapi = &tp->napi[i];
15148
15149 tnapi->tp = tp;
15150 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15151
15152 tnapi->int_mbox = intmbx;
15153 if (i < 4)
15154 intmbx += 0x8;
15155 else
15156 intmbx += 0x4;
15157
15158 tnapi->consmbox = rcvmbx;
15159 tnapi->prodmbox = sndmbx;
15160
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015161 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015162 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015163 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015164 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015165
Joe Perches63c3a662011-04-26 08:12:10 +000015166 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015167 break;
15168
15169 /*
15170 * If we support MSIX, we'll be using RSS. If we're using
15171 * RSS, the first vector only handles link interrupts and the
15172 * remaining vectors handle rx and tx interrupts. Reuse the
15173 * mailbox values for the next iteration. The values we setup
15174 * above are still useful for the single vectored mode.
15175 */
15176 if (!i)
15177 continue;
15178
15179 rcvmbx += 0x8;
15180
15181 if (sndmbx & 0x4)
15182 sndmbx -= 0x4;
15183 else
15184 sndmbx += 0xc;
15185 }
15186
Matt Carlsonc88864d2007-11-12 21:07:01 -080015187 tg3_init_coal(tp);
15188
Michael Chanc49a1562006-12-17 17:07:29 -080015189 pci_set_drvdata(pdev, dev);
15190
Linus Torvalds1da177e2005-04-16 15:20:36 -070015191 err = register_netdev(dev);
15192 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015193 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015194 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015195 }
15196
Joe Perches05dbe002010-02-17 19:44:19 +000015197 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15198 tp->board_part_number,
15199 tp->pci_chip_rev_id,
15200 tg3_bus_string(tp, str),
15201 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015202
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015203 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015204 struct phy_device *phydev;
15205 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015206 netdev_info(dev,
15207 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015208 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015209 } else {
15210 char *ethtype;
15211
15212 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15213 ethtype = "10/100Base-TX";
15214 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15215 ethtype = "1000Base-SX";
15216 else
15217 ethtype = "10/100/1000Base-T";
15218
Matt Carlson5129c3a2010-04-05 10:19:23 +000015219 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015220 "(WireSpeed[%d], EEE[%d])\n",
15221 tg3_phy_string(tp), ethtype,
15222 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15223 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015224 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015225
Joe Perches05dbe002010-02-17 19:44:19 +000015226 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015227 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015228 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015229 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015230 tg3_flag(tp, ENABLE_ASF) != 0,
15231 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015232 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15233 tp->dma_rwctrl,
15234 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15235 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015236
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015237 pci_save_state(pdev);
15238
Linus Torvalds1da177e2005-04-16 15:20:36 -070015239 return 0;
15240
Matt Carlson0d3031d2007-10-10 18:02:43 -070015241err_out_apeunmap:
15242 if (tp->aperegs) {
15243 iounmap(tp->aperegs);
15244 tp->aperegs = NULL;
15245 }
15246
Linus Torvalds1da177e2005-04-16 15:20:36 -070015247err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015248 if (tp->regs) {
15249 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015250 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015252
15253err_out_free_dev:
15254 free_netdev(dev);
15255
15256err_out_free_res:
15257 pci_release_regions(pdev);
15258
15259err_out_disable_pdev:
15260 pci_disable_device(pdev);
15261 pci_set_drvdata(pdev, NULL);
15262 return err;
15263}
15264
15265static void __devexit tg3_remove_one(struct pci_dev *pdev)
15266{
15267 struct net_device *dev = pci_get_drvdata(pdev);
15268
15269 if (dev) {
15270 struct tg3 *tp = netdev_priv(dev);
15271
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015272 if (tp->fw)
15273 release_firmware(tp->fw);
15274
Tejun Heo23f333a2010-12-12 16:45:14 +010015275 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015276
Joe Perches63c3a662011-04-26 08:12:10 +000015277 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015278 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015279 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015280 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015281
Linus Torvalds1da177e2005-04-16 15:20:36 -070015282 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015283 if (tp->aperegs) {
15284 iounmap(tp->aperegs);
15285 tp->aperegs = NULL;
15286 }
Michael Chan68929142005-08-09 20:17:14 -070015287 if (tp->regs) {
15288 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015289 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015291 free_netdev(dev);
15292 pci_release_regions(pdev);
15293 pci_disable_device(pdev);
15294 pci_set_drvdata(pdev, NULL);
15295 }
15296}
15297
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015298#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015299static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015300{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015301 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015302 struct net_device *dev = pci_get_drvdata(pdev);
15303 struct tg3 *tp = netdev_priv(dev);
15304 int err;
15305
15306 if (!netif_running(dev))
15307 return 0;
15308
Tejun Heo23f333a2010-12-12 16:45:14 +010015309 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015310 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015311 tg3_netif_stop(tp);
15312
15313 del_timer_sync(&tp->timer);
15314
David S. Millerf47c11e2005-06-24 20:18:35 -070015315 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015316 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015317 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015318
15319 netif_device_detach(dev);
15320
David S. Millerf47c11e2005-06-24 20:18:35 -070015321 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015322 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015323 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015324 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015325
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015326 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015327 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015328 int err2;
15329
David S. Millerf47c11e2005-06-24 20:18:35 -070015330 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015331
Joe Perches63c3a662011-04-26 08:12:10 +000015332 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015333 err2 = tg3_restart_hw(tp, 1);
15334 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015335 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015336
15337 tp->timer.expires = jiffies + tp->timer_offset;
15338 add_timer(&tp->timer);
15339
15340 netif_device_attach(dev);
15341 tg3_netif_start(tp);
15342
Michael Chanb9ec6c12006-07-25 16:37:27 -070015343out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015344 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015345
15346 if (!err2)
15347 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015348 }
15349
15350 return err;
15351}
15352
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015353static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015354{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015355 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015356 struct net_device *dev = pci_get_drvdata(pdev);
15357 struct tg3 *tp = netdev_priv(dev);
15358 int err;
15359
15360 if (!netif_running(dev))
15361 return 0;
15362
Linus Torvalds1da177e2005-04-16 15:20:36 -070015363 netif_device_attach(dev);
15364
David S. Millerf47c11e2005-06-24 20:18:35 -070015365 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015366
Joe Perches63c3a662011-04-26 08:12:10 +000015367 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015368 err = tg3_restart_hw(tp, 1);
15369 if (err)
15370 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015371
15372 tp->timer.expires = jiffies + tp->timer_offset;
15373 add_timer(&tp->timer);
15374
Linus Torvalds1da177e2005-04-16 15:20:36 -070015375 tg3_netif_start(tp);
15376
Michael Chanb9ec6c12006-07-25 16:37:27 -070015377out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015378 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015379
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015380 if (!err)
15381 tg3_phy_start(tp);
15382
Michael Chanb9ec6c12006-07-25 16:37:27 -070015383 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015384}
15385
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015386static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015387#define TG3_PM_OPS (&tg3_pm_ops)
15388
15389#else
15390
15391#define TG3_PM_OPS NULL
15392
15393#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015394
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015395/**
15396 * tg3_io_error_detected - called when PCI error is detected
15397 * @pdev: Pointer to PCI device
15398 * @state: The current pci connection state
15399 *
15400 * This function is called after a PCI bus error affecting
15401 * this device has been detected.
15402 */
15403static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15404 pci_channel_state_t state)
15405{
15406 struct net_device *netdev = pci_get_drvdata(pdev);
15407 struct tg3 *tp = netdev_priv(netdev);
15408 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15409
15410 netdev_info(netdev, "PCI I/O error detected\n");
15411
15412 rtnl_lock();
15413
15414 if (!netif_running(netdev))
15415 goto done;
15416
15417 tg3_phy_stop(tp);
15418
15419 tg3_netif_stop(tp);
15420
15421 del_timer_sync(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +000015422 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015423
15424 /* Want to make sure that the reset task doesn't run */
15425 cancel_work_sync(&tp->reset_task);
Joe Perches63c3a662011-04-26 08:12:10 +000015426 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
15427 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015428
15429 netif_device_detach(netdev);
15430
15431 /* Clean up software state, even if MMIO is blocked */
15432 tg3_full_lock(tp, 0);
15433 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15434 tg3_full_unlock(tp);
15435
15436done:
15437 if (state == pci_channel_io_perm_failure)
15438 err = PCI_ERS_RESULT_DISCONNECT;
15439 else
15440 pci_disable_device(pdev);
15441
15442 rtnl_unlock();
15443
15444 return err;
15445}
15446
15447/**
15448 * tg3_io_slot_reset - called after the pci bus has been reset.
15449 * @pdev: Pointer to PCI device
15450 *
15451 * Restart the card from scratch, as if from a cold-boot.
15452 * At this point, the card has exprienced a hard reset,
15453 * followed by fixups by BIOS, and has its config space
15454 * set up identically to what it was at cold boot.
15455 */
15456static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15457{
15458 struct net_device *netdev = pci_get_drvdata(pdev);
15459 struct tg3 *tp = netdev_priv(netdev);
15460 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15461 int err;
15462
15463 rtnl_lock();
15464
15465 if (pci_enable_device(pdev)) {
15466 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15467 goto done;
15468 }
15469
15470 pci_set_master(pdev);
15471 pci_restore_state(pdev);
15472 pci_save_state(pdev);
15473
15474 if (!netif_running(netdev)) {
15475 rc = PCI_ERS_RESULT_RECOVERED;
15476 goto done;
15477 }
15478
15479 err = tg3_power_up(tp);
15480 if (err) {
15481 netdev_err(netdev, "Failed to restore register access.\n");
15482 goto done;
15483 }
15484
15485 rc = PCI_ERS_RESULT_RECOVERED;
15486
15487done:
15488 rtnl_unlock();
15489
15490 return rc;
15491}
15492
15493/**
15494 * tg3_io_resume - called when traffic can start flowing again.
15495 * @pdev: Pointer to PCI device
15496 *
15497 * This callback is called when the error recovery driver tells
15498 * us that its OK to resume normal operation.
15499 */
15500static void tg3_io_resume(struct pci_dev *pdev)
15501{
15502 struct net_device *netdev = pci_get_drvdata(pdev);
15503 struct tg3 *tp = netdev_priv(netdev);
15504 int err;
15505
15506 rtnl_lock();
15507
15508 if (!netif_running(netdev))
15509 goto done;
15510
15511 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015512 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015513 err = tg3_restart_hw(tp, 1);
15514 tg3_full_unlock(tp);
15515 if (err) {
15516 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15517 goto done;
15518 }
15519
15520 netif_device_attach(netdev);
15521
15522 tp->timer.expires = jiffies + tp->timer_offset;
15523 add_timer(&tp->timer);
15524
15525 tg3_netif_start(tp);
15526
15527 tg3_phy_start(tp);
15528
15529done:
15530 rtnl_unlock();
15531}
15532
15533static struct pci_error_handlers tg3_err_handler = {
15534 .error_detected = tg3_io_error_detected,
15535 .slot_reset = tg3_io_slot_reset,
15536 .resume = tg3_io_resume
15537};
15538
Linus Torvalds1da177e2005-04-16 15:20:36 -070015539static struct pci_driver tg3_driver = {
15540 .name = DRV_MODULE_NAME,
15541 .id_table = tg3_pci_tbl,
15542 .probe = tg3_init_one,
15543 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015544 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015545 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015546};
15547
15548static int __init tg3_init(void)
15549{
Jeff Garzik29917622006-08-19 17:48:59 -040015550 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015551}
15552
15553static void __exit tg3_cleanup(void)
15554{
15555 pci_unregister_driver(&tg3_driver);
15556}
15557
15558module_init(tg3_init);
15559module_exit(tg3_cleanup);