blob: bd331174550b0866ce0dc22b2111294be7e12d1f [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 Carlsonba5b0bf2010-01-12 10:11:40 +00007 * Copyright (C) 2005-2010 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>
21#include <linux/kernel.h>
22#include <linux/types.h>
23#include <linux/compiler.h>
24#include <linux/slab.h>
25#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020026#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
28#include <linux/ioport.h>
29#include <linux/pci.h>
30#include <linux/netdevice.h>
31#include <linux/etherdevice.h>
32#include <linux/skbuff.h>
33#include <linux/ethtool.h>
34#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070035#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070036#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/if_vlan.h>
38#include <linux/ip.h>
39#include <linux/tcp.h>
40#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070041#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020042#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080043#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030046#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <asm/system.h>
49#include <asm/io.h>
50#include <asm/byteorder.h>
51#include <asm/uaccess.h>
52
David S. Miller49b6e95f2007-03-29 01:38:42 -070053#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070055#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#endif
57
Matt Carlson63532392008-11-03 16:49:57 -080058#define BAR_0 0
59#define BAR_2 2
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
62#define TG3_VLAN_TAG_USED 1
63#else
64#define TG3_VLAN_TAG_USED 0
65#endif
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include "tg3.h"
68
69#define DRV_MODULE_NAME "tg3"
Matt Carlson3941f182010-04-12 06:58:31 +000070#define DRV_MODULE_VERSION "3.110"
71#define DRV_MODULE_RELDATE "April 9, 2010"
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73#define TG3_DEF_MAC_MODE 0
74#define TG3_DEF_RX_MODE 0
75#define TG3_DEF_TX_MODE 0
76#define TG3_DEF_MSG_ENABLE \
77 (NETIF_MSG_DRV | \
78 NETIF_MSG_PROBE | \
79 NETIF_MSG_LINK | \
80 NETIF_MSG_TIMER | \
81 NETIF_MSG_IFDOWN | \
82 NETIF_MSG_IFUP | \
83 NETIF_MSG_RX_ERR | \
84 NETIF_MSG_TX_ERR)
85
86/* length of time before we decide the hardware is borked,
87 * and dev->tx_timeout() should be called to fix the problem
88 */
89#define TG3_TX_TIMEOUT (5 * HZ)
90
91/* hardware minimum and maximum for a single frame's data payload */
92#define TG3_MIN_MTU 60
93#define TG3_MAX_MTU(tp) \
Matt Carlson8f666b02009-08-28 13:58:24 +000094 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/* These numbers seem to be hard coded in the NIC firmware somehow.
97 * You can't change the ring sizes, but you can change where you place
98 * them in the NIC onboard memory.
99 */
100#define TG3_RX_RING_SIZE 512
101#define TG3_DEF_RX_RING_PENDING 200
102#define TG3_RX_JUMBO_RING_SIZE 256
103#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000104#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/* Do not place this n-ring entries value into the tp struct itself,
107 * we really want to expose these constants to GCC so that modulo et
108 * al. operations are done with shifts and masks instead of with
109 * hw multiply/modulo instructions. Another solution would be to
110 * replace things like '% foo' with '& (foo - 1)'.
111 */
112#define TG3_RX_RCB_RING_SIZE(tp) \
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000113 (((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) && \
Matt Carlson5ea1c502009-09-11 16:50:16 -0700114 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) ? 1024 : 512)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116#define TG3_TX_RING_SIZE 512
117#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
118
119#define TG3_RX_RING_BYTES (sizeof(struct tg3_rx_buffer_desc) * \
120 TG3_RX_RING_SIZE)
Matt Carlson79ed5ac2009-08-28 14:00:55 +0000121#define TG3_RX_JUMBO_RING_BYTES (sizeof(struct tg3_ext_rx_buffer_desc) * \
122 TG3_RX_JUMBO_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#define TG3_RX_RCB_RING_BYTES(tp) (sizeof(struct tg3_rx_buffer_desc) * \
Matt Carlson79ed5ac2009-08-28 14:00:55 +0000124 TG3_RX_RCB_RING_SIZE(tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
126 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
128
Matt Carlson9dc7a112010-04-12 06:58:28 +0000129#define TG3_RX_DMA_ALIGN 16
130#define TG3_RX_HEADROOM ALIGN(VLAN_HLEN, TG3_RX_DMA_ALIGN)
131
Matt Carlson287be122009-08-28 13:58:46 +0000132#define TG3_DMA_BYTE_ENAB 64
133
134#define TG3_RX_STD_DMA_SZ 1536
135#define TG3_RX_JMB_DMA_SZ 9046
136
137#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
138
139#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
140#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000142#define TG3_RX_STD_BUFF_RING_SIZE \
143 (sizeof(struct ring_info) * TG3_RX_RING_SIZE)
144
145#define TG3_RX_JMB_BUFF_RING_SIZE \
146 (sizeof(struct ring_info) * TG3_RX_JUMBO_RING_SIZE)
147
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000148#define TG3_RSS_MIN_NUM_MSIX_VECS 2
149
Matt Carlsond2757fc2010-04-12 06:58:27 +0000150/* Due to a hardware bug, the 5701 can only DMA to memory addresses
151 * that are at least dword aligned when used in PCIX mode. The driver
152 * works around this bug by double copying the packet. This workaround
153 * is built into the normal double copy length check for efficiency.
154 *
155 * However, the double copy is only necessary on those architectures
156 * where unaligned memory accesses are inefficient. For those architectures
157 * where unaligned memory accesses incur little penalty, we can reintegrate
158 * the 5701 in the normal rx path. Doing so saves a device structure
159 * dereference by hardcoding the double copy threshold in place.
160 */
161#define TG3_RX_COPY_THRESHOLD 256
162#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
163 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
164#else
165 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
166#endif
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000169#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Matt Carlsonad829262008-11-21 17:16:16 -0800171#define TG3_RAW_IP_ALIGN 2
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/* number of ETHTOOL_GSTATS u64's */
174#define TG3_NUM_STATS (sizeof(struct tg3_ethtool_stats)/sizeof(u64))
175
Michael Chan4cafd3f2005-05-29 14:56:34 -0700176#define TG3_NUM_TEST 6
177
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000178#define TG3_FW_UPDATE_TIMEOUT_SEC 5
179
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800180#define FIRMWARE_TG3 "tigon/tg3.bin"
181#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
182#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000185 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
188MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
189MODULE_LICENSE("GPL");
190MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800191MODULE_FIRMWARE(FIRMWARE_TG3);
192MODULE_FIRMWARE(FIRMWARE_TG3TSO);
193MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
196module_param(tg3_debug, int, 0);
197MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
198
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000199static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700200 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
201 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
202 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
203 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
204 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
205 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
206 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
207 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
208 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
209 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
210 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
211 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
212 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
213 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
214 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5720)},
223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750M)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5724)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700275 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
276 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
277 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
278 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
279 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
280 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
281 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
282 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283};
284
285MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
286
Andreas Mohr50da8592006-08-14 23:54:30 -0700287static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 const char string[ETH_GSTRING_LEN];
289} ethtool_stats_keys[TG3_NUM_STATS] = {
290 { "rx_octets" },
291 { "rx_fragments" },
292 { "rx_ucast_packets" },
293 { "rx_mcast_packets" },
294 { "rx_bcast_packets" },
295 { "rx_fcs_errors" },
296 { "rx_align_errors" },
297 { "rx_xon_pause_rcvd" },
298 { "rx_xoff_pause_rcvd" },
299 { "rx_mac_ctrl_rcvd" },
300 { "rx_xoff_entered" },
301 { "rx_frame_too_long_errors" },
302 { "rx_jabbers" },
303 { "rx_undersize_packets" },
304 { "rx_in_length_errors" },
305 { "rx_out_length_errors" },
306 { "rx_64_or_less_octet_packets" },
307 { "rx_65_to_127_octet_packets" },
308 { "rx_128_to_255_octet_packets" },
309 { "rx_256_to_511_octet_packets" },
310 { "rx_512_to_1023_octet_packets" },
311 { "rx_1024_to_1522_octet_packets" },
312 { "rx_1523_to_2047_octet_packets" },
313 { "rx_2048_to_4095_octet_packets" },
314 { "rx_4096_to_8191_octet_packets" },
315 { "rx_8192_to_9022_octet_packets" },
316
317 { "tx_octets" },
318 { "tx_collisions" },
319
320 { "tx_xon_sent" },
321 { "tx_xoff_sent" },
322 { "tx_flow_control" },
323 { "tx_mac_errors" },
324 { "tx_single_collisions" },
325 { "tx_mult_collisions" },
326 { "tx_deferred" },
327 { "tx_excessive_collisions" },
328 { "tx_late_collisions" },
329 { "tx_collide_2times" },
330 { "tx_collide_3times" },
331 { "tx_collide_4times" },
332 { "tx_collide_5times" },
333 { "tx_collide_6times" },
334 { "tx_collide_7times" },
335 { "tx_collide_8times" },
336 { "tx_collide_9times" },
337 { "tx_collide_10times" },
338 { "tx_collide_11times" },
339 { "tx_collide_12times" },
340 { "tx_collide_13times" },
341 { "tx_collide_14times" },
342 { "tx_collide_15times" },
343 { "tx_ucast_packets" },
344 { "tx_mcast_packets" },
345 { "tx_bcast_packets" },
346 { "tx_carrier_sense_errors" },
347 { "tx_discards" },
348 { "tx_errors" },
349
350 { "dma_writeq_full" },
351 { "dma_write_prioq_full" },
352 { "rxbds_empty" },
353 { "rx_discards" },
354 { "rx_errors" },
355 { "rx_threshold_hit" },
356
357 { "dma_readq_full" },
358 { "dma_read_prioq_full" },
359 { "tx_comp_queue_full" },
360
361 { "ring_set_send_prod_index" },
362 { "ring_status_update" },
363 { "nic_irqs" },
364 { "nic_avoided_irqs" },
365 { "nic_tx_threshold_hit" }
366};
367
Andreas Mohr50da8592006-08-14 23:54:30 -0700368static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700369 const char string[ETH_GSTRING_LEN];
370} ethtool_test_keys[TG3_NUM_TEST] = {
371 { "nvram test (online) " },
372 { "link test (online) " },
373 { "register test (offline)" },
374 { "memory test (offline)" },
375 { "loopback test (offline)" },
376 { "interrupt test (offline)" },
377};
378
Michael Chanb401e9e2005-12-19 16:27:04 -0800379static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
380{
381 writel(val, tp->regs + off);
382}
383
384static u32 tg3_read32(struct tg3 *tp, u32 off)
385{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000386 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800387}
388
Matt Carlson0d3031d2007-10-10 18:02:43 -0700389static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
390{
391 writel(val, tp->aperegs + off);
392}
393
394static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
395{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000396 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700397}
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
400{
Michael Chan68929142005-08-09 20:17:14 -0700401 unsigned long flags;
402
403 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700404 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
405 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700406 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700407}
408
409static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
410{
411 writel(val, tp->regs + off);
412 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Michael Chan68929142005-08-09 20:17:14 -0700415static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
416{
417 unsigned long flags;
418 u32 val;
419
420 spin_lock_irqsave(&tp->indirect_lock, flags);
421 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
422 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
423 spin_unlock_irqrestore(&tp->indirect_lock, flags);
424 return val;
425}
426
427static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
428{
429 unsigned long flags;
430
431 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
432 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
433 TG3_64BIT_REG_LOW, val);
434 return;
435 }
Matt Carlson66711e62009-11-13 13:03:49 +0000436 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700437 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
438 TG3_64BIT_REG_LOW, val);
439 return;
440 }
441
442 spin_lock_irqsave(&tp->indirect_lock, flags);
443 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
444 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
445 spin_unlock_irqrestore(&tp->indirect_lock, flags);
446
447 /* In indirect mode when disabling interrupts, we also need
448 * to clear the interrupt bit in the GRC local ctrl register.
449 */
450 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
451 (val == 0x1)) {
452 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
453 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
454 }
455}
456
457static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
458{
459 unsigned long flags;
460 u32 val;
461
462 spin_lock_irqsave(&tp->indirect_lock, flags);
463 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
464 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
465 spin_unlock_irqrestore(&tp->indirect_lock, flags);
466 return val;
467}
468
Michael Chanb401e9e2005-12-19 16:27:04 -0800469/* usec_wait specifies the wait time in usec when writing to certain registers
470 * where it is unsafe to read back the register without some delay.
471 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
472 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
473 */
474static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Michael Chanb401e9e2005-12-19 16:27:04 -0800476 if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
477 (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
478 /* Non-posted methods */
479 tp->write32(tp, off, val);
480 else {
481 /* Posted method */
482 tg3_write32(tp, off, val);
483 if (usec_wait)
484 udelay(usec_wait);
485 tp->read32(tp, off);
486 }
487 /* Wait again after the read for the posted method to guarantee that
488 * the wait time is met.
489 */
490 if (usec_wait)
491 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Michael Chan09ee9292005-08-09 20:17:00 -0700494static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
495{
496 tp->write32_mbox(tp, off, val);
Michael Chan68929142005-08-09 20:17:14 -0700497 if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
498 !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
499 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700500}
501
Michael Chan20094932005-08-09 20:16:32 -0700502static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 void __iomem *mbox = tp->regs + off;
505 writel(val, mbox);
506 if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
507 writel(val, mbox);
508 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
509 readl(mbox);
510}
511
Michael Chanb5d37722006-09-27 16:06:21 -0700512static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
513{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000514 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700515}
516
517static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
518{
519 writel(val, tp->regs + off + GRCMBOX_BASE);
520}
521
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000522#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700523#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000524#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
525#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
526#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700527
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000528#define tw32(reg, val) tp->write32(tp, reg, val)
529#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
530#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
531#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
534{
Michael Chan68929142005-08-09 20:17:14 -0700535 unsigned long flags;
536
Michael Chanb5d37722006-09-27 16:06:21 -0700537 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
538 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
539 return;
540
Michael Chan68929142005-08-09 20:17:14 -0700541 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700542 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
543 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
544 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Michael Chanbbadf502006-04-06 21:46:34 -0700546 /* Always leave this as zero. */
547 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
548 } else {
549 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
550 tw32_f(TG3PCI_MEM_WIN_DATA, val);
551
552 /* Always leave this as zero. */
553 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
554 }
Michael Chan68929142005-08-09 20:17:14 -0700555 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
558static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
559{
Michael Chan68929142005-08-09 20:17:14 -0700560 unsigned long flags;
561
Michael Chanb5d37722006-09-27 16:06:21 -0700562 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
563 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
564 *val = 0;
565 return;
566 }
567
Michael Chan68929142005-08-09 20:17:14 -0700568 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700569 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
570 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
571 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Michael Chanbbadf502006-04-06 21:46:34 -0700573 /* Always leave this as zero. */
574 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
575 } else {
576 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
577 *val = tr32(TG3PCI_MEM_WIN_DATA);
578
579 /* Always leave this as zero. */
580 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
581 }
Michael Chan68929142005-08-09 20:17:14 -0700582 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Matt Carlson0d3031d2007-10-10 18:02:43 -0700585static void tg3_ape_lock_init(struct tg3 *tp)
586{
587 int i;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000588 u32 regbase;
589
590 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
591 regbase = TG3_APE_LOCK_GRANT;
592 else
593 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700594
595 /* Make sure the driver hasn't any stale locks. */
596 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000597 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700598}
599
600static int tg3_ape_lock(struct tg3 *tp, int locknum)
601{
602 int i, off;
603 int ret = 0;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000604 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700605
606 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
607 return 0;
608
609 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000610 case TG3_APE_LOCK_GRC:
611 case TG3_APE_LOCK_MEM:
612 break;
613 default:
614 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700615 }
616
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000617 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
618 req = TG3_APE_LOCK_REQ;
619 gnt = TG3_APE_LOCK_GRANT;
620 } else {
621 req = TG3_APE_PER_LOCK_REQ;
622 gnt = TG3_APE_PER_LOCK_GRANT;
623 }
624
Matt Carlson0d3031d2007-10-10 18:02:43 -0700625 off = 4 * locknum;
626
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000627 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700628
629 /* Wait for up to 1 millisecond to acquire lock. */
630 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000631 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700632 if (status == APE_LOCK_GRANT_DRIVER)
633 break;
634 udelay(10);
635 }
636
637 if (status != APE_LOCK_GRANT_DRIVER) {
638 /* Revoke the lock request. */
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000639 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700640 APE_LOCK_GRANT_DRIVER);
641
642 ret = -EBUSY;
643 }
644
645 return ret;
646}
647
648static void tg3_ape_unlock(struct tg3 *tp, int locknum)
649{
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000650 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700651
652 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
653 return;
654
655 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000656 case TG3_APE_LOCK_GRC:
657 case TG3_APE_LOCK_MEM:
658 break;
659 default:
660 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700661 }
662
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000663 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
664 gnt = TG3_APE_LOCK_GRANT;
665 else
666 gnt = TG3_APE_PER_LOCK_GRANT;
667
668 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700669}
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671static void tg3_disable_ints(struct tg3 *tp)
672{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000673 int i;
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 tw32(TG3PCI_MISC_HOST_CTRL,
676 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000677 for (i = 0; i < tp->irq_max; i++)
678 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681static void tg3_enable_ints(struct tg3 *tp)
682{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000683 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000684
Michael Chanbbe832c2005-06-24 20:20:04 -0700685 tp->irq_sync = 0;
686 wmb();
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 tw32(TG3PCI_MISC_HOST_CTRL,
689 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000690
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000691 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000692 for (i = 0; i < tp->irq_cnt; i++) {
693 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000694
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000695 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
696 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
697 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
698
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000699 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000700 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000701
702 /* Force an initial interrupt */
703 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
704 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
705 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
706 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000707 tw32(HOSTCC_MODE, tp->coal_now);
708
709 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Matt Carlson17375d22009-08-28 14:02:18 +0000712static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700713{
Matt Carlson17375d22009-08-28 14:02:18 +0000714 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000715 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700716 unsigned int work_exists = 0;
717
718 /* check for phy events */
719 if (!(tp->tg3_flags &
720 (TG3_FLAG_USE_LINKCHG_REG |
721 TG3_FLAG_POLL_SERDES))) {
722 if (sblk->status & SD_STATUS_LINK_CHG)
723 work_exists = 1;
724 }
725 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000726 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000727 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700728 work_exists = 1;
729
730 return work_exists;
731}
732
Matt Carlson17375d22009-08-28 14:02:18 +0000733/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700734 * similar to tg3_enable_ints, but it accurately determines whether there
735 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400736 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 */
Matt Carlson17375d22009-08-28 14:02:18 +0000738static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Matt Carlson17375d22009-08-28 14:02:18 +0000740 struct tg3 *tp = tnapi->tp;
741
Matt Carlson898a56f2009-08-28 14:02:40 +0000742 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 mmiowb();
744
David S. Millerfac9b832005-05-18 22:46:34 -0700745 /* When doing tagged status, this work check is unnecessary.
746 * The last_tag we write above tells the chip which piece of
747 * work we've completed.
748 */
749 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
Matt Carlson17375d22009-08-28 14:02:18 +0000750 tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700751 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000752 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Matt Carlsonfed97812009-09-01 13:10:19 +0000755static void tg3_napi_disable(struct tg3 *tp)
756{
757 int i;
758
759 for (i = tp->irq_cnt - 1; i >= 0; i--)
760 napi_disable(&tp->napi[i].napi);
761}
762
763static void tg3_napi_enable(struct tg3 *tp)
764{
765 int i;
766
767 for (i = 0; i < tp->irq_cnt; i++)
768 napi_enable(&tp->napi[i].napi);
769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771static inline void tg3_netif_stop(struct tg3 *tp)
772{
Michael Chanbbe832c2005-06-24 20:20:04 -0700773 tp->dev->trans_start = jiffies; /* prevent tx timeout */
Matt Carlsonfed97812009-09-01 13:10:19 +0000774 tg3_napi_disable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 netif_tx_disable(tp->dev);
776}
777
778static inline void tg3_netif_start(struct tg3 *tp)
779{
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000780 /* NOTE: unconditional netif_tx_wake_all_queues is only
781 * appropriate so long as all callers are assured to
782 * have free tx slots (such as after tg3_init_hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 */
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000784 netif_tx_wake_all_queues(tp->dev);
785
Matt Carlsonfed97812009-09-01 13:10:19 +0000786 tg3_napi_enable(tp);
787 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
David S. Millerf47c11e2005-06-24 20:18:35 -0700788 tg3_enable_ints(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
791static void tg3_switch_clocks(struct tg3 *tp)
792{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000793 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 u32 orig_clock_ctrl;
795
Matt Carlson795d01c2007-10-07 23:28:17 -0700796 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
797 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700798 return;
799
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000800 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 orig_clock_ctrl = clock_ctrl;
803 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
804 CLOCK_CTRL_CLKRUN_OENABLE |
805 0x1f);
806 tp->pci_clock_ctrl = clock_ctrl;
807
808 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
809 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800810 tw32_wait_f(TG3PCI_CLOCK_CTRL,
811 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800814 tw32_wait_f(TG3PCI_CLOCK_CTRL,
815 clock_ctrl |
816 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
817 40);
818 tw32_wait_f(TG3PCI_CLOCK_CTRL,
819 clock_ctrl | (CLOCK_CTRL_ALTCLK),
820 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800822 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
825#define PHY_BUSY_LOOPS 5000
826
827static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
828{
829 u32 frame_val;
830 unsigned int loops;
831 int ret;
832
833 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
834 tw32_f(MAC_MI_MODE,
835 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
836 udelay(80);
837 }
838
839 *val = 0x0;
840
Matt Carlson882e9792009-09-01 13:21:36 +0000841 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 MI_COM_PHY_ADDR_MASK);
843 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
844 MI_COM_REG_ADDR_MASK);
845 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 tw32_f(MAC_MI_COM, frame_val);
848
849 loops = PHY_BUSY_LOOPS;
850 while (loops != 0) {
851 udelay(10);
852 frame_val = tr32(MAC_MI_COM);
853
854 if ((frame_val & MI_COM_BUSY) == 0) {
855 udelay(5);
856 frame_val = tr32(MAC_MI_COM);
857 break;
858 }
859 loops -= 1;
860 }
861
862 ret = -EBUSY;
863 if (loops != 0) {
864 *val = frame_val & MI_COM_DATA_MASK;
865 ret = 0;
866 }
867
868 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
869 tw32_f(MAC_MI_MODE, tp->mi_mode);
870 udelay(80);
871 }
872
873 return ret;
874}
875
876static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
877{
878 u32 frame_val;
879 unsigned int loops;
880 int ret;
881
Matt Carlson7f97a4b2009-08-25 10:10:03 +0000882 if ((tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700883 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
884 return 0;
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
887 tw32_f(MAC_MI_MODE,
888 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
889 udelay(80);
890 }
891
Matt Carlson882e9792009-09-01 13:21:36 +0000892 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 MI_COM_PHY_ADDR_MASK);
894 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
895 MI_COM_REG_ADDR_MASK);
896 frame_val |= (val & MI_COM_DATA_MASK);
897 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 tw32_f(MAC_MI_COM, frame_val);
900
901 loops = PHY_BUSY_LOOPS;
902 while (loops != 0) {
903 udelay(10);
904 frame_val = tr32(MAC_MI_COM);
905 if ((frame_val & MI_COM_BUSY) == 0) {
906 udelay(5);
907 frame_val = tr32(MAC_MI_COM);
908 break;
909 }
910 loops -= 1;
911 }
912
913 ret = -EBUSY;
914 if (loops != 0)
915 ret = 0;
916
917 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
918 tw32_f(MAC_MI_MODE, tp->mi_mode);
919 udelay(80);
920 }
921
922 return ret;
923}
924
Matt Carlson95e28692008-05-25 23:44:14 -0700925static int tg3_bmcr_reset(struct tg3 *tp)
926{
927 u32 phy_control;
928 int limit, err;
929
930 /* OK, reset it, and poll the BMCR_RESET bit until it
931 * clears or we time out.
932 */
933 phy_control = BMCR_RESET;
934 err = tg3_writephy(tp, MII_BMCR, phy_control);
935 if (err != 0)
936 return -EBUSY;
937
938 limit = 5000;
939 while (limit--) {
940 err = tg3_readphy(tp, MII_BMCR, &phy_control);
941 if (err != 0)
942 return -EBUSY;
943
944 if ((phy_control & BMCR_RESET) == 0) {
945 udelay(40);
946 break;
947 }
948 udelay(10);
949 }
Roel Kluind4675b52009-02-12 16:33:27 -0800950 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -0700951 return -EBUSY;
952
953 return 0;
954}
955
Matt Carlson158d7ab2008-05-29 01:37:54 -0700956static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
957{
Francois Romieu3d165432009-01-19 16:56:50 -0800958 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700959 u32 val;
960
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000961 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700962
963 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000964 val = -EIO;
965
966 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700967
968 return val;
969}
970
971static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
972{
Francois Romieu3d165432009-01-19 16:56:50 -0800973 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000974 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700975
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000976 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700977
978 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000979 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700980
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000981 spin_unlock_bh(&tp->lock);
982
983 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700984}
985
986static int tg3_mdio_reset(struct mii_bus *bp)
987{
988 return 0;
989}
990
Matt Carlson9c61d6b2008-11-03 16:54:56 -0800991static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -0700992{
993 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800994 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -0700995
Matt Carlson3f0e3ad2009-11-02 14:24:36 +0000996 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800997 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +0000998 case PHY_ID_BCM50610:
999 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001000 val = MAC_PHYCFG2_50610_LED_MODES;
1001 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001002 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001003 val = MAC_PHYCFG2_AC131_LED_MODES;
1004 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001005 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001006 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1007 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001008 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001009 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1010 break;
1011 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001012 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001013 }
1014
1015 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1016 tw32(MAC_PHYCFG2, val);
1017
1018 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001019 val &= ~(MAC_PHYCFG1_RGMII_INT |
1020 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1021 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001022 tw32(MAC_PHYCFG1, val);
1023
1024 return;
1025 }
1026
Matt Carlson14417062010-02-17 15:16:59 +00001027 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001028 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1029 MAC_PHYCFG2_FMODE_MASK_MASK |
1030 MAC_PHYCFG2_GMODE_MASK_MASK |
1031 MAC_PHYCFG2_ACT_MASK_MASK |
1032 MAC_PHYCFG2_QUAL_MASK_MASK |
1033 MAC_PHYCFG2_INBAND_ENABLE;
1034
1035 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001036
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001037 val = tr32(MAC_PHYCFG1);
1038 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1039 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Matt Carlson14417062010-02-17 15:16:59 +00001040 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001041 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1042 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
1043 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1044 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1045 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001046 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1047 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1048 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001049
Matt Carlsona9daf362008-05-25 23:49:44 -07001050 val = tr32(MAC_EXT_RGMII_MODE);
1051 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1052 MAC_RGMII_MODE_RX_QUALITY |
1053 MAC_RGMII_MODE_RX_ACTIVITY |
1054 MAC_RGMII_MODE_RX_ENG_DET |
1055 MAC_RGMII_MODE_TX_ENABLE |
1056 MAC_RGMII_MODE_TX_LOWPWR |
1057 MAC_RGMII_MODE_TX_RESET);
Matt Carlson14417062010-02-17 15:16:59 +00001058 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001059 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1060 val |= MAC_RGMII_MODE_RX_INT_B |
1061 MAC_RGMII_MODE_RX_QUALITY |
1062 MAC_RGMII_MODE_RX_ACTIVITY |
1063 MAC_RGMII_MODE_RX_ENG_DET;
1064 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1065 val |= MAC_RGMII_MODE_TX_ENABLE |
1066 MAC_RGMII_MODE_TX_LOWPWR |
1067 MAC_RGMII_MODE_TX_RESET;
1068 }
1069 tw32(MAC_EXT_RGMII_MODE, val);
1070}
1071
Matt Carlson158d7ab2008-05-29 01:37:54 -07001072static void tg3_mdio_start(struct tg3 *tp)
1073{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001074 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1075 tw32_f(MAC_MI_MODE, tp->mi_mode);
1076 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001077
Matt Carlson9ea48182010-02-17 15:17:01 +00001078 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1079 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1080 tg3_mdio_config_5785(tp);
1081}
1082
1083static int tg3_mdio_init(struct tg3 *tp)
1084{
1085 int i;
1086 u32 reg;
1087 struct phy_device *phydev;
1088
Matt Carlson882e9792009-09-01 13:21:36 +00001089 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
1090 u32 funcnum, is_serdes;
1091
1092 funcnum = tr32(TG3_CPMU_STATUS) & TG3_CPMU_STATUS_PCIE_FUNC;
1093 if (funcnum)
1094 tp->phy_addr = 2;
1095 else
1096 tp->phy_addr = 1;
1097
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001098 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1099 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1100 else
1101 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1102 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001103 if (is_serdes)
1104 tp->phy_addr += 7;
1105 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001106 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001107
Matt Carlson158d7ab2008-05-29 01:37:54 -07001108 tg3_mdio_start(tp);
1109
1110 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1111 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1112 return 0;
1113
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001114 tp->mdio_bus = mdiobus_alloc();
1115 if (tp->mdio_bus == NULL)
1116 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001117
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001118 tp->mdio_bus->name = "tg3 mdio bus";
1119 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001120 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001121 tp->mdio_bus->priv = tp;
1122 tp->mdio_bus->parent = &tp->pdev->dev;
1123 tp->mdio_bus->read = &tg3_mdio_read;
1124 tp->mdio_bus->write = &tg3_mdio_write;
1125 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001126 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001127 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001128
1129 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001130 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001131
1132 /* The bus registration will look for all the PHYs on the mdio bus.
1133 * Unfortunately, it does not ensure the PHY is powered up before
1134 * accessing the PHY ID registers. A chip reset is the
1135 * quickest way to bring the device back to an operational state..
1136 */
1137 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1138 tg3_bmcr_reset(tp);
1139
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001140 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001141 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001142 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001143 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001144 return i;
1145 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001146
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001147 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001148
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001149 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001150 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001151 mdiobus_unregister(tp->mdio_bus);
1152 mdiobus_free(tp->mdio_bus);
1153 return -ENODEV;
1154 }
1155
1156 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001157 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001158 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001159 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001160 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001161 case PHY_ID_BCM50610:
1162 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001163 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001164 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001165 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001166 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson14417062010-02-17 15:16:59 +00001167 if (tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)
Matt Carlsona9daf362008-05-25 23:49:44 -07001168 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1169 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1170 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1171 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1172 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001173 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001174 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001175 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001176 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001177 case PHY_ID_RTL8201E:
1178 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001179 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001180 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001181 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001182 break;
1183 }
1184
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001185 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1186
1187 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1188 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001189
1190 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001191}
1192
1193static void tg3_mdio_fini(struct tg3 *tp)
1194{
1195 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1196 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001197 mdiobus_unregister(tp->mdio_bus);
1198 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001199 }
1200}
1201
Matt Carlson95e28692008-05-25 23:44:14 -07001202/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001203static inline void tg3_generate_fw_event(struct tg3 *tp)
1204{
1205 u32 val;
1206
1207 val = tr32(GRC_RX_CPU_EVENT);
1208 val |= GRC_RX_CPU_DRIVER_EVENT;
1209 tw32_f(GRC_RX_CPU_EVENT, val);
1210
1211 tp->last_event_jiffies = jiffies;
1212}
1213
1214#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1215
1216/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001217static void tg3_wait_for_event_ack(struct tg3 *tp)
1218{
1219 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001220 unsigned int delay_cnt;
1221 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001222
Matt Carlson4ba526c2008-08-15 14:10:04 -07001223 /* If enough time has passed, no wait is necessary. */
1224 time_remain = (long)(tp->last_event_jiffies + 1 +
1225 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1226 (long)jiffies;
1227 if (time_remain < 0)
1228 return;
1229
1230 /* Check if we can shorten the wait time. */
1231 delay_cnt = jiffies_to_usecs(time_remain);
1232 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1233 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1234 delay_cnt = (delay_cnt >> 3) + 1;
1235
1236 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001237 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1238 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001239 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001240 }
1241}
1242
1243/* tp->lock is held. */
1244static void tg3_ump_link_report(struct tg3 *tp)
1245{
1246 u32 reg;
1247 u32 val;
1248
1249 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1250 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1251 return;
1252
1253 tg3_wait_for_event_ack(tp);
1254
1255 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1256
1257 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1258
1259 val = 0;
1260 if (!tg3_readphy(tp, MII_BMCR, &reg))
1261 val = reg << 16;
1262 if (!tg3_readphy(tp, MII_BMSR, &reg))
1263 val |= (reg & 0xffff);
1264 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1265
1266 val = 0;
1267 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1268 val = reg << 16;
1269 if (!tg3_readphy(tp, MII_LPA, &reg))
1270 val |= (reg & 0xffff);
1271 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1272
1273 val = 0;
1274 if (!(tp->tg3_flags2 & TG3_FLG2_MII_SERDES)) {
1275 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1276 val = reg << 16;
1277 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1278 val |= (reg & 0xffff);
1279 }
1280 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1281
1282 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1283 val = reg << 16;
1284 else
1285 val = 0;
1286 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1287
Matt Carlson4ba526c2008-08-15 14:10:04 -07001288 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001289}
1290
1291static void tg3_link_report(struct tg3 *tp)
1292{
1293 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001294 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001295 tg3_ump_link_report(tp);
1296 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001297 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1298 (tp->link_config.active_speed == SPEED_1000 ?
1299 1000 :
1300 (tp->link_config.active_speed == SPEED_100 ?
1301 100 : 10)),
1302 (tp->link_config.active_duplex == DUPLEX_FULL ?
1303 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001304
Joe Perches05dbe002010-02-17 19:44:19 +00001305 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1306 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1307 "on" : "off",
1308 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1309 "on" : "off");
Matt Carlson95e28692008-05-25 23:44:14 -07001310 tg3_ump_link_report(tp);
1311 }
1312}
1313
1314static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1315{
1316 u16 miireg;
1317
Steve Glendinninge18ce342008-12-16 02:00:00 -08001318 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001319 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001320 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001321 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001322 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001323 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1324 else
1325 miireg = 0;
1326
1327 return miireg;
1328}
1329
1330static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1331{
1332 u16 miireg;
1333
Steve Glendinninge18ce342008-12-16 02:00:00 -08001334 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001335 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001336 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001337 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001338 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001339 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1340 else
1341 miireg = 0;
1342
1343 return miireg;
1344}
1345
Matt Carlson95e28692008-05-25 23:44:14 -07001346static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1347{
1348 u8 cap = 0;
1349
1350 if (lcladv & ADVERTISE_1000XPAUSE) {
1351 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1352 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001353 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001354 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001355 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001356 } else {
1357 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001358 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001359 }
1360 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1361 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001362 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001363 }
1364
1365 return cap;
1366}
1367
Matt Carlsonf51f3562008-05-25 23:45:08 -07001368static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001369{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001370 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001371 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001372 u32 old_rx_mode = tp->rx_mode;
1373 u32 old_tx_mode = tp->tx_mode;
1374
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001375 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001376 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001377 else
1378 autoneg = tp->link_config.autoneg;
1379
1380 if (autoneg == AUTONEG_ENABLE &&
Matt Carlson95e28692008-05-25 23:44:14 -07001381 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
1382 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001383 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001384 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001385 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001386 } else
1387 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001388
Matt Carlsonf51f3562008-05-25 23:45:08 -07001389 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001390
Steve Glendinninge18ce342008-12-16 02:00:00 -08001391 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001392 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1393 else
1394 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1395
Matt Carlsonf51f3562008-05-25 23:45:08 -07001396 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001397 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001398
Steve Glendinninge18ce342008-12-16 02:00:00 -08001399 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001400 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1401 else
1402 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1403
Matt Carlsonf51f3562008-05-25 23:45:08 -07001404 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001405 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001406}
1407
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001408static void tg3_adjust_link(struct net_device *dev)
1409{
1410 u8 oldflowctrl, linkmesg = 0;
1411 u32 mac_mode, lcl_adv, rmt_adv;
1412 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001413 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001414
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001415 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001416
1417 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1418 MAC_MODE_HALF_DUPLEX);
1419
1420 oldflowctrl = tp->link_config.active_flowctrl;
1421
1422 if (phydev->link) {
1423 lcl_adv = 0;
1424 rmt_adv = 0;
1425
1426 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1427 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001428 else if (phydev->speed == SPEED_1000 ||
1429 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001430 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001431 else
1432 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001433
1434 if (phydev->duplex == DUPLEX_HALF)
1435 mac_mode |= MAC_MODE_HALF_DUPLEX;
1436 else {
1437 lcl_adv = tg3_advert_flowctrl_1000T(
1438 tp->link_config.flowctrl);
1439
1440 if (phydev->pause)
1441 rmt_adv = LPA_PAUSE_CAP;
1442 if (phydev->asym_pause)
1443 rmt_adv |= LPA_PAUSE_ASYM;
1444 }
1445
1446 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1447 } else
1448 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1449
1450 if (mac_mode != tp->mac_mode) {
1451 tp->mac_mode = mac_mode;
1452 tw32_f(MAC_MODE, tp->mac_mode);
1453 udelay(40);
1454 }
1455
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001456 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1457 if (phydev->speed == SPEED_10)
1458 tw32(MAC_MI_STAT,
1459 MAC_MI_STAT_10MBPS_MODE |
1460 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1461 else
1462 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1463 }
1464
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001465 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1466 tw32(MAC_TX_LENGTHS,
1467 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1468 (6 << TX_LENGTHS_IPG_SHIFT) |
1469 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1470 else
1471 tw32(MAC_TX_LENGTHS,
1472 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1473 (6 << TX_LENGTHS_IPG_SHIFT) |
1474 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1475
1476 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1477 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1478 phydev->speed != tp->link_config.active_speed ||
1479 phydev->duplex != tp->link_config.active_duplex ||
1480 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001481 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001482
1483 tp->link_config.active_speed = phydev->speed;
1484 tp->link_config.active_duplex = phydev->duplex;
1485
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001486 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001487
1488 if (linkmesg)
1489 tg3_link_report(tp);
1490}
1491
1492static int tg3_phy_init(struct tg3 *tp)
1493{
1494 struct phy_device *phydev;
1495
1496 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED)
1497 return 0;
1498
1499 /* Bring the PHY back to a known state. */
1500 tg3_bmcr_reset(tp);
1501
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001502 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001503
1504 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001505 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001506 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001507 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001508 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001509 return PTR_ERR(phydev);
1510 }
1511
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001512 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001513 switch (phydev->interface) {
1514 case PHY_INTERFACE_MODE_GMII:
1515 case PHY_INTERFACE_MODE_RGMII:
Matt Carlson321d32a2008-11-21 17:22:19 -08001516 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
1517 phydev->supported &= (PHY_GBIT_FEATURES |
1518 SUPPORTED_Pause |
1519 SUPPORTED_Asym_Pause);
1520 break;
1521 }
1522 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001523 case PHY_INTERFACE_MODE_MII:
1524 phydev->supported &= (PHY_BASIC_FEATURES |
1525 SUPPORTED_Pause |
1526 SUPPORTED_Asym_Pause);
1527 break;
1528 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001529 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001530 return -EINVAL;
1531 }
1532
1533 tp->tg3_flags3 |= TG3_FLG3_PHY_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001534
1535 phydev->advertising = phydev->supported;
1536
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001537 return 0;
1538}
1539
1540static void tg3_phy_start(struct tg3 *tp)
1541{
1542 struct phy_device *phydev;
1543
1544 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1545 return;
1546
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001547 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001548
1549 if (tp->link_config.phy_is_low_power) {
1550 tp->link_config.phy_is_low_power = 0;
1551 phydev->speed = tp->link_config.orig_speed;
1552 phydev->duplex = tp->link_config.orig_duplex;
1553 phydev->autoneg = tp->link_config.orig_autoneg;
1554 phydev->advertising = tp->link_config.orig_advertising;
1555 }
1556
1557 phy_start(phydev);
1558
1559 phy_start_aneg(phydev);
1560}
1561
1562static void tg3_phy_stop(struct tg3 *tp)
1563{
1564 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1565 return;
1566
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001567 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001568}
1569
1570static void tg3_phy_fini(struct tg3 *tp)
1571{
1572 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001573 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001574 tp->tg3_flags3 &= ~TG3_FLG3_PHY_CONNECTED;
1575 }
1576}
1577
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001578static void tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1579{
1580 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1581 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1582}
1583
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001584static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1585{
1586 u32 phytest;
1587
1588 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1589 u32 phy;
1590
1591 tg3_writephy(tp, MII_TG3_FET_TEST,
1592 phytest | MII_TG3_FET_SHADOW_EN);
1593 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1594 if (enable)
1595 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1596 else
1597 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1598 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1599 }
1600 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1601 }
1602}
1603
Matt Carlson6833c042008-11-21 17:18:59 -08001604static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1605{
1606 u32 reg;
1607
Matt Carlsonecf14102010-01-20 16:58:05 +00001608 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1609 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
1610 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001611 return;
1612
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001613 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
1614 tg3_phy_fet_toggle_apd(tp, enable);
1615 return;
1616 }
1617
Matt Carlson6833c042008-11-21 17:18:59 -08001618 reg = MII_TG3_MISC_SHDW_WREN |
1619 MII_TG3_MISC_SHDW_SCR5_SEL |
1620 MII_TG3_MISC_SHDW_SCR5_LPED |
1621 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1622 MII_TG3_MISC_SHDW_SCR5_SDTL |
1623 MII_TG3_MISC_SHDW_SCR5_C125OE;
1624 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1625 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1626
1627 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1628
1629
1630 reg = MII_TG3_MISC_SHDW_WREN |
1631 MII_TG3_MISC_SHDW_APD_SEL |
1632 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1633 if (enable)
1634 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1635
1636 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1637}
1638
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001639static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1640{
1641 u32 phy;
1642
1643 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1644 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
1645 return;
1646
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001647 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001648 u32 ephy;
1649
Matt Carlson535ef6e2009-08-25 10:09:36 +00001650 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1651 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1652
1653 tg3_writephy(tp, MII_TG3_FET_TEST,
1654 ephy | MII_TG3_FET_SHADOW_EN);
1655 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001656 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001657 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001658 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001659 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1660 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001661 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001662 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001663 }
1664 } else {
1665 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1666 MII_TG3_AUXCTL_SHDWSEL_MISC;
1667 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1668 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1669 if (enable)
1670 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1671 else
1672 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1673 phy |= MII_TG3_AUXCTL_MISC_WREN;
1674 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1675 }
1676 }
1677}
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679static void tg3_phy_set_wirespeed(struct tg3 *tp)
1680{
1681 u32 val;
1682
1683 if (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED)
1684 return;
1685
1686 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1687 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1688 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1689 (val | (1 << 15) | (1 << 4)));
1690}
1691
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001692static void tg3_phy_apply_otp(struct tg3 *tp)
1693{
1694 u32 otp, phy;
1695
1696 if (!tp->phy_otp)
1697 return;
1698
1699 otp = tp->phy_otp;
1700
1701 /* Enable SM_DSP clock and tx 6dB coding. */
1702 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1703 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1704 MII_TG3_AUXCTL_ACTL_TX_6DB;
1705 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1706
1707 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1708 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1709 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1710
1711 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1712 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1713 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1714
1715 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1716 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1717 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1718
1719 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1720 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1721
1722 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1723 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1724
1725 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1726 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1727 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1728
1729 /* Turn off SM_DSP clock. */
1730 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1731 MII_TG3_AUXCTL_ACTL_TX_6DB;
1732 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1733}
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735static int tg3_wait_macro_done(struct tg3 *tp)
1736{
1737 int limit = 100;
1738
1739 while (limit--) {
1740 u32 tmp32;
1741
1742 if (!tg3_readphy(tp, 0x16, &tmp32)) {
1743 if ((tmp32 & 0x1000) == 0)
1744 break;
1745 }
1746 }
Roel Kluind4675b52009-02-12 16:33:27 -08001747 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 return -EBUSY;
1749
1750 return 0;
1751}
1752
1753static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1754{
1755 static const u32 test_pat[4][6] = {
1756 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1757 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1758 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1759 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1760 };
1761 int chan;
1762
1763 for (chan = 0; chan < 4; chan++) {
1764 int i;
1765
1766 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1767 (chan * 0x2000) | 0x0200);
1768 tg3_writephy(tp, 0x16, 0x0002);
1769
1770 for (i = 0; i < 6; i++)
1771 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1772 test_pat[chan][i]);
1773
1774 tg3_writephy(tp, 0x16, 0x0202);
1775 if (tg3_wait_macro_done(tp)) {
1776 *resetp = 1;
1777 return -EBUSY;
1778 }
1779
1780 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1781 (chan * 0x2000) | 0x0200);
1782 tg3_writephy(tp, 0x16, 0x0082);
1783 if (tg3_wait_macro_done(tp)) {
1784 *resetp = 1;
1785 return -EBUSY;
1786 }
1787
1788 tg3_writephy(tp, 0x16, 0x0802);
1789 if (tg3_wait_macro_done(tp)) {
1790 *resetp = 1;
1791 return -EBUSY;
1792 }
1793
1794 for (i = 0; i < 6; i += 2) {
1795 u32 low, high;
1796
1797 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1798 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1799 tg3_wait_macro_done(tp)) {
1800 *resetp = 1;
1801 return -EBUSY;
1802 }
1803 low &= 0x7fff;
1804 high &= 0x000f;
1805 if (low != test_pat[chan][i] ||
1806 high != test_pat[chan][i+1]) {
1807 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1808 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1809 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1810
1811 return -EBUSY;
1812 }
1813 }
1814 }
1815
1816 return 0;
1817}
1818
1819static int tg3_phy_reset_chanpat(struct tg3 *tp)
1820{
1821 int chan;
1822
1823 for (chan = 0; chan < 4; chan++) {
1824 int i;
1825
1826 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1827 (chan * 0x2000) | 0x0200);
1828 tg3_writephy(tp, 0x16, 0x0002);
1829 for (i = 0; i < 6; i++)
1830 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
1831 tg3_writephy(tp, 0x16, 0x0202);
1832 if (tg3_wait_macro_done(tp))
1833 return -EBUSY;
1834 }
1835
1836 return 0;
1837}
1838
1839static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1840{
1841 u32 reg32, phy9_orig;
1842 int retries, do_phy_reset, err;
1843
1844 retries = 10;
1845 do_phy_reset = 1;
1846 do {
1847 if (do_phy_reset) {
1848 err = tg3_bmcr_reset(tp);
1849 if (err)
1850 return err;
1851 do_phy_reset = 0;
1852 }
1853
1854 /* Disable transmitter and interrupt. */
1855 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1856 continue;
1857
1858 reg32 |= 0x3000;
1859 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1860
1861 /* Set full-duplex, 1000 mbps. */
1862 tg3_writephy(tp, MII_BMCR,
1863 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1864
1865 /* Set to master mode. */
1866 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1867 continue;
1868
1869 tg3_writephy(tp, MII_TG3_CTRL,
1870 (MII_TG3_CTRL_AS_MASTER |
1871 MII_TG3_CTRL_ENABLE_AS_MASTER));
1872
1873 /* Enable SM_DSP_CLOCK and 6dB. */
1874 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1875
1876 /* Block the PHY control access. */
1877 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1878 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0800);
1879
1880 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1881 if (!err)
1882 break;
1883 } while (--retries);
1884
1885 err = tg3_phy_reset_chanpat(tp);
1886 if (err)
1887 return err;
1888
1889 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1890 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0000);
1891
1892 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
1893 tg3_writephy(tp, 0x16, 0x0000);
1894
1895 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1896 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1897 /* Set Extended packet length bit for jumbo frames */
1898 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
Matt Carlson859a588792010-04-05 10:19:28 +00001899 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1901 }
1902
1903 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1904
1905 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1906 reg32 &= ~0x3000;
1907 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1908 } else if (!err)
1909 err = -EBUSY;
1910
1911 return err;
1912}
1913
1914/* This will reset the tigon3 PHY if there is no valid
1915 * link unless the FORCE argument is non-zero.
1916 */
1917static int tg3_phy_reset(struct tg3 *tp)
1918{
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001919 u32 cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 u32 phy_status;
1921 int err;
1922
Michael Chan60189dd2006-12-17 17:08:07 -08001923 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1924 u32 val;
1925
1926 val = tr32(GRC_MISC_CFG);
1927 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1928 udelay(40);
1929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 err = tg3_readphy(tp, MII_BMSR, &phy_status);
1931 err |= tg3_readphy(tp, MII_BMSR, &phy_status);
1932 if (err != 0)
1933 return -EBUSY;
1934
Michael Chanc8e1e822006-04-29 18:55:17 -07001935 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
1936 netif_carrier_off(tp->dev);
1937 tg3_link_report(tp);
1938 }
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1941 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
1942 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
1943 err = tg3_phy_reset_5703_4_5(tp);
1944 if (err)
1945 return err;
1946 goto out;
1947 }
1948
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001949 cpmuctrl = 0;
1950 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
1951 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
1952 cpmuctrl = tr32(TG3_CPMU_CTRL);
1953 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
1954 tw32(TG3_CPMU_CTRL,
1955 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
1956 }
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 err = tg3_bmcr_reset(tp);
1959 if (err)
1960 return err;
1961
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001962 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
1963 u32 phy;
1964
1965 phy = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
1966 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, phy);
1967
1968 tw32(TG3_CPMU_CTRL, cpmuctrl);
1969 }
1970
Matt Carlsonbcb37f62008-11-03 16:52:09 -08001971 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
1972 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08001973 u32 val;
1974
1975 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
1976 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
1977 CPMU_LSPD_1000MB_MACCLK_12_5) {
1978 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
1979 udelay(40);
1980 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
1981 }
1982 }
1983
Matt Carlsonecf14102010-01-20 16:58:05 +00001984 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
1985 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES))
1986 return 0;
1987
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001988 tg3_phy_apply_otp(tp);
1989
Matt Carlson6833c042008-11-21 17:18:59 -08001990 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
1991 tg3_phy_toggle_apd(tp, true);
1992 else
1993 tg3_phy_toggle_apd(tp, false);
1994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995out:
1996 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADC_BUG) {
1997 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1998 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
1999 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x2aaa);
2000 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2001 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0323);
2002 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2003 }
2004 if (tp->tg3_flags2 & TG3_FLG2_PHY_5704_A0_BUG) {
2005 tg3_writephy(tp, 0x1c, 0x8d68);
2006 tg3_writephy(tp, 0x1c, 0x8d68);
2007 }
2008 if (tp->tg3_flags2 & TG3_FLG2_PHY_BER_BUG) {
2009 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2010 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2011 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x310b);
2012 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2013 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x9506);
2014 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x401f);
2015 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x14e2);
2016 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
Matt Carlson859a588792010-04-05 10:19:28 +00002017 } else if (tp->tg3_flags2 & TG3_FLG2_PHY_JITTER_BUG) {
Michael Chanc424cb22006-04-29 18:56:34 -07002018 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2019 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Michael Chanc1d2a192007-01-08 19:57:20 -08002020 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADJUST_TRIM) {
2021 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2022 tg3_writephy(tp, MII_TG3_TEST1,
2023 MII_TG3_TEST1_TRIM_EN | 0x4);
2024 } else
2025 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07002026 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 /* Set Extended packet length bit (bit 14) on all chips that */
2029 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002030 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 /* Cannot do read-modify-write on 5401 */
2032 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00002033 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 u32 phy_reg;
2035
2036 /* Set bit 14 with read-modify-write to preserve other bits */
2037 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
2038 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy_reg))
2039 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy_reg | 0x4000);
2040 }
2041
2042 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2043 * jumbo frames transmission.
2044 */
Matt Carlson8f666b02009-08-28 13:58:24 +00002045 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 u32 phy_reg;
2047
2048 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &phy_reg))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002049 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2050 phy_reg | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052
Michael Chan715116a2006-09-27 16:09:25 -07002053 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002054 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002055 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002056 }
2057
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002058 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 tg3_phy_set_wirespeed(tp);
2060 return 0;
2061}
2062
2063static void tg3_frob_aux_power(struct tg3 *tp)
2064{
2065 struct tg3 *tp_peer = tp;
2066
Matt Carlson334355a2010-01-20 16:58:10 +00002067 /* The GPIOs do something completely different on 57765. */
2068 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
2069 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return;
2071
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00002072 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2073 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
2074 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002075 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002077 dev_peer = pci_get_drvdata(tp->pdev_peer);
Michael Chanbc1c7562006-03-20 17:48:03 -08002078 /* remove_one() may have been run on the peer. */
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002079 if (!dev_peer)
Michael Chanbc1c7562006-03-20 17:48:03 -08002080 tp_peer = tp;
2081 else
2082 tp_peer = netdev_priv(dev_peer);
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
Michael Chan6921d202005-12-13 21:15:53 -08002086 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0 ||
2087 (tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
2088 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2090 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002091 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2092 (GRC_LCLCTRL_GPIO_OE0 |
2093 GRC_LCLCTRL_GPIO_OE1 |
2094 GRC_LCLCTRL_GPIO_OE2 |
2095 GRC_LCLCTRL_GPIO_OUTPUT0 |
2096 GRC_LCLCTRL_GPIO_OUTPUT1),
2097 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002098 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2099 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002100 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2101 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2102 GRC_LCLCTRL_GPIO_OE1 |
2103 GRC_LCLCTRL_GPIO_OE2 |
2104 GRC_LCLCTRL_GPIO_OUTPUT0 |
2105 GRC_LCLCTRL_GPIO_OUTPUT1 |
2106 tp->grc_local_ctrl;
2107 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2108
2109 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2110 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2111
2112 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2113 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 } else {
2115 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002116 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 if (tp_peer != tp &&
2119 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2120 return;
2121
Michael Chandc56b7d2005-12-19 16:26:28 -08002122 /* Workaround to prevent overdrawing Amps. */
2123 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2124 ASIC_REV_5714) {
2125 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002126 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2127 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002128 }
2129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 /* On 5753 and variants, GPIO2 cannot be used. */
2131 no_gpio2 = tp->nic_sram_data_cfg &
2132 NIC_SRAM_DATA_CFG_NO_GPIO2;
2133
Michael Chandc56b7d2005-12-19 16:26:28 -08002134 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 GRC_LCLCTRL_GPIO_OE1 |
2136 GRC_LCLCTRL_GPIO_OE2 |
2137 GRC_LCLCTRL_GPIO_OUTPUT1 |
2138 GRC_LCLCTRL_GPIO_OUTPUT2;
2139 if (no_gpio2) {
2140 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2141 GRC_LCLCTRL_GPIO_OUTPUT2);
2142 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002143 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2144 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2147
Michael Chanb401e9e2005-12-19 16:27:04 -08002148 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2149 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 if (!no_gpio2) {
2152 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002153 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2154 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
2156 }
2157 } else {
2158 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2159 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2160 if (tp_peer != tp &&
2161 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2162 return;
2163
Michael Chanb401e9e2005-12-19 16:27:04 -08002164 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2165 (GRC_LCLCTRL_GPIO_OE1 |
2166 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Michael Chanb401e9e2005-12-19 16:27:04 -08002168 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2169 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Michael Chanb401e9e2005-12-19 16:27:04 -08002171 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2172 (GRC_LCLCTRL_GPIO_OE1 |
2173 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
2175 }
2176}
2177
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002178static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2179{
2180 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2181 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002182 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002183 if (speed != SPEED_10)
2184 return 1;
2185 } else if (speed == SPEED_10)
2186 return 1;
2187
2188 return 0;
2189}
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191static int tg3_setup_phy(struct tg3 *, int);
2192
2193#define RESET_KIND_SHUTDOWN 0
2194#define RESET_KIND_INIT 1
2195#define RESET_KIND_SUSPEND 2
2196
2197static void tg3_write_sig_post_reset(struct tg3 *, int);
2198static int tg3_halt_cpu(struct tg3 *, u32);
2199
Matt Carlson0a459aa2008-11-03 16:54:15 -08002200static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002201{
Matt Carlsonce057f02007-11-12 21:08:03 -08002202 u32 val;
2203
Michael Chan51297242007-02-13 12:17:57 -08002204 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
2205 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2206 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2207 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2208
2209 sg_dig_ctrl |=
2210 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2211 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2212 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2213 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002214 return;
Michael Chan51297242007-02-13 12:17:57 -08002215 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002216
Michael Chan60189dd2006-12-17 17:08:07 -08002217 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002218 tg3_bmcr_reset(tp);
2219 val = tr32(GRC_MISC_CFG);
2220 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2221 udelay(40);
2222 return;
Matt Carlson0e5f7842009-11-02 14:26:38 +00002223 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
2224 u32 phytest;
2225 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2226 u32 phy;
2227
2228 tg3_writephy(tp, MII_ADVERTISE, 0);
2229 tg3_writephy(tp, MII_BMCR,
2230 BMCR_ANENABLE | BMCR_ANRESTART);
2231
2232 tg3_writephy(tp, MII_TG3_FET_TEST,
2233 phytest | MII_TG3_FET_SHADOW_EN);
2234 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2235 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2236 tg3_writephy(tp,
2237 MII_TG3_FET_SHDW_AUXMODE4,
2238 phy);
2239 }
2240 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2241 }
2242 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002243 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002244 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2245 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002246
2247 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2248 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2249 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2250 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2251 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002252 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002253
Michael Chan15c3b692006-03-22 01:06:52 -08002254 /* The PHY should not be powered down on some chips because
2255 * of bugs.
2256 */
2257 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2258 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2259 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
2260 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
2261 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002262
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002263 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2264 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002265 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2266 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2267 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2268 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2269 }
2270
Michael Chan15c3b692006-03-22 01:06:52 -08002271 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2272}
2273
Matt Carlson3f007892008-11-03 16:51:36 -08002274/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002275static int tg3_nvram_lock(struct tg3 *tp)
2276{
2277 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2278 int i;
2279
2280 if (tp->nvram_lock_cnt == 0) {
2281 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2282 for (i = 0; i < 8000; i++) {
2283 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2284 break;
2285 udelay(20);
2286 }
2287 if (i == 8000) {
2288 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2289 return -ENODEV;
2290 }
2291 }
2292 tp->nvram_lock_cnt++;
2293 }
2294 return 0;
2295}
2296
2297/* tp->lock is held. */
2298static void tg3_nvram_unlock(struct tg3 *tp)
2299{
2300 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2301 if (tp->nvram_lock_cnt > 0)
2302 tp->nvram_lock_cnt--;
2303 if (tp->nvram_lock_cnt == 0)
2304 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2305 }
2306}
2307
2308/* tp->lock is held. */
2309static void tg3_enable_nvram_access(struct tg3 *tp)
2310{
2311 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002312 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002313 u32 nvaccess = tr32(NVRAM_ACCESS);
2314
2315 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2316 }
2317}
2318
2319/* tp->lock is held. */
2320static void tg3_disable_nvram_access(struct tg3 *tp)
2321{
2322 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002323 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002324 u32 nvaccess = tr32(NVRAM_ACCESS);
2325
2326 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2327 }
2328}
2329
2330static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2331 u32 offset, u32 *val)
2332{
2333 u32 tmp;
2334 int i;
2335
2336 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2337 return -EINVAL;
2338
2339 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2340 EEPROM_ADDR_DEVID_MASK |
2341 EEPROM_ADDR_READ);
2342 tw32(GRC_EEPROM_ADDR,
2343 tmp |
2344 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2345 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2346 EEPROM_ADDR_ADDR_MASK) |
2347 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2348
2349 for (i = 0; i < 1000; i++) {
2350 tmp = tr32(GRC_EEPROM_ADDR);
2351
2352 if (tmp & EEPROM_ADDR_COMPLETE)
2353 break;
2354 msleep(1);
2355 }
2356 if (!(tmp & EEPROM_ADDR_COMPLETE))
2357 return -EBUSY;
2358
Matt Carlson62cedd12009-04-20 14:52:29 -07002359 tmp = tr32(GRC_EEPROM_DATA);
2360
2361 /*
2362 * The data will always be opposite the native endian
2363 * format. Perform a blind byteswap to compensate.
2364 */
2365 *val = swab32(tmp);
2366
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002367 return 0;
2368}
2369
2370#define NVRAM_CMD_TIMEOUT 10000
2371
2372static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2373{
2374 int i;
2375
2376 tw32(NVRAM_CMD, nvram_cmd);
2377 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2378 udelay(10);
2379 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2380 udelay(10);
2381 break;
2382 }
2383 }
2384
2385 if (i == NVRAM_CMD_TIMEOUT)
2386 return -EBUSY;
2387
2388 return 0;
2389}
2390
2391static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2392{
2393 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2394 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2395 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2396 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2397 (tp->nvram_jedecnum == JEDEC_ATMEL))
2398
2399 addr = ((addr / tp->nvram_pagesize) <<
2400 ATMEL_AT45DB0X1B_PAGE_POS) +
2401 (addr % tp->nvram_pagesize);
2402
2403 return addr;
2404}
2405
2406static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2407{
2408 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2409 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2410 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2411 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2412 (tp->nvram_jedecnum == JEDEC_ATMEL))
2413
2414 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2415 tp->nvram_pagesize) +
2416 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2417
2418 return addr;
2419}
2420
Matt Carlsone4f34112009-02-25 14:25:00 +00002421/* NOTE: Data read in from NVRAM is byteswapped according to
2422 * the byteswapping settings for all other register accesses.
2423 * tg3 devices are BE devices, so on a BE machine, the data
2424 * returned will be exactly as it is seen in NVRAM. On a LE
2425 * machine, the 32-bit value will be byteswapped.
2426 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002427static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2428{
2429 int ret;
2430
2431 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2432 return tg3_nvram_read_using_eeprom(tp, offset, val);
2433
2434 offset = tg3_nvram_phys_addr(tp, offset);
2435
2436 if (offset > NVRAM_ADDR_MSK)
2437 return -EINVAL;
2438
2439 ret = tg3_nvram_lock(tp);
2440 if (ret)
2441 return ret;
2442
2443 tg3_enable_nvram_access(tp);
2444
2445 tw32(NVRAM_ADDR, offset);
2446 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2447 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2448
2449 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002450 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002451
2452 tg3_disable_nvram_access(tp);
2453
2454 tg3_nvram_unlock(tp);
2455
2456 return ret;
2457}
2458
Matt Carlsona9dc5292009-02-25 14:25:30 +00002459/* Ensures NVRAM data is in bytestream format. */
2460static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002461{
2462 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002463 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002464 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002465 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002466 return res;
2467}
2468
2469/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002470static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2471{
2472 u32 addr_high, addr_low;
2473 int i;
2474
2475 addr_high = ((tp->dev->dev_addr[0] << 8) |
2476 tp->dev->dev_addr[1]);
2477 addr_low = ((tp->dev->dev_addr[2] << 24) |
2478 (tp->dev->dev_addr[3] << 16) |
2479 (tp->dev->dev_addr[4] << 8) |
2480 (tp->dev->dev_addr[5] << 0));
2481 for (i = 0; i < 4; i++) {
2482 if (i == 1 && skip_mac_1)
2483 continue;
2484 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2485 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2486 }
2487
2488 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2489 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2490 for (i = 0; i < 12; i++) {
2491 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2492 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2493 }
2494 }
2495
2496 addr_high = (tp->dev->dev_addr[0] +
2497 tp->dev->dev_addr[1] +
2498 tp->dev->dev_addr[2] +
2499 tp->dev->dev_addr[3] +
2500 tp->dev->dev_addr[4] +
2501 tp->dev->dev_addr[5]) &
2502 TX_BACKOFF_SEED_MASK;
2503 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2504}
2505
Michael Chanbc1c7562006-03-20 17:48:03 -08002506static int tg3_set_power_state(struct tg3 *tp, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
2508 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002509 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 /* Make sure register accesses (indirect or otherwise)
2512 * will function correctly.
2513 */
2514 pci_write_config_dword(tp->pdev,
2515 TG3PCI_MISC_HOST_CTRL,
2516 tp->misc_host_ctrl);
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 switch (state) {
Michael Chanbc1c7562006-03-20 17:48:03 -08002519 case PCI_D0:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002520 pci_enable_wake(tp->pdev, state, false);
2521 pci_set_power_state(tp->pdev, PCI_D0);
Michael Chan8c6bda12005-04-21 17:09:08 -07002522
Michael Chan9d26e212006-12-07 00:21:14 -08002523 /* Switch out of Vaux if it is a NIC */
2524 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
Michael Chanb401e9e2005-12-19 16:27:04 -08002525 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527 return 0;
2528
Michael Chanbc1c7562006-03-20 17:48:03 -08002529 case PCI_D1:
Michael Chanbc1c7562006-03-20 17:48:03 -08002530 case PCI_D2:
Michael Chanbc1c7562006-03-20 17:48:03 -08002531 case PCI_D3hot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 break;
2533
2534 default:
Joe Perches05dbe002010-02-17 19:44:19 +00002535 netdev_err(tp->dev, "Invalid power state (D%d) requested\n",
2536 state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002538 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002539
2540 /* Restore the CLKREQ setting. */
2541 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2542 u16 lnkctl;
2543
2544 pci_read_config_word(tp->pdev,
2545 tp->pcie_cap + PCI_EXP_LNKCTL,
2546 &lnkctl);
2547 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2548 pci_write_config_word(tp->pdev,
2549 tp->pcie_cap + PCI_EXP_LNKCTL,
2550 lnkctl);
2551 }
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2554 tw32(TG3PCI_MISC_HOST_CTRL,
2555 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2556
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002557 device_should_wake = pci_pme_capable(tp->pdev, state) &&
2558 device_may_wakeup(&tp->pdev->dev) &&
2559 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2560
Matt Carlsondd477002008-05-25 23:45:58 -07002561 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002562 do_low_power = false;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002563 if ((tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) &&
2564 !tp->link_config.phy_is_low_power) {
2565 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002566 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002567
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002568 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002569
2570 tp->link_config.phy_is_low_power = 1;
2571
2572 tp->link_config.orig_speed = phydev->speed;
2573 tp->link_config.orig_duplex = phydev->duplex;
2574 tp->link_config.orig_autoneg = phydev->autoneg;
2575 tp->link_config.orig_advertising = phydev->advertising;
2576
2577 advertising = ADVERTISED_TP |
2578 ADVERTISED_Pause |
2579 ADVERTISED_Autoneg |
2580 ADVERTISED_10baseT_Half;
2581
2582 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002583 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002584 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2585 advertising |=
2586 ADVERTISED_100baseT_Half |
2587 ADVERTISED_100baseT_Full |
2588 ADVERTISED_10baseT_Full;
2589 else
2590 advertising |= ADVERTISED_10baseT_Full;
2591 }
2592
2593 phydev->advertising = advertising;
2594
2595 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002596
2597 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002598 if (phyid != PHY_ID_BCMAC131) {
2599 phyid &= PHY_BCM_OUI_MASK;
2600 if (phyid == PHY_BCM_OUI_1 ||
2601 phyid == PHY_BCM_OUI_2 ||
2602 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002603 do_low_power = true;
2604 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002605 }
Matt Carlsondd477002008-05-25 23:45:58 -07002606 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002607 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002608
Matt Carlsondd477002008-05-25 23:45:58 -07002609 if (tp->link_config.phy_is_low_power == 0) {
2610 tp->link_config.phy_is_low_power = 1;
2611 tp->link_config.orig_speed = tp->link_config.speed;
2612 tp->link_config.orig_duplex = tp->link_config.duplex;
2613 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Matt Carlsondd477002008-05-25 23:45:58 -07002616 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
2617 tp->link_config.speed = SPEED_10;
2618 tp->link_config.duplex = DUPLEX_HALF;
2619 tp->link_config.autoneg = AUTONEG_ENABLE;
2620 tg3_setup_phy(tp, 0);
2621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 }
2623
Michael Chanb5d37722006-09-27 16:06:21 -07002624 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2625 u32 val;
2626
2627 val = tr32(GRC_VCPU_EXT_CTRL);
2628 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2629 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002630 int i;
2631 u32 val;
2632
2633 for (i = 0; i < 200; i++) {
2634 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2635 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2636 break;
2637 msleep(1);
2638 }
2639 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002640 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2641 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2642 WOL_DRV_STATE_SHUTDOWN |
2643 WOL_DRV_WOL |
2644 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002645
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002646 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 u32 mac_mode;
2648
2649 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002650 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002651 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2652 udelay(40);
2653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
Michael Chan3f7045c2006-09-27 16:02:29 -07002655 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
2656 mac_mode = MAC_MODE_PORT_MODE_GMII;
2657 else
2658 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002660 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2661 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2662 ASIC_REV_5700) {
2663 u32 speed = (tp->tg3_flags &
2664 TG3_FLAG_WOL_SPEED_100MB) ?
2665 SPEED_100 : SPEED_10;
2666 if (tg3_5700_link_polarity(tp, speed))
2667 mac_mode |= MAC_MODE_LINK_POLARITY;
2668 else
2669 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 } else {
2672 mac_mode = MAC_MODE_PORT_MODE_TBI;
2673 }
2674
John W. Linvillecbf46852005-04-21 17:01:29 -07002675 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 tw32(MAC_LED_CTRL, tp->led_ctrl);
2677
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002678 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2679 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2680 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2681 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2682 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2683 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Matt Carlson3bda1252008-08-15 14:08:22 -07002685 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
2686 mac_mode |= tp->mac_mode &
2687 (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
2688 if (mac_mode & MAC_MODE_APE_TX_EN)
2689 mac_mode |= MAC_MODE_TDE_ENABLE;
2690 }
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 tw32_f(MAC_MODE, mac_mode);
2693 udelay(100);
2694
2695 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2696 udelay(10);
2697 }
2698
2699 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2700 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2701 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2702 u32 base_val;
2703
2704 base_val = tp->pci_clock_ctrl;
2705 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2706 CLOCK_CTRL_TXCLK_DISABLE);
2707
Michael Chanb401e9e2005-12-19 16:27:04 -08002708 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2709 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002710 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002711 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002712 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002713 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002714 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2716 u32 newbits1, newbits2;
2717
2718 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2719 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2720 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2721 CLOCK_CTRL_TXCLK_DISABLE |
2722 CLOCK_CTRL_ALTCLK);
2723 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2724 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2725 newbits1 = CLOCK_CTRL_625_CORE;
2726 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2727 } else {
2728 newbits1 = CLOCK_CTRL_ALTCLK;
2729 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2730 }
2731
Michael Chanb401e9e2005-12-19 16:27:04 -08002732 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2733 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Michael Chanb401e9e2005-12-19 16:27:04 -08002735 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2736 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2739 u32 newbits3;
2740
2741 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2742 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2743 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2744 CLOCK_CTRL_TXCLK_DISABLE |
2745 CLOCK_CTRL_44MHZ_CORE);
2746 } else {
2747 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2748 }
2749
Michael Chanb401e9e2005-12-19 16:27:04 -08002750 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2751 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
2753 }
2754
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002755 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002756 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002757 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 tg3_frob_aux_power(tp);
2760
2761 /* Workaround for unstable PLL clock */
2762 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2763 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2764 u32 val = tr32(0x7d00);
2765
2766 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2767 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002768 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002769 int err;
2770
2771 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002773 if (!err)
2774 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 }
2777
Michael Chanbbadf502006-04-06 21:46:34 -07002778 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2779
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002780 if (device_should_wake)
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002781 pci_enable_wake(tp->pdev, state, true);
2782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 /* Finally, set the new power state. */
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002784 pci_set_power_state(tp->pdev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return 0;
2787}
2788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2790{
2791 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2792 case MII_TG3_AUX_STAT_10HALF:
2793 *speed = SPEED_10;
2794 *duplex = DUPLEX_HALF;
2795 break;
2796
2797 case MII_TG3_AUX_STAT_10FULL:
2798 *speed = SPEED_10;
2799 *duplex = DUPLEX_FULL;
2800 break;
2801
2802 case MII_TG3_AUX_STAT_100HALF:
2803 *speed = SPEED_100;
2804 *duplex = DUPLEX_HALF;
2805 break;
2806
2807 case MII_TG3_AUX_STAT_100FULL:
2808 *speed = SPEED_100;
2809 *duplex = DUPLEX_FULL;
2810 break;
2811
2812 case MII_TG3_AUX_STAT_1000HALF:
2813 *speed = SPEED_1000;
2814 *duplex = DUPLEX_HALF;
2815 break;
2816
2817 case MII_TG3_AUX_STAT_1000FULL:
2818 *speed = SPEED_1000;
2819 *duplex = DUPLEX_FULL;
2820 break;
2821
2822 default:
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002823 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002824 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2825 SPEED_10;
2826 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2827 DUPLEX_HALF;
2828 break;
2829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 *speed = SPEED_INVALID;
2831 *duplex = DUPLEX_INVALID;
2832 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834}
2835
2836static void tg3_phy_copper_begin(struct tg3 *tp)
2837{
2838 u32 new_adv;
2839 int i;
2840
2841 if (tp->link_config.phy_is_low_power) {
2842 /* Entering low power mode. Disable gigabit and
2843 * 100baseT advertisements.
2844 */
2845 tg3_writephy(tp, MII_TG3_CTRL, 0);
2846
2847 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2848 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2849 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2850 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2851
2852 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2853 } else if (tp->link_config.speed == SPEED_INVALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
2855 tp->link_config.advertising &=
2856 ~(ADVERTISED_1000baseT_Half |
2857 ADVERTISED_1000baseT_Full);
2858
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002859 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2861 new_adv |= ADVERTISE_10HALF;
2862 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2863 new_adv |= ADVERTISE_10FULL;
2864 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2865 new_adv |= ADVERTISE_100HALF;
2866 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2867 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002868
2869 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2872
2873 if (tp->link_config.advertising &
2874 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2875 new_adv = 0;
2876 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2877 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2878 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2879 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
2880 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY) &&
2881 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2882 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2883 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2884 MII_TG3_CTRL_ENABLE_AS_MASTER);
2885 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2886 } else {
2887 tg3_writephy(tp, MII_TG3_CTRL, 0);
2888 }
2889 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002890 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2891 new_adv |= ADVERTISE_CSMA;
2892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 /* Asking for a specific link mode. */
2894 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2896
2897 if (tp->link_config.duplex == DUPLEX_FULL)
2898 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2899 else
2900 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2901 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2902 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2903 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2904 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 if (tp->link_config.speed == SPEED_100) {
2907 if (tp->link_config.duplex == DUPLEX_FULL)
2908 new_adv |= ADVERTISE_100FULL;
2909 else
2910 new_adv |= ADVERTISE_100HALF;
2911 } else {
2912 if (tp->link_config.duplex == DUPLEX_FULL)
2913 new_adv |= ADVERTISE_10FULL;
2914 else
2915 new_adv |= ADVERTISE_10HALF;
2916 }
2917 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002918
2919 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002921
2922 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 }
2924
2925 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
2926 tp->link_config.speed != SPEED_INVALID) {
2927 u32 bmcr, orig_bmcr;
2928
2929 tp->link_config.active_speed = tp->link_config.speed;
2930 tp->link_config.active_duplex = tp->link_config.duplex;
2931
2932 bmcr = 0;
2933 switch (tp->link_config.speed) {
2934 default:
2935 case SPEED_10:
2936 break;
2937
2938 case SPEED_100:
2939 bmcr |= BMCR_SPEED100;
2940 break;
2941
2942 case SPEED_1000:
2943 bmcr |= TG3_BMCR_SPEED1000;
2944 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
2947 if (tp->link_config.duplex == DUPLEX_FULL)
2948 bmcr |= BMCR_FULLDPLX;
2949
2950 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
2951 (bmcr != orig_bmcr)) {
2952 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
2953 for (i = 0; i < 1500; i++) {
2954 u32 tmp;
2955
2956 udelay(10);
2957 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
2958 tg3_readphy(tp, MII_BMSR, &tmp))
2959 continue;
2960 if (!(tmp & BMSR_LSTATUS)) {
2961 udelay(40);
2962 break;
2963 }
2964 }
2965 tg3_writephy(tp, MII_BMCR, bmcr);
2966 udelay(40);
2967 }
2968 } else {
2969 tg3_writephy(tp, MII_BMCR,
2970 BMCR_ANENABLE | BMCR_ANRESTART);
2971 }
2972}
2973
2974static int tg3_init_5401phy_dsp(struct tg3 *tp)
2975{
2976 int err;
2977
2978 /* Turn off tap power management. */
2979 /* Set Extended packet length bit */
2980 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
2981
2982 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0012);
2983 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1804);
2984
2985 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0013);
2986 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1204);
2987
2988 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2989 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0132);
2990
2991 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2992 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0232);
2993
2994 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2995 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0a20);
2996
2997 udelay(40);
2998
2999 return err;
3000}
3001
Michael Chan3600d912006-12-07 00:21:48 -08003002static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003{
Michael Chan3600d912006-12-07 00:21:48 -08003004 u32 adv_reg, all_mask = 0;
3005
3006 if (mask & ADVERTISED_10baseT_Half)
3007 all_mask |= ADVERTISE_10HALF;
3008 if (mask & ADVERTISED_10baseT_Full)
3009 all_mask |= ADVERTISE_10FULL;
3010 if (mask & ADVERTISED_100baseT_Half)
3011 all_mask |= ADVERTISE_100HALF;
3012 if (mask & ADVERTISED_100baseT_Full)
3013 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
3015 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3016 return 0;
3017
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 if ((adv_reg & all_mask) != all_mask)
3019 return 0;
3020 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
3021 u32 tg3_ctrl;
3022
Michael Chan3600d912006-12-07 00:21:48 -08003023 all_mask = 0;
3024 if (mask & ADVERTISED_1000baseT_Half)
3025 all_mask |= ADVERTISE_1000HALF;
3026 if (mask & ADVERTISED_1000baseT_Full)
3027 all_mask |= ADVERTISE_1000FULL;
3028
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3030 return 0;
3031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 if ((tg3_ctrl & all_mask) != all_mask)
3033 return 0;
3034 }
3035 return 1;
3036}
3037
Matt Carlsonef167e22007-12-20 20:10:01 -08003038static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3039{
3040 u32 curadv, reqadv;
3041
3042 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3043 return 1;
3044
3045 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3046 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3047
3048 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3049 if (curadv != reqadv)
3050 return 0;
3051
3052 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3053 tg3_readphy(tp, MII_LPA, rmtadv);
3054 } else {
3055 /* Reprogram the advertisement register, even if it
3056 * does not affect the current link. If the link
3057 * gets renegotiated in the future, we can save an
3058 * additional renegotiation cycle by advertising
3059 * it correctly in the first place.
3060 */
3061 if (curadv != reqadv) {
3062 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3063 ADVERTISE_PAUSE_ASYM);
3064 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3065 }
3066 }
3067
3068 return 1;
3069}
3070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3072{
3073 int current_link_up;
3074 u32 bmsr, dummy;
Matt Carlsonef167e22007-12-20 20:10:01 -08003075 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 u16 current_speed;
3077 u8 current_duplex;
3078 int i, err;
3079
3080 tw32(MAC_EVENT, 0);
3081
3082 tw32_f(MAC_STATUS,
3083 (MAC_STATUS_SYNC_CHANGED |
3084 MAC_STATUS_CFG_CHANGED |
3085 MAC_STATUS_MI_COMPLETION |
3086 MAC_STATUS_LNKSTATE_CHANGED));
3087 udelay(40);
3088
Matt Carlson8ef21422008-05-02 16:47:53 -07003089 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3090 tw32_f(MAC_MI_MODE,
3091 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3092 udelay(80);
3093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
3095 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3096
3097 /* Some third-party PHYs need to be reset on link going
3098 * down.
3099 */
3100 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3101 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3102 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3103 netif_carrier_ok(tp->dev)) {
3104 tg3_readphy(tp, MII_BMSR, &bmsr);
3105 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3106 !(bmsr & BMSR_LSTATUS))
3107 force_reset = 1;
3108 }
3109 if (force_reset)
3110 tg3_phy_reset(tp);
3111
Matt Carlson79eb6902010-02-17 15:17:03 +00003112 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 tg3_readphy(tp, MII_BMSR, &bmsr);
3114 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3115 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3116 bmsr = 0;
3117
3118 if (!(bmsr & BMSR_LSTATUS)) {
3119 err = tg3_init_5401phy_dsp(tp);
3120 if (err)
3121 return err;
3122
3123 tg3_readphy(tp, MII_BMSR, &bmsr);
3124 for (i = 0; i < 1000; i++) {
3125 udelay(10);
3126 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3127 (bmsr & BMSR_LSTATUS)) {
3128 udelay(40);
3129 break;
3130 }
3131 }
3132
Matt Carlson79eb6902010-02-17 15:17:03 +00003133 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3134 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 !(bmsr & BMSR_LSTATUS) &&
3136 tp->link_config.active_speed == SPEED_1000) {
3137 err = tg3_phy_reset(tp);
3138 if (!err)
3139 err = tg3_init_5401phy_dsp(tp);
3140 if (err)
3141 return err;
3142 }
3143 }
3144 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3145 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3146 /* 5701 {A0,B0} CRC bug workaround */
3147 tg3_writephy(tp, 0x15, 0x0a75);
3148 tg3_writephy(tp, 0x1c, 0x8c68);
3149 tg3_writephy(tp, 0x1c, 0x8d68);
3150 tg3_writephy(tp, 0x1c, 0x8c68);
3151 }
3152
3153 /* Clear pending interrupts... */
3154 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3155 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3156
3157 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT)
3158 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003159 else if (!(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3161
3162 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3163 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3164 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3165 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3166 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3167 else
3168 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3169 }
3170
3171 current_link_up = 0;
3172 current_speed = SPEED_INVALID;
3173 current_duplex = DUPLEX_INVALID;
3174
3175 if (tp->tg3_flags2 & TG3_FLG2_CAPACITIVE_COUPLING) {
3176 u32 val;
3177
3178 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3179 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3180 if (!(val & (1 << 10))) {
3181 val |= (1 << 10);
3182 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3183 goto relink;
3184 }
3185 }
3186
3187 bmsr = 0;
3188 for (i = 0; i < 100; i++) {
3189 tg3_readphy(tp, MII_BMSR, &bmsr);
3190 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3191 (bmsr & BMSR_LSTATUS))
3192 break;
3193 udelay(40);
3194 }
3195
3196 if (bmsr & BMSR_LSTATUS) {
3197 u32 aux_stat, bmcr;
3198
3199 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3200 for (i = 0; i < 2000; i++) {
3201 udelay(10);
3202 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3203 aux_stat)
3204 break;
3205 }
3206
3207 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3208 &current_speed,
3209 &current_duplex);
3210
3211 bmcr = 0;
3212 for (i = 0; i < 200; i++) {
3213 tg3_readphy(tp, MII_BMCR, &bmcr);
3214 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3215 continue;
3216 if (bmcr && bmcr != 0x7fff)
3217 break;
3218 udelay(10);
3219 }
3220
Matt Carlsonef167e22007-12-20 20:10:01 -08003221 lcl_adv = 0;
3222 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
Matt Carlsonef167e22007-12-20 20:10:01 -08003224 tp->link_config.active_speed = current_speed;
3225 tp->link_config.active_duplex = current_duplex;
3226
3227 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3228 if ((bmcr & BMCR_ANENABLE) &&
3229 tg3_copper_is_advertising_all(tp,
3230 tp->link_config.advertising)) {
3231 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3232 &rmt_adv))
3233 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 }
3235 } else {
3236 if (!(bmcr & BMCR_ANENABLE) &&
3237 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003238 tp->link_config.duplex == current_duplex &&
3239 tp->link_config.flowctrl ==
3240 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 }
3243 }
3244
Matt Carlsonef167e22007-12-20 20:10:01 -08003245 if (current_link_up == 1 &&
3246 tp->link_config.active_duplex == DUPLEX_FULL)
3247 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 }
3249
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250relink:
Michael Chan6921d202005-12-13 21:15:53 -08003251 if (current_link_up == 0 || tp->link_config.phy_is_low_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 u32 tmp;
3253
3254 tg3_phy_copper_begin(tp);
3255
3256 tg3_readphy(tp, MII_BMSR, &tmp);
3257 if (!tg3_readphy(tp, MII_BMSR, &tmp) &&
3258 (tmp & BMSR_LSTATUS))
3259 current_link_up = 1;
3260 }
3261
3262 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3263 if (current_link_up == 1) {
3264 if (tp->link_config.active_speed == SPEED_100 ||
3265 tp->link_config.active_speed == SPEED_10)
3266 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3267 else
3268 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003269 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)
3270 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3271 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3273
3274 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3275 if (tp->link_config.active_duplex == DUPLEX_HALF)
3276 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3277
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003279 if (current_link_up == 1 &&
3280 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003282 else
3283 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 }
3285
3286 /* ??? Without this setting Netgear GA302T PHY does not
3287 * ??? send/receive packets...
3288 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003289 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3291 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3292 tw32_f(MAC_MI_MODE, tp->mi_mode);
3293 udelay(80);
3294 }
3295
3296 tw32_f(MAC_MODE, tp->mac_mode);
3297 udelay(40);
3298
3299 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3300 /* Polled via timer. */
3301 tw32_f(MAC_EVENT, 0);
3302 } else {
3303 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3304 }
3305 udelay(40);
3306
3307 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3308 current_link_up == 1 &&
3309 tp->link_config.active_speed == SPEED_1000 &&
3310 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3311 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3312 udelay(120);
3313 tw32_f(MAC_STATUS,
3314 (MAC_STATUS_SYNC_CHANGED |
3315 MAC_STATUS_CFG_CHANGED));
3316 udelay(40);
3317 tg3_write_mem(tp,
3318 NIC_SRAM_FIRMWARE_MBOX,
3319 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3320 }
3321
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003322 /* Prevent send BD corruption. */
3323 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3324 u16 oldlnkctl, newlnkctl;
3325
3326 pci_read_config_word(tp->pdev,
3327 tp->pcie_cap + PCI_EXP_LNKCTL,
3328 &oldlnkctl);
3329 if (tp->link_config.active_speed == SPEED_100 ||
3330 tp->link_config.active_speed == SPEED_10)
3331 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3332 else
3333 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3334 if (newlnkctl != oldlnkctl)
3335 pci_write_config_word(tp->pdev,
3336 tp->pcie_cap + PCI_EXP_LNKCTL,
3337 newlnkctl);
3338 }
3339
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 if (current_link_up != netif_carrier_ok(tp->dev)) {
3341 if (current_link_up)
3342 netif_carrier_on(tp->dev);
3343 else
3344 netif_carrier_off(tp->dev);
3345 tg3_link_report(tp);
3346 }
3347
3348 return 0;
3349}
3350
3351struct tg3_fiber_aneginfo {
3352 int state;
3353#define ANEG_STATE_UNKNOWN 0
3354#define ANEG_STATE_AN_ENABLE 1
3355#define ANEG_STATE_RESTART_INIT 2
3356#define ANEG_STATE_RESTART 3
3357#define ANEG_STATE_DISABLE_LINK_OK 4
3358#define ANEG_STATE_ABILITY_DETECT_INIT 5
3359#define ANEG_STATE_ABILITY_DETECT 6
3360#define ANEG_STATE_ACK_DETECT_INIT 7
3361#define ANEG_STATE_ACK_DETECT 8
3362#define ANEG_STATE_COMPLETE_ACK_INIT 9
3363#define ANEG_STATE_COMPLETE_ACK 10
3364#define ANEG_STATE_IDLE_DETECT_INIT 11
3365#define ANEG_STATE_IDLE_DETECT 12
3366#define ANEG_STATE_LINK_OK 13
3367#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3368#define ANEG_STATE_NEXT_PAGE_WAIT 15
3369
3370 u32 flags;
3371#define MR_AN_ENABLE 0x00000001
3372#define MR_RESTART_AN 0x00000002
3373#define MR_AN_COMPLETE 0x00000004
3374#define MR_PAGE_RX 0x00000008
3375#define MR_NP_LOADED 0x00000010
3376#define MR_TOGGLE_TX 0x00000020
3377#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3378#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3379#define MR_LP_ADV_SYM_PAUSE 0x00000100
3380#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3381#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3382#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3383#define MR_LP_ADV_NEXT_PAGE 0x00001000
3384#define MR_TOGGLE_RX 0x00002000
3385#define MR_NP_RX 0x00004000
3386
3387#define MR_LINK_OK 0x80000000
3388
3389 unsigned long link_time, cur_time;
3390
3391 u32 ability_match_cfg;
3392 int ability_match_count;
3393
3394 char ability_match, idle_match, ack_match;
3395
3396 u32 txconfig, rxconfig;
3397#define ANEG_CFG_NP 0x00000080
3398#define ANEG_CFG_ACK 0x00000040
3399#define ANEG_CFG_RF2 0x00000020
3400#define ANEG_CFG_RF1 0x00000010
3401#define ANEG_CFG_PS2 0x00000001
3402#define ANEG_CFG_PS1 0x00008000
3403#define ANEG_CFG_HD 0x00004000
3404#define ANEG_CFG_FD 0x00002000
3405#define ANEG_CFG_INVAL 0x00001f06
3406
3407};
3408#define ANEG_OK 0
3409#define ANEG_DONE 1
3410#define ANEG_TIMER_ENAB 2
3411#define ANEG_FAILED -1
3412
3413#define ANEG_STATE_SETTLE_TIME 10000
3414
3415static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3416 struct tg3_fiber_aneginfo *ap)
3417{
Matt Carlson5be73b42007-12-20 20:09:29 -08003418 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 unsigned long delta;
3420 u32 rx_cfg_reg;
3421 int ret;
3422
3423 if (ap->state == ANEG_STATE_UNKNOWN) {
3424 ap->rxconfig = 0;
3425 ap->link_time = 0;
3426 ap->cur_time = 0;
3427 ap->ability_match_cfg = 0;
3428 ap->ability_match_count = 0;
3429 ap->ability_match = 0;
3430 ap->idle_match = 0;
3431 ap->ack_match = 0;
3432 }
3433 ap->cur_time++;
3434
3435 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3436 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3437
3438 if (rx_cfg_reg != ap->ability_match_cfg) {
3439 ap->ability_match_cfg = rx_cfg_reg;
3440 ap->ability_match = 0;
3441 ap->ability_match_count = 0;
3442 } else {
3443 if (++ap->ability_match_count > 1) {
3444 ap->ability_match = 1;
3445 ap->ability_match_cfg = rx_cfg_reg;
3446 }
3447 }
3448 if (rx_cfg_reg & ANEG_CFG_ACK)
3449 ap->ack_match = 1;
3450 else
3451 ap->ack_match = 0;
3452
3453 ap->idle_match = 0;
3454 } else {
3455 ap->idle_match = 1;
3456 ap->ability_match_cfg = 0;
3457 ap->ability_match_count = 0;
3458 ap->ability_match = 0;
3459 ap->ack_match = 0;
3460
3461 rx_cfg_reg = 0;
3462 }
3463
3464 ap->rxconfig = rx_cfg_reg;
3465 ret = ANEG_OK;
3466
Matt Carlson33f401a2010-04-05 10:19:27 +00003467 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 case ANEG_STATE_UNKNOWN:
3469 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3470 ap->state = ANEG_STATE_AN_ENABLE;
3471
3472 /* fallthru */
3473 case ANEG_STATE_AN_ENABLE:
3474 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3475 if (ap->flags & MR_AN_ENABLE) {
3476 ap->link_time = 0;
3477 ap->cur_time = 0;
3478 ap->ability_match_cfg = 0;
3479 ap->ability_match_count = 0;
3480 ap->ability_match = 0;
3481 ap->idle_match = 0;
3482 ap->ack_match = 0;
3483
3484 ap->state = ANEG_STATE_RESTART_INIT;
3485 } else {
3486 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3487 }
3488 break;
3489
3490 case ANEG_STATE_RESTART_INIT:
3491 ap->link_time = ap->cur_time;
3492 ap->flags &= ~(MR_NP_LOADED);
3493 ap->txconfig = 0;
3494 tw32(MAC_TX_AUTO_NEG, 0);
3495 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3496 tw32_f(MAC_MODE, tp->mac_mode);
3497 udelay(40);
3498
3499 ret = ANEG_TIMER_ENAB;
3500 ap->state = ANEG_STATE_RESTART;
3501
3502 /* fallthru */
3503 case ANEG_STATE_RESTART:
3504 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00003505 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00003507 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 break;
3510
3511 case ANEG_STATE_DISABLE_LINK_OK:
3512 ret = ANEG_DONE;
3513 break;
3514
3515 case ANEG_STATE_ABILITY_DETECT_INIT:
3516 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003517 ap->txconfig = ANEG_CFG_FD;
3518 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3519 if (flowctrl & ADVERTISE_1000XPAUSE)
3520 ap->txconfig |= ANEG_CFG_PS1;
3521 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3522 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3524 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3525 tw32_f(MAC_MODE, tp->mac_mode);
3526 udelay(40);
3527
3528 ap->state = ANEG_STATE_ABILITY_DETECT;
3529 break;
3530
3531 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00003532 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 break;
3535
3536 case ANEG_STATE_ACK_DETECT_INIT:
3537 ap->txconfig |= ANEG_CFG_ACK;
3538 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3539 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3540 tw32_f(MAC_MODE, tp->mac_mode);
3541 udelay(40);
3542
3543 ap->state = ANEG_STATE_ACK_DETECT;
3544
3545 /* fallthru */
3546 case ANEG_STATE_ACK_DETECT:
3547 if (ap->ack_match != 0) {
3548 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3549 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3550 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3551 } else {
3552 ap->state = ANEG_STATE_AN_ENABLE;
3553 }
3554 } else if (ap->ability_match != 0 &&
3555 ap->rxconfig == 0) {
3556 ap->state = ANEG_STATE_AN_ENABLE;
3557 }
3558 break;
3559
3560 case ANEG_STATE_COMPLETE_ACK_INIT:
3561 if (ap->rxconfig & ANEG_CFG_INVAL) {
3562 ret = ANEG_FAILED;
3563 break;
3564 }
3565 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3566 MR_LP_ADV_HALF_DUPLEX |
3567 MR_LP_ADV_SYM_PAUSE |
3568 MR_LP_ADV_ASYM_PAUSE |
3569 MR_LP_ADV_REMOTE_FAULT1 |
3570 MR_LP_ADV_REMOTE_FAULT2 |
3571 MR_LP_ADV_NEXT_PAGE |
3572 MR_TOGGLE_RX |
3573 MR_NP_RX);
3574 if (ap->rxconfig & ANEG_CFG_FD)
3575 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3576 if (ap->rxconfig & ANEG_CFG_HD)
3577 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3578 if (ap->rxconfig & ANEG_CFG_PS1)
3579 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3580 if (ap->rxconfig & ANEG_CFG_PS2)
3581 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3582 if (ap->rxconfig & ANEG_CFG_RF1)
3583 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3584 if (ap->rxconfig & ANEG_CFG_RF2)
3585 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3586 if (ap->rxconfig & ANEG_CFG_NP)
3587 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3588
3589 ap->link_time = ap->cur_time;
3590
3591 ap->flags ^= (MR_TOGGLE_TX);
3592 if (ap->rxconfig & 0x0008)
3593 ap->flags |= MR_TOGGLE_RX;
3594 if (ap->rxconfig & ANEG_CFG_NP)
3595 ap->flags |= MR_NP_RX;
3596 ap->flags |= MR_PAGE_RX;
3597
3598 ap->state = ANEG_STATE_COMPLETE_ACK;
3599 ret = ANEG_TIMER_ENAB;
3600 break;
3601
3602 case ANEG_STATE_COMPLETE_ACK:
3603 if (ap->ability_match != 0 &&
3604 ap->rxconfig == 0) {
3605 ap->state = ANEG_STATE_AN_ENABLE;
3606 break;
3607 }
3608 delta = ap->cur_time - ap->link_time;
3609 if (delta > ANEG_STATE_SETTLE_TIME) {
3610 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3611 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3612 } else {
3613 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3614 !(ap->flags & MR_NP_RX)) {
3615 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3616 } else {
3617 ret = ANEG_FAILED;
3618 }
3619 }
3620 }
3621 break;
3622
3623 case ANEG_STATE_IDLE_DETECT_INIT:
3624 ap->link_time = ap->cur_time;
3625 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3626 tw32_f(MAC_MODE, tp->mac_mode);
3627 udelay(40);
3628
3629 ap->state = ANEG_STATE_IDLE_DETECT;
3630 ret = ANEG_TIMER_ENAB;
3631 break;
3632
3633 case ANEG_STATE_IDLE_DETECT:
3634 if (ap->ability_match != 0 &&
3635 ap->rxconfig == 0) {
3636 ap->state = ANEG_STATE_AN_ENABLE;
3637 break;
3638 }
3639 delta = ap->cur_time - ap->link_time;
3640 if (delta > ANEG_STATE_SETTLE_TIME) {
3641 /* XXX another gem from the Broadcom driver :( */
3642 ap->state = ANEG_STATE_LINK_OK;
3643 }
3644 break;
3645
3646 case ANEG_STATE_LINK_OK:
3647 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3648 ret = ANEG_DONE;
3649 break;
3650
3651 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3652 /* ??? unimplemented */
3653 break;
3654
3655 case ANEG_STATE_NEXT_PAGE_WAIT:
3656 /* ??? unimplemented */
3657 break;
3658
3659 default:
3660 ret = ANEG_FAILED;
3661 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
3664 return ret;
3665}
3666
Matt Carlson5be73b42007-12-20 20:09:29 -08003667static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668{
3669 int res = 0;
3670 struct tg3_fiber_aneginfo aninfo;
3671 int status = ANEG_FAILED;
3672 unsigned int tick;
3673 u32 tmp;
3674
3675 tw32_f(MAC_TX_AUTO_NEG, 0);
3676
3677 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3678 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3679 udelay(40);
3680
3681 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3682 udelay(40);
3683
3684 memset(&aninfo, 0, sizeof(aninfo));
3685 aninfo.flags |= MR_AN_ENABLE;
3686 aninfo.state = ANEG_STATE_UNKNOWN;
3687 aninfo.cur_time = 0;
3688 tick = 0;
3689 while (++tick < 195000) {
3690 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3691 if (status == ANEG_DONE || status == ANEG_FAILED)
3692 break;
3693
3694 udelay(1);
3695 }
3696
3697 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3698 tw32_f(MAC_MODE, tp->mac_mode);
3699 udelay(40);
3700
Matt Carlson5be73b42007-12-20 20:09:29 -08003701 *txflags = aninfo.txconfig;
3702 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
3704 if (status == ANEG_DONE &&
3705 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3706 MR_LP_ADV_FULL_DUPLEX)))
3707 res = 1;
3708
3709 return res;
3710}
3711
3712static void tg3_init_bcm8002(struct tg3 *tp)
3713{
3714 u32 mac_status = tr32(MAC_STATUS);
3715 int i;
3716
3717 /* Reset when initting first time or we have a link. */
3718 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3719 !(mac_status & MAC_STATUS_PCS_SYNCED))
3720 return;
3721
3722 /* Set PLL lock range. */
3723 tg3_writephy(tp, 0x16, 0x8007);
3724
3725 /* SW reset */
3726 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3727
3728 /* Wait for reset to complete. */
3729 /* XXX schedule_timeout() ... */
3730 for (i = 0; i < 500; i++)
3731 udelay(10);
3732
3733 /* Config mode; select PMA/Ch 1 regs. */
3734 tg3_writephy(tp, 0x10, 0x8411);
3735
3736 /* Enable auto-lock and comdet, select txclk for tx. */
3737 tg3_writephy(tp, 0x11, 0x0a10);
3738
3739 tg3_writephy(tp, 0x18, 0x00a0);
3740 tg3_writephy(tp, 0x16, 0x41ff);
3741
3742 /* Assert and deassert POR. */
3743 tg3_writephy(tp, 0x13, 0x0400);
3744 udelay(40);
3745 tg3_writephy(tp, 0x13, 0x0000);
3746
3747 tg3_writephy(tp, 0x11, 0x0a50);
3748 udelay(40);
3749 tg3_writephy(tp, 0x11, 0x0a10);
3750
3751 /* Wait for signal to stabilize */
3752 /* XXX schedule_timeout() ... */
3753 for (i = 0; i < 15000; i++)
3754 udelay(10);
3755
3756 /* Deselect the channel register so we can read the PHYID
3757 * later.
3758 */
3759 tg3_writephy(tp, 0x10, 0x8011);
3760}
3761
3762static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3763{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003764 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 u32 sg_dig_ctrl, sg_dig_status;
3766 u32 serdes_cfg, expected_sg_dig_ctrl;
3767 int workaround, port_a;
3768 int current_link_up;
3769
3770 serdes_cfg = 0;
3771 expected_sg_dig_ctrl = 0;
3772 workaround = 0;
3773 port_a = 1;
3774 current_link_up = 0;
3775
3776 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3777 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3778 workaround = 1;
3779 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3780 port_a = 0;
3781
3782 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3783 /* preserve bits 20-23 for voltage regulator */
3784 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3785 }
3786
3787 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3788
3789 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003790 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 if (workaround) {
3792 u32 val = serdes_cfg;
3793
3794 if (port_a)
3795 val |= 0xc010000;
3796 else
3797 val |= 0x4010000;
3798 tw32_f(MAC_SERDES_CFG, val);
3799 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003800
3801 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 }
3803 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3804 tg3_setup_flow_control(tp, 0, 0);
3805 current_link_up = 1;
3806 }
3807 goto out;
3808 }
3809
3810 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003811 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
Matt Carlson82cd3d12007-12-20 20:09:00 -08003813 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3814 if (flowctrl & ADVERTISE_1000XPAUSE)
3815 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3816 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3817 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
3819 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003820 if ((tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT) &&
3821 tp->serdes_counter &&
3822 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3823 MAC_STATUS_RCVD_CFG)) ==
3824 MAC_STATUS_PCS_SYNCED)) {
3825 tp->serdes_counter--;
3826 current_link_up = 1;
3827 goto out;
3828 }
3829restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 if (workaround)
3831 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003832 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 udelay(5);
3834 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3835
Michael Chan3d3ebe72006-09-27 15:59:15 -07003836 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3837 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3839 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003840 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 mac_status = tr32(MAC_STATUS);
3842
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003843 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003845 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
Matt Carlson82cd3d12007-12-20 20:09:00 -08003847 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3848 local_adv |= ADVERTISE_1000XPAUSE;
3849 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3850 local_adv |= ADVERTISE_1000XPSE_ASYM;
3851
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003852 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003853 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003854 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003855 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
3857 tg3_setup_flow_control(tp, local_adv, remote_adv);
3858 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003859 tp->serdes_counter = 0;
3860 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003861 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003862 if (tp->serdes_counter)
3863 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 else {
3865 if (workaround) {
3866 u32 val = serdes_cfg;
3867
3868 if (port_a)
3869 val |= 0xc010000;
3870 else
3871 val |= 0x4010000;
3872
3873 tw32_f(MAC_SERDES_CFG, val);
3874 }
3875
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003876 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 udelay(40);
3878
3879 /* Link parallel detection - link is up */
3880 /* only if we have PCS_SYNC and not */
3881 /* receiving config code words */
3882 mac_status = tr32(MAC_STATUS);
3883 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3884 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3885 tg3_setup_flow_control(tp, 0, 0);
3886 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003887 tp->tg3_flags2 |=
3888 TG3_FLG2_PARALLEL_DETECT;
3889 tp->serdes_counter =
3890 SERDES_PARALLEL_DET_TIMEOUT;
3891 } else
3892 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 }
3894 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003895 } else {
3896 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3897 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 }
3899
3900out:
3901 return current_link_up;
3902}
3903
3904static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3905{
3906 int current_link_up = 0;
3907
Michael Chan5cf64b8a2007-05-05 12:11:21 -07003908 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
3911 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003912 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003914
Matt Carlson5be73b42007-12-20 20:09:29 -08003915 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3916 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
Matt Carlson5be73b42007-12-20 20:09:29 -08003918 if (txflags & ANEG_CFG_PS1)
3919 local_adv |= ADVERTISE_1000XPAUSE;
3920 if (txflags & ANEG_CFG_PS2)
3921 local_adv |= ADVERTISE_1000XPSE_ASYM;
3922
3923 if (rxflags & MR_LP_ADV_SYM_PAUSE)
3924 remote_adv |= LPA_1000XPAUSE;
3925 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3926 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
3928 tg3_setup_flow_control(tp, local_adv, remote_adv);
3929
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 current_link_up = 1;
3931 }
3932 for (i = 0; i < 30; i++) {
3933 udelay(20);
3934 tw32_f(MAC_STATUS,
3935 (MAC_STATUS_SYNC_CHANGED |
3936 MAC_STATUS_CFG_CHANGED));
3937 udelay(40);
3938 if ((tr32(MAC_STATUS) &
3939 (MAC_STATUS_SYNC_CHANGED |
3940 MAC_STATUS_CFG_CHANGED)) == 0)
3941 break;
3942 }
3943
3944 mac_status = tr32(MAC_STATUS);
3945 if (current_link_up == 0 &&
3946 (mac_status & MAC_STATUS_PCS_SYNCED) &&
3947 !(mac_status & MAC_STATUS_RCVD_CFG))
3948 current_link_up = 1;
3949 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08003950 tg3_setup_flow_control(tp, 0, 0);
3951
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 /* Forcing 1000FD link up. */
3953 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
3955 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
3956 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003957
3958 tw32_f(MAC_MODE, tp->mac_mode);
3959 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 }
3961
3962out:
3963 return current_link_up;
3964}
3965
3966static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
3967{
3968 u32 orig_pause_cfg;
3969 u16 orig_active_speed;
3970 u8 orig_active_duplex;
3971 u32 mac_status;
3972 int current_link_up;
3973 int i;
3974
Matt Carlson8d018622007-12-20 20:05:44 -08003975 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 orig_active_speed = tp->link_config.active_speed;
3977 orig_active_duplex = tp->link_config.active_duplex;
3978
3979 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
3980 netif_carrier_ok(tp->dev) &&
3981 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
3982 mac_status = tr32(MAC_STATUS);
3983 mac_status &= (MAC_STATUS_PCS_SYNCED |
3984 MAC_STATUS_SIGNAL_DET |
3985 MAC_STATUS_CFG_CHANGED |
3986 MAC_STATUS_RCVD_CFG);
3987 if (mac_status == (MAC_STATUS_PCS_SYNCED |
3988 MAC_STATUS_SIGNAL_DET)) {
3989 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3990 MAC_STATUS_CFG_CHANGED));
3991 return 0;
3992 }
3993 }
3994
3995 tw32_f(MAC_TX_AUTO_NEG, 0);
3996
3997 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
3998 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
3999 tw32_f(MAC_MODE, tp->mac_mode);
4000 udelay(40);
4001
Matt Carlson79eb6902010-02-17 15:17:03 +00004002 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 tg3_init_bcm8002(tp);
4004
4005 /* Enable link change event even when serdes polling. */
4006 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4007 udelay(40);
4008
4009 current_link_up = 0;
4010 mac_status = tr32(MAC_STATUS);
4011
4012 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
4013 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4014 else
4015 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4016
Matt Carlson898a56f2009-08-28 14:02:40 +00004017 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004019 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
4021 for (i = 0; i < 100; i++) {
4022 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4023 MAC_STATUS_CFG_CHANGED));
4024 udelay(5);
4025 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004026 MAC_STATUS_CFG_CHANGED |
4027 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 break;
4029 }
4030
4031 mac_status = tr32(MAC_STATUS);
4032 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4033 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004034 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4035 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 tw32_f(MAC_MODE, (tp->mac_mode |
4037 MAC_MODE_SEND_CONFIGS));
4038 udelay(1);
4039 tw32_f(MAC_MODE, tp->mac_mode);
4040 }
4041 }
4042
4043 if (current_link_up == 1) {
4044 tp->link_config.active_speed = SPEED_1000;
4045 tp->link_config.active_duplex = DUPLEX_FULL;
4046 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4047 LED_CTRL_LNKLED_OVERRIDE |
4048 LED_CTRL_1000MBPS_ON));
4049 } else {
4050 tp->link_config.active_speed = SPEED_INVALID;
4051 tp->link_config.active_duplex = DUPLEX_INVALID;
4052 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4053 LED_CTRL_LNKLED_OVERRIDE |
4054 LED_CTRL_TRAFFIC_OVERRIDE));
4055 }
4056
4057 if (current_link_up != netif_carrier_ok(tp->dev)) {
4058 if (current_link_up)
4059 netif_carrier_on(tp->dev);
4060 else
4061 netif_carrier_off(tp->dev);
4062 tg3_link_report(tp);
4063 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004064 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 if (orig_pause_cfg != now_pause_cfg ||
4066 orig_active_speed != tp->link_config.active_speed ||
4067 orig_active_duplex != tp->link_config.active_duplex)
4068 tg3_link_report(tp);
4069 }
4070
4071 return 0;
4072}
4073
Michael Chan747e8f82005-07-25 12:33:22 -07004074static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4075{
4076 int current_link_up, err = 0;
4077 u32 bmsr, bmcr;
4078 u16 current_speed;
4079 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004080 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004081
4082 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4083 tw32_f(MAC_MODE, tp->mac_mode);
4084 udelay(40);
4085
4086 tw32(MAC_EVENT, 0);
4087
4088 tw32_f(MAC_STATUS,
4089 (MAC_STATUS_SYNC_CHANGED |
4090 MAC_STATUS_CFG_CHANGED |
4091 MAC_STATUS_MI_COMPLETION |
4092 MAC_STATUS_LNKSTATE_CHANGED));
4093 udelay(40);
4094
4095 if (force_reset)
4096 tg3_phy_reset(tp);
4097
4098 current_link_up = 0;
4099 current_speed = SPEED_INVALID;
4100 current_duplex = DUPLEX_INVALID;
4101
4102 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4103 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004104 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4105 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4106 bmsr |= BMSR_LSTATUS;
4107 else
4108 bmsr &= ~BMSR_LSTATUS;
4109 }
Michael Chan747e8f82005-07-25 12:33:22 -07004110
4111 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4112
4113 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlson2bd3ed02008-06-09 15:39:55 -07004114 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004115 /* do nothing, just check for link up at the end */
4116 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4117 u32 adv, new_adv;
4118
4119 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4120 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4121 ADVERTISE_1000XPAUSE |
4122 ADVERTISE_1000XPSE_ASYM |
4123 ADVERTISE_SLCT);
4124
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004125 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004126
4127 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4128 new_adv |= ADVERTISE_1000XHALF;
4129 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4130 new_adv |= ADVERTISE_1000XFULL;
4131
4132 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4133 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4134 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4135 tg3_writephy(tp, MII_BMCR, bmcr);
4136
4137 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004138 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Michael Chan747e8f82005-07-25 12:33:22 -07004139 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4140
4141 return err;
4142 }
4143 } else {
4144 u32 new_bmcr;
4145
4146 bmcr &= ~BMCR_SPEED1000;
4147 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4148
4149 if (tp->link_config.duplex == DUPLEX_FULL)
4150 new_bmcr |= BMCR_FULLDPLX;
4151
4152 if (new_bmcr != bmcr) {
4153 /* BMCR_SPEED1000 is a reserved bit that needs
4154 * to be set on write.
4155 */
4156 new_bmcr |= BMCR_SPEED1000;
4157
4158 /* Force a linkdown */
4159 if (netif_carrier_ok(tp->dev)) {
4160 u32 adv;
4161
4162 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4163 adv &= ~(ADVERTISE_1000XFULL |
4164 ADVERTISE_1000XHALF |
4165 ADVERTISE_SLCT);
4166 tg3_writephy(tp, MII_ADVERTISE, adv);
4167 tg3_writephy(tp, MII_BMCR, bmcr |
4168 BMCR_ANRESTART |
4169 BMCR_ANENABLE);
4170 udelay(10);
4171 netif_carrier_off(tp->dev);
4172 }
4173 tg3_writephy(tp, MII_BMCR, new_bmcr);
4174 bmcr = new_bmcr;
4175 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4176 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004177 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4178 ASIC_REV_5714) {
4179 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4180 bmsr |= BMSR_LSTATUS;
4181 else
4182 bmsr &= ~BMSR_LSTATUS;
4183 }
Michael Chan747e8f82005-07-25 12:33:22 -07004184 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4185 }
4186 }
4187
4188 if (bmsr & BMSR_LSTATUS) {
4189 current_speed = SPEED_1000;
4190 current_link_up = 1;
4191 if (bmcr & BMCR_FULLDPLX)
4192 current_duplex = DUPLEX_FULL;
4193 else
4194 current_duplex = DUPLEX_HALF;
4195
Matt Carlsonef167e22007-12-20 20:10:01 -08004196 local_adv = 0;
4197 remote_adv = 0;
4198
Michael Chan747e8f82005-07-25 12:33:22 -07004199 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004200 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004201
4202 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4203 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4204 common = local_adv & remote_adv;
4205 if (common & (ADVERTISE_1000XHALF |
4206 ADVERTISE_1000XFULL)) {
4207 if (common & ADVERTISE_1000XFULL)
4208 current_duplex = DUPLEX_FULL;
4209 else
4210 current_duplex = DUPLEX_HALF;
Matt Carlson859a588792010-04-05 10:19:28 +00004211 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004212 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004213 }
Michael Chan747e8f82005-07-25 12:33:22 -07004214 }
4215 }
4216
Matt Carlsonef167e22007-12-20 20:10:01 -08004217 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4218 tg3_setup_flow_control(tp, local_adv, remote_adv);
4219
Michael Chan747e8f82005-07-25 12:33:22 -07004220 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4221 if (tp->link_config.active_duplex == DUPLEX_HALF)
4222 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4223
4224 tw32_f(MAC_MODE, tp->mac_mode);
4225 udelay(40);
4226
4227 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4228
4229 tp->link_config.active_speed = current_speed;
4230 tp->link_config.active_duplex = current_duplex;
4231
4232 if (current_link_up != netif_carrier_ok(tp->dev)) {
4233 if (current_link_up)
4234 netif_carrier_on(tp->dev);
4235 else {
4236 netif_carrier_off(tp->dev);
4237 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4238 }
4239 tg3_link_report(tp);
4240 }
4241 return err;
4242}
4243
4244static void tg3_serdes_parallel_detect(struct tg3 *tp)
4245{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004246 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004247 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004248 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004249 return;
4250 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004251
Michael Chan747e8f82005-07-25 12:33:22 -07004252 if (!netif_carrier_ok(tp->dev) &&
4253 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4254 u32 bmcr;
4255
4256 tg3_readphy(tp, MII_BMCR, &bmcr);
4257 if (bmcr & BMCR_ANENABLE) {
4258 u32 phy1, phy2;
4259
4260 /* Select shadow register 0x1f */
4261 tg3_writephy(tp, 0x1c, 0x7c00);
4262 tg3_readphy(tp, 0x1c, &phy1);
4263
4264 /* Select expansion interrupt status register */
4265 tg3_writephy(tp, 0x17, 0x0f01);
4266 tg3_readphy(tp, 0x15, &phy2);
4267 tg3_readphy(tp, 0x15, &phy2);
4268
4269 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4270 /* We have signal detect and not receiving
4271 * config code words, link is up by parallel
4272 * detection.
4273 */
4274
4275 bmcr &= ~BMCR_ANENABLE;
4276 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4277 tg3_writephy(tp, MII_BMCR, bmcr);
4278 tp->tg3_flags2 |= TG3_FLG2_PARALLEL_DETECT;
4279 }
4280 }
Matt Carlson859a588792010-04-05 10:19:28 +00004281 } else if (netif_carrier_ok(tp->dev) &&
4282 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
4283 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004284 u32 phy2;
4285
4286 /* Select expansion interrupt status register */
4287 tg3_writephy(tp, 0x17, 0x0f01);
4288 tg3_readphy(tp, 0x15, &phy2);
4289 if (phy2 & 0x20) {
4290 u32 bmcr;
4291
4292 /* Config code words received, turn on autoneg. */
4293 tg3_readphy(tp, MII_BMCR, &bmcr);
4294 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4295
4296 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4297
4298 }
4299 }
4300}
4301
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4303{
4304 int err;
4305
Matt Carlson859a588792010-04-05 10:19:28 +00004306 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004308 else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004309 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004310 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004313 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004314 u32 val, scale;
4315
4316 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4317 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4318 scale = 65;
4319 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4320 scale = 6;
4321 else
4322 scale = 12;
4323
4324 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4325 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4326 tw32(GRC_MISC_CFG, val);
4327 }
4328
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 if (tp->link_config.active_speed == SPEED_1000 &&
4330 tp->link_config.active_duplex == DUPLEX_HALF)
4331 tw32(MAC_TX_LENGTHS,
4332 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4333 (6 << TX_LENGTHS_IPG_SHIFT) |
4334 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4335 else
4336 tw32(MAC_TX_LENGTHS,
4337 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4338 (6 << TX_LENGTHS_IPG_SHIFT) |
4339 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4340
4341 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4342 if (netif_carrier_ok(tp->dev)) {
4343 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004344 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 } else {
4346 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4347 }
4348 }
4349
Matt Carlson8ed5d972007-05-07 00:25:49 -07004350 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4351 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4352 if (!netif_carrier_ok(tp->dev))
4353 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4354 tp->pwrmgmt_thresh;
4355 else
4356 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4357 tw32(PCIE_PWR_MGMT_THRESH, val);
4358 }
4359
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 return err;
4361}
4362
Michael Chandf3e6542006-05-26 17:48:07 -07004363/* This is called whenever we suspect that the system chipset is re-
4364 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4365 * is bogus tx completions. We try to recover by setting the
4366 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4367 * in the workqueue.
4368 */
4369static void tg3_tx_recover(struct tg3 *tp)
4370{
4371 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4372 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4373
Matt Carlson5129c3a2010-04-05 10:19:23 +00004374 netdev_warn(tp->dev,
4375 "The system may be re-ordering memory-mapped I/O "
4376 "cycles to the network device, attempting to recover. "
4377 "Please report the problem to the driver maintainer "
4378 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004379
4380 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004381 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004382 spin_unlock(&tp->lock);
4383}
4384
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004385static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004386{
4387 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004388 return tnapi->tx_pending -
4389 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004390}
4391
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392/* Tigon3 never reports partial packet sends. So we do not
4393 * need special logic to handle SKBs that have not had all
4394 * of their frags sent yet, like SunGEM does.
4395 */
Matt Carlson17375d22009-08-28 14:02:18 +00004396static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397{
Matt Carlson17375d22009-08-28 14:02:18 +00004398 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004399 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004400 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004401 struct netdev_queue *txq;
4402 int index = tnapi - tp->napi;
4403
Matt Carlson19cfaec2009-12-03 08:36:20 +00004404 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004405 index--;
4406
4407 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
4409 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004410 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004412 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
Michael Chandf3e6542006-05-26 17:48:07 -07004414 if (unlikely(skb == NULL)) {
4415 tg3_tx_recover(tp);
4416 return;
4417 }
4418
Alexander Duyckf4188d82009-12-02 16:48:38 +00004419 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004420 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004421 skb_headlen(skb),
4422 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
4424 ri->skb = NULL;
4425
4426 sw_idx = NEXT_TX(sw_idx);
4427
4428 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004429 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004430 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4431 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004432
4433 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004434 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004435 skb_shinfo(skb)->frags[i].size,
4436 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 sw_idx = NEXT_TX(sw_idx);
4438 }
4439
David S. Millerf47c11e2005-06-24 20:18:35 -07004440 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004441
4442 if (unlikely(tx_bug)) {
4443 tg3_tx_recover(tp);
4444 return;
4445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 }
4447
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004448 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
Michael Chan1b2a7202006-08-07 21:46:02 -07004450 /* Need to make the tx_cons update visible to tg3_start_xmit()
4451 * before checking for netif_queue_stopped(). Without the
4452 * memory barrier, there is a small possibility that tg3_start_xmit()
4453 * will miss it and cause the queue to be stopped forever.
4454 */
4455 smp_mb();
4456
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004457 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004458 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004459 __netif_tx_lock(txq, smp_processor_id());
4460 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004461 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004462 netif_tx_wake_queue(txq);
4463 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465}
4466
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004467static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4468{
4469 if (!ri->skb)
4470 return;
4471
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004472 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004473 map_sz, PCI_DMA_FROMDEVICE);
4474 dev_kfree_skb_any(ri->skb);
4475 ri->skb = NULL;
4476}
4477
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478/* Returns size of skb allocated or < 0 on error.
4479 *
4480 * We only need to fill in the address because the other members
4481 * of the RX descriptor are invariant, see tg3_init_rings.
4482 *
4483 * Note the purposeful assymetry of cpu vs. chip accesses. For
4484 * posting buffers we only dirty the first cache line of the RX
4485 * descriptor (containing the address). Whereas for the RX status
4486 * buffers the cpu only reads the last cacheline of the RX descriptor
4487 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4488 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004489static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004490 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491{
4492 struct tg3_rx_buffer_desc *desc;
4493 struct ring_info *map, *src_map;
4494 struct sk_buff *skb;
4495 dma_addr_t mapping;
4496 int skb_size, dest_idx;
4497
4498 src_map = NULL;
4499 switch (opaque_key) {
4500 case RXD_OPAQUE_RING_STD:
4501 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlson21f581a2009-08-28 14:00:25 +00004502 desc = &tpr->rx_std[dest_idx];
4503 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004504 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 break;
4506
4507 case RXD_OPAQUE_RING_JUMBO:
4508 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004509 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004510 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004511 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 break;
4513
4514 default:
4515 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517
4518 /* Do not overwrite any of the map or rp information
4519 * until we are sure we can commit to a new buffer.
4520 *
4521 * Callers depend upon this behavior and assume that
4522 * we leave everything unchanged if we fail.
4523 */
Matt Carlson287be122009-08-28 13:58:46 +00004524 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 if (skb == NULL)
4526 return -ENOMEM;
4527
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 skb_reserve(skb, tp->rx_offset);
4529
Matt Carlson287be122009-08-28 13:58:46 +00004530 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004532 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4533 dev_kfree_skb(skb);
4534 return -EIO;
4535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536
4537 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004538 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 desc->addr_hi = ((u64)mapping >> 32);
4541 desc->addr_lo = ((u64)mapping & 0xffffffff);
4542
4543 return skb_size;
4544}
4545
4546/* We only need to move over in the address because the other
4547 * members of the RX descriptor are invariant. See notes above
4548 * tg3_alloc_rx_skb for full details.
4549 */
Matt Carlsona3896162009-11-13 13:03:44 +00004550static void tg3_recycle_rx(struct tg3_napi *tnapi,
4551 struct tg3_rx_prodring_set *dpr,
4552 u32 opaque_key, int src_idx,
4553 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554{
Matt Carlson17375d22009-08-28 14:02:18 +00004555 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4557 struct ring_info *src_map, *dest_map;
Matt Carlsona3896162009-11-13 13:03:44 +00004558 struct tg3_rx_prodring_set *spr = &tp->prodring[0];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004559 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560
4561 switch (opaque_key) {
4562 case RXD_OPAQUE_RING_STD:
4563 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004564 dest_desc = &dpr->rx_std[dest_idx];
4565 dest_map = &dpr->rx_std_buffers[dest_idx];
4566 src_desc = &spr->rx_std[src_idx];
4567 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 break;
4569
4570 case RXD_OPAQUE_RING_JUMBO:
4571 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004572 dest_desc = &dpr->rx_jmb[dest_idx].std;
4573 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4574 src_desc = &spr->rx_jmb[src_idx].std;
4575 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 break;
4577
4578 default:
4579 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
4582 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004583 dma_unmap_addr_set(dest_map, mapping,
4584 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 dest_desc->addr_hi = src_desc->addr_hi;
4586 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004587
4588 /* Ensure that the update to the skb happens after the physical
4589 * addresses have been transferred to the new BD location.
4590 */
4591 smp_wmb();
4592
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 src_map->skb = NULL;
4594}
4595
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596/* The RX ring scheme is composed of multiple rings which post fresh
4597 * buffers to the chip, and one special ring the chip uses to report
4598 * status back to the host.
4599 *
4600 * The special ring reports the status of received packets to the
4601 * host. The chip does not write into the original descriptor the
4602 * RX buffer was obtained from. The chip simply takes the original
4603 * descriptor as provided by the host, updates the status and length
4604 * field, then writes this into the next status ring entry.
4605 *
4606 * Each ring the host uses to post buffers to the chip is described
4607 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4608 * it is first placed into the on-chip ram. When the packet's length
4609 * is known, it walks down the TG3_BDINFO entries to select the ring.
4610 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4611 * which is within the range of the new packet's length is chosen.
4612 *
4613 * The "separate ring for rx status" scheme may sound queer, but it makes
4614 * sense from a cache coherency perspective. If only the host writes
4615 * to the buffer post rings, and only the chip writes to the rx status
4616 * rings, then cache lines never move beyond shared-modified state.
4617 * If both the host and chip were to write into the same ring, cache line
4618 * eviction could occur since both entities want it in an exclusive state.
4619 */
Matt Carlson17375d22009-08-28 14:02:18 +00004620static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621{
Matt Carlson17375d22009-08-28 14:02:18 +00004622 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004623 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004624 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004625 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004626 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 int received;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004628 struct tg3_rx_prodring_set *tpr = tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004630 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 /*
4632 * We need to order the read of hw_idx and the read of
4633 * the opaque cookie.
4634 */
4635 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 work_mask = 0;
4637 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004638 std_prod_idx = tpr->rx_std_prod_idx;
4639 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004641 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004642 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 unsigned int len;
4644 struct sk_buff *skb;
4645 dma_addr_t dma_addr;
4646 u32 opaque_key, desc_idx, *post_ptr;
Matt Carlson9dc7a112010-04-12 06:58:28 +00004647 bool hw_vlan __maybe_unused = false;
4648 u16 vtag __maybe_unused = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
4650 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4651 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4652 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004653 ri = &tp->prodring[0].rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004654 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004655 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004656 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004657 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004659 ri = &tp->prodring[0].rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004660 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004661 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004662 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004663 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
4666 work_mask |= opaque_key;
4667
4668 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4669 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4670 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004671 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 desc_idx, *post_ptr);
4673 drop_it_no_recycle:
4674 /* Other statistics kept track of by card. */
4675 tp->net_stats.rx_dropped++;
4676 goto next_pkt;
4677 }
4678
Matt Carlsonad829262008-11-21 17:16:16 -08004679 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4680 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681
Matt Carlsond2757fc2010-04-12 06:58:27 +00004682 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 int skb_size;
4684
Matt Carlson86b21e52009-11-13 13:03:45 +00004685 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004686 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 if (skb_size < 0)
4688 goto drop_it;
4689
Matt Carlson287be122009-08-28 13:58:46 +00004690 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 PCI_DMA_FROMDEVICE);
4692
Matt Carlson61e800c2010-02-17 15:16:54 +00004693 /* Ensure that the update to the skb happens
4694 * after the usage of the old DMA mapping.
4695 */
4696 smp_wmb();
4697
4698 ri->skb = NULL;
4699
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 skb_put(skb, len);
4701 } else {
4702 struct sk_buff *copy_skb;
4703
Matt Carlsona3896162009-11-13 13:03:44 +00004704 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 desc_idx, *post_ptr);
4706
Matt Carlson9dc7a112010-04-12 06:58:28 +00004707 copy_skb = netdev_alloc_skb(tp->dev, len + VLAN_HLEN +
4708 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 if (copy_skb == NULL)
4710 goto drop_it_no_recycle;
4711
Matt Carlson9dc7a112010-04-12 06:58:28 +00004712 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 skb_put(copy_skb, len);
4714 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004715 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4717
4718 /* We'll reuse the original ring buffer. */
4719 skb = copy_skb;
4720 }
4721
4722 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4723 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4724 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4725 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4726 skb->ip_summed = CHECKSUM_UNNECESSARY;
4727 else
4728 skb->ip_summed = CHECKSUM_NONE;
4729
4730 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004731
4732 if (len > (tp->dev->mtu + ETH_HLEN) &&
4733 skb->protocol != htons(ETH_P_8021Q)) {
4734 dev_kfree_skb(skb);
4735 goto next_pkt;
4736 }
4737
Matt Carlson9dc7a112010-04-12 06:58:28 +00004738 if (desc->type_flags & RXD_FLAG_VLAN &&
4739 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG)) {
4740 vtag = desc->err_vlan & RXD_VLAN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741#if TG3_VLAN_TAG_USED
Matt Carlson9dc7a112010-04-12 06:58:28 +00004742 if (tp->vlgrp)
4743 hw_vlan = true;
4744 else
4745#endif
4746 {
4747 struct vlan_ethhdr *ve = (struct vlan_ethhdr *)
4748 __skb_push(skb, VLAN_HLEN);
4749
4750 memmove(ve, skb->data + VLAN_HLEN,
4751 ETH_ALEN * 2);
4752 ve->h_vlan_proto = htons(ETH_P_8021Q);
4753 ve->h_vlan_TCI = htons(vtag);
4754 }
4755 }
4756
4757#if TG3_VLAN_TAG_USED
4758 if (hw_vlan)
4759 vlan_gro_receive(&tnapi->napi, tp->vlgrp, vtag, skb);
4760 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761#endif
Matt Carlson17375d22009-08-28 14:02:18 +00004762 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 received++;
4765 budget--;
4766
4767next_pkt:
4768 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004769
4770 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson86cfe4f2010-01-12 10:11:37 +00004771 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
4772 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4773 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07004774 work_mask &= ~RXD_OPAQUE_RING_STD;
4775 rx_std_posted = 0;
4776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004778 sw_idx++;
Eric Dumazet6b31a512007-02-06 13:29:21 -08004779 sw_idx &= (TG3_RX_RCB_RING_SIZE(tp) - 1);
Michael Chan52f6d692005-04-25 15:14:32 -07004780
4781 /* Refresh hw_idx to see if there is new work */
4782 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004783 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004784 rmb();
4785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 }
4787
4788 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004789 tnapi->rx_rcb_ptr = sw_idx;
4790 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
4792 /* Refill RX ring(s). */
Matt Carlsone4af1af2010-02-12 14:47:05 +00004793 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004794 if (work_mask & RXD_OPAQUE_RING_STD) {
4795 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
4796 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4797 tpr->rx_std_prod_idx);
4798 }
4799 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
4800 tpr->rx_jmb_prod_idx = jmb_prod_idx %
4801 TG3_RX_JUMBO_RING_SIZE;
4802 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
4803 tpr->rx_jmb_prod_idx);
4804 }
4805 mmiowb();
4806 } else if (work_mask) {
4807 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
4808 * updated before the producer indices can be updated.
4809 */
4810 smp_wmb();
4811
Matt Carlson43619352009-11-13 13:03:47 +00004812 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
Matt Carlson43619352009-11-13 13:03:47 +00004813 tpr->rx_jmb_prod_idx = jmb_prod_idx % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004814
Matt Carlsone4af1af2010-02-12 14:47:05 +00004815 if (tnapi != &tp->napi[1])
4816 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
4819 return received;
4820}
4821
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004822static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 /* handle link change and other phy events */
4825 if (!(tp->tg3_flags &
4826 (TG3_FLAG_USE_LINKCHG_REG |
4827 TG3_FLAG_POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004828 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
4829
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 if (sblk->status & SD_STATUS_LINK_CHG) {
4831 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004832 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004833 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004834 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4835 tw32_f(MAC_STATUS,
4836 (MAC_STATUS_SYNC_CHANGED |
4837 MAC_STATUS_CFG_CHANGED |
4838 MAC_STATUS_MI_COMPLETION |
4839 MAC_STATUS_LNKSTATE_CHANGED));
4840 udelay(40);
4841 } else
4842 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004843 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 }
4845 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004846}
4847
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004848static int tg3_rx_prodring_xfer(struct tg3 *tp,
4849 struct tg3_rx_prodring_set *dpr,
4850 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004851{
4852 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004853 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004854
4855 while (1) {
4856 src_prod_idx = spr->rx_std_prod_idx;
4857
4858 /* Make sure updates to the rx_std_buffers[] entries and the
4859 * standard producer index are seen in the correct order.
4860 */
4861 smp_rmb();
4862
4863 if (spr->rx_std_cons_idx == src_prod_idx)
4864 break;
4865
4866 if (spr->rx_std_cons_idx < src_prod_idx)
4867 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
4868 else
4869 cpycnt = TG3_RX_RING_SIZE - spr->rx_std_cons_idx;
4870
4871 cpycnt = min(cpycnt, TG3_RX_RING_SIZE - dpr->rx_std_prod_idx);
4872
4873 si = spr->rx_std_cons_idx;
4874 di = dpr->rx_std_prod_idx;
4875
Matt Carlsone92967b2010-02-12 14:47:06 +00004876 for (i = di; i < di + cpycnt; i++) {
4877 if (dpr->rx_std_buffers[i].skb) {
4878 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004879 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00004880 break;
4881 }
4882 }
4883
4884 if (!cpycnt)
4885 break;
4886
4887 /* Ensure that updates to the rx_std_buffers ring and the
4888 * shadowed hardware producer ring from tg3_recycle_skb() are
4889 * ordered correctly WRT the skb check above.
4890 */
4891 smp_rmb();
4892
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004893 memcpy(&dpr->rx_std_buffers[di],
4894 &spr->rx_std_buffers[si],
4895 cpycnt * sizeof(struct ring_info));
4896
4897 for (i = 0; i < cpycnt; i++, di++, si++) {
4898 struct tg3_rx_buffer_desc *sbd, *dbd;
4899 sbd = &spr->rx_std[si];
4900 dbd = &dpr->rx_std[di];
4901 dbd->addr_hi = sbd->addr_hi;
4902 dbd->addr_lo = sbd->addr_lo;
4903 }
4904
4905 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) %
4906 TG3_RX_RING_SIZE;
4907 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) %
4908 TG3_RX_RING_SIZE;
4909 }
4910
4911 while (1) {
4912 src_prod_idx = spr->rx_jmb_prod_idx;
4913
4914 /* Make sure updates to the rx_jmb_buffers[] entries and
4915 * the jumbo producer index are seen in the correct order.
4916 */
4917 smp_rmb();
4918
4919 if (spr->rx_jmb_cons_idx == src_prod_idx)
4920 break;
4921
4922 if (spr->rx_jmb_cons_idx < src_prod_idx)
4923 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
4924 else
4925 cpycnt = TG3_RX_JUMBO_RING_SIZE - spr->rx_jmb_cons_idx;
4926
4927 cpycnt = min(cpycnt,
4928 TG3_RX_JUMBO_RING_SIZE - dpr->rx_jmb_prod_idx);
4929
4930 si = spr->rx_jmb_cons_idx;
4931 di = dpr->rx_jmb_prod_idx;
4932
Matt Carlsone92967b2010-02-12 14:47:06 +00004933 for (i = di; i < di + cpycnt; i++) {
4934 if (dpr->rx_jmb_buffers[i].skb) {
4935 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004936 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00004937 break;
4938 }
4939 }
4940
4941 if (!cpycnt)
4942 break;
4943
4944 /* Ensure that updates to the rx_jmb_buffers ring and the
4945 * shadowed hardware producer ring from tg3_recycle_skb() are
4946 * ordered correctly WRT the skb check above.
4947 */
4948 smp_rmb();
4949
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004950 memcpy(&dpr->rx_jmb_buffers[di],
4951 &spr->rx_jmb_buffers[si],
4952 cpycnt * sizeof(struct ring_info));
4953
4954 for (i = 0; i < cpycnt; i++, di++, si++) {
4955 struct tg3_rx_buffer_desc *sbd, *dbd;
4956 sbd = &spr->rx_jmb[si].std;
4957 dbd = &dpr->rx_jmb[di].std;
4958 dbd->addr_hi = sbd->addr_hi;
4959 dbd->addr_lo = sbd->addr_lo;
4960 }
4961
4962 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) %
4963 TG3_RX_JUMBO_RING_SIZE;
4964 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) %
4965 TG3_RX_JUMBO_RING_SIZE;
4966 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004967
4968 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004969}
4970
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004971static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
4972{
4973 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974
4975 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004976 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00004977 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004978 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07004979 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 }
4981
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 /* run RX thread, within the bounds set by NAPI.
4983 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004984 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004986 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00004987 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004989 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00004990 struct tg3_rx_prodring_set *dpr = &tp->prodring[0];
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004991 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00004992 u32 std_prod_idx = dpr->rx_std_prod_idx;
4993 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004994
Matt Carlsone4af1af2010-02-12 14:47:05 +00004995 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004996 err |= tg3_rx_prodring_xfer(tp, dpr,
4997 tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004998
4999 wmb();
5000
Matt Carlsone4af1af2010-02-12 14:47:05 +00005001 if (std_prod_idx != dpr->rx_std_prod_idx)
5002 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5003 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005004
Matt Carlsone4af1af2010-02-12 14:47:05 +00005005 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5006 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5007 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005008
5009 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005010
5011 if (err)
5012 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005013 }
5014
David S. Miller6f535762007-10-11 18:08:29 -07005015 return work_done;
5016}
David S. Millerf7383c22005-05-18 22:50:53 -07005017
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005018static int tg3_poll_msix(struct napi_struct *napi, int budget)
5019{
5020 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5021 struct tg3 *tp = tnapi->tp;
5022 int work_done = 0;
5023 struct tg3_hw_status *sblk = tnapi->hw_status;
5024
5025 while (1) {
5026 work_done = tg3_poll_work(tnapi, work_done, budget);
5027
5028 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5029 goto tx_recovery;
5030
5031 if (unlikely(work_done >= budget))
5032 break;
5033
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005034 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005035 * to tell the hw how much work has been processed,
5036 * so we must read it before checking for more work.
5037 */
5038 tnapi->last_tag = sblk->status_tag;
5039 tnapi->last_irq_tag = tnapi->last_tag;
5040 rmb();
5041
5042 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005043 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5044 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005045 napi_complete(napi);
5046 /* Reenable interrupts. */
5047 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5048 mmiowb();
5049 break;
5050 }
5051 }
5052
5053 return work_done;
5054
5055tx_recovery:
5056 /* work_done is guaranteed to be less than budget. */
5057 napi_complete(napi);
5058 schedule_work(&tp->reset_task);
5059 return work_done;
5060}
5061
David S. Miller6f535762007-10-11 18:08:29 -07005062static int tg3_poll(struct napi_struct *napi, int budget)
5063{
Matt Carlson8ef04422009-08-28 14:01:37 +00005064 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5065 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005066 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005067 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005068
5069 while (1) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005070 tg3_poll_link(tp);
5071
Matt Carlson17375d22009-08-28 14:02:18 +00005072 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005073
5074 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5075 goto tx_recovery;
5076
5077 if (unlikely(work_done >= budget))
5078 break;
5079
Michael Chan4fd7ab52007-10-12 01:39:50 -07005080 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00005081 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005082 * to tell the hw how much work has been processed,
5083 * so we must read it before checking for more work.
5084 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005085 tnapi->last_tag = sblk->status_tag;
5086 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005087 rmb();
5088 } else
5089 sblk->status &= ~SD_STATUS_UPDATED;
5090
Matt Carlson17375d22009-08-28 14:02:18 +00005091 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005092 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005093 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005094 break;
5095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 }
5097
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005098 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005099
5100tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005101 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005102 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005103 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005104 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105}
5106
David S. Millerf47c11e2005-06-24 20:18:35 -07005107static void tg3_irq_quiesce(struct tg3 *tp)
5108{
Matt Carlson4f125f42009-09-01 12:55:02 +00005109 int i;
5110
David S. Millerf47c11e2005-06-24 20:18:35 -07005111 BUG_ON(tp->irq_sync);
5112
5113 tp->irq_sync = 1;
5114 smp_mb();
5115
Matt Carlson4f125f42009-09-01 12:55:02 +00005116 for (i = 0; i < tp->irq_cnt; i++)
5117 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005118}
5119
5120static inline int tg3_irq_sync(struct tg3 *tp)
5121{
5122 return tp->irq_sync;
5123}
5124
5125/* Fully shutdown all tg3 driver activity elsewhere in the system.
5126 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5127 * with as well. Most of the time, this is not necessary except when
5128 * shutting down the device.
5129 */
5130static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5131{
Michael Chan46966542007-07-11 19:47:19 -07005132 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005133 if (irq_sync)
5134 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005135}
5136
5137static inline void tg3_full_unlock(struct tg3 *tp)
5138{
David S. Millerf47c11e2005-06-24 20:18:35 -07005139 spin_unlock_bh(&tp->lock);
5140}
5141
Michael Chanfcfa0a32006-03-20 22:28:41 -08005142/* One-shot MSI handler - Chip automatically disables interrupt
5143 * after sending MSI so driver doesn't have to do it.
5144 */
David Howells7d12e782006-10-05 14:55:46 +01005145static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005146{
Matt Carlson09943a12009-08-28 14:01:57 +00005147 struct tg3_napi *tnapi = dev_id;
5148 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005149
Matt Carlson898a56f2009-08-28 14:02:40 +00005150 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005151 if (tnapi->rx_rcb)
5152 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005153
5154 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005155 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005156
5157 return IRQ_HANDLED;
5158}
5159
Michael Chan88b06bc22005-04-21 17:13:25 -07005160/* MSI ISR - No need to check for interrupt sharing and no need to
5161 * flush status block and interrupt mailbox. PCI ordering rules
5162 * guarantee that MSI will arrive after the status block.
5163 */
David Howells7d12e782006-10-05 14:55:46 +01005164static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005165{
Matt Carlson09943a12009-08-28 14:01:57 +00005166 struct tg3_napi *tnapi = dev_id;
5167 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005168
Matt Carlson898a56f2009-08-28 14:02:40 +00005169 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005170 if (tnapi->rx_rcb)
5171 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005172 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005173 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005174 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005175 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005176 * NIC to stop sending us irqs, engaging "in-intr-handler"
5177 * event coalescing.
5178 */
5179 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005180 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005181 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005182
Michael Chan88b06bc22005-04-21 17:13:25 -07005183 return IRQ_RETVAL(1);
5184}
5185
David Howells7d12e782006-10-05 14:55:46 +01005186static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187{
Matt Carlson09943a12009-08-28 14:01:57 +00005188 struct tg3_napi *tnapi = dev_id;
5189 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005190 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 unsigned int handled = 1;
5192
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 /* In INTx mode, it is possible for the interrupt to arrive at
5194 * the CPU before the status block posted prior to the interrupt.
5195 * Reading the PCI State register will confirm whether the
5196 * interrupt is ours and will flush the status block.
5197 */
Michael Chand18edcb2007-03-24 20:57:11 -07005198 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5199 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5200 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5201 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005202 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005203 }
Michael Chand18edcb2007-03-24 20:57:11 -07005204 }
5205
5206 /*
5207 * Writing any value to intr-mbox-0 clears PCI INTA# and
5208 * chip-internal interrupt pending events.
5209 * Writing non-zero to intr-mbox-0 additional tells the
5210 * NIC to stop sending us irqs, engaging "in-intr-handler"
5211 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005212 *
5213 * Flush the mailbox to de-assert the IRQ immediately to prevent
5214 * spurious interrupts. The flush impacts performance but
5215 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005216 */
Michael Chanc04cb342007-05-07 00:26:15 -07005217 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005218 if (tg3_irq_sync(tp))
5219 goto out;
5220 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005221 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005222 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005223 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005224 } else {
5225 /* No work, shared interrupt perhaps? re-enable
5226 * interrupts, and flush that PCI write
5227 */
5228 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5229 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005230 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005231out:
David S. Millerfac9b832005-05-18 22:46:34 -07005232 return IRQ_RETVAL(handled);
5233}
5234
David Howells7d12e782006-10-05 14:55:46 +01005235static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005236{
Matt Carlson09943a12009-08-28 14:01:57 +00005237 struct tg3_napi *tnapi = dev_id;
5238 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005239 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005240 unsigned int handled = 1;
5241
David S. Millerfac9b832005-05-18 22:46:34 -07005242 /* In INTx mode, it is possible for the interrupt to arrive at
5243 * the CPU before the status block posted prior to the interrupt.
5244 * Reading the PCI State register will confirm whether the
5245 * interrupt is ours and will flush the status block.
5246 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005247 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07005248 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5249 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5250 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005251 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 }
Michael Chand18edcb2007-03-24 20:57:11 -07005253 }
5254
5255 /*
5256 * writing any value to intr-mbox-0 clears PCI INTA# and
5257 * chip-internal interrupt pending events.
5258 * writing non-zero to intr-mbox-0 additional tells the
5259 * NIC to stop sending us irqs, engaging "in-intr-handler"
5260 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005261 *
5262 * Flush the mailbox to de-assert the IRQ immediately to prevent
5263 * spurious interrupts. The flush impacts performance but
5264 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005265 */
Michael Chanc04cb342007-05-07 00:26:15 -07005266 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005267
5268 /*
5269 * In a shared interrupt configuration, sometimes other devices'
5270 * interrupts will scream. We record the current status tag here
5271 * so that the above check can report that the screaming interrupts
5272 * are unhandled. Eventually they will be silenced.
5273 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005274 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005275
Michael Chand18edcb2007-03-24 20:57:11 -07005276 if (tg3_irq_sync(tp))
5277 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005278
Matt Carlson72334482009-08-28 14:03:01 +00005279 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005280
Matt Carlson09943a12009-08-28 14:01:57 +00005281 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005282
David S. Millerf47c11e2005-06-24 20:18:35 -07005283out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 return IRQ_RETVAL(handled);
5285}
5286
Michael Chan79381092005-04-21 17:13:59 -07005287/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005288static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005289{
Matt Carlson09943a12009-08-28 14:01:57 +00005290 struct tg3_napi *tnapi = dev_id;
5291 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005292 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005293
Michael Chanf9804dd2005-09-27 12:13:10 -07005294 if ((sblk->status & SD_STATUS_UPDATED) ||
5295 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005296 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005297 return IRQ_RETVAL(1);
5298 }
5299 return IRQ_RETVAL(0);
5300}
5301
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005302static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005303static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304
Michael Chanb9ec6c12006-07-25 16:37:27 -07005305/* Restart hardware after configuration changes, self-test, etc.
5306 * Invoked with tp->lock held.
5307 */
5308static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005309 __releases(tp->lock)
5310 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005311{
5312 int err;
5313
5314 err = tg3_init_hw(tp, reset_phy);
5315 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005316 netdev_err(tp->dev,
5317 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005318 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5319 tg3_full_unlock(tp);
5320 del_timer_sync(&tp->timer);
5321 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005322 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005323 dev_close(tp->dev);
5324 tg3_full_lock(tp, 0);
5325 }
5326 return err;
5327}
5328
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329#ifdef CONFIG_NET_POLL_CONTROLLER
5330static void tg3_poll_controller(struct net_device *dev)
5331{
Matt Carlson4f125f42009-09-01 12:55:02 +00005332 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005333 struct tg3 *tp = netdev_priv(dev);
5334
Matt Carlson4f125f42009-09-01 12:55:02 +00005335 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005336 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337}
5338#endif
5339
David Howellsc4028952006-11-22 14:57:56 +00005340static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341{
David Howellsc4028952006-11-22 14:57:56 +00005342 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005343 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 unsigned int restart_timer;
5345
Michael Chan7faa0062006-02-02 17:29:28 -08005346 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005347
5348 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005349 tg3_full_unlock(tp);
5350 return;
5351 }
5352
5353 tg3_full_unlock(tp);
5354
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005355 tg3_phy_stop(tp);
5356
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 tg3_netif_stop(tp);
5358
David S. Millerf47c11e2005-06-24 20:18:35 -07005359 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360
5361 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5362 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5363
Michael Chandf3e6542006-05-26 17:48:07 -07005364 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5365 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5366 tp->write32_rx_mbox = tg3_write_flush_reg32;
5367 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5368 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5369 }
5370
Michael Chan944d9802005-05-29 14:57:48 -07005371 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005372 err = tg3_init_hw(tp, 1);
5373 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005374 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375
5376 tg3_netif_start(tp);
5377
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 if (restart_timer)
5379 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005380
Michael Chanb9ec6c12006-07-25 16:37:27 -07005381out:
Michael Chan7faa0062006-02-02 17:29:28 -08005382 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005383
5384 if (!err)
5385 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386}
5387
Michael Chanb0408752007-02-13 12:18:30 -08005388static void tg3_dump_short_state(struct tg3 *tp)
5389{
Joe Perches05dbe002010-02-17 19:44:19 +00005390 netdev_err(tp->dev, "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5391 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5392 netdev_err(tp->dev, "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5393 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
Michael Chanb0408752007-02-13 12:18:30 -08005394}
5395
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396static void tg3_tx_timeout(struct net_device *dev)
5397{
5398 struct tg3 *tp = netdev_priv(dev);
5399
Michael Chanb0408752007-02-13 12:18:30 -08005400 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005401 netdev_err(dev, "transmit timed out, resetting\n");
Michael Chanb0408752007-02-13 12:18:30 -08005402 tg3_dump_short_state(tp);
5403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404
5405 schedule_work(&tp->reset_task);
5406}
5407
Michael Chanc58ec932005-09-17 00:46:27 -07005408/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5409static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5410{
5411 u32 base = (u32) mapping & 0xffffffff;
5412
5413 return ((base > 0xffffdcc0) &&
5414 (base + len + 8 < base));
5415}
5416
Michael Chan72f2afb2006-03-06 19:28:35 -08005417/* Test for DMA addresses > 40-bit */
5418static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5419 int len)
5420{
5421#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005422 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Yang Hongyang50cf1562009-04-06 19:01:14 -07005423 return (((u64) mapping + len) > DMA_BIT_MASK(40));
Michael Chan72f2afb2006-03-06 19:28:35 -08005424 return 0;
5425#else
5426 return 0;
5427#endif
5428}
5429
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005430static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431
Michael Chan72f2afb2006-03-06 19:28:35 -08005432/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005433static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5434 struct sk_buff *skb, u32 last_plus_one,
5435 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005437 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005438 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005439 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005441 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
Matt Carlson41588ba2008-04-19 18:12:33 -07005443 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5444 new_skb = skb_copy(skb, GFP_ATOMIC);
5445 else {
5446 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5447
5448 new_skb = skb_copy_expand(skb,
5449 skb_headroom(skb) + more_headroom,
5450 skb_tailroom(skb), GFP_ATOMIC);
5451 }
5452
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005454 ret = -1;
5455 } else {
5456 /* New SKB is guaranteed to be linear. */
5457 entry = *start;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005458 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5459 PCI_DMA_TODEVICE);
5460 /* Make sure the mapping succeeded */
5461 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5462 ret = -1;
5463 dev_kfree_skb(new_skb);
5464 new_skb = NULL;
David S. Miller90079ce2008-09-11 04:52:51 -07005465
Michael Chanc58ec932005-09-17 00:46:27 -07005466 /* Make sure new skb does not cross any 4G boundaries.
5467 * Drop the packet if it does.
5468 */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005469 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5470 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5471 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5472 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005473 ret = -1;
5474 dev_kfree_skb(new_skb);
5475 new_skb = NULL;
5476 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005477 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005478 base_flags, 1 | (mss << 1));
5479 *start = NEXT_TX(entry);
5480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 }
5482
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 /* Now clean up the sw ring entries. */
5484 i = 0;
5485 while (entry != last_plus_one) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005486 int len;
5487
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005488 if (i == 0)
Alexander Duyckf4188d82009-12-02 16:48:38 +00005489 len = skb_headlen(skb);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005490 else
Alexander Duyckf4188d82009-12-02 16:48:38 +00005491 len = skb_shinfo(skb)->frags[i-1].size;
5492
5493 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005494 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005495 mapping),
5496 len, PCI_DMA_TODEVICE);
5497 if (i == 0) {
5498 tnapi->tx_buffers[entry].skb = new_skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005499 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005500 new_addr);
5501 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005502 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 entry = NEXT_TX(entry);
5505 i++;
5506 }
5507
5508 dev_kfree_skb(skb);
5509
Michael Chanc58ec932005-09-17 00:46:27 -07005510 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511}
5512
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005513static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 dma_addr_t mapping, int len, u32 flags,
5515 u32 mss_and_is_end)
5516{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005517 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 int is_end = (mss_and_is_end & 0x1);
5519 u32 mss = (mss_and_is_end >> 1);
5520 u32 vlan_tag = 0;
5521
5522 if (is_end)
5523 flags |= TXD_FLAG_END;
5524 if (flags & TXD_FLAG_VLAN) {
5525 vlan_tag = flags >> 16;
5526 flags &= 0xffff;
5527 }
5528 vlan_tag |= (mss << TXD_MSS_SHIFT);
5529
5530 txd->addr_hi = ((u64) mapping >> 32);
5531 txd->addr_lo = ((u64) mapping & 0xffffffff);
5532 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5533 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5534}
5535
Michael Chan5a6f3072006-03-20 22:28:05 -08005536/* hard_start_xmit for devices that don't have any bugs and
Matt Carlsone849cdc2009-11-13 13:03:38 +00005537 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005538 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005539static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5540 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541{
5542 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005544 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005545 struct tg3_napi *tnapi;
5546 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005547 unsigned int i, last;
5548
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005549 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5550 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005551 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005552 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005553
Michael Chan00b70502006-06-17 21:58:45 -07005554 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005555 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005556 * interrupt. Furthermore, IRQ processing runs lockless so we have
5557 * no IRQ context deadlocks to worry about either. Rejoice!
5558 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005559 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005560 if (!netif_tx_queue_stopped(txq)) {
5561 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005562
5563 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005564 netdev_err(dev,
5565 "BUG! Tx Ring full when queue awake!\n");
Michael Chan5a6f3072006-03-20 22:28:05 -08005566 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005567 return NETDEV_TX_BUSY;
5568 }
5569
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005570 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005571 base_flags = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005572 mss = 0;
Matt Carlsonc13e3712007-05-05 11:50:04 -07005573 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005574 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005575 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005576
5577 if (skb_header_cloned(skb) &&
5578 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5579 dev_kfree_skb(skb);
5580 goto out_unlock;
5581 }
5582
Michael Chanb0026622006-07-03 19:42:14 -07005583 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005584 hdrlen = skb_headlen(skb) - ETH_HLEN;
Michael Chanb0026622006-07-03 19:42:14 -07005585 else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005586 struct iphdr *iph = ip_hdr(skb);
5587
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005588 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005589 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005590
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005591 iph->check = 0;
5592 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005593 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005594 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005595
Matt Carlsone849cdc2009-11-13 13:03:38 +00005596 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005597 mss |= (hdrlen & 0xc) << 12;
5598 if (hdrlen & 0x10)
5599 base_flags |= 0x00000010;
5600 base_flags |= (hdrlen & 0x3e0) << 5;
5601 } else
5602 mss |= hdrlen << 9;
5603
Michael Chan5a6f3072006-03-20 22:28:05 -08005604 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5605 TXD_FLAG_CPU_POST_DMA);
5606
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005607 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005608
Matt Carlson859a588792010-04-05 10:19:28 +00005609 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005610 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson859a588792010-04-05 10:19:28 +00005611 }
5612
Michael Chan5a6f3072006-03-20 22:28:05 -08005613#if TG3_VLAN_TAG_USED
5614 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5615 base_flags |= (TXD_FLAG_VLAN |
5616 (vlan_tx_tag_get(skb) << 16));
5617#endif
5618
Alexander Duyckf4188d82009-12-02 16:48:38 +00005619 len = skb_headlen(skb);
5620
5621 /* Queue skb data, a.k.a. the main skb fragment. */
5622 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5623 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005624 dev_kfree_skb(skb);
5625 goto out_unlock;
5626 }
5627
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005628 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005629 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005630
Matt Carlsonb703df62009-12-03 08:36:21 +00005631 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005632 !mss && skb->len > ETH_DATA_LEN)
5633 base_flags |= TXD_FLAG_JMB_PKT;
5634
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005635 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005636 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5637
5638 entry = NEXT_TX(entry);
5639
5640 /* Now loop through additional data fragments, and queue them. */
5641 if (skb_shinfo(skb)->nr_frags > 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005642 last = skb_shinfo(skb)->nr_frags - 1;
5643 for (i = 0; i <= last; i++) {
5644 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5645
5646 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005647 mapping = pci_map_page(tp->pdev,
5648 frag->page,
5649 frag->page_offset,
5650 len, PCI_DMA_TODEVICE);
5651 if (pci_dma_mapping_error(tp->pdev, mapping))
5652 goto dma_error;
5653
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005654 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005655 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005656 mapping);
Michael Chan5a6f3072006-03-20 22:28:05 -08005657
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005658 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005659 base_flags, (i == last) | (mss << 1));
5660
5661 entry = NEXT_TX(entry);
5662 }
5663 }
5664
5665 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005666 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005667
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005668 tnapi->tx_prod = entry;
5669 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005670 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005671 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005672 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005673 }
5674
5675out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005676 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005677
5678 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005679
5680dma_error:
5681 last = i;
5682 entry = tnapi->tx_prod;
5683 tnapi->tx_buffers[entry].skb = NULL;
5684 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005685 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005686 skb_headlen(skb),
5687 PCI_DMA_TODEVICE);
5688 for (i = 0; i <= last; i++) {
5689 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5690 entry = NEXT_TX(entry);
5691
5692 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005693 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005694 mapping),
5695 frag->size, PCI_DMA_TODEVICE);
5696 }
5697
5698 dev_kfree_skb(skb);
5699 return NETDEV_TX_OK;
Michael Chan5a6f3072006-03-20 22:28:05 -08005700}
5701
Stephen Hemminger613573252009-08-31 19:50:58 +00005702static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5703 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005704
5705/* Use GSO to workaround a rare TSO bug that may be triggered when the
5706 * TSO header is greater than 80 bytes.
5707 */
5708static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5709{
5710 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005711 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005712
5713 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005714 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005715 netif_stop_queue(tp->dev);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005716 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005717 return NETDEV_TX_BUSY;
5718
5719 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005720 }
5721
5722 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005723 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005724 goto tg3_tso_bug_end;
5725
5726 do {
5727 nskb = segs;
5728 segs = segs->next;
5729 nskb->next = NULL;
5730 tg3_start_xmit_dma_bug(nskb, tp->dev);
5731 } while (segs);
5732
5733tg3_tso_bug_end:
5734 dev_kfree_skb(skb);
5735
5736 return NETDEV_TX_OK;
5737}
Michael Chan52c0fd82006-06-29 20:15:54 -07005738
Michael Chan5a6f3072006-03-20 22:28:05 -08005739/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5740 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5741 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005742static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5743 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005744{
5745 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005746 u32 len, entry, base_flags, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005748 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005749 struct tg3_napi *tnapi;
5750 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005751 unsigned int i, last;
5752
Matt Carlson24f4efd2009-11-13 13:03:35 +00005753 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5754 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005755 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlson24f4efd2009-11-13 13:03:35 +00005756 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757
Michael Chan00b70502006-06-17 21:58:45 -07005758 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005759 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005760 * interrupt. Furthermore, IRQ processing runs lockless so we have
5761 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005763 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005764 if (!netif_tx_queue_stopped(txq)) {
5765 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005766
5767 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005768 netdev_err(dev,
5769 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 return NETDEV_TX_BUSY;
5772 }
5773
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005774 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005776 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005778
Matt Carlsonc13e3712007-05-05 11:50:04 -07005779 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005780 struct iphdr *iph;
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005781 u32 tcp_opt_len, ip_tcp_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782
5783 if (skb_header_cloned(skb) &&
5784 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5785 dev_kfree_skb(skb);
5786 goto out_unlock;
5787 }
5788
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005789 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005790 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791
Michael Chan52c0fd82006-06-29 20:15:54 -07005792 hdr_len = ip_tcp_len + tcp_opt_len;
5793 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005794 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00005795 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07005796
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5798 TXD_FLAG_CPU_POST_DMA);
5799
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005800 iph = ip_hdr(skb);
5801 iph->check = 0;
5802 iph->tot_len = htons(mss + hdr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005804 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005806 } else
5807 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5808 iph->daddr, 0,
5809 IPPROTO_TCP,
5810 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811
Matt Carlson615774f2009-11-13 13:03:39 +00005812 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
5813 mss |= (hdr_len & 0xc) << 12;
5814 if (hdr_len & 0x10)
5815 base_flags |= 0x00000010;
5816 base_flags |= (hdr_len & 0x3e0) << 5;
5817 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005818 mss |= hdr_len << 9;
5819 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
5820 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005821 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 int tsflags;
5823
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005824 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 mss |= (tsflags << 11);
5826 }
5827 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005828 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 int tsflags;
5830
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005831 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 base_flags |= tsflags << 12;
5833 }
5834 }
5835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836#if TG3_VLAN_TAG_USED
5837 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5838 base_flags |= (TXD_FLAG_VLAN |
5839 (vlan_tx_tag_get(skb) << 16));
5840#endif
5841
Matt Carlsonb703df62009-12-03 08:36:21 +00005842 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson615774f2009-11-13 13:03:39 +00005843 !mss && skb->len > ETH_DATA_LEN)
5844 base_flags |= TXD_FLAG_JMB_PKT;
5845
Alexander Duyckf4188d82009-12-02 16:48:38 +00005846 len = skb_headlen(skb);
5847
5848 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5849 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005850 dev_kfree_skb(skb);
5851 goto out_unlock;
5852 }
5853
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005854 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005855 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856
5857 would_hit_hwbug = 0;
5858
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005859 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
5860 would_hit_hwbug = 1;
5861
Matt Carlson0e1406d2009-11-02 12:33:33 +00005862 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5863 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07005864 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00005865
5866 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5867 tg3_40bit_overflow_test(tp, mapping, len))
5868 would_hit_hwbug = 1;
5869
5870 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07005871 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005873 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5875
5876 entry = NEXT_TX(entry);
5877
5878 /* Now loop through additional data fragments, and queue them. */
5879 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 last = skb_shinfo(skb)->nr_frags - 1;
5881 for (i = 0; i <= last; i++) {
5882 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5883
5884 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005885 mapping = pci_map_page(tp->pdev,
5886 frag->page,
5887 frag->page_offset,
5888 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005890 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005891 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005892 mapping);
5893 if (pci_dma_mapping_error(tp->pdev, mapping))
5894 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005896 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
5897 len <= 8)
5898 would_hit_hwbug = 1;
5899
Matt Carlson0e1406d2009-11-02 12:33:33 +00005900 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5901 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07005902 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903
Matt Carlson0e1406d2009-11-02 12:33:33 +00005904 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5905 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08005906 would_hit_hwbug = 1;
5907
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005909 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 base_flags, (i == last)|(mss << 1));
5911 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005912 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 base_flags, (i == last));
5914
5915 entry = NEXT_TX(entry);
5916 }
5917 }
5918
5919 if (would_hit_hwbug) {
5920 u32 last_plus_one = entry;
5921 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922
Michael Chanc58ec932005-09-17 00:46:27 -07005923 start = entry - 1 - skb_shinfo(skb)->nr_frags;
5924 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925
5926 /* If the workaround fails due to memory/mapping
5927 * failure, silently drop this packet.
5928 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005929 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07005930 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 goto out_unlock;
5932
5933 entry = start;
5934 }
5935
5936 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005937 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005939 tnapi->tx_prod = entry;
5940 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005941 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005942 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00005943 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945
5946out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005947 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948
5949 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005950
5951dma_error:
5952 last = i;
5953 entry = tnapi->tx_prod;
5954 tnapi->tx_buffers[entry].skb = NULL;
5955 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005956 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005957 skb_headlen(skb),
5958 PCI_DMA_TODEVICE);
5959 for (i = 0; i <= last; i++) {
5960 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5961 entry = NEXT_TX(entry);
5962
5963 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005964 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005965 mapping),
5966 frag->size, PCI_DMA_TODEVICE);
5967 }
5968
5969 dev_kfree_skb(skb);
5970 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971}
5972
5973static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
5974 int new_mtu)
5975{
5976 dev->mtu = new_mtu;
5977
Michael Chanef7f5ec2005-07-25 12:32:25 -07005978 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07005979 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005980 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
5981 ethtool_op_set_tso(dev, 0);
Matt Carlson859a588792010-04-05 10:19:28 +00005982 } else {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005983 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Matt Carlson859a588792010-04-05 10:19:28 +00005984 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07005985 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07005986 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07005987 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07005988 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07005989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990}
5991
5992static int tg3_change_mtu(struct net_device *dev, int new_mtu)
5993{
5994 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005995 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996
5997 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
5998 return -EINVAL;
5999
6000 if (!netif_running(dev)) {
6001 /* We'll just catch it later when the
6002 * device is up'd.
6003 */
6004 tg3_set_mtu(dev, tp, new_mtu);
6005 return 0;
6006 }
6007
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006008 tg3_phy_stop(tp);
6009
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006011
6012 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013
Michael Chan944d9802005-05-29 14:57:48 -07006014 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015
6016 tg3_set_mtu(dev, tp, new_mtu);
6017
Michael Chanb9ec6c12006-07-25 16:37:27 -07006018 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019
Michael Chanb9ec6c12006-07-25 16:37:27 -07006020 if (!err)
6021 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022
David S. Millerf47c11e2005-06-24 20:18:35 -07006023 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006025 if (!err)
6026 tg3_phy_start(tp);
6027
Michael Chanb9ec6c12006-07-25 16:37:27 -07006028 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029}
6030
Matt Carlson21f581a2009-08-28 14:00:25 +00006031static void tg3_rx_prodring_free(struct tg3 *tp,
6032 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 int i;
6035
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006036 if (tpr != &tp->prodring[0]) {
6037 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
6038 i = (i + 1) % TG3_RX_RING_SIZE)
6039 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6040 tp->rx_pkt_map_sz);
6041
6042 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
6043 for (i = tpr->rx_jmb_cons_idx;
6044 i != tpr->rx_jmb_prod_idx;
6045 i = (i + 1) % TG3_RX_JUMBO_RING_SIZE) {
6046 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6047 TG3_RX_JMB_MAP_SZ);
6048 }
6049 }
6050
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006051 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006054 for (i = 0; i < TG3_RX_RING_SIZE; i++)
6055 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6056 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006058 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006059 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++)
6060 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6061 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 }
6063}
6064
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006065/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 *
6067 * The chip has been shut down and the driver detached from
6068 * the networking, so no interrupts or new tx packets will
6069 * end up in the driver. tp->{tx,}lock are held and thus
6070 * we may not sleep.
6071 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006072static int tg3_rx_prodring_alloc(struct tg3 *tp,
6073 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074{
Matt Carlson287be122009-08-28 13:58:46 +00006075 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006077 tpr->rx_std_cons_idx = 0;
6078 tpr->rx_std_prod_idx = 0;
6079 tpr->rx_jmb_cons_idx = 0;
6080 tpr->rx_jmb_prod_idx = 0;
6081
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006082 if (tpr != &tp->prodring[0]) {
6083 memset(&tpr->rx_std_buffers[0], 0, TG3_RX_STD_BUFF_RING_SIZE);
6084 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE)
6085 memset(&tpr->rx_jmb_buffers[0], 0,
6086 TG3_RX_JMB_BUFF_RING_SIZE);
6087 goto done;
6088 }
6089
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 /* Zero out all descriptors. */
Matt Carlson21f581a2009-08-28 14:00:25 +00006091 memset(tpr->rx_std, 0, TG3_RX_RING_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092
Matt Carlson287be122009-08-28 13:58:46 +00006093 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07006094 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006095 tp->dev->mtu > ETH_DATA_LEN)
6096 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6097 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006098
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 /* Initialize invariants of the rings, we only set this
6100 * stuff once. This works because the card does not
6101 * write into the rx buffer posting rings.
6102 */
6103 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
6104 struct tg3_rx_buffer_desc *rxd;
6105
Matt Carlson21f581a2009-08-28 14:00:25 +00006106 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006107 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6109 rxd->opaque = (RXD_OPAQUE_RING_STD |
6110 (i << RXD_OPAQUE_INDEX_SHIFT));
6111 }
6112
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006113 /* Now allocate fresh SKBs for each rx ring. */
6114 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006115 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006116 netdev_warn(tp->dev,
6117 "Using a smaller RX standard ring. Only "
6118 "%d out of %d buffers were allocated "
6119 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006120 if (i == 0)
6121 goto initfail;
6122 tp->rx_pending = i;
6123 break;
6124 }
6125 }
6126
6127 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE))
6128 goto done;
6129
Matt Carlson21f581a2009-08-28 14:00:25 +00006130 memset(tpr->rx_jmb, 0, TG3_RX_JUMBO_RING_BYTES);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006131
Matt Carlson0d86df82010-02-17 15:17:00 +00006132 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE))
6133 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134
Matt Carlson0d86df82010-02-17 15:17:00 +00006135 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
6136 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137
Matt Carlson0d86df82010-02-17 15:17:00 +00006138 rxd = &tpr->rx_jmb[i].std;
6139 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6140 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6141 RXD_FLAG_JUMBO;
6142 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6143 (i << RXD_OPAQUE_INDEX_SHIFT));
6144 }
6145
6146 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6147 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006148 netdev_warn(tp->dev,
6149 "Using a smaller RX jumbo ring. Only %d "
6150 "out of %d buffers were allocated "
6151 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006152 if (i == 0)
6153 goto initfail;
6154 tp->rx_jumbo_pending = i;
6155 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 }
6157 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006158
6159done:
Michael Chan32d8c572006-07-25 16:38:29 -07006160 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006161
6162initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006163 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006164 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165}
6166
Matt Carlson21f581a2009-08-28 14:00:25 +00006167static void tg3_rx_prodring_fini(struct tg3 *tp,
6168 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169{
Matt Carlson21f581a2009-08-28 14:00:25 +00006170 kfree(tpr->rx_std_buffers);
6171 tpr->rx_std_buffers = NULL;
6172 kfree(tpr->rx_jmb_buffers);
6173 tpr->rx_jmb_buffers = NULL;
6174 if (tpr->rx_std) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 pci_free_consistent(tp->pdev, TG3_RX_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00006176 tpr->rx_std, tpr->rx_std_mapping);
6177 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006179 if (tpr->rx_jmb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180 pci_free_consistent(tp->pdev, TG3_RX_JUMBO_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00006181 tpr->rx_jmb, tpr->rx_jmb_mapping);
6182 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006184}
6185
Matt Carlson21f581a2009-08-28 14:00:25 +00006186static int tg3_rx_prodring_init(struct tg3 *tp,
6187 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006188{
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006189 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE, GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006190 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006191 return -ENOMEM;
6192
Matt Carlson21f581a2009-08-28 14:00:25 +00006193 tpr->rx_std = pci_alloc_consistent(tp->pdev, TG3_RX_RING_BYTES,
6194 &tpr->rx_std_mapping);
6195 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006196 goto err_out;
6197
6198 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006199 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE,
Matt Carlson21f581a2009-08-28 14:00:25 +00006200 GFP_KERNEL);
6201 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006202 goto err_out;
6203
Matt Carlson21f581a2009-08-28 14:00:25 +00006204 tpr->rx_jmb = pci_alloc_consistent(tp->pdev,
6205 TG3_RX_JUMBO_RING_BYTES,
6206 &tpr->rx_jmb_mapping);
6207 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006208 goto err_out;
6209 }
6210
6211 return 0;
6212
6213err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006214 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006215 return -ENOMEM;
6216}
6217
6218/* Free up pending packets in all rx/tx rings.
6219 *
6220 * The chip has been shut down and the driver detached from
6221 * the networking, so no interrupts or new tx packets will
6222 * end up in the driver. tp->{tx,}lock is not held and we are not
6223 * in an interrupt context and thus may sleep.
6224 */
6225static void tg3_free_rings(struct tg3 *tp)
6226{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006227 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006228
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006229 for (j = 0; j < tp->irq_cnt; j++) {
6230 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006231
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006232 if (!tnapi->tx_buffers)
6233 continue;
6234
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006235 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006236 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006237 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006238 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006239
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006240 txp = &tnapi->tx_buffers[i];
6241 skb = txp->skb;
6242
6243 if (skb == NULL) {
6244 i++;
6245 continue;
6246 }
6247
Alexander Duyckf4188d82009-12-02 16:48:38 +00006248 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006249 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006250 skb_headlen(skb),
6251 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006252 txp->skb = NULL;
6253
Alexander Duyckf4188d82009-12-02 16:48:38 +00006254 i++;
6255
6256 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6257 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6258 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006259 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006260 skb_shinfo(skb)->frags[k].size,
6261 PCI_DMA_TODEVICE);
6262 i++;
6263 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006264
6265 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006266 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006267
Matt Carlsone4af1af2010-02-12 14:47:05 +00006268 tg3_rx_prodring_free(tp, &tp->prodring[j]);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006269 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006270}
6271
6272/* Initialize tx/rx rings for packet processing.
6273 *
6274 * The chip has been shut down and the driver detached from
6275 * the networking, so no interrupts or new tx packets will
6276 * end up in the driver. tp->{tx,}lock are held and thus
6277 * we may not sleep.
6278 */
6279static int tg3_init_rings(struct tg3 *tp)
6280{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006281 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006282
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006283 /* Free up all the SKBs. */
6284 tg3_free_rings(tp);
6285
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006286 for (i = 0; i < tp->irq_cnt; i++) {
6287 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006288
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006289 tnapi->last_tag = 0;
6290 tnapi->last_irq_tag = 0;
6291 tnapi->hw_status->status = 0;
6292 tnapi->hw_status->status_tag = 0;
6293 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6294
6295 tnapi->tx_prod = 0;
6296 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006297 if (tnapi->tx_ring)
6298 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006299
6300 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006301 if (tnapi->rx_rcb)
6302 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006303
Matt Carlsone4af1af2010-02-12 14:47:05 +00006304 if (tg3_rx_prodring_alloc(tp, &tp->prodring[i])) {
6305 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006306 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006307 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006308 }
Matt Carlson72334482009-08-28 14:03:01 +00006309
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006310 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006311}
6312
6313/*
6314 * Must not be invoked with interrupt sources disabled and
6315 * the hardware shutdown down.
6316 */
6317static void tg3_free_consistent(struct tg3 *tp)
6318{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006319 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006320
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006321 for (i = 0; i < tp->irq_cnt; i++) {
6322 struct tg3_napi *tnapi = &tp->napi[i];
6323
6324 if (tnapi->tx_ring) {
6325 pci_free_consistent(tp->pdev, TG3_TX_RING_BYTES,
6326 tnapi->tx_ring, tnapi->tx_desc_mapping);
6327 tnapi->tx_ring = NULL;
6328 }
6329
6330 kfree(tnapi->tx_buffers);
6331 tnapi->tx_buffers = NULL;
6332
6333 if (tnapi->rx_rcb) {
6334 pci_free_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
6335 tnapi->rx_rcb,
6336 tnapi->rx_rcb_mapping);
6337 tnapi->rx_rcb = NULL;
6338 }
6339
6340 if (tnapi->hw_status) {
6341 pci_free_consistent(tp->pdev, TG3_HW_STATUS_SIZE,
6342 tnapi->hw_status,
6343 tnapi->status_mapping);
6344 tnapi->hw_status = NULL;
6345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006347
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 if (tp->hw_stats) {
6349 pci_free_consistent(tp->pdev, sizeof(struct tg3_hw_stats),
6350 tp->hw_stats, tp->stats_mapping);
6351 tp->hw_stats = NULL;
6352 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006353
Matt Carlsone4af1af2010-02-12 14:47:05 +00006354 for (i = 0; i < tp->irq_cnt; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006355 tg3_rx_prodring_fini(tp, &tp->prodring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356}
6357
6358/*
6359 * Must not be invoked with interrupt sources disabled and
6360 * the hardware shutdown down. Can sleep.
6361 */
6362static int tg3_alloc_consistent(struct tg3 *tp)
6363{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006364 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006365
Matt Carlsone4af1af2010-02-12 14:47:05 +00006366 for (i = 0; i < tp->irq_cnt; i++) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006367 if (tg3_rx_prodring_init(tp, &tp->prodring[i]))
6368 goto err_out;
6369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 tp->hw_stats = pci_alloc_consistent(tp->pdev,
6372 sizeof(struct tg3_hw_stats),
6373 &tp->stats_mapping);
6374 if (!tp->hw_stats)
6375 goto err_out;
6376
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6378
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006379 for (i = 0; i < tp->irq_cnt; i++) {
6380 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006381 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006382
6383 tnapi->hw_status = pci_alloc_consistent(tp->pdev,
6384 TG3_HW_STATUS_SIZE,
6385 &tnapi->status_mapping);
6386 if (!tnapi->hw_status)
6387 goto err_out;
6388
6389 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006390 sblk = tnapi->hw_status;
6391
Matt Carlson19cfaec2009-12-03 08:36:20 +00006392 /* If multivector TSS is enabled, vector 0 does not handle
6393 * tx interrupts. Don't allocate any resources for it.
6394 */
6395 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6396 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6397 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6398 TG3_TX_RING_SIZE,
6399 GFP_KERNEL);
6400 if (!tnapi->tx_buffers)
6401 goto err_out;
6402
6403 tnapi->tx_ring = pci_alloc_consistent(tp->pdev,
6404 TG3_TX_RING_BYTES,
6405 &tnapi->tx_desc_mapping);
6406 if (!tnapi->tx_ring)
6407 goto err_out;
6408 }
6409
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006410 /*
6411 * When RSS is enabled, the status block format changes
6412 * slightly. The "rx_jumbo_consumer", "reserved",
6413 * and "rx_mini_consumer" members get mapped to the
6414 * other three rx return ring producer indexes.
6415 */
6416 switch (i) {
6417 default:
6418 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6419 break;
6420 case 2:
6421 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6422 break;
6423 case 3:
6424 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6425 break;
6426 case 4:
6427 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6428 break;
6429 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006430
Matt Carlsone4af1af2010-02-12 14:47:05 +00006431 tnapi->prodring = &tp->prodring[i];
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006432
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006433 /*
6434 * If multivector RSS is enabled, vector 0 does not handle
6435 * rx or tx interrupts. Don't allocate any resources for it.
6436 */
6437 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6438 continue;
6439
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006440 tnapi->rx_rcb = pci_alloc_consistent(tp->pdev,
6441 TG3_RX_RCB_RING_BYTES(tp),
6442 &tnapi->rx_rcb_mapping);
6443 if (!tnapi->rx_rcb)
6444 goto err_out;
6445
6446 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006447 }
6448
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449 return 0;
6450
6451err_out:
6452 tg3_free_consistent(tp);
6453 return -ENOMEM;
6454}
6455
6456#define MAX_WAIT_CNT 1000
6457
6458/* To stop a block, clear the enable bit and poll till it
6459 * clears. tp->lock is held.
6460 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006461static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462{
6463 unsigned int i;
6464 u32 val;
6465
6466 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6467 switch (ofs) {
6468 case RCVLSC_MODE:
6469 case DMAC_MODE:
6470 case MBFREE_MODE:
6471 case BUFMGR_MODE:
6472 case MEMARB_MODE:
6473 /* We can't enable/disable these bits of the
6474 * 5705/5750, just say success.
6475 */
6476 return 0;
6477
6478 default:
6479 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 }
6482
6483 val = tr32(ofs);
6484 val &= ~enable_bit;
6485 tw32_f(ofs, val);
6486
6487 for (i = 0; i < MAX_WAIT_CNT; i++) {
6488 udelay(100);
6489 val = tr32(ofs);
6490 if ((val & enable_bit) == 0)
6491 break;
6492 }
6493
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006494 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006495 dev_err(&tp->pdev->dev,
6496 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6497 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498 return -ENODEV;
6499 }
6500
6501 return 0;
6502}
6503
6504/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006505static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506{
6507 int i, err;
6508
6509 tg3_disable_ints(tp);
6510
6511 tp->rx_mode &= ~RX_MODE_ENABLE;
6512 tw32_f(MAC_RX_MODE, tp->rx_mode);
6513 udelay(10);
6514
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006515 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6516 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6517 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6518 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6519 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6520 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006522 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6523 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6524 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6525 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6526 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6527 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6528 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529
6530 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6531 tw32_f(MAC_MODE, tp->mac_mode);
6532 udelay(40);
6533
6534 tp->tx_mode &= ~TX_MODE_ENABLE;
6535 tw32_f(MAC_TX_MODE, tp->tx_mode);
6536
6537 for (i = 0; i < MAX_WAIT_CNT; i++) {
6538 udelay(100);
6539 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6540 break;
6541 }
6542 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006543 dev_err(&tp->pdev->dev,
6544 "%s timed out, TX_MODE_ENABLE will not clear "
6545 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006546 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 }
6548
Michael Chane6de8ad2005-05-05 14:42:41 -07006549 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006550 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6551 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552
6553 tw32(FTQ_RESET, 0xffffffff);
6554 tw32(FTQ_RESET, 0x00000000);
6555
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006556 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6557 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006559 for (i = 0; i < tp->irq_cnt; i++) {
6560 struct tg3_napi *tnapi = &tp->napi[i];
6561 if (tnapi->hw_status)
6562 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564 if (tp->hw_stats)
6565 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6566
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567 return err;
6568}
6569
Matt Carlson0d3031d2007-10-10 18:02:43 -07006570static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6571{
6572 int i;
6573 u32 apedata;
6574
6575 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6576 if (apedata != APE_SEG_SIG_MAGIC)
6577 return;
6578
6579 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006580 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006581 return;
6582
6583 /* Wait for up to 1 millisecond for APE to service previous event. */
6584 for (i = 0; i < 10; i++) {
6585 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6586 return;
6587
6588 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6589
6590 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6591 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6592 event | APE_EVENT_STATUS_EVENT_PENDING);
6593
6594 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6595
6596 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6597 break;
6598
6599 udelay(100);
6600 }
6601
6602 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6603 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6604}
6605
6606static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6607{
6608 u32 event;
6609 u32 apedata;
6610
6611 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6612 return;
6613
6614 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006615 case RESET_KIND_INIT:
6616 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6617 APE_HOST_SEG_SIG_MAGIC);
6618 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6619 APE_HOST_SEG_LEN_MAGIC);
6620 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6621 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6622 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
6623 APE_HOST_DRIVER_ID_MAGIC);
6624 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6625 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006626
Matt Carlson33f401a2010-04-05 10:19:27 +00006627 event = APE_EVENT_STATUS_STATE_START;
6628 break;
6629 case RESET_KIND_SHUTDOWN:
6630 /* With the interface we are currently using,
6631 * APE does not track driver state. Wiping
6632 * out the HOST SEGMENT SIGNATURE forces
6633 * the APE to assume OS absent status.
6634 */
6635 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006636
Matt Carlson33f401a2010-04-05 10:19:27 +00006637 event = APE_EVENT_STATUS_STATE_UNLOAD;
6638 break;
6639 case RESET_KIND_SUSPEND:
6640 event = APE_EVENT_STATUS_STATE_SUSPEND;
6641 break;
6642 default:
6643 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006644 }
6645
6646 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6647
6648 tg3_ape_send_event(tp, event);
6649}
6650
Michael Chane6af3012005-04-21 17:12:05 -07006651/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6653{
David S. Millerf49639e2006-06-09 11:58:36 -07006654 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6655 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656
6657 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6658 switch (kind) {
6659 case RESET_KIND_INIT:
6660 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6661 DRV_STATE_START);
6662 break;
6663
6664 case RESET_KIND_SHUTDOWN:
6665 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6666 DRV_STATE_UNLOAD);
6667 break;
6668
6669 case RESET_KIND_SUSPEND:
6670 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6671 DRV_STATE_SUSPEND);
6672 break;
6673
6674 default:
6675 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006678
6679 if (kind == RESET_KIND_INIT ||
6680 kind == RESET_KIND_SUSPEND)
6681 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682}
6683
6684/* tp->lock is held. */
6685static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6686{
6687 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6688 switch (kind) {
6689 case RESET_KIND_INIT:
6690 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6691 DRV_STATE_START_DONE);
6692 break;
6693
6694 case RESET_KIND_SHUTDOWN:
6695 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6696 DRV_STATE_UNLOAD_DONE);
6697 break;
6698
6699 default:
6700 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006703
6704 if (kind == RESET_KIND_SHUTDOWN)
6705 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706}
6707
6708/* tp->lock is held. */
6709static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6710{
6711 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
6712 switch (kind) {
6713 case RESET_KIND_INIT:
6714 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6715 DRV_STATE_START);
6716 break;
6717
6718 case RESET_KIND_SHUTDOWN:
6719 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6720 DRV_STATE_UNLOAD);
6721 break;
6722
6723 case RESET_KIND_SUSPEND:
6724 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6725 DRV_STATE_SUSPEND);
6726 break;
6727
6728 default:
6729 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 }
6732}
6733
Michael Chan7a6f4362006-09-27 16:03:31 -07006734static int tg3_poll_fw(struct tg3 *tp)
6735{
6736 int i;
6737 u32 val;
6738
Michael Chanb5d37722006-09-27 16:06:21 -07006739 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006740 /* Wait up to 20ms for init done. */
6741 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006742 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6743 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006744 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006745 }
6746 return -ENODEV;
6747 }
6748
Michael Chan7a6f4362006-09-27 16:03:31 -07006749 /* Wait for firmware initialization to complete. */
6750 for (i = 0; i < 100000; i++) {
6751 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6752 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6753 break;
6754 udelay(10);
6755 }
6756
6757 /* Chip might not be fitted with firmware. Some Sun onboard
6758 * parts are configured like that. So don't signal the timeout
6759 * of the above loop as an error, but do report the lack of
6760 * running firmware once.
6761 */
6762 if (i >= 100000 &&
6763 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6764 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6765
Joe Perches05dbe002010-02-17 19:44:19 +00006766 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07006767 }
6768
Matt Carlson6b10c162010-02-12 14:47:08 +00006769 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
6770 /* The 57765 A0 needs a little more
6771 * time to do some important work.
6772 */
6773 mdelay(10);
6774 }
6775
Michael Chan7a6f4362006-09-27 16:03:31 -07006776 return 0;
6777}
6778
Michael Chanee6a99b2007-07-18 21:49:10 -07006779/* Save PCI command register before chip reset */
6780static void tg3_save_pci_state(struct tg3 *tp)
6781{
Matt Carlson8a6eac92007-10-21 16:17:55 -07006782 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006783}
6784
6785/* Restore PCI state after chip reset */
6786static void tg3_restore_pci_state(struct tg3 *tp)
6787{
6788 u32 val;
6789
6790 /* Re-enable indirect register accesses. */
6791 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6792 tp->misc_host_ctrl);
6793
6794 /* Set MAX PCI retry to zero. */
6795 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6796 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6797 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6798 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006799 /* Allow reads and writes to the APE register and memory space. */
6800 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6801 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00006802 PCISTATE_ALLOW_APE_SHMEM_WR |
6803 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07006804 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6805
Matt Carlson8a6eac92007-10-21 16:17:55 -07006806 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006807
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006808 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6809 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
6810 pcie_set_readrq(tp->pdev, 4096);
6811 else {
6812 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6813 tp->pci_cacheline_sz);
6814 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6815 tp->pci_lat_timer);
6816 }
Michael Chan114342f2007-10-15 02:12:26 -07006817 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08006818
Michael Chanee6a99b2007-07-18 21:49:10 -07006819 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08006820 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07006821 u16 pcix_cmd;
6822
6823 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6824 &pcix_cmd);
6825 pcix_cmd &= ~PCI_X_CMD_ERO;
6826 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6827 pcix_cmd);
6828 }
Michael Chanee6a99b2007-07-18 21:49:10 -07006829
6830 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07006831
6832 /* Chip reset on 5780 will reset MSI enable bit,
6833 * so need to restore it.
6834 */
6835 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
6836 u16 ctrl;
6837
6838 pci_read_config_word(tp->pdev,
6839 tp->msi_cap + PCI_MSI_FLAGS,
6840 &ctrl);
6841 pci_write_config_word(tp->pdev,
6842 tp->msi_cap + PCI_MSI_FLAGS,
6843 ctrl | PCI_MSI_FLAGS_ENABLE);
6844 val = tr32(MSGINT_MODE);
6845 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
6846 }
6847 }
6848}
6849
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850static void tg3_stop_fw(struct tg3 *);
6851
6852/* tp->lock is held. */
6853static int tg3_chip_reset(struct tg3 *tp)
6854{
6855 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07006856 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00006857 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858
David S. Millerf49639e2006-06-09 11:58:36 -07006859 tg3_nvram_lock(tp);
6860
Matt Carlson77b483f2008-08-15 14:07:24 -07006861 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
6862
David S. Millerf49639e2006-06-09 11:58:36 -07006863 /* No matching tg3_nvram_unlock() after this because
6864 * chip reset below will undo the nvram lock.
6865 */
6866 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867
Michael Chanee6a99b2007-07-18 21:49:10 -07006868 /* GRC_MISC_CFG core clock reset will clear the memory
6869 * enable bit in PCI register 4 and the MSI enable bit
6870 * on some chips, so we save relevant registers here.
6871 */
6872 tg3_save_pci_state(tp);
6873
Michael Chand9ab5ad2006-03-20 22:27:35 -08006874 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08006875 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08006876 tw32(GRC_FASTBOOT_PC, 0);
6877
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 /*
6879 * We must avoid the readl() that normally takes place.
6880 * It locks machines, causes machine checks, and other
6881 * fun things. So, temporarily disable the 5701
6882 * hardware workaround, while we do the reset.
6883 */
Michael Chan1ee582d2005-08-09 20:16:46 -07006884 write_op = tp->write32;
6885 if (write_op == tg3_write_flush_reg32)
6886 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887
Michael Chand18edcb2007-03-24 20:57:11 -07006888 /* Prevent the irq handler from reading or writing PCI registers
6889 * during chip reset when the memory enable bit in the PCI command
6890 * register may be cleared. The chip does not generate interrupt
6891 * at this time, but the irq handler may still be called due to irq
6892 * sharing or irqpoll.
6893 */
6894 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006895 for (i = 0; i < tp->irq_cnt; i++) {
6896 struct tg3_napi *tnapi = &tp->napi[i];
6897 if (tnapi->hw_status) {
6898 tnapi->hw_status->status = 0;
6899 tnapi->hw_status->status_tag = 0;
6900 }
6901 tnapi->last_tag = 0;
6902 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07006903 }
Michael Chand18edcb2007-03-24 20:57:11 -07006904 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00006905
6906 for (i = 0; i < tp->irq_cnt; i++)
6907 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07006908
Matt Carlson255ca312009-08-25 10:07:27 +00006909 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6910 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
6911 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
6912 }
6913
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 /* do the reset */
6915 val = GRC_MISC_CFG_CORECLK_RESET;
6916
6917 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
6918 if (tr32(0x7e2c) == 0x60) {
6919 tw32(0x7e2c, 0x20);
6920 }
6921 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
6922 tw32(GRC_MISC_CFG, (1 << 29));
6923 val |= (1 << 29);
6924 }
6925 }
6926
Michael Chanb5d37722006-09-27 16:06:21 -07006927 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6928 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
6929 tw32(GRC_VCPU_EXT_CTRL,
6930 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
6931 }
6932
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6934 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
6935 tw32(GRC_MISC_CFG, val);
6936
Michael Chan1ee582d2005-08-09 20:16:46 -07006937 /* restore 5701 hardware bug workaround write method */
6938 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939
6940 /* Unfortunately, we have to delay before the PCI read back.
6941 * Some 575X chips even will not respond to a PCI cfg access
6942 * when the reset command is given to the chip.
6943 *
6944 * How do these hardware designers expect things to work
6945 * properly if the PCI write is posted for a long period
6946 * of time? It is always necessary to have some method by
6947 * which a register read back can occur to push the write
6948 * out which does the reset.
6949 *
6950 * For most tg3 variants the trick below was working.
6951 * Ho hum...
6952 */
6953 udelay(120);
6954
6955 /* Flush PCI posted writes. The normal MMIO registers
6956 * are inaccessible at this time so this is the only
6957 * way to make this reliably (actually, this is no longer
6958 * the case, see above). I tried to use indirect
6959 * register read/write but this upset some 5701 variants.
6960 */
6961 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
6962
6963 udelay(120);
6964
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006965 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00006966 u16 val16;
6967
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
6969 int i;
6970 u32 cfg_val;
6971
6972 /* Wait for link training to complete. */
6973 for (i = 0; i < 5000; i++)
6974 udelay(100);
6975
6976 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
6977 pci_write_config_dword(tp->pdev, 0xc4,
6978 cfg_val | (1 << 15));
6979 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006980
Matt Carlsone7126992009-08-25 10:08:16 +00006981 /* Clear the "no snoop" and "relaxed ordering" bits. */
6982 pci_read_config_word(tp->pdev,
6983 tp->pcie_cap + PCI_EXP_DEVCTL,
6984 &val16);
6985 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
6986 PCI_EXP_DEVCTL_NOSNOOP_EN);
6987 /*
6988 * Older PCIe devices only support the 128 byte
6989 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006990 */
Matt Carlsone7126992009-08-25 10:08:16 +00006991 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
6992 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784))
6993 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006994 pci_write_config_word(tp->pdev,
6995 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00006996 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006997
6998 pcie_set_readrq(tp->pdev, 4096);
6999
7000 /* Clear error status */
7001 pci_write_config_word(tp->pdev,
7002 tp->pcie_cap + PCI_EXP_DEVSTA,
7003 PCI_EXP_DEVSTA_CED |
7004 PCI_EXP_DEVSTA_NFED |
7005 PCI_EXP_DEVSTA_FED |
7006 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007 }
7008
Michael Chanee6a99b2007-07-18 21:49:10 -07007009 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010
Michael Chand18edcb2007-03-24 20:57:11 -07007011 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
7012
Michael Chanee6a99b2007-07-18 21:49:10 -07007013 val = 0;
7014 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07007015 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007016 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017
7018 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7019 tg3_stop_fw(tp);
7020 tw32(0x5000, 0x400);
7021 }
7022
7023 tw32(GRC_MODE, tp->grc_mode);
7024
7025 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007026 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027
7028 tw32(0xc4, val | (1 << 15));
7029 }
7030
7031 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7032 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7033 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7034 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7035 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7036 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7037 }
7038
7039 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
7040 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
7041 tw32_f(MAC_MODE, tp->mac_mode);
Michael Chan747e8f82005-07-25 12:33:22 -07007042 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
7043 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
7044 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlson3bda1252008-08-15 14:08:22 -07007045 } else if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7046 tp->mac_mode &= (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
7047 if (tp->mac_mode & MAC_MODE_APE_TX_EN)
7048 tp->mac_mode |= MAC_MODE_TDE_ENABLE;
7049 tw32_f(MAC_MODE, tp->mac_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050 } else
7051 tw32_f(MAC_MODE, 0);
7052 udelay(40);
7053
Matt Carlson77b483f2008-08-15 14:07:24 -07007054 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7055
Michael Chan7a6f4362006-09-27 16:03:31 -07007056 err = tg3_poll_fw(tp);
7057 if (err)
7058 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059
Matt Carlson0a9140c2009-08-28 12:27:50 +00007060 tg3_mdio_start(tp);
7061
Matt Carlson52cdf852009-11-02 14:25:06 +00007062 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7063 u8 phy_addr;
7064
7065 phy_addr = tp->phy_addr;
7066 tp->phy_addr = TG3_PHY_PCIE_ADDR;
7067
7068 tg3_writephy(tp, TG3_PCIEPHY_BLOCK_ADDR,
7069 TG3_PCIEPHY_TXB_BLK << TG3_PCIEPHY_BLOCK_SHIFT);
7070 val = TG3_PCIEPHY_TX0CTRL1_TXOCM | TG3_PCIEPHY_TX0CTRL1_RDCTL |
7071 TG3_PCIEPHY_TX0CTRL1_TXCMV | TG3_PCIEPHY_TX0CTRL1_TKSEL |
7072 TG3_PCIEPHY_TX0CTRL1_NB_EN;
7073 tg3_writephy(tp, TG3_PCIEPHY_TX0CTRL1, val);
7074 udelay(10);
7075
7076 tg3_writephy(tp, TG3_PCIEPHY_BLOCK_ADDR,
7077 TG3_PCIEPHY_XGXS_BLK1 << TG3_PCIEPHY_BLOCK_SHIFT);
7078 val = TG3_PCIEPHY_PWRMGMT4_LOWPWR_EN |
7079 TG3_PCIEPHY_PWRMGMT4_L1PLLPD_EN;
7080 tg3_writephy(tp, TG3_PCIEPHY_PWRMGMT4, val);
7081 udelay(10);
7082
7083 tp->phy_addr = phy_addr;
7084 }
7085
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007087 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7088 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonb703df62009-12-03 08:36:21 +00007089 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
7090 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007091 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092
7093 tw32(0x7c00, val | (1 << 25));
7094 }
7095
7096 /* Reprobe ASF enable state. */
7097 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
7098 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
7099 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7100 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7101 u32 nic_cfg;
7102
7103 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7104 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
7105 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07007106 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07007107 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7109 }
7110 }
7111
7112 return 0;
7113}
7114
7115/* tp->lock is held. */
7116static void tg3_stop_fw(struct tg3 *tp)
7117{
Matt Carlson0d3031d2007-10-10 18:02:43 -07007118 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7119 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007120 /* Wait for RX cpu to ACK the previous event. */
7121 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122
7123 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007124
7125 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126
Matt Carlson7c5026a2008-05-02 16:49:29 -07007127 /* Wait for RX cpu to ACK this event. */
7128 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129 }
7130}
7131
7132/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007133static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134{
7135 int err;
7136
7137 tg3_stop_fw(tp);
7138
Michael Chan944d9802005-05-29 14:57:48 -07007139 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007141 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142 err = tg3_chip_reset(tp);
7143
Matt Carlsondaba2a62009-04-20 06:58:52 +00007144 __tg3_set_mac_addr(tp, 0);
7145
Michael Chan944d9802005-05-29 14:57:48 -07007146 tg3_write_sig_legacy(tp, kind);
7147 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148
7149 if (err)
7150 return err;
7151
7152 return 0;
7153}
7154
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155#define RX_CPU_SCRATCH_BASE 0x30000
7156#define RX_CPU_SCRATCH_SIZE 0x04000
7157#define TX_CPU_SCRATCH_BASE 0x34000
7158#define TX_CPU_SCRATCH_SIZE 0x04000
7159
7160/* tp->lock is held. */
7161static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7162{
7163 int i;
7164
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02007165 BUG_ON(offset == TX_CPU_BASE &&
7166 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167
Michael Chanb5d37722006-09-27 16:06:21 -07007168 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7169 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7170
7171 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7172 return 0;
7173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 if (offset == RX_CPU_BASE) {
7175 for (i = 0; i < 10000; i++) {
7176 tw32(offset + CPU_STATE, 0xffffffff);
7177 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7178 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7179 break;
7180 }
7181
7182 tw32(offset + CPU_STATE, 0xffffffff);
7183 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7184 udelay(10);
7185 } else {
7186 for (i = 0; i < 10000; i++) {
7187 tw32(offset + CPU_STATE, 0xffffffff);
7188 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7189 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7190 break;
7191 }
7192 }
7193
7194 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007195 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7196 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 return -ENODEV;
7198 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007199
7200 /* Clear firmware's nvram arbitration. */
7201 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7202 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203 return 0;
7204}
7205
7206struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007207 unsigned int fw_base;
7208 unsigned int fw_len;
7209 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210};
7211
7212/* tp->lock is held. */
7213static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7214 int cpu_scratch_size, struct fw_info *info)
7215{
Michael Chanec41c7d2006-01-17 02:40:55 -08007216 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 void (*write_op)(struct tg3 *, u32, u32);
7218
7219 if (cpu_base == TX_CPU_BASE &&
7220 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007221 netdev_err(tp->dev,
7222 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007223 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224 return -EINVAL;
7225 }
7226
7227 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7228 write_op = tg3_write_mem;
7229 else
7230 write_op = tg3_write_indirect_reg32;
7231
Michael Chan1b628152005-05-29 14:59:49 -07007232 /* It is possible that bootcode is still loading at this point.
7233 * Get the nvram lock first before halting the cpu.
7234 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007235 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007237 if (!lock_err)
7238 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239 if (err)
7240 goto out;
7241
7242 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7243 write_op(tp, cpu_scratch_base + i, 0);
7244 tw32(cpu_base + CPU_STATE, 0xffffffff);
7245 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007246 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007248 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007250 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251
7252 err = 0;
7253
7254out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255 return err;
7256}
7257
7258/* tp->lock is held. */
7259static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7260{
7261 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007262 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263 int err, i;
7264
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007265 fw_data = (void *)tp->fw->data;
7266
7267 /* Firmware blob starts with version numbers, followed by
7268 start address and length. We are setting complete length.
7269 length = end_address_of_bss - start_address_of_text.
7270 Remainder is the blob to be loaded contiguously
7271 from start address. */
7272
7273 info.fw_base = be32_to_cpu(fw_data[1]);
7274 info.fw_len = tp->fw->size - 12;
7275 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276
7277 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7278 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7279 &info);
7280 if (err)
7281 return err;
7282
7283 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7284 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7285 &info);
7286 if (err)
7287 return err;
7288
7289 /* Now startup only the RX cpu. */
7290 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007291 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292
7293 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007294 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295 break;
7296 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7297 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007298 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007299 udelay(1000);
7300 }
7301 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007302 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7303 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007304 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305 return -ENODEV;
7306 }
7307 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7308 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7309
7310 return 0;
7311}
7312
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314
7315/* tp->lock is held. */
7316static int tg3_load_tso_firmware(struct tg3 *tp)
7317{
7318 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007319 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007320 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7321 int err, i;
7322
7323 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7324 return 0;
7325
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007326 fw_data = (void *)tp->fw->data;
7327
7328 /* Firmware blob starts with version numbers, followed by
7329 start address and length. We are setting complete length.
7330 length = end_address_of_bss - start_address_of_text.
7331 Remainder is the blob to be loaded contiguously
7332 from start address. */
7333
7334 info.fw_base = be32_to_cpu(fw_data[1]);
7335 cpu_scratch_size = tp->fw_len;
7336 info.fw_len = tp->fw->size - 12;
7337 info.fw_data = &fw_data[3];
7338
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340 cpu_base = RX_CPU_BASE;
7341 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343 cpu_base = TX_CPU_BASE;
7344 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7345 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7346 }
7347
7348 err = tg3_load_firmware_cpu(tp, cpu_base,
7349 cpu_scratch_base, cpu_scratch_size,
7350 &info);
7351 if (err)
7352 return err;
7353
7354 /* Now startup the cpu. */
7355 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007356 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357
7358 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007359 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360 break;
7361 tw32(cpu_base + CPU_STATE, 0xffffffff);
7362 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007363 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 udelay(1000);
7365 }
7366 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007367 netdev_err(tp->dev,
7368 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007369 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370 return -ENODEV;
7371 }
7372 tw32(cpu_base + CPU_STATE, 0xffffffff);
7373 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7374 return 0;
7375}
7376
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378static int tg3_set_mac_addr(struct net_device *dev, void *p)
7379{
7380 struct tg3 *tp = netdev_priv(dev);
7381 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007382 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383
Michael Chanf9804dd2005-09-27 12:13:10 -07007384 if (!is_valid_ether_addr(addr->sa_data))
7385 return -EINVAL;
7386
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7388
Michael Chane75f7c92006-03-20 21:33:26 -08007389 if (!netif_running(dev))
7390 return 0;
7391
Michael Chan58712ef2006-04-29 18:58:01 -07007392 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007393 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007394
Michael Chan986e0ae2007-05-05 12:10:20 -07007395 addr0_high = tr32(MAC_ADDR_0_HIGH);
7396 addr0_low = tr32(MAC_ADDR_0_LOW);
7397 addr1_high = tr32(MAC_ADDR_1_HIGH);
7398 addr1_low = tr32(MAC_ADDR_1_LOW);
7399
7400 /* Skip MAC addr 1 if ASF is using it. */
7401 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7402 !(addr1_high == 0 && addr1_low == 0))
7403 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007404 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007405 spin_lock_bh(&tp->lock);
7406 __tg3_set_mac_addr(tp, skip_mac_1);
7407 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408
Michael Chanb9ec6c12006-07-25 16:37:27 -07007409 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410}
7411
7412/* tp->lock is held. */
7413static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7414 dma_addr_t mapping, u32 maxlen_flags,
7415 u32 nic_addr)
7416{
7417 tg3_write_mem(tp,
7418 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7419 ((u64) mapping >> 32));
7420 tg3_write_mem(tp,
7421 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7422 ((u64) mapping & 0xffffffff));
7423 tg3_write_mem(tp,
7424 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7425 maxlen_flags);
7426
7427 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7428 tg3_write_mem(tp,
7429 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7430 nic_addr);
7431}
7432
7433static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007434static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007435{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007436 int i;
7437
Matt Carlson19cfaec2009-12-03 08:36:20 +00007438 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007439 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7440 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7441 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007442 } else {
7443 tw32(HOSTCC_TXCOL_TICKS, 0);
7444 tw32(HOSTCC_TXMAX_FRAMES, 0);
7445 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007446 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007447
Matt Carlson19cfaec2009-12-03 08:36:20 +00007448 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
7449 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7450 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7451 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7452 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007453 tw32(HOSTCC_RXCOL_TICKS, 0);
7454 tw32(HOSTCC_RXMAX_FRAMES, 0);
7455 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007456 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007457
David S. Miller15f98502005-05-18 22:49:26 -07007458 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7459 u32 val = ec->stats_block_coalesce_usecs;
7460
Matt Carlsonb6080e12009-09-01 13:12:00 +00007461 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7462 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7463
David S. Miller15f98502005-05-18 22:49:26 -07007464 if (!netif_carrier_ok(tp->dev))
7465 val = 0;
7466
7467 tw32(HOSTCC_STAT_COAL_TICKS, val);
7468 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007469
7470 for (i = 0; i < tp->irq_cnt - 1; i++) {
7471 u32 reg;
7472
7473 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7474 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007475 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7476 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007477 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7478 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007479
7480 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7481 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7482 tw32(reg, ec->tx_coalesce_usecs);
7483 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7484 tw32(reg, ec->tx_max_coalesced_frames);
7485 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7486 tw32(reg, ec->tx_max_coalesced_frames_irq);
7487 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007488 }
7489
7490 for (; i < tp->irq_max - 1; i++) {
7491 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007492 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007493 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007494
7495 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7496 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7497 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7498 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7499 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007500 }
David S. Miller15f98502005-05-18 22:49:26 -07007501}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502
7503/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007504static void tg3_rings_reset(struct tg3 *tp)
7505{
7506 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007507 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007508 struct tg3_napi *tnapi = &tp->napi[0];
7509
7510 /* Disable all transmit rings but the first. */
7511 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7512 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007513 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7514 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007515 else
7516 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7517
7518 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7519 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7520 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7521 BDINFO_FLAGS_DISABLED);
7522
7523
7524 /* Disable all receive return rings but the first. */
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007525 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
7526 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7527 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007528 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007529 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7530 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007531 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7532 else
7533 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7534
7535 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7536 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7537 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7538 BDINFO_FLAGS_DISABLED);
7539
7540 /* Disable interrupts */
7541 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7542
7543 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007544 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
7545 for (i = 1; i < TG3_IRQ_MAX_VECS; i++) {
7546 tp->napi[i].tx_prod = 0;
7547 tp->napi[i].tx_cons = 0;
Matt Carlsonc2353a32010-01-20 16:58:08 +00007548 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7549 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007550 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7551 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7552 }
Matt Carlsonc2353a32010-01-20 16:58:08 +00007553 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7554 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007555 } else {
7556 tp->napi[0].tx_prod = 0;
7557 tp->napi[0].tx_cons = 0;
7558 tw32_mailbox(tp->napi[0].prodmbox, 0);
7559 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7560 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007561
7562 /* Make sure the NIC-based send BD rings are disabled. */
7563 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7564 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7565 for (i = 0; i < 16; i++)
7566 tw32_tx_mbox(mbox + i * 8, 0);
7567 }
7568
7569 txrcb = NIC_SRAM_SEND_RCB;
7570 rxrcb = NIC_SRAM_RCV_RET_RCB;
7571
7572 /* Clear status block in ram. */
7573 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7574
7575 /* Set status block DMA address */
7576 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7577 ((u64) tnapi->status_mapping >> 32));
7578 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7579 ((u64) tnapi->status_mapping & 0xffffffff));
7580
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007581 if (tnapi->tx_ring) {
7582 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7583 (TG3_TX_RING_SIZE <<
7584 BDINFO_FLAGS_MAXLEN_SHIFT),
7585 NIC_SRAM_TX_BUFFER_DESC);
7586 txrcb += TG3_BDINFO_SIZE;
7587 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007588
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007589 if (tnapi->rx_rcb) {
7590 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7591 (TG3_RX_RCB_RING_SIZE(tp) <<
7592 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7593 rxrcb += TG3_BDINFO_SIZE;
7594 }
7595
7596 stblk = HOSTCC_STATBLCK_RING1;
7597
7598 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7599 u64 mapping = (u64)tnapi->status_mapping;
7600 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7601 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7602
7603 /* Clear status block in ram. */
7604 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7605
Matt Carlson19cfaec2009-12-03 08:36:20 +00007606 if (tnapi->tx_ring) {
7607 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7608 (TG3_TX_RING_SIZE <<
7609 BDINFO_FLAGS_MAXLEN_SHIFT),
7610 NIC_SRAM_TX_BUFFER_DESC);
7611 txrcb += TG3_BDINFO_SIZE;
7612 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007613
7614 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7615 (TG3_RX_RCB_RING_SIZE(tp) <<
7616 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7617
7618 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007619 rxrcb += TG3_BDINFO_SIZE;
7620 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007621}
7622
7623/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007624static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625{
7626 u32 val, rdmac_mode;
7627 int i, err, limit;
Matt Carlson21f581a2009-08-28 14:00:25 +00007628 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629
7630 tg3_disable_ints(tp);
7631
7632 tg3_stop_fw(tp);
7633
7634 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7635
Matt Carlson859a588792010-04-05 10:19:28 +00007636 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
Michael Chane6de8ad2005-05-05 14:42:41 -07007637 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007638
Matt Carlson603f1172010-02-12 14:47:10 +00007639 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007640 tg3_phy_reset(tp);
7641
Linus Torvalds1da177e2005-04-16 15:20:36 -07007642 err = tg3_chip_reset(tp);
7643 if (err)
7644 return err;
7645
7646 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7647
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007648 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007649 val = tr32(TG3_CPMU_CTRL);
7650 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7651 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007652
7653 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7654 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7655 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7656 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7657
7658 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7659 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7660 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7661 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7662
7663 val = tr32(TG3_CPMU_HST_ACC);
7664 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7665 val |= CPMU_HST_ACC_MACCLK_6_25;
7666 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007667 }
7668
Matt Carlson33466d92009-04-20 06:57:41 +00007669 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7670 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7671 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7672 PCIE_PWR_MGMT_L1_THRESH_4MS;
7673 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007674
7675 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7676 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7677
7678 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007679
Matt Carlsonf40386c2009-11-02 14:24:02 +00007680 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7681 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007682 }
7683
Matt Carlson614b0592010-01-20 16:58:02 +00007684 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
7685 u32 grc_mode = tr32(GRC_MODE);
7686
7687 /* Access the lower 1K of PL PCIE block registers. */
7688 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7689 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7690
7691 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7692 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7693 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7694
7695 tw32(GRC_MODE, grc_mode);
7696 }
7697
Matt Carlsoncea46462010-04-12 06:58:24 +00007698 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7699 u32 grc_mode = tr32(GRC_MODE);
7700
7701 /* Access the lower 1K of PL PCIE block registers. */
7702 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7703 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7704
7705 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5);
7706 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
7707 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
7708
7709 tw32(GRC_MODE, grc_mode);
Matt Carlsona977dbe2010-04-12 06:58:26 +00007710
7711 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7712 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7713 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7714 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00007715 }
7716
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717 /* This works around an issue with Athlon chipsets on
7718 * B3 tigon3 silicon. This bit has no effect on any
7719 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07007720 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007721 */
Matt Carlson795d01c2007-10-07 23:28:17 -07007722 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7723 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7724 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7725 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007727
7728 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7729 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7730 val = tr32(TG3PCI_PCISTATE);
7731 val |= PCISTATE_RETRY_SAME_DMA;
7732 tw32(TG3PCI_PCISTATE, val);
7733 }
7734
Matt Carlson0d3031d2007-10-10 18:02:43 -07007735 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7736 /* Allow reads and writes to the
7737 * APE register and memory space.
7738 */
7739 val = tr32(TG3PCI_PCISTATE);
7740 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007741 PCISTATE_ALLOW_APE_SHMEM_WR |
7742 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007743 tw32(TG3PCI_PCISTATE, val);
7744 }
7745
Linus Torvalds1da177e2005-04-16 15:20:36 -07007746 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7747 /* Enable some hw fixes. */
7748 val = tr32(TG3PCI_MSI_DATA);
7749 val |= (1 << 26) | (1 << 28) | (1 << 29);
7750 tw32(TG3PCI_MSI_DATA, val);
7751 }
7752
7753 /* Descriptor ring init may make accesses to the
7754 * NIC SRAM area to setup the TX descriptors, so we
7755 * can only do this after the hardware has been
7756 * successfully reset.
7757 */
Michael Chan32d8c572006-07-25 16:38:29 -07007758 err = tg3_init_rings(tp);
7759 if (err)
7760 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007761
Matt Carlsonb703df62009-12-03 08:36:21 +00007762 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7763 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007764 val = tr32(TG3PCI_DMA_RW_CTRL) &
7765 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00007766 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
7767 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007768 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
7769 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
7770 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007771 /* This value is determined during the probe time DMA
7772 * engine test, tg3_test_dma.
7773 */
7774 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776
7777 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7778 GRC_MODE_4X_NIC_SEND_RINGS |
7779 GRC_MODE_NO_TX_PHDR_CSUM |
7780 GRC_MODE_NO_RX_PHDR_CSUM);
7781 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07007782
7783 /* Pseudo-header checksum is done by hardware logic and not
7784 * the offload processers, so make the chip do the pseudo-
7785 * header checksums on receive. For transmit it is more
7786 * convenient to do the pseudo-header checksum in software
7787 * as Linux does that on transmit for us in all cases.
7788 */
7789 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790
7791 tw32(GRC_MODE,
7792 tp->grc_mode |
7793 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7794
7795 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7796 val = tr32(GRC_MISC_CFG);
7797 val &= ~0xff;
7798 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7799 tw32(GRC_MISC_CFG, val);
7800
7801 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07007802 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803 /* Do nothing. */
7804 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
7805 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
7806 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
7807 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
7808 else
7809 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
7810 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
7811 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Matt Carlson859a588792010-04-05 10:19:28 +00007812 } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813 int fw_len;
7814
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007815 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
7817 tw32(BUFMGR_MB_POOL_ADDR,
7818 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
7819 tw32(BUFMGR_MB_POOL_SIZE,
7820 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
7821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822
Michael Chan0f893dc2005-07-25 12:30:38 -07007823 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7825 tp->bufmgr_config.mbuf_read_dma_low_water);
7826 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7827 tp->bufmgr_config.mbuf_mac_rx_low_water);
7828 tw32(BUFMGR_MB_HIGH_WATER,
7829 tp->bufmgr_config.mbuf_high_water);
7830 } else {
7831 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7832 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
7833 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7834 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
7835 tw32(BUFMGR_MB_HIGH_WATER,
7836 tp->bufmgr_config.mbuf_high_water_jumbo);
7837 }
7838 tw32(BUFMGR_DMA_LOW_WATER,
7839 tp->bufmgr_config.dma_low_water);
7840 tw32(BUFMGR_DMA_HIGH_WATER,
7841 tp->bufmgr_config.dma_high_water);
7842
7843 tw32(BUFMGR_MODE, BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE);
7844 for (i = 0; i < 2000; i++) {
7845 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
7846 break;
7847 udelay(10);
7848 }
7849 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007850 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007851 return -ENODEV;
7852 }
7853
7854 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07007855 val = tp->rx_pending / 8;
7856 if (val == 0)
7857 val = 1;
7858 else if (val > tp->rx_std_max_post)
7859 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07007860 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7861 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
7862 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
7863
7864 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
7865 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
7866 }
Michael Chanf92905d2006-06-29 20:14:29 -07007867
7868 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007869
7870 /* Initialize TG3_BDINFO's at:
7871 * RCVDBDI_STD_BD: standard eth size rx ring
7872 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
7873 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
7874 *
7875 * like so:
7876 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
7877 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
7878 * ring attribute flags
7879 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
7880 *
7881 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
7882 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
7883 *
7884 * The size of each ring is fixed in the firmware, but the location is
7885 * configurable.
7886 */
7887 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007888 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007890 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlson13fa95b02010-01-12 10:11:36 +00007891 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
Matt Carlson87668d32009-11-13 13:03:34 +00007892 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
7893 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007895 /* Disable the mini ring */
7896 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007897 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
7898 BDINFO_FLAGS_DISABLED);
7899
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007900 /* Program the jumbo buffer descriptor ring control
7901 * blocks on those devices that have them.
7902 */
Matt Carlson8f666b02009-08-28 13:58:24 +00007903 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007904 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007905 /* Setup replenish threshold. */
7906 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
7907
Michael Chan0f893dc2005-07-25 12:30:38 -07007908 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007910 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007912 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlson79ed5ac2009-08-28 14:00:55 +00007914 (RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT) |
7915 BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlson5fd68fb2010-01-20 16:58:07 +00007916 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
Matt Carlson87668d32009-11-13 13:03:34 +00007917 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
7918 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919 } else {
7920 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
7921 BDINFO_FLAGS_DISABLED);
7922 }
7923
Matt Carlsonb703df62009-12-03 08:36:21 +00007924 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7925 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007926 val = (RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT) |
Matt Carlson04380d42010-04-12 06:58:29 +00007927 (TG3_RX_STD_DMA_SZ << 2);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007928 else
Matt Carlson04380d42010-04-12 06:58:29 +00007929 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007930 } else
7931 val = RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT;
7932
7933 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007934
Matt Carlson411da642009-11-13 13:03:46 +00007935 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00007936 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007937
Matt Carlson411da642009-11-13 13:03:46 +00007938 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Matt Carlson21f581a2009-08-28 14:00:25 +00007939 tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00007940 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941
Matt Carlsonb703df62009-12-03 08:36:21 +00007942 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7943 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007944 tw32(STD_REPLENISH_LWM, 32);
7945 tw32(JMB_REPLENISH_LWM, 16);
7946 }
7947
Matt Carlson2d31eca2009-09-01 12:53:31 +00007948 tg3_rings_reset(tp);
7949
Linus Torvalds1da177e2005-04-16 15:20:36 -07007950 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07007951 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007952
7953 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00007954 tw32(MAC_RX_MTU_SIZE,
7955 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007956
7957 /* The slot time is changed by tg3_setup_phy if we
7958 * run at gigabit with half duplex.
7959 */
7960 tw32(MAC_TX_LENGTHS,
7961 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
7962 (6 << TX_LENGTHS_IPG_SHIFT) |
7963 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
7964
7965 /* Receive rules. */
7966 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
7967 tw32(RCVLPC_CONFIG, 0x0181);
7968
7969 /* Calculate RDMAC_MODE setting early, we need it to determine
7970 * the RCVLPC_STATE_ENABLE mask.
7971 */
7972 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
7973 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
7974 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
7975 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
7976 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07007977
Matt Carlson0339e4e2010-02-12 14:47:09 +00007978 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
7979 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
7980
Matt Carlson57e69832008-05-25 23:48:31 -07007981 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007982 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7983 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07007984 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
7985 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
7986 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
7987
Michael Chan85e94ce2005-04-21 17:05:28 -07007988 /* If statement applies to 5705 and 5750 PCI devices only */
7989 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7990 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7991 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007992 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07007993 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007994 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
7995 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7996 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
7997 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7998 }
7999 }
8000
Michael Chan85e94ce2005-04-21 17:05:28 -07008001 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
8002 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8003
Linus Torvalds1da177e2005-04-16 15:20:36 -07008004 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08008005 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8006
Matt Carlsone849cdc2009-11-13 13:03:38 +00008007 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
8008 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008009 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8010 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008011
8012 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07008013 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
8014 val = tr32(RCVLPC_STATS_ENABLE);
8015 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8016 tw32(RCVLPC_STATS_ENABLE, val);
8017 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
8018 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008019 val = tr32(RCVLPC_STATS_ENABLE);
8020 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8021 tw32(RCVLPC_STATS_ENABLE, val);
8022 } else {
8023 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8024 }
8025 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8026 tw32(SNDDATAI_STATSENAB, 0xffffff);
8027 tw32(SNDDATAI_STATSCTRL,
8028 (SNDDATAI_SCTRL_ENABLE |
8029 SNDDATAI_SCTRL_FASTUPD));
8030
8031 /* Setup host coalescing engine. */
8032 tw32(HOSTCC_MODE, 0);
8033 for (i = 0; i < 2000; i++) {
8034 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8035 break;
8036 udelay(10);
8037 }
8038
Michael Chand244c892005-07-05 14:42:33 -07008039 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008040
Linus Torvalds1da177e2005-04-16 15:20:36 -07008041 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8042 /* Status/statistics block address. See tg3_timer,
8043 * the tg3_periodic_fetch_stats call there, and
8044 * tg3_get_stats to see how this works for 5705/5750 chips.
8045 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008046 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8047 ((u64) tp->stats_mapping >> 32));
8048 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8049 ((u64) tp->stats_mapping & 0xffffffff));
8050 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008051
Linus Torvalds1da177e2005-04-16 15:20:36 -07008052 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008053
8054 /* Clear statistics and status block memory areas */
8055 for (i = NIC_SRAM_STATS_BLK;
8056 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8057 i += sizeof(u32)) {
8058 tg3_write_mem(tp, i, 0);
8059 udelay(40);
8060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008061 }
8062
8063 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8064
8065 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8066 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
8067 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8068 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8069
Michael Chanc94e3942005-09-27 12:12:42 -07008070 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
8071 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
8072 /* reset to prevent losing 1st rx packet intermittently */
8073 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8074 udelay(10);
8075 }
8076
Matt Carlson3bda1252008-08-15 14:08:22 -07008077 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8078 tp->mac_mode &= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
8079 else
8080 tp->mac_mode = 0;
8081 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008082 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008083 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
8084 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
8085 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8086 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008087 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8088 udelay(40);
8089
Michael Chan314fba32005-04-21 17:07:04 -07008090 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08008091 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008092 * register to preserve the GPIO settings for LOMs. The GPIOs,
8093 * whether used as inputs or outputs, are set by boot code after
8094 * reset.
8095 */
Michael Chan9d26e212006-12-07 00:21:14 -08008096 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008097 u32 gpio_mask;
8098
Michael Chan9d26e212006-12-07 00:21:14 -08008099 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8100 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8101 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008102
8103 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8104 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8105 GRC_LCLCTRL_GPIO_OUTPUT3;
8106
Michael Chanaf36e6b2006-03-23 01:28:06 -08008107 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8108 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8109
Gary Zambranoaaf84462007-05-05 11:51:45 -07008110 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008111 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8112
8113 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08008114 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
8115 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8116 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008118 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8119 udelay(100);
8120
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008121 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX) {
8122 val = tr32(MSGINT_MODE);
8123 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8124 tw32(MSGINT_MODE, val);
8125 }
8126
Linus Torvalds1da177e2005-04-16 15:20:36 -07008127 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8128 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8129 udelay(40);
8130 }
8131
8132 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8133 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8134 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8135 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8136 WDMAC_MODE_LNGREAD_ENAB);
8137
Michael Chan85e94ce2005-04-21 17:05:28 -07008138 /* If statement applies to 5705 and 5750 PCI devices only */
8139 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8140 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
8141 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
Matt Carlson29ea0952009-08-25 10:07:54 +00008142 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008143 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8144 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8145 /* nothing */
8146 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8147 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
8148 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
8149 val |= WDMAC_MODE_RX_ACCEL;
8150 }
8151 }
8152
Michael Chand9ab5ad2006-03-20 22:27:35 -08008153 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08008154 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07008155 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008156
Matt Carlson788a0352009-11-02 14:26:03 +00008157 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8158 val |= WDMAC_MODE_BURST_ALL_DATA;
8159
Linus Torvalds1da177e2005-04-16 15:20:36 -07008160 tw32_f(WDMAC_MODE, val);
8161 udelay(40);
8162
Matt Carlson9974a352007-10-07 23:27:28 -07008163 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8164 u16 pcix_cmd;
8165
8166 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8167 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008168 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008169 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8170 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008171 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008172 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8173 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008174 }
Matt Carlson9974a352007-10-07 23:27:28 -07008175 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8176 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008177 }
8178
8179 tw32_f(RDMAC_MODE, rdmac_mode);
8180 udelay(40);
8181
8182 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8183 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8184 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008185
8186 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8187 tw32(SNDDATAC_MODE,
8188 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8189 else
8190 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8191
Linus Torvalds1da177e2005-04-16 15:20:36 -07008192 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8193 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
8194 tw32(RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ);
8195 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008196 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8197 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008198 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008199 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008200 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8201 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008202 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8203
8204 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8205 err = tg3_load_5701_a0_firmware_fix(tp);
8206 if (err)
8207 return err;
8208 }
8209
Linus Torvalds1da177e2005-04-16 15:20:36 -07008210 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8211 err = tg3_load_tso_firmware(tp);
8212 if (err)
8213 return err;
8214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008215
8216 tp->tx_mode = TX_MODE_ENABLE;
8217 tw32_f(MAC_TX_MODE, tp->tx_mode);
8218 udelay(100);
8219
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008220 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8221 u32 reg = MAC_RSS_INDIR_TBL_0;
8222 u8 *ent = (u8 *)&val;
8223
8224 /* Setup the indirection table */
8225 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8226 int idx = i % sizeof(val);
8227
8228 ent[idx] = i % (tp->irq_cnt - 1);
8229 if (idx == sizeof(val) - 1) {
8230 tw32(reg, val);
8231 reg += 4;
8232 }
8233 }
8234
8235 /* Setup the "secret" hash key. */
8236 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8237 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8238 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8239 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8240 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8241 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8242 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8243 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8244 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8245 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8246 }
8247
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08008249 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08008250 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8251
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008252 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8253 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8254 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8255 RX_MODE_RSS_IPV6_HASH_EN |
8256 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8257 RX_MODE_RSS_IPV4_HASH_EN |
8258 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8259
Linus Torvalds1da177e2005-04-16 15:20:36 -07008260 tw32_f(MAC_RX_MODE, tp->rx_mode);
8261 udelay(10);
8262
Linus Torvalds1da177e2005-04-16 15:20:36 -07008263 tw32(MAC_LED_CTRL, tp->led_ctrl);
8264
8265 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Michael Chanc94e3942005-09-27 12:12:42 -07008266 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008267 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8268 udelay(10);
8269 }
8270 tw32_f(MAC_RX_MODE, tp->rx_mode);
8271 udelay(10);
8272
8273 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
8274 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
8275 !(tp->tg3_flags2 & TG3_FLG2_SERDES_PREEMPHASIS)) {
8276 /* Set drive transmission level to 1.2V */
8277 /* only if the signal pre-emphasis bit is not set */
8278 val = tr32(MAC_SERDES_CFG);
8279 val &= 0xfffff000;
8280 val |= 0x880;
8281 tw32(MAC_SERDES_CFG, val);
8282 }
8283 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8284 tw32(MAC_SERDES_CFG, 0x616000);
8285 }
8286
8287 /* Prevent chip from dropping frames when flow control
8288 * is enabled.
8289 */
Matt Carlson666bc832010-01-20 16:58:03 +00008290 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8291 val = 1;
8292 else
8293 val = 2;
8294 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008295
8296 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
8297 (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
8298 /* Use hardware link auto-negotiation */
8299 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8300 }
8301
Michael Chand4d2c552006-03-20 17:47:20 -08008302 if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
8303 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8304 u32 tmp;
8305
8306 tmp = tr32(SERDES_RX_CTRL);
8307 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8308 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8309 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8310 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8311 }
8312
Matt Carlsondd477002008-05-25 23:45:58 -07008313 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
8314 if (tp->link_config.phy_is_low_power) {
8315 tp->link_config.phy_is_low_power = 0;
8316 tp->link_config.speed = tp->link_config.orig_speed;
8317 tp->link_config.duplex = tp->link_config.orig_duplex;
8318 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008320
Matt Carlsondd477002008-05-25 23:45:58 -07008321 err = tg3_setup_phy(tp, 0);
8322 if (err)
8323 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008324
Matt Carlsondd477002008-05-25 23:45:58 -07008325 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +00008326 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008327 u32 tmp;
8328
8329 /* Clear CRC stats. */
8330 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8331 tg3_writephy(tp, MII_TG3_TEST1,
8332 tmp | MII_TG3_TEST1_CRC_EN);
8333 tg3_readphy(tp, 0x14, &tmp);
8334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008335 }
8336 }
8337
8338 __tg3_set_rx_mode(tp->dev);
8339
8340 /* Initialize receive rules. */
8341 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8342 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8343 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8344 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8345
Michael Chan4cf78e42005-07-25 12:29:19 -07008346 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008347 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008348 limit = 8;
8349 else
8350 limit = 16;
8351 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8352 limit -= 4;
8353 switch (limit) {
8354 case 16:
8355 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8356 case 15:
8357 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8358 case 14:
8359 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8360 case 13:
8361 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8362 case 12:
8363 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8364 case 11:
8365 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8366 case 10:
8367 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8368 case 9:
8369 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8370 case 8:
8371 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8372 case 7:
8373 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8374 case 6:
8375 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8376 case 5:
8377 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8378 case 4:
8379 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8380 case 3:
8381 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8382 case 2:
8383 case 1:
8384
8385 default:
8386 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008388
Matt Carlson9ce768e2007-10-11 19:49:11 -07008389 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8390 /* Write our heartbeat update interval to APE. */
8391 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8392 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008393
Linus Torvalds1da177e2005-04-16 15:20:36 -07008394 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8395
Linus Torvalds1da177e2005-04-16 15:20:36 -07008396 return 0;
8397}
8398
8399/* Called at device open time to get the chip ready for
8400 * packet processing. Invoked with tp->lock held.
8401 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008402static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008403{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008404 tg3_switch_clocks(tp);
8405
8406 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8407
Matt Carlson2f751b62008-08-04 23:17:34 -07008408 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008409}
8410
8411#define TG3_STAT_ADD32(PSTAT, REG) \
8412do { u32 __val = tr32(REG); \
8413 (PSTAT)->low += __val; \
8414 if ((PSTAT)->low < __val) \
8415 (PSTAT)->high += 1; \
8416} while (0)
8417
8418static void tg3_periodic_fetch_stats(struct tg3 *tp)
8419{
8420 struct tg3_hw_stats *sp = tp->hw_stats;
8421
8422 if (!netif_carrier_ok(tp->dev))
8423 return;
8424
8425 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8426 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8427 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8428 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8429 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8430 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8431 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8432 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8433 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8434 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8435 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8436 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8437 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8438
8439 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8440 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8441 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8442 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8443 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8444 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8445 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8446 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8447 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8448 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8449 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8450 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8451 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8452 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008453
8454 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
8455 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8456 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008457}
8458
8459static void tg3_timer(unsigned long __opaque)
8460{
8461 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008462
Michael Chanf475f162006-03-27 23:20:14 -08008463 if (tp->irq_sync)
8464 goto restart_timer;
8465
David S. Millerf47c11e2005-06-24 20:18:35 -07008466 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008467
David S. Millerfac9b832005-05-18 22:46:34 -07008468 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8469 /* All of this garbage is because when using non-tagged
8470 * IRQ status the mailbox/status_block protocol the chip
8471 * uses with the cpu is race prone.
8472 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008473 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008474 tw32(GRC_LOCAL_CTRL,
8475 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8476 } else {
8477 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008478 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008480
David S. Millerfac9b832005-05-18 22:46:34 -07008481 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8482 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008483 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008484 schedule_work(&tp->reset_task);
8485 return;
8486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008487 }
8488
Linus Torvalds1da177e2005-04-16 15:20:36 -07008489 /* This part only runs once per second. */
8490 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008491 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8492 tg3_periodic_fetch_stats(tp);
8493
Linus Torvalds1da177e2005-04-16 15:20:36 -07008494 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8495 u32 mac_stat;
8496 int phy_event;
8497
8498 mac_stat = tr32(MAC_STATUS);
8499
8500 phy_event = 0;
8501 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) {
8502 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8503 phy_event = 1;
8504 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8505 phy_event = 1;
8506
8507 if (phy_event)
8508 tg3_setup_phy(tp, 0);
8509 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8510 u32 mac_stat = tr32(MAC_STATUS);
8511 int need_setup = 0;
8512
8513 if (netif_carrier_ok(tp->dev) &&
8514 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8515 need_setup = 1;
8516 }
8517 if (! netif_carrier_ok(tp->dev) &&
8518 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8519 MAC_STATUS_SIGNAL_DET))) {
8520 need_setup = 1;
8521 }
8522 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008523 if (!tp->serdes_counter) {
8524 tw32_f(MAC_MODE,
8525 (tp->mac_mode &
8526 ~MAC_MODE_PORT_MODE_MASK));
8527 udelay(40);
8528 tw32_f(MAC_MODE, tp->mac_mode);
8529 udelay(40);
8530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008531 tg3_setup_phy(tp, 0);
8532 }
Michael Chan747e8f82005-07-25 12:33:22 -07008533 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
8534 tg3_serdes_parallel_detect(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008535
8536 tp->timer_counter = tp->timer_multiplier;
8537 }
8538
Michael Chan130b8e42006-09-27 16:00:40 -07008539 /* Heartbeat is only sent once every 2 seconds.
8540 *
8541 * The heartbeat is to tell the ASF firmware that the host
8542 * driver is still alive. In the event that the OS crashes,
8543 * ASF needs to reset the hardware to free up the FIFO space
8544 * that may be filled with rx packets destined for the host.
8545 * If the FIFO is full, ASF will no longer function properly.
8546 *
8547 * Unintended resets have been reported on real time kernels
8548 * where the timer doesn't run on time. Netpoll will also have
8549 * same problem.
8550 *
8551 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8552 * to check the ring condition when the heartbeat is expiring
8553 * before doing the reset. This will prevent most unintended
8554 * resets.
8555 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008556 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008557 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8558 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008559 tg3_wait_for_event_ack(tp);
8560
Michael Chanbbadf502006-04-06 21:46:34 -07008561 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008562 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008563 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008564 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8565 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008566
8567 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008568 }
8569 tp->asf_counter = tp->asf_multiplier;
8570 }
8571
David S. Millerf47c11e2005-06-24 20:18:35 -07008572 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008573
Michael Chanf475f162006-03-27 23:20:14 -08008574restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008575 tp->timer.expires = jiffies + tp->timer_offset;
8576 add_timer(&tp->timer);
8577}
8578
Matt Carlson4f125f42009-09-01 12:55:02 +00008579static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008580{
David Howells7d12e782006-10-05 14:55:46 +01008581 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008582 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008583 char *name;
8584 struct tg3_napi *tnapi = &tp->napi[irq_num];
8585
8586 if (tp->irq_cnt == 1)
8587 name = tp->dev->name;
8588 else {
8589 name = &tnapi->irq_lbl[0];
8590 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8591 name[IFNAMSIZ-1] = 0;
8592 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008593
Matt Carlson679563f2009-09-01 12:55:46 +00008594 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008595 fn = tg3_msi;
8596 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8597 fn = tg3_msi_1shot;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008598 flags = IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008599 } else {
8600 fn = tg3_interrupt;
8601 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8602 fn = tg3_interrupt_tagged;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008603 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008604 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008605
8606 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008607}
8608
Michael Chan79381092005-04-21 17:13:59 -07008609static int tg3_test_interrupt(struct tg3 *tp)
8610{
Matt Carlson09943a12009-08-28 14:01:57 +00008611 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008612 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008613 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008614 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008615
Michael Chand4bc3922005-05-29 14:59:20 -07008616 if (!netif_running(dev))
8617 return -ENODEV;
8618
Michael Chan79381092005-04-21 17:13:59 -07008619 tg3_disable_ints(tp);
8620
Matt Carlson4f125f42009-09-01 12:55:02 +00008621 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008622
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008623 /*
8624 * Turn off MSI one shot mode. Otherwise this test has no
8625 * observable way to know whether the interrupt was delivered.
8626 */
Matt Carlsonb703df62009-12-03 08:36:21 +00008627 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8628 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008629 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8630 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8631 tw32(MSGINT_MODE, val);
8632 }
8633
Matt Carlson4f125f42009-09-01 12:55:02 +00008634 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008635 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008636 if (err)
8637 return err;
8638
Matt Carlson898a56f2009-08-28 14:02:40 +00008639 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008640 tg3_enable_ints(tp);
8641
8642 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008643 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07008644
8645 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07008646 u32 int_mbox, misc_host_ctrl;
8647
Matt Carlson898a56f2009-08-28 14:02:40 +00008648 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07008649 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8650
8651 if ((int_mbox != 0) ||
8652 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8653 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07008654 break;
Michael Chanb16250e2006-09-27 16:10:14 -07008655 }
8656
Michael Chan79381092005-04-21 17:13:59 -07008657 msleep(10);
8658 }
8659
8660 tg3_disable_ints(tp);
8661
Matt Carlson4f125f42009-09-01 12:55:02 +00008662 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008663
Matt Carlson4f125f42009-09-01 12:55:02 +00008664 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008665
8666 if (err)
8667 return err;
8668
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008669 if (intr_ok) {
8670 /* Reenable MSI one shot mode. */
Matt Carlsonb703df62009-12-03 08:36:21 +00008671 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8672 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008673 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8674 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8675 tw32(MSGINT_MODE, val);
8676 }
Michael Chan79381092005-04-21 17:13:59 -07008677 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008678 }
Michael Chan79381092005-04-21 17:13:59 -07008679
8680 return -EIO;
8681}
8682
8683/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8684 * successfully restored
8685 */
8686static int tg3_test_msi(struct tg3 *tp)
8687{
Michael Chan79381092005-04-21 17:13:59 -07008688 int err;
8689 u16 pci_cmd;
8690
8691 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8692 return 0;
8693
8694 /* Turn off SERR reporting in case MSI terminates with Master
8695 * Abort.
8696 */
8697 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8698 pci_write_config_word(tp->pdev, PCI_COMMAND,
8699 pci_cmd & ~PCI_COMMAND_SERR);
8700
8701 err = tg3_test_interrupt(tp);
8702
8703 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8704
8705 if (!err)
8706 return 0;
8707
8708 /* other failures */
8709 if (err != -EIO)
8710 return err;
8711
8712 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008713 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
8714 "to INTx mode. Please report this failure to the PCI "
8715 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07008716
Matt Carlson4f125f42009-09-01 12:55:02 +00008717 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00008718
Michael Chan79381092005-04-21 17:13:59 -07008719 pci_disable_msi(tp->pdev);
8720
8721 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
Andre Detschdc8bf1b2010-04-26 07:27:07 +00008722 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07008723
Matt Carlson4f125f42009-09-01 12:55:02 +00008724 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008725 if (err)
8726 return err;
8727
8728 /* Need to reset the chip because the MSI cycle may have terminated
8729 * with Master Abort.
8730 */
David S. Millerf47c11e2005-06-24 20:18:35 -07008731 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008732
Michael Chan944d9802005-05-29 14:57:48 -07008733 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008734 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008735
David S. Millerf47c11e2005-06-24 20:18:35 -07008736 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008737
8738 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00008739 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07008740
8741 return err;
8742}
8743
Matt Carlson9e9fd122009-01-19 16:57:45 -08008744static int tg3_request_firmware(struct tg3 *tp)
8745{
8746 const __be32 *fw_data;
8747
8748 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008749 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
8750 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008751 return -ENOENT;
8752 }
8753
8754 fw_data = (void *)tp->fw->data;
8755
8756 /* Firmware blob starts with version numbers, followed by
8757 * start address and _full_ length including BSS sections
8758 * (which must be longer than the actual data, of course
8759 */
8760
8761 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
8762 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008763 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
8764 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008765 release_firmware(tp->fw);
8766 tp->fw = NULL;
8767 return -EINVAL;
8768 }
8769
8770 /* We no longer need firmware; we have it. */
8771 tp->fw_needed = NULL;
8772 return 0;
8773}
8774
Matt Carlson679563f2009-09-01 12:55:46 +00008775static bool tg3_enable_msix(struct tg3 *tp)
8776{
8777 int i, rc, cpus = num_online_cpus();
8778 struct msix_entry msix_ent[tp->irq_max];
8779
8780 if (cpus == 1)
8781 /* Just fallback to the simpler MSI mode. */
8782 return false;
8783
8784 /*
8785 * We want as many rx rings enabled as there are cpus.
8786 * The first MSIX vector only deals with link interrupts, etc,
8787 * so we add one to the number of vectors we are requesting.
8788 */
8789 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
8790
8791 for (i = 0; i < tp->irq_max; i++) {
8792 msix_ent[i].entry = i;
8793 msix_ent[i].vector = 0;
8794 }
8795
8796 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
8797 if (rc != 0) {
8798 if (rc < TG3_RSS_MIN_NUM_MSIX_VECS)
8799 return false;
8800 if (pci_enable_msix(tp->pdev, msix_ent, rc))
8801 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00008802 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
8803 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00008804 tp->irq_cnt = rc;
8805 }
8806
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008807 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
8808
Matt Carlson679563f2009-09-01 12:55:46 +00008809 for (i = 0; i < tp->irq_max; i++)
8810 tp->napi[i].irq_vec = msix_ent[i].vector;
8811
Matt Carlson19cfaec2009-12-03 08:36:20 +00008812 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
8813 tp->tg3_flags3 |= TG3_FLG3_ENABLE_TSS;
8814 tp->dev->real_num_tx_queues = tp->irq_cnt - 1;
8815 } else
8816 tp->dev->real_num_tx_queues = 1;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008817
Matt Carlson679563f2009-09-01 12:55:46 +00008818 return true;
8819}
8820
Matt Carlson07b01732009-08-28 14:01:15 +00008821static void tg3_ints_init(struct tg3 *tp)
8822{
Matt Carlson679563f2009-09-01 12:55:46 +00008823 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
8824 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00008825 /* All MSI supporting chips should support tagged
8826 * status. Assert that this is the case.
8827 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008828 netdev_warn(tp->dev,
8829 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00008830 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00008831 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008832
Matt Carlson679563f2009-09-01 12:55:46 +00008833 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
8834 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
8835 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
8836 pci_enable_msi(tp->pdev) == 0)
8837 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
8838
8839 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
8840 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008841 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8842 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00008843 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
8844 }
8845defcfg:
8846 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
8847 tp->irq_cnt = 1;
8848 tp->napi[0].irq_vec = tp->pdev->irq;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008849 tp->dev->real_num_tx_queues = 1;
Matt Carlson679563f2009-09-01 12:55:46 +00008850 }
Matt Carlson07b01732009-08-28 14:01:15 +00008851}
8852
8853static void tg3_ints_fini(struct tg3 *tp)
8854{
Matt Carlson679563f2009-09-01 12:55:46 +00008855 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8856 pci_disable_msix(tp->pdev);
8857 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
8858 pci_disable_msi(tp->pdev);
8859 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008860 tp->tg3_flags3 &= ~TG3_FLG3_ENABLE_RSS;
Matt Carlson07b01732009-08-28 14:01:15 +00008861}
8862
Linus Torvalds1da177e2005-04-16 15:20:36 -07008863static int tg3_open(struct net_device *dev)
8864{
8865 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00008866 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008867
Matt Carlson9e9fd122009-01-19 16:57:45 -08008868 if (tp->fw_needed) {
8869 err = tg3_request_firmware(tp);
8870 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8871 if (err)
8872 return err;
8873 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00008874 netdev_warn(tp->dev, "TSO capability disabled\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08008875 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
8876 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008877 netdev_notice(tp->dev, "TSO capability restored\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08008878 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
8879 }
8880 }
8881
Michael Chanc49a1562006-12-17 17:07:29 -08008882 netif_carrier_off(tp->dev);
8883
Michael Chanbc1c7562006-03-20 17:48:03 -08008884 err = tg3_set_power_state(tp, PCI_D0);
Matt Carlson2f751b62008-08-04 23:17:34 -07008885 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08008886 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07008887
8888 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08008889
Linus Torvalds1da177e2005-04-16 15:20:36 -07008890 tg3_disable_ints(tp);
8891 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
8892
David S. Millerf47c11e2005-06-24 20:18:35 -07008893 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008894
Matt Carlson679563f2009-09-01 12:55:46 +00008895 /*
8896 * Setup interrupts first so we know how
8897 * many NAPI resources to allocate
8898 */
8899 tg3_ints_init(tp);
8900
Linus Torvalds1da177e2005-04-16 15:20:36 -07008901 /* The placement of this call is tied
8902 * to the setup and use of Host TX descriptors.
8903 */
8904 err = tg3_alloc_consistent(tp);
8905 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008906 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008907
Matt Carlsonfed97812009-09-01 13:10:19 +00008908 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07008909
Matt Carlson4f125f42009-09-01 12:55:02 +00008910 for (i = 0; i < tp->irq_cnt; i++) {
8911 struct tg3_napi *tnapi = &tp->napi[i];
8912 err = tg3_request_irq(tp, i);
8913 if (err) {
8914 for (i--; i >= 0; i--)
8915 free_irq(tnapi->irq_vec, tnapi);
8916 break;
8917 }
8918 }
Matt Carlson07b01732009-08-28 14:01:15 +00008919
8920 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008921 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00008922
David S. Millerf47c11e2005-06-24 20:18:35 -07008923 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008924
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008925 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008926 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07008927 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008928 tg3_free_rings(tp);
8929 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07008930 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8931 tp->timer_offset = HZ;
8932 else
8933 tp->timer_offset = HZ / 10;
8934
8935 BUG_ON(tp->timer_offset > HZ);
8936 tp->timer_counter = tp->timer_multiplier =
8937 (HZ / tp->timer_offset);
8938 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07008939 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008940
8941 init_timer(&tp->timer);
8942 tp->timer.expires = jiffies + tp->timer_offset;
8943 tp->timer.data = (unsigned long) tp;
8944 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008945 }
8946
David S. Millerf47c11e2005-06-24 20:18:35 -07008947 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008948
Matt Carlson07b01732009-08-28 14:01:15 +00008949 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008950 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008951
Michael Chan79381092005-04-21 17:13:59 -07008952 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
8953 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07008954
Michael Chan79381092005-04-21 17:13:59 -07008955 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07008956 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07008957 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07008958 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07008959 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008960
Matt Carlson679563f2009-09-01 12:55:46 +00008961 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07008962 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008963
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008964 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
Matt Carlsonb703df62009-12-03 08:36:21 +00008965 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008966 (tp->tg3_flags2 & TG3_FLG2_USING_MSI) &&
8967 (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)) {
8968 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008969
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008970 tw32(PCIE_TRANSACTION_CFG,
8971 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008972 }
Michael Chan79381092005-04-21 17:13:59 -07008973 }
8974
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008975 tg3_phy_start(tp);
8976
David S. Millerf47c11e2005-06-24 20:18:35 -07008977 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008978
Michael Chan79381092005-04-21 17:13:59 -07008979 add_timer(&tp->timer);
8980 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981 tg3_enable_ints(tp);
8982
David S. Millerf47c11e2005-06-24 20:18:35 -07008983 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008984
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008985 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008986
8987 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00008988
Matt Carlson679563f2009-09-01 12:55:46 +00008989err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00008990 for (i = tp->irq_cnt - 1; i >= 0; i--) {
8991 struct tg3_napi *tnapi = &tp->napi[i];
8992 free_irq(tnapi->irq_vec, tnapi);
8993 }
Matt Carlson07b01732009-08-28 14:01:15 +00008994
Matt Carlson679563f2009-09-01 12:55:46 +00008995err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00008996 tg3_napi_disable(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008997 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00008998
8999err_out1:
9000 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009001 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009002}
9003
Linus Torvalds1da177e2005-04-16 15:20:36 -07009004static struct net_device_stats *tg3_get_stats(struct net_device *);
9005static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9006
9007static int tg3_close(struct net_device *dev)
9008{
Matt Carlson4f125f42009-09-01 12:55:02 +00009009 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009010 struct tg3 *tp = netdev_priv(dev);
9011
Matt Carlsonfed97812009-09-01 13:10:19 +00009012 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009013 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009014
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009015 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009016
9017 del_timer_sync(&tp->timer);
9018
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009019 tg3_phy_stop(tp);
9020
David S. Millerf47c11e2005-06-24 20:18:35 -07009021 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009022
9023 tg3_disable_ints(tp);
9024
Michael Chan944d9802005-05-29 14:57:48 -07009025 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009026 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07009027 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009028
David S. Millerf47c11e2005-06-24 20:18:35 -07009029 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009030
Matt Carlson4f125f42009-09-01 12:55:02 +00009031 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9032 struct tg3_napi *tnapi = &tp->napi[i];
9033 free_irq(tnapi->irq_vec, tnapi);
9034 }
Matt Carlson07b01732009-08-28 14:01:15 +00009035
9036 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009037
9038 memcpy(&tp->net_stats_prev, tg3_get_stats(tp->dev),
9039 sizeof(tp->net_stats_prev));
9040 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9041 sizeof(tp->estats_prev));
9042
9043 tg3_free_consistent(tp);
9044
Michael Chanbc1c7562006-03-20 17:48:03 -08009045 tg3_set_power_state(tp, PCI_D3hot);
9046
9047 netif_carrier_off(tp->dev);
9048
Linus Torvalds1da177e2005-04-16 15:20:36 -07009049 return 0;
9050}
9051
9052static inline unsigned long get_stat64(tg3_stat64_t *val)
9053{
9054 unsigned long ret;
9055
9056#if (BITS_PER_LONG == 32)
9057 ret = val->low;
9058#else
9059 ret = ((u64)val->high << 32) | ((u64)val->low);
9060#endif
9061 return ret;
9062}
9063
Stefan Buehler816f8b82008-08-15 14:10:54 -07009064static inline u64 get_estat64(tg3_stat64_t *val)
9065{
9066 return ((u64)val->high << 32) | ((u64)val->low);
9067}
9068
Linus Torvalds1da177e2005-04-16 15:20:36 -07009069static unsigned long calc_crc_errors(struct tg3 *tp)
9070{
9071 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9072
9073 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
9074 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9075 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009076 u32 val;
9077
David S. Millerf47c11e2005-06-24 20:18:35 -07009078 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009079 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9080 tg3_writephy(tp, MII_TG3_TEST1,
9081 val | MII_TG3_TEST1_CRC_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009082 tg3_readphy(tp, 0x14, &val);
9083 } else
9084 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009085 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009086
9087 tp->phy_crc_errors += val;
9088
9089 return tp->phy_crc_errors;
9090 }
9091
9092 return get_stat64(&hw_stats->rx_fcs_errors);
9093}
9094
9095#define ESTAT_ADD(member) \
9096 estats->member = old_estats->member + \
Stefan Buehler816f8b82008-08-15 14:10:54 -07009097 get_estat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009098
9099static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9100{
9101 struct tg3_ethtool_stats *estats = &tp->estats;
9102 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9103 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9104
9105 if (!hw_stats)
9106 return old_estats;
9107
9108 ESTAT_ADD(rx_octets);
9109 ESTAT_ADD(rx_fragments);
9110 ESTAT_ADD(rx_ucast_packets);
9111 ESTAT_ADD(rx_mcast_packets);
9112 ESTAT_ADD(rx_bcast_packets);
9113 ESTAT_ADD(rx_fcs_errors);
9114 ESTAT_ADD(rx_align_errors);
9115 ESTAT_ADD(rx_xon_pause_rcvd);
9116 ESTAT_ADD(rx_xoff_pause_rcvd);
9117 ESTAT_ADD(rx_mac_ctrl_rcvd);
9118 ESTAT_ADD(rx_xoff_entered);
9119 ESTAT_ADD(rx_frame_too_long_errors);
9120 ESTAT_ADD(rx_jabbers);
9121 ESTAT_ADD(rx_undersize_packets);
9122 ESTAT_ADD(rx_in_length_errors);
9123 ESTAT_ADD(rx_out_length_errors);
9124 ESTAT_ADD(rx_64_or_less_octet_packets);
9125 ESTAT_ADD(rx_65_to_127_octet_packets);
9126 ESTAT_ADD(rx_128_to_255_octet_packets);
9127 ESTAT_ADD(rx_256_to_511_octet_packets);
9128 ESTAT_ADD(rx_512_to_1023_octet_packets);
9129 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9130 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9131 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9132 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9133 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9134
9135 ESTAT_ADD(tx_octets);
9136 ESTAT_ADD(tx_collisions);
9137 ESTAT_ADD(tx_xon_sent);
9138 ESTAT_ADD(tx_xoff_sent);
9139 ESTAT_ADD(tx_flow_control);
9140 ESTAT_ADD(tx_mac_errors);
9141 ESTAT_ADD(tx_single_collisions);
9142 ESTAT_ADD(tx_mult_collisions);
9143 ESTAT_ADD(tx_deferred);
9144 ESTAT_ADD(tx_excessive_collisions);
9145 ESTAT_ADD(tx_late_collisions);
9146 ESTAT_ADD(tx_collide_2times);
9147 ESTAT_ADD(tx_collide_3times);
9148 ESTAT_ADD(tx_collide_4times);
9149 ESTAT_ADD(tx_collide_5times);
9150 ESTAT_ADD(tx_collide_6times);
9151 ESTAT_ADD(tx_collide_7times);
9152 ESTAT_ADD(tx_collide_8times);
9153 ESTAT_ADD(tx_collide_9times);
9154 ESTAT_ADD(tx_collide_10times);
9155 ESTAT_ADD(tx_collide_11times);
9156 ESTAT_ADD(tx_collide_12times);
9157 ESTAT_ADD(tx_collide_13times);
9158 ESTAT_ADD(tx_collide_14times);
9159 ESTAT_ADD(tx_collide_15times);
9160 ESTAT_ADD(tx_ucast_packets);
9161 ESTAT_ADD(tx_mcast_packets);
9162 ESTAT_ADD(tx_bcast_packets);
9163 ESTAT_ADD(tx_carrier_sense_errors);
9164 ESTAT_ADD(tx_discards);
9165 ESTAT_ADD(tx_errors);
9166
9167 ESTAT_ADD(dma_writeq_full);
9168 ESTAT_ADD(dma_write_prioq_full);
9169 ESTAT_ADD(rxbds_empty);
9170 ESTAT_ADD(rx_discards);
9171 ESTAT_ADD(rx_errors);
9172 ESTAT_ADD(rx_threshold_hit);
9173
9174 ESTAT_ADD(dma_readq_full);
9175 ESTAT_ADD(dma_read_prioq_full);
9176 ESTAT_ADD(tx_comp_queue_full);
9177
9178 ESTAT_ADD(ring_set_send_prod_index);
9179 ESTAT_ADD(ring_status_update);
9180 ESTAT_ADD(nic_irqs);
9181 ESTAT_ADD(nic_avoided_irqs);
9182 ESTAT_ADD(nic_tx_threshold_hit);
9183
9184 return estats;
9185}
9186
9187static struct net_device_stats *tg3_get_stats(struct net_device *dev)
9188{
9189 struct tg3 *tp = netdev_priv(dev);
9190 struct net_device_stats *stats = &tp->net_stats;
9191 struct net_device_stats *old_stats = &tp->net_stats_prev;
9192 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9193
9194 if (!hw_stats)
9195 return old_stats;
9196
9197 stats->rx_packets = old_stats->rx_packets +
9198 get_stat64(&hw_stats->rx_ucast_packets) +
9199 get_stat64(&hw_stats->rx_mcast_packets) +
9200 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009201
Linus Torvalds1da177e2005-04-16 15:20:36 -07009202 stats->tx_packets = old_stats->tx_packets +
9203 get_stat64(&hw_stats->tx_ucast_packets) +
9204 get_stat64(&hw_stats->tx_mcast_packets) +
9205 get_stat64(&hw_stats->tx_bcast_packets);
9206
9207 stats->rx_bytes = old_stats->rx_bytes +
9208 get_stat64(&hw_stats->rx_octets);
9209 stats->tx_bytes = old_stats->tx_bytes +
9210 get_stat64(&hw_stats->tx_octets);
9211
9212 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009213 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009214 stats->tx_errors = old_stats->tx_errors +
9215 get_stat64(&hw_stats->tx_errors) +
9216 get_stat64(&hw_stats->tx_mac_errors) +
9217 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9218 get_stat64(&hw_stats->tx_discards);
9219
9220 stats->multicast = old_stats->multicast +
9221 get_stat64(&hw_stats->rx_mcast_packets);
9222 stats->collisions = old_stats->collisions +
9223 get_stat64(&hw_stats->tx_collisions);
9224
9225 stats->rx_length_errors = old_stats->rx_length_errors +
9226 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9227 get_stat64(&hw_stats->rx_undersize_packets);
9228
9229 stats->rx_over_errors = old_stats->rx_over_errors +
9230 get_stat64(&hw_stats->rxbds_empty);
9231 stats->rx_frame_errors = old_stats->rx_frame_errors +
9232 get_stat64(&hw_stats->rx_align_errors);
9233 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9234 get_stat64(&hw_stats->tx_discards);
9235 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9236 get_stat64(&hw_stats->tx_carrier_sense_errors);
9237
9238 stats->rx_crc_errors = old_stats->rx_crc_errors +
9239 calc_crc_errors(tp);
9240
John W. Linville4f63b872005-09-12 14:43:18 -07009241 stats->rx_missed_errors = old_stats->rx_missed_errors +
9242 get_stat64(&hw_stats->rx_discards);
9243
Linus Torvalds1da177e2005-04-16 15:20:36 -07009244 return stats;
9245}
9246
9247static inline u32 calc_crc(unsigned char *buf, int len)
9248{
9249 u32 reg;
9250 u32 tmp;
9251 int j, k;
9252
9253 reg = 0xffffffff;
9254
9255 for (j = 0; j < len; j++) {
9256 reg ^= buf[j];
9257
9258 for (k = 0; k < 8; k++) {
9259 tmp = reg & 0x01;
9260
9261 reg >>= 1;
9262
Matt Carlson859a588792010-04-05 10:19:28 +00009263 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009264 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009265 }
9266 }
9267
9268 return ~reg;
9269}
9270
9271static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9272{
9273 /* accept or reject all multicast frames */
9274 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9275 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9276 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9277 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9278}
9279
9280static void __tg3_set_rx_mode(struct net_device *dev)
9281{
9282 struct tg3 *tp = netdev_priv(dev);
9283 u32 rx_mode;
9284
9285 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9286 RX_MODE_KEEP_VLAN_TAG);
9287
9288 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9289 * flag clear.
9290 */
9291#if TG3_VLAN_TAG_USED
9292 if (!tp->vlgrp &&
9293 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9294 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9295#else
9296 /* By definition, VLAN is disabled always in this
9297 * case.
9298 */
9299 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9300 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9301#endif
9302
9303 if (dev->flags & IFF_PROMISC) {
9304 /* Promiscuous mode. */
9305 rx_mode |= RX_MODE_PROMISC;
9306 } else if (dev->flags & IFF_ALLMULTI) {
9307 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009308 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009309 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009310 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009311 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312 } else {
9313 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +00009314 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009315 u32 mc_filter[4] = { 0, };
9316 u32 regidx;
9317 u32 bit;
9318 u32 crc;
9319
Jiri Pirko22bedad32010-04-01 21:22:57 +00009320 netdev_for_each_mc_addr(ha, dev) {
9321 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009322 bit = ~crc & 0x7f;
9323 regidx = (bit & 0x60) >> 5;
9324 bit &= 0x1f;
9325 mc_filter[regidx] |= (1 << bit);
9326 }
9327
9328 tw32(MAC_HASH_REG_0, mc_filter[0]);
9329 tw32(MAC_HASH_REG_1, mc_filter[1]);
9330 tw32(MAC_HASH_REG_2, mc_filter[2]);
9331 tw32(MAC_HASH_REG_3, mc_filter[3]);
9332 }
9333
9334 if (rx_mode != tp->rx_mode) {
9335 tp->rx_mode = rx_mode;
9336 tw32_f(MAC_RX_MODE, rx_mode);
9337 udelay(10);
9338 }
9339}
9340
9341static void tg3_set_rx_mode(struct net_device *dev)
9342{
9343 struct tg3 *tp = netdev_priv(dev);
9344
Michael Chane75f7c92006-03-20 21:33:26 -08009345 if (!netif_running(dev))
9346 return;
9347
David S. Millerf47c11e2005-06-24 20:18:35 -07009348 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009349 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009350 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009351}
9352
9353#define TG3_REGDUMP_LEN (32 * 1024)
9354
9355static int tg3_get_regs_len(struct net_device *dev)
9356{
9357 return TG3_REGDUMP_LEN;
9358}
9359
9360static void tg3_get_regs(struct net_device *dev,
9361 struct ethtool_regs *regs, void *_p)
9362{
9363 u32 *p = _p;
9364 struct tg3 *tp = netdev_priv(dev);
9365 u8 *orig_p = _p;
9366 int i;
9367
9368 regs->version = 0;
9369
9370 memset(p, 0, TG3_REGDUMP_LEN);
9371
Michael Chanbc1c7562006-03-20 17:48:03 -08009372 if (tp->link_config.phy_is_low_power)
9373 return;
9374
David S. Millerf47c11e2005-06-24 20:18:35 -07009375 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009376
9377#define __GET_REG32(reg) (*(p)++ = tr32(reg))
9378#define GET_REG32_LOOP(base,len) \
9379do { p = (u32 *)(orig_p + (base)); \
9380 for (i = 0; i < len; i += 4) \
9381 __GET_REG32((base) + i); \
9382} while (0)
9383#define GET_REG32_1(reg) \
9384do { p = (u32 *)(orig_p + (reg)); \
9385 __GET_REG32((reg)); \
9386} while (0)
9387
9388 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9389 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9390 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9391 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9392 GET_REG32_1(SNDDATAC_MODE);
9393 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9394 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9395 GET_REG32_1(SNDBDC_MODE);
9396 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9397 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9398 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9399 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9400 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9401 GET_REG32_1(RCVDCC_MODE);
9402 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9403 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9404 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9405 GET_REG32_1(MBFREE_MODE);
9406 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9407 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9408 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9409 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9410 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08009411 GET_REG32_1(RX_CPU_MODE);
9412 GET_REG32_1(RX_CPU_STATE);
9413 GET_REG32_1(RX_CPU_PGMCTR);
9414 GET_REG32_1(RX_CPU_HWBKPT);
9415 GET_REG32_1(TX_CPU_MODE);
9416 GET_REG32_1(TX_CPU_STATE);
9417 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009418 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9419 GET_REG32_LOOP(FTQ_RESET, 0x120);
9420 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9421 GET_REG32_1(DMAC_MODE);
9422 GET_REG32_LOOP(GRC_MODE, 0x4c);
9423 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9424 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9425
9426#undef __GET_REG32
9427#undef GET_REG32_LOOP
9428#undef GET_REG32_1
9429
David S. Millerf47c11e2005-06-24 20:18:35 -07009430 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009431}
9432
9433static int tg3_get_eeprom_len(struct net_device *dev)
9434{
9435 struct tg3 *tp = netdev_priv(dev);
9436
9437 return tp->nvram_size;
9438}
9439
Linus Torvalds1da177e2005-04-16 15:20:36 -07009440static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9441{
9442 struct tg3 *tp = netdev_priv(dev);
9443 int ret;
9444 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009445 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009446 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009447
Matt Carlsondf259d82009-04-20 06:57:14 +00009448 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9449 return -EINVAL;
9450
Michael Chanbc1c7562006-03-20 17:48:03 -08009451 if (tp->link_config.phy_is_low_power)
9452 return -EAGAIN;
9453
Linus Torvalds1da177e2005-04-16 15:20:36 -07009454 offset = eeprom->offset;
9455 len = eeprom->len;
9456 eeprom->len = 0;
9457
9458 eeprom->magic = TG3_EEPROM_MAGIC;
9459
9460 if (offset & 3) {
9461 /* adjustments to start on required 4 byte boundary */
9462 b_offset = offset & 3;
9463 b_count = 4 - b_offset;
9464 if (b_count > len) {
9465 /* i.e. offset=1 len=2 */
9466 b_count = len;
9467 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009468 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009469 if (ret)
9470 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009471 memcpy(data, ((char*)&val) + b_offset, b_count);
9472 len -= b_count;
9473 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009474 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009475 }
9476
9477 /* read bytes upto the last 4 byte boundary */
9478 pd = &data[eeprom->len];
9479 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009480 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009481 if (ret) {
9482 eeprom->len += i;
9483 return ret;
9484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009485 memcpy(pd + i, &val, 4);
9486 }
9487 eeprom->len += i;
9488
9489 if (len & 3) {
9490 /* read last bytes not ending on 4 byte boundary */
9491 pd = &data[eeprom->len];
9492 b_count = len & 3;
9493 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009494 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009495 if (ret)
9496 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009497 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009498 eeprom->len += b_count;
9499 }
9500 return 0;
9501}
9502
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009503static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009504
9505static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9506{
9507 struct tg3 *tp = netdev_priv(dev);
9508 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009509 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009510 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009511 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009512
Michael Chanbc1c7562006-03-20 17:48:03 -08009513 if (tp->link_config.phy_is_low_power)
9514 return -EAGAIN;
9515
Matt Carlsondf259d82009-04-20 06:57:14 +00009516 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9517 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009518 return -EINVAL;
9519
9520 offset = eeprom->offset;
9521 len = eeprom->len;
9522
9523 if ((b_offset = (offset & 3))) {
9524 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009525 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009526 if (ret)
9527 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009528 len += b_offset;
9529 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009530 if (len < 4)
9531 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009532 }
9533
9534 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009535 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009536 /* adjustments to end on required 4 byte boundary */
9537 odd_len = 1;
9538 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009539 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009540 if (ret)
9541 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009542 }
9543
9544 buf = data;
9545 if (b_offset || odd_len) {
9546 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009547 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009548 return -ENOMEM;
9549 if (b_offset)
9550 memcpy(buf, &start, 4);
9551 if (odd_len)
9552 memcpy(buf+len-4, &end, 4);
9553 memcpy(buf + b_offset, data, eeprom->len);
9554 }
9555
9556 ret = tg3_nvram_write_block(tp, offset, len, buf);
9557
9558 if (buf != data)
9559 kfree(buf);
9560
9561 return ret;
9562}
9563
9564static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9565{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009566 struct tg3 *tp = netdev_priv(dev);
9567
9568 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009569 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009570 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9571 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009572 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9573 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009574 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009575
Linus Torvalds1da177e2005-04-16 15:20:36 -07009576 cmd->supported = (SUPPORTED_Autoneg);
9577
9578 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
9579 cmd->supported |= (SUPPORTED_1000baseT_Half |
9580 SUPPORTED_1000baseT_Full);
9581
Karsten Keilef348142006-05-12 12:49:08 -07009582 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009583 cmd->supported |= (SUPPORTED_100baseT_Half |
9584 SUPPORTED_100baseT_Full |
9585 SUPPORTED_10baseT_Half |
9586 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009587 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009588 cmd->port = PORT_TP;
9589 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009590 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009591 cmd->port = PORT_FIBRE;
9592 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009593
Linus Torvalds1da177e2005-04-16 15:20:36 -07009594 cmd->advertising = tp->link_config.advertising;
9595 if (netif_running(dev)) {
9596 cmd->speed = tp->link_config.active_speed;
9597 cmd->duplex = tp->link_config.active_duplex;
9598 }
Matt Carlson882e9792009-09-01 13:21:36 +00009599 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009600 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009601 cmd->autoneg = tp->link_config.autoneg;
9602 cmd->maxtxpkt = 0;
9603 cmd->maxrxpkt = 0;
9604 return 0;
9605}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009606
Linus Torvalds1da177e2005-04-16 15:20:36 -07009607static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9608{
9609 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009610
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009611 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009612 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009613 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9614 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009615 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9616 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009617 }
9618
Matt Carlson7e5856b2009-02-25 14:23:01 +00009619 if (cmd->autoneg != AUTONEG_ENABLE &&
9620 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009621 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009622
9623 if (cmd->autoneg == AUTONEG_DISABLE &&
9624 cmd->duplex != DUPLEX_FULL &&
9625 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009626 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009627
Matt Carlson7e5856b2009-02-25 14:23:01 +00009628 if (cmd->autoneg == AUTONEG_ENABLE) {
9629 u32 mask = ADVERTISED_Autoneg |
9630 ADVERTISED_Pause |
9631 ADVERTISED_Asym_Pause;
9632
Julia Lawall3f07d122010-03-13 12:22:16 -08009633 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009634 mask |= ADVERTISED_1000baseT_Half |
9635 ADVERTISED_1000baseT_Full;
9636
9637 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
9638 mask |= ADVERTISED_100baseT_Half |
9639 ADVERTISED_100baseT_Full |
9640 ADVERTISED_10baseT_Half |
9641 ADVERTISED_10baseT_Full |
9642 ADVERTISED_TP;
9643 else
9644 mask |= ADVERTISED_FIBRE;
9645
9646 if (cmd->advertising & ~mask)
9647 return -EINVAL;
9648
9649 mask &= (ADVERTISED_1000baseT_Half |
9650 ADVERTISED_1000baseT_Full |
9651 ADVERTISED_100baseT_Half |
9652 ADVERTISED_100baseT_Full |
9653 ADVERTISED_10baseT_Half |
9654 ADVERTISED_10baseT_Full);
9655
9656 cmd->advertising &= mask;
9657 } else {
9658 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) {
9659 if (cmd->speed != SPEED_1000)
9660 return -EINVAL;
9661
9662 if (cmd->duplex != DUPLEX_FULL)
9663 return -EINVAL;
9664 } else {
9665 if (cmd->speed != SPEED_100 &&
9666 cmd->speed != SPEED_10)
9667 return -EINVAL;
9668 }
9669 }
9670
David S. Millerf47c11e2005-06-24 20:18:35 -07009671 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009672
9673 tp->link_config.autoneg = cmd->autoneg;
9674 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009675 tp->link_config.advertising = (cmd->advertising |
9676 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009677 tp->link_config.speed = SPEED_INVALID;
9678 tp->link_config.duplex = DUPLEX_INVALID;
9679 } else {
9680 tp->link_config.advertising = 0;
9681 tp->link_config.speed = cmd->speed;
9682 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009683 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009684
Michael Chan24fcad62006-12-17 17:06:46 -08009685 tp->link_config.orig_speed = tp->link_config.speed;
9686 tp->link_config.orig_duplex = tp->link_config.duplex;
9687 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9688
Linus Torvalds1da177e2005-04-16 15:20:36 -07009689 if (netif_running(dev))
9690 tg3_setup_phy(tp, 1);
9691
David S. Millerf47c11e2005-06-24 20:18:35 -07009692 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009693
Linus Torvalds1da177e2005-04-16 15:20:36 -07009694 return 0;
9695}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009696
Linus Torvalds1da177e2005-04-16 15:20:36 -07009697static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9698{
9699 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009700
Linus Torvalds1da177e2005-04-16 15:20:36 -07009701 strcpy(info->driver, DRV_MODULE_NAME);
9702 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08009703 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009704 strcpy(info->bus_info, pci_name(tp->pdev));
9705}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009706
Linus Torvalds1da177e2005-04-16 15:20:36 -07009707static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9708{
9709 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009710
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009711 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9712 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07009713 wol->supported = WAKE_MAGIC;
9714 else
9715 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009716 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08009717 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9718 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009719 wol->wolopts = WAKE_MAGIC;
9720 memset(&wol->sopass, 0, sizeof(wol->sopass));
9721}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009722
Linus Torvalds1da177e2005-04-16 15:20:36 -07009723static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9724{
9725 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009726 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009727
Linus Torvalds1da177e2005-04-16 15:20:36 -07009728 if (wol->wolopts & ~WAKE_MAGIC)
9729 return -EINVAL;
9730 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009731 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009732 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009733
David S. Millerf47c11e2005-06-24 20:18:35 -07009734 spin_lock_bh(&tp->lock);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009735 if (wol->wolopts & WAKE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009736 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009737 device_set_wakeup_enable(dp, true);
9738 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009739 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009740 device_set_wakeup_enable(dp, false);
9741 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009742 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009743
Linus Torvalds1da177e2005-04-16 15:20:36 -07009744 return 0;
9745}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009746
Linus Torvalds1da177e2005-04-16 15:20:36 -07009747static u32 tg3_get_msglevel(struct net_device *dev)
9748{
9749 struct tg3 *tp = netdev_priv(dev);
9750 return tp->msg_enable;
9751}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009752
Linus Torvalds1da177e2005-04-16 15:20:36 -07009753static void tg3_set_msglevel(struct net_device *dev, u32 value)
9754{
9755 struct tg3 *tp = netdev_priv(dev);
9756 tp->msg_enable = value;
9757}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009758
Linus Torvalds1da177e2005-04-16 15:20:36 -07009759static int tg3_set_tso(struct net_device *dev, u32 value)
9760{
9761 struct tg3 *tp = netdev_priv(dev);
9762
9763 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9764 if (value)
9765 return -EINVAL;
9766 return 0;
9767 }
Matt Carlson027455a2008-12-21 20:19:30 -08009768 if ((dev->features & NETIF_F_IPV6_CSUM) &&
Matt Carlsone849cdc2009-11-13 13:03:38 +00009769 ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
9770 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3))) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009771 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -07009772 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +00009773 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
9774 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -07009775 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
9776 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009777 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00009778 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009779 dev->features |= NETIF_F_TSO_ECN;
9780 } else
9781 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -07009782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009783 return ethtool_op_set_tso(dev, value);
9784}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009785
Linus Torvalds1da177e2005-04-16 15:20:36 -07009786static int tg3_nway_reset(struct net_device *dev)
9787{
9788 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009789 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009790
Linus Torvalds1da177e2005-04-16 15:20:36 -07009791 if (!netif_running(dev))
9792 return -EAGAIN;
9793
Michael Chanc94e3942005-09-27 12:12:42 -07009794 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
9795 return -EINVAL;
9796
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009797 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9798 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9799 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009800 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009801 } else {
9802 u32 bmcr;
9803
9804 spin_lock_bh(&tp->lock);
9805 r = -EINVAL;
9806 tg3_readphy(tp, MII_BMCR, &bmcr);
9807 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
9808 ((bmcr & BMCR_ANENABLE) ||
9809 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT))) {
9810 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
9811 BMCR_ANENABLE);
9812 r = 0;
9813 }
9814 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009815 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009816
Linus Torvalds1da177e2005-04-16 15:20:36 -07009817 return r;
9818}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009819
Linus Torvalds1da177e2005-04-16 15:20:36 -07009820static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9821{
9822 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009823
Linus Torvalds1da177e2005-04-16 15:20:36 -07009824 ering->rx_max_pending = TG3_RX_RING_SIZE - 1;
9825 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009826 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9827 ering->rx_jumbo_max_pending = TG3_RX_JUMBO_RING_SIZE - 1;
9828 else
9829 ering->rx_jumbo_max_pending = 0;
9830
9831 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009832
9833 ering->rx_pending = tp->rx_pending;
9834 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009835 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9836 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
9837 else
9838 ering->rx_jumbo_pending = 0;
9839
Matt Carlsonf3f3f272009-08-28 14:03:21 +00009840 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009841}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009842
Linus Torvalds1da177e2005-04-16 15:20:36 -07009843static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9844{
9845 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +00009846 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009847
Linus Torvalds1da177e2005-04-16 15:20:36 -07009848 if ((ering->rx_pending > TG3_RX_RING_SIZE - 1) ||
9849 (ering->rx_jumbo_pending > TG3_RX_JUMBO_RING_SIZE - 1) ||
Michael Chanbc3a9252006-10-18 20:55:18 -07009850 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
9851 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -08009852 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -07009853 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009854 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009855
Michael Chanbbe832c2005-06-24 20:20:04 -07009856 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009857 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009858 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009859 irq_sync = 1;
9860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009861
Michael Chanbbe832c2005-06-24 20:20:04 -07009862 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009863
Linus Torvalds1da177e2005-04-16 15:20:36 -07009864 tp->rx_pending = ering->rx_pending;
9865
9866 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
9867 tp->rx_pending > 63)
9868 tp->rx_pending = 63;
9869 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +00009870
9871 for (i = 0; i < TG3_IRQ_MAX_VECS; i++)
9872 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009873
9874 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -07009875 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -07009876 err = tg3_restart_hw(tp, 1);
9877 if (!err)
9878 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009879 }
9880
David S. Millerf47c11e2005-06-24 20:18:35 -07009881 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009882
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009883 if (irq_sync && !err)
9884 tg3_phy_start(tp);
9885
Michael Chanb9ec6c12006-07-25 16:37:27 -07009886 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009887}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009888
Linus Torvalds1da177e2005-04-16 15:20:36 -07009889static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9890{
9891 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009892
Linus Torvalds1da177e2005-04-16 15:20:36 -07009893 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -08009894
Steve Glendinninge18ce342008-12-16 02:00:00 -08009895 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -08009896 epause->rx_pause = 1;
9897 else
9898 epause->rx_pause = 0;
9899
Steve Glendinninge18ce342008-12-16 02:00:00 -08009900 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -08009901 epause->tx_pause = 1;
9902 else
9903 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009904}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009905
Linus Torvalds1da177e2005-04-16 15:20:36 -07009906static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9907{
9908 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009909 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009910
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009911 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson27121682010-02-17 15:16:57 +00009912 u32 newadv;
9913 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009914
Matt Carlson27121682010-02-17 15:16:57 +00009915 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009916
Matt Carlson27121682010-02-17 15:16:57 +00009917 if (!(phydev->supported & SUPPORTED_Pause) ||
9918 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
9919 ((epause->rx_pause && !epause->tx_pause) ||
9920 (!epause->rx_pause && epause->tx_pause))))
9921 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009922
Matt Carlson27121682010-02-17 15:16:57 +00009923 tp->link_config.flowctrl = 0;
9924 if (epause->rx_pause) {
9925 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009926
Matt Carlson27121682010-02-17 15:16:57 +00009927 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -08009928 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +00009929 newadv = ADVERTISED_Pause;
9930 } else
9931 newadv = ADVERTISED_Pause |
9932 ADVERTISED_Asym_Pause;
9933 } else if (epause->tx_pause) {
9934 tp->link_config.flowctrl |= FLOW_CTRL_TX;
9935 newadv = ADVERTISED_Asym_Pause;
9936 } else
9937 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009938
Matt Carlson27121682010-02-17 15:16:57 +00009939 if (epause->autoneg)
9940 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9941 else
9942 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9943
9944 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
9945 u32 oldadv = phydev->advertising &
9946 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
9947 if (oldadv != newadv) {
9948 phydev->advertising &=
9949 ~(ADVERTISED_Pause |
9950 ADVERTISED_Asym_Pause);
9951 phydev->advertising |= newadv;
9952 if (phydev->autoneg) {
9953 /*
9954 * Always renegotiate the link to
9955 * inform our link partner of our
9956 * flow control settings, even if the
9957 * flow control is forced. Let
9958 * tg3_adjust_link() do the final
9959 * flow control setup.
9960 */
9961 return phy_start_aneg(phydev);
9962 }
9963 }
9964
9965 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009966 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +00009967 } else {
9968 tp->link_config.orig_advertising &=
9969 ~(ADVERTISED_Pause |
9970 ADVERTISED_Asym_Pause);
9971 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009972 }
9973 } else {
9974 int irq_sync = 0;
9975
9976 if (netif_running(dev)) {
9977 tg3_netif_stop(tp);
9978 irq_sync = 1;
9979 }
9980
9981 tg3_full_lock(tp, irq_sync);
9982
9983 if (epause->autoneg)
9984 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9985 else
9986 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9987 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009988 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009989 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009990 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009991 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009992 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009993 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009994 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009995
9996 if (netif_running(dev)) {
9997 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9998 err = tg3_restart_hw(tp, 1);
9999 if (!err)
10000 tg3_netif_start(tp);
10001 }
10002
10003 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010005
Michael Chanb9ec6c12006-07-25 16:37:27 -070010006 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010007}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010008
Linus Torvalds1da177e2005-04-16 15:20:36 -070010009static u32 tg3_get_rx_csum(struct net_device *dev)
10010{
10011 struct tg3 *tp = netdev_priv(dev);
10012 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
10013}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010014
Linus Torvalds1da177e2005-04-16 15:20:36 -070010015static int tg3_set_rx_csum(struct net_device *dev, u32 data)
10016{
10017 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010018
Linus Torvalds1da177e2005-04-16 15:20:36 -070010019 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10020 if (data != 0)
10021 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010022 return 0;
10023 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010024
David S. Millerf47c11e2005-06-24 20:18:35 -070010025 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010026 if (data)
10027 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
10028 else
10029 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -070010030 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010031
Linus Torvalds1da177e2005-04-16 15:20:36 -070010032 return 0;
10033}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010034
Linus Torvalds1da177e2005-04-16 15:20:36 -070010035static int tg3_set_tx_csum(struct net_device *dev, u32 data)
10036{
10037 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010038
Linus Torvalds1da177e2005-04-16 15:20:36 -070010039 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10040 if (data != 0)
10041 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010042 return 0;
10043 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010044
Matt Carlson321d32a2008-11-21 17:22:19 -080010045 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -070010046 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010047 else
Michael Chan9c27dbd2006-03-20 22:28:27 -080010048 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010049
10050 return 0;
10051}
10052
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010053static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010054{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010055 switch (sset) {
10056 case ETH_SS_TEST:
10057 return TG3_NUM_TEST;
10058 case ETH_SS_STATS:
10059 return TG3_NUM_STATS;
10060 default:
10061 return -EOPNOTSUPP;
10062 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010063}
10064
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010065static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010066{
10067 switch (stringset) {
10068 case ETH_SS_STATS:
10069 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10070 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010071 case ETH_SS_TEST:
10072 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10073 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010074 default:
10075 WARN_ON(1); /* we need a WARN() */
10076 break;
10077 }
10078}
10079
Michael Chan4009a932005-09-05 17:52:54 -070010080static int tg3_phys_id(struct net_device *dev, u32 data)
10081{
10082 struct tg3 *tp = netdev_priv(dev);
10083 int i;
10084
10085 if (!netif_running(tp->dev))
10086 return -EAGAIN;
10087
10088 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -080010089 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -070010090
10091 for (i = 0; i < (data * 2); i++) {
10092 if ((i % 2) == 0)
10093 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10094 LED_CTRL_1000MBPS_ON |
10095 LED_CTRL_100MBPS_ON |
10096 LED_CTRL_10MBPS_ON |
10097 LED_CTRL_TRAFFIC_OVERRIDE |
10098 LED_CTRL_TRAFFIC_BLINK |
10099 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010100
Michael Chan4009a932005-09-05 17:52:54 -070010101 else
10102 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10103 LED_CTRL_TRAFFIC_OVERRIDE);
10104
10105 if (msleep_interruptible(500))
10106 break;
10107 }
10108 tw32(MAC_LED_CTRL, tp->led_ctrl);
10109 return 0;
10110}
10111
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010112static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010113 struct ethtool_stats *estats, u64 *tmp_stats)
10114{
10115 struct tg3 *tp = netdev_priv(dev);
10116 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10117}
10118
Michael Chan566f86a2005-05-29 14:56:58 -070010119#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010120#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10121#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10122#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010123#define NVRAM_SELFBOOT_HW_SIZE 0x20
10124#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010125
10126static int tg3_test_nvram(struct tg3 *tp)
10127{
Al Virob9fc7dc2007-12-17 22:59:57 -080010128 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010129 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010130 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010131
Matt Carlsondf259d82009-04-20 06:57:14 +000010132 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10133 return 0;
10134
Matt Carlsone4f34112009-02-25 14:25:00 +000010135 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010136 return -EIO;
10137
Michael Chan1b277772006-03-20 22:27:48 -080010138 if (magic == TG3_EEPROM_MAGIC)
10139 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010140 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010141 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10142 TG3_EEPROM_SB_FORMAT_1) {
10143 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10144 case TG3_EEPROM_SB_REVISION_0:
10145 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10146 break;
10147 case TG3_EEPROM_SB_REVISION_2:
10148 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10149 break;
10150 case TG3_EEPROM_SB_REVISION_3:
10151 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10152 break;
10153 default:
10154 return 0;
10155 }
10156 } else
Michael Chan1b277772006-03-20 22:27:48 -080010157 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010158 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10159 size = NVRAM_SELFBOOT_HW_SIZE;
10160 else
Michael Chan1b277772006-03-20 22:27:48 -080010161 return -EIO;
10162
10163 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010164 if (buf == NULL)
10165 return -ENOMEM;
10166
Michael Chan1b277772006-03-20 22:27:48 -080010167 err = -EIO;
10168 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010169 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10170 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010171 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010172 }
Michael Chan1b277772006-03-20 22:27:48 -080010173 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010174 goto out;
10175
Michael Chan1b277772006-03-20 22:27:48 -080010176 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010177 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010178 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010179 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010180 u8 *buf8 = (u8 *) buf, csum8 = 0;
10181
Al Virob9fc7dc2007-12-17 22:59:57 -080010182 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010183 TG3_EEPROM_SB_REVISION_2) {
10184 /* For rev 2, the csum doesn't include the MBA. */
10185 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10186 csum8 += buf8[i];
10187 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10188 csum8 += buf8[i];
10189 } else {
10190 for (i = 0; i < size; i++)
10191 csum8 += buf8[i];
10192 }
Michael Chan1b277772006-03-20 22:27:48 -080010193
Adrian Bunkad96b482006-04-05 22:21:04 -070010194 if (csum8 == 0) {
10195 err = 0;
10196 goto out;
10197 }
10198
10199 err = -EIO;
10200 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010201 }
Michael Chan566f86a2005-05-29 14:56:58 -070010202
Al Virob9fc7dc2007-12-17 22:59:57 -080010203 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010204 TG3_EEPROM_MAGIC_HW) {
10205 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010206 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010207 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010208
10209 /* Separate the parity bits and the data bytes. */
10210 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10211 if ((i == 0) || (i == 8)) {
10212 int l;
10213 u8 msk;
10214
10215 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10216 parity[k++] = buf8[i] & msk;
10217 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010218 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010219 int l;
10220 u8 msk;
10221
10222 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10223 parity[k++] = buf8[i] & msk;
10224 i++;
10225
10226 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10227 parity[k++] = buf8[i] & msk;
10228 i++;
10229 }
10230 data[j++] = buf8[i];
10231 }
10232
10233 err = -EIO;
10234 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10235 u8 hw8 = hweight8(data[i]);
10236
10237 if ((hw8 & 0x1) && parity[i])
10238 goto out;
10239 else if (!(hw8 & 0x1) && !parity[i])
10240 goto out;
10241 }
10242 err = 0;
10243 goto out;
10244 }
10245
Michael Chan566f86a2005-05-29 14:56:58 -070010246 /* Bootstrap checksum at offset 0x10 */
10247 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010248 if (csum != be32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010249 goto out;
10250
10251 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10252 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010253 if (csum != be32_to_cpu(buf[0xfc/4]))
10254 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010255
10256 err = 0;
10257
10258out:
10259 kfree(buf);
10260 return err;
10261}
10262
Michael Chanca430072005-05-29 14:57:23 -070010263#define TG3_SERDES_TIMEOUT_SEC 2
10264#define TG3_COPPER_TIMEOUT_SEC 6
10265
10266static int tg3_test_link(struct tg3 *tp)
10267{
10268 int i, max;
10269
10270 if (!netif_running(tp->dev))
10271 return -ENODEV;
10272
Michael Chan4c987482005-09-05 17:52:38 -070010273 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010274 max = TG3_SERDES_TIMEOUT_SEC;
10275 else
10276 max = TG3_COPPER_TIMEOUT_SEC;
10277
10278 for (i = 0; i < max; i++) {
10279 if (netif_carrier_ok(tp->dev))
10280 return 0;
10281
10282 if (msleep_interruptible(1000))
10283 break;
10284 }
10285
10286 return -EIO;
10287}
10288
Michael Chana71116d2005-05-29 14:58:11 -070010289/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010290static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010291{
Michael Chanb16250e2006-09-27 16:10:14 -070010292 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010293 u32 offset, read_mask, write_mask, val, save_val, read_val;
10294 static struct {
10295 u16 offset;
10296 u16 flags;
10297#define TG3_FL_5705 0x1
10298#define TG3_FL_NOT_5705 0x2
10299#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010300#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010301 u32 read_mask;
10302 u32 write_mask;
10303 } reg_tbl[] = {
10304 /* MAC Control Registers */
10305 { MAC_MODE, TG3_FL_NOT_5705,
10306 0x00000000, 0x00ef6f8c },
10307 { MAC_MODE, TG3_FL_5705,
10308 0x00000000, 0x01ef6b8c },
10309 { MAC_STATUS, TG3_FL_NOT_5705,
10310 0x03800107, 0x00000000 },
10311 { MAC_STATUS, TG3_FL_5705,
10312 0x03800100, 0x00000000 },
10313 { MAC_ADDR_0_HIGH, 0x0000,
10314 0x00000000, 0x0000ffff },
10315 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010316 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010317 { MAC_RX_MTU_SIZE, 0x0000,
10318 0x00000000, 0x0000ffff },
10319 { MAC_TX_MODE, 0x0000,
10320 0x00000000, 0x00000070 },
10321 { MAC_TX_LENGTHS, 0x0000,
10322 0x00000000, 0x00003fff },
10323 { MAC_RX_MODE, TG3_FL_NOT_5705,
10324 0x00000000, 0x000007fc },
10325 { MAC_RX_MODE, TG3_FL_5705,
10326 0x00000000, 0x000007dc },
10327 { MAC_HASH_REG_0, 0x0000,
10328 0x00000000, 0xffffffff },
10329 { MAC_HASH_REG_1, 0x0000,
10330 0x00000000, 0xffffffff },
10331 { MAC_HASH_REG_2, 0x0000,
10332 0x00000000, 0xffffffff },
10333 { MAC_HASH_REG_3, 0x0000,
10334 0x00000000, 0xffffffff },
10335
10336 /* Receive Data and Receive BD Initiator Control Registers. */
10337 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10338 0x00000000, 0xffffffff },
10339 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10340 0x00000000, 0xffffffff },
10341 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10342 0x00000000, 0x00000003 },
10343 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10344 0x00000000, 0xffffffff },
10345 { RCVDBDI_STD_BD+0, 0x0000,
10346 0x00000000, 0xffffffff },
10347 { RCVDBDI_STD_BD+4, 0x0000,
10348 0x00000000, 0xffffffff },
10349 { RCVDBDI_STD_BD+8, 0x0000,
10350 0x00000000, 0xffff0002 },
10351 { RCVDBDI_STD_BD+0xc, 0x0000,
10352 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010353
Michael Chana71116d2005-05-29 14:58:11 -070010354 /* Receive BD Initiator Control Registers. */
10355 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10356 0x00000000, 0xffffffff },
10357 { RCVBDI_STD_THRESH, TG3_FL_5705,
10358 0x00000000, 0x000003ff },
10359 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10360 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010361
Michael Chana71116d2005-05-29 14:58:11 -070010362 /* Host Coalescing Control Registers. */
10363 { HOSTCC_MODE, TG3_FL_NOT_5705,
10364 0x00000000, 0x00000004 },
10365 { HOSTCC_MODE, TG3_FL_5705,
10366 0x00000000, 0x000000f6 },
10367 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10368 0x00000000, 0xffffffff },
10369 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10370 0x00000000, 0x000003ff },
10371 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10372 0x00000000, 0xffffffff },
10373 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10374 0x00000000, 0x000003ff },
10375 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10376 0x00000000, 0xffffffff },
10377 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10378 0x00000000, 0x000000ff },
10379 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10380 0x00000000, 0xffffffff },
10381 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10382 0x00000000, 0x000000ff },
10383 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10384 0x00000000, 0xffffffff },
10385 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10386 0x00000000, 0xffffffff },
10387 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10388 0x00000000, 0xffffffff },
10389 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10390 0x00000000, 0x000000ff },
10391 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10392 0x00000000, 0xffffffff },
10393 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10394 0x00000000, 0x000000ff },
10395 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10396 0x00000000, 0xffffffff },
10397 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10398 0x00000000, 0xffffffff },
10399 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10400 0x00000000, 0xffffffff },
10401 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10402 0x00000000, 0xffffffff },
10403 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10404 0x00000000, 0xffffffff },
10405 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10406 0xffffffff, 0x00000000 },
10407 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10408 0xffffffff, 0x00000000 },
10409
10410 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010411 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010412 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010413 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010414 0x00000000, 0x007fffff },
10415 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10416 0x00000000, 0x0000003f },
10417 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10418 0x00000000, 0x000001ff },
10419 { BUFMGR_MB_HIGH_WATER, 0x0000,
10420 0x00000000, 0x000001ff },
10421 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10422 0xffffffff, 0x00000000 },
10423 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10424 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010425
Michael Chana71116d2005-05-29 14:58:11 -070010426 /* Mailbox Registers */
10427 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10428 0x00000000, 0x000001ff },
10429 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10430 0x00000000, 0x000001ff },
10431 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10432 0x00000000, 0x000007ff },
10433 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10434 0x00000000, 0x000001ff },
10435
10436 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10437 };
10438
Michael Chanb16250e2006-09-27 16:10:14 -070010439 is_5705 = is_5750 = 0;
10440 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010441 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010442 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10443 is_5750 = 1;
10444 }
Michael Chana71116d2005-05-29 14:58:11 -070010445
10446 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10447 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10448 continue;
10449
10450 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10451 continue;
10452
10453 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10454 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10455 continue;
10456
Michael Chanb16250e2006-09-27 16:10:14 -070010457 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10458 continue;
10459
Michael Chana71116d2005-05-29 14:58:11 -070010460 offset = (u32) reg_tbl[i].offset;
10461 read_mask = reg_tbl[i].read_mask;
10462 write_mask = reg_tbl[i].write_mask;
10463
10464 /* Save the original register content */
10465 save_val = tr32(offset);
10466
10467 /* Determine the read-only value. */
10468 read_val = save_val & read_mask;
10469
10470 /* Write zero to the register, then make sure the read-only bits
10471 * are not changed and the read/write bits are all zeros.
10472 */
10473 tw32(offset, 0);
10474
10475 val = tr32(offset);
10476
10477 /* Test the read-only and read/write bits. */
10478 if (((val & read_mask) != read_val) || (val & write_mask))
10479 goto out;
10480
10481 /* Write ones to all the bits defined by RdMask and WrMask, then
10482 * make sure the read-only bits are not changed and the
10483 * read/write bits are all ones.
10484 */
10485 tw32(offset, read_mask | write_mask);
10486
10487 val = tr32(offset);
10488
10489 /* Test the read-only bits. */
10490 if ((val & read_mask) != read_val)
10491 goto out;
10492
10493 /* Test the read/write bits. */
10494 if ((val & write_mask) != write_mask)
10495 goto out;
10496
10497 tw32(offset, save_val);
10498 }
10499
10500 return 0;
10501
10502out:
Michael Chan9f88f292006-12-07 00:22:54 -080010503 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010504 netdev_err(tp->dev,
10505 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010506 tw32(offset, save_val);
10507 return -EIO;
10508}
10509
Michael Chan7942e1d2005-05-29 14:58:36 -070010510static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10511{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010512 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010513 int i;
10514 u32 j;
10515
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010516 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010517 for (j = 0; j < len; j += 4) {
10518 u32 val;
10519
10520 tg3_write_mem(tp, offset + j, test_pattern[i]);
10521 tg3_read_mem(tp, offset + j, &val);
10522 if (val != test_pattern[i])
10523 return -EIO;
10524 }
10525 }
10526 return 0;
10527}
10528
10529static int tg3_test_memory(struct tg3 *tp)
10530{
10531 static struct mem_entry {
10532 u32 offset;
10533 u32 len;
10534 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010535 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010536 { 0x00002000, 0x1c000},
10537 { 0xffffffff, 0x00000}
10538 }, mem_tbl_5705[] = {
10539 { 0x00000100, 0x0000c},
10540 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010541 { 0x00004000, 0x00800},
10542 { 0x00006000, 0x01000},
10543 { 0x00008000, 0x02000},
10544 { 0x00010000, 0x0e000},
10545 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010546 }, mem_tbl_5755[] = {
10547 { 0x00000200, 0x00008},
10548 { 0x00004000, 0x00800},
10549 { 0x00006000, 0x00800},
10550 { 0x00008000, 0x02000},
10551 { 0x00010000, 0x0c000},
10552 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010553 }, mem_tbl_5906[] = {
10554 { 0x00000200, 0x00008},
10555 { 0x00004000, 0x00400},
10556 { 0x00006000, 0x00400},
10557 { 0x00008000, 0x01000},
10558 { 0x00010000, 0x01000},
10559 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010560 }, mem_tbl_5717[] = {
10561 { 0x00000200, 0x00008},
10562 { 0x00010000, 0x0a000},
10563 { 0x00020000, 0x13c00},
10564 { 0xffffffff, 0x00000}
10565 }, mem_tbl_57765[] = {
10566 { 0x00000200, 0x00008},
10567 { 0x00004000, 0x00800},
10568 { 0x00006000, 0x09800},
10569 { 0x00010000, 0x0a000},
10570 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010571 };
10572 struct mem_entry *mem_tbl;
10573 int err = 0;
10574 int i;
10575
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010576 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
10577 mem_tbl = mem_tbl_5717;
10578 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10579 mem_tbl = mem_tbl_57765;
10580 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlson321d32a2008-11-21 17:22:19 -080010581 mem_tbl = mem_tbl_5755;
10582 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10583 mem_tbl = mem_tbl_5906;
10584 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10585 mem_tbl = mem_tbl_5705;
10586 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010587 mem_tbl = mem_tbl_570x;
10588
10589 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
10590 if ((err = tg3_do_mem_test(tp, mem_tbl[i].offset,
10591 mem_tbl[i].len)) != 0)
10592 break;
10593 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010594
Michael Chan7942e1d2005-05-29 14:58:36 -070010595 return err;
10596}
10597
Michael Chan9f40dea2005-09-05 17:53:06 -070010598#define TG3_MAC_LOOPBACK 0
10599#define TG3_PHY_LOOPBACK 1
10600
10601static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010602{
Michael Chan9f40dea2005-09-05 17:53:06 -070010603 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010604 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010605 struct sk_buff *skb, *rx_skb;
10606 u8 *tx_data;
10607 dma_addr_t map;
10608 int num_pkts, tx_len, rx_len, i, err;
10609 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010610 struct tg3_napi *tnapi, *rnapi;
Matt Carlson21f581a2009-08-28 14:00:25 +000010611 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Michael Chanc76949a2005-05-29 14:58:59 -070010612
Matt Carlsonc8873402010-02-12 14:47:11 +000010613 tnapi = &tp->napi[0];
10614 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010615 if (tp->irq_cnt > 1) {
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010616 rnapi = &tp->napi[1];
Matt Carlsonc8873402010-02-12 14:47:11 +000010617 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
10618 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010619 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010620 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010621
Michael Chan9f40dea2005-09-05 17:53:06 -070010622 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010623 /* HW errata - mac loopback fails in some cases on 5780.
10624 * Normal traffic and PHY loopback are not affected by
10625 * errata.
10626 */
10627 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
10628 return 0;
10629
Michael Chan9f40dea2005-09-05 17:53:06 -070010630 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010631 MAC_MODE_PORT_INT_LPBACK;
10632 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10633 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chan3f7045c2006-09-27 16:02:29 -070010634 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
10635 mac_mode |= MAC_MODE_PORT_MODE_MII;
10636 else
10637 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010638 tw32(MAC_MODE, mac_mode);
10639 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010640 u32 val;
10641
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010642 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10643 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010644 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10645 } else
10646 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010647
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010648 tg3_phy_toggle_automdix(tp, 0);
10649
Michael Chan3f7045c2006-09-27 16:02:29 -070010650 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010651 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010652
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010653 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010654 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000010655 tg3_writephy(tp, MII_TG3_FET_PTEST,
10656 MII_TG3_FET_PTEST_FRC_TX_LINK |
10657 MII_TG3_FET_PTEST_FRC_TX_LOCK);
10658 /* The write needs to be flushed for the AC131 */
10659 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
10660 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080010661 mac_mode |= MAC_MODE_PORT_MODE_MII;
10662 } else
10663 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010664
Michael Chanc94e3942005-09-27 12:12:42 -070010665 /* reset to prevent losing 1st rx packet intermittently */
10666 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
10667 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10668 udelay(10);
10669 tw32_f(MAC_RX_MODE, tp->rx_mode);
10670 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010671 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000010672 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
10673 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010674 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000010675 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010676 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010677 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10678 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10679 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010680 tw32(MAC_MODE, mac_mode);
Matt Carlson859a588792010-04-05 10:19:28 +000010681 } else {
Michael Chan9f40dea2005-09-05 17:53:06 -070010682 return -EINVAL;
Matt Carlson859a588792010-04-05 10:19:28 +000010683 }
Michael Chanc76949a2005-05-29 14:58:59 -070010684
10685 err = -EIO;
10686
Michael Chanc76949a2005-05-29 14:58:59 -070010687 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010688 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010689 if (!skb)
10690 return -ENOMEM;
10691
Michael Chanc76949a2005-05-29 14:58:59 -070010692 tx_data = skb_put(skb, tx_len);
10693 memcpy(tx_data, tp->dev->dev_addr, 6);
10694 memset(tx_data + 6, 0x0, 8);
10695
10696 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10697
10698 for (i = 14; i < tx_len; i++)
10699 tx_data[i] = (u8) (i & 0xff);
10700
Alexander Duyckf4188d82009-12-02 16:48:38 +000010701 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
10702 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000010703 dev_kfree_skb(skb);
10704 return -EIO;
10705 }
Michael Chanc76949a2005-05-29 14:58:59 -070010706
10707 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010708 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010709
10710 udelay(10);
10711
Matt Carlson898a56f2009-08-28 14:02:40 +000010712 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070010713
Michael Chanc76949a2005-05-29 14:58:59 -070010714 num_pkts = 0;
10715
Alexander Duyckf4188d82009-12-02 16:48:38 +000010716 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070010717
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010718 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070010719 num_pkts++;
10720
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010721 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
10722 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070010723
10724 udelay(10);
10725
Matt Carlson303fc922009-11-02 14:27:34 +000010726 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
10727 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070010728 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010729 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010730
10731 udelay(10);
10732
Matt Carlson898a56f2009-08-28 14:02:40 +000010733 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
10734 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010735 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070010736 (rx_idx == (rx_start_idx + num_pkts)))
10737 break;
10738 }
10739
Alexander Duyckf4188d82009-12-02 16:48:38 +000010740 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070010741 dev_kfree_skb(skb);
10742
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010743 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070010744 goto out;
10745
10746 if (rx_idx != rx_start_idx + num_pkts)
10747 goto out;
10748
Matt Carlson72334482009-08-28 14:03:01 +000010749 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070010750 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
10751 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
10752 if (opaque_key != RXD_OPAQUE_RING_STD)
10753 goto out;
10754
10755 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
10756 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
10757 goto out;
10758
10759 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
10760 if (rx_len != tx_len)
10761 goto out;
10762
Matt Carlson21f581a2009-08-28 14:00:25 +000010763 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070010764
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +000010765 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070010766 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
10767
10768 for (i = 14; i < tx_len; i++) {
10769 if (*(rx_skb->data + i) != (u8) (i & 0xff))
10770 goto out;
10771 }
10772 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010773
Michael Chanc76949a2005-05-29 14:58:59 -070010774 /* tg3_free_rings will unmap and free the rx_skb */
10775out:
10776 return err;
10777}
10778
Michael Chan9f40dea2005-09-05 17:53:06 -070010779#define TG3_MAC_LOOPBACK_FAILED 1
10780#define TG3_PHY_LOOPBACK_FAILED 2
10781#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
10782 TG3_PHY_LOOPBACK_FAILED)
10783
10784static int tg3_test_loopback(struct tg3 *tp)
10785{
10786 int err = 0;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010787 u32 cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070010788
10789 if (!netif_running(tp->dev))
10790 return TG3_LOOPBACK_FAILED;
10791
Michael Chanb9ec6c12006-07-25 16:37:27 -070010792 err = tg3_reset_hw(tp, 1);
10793 if (err)
10794 return TG3_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070010795
Matt Carlson6833c042008-11-21 17:18:59 -080010796 /* Turn off gphy autopowerdown. */
10797 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10798 tg3_phy_toggle_apd(tp, false);
10799
Matt Carlson321d32a2008-11-21 17:22:19 -080010800 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010801 int i;
10802 u32 status;
10803
10804 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
10805
10806 /* Wait for up to 40 microseconds to acquire lock. */
10807 for (i = 0; i < 4; i++) {
10808 status = tr32(TG3_CPMU_MUTEX_GNT);
10809 if (status == CPMU_MUTEX_GNT_DRIVER)
10810 break;
10811 udelay(10);
10812 }
10813
10814 if (status != CPMU_MUTEX_GNT_DRIVER)
10815 return TG3_LOOPBACK_FAILED;
10816
Matt Carlsonb2a5c192008-04-03 21:44:44 -070010817 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080010818 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070010819 tw32(TG3_CPMU_CTRL,
10820 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
10821 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070010822 }
10823
Michael Chan9f40dea2005-09-05 17:53:06 -070010824 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
10825 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010826
Matt Carlson321d32a2008-11-21 17:22:19 -080010827 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010828 tw32(TG3_CPMU_CTRL, cpmuctrl);
10829
10830 /* Release the mutex */
10831 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
10832 }
10833
Matt Carlsondd477002008-05-25 23:45:58 -070010834 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
10835 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070010836 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
10837 err |= TG3_PHY_LOOPBACK_FAILED;
10838 }
10839
Matt Carlson6833c042008-11-21 17:18:59 -080010840 /* Re-enable gphy autopowerdown. */
10841 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10842 tg3_phy_toggle_apd(tp, true);
10843
Michael Chan9f40dea2005-09-05 17:53:06 -070010844 return err;
10845}
10846
Michael Chan4cafd3f2005-05-29 14:56:34 -070010847static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
10848 u64 *data)
10849{
Michael Chan566f86a2005-05-29 14:56:58 -070010850 struct tg3 *tp = netdev_priv(dev);
10851
Michael Chanbc1c7562006-03-20 17:48:03 -080010852 if (tp->link_config.phy_is_low_power)
10853 tg3_set_power_state(tp, PCI_D0);
10854
Michael Chan566f86a2005-05-29 14:56:58 -070010855 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
10856
10857 if (tg3_test_nvram(tp) != 0) {
10858 etest->flags |= ETH_TEST_FL_FAILED;
10859 data[0] = 1;
10860 }
Michael Chanca430072005-05-29 14:57:23 -070010861 if (tg3_test_link(tp) != 0) {
10862 etest->flags |= ETH_TEST_FL_FAILED;
10863 data[1] = 1;
10864 }
Michael Chana71116d2005-05-29 14:58:11 -070010865 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010866 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070010867
Michael Chanbbe832c2005-06-24 20:20:04 -070010868 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010869 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010870 tg3_netif_stop(tp);
10871 irq_sync = 1;
10872 }
10873
10874 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070010875
10876 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080010877 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010878 tg3_halt_cpu(tp, RX_CPU_BASE);
10879 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10880 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080010881 if (!err)
10882 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010883
Michael Chand9ab5ad2006-03-20 22:27:35 -080010884 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
10885 tg3_phy_reset(tp);
10886
Michael Chana71116d2005-05-29 14:58:11 -070010887 if (tg3_test_registers(tp) != 0) {
10888 etest->flags |= ETH_TEST_FL_FAILED;
10889 data[2] = 1;
10890 }
Michael Chan7942e1d2005-05-29 14:58:36 -070010891 if (tg3_test_memory(tp) != 0) {
10892 etest->flags |= ETH_TEST_FL_FAILED;
10893 data[3] = 1;
10894 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010895 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070010896 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070010897
David S. Millerf47c11e2005-06-24 20:18:35 -070010898 tg3_full_unlock(tp);
10899
Michael Chand4bc3922005-05-29 14:59:20 -070010900 if (tg3_test_interrupt(tp) != 0) {
10901 etest->flags |= ETH_TEST_FL_FAILED;
10902 data[5] = 1;
10903 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010904
10905 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070010906
Michael Chana71116d2005-05-29 14:58:11 -070010907 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10908 if (netif_running(dev)) {
10909 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010910 err2 = tg3_restart_hw(tp, 1);
10911 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070010912 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010913 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010914
10915 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010916
10917 if (irq_sync && !err2)
10918 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010919 }
Michael Chanbc1c7562006-03-20 17:48:03 -080010920 if (tp->link_config.phy_is_low_power)
10921 tg3_set_power_state(tp, PCI_D3hot);
10922
Michael Chan4cafd3f2005-05-29 14:56:34 -070010923}
10924
Linus Torvalds1da177e2005-04-16 15:20:36 -070010925static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10926{
10927 struct mii_ioctl_data *data = if_mii(ifr);
10928 struct tg3 *tp = netdev_priv(dev);
10929 int err;
10930
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010931 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010932 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010933 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
10934 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010935 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10936 return phy_mii_ioctl(phydev, data, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010937 }
10938
Matt Carlson33f401a2010-04-05 10:19:27 +000010939 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010940 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000010941 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010942
10943 /* fallthru */
10944 case SIOCGMIIREG: {
10945 u32 mii_regval;
10946
10947 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10948 break; /* We have no PHY */
10949
Michael Chanbc1c7562006-03-20 17:48:03 -080010950 if (tp->link_config.phy_is_low_power)
10951 return -EAGAIN;
10952
David S. Millerf47c11e2005-06-24 20:18:35 -070010953 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010954 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070010955 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010956
10957 data->val_out = mii_regval;
10958
10959 return err;
10960 }
10961
10962 case SIOCSMIIREG:
10963 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10964 break; /* We have no PHY */
10965
Michael Chanbc1c7562006-03-20 17:48:03 -080010966 if (tp->link_config.phy_is_low_power)
10967 return -EAGAIN;
10968
David S. Millerf47c11e2005-06-24 20:18:35 -070010969 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010970 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070010971 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010972
10973 return err;
10974
10975 default:
10976 /* do nothing */
10977 break;
10978 }
10979 return -EOPNOTSUPP;
10980}
10981
10982#if TG3_VLAN_TAG_USED
10983static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
10984{
10985 struct tg3 *tp = netdev_priv(dev);
10986
Matt Carlson844b3ee2009-02-25 14:23:56 +000010987 if (!netif_running(dev)) {
10988 tp->vlgrp = grp;
10989 return;
10990 }
10991
10992 tg3_netif_stop(tp);
Michael Chan29315e82006-06-29 20:12:30 -070010993
David S. Millerf47c11e2005-06-24 20:18:35 -070010994 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010995
10996 tp->vlgrp = grp;
10997
10998 /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
10999 __tg3_set_rx_mode(dev);
11000
Matt Carlson844b3ee2009-02-25 14:23:56 +000011001 tg3_netif_start(tp);
Michael Chan46966542007-07-11 19:47:19 -070011002
11003 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011004}
Linus Torvalds1da177e2005-04-16 15:20:36 -070011005#endif
11006
David S. Miller15f98502005-05-18 22:49:26 -070011007static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11008{
11009 struct tg3 *tp = netdev_priv(dev);
11010
11011 memcpy(ec, &tp->coal, sizeof(*ec));
11012 return 0;
11013}
11014
Michael Chand244c892005-07-05 14:42:33 -070011015static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11016{
11017 struct tg3 *tp = netdev_priv(dev);
11018 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11019 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11020
11021 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11022 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11023 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11024 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11025 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11026 }
11027
11028 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11029 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11030 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11031 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11032 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11033 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11034 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11035 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11036 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11037 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11038 return -EINVAL;
11039
11040 /* No rx interrupts will be generated if both are zero */
11041 if ((ec->rx_coalesce_usecs == 0) &&
11042 (ec->rx_max_coalesced_frames == 0))
11043 return -EINVAL;
11044
11045 /* No tx interrupts will be generated if both are zero */
11046 if ((ec->tx_coalesce_usecs == 0) &&
11047 (ec->tx_max_coalesced_frames == 0))
11048 return -EINVAL;
11049
11050 /* Only copy relevant parameters, ignore all others. */
11051 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11052 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11053 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11054 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11055 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11056 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11057 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11058 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11059 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11060
11061 if (netif_running(dev)) {
11062 tg3_full_lock(tp, 0);
11063 __tg3_set_coalesce(tp, &tp->coal);
11064 tg3_full_unlock(tp);
11065 }
11066 return 0;
11067}
11068
Jeff Garzik7282d492006-09-13 14:30:00 -040011069static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011070 .get_settings = tg3_get_settings,
11071 .set_settings = tg3_set_settings,
11072 .get_drvinfo = tg3_get_drvinfo,
11073 .get_regs_len = tg3_get_regs_len,
11074 .get_regs = tg3_get_regs,
11075 .get_wol = tg3_get_wol,
11076 .set_wol = tg3_set_wol,
11077 .get_msglevel = tg3_get_msglevel,
11078 .set_msglevel = tg3_set_msglevel,
11079 .nway_reset = tg3_nway_reset,
11080 .get_link = ethtool_op_get_link,
11081 .get_eeprom_len = tg3_get_eeprom_len,
11082 .get_eeprom = tg3_get_eeprom,
11083 .set_eeprom = tg3_set_eeprom,
11084 .get_ringparam = tg3_get_ringparam,
11085 .set_ringparam = tg3_set_ringparam,
11086 .get_pauseparam = tg3_get_pauseparam,
11087 .set_pauseparam = tg3_set_pauseparam,
11088 .get_rx_csum = tg3_get_rx_csum,
11089 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011090 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011091 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011092 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011093 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011094 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070011095 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011096 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011097 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011098 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011099 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011100};
11101
11102static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11103{
Michael Chan1b277772006-03-20 22:27:48 -080011104 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011105
11106 tp->nvram_size = EEPROM_CHIP_SIZE;
11107
Matt Carlsone4f34112009-02-25 14:25:00 +000011108 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011109 return;
11110
Michael Chanb16250e2006-09-27 16:10:14 -070011111 if ((magic != TG3_EEPROM_MAGIC) &&
11112 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11113 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011114 return;
11115
11116 /*
11117 * Size the chip by reading offsets at increasing powers of two.
11118 * When we encounter our validation signature, we know the addressing
11119 * has wrapped around, and thus have our chip size.
11120 */
Michael Chan1b277772006-03-20 22:27:48 -080011121 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011122
11123 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011124 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011125 return;
11126
Michael Chan18201802006-03-20 22:29:15 -080011127 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011128 break;
11129
11130 cursize <<= 1;
11131 }
11132
11133 tp->nvram_size = cursize;
11134}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011135
Linus Torvalds1da177e2005-04-16 15:20:36 -070011136static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11137{
11138 u32 val;
11139
Matt Carlsondf259d82009-04-20 06:57:14 +000011140 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11141 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011142 return;
11143
11144 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011145 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011146 tg3_get_eeprom_size(tp);
11147 return;
11148 }
11149
Matt Carlson6d348f22009-02-25 14:25:52 +000011150 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011151 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011152 /* This is confusing. We want to operate on the
11153 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11154 * call will read from NVRAM and byteswap the data
11155 * according to the byteswapping settings for all
11156 * other register accesses. This ensures the data we
11157 * want will always reside in the lower 16-bits.
11158 * However, the data in NVRAM is in LE format, which
11159 * means the data from the NVRAM read will always be
11160 * opposite the endianness of the CPU. The 16-bit
11161 * byteswap then brings the data to CPU endianness.
11162 */
11163 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011164 return;
11165 }
11166 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011167 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011168}
11169
11170static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11171{
11172 u32 nvcfg1;
11173
11174 nvcfg1 = tr32(NVRAM_CFG1);
11175 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11176 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011177 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011178 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11179 tw32(NVRAM_CFG1, nvcfg1);
11180 }
11181
Michael Chan4c987482005-09-05 17:52:38 -070011182 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011183 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011184 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011185 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11186 tp->nvram_jedecnum = JEDEC_ATMEL;
11187 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11188 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11189 break;
11190 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11191 tp->nvram_jedecnum = JEDEC_ATMEL;
11192 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11193 break;
11194 case FLASH_VENDOR_ATMEL_EEPROM:
11195 tp->nvram_jedecnum = JEDEC_ATMEL;
11196 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11197 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11198 break;
11199 case FLASH_VENDOR_ST:
11200 tp->nvram_jedecnum = JEDEC_ST;
11201 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11202 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11203 break;
11204 case FLASH_VENDOR_SAIFUN:
11205 tp->nvram_jedecnum = JEDEC_SAIFUN;
11206 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11207 break;
11208 case FLASH_VENDOR_SST_SMALL:
11209 case FLASH_VENDOR_SST_LARGE:
11210 tp->nvram_jedecnum = JEDEC_SST;
11211 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11212 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011213 }
Matt Carlson8590a602009-08-28 12:29:16 +000011214 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011215 tp->nvram_jedecnum = JEDEC_ATMEL;
11216 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11217 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11218 }
11219}
11220
Matt Carlsona1b950d2009-09-01 13:20:17 +000011221static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11222{
11223 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11224 case FLASH_5752PAGE_SIZE_256:
11225 tp->nvram_pagesize = 256;
11226 break;
11227 case FLASH_5752PAGE_SIZE_512:
11228 tp->nvram_pagesize = 512;
11229 break;
11230 case FLASH_5752PAGE_SIZE_1K:
11231 tp->nvram_pagesize = 1024;
11232 break;
11233 case FLASH_5752PAGE_SIZE_2K:
11234 tp->nvram_pagesize = 2048;
11235 break;
11236 case FLASH_5752PAGE_SIZE_4K:
11237 tp->nvram_pagesize = 4096;
11238 break;
11239 case FLASH_5752PAGE_SIZE_264:
11240 tp->nvram_pagesize = 264;
11241 break;
11242 case FLASH_5752PAGE_SIZE_528:
11243 tp->nvram_pagesize = 528;
11244 break;
11245 }
11246}
11247
Michael Chan361b4ac2005-04-21 17:11:21 -070011248static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11249{
11250 u32 nvcfg1;
11251
11252 nvcfg1 = tr32(NVRAM_CFG1);
11253
Michael Chane6af3012005-04-21 17:12:05 -070011254 /* NVRAM protection for TPM */
11255 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011256 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011257
Michael Chan361b4ac2005-04-21 17:11:21 -070011258 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011259 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11260 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11261 tp->nvram_jedecnum = JEDEC_ATMEL;
11262 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11263 break;
11264 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11265 tp->nvram_jedecnum = JEDEC_ATMEL;
11266 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11267 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11268 break;
11269 case FLASH_5752VENDOR_ST_M45PE10:
11270 case FLASH_5752VENDOR_ST_M45PE20:
11271 case FLASH_5752VENDOR_ST_M45PE40:
11272 tp->nvram_jedecnum = JEDEC_ST;
11273 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11274 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11275 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011276 }
11277
11278 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011279 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011280 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011281 /* For eeprom, set pagesize to maximum eeprom size */
11282 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11283
11284 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11285 tw32(NVRAM_CFG1, nvcfg1);
11286 }
11287}
11288
Michael Chand3c7b882006-03-23 01:28:25 -080011289static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11290{
Matt Carlson989a9d22007-05-05 11:51:05 -070011291 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011292
11293 nvcfg1 = tr32(NVRAM_CFG1);
11294
11295 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011296 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011297 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011298 protect = 1;
11299 }
Michael Chand3c7b882006-03-23 01:28:25 -080011300
Matt Carlson989a9d22007-05-05 11:51:05 -070011301 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11302 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011303 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11304 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11305 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11306 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11307 tp->nvram_jedecnum = JEDEC_ATMEL;
11308 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11309 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11310 tp->nvram_pagesize = 264;
11311 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11312 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11313 tp->nvram_size = (protect ? 0x3e200 :
11314 TG3_NVRAM_SIZE_512KB);
11315 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11316 tp->nvram_size = (protect ? 0x1f200 :
11317 TG3_NVRAM_SIZE_256KB);
11318 else
11319 tp->nvram_size = (protect ? 0x1f200 :
11320 TG3_NVRAM_SIZE_128KB);
11321 break;
11322 case FLASH_5752VENDOR_ST_M45PE10:
11323 case FLASH_5752VENDOR_ST_M45PE20:
11324 case FLASH_5752VENDOR_ST_M45PE40:
11325 tp->nvram_jedecnum = JEDEC_ST;
11326 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11327 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11328 tp->nvram_pagesize = 256;
11329 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11330 tp->nvram_size = (protect ?
11331 TG3_NVRAM_SIZE_64KB :
11332 TG3_NVRAM_SIZE_128KB);
11333 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11334 tp->nvram_size = (protect ?
11335 TG3_NVRAM_SIZE_64KB :
11336 TG3_NVRAM_SIZE_256KB);
11337 else
11338 tp->nvram_size = (protect ?
11339 TG3_NVRAM_SIZE_128KB :
11340 TG3_NVRAM_SIZE_512KB);
11341 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011342 }
11343}
11344
Michael Chan1b277772006-03-20 22:27:48 -080011345static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11346{
11347 u32 nvcfg1;
11348
11349 nvcfg1 = tr32(NVRAM_CFG1);
11350
11351 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011352 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11353 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11354 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11355 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11356 tp->nvram_jedecnum = JEDEC_ATMEL;
11357 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11358 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011359
Matt Carlson8590a602009-08-28 12:29:16 +000011360 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11361 tw32(NVRAM_CFG1, nvcfg1);
11362 break;
11363 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11364 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11365 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11366 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11367 tp->nvram_jedecnum = JEDEC_ATMEL;
11368 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11369 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11370 tp->nvram_pagesize = 264;
11371 break;
11372 case FLASH_5752VENDOR_ST_M45PE10:
11373 case FLASH_5752VENDOR_ST_M45PE20:
11374 case FLASH_5752VENDOR_ST_M45PE40:
11375 tp->nvram_jedecnum = JEDEC_ST;
11376 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11377 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11378 tp->nvram_pagesize = 256;
11379 break;
Michael Chan1b277772006-03-20 22:27:48 -080011380 }
11381}
11382
Matt Carlson6b91fa02007-10-10 18:01:09 -070011383static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11384{
11385 u32 nvcfg1, protect = 0;
11386
11387 nvcfg1 = tr32(NVRAM_CFG1);
11388
11389 /* NVRAM protection for TPM */
11390 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011391 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011392 protect = 1;
11393 }
11394
11395 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11396 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011397 case FLASH_5761VENDOR_ATMEL_ADB021D:
11398 case FLASH_5761VENDOR_ATMEL_ADB041D:
11399 case FLASH_5761VENDOR_ATMEL_ADB081D:
11400 case FLASH_5761VENDOR_ATMEL_ADB161D:
11401 case FLASH_5761VENDOR_ATMEL_MDB021D:
11402 case FLASH_5761VENDOR_ATMEL_MDB041D:
11403 case FLASH_5761VENDOR_ATMEL_MDB081D:
11404 case FLASH_5761VENDOR_ATMEL_MDB161D:
11405 tp->nvram_jedecnum = JEDEC_ATMEL;
11406 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11407 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11408 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11409 tp->nvram_pagesize = 256;
11410 break;
11411 case FLASH_5761VENDOR_ST_A_M45PE20:
11412 case FLASH_5761VENDOR_ST_A_M45PE40:
11413 case FLASH_5761VENDOR_ST_A_M45PE80:
11414 case FLASH_5761VENDOR_ST_A_M45PE16:
11415 case FLASH_5761VENDOR_ST_M_M45PE20:
11416 case FLASH_5761VENDOR_ST_M_M45PE40:
11417 case FLASH_5761VENDOR_ST_M_M45PE80:
11418 case FLASH_5761VENDOR_ST_M_M45PE16:
11419 tp->nvram_jedecnum = JEDEC_ST;
11420 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11421 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11422 tp->nvram_pagesize = 256;
11423 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011424 }
11425
11426 if (protect) {
11427 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11428 } else {
11429 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011430 case FLASH_5761VENDOR_ATMEL_ADB161D:
11431 case FLASH_5761VENDOR_ATMEL_MDB161D:
11432 case FLASH_5761VENDOR_ST_A_M45PE16:
11433 case FLASH_5761VENDOR_ST_M_M45PE16:
11434 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11435 break;
11436 case FLASH_5761VENDOR_ATMEL_ADB081D:
11437 case FLASH_5761VENDOR_ATMEL_MDB081D:
11438 case FLASH_5761VENDOR_ST_A_M45PE80:
11439 case FLASH_5761VENDOR_ST_M_M45PE80:
11440 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11441 break;
11442 case FLASH_5761VENDOR_ATMEL_ADB041D:
11443 case FLASH_5761VENDOR_ATMEL_MDB041D:
11444 case FLASH_5761VENDOR_ST_A_M45PE40:
11445 case FLASH_5761VENDOR_ST_M_M45PE40:
11446 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11447 break;
11448 case FLASH_5761VENDOR_ATMEL_ADB021D:
11449 case FLASH_5761VENDOR_ATMEL_MDB021D:
11450 case FLASH_5761VENDOR_ST_A_M45PE20:
11451 case FLASH_5761VENDOR_ST_M_M45PE20:
11452 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11453 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011454 }
11455 }
11456}
11457
Michael Chanb5d37722006-09-27 16:06:21 -070011458static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11459{
11460 tp->nvram_jedecnum = JEDEC_ATMEL;
11461 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11462 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11463}
11464
Matt Carlson321d32a2008-11-21 17:22:19 -080011465static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11466{
11467 u32 nvcfg1;
11468
11469 nvcfg1 = tr32(NVRAM_CFG1);
11470
11471 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11472 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11473 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11474 tp->nvram_jedecnum = JEDEC_ATMEL;
11475 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11476 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11477
11478 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11479 tw32(NVRAM_CFG1, nvcfg1);
11480 return;
11481 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11482 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11483 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11484 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11485 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11486 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11487 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11488 tp->nvram_jedecnum = JEDEC_ATMEL;
11489 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11490 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11491
11492 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11493 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11494 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11495 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11496 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11497 break;
11498 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11499 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11500 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11501 break;
11502 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11503 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11504 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11505 break;
11506 }
11507 break;
11508 case FLASH_5752VENDOR_ST_M45PE10:
11509 case FLASH_5752VENDOR_ST_M45PE20:
11510 case FLASH_5752VENDOR_ST_M45PE40:
11511 tp->nvram_jedecnum = JEDEC_ST;
11512 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11513 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11514
11515 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11516 case FLASH_5752VENDOR_ST_M45PE10:
11517 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11518 break;
11519 case FLASH_5752VENDOR_ST_M45PE20:
11520 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11521 break;
11522 case FLASH_5752VENDOR_ST_M45PE40:
11523 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11524 break;
11525 }
11526 break;
11527 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011528 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011529 return;
11530 }
11531
Matt Carlsona1b950d2009-09-01 13:20:17 +000011532 tg3_nvram_get_pagesize(tp, nvcfg1);
11533 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011534 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011535}
11536
11537
11538static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11539{
11540 u32 nvcfg1;
11541
11542 nvcfg1 = tr32(NVRAM_CFG1);
11543
11544 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11545 case FLASH_5717VENDOR_ATMEL_EEPROM:
11546 case FLASH_5717VENDOR_MICRO_EEPROM:
11547 tp->nvram_jedecnum = JEDEC_ATMEL;
11548 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11549 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11550
11551 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11552 tw32(NVRAM_CFG1, nvcfg1);
11553 return;
11554 case FLASH_5717VENDOR_ATMEL_MDB011D:
11555 case FLASH_5717VENDOR_ATMEL_ADB011B:
11556 case FLASH_5717VENDOR_ATMEL_ADB011D:
11557 case FLASH_5717VENDOR_ATMEL_MDB021D:
11558 case FLASH_5717VENDOR_ATMEL_ADB021B:
11559 case FLASH_5717VENDOR_ATMEL_ADB021D:
11560 case FLASH_5717VENDOR_ATMEL_45USPT:
11561 tp->nvram_jedecnum = JEDEC_ATMEL;
11562 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11563 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11564
11565 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11566 case FLASH_5717VENDOR_ATMEL_MDB021D:
11567 case FLASH_5717VENDOR_ATMEL_ADB021B:
11568 case FLASH_5717VENDOR_ATMEL_ADB021D:
11569 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11570 break;
11571 default:
11572 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11573 break;
11574 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011575 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011576 case FLASH_5717VENDOR_ST_M_M25PE10:
11577 case FLASH_5717VENDOR_ST_A_M25PE10:
11578 case FLASH_5717VENDOR_ST_M_M45PE10:
11579 case FLASH_5717VENDOR_ST_A_M45PE10:
11580 case FLASH_5717VENDOR_ST_M_M25PE20:
11581 case FLASH_5717VENDOR_ST_A_M25PE20:
11582 case FLASH_5717VENDOR_ST_M_M45PE20:
11583 case FLASH_5717VENDOR_ST_A_M45PE20:
11584 case FLASH_5717VENDOR_ST_25USPT:
11585 case FLASH_5717VENDOR_ST_45USPT:
11586 tp->nvram_jedecnum = JEDEC_ST;
11587 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11588 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11589
11590 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11591 case FLASH_5717VENDOR_ST_M_M25PE20:
11592 case FLASH_5717VENDOR_ST_A_M25PE20:
11593 case FLASH_5717VENDOR_ST_M_M45PE20:
11594 case FLASH_5717VENDOR_ST_A_M45PE20:
11595 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11596 break;
11597 default:
11598 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11599 break;
11600 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011601 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011602 default:
11603 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11604 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011605 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011606
11607 tg3_nvram_get_pagesize(tp, nvcfg1);
11608 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11609 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011610}
11611
Linus Torvalds1da177e2005-04-16 15:20:36 -070011612/* Chips other than 5700/5701 use the NVRAM for fetching info. */
11613static void __devinit tg3_nvram_init(struct tg3 *tp)
11614{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011615 tw32_f(GRC_EEPROM_ADDR,
11616 (EEPROM_ADDR_FSM_RESET |
11617 (EEPROM_DEFAULT_CLOCK_PERIOD <<
11618 EEPROM_ADDR_CLKPERD_SHIFT)));
11619
Michael Chan9d57f012006-12-07 00:23:25 -080011620 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011621
11622 /* Enable seeprom accesses. */
11623 tw32_f(GRC_LOCAL_CTRL,
11624 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
11625 udelay(100);
11626
11627 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
11628 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
11629 tp->tg3_flags |= TG3_FLAG_NVRAM;
11630
Michael Chanec41c7d2006-01-17 02:40:55 -080011631 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000011632 netdev_warn(tp->dev,
11633 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000011634 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080011635 return;
11636 }
Michael Chane6af3012005-04-21 17:12:05 -070011637 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011638
Matt Carlson989a9d22007-05-05 11:51:05 -070011639 tp->nvram_size = 0;
11640
Michael Chan361b4ac2005-04-21 17:11:21 -070011641 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11642 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080011643 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
11644 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070011645 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011646 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
11647 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080011648 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011649 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
11650 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070011651 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11652 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000011653 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
11654 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080011655 tg3_get_57780_nvram_info(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000011656 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
11657 tg3_get_5717_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070011658 else
11659 tg3_get_nvram_info(tp);
11660
Matt Carlson989a9d22007-05-05 11:51:05 -070011661 if (tp->nvram_size == 0)
11662 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011663
Michael Chane6af3012005-04-21 17:12:05 -070011664 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080011665 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011666
11667 } else {
11668 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
11669
11670 tg3_get_eeprom_size(tp);
11671 }
11672}
11673
Linus Torvalds1da177e2005-04-16 15:20:36 -070011674static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
11675 u32 offset, u32 len, u8 *buf)
11676{
11677 int i, j, rc = 0;
11678 u32 val;
11679
11680 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011681 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011682 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011683
11684 addr = offset + i;
11685
11686 memcpy(&data, buf + i, 4);
11687
Matt Carlson62cedd12009-04-20 14:52:29 -070011688 /*
11689 * The SEEPROM interface expects the data to always be opposite
11690 * the native endian format. We accomplish this by reversing
11691 * all the operations that would have been performed on the
11692 * data from a call to tg3_nvram_read_be32().
11693 */
11694 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011695
11696 val = tr32(GRC_EEPROM_ADDR);
11697 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
11698
11699 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
11700 EEPROM_ADDR_READ);
11701 tw32(GRC_EEPROM_ADDR, val |
11702 (0 << EEPROM_ADDR_DEVID_SHIFT) |
11703 (addr & EEPROM_ADDR_ADDR_MASK) |
11704 EEPROM_ADDR_START |
11705 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011706
Michael Chan9d57f012006-12-07 00:23:25 -080011707 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011708 val = tr32(GRC_EEPROM_ADDR);
11709
11710 if (val & EEPROM_ADDR_COMPLETE)
11711 break;
Michael Chan9d57f012006-12-07 00:23:25 -080011712 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011713 }
11714 if (!(val & EEPROM_ADDR_COMPLETE)) {
11715 rc = -EBUSY;
11716 break;
11717 }
11718 }
11719
11720 return rc;
11721}
11722
11723/* offset and length are dword aligned */
11724static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
11725 u8 *buf)
11726{
11727 int ret = 0;
11728 u32 pagesize = tp->nvram_pagesize;
11729 u32 pagemask = pagesize - 1;
11730 u32 nvram_cmd;
11731 u8 *tmp;
11732
11733 tmp = kmalloc(pagesize, GFP_KERNEL);
11734 if (tmp == NULL)
11735 return -ENOMEM;
11736
11737 while (len) {
11738 int j;
Michael Chane6af3012005-04-21 17:12:05 -070011739 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011740
11741 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011742
Linus Torvalds1da177e2005-04-16 15:20:36 -070011743 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011744 ret = tg3_nvram_read_be32(tp, phy_addr + j,
11745 (__be32 *) (tmp + j));
11746 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011747 break;
11748 }
11749 if (ret)
11750 break;
11751
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011752 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011753 size = pagesize;
11754 if (len < size)
11755 size = len;
11756
11757 len -= size;
11758
11759 memcpy(tmp + page_off, buf, size);
11760
11761 offset = offset + (pagesize - page_off);
11762
Michael Chane6af3012005-04-21 17:12:05 -070011763 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011764
11765 /*
11766 * Before we can erase the flash page, we need
11767 * to issue a special "write enable" command.
11768 */
11769 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11770
11771 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11772 break;
11773
11774 /* Erase the target page */
11775 tw32(NVRAM_ADDR, phy_addr);
11776
11777 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
11778 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
11779
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011780 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011781 break;
11782
11783 /* Issue another write enable to start the write. */
11784 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11785
11786 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11787 break;
11788
11789 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011790 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011791
Al Virob9fc7dc2007-12-17 22:59:57 -080011792 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000011793
Al Virob9fc7dc2007-12-17 22:59:57 -080011794 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011795
11796 tw32(NVRAM_ADDR, phy_addr + j);
11797
11798 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
11799 NVRAM_CMD_WR;
11800
11801 if (j == 0)
11802 nvram_cmd |= NVRAM_CMD_FIRST;
11803 else if (j == (pagesize - 4))
11804 nvram_cmd |= NVRAM_CMD_LAST;
11805
11806 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11807 break;
11808 }
11809 if (ret)
11810 break;
11811 }
11812
11813 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11814 tg3_nvram_exec_cmd(tp, nvram_cmd);
11815
11816 kfree(tmp);
11817
11818 return ret;
11819}
11820
11821/* offset and length are dword aligned */
11822static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
11823 u8 *buf)
11824{
11825 int i, ret = 0;
11826
11827 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011828 u32 page_off, phy_addr, nvram_cmd;
11829 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011830
11831 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080011832 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011833
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011834 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011835
Michael Chan18201802006-03-20 22:29:15 -080011836 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011837
11838 tw32(NVRAM_ADDR, phy_addr);
11839
11840 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
11841
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011842 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011843 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070011844 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011845 nvram_cmd |= NVRAM_CMD_LAST;
11846
11847 if (i == (len - 4))
11848 nvram_cmd |= NVRAM_CMD_LAST;
11849
Matt Carlson321d32a2008-11-21 17:22:19 -080011850 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
11851 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070011852 (tp->nvram_jedecnum == JEDEC_ST) &&
11853 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011854
11855 if ((ret = tg3_nvram_exec_cmd(tp,
11856 NVRAM_CMD_WREN | NVRAM_CMD_GO |
11857 NVRAM_CMD_DONE)))
11858
11859 break;
11860 }
11861 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11862 /* We always do complete word writes to eeprom. */
11863 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
11864 }
11865
11866 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11867 break;
11868 }
11869 return ret;
11870}
11871
11872/* offset and length are dword aligned */
11873static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
11874{
11875 int ret;
11876
Linus Torvalds1da177e2005-04-16 15:20:36 -070011877 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011878 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
11879 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011880 udelay(40);
11881 }
11882
11883 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
11884 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000011885 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011886 u32 grc_mode;
11887
Michael Chanec41c7d2006-01-17 02:40:55 -080011888 ret = tg3_nvram_lock(tp);
11889 if (ret)
11890 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011891
Michael Chane6af3012005-04-21 17:12:05 -070011892 tg3_enable_nvram_access(tp);
11893 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011894 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011895 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011896
11897 grc_mode = tr32(GRC_MODE);
11898 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
11899
11900 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
11901 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11902
11903 ret = tg3_nvram_write_block_buffered(tp, offset, len,
11904 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000011905 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011906 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
11907 buf);
11908 }
11909
11910 grc_mode = tr32(GRC_MODE);
11911 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
11912
Michael Chane6af3012005-04-21 17:12:05 -070011913 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011914 tg3_nvram_unlock(tp);
11915 }
11916
11917 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011918 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011919 udelay(40);
11920 }
11921
11922 return ret;
11923}
11924
11925struct subsys_tbl_ent {
11926 u16 subsys_vendor, subsys_devid;
11927 u32 phy_id;
11928};
11929
Matt Carlson24daf2b2010-02-17 15:17:02 +000011930static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011931 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011932 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011933 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011934 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011935 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011936 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011937 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011938 { TG3PCI_SUBVENDOR_ID_BROADCOM,
11939 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
11940 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011941 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011942 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011943 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011944 { TG3PCI_SUBVENDOR_ID_BROADCOM,
11945 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
11946 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011947 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011948 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011949 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011950 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011951 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011952 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011953 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011954
11955 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011956 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011957 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011958 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011959 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011960 { TG3PCI_SUBVENDOR_ID_3COM,
11961 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
11962 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011963 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011964 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011965 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011966
11967 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011968 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011969 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011970 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011971 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011972 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011973 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011974 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011975 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011976
11977 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011978 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011979 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011980 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011981 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011982 { TG3PCI_SUBVENDOR_ID_COMPAQ,
11983 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
11984 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011985 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011986 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011987 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011988
11989 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011990 { TG3PCI_SUBVENDOR_ID_IBM,
11991 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011992};
11993
Matt Carlson24daf2b2010-02-17 15:17:02 +000011994static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011995{
11996 int i;
11997
11998 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
11999 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12000 tp->pdev->subsystem_vendor) &&
12001 (subsys_id_to_phy_id[i].subsys_devid ==
12002 tp->pdev->subsystem_device))
12003 return &subsys_id_to_phy_id[i];
12004 }
12005 return NULL;
12006}
12007
Michael Chan7d0c41e2005-04-21 17:06:20 -070012008static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012009{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012010 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012011 u16 pmcsr;
12012
12013 /* On some early chips the SRAM cannot be accessed in D3hot state,
12014 * so need make sure we're in D0.
12015 */
12016 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12017 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12018 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12019 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012020
12021 /* Make sure register accesses (indirect or otherwise)
12022 * will function correctly.
12023 */
12024 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12025 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012026
David S. Millerf49639e2006-06-09 11:58:36 -070012027 /* The memory arbiter has to be enabled in order for SRAM accesses
12028 * to succeed. Normally on powerup the tg3 chip firmware will make
12029 * sure it is enabled, but other entities such as system netboot
12030 * code might disable it.
12031 */
12032 val = tr32(MEMARB_MODE);
12033 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12034
Matt Carlson79eb6902010-02-17 15:17:03 +000012035 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012036 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12037
Gary Zambranoa85feb82007-05-05 11:52:19 -070012038 /* Assume an onboard device and WOL capable by default. */
12039 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080012040
Michael Chanb5d37722006-09-27 16:06:21 -070012041 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012042 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070012043 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012044 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12045 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012046 val = tr32(VCPU_CFGSHDW);
12047 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070012048 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070012049 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080012050 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070012051 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012052 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012053 }
12054
Linus Torvalds1da177e2005-04-16 15:20:36 -070012055 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12056 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12057 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012058 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012059 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012060
12061 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12062 tp->nic_sram_data_cfg = nic_cfg;
12063
12064 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12065 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12066 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12067 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12068 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12069 (ver > 0) && (ver < 0x100))
12070 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12071
Matt Carlsona9daf362008-05-25 23:49:44 -070012072 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12073 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12074
Linus Torvalds1da177e2005-04-16 15:20:36 -070012075 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12076 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12077 eeprom_phy_serdes = 1;
12078
12079 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12080 if (nic_phy_id != 0) {
12081 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12082 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12083
12084 eeprom_phy_id = (id1 >> 16) << 10;
12085 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12086 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12087 } else
12088 eeprom_phy_id = 0;
12089
Michael Chan7d0c41e2005-04-21 17:06:20 -070012090 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012091 if (eeprom_phy_serdes) {
Matt Carlsond1ec96a2010-01-12 10:11:38 +000012092 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
12093 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan747e8f82005-07-25 12:33:22 -070012094 tp->tg3_flags2 |= TG3_FLG2_MII_SERDES;
12095 else
12096 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
12097 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012098
John W. Linvillecbf46852005-04-21 17:01:29 -070012099 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012100 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12101 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012102 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012103 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12104
12105 switch (led_cfg) {
12106 default:
12107 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12108 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12109 break;
12110
12111 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12112 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12113 break;
12114
12115 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12116 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012117
12118 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12119 * read on some older 5700/5701 bootcode.
12120 */
12121 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12122 ASIC_REV_5700 ||
12123 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12124 ASIC_REV_5701)
12125 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12126
Linus Torvalds1da177e2005-04-16 15:20:36 -070012127 break;
12128
12129 case SHASTA_EXT_LED_SHARED:
12130 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12131 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12132 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12133 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12134 LED_CTRL_MODE_PHY_2);
12135 break;
12136
12137 case SHASTA_EXT_LED_MAC:
12138 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12139 break;
12140
12141 case SHASTA_EXT_LED_COMBO:
12142 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12143 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12144 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12145 LED_CTRL_MODE_PHY_2);
12146 break;
12147
Stephen Hemminger855e1112008-04-16 16:37:28 -070012148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012149
12150 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12151 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12152 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12153 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12154
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012155 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12156 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012157
Michael Chan9d26e212006-12-07 00:21:14 -080012158 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012159 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012160 if ((tp->pdev->subsystem_vendor ==
12161 PCI_VENDOR_ID_ARIMA) &&
12162 (tp->pdev->subsystem_device == 0x205a ||
12163 tp->pdev->subsystem_device == 0x2063))
12164 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12165 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012166 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012167 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012169
12170 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12171 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012172 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012173 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12174 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012175
12176 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12177 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012178 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012179
Gary Zambranoa85feb82007-05-05 11:52:19 -070012180 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES &&
12181 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12182 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012183
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012184 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012185 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012186 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12187
Linus Torvalds1da177e2005-04-16 15:20:36 -070012188 if (cfg2 & (1 << 17))
12189 tp->tg3_flags2 |= TG3_FLG2_CAPACITIVE_COUPLING;
12190
12191 /* serdes signal pre-emphasis in register 0x590 set by */
12192 /* bootcode if bit 18 is set */
12193 if (cfg2 & (1 << 18))
12194 tp->tg3_flags2 |= TG3_FLG2_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012195
Matt Carlson321d32a2008-11-21 17:22:19 -080012196 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12197 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012198 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
12199 tp->tg3_flags3 |= TG3_FLG3_PHY_ENABLE_APD;
12200
Matt Carlson8ed5d972007-05-07 00:25:49 -070012201 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
12202 u32 cfg3;
12203
12204 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12205 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12206 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12207 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012208
Matt Carlson14417062010-02-17 15:16:59 +000012209 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
12210 tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
Matt Carlsona9daf362008-05-25 23:49:44 -070012211 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12212 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12213 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12214 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012215 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012216done:
12217 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
12218 device_set_wakeup_enable(&tp->pdev->dev,
12219 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012220}
12221
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012222static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12223{
12224 int i;
12225 u32 val;
12226
12227 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12228 tw32(OTP_CTRL, cmd);
12229
12230 /* Wait for up to 1 ms for command to execute. */
12231 for (i = 0; i < 100; i++) {
12232 val = tr32(OTP_STATUS);
12233 if (val & OTP_STATUS_CMD_DONE)
12234 break;
12235 udelay(10);
12236 }
12237
12238 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12239}
12240
12241/* Read the gphy configuration from the OTP region of the chip. The gphy
12242 * configuration is a 32-bit value that straddles the alignment boundary.
12243 * We do two 32-bit reads and then shift and merge the results.
12244 */
12245static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12246{
12247 u32 bhalf_otp, thalf_otp;
12248
12249 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12250
12251 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12252 return 0;
12253
12254 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12255
12256 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12257 return 0;
12258
12259 thalf_otp = tr32(OTP_READ_DATA);
12260
12261 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12262
12263 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12264 return 0;
12265
12266 bhalf_otp = tr32(OTP_READ_DATA);
12267
12268 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12269}
12270
Michael Chan7d0c41e2005-04-21 17:06:20 -070012271static int __devinit tg3_phy_probe(struct tg3 *tp)
12272{
12273 u32 hw_phy_id_1, hw_phy_id_2;
12274 u32 hw_phy_id, hw_phy_id_masked;
12275 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012276
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012277 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12278 return tg3_phy_init(tp);
12279
Linus Torvalds1da177e2005-04-16 15:20:36 -070012280 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012281 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012282 */
12283 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012284 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12285 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012286 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012287 } else {
12288 /* Now read the physical PHY_ID from the chip and verify
12289 * that it is sane. If it doesn't look good, we fall back
12290 * to either the hard-coded table based PHY_ID and failing
12291 * that the value found in the eeprom area.
12292 */
12293 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12294 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12295
12296 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12297 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12298 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12299
Matt Carlson79eb6902010-02-17 15:17:03 +000012300 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012301 }
12302
Matt Carlson79eb6902010-02-17 15:17:03 +000012303 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012304 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012305 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012306 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012307 else
12308 tp->tg3_flags2 &= ~TG3_FLG2_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012309 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012310 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012311 /* Do nothing, phy ID already set up in
12312 * tg3_get_eeprom_hw_cfg().
12313 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012314 } else {
12315 struct subsys_tbl_ent *p;
12316
12317 /* No eeprom signature? Try the hardcoded
12318 * subsys device table.
12319 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012320 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012321 if (!p)
12322 return -ENODEV;
12323
12324 tp->phy_id = p->phy_id;
12325 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012326 tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012327 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
12328 }
12329 }
12330
Michael Chan747e8f82005-07-25 12:33:22 -070012331 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012332 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012333 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012334 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012335
12336 tg3_readphy(tp, MII_BMSR, &bmsr);
12337 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12338 (bmsr & BMSR_LSTATUS))
12339 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012340
Linus Torvalds1da177e2005-04-16 15:20:36 -070012341 err = tg3_phy_reset(tp);
12342 if (err)
12343 return err;
12344
12345 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12346 ADVERTISE_100HALF | ADVERTISE_100FULL |
12347 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12348 tg3_ctrl = 0;
12349 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
12350 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12351 MII_TG3_CTRL_ADV_1000_FULL);
12352 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12353 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12354 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12355 MII_TG3_CTRL_ENABLE_AS_MASTER);
12356 }
12357
Michael Chan3600d912006-12-07 00:21:48 -080012358 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12359 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12360 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12361 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012362 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12363
12364 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12365 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12366
12367 tg3_writephy(tp, MII_BMCR,
12368 BMCR_ANENABLE | BMCR_ANRESTART);
12369 }
12370 tg3_phy_set_wirespeed(tp);
12371
12372 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12373 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12374 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12375 }
12376
12377skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012378 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012379 err = tg3_init_5401phy_dsp(tp);
12380 if (err)
12381 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012382
Linus Torvalds1da177e2005-04-16 15:20:36 -070012383 err = tg3_init_5401phy_dsp(tp);
12384 }
12385
Michael Chan747e8f82005-07-25 12:33:22 -070012386 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012387 tp->link_config.advertising =
12388 (ADVERTISED_1000baseT_Half |
12389 ADVERTISED_1000baseT_Full |
12390 ADVERTISED_Autoneg |
12391 ADVERTISED_FIBRE);
12392 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
12393 tp->link_config.advertising &=
12394 ~(ADVERTISED_1000baseT_Half |
12395 ADVERTISED_1000baseT_Full);
12396
12397 return err;
12398}
12399
Matt Carlson184b8902010-04-05 10:19:25 +000012400static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012401{
Matt Carlson184b8902010-04-05 10:19:25 +000012402 u8 vpd_data[TG3_NVM_VPD_LEN];
Matt Carlson4181b2c2010-02-26 14:04:45 +000012403 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000012404 int j, i = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012405 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012406
Matt Carlsondf259d82009-04-20 06:57:14 +000012407 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12408 tg3_nvram_read(tp, 0x0, &magic))
David S. Millerf49639e2006-06-09 11:58:36 -070012409 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012410
Michael Chan18201802006-03-20 22:29:15 -080012411 if (magic == TG3_EEPROM_MAGIC) {
Matt Carlson141518c2009-12-03 08:36:22 +000012412 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
Michael Chan1b277772006-03-20 22:27:48 -080012413 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012414
Matt Carlson6d348f22009-02-25 14:25:52 +000012415 /* The data is in little-endian format in NVRAM.
12416 * Use the big-endian read routines to preserve
12417 * the byte order as it exists in NVRAM.
12418 */
Matt Carlson141518c2009-12-03 08:36:22 +000012419 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012420 goto out_not_found;
12421
Matt Carlson6d348f22009-02-25 14:25:52 +000012422 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012423 }
12424 } else {
Matt Carlson94c982b2009-12-03 08:36:23 +000012425 ssize_t cnt;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012426 unsigned int pos = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012427
Matt Carlson94c982b2009-12-03 08:36:23 +000012428 for (; pos < TG3_NVM_VPD_LEN && i < 3; i++, pos += cnt) {
12429 cnt = pci_read_vpd(tp->pdev, pos,
12430 TG3_NVM_VPD_LEN - pos,
12431 &vpd_data[pos]);
12432 if (cnt == -ETIMEDOUT || -EINTR)
12433 cnt = 0;
12434 else if (cnt < 0)
David S. Millerf49639e2006-06-09 11:58:36 -070012435 goto out_not_found;
Michael Chan1b277772006-03-20 22:27:48 -080012436 }
Matt Carlson94c982b2009-12-03 08:36:23 +000012437 if (pos != TG3_NVM_VPD_LEN)
12438 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012439 }
12440
Matt Carlson4181b2c2010-02-26 14:04:45 +000012441 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
12442 PCI_VPD_LRDT_RO_DATA);
12443 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012444 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012445
12446 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
12447 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
12448 i += PCI_VPD_LRDT_TAG_SIZE;
12449
12450 if (block_end > TG3_NVM_VPD_LEN)
12451 goto out_not_found;
12452
Matt Carlson184b8902010-04-05 10:19:25 +000012453 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12454 PCI_VPD_RO_KEYWORD_MFR_ID);
12455 if (j > 0) {
12456 len = pci_vpd_info_field_size(&vpd_data[j]);
12457
12458 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12459 if (j + len > block_end || len != 4 ||
12460 memcmp(&vpd_data[j], "1028", 4))
12461 goto partno;
12462
12463 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12464 PCI_VPD_RO_KEYWORD_VENDOR0);
12465 if (j < 0)
12466 goto partno;
12467
12468 len = pci_vpd_info_field_size(&vpd_data[j]);
12469
12470 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12471 if (j + len > block_end)
12472 goto partno;
12473
12474 memcpy(tp->fw_ver, &vpd_data[j], len);
12475 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
12476 }
12477
12478partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000012479 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12480 PCI_VPD_RO_KEYWORD_PARTNO);
12481 if (i < 0)
12482 goto out_not_found;
12483
12484 len = pci_vpd_info_field_size(&vpd_data[i]);
12485
12486 i += PCI_VPD_INFO_FLD_HDR_SIZE;
12487 if (len > TG3_BPN_SIZE ||
12488 (len + i) > TG3_NVM_VPD_LEN)
12489 goto out_not_found;
12490
12491 memcpy(tp->board_part_number, &vpd_data[i], len);
12492
12493 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012494
12495out_not_found:
Michael Chanb5d37722006-09-27 16:06:21 -070012496 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12497 strcpy(tp->board_part_number, "BCM95906");
Matt Carlsondf259d82009-04-20 06:57:14 +000012498 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12499 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12500 strcpy(tp->board_part_number, "BCM57780");
12501 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12502 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12503 strcpy(tp->board_part_number, "BCM57760");
12504 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12505 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12506 strcpy(tp->board_part_number, "BCM57790");
Matt Carlson5e7ccf22009-08-25 10:08:42 +000012507 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12508 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12509 strcpy(tp->board_part_number, "BCM57788");
Matt Carlsonb474eca2010-02-17 15:16:58 +000012510 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12511 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
12512 strcpy(tp->board_part_number, "BCM57761");
12513 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12514 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
Matt Carlsonb703df62009-12-03 08:36:21 +000012515 strcpy(tp->board_part_number, "BCM57765");
Matt Carlsonb474eca2010-02-17 15:16:58 +000012516 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12517 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
12518 strcpy(tp->board_part_number, "BCM57781");
12519 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12520 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
12521 strcpy(tp->board_part_number, "BCM57785");
12522 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12523 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
12524 strcpy(tp->board_part_number, "BCM57791");
12525 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12526 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12527 strcpy(tp->board_part_number, "BCM57795");
Michael Chanb5d37722006-09-27 16:06:21 -070012528 else
12529 strcpy(tp->board_part_number, "none");
Linus Torvalds1da177e2005-04-16 15:20:36 -070012530}
12531
Matt Carlson9c8a6202007-10-21 16:16:08 -070012532static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12533{
12534 u32 val;
12535
Matt Carlsone4f34112009-02-25 14:25:00 +000012536 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012537 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012538 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012539 val != 0)
12540 return 0;
12541
12542 return 1;
12543}
12544
Matt Carlsonacd9c112009-02-25 14:26:33 +000012545static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12546{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012547 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000012548 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012549 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012550
12551 if (tg3_nvram_read(tp, 0xc, &offset) ||
12552 tg3_nvram_read(tp, 0x4, &start))
12553 return;
12554
12555 offset = tg3_nvram_logical_addr(tp, offset);
12556
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012557 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012558 return;
12559
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012560 if ((val & 0xfc000000) == 0x0c000000) {
12561 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012562 return;
12563
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012564 if (val == 0)
12565 newver = true;
12566 }
12567
Matt Carlson75f99362010-04-05 10:19:24 +000012568 dst_off = strlen(tp->fw_ver);
12569
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012570 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000012571 if (TG3_VER_SIZE - dst_off < 16 ||
12572 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012573 return;
12574
12575 offset = offset + ver_offset - start;
12576 for (i = 0; i < 16; i += 4) {
12577 __be32 v;
12578 if (tg3_nvram_read_be32(tp, offset + i, &v))
12579 return;
12580
Matt Carlson75f99362010-04-05 10:19:24 +000012581 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012582 }
12583 } else {
12584 u32 major, minor;
12585
12586 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
12587 return;
12588
12589 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
12590 TG3_NVM_BCVER_MAJSFT;
12591 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000012592 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
12593 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012594 }
12595}
12596
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012597static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
12598{
12599 u32 val, major, minor;
12600
12601 /* Use native endian representation */
12602 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
12603 return;
12604
12605 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
12606 TG3_NVM_HWSB_CFG1_MAJSFT;
12607 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
12608 TG3_NVM_HWSB_CFG1_MINSFT;
12609
12610 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
12611}
12612
Matt Carlsondfe00d72008-11-21 17:19:41 -080012613static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
12614{
12615 u32 offset, major, minor, build;
12616
Matt Carlson75f99362010-04-05 10:19:24 +000012617 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012618
12619 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
12620 return;
12621
12622 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
12623 case TG3_EEPROM_SB_REVISION_0:
12624 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
12625 break;
12626 case TG3_EEPROM_SB_REVISION_2:
12627 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
12628 break;
12629 case TG3_EEPROM_SB_REVISION_3:
12630 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
12631 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000012632 case TG3_EEPROM_SB_REVISION_4:
12633 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
12634 break;
12635 case TG3_EEPROM_SB_REVISION_5:
12636 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
12637 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012638 default:
12639 return;
12640 }
12641
Matt Carlsone4f34112009-02-25 14:25:00 +000012642 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080012643 return;
12644
12645 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
12646 TG3_EEPROM_SB_EDH_BLD_SHFT;
12647 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
12648 TG3_EEPROM_SB_EDH_MAJ_SHFT;
12649 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
12650
12651 if (minor > 99 || build > 26)
12652 return;
12653
Matt Carlson75f99362010-04-05 10:19:24 +000012654 offset = strlen(tp->fw_ver);
12655 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
12656 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012657
12658 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000012659 offset = strlen(tp->fw_ver);
12660 if (offset < TG3_VER_SIZE - 1)
12661 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012662 }
12663}
12664
Matt Carlsonacd9c112009-02-25 14:26:33 +000012665static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080012666{
12667 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012668 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070012669
12670 for (offset = TG3_NVM_DIR_START;
12671 offset < TG3_NVM_DIR_END;
12672 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012673 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012674 return;
12675
12676 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
12677 break;
12678 }
12679
12680 if (offset == TG3_NVM_DIR_END)
12681 return;
12682
12683 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12684 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000012685 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012686 return;
12687
Matt Carlsone4f34112009-02-25 14:25:00 +000012688 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012689 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012690 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012691 return;
12692
12693 offset += val - start;
12694
Matt Carlsonacd9c112009-02-25 14:26:33 +000012695 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012696
Matt Carlsonacd9c112009-02-25 14:26:33 +000012697 tp->fw_ver[vlen++] = ',';
12698 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070012699
12700 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012701 __be32 v;
12702 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012703 return;
12704
Al Virob9fc7dc2007-12-17 22:59:57 -080012705 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012706
Matt Carlsonacd9c112009-02-25 14:26:33 +000012707 if (vlen > TG3_VER_SIZE - sizeof(v)) {
12708 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012709 break;
12710 }
12711
Matt Carlsonacd9c112009-02-25 14:26:33 +000012712 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
12713 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012714 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000012715}
12716
Matt Carlson7fd76442009-02-25 14:27:20 +000012717static void __devinit tg3_read_dash_ver(struct tg3 *tp)
12718{
12719 int vlen;
12720 u32 apedata;
12721
12722 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
12723 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
12724 return;
12725
12726 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
12727 if (apedata != APE_SEG_SIG_MAGIC)
12728 return;
12729
12730 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
12731 if (!(apedata & APE_FW_STATUS_READY))
12732 return;
12733
12734 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
12735
12736 vlen = strlen(tp->fw_ver);
12737
12738 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " DASH v%d.%d.%d.%d",
12739 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
12740 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
12741 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
12742 (apedata & APE_FW_VERSION_BLDMSK));
12743}
12744
Matt Carlsonacd9c112009-02-25 14:26:33 +000012745static void __devinit tg3_read_fw_ver(struct tg3 *tp)
12746{
12747 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000012748 bool vpd_vers = false;
12749
12750 if (tp->fw_ver[0] != 0)
12751 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012752
Matt Carlsondf259d82009-04-20 06:57:14 +000012753 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
Matt Carlson75f99362010-04-05 10:19:24 +000012754 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000012755 return;
12756 }
12757
Matt Carlsonacd9c112009-02-25 14:26:33 +000012758 if (tg3_nvram_read(tp, 0, &val))
12759 return;
12760
12761 if (val == TG3_EEPROM_MAGIC)
12762 tg3_read_bc_ver(tp);
12763 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
12764 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012765 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12766 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012767 else
12768 return;
12769
12770 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson75f99362010-04-05 10:19:24 +000012771 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) || vpd_vers)
12772 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012773
12774 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012775
Matt Carlson75f99362010-04-05 10:19:24 +000012776done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070012777 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080012778}
12779
Michael Chan7544b092007-05-05 13:08:32 -070012780static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
12781
Linus Torvalds1da177e2005-04-16 15:20:36 -070012782static int __devinit tg3_get_invariants(struct tg3 *tp)
12783{
12784 static struct pci_device_id write_reorder_chipsets[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012785 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012786 PCI_DEVICE_ID_AMD_FE_GATE_700C) },
John W. Linvillec165b002006-07-08 13:28:53 -070012787 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012788 PCI_DEVICE_ID_AMD_8131_BRIDGE) },
Michael Chan399de502005-10-03 14:02:39 -070012789 { PCI_DEVICE(PCI_VENDOR_ID_VIA,
12790 PCI_DEVICE_ID_VIA_8385_0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012791 { },
12792 };
12793 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012794 u32 pci_state_reg, grc_misc_cfg;
12795 u32 val;
12796 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012797 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012798
Linus Torvalds1da177e2005-04-16 15:20:36 -070012799 /* Force memory write invalidate off. If we leave it on,
12800 * then on 5700_BX chips we have to enable a workaround.
12801 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
12802 * to match the cacheline size. The Broadcom driver have this
12803 * workaround but turns MWI off all the times so never uses
12804 * it. This seems to suggest that the workaround is insufficient.
12805 */
12806 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12807 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
12808 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12809
12810 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
12811 * has the register indirect write enable bit set before
12812 * we try to access any of the MMIO registers. It is also
12813 * critical that the PCI-X hw workaround situation is decided
12814 * before that as well.
12815 */
12816 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12817 &misc_ctrl_reg);
12818
12819 tp->pci_chip_rev_id = (misc_ctrl_reg >>
12820 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070012821 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
12822 u32 prod_id_asic_rev;
12823
Matt Carlson5001e2f2009-11-13 13:03:51 +000012824 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
12825 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
12826 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5724)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012827 pci_read_config_dword(tp->pdev,
12828 TG3PCI_GEN2_PRODID_ASICREV,
12829 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000012830 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
12831 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
12832 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
12833 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
12834 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
12835 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12836 pci_read_config_dword(tp->pdev,
12837 TG3PCI_GEN15_PRODID_ASICREV,
12838 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012839 else
12840 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
12841 &prod_id_asic_rev);
12842
Matt Carlson321d32a2008-11-21 17:22:19 -080012843 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070012844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012845
Michael Chanff645be2005-04-21 17:09:53 -070012846 /* Wrong chip ID in 5752 A0. This code can be removed later
12847 * as A0 is not in production.
12848 */
12849 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
12850 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
12851
Michael Chan68929142005-08-09 20:17:14 -070012852 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
12853 * we need to disable memory and use config. cycles
12854 * only to access all registers. The 5702/03 chips
12855 * can mistakenly decode the special cycles from the
12856 * ICH chipsets as memory write cycles, causing corruption
12857 * of register and memory space. Only certain ICH bridges
12858 * will drive special cycles with non-zero data during the
12859 * address phase which can fall within the 5703's address
12860 * range. This is not an ICH bug as the PCI spec allows
12861 * non-zero address during special cycles. However, only
12862 * these ICH bridges are known to drive non-zero addresses
12863 * during special cycles.
12864 *
12865 * Since special cycles do not cross PCI bridges, we only
12866 * enable this workaround if the 5703 is on the secondary
12867 * bus of these ICH bridges.
12868 */
12869 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
12870 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
12871 static struct tg3_dev_id {
12872 u32 vendor;
12873 u32 device;
12874 u32 rev;
12875 } ich_chipsets[] = {
12876 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
12877 PCI_ANY_ID },
12878 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
12879 PCI_ANY_ID },
12880 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
12881 0xa },
12882 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
12883 PCI_ANY_ID },
12884 { },
12885 };
12886 struct tg3_dev_id *pci_id = &ich_chipsets[0];
12887 struct pci_dev *bridge = NULL;
12888
12889 while (pci_id->vendor != 0) {
12890 bridge = pci_get_device(pci_id->vendor, pci_id->device,
12891 bridge);
12892 if (!bridge) {
12893 pci_id++;
12894 continue;
12895 }
12896 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070012897 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070012898 continue;
12899 }
12900 if (bridge->subordinate &&
12901 (bridge->subordinate->number ==
12902 tp->pdev->bus->number)) {
12903
12904 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
12905 pci_dev_put(bridge);
12906 break;
12907 }
12908 }
12909 }
12910
Matt Carlson41588ba2008-04-19 18:12:33 -070012911 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
12912 static struct tg3_dev_id {
12913 u32 vendor;
12914 u32 device;
12915 } bridge_chipsets[] = {
12916 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
12917 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
12918 { },
12919 };
12920 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
12921 struct pci_dev *bridge = NULL;
12922
12923 while (pci_id->vendor != 0) {
12924 bridge = pci_get_device(pci_id->vendor,
12925 pci_id->device,
12926 bridge);
12927 if (!bridge) {
12928 pci_id++;
12929 continue;
12930 }
12931 if (bridge->subordinate &&
12932 (bridge->subordinate->number <=
12933 tp->pdev->bus->number) &&
12934 (bridge->subordinate->subordinate >=
12935 tp->pdev->bus->number)) {
12936 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
12937 pci_dev_put(bridge);
12938 break;
12939 }
12940 }
12941 }
12942
Michael Chan4a29cc22006-03-19 13:21:12 -080012943 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
12944 * DMA addresses > 40-bit. This bridge may have other additional
12945 * 57xx devices behind it in some 4-port NIC designs for example.
12946 * Any tg3 device found behind the bridge will also need the 40-bit
12947 * DMA workaround.
12948 */
Michael Chana4e2b342005-10-26 15:46:52 -070012949 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
12950 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
12951 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080012952 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070012953 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000012954 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080012955 struct pci_dev *bridge = NULL;
12956
12957 do {
12958 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
12959 PCI_DEVICE_ID_SERVERWORKS_EPB,
12960 bridge);
12961 if (bridge && bridge->subordinate &&
12962 (bridge->subordinate->number <=
12963 tp->pdev->bus->number) &&
12964 (bridge->subordinate->subordinate >=
12965 tp->pdev->bus->number)) {
12966 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
12967 pci_dev_put(bridge);
12968 break;
12969 }
12970 } while (bridge);
12971 }
Michael Chan4cf78e42005-07-25 12:29:19 -070012972
Linus Torvalds1da177e2005-04-16 15:20:36 -070012973 /* Initialize misc host control in PCI block. */
12974 tp->misc_host_ctrl |= (misc_ctrl_reg &
12975 MISC_HOST_CTRL_CHIPREV);
12976 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12977 tp->misc_host_ctrl);
12978
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012979 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
12980 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
12981 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan7544b092007-05-05 13:08:32 -070012982 tp->pdev_peer = tg3_find_peer(tp);
12983
Matt Carlson321d32a2008-11-21 17:22:19 -080012984 /* Intentionally exclude ASIC_REV_5906 */
12985 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080012986 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070012987 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070012988 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012989 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012990 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonb703df62009-12-03 08:36:21 +000012991 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12992 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012993 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
12994
12995 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
12996 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070012997 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012998 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070012999 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070013000 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
13001
John W. Linville1b440c562005-04-21 17:03:18 -070013002 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
13003 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
13004 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
13005
Matt Carlson027455a2008-12-21 20:19:30 -080013006 /* 5700 B0 chips do not support checksumming correctly due
13007 * to hardware bugs.
13008 */
13009 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
13010 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
13011 else {
13012 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
13013 tp->dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
13014 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
13015 tp->dev->features |= NETIF_F_IPV6_CSUM;
David S. Millercb903bf2010-04-20 18:49:45 -070013016 tp->dev->features |= NETIF_F_GRO;
Matt Carlson027455a2008-12-21 20:19:30 -080013017 }
13018
Matt Carlson507399f2009-11-13 13:03:37 +000013019 /* Determine TSO capabilities */
Matt Carlsonb703df62009-12-03 08:36:21 +000013020 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13021 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsone849cdc2009-11-13 13:03:38 +000013022 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13023 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13024 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000013025 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13026 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13027 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13028 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13029 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13030 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13031 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13032 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13033 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13034 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13035 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13036 tp->fw_needed = FIRMWARE_TG3TSO5;
13037 else
13038 tp->fw_needed = FIRMWARE_TG3TSO;
13039 }
13040
13041 tp->irq_max = 1;
13042
Michael Chan5a6f3072006-03-20 22:28:05 -080013043 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070013044 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13045 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13046 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13047 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13048 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13049 tp->pdev_peer == tp->pdev))
13050 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13051
Matt Carlson321d32a2008-11-21 17:22:19 -080013052 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013053 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080013054 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070013055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013056
Matt Carlsonb703df62009-12-03 08:36:21 +000013057 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13058 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlson507399f2009-11-13 13:03:37 +000013059 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13060 tp->irq_max = TG3_IRQ_MAX_VECS;
13061 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013062 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013063
Matt Carlson615774f2009-11-13 13:03:39 +000013064 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13065 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13066 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13067 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13068 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13069 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000013070 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013071
Matt Carlsonb703df62009-12-03 08:36:21 +000013072 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13073 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
13074 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13075
Matt Carlsonf51f3562008-05-25 23:45:08 -070013076 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013077 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
13078 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
Matt Carlson8f666b02009-08-28 13:58:24 +000013079 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070013080
Matt Carlson52f44902008-11-21 17:17:04 -080013081 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13082 &pci_state_reg);
13083
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013084 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13085 if (tp->pcie_cap != 0) {
13086 u16 lnkctl;
13087
Linus Torvalds1da177e2005-04-16 15:20:36 -070013088 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013089
13090 pcie_set_readrq(tp->pdev, 4096);
13091
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013092 pci_read_config_word(tp->pdev,
13093 tp->pcie_cap + PCI_EXP_LNKCTL,
13094 &lnkctl);
13095 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13096 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080013097 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013098 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013099 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013100 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13101 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013102 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Matt Carlson614b0592010-01-20 16:58:02 +000013103 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13104 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
Michael Chanc7835a72006-11-15 21:14:42 -080013105 }
Matt Carlson52f44902008-11-21 17:17:04 -080013106 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080013107 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080013108 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13109 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13110 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13111 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013112 dev_err(&tp->pdev->dev,
13113 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013114 return -EIO;
13115 }
13116
13117 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13118 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013120
Michael Chan399de502005-10-03 14:02:39 -070013121 /* If we have an AMD 762 or VIA K8T800 chipset, write
13122 * reordering to the mailbox registers done by the host
13123 * controller can cause major troubles. We read back from
13124 * every mailbox register write to force the writes to be
13125 * posted to the chip in order.
13126 */
13127 if (pci_dev_present(write_reorder_chipsets) &&
13128 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13129 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13130
Matt Carlson69fc4052008-12-21 20:19:57 -080013131 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13132 &tp->pci_cacheline_sz);
13133 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13134 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013135 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13136 tp->pci_lat_timer < 64) {
13137 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013138 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13139 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013140 }
13141
Matt Carlson52f44902008-11-21 17:17:04 -080013142 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13143 /* 5700 BX chips need to have their TX producer index
13144 * mailboxes written twice to workaround a bug.
13145 */
13146 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013147
Matt Carlson52f44902008-11-21 17:17:04 -080013148 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013149 *
13150 * The workaround is to use indirect register accesses
13151 * for all chip writes not to mailbox registers.
13152 */
Matt Carlson52f44902008-11-21 17:17:04 -080013153 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013154 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013155
13156 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13157
13158 /* The chip can have it's power management PCI config
13159 * space registers clobbered due to this bug.
13160 * So explicitly force the chip into D0 here.
13161 */
Matt Carlson9974a352007-10-07 23:27:28 -070013162 pci_read_config_dword(tp->pdev,
13163 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013164 &pm_reg);
13165 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13166 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013167 pci_write_config_dword(tp->pdev,
13168 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013169 pm_reg);
13170
13171 /* Also, force SERR#/PERR# in PCI command. */
13172 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13173 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13174 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13175 }
13176 }
13177
Linus Torvalds1da177e2005-04-16 15:20:36 -070013178 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13179 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13180 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13181 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13182
13183 /* Chip-specific fixup from Broadcom driver */
13184 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13185 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13186 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13187 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13188 }
13189
Michael Chan1ee582d2005-08-09 20:16:46 -070013190 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013191 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013192 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013193 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013194 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013195 tp->write32_tx_mbox = tg3_write32;
13196 tp->write32_rx_mbox = tg3_write32;
13197
13198 /* Various workaround register access methods */
13199 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13200 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013201 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13202 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13203 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13204 /*
13205 * Back to back register writes can cause problems on these
13206 * chips, the workaround is to read back all reg writes
13207 * except those to mailbox regs.
13208 *
13209 * See tg3_write_indirect_reg32().
13210 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013211 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013212 }
13213
Michael Chan1ee582d2005-08-09 20:16:46 -070013214 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13215 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13216 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13217 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13218 tp->write32_rx_mbox = tg3_write_flush_reg32;
13219 }
Michael Chan20094932005-08-09 20:16:32 -070013220
Michael Chan68929142005-08-09 20:17:14 -070013221 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13222 tp->read32 = tg3_read_indirect_reg32;
13223 tp->write32 = tg3_write_indirect_reg32;
13224 tp->read32_mbox = tg3_read_indirect_mbox;
13225 tp->write32_mbox = tg3_write_indirect_mbox;
13226 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13227 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13228
13229 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013230 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013231
13232 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13233 pci_cmd &= ~PCI_COMMAND_MEMORY;
13234 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13235 }
Michael Chanb5d37722006-09-27 16:06:21 -070013236 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13237 tp->read32_mbox = tg3_read32_mbox_5906;
13238 tp->write32_mbox = tg3_write32_mbox_5906;
13239 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13240 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13241 }
Michael Chan68929142005-08-09 20:17:14 -070013242
Michael Chanbbadf502006-04-06 21:46:34 -070013243 if (tp->write32 == tg3_write_indirect_reg32 ||
13244 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13245 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013246 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013247 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13248
Michael Chan7d0c41e2005-04-21 17:06:20 -070013249 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013250 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013251 * determined before calling tg3_set_power_state() so that
13252 * we know whether or not to switch out of Vaux power.
13253 * When the flag is set, it means that GPIO1 is used for eeprom
13254 * write protect and also implies that it is a LOM where GPIOs
13255 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013256 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013257 tg3_get_eeprom_hw_cfg(tp);
13258
Matt Carlson0d3031d2007-10-10 18:02:43 -070013259 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13260 /* Allow reads and writes to the
13261 * APE register and memory space.
13262 */
13263 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000013264 PCISTATE_ALLOW_APE_SHMEM_WR |
13265 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013266 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13267 pci_state_reg);
13268 }
13269
Matt Carlson9936bcf2007-10-10 18:03:07 -070013270 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013271 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013272 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013273 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonb703df62009-12-03 08:36:21 +000013274 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13275 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsond30cdd22007-10-07 23:28:35 -070013276 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13277
Michael Chan314fba32005-04-21 17:07:04 -070013278 /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
13279 * GPIO1 driven high will bring 5700's external PHY out of reset.
13280 * It is also used as eeprom write protect on LOMs.
13281 */
13282 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13283 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13284 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13285 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13286 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013287 /* Unused GPIO3 must be driven as output on 5752 because there
13288 * are no pull-up resistors on unused GPIO pins.
13289 */
13290 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13291 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013292
Matt Carlson321d32a2008-11-21 17:22:19 -080013293 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013294 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13295 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013296 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13297
Matt Carlson8d519ab2009-04-20 06:58:01 +000013298 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13299 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013300 /* Turn off the debug UART. */
13301 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13302 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13303 /* Keep VMain power. */
13304 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13305 GRC_LCLCTRL_GPIO_OUTPUT0;
13306 }
13307
Linus Torvalds1da177e2005-04-16 15:20:36 -070013308 /* Force the chip into D0. */
Michael Chanbc1c7562006-03-20 17:48:03 -080013309 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013310 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013311 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013312 return err;
13313 }
13314
Linus Torvalds1da177e2005-04-16 15:20:36 -070013315 /* Derive initial jumbo mode from MTU assigned in
13316 * ether_setup() via the alloc_etherdev() call
13317 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013318 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013319 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013320 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013321
13322 /* Determine WakeOnLan speed to use. */
13323 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13324 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13325 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13326 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13327 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13328 } else {
13329 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13330 }
13331
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013332 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13333 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
13334
Linus Torvalds1da177e2005-04-16 15:20:36 -070013335 /* A few boards don't want Ethernet@WireSpeed phy feature */
13336 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13337 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13338 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013339 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013340 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) ||
Michael Chan747e8f82005-07-25 12:33:22 -070013341 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013342 tp->tg3_flags2 |= TG3_FLG2_NO_ETH_WIRE_SPEED;
13343
13344 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13345 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
13346 tp->tg3_flags2 |= TG3_FLG2_PHY_ADC_BUG;
13347 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
13348 tp->tg3_flags2 |= TG3_FLG2_PHY_5704_A0_BUG;
13349
Matt Carlson321d32a2008-11-21 17:22:19 -080013350 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013351 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013352 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013353 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Matt Carlsonb703df62009-12-03 08:36:21 +000013354 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
13355 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765) {
Michael Chanc424cb22006-04-29 18:56:34 -070013356 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013357 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013358 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13359 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013360 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13361 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
13362 tp->tg3_flags2 |= TG3_FLG2_PHY_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013363 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
13364 tp->tg3_flags2 |= TG3_FLG2_PHY_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013365 } else
Michael Chanc424cb22006-04-29 18:56:34 -070013366 tp->tg3_flags2 |= TG3_FLG2_PHY_BER_BUG;
13367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013368
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013369 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13370 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13371 tp->phy_otp = tg3_read_otp_phycfg(tp);
13372 if (tp->phy_otp == 0)
13373 tp->phy_otp = TG3_OTP_DEFAULT;
13374 }
13375
Matt Carlsonf51f3562008-05-25 23:45:08 -070013376 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070013377 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13378 else
13379 tp->mi_mode = MAC_MI_MODE_BASE;
13380
Linus Torvalds1da177e2005-04-16 15:20:36 -070013381 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013382 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13383 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13384 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13385
Matt Carlson321d32a2008-11-21 17:22:19 -080013386 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13387 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070013388 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
13389
Matt Carlson158d7ab2008-05-29 01:37:54 -070013390 err = tg3_mdio_init(tp);
13391 if (err)
13392 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013393
Matt Carlson55dffe72010-01-12 10:11:39 +000013394 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
13395 (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0 ||
13396 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
13397 return -ENOTSUPP;
13398
Linus Torvalds1da177e2005-04-16 15:20:36 -070013399 /* Initialize data/descriptor byte/word swapping. */
13400 val = tr32(GRC_MODE);
13401 val &= GRC_MODE_HOST_STACKUP;
13402 tw32(GRC_MODE, val | tp->grc_mode);
13403
13404 tg3_switch_clocks(tp);
13405
13406 /* Clear this out for sanity. */
13407 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
13408
13409 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13410 &pci_state_reg);
13411 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
13412 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
13413 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
13414
13415 if (chiprevid == CHIPREV_ID_5701_A0 ||
13416 chiprevid == CHIPREV_ID_5701_B0 ||
13417 chiprevid == CHIPREV_ID_5701_B2 ||
13418 chiprevid == CHIPREV_ID_5701_B5) {
13419 void __iomem *sram_base;
13420
13421 /* Write some dummy words into the SRAM status block
13422 * area, see if it reads back correctly. If the return
13423 * value is bad, force enable the PCIX workaround.
13424 */
13425 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13426
13427 writel(0x00000000, sram_base);
13428 writel(0x00000000, sram_base + 4);
13429 writel(0xffffffff, sram_base + 4);
13430 if (readl(sram_base) != 0x00000000)
13431 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13432 }
13433 }
13434
13435 udelay(50);
13436 tg3_nvram_init(tp);
13437
13438 grc_misc_cfg = tr32(GRC_MISC_CFG);
13439 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13440
Linus Torvalds1da177e2005-04-16 15:20:36 -070013441 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13442 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13443 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13444 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13445
David S. Millerfac9b832005-05-18 22:46:34 -070013446 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13447 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13448 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13449 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13450 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13451 HOSTCC_MODE_CLRTICK_TXBD);
13452
13453 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13454 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13455 tp->misc_host_ctrl);
13456 }
13457
Matt Carlson3bda1252008-08-15 14:08:22 -070013458 /* Preserve the APE MAC_MODE bits */
13459 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
13460 tp->mac_mode = tr32(MAC_MODE) |
13461 MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
13462 else
13463 tp->mac_mode = TG3_DEF_MAC_MODE;
13464
Linus Torvalds1da177e2005-04-16 15:20:36 -070013465 /* these are limited to 10/100 only */
13466 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13467 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13468 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13469 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13470 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13471 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13472 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13473 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13474 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013475 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13476 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013477 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000013478 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13479 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013480 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013481 tp->tg3_flags |= TG3_FLAG_10_100_ONLY;
13482
13483 err = tg3_phy_probe(tp);
13484 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013485 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013486 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013487 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013488 }
13489
Matt Carlson184b8902010-04-05 10:19:25 +000013490 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013491 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013492
13493 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
13494 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13495 } else {
13496 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13497 tp->tg3_flags |= TG3_FLAG_USE_MI_INTERRUPT;
13498 else
13499 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13500 }
13501
13502 /* 5700 {AX,BX} chips have a broken status block link
13503 * change bit implementation, so we must use the
13504 * status register in those cases.
13505 */
13506 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13507 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13508 else
13509 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13510
13511 /* The led_ctrl is set during tg3_phy_probe, here we might
13512 * have to force the link status polling mechanism based
13513 * upon subsystem IDs.
13514 */
13515 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070013516 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013517 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
13518 tp->tg3_flags |= (TG3_FLAG_USE_MI_INTERRUPT |
13519 TG3_FLAG_USE_LINKCHG_REG);
13520 }
13521
13522 /* For all SERDES we poll the MAC status register. */
13523 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
13524 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
13525 else
13526 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
13527
Matt Carlson9dc7a112010-04-12 06:58:28 +000013528 tp->rx_offset = NET_IP_ALIGN + TG3_RX_HEADROOM;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013529 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013530 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsond2757fc2010-04-12 06:58:27 +000013531 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
Matt Carlson9dc7a112010-04-12 06:58:28 +000013532 tp->rx_offset -= NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013533#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000013534 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013535#endif
13536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013537
Michael Chanf92905d2006-06-29 20:14:29 -070013538 tp->rx_std_max_post = TG3_RX_RING_SIZE;
13539
13540 /* Increment the rx prod index on the rx std ring by at most
13541 * 8 for these chips to workaround hw errata.
13542 */
13543 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13544 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13545 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13546 tp->rx_std_max_post = 8;
13547
Matt Carlson8ed5d972007-05-07 00:25:49 -070013548 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
13549 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
13550 PCIE_PWR_MGMT_L1_THRESH_MSK;
13551
Linus Torvalds1da177e2005-04-16 15:20:36 -070013552 return err;
13553}
13554
David S. Miller49b6e95f2007-03-29 01:38:42 -070013555#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013556static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
13557{
13558 struct net_device *dev = tp->dev;
13559 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013560 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070013561 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013562 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013563
David S. Miller49b6e95f2007-03-29 01:38:42 -070013564 addr = of_get_property(dp, "local-mac-address", &len);
13565 if (addr && len == 6) {
13566 memcpy(dev->dev_addr, addr, 6);
13567 memcpy(dev->perm_addr, dev->dev_addr, 6);
13568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013569 }
13570 return -ENODEV;
13571}
13572
13573static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
13574{
13575 struct net_device *dev = tp->dev;
13576
13577 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070013578 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013579 return 0;
13580}
13581#endif
13582
13583static int __devinit tg3_get_device_address(struct tg3 *tp)
13584{
13585 struct net_device *dev = tp->dev;
13586 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080013587 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013588
David S. Miller49b6e95f2007-03-29 01:38:42 -070013589#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013590 if (!tg3_get_macaddr_sparc(tp))
13591 return 0;
13592#endif
13593
13594 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070013595 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013596 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013597 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
13598 mac_offset = 0xcc;
13599 if (tg3_nvram_lock(tp))
13600 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
13601 else
13602 tg3_nvram_unlock(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013603 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13604 if (tr32(TG3_CPMU_STATUS) & TG3_CPMU_STATUS_PCIE_FUNC)
13605 mac_offset = 0xcc;
13606 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070013607 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013608
13609 /* First try to get it from MAC address mailbox. */
13610 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
13611 if ((hi >> 16) == 0x484b) {
13612 dev->dev_addr[0] = (hi >> 8) & 0xff;
13613 dev->dev_addr[1] = (hi >> 0) & 0xff;
13614
13615 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
13616 dev->dev_addr[2] = (lo >> 24) & 0xff;
13617 dev->dev_addr[3] = (lo >> 16) & 0xff;
13618 dev->dev_addr[4] = (lo >> 8) & 0xff;
13619 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013620
Michael Chan008652b2006-03-27 23:14:53 -080013621 /* Some old bootcode may report a 0 MAC address in SRAM */
13622 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
13623 }
13624 if (!addr_ok) {
13625 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000013626 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
13627 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000013628 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070013629 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
13630 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080013631 }
13632 /* Finally just fetch it out of the MAC control regs. */
13633 else {
13634 hi = tr32(MAC_ADDR_0_HIGH);
13635 lo = tr32(MAC_ADDR_0_LOW);
13636
13637 dev->dev_addr[5] = lo & 0xff;
13638 dev->dev_addr[4] = (lo >> 8) & 0xff;
13639 dev->dev_addr[3] = (lo >> 16) & 0xff;
13640 dev->dev_addr[2] = (lo >> 24) & 0xff;
13641 dev->dev_addr[1] = hi & 0xff;
13642 dev->dev_addr[0] = (hi >> 8) & 0xff;
13643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013644 }
13645
13646 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070013647#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013648 if (!tg3_get_default_macaddr_sparc(tp))
13649 return 0;
13650#endif
13651 return -EINVAL;
13652 }
John W. Linville2ff43692005-09-12 14:44:20 -070013653 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013654 return 0;
13655}
13656
David S. Miller59e6b432005-05-18 22:50:10 -070013657#define BOUNDARY_SINGLE_CACHELINE 1
13658#define BOUNDARY_MULTI_CACHELINE 2
13659
13660static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
13661{
13662 int cacheline_size;
13663 u8 byte;
13664 int goal;
13665
13666 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
13667 if (byte == 0)
13668 cacheline_size = 1024;
13669 else
13670 cacheline_size = (int) byte * 4;
13671
13672 /* On 5703 and later chips, the boundary bits have no
13673 * effect.
13674 */
13675 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13676 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13677 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13678 goto out;
13679
13680#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
13681 goal = BOUNDARY_MULTI_CACHELINE;
13682#else
13683#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
13684 goal = BOUNDARY_SINGLE_CACHELINE;
13685#else
13686 goal = 0;
13687#endif
13688#endif
13689
Matt Carlsonb703df62009-12-03 08:36:21 +000013690 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13691 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013692 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
13693 goto out;
13694 }
13695
David S. Miller59e6b432005-05-18 22:50:10 -070013696 if (!goal)
13697 goto out;
13698
13699 /* PCI controllers on most RISC systems tend to disconnect
13700 * when a device tries to burst across a cache-line boundary.
13701 * Therefore, letting tg3 do so just wastes PCI bandwidth.
13702 *
13703 * Unfortunately, for PCI-E there are only limited
13704 * write-side controls for this, and thus for reads
13705 * we will still get the disconnects. We'll also waste
13706 * these PCI cycles for both read and write for chips
13707 * other than 5700 and 5701 which do not implement the
13708 * boundary bits.
13709 */
13710 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13711 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
13712 switch (cacheline_size) {
13713 case 16:
13714 case 32:
13715 case 64:
13716 case 128:
13717 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13718 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
13719 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
13720 } else {
13721 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13722 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13723 }
13724 break;
13725
13726 case 256:
13727 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
13728 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
13729 break;
13730
13731 default:
13732 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13733 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13734 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013735 }
David S. Miller59e6b432005-05-18 22:50:10 -070013736 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13737 switch (cacheline_size) {
13738 case 16:
13739 case 32:
13740 case 64:
13741 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13742 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13743 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
13744 break;
13745 }
13746 /* fallthrough */
13747 case 128:
13748 default:
13749 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13750 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
13751 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013752 }
David S. Miller59e6b432005-05-18 22:50:10 -070013753 } else {
13754 switch (cacheline_size) {
13755 case 16:
13756 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13757 val |= (DMA_RWCTRL_READ_BNDRY_16 |
13758 DMA_RWCTRL_WRITE_BNDRY_16);
13759 break;
13760 }
13761 /* fallthrough */
13762 case 32:
13763 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13764 val |= (DMA_RWCTRL_READ_BNDRY_32 |
13765 DMA_RWCTRL_WRITE_BNDRY_32);
13766 break;
13767 }
13768 /* fallthrough */
13769 case 64:
13770 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13771 val |= (DMA_RWCTRL_READ_BNDRY_64 |
13772 DMA_RWCTRL_WRITE_BNDRY_64);
13773 break;
13774 }
13775 /* fallthrough */
13776 case 128:
13777 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13778 val |= (DMA_RWCTRL_READ_BNDRY_128 |
13779 DMA_RWCTRL_WRITE_BNDRY_128);
13780 break;
13781 }
13782 /* fallthrough */
13783 case 256:
13784 val |= (DMA_RWCTRL_READ_BNDRY_256 |
13785 DMA_RWCTRL_WRITE_BNDRY_256);
13786 break;
13787 case 512:
13788 val |= (DMA_RWCTRL_READ_BNDRY_512 |
13789 DMA_RWCTRL_WRITE_BNDRY_512);
13790 break;
13791 case 1024:
13792 default:
13793 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
13794 DMA_RWCTRL_WRITE_BNDRY_1024);
13795 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013796 }
David S. Miller59e6b432005-05-18 22:50:10 -070013797 }
13798
13799out:
13800 return val;
13801}
13802
Linus Torvalds1da177e2005-04-16 15:20:36 -070013803static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
13804{
13805 struct tg3_internal_buffer_desc test_desc;
13806 u32 sram_dma_descs;
13807 int i, ret;
13808
13809 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
13810
13811 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
13812 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
13813 tw32(RDMAC_STATUS, 0);
13814 tw32(WDMAC_STATUS, 0);
13815
13816 tw32(BUFMGR_MODE, 0);
13817 tw32(FTQ_RESET, 0);
13818
13819 test_desc.addr_hi = ((u64) buf_dma) >> 32;
13820 test_desc.addr_lo = buf_dma & 0xffffffff;
13821 test_desc.nic_mbuf = 0x00002100;
13822 test_desc.len = size;
13823
13824 /*
13825 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
13826 * the *second* time the tg3 driver was getting loaded after an
13827 * initial scan.
13828 *
13829 * Broadcom tells me:
13830 * ...the DMA engine is connected to the GRC block and a DMA
13831 * reset may affect the GRC block in some unpredictable way...
13832 * The behavior of resets to individual blocks has not been tested.
13833 *
13834 * Broadcom noted the GRC reset will also reset all sub-components.
13835 */
13836 if (to_device) {
13837 test_desc.cqid_sqid = (13 << 8) | 2;
13838
13839 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
13840 udelay(40);
13841 } else {
13842 test_desc.cqid_sqid = (16 << 8) | 7;
13843
13844 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
13845 udelay(40);
13846 }
13847 test_desc.flags = 0x00000005;
13848
13849 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
13850 u32 val;
13851
13852 val = *(((u32 *)&test_desc) + i);
13853 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
13854 sram_dma_descs + (i * sizeof(u32)));
13855 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
13856 }
13857 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
13858
Matt Carlson859a588792010-04-05 10:19:28 +000013859 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013860 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000013861 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013862 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013863
13864 ret = -ENODEV;
13865 for (i = 0; i < 40; i++) {
13866 u32 val;
13867
13868 if (to_device)
13869 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
13870 else
13871 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
13872 if ((val & 0xffff) == sram_dma_descs) {
13873 ret = 0;
13874 break;
13875 }
13876
13877 udelay(100);
13878 }
13879
13880 return ret;
13881}
13882
David S. Millerded73402005-05-23 13:59:47 -070013883#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070013884
13885static int __devinit tg3_test_dma(struct tg3 *tp)
13886{
13887 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070013888 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013889 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013890
13891 buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
13892 if (!buf) {
13893 ret = -ENOMEM;
13894 goto out_nofree;
13895 }
13896
13897 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
13898 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
13899
David S. Miller59e6b432005-05-18 22:50:10 -070013900 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013901
Matt Carlsonb703df62009-12-03 08:36:21 +000013902 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13903 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013904 goto out;
13905
Linus Torvalds1da177e2005-04-16 15:20:36 -070013906 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13907 /* DMA read watermark not used on PCIE */
13908 tp->dma_rwctrl |= 0x00180000;
13909 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070013910 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
13911 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013912 tp->dma_rwctrl |= 0x003f0000;
13913 else
13914 tp->dma_rwctrl |= 0x003f000f;
13915 } else {
13916 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13917 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
13918 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080013919 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013920
Michael Chan4a29cc22006-03-19 13:21:12 -080013921 /* If the 5704 is behind the EPB bridge, we can
13922 * do the less restrictive ONE_DMA workaround for
13923 * better performance.
13924 */
13925 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
13926 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13927 tp->dma_rwctrl |= 0x8000;
13928 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013929 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
13930
Michael Chan49afdeb2007-02-13 12:17:03 -080013931 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
13932 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070013933 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080013934 tp->dma_rwctrl |=
13935 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
13936 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
13937 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070013938 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
13939 /* 5780 always in PCIX mode */
13940 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070013941 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13942 /* 5714 always in PCIX mode */
13943 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013944 } else {
13945 tp->dma_rwctrl |= 0x001b000f;
13946 }
13947 }
13948
13949 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13950 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13951 tp->dma_rwctrl &= 0xfffffff0;
13952
13953 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13954 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
13955 /* Remove this if it causes problems for some boards. */
13956 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
13957
13958 /* On 5700/5701 chips, we need to set this bit.
13959 * Otherwise the chip will issue cacheline transactions
13960 * to streamable DMA memory with not all the byte
13961 * enables turned on. This is an error on several
13962 * RISC PCI controllers, in particular sparc64.
13963 *
13964 * On 5703/5704 chips, this bit has been reassigned
13965 * a different meaning. In particular, it is used
13966 * on those chips to enable a PCI-X workaround.
13967 */
13968 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
13969 }
13970
13971 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13972
13973#if 0
13974 /* Unneeded, already done by tg3_get_invariants. */
13975 tg3_switch_clocks(tp);
13976#endif
13977
Linus Torvalds1da177e2005-04-16 15:20:36 -070013978 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13979 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
13980 goto out;
13981
David S. Miller59e6b432005-05-18 22:50:10 -070013982 /* It is best to perform DMA test with maximum write burst size
13983 * to expose the 5700/5701 write DMA bug.
13984 */
13985 saved_dma_rwctrl = tp->dma_rwctrl;
13986 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
13987 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13988
Linus Torvalds1da177e2005-04-16 15:20:36 -070013989 while (1) {
13990 u32 *p = buf, i;
13991
13992 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
13993 p[i] = i;
13994
13995 /* Send the buffer to the chip. */
13996 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
13997 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000013998 dev_err(&tp->pdev->dev,
13999 "%s: Buffer write failed. err = %d\n",
14000 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014001 break;
14002 }
14003
14004#if 0
14005 /* validate data reached card RAM correctly. */
14006 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14007 u32 val;
14008 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14009 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014010 dev_err(&tp->pdev->dev,
14011 "%s: Buffer corrupted on device! "
14012 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014013 /* ret = -ENODEV here? */
14014 }
14015 p[i] = 0;
14016 }
14017#endif
14018 /* Now read it back. */
14019 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14020 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014021 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14022 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014023 break;
14024 }
14025
14026 /* Verify it. */
14027 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14028 if (p[i] == i)
14029 continue;
14030
David S. Miller59e6b432005-05-18 22:50:10 -070014031 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14032 DMA_RWCTRL_WRITE_BNDRY_16) {
14033 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014034 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14035 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14036 break;
14037 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014038 dev_err(&tp->pdev->dev,
14039 "%s: Buffer corrupted on read back! "
14040 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014041 ret = -ENODEV;
14042 goto out;
14043 }
14044 }
14045
14046 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14047 /* Success. */
14048 ret = 0;
14049 break;
14050 }
14051 }
David S. Miller59e6b432005-05-18 22:50:10 -070014052 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14053 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014054 static struct pci_device_id dma_wait_state_chipsets[] = {
14055 { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
14056 PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14057 { },
14058 };
14059
David S. Miller59e6b432005-05-18 22:50:10 -070014060 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014061 * now look for chipsets that are known to expose the
14062 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014063 */
Michael Chan6d1cfba2005-06-08 14:13:14 -070014064 if (pci_dev_present(dma_wait_state_chipsets)) {
14065 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14066 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000014067 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014068 /* Safe to use the calculated DMA boundary. */
14069 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000014070 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014071
David S. Miller59e6b432005-05-18 22:50:10 -070014072 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014074
14075out:
14076 pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
14077out_nofree:
14078 return ret;
14079}
14080
14081static void __devinit tg3_init_link_config(struct tg3 *tp)
14082{
14083 tp->link_config.advertising =
14084 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
14085 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
14086 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
14087 ADVERTISED_Autoneg | ADVERTISED_MII);
14088 tp->link_config.speed = SPEED_INVALID;
14089 tp->link_config.duplex = DUPLEX_INVALID;
14090 tp->link_config.autoneg = AUTONEG_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014091 tp->link_config.active_speed = SPEED_INVALID;
14092 tp->link_config.active_duplex = DUPLEX_INVALID;
14093 tp->link_config.phy_is_low_power = 0;
14094 tp->link_config.orig_speed = SPEED_INVALID;
14095 tp->link_config.orig_duplex = DUPLEX_INVALID;
14096 tp->link_config.orig_autoneg = AUTONEG_INVALID;
14097}
14098
14099static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14100{
Matt Carlson666bc832010-01-20 16:58:03 +000014101 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14102 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
14103 tp->bufmgr_config.mbuf_read_dma_low_water =
14104 DEFAULT_MB_RDMA_LOW_WATER_5705;
14105 tp->bufmgr_config.mbuf_mac_rx_low_water =
14106 DEFAULT_MB_MACRX_LOW_WATER_57765;
14107 tp->bufmgr_config.mbuf_high_water =
14108 DEFAULT_MB_HIGH_WATER_57765;
14109
14110 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14111 DEFAULT_MB_RDMA_LOW_WATER_5705;
14112 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14113 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14114 tp->bufmgr_config.mbuf_high_water_jumbo =
14115 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14116 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014117 tp->bufmgr_config.mbuf_read_dma_low_water =
14118 DEFAULT_MB_RDMA_LOW_WATER_5705;
14119 tp->bufmgr_config.mbuf_mac_rx_low_water =
14120 DEFAULT_MB_MACRX_LOW_WATER_5705;
14121 tp->bufmgr_config.mbuf_high_water =
14122 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014123 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14124 tp->bufmgr_config.mbuf_mac_rx_low_water =
14125 DEFAULT_MB_MACRX_LOW_WATER_5906;
14126 tp->bufmgr_config.mbuf_high_water =
14127 DEFAULT_MB_HIGH_WATER_5906;
14128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014129
Michael Chanfdfec1722005-07-25 12:31:48 -070014130 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14131 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14132 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14133 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14134 tp->bufmgr_config.mbuf_high_water_jumbo =
14135 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14136 } else {
14137 tp->bufmgr_config.mbuf_read_dma_low_water =
14138 DEFAULT_MB_RDMA_LOW_WATER;
14139 tp->bufmgr_config.mbuf_mac_rx_low_water =
14140 DEFAULT_MB_MACRX_LOW_WATER;
14141 tp->bufmgr_config.mbuf_high_water =
14142 DEFAULT_MB_HIGH_WATER;
14143
14144 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14145 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14146 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14147 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14148 tp->bufmgr_config.mbuf_high_water_jumbo =
14149 DEFAULT_MB_HIGH_WATER_JUMBO;
14150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014151
14152 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14153 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14154}
14155
14156static char * __devinit tg3_phy_string(struct tg3 *tp)
14157{
Matt Carlson79eb6902010-02-17 15:17:03 +000014158 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14159 case TG3_PHY_ID_BCM5400: return "5400";
14160 case TG3_PHY_ID_BCM5401: return "5401";
14161 case TG3_PHY_ID_BCM5411: return "5411";
14162 case TG3_PHY_ID_BCM5701: return "5701";
14163 case TG3_PHY_ID_BCM5703: return "5703";
14164 case TG3_PHY_ID_BCM5704: return "5704";
14165 case TG3_PHY_ID_BCM5705: return "5705";
14166 case TG3_PHY_ID_BCM5750: return "5750";
14167 case TG3_PHY_ID_BCM5752: return "5752";
14168 case TG3_PHY_ID_BCM5714: return "5714";
14169 case TG3_PHY_ID_BCM5780: return "5780";
14170 case TG3_PHY_ID_BCM5755: return "5755";
14171 case TG3_PHY_ID_BCM5787: return "5787";
14172 case TG3_PHY_ID_BCM5784: return "5784";
14173 case TG3_PHY_ID_BCM5756: return "5722/5756";
14174 case TG3_PHY_ID_BCM5906: return "5906";
14175 case TG3_PHY_ID_BCM5761: return "5761";
14176 case TG3_PHY_ID_BCM5718C: return "5718C";
14177 case TG3_PHY_ID_BCM5718S: return "5718S";
14178 case TG3_PHY_ID_BCM57765: return "57765";
14179 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014180 case 0: return "serdes";
14181 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014183}
14184
Michael Chanf9804dd2005-09-27 12:13:10 -070014185static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14186{
14187 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14188 strcpy(str, "PCI Express");
14189 return str;
14190 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14191 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14192
14193 strcpy(str, "PCIX:");
14194
14195 if ((clock_ctrl == 7) ||
14196 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14197 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14198 strcat(str, "133MHz");
14199 else if (clock_ctrl == 0)
14200 strcat(str, "33MHz");
14201 else if (clock_ctrl == 2)
14202 strcat(str, "50MHz");
14203 else if (clock_ctrl == 4)
14204 strcat(str, "66MHz");
14205 else if (clock_ctrl == 6)
14206 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014207 } else {
14208 strcpy(str, "PCI:");
14209 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14210 strcat(str, "66MHz");
14211 else
14212 strcat(str, "33MHz");
14213 }
14214 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14215 strcat(str, ":32-bit");
14216 else
14217 strcat(str, ":64-bit");
14218 return str;
14219}
14220
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014221static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014222{
14223 struct pci_dev *peer;
14224 unsigned int func, devnr = tp->pdev->devfn & ~7;
14225
14226 for (func = 0; func < 8; func++) {
14227 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14228 if (peer && peer != tp->pdev)
14229 break;
14230 pci_dev_put(peer);
14231 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014232 /* 5704 can be configured in single-port mode, set peer to
14233 * tp->pdev in that case.
14234 */
14235 if (!peer) {
14236 peer = tp->pdev;
14237 return peer;
14238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014239
14240 /*
14241 * We don't need to keep the refcount elevated; there's no way
14242 * to remove one half of this device without removing the other
14243 */
14244 pci_dev_put(peer);
14245
14246 return peer;
14247}
14248
David S. Miller15f98502005-05-18 22:49:26 -070014249static void __devinit tg3_init_coal(struct tg3 *tp)
14250{
14251 struct ethtool_coalesce *ec = &tp->coal;
14252
14253 memset(ec, 0, sizeof(*ec));
14254 ec->cmd = ETHTOOL_GCOALESCE;
14255 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14256 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14257 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14258 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14259 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14260 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14261 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14262 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14263 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14264
14265 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14266 HOSTCC_MODE_CLRTICK_TXBD)) {
14267 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14268 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14269 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14270 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14271 }
Michael Chand244c892005-07-05 14:42:33 -070014272
14273 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14274 ec->rx_coalesce_usecs_irq = 0;
14275 ec->tx_coalesce_usecs_irq = 0;
14276 ec->stats_block_coalesce_usecs = 0;
14277 }
David S. Miller15f98502005-05-18 22:49:26 -070014278}
14279
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014280static const struct net_device_ops tg3_netdev_ops = {
14281 .ndo_open = tg3_open,
14282 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014283 .ndo_start_xmit = tg3_start_xmit,
14284 .ndo_get_stats = tg3_get_stats,
14285 .ndo_validate_addr = eth_validate_addr,
14286 .ndo_set_multicast_list = tg3_set_rx_mode,
14287 .ndo_set_mac_address = tg3_set_mac_addr,
14288 .ndo_do_ioctl = tg3_ioctl,
14289 .ndo_tx_timeout = tg3_tx_timeout,
14290 .ndo_change_mtu = tg3_change_mtu,
14291#if TG3_VLAN_TAG_USED
14292 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14293#endif
14294#ifdef CONFIG_NET_POLL_CONTROLLER
14295 .ndo_poll_controller = tg3_poll_controller,
14296#endif
14297};
14298
14299static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14300 .ndo_open = tg3_open,
14301 .ndo_stop = tg3_close,
14302 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014303 .ndo_get_stats = tg3_get_stats,
14304 .ndo_validate_addr = eth_validate_addr,
14305 .ndo_set_multicast_list = tg3_set_rx_mode,
14306 .ndo_set_mac_address = tg3_set_mac_addr,
14307 .ndo_do_ioctl = tg3_ioctl,
14308 .ndo_tx_timeout = tg3_tx_timeout,
14309 .ndo_change_mtu = tg3_change_mtu,
14310#if TG3_VLAN_TAG_USED
14311 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14312#endif
14313#ifdef CONFIG_NET_POLL_CONTROLLER
14314 .ndo_poll_controller = tg3_poll_controller,
14315#endif
14316};
14317
Linus Torvalds1da177e2005-04-16 15:20:36 -070014318static int __devinit tg3_init_one(struct pci_dev *pdev,
14319 const struct pci_device_id *ent)
14320{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014321 struct net_device *dev;
14322 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014323 int i, err, pm_cap;
14324 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014325 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014326 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014327
Joe Perches05dbe002010-02-17 19:44:19 +000014328 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014329
14330 err = pci_enable_device(pdev);
14331 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014332 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014333 return err;
14334 }
14335
Linus Torvalds1da177e2005-04-16 15:20:36 -070014336 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14337 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014338 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014339 goto err_out_disable_pdev;
14340 }
14341
14342 pci_set_master(pdev);
14343
14344 /* Find power-management capability. */
14345 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14346 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014347 dev_err(&pdev->dev,
14348 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014349 err = -EIO;
14350 goto err_out_free_res;
14351 }
14352
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014353 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014354 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014355 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014356 err = -ENOMEM;
14357 goto err_out_free_res;
14358 }
14359
Linus Torvalds1da177e2005-04-16 15:20:36 -070014360 SET_NETDEV_DEV(dev, &pdev->dev);
14361
Linus Torvalds1da177e2005-04-16 15:20:36 -070014362#if TG3_VLAN_TAG_USED
14363 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014364#endif
14365
14366 tp = netdev_priv(dev);
14367 tp->pdev = pdev;
14368 tp->dev = dev;
14369 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014370 tp->rx_mode = TG3_DEF_RX_MODE;
14371 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014372
Linus Torvalds1da177e2005-04-16 15:20:36 -070014373 if (tg3_debug > 0)
14374 tp->msg_enable = tg3_debug;
14375 else
14376 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14377
14378 /* The word/byte swap controls here control register access byte
14379 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14380 * setting below.
14381 */
14382 tp->misc_host_ctrl =
14383 MISC_HOST_CTRL_MASK_PCI_INT |
14384 MISC_HOST_CTRL_WORD_SWAP |
14385 MISC_HOST_CTRL_INDIR_ACCESS |
14386 MISC_HOST_CTRL_PCISTATE_RW;
14387
14388 /* The NONFRM (non-frame) byte/word swap controls take effect
14389 * on descriptor entries, anything which isn't packet data.
14390 *
14391 * The StrongARM chips on the board (one for tx, one for rx)
14392 * are running in big-endian mode.
14393 */
14394 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14395 GRC_MODE_WSWAP_NONFRM_DATA);
14396#ifdef __BIG_ENDIAN
14397 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14398#endif
14399 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014400 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014401 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014402
Matt Carlsond5fe4882008-11-21 17:20:32 -080014403 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014404 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014405 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014406 err = -ENOMEM;
14407 goto err_out_free_dev;
14408 }
14409
14410 tg3_init_link_config(tp);
14411
Linus Torvalds1da177e2005-04-16 15:20:36 -070014412 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14413 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014414
Linus Torvalds1da177e2005-04-16 15:20:36 -070014415 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014416 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014417 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014418
14419 err = tg3_get_invariants(tp);
14420 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014421 dev_err(&pdev->dev,
14422 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014423 goto err_out_iounmap;
14424 }
14425
Matt Carlson615774f2009-11-13 13:03:39 +000014426 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
14427 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
Stephen Hemminger00829822008-11-20 20:14:53 -080014428 dev->netdev_ops = &tg3_netdev_ops;
14429 else
14430 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14431
14432
Michael Chan4a29cc22006-03-19 13:21:12 -080014433 /* The EPB bridge inside 5714, 5715, and 5780 and any
14434 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014435 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14436 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14437 * do DMA address check in tg3_start_xmit().
14438 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014439 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070014440 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014441 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014442 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014443#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014444 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014445#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014446 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014447 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014448
14449 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014450 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014451 err = pci_set_dma_mask(pdev, dma_mask);
14452 if (!err) {
14453 dev->features |= NETIF_F_HIGHDMA;
14454 err = pci_set_consistent_dma_mask(pdev,
14455 persist_dma_mask);
14456 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014457 dev_err(&pdev->dev, "Unable to obtain 64 bit "
14458 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014459 goto err_out_iounmap;
14460 }
14461 }
14462 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014463 if (err || dma_mask == DMA_BIT_MASK(32)) {
14464 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014465 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014466 dev_err(&pdev->dev,
14467 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014468 goto err_out_iounmap;
14469 }
14470 }
14471
Michael Chanfdfec1722005-07-25 12:31:48 -070014472 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014473
Matt Carlson507399f2009-11-13 13:03:37 +000014474 /* Selectively allow TSO based on operating conditions */
14475 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
14476 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
14477 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14478 else {
14479 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
14480 tp->fw_needed = NULL;
14481 }
14482
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014483 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014484 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014485
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014486 /* TSO is on by default on chips that support hardware TSO.
14487 * Firmware TSO on older chips gives lower performance, so it
14488 * is off by default, but can be enabled using ethtool.
14489 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000014490 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
14491 (dev->features & NETIF_F_IP_CSUM))
14492 dev->features |= NETIF_F_TSO;
14493
14494 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
14495 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
14496 if (dev->features & NETIF_F_IPV6_CSUM)
Michael Chanb0026622006-07-03 19:42:14 -070014497 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +000014498 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
14499 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014500 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14501 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014502 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000014503 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -070014504 dev->features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070014505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014506
Linus Torvalds1da177e2005-04-16 15:20:36 -070014507 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14508 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14509 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14510 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14511 tp->rx_pending = 63;
14512 }
14513
Linus Torvalds1da177e2005-04-16 15:20:36 -070014514 err = tg3_get_device_address(tp);
14515 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014516 dev_err(&pdev->dev,
14517 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000014518 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014519 }
14520
Matt Carlson0d3031d2007-10-10 18:02:43 -070014521 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080014522 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080014523 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014524 dev_err(&pdev->dev,
14525 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014526 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000014527 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014528 }
14529
14530 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000014531
14532 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14533 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014534 }
14535
Matt Carlsonc88864d2007-11-12 21:07:01 -080014536 /*
14537 * Reset chip in case UNDI or EFI driver did not shutdown
14538 * DMA self test will enable WDMAC and we'll see (spurious)
14539 * pending DMA on the PCI bus at that point.
14540 */
14541 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
14542 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
14543 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
14544 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14545 }
14546
14547 err = tg3_test_dma(tp);
14548 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014549 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080014550 goto err_out_apeunmap;
14551 }
14552
Matt Carlsonc88864d2007-11-12 21:07:01 -080014553 /* flow control autonegotiation is default behavior */
14554 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
Steve Glendinninge18ce342008-12-16 02:00:00 -080014555 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlsonc88864d2007-11-12 21:07:01 -080014556
Matt Carlson78f90dc2009-11-13 13:03:42 +000014557 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
14558 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
14559 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
14560 for (i = 0; i < TG3_IRQ_MAX_VECS; i++) {
14561 struct tg3_napi *tnapi = &tp->napi[i];
14562
14563 tnapi->tp = tp;
14564 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
14565
14566 tnapi->int_mbox = intmbx;
14567 if (i < 4)
14568 intmbx += 0x8;
14569 else
14570 intmbx += 0x4;
14571
14572 tnapi->consmbox = rcvmbx;
14573 tnapi->prodmbox = sndmbx;
14574
14575 if (i) {
14576 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
14577 netif_napi_add(dev, &tnapi->napi, tg3_poll_msix, 64);
14578 } else {
14579 tnapi->coal_now = HOSTCC_MODE_NOW;
14580 netif_napi_add(dev, &tnapi->napi, tg3_poll, 64);
14581 }
14582
14583 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
14584 break;
14585
14586 /*
14587 * If we support MSIX, we'll be using RSS. If we're using
14588 * RSS, the first vector only handles link interrupts and the
14589 * remaining vectors handle rx and tx interrupts. Reuse the
14590 * mailbox values for the next iteration. The values we setup
14591 * above are still useful for the single vectored mode.
14592 */
14593 if (!i)
14594 continue;
14595
14596 rcvmbx += 0x8;
14597
14598 if (sndmbx & 0x4)
14599 sndmbx -= 0x4;
14600 else
14601 sndmbx += 0xc;
14602 }
14603
Matt Carlsonc88864d2007-11-12 21:07:01 -080014604 tg3_init_coal(tp);
14605
Michael Chanc49a1562006-12-17 17:07:29 -080014606 pci_set_drvdata(pdev, dev);
14607
Linus Torvalds1da177e2005-04-16 15:20:36 -070014608 err = register_netdev(dev);
14609 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014610 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014611 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014612 }
14613
Joe Perches05dbe002010-02-17 19:44:19 +000014614 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
14615 tp->board_part_number,
14616 tp->pci_chip_rev_id,
14617 tg3_bus_string(tp, str),
14618 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014619
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014620 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
14621 struct phy_device *phydev;
14622 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000014623 netdev_info(dev,
14624 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014625 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014626 } else
Matt Carlson5129c3a2010-04-05 10:19:23 +000014627 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
14628 "(WireSpeed[%d])\n", tg3_phy_string(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000014629 ((tp->tg3_flags & TG3_FLAG_10_100_ONLY) ? "10/100Base-TX" :
14630 ((tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) ? "1000Base-SX" :
14631 "10/100/1000Base-T")),
14632 (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED) == 0);
Matt Carlsondf59c942008-11-03 16:52:56 -080014633
Joe Perches05dbe002010-02-17 19:44:19 +000014634 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
14635 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
14636 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
14637 (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) != 0,
14638 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
14639 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
14640 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
14641 tp->dma_rwctrl,
14642 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
14643 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014644
14645 return 0;
14646
Matt Carlson0d3031d2007-10-10 18:02:43 -070014647err_out_apeunmap:
14648 if (tp->aperegs) {
14649 iounmap(tp->aperegs);
14650 tp->aperegs = NULL;
14651 }
14652
Linus Torvalds1da177e2005-04-16 15:20:36 -070014653err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070014654 if (tp->regs) {
14655 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014656 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014658
14659err_out_free_dev:
14660 free_netdev(dev);
14661
14662err_out_free_res:
14663 pci_release_regions(pdev);
14664
14665err_out_disable_pdev:
14666 pci_disable_device(pdev);
14667 pci_set_drvdata(pdev, NULL);
14668 return err;
14669}
14670
14671static void __devexit tg3_remove_one(struct pci_dev *pdev)
14672{
14673 struct net_device *dev = pci_get_drvdata(pdev);
14674
14675 if (dev) {
14676 struct tg3 *tp = netdev_priv(dev);
14677
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014678 if (tp->fw)
14679 release_firmware(tp->fw);
14680
Michael Chan7faa0062006-02-02 17:29:28 -080014681 flush_scheduled_work();
Matt Carlson158d7ab2008-05-29 01:37:54 -070014682
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014683 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
14684 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070014685 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014686 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070014687
Linus Torvalds1da177e2005-04-16 15:20:36 -070014688 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014689 if (tp->aperegs) {
14690 iounmap(tp->aperegs);
14691 tp->aperegs = NULL;
14692 }
Michael Chan68929142005-08-09 20:17:14 -070014693 if (tp->regs) {
14694 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014695 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014697 free_netdev(dev);
14698 pci_release_regions(pdev);
14699 pci_disable_device(pdev);
14700 pci_set_drvdata(pdev, NULL);
14701 }
14702}
14703
14704static int tg3_suspend(struct pci_dev *pdev, pm_message_t state)
14705{
14706 struct net_device *dev = pci_get_drvdata(pdev);
14707 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014708 pci_power_t target_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014709 int err;
14710
Michael Chan3e0c95f2007-08-03 20:56:54 -070014711 /* PCI register 4 needs to be saved whether netif_running() or not.
14712 * MSI address and data need to be saved if using MSI and
14713 * netif_running().
14714 */
14715 pci_save_state(pdev);
14716
Linus Torvalds1da177e2005-04-16 15:20:36 -070014717 if (!netif_running(dev))
14718 return 0;
14719
Michael Chan7faa0062006-02-02 17:29:28 -080014720 flush_scheduled_work();
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014721 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014722 tg3_netif_stop(tp);
14723
14724 del_timer_sync(&tp->timer);
14725
David S. Millerf47c11e2005-06-24 20:18:35 -070014726 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014727 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070014728 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014729
14730 netif_device_detach(dev);
14731
David S. Millerf47c11e2005-06-24 20:18:35 -070014732 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070014733 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080014734 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070014735 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014736
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014737 target_state = pdev->pm_cap ? pci_target_state(pdev) : PCI_D3hot;
14738
14739 err = tg3_set_power_state(tp, target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014740 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014741 int err2;
14742
David S. Millerf47c11e2005-06-24 20:18:35 -070014743 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014744
Michael Chan6a9eba12005-12-13 21:08:58 -080014745 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014746 err2 = tg3_restart_hw(tp, 1);
14747 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070014748 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014749
14750 tp->timer.expires = jiffies + tp->timer_offset;
14751 add_timer(&tp->timer);
14752
14753 netif_device_attach(dev);
14754 tg3_netif_start(tp);
14755
Michael Chanb9ec6c12006-07-25 16:37:27 -070014756out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014757 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014758
14759 if (!err2)
14760 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014761 }
14762
14763 return err;
14764}
14765
14766static int tg3_resume(struct pci_dev *pdev)
14767{
14768 struct net_device *dev = pci_get_drvdata(pdev);
14769 struct tg3 *tp = netdev_priv(dev);
14770 int err;
14771
Michael Chan3e0c95f2007-08-03 20:56:54 -070014772 pci_restore_state(tp->pdev);
14773
Linus Torvalds1da177e2005-04-16 15:20:36 -070014774 if (!netif_running(dev))
14775 return 0;
14776
Michael Chanbc1c7562006-03-20 17:48:03 -080014777 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014778 if (err)
14779 return err;
14780
14781 netif_device_attach(dev);
14782
David S. Millerf47c11e2005-06-24 20:18:35 -070014783 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014784
Michael Chan6a9eba12005-12-13 21:08:58 -080014785 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070014786 err = tg3_restart_hw(tp, 1);
14787 if (err)
14788 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014789
14790 tp->timer.expires = jiffies + tp->timer_offset;
14791 add_timer(&tp->timer);
14792
Linus Torvalds1da177e2005-04-16 15:20:36 -070014793 tg3_netif_start(tp);
14794
Michael Chanb9ec6c12006-07-25 16:37:27 -070014795out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014796 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014797
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014798 if (!err)
14799 tg3_phy_start(tp);
14800
Michael Chanb9ec6c12006-07-25 16:37:27 -070014801 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014802}
14803
14804static struct pci_driver tg3_driver = {
14805 .name = DRV_MODULE_NAME,
14806 .id_table = tg3_pci_tbl,
14807 .probe = tg3_init_one,
14808 .remove = __devexit_p(tg3_remove_one),
14809 .suspend = tg3_suspend,
14810 .resume = tg3_resume
14811};
14812
14813static int __init tg3_init(void)
14814{
Jeff Garzik29917622006-08-19 17:48:59 -040014815 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014816}
14817
14818static void __exit tg3_cleanup(void)
14819{
14820 pci_unregister_driver(&tg3_driver);
14821}
14822
14823module_init(tg3_init);
14824module_exit(tg3_cleanup);