blob: 573054ae7b58687dbe52eee0be849f0436159e1c [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;
588
589 /* Make sure the driver hasn't any stale locks. */
590 for (i = 0; i < 8; i++)
591 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + 4 * i,
592 APE_LOCK_GRANT_DRIVER);
593}
594
595static int tg3_ape_lock(struct tg3 *tp, int locknum)
596{
597 int i, off;
598 int ret = 0;
599 u32 status;
600
601 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
602 return 0;
603
604 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000605 case TG3_APE_LOCK_GRC:
606 case TG3_APE_LOCK_MEM:
607 break;
608 default:
609 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700610 }
611
612 off = 4 * locknum;
613
614 tg3_ape_write32(tp, TG3_APE_LOCK_REQ + off, APE_LOCK_REQ_DRIVER);
615
616 /* Wait for up to 1 millisecond to acquire lock. */
617 for (i = 0; i < 100; i++) {
618 status = tg3_ape_read32(tp, TG3_APE_LOCK_GRANT + off);
619 if (status == APE_LOCK_GRANT_DRIVER)
620 break;
621 udelay(10);
622 }
623
624 if (status != APE_LOCK_GRANT_DRIVER) {
625 /* Revoke the lock request. */
626 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + off,
627 APE_LOCK_GRANT_DRIVER);
628
629 ret = -EBUSY;
630 }
631
632 return ret;
633}
634
635static void tg3_ape_unlock(struct tg3 *tp, int locknum)
636{
637 int off;
638
639 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
640 return;
641
642 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000643 case TG3_APE_LOCK_GRC:
644 case TG3_APE_LOCK_MEM:
645 break;
646 default:
647 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700648 }
649
650 off = 4 * locknum;
651 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + off, APE_LOCK_GRANT_DRIVER);
652}
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654static void tg3_disable_ints(struct tg3 *tp)
655{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000656 int i;
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 tw32(TG3PCI_MISC_HOST_CTRL,
659 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000660 for (i = 0; i < tp->irq_max; i++)
661 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664static void tg3_enable_ints(struct tg3 *tp)
665{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000666 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000667
Michael Chanbbe832c2005-06-24 20:20:04 -0700668 tp->irq_sync = 0;
669 wmb();
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 tw32(TG3PCI_MISC_HOST_CTRL,
672 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000673
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000674 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000675 for (i = 0; i < tp->irq_cnt; i++) {
676 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000677
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000678 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
679 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
680 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
681
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000682 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000683 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000684
685 /* Force an initial interrupt */
686 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
687 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
688 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
689 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000690 tw32(HOSTCC_MODE, tp->coal_now);
691
692 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Matt Carlson17375d22009-08-28 14:02:18 +0000695static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700696{
Matt Carlson17375d22009-08-28 14:02:18 +0000697 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000698 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700699 unsigned int work_exists = 0;
700
701 /* check for phy events */
702 if (!(tp->tg3_flags &
703 (TG3_FLAG_USE_LINKCHG_REG |
704 TG3_FLAG_POLL_SERDES))) {
705 if (sblk->status & SD_STATUS_LINK_CHG)
706 work_exists = 1;
707 }
708 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000709 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000710 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700711 work_exists = 1;
712
713 return work_exists;
714}
715
Matt Carlson17375d22009-08-28 14:02:18 +0000716/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700717 * similar to tg3_enable_ints, but it accurately determines whether there
718 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400719 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
Matt Carlson17375d22009-08-28 14:02:18 +0000721static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Matt Carlson17375d22009-08-28 14:02:18 +0000723 struct tg3 *tp = tnapi->tp;
724
Matt Carlson898a56f2009-08-28 14:02:40 +0000725 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 mmiowb();
727
David S. Millerfac9b832005-05-18 22:46:34 -0700728 /* When doing tagged status, this work check is unnecessary.
729 * The last_tag we write above tells the chip which piece of
730 * work we've completed.
731 */
732 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
Matt Carlson17375d22009-08-28 14:02:18 +0000733 tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700734 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000735 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Matt Carlsonfed97812009-09-01 13:10:19 +0000738static void tg3_napi_disable(struct tg3 *tp)
739{
740 int i;
741
742 for (i = tp->irq_cnt - 1; i >= 0; i--)
743 napi_disable(&tp->napi[i].napi);
744}
745
746static void tg3_napi_enable(struct tg3 *tp)
747{
748 int i;
749
750 for (i = 0; i < tp->irq_cnt; i++)
751 napi_enable(&tp->napi[i].napi);
752}
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754static inline void tg3_netif_stop(struct tg3 *tp)
755{
Michael Chanbbe832c2005-06-24 20:20:04 -0700756 tp->dev->trans_start = jiffies; /* prevent tx timeout */
Matt Carlsonfed97812009-09-01 13:10:19 +0000757 tg3_napi_disable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 netif_tx_disable(tp->dev);
759}
760
761static inline void tg3_netif_start(struct tg3 *tp)
762{
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000763 /* NOTE: unconditional netif_tx_wake_all_queues is only
764 * appropriate so long as all callers are assured to
765 * have free tx slots (such as after tg3_init_hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 */
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000767 netif_tx_wake_all_queues(tp->dev);
768
Matt Carlsonfed97812009-09-01 13:10:19 +0000769 tg3_napi_enable(tp);
770 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
David S. Millerf47c11e2005-06-24 20:18:35 -0700771 tg3_enable_ints(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774static void tg3_switch_clocks(struct tg3 *tp)
775{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000776 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 u32 orig_clock_ctrl;
778
Matt Carlson795d01c2007-10-07 23:28:17 -0700779 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
780 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700781 return;
782
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000783 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 orig_clock_ctrl = clock_ctrl;
786 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
787 CLOCK_CTRL_CLKRUN_OENABLE |
788 0x1f);
789 tp->pci_clock_ctrl = clock_ctrl;
790
791 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
792 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800793 tw32_wait_f(TG3PCI_CLOCK_CTRL,
794 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800797 tw32_wait_f(TG3PCI_CLOCK_CTRL,
798 clock_ctrl |
799 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
800 40);
801 tw32_wait_f(TG3PCI_CLOCK_CTRL,
802 clock_ctrl | (CLOCK_CTRL_ALTCLK),
803 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800805 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
808#define PHY_BUSY_LOOPS 5000
809
810static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
811{
812 u32 frame_val;
813 unsigned int loops;
814 int ret;
815
816 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
817 tw32_f(MAC_MI_MODE,
818 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
819 udelay(80);
820 }
821
822 *val = 0x0;
823
Matt Carlson882e9792009-09-01 13:21:36 +0000824 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 MI_COM_PHY_ADDR_MASK);
826 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
827 MI_COM_REG_ADDR_MASK);
828 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 tw32_f(MAC_MI_COM, frame_val);
831
832 loops = PHY_BUSY_LOOPS;
833 while (loops != 0) {
834 udelay(10);
835 frame_val = tr32(MAC_MI_COM);
836
837 if ((frame_val & MI_COM_BUSY) == 0) {
838 udelay(5);
839 frame_val = tr32(MAC_MI_COM);
840 break;
841 }
842 loops -= 1;
843 }
844
845 ret = -EBUSY;
846 if (loops != 0) {
847 *val = frame_val & MI_COM_DATA_MASK;
848 ret = 0;
849 }
850
851 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
852 tw32_f(MAC_MI_MODE, tp->mi_mode);
853 udelay(80);
854 }
855
856 return ret;
857}
858
859static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
860{
861 u32 frame_val;
862 unsigned int loops;
863 int ret;
864
Matt Carlson7f97a4b2009-08-25 10:10:03 +0000865 if ((tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700866 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
867 return 0;
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
870 tw32_f(MAC_MI_MODE,
871 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
872 udelay(80);
873 }
874
Matt Carlson882e9792009-09-01 13:21:36 +0000875 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 MI_COM_PHY_ADDR_MASK);
877 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
878 MI_COM_REG_ADDR_MASK);
879 frame_val |= (val & MI_COM_DATA_MASK);
880 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 tw32_f(MAC_MI_COM, frame_val);
883
884 loops = PHY_BUSY_LOOPS;
885 while (loops != 0) {
886 udelay(10);
887 frame_val = tr32(MAC_MI_COM);
888 if ((frame_val & MI_COM_BUSY) == 0) {
889 udelay(5);
890 frame_val = tr32(MAC_MI_COM);
891 break;
892 }
893 loops -= 1;
894 }
895
896 ret = -EBUSY;
897 if (loops != 0)
898 ret = 0;
899
900 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
901 tw32_f(MAC_MI_MODE, tp->mi_mode);
902 udelay(80);
903 }
904
905 return ret;
906}
907
Matt Carlson95e28692008-05-25 23:44:14 -0700908static int tg3_bmcr_reset(struct tg3 *tp)
909{
910 u32 phy_control;
911 int limit, err;
912
913 /* OK, reset it, and poll the BMCR_RESET bit until it
914 * clears or we time out.
915 */
916 phy_control = BMCR_RESET;
917 err = tg3_writephy(tp, MII_BMCR, phy_control);
918 if (err != 0)
919 return -EBUSY;
920
921 limit = 5000;
922 while (limit--) {
923 err = tg3_readphy(tp, MII_BMCR, &phy_control);
924 if (err != 0)
925 return -EBUSY;
926
927 if ((phy_control & BMCR_RESET) == 0) {
928 udelay(40);
929 break;
930 }
931 udelay(10);
932 }
Roel Kluind4675b52009-02-12 16:33:27 -0800933 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -0700934 return -EBUSY;
935
936 return 0;
937}
938
Matt Carlson158d7ab2008-05-29 01:37:54 -0700939static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
940{
Francois Romieu3d165432009-01-19 16:56:50 -0800941 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700942 u32 val;
943
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000944 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700945
946 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000947 val = -EIO;
948
949 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700950
951 return val;
952}
953
954static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
955{
Francois Romieu3d165432009-01-19 16:56:50 -0800956 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000957 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700958
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000959 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700960
961 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000962 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700963
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000964 spin_unlock_bh(&tp->lock);
965
966 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700967}
968
969static int tg3_mdio_reset(struct mii_bus *bp)
970{
971 return 0;
972}
973
Matt Carlson9c61d6b2008-11-03 16:54:56 -0800974static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -0700975{
976 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800977 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -0700978
Matt Carlson3f0e3ad2009-11-02 14:24:36 +0000979 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800980 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +0000981 case PHY_ID_BCM50610:
982 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800983 val = MAC_PHYCFG2_50610_LED_MODES;
984 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000985 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800986 val = MAC_PHYCFG2_AC131_LED_MODES;
987 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000988 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800989 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
990 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000991 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800992 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
993 break;
994 default:
Matt Carlsona9daf362008-05-25 23:49:44 -0700995 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800996 }
997
998 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
999 tw32(MAC_PHYCFG2, val);
1000
1001 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001002 val &= ~(MAC_PHYCFG1_RGMII_INT |
1003 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1004 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001005 tw32(MAC_PHYCFG1, val);
1006
1007 return;
1008 }
1009
Matt Carlson14417062010-02-17 15:16:59 +00001010 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001011 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1012 MAC_PHYCFG2_FMODE_MASK_MASK |
1013 MAC_PHYCFG2_GMODE_MASK_MASK |
1014 MAC_PHYCFG2_ACT_MASK_MASK |
1015 MAC_PHYCFG2_QUAL_MASK_MASK |
1016 MAC_PHYCFG2_INBAND_ENABLE;
1017
1018 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001019
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001020 val = tr32(MAC_PHYCFG1);
1021 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1022 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Matt Carlson14417062010-02-17 15:16:59 +00001023 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001024 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1025 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
1026 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1027 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1028 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001029 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1030 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1031 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001032
Matt Carlsona9daf362008-05-25 23:49:44 -07001033 val = tr32(MAC_EXT_RGMII_MODE);
1034 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1035 MAC_RGMII_MODE_RX_QUALITY |
1036 MAC_RGMII_MODE_RX_ACTIVITY |
1037 MAC_RGMII_MODE_RX_ENG_DET |
1038 MAC_RGMII_MODE_TX_ENABLE |
1039 MAC_RGMII_MODE_TX_LOWPWR |
1040 MAC_RGMII_MODE_TX_RESET);
Matt Carlson14417062010-02-17 15:16:59 +00001041 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001042 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1043 val |= MAC_RGMII_MODE_RX_INT_B |
1044 MAC_RGMII_MODE_RX_QUALITY |
1045 MAC_RGMII_MODE_RX_ACTIVITY |
1046 MAC_RGMII_MODE_RX_ENG_DET;
1047 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1048 val |= MAC_RGMII_MODE_TX_ENABLE |
1049 MAC_RGMII_MODE_TX_LOWPWR |
1050 MAC_RGMII_MODE_TX_RESET;
1051 }
1052 tw32(MAC_EXT_RGMII_MODE, val);
1053}
1054
Matt Carlson158d7ab2008-05-29 01:37:54 -07001055static void tg3_mdio_start(struct tg3 *tp)
1056{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001057 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1058 tw32_f(MAC_MI_MODE, tp->mi_mode);
1059 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001060
Matt Carlson9ea48182010-02-17 15:17:01 +00001061 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1062 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1063 tg3_mdio_config_5785(tp);
1064}
1065
1066static int tg3_mdio_init(struct tg3 *tp)
1067{
1068 int i;
1069 u32 reg;
1070 struct phy_device *phydev;
1071
Matt Carlson882e9792009-09-01 13:21:36 +00001072 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
1073 u32 funcnum, is_serdes;
1074
1075 funcnum = tr32(TG3_CPMU_STATUS) & TG3_CPMU_STATUS_PCIE_FUNC;
1076 if (funcnum)
1077 tp->phy_addr = 2;
1078 else
1079 tp->phy_addr = 1;
1080
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001081 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1082 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1083 else
1084 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1085 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001086 if (is_serdes)
1087 tp->phy_addr += 7;
1088 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001089 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001090
Matt Carlson158d7ab2008-05-29 01:37:54 -07001091 tg3_mdio_start(tp);
1092
1093 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1094 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1095 return 0;
1096
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001097 tp->mdio_bus = mdiobus_alloc();
1098 if (tp->mdio_bus == NULL)
1099 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001100
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001101 tp->mdio_bus->name = "tg3 mdio bus";
1102 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001103 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001104 tp->mdio_bus->priv = tp;
1105 tp->mdio_bus->parent = &tp->pdev->dev;
1106 tp->mdio_bus->read = &tg3_mdio_read;
1107 tp->mdio_bus->write = &tg3_mdio_write;
1108 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001109 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001110 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001111
1112 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001113 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001114
1115 /* The bus registration will look for all the PHYs on the mdio bus.
1116 * Unfortunately, it does not ensure the PHY is powered up before
1117 * accessing the PHY ID registers. A chip reset is the
1118 * quickest way to bring the device back to an operational state..
1119 */
1120 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1121 tg3_bmcr_reset(tp);
1122
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001123 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001124 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001125 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001126 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001127 return i;
1128 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001129
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001130 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001131
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001132 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001133 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001134 mdiobus_unregister(tp->mdio_bus);
1135 mdiobus_free(tp->mdio_bus);
1136 return -ENODEV;
1137 }
1138
1139 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001140 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001141 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001142 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001143 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001144 case PHY_ID_BCM50610:
1145 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001146 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001147 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001148 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001149 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson14417062010-02-17 15:16:59 +00001150 if (tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)
Matt Carlsona9daf362008-05-25 23:49:44 -07001151 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1152 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1153 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1154 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1155 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001156 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001157 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001158 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001159 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001160 case PHY_ID_RTL8201E:
1161 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001162 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001163 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001164 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001165 break;
1166 }
1167
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001168 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1169
1170 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1171 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001172
1173 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001174}
1175
1176static void tg3_mdio_fini(struct tg3 *tp)
1177{
1178 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1179 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001180 mdiobus_unregister(tp->mdio_bus);
1181 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001182 }
1183}
1184
Matt Carlson95e28692008-05-25 23:44:14 -07001185/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001186static inline void tg3_generate_fw_event(struct tg3 *tp)
1187{
1188 u32 val;
1189
1190 val = tr32(GRC_RX_CPU_EVENT);
1191 val |= GRC_RX_CPU_DRIVER_EVENT;
1192 tw32_f(GRC_RX_CPU_EVENT, val);
1193
1194 tp->last_event_jiffies = jiffies;
1195}
1196
1197#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1198
1199/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001200static void tg3_wait_for_event_ack(struct tg3 *tp)
1201{
1202 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001203 unsigned int delay_cnt;
1204 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001205
Matt Carlson4ba526c2008-08-15 14:10:04 -07001206 /* If enough time has passed, no wait is necessary. */
1207 time_remain = (long)(tp->last_event_jiffies + 1 +
1208 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1209 (long)jiffies;
1210 if (time_remain < 0)
1211 return;
1212
1213 /* Check if we can shorten the wait time. */
1214 delay_cnt = jiffies_to_usecs(time_remain);
1215 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1216 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1217 delay_cnt = (delay_cnt >> 3) + 1;
1218
1219 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001220 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1221 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001222 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001223 }
1224}
1225
1226/* tp->lock is held. */
1227static void tg3_ump_link_report(struct tg3 *tp)
1228{
1229 u32 reg;
1230 u32 val;
1231
1232 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1233 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1234 return;
1235
1236 tg3_wait_for_event_ack(tp);
1237
1238 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1239
1240 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1241
1242 val = 0;
1243 if (!tg3_readphy(tp, MII_BMCR, &reg))
1244 val = reg << 16;
1245 if (!tg3_readphy(tp, MII_BMSR, &reg))
1246 val |= (reg & 0xffff);
1247 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1248
1249 val = 0;
1250 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1251 val = reg << 16;
1252 if (!tg3_readphy(tp, MII_LPA, &reg))
1253 val |= (reg & 0xffff);
1254 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1255
1256 val = 0;
1257 if (!(tp->tg3_flags2 & TG3_FLG2_MII_SERDES)) {
1258 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1259 val = reg << 16;
1260 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1261 val |= (reg & 0xffff);
1262 }
1263 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1264
1265 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1266 val = reg << 16;
1267 else
1268 val = 0;
1269 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1270
Matt Carlson4ba526c2008-08-15 14:10:04 -07001271 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001272}
1273
1274static void tg3_link_report(struct tg3 *tp)
1275{
1276 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001277 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001278 tg3_ump_link_report(tp);
1279 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001280 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1281 (tp->link_config.active_speed == SPEED_1000 ?
1282 1000 :
1283 (tp->link_config.active_speed == SPEED_100 ?
1284 100 : 10)),
1285 (tp->link_config.active_duplex == DUPLEX_FULL ?
1286 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001287
Joe Perches05dbe002010-02-17 19:44:19 +00001288 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1289 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1290 "on" : "off",
1291 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1292 "on" : "off");
Matt Carlson95e28692008-05-25 23:44:14 -07001293 tg3_ump_link_report(tp);
1294 }
1295}
1296
1297static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1298{
1299 u16 miireg;
1300
Steve Glendinninge18ce342008-12-16 02:00:00 -08001301 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001302 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001303 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001304 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001305 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001306 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1307 else
1308 miireg = 0;
1309
1310 return miireg;
1311}
1312
1313static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1314{
1315 u16 miireg;
1316
Steve Glendinninge18ce342008-12-16 02:00:00 -08001317 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001318 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001319 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001320 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001321 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001322 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1323 else
1324 miireg = 0;
1325
1326 return miireg;
1327}
1328
Matt Carlson95e28692008-05-25 23:44:14 -07001329static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1330{
1331 u8 cap = 0;
1332
1333 if (lcladv & ADVERTISE_1000XPAUSE) {
1334 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1335 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001336 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001337 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001338 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001339 } else {
1340 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001341 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001342 }
1343 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1344 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001345 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001346 }
1347
1348 return cap;
1349}
1350
Matt Carlsonf51f3562008-05-25 23:45:08 -07001351static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001352{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001353 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001354 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001355 u32 old_rx_mode = tp->rx_mode;
1356 u32 old_tx_mode = tp->tx_mode;
1357
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001358 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001359 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001360 else
1361 autoneg = tp->link_config.autoneg;
1362
1363 if (autoneg == AUTONEG_ENABLE &&
Matt Carlson95e28692008-05-25 23:44:14 -07001364 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
1365 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001366 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001367 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001368 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001369 } else
1370 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001371
Matt Carlsonf51f3562008-05-25 23:45:08 -07001372 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001373
Steve Glendinninge18ce342008-12-16 02:00:00 -08001374 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001375 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1376 else
1377 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1378
Matt Carlsonf51f3562008-05-25 23:45:08 -07001379 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001380 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001381
Steve Glendinninge18ce342008-12-16 02:00:00 -08001382 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001383 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1384 else
1385 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1386
Matt Carlsonf51f3562008-05-25 23:45:08 -07001387 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001388 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001389}
1390
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001391static void tg3_adjust_link(struct net_device *dev)
1392{
1393 u8 oldflowctrl, linkmesg = 0;
1394 u32 mac_mode, lcl_adv, rmt_adv;
1395 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001396 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001397
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001398 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001399
1400 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1401 MAC_MODE_HALF_DUPLEX);
1402
1403 oldflowctrl = tp->link_config.active_flowctrl;
1404
1405 if (phydev->link) {
1406 lcl_adv = 0;
1407 rmt_adv = 0;
1408
1409 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1410 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001411 else if (phydev->speed == SPEED_1000 ||
1412 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001413 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001414 else
1415 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001416
1417 if (phydev->duplex == DUPLEX_HALF)
1418 mac_mode |= MAC_MODE_HALF_DUPLEX;
1419 else {
1420 lcl_adv = tg3_advert_flowctrl_1000T(
1421 tp->link_config.flowctrl);
1422
1423 if (phydev->pause)
1424 rmt_adv = LPA_PAUSE_CAP;
1425 if (phydev->asym_pause)
1426 rmt_adv |= LPA_PAUSE_ASYM;
1427 }
1428
1429 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1430 } else
1431 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1432
1433 if (mac_mode != tp->mac_mode) {
1434 tp->mac_mode = mac_mode;
1435 tw32_f(MAC_MODE, tp->mac_mode);
1436 udelay(40);
1437 }
1438
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001439 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1440 if (phydev->speed == SPEED_10)
1441 tw32(MAC_MI_STAT,
1442 MAC_MI_STAT_10MBPS_MODE |
1443 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1444 else
1445 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1446 }
1447
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001448 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1449 tw32(MAC_TX_LENGTHS,
1450 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1451 (6 << TX_LENGTHS_IPG_SHIFT) |
1452 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1453 else
1454 tw32(MAC_TX_LENGTHS,
1455 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1456 (6 << TX_LENGTHS_IPG_SHIFT) |
1457 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1458
1459 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1460 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1461 phydev->speed != tp->link_config.active_speed ||
1462 phydev->duplex != tp->link_config.active_duplex ||
1463 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001464 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001465
1466 tp->link_config.active_speed = phydev->speed;
1467 tp->link_config.active_duplex = phydev->duplex;
1468
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001469 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001470
1471 if (linkmesg)
1472 tg3_link_report(tp);
1473}
1474
1475static int tg3_phy_init(struct tg3 *tp)
1476{
1477 struct phy_device *phydev;
1478
1479 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED)
1480 return 0;
1481
1482 /* Bring the PHY back to a known state. */
1483 tg3_bmcr_reset(tp);
1484
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001485 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001486
1487 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001488 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001489 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001490 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001491 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001492 return PTR_ERR(phydev);
1493 }
1494
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001495 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001496 switch (phydev->interface) {
1497 case PHY_INTERFACE_MODE_GMII:
1498 case PHY_INTERFACE_MODE_RGMII:
Matt Carlson321d32a2008-11-21 17:22:19 -08001499 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
1500 phydev->supported &= (PHY_GBIT_FEATURES |
1501 SUPPORTED_Pause |
1502 SUPPORTED_Asym_Pause);
1503 break;
1504 }
1505 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001506 case PHY_INTERFACE_MODE_MII:
1507 phydev->supported &= (PHY_BASIC_FEATURES |
1508 SUPPORTED_Pause |
1509 SUPPORTED_Asym_Pause);
1510 break;
1511 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001512 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001513 return -EINVAL;
1514 }
1515
1516 tp->tg3_flags3 |= TG3_FLG3_PHY_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001517
1518 phydev->advertising = phydev->supported;
1519
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001520 return 0;
1521}
1522
1523static void tg3_phy_start(struct tg3 *tp)
1524{
1525 struct phy_device *phydev;
1526
1527 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1528 return;
1529
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001530 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001531
1532 if (tp->link_config.phy_is_low_power) {
1533 tp->link_config.phy_is_low_power = 0;
1534 phydev->speed = tp->link_config.orig_speed;
1535 phydev->duplex = tp->link_config.orig_duplex;
1536 phydev->autoneg = tp->link_config.orig_autoneg;
1537 phydev->advertising = tp->link_config.orig_advertising;
1538 }
1539
1540 phy_start(phydev);
1541
1542 phy_start_aneg(phydev);
1543}
1544
1545static void tg3_phy_stop(struct tg3 *tp)
1546{
1547 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1548 return;
1549
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001550 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001551}
1552
1553static void tg3_phy_fini(struct tg3 *tp)
1554{
1555 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001556 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001557 tp->tg3_flags3 &= ~TG3_FLG3_PHY_CONNECTED;
1558 }
1559}
1560
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001561static void tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1562{
1563 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1564 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1565}
1566
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001567static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1568{
1569 u32 phytest;
1570
1571 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1572 u32 phy;
1573
1574 tg3_writephy(tp, MII_TG3_FET_TEST,
1575 phytest | MII_TG3_FET_SHADOW_EN);
1576 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1577 if (enable)
1578 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1579 else
1580 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1581 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1582 }
1583 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1584 }
1585}
1586
Matt Carlson6833c042008-11-21 17:18:59 -08001587static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1588{
1589 u32 reg;
1590
Matt Carlsonecf14102010-01-20 16:58:05 +00001591 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1592 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
1593 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001594 return;
1595
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001596 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
1597 tg3_phy_fet_toggle_apd(tp, enable);
1598 return;
1599 }
1600
Matt Carlson6833c042008-11-21 17:18:59 -08001601 reg = MII_TG3_MISC_SHDW_WREN |
1602 MII_TG3_MISC_SHDW_SCR5_SEL |
1603 MII_TG3_MISC_SHDW_SCR5_LPED |
1604 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1605 MII_TG3_MISC_SHDW_SCR5_SDTL |
1606 MII_TG3_MISC_SHDW_SCR5_C125OE;
1607 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1608 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1609
1610 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1611
1612
1613 reg = MII_TG3_MISC_SHDW_WREN |
1614 MII_TG3_MISC_SHDW_APD_SEL |
1615 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1616 if (enable)
1617 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1618
1619 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1620}
1621
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001622static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1623{
1624 u32 phy;
1625
1626 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1627 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
1628 return;
1629
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001630 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001631 u32 ephy;
1632
Matt Carlson535ef6e2009-08-25 10:09:36 +00001633 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1634 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1635
1636 tg3_writephy(tp, MII_TG3_FET_TEST,
1637 ephy | MII_TG3_FET_SHADOW_EN);
1638 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001639 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001640 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001641 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001642 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1643 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001644 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001645 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001646 }
1647 } else {
1648 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1649 MII_TG3_AUXCTL_SHDWSEL_MISC;
1650 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1651 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1652 if (enable)
1653 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1654 else
1655 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1656 phy |= MII_TG3_AUXCTL_MISC_WREN;
1657 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1658 }
1659 }
1660}
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662static void tg3_phy_set_wirespeed(struct tg3 *tp)
1663{
1664 u32 val;
1665
1666 if (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED)
1667 return;
1668
1669 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1670 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1671 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1672 (val | (1 << 15) | (1 << 4)));
1673}
1674
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001675static void tg3_phy_apply_otp(struct tg3 *tp)
1676{
1677 u32 otp, phy;
1678
1679 if (!tp->phy_otp)
1680 return;
1681
1682 otp = tp->phy_otp;
1683
1684 /* Enable SM_DSP clock and tx 6dB coding. */
1685 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1686 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1687 MII_TG3_AUXCTL_ACTL_TX_6DB;
1688 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1689
1690 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1691 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1692 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1693
1694 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1695 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1696 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1697
1698 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1699 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1700 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1701
1702 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1703 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1704
1705 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1706 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1707
1708 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1709 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1710 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1711
1712 /* Turn off SM_DSP clock. */
1713 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1714 MII_TG3_AUXCTL_ACTL_TX_6DB;
1715 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1716}
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718static int tg3_wait_macro_done(struct tg3 *tp)
1719{
1720 int limit = 100;
1721
1722 while (limit--) {
1723 u32 tmp32;
1724
1725 if (!tg3_readphy(tp, 0x16, &tmp32)) {
1726 if ((tmp32 & 0x1000) == 0)
1727 break;
1728 }
1729 }
Roel Kluind4675b52009-02-12 16:33:27 -08001730 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return -EBUSY;
1732
1733 return 0;
1734}
1735
1736static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1737{
1738 static const u32 test_pat[4][6] = {
1739 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1740 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1741 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1742 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1743 };
1744 int chan;
1745
1746 for (chan = 0; chan < 4; chan++) {
1747 int i;
1748
1749 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1750 (chan * 0x2000) | 0x0200);
1751 tg3_writephy(tp, 0x16, 0x0002);
1752
1753 for (i = 0; i < 6; i++)
1754 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1755 test_pat[chan][i]);
1756
1757 tg3_writephy(tp, 0x16, 0x0202);
1758 if (tg3_wait_macro_done(tp)) {
1759 *resetp = 1;
1760 return -EBUSY;
1761 }
1762
1763 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1764 (chan * 0x2000) | 0x0200);
1765 tg3_writephy(tp, 0x16, 0x0082);
1766 if (tg3_wait_macro_done(tp)) {
1767 *resetp = 1;
1768 return -EBUSY;
1769 }
1770
1771 tg3_writephy(tp, 0x16, 0x0802);
1772 if (tg3_wait_macro_done(tp)) {
1773 *resetp = 1;
1774 return -EBUSY;
1775 }
1776
1777 for (i = 0; i < 6; i += 2) {
1778 u32 low, high;
1779
1780 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1781 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1782 tg3_wait_macro_done(tp)) {
1783 *resetp = 1;
1784 return -EBUSY;
1785 }
1786 low &= 0x7fff;
1787 high &= 0x000f;
1788 if (low != test_pat[chan][i] ||
1789 high != test_pat[chan][i+1]) {
1790 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1791 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1792 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1793
1794 return -EBUSY;
1795 }
1796 }
1797 }
1798
1799 return 0;
1800}
1801
1802static int tg3_phy_reset_chanpat(struct tg3 *tp)
1803{
1804 int chan;
1805
1806 for (chan = 0; chan < 4; chan++) {
1807 int i;
1808
1809 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1810 (chan * 0x2000) | 0x0200);
1811 tg3_writephy(tp, 0x16, 0x0002);
1812 for (i = 0; i < 6; i++)
1813 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
1814 tg3_writephy(tp, 0x16, 0x0202);
1815 if (tg3_wait_macro_done(tp))
1816 return -EBUSY;
1817 }
1818
1819 return 0;
1820}
1821
1822static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1823{
1824 u32 reg32, phy9_orig;
1825 int retries, do_phy_reset, err;
1826
1827 retries = 10;
1828 do_phy_reset = 1;
1829 do {
1830 if (do_phy_reset) {
1831 err = tg3_bmcr_reset(tp);
1832 if (err)
1833 return err;
1834 do_phy_reset = 0;
1835 }
1836
1837 /* Disable transmitter and interrupt. */
1838 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1839 continue;
1840
1841 reg32 |= 0x3000;
1842 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1843
1844 /* Set full-duplex, 1000 mbps. */
1845 tg3_writephy(tp, MII_BMCR,
1846 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1847
1848 /* Set to master mode. */
1849 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1850 continue;
1851
1852 tg3_writephy(tp, MII_TG3_CTRL,
1853 (MII_TG3_CTRL_AS_MASTER |
1854 MII_TG3_CTRL_ENABLE_AS_MASTER));
1855
1856 /* Enable SM_DSP_CLOCK and 6dB. */
1857 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1858
1859 /* Block the PHY control access. */
1860 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1861 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0800);
1862
1863 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1864 if (!err)
1865 break;
1866 } while (--retries);
1867
1868 err = tg3_phy_reset_chanpat(tp);
1869 if (err)
1870 return err;
1871
1872 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1873 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0000);
1874
1875 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
1876 tg3_writephy(tp, 0x16, 0x0000);
1877
1878 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1879 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1880 /* Set Extended packet length bit for jumbo frames */
1881 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
Matt Carlson859a588792010-04-05 10:19:28 +00001882 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1884 }
1885
1886 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1887
1888 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1889 reg32 &= ~0x3000;
1890 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1891 } else if (!err)
1892 err = -EBUSY;
1893
1894 return err;
1895}
1896
1897/* This will reset the tigon3 PHY if there is no valid
1898 * link unless the FORCE argument is non-zero.
1899 */
1900static int tg3_phy_reset(struct tg3 *tp)
1901{
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001902 u32 cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 u32 phy_status;
1904 int err;
1905
Michael Chan60189dd2006-12-17 17:08:07 -08001906 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1907 u32 val;
1908
1909 val = tr32(GRC_MISC_CFG);
1910 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1911 udelay(40);
1912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 err = tg3_readphy(tp, MII_BMSR, &phy_status);
1914 err |= tg3_readphy(tp, MII_BMSR, &phy_status);
1915 if (err != 0)
1916 return -EBUSY;
1917
Michael Chanc8e1e822006-04-29 18:55:17 -07001918 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
1919 netif_carrier_off(tp->dev);
1920 tg3_link_report(tp);
1921 }
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1924 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
1925 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
1926 err = tg3_phy_reset_5703_4_5(tp);
1927 if (err)
1928 return err;
1929 goto out;
1930 }
1931
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001932 cpmuctrl = 0;
1933 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
1934 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
1935 cpmuctrl = tr32(TG3_CPMU_CTRL);
1936 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
1937 tw32(TG3_CPMU_CTRL,
1938 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
1939 }
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 err = tg3_bmcr_reset(tp);
1942 if (err)
1943 return err;
1944
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001945 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
1946 u32 phy;
1947
1948 phy = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
1949 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, phy);
1950
1951 tw32(TG3_CPMU_CTRL, cpmuctrl);
1952 }
1953
Matt Carlsonbcb37f62008-11-03 16:52:09 -08001954 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
1955 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08001956 u32 val;
1957
1958 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
1959 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
1960 CPMU_LSPD_1000MB_MACCLK_12_5) {
1961 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
1962 udelay(40);
1963 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
1964 }
1965 }
1966
Matt Carlsonecf14102010-01-20 16:58:05 +00001967 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
1968 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES))
1969 return 0;
1970
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001971 tg3_phy_apply_otp(tp);
1972
Matt Carlson6833c042008-11-21 17:18:59 -08001973 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
1974 tg3_phy_toggle_apd(tp, true);
1975 else
1976 tg3_phy_toggle_apd(tp, false);
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978out:
1979 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADC_BUG) {
1980 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1981 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
1982 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x2aaa);
1983 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
1984 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0323);
1985 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1986 }
1987 if (tp->tg3_flags2 & TG3_FLG2_PHY_5704_A0_BUG) {
1988 tg3_writephy(tp, 0x1c, 0x8d68);
1989 tg3_writephy(tp, 0x1c, 0x8d68);
1990 }
1991 if (tp->tg3_flags2 & TG3_FLG2_PHY_BER_BUG) {
1992 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1993 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
1994 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x310b);
1995 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
1996 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x9506);
1997 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x401f);
1998 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x14e2);
1999 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
Matt Carlson859a588792010-04-05 10:19:28 +00002000 } else if (tp->tg3_flags2 & TG3_FLG2_PHY_JITTER_BUG) {
Michael Chanc424cb22006-04-29 18:56:34 -07002001 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2002 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Michael Chanc1d2a192007-01-08 19:57:20 -08002003 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADJUST_TRIM) {
2004 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2005 tg3_writephy(tp, MII_TG3_TEST1,
2006 MII_TG3_TEST1_TRIM_EN | 0x4);
2007 } else
2008 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07002009 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 /* Set Extended packet length bit (bit 14) on all chips that */
2012 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002013 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 /* Cannot do read-modify-write on 5401 */
2015 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00002016 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 u32 phy_reg;
2018
2019 /* Set bit 14 with read-modify-write to preserve other bits */
2020 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
2021 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy_reg))
2022 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy_reg | 0x4000);
2023 }
2024
2025 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2026 * jumbo frames transmission.
2027 */
Matt Carlson8f666b02009-08-28 13:58:24 +00002028 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 u32 phy_reg;
2030
2031 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &phy_reg))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002032 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2033 phy_reg | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
2035
Michael Chan715116a2006-09-27 16:09:25 -07002036 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002037 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002038 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002039 }
2040
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002041 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 tg3_phy_set_wirespeed(tp);
2043 return 0;
2044}
2045
2046static void tg3_frob_aux_power(struct tg3 *tp)
2047{
2048 struct tg3 *tp_peer = tp;
2049
Matt Carlson334355a2010-01-20 16:58:10 +00002050 /* The GPIOs do something completely different on 57765. */
2051 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
2052 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return;
2054
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00002055 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2056 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
2057 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002058 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002060 dev_peer = pci_get_drvdata(tp->pdev_peer);
Michael Chanbc1c7562006-03-20 17:48:03 -08002061 /* remove_one() may have been run on the peer. */
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002062 if (!dev_peer)
Michael Chanbc1c7562006-03-20 17:48:03 -08002063 tp_peer = tp;
2064 else
2065 tp_peer = netdev_priv(dev_peer);
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
Michael Chan6921d202005-12-13 21:15:53 -08002069 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0 ||
2070 (tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
2071 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2073 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002074 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2075 (GRC_LCLCTRL_GPIO_OE0 |
2076 GRC_LCLCTRL_GPIO_OE1 |
2077 GRC_LCLCTRL_GPIO_OE2 |
2078 GRC_LCLCTRL_GPIO_OUTPUT0 |
2079 GRC_LCLCTRL_GPIO_OUTPUT1),
2080 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002081 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2082 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002083 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2084 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2085 GRC_LCLCTRL_GPIO_OE1 |
2086 GRC_LCLCTRL_GPIO_OE2 |
2087 GRC_LCLCTRL_GPIO_OUTPUT0 |
2088 GRC_LCLCTRL_GPIO_OUTPUT1 |
2089 tp->grc_local_ctrl;
2090 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2091
2092 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2093 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2094
2095 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2096 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 } else {
2098 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002099 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 if (tp_peer != tp &&
2102 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2103 return;
2104
Michael Chandc56b7d2005-12-19 16:26:28 -08002105 /* Workaround to prevent overdrawing Amps. */
2106 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2107 ASIC_REV_5714) {
2108 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002109 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2110 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002111 }
2112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 /* On 5753 and variants, GPIO2 cannot be used. */
2114 no_gpio2 = tp->nic_sram_data_cfg &
2115 NIC_SRAM_DATA_CFG_NO_GPIO2;
2116
Michael Chandc56b7d2005-12-19 16:26:28 -08002117 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 GRC_LCLCTRL_GPIO_OE1 |
2119 GRC_LCLCTRL_GPIO_OE2 |
2120 GRC_LCLCTRL_GPIO_OUTPUT1 |
2121 GRC_LCLCTRL_GPIO_OUTPUT2;
2122 if (no_gpio2) {
2123 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2124 GRC_LCLCTRL_GPIO_OUTPUT2);
2125 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002126 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2127 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2130
Michael Chanb401e9e2005-12-19 16:27:04 -08002131 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2132 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 if (!no_gpio2) {
2135 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002136 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2137 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
2139 }
2140 } else {
2141 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2142 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2143 if (tp_peer != tp &&
2144 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2145 return;
2146
Michael Chanb401e9e2005-12-19 16:27:04 -08002147 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2148 (GRC_LCLCTRL_GPIO_OE1 |
2149 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Michael Chanb401e9e2005-12-19 16:27:04 -08002151 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2152 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Michael Chanb401e9e2005-12-19 16:27:04 -08002154 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2155 (GRC_LCLCTRL_GPIO_OE1 |
2156 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
2158 }
2159}
2160
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002161static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2162{
2163 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2164 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002165 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002166 if (speed != SPEED_10)
2167 return 1;
2168 } else if (speed == SPEED_10)
2169 return 1;
2170
2171 return 0;
2172}
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174static int tg3_setup_phy(struct tg3 *, int);
2175
2176#define RESET_KIND_SHUTDOWN 0
2177#define RESET_KIND_INIT 1
2178#define RESET_KIND_SUSPEND 2
2179
2180static void tg3_write_sig_post_reset(struct tg3 *, int);
2181static int tg3_halt_cpu(struct tg3 *, u32);
2182
Matt Carlson0a459aa2008-11-03 16:54:15 -08002183static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002184{
Matt Carlsonce057f02007-11-12 21:08:03 -08002185 u32 val;
2186
Michael Chan51297242007-02-13 12:17:57 -08002187 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
2188 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2189 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2190 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2191
2192 sg_dig_ctrl |=
2193 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2194 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2195 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2196 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002197 return;
Michael Chan51297242007-02-13 12:17:57 -08002198 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002199
Michael Chan60189dd2006-12-17 17:08:07 -08002200 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002201 tg3_bmcr_reset(tp);
2202 val = tr32(GRC_MISC_CFG);
2203 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2204 udelay(40);
2205 return;
Matt Carlson0e5f7842009-11-02 14:26:38 +00002206 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
2207 u32 phytest;
2208 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2209 u32 phy;
2210
2211 tg3_writephy(tp, MII_ADVERTISE, 0);
2212 tg3_writephy(tp, MII_BMCR,
2213 BMCR_ANENABLE | BMCR_ANRESTART);
2214
2215 tg3_writephy(tp, MII_TG3_FET_TEST,
2216 phytest | MII_TG3_FET_SHADOW_EN);
2217 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2218 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2219 tg3_writephy(tp,
2220 MII_TG3_FET_SHDW_AUXMODE4,
2221 phy);
2222 }
2223 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2224 }
2225 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002226 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002227 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2228 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002229
2230 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2231 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2232 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2233 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2234 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002235 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002236
Michael Chan15c3b692006-03-22 01:06:52 -08002237 /* The PHY should not be powered down on some chips because
2238 * of bugs.
2239 */
2240 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2241 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2242 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
2243 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
2244 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002245
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002246 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2247 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002248 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2249 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2250 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2251 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2252 }
2253
Michael Chan15c3b692006-03-22 01:06:52 -08002254 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2255}
2256
Matt Carlson3f007892008-11-03 16:51:36 -08002257/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002258static int tg3_nvram_lock(struct tg3 *tp)
2259{
2260 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2261 int i;
2262
2263 if (tp->nvram_lock_cnt == 0) {
2264 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2265 for (i = 0; i < 8000; i++) {
2266 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2267 break;
2268 udelay(20);
2269 }
2270 if (i == 8000) {
2271 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2272 return -ENODEV;
2273 }
2274 }
2275 tp->nvram_lock_cnt++;
2276 }
2277 return 0;
2278}
2279
2280/* tp->lock is held. */
2281static void tg3_nvram_unlock(struct tg3 *tp)
2282{
2283 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2284 if (tp->nvram_lock_cnt > 0)
2285 tp->nvram_lock_cnt--;
2286 if (tp->nvram_lock_cnt == 0)
2287 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2288 }
2289}
2290
2291/* tp->lock is held. */
2292static void tg3_enable_nvram_access(struct tg3 *tp)
2293{
2294 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002295 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002296 u32 nvaccess = tr32(NVRAM_ACCESS);
2297
2298 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2299 }
2300}
2301
2302/* tp->lock is held. */
2303static void tg3_disable_nvram_access(struct tg3 *tp)
2304{
2305 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002306 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002307 u32 nvaccess = tr32(NVRAM_ACCESS);
2308
2309 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2310 }
2311}
2312
2313static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2314 u32 offset, u32 *val)
2315{
2316 u32 tmp;
2317 int i;
2318
2319 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2320 return -EINVAL;
2321
2322 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2323 EEPROM_ADDR_DEVID_MASK |
2324 EEPROM_ADDR_READ);
2325 tw32(GRC_EEPROM_ADDR,
2326 tmp |
2327 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2328 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2329 EEPROM_ADDR_ADDR_MASK) |
2330 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2331
2332 for (i = 0; i < 1000; i++) {
2333 tmp = tr32(GRC_EEPROM_ADDR);
2334
2335 if (tmp & EEPROM_ADDR_COMPLETE)
2336 break;
2337 msleep(1);
2338 }
2339 if (!(tmp & EEPROM_ADDR_COMPLETE))
2340 return -EBUSY;
2341
Matt Carlson62cedd12009-04-20 14:52:29 -07002342 tmp = tr32(GRC_EEPROM_DATA);
2343
2344 /*
2345 * The data will always be opposite the native endian
2346 * format. Perform a blind byteswap to compensate.
2347 */
2348 *val = swab32(tmp);
2349
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002350 return 0;
2351}
2352
2353#define NVRAM_CMD_TIMEOUT 10000
2354
2355static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2356{
2357 int i;
2358
2359 tw32(NVRAM_CMD, nvram_cmd);
2360 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2361 udelay(10);
2362 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2363 udelay(10);
2364 break;
2365 }
2366 }
2367
2368 if (i == NVRAM_CMD_TIMEOUT)
2369 return -EBUSY;
2370
2371 return 0;
2372}
2373
2374static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2375{
2376 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2377 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2378 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2379 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2380 (tp->nvram_jedecnum == JEDEC_ATMEL))
2381
2382 addr = ((addr / tp->nvram_pagesize) <<
2383 ATMEL_AT45DB0X1B_PAGE_POS) +
2384 (addr % tp->nvram_pagesize);
2385
2386 return addr;
2387}
2388
2389static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2390{
2391 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2392 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2393 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2394 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2395 (tp->nvram_jedecnum == JEDEC_ATMEL))
2396
2397 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2398 tp->nvram_pagesize) +
2399 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2400
2401 return addr;
2402}
2403
Matt Carlsone4f34112009-02-25 14:25:00 +00002404/* NOTE: Data read in from NVRAM is byteswapped according to
2405 * the byteswapping settings for all other register accesses.
2406 * tg3 devices are BE devices, so on a BE machine, the data
2407 * returned will be exactly as it is seen in NVRAM. On a LE
2408 * machine, the 32-bit value will be byteswapped.
2409 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002410static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2411{
2412 int ret;
2413
2414 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2415 return tg3_nvram_read_using_eeprom(tp, offset, val);
2416
2417 offset = tg3_nvram_phys_addr(tp, offset);
2418
2419 if (offset > NVRAM_ADDR_MSK)
2420 return -EINVAL;
2421
2422 ret = tg3_nvram_lock(tp);
2423 if (ret)
2424 return ret;
2425
2426 tg3_enable_nvram_access(tp);
2427
2428 tw32(NVRAM_ADDR, offset);
2429 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2430 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2431
2432 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002433 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002434
2435 tg3_disable_nvram_access(tp);
2436
2437 tg3_nvram_unlock(tp);
2438
2439 return ret;
2440}
2441
Matt Carlsona9dc5292009-02-25 14:25:30 +00002442/* Ensures NVRAM data is in bytestream format. */
2443static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002444{
2445 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002446 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002447 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002448 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002449 return res;
2450}
2451
2452/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002453static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2454{
2455 u32 addr_high, addr_low;
2456 int i;
2457
2458 addr_high = ((tp->dev->dev_addr[0] << 8) |
2459 tp->dev->dev_addr[1]);
2460 addr_low = ((tp->dev->dev_addr[2] << 24) |
2461 (tp->dev->dev_addr[3] << 16) |
2462 (tp->dev->dev_addr[4] << 8) |
2463 (tp->dev->dev_addr[5] << 0));
2464 for (i = 0; i < 4; i++) {
2465 if (i == 1 && skip_mac_1)
2466 continue;
2467 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2468 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2469 }
2470
2471 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2472 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2473 for (i = 0; i < 12; i++) {
2474 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2475 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2476 }
2477 }
2478
2479 addr_high = (tp->dev->dev_addr[0] +
2480 tp->dev->dev_addr[1] +
2481 tp->dev->dev_addr[2] +
2482 tp->dev->dev_addr[3] +
2483 tp->dev->dev_addr[4] +
2484 tp->dev->dev_addr[5]) &
2485 TX_BACKOFF_SEED_MASK;
2486 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2487}
2488
Michael Chanbc1c7562006-03-20 17:48:03 -08002489static int tg3_set_power_state(struct tg3 *tp, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
2491 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002492 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 /* Make sure register accesses (indirect or otherwise)
2495 * will function correctly.
2496 */
2497 pci_write_config_dword(tp->pdev,
2498 TG3PCI_MISC_HOST_CTRL,
2499 tp->misc_host_ctrl);
2500
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 switch (state) {
Michael Chanbc1c7562006-03-20 17:48:03 -08002502 case PCI_D0:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002503 pci_enable_wake(tp->pdev, state, false);
2504 pci_set_power_state(tp->pdev, PCI_D0);
Michael Chan8c6bda12005-04-21 17:09:08 -07002505
Michael Chan9d26e212006-12-07 00:21:14 -08002506 /* Switch out of Vaux if it is a NIC */
2507 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
Michael Chanb401e9e2005-12-19 16:27:04 -08002508 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
2510 return 0;
2511
Michael Chanbc1c7562006-03-20 17:48:03 -08002512 case PCI_D1:
Michael Chanbc1c7562006-03-20 17:48:03 -08002513 case PCI_D2:
Michael Chanbc1c7562006-03-20 17:48:03 -08002514 case PCI_D3hot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 break;
2516
2517 default:
Joe Perches05dbe002010-02-17 19:44:19 +00002518 netdev_err(tp->dev, "Invalid power state (D%d) requested\n",
2519 state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002521 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002522
2523 /* Restore the CLKREQ setting. */
2524 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2525 u16 lnkctl;
2526
2527 pci_read_config_word(tp->pdev,
2528 tp->pcie_cap + PCI_EXP_LNKCTL,
2529 &lnkctl);
2530 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2531 pci_write_config_word(tp->pdev,
2532 tp->pcie_cap + PCI_EXP_LNKCTL,
2533 lnkctl);
2534 }
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2537 tw32(TG3PCI_MISC_HOST_CTRL,
2538 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2539
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002540 device_should_wake = pci_pme_capable(tp->pdev, state) &&
2541 device_may_wakeup(&tp->pdev->dev) &&
2542 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2543
Matt Carlsondd477002008-05-25 23:45:58 -07002544 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002545 do_low_power = false;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002546 if ((tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) &&
2547 !tp->link_config.phy_is_low_power) {
2548 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002549 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002550
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002551 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002552
2553 tp->link_config.phy_is_low_power = 1;
2554
2555 tp->link_config.orig_speed = phydev->speed;
2556 tp->link_config.orig_duplex = phydev->duplex;
2557 tp->link_config.orig_autoneg = phydev->autoneg;
2558 tp->link_config.orig_advertising = phydev->advertising;
2559
2560 advertising = ADVERTISED_TP |
2561 ADVERTISED_Pause |
2562 ADVERTISED_Autoneg |
2563 ADVERTISED_10baseT_Half;
2564
2565 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002566 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002567 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2568 advertising |=
2569 ADVERTISED_100baseT_Half |
2570 ADVERTISED_100baseT_Full |
2571 ADVERTISED_10baseT_Full;
2572 else
2573 advertising |= ADVERTISED_10baseT_Full;
2574 }
2575
2576 phydev->advertising = advertising;
2577
2578 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002579
2580 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002581 if (phyid != PHY_ID_BCMAC131) {
2582 phyid &= PHY_BCM_OUI_MASK;
2583 if (phyid == PHY_BCM_OUI_1 ||
2584 phyid == PHY_BCM_OUI_2 ||
2585 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002586 do_low_power = true;
2587 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002588 }
Matt Carlsondd477002008-05-25 23:45:58 -07002589 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002590 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002591
Matt Carlsondd477002008-05-25 23:45:58 -07002592 if (tp->link_config.phy_is_low_power == 0) {
2593 tp->link_config.phy_is_low_power = 1;
2594 tp->link_config.orig_speed = tp->link_config.speed;
2595 tp->link_config.orig_duplex = tp->link_config.duplex;
2596 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Matt Carlsondd477002008-05-25 23:45:58 -07002599 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
2600 tp->link_config.speed = SPEED_10;
2601 tp->link_config.duplex = DUPLEX_HALF;
2602 tp->link_config.autoneg = AUTONEG_ENABLE;
2603 tg3_setup_phy(tp, 0);
2604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 }
2606
Michael Chanb5d37722006-09-27 16:06:21 -07002607 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2608 u32 val;
2609
2610 val = tr32(GRC_VCPU_EXT_CTRL);
2611 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2612 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002613 int i;
2614 u32 val;
2615
2616 for (i = 0; i < 200; i++) {
2617 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2618 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2619 break;
2620 msleep(1);
2621 }
2622 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002623 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2624 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2625 WOL_DRV_STATE_SHUTDOWN |
2626 WOL_DRV_WOL |
2627 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002628
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002629 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 u32 mac_mode;
2631
2632 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002633 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002634 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2635 udelay(40);
2636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Michael Chan3f7045c2006-09-27 16:02:29 -07002638 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
2639 mac_mode = MAC_MODE_PORT_MODE_GMII;
2640 else
2641 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002643 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2644 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2645 ASIC_REV_5700) {
2646 u32 speed = (tp->tg3_flags &
2647 TG3_FLAG_WOL_SPEED_100MB) ?
2648 SPEED_100 : SPEED_10;
2649 if (tg3_5700_link_polarity(tp, speed))
2650 mac_mode |= MAC_MODE_LINK_POLARITY;
2651 else
2652 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 } else {
2655 mac_mode = MAC_MODE_PORT_MODE_TBI;
2656 }
2657
John W. Linvillecbf46852005-04-21 17:01:29 -07002658 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 tw32(MAC_LED_CTRL, tp->led_ctrl);
2660
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002661 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2662 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2663 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2664 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2665 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2666 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Matt Carlson3bda1252008-08-15 14:08:22 -07002668 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
2669 mac_mode |= tp->mac_mode &
2670 (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
2671 if (mac_mode & MAC_MODE_APE_TX_EN)
2672 mac_mode |= MAC_MODE_TDE_ENABLE;
2673 }
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 tw32_f(MAC_MODE, mac_mode);
2676 udelay(100);
2677
2678 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2679 udelay(10);
2680 }
2681
2682 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2683 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2684 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2685 u32 base_val;
2686
2687 base_val = tp->pci_clock_ctrl;
2688 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2689 CLOCK_CTRL_TXCLK_DISABLE);
2690
Michael Chanb401e9e2005-12-19 16:27:04 -08002691 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2692 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002693 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002694 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002695 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002696 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002697 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2699 u32 newbits1, newbits2;
2700
2701 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2702 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2703 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2704 CLOCK_CTRL_TXCLK_DISABLE |
2705 CLOCK_CTRL_ALTCLK);
2706 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2707 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2708 newbits1 = CLOCK_CTRL_625_CORE;
2709 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2710 } else {
2711 newbits1 = CLOCK_CTRL_ALTCLK;
2712 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2713 }
2714
Michael Chanb401e9e2005-12-19 16:27:04 -08002715 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2716 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
Michael Chanb401e9e2005-12-19 16:27:04 -08002718 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2719 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
2721 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2722 u32 newbits3;
2723
2724 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2725 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2726 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2727 CLOCK_CTRL_TXCLK_DISABLE |
2728 CLOCK_CTRL_44MHZ_CORE);
2729 } else {
2730 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2731 }
2732
Michael Chanb401e9e2005-12-19 16:27:04 -08002733 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2734 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 }
2736 }
2737
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002738 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002739 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002740 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 tg3_frob_aux_power(tp);
2743
2744 /* Workaround for unstable PLL clock */
2745 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2746 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2747 u32 val = tr32(0x7d00);
2748
2749 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2750 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002751 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002752 int err;
2753
2754 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002756 if (!err)
2757 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 }
2760
Michael Chanbbadf502006-04-06 21:46:34 -07002761 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2762
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002763 if (device_should_wake)
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002764 pci_enable_wake(tp->pdev, state, true);
2765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 /* Finally, set the new power state. */
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002767 pci_set_power_state(tp->pdev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 return 0;
2770}
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2773{
2774 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2775 case MII_TG3_AUX_STAT_10HALF:
2776 *speed = SPEED_10;
2777 *duplex = DUPLEX_HALF;
2778 break;
2779
2780 case MII_TG3_AUX_STAT_10FULL:
2781 *speed = SPEED_10;
2782 *duplex = DUPLEX_FULL;
2783 break;
2784
2785 case MII_TG3_AUX_STAT_100HALF:
2786 *speed = SPEED_100;
2787 *duplex = DUPLEX_HALF;
2788 break;
2789
2790 case MII_TG3_AUX_STAT_100FULL:
2791 *speed = SPEED_100;
2792 *duplex = DUPLEX_FULL;
2793 break;
2794
2795 case MII_TG3_AUX_STAT_1000HALF:
2796 *speed = SPEED_1000;
2797 *duplex = DUPLEX_HALF;
2798 break;
2799
2800 case MII_TG3_AUX_STAT_1000FULL:
2801 *speed = SPEED_1000;
2802 *duplex = DUPLEX_FULL;
2803 break;
2804
2805 default:
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002806 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002807 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2808 SPEED_10;
2809 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2810 DUPLEX_HALF;
2811 break;
2812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 *speed = SPEED_INVALID;
2814 *duplex = DUPLEX_INVALID;
2815 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817}
2818
2819static void tg3_phy_copper_begin(struct tg3 *tp)
2820{
2821 u32 new_adv;
2822 int i;
2823
2824 if (tp->link_config.phy_is_low_power) {
2825 /* Entering low power mode. Disable gigabit and
2826 * 100baseT advertisements.
2827 */
2828 tg3_writephy(tp, MII_TG3_CTRL, 0);
2829
2830 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2831 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2832 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2833 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2834
2835 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2836 } else if (tp->link_config.speed == SPEED_INVALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
2838 tp->link_config.advertising &=
2839 ~(ADVERTISED_1000baseT_Half |
2840 ADVERTISED_1000baseT_Full);
2841
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002842 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2844 new_adv |= ADVERTISE_10HALF;
2845 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2846 new_adv |= ADVERTISE_10FULL;
2847 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2848 new_adv |= ADVERTISE_100HALF;
2849 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2850 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002851
2852 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2855
2856 if (tp->link_config.advertising &
2857 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2858 new_adv = 0;
2859 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2860 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2861 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2862 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
2863 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY) &&
2864 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2865 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2866 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2867 MII_TG3_CTRL_ENABLE_AS_MASTER);
2868 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2869 } else {
2870 tg3_writephy(tp, MII_TG3_CTRL, 0);
2871 }
2872 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002873 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2874 new_adv |= ADVERTISE_CSMA;
2875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 /* Asking for a specific link mode. */
2877 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2879
2880 if (tp->link_config.duplex == DUPLEX_FULL)
2881 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2882 else
2883 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2884 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2885 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2886 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2887 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (tp->link_config.speed == SPEED_100) {
2890 if (tp->link_config.duplex == DUPLEX_FULL)
2891 new_adv |= ADVERTISE_100FULL;
2892 else
2893 new_adv |= ADVERTISE_100HALF;
2894 } else {
2895 if (tp->link_config.duplex == DUPLEX_FULL)
2896 new_adv |= ADVERTISE_10FULL;
2897 else
2898 new_adv |= ADVERTISE_10HALF;
2899 }
2900 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002901
2902 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002904
2905 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 }
2907
2908 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
2909 tp->link_config.speed != SPEED_INVALID) {
2910 u32 bmcr, orig_bmcr;
2911
2912 tp->link_config.active_speed = tp->link_config.speed;
2913 tp->link_config.active_duplex = tp->link_config.duplex;
2914
2915 bmcr = 0;
2916 switch (tp->link_config.speed) {
2917 default:
2918 case SPEED_10:
2919 break;
2920
2921 case SPEED_100:
2922 bmcr |= BMCR_SPEED100;
2923 break;
2924
2925 case SPEED_1000:
2926 bmcr |= TG3_BMCR_SPEED1000;
2927 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
2930 if (tp->link_config.duplex == DUPLEX_FULL)
2931 bmcr |= BMCR_FULLDPLX;
2932
2933 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
2934 (bmcr != orig_bmcr)) {
2935 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
2936 for (i = 0; i < 1500; i++) {
2937 u32 tmp;
2938
2939 udelay(10);
2940 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
2941 tg3_readphy(tp, MII_BMSR, &tmp))
2942 continue;
2943 if (!(tmp & BMSR_LSTATUS)) {
2944 udelay(40);
2945 break;
2946 }
2947 }
2948 tg3_writephy(tp, MII_BMCR, bmcr);
2949 udelay(40);
2950 }
2951 } else {
2952 tg3_writephy(tp, MII_BMCR,
2953 BMCR_ANENABLE | BMCR_ANRESTART);
2954 }
2955}
2956
2957static int tg3_init_5401phy_dsp(struct tg3 *tp)
2958{
2959 int err;
2960
2961 /* Turn off tap power management. */
2962 /* Set Extended packet length bit */
2963 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
2964
2965 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0012);
2966 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1804);
2967
2968 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0013);
2969 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1204);
2970
2971 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2972 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0132);
2973
2974 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2975 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0232);
2976
2977 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2978 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0a20);
2979
2980 udelay(40);
2981
2982 return err;
2983}
2984
Michael Chan3600d912006-12-07 00:21:48 -08002985static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
Michael Chan3600d912006-12-07 00:21:48 -08002987 u32 adv_reg, all_mask = 0;
2988
2989 if (mask & ADVERTISED_10baseT_Half)
2990 all_mask |= ADVERTISE_10HALF;
2991 if (mask & ADVERTISED_10baseT_Full)
2992 all_mask |= ADVERTISE_10FULL;
2993 if (mask & ADVERTISED_100baseT_Half)
2994 all_mask |= ADVERTISE_100HALF;
2995 if (mask & ADVERTISED_100baseT_Full)
2996 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
2998 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
2999 return 0;
3000
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 if ((adv_reg & all_mask) != all_mask)
3002 return 0;
3003 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
3004 u32 tg3_ctrl;
3005
Michael Chan3600d912006-12-07 00:21:48 -08003006 all_mask = 0;
3007 if (mask & ADVERTISED_1000baseT_Half)
3008 all_mask |= ADVERTISE_1000HALF;
3009 if (mask & ADVERTISED_1000baseT_Full)
3010 all_mask |= ADVERTISE_1000FULL;
3011
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3013 return 0;
3014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 if ((tg3_ctrl & all_mask) != all_mask)
3016 return 0;
3017 }
3018 return 1;
3019}
3020
Matt Carlsonef167e22007-12-20 20:10:01 -08003021static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3022{
3023 u32 curadv, reqadv;
3024
3025 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3026 return 1;
3027
3028 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3029 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3030
3031 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3032 if (curadv != reqadv)
3033 return 0;
3034
3035 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3036 tg3_readphy(tp, MII_LPA, rmtadv);
3037 } else {
3038 /* Reprogram the advertisement register, even if it
3039 * does not affect the current link. If the link
3040 * gets renegotiated in the future, we can save an
3041 * additional renegotiation cycle by advertising
3042 * it correctly in the first place.
3043 */
3044 if (curadv != reqadv) {
3045 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3046 ADVERTISE_PAUSE_ASYM);
3047 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3048 }
3049 }
3050
3051 return 1;
3052}
3053
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3055{
3056 int current_link_up;
3057 u32 bmsr, dummy;
Matt Carlsonef167e22007-12-20 20:10:01 -08003058 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 u16 current_speed;
3060 u8 current_duplex;
3061 int i, err;
3062
3063 tw32(MAC_EVENT, 0);
3064
3065 tw32_f(MAC_STATUS,
3066 (MAC_STATUS_SYNC_CHANGED |
3067 MAC_STATUS_CFG_CHANGED |
3068 MAC_STATUS_MI_COMPLETION |
3069 MAC_STATUS_LNKSTATE_CHANGED));
3070 udelay(40);
3071
Matt Carlson8ef21422008-05-02 16:47:53 -07003072 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3073 tw32_f(MAC_MI_MODE,
3074 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3075 udelay(80);
3076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
3078 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3079
3080 /* Some third-party PHYs need to be reset on link going
3081 * down.
3082 */
3083 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3084 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3085 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3086 netif_carrier_ok(tp->dev)) {
3087 tg3_readphy(tp, MII_BMSR, &bmsr);
3088 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3089 !(bmsr & BMSR_LSTATUS))
3090 force_reset = 1;
3091 }
3092 if (force_reset)
3093 tg3_phy_reset(tp);
3094
Matt Carlson79eb6902010-02-17 15:17:03 +00003095 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 tg3_readphy(tp, MII_BMSR, &bmsr);
3097 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3098 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3099 bmsr = 0;
3100
3101 if (!(bmsr & BMSR_LSTATUS)) {
3102 err = tg3_init_5401phy_dsp(tp);
3103 if (err)
3104 return err;
3105
3106 tg3_readphy(tp, MII_BMSR, &bmsr);
3107 for (i = 0; i < 1000; i++) {
3108 udelay(10);
3109 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3110 (bmsr & BMSR_LSTATUS)) {
3111 udelay(40);
3112 break;
3113 }
3114 }
3115
Matt Carlson79eb6902010-02-17 15:17:03 +00003116 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3117 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 !(bmsr & BMSR_LSTATUS) &&
3119 tp->link_config.active_speed == SPEED_1000) {
3120 err = tg3_phy_reset(tp);
3121 if (!err)
3122 err = tg3_init_5401phy_dsp(tp);
3123 if (err)
3124 return err;
3125 }
3126 }
3127 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3128 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3129 /* 5701 {A0,B0} CRC bug workaround */
3130 tg3_writephy(tp, 0x15, 0x0a75);
3131 tg3_writephy(tp, 0x1c, 0x8c68);
3132 tg3_writephy(tp, 0x1c, 0x8d68);
3133 tg3_writephy(tp, 0x1c, 0x8c68);
3134 }
3135
3136 /* Clear pending interrupts... */
3137 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3138 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3139
3140 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT)
3141 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003142 else if (!(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3144
3145 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3146 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3147 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3148 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3149 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3150 else
3151 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3152 }
3153
3154 current_link_up = 0;
3155 current_speed = SPEED_INVALID;
3156 current_duplex = DUPLEX_INVALID;
3157
3158 if (tp->tg3_flags2 & TG3_FLG2_CAPACITIVE_COUPLING) {
3159 u32 val;
3160
3161 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3162 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3163 if (!(val & (1 << 10))) {
3164 val |= (1 << 10);
3165 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3166 goto relink;
3167 }
3168 }
3169
3170 bmsr = 0;
3171 for (i = 0; i < 100; i++) {
3172 tg3_readphy(tp, MII_BMSR, &bmsr);
3173 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3174 (bmsr & BMSR_LSTATUS))
3175 break;
3176 udelay(40);
3177 }
3178
3179 if (bmsr & BMSR_LSTATUS) {
3180 u32 aux_stat, bmcr;
3181
3182 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3183 for (i = 0; i < 2000; i++) {
3184 udelay(10);
3185 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3186 aux_stat)
3187 break;
3188 }
3189
3190 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3191 &current_speed,
3192 &current_duplex);
3193
3194 bmcr = 0;
3195 for (i = 0; i < 200; i++) {
3196 tg3_readphy(tp, MII_BMCR, &bmcr);
3197 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3198 continue;
3199 if (bmcr && bmcr != 0x7fff)
3200 break;
3201 udelay(10);
3202 }
3203
Matt Carlsonef167e22007-12-20 20:10:01 -08003204 lcl_adv = 0;
3205 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Matt Carlsonef167e22007-12-20 20:10:01 -08003207 tp->link_config.active_speed = current_speed;
3208 tp->link_config.active_duplex = current_duplex;
3209
3210 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3211 if ((bmcr & BMCR_ANENABLE) &&
3212 tg3_copper_is_advertising_all(tp,
3213 tp->link_config.advertising)) {
3214 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3215 &rmt_adv))
3216 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 }
3218 } else {
3219 if (!(bmcr & BMCR_ANENABLE) &&
3220 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003221 tp->link_config.duplex == current_duplex &&
3222 tp->link_config.flowctrl ==
3223 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 }
3226 }
3227
Matt Carlsonef167e22007-12-20 20:10:01 -08003228 if (current_link_up == 1 &&
3229 tp->link_config.active_duplex == DUPLEX_FULL)
3230 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 }
3232
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233relink:
Michael Chan6921d202005-12-13 21:15:53 -08003234 if (current_link_up == 0 || tp->link_config.phy_is_low_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 u32 tmp;
3236
3237 tg3_phy_copper_begin(tp);
3238
3239 tg3_readphy(tp, MII_BMSR, &tmp);
3240 if (!tg3_readphy(tp, MII_BMSR, &tmp) &&
3241 (tmp & BMSR_LSTATUS))
3242 current_link_up = 1;
3243 }
3244
3245 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3246 if (current_link_up == 1) {
3247 if (tp->link_config.active_speed == SPEED_100 ||
3248 tp->link_config.active_speed == SPEED_10)
3249 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3250 else
3251 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003252 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)
3253 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3254 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3256
3257 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3258 if (tp->link_config.active_duplex == DUPLEX_HALF)
3259 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3260
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003262 if (current_link_up == 1 &&
3263 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003265 else
3266 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 }
3268
3269 /* ??? Without this setting Netgear GA302T PHY does not
3270 * ??? send/receive packets...
3271 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003272 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3274 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3275 tw32_f(MAC_MI_MODE, tp->mi_mode);
3276 udelay(80);
3277 }
3278
3279 tw32_f(MAC_MODE, tp->mac_mode);
3280 udelay(40);
3281
3282 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3283 /* Polled via timer. */
3284 tw32_f(MAC_EVENT, 0);
3285 } else {
3286 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3287 }
3288 udelay(40);
3289
3290 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3291 current_link_up == 1 &&
3292 tp->link_config.active_speed == SPEED_1000 &&
3293 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3294 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3295 udelay(120);
3296 tw32_f(MAC_STATUS,
3297 (MAC_STATUS_SYNC_CHANGED |
3298 MAC_STATUS_CFG_CHANGED));
3299 udelay(40);
3300 tg3_write_mem(tp,
3301 NIC_SRAM_FIRMWARE_MBOX,
3302 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3303 }
3304
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003305 /* Prevent send BD corruption. */
3306 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3307 u16 oldlnkctl, newlnkctl;
3308
3309 pci_read_config_word(tp->pdev,
3310 tp->pcie_cap + PCI_EXP_LNKCTL,
3311 &oldlnkctl);
3312 if (tp->link_config.active_speed == SPEED_100 ||
3313 tp->link_config.active_speed == SPEED_10)
3314 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3315 else
3316 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3317 if (newlnkctl != oldlnkctl)
3318 pci_write_config_word(tp->pdev,
3319 tp->pcie_cap + PCI_EXP_LNKCTL,
3320 newlnkctl);
3321 }
3322
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 if (current_link_up != netif_carrier_ok(tp->dev)) {
3324 if (current_link_up)
3325 netif_carrier_on(tp->dev);
3326 else
3327 netif_carrier_off(tp->dev);
3328 tg3_link_report(tp);
3329 }
3330
3331 return 0;
3332}
3333
3334struct tg3_fiber_aneginfo {
3335 int state;
3336#define ANEG_STATE_UNKNOWN 0
3337#define ANEG_STATE_AN_ENABLE 1
3338#define ANEG_STATE_RESTART_INIT 2
3339#define ANEG_STATE_RESTART 3
3340#define ANEG_STATE_DISABLE_LINK_OK 4
3341#define ANEG_STATE_ABILITY_DETECT_INIT 5
3342#define ANEG_STATE_ABILITY_DETECT 6
3343#define ANEG_STATE_ACK_DETECT_INIT 7
3344#define ANEG_STATE_ACK_DETECT 8
3345#define ANEG_STATE_COMPLETE_ACK_INIT 9
3346#define ANEG_STATE_COMPLETE_ACK 10
3347#define ANEG_STATE_IDLE_DETECT_INIT 11
3348#define ANEG_STATE_IDLE_DETECT 12
3349#define ANEG_STATE_LINK_OK 13
3350#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3351#define ANEG_STATE_NEXT_PAGE_WAIT 15
3352
3353 u32 flags;
3354#define MR_AN_ENABLE 0x00000001
3355#define MR_RESTART_AN 0x00000002
3356#define MR_AN_COMPLETE 0x00000004
3357#define MR_PAGE_RX 0x00000008
3358#define MR_NP_LOADED 0x00000010
3359#define MR_TOGGLE_TX 0x00000020
3360#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3361#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3362#define MR_LP_ADV_SYM_PAUSE 0x00000100
3363#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3364#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3365#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3366#define MR_LP_ADV_NEXT_PAGE 0x00001000
3367#define MR_TOGGLE_RX 0x00002000
3368#define MR_NP_RX 0x00004000
3369
3370#define MR_LINK_OK 0x80000000
3371
3372 unsigned long link_time, cur_time;
3373
3374 u32 ability_match_cfg;
3375 int ability_match_count;
3376
3377 char ability_match, idle_match, ack_match;
3378
3379 u32 txconfig, rxconfig;
3380#define ANEG_CFG_NP 0x00000080
3381#define ANEG_CFG_ACK 0x00000040
3382#define ANEG_CFG_RF2 0x00000020
3383#define ANEG_CFG_RF1 0x00000010
3384#define ANEG_CFG_PS2 0x00000001
3385#define ANEG_CFG_PS1 0x00008000
3386#define ANEG_CFG_HD 0x00004000
3387#define ANEG_CFG_FD 0x00002000
3388#define ANEG_CFG_INVAL 0x00001f06
3389
3390};
3391#define ANEG_OK 0
3392#define ANEG_DONE 1
3393#define ANEG_TIMER_ENAB 2
3394#define ANEG_FAILED -1
3395
3396#define ANEG_STATE_SETTLE_TIME 10000
3397
3398static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3399 struct tg3_fiber_aneginfo *ap)
3400{
Matt Carlson5be73b42007-12-20 20:09:29 -08003401 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 unsigned long delta;
3403 u32 rx_cfg_reg;
3404 int ret;
3405
3406 if (ap->state == ANEG_STATE_UNKNOWN) {
3407 ap->rxconfig = 0;
3408 ap->link_time = 0;
3409 ap->cur_time = 0;
3410 ap->ability_match_cfg = 0;
3411 ap->ability_match_count = 0;
3412 ap->ability_match = 0;
3413 ap->idle_match = 0;
3414 ap->ack_match = 0;
3415 }
3416 ap->cur_time++;
3417
3418 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3419 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3420
3421 if (rx_cfg_reg != ap->ability_match_cfg) {
3422 ap->ability_match_cfg = rx_cfg_reg;
3423 ap->ability_match = 0;
3424 ap->ability_match_count = 0;
3425 } else {
3426 if (++ap->ability_match_count > 1) {
3427 ap->ability_match = 1;
3428 ap->ability_match_cfg = rx_cfg_reg;
3429 }
3430 }
3431 if (rx_cfg_reg & ANEG_CFG_ACK)
3432 ap->ack_match = 1;
3433 else
3434 ap->ack_match = 0;
3435
3436 ap->idle_match = 0;
3437 } else {
3438 ap->idle_match = 1;
3439 ap->ability_match_cfg = 0;
3440 ap->ability_match_count = 0;
3441 ap->ability_match = 0;
3442 ap->ack_match = 0;
3443
3444 rx_cfg_reg = 0;
3445 }
3446
3447 ap->rxconfig = rx_cfg_reg;
3448 ret = ANEG_OK;
3449
Matt Carlson33f401a2010-04-05 10:19:27 +00003450 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 case ANEG_STATE_UNKNOWN:
3452 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3453 ap->state = ANEG_STATE_AN_ENABLE;
3454
3455 /* fallthru */
3456 case ANEG_STATE_AN_ENABLE:
3457 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3458 if (ap->flags & MR_AN_ENABLE) {
3459 ap->link_time = 0;
3460 ap->cur_time = 0;
3461 ap->ability_match_cfg = 0;
3462 ap->ability_match_count = 0;
3463 ap->ability_match = 0;
3464 ap->idle_match = 0;
3465 ap->ack_match = 0;
3466
3467 ap->state = ANEG_STATE_RESTART_INIT;
3468 } else {
3469 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3470 }
3471 break;
3472
3473 case ANEG_STATE_RESTART_INIT:
3474 ap->link_time = ap->cur_time;
3475 ap->flags &= ~(MR_NP_LOADED);
3476 ap->txconfig = 0;
3477 tw32(MAC_TX_AUTO_NEG, 0);
3478 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3479 tw32_f(MAC_MODE, tp->mac_mode);
3480 udelay(40);
3481
3482 ret = ANEG_TIMER_ENAB;
3483 ap->state = ANEG_STATE_RESTART;
3484
3485 /* fallthru */
3486 case ANEG_STATE_RESTART:
3487 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00003488 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00003490 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 break;
3493
3494 case ANEG_STATE_DISABLE_LINK_OK:
3495 ret = ANEG_DONE;
3496 break;
3497
3498 case ANEG_STATE_ABILITY_DETECT_INIT:
3499 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003500 ap->txconfig = ANEG_CFG_FD;
3501 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3502 if (flowctrl & ADVERTISE_1000XPAUSE)
3503 ap->txconfig |= ANEG_CFG_PS1;
3504 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3505 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3507 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3508 tw32_f(MAC_MODE, tp->mac_mode);
3509 udelay(40);
3510
3511 ap->state = ANEG_STATE_ABILITY_DETECT;
3512 break;
3513
3514 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00003515 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 break;
3518
3519 case ANEG_STATE_ACK_DETECT_INIT:
3520 ap->txconfig |= ANEG_CFG_ACK;
3521 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3522 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3523 tw32_f(MAC_MODE, tp->mac_mode);
3524 udelay(40);
3525
3526 ap->state = ANEG_STATE_ACK_DETECT;
3527
3528 /* fallthru */
3529 case ANEG_STATE_ACK_DETECT:
3530 if (ap->ack_match != 0) {
3531 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3532 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3533 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3534 } else {
3535 ap->state = ANEG_STATE_AN_ENABLE;
3536 }
3537 } else if (ap->ability_match != 0 &&
3538 ap->rxconfig == 0) {
3539 ap->state = ANEG_STATE_AN_ENABLE;
3540 }
3541 break;
3542
3543 case ANEG_STATE_COMPLETE_ACK_INIT:
3544 if (ap->rxconfig & ANEG_CFG_INVAL) {
3545 ret = ANEG_FAILED;
3546 break;
3547 }
3548 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3549 MR_LP_ADV_HALF_DUPLEX |
3550 MR_LP_ADV_SYM_PAUSE |
3551 MR_LP_ADV_ASYM_PAUSE |
3552 MR_LP_ADV_REMOTE_FAULT1 |
3553 MR_LP_ADV_REMOTE_FAULT2 |
3554 MR_LP_ADV_NEXT_PAGE |
3555 MR_TOGGLE_RX |
3556 MR_NP_RX);
3557 if (ap->rxconfig & ANEG_CFG_FD)
3558 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3559 if (ap->rxconfig & ANEG_CFG_HD)
3560 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3561 if (ap->rxconfig & ANEG_CFG_PS1)
3562 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3563 if (ap->rxconfig & ANEG_CFG_PS2)
3564 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3565 if (ap->rxconfig & ANEG_CFG_RF1)
3566 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3567 if (ap->rxconfig & ANEG_CFG_RF2)
3568 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3569 if (ap->rxconfig & ANEG_CFG_NP)
3570 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3571
3572 ap->link_time = ap->cur_time;
3573
3574 ap->flags ^= (MR_TOGGLE_TX);
3575 if (ap->rxconfig & 0x0008)
3576 ap->flags |= MR_TOGGLE_RX;
3577 if (ap->rxconfig & ANEG_CFG_NP)
3578 ap->flags |= MR_NP_RX;
3579 ap->flags |= MR_PAGE_RX;
3580
3581 ap->state = ANEG_STATE_COMPLETE_ACK;
3582 ret = ANEG_TIMER_ENAB;
3583 break;
3584
3585 case ANEG_STATE_COMPLETE_ACK:
3586 if (ap->ability_match != 0 &&
3587 ap->rxconfig == 0) {
3588 ap->state = ANEG_STATE_AN_ENABLE;
3589 break;
3590 }
3591 delta = ap->cur_time - ap->link_time;
3592 if (delta > ANEG_STATE_SETTLE_TIME) {
3593 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3594 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3595 } else {
3596 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3597 !(ap->flags & MR_NP_RX)) {
3598 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3599 } else {
3600 ret = ANEG_FAILED;
3601 }
3602 }
3603 }
3604 break;
3605
3606 case ANEG_STATE_IDLE_DETECT_INIT:
3607 ap->link_time = ap->cur_time;
3608 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3609 tw32_f(MAC_MODE, tp->mac_mode);
3610 udelay(40);
3611
3612 ap->state = ANEG_STATE_IDLE_DETECT;
3613 ret = ANEG_TIMER_ENAB;
3614 break;
3615
3616 case ANEG_STATE_IDLE_DETECT:
3617 if (ap->ability_match != 0 &&
3618 ap->rxconfig == 0) {
3619 ap->state = ANEG_STATE_AN_ENABLE;
3620 break;
3621 }
3622 delta = ap->cur_time - ap->link_time;
3623 if (delta > ANEG_STATE_SETTLE_TIME) {
3624 /* XXX another gem from the Broadcom driver :( */
3625 ap->state = ANEG_STATE_LINK_OK;
3626 }
3627 break;
3628
3629 case ANEG_STATE_LINK_OK:
3630 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3631 ret = ANEG_DONE;
3632 break;
3633
3634 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3635 /* ??? unimplemented */
3636 break;
3637
3638 case ANEG_STATE_NEXT_PAGE_WAIT:
3639 /* ??? unimplemented */
3640 break;
3641
3642 default:
3643 ret = ANEG_FAILED;
3644 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
3647 return ret;
3648}
3649
Matt Carlson5be73b42007-12-20 20:09:29 -08003650static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651{
3652 int res = 0;
3653 struct tg3_fiber_aneginfo aninfo;
3654 int status = ANEG_FAILED;
3655 unsigned int tick;
3656 u32 tmp;
3657
3658 tw32_f(MAC_TX_AUTO_NEG, 0);
3659
3660 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3661 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3662 udelay(40);
3663
3664 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3665 udelay(40);
3666
3667 memset(&aninfo, 0, sizeof(aninfo));
3668 aninfo.flags |= MR_AN_ENABLE;
3669 aninfo.state = ANEG_STATE_UNKNOWN;
3670 aninfo.cur_time = 0;
3671 tick = 0;
3672 while (++tick < 195000) {
3673 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3674 if (status == ANEG_DONE || status == ANEG_FAILED)
3675 break;
3676
3677 udelay(1);
3678 }
3679
3680 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3681 tw32_f(MAC_MODE, tp->mac_mode);
3682 udelay(40);
3683
Matt Carlson5be73b42007-12-20 20:09:29 -08003684 *txflags = aninfo.txconfig;
3685 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686
3687 if (status == ANEG_DONE &&
3688 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3689 MR_LP_ADV_FULL_DUPLEX)))
3690 res = 1;
3691
3692 return res;
3693}
3694
3695static void tg3_init_bcm8002(struct tg3 *tp)
3696{
3697 u32 mac_status = tr32(MAC_STATUS);
3698 int i;
3699
3700 /* Reset when initting first time or we have a link. */
3701 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3702 !(mac_status & MAC_STATUS_PCS_SYNCED))
3703 return;
3704
3705 /* Set PLL lock range. */
3706 tg3_writephy(tp, 0x16, 0x8007);
3707
3708 /* SW reset */
3709 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3710
3711 /* Wait for reset to complete. */
3712 /* XXX schedule_timeout() ... */
3713 for (i = 0; i < 500; i++)
3714 udelay(10);
3715
3716 /* Config mode; select PMA/Ch 1 regs. */
3717 tg3_writephy(tp, 0x10, 0x8411);
3718
3719 /* Enable auto-lock and comdet, select txclk for tx. */
3720 tg3_writephy(tp, 0x11, 0x0a10);
3721
3722 tg3_writephy(tp, 0x18, 0x00a0);
3723 tg3_writephy(tp, 0x16, 0x41ff);
3724
3725 /* Assert and deassert POR. */
3726 tg3_writephy(tp, 0x13, 0x0400);
3727 udelay(40);
3728 tg3_writephy(tp, 0x13, 0x0000);
3729
3730 tg3_writephy(tp, 0x11, 0x0a50);
3731 udelay(40);
3732 tg3_writephy(tp, 0x11, 0x0a10);
3733
3734 /* Wait for signal to stabilize */
3735 /* XXX schedule_timeout() ... */
3736 for (i = 0; i < 15000; i++)
3737 udelay(10);
3738
3739 /* Deselect the channel register so we can read the PHYID
3740 * later.
3741 */
3742 tg3_writephy(tp, 0x10, 0x8011);
3743}
3744
3745static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3746{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003747 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 u32 sg_dig_ctrl, sg_dig_status;
3749 u32 serdes_cfg, expected_sg_dig_ctrl;
3750 int workaround, port_a;
3751 int current_link_up;
3752
3753 serdes_cfg = 0;
3754 expected_sg_dig_ctrl = 0;
3755 workaround = 0;
3756 port_a = 1;
3757 current_link_up = 0;
3758
3759 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3760 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3761 workaround = 1;
3762 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3763 port_a = 0;
3764
3765 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3766 /* preserve bits 20-23 for voltage regulator */
3767 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3768 }
3769
3770 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3771
3772 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003773 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 if (workaround) {
3775 u32 val = serdes_cfg;
3776
3777 if (port_a)
3778 val |= 0xc010000;
3779 else
3780 val |= 0x4010000;
3781 tw32_f(MAC_SERDES_CFG, val);
3782 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003783
3784 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 }
3786 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3787 tg3_setup_flow_control(tp, 0, 0);
3788 current_link_up = 1;
3789 }
3790 goto out;
3791 }
3792
3793 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003794 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
Matt Carlson82cd3d12007-12-20 20:09:00 -08003796 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3797 if (flowctrl & ADVERTISE_1000XPAUSE)
3798 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3799 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3800 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
3802 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003803 if ((tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT) &&
3804 tp->serdes_counter &&
3805 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3806 MAC_STATUS_RCVD_CFG)) ==
3807 MAC_STATUS_PCS_SYNCED)) {
3808 tp->serdes_counter--;
3809 current_link_up = 1;
3810 goto out;
3811 }
3812restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 if (workaround)
3814 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003815 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 udelay(5);
3817 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3818
Michael Chan3d3ebe72006-09-27 15:59:15 -07003819 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3820 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3822 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003823 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 mac_status = tr32(MAC_STATUS);
3825
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003826 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003828 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
Matt Carlson82cd3d12007-12-20 20:09:00 -08003830 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3831 local_adv |= ADVERTISE_1000XPAUSE;
3832 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3833 local_adv |= ADVERTISE_1000XPSE_ASYM;
3834
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003835 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003836 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003837 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003838 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
3840 tg3_setup_flow_control(tp, local_adv, remote_adv);
3841 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003842 tp->serdes_counter = 0;
3843 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003844 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003845 if (tp->serdes_counter)
3846 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 else {
3848 if (workaround) {
3849 u32 val = serdes_cfg;
3850
3851 if (port_a)
3852 val |= 0xc010000;
3853 else
3854 val |= 0x4010000;
3855
3856 tw32_f(MAC_SERDES_CFG, val);
3857 }
3858
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003859 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 udelay(40);
3861
3862 /* Link parallel detection - link is up */
3863 /* only if we have PCS_SYNC and not */
3864 /* receiving config code words */
3865 mac_status = tr32(MAC_STATUS);
3866 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3867 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3868 tg3_setup_flow_control(tp, 0, 0);
3869 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003870 tp->tg3_flags2 |=
3871 TG3_FLG2_PARALLEL_DETECT;
3872 tp->serdes_counter =
3873 SERDES_PARALLEL_DET_TIMEOUT;
3874 } else
3875 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 }
3877 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003878 } else {
3879 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3880 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 }
3882
3883out:
3884 return current_link_up;
3885}
3886
3887static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3888{
3889 int current_link_up = 0;
3890
Michael Chan5cf64b8a2007-05-05 12:11:21 -07003891 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
3894 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003895 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003897
Matt Carlson5be73b42007-12-20 20:09:29 -08003898 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3899 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
Matt Carlson5be73b42007-12-20 20:09:29 -08003901 if (txflags & ANEG_CFG_PS1)
3902 local_adv |= ADVERTISE_1000XPAUSE;
3903 if (txflags & ANEG_CFG_PS2)
3904 local_adv |= ADVERTISE_1000XPSE_ASYM;
3905
3906 if (rxflags & MR_LP_ADV_SYM_PAUSE)
3907 remote_adv |= LPA_1000XPAUSE;
3908 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3909 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
3911 tg3_setup_flow_control(tp, local_adv, remote_adv);
3912
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 current_link_up = 1;
3914 }
3915 for (i = 0; i < 30; i++) {
3916 udelay(20);
3917 tw32_f(MAC_STATUS,
3918 (MAC_STATUS_SYNC_CHANGED |
3919 MAC_STATUS_CFG_CHANGED));
3920 udelay(40);
3921 if ((tr32(MAC_STATUS) &
3922 (MAC_STATUS_SYNC_CHANGED |
3923 MAC_STATUS_CFG_CHANGED)) == 0)
3924 break;
3925 }
3926
3927 mac_status = tr32(MAC_STATUS);
3928 if (current_link_up == 0 &&
3929 (mac_status & MAC_STATUS_PCS_SYNCED) &&
3930 !(mac_status & MAC_STATUS_RCVD_CFG))
3931 current_link_up = 1;
3932 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08003933 tg3_setup_flow_control(tp, 0, 0);
3934
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 /* Forcing 1000FD link up. */
3936 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
3938 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
3939 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003940
3941 tw32_f(MAC_MODE, tp->mac_mode);
3942 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 }
3944
3945out:
3946 return current_link_up;
3947}
3948
3949static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
3950{
3951 u32 orig_pause_cfg;
3952 u16 orig_active_speed;
3953 u8 orig_active_duplex;
3954 u32 mac_status;
3955 int current_link_up;
3956 int i;
3957
Matt Carlson8d018622007-12-20 20:05:44 -08003958 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 orig_active_speed = tp->link_config.active_speed;
3960 orig_active_duplex = tp->link_config.active_duplex;
3961
3962 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
3963 netif_carrier_ok(tp->dev) &&
3964 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
3965 mac_status = tr32(MAC_STATUS);
3966 mac_status &= (MAC_STATUS_PCS_SYNCED |
3967 MAC_STATUS_SIGNAL_DET |
3968 MAC_STATUS_CFG_CHANGED |
3969 MAC_STATUS_RCVD_CFG);
3970 if (mac_status == (MAC_STATUS_PCS_SYNCED |
3971 MAC_STATUS_SIGNAL_DET)) {
3972 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3973 MAC_STATUS_CFG_CHANGED));
3974 return 0;
3975 }
3976 }
3977
3978 tw32_f(MAC_TX_AUTO_NEG, 0);
3979
3980 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
3981 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
3982 tw32_f(MAC_MODE, tp->mac_mode);
3983 udelay(40);
3984
Matt Carlson79eb6902010-02-17 15:17:03 +00003985 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 tg3_init_bcm8002(tp);
3987
3988 /* Enable link change event even when serdes polling. */
3989 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3990 udelay(40);
3991
3992 current_link_up = 0;
3993 mac_status = tr32(MAC_STATUS);
3994
3995 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
3996 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
3997 else
3998 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
3999
Matt Carlson898a56f2009-08-28 14:02:40 +00004000 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004002 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
4004 for (i = 0; i < 100; i++) {
4005 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4006 MAC_STATUS_CFG_CHANGED));
4007 udelay(5);
4008 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004009 MAC_STATUS_CFG_CHANGED |
4010 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 break;
4012 }
4013
4014 mac_status = tr32(MAC_STATUS);
4015 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4016 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004017 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4018 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 tw32_f(MAC_MODE, (tp->mac_mode |
4020 MAC_MODE_SEND_CONFIGS));
4021 udelay(1);
4022 tw32_f(MAC_MODE, tp->mac_mode);
4023 }
4024 }
4025
4026 if (current_link_up == 1) {
4027 tp->link_config.active_speed = SPEED_1000;
4028 tp->link_config.active_duplex = DUPLEX_FULL;
4029 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4030 LED_CTRL_LNKLED_OVERRIDE |
4031 LED_CTRL_1000MBPS_ON));
4032 } else {
4033 tp->link_config.active_speed = SPEED_INVALID;
4034 tp->link_config.active_duplex = DUPLEX_INVALID;
4035 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4036 LED_CTRL_LNKLED_OVERRIDE |
4037 LED_CTRL_TRAFFIC_OVERRIDE));
4038 }
4039
4040 if (current_link_up != netif_carrier_ok(tp->dev)) {
4041 if (current_link_up)
4042 netif_carrier_on(tp->dev);
4043 else
4044 netif_carrier_off(tp->dev);
4045 tg3_link_report(tp);
4046 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004047 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 if (orig_pause_cfg != now_pause_cfg ||
4049 orig_active_speed != tp->link_config.active_speed ||
4050 orig_active_duplex != tp->link_config.active_duplex)
4051 tg3_link_report(tp);
4052 }
4053
4054 return 0;
4055}
4056
Michael Chan747e8f82005-07-25 12:33:22 -07004057static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4058{
4059 int current_link_up, err = 0;
4060 u32 bmsr, bmcr;
4061 u16 current_speed;
4062 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004063 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004064
4065 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4066 tw32_f(MAC_MODE, tp->mac_mode);
4067 udelay(40);
4068
4069 tw32(MAC_EVENT, 0);
4070
4071 tw32_f(MAC_STATUS,
4072 (MAC_STATUS_SYNC_CHANGED |
4073 MAC_STATUS_CFG_CHANGED |
4074 MAC_STATUS_MI_COMPLETION |
4075 MAC_STATUS_LNKSTATE_CHANGED));
4076 udelay(40);
4077
4078 if (force_reset)
4079 tg3_phy_reset(tp);
4080
4081 current_link_up = 0;
4082 current_speed = SPEED_INVALID;
4083 current_duplex = DUPLEX_INVALID;
4084
4085 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4086 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004087 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4088 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4089 bmsr |= BMSR_LSTATUS;
4090 else
4091 bmsr &= ~BMSR_LSTATUS;
4092 }
Michael Chan747e8f82005-07-25 12:33:22 -07004093
4094 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4095
4096 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlson2bd3ed02008-06-09 15:39:55 -07004097 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004098 /* do nothing, just check for link up at the end */
4099 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4100 u32 adv, new_adv;
4101
4102 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4103 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4104 ADVERTISE_1000XPAUSE |
4105 ADVERTISE_1000XPSE_ASYM |
4106 ADVERTISE_SLCT);
4107
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004108 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004109
4110 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4111 new_adv |= ADVERTISE_1000XHALF;
4112 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4113 new_adv |= ADVERTISE_1000XFULL;
4114
4115 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4116 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4117 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4118 tg3_writephy(tp, MII_BMCR, bmcr);
4119
4120 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004121 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Michael Chan747e8f82005-07-25 12:33:22 -07004122 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4123
4124 return err;
4125 }
4126 } else {
4127 u32 new_bmcr;
4128
4129 bmcr &= ~BMCR_SPEED1000;
4130 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4131
4132 if (tp->link_config.duplex == DUPLEX_FULL)
4133 new_bmcr |= BMCR_FULLDPLX;
4134
4135 if (new_bmcr != bmcr) {
4136 /* BMCR_SPEED1000 is a reserved bit that needs
4137 * to be set on write.
4138 */
4139 new_bmcr |= BMCR_SPEED1000;
4140
4141 /* Force a linkdown */
4142 if (netif_carrier_ok(tp->dev)) {
4143 u32 adv;
4144
4145 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4146 adv &= ~(ADVERTISE_1000XFULL |
4147 ADVERTISE_1000XHALF |
4148 ADVERTISE_SLCT);
4149 tg3_writephy(tp, MII_ADVERTISE, adv);
4150 tg3_writephy(tp, MII_BMCR, bmcr |
4151 BMCR_ANRESTART |
4152 BMCR_ANENABLE);
4153 udelay(10);
4154 netif_carrier_off(tp->dev);
4155 }
4156 tg3_writephy(tp, MII_BMCR, new_bmcr);
4157 bmcr = new_bmcr;
4158 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4159 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004160 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4161 ASIC_REV_5714) {
4162 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4163 bmsr |= BMSR_LSTATUS;
4164 else
4165 bmsr &= ~BMSR_LSTATUS;
4166 }
Michael Chan747e8f82005-07-25 12:33:22 -07004167 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4168 }
4169 }
4170
4171 if (bmsr & BMSR_LSTATUS) {
4172 current_speed = SPEED_1000;
4173 current_link_up = 1;
4174 if (bmcr & BMCR_FULLDPLX)
4175 current_duplex = DUPLEX_FULL;
4176 else
4177 current_duplex = DUPLEX_HALF;
4178
Matt Carlsonef167e22007-12-20 20:10:01 -08004179 local_adv = 0;
4180 remote_adv = 0;
4181
Michael Chan747e8f82005-07-25 12:33:22 -07004182 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004183 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004184
4185 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4186 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4187 common = local_adv & remote_adv;
4188 if (common & (ADVERTISE_1000XHALF |
4189 ADVERTISE_1000XFULL)) {
4190 if (common & ADVERTISE_1000XFULL)
4191 current_duplex = DUPLEX_FULL;
4192 else
4193 current_duplex = DUPLEX_HALF;
Matt Carlson859a588792010-04-05 10:19:28 +00004194 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004195 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004196 }
Michael Chan747e8f82005-07-25 12:33:22 -07004197 }
4198 }
4199
Matt Carlsonef167e22007-12-20 20:10:01 -08004200 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4201 tg3_setup_flow_control(tp, local_adv, remote_adv);
4202
Michael Chan747e8f82005-07-25 12:33:22 -07004203 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4204 if (tp->link_config.active_duplex == DUPLEX_HALF)
4205 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4206
4207 tw32_f(MAC_MODE, tp->mac_mode);
4208 udelay(40);
4209
4210 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4211
4212 tp->link_config.active_speed = current_speed;
4213 tp->link_config.active_duplex = current_duplex;
4214
4215 if (current_link_up != netif_carrier_ok(tp->dev)) {
4216 if (current_link_up)
4217 netif_carrier_on(tp->dev);
4218 else {
4219 netif_carrier_off(tp->dev);
4220 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4221 }
4222 tg3_link_report(tp);
4223 }
4224 return err;
4225}
4226
4227static void tg3_serdes_parallel_detect(struct tg3 *tp)
4228{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004229 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004230 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004231 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004232 return;
4233 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004234
Michael Chan747e8f82005-07-25 12:33:22 -07004235 if (!netif_carrier_ok(tp->dev) &&
4236 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4237 u32 bmcr;
4238
4239 tg3_readphy(tp, MII_BMCR, &bmcr);
4240 if (bmcr & BMCR_ANENABLE) {
4241 u32 phy1, phy2;
4242
4243 /* Select shadow register 0x1f */
4244 tg3_writephy(tp, 0x1c, 0x7c00);
4245 tg3_readphy(tp, 0x1c, &phy1);
4246
4247 /* Select expansion interrupt status register */
4248 tg3_writephy(tp, 0x17, 0x0f01);
4249 tg3_readphy(tp, 0x15, &phy2);
4250 tg3_readphy(tp, 0x15, &phy2);
4251
4252 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4253 /* We have signal detect and not receiving
4254 * config code words, link is up by parallel
4255 * detection.
4256 */
4257
4258 bmcr &= ~BMCR_ANENABLE;
4259 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4260 tg3_writephy(tp, MII_BMCR, bmcr);
4261 tp->tg3_flags2 |= TG3_FLG2_PARALLEL_DETECT;
4262 }
4263 }
Matt Carlson859a588792010-04-05 10:19:28 +00004264 } else if (netif_carrier_ok(tp->dev) &&
4265 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
4266 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004267 u32 phy2;
4268
4269 /* Select expansion interrupt status register */
4270 tg3_writephy(tp, 0x17, 0x0f01);
4271 tg3_readphy(tp, 0x15, &phy2);
4272 if (phy2 & 0x20) {
4273 u32 bmcr;
4274
4275 /* Config code words received, turn on autoneg. */
4276 tg3_readphy(tp, MII_BMCR, &bmcr);
4277 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4278
4279 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4280
4281 }
4282 }
4283}
4284
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4286{
4287 int err;
4288
Matt Carlson859a588792010-04-05 10:19:28 +00004289 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004291 else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004292 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004293 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004296 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004297 u32 val, scale;
4298
4299 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4300 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4301 scale = 65;
4302 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4303 scale = 6;
4304 else
4305 scale = 12;
4306
4307 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4308 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4309 tw32(GRC_MISC_CFG, val);
4310 }
4311
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 if (tp->link_config.active_speed == SPEED_1000 &&
4313 tp->link_config.active_duplex == DUPLEX_HALF)
4314 tw32(MAC_TX_LENGTHS,
4315 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4316 (6 << TX_LENGTHS_IPG_SHIFT) |
4317 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4318 else
4319 tw32(MAC_TX_LENGTHS,
4320 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4321 (6 << TX_LENGTHS_IPG_SHIFT) |
4322 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4323
4324 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4325 if (netif_carrier_ok(tp->dev)) {
4326 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004327 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 } else {
4329 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4330 }
4331 }
4332
Matt Carlson8ed5d972007-05-07 00:25:49 -07004333 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4334 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4335 if (!netif_carrier_ok(tp->dev))
4336 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4337 tp->pwrmgmt_thresh;
4338 else
4339 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4340 tw32(PCIE_PWR_MGMT_THRESH, val);
4341 }
4342
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 return err;
4344}
4345
Michael Chandf3e6542006-05-26 17:48:07 -07004346/* This is called whenever we suspect that the system chipset is re-
4347 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4348 * is bogus tx completions. We try to recover by setting the
4349 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4350 * in the workqueue.
4351 */
4352static void tg3_tx_recover(struct tg3 *tp)
4353{
4354 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4355 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4356
Matt Carlson5129c3a2010-04-05 10:19:23 +00004357 netdev_warn(tp->dev,
4358 "The system may be re-ordering memory-mapped I/O "
4359 "cycles to the network device, attempting to recover. "
4360 "Please report the problem to the driver maintainer "
4361 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004362
4363 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004364 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004365 spin_unlock(&tp->lock);
4366}
4367
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004368static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004369{
4370 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004371 return tnapi->tx_pending -
4372 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004373}
4374
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375/* Tigon3 never reports partial packet sends. So we do not
4376 * need special logic to handle SKBs that have not had all
4377 * of their frags sent yet, like SunGEM does.
4378 */
Matt Carlson17375d22009-08-28 14:02:18 +00004379static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380{
Matt Carlson17375d22009-08-28 14:02:18 +00004381 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004382 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004383 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004384 struct netdev_queue *txq;
4385 int index = tnapi - tp->napi;
4386
Matt Carlson19cfaec2009-12-03 08:36:20 +00004387 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004388 index--;
4389
4390 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391
4392 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004393 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004395 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
Michael Chandf3e6542006-05-26 17:48:07 -07004397 if (unlikely(skb == NULL)) {
4398 tg3_tx_recover(tp);
4399 return;
4400 }
4401
Alexander Duyckf4188d82009-12-02 16:48:38 +00004402 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004403 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004404 skb_headlen(skb),
4405 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406
4407 ri->skb = NULL;
4408
4409 sw_idx = NEXT_TX(sw_idx);
4410
4411 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004412 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004413 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4414 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004415
4416 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004417 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004418 skb_shinfo(skb)->frags[i].size,
4419 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 sw_idx = NEXT_TX(sw_idx);
4421 }
4422
David S. Millerf47c11e2005-06-24 20:18:35 -07004423 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004424
4425 if (unlikely(tx_bug)) {
4426 tg3_tx_recover(tp);
4427 return;
4428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 }
4430
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004431 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
Michael Chan1b2a7202006-08-07 21:46:02 -07004433 /* Need to make the tx_cons update visible to tg3_start_xmit()
4434 * before checking for netif_queue_stopped(). Without the
4435 * memory barrier, there is a small possibility that tg3_start_xmit()
4436 * will miss it and cause the queue to be stopped forever.
4437 */
4438 smp_mb();
4439
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004440 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004441 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004442 __netif_tx_lock(txq, smp_processor_id());
4443 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004444 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004445 netif_tx_wake_queue(txq);
4446 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448}
4449
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004450static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4451{
4452 if (!ri->skb)
4453 return;
4454
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004455 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004456 map_sz, PCI_DMA_FROMDEVICE);
4457 dev_kfree_skb_any(ri->skb);
4458 ri->skb = NULL;
4459}
4460
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461/* Returns size of skb allocated or < 0 on error.
4462 *
4463 * We only need to fill in the address because the other members
4464 * of the RX descriptor are invariant, see tg3_init_rings.
4465 *
4466 * Note the purposeful assymetry of cpu vs. chip accesses. For
4467 * posting buffers we only dirty the first cache line of the RX
4468 * descriptor (containing the address). Whereas for the RX status
4469 * buffers the cpu only reads the last cacheline of the RX descriptor
4470 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4471 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004472static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004473 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474{
4475 struct tg3_rx_buffer_desc *desc;
4476 struct ring_info *map, *src_map;
4477 struct sk_buff *skb;
4478 dma_addr_t mapping;
4479 int skb_size, dest_idx;
4480
4481 src_map = NULL;
4482 switch (opaque_key) {
4483 case RXD_OPAQUE_RING_STD:
4484 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlson21f581a2009-08-28 14:00:25 +00004485 desc = &tpr->rx_std[dest_idx];
4486 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004487 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 break;
4489
4490 case RXD_OPAQUE_RING_JUMBO:
4491 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004492 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004493 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004494 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 break;
4496
4497 default:
4498 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
4501 /* Do not overwrite any of the map or rp information
4502 * until we are sure we can commit to a new buffer.
4503 *
4504 * Callers depend upon this behavior and assume that
4505 * we leave everything unchanged if we fail.
4506 */
Matt Carlson287be122009-08-28 13:58:46 +00004507 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 if (skb == NULL)
4509 return -ENOMEM;
4510
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 skb_reserve(skb, tp->rx_offset);
4512
Matt Carlson287be122009-08-28 13:58:46 +00004513 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004515 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4516 dev_kfree_skb(skb);
4517 return -EIO;
4518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
4520 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004521 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 desc->addr_hi = ((u64)mapping >> 32);
4524 desc->addr_lo = ((u64)mapping & 0xffffffff);
4525
4526 return skb_size;
4527}
4528
4529/* We only need to move over in the address because the other
4530 * members of the RX descriptor are invariant. See notes above
4531 * tg3_alloc_rx_skb for full details.
4532 */
Matt Carlsona3896162009-11-13 13:03:44 +00004533static void tg3_recycle_rx(struct tg3_napi *tnapi,
4534 struct tg3_rx_prodring_set *dpr,
4535 u32 opaque_key, int src_idx,
4536 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537{
Matt Carlson17375d22009-08-28 14:02:18 +00004538 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4540 struct ring_info *src_map, *dest_map;
Matt Carlsona3896162009-11-13 13:03:44 +00004541 struct tg3_rx_prodring_set *spr = &tp->prodring[0];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004542 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
4544 switch (opaque_key) {
4545 case RXD_OPAQUE_RING_STD:
4546 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004547 dest_desc = &dpr->rx_std[dest_idx];
4548 dest_map = &dpr->rx_std_buffers[dest_idx];
4549 src_desc = &spr->rx_std[src_idx];
4550 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 break;
4552
4553 case RXD_OPAQUE_RING_JUMBO:
4554 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsona3896162009-11-13 13:03:44 +00004555 dest_desc = &dpr->rx_jmb[dest_idx].std;
4556 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4557 src_desc = &spr->rx_jmb[src_idx].std;
4558 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 break;
4560
4561 default:
4562 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
4565 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004566 dma_unmap_addr_set(dest_map, mapping,
4567 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 dest_desc->addr_hi = src_desc->addr_hi;
4569 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004570
4571 /* Ensure that the update to the skb happens after the physical
4572 * addresses have been transferred to the new BD location.
4573 */
4574 smp_wmb();
4575
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 src_map->skb = NULL;
4577}
4578
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579/* The RX ring scheme is composed of multiple rings which post fresh
4580 * buffers to the chip, and one special ring the chip uses to report
4581 * status back to the host.
4582 *
4583 * The special ring reports the status of received packets to the
4584 * host. The chip does not write into the original descriptor the
4585 * RX buffer was obtained from. The chip simply takes the original
4586 * descriptor as provided by the host, updates the status and length
4587 * field, then writes this into the next status ring entry.
4588 *
4589 * Each ring the host uses to post buffers to the chip is described
4590 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4591 * it is first placed into the on-chip ram. When the packet's length
4592 * is known, it walks down the TG3_BDINFO entries to select the ring.
4593 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4594 * which is within the range of the new packet's length is chosen.
4595 *
4596 * The "separate ring for rx status" scheme may sound queer, but it makes
4597 * sense from a cache coherency perspective. If only the host writes
4598 * to the buffer post rings, and only the chip writes to the rx status
4599 * rings, then cache lines never move beyond shared-modified state.
4600 * If both the host and chip were to write into the same ring, cache line
4601 * eviction could occur since both entities want it in an exclusive state.
4602 */
Matt Carlson17375d22009-08-28 14:02:18 +00004603static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604{
Matt Carlson17375d22009-08-28 14:02:18 +00004605 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004606 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004607 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004608 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004609 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 int received;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004611 struct tg3_rx_prodring_set *tpr = tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004613 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 /*
4615 * We need to order the read of hw_idx and the read of
4616 * the opaque cookie.
4617 */
4618 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 work_mask = 0;
4620 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004621 std_prod_idx = tpr->rx_std_prod_idx;
4622 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004624 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004625 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 unsigned int len;
4627 struct sk_buff *skb;
4628 dma_addr_t dma_addr;
4629 u32 opaque_key, desc_idx, *post_ptr;
Matt Carlson9dc7a112010-04-12 06:58:28 +00004630 bool hw_vlan __maybe_unused = false;
4631 u16 vtag __maybe_unused = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
4633 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4634 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4635 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004636 ri = &tp->prodring[0].rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004637 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004638 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004639 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004640 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004642 ri = &tp->prodring[0].rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004643 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004644 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004645 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004646 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
4649 work_mask |= opaque_key;
4650
4651 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4652 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4653 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004654 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 desc_idx, *post_ptr);
4656 drop_it_no_recycle:
4657 /* Other statistics kept track of by card. */
4658 tp->net_stats.rx_dropped++;
4659 goto next_pkt;
4660 }
4661
Matt Carlsonad829262008-11-21 17:16:16 -08004662 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4663 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
Matt Carlsond2757fc2010-04-12 06:58:27 +00004665 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 int skb_size;
4667
Matt Carlson86b21e52009-11-13 13:03:45 +00004668 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004669 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 if (skb_size < 0)
4671 goto drop_it;
4672
Matt Carlson287be122009-08-28 13:58:46 +00004673 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 PCI_DMA_FROMDEVICE);
4675
Matt Carlson61e800c2010-02-17 15:16:54 +00004676 /* Ensure that the update to the skb happens
4677 * after the usage of the old DMA mapping.
4678 */
4679 smp_wmb();
4680
4681 ri->skb = NULL;
4682
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 skb_put(skb, len);
4684 } else {
4685 struct sk_buff *copy_skb;
4686
Matt Carlsona3896162009-11-13 13:03:44 +00004687 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 desc_idx, *post_ptr);
4689
Matt Carlson9dc7a112010-04-12 06:58:28 +00004690 copy_skb = netdev_alloc_skb(tp->dev, len + VLAN_HLEN +
4691 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 if (copy_skb == NULL)
4693 goto drop_it_no_recycle;
4694
Matt Carlson9dc7a112010-04-12 06:58:28 +00004695 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 skb_put(copy_skb, len);
4697 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004698 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4700
4701 /* We'll reuse the original ring buffer. */
4702 skb = copy_skb;
4703 }
4704
4705 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4706 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4707 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4708 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4709 skb->ip_summed = CHECKSUM_UNNECESSARY;
4710 else
4711 skb->ip_summed = CHECKSUM_NONE;
4712
4713 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004714
4715 if (len > (tp->dev->mtu + ETH_HLEN) &&
4716 skb->protocol != htons(ETH_P_8021Q)) {
4717 dev_kfree_skb(skb);
4718 goto next_pkt;
4719 }
4720
Matt Carlson9dc7a112010-04-12 06:58:28 +00004721 if (desc->type_flags & RXD_FLAG_VLAN &&
4722 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG)) {
4723 vtag = desc->err_vlan & RXD_VLAN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724#if TG3_VLAN_TAG_USED
Matt Carlson9dc7a112010-04-12 06:58:28 +00004725 if (tp->vlgrp)
4726 hw_vlan = true;
4727 else
4728#endif
4729 {
4730 struct vlan_ethhdr *ve = (struct vlan_ethhdr *)
4731 __skb_push(skb, VLAN_HLEN);
4732
4733 memmove(ve, skb->data + VLAN_HLEN,
4734 ETH_ALEN * 2);
4735 ve->h_vlan_proto = htons(ETH_P_8021Q);
4736 ve->h_vlan_TCI = htons(vtag);
4737 }
4738 }
4739
4740#if TG3_VLAN_TAG_USED
4741 if (hw_vlan)
4742 vlan_gro_receive(&tnapi->napi, tp->vlgrp, vtag, skb);
4743 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744#endif
Matt Carlson17375d22009-08-28 14:02:18 +00004745 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 received++;
4748 budget--;
4749
4750next_pkt:
4751 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004752
4753 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson86cfe4f2010-01-12 10:11:37 +00004754 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
4755 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4756 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07004757 work_mask &= ~RXD_OPAQUE_RING_STD;
4758 rx_std_posted = 0;
4759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004761 sw_idx++;
Eric Dumazet6b31a512007-02-06 13:29:21 -08004762 sw_idx &= (TG3_RX_RCB_RING_SIZE(tp) - 1);
Michael Chan52f6d692005-04-25 15:14:32 -07004763
4764 /* Refresh hw_idx to see if there is new work */
4765 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004766 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004767 rmb();
4768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 }
4770
4771 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004772 tnapi->rx_rcb_ptr = sw_idx;
4773 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
4775 /* Refill RX ring(s). */
Matt Carlsone4af1af2010-02-12 14:47:05 +00004776 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004777 if (work_mask & RXD_OPAQUE_RING_STD) {
4778 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
4779 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4780 tpr->rx_std_prod_idx);
4781 }
4782 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
4783 tpr->rx_jmb_prod_idx = jmb_prod_idx %
4784 TG3_RX_JUMBO_RING_SIZE;
4785 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
4786 tpr->rx_jmb_prod_idx);
4787 }
4788 mmiowb();
4789 } else if (work_mask) {
4790 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
4791 * updated before the producer indices can be updated.
4792 */
4793 smp_wmb();
4794
Matt Carlson43619352009-11-13 13:03:47 +00004795 tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
Matt Carlson43619352009-11-13 13:03:47 +00004796 tpr->rx_jmb_prod_idx = jmb_prod_idx % TG3_RX_JUMBO_RING_SIZE;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004797
Matt Carlsone4af1af2010-02-12 14:47:05 +00004798 if (tnapi != &tp->napi[1])
4799 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801
4802 return received;
4803}
4804
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004805static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 /* handle link change and other phy events */
4808 if (!(tp->tg3_flags &
4809 (TG3_FLAG_USE_LINKCHG_REG |
4810 TG3_FLAG_POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004811 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
4812
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 if (sblk->status & SD_STATUS_LINK_CHG) {
4814 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004815 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004816 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004817 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4818 tw32_f(MAC_STATUS,
4819 (MAC_STATUS_SYNC_CHANGED |
4820 MAC_STATUS_CFG_CHANGED |
4821 MAC_STATUS_MI_COMPLETION |
4822 MAC_STATUS_LNKSTATE_CHANGED));
4823 udelay(40);
4824 } else
4825 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004826 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 }
4828 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004829}
4830
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004831static int tg3_rx_prodring_xfer(struct tg3 *tp,
4832 struct tg3_rx_prodring_set *dpr,
4833 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004834{
4835 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004836 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004837
4838 while (1) {
4839 src_prod_idx = spr->rx_std_prod_idx;
4840
4841 /* Make sure updates to the rx_std_buffers[] entries and the
4842 * standard producer index are seen in the correct order.
4843 */
4844 smp_rmb();
4845
4846 if (spr->rx_std_cons_idx == src_prod_idx)
4847 break;
4848
4849 if (spr->rx_std_cons_idx < src_prod_idx)
4850 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
4851 else
4852 cpycnt = TG3_RX_RING_SIZE - spr->rx_std_cons_idx;
4853
4854 cpycnt = min(cpycnt, TG3_RX_RING_SIZE - dpr->rx_std_prod_idx);
4855
4856 si = spr->rx_std_cons_idx;
4857 di = dpr->rx_std_prod_idx;
4858
Matt Carlsone92967b2010-02-12 14:47:06 +00004859 for (i = di; i < di + cpycnt; i++) {
4860 if (dpr->rx_std_buffers[i].skb) {
4861 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004862 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00004863 break;
4864 }
4865 }
4866
4867 if (!cpycnt)
4868 break;
4869
4870 /* Ensure that updates to the rx_std_buffers ring and the
4871 * shadowed hardware producer ring from tg3_recycle_skb() are
4872 * ordered correctly WRT the skb check above.
4873 */
4874 smp_rmb();
4875
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004876 memcpy(&dpr->rx_std_buffers[di],
4877 &spr->rx_std_buffers[si],
4878 cpycnt * sizeof(struct ring_info));
4879
4880 for (i = 0; i < cpycnt; i++, di++, si++) {
4881 struct tg3_rx_buffer_desc *sbd, *dbd;
4882 sbd = &spr->rx_std[si];
4883 dbd = &dpr->rx_std[di];
4884 dbd->addr_hi = sbd->addr_hi;
4885 dbd->addr_lo = sbd->addr_lo;
4886 }
4887
4888 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) %
4889 TG3_RX_RING_SIZE;
4890 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) %
4891 TG3_RX_RING_SIZE;
4892 }
4893
4894 while (1) {
4895 src_prod_idx = spr->rx_jmb_prod_idx;
4896
4897 /* Make sure updates to the rx_jmb_buffers[] entries and
4898 * the jumbo producer index are seen in the correct order.
4899 */
4900 smp_rmb();
4901
4902 if (spr->rx_jmb_cons_idx == src_prod_idx)
4903 break;
4904
4905 if (spr->rx_jmb_cons_idx < src_prod_idx)
4906 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
4907 else
4908 cpycnt = TG3_RX_JUMBO_RING_SIZE - spr->rx_jmb_cons_idx;
4909
4910 cpycnt = min(cpycnt,
4911 TG3_RX_JUMBO_RING_SIZE - dpr->rx_jmb_prod_idx);
4912
4913 si = spr->rx_jmb_cons_idx;
4914 di = dpr->rx_jmb_prod_idx;
4915
Matt Carlsone92967b2010-02-12 14:47:06 +00004916 for (i = di; i < di + cpycnt; i++) {
4917 if (dpr->rx_jmb_buffers[i].skb) {
4918 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004919 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00004920 break;
4921 }
4922 }
4923
4924 if (!cpycnt)
4925 break;
4926
4927 /* Ensure that updates to the rx_jmb_buffers ring and the
4928 * shadowed hardware producer ring from tg3_recycle_skb() are
4929 * ordered correctly WRT the skb check above.
4930 */
4931 smp_rmb();
4932
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004933 memcpy(&dpr->rx_jmb_buffers[di],
4934 &spr->rx_jmb_buffers[si],
4935 cpycnt * sizeof(struct ring_info));
4936
4937 for (i = 0; i < cpycnt; i++, di++, si++) {
4938 struct tg3_rx_buffer_desc *sbd, *dbd;
4939 sbd = &spr->rx_jmb[si].std;
4940 dbd = &dpr->rx_jmb[di].std;
4941 dbd->addr_hi = sbd->addr_hi;
4942 dbd->addr_lo = sbd->addr_lo;
4943 }
4944
4945 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) %
4946 TG3_RX_JUMBO_RING_SIZE;
4947 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) %
4948 TG3_RX_JUMBO_RING_SIZE;
4949 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004950
4951 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004952}
4953
Matt Carlson35f2d7d2009-11-13 13:03:41 +00004954static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
4955{
4956 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
4958 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004959 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00004960 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004961 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07004962 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 }
4964
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 /* run RX thread, within the bounds set by NAPI.
4966 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004967 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004969 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00004970 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004972 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00004973 struct tg3_rx_prodring_set *dpr = &tp->prodring[0];
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004974 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00004975 u32 std_prod_idx = dpr->rx_std_prod_idx;
4976 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004977
Matt Carlsone4af1af2010-02-12 14:47:05 +00004978 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004979 err |= tg3_rx_prodring_xfer(tp, dpr,
4980 tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004981
4982 wmb();
4983
Matt Carlsone4af1af2010-02-12 14:47:05 +00004984 if (std_prod_idx != dpr->rx_std_prod_idx)
4985 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4986 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004987
Matt Carlsone4af1af2010-02-12 14:47:05 +00004988 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
4989 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
4990 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004991
4992 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00004993
4994 if (err)
4995 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004996 }
4997
David S. Miller6f535762007-10-11 18:08:29 -07004998 return work_done;
4999}
David S. Millerf7383c22005-05-18 22:50:53 -07005000
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005001static int tg3_poll_msix(struct napi_struct *napi, int budget)
5002{
5003 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5004 struct tg3 *tp = tnapi->tp;
5005 int work_done = 0;
5006 struct tg3_hw_status *sblk = tnapi->hw_status;
5007
5008 while (1) {
5009 work_done = tg3_poll_work(tnapi, work_done, budget);
5010
5011 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5012 goto tx_recovery;
5013
5014 if (unlikely(work_done >= budget))
5015 break;
5016
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005017 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005018 * to tell the hw how much work has been processed,
5019 * so we must read it before checking for more work.
5020 */
5021 tnapi->last_tag = sblk->status_tag;
5022 tnapi->last_irq_tag = tnapi->last_tag;
5023 rmb();
5024
5025 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005026 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5027 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005028 napi_complete(napi);
5029 /* Reenable interrupts. */
5030 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5031 mmiowb();
5032 break;
5033 }
5034 }
5035
5036 return work_done;
5037
5038tx_recovery:
5039 /* work_done is guaranteed to be less than budget. */
5040 napi_complete(napi);
5041 schedule_work(&tp->reset_task);
5042 return work_done;
5043}
5044
David S. Miller6f535762007-10-11 18:08:29 -07005045static int tg3_poll(struct napi_struct *napi, int budget)
5046{
Matt Carlson8ef04422009-08-28 14:01:37 +00005047 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5048 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005049 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005050 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005051
5052 while (1) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005053 tg3_poll_link(tp);
5054
Matt Carlson17375d22009-08-28 14:02:18 +00005055 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005056
5057 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5058 goto tx_recovery;
5059
5060 if (unlikely(work_done >= budget))
5061 break;
5062
Michael Chan4fd7ab52007-10-12 01:39:50 -07005063 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00005064 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005065 * to tell the hw how much work has been processed,
5066 * so we must read it before checking for more work.
5067 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005068 tnapi->last_tag = sblk->status_tag;
5069 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005070 rmb();
5071 } else
5072 sblk->status &= ~SD_STATUS_UPDATED;
5073
Matt Carlson17375d22009-08-28 14:02:18 +00005074 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005075 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005076 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005077 break;
5078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 }
5080
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005081 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005082
5083tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005084 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005085 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005086 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005087 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088}
5089
David S. Millerf47c11e2005-06-24 20:18:35 -07005090static void tg3_irq_quiesce(struct tg3 *tp)
5091{
Matt Carlson4f125f42009-09-01 12:55:02 +00005092 int i;
5093
David S. Millerf47c11e2005-06-24 20:18:35 -07005094 BUG_ON(tp->irq_sync);
5095
5096 tp->irq_sync = 1;
5097 smp_mb();
5098
Matt Carlson4f125f42009-09-01 12:55:02 +00005099 for (i = 0; i < tp->irq_cnt; i++)
5100 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005101}
5102
5103static inline int tg3_irq_sync(struct tg3 *tp)
5104{
5105 return tp->irq_sync;
5106}
5107
5108/* Fully shutdown all tg3 driver activity elsewhere in the system.
5109 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5110 * with as well. Most of the time, this is not necessary except when
5111 * shutting down the device.
5112 */
5113static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5114{
Michael Chan46966542007-07-11 19:47:19 -07005115 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005116 if (irq_sync)
5117 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005118}
5119
5120static inline void tg3_full_unlock(struct tg3 *tp)
5121{
David S. Millerf47c11e2005-06-24 20:18:35 -07005122 spin_unlock_bh(&tp->lock);
5123}
5124
Michael Chanfcfa0a32006-03-20 22:28:41 -08005125/* One-shot MSI handler - Chip automatically disables interrupt
5126 * after sending MSI so driver doesn't have to do it.
5127 */
David Howells7d12e782006-10-05 14:55:46 +01005128static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005129{
Matt Carlson09943a12009-08-28 14:01:57 +00005130 struct tg3_napi *tnapi = dev_id;
5131 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005132
Matt Carlson898a56f2009-08-28 14:02:40 +00005133 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005134 if (tnapi->rx_rcb)
5135 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005136
5137 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005138 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005139
5140 return IRQ_HANDLED;
5141}
5142
Michael Chan88b06bc22005-04-21 17:13:25 -07005143/* MSI ISR - No need to check for interrupt sharing and no need to
5144 * flush status block and interrupt mailbox. PCI ordering rules
5145 * guarantee that MSI will arrive after the status block.
5146 */
David Howells7d12e782006-10-05 14:55:46 +01005147static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005148{
Matt Carlson09943a12009-08-28 14:01:57 +00005149 struct tg3_napi *tnapi = dev_id;
5150 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005151
Matt Carlson898a56f2009-08-28 14:02:40 +00005152 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005153 if (tnapi->rx_rcb)
5154 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005155 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005156 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005157 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005158 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005159 * NIC to stop sending us irqs, engaging "in-intr-handler"
5160 * event coalescing.
5161 */
5162 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005163 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005164 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005165
Michael Chan88b06bc22005-04-21 17:13:25 -07005166 return IRQ_RETVAL(1);
5167}
5168
David Howells7d12e782006-10-05 14:55:46 +01005169static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170{
Matt Carlson09943a12009-08-28 14:01:57 +00005171 struct tg3_napi *tnapi = dev_id;
5172 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005173 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 unsigned int handled = 1;
5175
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 /* In INTx mode, it is possible for the interrupt to arrive at
5177 * the CPU before the status block posted prior to the interrupt.
5178 * Reading the PCI State register will confirm whether the
5179 * interrupt is ours and will flush the status block.
5180 */
Michael Chand18edcb2007-03-24 20:57:11 -07005181 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5182 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5183 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5184 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005185 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005186 }
Michael Chand18edcb2007-03-24 20:57:11 -07005187 }
5188
5189 /*
5190 * Writing any value to intr-mbox-0 clears PCI INTA# and
5191 * chip-internal interrupt pending events.
5192 * Writing non-zero to intr-mbox-0 additional tells the
5193 * NIC to stop sending us irqs, engaging "in-intr-handler"
5194 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005195 *
5196 * Flush the mailbox to de-assert the IRQ immediately to prevent
5197 * spurious interrupts. The flush impacts performance but
5198 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005199 */
Michael Chanc04cb342007-05-07 00:26:15 -07005200 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005201 if (tg3_irq_sync(tp))
5202 goto out;
5203 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005204 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005205 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005206 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005207 } else {
5208 /* No work, shared interrupt perhaps? re-enable
5209 * interrupts, and flush that PCI write
5210 */
5211 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5212 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005213 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005214out:
David S. Millerfac9b832005-05-18 22:46:34 -07005215 return IRQ_RETVAL(handled);
5216}
5217
David Howells7d12e782006-10-05 14:55:46 +01005218static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005219{
Matt Carlson09943a12009-08-28 14:01:57 +00005220 struct tg3_napi *tnapi = dev_id;
5221 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005222 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005223 unsigned int handled = 1;
5224
David S. Millerfac9b832005-05-18 22:46:34 -07005225 /* In INTx mode, it is possible for the interrupt to arrive at
5226 * the CPU before the status block posted prior to the interrupt.
5227 * Reading the PCI State register will confirm whether the
5228 * interrupt is ours and will flush the status block.
5229 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005230 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07005231 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5232 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5233 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005234 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 }
Michael Chand18edcb2007-03-24 20:57:11 -07005236 }
5237
5238 /*
5239 * writing any value to intr-mbox-0 clears PCI INTA# and
5240 * chip-internal interrupt pending events.
5241 * writing non-zero to intr-mbox-0 additional tells the
5242 * NIC to stop sending us irqs, engaging "in-intr-handler"
5243 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005244 *
5245 * Flush the mailbox to de-assert the IRQ immediately to prevent
5246 * spurious interrupts. The flush impacts performance but
5247 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005248 */
Michael Chanc04cb342007-05-07 00:26:15 -07005249 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005250
5251 /*
5252 * In a shared interrupt configuration, sometimes other devices'
5253 * interrupts will scream. We record the current status tag here
5254 * so that the above check can report that the screaming interrupts
5255 * are unhandled. Eventually they will be silenced.
5256 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005257 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005258
Michael Chand18edcb2007-03-24 20:57:11 -07005259 if (tg3_irq_sync(tp))
5260 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005261
Matt Carlson72334482009-08-28 14:03:01 +00005262 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005263
Matt Carlson09943a12009-08-28 14:01:57 +00005264 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005265
David S. Millerf47c11e2005-06-24 20:18:35 -07005266out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 return IRQ_RETVAL(handled);
5268}
5269
Michael Chan79381092005-04-21 17:13:59 -07005270/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005271static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005272{
Matt Carlson09943a12009-08-28 14:01:57 +00005273 struct tg3_napi *tnapi = dev_id;
5274 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005275 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005276
Michael Chanf9804dd2005-09-27 12:13:10 -07005277 if ((sblk->status & SD_STATUS_UPDATED) ||
5278 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005279 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005280 return IRQ_RETVAL(1);
5281 }
5282 return IRQ_RETVAL(0);
5283}
5284
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005285static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005286static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287
Michael Chanb9ec6c12006-07-25 16:37:27 -07005288/* Restart hardware after configuration changes, self-test, etc.
5289 * Invoked with tp->lock held.
5290 */
5291static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005292 __releases(tp->lock)
5293 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005294{
5295 int err;
5296
5297 err = tg3_init_hw(tp, reset_phy);
5298 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005299 netdev_err(tp->dev,
5300 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005301 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5302 tg3_full_unlock(tp);
5303 del_timer_sync(&tp->timer);
5304 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005305 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005306 dev_close(tp->dev);
5307 tg3_full_lock(tp, 0);
5308 }
5309 return err;
5310}
5311
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312#ifdef CONFIG_NET_POLL_CONTROLLER
5313static void tg3_poll_controller(struct net_device *dev)
5314{
Matt Carlson4f125f42009-09-01 12:55:02 +00005315 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005316 struct tg3 *tp = netdev_priv(dev);
5317
Matt Carlson4f125f42009-09-01 12:55:02 +00005318 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005319 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320}
5321#endif
5322
David Howellsc4028952006-11-22 14:57:56 +00005323static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324{
David Howellsc4028952006-11-22 14:57:56 +00005325 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005326 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 unsigned int restart_timer;
5328
Michael Chan7faa0062006-02-02 17:29:28 -08005329 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005330
5331 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005332 tg3_full_unlock(tp);
5333 return;
5334 }
5335
5336 tg3_full_unlock(tp);
5337
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005338 tg3_phy_stop(tp);
5339
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 tg3_netif_stop(tp);
5341
David S. Millerf47c11e2005-06-24 20:18:35 -07005342 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343
5344 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5345 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5346
Michael Chandf3e6542006-05-26 17:48:07 -07005347 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5348 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5349 tp->write32_rx_mbox = tg3_write_flush_reg32;
5350 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5351 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5352 }
5353
Michael Chan944d9802005-05-29 14:57:48 -07005354 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005355 err = tg3_init_hw(tp, 1);
5356 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005357 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
5359 tg3_netif_start(tp);
5360
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 if (restart_timer)
5362 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005363
Michael Chanb9ec6c12006-07-25 16:37:27 -07005364out:
Michael Chan7faa0062006-02-02 17:29:28 -08005365 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005366
5367 if (!err)
5368 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369}
5370
Michael Chanb0408752007-02-13 12:18:30 -08005371static void tg3_dump_short_state(struct tg3 *tp)
5372{
Joe Perches05dbe002010-02-17 19:44:19 +00005373 netdev_err(tp->dev, "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5374 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5375 netdev_err(tp->dev, "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5376 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
Michael Chanb0408752007-02-13 12:18:30 -08005377}
5378
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379static void tg3_tx_timeout(struct net_device *dev)
5380{
5381 struct tg3 *tp = netdev_priv(dev);
5382
Michael Chanb0408752007-02-13 12:18:30 -08005383 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005384 netdev_err(dev, "transmit timed out, resetting\n");
Michael Chanb0408752007-02-13 12:18:30 -08005385 tg3_dump_short_state(tp);
5386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387
5388 schedule_work(&tp->reset_task);
5389}
5390
Michael Chanc58ec932005-09-17 00:46:27 -07005391/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5392static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5393{
5394 u32 base = (u32) mapping & 0xffffffff;
5395
5396 return ((base > 0xffffdcc0) &&
5397 (base + len + 8 < base));
5398}
5399
Michael Chan72f2afb2006-03-06 19:28:35 -08005400/* Test for DMA addresses > 40-bit */
5401static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5402 int len)
5403{
5404#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005405 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Yang Hongyang50cf1562009-04-06 19:01:14 -07005406 return (((u64) mapping + len) > DMA_BIT_MASK(40));
Michael Chan72f2afb2006-03-06 19:28:35 -08005407 return 0;
5408#else
5409 return 0;
5410#endif
5411}
5412
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005413static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414
Michael Chan72f2afb2006-03-06 19:28:35 -08005415/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005416static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5417 struct sk_buff *skb, u32 last_plus_one,
5418 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005420 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005421 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005422 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005424 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
Matt Carlson41588ba2008-04-19 18:12:33 -07005426 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5427 new_skb = skb_copy(skb, GFP_ATOMIC);
5428 else {
5429 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5430
5431 new_skb = skb_copy_expand(skb,
5432 skb_headroom(skb) + more_headroom,
5433 skb_tailroom(skb), GFP_ATOMIC);
5434 }
5435
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005437 ret = -1;
5438 } else {
5439 /* New SKB is guaranteed to be linear. */
5440 entry = *start;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005441 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5442 PCI_DMA_TODEVICE);
5443 /* Make sure the mapping succeeded */
5444 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5445 ret = -1;
5446 dev_kfree_skb(new_skb);
5447 new_skb = NULL;
David S. Miller90079ce2008-09-11 04:52:51 -07005448
Michael Chanc58ec932005-09-17 00:46:27 -07005449 /* Make sure new skb does not cross any 4G boundaries.
5450 * Drop the packet if it does.
5451 */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005452 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5453 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5454 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5455 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005456 ret = -1;
5457 dev_kfree_skb(new_skb);
5458 new_skb = NULL;
5459 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005460 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005461 base_flags, 1 | (mss << 1));
5462 *start = NEXT_TX(entry);
5463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 }
5465
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 /* Now clean up the sw ring entries. */
5467 i = 0;
5468 while (entry != last_plus_one) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005469 int len;
5470
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005471 if (i == 0)
Alexander Duyckf4188d82009-12-02 16:48:38 +00005472 len = skb_headlen(skb);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005473 else
Alexander Duyckf4188d82009-12-02 16:48:38 +00005474 len = skb_shinfo(skb)->frags[i-1].size;
5475
5476 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005477 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005478 mapping),
5479 len, PCI_DMA_TODEVICE);
5480 if (i == 0) {
5481 tnapi->tx_buffers[entry].skb = new_skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005482 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005483 new_addr);
5484 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005485 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 entry = NEXT_TX(entry);
5488 i++;
5489 }
5490
5491 dev_kfree_skb(skb);
5492
Michael Chanc58ec932005-09-17 00:46:27 -07005493 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494}
5495
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005496static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 dma_addr_t mapping, int len, u32 flags,
5498 u32 mss_and_is_end)
5499{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005500 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 int is_end = (mss_and_is_end & 0x1);
5502 u32 mss = (mss_and_is_end >> 1);
5503 u32 vlan_tag = 0;
5504
5505 if (is_end)
5506 flags |= TXD_FLAG_END;
5507 if (flags & TXD_FLAG_VLAN) {
5508 vlan_tag = flags >> 16;
5509 flags &= 0xffff;
5510 }
5511 vlan_tag |= (mss << TXD_MSS_SHIFT);
5512
5513 txd->addr_hi = ((u64) mapping >> 32);
5514 txd->addr_lo = ((u64) mapping & 0xffffffff);
5515 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5516 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5517}
5518
Michael Chan5a6f3072006-03-20 22:28:05 -08005519/* hard_start_xmit for devices that don't have any bugs and
Matt Carlsone849cdc2009-11-13 13:03:38 +00005520 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005521 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005522static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5523 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524{
5525 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005527 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005528 struct tg3_napi *tnapi;
5529 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005530 unsigned int i, last;
5531
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005532 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5533 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005534 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005535 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005536
Michael Chan00b70502006-06-17 21:58:45 -07005537 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005538 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005539 * interrupt. Furthermore, IRQ processing runs lockless so we have
5540 * no IRQ context deadlocks to worry about either. Rejoice!
5541 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005542 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005543 if (!netif_tx_queue_stopped(txq)) {
5544 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005545
5546 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005547 netdev_err(dev,
5548 "BUG! Tx Ring full when queue awake!\n");
Michael Chan5a6f3072006-03-20 22:28:05 -08005549 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005550 return NETDEV_TX_BUSY;
5551 }
5552
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005553 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005554 base_flags = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005555 mss = 0;
Matt Carlsonc13e3712007-05-05 11:50:04 -07005556 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005557 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005558 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005559
5560 if (skb_header_cloned(skb) &&
5561 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5562 dev_kfree_skb(skb);
5563 goto out_unlock;
5564 }
5565
Michael Chanb0026622006-07-03 19:42:14 -07005566 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005567 hdrlen = skb_headlen(skb) - ETH_HLEN;
Michael Chanb0026622006-07-03 19:42:14 -07005568 else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005569 struct iphdr *iph = ip_hdr(skb);
5570
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005571 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005572 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005573
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005574 iph->check = 0;
5575 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005576 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005577 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005578
Matt Carlsone849cdc2009-11-13 13:03:38 +00005579 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005580 mss |= (hdrlen & 0xc) << 12;
5581 if (hdrlen & 0x10)
5582 base_flags |= 0x00000010;
5583 base_flags |= (hdrlen & 0x3e0) << 5;
5584 } else
5585 mss |= hdrlen << 9;
5586
Michael Chan5a6f3072006-03-20 22:28:05 -08005587 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5588 TXD_FLAG_CPU_POST_DMA);
5589
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005590 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005591
Matt Carlson859a588792010-04-05 10:19:28 +00005592 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005593 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson859a588792010-04-05 10:19:28 +00005594 }
5595
Michael Chan5a6f3072006-03-20 22:28:05 -08005596#if TG3_VLAN_TAG_USED
5597 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5598 base_flags |= (TXD_FLAG_VLAN |
5599 (vlan_tx_tag_get(skb) << 16));
5600#endif
5601
Alexander Duyckf4188d82009-12-02 16:48:38 +00005602 len = skb_headlen(skb);
5603
5604 /* Queue skb data, a.k.a. the main skb fragment. */
5605 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5606 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005607 dev_kfree_skb(skb);
5608 goto out_unlock;
5609 }
5610
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005611 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005612 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005613
Matt Carlsonb703df62009-12-03 08:36:21 +00005614 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005615 !mss && skb->len > ETH_DATA_LEN)
5616 base_flags |= TXD_FLAG_JMB_PKT;
5617
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005618 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005619 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5620
5621 entry = NEXT_TX(entry);
5622
5623 /* Now loop through additional data fragments, and queue them. */
5624 if (skb_shinfo(skb)->nr_frags > 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005625 last = skb_shinfo(skb)->nr_frags - 1;
5626 for (i = 0; i <= last; i++) {
5627 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5628
5629 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005630 mapping = pci_map_page(tp->pdev,
5631 frag->page,
5632 frag->page_offset,
5633 len, PCI_DMA_TODEVICE);
5634 if (pci_dma_mapping_error(tp->pdev, mapping))
5635 goto dma_error;
5636
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005637 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005638 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005639 mapping);
Michael Chan5a6f3072006-03-20 22:28:05 -08005640
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005641 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005642 base_flags, (i == last) | (mss << 1));
5643
5644 entry = NEXT_TX(entry);
5645 }
5646 }
5647
5648 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005649 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005650
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005651 tnapi->tx_prod = entry;
5652 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005653 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005654 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005655 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005656 }
5657
5658out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005659 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005660
5661 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005662
5663dma_error:
5664 last = i;
5665 entry = tnapi->tx_prod;
5666 tnapi->tx_buffers[entry].skb = NULL;
5667 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005668 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005669 skb_headlen(skb),
5670 PCI_DMA_TODEVICE);
5671 for (i = 0; i <= last; i++) {
5672 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5673 entry = NEXT_TX(entry);
5674
5675 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005676 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005677 mapping),
5678 frag->size, PCI_DMA_TODEVICE);
5679 }
5680
5681 dev_kfree_skb(skb);
5682 return NETDEV_TX_OK;
Michael Chan5a6f3072006-03-20 22:28:05 -08005683}
5684
Stephen Hemminger613573252009-08-31 19:50:58 +00005685static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5686 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005687
5688/* Use GSO to workaround a rare TSO bug that may be triggered when the
5689 * TSO header is greater than 80 bytes.
5690 */
5691static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5692{
5693 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005694 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005695
5696 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005697 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005698 netif_stop_queue(tp->dev);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005699 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005700 return NETDEV_TX_BUSY;
5701
5702 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005703 }
5704
5705 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005706 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005707 goto tg3_tso_bug_end;
5708
5709 do {
5710 nskb = segs;
5711 segs = segs->next;
5712 nskb->next = NULL;
5713 tg3_start_xmit_dma_bug(nskb, tp->dev);
5714 } while (segs);
5715
5716tg3_tso_bug_end:
5717 dev_kfree_skb(skb);
5718
5719 return NETDEV_TX_OK;
5720}
Michael Chan52c0fd82006-06-29 20:15:54 -07005721
Michael Chan5a6f3072006-03-20 22:28:05 -08005722/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5723 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5724 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005725static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5726 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005727{
5728 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005729 u32 len, entry, base_flags, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005731 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005732 struct tg3_napi *tnapi;
5733 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005734 unsigned int i, last;
5735
Matt Carlson24f4efd2009-11-13 13:03:35 +00005736 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5737 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005738 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlson24f4efd2009-11-13 13:03:35 +00005739 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740
Michael Chan00b70502006-06-17 21:58:45 -07005741 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005742 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005743 * interrupt. Furthermore, IRQ processing runs lockless so we have
5744 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005746 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005747 if (!netif_tx_queue_stopped(txq)) {
5748 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005749
5750 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005751 netdev_err(dev,
5752 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 return NETDEV_TX_BUSY;
5755 }
5756
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005757 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005759 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005761
Matt Carlsonc13e3712007-05-05 11:50:04 -07005762 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005763 struct iphdr *iph;
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005764 u32 tcp_opt_len, ip_tcp_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765
5766 if (skb_header_cloned(skb) &&
5767 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5768 dev_kfree_skb(skb);
5769 goto out_unlock;
5770 }
5771
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005772 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005773 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774
Michael Chan52c0fd82006-06-29 20:15:54 -07005775 hdr_len = ip_tcp_len + tcp_opt_len;
5776 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005777 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00005778 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07005779
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5781 TXD_FLAG_CPU_POST_DMA);
5782
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005783 iph = ip_hdr(skb);
5784 iph->check = 0;
5785 iph->tot_len = htons(mss + hdr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005787 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005789 } else
5790 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5791 iph->daddr, 0,
5792 IPPROTO_TCP,
5793 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794
Matt Carlson615774f2009-11-13 13:03:39 +00005795 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
5796 mss |= (hdr_len & 0xc) << 12;
5797 if (hdr_len & 0x10)
5798 base_flags |= 0x00000010;
5799 base_flags |= (hdr_len & 0x3e0) << 5;
5800 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005801 mss |= hdr_len << 9;
5802 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
5803 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005804 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 int tsflags;
5806
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005807 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 mss |= (tsflags << 11);
5809 }
5810 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005811 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 int tsflags;
5813
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005814 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 base_flags |= tsflags << 12;
5816 }
5817 }
5818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819#if TG3_VLAN_TAG_USED
5820 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5821 base_flags |= (TXD_FLAG_VLAN |
5822 (vlan_tx_tag_get(skb) << 16));
5823#endif
5824
Matt Carlsonb703df62009-12-03 08:36:21 +00005825 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson615774f2009-11-13 13:03:39 +00005826 !mss && skb->len > ETH_DATA_LEN)
5827 base_flags |= TXD_FLAG_JMB_PKT;
5828
Alexander Duyckf4188d82009-12-02 16:48:38 +00005829 len = skb_headlen(skb);
5830
5831 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5832 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005833 dev_kfree_skb(skb);
5834 goto out_unlock;
5835 }
5836
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005837 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005838 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839
5840 would_hit_hwbug = 0;
5841
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005842 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
5843 would_hit_hwbug = 1;
5844
Matt Carlson0e1406d2009-11-02 12:33:33 +00005845 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5846 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07005847 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00005848
5849 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5850 tg3_40bit_overflow_test(tp, mapping, len))
5851 would_hit_hwbug = 1;
5852
5853 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07005854 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005856 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5858
5859 entry = NEXT_TX(entry);
5860
5861 /* Now loop through additional data fragments, and queue them. */
5862 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 last = skb_shinfo(skb)->nr_frags - 1;
5864 for (i = 0; i <= last; i++) {
5865 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5866
5867 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005868 mapping = pci_map_page(tp->pdev,
5869 frag->page,
5870 frag->page_offset,
5871 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005873 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005874 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005875 mapping);
5876 if (pci_dma_mapping_error(tp->pdev, mapping))
5877 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005879 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
5880 len <= 8)
5881 would_hit_hwbug = 1;
5882
Matt Carlson0e1406d2009-11-02 12:33:33 +00005883 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5884 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07005885 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886
Matt Carlson0e1406d2009-11-02 12:33:33 +00005887 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5888 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08005889 would_hit_hwbug = 1;
5890
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005892 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 base_flags, (i == last)|(mss << 1));
5894 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005895 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 base_flags, (i == last));
5897
5898 entry = NEXT_TX(entry);
5899 }
5900 }
5901
5902 if (would_hit_hwbug) {
5903 u32 last_plus_one = entry;
5904 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905
Michael Chanc58ec932005-09-17 00:46:27 -07005906 start = entry - 1 - skb_shinfo(skb)->nr_frags;
5907 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
5909 /* If the workaround fails due to memory/mapping
5910 * failure, silently drop this packet.
5911 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005912 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07005913 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 goto out_unlock;
5915
5916 entry = start;
5917 }
5918
5919 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005920 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005922 tnapi->tx_prod = entry;
5923 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005924 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005925 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00005926 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928
5929out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005930 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931
5932 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005933
5934dma_error:
5935 last = i;
5936 entry = tnapi->tx_prod;
5937 tnapi->tx_buffers[entry].skb = NULL;
5938 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005939 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005940 skb_headlen(skb),
5941 PCI_DMA_TODEVICE);
5942 for (i = 0; i <= last; i++) {
5943 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5944 entry = NEXT_TX(entry);
5945
5946 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005947 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005948 mapping),
5949 frag->size, PCI_DMA_TODEVICE);
5950 }
5951
5952 dev_kfree_skb(skb);
5953 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954}
5955
5956static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
5957 int new_mtu)
5958{
5959 dev->mtu = new_mtu;
5960
Michael Chanef7f5ec2005-07-25 12:32:25 -07005961 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07005962 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005963 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
5964 ethtool_op_set_tso(dev, 0);
Matt Carlson859a588792010-04-05 10:19:28 +00005965 } else {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005966 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Matt Carlson859a588792010-04-05 10:19:28 +00005967 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07005968 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07005969 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07005970 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07005971 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07005972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973}
5974
5975static int tg3_change_mtu(struct net_device *dev, int new_mtu)
5976{
5977 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005978 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979
5980 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
5981 return -EINVAL;
5982
5983 if (!netif_running(dev)) {
5984 /* We'll just catch it later when the
5985 * device is up'd.
5986 */
5987 tg3_set_mtu(dev, tp, new_mtu);
5988 return 0;
5989 }
5990
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005991 tg3_phy_stop(tp);
5992
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005994
5995 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996
Michael Chan944d9802005-05-29 14:57:48 -07005997 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998
5999 tg3_set_mtu(dev, tp, new_mtu);
6000
Michael Chanb9ec6c12006-07-25 16:37:27 -07006001 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002
Michael Chanb9ec6c12006-07-25 16:37:27 -07006003 if (!err)
6004 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
David S. Millerf47c11e2005-06-24 20:18:35 -07006006 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006008 if (!err)
6009 tg3_phy_start(tp);
6010
Michael Chanb9ec6c12006-07-25 16:37:27 -07006011 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012}
6013
Matt Carlson21f581a2009-08-28 14:00:25 +00006014static void tg3_rx_prodring_free(struct tg3 *tp,
6015 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 int i;
6018
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006019 if (tpr != &tp->prodring[0]) {
6020 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
6021 i = (i + 1) % TG3_RX_RING_SIZE)
6022 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6023 tp->rx_pkt_map_sz);
6024
6025 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
6026 for (i = tpr->rx_jmb_cons_idx;
6027 i != tpr->rx_jmb_prod_idx;
6028 i = (i + 1) % TG3_RX_JUMBO_RING_SIZE) {
6029 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6030 TG3_RX_JMB_MAP_SZ);
6031 }
6032 }
6033
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006034 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006037 for (i = 0; i < TG3_RX_RING_SIZE; i++)
6038 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6039 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006041 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006042 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++)
6043 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6044 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 }
6046}
6047
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006048/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 *
6050 * The chip has been shut down and the driver detached from
6051 * the networking, so no interrupts or new tx packets will
6052 * end up in the driver. tp->{tx,}lock are held and thus
6053 * we may not sleep.
6054 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006055static int tg3_rx_prodring_alloc(struct tg3 *tp,
6056 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057{
Matt Carlson287be122009-08-28 13:58:46 +00006058 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006060 tpr->rx_std_cons_idx = 0;
6061 tpr->rx_std_prod_idx = 0;
6062 tpr->rx_jmb_cons_idx = 0;
6063 tpr->rx_jmb_prod_idx = 0;
6064
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006065 if (tpr != &tp->prodring[0]) {
6066 memset(&tpr->rx_std_buffers[0], 0, TG3_RX_STD_BUFF_RING_SIZE);
6067 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE)
6068 memset(&tpr->rx_jmb_buffers[0], 0,
6069 TG3_RX_JMB_BUFF_RING_SIZE);
6070 goto done;
6071 }
6072
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 /* Zero out all descriptors. */
Matt Carlson21f581a2009-08-28 14:00:25 +00006074 memset(tpr->rx_std, 0, TG3_RX_RING_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075
Matt Carlson287be122009-08-28 13:58:46 +00006076 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07006077 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006078 tp->dev->mtu > ETH_DATA_LEN)
6079 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6080 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006081
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 /* Initialize invariants of the rings, we only set this
6083 * stuff once. This works because the card does not
6084 * write into the rx buffer posting rings.
6085 */
6086 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
6087 struct tg3_rx_buffer_desc *rxd;
6088
Matt Carlson21f581a2009-08-28 14:00:25 +00006089 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006090 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6092 rxd->opaque = (RXD_OPAQUE_RING_STD |
6093 (i << RXD_OPAQUE_INDEX_SHIFT));
6094 }
6095
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006096 /* Now allocate fresh SKBs for each rx ring. */
6097 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006098 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006099 netdev_warn(tp->dev,
6100 "Using a smaller RX standard ring. Only "
6101 "%d out of %d buffers were allocated "
6102 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006103 if (i == 0)
6104 goto initfail;
6105 tp->rx_pending = i;
6106 break;
6107 }
6108 }
6109
6110 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE))
6111 goto done;
6112
Matt Carlson21f581a2009-08-28 14:00:25 +00006113 memset(tpr->rx_jmb, 0, TG3_RX_JUMBO_RING_BYTES);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006114
Matt Carlson0d86df82010-02-17 15:17:00 +00006115 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE))
6116 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117
Matt Carlson0d86df82010-02-17 15:17:00 +00006118 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
6119 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
Matt Carlson0d86df82010-02-17 15:17:00 +00006121 rxd = &tpr->rx_jmb[i].std;
6122 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6123 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6124 RXD_FLAG_JUMBO;
6125 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6126 (i << RXD_OPAQUE_INDEX_SHIFT));
6127 }
6128
6129 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6130 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006131 netdev_warn(tp->dev,
6132 "Using a smaller RX jumbo ring. Only %d "
6133 "out of %d buffers were allocated "
6134 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006135 if (i == 0)
6136 goto initfail;
6137 tp->rx_jumbo_pending = i;
6138 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139 }
6140 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006141
6142done:
Michael Chan32d8c572006-07-25 16:38:29 -07006143 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006144
6145initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006146 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006147 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148}
6149
Matt Carlson21f581a2009-08-28 14:00:25 +00006150static void tg3_rx_prodring_fini(struct tg3 *tp,
6151 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152{
Matt Carlson21f581a2009-08-28 14:00:25 +00006153 kfree(tpr->rx_std_buffers);
6154 tpr->rx_std_buffers = NULL;
6155 kfree(tpr->rx_jmb_buffers);
6156 tpr->rx_jmb_buffers = NULL;
6157 if (tpr->rx_std) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 pci_free_consistent(tp->pdev, TG3_RX_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00006159 tpr->rx_std, tpr->rx_std_mapping);
6160 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006162 if (tpr->rx_jmb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 pci_free_consistent(tp->pdev, TG3_RX_JUMBO_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00006164 tpr->rx_jmb, tpr->rx_jmb_mapping);
6165 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006167}
6168
Matt Carlson21f581a2009-08-28 14:00:25 +00006169static int tg3_rx_prodring_init(struct tg3 *tp,
6170 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006171{
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006172 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE, GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006173 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006174 return -ENOMEM;
6175
Matt Carlson21f581a2009-08-28 14:00:25 +00006176 tpr->rx_std = pci_alloc_consistent(tp->pdev, TG3_RX_RING_BYTES,
6177 &tpr->rx_std_mapping);
6178 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006179 goto err_out;
6180
6181 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006182 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE,
Matt Carlson21f581a2009-08-28 14:00:25 +00006183 GFP_KERNEL);
6184 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006185 goto err_out;
6186
Matt Carlson21f581a2009-08-28 14:00:25 +00006187 tpr->rx_jmb = pci_alloc_consistent(tp->pdev,
6188 TG3_RX_JUMBO_RING_BYTES,
6189 &tpr->rx_jmb_mapping);
6190 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006191 goto err_out;
6192 }
6193
6194 return 0;
6195
6196err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006197 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006198 return -ENOMEM;
6199}
6200
6201/* Free up pending packets in all rx/tx rings.
6202 *
6203 * The chip has been shut down and the driver detached from
6204 * the networking, so no interrupts or new tx packets will
6205 * end up in the driver. tp->{tx,}lock is not held and we are not
6206 * in an interrupt context and thus may sleep.
6207 */
6208static void tg3_free_rings(struct tg3 *tp)
6209{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006210 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006211
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006212 for (j = 0; j < tp->irq_cnt; j++) {
6213 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006214
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006215 if (!tnapi->tx_buffers)
6216 continue;
6217
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006218 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006219 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006220 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006221 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006222
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006223 txp = &tnapi->tx_buffers[i];
6224 skb = txp->skb;
6225
6226 if (skb == NULL) {
6227 i++;
6228 continue;
6229 }
6230
Alexander Duyckf4188d82009-12-02 16:48:38 +00006231 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006232 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006233 skb_headlen(skb),
6234 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006235 txp->skb = NULL;
6236
Alexander Duyckf4188d82009-12-02 16:48:38 +00006237 i++;
6238
6239 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6240 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6241 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006242 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006243 skb_shinfo(skb)->frags[k].size,
6244 PCI_DMA_TODEVICE);
6245 i++;
6246 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006247
6248 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006249 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006250
Matt Carlsone4af1af2010-02-12 14:47:05 +00006251 tg3_rx_prodring_free(tp, &tp->prodring[j]);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006252 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006253}
6254
6255/* Initialize tx/rx rings for packet processing.
6256 *
6257 * The chip has been shut down and the driver detached from
6258 * the networking, so no interrupts or new tx packets will
6259 * end up in the driver. tp->{tx,}lock are held and thus
6260 * we may not sleep.
6261 */
6262static int tg3_init_rings(struct tg3 *tp)
6263{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006264 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006265
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006266 /* Free up all the SKBs. */
6267 tg3_free_rings(tp);
6268
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006269 for (i = 0; i < tp->irq_cnt; i++) {
6270 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006271
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006272 tnapi->last_tag = 0;
6273 tnapi->last_irq_tag = 0;
6274 tnapi->hw_status->status = 0;
6275 tnapi->hw_status->status_tag = 0;
6276 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6277
6278 tnapi->tx_prod = 0;
6279 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006280 if (tnapi->tx_ring)
6281 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006282
6283 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006284 if (tnapi->rx_rcb)
6285 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006286
Matt Carlsone4af1af2010-02-12 14:47:05 +00006287 if (tg3_rx_prodring_alloc(tp, &tp->prodring[i])) {
6288 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006289 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006290 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006291 }
Matt Carlson72334482009-08-28 14:03:01 +00006292
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006293 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006294}
6295
6296/*
6297 * Must not be invoked with interrupt sources disabled and
6298 * the hardware shutdown down.
6299 */
6300static void tg3_free_consistent(struct tg3 *tp)
6301{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006302 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006303
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006304 for (i = 0; i < tp->irq_cnt; i++) {
6305 struct tg3_napi *tnapi = &tp->napi[i];
6306
6307 if (tnapi->tx_ring) {
6308 pci_free_consistent(tp->pdev, TG3_TX_RING_BYTES,
6309 tnapi->tx_ring, tnapi->tx_desc_mapping);
6310 tnapi->tx_ring = NULL;
6311 }
6312
6313 kfree(tnapi->tx_buffers);
6314 tnapi->tx_buffers = NULL;
6315
6316 if (tnapi->rx_rcb) {
6317 pci_free_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
6318 tnapi->rx_rcb,
6319 tnapi->rx_rcb_mapping);
6320 tnapi->rx_rcb = NULL;
6321 }
6322
6323 if (tnapi->hw_status) {
6324 pci_free_consistent(tp->pdev, TG3_HW_STATUS_SIZE,
6325 tnapi->hw_status,
6326 tnapi->status_mapping);
6327 tnapi->hw_status = NULL;
6328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006330
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331 if (tp->hw_stats) {
6332 pci_free_consistent(tp->pdev, sizeof(struct tg3_hw_stats),
6333 tp->hw_stats, tp->stats_mapping);
6334 tp->hw_stats = NULL;
6335 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006336
Matt Carlsone4af1af2010-02-12 14:47:05 +00006337 for (i = 0; i < tp->irq_cnt; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006338 tg3_rx_prodring_fini(tp, &tp->prodring[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339}
6340
6341/*
6342 * Must not be invoked with interrupt sources disabled and
6343 * the hardware shutdown down. Can sleep.
6344 */
6345static int tg3_alloc_consistent(struct tg3 *tp)
6346{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006347 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006348
Matt Carlsone4af1af2010-02-12 14:47:05 +00006349 for (i = 0; i < tp->irq_cnt; i++) {
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006350 if (tg3_rx_prodring_init(tp, &tp->prodring[i]))
6351 goto err_out;
6352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 tp->hw_stats = pci_alloc_consistent(tp->pdev,
6355 sizeof(struct tg3_hw_stats),
6356 &tp->stats_mapping);
6357 if (!tp->hw_stats)
6358 goto err_out;
6359
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6361
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006362 for (i = 0; i < tp->irq_cnt; i++) {
6363 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006364 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006365
6366 tnapi->hw_status = pci_alloc_consistent(tp->pdev,
6367 TG3_HW_STATUS_SIZE,
6368 &tnapi->status_mapping);
6369 if (!tnapi->hw_status)
6370 goto err_out;
6371
6372 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006373 sblk = tnapi->hw_status;
6374
Matt Carlson19cfaec2009-12-03 08:36:20 +00006375 /* If multivector TSS is enabled, vector 0 does not handle
6376 * tx interrupts. Don't allocate any resources for it.
6377 */
6378 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6379 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6380 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6381 TG3_TX_RING_SIZE,
6382 GFP_KERNEL);
6383 if (!tnapi->tx_buffers)
6384 goto err_out;
6385
6386 tnapi->tx_ring = pci_alloc_consistent(tp->pdev,
6387 TG3_TX_RING_BYTES,
6388 &tnapi->tx_desc_mapping);
6389 if (!tnapi->tx_ring)
6390 goto err_out;
6391 }
6392
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006393 /*
6394 * When RSS is enabled, the status block format changes
6395 * slightly. The "rx_jumbo_consumer", "reserved",
6396 * and "rx_mini_consumer" members get mapped to the
6397 * other three rx return ring producer indexes.
6398 */
6399 switch (i) {
6400 default:
6401 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6402 break;
6403 case 2:
6404 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6405 break;
6406 case 3:
6407 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6408 break;
6409 case 4:
6410 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6411 break;
6412 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006413
Matt Carlsone4af1af2010-02-12 14:47:05 +00006414 tnapi->prodring = &tp->prodring[i];
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006415
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006416 /*
6417 * If multivector RSS is enabled, vector 0 does not handle
6418 * rx or tx interrupts. Don't allocate any resources for it.
6419 */
6420 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6421 continue;
6422
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006423 tnapi->rx_rcb = pci_alloc_consistent(tp->pdev,
6424 TG3_RX_RCB_RING_BYTES(tp),
6425 &tnapi->rx_rcb_mapping);
6426 if (!tnapi->rx_rcb)
6427 goto err_out;
6428
6429 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006430 }
6431
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432 return 0;
6433
6434err_out:
6435 tg3_free_consistent(tp);
6436 return -ENOMEM;
6437}
6438
6439#define MAX_WAIT_CNT 1000
6440
6441/* To stop a block, clear the enable bit and poll till it
6442 * clears. tp->lock is held.
6443 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006444static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445{
6446 unsigned int i;
6447 u32 val;
6448
6449 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6450 switch (ofs) {
6451 case RCVLSC_MODE:
6452 case DMAC_MODE:
6453 case MBFREE_MODE:
6454 case BUFMGR_MODE:
6455 case MEMARB_MODE:
6456 /* We can't enable/disable these bits of the
6457 * 5705/5750, just say success.
6458 */
6459 return 0;
6460
6461 default:
6462 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464 }
6465
6466 val = tr32(ofs);
6467 val &= ~enable_bit;
6468 tw32_f(ofs, val);
6469
6470 for (i = 0; i < MAX_WAIT_CNT; i++) {
6471 udelay(100);
6472 val = tr32(ofs);
6473 if ((val & enable_bit) == 0)
6474 break;
6475 }
6476
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006477 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006478 dev_err(&tp->pdev->dev,
6479 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6480 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 return -ENODEV;
6482 }
6483
6484 return 0;
6485}
6486
6487/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006488static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489{
6490 int i, err;
6491
6492 tg3_disable_ints(tp);
6493
6494 tp->rx_mode &= ~RX_MODE_ENABLE;
6495 tw32_f(MAC_RX_MODE, tp->rx_mode);
6496 udelay(10);
6497
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006498 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6499 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6500 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6501 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6502 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6503 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006505 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6506 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6507 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6508 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6509 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6510 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6511 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512
6513 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6514 tw32_f(MAC_MODE, tp->mac_mode);
6515 udelay(40);
6516
6517 tp->tx_mode &= ~TX_MODE_ENABLE;
6518 tw32_f(MAC_TX_MODE, tp->tx_mode);
6519
6520 for (i = 0; i < MAX_WAIT_CNT; i++) {
6521 udelay(100);
6522 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6523 break;
6524 }
6525 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006526 dev_err(&tp->pdev->dev,
6527 "%s timed out, TX_MODE_ENABLE will not clear "
6528 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006529 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530 }
6531
Michael Chane6de8ad2005-05-05 14:42:41 -07006532 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006533 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6534 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535
6536 tw32(FTQ_RESET, 0xffffffff);
6537 tw32(FTQ_RESET, 0x00000000);
6538
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006539 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6540 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006542 for (i = 0; i < tp->irq_cnt; i++) {
6543 struct tg3_napi *tnapi = &tp->napi[i];
6544 if (tnapi->hw_status)
6545 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 if (tp->hw_stats)
6548 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6549
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 return err;
6551}
6552
Matt Carlson0d3031d2007-10-10 18:02:43 -07006553static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6554{
6555 int i;
6556 u32 apedata;
6557
6558 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6559 if (apedata != APE_SEG_SIG_MAGIC)
6560 return;
6561
6562 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006563 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006564 return;
6565
6566 /* Wait for up to 1 millisecond for APE to service previous event. */
6567 for (i = 0; i < 10; i++) {
6568 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6569 return;
6570
6571 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6572
6573 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6574 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6575 event | APE_EVENT_STATUS_EVENT_PENDING);
6576
6577 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6578
6579 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6580 break;
6581
6582 udelay(100);
6583 }
6584
6585 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6586 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6587}
6588
6589static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6590{
6591 u32 event;
6592 u32 apedata;
6593
6594 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6595 return;
6596
6597 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006598 case RESET_KIND_INIT:
6599 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6600 APE_HOST_SEG_SIG_MAGIC);
6601 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6602 APE_HOST_SEG_LEN_MAGIC);
6603 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6604 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6605 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
6606 APE_HOST_DRIVER_ID_MAGIC);
6607 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6608 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006609
Matt Carlson33f401a2010-04-05 10:19:27 +00006610 event = APE_EVENT_STATUS_STATE_START;
6611 break;
6612 case RESET_KIND_SHUTDOWN:
6613 /* With the interface we are currently using,
6614 * APE does not track driver state. Wiping
6615 * out the HOST SEGMENT SIGNATURE forces
6616 * the APE to assume OS absent status.
6617 */
6618 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006619
Matt Carlson33f401a2010-04-05 10:19:27 +00006620 event = APE_EVENT_STATUS_STATE_UNLOAD;
6621 break;
6622 case RESET_KIND_SUSPEND:
6623 event = APE_EVENT_STATUS_STATE_SUSPEND;
6624 break;
6625 default:
6626 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006627 }
6628
6629 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6630
6631 tg3_ape_send_event(tp, event);
6632}
6633
Michael Chane6af3012005-04-21 17:12:05 -07006634/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6636{
David S. Millerf49639e2006-06-09 11:58:36 -07006637 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6638 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639
6640 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6641 switch (kind) {
6642 case RESET_KIND_INIT:
6643 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6644 DRV_STATE_START);
6645 break;
6646
6647 case RESET_KIND_SHUTDOWN:
6648 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6649 DRV_STATE_UNLOAD);
6650 break;
6651
6652 case RESET_KIND_SUSPEND:
6653 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6654 DRV_STATE_SUSPEND);
6655 break;
6656
6657 default:
6658 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006661
6662 if (kind == RESET_KIND_INIT ||
6663 kind == RESET_KIND_SUSPEND)
6664 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665}
6666
6667/* tp->lock is held. */
6668static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6669{
6670 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6671 switch (kind) {
6672 case RESET_KIND_INIT:
6673 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6674 DRV_STATE_START_DONE);
6675 break;
6676
6677 case RESET_KIND_SHUTDOWN:
6678 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6679 DRV_STATE_UNLOAD_DONE);
6680 break;
6681
6682 default:
6683 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006686
6687 if (kind == RESET_KIND_SHUTDOWN)
6688 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006689}
6690
6691/* tp->lock is held. */
6692static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6693{
6694 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
6695 switch (kind) {
6696 case RESET_KIND_INIT:
6697 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6698 DRV_STATE_START);
6699 break;
6700
6701 case RESET_KIND_SHUTDOWN:
6702 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6703 DRV_STATE_UNLOAD);
6704 break;
6705
6706 case RESET_KIND_SUSPEND:
6707 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6708 DRV_STATE_SUSPEND);
6709 break;
6710
6711 default:
6712 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 }
6715}
6716
Michael Chan7a6f4362006-09-27 16:03:31 -07006717static int tg3_poll_fw(struct tg3 *tp)
6718{
6719 int i;
6720 u32 val;
6721
Michael Chanb5d37722006-09-27 16:06:21 -07006722 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006723 /* Wait up to 20ms for init done. */
6724 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006725 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6726 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006727 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006728 }
6729 return -ENODEV;
6730 }
6731
Michael Chan7a6f4362006-09-27 16:03:31 -07006732 /* Wait for firmware initialization to complete. */
6733 for (i = 0; i < 100000; i++) {
6734 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6735 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6736 break;
6737 udelay(10);
6738 }
6739
6740 /* Chip might not be fitted with firmware. Some Sun onboard
6741 * parts are configured like that. So don't signal the timeout
6742 * of the above loop as an error, but do report the lack of
6743 * running firmware once.
6744 */
6745 if (i >= 100000 &&
6746 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6747 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6748
Joe Perches05dbe002010-02-17 19:44:19 +00006749 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07006750 }
6751
Matt Carlson6b10c162010-02-12 14:47:08 +00006752 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
6753 /* The 57765 A0 needs a little more
6754 * time to do some important work.
6755 */
6756 mdelay(10);
6757 }
6758
Michael Chan7a6f4362006-09-27 16:03:31 -07006759 return 0;
6760}
6761
Michael Chanee6a99b2007-07-18 21:49:10 -07006762/* Save PCI command register before chip reset */
6763static void tg3_save_pci_state(struct tg3 *tp)
6764{
Matt Carlson8a6eac92007-10-21 16:17:55 -07006765 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006766}
6767
6768/* Restore PCI state after chip reset */
6769static void tg3_restore_pci_state(struct tg3 *tp)
6770{
6771 u32 val;
6772
6773 /* Re-enable indirect register accesses. */
6774 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6775 tp->misc_host_ctrl);
6776
6777 /* Set MAX PCI retry to zero. */
6778 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6779 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6780 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6781 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006782 /* Allow reads and writes to the APE register and memory space. */
6783 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6784 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
6785 PCISTATE_ALLOW_APE_SHMEM_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07006786 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6787
Matt Carlson8a6eac92007-10-21 16:17:55 -07006788 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006789
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006790 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6791 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
6792 pcie_set_readrq(tp->pdev, 4096);
6793 else {
6794 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6795 tp->pci_cacheline_sz);
6796 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6797 tp->pci_lat_timer);
6798 }
Michael Chan114342f2007-10-15 02:12:26 -07006799 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08006800
Michael Chanee6a99b2007-07-18 21:49:10 -07006801 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08006802 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07006803 u16 pcix_cmd;
6804
6805 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6806 &pcix_cmd);
6807 pcix_cmd &= ~PCI_X_CMD_ERO;
6808 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6809 pcix_cmd);
6810 }
Michael Chanee6a99b2007-07-18 21:49:10 -07006811
6812 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07006813
6814 /* Chip reset on 5780 will reset MSI enable bit,
6815 * so need to restore it.
6816 */
6817 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
6818 u16 ctrl;
6819
6820 pci_read_config_word(tp->pdev,
6821 tp->msi_cap + PCI_MSI_FLAGS,
6822 &ctrl);
6823 pci_write_config_word(tp->pdev,
6824 tp->msi_cap + PCI_MSI_FLAGS,
6825 ctrl | PCI_MSI_FLAGS_ENABLE);
6826 val = tr32(MSGINT_MODE);
6827 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
6828 }
6829 }
6830}
6831
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832static void tg3_stop_fw(struct tg3 *);
6833
6834/* tp->lock is held. */
6835static int tg3_chip_reset(struct tg3 *tp)
6836{
6837 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07006838 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00006839 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840
David S. Millerf49639e2006-06-09 11:58:36 -07006841 tg3_nvram_lock(tp);
6842
Matt Carlson77b483f2008-08-15 14:07:24 -07006843 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
6844
David S. Millerf49639e2006-06-09 11:58:36 -07006845 /* No matching tg3_nvram_unlock() after this because
6846 * chip reset below will undo the nvram lock.
6847 */
6848 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849
Michael Chanee6a99b2007-07-18 21:49:10 -07006850 /* GRC_MISC_CFG core clock reset will clear the memory
6851 * enable bit in PCI register 4 and the MSI enable bit
6852 * on some chips, so we save relevant registers here.
6853 */
6854 tg3_save_pci_state(tp);
6855
Michael Chand9ab5ad2006-03-20 22:27:35 -08006856 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08006857 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08006858 tw32(GRC_FASTBOOT_PC, 0);
6859
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 /*
6861 * We must avoid the readl() that normally takes place.
6862 * It locks machines, causes machine checks, and other
6863 * fun things. So, temporarily disable the 5701
6864 * hardware workaround, while we do the reset.
6865 */
Michael Chan1ee582d2005-08-09 20:16:46 -07006866 write_op = tp->write32;
6867 if (write_op == tg3_write_flush_reg32)
6868 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869
Michael Chand18edcb2007-03-24 20:57:11 -07006870 /* Prevent the irq handler from reading or writing PCI registers
6871 * during chip reset when the memory enable bit in the PCI command
6872 * register may be cleared. The chip does not generate interrupt
6873 * at this time, but the irq handler may still be called due to irq
6874 * sharing or irqpoll.
6875 */
6876 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006877 for (i = 0; i < tp->irq_cnt; i++) {
6878 struct tg3_napi *tnapi = &tp->napi[i];
6879 if (tnapi->hw_status) {
6880 tnapi->hw_status->status = 0;
6881 tnapi->hw_status->status_tag = 0;
6882 }
6883 tnapi->last_tag = 0;
6884 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07006885 }
Michael Chand18edcb2007-03-24 20:57:11 -07006886 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00006887
6888 for (i = 0; i < tp->irq_cnt; i++)
6889 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07006890
Matt Carlson255ca312009-08-25 10:07:27 +00006891 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6892 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
6893 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
6894 }
6895
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 /* do the reset */
6897 val = GRC_MISC_CFG_CORECLK_RESET;
6898
6899 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
6900 if (tr32(0x7e2c) == 0x60) {
6901 tw32(0x7e2c, 0x20);
6902 }
6903 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
6904 tw32(GRC_MISC_CFG, (1 << 29));
6905 val |= (1 << 29);
6906 }
6907 }
6908
Michael Chanb5d37722006-09-27 16:06:21 -07006909 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6910 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
6911 tw32(GRC_VCPU_EXT_CTRL,
6912 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
6913 }
6914
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6916 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
6917 tw32(GRC_MISC_CFG, val);
6918
Michael Chan1ee582d2005-08-09 20:16:46 -07006919 /* restore 5701 hardware bug workaround write method */
6920 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921
6922 /* Unfortunately, we have to delay before the PCI read back.
6923 * Some 575X chips even will not respond to a PCI cfg access
6924 * when the reset command is given to the chip.
6925 *
6926 * How do these hardware designers expect things to work
6927 * properly if the PCI write is posted for a long period
6928 * of time? It is always necessary to have some method by
6929 * which a register read back can occur to push the write
6930 * out which does the reset.
6931 *
6932 * For most tg3 variants the trick below was working.
6933 * Ho hum...
6934 */
6935 udelay(120);
6936
6937 /* Flush PCI posted writes. The normal MMIO registers
6938 * are inaccessible at this time so this is the only
6939 * way to make this reliably (actually, this is no longer
6940 * the case, see above). I tried to use indirect
6941 * register read/write but this upset some 5701 variants.
6942 */
6943 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
6944
6945 udelay(120);
6946
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006947 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00006948 u16 val16;
6949
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
6951 int i;
6952 u32 cfg_val;
6953
6954 /* Wait for link training to complete. */
6955 for (i = 0; i < 5000; i++)
6956 udelay(100);
6957
6958 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
6959 pci_write_config_dword(tp->pdev, 0xc4,
6960 cfg_val | (1 << 15));
6961 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006962
Matt Carlsone7126992009-08-25 10:08:16 +00006963 /* Clear the "no snoop" and "relaxed ordering" bits. */
6964 pci_read_config_word(tp->pdev,
6965 tp->pcie_cap + PCI_EXP_DEVCTL,
6966 &val16);
6967 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
6968 PCI_EXP_DEVCTL_NOSNOOP_EN);
6969 /*
6970 * Older PCIe devices only support the 128 byte
6971 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006972 */
Matt Carlsone7126992009-08-25 10:08:16 +00006973 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
6974 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784))
6975 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006976 pci_write_config_word(tp->pdev,
6977 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00006978 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006979
6980 pcie_set_readrq(tp->pdev, 4096);
6981
6982 /* Clear error status */
6983 pci_write_config_word(tp->pdev,
6984 tp->pcie_cap + PCI_EXP_DEVSTA,
6985 PCI_EXP_DEVSTA_CED |
6986 PCI_EXP_DEVSTA_NFED |
6987 PCI_EXP_DEVSTA_FED |
6988 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989 }
6990
Michael Chanee6a99b2007-07-18 21:49:10 -07006991 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992
Michael Chand18edcb2007-03-24 20:57:11 -07006993 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
6994
Michael Chanee6a99b2007-07-18 21:49:10 -07006995 val = 0;
6996 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07006997 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07006998 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999
7000 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7001 tg3_stop_fw(tp);
7002 tw32(0x5000, 0x400);
7003 }
7004
7005 tw32(GRC_MODE, tp->grc_mode);
7006
7007 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007008 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009
7010 tw32(0xc4, val | (1 << 15));
7011 }
7012
7013 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7014 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7015 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7016 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7017 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7018 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7019 }
7020
7021 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
7022 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
7023 tw32_f(MAC_MODE, tp->mac_mode);
Michael Chan747e8f82005-07-25 12:33:22 -07007024 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
7025 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
7026 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlson3bda1252008-08-15 14:08:22 -07007027 } else if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7028 tp->mac_mode &= (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
7029 if (tp->mac_mode & MAC_MODE_APE_TX_EN)
7030 tp->mac_mode |= MAC_MODE_TDE_ENABLE;
7031 tw32_f(MAC_MODE, tp->mac_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 } else
7033 tw32_f(MAC_MODE, 0);
7034 udelay(40);
7035
Matt Carlson77b483f2008-08-15 14:07:24 -07007036 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7037
Michael Chan7a6f4362006-09-27 16:03:31 -07007038 err = tg3_poll_fw(tp);
7039 if (err)
7040 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041
Matt Carlson0a9140c2009-08-28 12:27:50 +00007042 tg3_mdio_start(tp);
7043
Matt Carlson52cdf852009-11-02 14:25:06 +00007044 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7045 u8 phy_addr;
7046
7047 phy_addr = tp->phy_addr;
7048 tp->phy_addr = TG3_PHY_PCIE_ADDR;
7049
7050 tg3_writephy(tp, TG3_PCIEPHY_BLOCK_ADDR,
7051 TG3_PCIEPHY_TXB_BLK << TG3_PCIEPHY_BLOCK_SHIFT);
7052 val = TG3_PCIEPHY_TX0CTRL1_TXOCM | TG3_PCIEPHY_TX0CTRL1_RDCTL |
7053 TG3_PCIEPHY_TX0CTRL1_TXCMV | TG3_PCIEPHY_TX0CTRL1_TKSEL |
7054 TG3_PCIEPHY_TX0CTRL1_NB_EN;
7055 tg3_writephy(tp, TG3_PCIEPHY_TX0CTRL1, val);
7056 udelay(10);
7057
7058 tg3_writephy(tp, TG3_PCIEPHY_BLOCK_ADDR,
7059 TG3_PCIEPHY_XGXS_BLK1 << TG3_PCIEPHY_BLOCK_SHIFT);
7060 val = TG3_PCIEPHY_PWRMGMT4_LOWPWR_EN |
7061 TG3_PCIEPHY_PWRMGMT4_L1PLLPD_EN;
7062 tg3_writephy(tp, TG3_PCIEPHY_PWRMGMT4, val);
7063 udelay(10);
7064
7065 tp->phy_addr = phy_addr;
7066 }
7067
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007069 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7070 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonb703df62009-12-03 08:36:21 +00007071 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
7072 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007073 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074
7075 tw32(0x7c00, val | (1 << 25));
7076 }
7077
7078 /* Reprobe ASF enable state. */
7079 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
7080 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
7081 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7082 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7083 u32 nic_cfg;
7084
7085 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7086 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
7087 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07007088 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07007089 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7091 }
7092 }
7093
7094 return 0;
7095}
7096
7097/* tp->lock is held. */
7098static void tg3_stop_fw(struct tg3 *tp)
7099{
Matt Carlson0d3031d2007-10-10 18:02:43 -07007100 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7101 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007102 /* Wait for RX cpu to ACK the previous event. */
7103 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104
7105 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007106
7107 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108
Matt Carlson7c5026a2008-05-02 16:49:29 -07007109 /* Wait for RX cpu to ACK this event. */
7110 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111 }
7112}
7113
7114/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007115static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116{
7117 int err;
7118
7119 tg3_stop_fw(tp);
7120
Michael Chan944d9802005-05-29 14:57:48 -07007121 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007123 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124 err = tg3_chip_reset(tp);
7125
Matt Carlsondaba2a62009-04-20 06:58:52 +00007126 __tg3_set_mac_addr(tp, 0);
7127
Michael Chan944d9802005-05-29 14:57:48 -07007128 tg3_write_sig_legacy(tp, kind);
7129 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130
7131 if (err)
7132 return err;
7133
7134 return 0;
7135}
7136
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137#define RX_CPU_SCRATCH_BASE 0x30000
7138#define RX_CPU_SCRATCH_SIZE 0x04000
7139#define TX_CPU_SCRATCH_BASE 0x34000
7140#define TX_CPU_SCRATCH_SIZE 0x04000
7141
7142/* tp->lock is held. */
7143static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7144{
7145 int i;
7146
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02007147 BUG_ON(offset == TX_CPU_BASE &&
7148 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149
Michael Chanb5d37722006-09-27 16:06:21 -07007150 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7151 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7152
7153 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7154 return 0;
7155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156 if (offset == RX_CPU_BASE) {
7157 for (i = 0; i < 10000; i++) {
7158 tw32(offset + CPU_STATE, 0xffffffff);
7159 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7160 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7161 break;
7162 }
7163
7164 tw32(offset + CPU_STATE, 0xffffffff);
7165 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7166 udelay(10);
7167 } else {
7168 for (i = 0; i < 10000; i++) {
7169 tw32(offset + CPU_STATE, 0xffffffff);
7170 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7171 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7172 break;
7173 }
7174 }
7175
7176 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007177 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7178 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179 return -ENODEV;
7180 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007181
7182 /* Clear firmware's nvram arbitration. */
7183 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7184 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185 return 0;
7186}
7187
7188struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007189 unsigned int fw_base;
7190 unsigned int fw_len;
7191 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192};
7193
7194/* tp->lock is held. */
7195static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7196 int cpu_scratch_size, struct fw_info *info)
7197{
Michael Chanec41c7d2006-01-17 02:40:55 -08007198 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 void (*write_op)(struct tg3 *, u32, u32);
7200
7201 if (cpu_base == TX_CPU_BASE &&
7202 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007203 netdev_err(tp->dev,
7204 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007205 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206 return -EINVAL;
7207 }
7208
7209 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7210 write_op = tg3_write_mem;
7211 else
7212 write_op = tg3_write_indirect_reg32;
7213
Michael Chan1b628152005-05-29 14:59:49 -07007214 /* It is possible that bootcode is still loading at this point.
7215 * Get the nvram lock first before halting the cpu.
7216 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007217 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007219 if (!lock_err)
7220 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 if (err)
7222 goto out;
7223
7224 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7225 write_op(tp, cpu_scratch_base + i, 0);
7226 tw32(cpu_base + CPU_STATE, 0xffffffff);
7227 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007228 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007230 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007232 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233
7234 err = 0;
7235
7236out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 return err;
7238}
7239
7240/* tp->lock is held. */
7241static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7242{
7243 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007244 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245 int err, i;
7246
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007247 fw_data = (void *)tp->fw->data;
7248
7249 /* Firmware blob starts with version numbers, followed by
7250 start address and length. We are setting complete length.
7251 length = end_address_of_bss - start_address_of_text.
7252 Remainder is the blob to be loaded contiguously
7253 from start address. */
7254
7255 info.fw_base = be32_to_cpu(fw_data[1]);
7256 info.fw_len = tp->fw->size - 12;
7257 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258
7259 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7260 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7261 &info);
7262 if (err)
7263 return err;
7264
7265 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7266 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7267 &info);
7268 if (err)
7269 return err;
7270
7271 /* Now startup only the RX cpu. */
7272 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007273 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274
7275 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007276 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 break;
7278 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7279 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007280 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281 udelay(1000);
7282 }
7283 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007284 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7285 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007286 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287 return -ENODEV;
7288 }
7289 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7290 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7291
7292 return 0;
7293}
7294
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296
7297/* tp->lock is held. */
7298static int tg3_load_tso_firmware(struct tg3 *tp)
7299{
7300 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007301 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7303 int err, i;
7304
7305 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7306 return 0;
7307
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007308 fw_data = (void *)tp->fw->data;
7309
7310 /* Firmware blob starts with version numbers, followed by
7311 start address and length. We are setting complete length.
7312 length = end_address_of_bss - start_address_of_text.
7313 Remainder is the blob to be loaded contiguously
7314 from start address. */
7315
7316 info.fw_base = be32_to_cpu(fw_data[1]);
7317 cpu_scratch_size = tp->fw_len;
7318 info.fw_len = tp->fw->size - 12;
7319 info.fw_data = &fw_data[3];
7320
Linus Torvalds1da177e2005-04-16 15:20:36 -07007321 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007322 cpu_base = RX_CPU_BASE;
7323 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325 cpu_base = TX_CPU_BASE;
7326 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7327 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7328 }
7329
7330 err = tg3_load_firmware_cpu(tp, cpu_base,
7331 cpu_scratch_base, cpu_scratch_size,
7332 &info);
7333 if (err)
7334 return err;
7335
7336 /* Now startup the cpu. */
7337 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007338 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339
7340 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007341 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342 break;
7343 tw32(cpu_base + CPU_STATE, 0xffffffff);
7344 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007345 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346 udelay(1000);
7347 }
7348 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007349 netdev_err(tp->dev,
7350 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007351 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352 return -ENODEV;
7353 }
7354 tw32(cpu_base + CPU_STATE, 0xffffffff);
7355 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7356 return 0;
7357}
7358
Linus Torvalds1da177e2005-04-16 15:20:36 -07007359
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360static int tg3_set_mac_addr(struct net_device *dev, void *p)
7361{
7362 struct tg3 *tp = netdev_priv(dev);
7363 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007364 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365
Michael Chanf9804dd2005-09-27 12:13:10 -07007366 if (!is_valid_ether_addr(addr->sa_data))
7367 return -EINVAL;
7368
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7370
Michael Chane75f7c92006-03-20 21:33:26 -08007371 if (!netif_running(dev))
7372 return 0;
7373
Michael Chan58712ef2006-04-29 18:58:01 -07007374 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007375 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007376
Michael Chan986e0ae2007-05-05 12:10:20 -07007377 addr0_high = tr32(MAC_ADDR_0_HIGH);
7378 addr0_low = tr32(MAC_ADDR_0_LOW);
7379 addr1_high = tr32(MAC_ADDR_1_HIGH);
7380 addr1_low = tr32(MAC_ADDR_1_LOW);
7381
7382 /* Skip MAC addr 1 if ASF is using it. */
7383 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7384 !(addr1_high == 0 && addr1_low == 0))
7385 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007386 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007387 spin_lock_bh(&tp->lock);
7388 __tg3_set_mac_addr(tp, skip_mac_1);
7389 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007390
Michael Chanb9ec6c12006-07-25 16:37:27 -07007391 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392}
7393
7394/* tp->lock is held. */
7395static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7396 dma_addr_t mapping, u32 maxlen_flags,
7397 u32 nic_addr)
7398{
7399 tg3_write_mem(tp,
7400 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7401 ((u64) mapping >> 32));
7402 tg3_write_mem(tp,
7403 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7404 ((u64) mapping & 0xffffffff));
7405 tg3_write_mem(tp,
7406 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7407 maxlen_flags);
7408
7409 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7410 tg3_write_mem(tp,
7411 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7412 nic_addr);
7413}
7414
7415static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007416static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007417{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007418 int i;
7419
Matt Carlson19cfaec2009-12-03 08:36:20 +00007420 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007421 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7422 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7423 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007424 } else {
7425 tw32(HOSTCC_TXCOL_TICKS, 0);
7426 tw32(HOSTCC_TXMAX_FRAMES, 0);
7427 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007428 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007429
Matt Carlson19cfaec2009-12-03 08:36:20 +00007430 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
7431 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7432 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7433 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7434 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007435 tw32(HOSTCC_RXCOL_TICKS, 0);
7436 tw32(HOSTCC_RXMAX_FRAMES, 0);
7437 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007438 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007439
David S. Miller15f98502005-05-18 22:49:26 -07007440 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7441 u32 val = ec->stats_block_coalesce_usecs;
7442
Matt Carlsonb6080e12009-09-01 13:12:00 +00007443 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7444 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7445
David S. Miller15f98502005-05-18 22:49:26 -07007446 if (!netif_carrier_ok(tp->dev))
7447 val = 0;
7448
7449 tw32(HOSTCC_STAT_COAL_TICKS, val);
7450 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007451
7452 for (i = 0; i < tp->irq_cnt - 1; i++) {
7453 u32 reg;
7454
7455 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7456 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007457 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7458 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007459 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7460 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007461
7462 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7463 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7464 tw32(reg, ec->tx_coalesce_usecs);
7465 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7466 tw32(reg, ec->tx_max_coalesced_frames);
7467 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7468 tw32(reg, ec->tx_max_coalesced_frames_irq);
7469 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007470 }
7471
7472 for (; i < tp->irq_max - 1; i++) {
7473 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007474 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007475 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007476
7477 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7478 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7479 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7480 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7481 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007482 }
David S. Miller15f98502005-05-18 22:49:26 -07007483}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484
7485/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007486static void tg3_rings_reset(struct tg3 *tp)
7487{
7488 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007489 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007490 struct tg3_napi *tnapi = &tp->napi[0];
7491
7492 /* Disable all transmit rings but the first. */
7493 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7494 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007495 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7496 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007497 else
7498 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7499
7500 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7501 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7502 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7503 BDINFO_FLAGS_DISABLED);
7504
7505
7506 /* Disable all receive return rings but the first. */
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007507 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
7508 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7509 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007510 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007511 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7512 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007513 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7514 else
7515 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7516
7517 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7518 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7519 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7520 BDINFO_FLAGS_DISABLED);
7521
7522 /* Disable interrupts */
7523 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7524
7525 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007526 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
7527 for (i = 1; i < TG3_IRQ_MAX_VECS; i++) {
7528 tp->napi[i].tx_prod = 0;
7529 tp->napi[i].tx_cons = 0;
Matt Carlsonc2353a32010-01-20 16:58:08 +00007530 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7531 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007532 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7533 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7534 }
Matt Carlsonc2353a32010-01-20 16:58:08 +00007535 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7536 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007537 } else {
7538 tp->napi[0].tx_prod = 0;
7539 tp->napi[0].tx_cons = 0;
7540 tw32_mailbox(tp->napi[0].prodmbox, 0);
7541 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7542 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007543
7544 /* Make sure the NIC-based send BD rings are disabled. */
7545 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7546 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7547 for (i = 0; i < 16; i++)
7548 tw32_tx_mbox(mbox + i * 8, 0);
7549 }
7550
7551 txrcb = NIC_SRAM_SEND_RCB;
7552 rxrcb = NIC_SRAM_RCV_RET_RCB;
7553
7554 /* Clear status block in ram. */
7555 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7556
7557 /* Set status block DMA address */
7558 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7559 ((u64) tnapi->status_mapping >> 32));
7560 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7561 ((u64) tnapi->status_mapping & 0xffffffff));
7562
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007563 if (tnapi->tx_ring) {
7564 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7565 (TG3_TX_RING_SIZE <<
7566 BDINFO_FLAGS_MAXLEN_SHIFT),
7567 NIC_SRAM_TX_BUFFER_DESC);
7568 txrcb += TG3_BDINFO_SIZE;
7569 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007570
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007571 if (tnapi->rx_rcb) {
7572 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7573 (TG3_RX_RCB_RING_SIZE(tp) <<
7574 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7575 rxrcb += TG3_BDINFO_SIZE;
7576 }
7577
7578 stblk = HOSTCC_STATBLCK_RING1;
7579
7580 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7581 u64 mapping = (u64)tnapi->status_mapping;
7582 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7583 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7584
7585 /* Clear status block in ram. */
7586 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7587
Matt Carlson19cfaec2009-12-03 08:36:20 +00007588 if (tnapi->tx_ring) {
7589 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7590 (TG3_TX_RING_SIZE <<
7591 BDINFO_FLAGS_MAXLEN_SHIFT),
7592 NIC_SRAM_TX_BUFFER_DESC);
7593 txrcb += TG3_BDINFO_SIZE;
7594 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007595
7596 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7597 (TG3_RX_RCB_RING_SIZE(tp) <<
7598 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7599
7600 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007601 rxrcb += TG3_BDINFO_SIZE;
7602 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007603}
7604
7605/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007606static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607{
7608 u32 val, rdmac_mode;
7609 int i, err, limit;
Matt Carlson21f581a2009-08-28 14:00:25 +00007610 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007611
7612 tg3_disable_ints(tp);
7613
7614 tg3_stop_fw(tp);
7615
7616 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7617
Matt Carlson859a588792010-04-05 10:19:28 +00007618 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
Michael Chane6de8ad2005-05-05 14:42:41 -07007619 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620
Matt Carlson603f1172010-02-12 14:47:10 +00007621 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007622 tg3_phy_reset(tp);
7623
Linus Torvalds1da177e2005-04-16 15:20:36 -07007624 err = tg3_chip_reset(tp);
7625 if (err)
7626 return err;
7627
7628 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7629
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007630 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007631 val = tr32(TG3_CPMU_CTRL);
7632 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7633 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007634
7635 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7636 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7637 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7638 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7639
7640 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7641 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7642 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7643 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7644
7645 val = tr32(TG3_CPMU_HST_ACC);
7646 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7647 val |= CPMU_HST_ACC_MACCLK_6_25;
7648 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007649 }
7650
Matt Carlson33466d92009-04-20 06:57:41 +00007651 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7652 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7653 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7654 PCIE_PWR_MGMT_L1_THRESH_4MS;
7655 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007656
7657 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7658 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7659
7660 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007661
Matt Carlsonf40386c2009-11-02 14:24:02 +00007662 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7663 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007664 }
7665
Matt Carlson614b0592010-01-20 16:58:02 +00007666 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
7667 u32 grc_mode = tr32(GRC_MODE);
7668
7669 /* Access the lower 1K of PL PCIE block registers. */
7670 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7671 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7672
7673 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7674 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7675 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7676
7677 tw32(GRC_MODE, grc_mode);
7678 }
7679
Matt Carlsoncea46462010-04-12 06:58:24 +00007680 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7681 u32 grc_mode = tr32(GRC_MODE);
7682
7683 /* Access the lower 1K of PL PCIE block registers. */
7684 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7685 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7686
7687 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5);
7688 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
7689 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
7690
7691 tw32(GRC_MODE, grc_mode);
Matt Carlsona977dbe2010-04-12 06:58:26 +00007692
7693 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7694 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7695 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7696 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00007697 }
7698
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699 /* This works around an issue with Athlon chipsets on
7700 * B3 tigon3 silicon. This bit has no effect on any
7701 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07007702 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007703 */
Matt Carlson795d01c2007-10-07 23:28:17 -07007704 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7705 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7706 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7707 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007709
7710 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7711 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7712 val = tr32(TG3PCI_PCISTATE);
7713 val |= PCISTATE_RETRY_SAME_DMA;
7714 tw32(TG3PCI_PCISTATE, val);
7715 }
7716
Matt Carlson0d3031d2007-10-10 18:02:43 -07007717 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7718 /* Allow reads and writes to the
7719 * APE register and memory space.
7720 */
7721 val = tr32(TG3PCI_PCISTATE);
7722 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
7723 PCISTATE_ALLOW_APE_SHMEM_WR;
7724 tw32(TG3PCI_PCISTATE, val);
7725 }
7726
Linus Torvalds1da177e2005-04-16 15:20:36 -07007727 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7728 /* Enable some hw fixes. */
7729 val = tr32(TG3PCI_MSI_DATA);
7730 val |= (1 << 26) | (1 << 28) | (1 << 29);
7731 tw32(TG3PCI_MSI_DATA, val);
7732 }
7733
7734 /* Descriptor ring init may make accesses to the
7735 * NIC SRAM area to setup the TX descriptors, so we
7736 * can only do this after the hardware has been
7737 * successfully reset.
7738 */
Michael Chan32d8c572006-07-25 16:38:29 -07007739 err = tg3_init_rings(tp);
7740 if (err)
7741 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007742
Matt Carlsonb703df62009-12-03 08:36:21 +00007743 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7744 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007745 val = tr32(TG3PCI_DMA_RW_CTRL) &
7746 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00007747 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
7748 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00007749 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
7750 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
7751 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007752 /* This value is determined during the probe time DMA
7753 * engine test, tg3_test_dma.
7754 */
7755 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757
7758 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7759 GRC_MODE_4X_NIC_SEND_RINGS |
7760 GRC_MODE_NO_TX_PHDR_CSUM |
7761 GRC_MODE_NO_RX_PHDR_CSUM);
7762 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07007763
7764 /* Pseudo-header checksum is done by hardware logic and not
7765 * the offload processers, so make the chip do the pseudo-
7766 * header checksums on receive. For transmit it is more
7767 * convenient to do the pseudo-header checksum in software
7768 * as Linux does that on transmit for us in all cases.
7769 */
7770 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771
7772 tw32(GRC_MODE,
7773 tp->grc_mode |
7774 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7775
7776 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7777 val = tr32(GRC_MISC_CFG);
7778 val &= ~0xff;
7779 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7780 tw32(GRC_MISC_CFG, val);
7781
7782 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07007783 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007784 /* Do nothing. */
7785 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
7786 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
7787 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
7788 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
7789 else
7790 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
7791 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
7792 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Matt Carlson859a588792010-04-05 10:19:28 +00007793 } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007794 int fw_len;
7795
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007796 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007797 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
7798 tw32(BUFMGR_MB_POOL_ADDR,
7799 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
7800 tw32(BUFMGR_MB_POOL_SIZE,
7801 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
7802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803
Michael Chan0f893dc2005-07-25 12:30:38 -07007804 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007805 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7806 tp->bufmgr_config.mbuf_read_dma_low_water);
7807 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7808 tp->bufmgr_config.mbuf_mac_rx_low_water);
7809 tw32(BUFMGR_MB_HIGH_WATER,
7810 tp->bufmgr_config.mbuf_high_water);
7811 } else {
7812 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7813 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
7814 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7815 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
7816 tw32(BUFMGR_MB_HIGH_WATER,
7817 tp->bufmgr_config.mbuf_high_water_jumbo);
7818 }
7819 tw32(BUFMGR_DMA_LOW_WATER,
7820 tp->bufmgr_config.dma_low_water);
7821 tw32(BUFMGR_DMA_HIGH_WATER,
7822 tp->bufmgr_config.dma_high_water);
7823
7824 tw32(BUFMGR_MODE, BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE);
7825 for (i = 0; i < 2000; i++) {
7826 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
7827 break;
7828 udelay(10);
7829 }
7830 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007831 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832 return -ENODEV;
7833 }
7834
7835 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07007836 val = tp->rx_pending / 8;
7837 if (val == 0)
7838 val = 1;
7839 else if (val > tp->rx_std_max_post)
7840 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07007841 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7842 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
7843 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
7844
7845 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
7846 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
7847 }
Michael Chanf92905d2006-06-29 20:14:29 -07007848
7849 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007850
7851 /* Initialize TG3_BDINFO's at:
7852 * RCVDBDI_STD_BD: standard eth size rx ring
7853 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
7854 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
7855 *
7856 * like so:
7857 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
7858 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
7859 * ring attribute flags
7860 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
7861 *
7862 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
7863 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
7864 *
7865 * The size of each ring is fixed in the firmware, but the location is
7866 * configurable.
7867 */
7868 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007869 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007871 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlson13fa95b02010-01-12 10:11:36 +00007872 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
Matt Carlson87668d32009-11-13 13:03:34 +00007873 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
7874 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007876 /* Disable the mini ring */
7877 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
7879 BDINFO_FLAGS_DISABLED);
7880
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007881 /* Program the jumbo buffer descriptor ring control
7882 * blocks on those devices that have them.
7883 */
Matt Carlson8f666b02009-08-28 13:58:24 +00007884 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007885 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007886 /* Setup replenish threshold. */
7887 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
7888
Michael Chan0f893dc2005-07-25 12:30:38 -07007889 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007891 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007893 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlson79ed5ac2009-08-28 14:00:55 +00007895 (RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT) |
7896 BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlson5fd68fb2010-01-20 16:58:07 +00007897 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
Matt Carlson87668d32009-11-13 13:03:34 +00007898 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
7899 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007900 } else {
7901 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
7902 BDINFO_FLAGS_DISABLED);
7903 }
7904
Matt Carlsonb703df62009-12-03 08:36:21 +00007905 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7906 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007907 val = (RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT) |
Matt Carlson04380d42010-04-12 06:58:29 +00007908 (TG3_RX_STD_DMA_SZ << 2);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007909 else
Matt Carlson04380d42010-04-12 06:58:29 +00007910 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007911 } else
7912 val = RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT;
7913
7914 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915
Matt Carlson411da642009-11-13 13:03:46 +00007916 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00007917 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007918
Matt Carlson411da642009-11-13 13:03:46 +00007919 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Matt Carlson21f581a2009-08-28 14:00:25 +00007920 tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00007921 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007922
Matt Carlsonb703df62009-12-03 08:36:21 +00007923 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
7924 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007925 tw32(STD_REPLENISH_LWM, 32);
7926 tw32(JMB_REPLENISH_LWM, 16);
7927 }
7928
Matt Carlson2d31eca2009-09-01 12:53:31 +00007929 tg3_rings_reset(tp);
7930
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07007932 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007933
7934 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00007935 tw32(MAC_RX_MTU_SIZE,
7936 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007937
7938 /* The slot time is changed by tg3_setup_phy if we
7939 * run at gigabit with half duplex.
7940 */
7941 tw32(MAC_TX_LENGTHS,
7942 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
7943 (6 << TX_LENGTHS_IPG_SHIFT) |
7944 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
7945
7946 /* Receive rules. */
7947 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
7948 tw32(RCVLPC_CONFIG, 0x0181);
7949
7950 /* Calculate RDMAC_MODE setting early, we need it to determine
7951 * the RCVLPC_STATE_ENABLE mask.
7952 */
7953 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
7954 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
7955 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
7956 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
7957 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07007958
Matt Carlson0339e4e2010-02-12 14:47:09 +00007959 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
7960 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
7961
Matt Carlson57e69832008-05-25 23:48:31 -07007962 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007963 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7964 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07007965 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
7966 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
7967 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
7968
Michael Chan85e94ce2005-04-21 17:05:28 -07007969 /* If statement applies to 5705 and 5750 PCI devices only */
7970 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7971 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7972 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007973 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07007974 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007975 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
7976 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7977 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
7978 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7979 }
7980 }
7981
Michael Chan85e94ce2005-04-21 17:05:28 -07007982 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
7983 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7984
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08007986 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
7987
Matt Carlsone849cdc2009-11-13 13:03:38 +00007988 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
7989 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08007990 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
7991 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007992
7993 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07007994 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
7995 val = tr32(RCVLPC_STATS_ENABLE);
7996 val &= ~RCVLPC_STATSENAB_DACK_FIX;
7997 tw32(RCVLPC_STATS_ENABLE, val);
7998 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
7999 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008000 val = tr32(RCVLPC_STATS_ENABLE);
8001 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8002 tw32(RCVLPC_STATS_ENABLE, val);
8003 } else {
8004 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8005 }
8006 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8007 tw32(SNDDATAI_STATSENAB, 0xffffff);
8008 tw32(SNDDATAI_STATSCTRL,
8009 (SNDDATAI_SCTRL_ENABLE |
8010 SNDDATAI_SCTRL_FASTUPD));
8011
8012 /* Setup host coalescing engine. */
8013 tw32(HOSTCC_MODE, 0);
8014 for (i = 0; i < 2000; i++) {
8015 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8016 break;
8017 udelay(10);
8018 }
8019
Michael Chand244c892005-07-05 14:42:33 -07008020 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008021
Linus Torvalds1da177e2005-04-16 15:20:36 -07008022 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8023 /* Status/statistics block address. See tg3_timer,
8024 * the tg3_periodic_fetch_stats call there, and
8025 * tg3_get_stats to see how this works for 5705/5750 chips.
8026 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008027 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8028 ((u64) tp->stats_mapping >> 32));
8029 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8030 ((u64) tp->stats_mapping & 0xffffffff));
8031 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008032
Linus Torvalds1da177e2005-04-16 15:20:36 -07008033 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008034
8035 /* Clear statistics and status block memory areas */
8036 for (i = NIC_SRAM_STATS_BLK;
8037 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8038 i += sizeof(u32)) {
8039 tg3_write_mem(tp, i, 0);
8040 udelay(40);
8041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008042 }
8043
8044 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8045
8046 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8047 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
8048 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8049 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8050
Michael Chanc94e3942005-09-27 12:12:42 -07008051 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
8052 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
8053 /* reset to prevent losing 1st rx packet intermittently */
8054 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8055 udelay(10);
8056 }
8057
Matt Carlson3bda1252008-08-15 14:08:22 -07008058 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8059 tp->mac_mode &= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
8060 else
8061 tp->mac_mode = 0;
8062 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008063 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008064 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
8065 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
8066 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8067 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008068 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8069 udelay(40);
8070
Michael Chan314fba32005-04-21 17:07:04 -07008071 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08008072 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008073 * register to preserve the GPIO settings for LOMs. The GPIOs,
8074 * whether used as inputs or outputs, are set by boot code after
8075 * reset.
8076 */
Michael Chan9d26e212006-12-07 00:21:14 -08008077 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008078 u32 gpio_mask;
8079
Michael Chan9d26e212006-12-07 00:21:14 -08008080 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8081 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8082 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008083
8084 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8085 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8086 GRC_LCLCTRL_GPIO_OUTPUT3;
8087
Michael Chanaf36e6b2006-03-23 01:28:06 -08008088 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8089 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8090
Gary Zambranoaaf84462007-05-05 11:51:45 -07008091 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008092 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8093
8094 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08008095 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
8096 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8097 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008099 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8100 udelay(100);
8101
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008102 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX) {
8103 val = tr32(MSGINT_MODE);
8104 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8105 tw32(MSGINT_MODE, val);
8106 }
8107
Linus Torvalds1da177e2005-04-16 15:20:36 -07008108 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8109 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8110 udelay(40);
8111 }
8112
8113 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8114 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8115 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8116 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8117 WDMAC_MODE_LNGREAD_ENAB);
8118
Michael Chan85e94ce2005-04-21 17:05:28 -07008119 /* If statement applies to 5705 and 5750 PCI devices only */
8120 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8121 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
8122 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
Matt Carlson29ea0952009-08-25 10:07:54 +00008123 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008124 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8125 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8126 /* nothing */
8127 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8128 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
8129 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
8130 val |= WDMAC_MODE_RX_ACCEL;
8131 }
8132 }
8133
Michael Chand9ab5ad2006-03-20 22:27:35 -08008134 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08008135 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07008136 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008137
Matt Carlson788a0352009-11-02 14:26:03 +00008138 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8139 val |= WDMAC_MODE_BURST_ALL_DATA;
8140
Linus Torvalds1da177e2005-04-16 15:20:36 -07008141 tw32_f(WDMAC_MODE, val);
8142 udelay(40);
8143
Matt Carlson9974a352007-10-07 23:27:28 -07008144 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8145 u16 pcix_cmd;
8146
8147 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8148 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008149 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008150 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8151 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008152 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008153 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8154 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008155 }
Matt Carlson9974a352007-10-07 23:27:28 -07008156 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8157 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008158 }
8159
8160 tw32_f(RDMAC_MODE, rdmac_mode);
8161 udelay(40);
8162
8163 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8164 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8165 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008166
8167 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8168 tw32(SNDDATAC_MODE,
8169 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8170 else
8171 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8172
Linus Torvalds1da177e2005-04-16 15:20:36 -07008173 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8174 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
8175 tw32(RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ);
8176 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008177 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8178 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008179 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008180 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008181 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8182 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008183 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8184
8185 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8186 err = tg3_load_5701_a0_firmware_fix(tp);
8187 if (err)
8188 return err;
8189 }
8190
Linus Torvalds1da177e2005-04-16 15:20:36 -07008191 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8192 err = tg3_load_tso_firmware(tp);
8193 if (err)
8194 return err;
8195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008196
8197 tp->tx_mode = TX_MODE_ENABLE;
8198 tw32_f(MAC_TX_MODE, tp->tx_mode);
8199 udelay(100);
8200
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008201 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8202 u32 reg = MAC_RSS_INDIR_TBL_0;
8203 u8 *ent = (u8 *)&val;
8204
8205 /* Setup the indirection table */
8206 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8207 int idx = i % sizeof(val);
8208
8209 ent[idx] = i % (tp->irq_cnt - 1);
8210 if (idx == sizeof(val) - 1) {
8211 tw32(reg, val);
8212 reg += 4;
8213 }
8214 }
8215
8216 /* Setup the "secret" hash key. */
8217 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8218 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8219 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8220 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8221 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8222 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8223 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8224 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8225 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8226 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8227 }
8228
Linus Torvalds1da177e2005-04-16 15:20:36 -07008229 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08008230 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08008231 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8232
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008233 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8234 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8235 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8236 RX_MODE_RSS_IPV6_HASH_EN |
8237 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8238 RX_MODE_RSS_IPV4_HASH_EN |
8239 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8240
Linus Torvalds1da177e2005-04-16 15:20:36 -07008241 tw32_f(MAC_RX_MODE, tp->rx_mode);
8242 udelay(10);
8243
Linus Torvalds1da177e2005-04-16 15:20:36 -07008244 tw32(MAC_LED_CTRL, tp->led_ctrl);
8245
8246 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Michael Chanc94e3942005-09-27 12:12:42 -07008247 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8249 udelay(10);
8250 }
8251 tw32_f(MAC_RX_MODE, tp->rx_mode);
8252 udelay(10);
8253
8254 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
8255 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
8256 !(tp->tg3_flags2 & TG3_FLG2_SERDES_PREEMPHASIS)) {
8257 /* Set drive transmission level to 1.2V */
8258 /* only if the signal pre-emphasis bit is not set */
8259 val = tr32(MAC_SERDES_CFG);
8260 val &= 0xfffff000;
8261 val |= 0x880;
8262 tw32(MAC_SERDES_CFG, val);
8263 }
8264 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8265 tw32(MAC_SERDES_CFG, 0x616000);
8266 }
8267
8268 /* Prevent chip from dropping frames when flow control
8269 * is enabled.
8270 */
Matt Carlson666bc832010-01-20 16:58:03 +00008271 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8272 val = 1;
8273 else
8274 val = 2;
8275 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008276
8277 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
8278 (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
8279 /* Use hardware link auto-negotiation */
8280 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8281 }
8282
Michael Chand4d2c552006-03-20 17:47:20 -08008283 if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
8284 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8285 u32 tmp;
8286
8287 tmp = tr32(SERDES_RX_CTRL);
8288 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8289 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8290 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8291 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8292 }
8293
Matt Carlsondd477002008-05-25 23:45:58 -07008294 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
8295 if (tp->link_config.phy_is_low_power) {
8296 tp->link_config.phy_is_low_power = 0;
8297 tp->link_config.speed = tp->link_config.orig_speed;
8298 tp->link_config.duplex = tp->link_config.orig_duplex;
8299 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008301
Matt Carlsondd477002008-05-25 23:45:58 -07008302 err = tg3_setup_phy(tp, 0);
8303 if (err)
8304 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008305
Matt Carlsondd477002008-05-25 23:45:58 -07008306 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +00008307 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008308 u32 tmp;
8309
8310 /* Clear CRC stats. */
8311 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8312 tg3_writephy(tp, MII_TG3_TEST1,
8313 tmp | MII_TG3_TEST1_CRC_EN);
8314 tg3_readphy(tp, 0x14, &tmp);
8315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008316 }
8317 }
8318
8319 __tg3_set_rx_mode(tp->dev);
8320
8321 /* Initialize receive rules. */
8322 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8323 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8324 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8325 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8326
Michael Chan4cf78e42005-07-25 12:29:19 -07008327 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008328 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008329 limit = 8;
8330 else
8331 limit = 16;
8332 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8333 limit -= 4;
8334 switch (limit) {
8335 case 16:
8336 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8337 case 15:
8338 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8339 case 14:
8340 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8341 case 13:
8342 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8343 case 12:
8344 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8345 case 11:
8346 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8347 case 10:
8348 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8349 case 9:
8350 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8351 case 8:
8352 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8353 case 7:
8354 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8355 case 6:
8356 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8357 case 5:
8358 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8359 case 4:
8360 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8361 case 3:
8362 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8363 case 2:
8364 case 1:
8365
8366 default:
8367 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008369
Matt Carlson9ce768e2007-10-11 19:49:11 -07008370 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8371 /* Write our heartbeat update interval to APE. */
8372 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8373 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008374
Linus Torvalds1da177e2005-04-16 15:20:36 -07008375 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8376
Linus Torvalds1da177e2005-04-16 15:20:36 -07008377 return 0;
8378}
8379
8380/* Called at device open time to get the chip ready for
8381 * packet processing. Invoked with tp->lock held.
8382 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008383static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008385 tg3_switch_clocks(tp);
8386
8387 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8388
Matt Carlson2f751b62008-08-04 23:17:34 -07008389 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008390}
8391
8392#define TG3_STAT_ADD32(PSTAT, REG) \
8393do { u32 __val = tr32(REG); \
8394 (PSTAT)->low += __val; \
8395 if ((PSTAT)->low < __val) \
8396 (PSTAT)->high += 1; \
8397} while (0)
8398
8399static void tg3_periodic_fetch_stats(struct tg3 *tp)
8400{
8401 struct tg3_hw_stats *sp = tp->hw_stats;
8402
8403 if (!netif_carrier_ok(tp->dev))
8404 return;
8405
8406 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8407 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8408 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8409 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8410 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8411 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8412 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8413 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8414 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8415 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8416 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8417 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8418 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8419
8420 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8421 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8422 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8423 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8424 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8425 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8426 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8427 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8428 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8429 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8430 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8431 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8432 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8433 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008434
8435 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
8436 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8437 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008438}
8439
8440static void tg3_timer(unsigned long __opaque)
8441{
8442 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008443
Michael Chanf475f162006-03-27 23:20:14 -08008444 if (tp->irq_sync)
8445 goto restart_timer;
8446
David S. Millerf47c11e2005-06-24 20:18:35 -07008447 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008448
David S. Millerfac9b832005-05-18 22:46:34 -07008449 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8450 /* All of this garbage is because when using non-tagged
8451 * IRQ status the mailbox/status_block protocol the chip
8452 * uses with the cpu is race prone.
8453 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008454 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008455 tw32(GRC_LOCAL_CTRL,
8456 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8457 } else {
8458 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008459 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008461
David S. Millerfac9b832005-05-18 22:46:34 -07008462 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8463 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008464 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008465 schedule_work(&tp->reset_task);
8466 return;
8467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008468 }
8469
Linus Torvalds1da177e2005-04-16 15:20:36 -07008470 /* This part only runs once per second. */
8471 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008472 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8473 tg3_periodic_fetch_stats(tp);
8474
Linus Torvalds1da177e2005-04-16 15:20:36 -07008475 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8476 u32 mac_stat;
8477 int phy_event;
8478
8479 mac_stat = tr32(MAC_STATUS);
8480
8481 phy_event = 0;
8482 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) {
8483 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8484 phy_event = 1;
8485 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8486 phy_event = 1;
8487
8488 if (phy_event)
8489 tg3_setup_phy(tp, 0);
8490 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8491 u32 mac_stat = tr32(MAC_STATUS);
8492 int need_setup = 0;
8493
8494 if (netif_carrier_ok(tp->dev) &&
8495 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8496 need_setup = 1;
8497 }
8498 if (! netif_carrier_ok(tp->dev) &&
8499 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8500 MAC_STATUS_SIGNAL_DET))) {
8501 need_setup = 1;
8502 }
8503 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008504 if (!tp->serdes_counter) {
8505 tw32_f(MAC_MODE,
8506 (tp->mac_mode &
8507 ~MAC_MODE_PORT_MODE_MASK));
8508 udelay(40);
8509 tw32_f(MAC_MODE, tp->mac_mode);
8510 udelay(40);
8511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008512 tg3_setup_phy(tp, 0);
8513 }
Michael Chan747e8f82005-07-25 12:33:22 -07008514 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
8515 tg3_serdes_parallel_detect(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516
8517 tp->timer_counter = tp->timer_multiplier;
8518 }
8519
Michael Chan130b8e42006-09-27 16:00:40 -07008520 /* Heartbeat is only sent once every 2 seconds.
8521 *
8522 * The heartbeat is to tell the ASF firmware that the host
8523 * driver is still alive. In the event that the OS crashes,
8524 * ASF needs to reset the hardware to free up the FIFO space
8525 * that may be filled with rx packets destined for the host.
8526 * If the FIFO is full, ASF will no longer function properly.
8527 *
8528 * Unintended resets have been reported on real time kernels
8529 * where the timer doesn't run on time. Netpoll will also have
8530 * same problem.
8531 *
8532 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8533 * to check the ring condition when the heartbeat is expiring
8534 * before doing the reset. This will prevent most unintended
8535 * resets.
8536 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008537 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008538 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8539 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008540 tg3_wait_for_event_ack(tp);
8541
Michael Chanbbadf502006-04-06 21:46:34 -07008542 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008543 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008544 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008545 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8546 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008547
8548 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008549 }
8550 tp->asf_counter = tp->asf_multiplier;
8551 }
8552
David S. Millerf47c11e2005-06-24 20:18:35 -07008553 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008554
Michael Chanf475f162006-03-27 23:20:14 -08008555restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008556 tp->timer.expires = jiffies + tp->timer_offset;
8557 add_timer(&tp->timer);
8558}
8559
Matt Carlson4f125f42009-09-01 12:55:02 +00008560static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008561{
David Howells7d12e782006-10-05 14:55:46 +01008562 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008563 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008564 char *name;
8565 struct tg3_napi *tnapi = &tp->napi[irq_num];
8566
8567 if (tp->irq_cnt == 1)
8568 name = tp->dev->name;
8569 else {
8570 name = &tnapi->irq_lbl[0];
8571 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8572 name[IFNAMSIZ-1] = 0;
8573 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008574
Matt Carlson679563f2009-09-01 12:55:46 +00008575 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008576 fn = tg3_msi;
8577 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8578 fn = tg3_msi_1shot;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008579 flags = IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008580 } else {
8581 fn = tg3_interrupt;
8582 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8583 fn = tg3_interrupt_tagged;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008584 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008585 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008586
8587 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008588}
8589
Michael Chan79381092005-04-21 17:13:59 -07008590static int tg3_test_interrupt(struct tg3 *tp)
8591{
Matt Carlson09943a12009-08-28 14:01:57 +00008592 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008593 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008594 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008595 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008596
Michael Chand4bc3922005-05-29 14:59:20 -07008597 if (!netif_running(dev))
8598 return -ENODEV;
8599
Michael Chan79381092005-04-21 17:13:59 -07008600 tg3_disable_ints(tp);
8601
Matt Carlson4f125f42009-09-01 12:55:02 +00008602 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008603
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008604 /*
8605 * Turn off MSI one shot mode. Otherwise this test has no
8606 * observable way to know whether the interrupt was delivered.
8607 */
Matt Carlsonb703df62009-12-03 08:36:21 +00008608 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8609 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008610 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8611 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8612 tw32(MSGINT_MODE, val);
8613 }
8614
Matt Carlson4f125f42009-09-01 12:55:02 +00008615 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008616 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008617 if (err)
8618 return err;
8619
Matt Carlson898a56f2009-08-28 14:02:40 +00008620 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008621 tg3_enable_ints(tp);
8622
8623 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008624 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07008625
8626 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07008627 u32 int_mbox, misc_host_ctrl;
8628
Matt Carlson898a56f2009-08-28 14:02:40 +00008629 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07008630 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8631
8632 if ((int_mbox != 0) ||
8633 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8634 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07008635 break;
Michael Chanb16250e2006-09-27 16:10:14 -07008636 }
8637
Michael Chan79381092005-04-21 17:13:59 -07008638 msleep(10);
8639 }
8640
8641 tg3_disable_ints(tp);
8642
Matt Carlson4f125f42009-09-01 12:55:02 +00008643 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008644
Matt Carlson4f125f42009-09-01 12:55:02 +00008645 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008646
8647 if (err)
8648 return err;
8649
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008650 if (intr_ok) {
8651 /* Reenable MSI one shot mode. */
Matt Carlsonb703df62009-12-03 08:36:21 +00008652 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8653 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008654 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8655 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8656 tw32(MSGINT_MODE, val);
8657 }
Michael Chan79381092005-04-21 17:13:59 -07008658 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008659 }
Michael Chan79381092005-04-21 17:13:59 -07008660
8661 return -EIO;
8662}
8663
8664/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8665 * successfully restored
8666 */
8667static int tg3_test_msi(struct tg3 *tp)
8668{
Michael Chan79381092005-04-21 17:13:59 -07008669 int err;
8670 u16 pci_cmd;
8671
8672 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8673 return 0;
8674
8675 /* Turn off SERR reporting in case MSI terminates with Master
8676 * Abort.
8677 */
8678 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8679 pci_write_config_word(tp->pdev, PCI_COMMAND,
8680 pci_cmd & ~PCI_COMMAND_SERR);
8681
8682 err = tg3_test_interrupt(tp);
8683
8684 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8685
8686 if (!err)
8687 return 0;
8688
8689 /* other failures */
8690 if (err != -EIO)
8691 return err;
8692
8693 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008694 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
8695 "to INTx mode. Please report this failure to the PCI "
8696 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07008697
Matt Carlson4f125f42009-09-01 12:55:02 +00008698 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00008699
Michael Chan79381092005-04-21 17:13:59 -07008700 pci_disable_msi(tp->pdev);
8701
8702 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
Andre Detschdc8bf1b2010-04-26 07:27:07 +00008703 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07008704
Matt Carlson4f125f42009-09-01 12:55:02 +00008705 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008706 if (err)
8707 return err;
8708
8709 /* Need to reset the chip because the MSI cycle may have terminated
8710 * with Master Abort.
8711 */
David S. Millerf47c11e2005-06-24 20:18:35 -07008712 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008713
Michael Chan944d9802005-05-29 14:57:48 -07008714 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008715 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008716
David S. Millerf47c11e2005-06-24 20:18:35 -07008717 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008718
8719 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00008720 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07008721
8722 return err;
8723}
8724
Matt Carlson9e9fd122009-01-19 16:57:45 -08008725static int tg3_request_firmware(struct tg3 *tp)
8726{
8727 const __be32 *fw_data;
8728
8729 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008730 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
8731 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008732 return -ENOENT;
8733 }
8734
8735 fw_data = (void *)tp->fw->data;
8736
8737 /* Firmware blob starts with version numbers, followed by
8738 * start address and _full_ length including BSS sections
8739 * (which must be longer than the actual data, of course
8740 */
8741
8742 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
8743 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008744 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
8745 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08008746 release_firmware(tp->fw);
8747 tp->fw = NULL;
8748 return -EINVAL;
8749 }
8750
8751 /* We no longer need firmware; we have it. */
8752 tp->fw_needed = NULL;
8753 return 0;
8754}
8755
Matt Carlson679563f2009-09-01 12:55:46 +00008756static bool tg3_enable_msix(struct tg3 *tp)
8757{
8758 int i, rc, cpus = num_online_cpus();
8759 struct msix_entry msix_ent[tp->irq_max];
8760
8761 if (cpus == 1)
8762 /* Just fallback to the simpler MSI mode. */
8763 return false;
8764
8765 /*
8766 * We want as many rx rings enabled as there are cpus.
8767 * The first MSIX vector only deals with link interrupts, etc,
8768 * so we add one to the number of vectors we are requesting.
8769 */
8770 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
8771
8772 for (i = 0; i < tp->irq_max; i++) {
8773 msix_ent[i].entry = i;
8774 msix_ent[i].vector = 0;
8775 }
8776
8777 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
8778 if (rc != 0) {
8779 if (rc < TG3_RSS_MIN_NUM_MSIX_VECS)
8780 return false;
8781 if (pci_enable_msix(tp->pdev, msix_ent, rc))
8782 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00008783 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
8784 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00008785 tp->irq_cnt = rc;
8786 }
8787
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008788 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
8789
Matt Carlson679563f2009-09-01 12:55:46 +00008790 for (i = 0; i < tp->irq_max; i++)
8791 tp->napi[i].irq_vec = msix_ent[i].vector;
8792
Matt Carlson19cfaec2009-12-03 08:36:20 +00008793 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
8794 tp->tg3_flags3 |= TG3_FLG3_ENABLE_TSS;
8795 tp->dev->real_num_tx_queues = tp->irq_cnt - 1;
8796 } else
8797 tp->dev->real_num_tx_queues = 1;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008798
Matt Carlson679563f2009-09-01 12:55:46 +00008799 return true;
8800}
8801
Matt Carlson07b01732009-08-28 14:01:15 +00008802static void tg3_ints_init(struct tg3 *tp)
8803{
Matt Carlson679563f2009-09-01 12:55:46 +00008804 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
8805 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00008806 /* All MSI supporting chips should support tagged
8807 * status. Assert that this is the case.
8808 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00008809 netdev_warn(tp->dev,
8810 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00008811 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00008812 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008813
Matt Carlson679563f2009-09-01 12:55:46 +00008814 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
8815 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
8816 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
8817 pci_enable_msi(tp->pdev) == 0)
8818 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
8819
8820 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
8821 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008822 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8823 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00008824 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
8825 }
8826defcfg:
8827 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
8828 tp->irq_cnt = 1;
8829 tp->napi[0].irq_vec = tp->pdev->irq;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008830 tp->dev->real_num_tx_queues = 1;
Matt Carlson679563f2009-09-01 12:55:46 +00008831 }
Matt Carlson07b01732009-08-28 14:01:15 +00008832}
8833
8834static void tg3_ints_fini(struct tg3 *tp)
8835{
Matt Carlson679563f2009-09-01 12:55:46 +00008836 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8837 pci_disable_msix(tp->pdev);
8838 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
8839 pci_disable_msi(tp->pdev);
8840 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008841 tp->tg3_flags3 &= ~TG3_FLG3_ENABLE_RSS;
Matt Carlson07b01732009-08-28 14:01:15 +00008842}
8843
Linus Torvalds1da177e2005-04-16 15:20:36 -07008844static int tg3_open(struct net_device *dev)
8845{
8846 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00008847 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008848
Matt Carlson9e9fd122009-01-19 16:57:45 -08008849 if (tp->fw_needed) {
8850 err = tg3_request_firmware(tp);
8851 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8852 if (err)
8853 return err;
8854 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00008855 netdev_warn(tp->dev, "TSO capability disabled\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08008856 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
8857 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00008858 netdev_notice(tp->dev, "TSO capability restored\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08008859 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
8860 }
8861 }
8862
Michael Chanc49a1562006-12-17 17:07:29 -08008863 netif_carrier_off(tp->dev);
8864
Michael Chanbc1c7562006-03-20 17:48:03 -08008865 err = tg3_set_power_state(tp, PCI_D0);
Matt Carlson2f751b62008-08-04 23:17:34 -07008866 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08008867 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07008868
8869 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08008870
Linus Torvalds1da177e2005-04-16 15:20:36 -07008871 tg3_disable_ints(tp);
8872 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
8873
David S. Millerf47c11e2005-06-24 20:18:35 -07008874 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008875
Matt Carlson679563f2009-09-01 12:55:46 +00008876 /*
8877 * Setup interrupts first so we know how
8878 * many NAPI resources to allocate
8879 */
8880 tg3_ints_init(tp);
8881
Linus Torvalds1da177e2005-04-16 15:20:36 -07008882 /* The placement of this call is tied
8883 * to the setup and use of Host TX descriptors.
8884 */
8885 err = tg3_alloc_consistent(tp);
8886 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008887 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008888
Matt Carlsonfed97812009-09-01 13:10:19 +00008889 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07008890
Matt Carlson4f125f42009-09-01 12:55:02 +00008891 for (i = 0; i < tp->irq_cnt; i++) {
8892 struct tg3_napi *tnapi = &tp->napi[i];
8893 err = tg3_request_irq(tp, i);
8894 if (err) {
8895 for (i--; i >= 0; i--)
8896 free_irq(tnapi->irq_vec, tnapi);
8897 break;
8898 }
8899 }
Matt Carlson07b01732009-08-28 14:01:15 +00008900
8901 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008902 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00008903
David S. Millerf47c11e2005-06-24 20:18:35 -07008904 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008905
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008906 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008907 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07008908 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008909 tg3_free_rings(tp);
8910 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07008911 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8912 tp->timer_offset = HZ;
8913 else
8914 tp->timer_offset = HZ / 10;
8915
8916 BUG_ON(tp->timer_offset > HZ);
8917 tp->timer_counter = tp->timer_multiplier =
8918 (HZ / tp->timer_offset);
8919 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07008920 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008921
8922 init_timer(&tp->timer);
8923 tp->timer.expires = jiffies + tp->timer_offset;
8924 tp->timer.data = (unsigned long) tp;
8925 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008926 }
8927
David S. Millerf47c11e2005-06-24 20:18:35 -07008928 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008929
Matt Carlson07b01732009-08-28 14:01:15 +00008930 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008931 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008932
Michael Chan79381092005-04-21 17:13:59 -07008933 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
8934 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07008935
Michael Chan79381092005-04-21 17:13:59 -07008936 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07008937 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07008938 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07008939 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07008940 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008941
Matt Carlson679563f2009-09-01 12:55:46 +00008942 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07008943 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008944
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008945 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
Matt Carlsonb703df62009-12-03 08:36:21 +00008946 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008947 (tp->tg3_flags2 & TG3_FLG2_USING_MSI) &&
8948 (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)) {
8949 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008950
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008951 tw32(PCIE_TRANSACTION_CFG,
8952 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008953 }
Michael Chan79381092005-04-21 17:13:59 -07008954 }
8955
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008956 tg3_phy_start(tp);
8957
David S. Millerf47c11e2005-06-24 20:18:35 -07008958 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008959
Michael Chan79381092005-04-21 17:13:59 -07008960 add_timer(&tp->timer);
8961 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008962 tg3_enable_ints(tp);
8963
David S. Millerf47c11e2005-06-24 20:18:35 -07008964 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008965
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008966 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008967
8968 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00008969
Matt Carlson679563f2009-09-01 12:55:46 +00008970err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00008971 for (i = tp->irq_cnt - 1; i >= 0; i--) {
8972 struct tg3_napi *tnapi = &tp->napi[i];
8973 free_irq(tnapi->irq_vec, tnapi);
8974 }
Matt Carlson07b01732009-08-28 14:01:15 +00008975
Matt Carlson679563f2009-09-01 12:55:46 +00008976err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00008977 tg3_napi_disable(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008978 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00008979
8980err_out1:
8981 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008982 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008983}
8984
Linus Torvalds1da177e2005-04-16 15:20:36 -07008985static struct net_device_stats *tg3_get_stats(struct net_device *);
8986static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
8987
8988static int tg3_close(struct net_device *dev)
8989{
Matt Carlson4f125f42009-09-01 12:55:02 +00008990 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008991 struct tg3 *tp = netdev_priv(dev);
8992
Matt Carlsonfed97812009-09-01 13:10:19 +00008993 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07008994 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08008995
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008996 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008997
8998 del_timer_sync(&tp->timer);
8999
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009000 tg3_phy_stop(tp);
9001
David S. Millerf47c11e2005-06-24 20:18:35 -07009002 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009003
9004 tg3_disable_ints(tp);
9005
Michael Chan944d9802005-05-29 14:57:48 -07009006 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009007 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07009008 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009009
David S. Millerf47c11e2005-06-24 20:18:35 -07009010 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009011
Matt Carlson4f125f42009-09-01 12:55:02 +00009012 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9013 struct tg3_napi *tnapi = &tp->napi[i];
9014 free_irq(tnapi->irq_vec, tnapi);
9015 }
Matt Carlson07b01732009-08-28 14:01:15 +00009016
9017 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009018
9019 memcpy(&tp->net_stats_prev, tg3_get_stats(tp->dev),
9020 sizeof(tp->net_stats_prev));
9021 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9022 sizeof(tp->estats_prev));
9023
9024 tg3_free_consistent(tp);
9025
Michael Chanbc1c7562006-03-20 17:48:03 -08009026 tg3_set_power_state(tp, PCI_D3hot);
9027
9028 netif_carrier_off(tp->dev);
9029
Linus Torvalds1da177e2005-04-16 15:20:36 -07009030 return 0;
9031}
9032
9033static inline unsigned long get_stat64(tg3_stat64_t *val)
9034{
9035 unsigned long ret;
9036
9037#if (BITS_PER_LONG == 32)
9038 ret = val->low;
9039#else
9040 ret = ((u64)val->high << 32) | ((u64)val->low);
9041#endif
9042 return ret;
9043}
9044
Stefan Buehler816f8b82008-08-15 14:10:54 -07009045static inline u64 get_estat64(tg3_stat64_t *val)
9046{
9047 return ((u64)val->high << 32) | ((u64)val->low);
9048}
9049
Linus Torvalds1da177e2005-04-16 15:20:36 -07009050static unsigned long calc_crc_errors(struct tg3 *tp)
9051{
9052 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9053
9054 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
9055 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9056 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009057 u32 val;
9058
David S. Millerf47c11e2005-06-24 20:18:35 -07009059 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009060 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9061 tg3_writephy(tp, MII_TG3_TEST1,
9062 val | MII_TG3_TEST1_CRC_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009063 tg3_readphy(tp, 0x14, &val);
9064 } else
9065 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009066 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009067
9068 tp->phy_crc_errors += val;
9069
9070 return tp->phy_crc_errors;
9071 }
9072
9073 return get_stat64(&hw_stats->rx_fcs_errors);
9074}
9075
9076#define ESTAT_ADD(member) \
9077 estats->member = old_estats->member + \
Stefan Buehler816f8b82008-08-15 14:10:54 -07009078 get_estat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009079
9080static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9081{
9082 struct tg3_ethtool_stats *estats = &tp->estats;
9083 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9084 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9085
9086 if (!hw_stats)
9087 return old_estats;
9088
9089 ESTAT_ADD(rx_octets);
9090 ESTAT_ADD(rx_fragments);
9091 ESTAT_ADD(rx_ucast_packets);
9092 ESTAT_ADD(rx_mcast_packets);
9093 ESTAT_ADD(rx_bcast_packets);
9094 ESTAT_ADD(rx_fcs_errors);
9095 ESTAT_ADD(rx_align_errors);
9096 ESTAT_ADD(rx_xon_pause_rcvd);
9097 ESTAT_ADD(rx_xoff_pause_rcvd);
9098 ESTAT_ADD(rx_mac_ctrl_rcvd);
9099 ESTAT_ADD(rx_xoff_entered);
9100 ESTAT_ADD(rx_frame_too_long_errors);
9101 ESTAT_ADD(rx_jabbers);
9102 ESTAT_ADD(rx_undersize_packets);
9103 ESTAT_ADD(rx_in_length_errors);
9104 ESTAT_ADD(rx_out_length_errors);
9105 ESTAT_ADD(rx_64_or_less_octet_packets);
9106 ESTAT_ADD(rx_65_to_127_octet_packets);
9107 ESTAT_ADD(rx_128_to_255_octet_packets);
9108 ESTAT_ADD(rx_256_to_511_octet_packets);
9109 ESTAT_ADD(rx_512_to_1023_octet_packets);
9110 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9111 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9112 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9113 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9114 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9115
9116 ESTAT_ADD(tx_octets);
9117 ESTAT_ADD(tx_collisions);
9118 ESTAT_ADD(tx_xon_sent);
9119 ESTAT_ADD(tx_xoff_sent);
9120 ESTAT_ADD(tx_flow_control);
9121 ESTAT_ADD(tx_mac_errors);
9122 ESTAT_ADD(tx_single_collisions);
9123 ESTAT_ADD(tx_mult_collisions);
9124 ESTAT_ADD(tx_deferred);
9125 ESTAT_ADD(tx_excessive_collisions);
9126 ESTAT_ADD(tx_late_collisions);
9127 ESTAT_ADD(tx_collide_2times);
9128 ESTAT_ADD(tx_collide_3times);
9129 ESTAT_ADD(tx_collide_4times);
9130 ESTAT_ADD(tx_collide_5times);
9131 ESTAT_ADD(tx_collide_6times);
9132 ESTAT_ADD(tx_collide_7times);
9133 ESTAT_ADD(tx_collide_8times);
9134 ESTAT_ADD(tx_collide_9times);
9135 ESTAT_ADD(tx_collide_10times);
9136 ESTAT_ADD(tx_collide_11times);
9137 ESTAT_ADD(tx_collide_12times);
9138 ESTAT_ADD(tx_collide_13times);
9139 ESTAT_ADD(tx_collide_14times);
9140 ESTAT_ADD(tx_collide_15times);
9141 ESTAT_ADD(tx_ucast_packets);
9142 ESTAT_ADD(tx_mcast_packets);
9143 ESTAT_ADD(tx_bcast_packets);
9144 ESTAT_ADD(tx_carrier_sense_errors);
9145 ESTAT_ADD(tx_discards);
9146 ESTAT_ADD(tx_errors);
9147
9148 ESTAT_ADD(dma_writeq_full);
9149 ESTAT_ADD(dma_write_prioq_full);
9150 ESTAT_ADD(rxbds_empty);
9151 ESTAT_ADD(rx_discards);
9152 ESTAT_ADD(rx_errors);
9153 ESTAT_ADD(rx_threshold_hit);
9154
9155 ESTAT_ADD(dma_readq_full);
9156 ESTAT_ADD(dma_read_prioq_full);
9157 ESTAT_ADD(tx_comp_queue_full);
9158
9159 ESTAT_ADD(ring_set_send_prod_index);
9160 ESTAT_ADD(ring_status_update);
9161 ESTAT_ADD(nic_irqs);
9162 ESTAT_ADD(nic_avoided_irqs);
9163 ESTAT_ADD(nic_tx_threshold_hit);
9164
9165 return estats;
9166}
9167
9168static struct net_device_stats *tg3_get_stats(struct net_device *dev)
9169{
9170 struct tg3 *tp = netdev_priv(dev);
9171 struct net_device_stats *stats = &tp->net_stats;
9172 struct net_device_stats *old_stats = &tp->net_stats_prev;
9173 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9174
9175 if (!hw_stats)
9176 return old_stats;
9177
9178 stats->rx_packets = old_stats->rx_packets +
9179 get_stat64(&hw_stats->rx_ucast_packets) +
9180 get_stat64(&hw_stats->rx_mcast_packets) +
9181 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009182
Linus Torvalds1da177e2005-04-16 15:20:36 -07009183 stats->tx_packets = old_stats->tx_packets +
9184 get_stat64(&hw_stats->tx_ucast_packets) +
9185 get_stat64(&hw_stats->tx_mcast_packets) +
9186 get_stat64(&hw_stats->tx_bcast_packets);
9187
9188 stats->rx_bytes = old_stats->rx_bytes +
9189 get_stat64(&hw_stats->rx_octets);
9190 stats->tx_bytes = old_stats->tx_bytes +
9191 get_stat64(&hw_stats->tx_octets);
9192
9193 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009194 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009195 stats->tx_errors = old_stats->tx_errors +
9196 get_stat64(&hw_stats->tx_errors) +
9197 get_stat64(&hw_stats->tx_mac_errors) +
9198 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9199 get_stat64(&hw_stats->tx_discards);
9200
9201 stats->multicast = old_stats->multicast +
9202 get_stat64(&hw_stats->rx_mcast_packets);
9203 stats->collisions = old_stats->collisions +
9204 get_stat64(&hw_stats->tx_collisions);
9205
9206 stats->rx_length_errors = old_stats->rx_length_errors +
9207 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9208 get_stat64(&hw_stats->rx_undersize_packets);
9209
9210 stats->rx_over_errors = old_stats->rx_over_errors +
9211 get_stat64(&hw_stats->rxbds_empty);
9212 stats->rx_frame_errors = old_stats->rx_frame_errors +
9213 get_stat64(&hw_stats->rx_align_errors);
9214 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9215 get_stat64(&hw_stats->tx_discards);
9216 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9217 get_stat64(&hw_stats->tx_carrier_sense_errors);
9218
9219 stats->rx_crc_errors = old_stats->rx_crc_errors +
9220 calc_crc_errors(tp);
9221
John W. Linville4f63b872005-09-12 14:43:18 -07009222 stats->rx_missed_errors = old_stats->rx_missed_errors +
9223 get_stat64(&hw_stats->rx_discards);
9224
Linus Torvalds1da177e2005-04-16 15:20:36 -07009225 return stats;
9226}
9227
9228static inline u32 calc_crc(unsigned char *buf, int len)
9229{
9230 u32 reg;
9231 u32 tmp;
9232 int j, k;
9233
9234 reg = 0xffffffff;
9235
9236 for (j = 0; j < len; j++) {
9237 reg ^= buf[j];
9238
9239 for (k = 0; k < 8; k++) {
9240 tmp = reg & 0x01;
9241
9242 reg >>= 1;
9243
Matt Carlson859a588792010-04-05 10:19:28 +00009244 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009245 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009246 }
9247 }
9248
9249 return ~reg;
9250}
9251
9252static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9253{
9254 /* accept or reject all multicast frames */
9255 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9256 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9257 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9258 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9259}
9260
9261static void __tg3_set_rx_mode(struct net_device *dev)
9262{
9263 struct tg3 *tp = netdev_priv(dev);
9264 u32 rx_mode;
9265
9266 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9267 RX_MODE_KEEP_VLAN_TAG);
9268
9269 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9270 * flag clear.
9271 */
9272#if TG3_VLAN_TAG_USED
9273 if (!tp->vlgrp &&
9274 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9275 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9276#else
9277 /* By definition, VLAN is disabled always in this
9278 * case.
9279 */
9280 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9281 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9282#endif
9283
9284 if (dev->flags & IFF_PROMISC) {
9285 /* Promiscuous mode. */
9286 rx_mode |= RX_MODE_PROMISC;
9287 } else if (dev->flags & IFF_ALLMULTI) {
9288 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009289 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009290 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009291 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009292 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009293 } else {
9294 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +00009295 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009296 u32 mc_filter[4] = { 0, };
9297 u32 regidx;
9298 u32 bit;
9299 u32 crc;
9300
Jiri Pirko22bedad32010-04-01 21:22:57 +00009301 netdev_for_each_mc_addr(ha, dev) {
9302 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009303 bit = ~crc & 0x7f;
9304 regidx = (bit & 0x60) >> 5;
9305 bit &= 0x1f;
9306 mc_filter[regidx] |= (1 << bit);
9307 }
9308
9309 tw32(MAC_HASH_REG_0, mc_filter[0]);
9310 tw32(MAC_HASH_REG_1, mc_filter[1]);
9311 tw32(MAC_HASH_REG_2, mc_filter[2]);
9312 tw32(MAC_HASH_REG_3, mc_filter[3]);
9313 }
9314
9315 if (rx_mode != tp->rx_mode) {
9316 tp->rx_mode = rx_mode;
9317 tw32_f(MAC_RX_MODE, rx_mode);
9318 udelay(10);
9319 }
9320}
9321
9322static void tg3_set_rx_mode(struct net_device *dev)
9323{
9324 struct tg3 *tp = netdev_priv(dev);
9325
Michael Chane75f7c92006-03-20 21:33:26 -08009326 if (!netif_running(dev))
9327 return;
9328
David S. Millerf47c11e2005-06-24 20:18:35 -07009329 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009330 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009331 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009332}
9333
9334#define TG3_REGDUMP_LEN (32 * 1024)
9335
9336static int tg3_get_regs_len(struct net_device *dev)
9337{
9338 return TG3_REGDUMP_LEN;
9339}
9340
9341static void tg3_get_regs(struct net_device *dev,
9342 struct ethtool_regs *regs, void *_p)
9343{
9344 u32 *p = _p;
9345 struct tg3 *tp = netdev_priv(dev);
9346 u8 *orig_p = _p;
9347 int i;
9348
9349 regs->version = 0;
9350
9351 memset(p, 0, TG3_REGDUMP_LEN);
9352
Michael Chanbc1c7562006-03-20 17:48:03 -08009353 if (tp->link_config.phy_is_low_power)
9354 return;
9355
David S. Millerf47c11e2005-06-24 20:18:35 -07009356 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009357
9358#define __GET_REG32(reg) (*(p)++ = tr32(reg))
9359#define GET_REG32_LOOP(base,len) \
9360do { p = (u32 *)(orig_p + (base)); \
9361 for (i = 0; i < len; i += 4) \
9362 __GET_REG32((base) + i); \
9363} while (0)
9364#define GET_REG32_1(reg) \
9365do { p = (u32 *)(orig_p + (reg)); \
9366 __GET_REG32((reg)); \
9367} while (0)
9368
9369 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9370 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9371 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9372 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9373 GET_REG32_1(SNDDATAC_MODE);
9374 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9375 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9376 GET_REG32_1(SNDBDC_MODE);
9377 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9378 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9379 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9380 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9381 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9382 GET_REG32_1(RCVDCC_MODE);
9383 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9384 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9385 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9386 GET_REG32_1(MBFREE_MODE);
9387 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9388 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9389 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9390 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9391 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08009392 GET_REG32_1(RX_CPU_MODE);
9393 GET_REG32_1(RX_CPU_STATE);
9394 GET_REG32_1(RX_CPU_PGMCTR);
9395 GET_REG32_1(RX_CPU_HWBKPT);
9396 GET_REG32_1(TX_CPU_MODE);
9397 GET_REG32_1(TX_CPU_STATE);
9398 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009399 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9400 GET_REG32_LOOP(FTQ_RESET, 0x120);
9401 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9402 GET_REG32_1(DMAC_MODE);
9403 GET_REG32_LOOP(GRC_MODE, 0x4c);
9404 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9405 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9406
9407#undef __GET_REG32
9408#undef GET_REG32_LOOP
9409#undef GET_REG32_1
9410
David S. Millerf47c11e2005-06-24 20:18:35 -07009411 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009412}
9413
9414static int tg3_get_eeprom_len(struct net_device *dev)
9415{
9416 struct tg3 *tp = netdev_priv(dev);
9417
9418 return tp->nvram_size;
9419}
9420
Linus Torvalds1da177e2005-04-16 15:20:36 -07009421static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9422{
9423 struct tg3 *tp = netdev_priv(dev);
9424 int ret;
9425 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009426 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009427 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009428
Matt Carlsondf259d82009-04-20 06:57:14 +00009429 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9430 return -EINVAL;
9431
Michael Chanbc1c7562006-03-20 17:48:03 -08009432 if (tp->link_config.phy_is_low_power)
9433 return -EAGAIN;
9434
Linus Torvalds1da177e2005-04-16 15:20:36 -07009435 offset = eeprom->offset;
9436 len = eeprom->len;
9437 eeprom->len = 0;
9438
9439 eeprom->magic = TG3_EEPROM_MAGIC;
9440
9441 if (offset & 3) {
9442 /* adjustments to start on required 4 byte boundary */
9443 b_offset = offset & 3;
9444 b_count = 4 - b_offset;
9445 if (b_count > len) {
9446 /* i.e. offset=1 len=2 */
9447 b_count = len;
9448 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009449 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009450 if (ret)
9451 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009452 memcpy(data, ((char*)&val) + b_offset, b_count);
9453 len -= b_count;
9454 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009455 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009456 }
9457
9458 /* read bytes upto the last 4 byte boundary */
9459 pd = &data[eeprom->len];
9460 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009461 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009462 if (ret) {
9463 eeprom->len += i;
9464 return ret;
9465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009466 memcpy(pd + i, &val, 4);
9467 }
9468 eeprom->len += i;
9469
9470 if (len & 3) {
9471 /* read last bytes not ending on 4 byte boundary */
9472 pd = &data[eeprom->len];
9473 b_count = len & 3;
9474 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009475 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009476 if (ret)
9477 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009478 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009479 eeprom->len += b_count;
9480 }
9481 return 0;
9482}
9483
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009484static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009485
9486static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9487{
9488 struct tg3 *tp = netdev_priv(dev);
9489 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009490 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009491 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009492 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009493
Michael Chanbc1c7562006-03-20 17:48:03 -08009494 if (tp->link_config.phy_is_low_power)
9495 return -EAGAIN;
9496
Matt Carlsondf259d82009-04-20 06:57:14 +00009497 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9498 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009499 return -EINVAL;
9500
9501 offset = eeprom->offset;
9502 len = eeprom->len;
9503
9504 if ((b_offset = (offset & 3))) {
9505 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009506 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009507 if (ret)
9508 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009509 len += b_offset;
9510 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009511 if (len < 4)
9512 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009513 }
9514
9515 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009516 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009517 /* adjustments to end on required 4 byte boundary */
9518 odd_len = 1;
9519 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009520 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009521 if (ret)
9522 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009523 }
9524
9525 buf = data;
9526 if (b_offset || odd_len) {
9527 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009528 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009529 return -ENOMEM;
9530 if (b_offset)
9531 memcpy(buf, &start, 4);
9532 if (odd_len)
9533 memcpy(buf+len-4, &end, 4);
9534 memcpy(buf + b_offset, data, eeprom->len);
9535 }
9536
9537 ret = tg3_nvram_write_block(tp, offset, len, buf);
9538
9539 if (buf != data)
9540 kfree(buf);
9541
9542 return ret;
9543}
9544
9545static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9546{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009547 struct tg3 *tp = netdev_priv(dev);
9548
9549 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009550 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009551 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9552 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009553 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9554 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009555 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009556
Linus Torvalds1da177e2005-04-16 15:20:36 -07009557 cmd->supported = (SUPPORTED_Autoneg);
9558
9559 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
9560 cmd->supported |= (SUPPORTED_1000baseT_Half |
9561 SUPPORTED_1000baseT_Full);
9562
Karsten Keilef348142006-05-12 12:49:08 -07009563 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009564 cmd->supported |= (SUPPORTED_100baseT_Half |
9565 SUPPORTED_100baseT_Full |
9566 SUPPORTED_10baseT_Half |
9567 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009568 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009569 cmd->port = PORT_TP;
9570 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009571 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009572 cmd->port = PORT_FIBRE;
9573 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009574
Linus Torvalds1da177e2005-04-16 15:20:36 -07009575 cmd->advertising = tp->link_config.advertising;
9576 if (netif_running(dev)) {
9577 cmd->speed = tp->link_config.active_speed;
9578 cmd->duplex = tp->link_config.active_duplex;
9579 }
Matt Carlson882e9792009-09-01 13:21:36 +00009580 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009581 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009582 cmd->autoneg = tp->link_config.autoneg;
9583 cmd->maxtxpkt = 0;
9584 cmd->maxrxpkt = 0;
9585 return 0;
9586}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009587
Linus Torvalds1da177e2005-04-16 15:20:36 -07009588static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9589{
9590 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009591
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009592 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009593 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009594 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9595 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009596 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9597 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009598 }
9599
Matt Carlson7e5856b2009-02-25 14:23:01 +00009600 if (cmd->autoneg != AUTONEG_ENABLE &&
9601 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009602 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009603
9604 if (cmd->autoneg == AUTONEG_DISABLE &&
9605 cmd->duplex != DUPLEX_FULL &&
9606 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009607 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009608
Matt Carlson7e5856b2009-02-25 14:23:01 +00009609 if (cmd->autoneg == AUTONEG_ENABLE) {
9610 u32 mask = ADVERTISED_Autoneg |
9611 ADVERTISED_Pause |
9612 ADVERTISED_Asym_Pause;
9613
Julia Lawall3f07d122010-03-13 12:22:16 -08009614 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009615 mask |= ADVERTISED_1000baseT_Half |
9616 ADVERTISED_1000baseT_Full;
9617
9618 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
9619 mask |= ADVERTISED_100baseT_Half |
9620 ADVERTISED_100baseT_Full |
9621 ADVERTISED_10baseT_Half |
9622 ADVERTISED_10baseT_Full |
9623 ADVERTISED_TP;
9624 else
9625 mask |= ADVERTISED_FIBRE;
9626
9627 if (cmd->advertising & ~mask)
9628 return -EINVAL;
9629
9630 mask &= (ADVERTISED_1000baseT_Half |
9631 ADVERTISED_1000baseT_Full |
9632 ADVERTISED_100baseT_Half |
9633 ADVERTISED_100baseT_Full |
9634 ADVERTISED_10baseT_Half |
9635 ADVERTISED_10baseT_Full);
9636
9637 cmd->advertising &= mask;
9638 } else {
9639 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) {
9640 if (cmd->speed != SPEED_1000)
9641 return -EINVAL;
9642
9643 if (cmd->duplex != DUPLEX_FULL)
9644 return -EINVAL;
9645 } else {
9646 if (cmd->speed != SPEED_100 &&
9647 cmd->speed != SPEED_10)
9648 return -EINVAL;
9649 }
9650 }
9651
David S. Millerf47c11e2005-06-24 20:18:35 -07009652 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009653
9654 tp->link_config.autoneg = cmd->autoneg;
9655 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009656 tp->link_config.advertising = (cmd->advertising |
9657 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009658 tp->link_config.speed = SPEED_INVALID;
9659 tp->link_config.duplex = DUPLEX_INVALID;
9660 } else {
9661 tp->link_config.advertising = 0;
9662 tp->link_config.speed = cmd->speed;
9663 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009664 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009665
Michael Chan24fcad62006-12-17 17:06:46 -08009666 tp->link_config.orig_speed = tp->link_config.speed;
9667 tp->link_config.orig_duplex = tp->link_config.duplex;
9668 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9669
Linus Torvalds1da177e2005-04-16 15:20:36 -07009670 if (netif_running(dev))
9671 tg3_setup_phy(tp, 1);
9672
David S. Millerf47c11e2005-06-24 20:18:35 -07009673 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009674
Linus Torvalds1da177e2005-04-16 15:20:36 -07009675 return 0;
9676}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009677
Linus Torvalds1da177e2005-04-16 15:20:36 -07009678static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9679{
9680 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009681
Linus Torvalds1da177e2005-04-16 15:20:36 -07009682 strcpy(info->driver, DRV_MODULE_NAME);
9683 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08009684 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009685 strcpy(info->bus_info, pci_name(tp->pdev));
9686}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009687
Linus Torvalds1da177e2005-04-16 15:20:36 -07009688static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9689{
9690 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009691
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009692 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9693 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07009694 wol->supported = WAKE_MAGIC;
9695 else
9696 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009697 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08009698 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9699 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009700 wol->wolopts = WAKE_MAGIC;
9701 memset(&wol->sopass, 0, sizeof(wol->sopass));
9702}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009703
Linus Torvalds1da177e2005-04-16 15:20:36 -07009704static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9705{
9706 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009707 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009708
Linus Torvalds1da177e2005-04-16 15:20:36 -07009709 if (wol->wolopts & ~WAKE_MAGIC)
9710 return -EINVAL;
9711 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009712 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009713 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009714
David S. Millerf47c11e2005-06-24 20:18:35 -07009715 spin_lock_bh(&tp->lock);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009716 if (wol->wolopts & WAKE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009717 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009718 device_set_wakeup_enable(dp, true);
9719 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009720 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009721 device_set_wakeup_enable(dp, false);
9722 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009723 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009724
Linus Torvalds1da177e2005-04-16 15:20:36 -07009725 return 0;
9726}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009727
Linus Torvalds1da177e2005-04-16 15:20:36 -07009728static u32 tg3_get_msglevel(struct net_device *dev)
9729{
9730 struct tg3 *tp = netdev_priv(dev);
9731 return tp->msg_enable;
9732}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009733
Linus Torvalds1da177e2005-04-16 15:20:36 -07009734static void tg3_set_msglevel(struct net_device *dev, u32 value)
9735{
9736 struct tg3 *tp = netdev_priv(dev);
9737 tp->msg_enable = value;
9738}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009739
Linus Torvalds1da177e2005-04-16 15:20:36 -07009740static int tg3_set_tso(struct net_device *dev, u32 value)
9741{
9742 struct tg3 *tp = netdev_priv(dev);
9743
9744 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9745 if (value)
9746 return -EINVAL;
9747 return 0;
9748 }
Matt Carlson027455a2008-12-21 20:19:30 -08009749 if ((dev->features & NETIF_F_IPV6_CSUM) &&
Matt Carlsone849cdc2009-11-13 13:03:38 +00009750 ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
9751 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3))) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009752 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -07009753 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +00009754 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
9755 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -07009756 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
9757 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009758 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00009759 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009760 dev->features |= NETIF_F_TSO_ECN;
9761 } else
9762 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -07009763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009764 return ethtool_op_set_tso(dev, value);
9765}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009766
Linus Torvalds1da177e2005-04-16 15:20:36 -07009767static int tg3_nway_reset(struct net_device *dev)
9768{
9769 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009770 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009771
Linus Torvalds1da177e2005-04-16 15:20:36 -07009772 if (!netif_running(dev))
9773 return -EAGAIN;
9774
Michael Chanc94e3942005-09-27 12:12:42 -07009775 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
9776 return -EINVAL;
9777
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009778 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9779 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9780 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009781 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009782 } else {
9783 u32 bmcr;
9784
9785 spin_lock_bh(&tp->lock);
9786 r = -EINVAL;
9787 tg3_readphy(tp, MII_BMCR, &bmcr);
9788 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
9789 ((bmcr & BMCR_ANENABLE) ||
9790 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT))) {
9791 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
9792 BMCR_ANENABLE);
9793 r = 0;
9794 }
9795 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009796 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009797
Linus Torvalds1da177e2005-04-16 15:20:36 -07009798 return r;
9799}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009800
Linus Torvalds1da177e2005-04-16 15:20:36 -07009801static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9802{
9803 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009804
Linus Torvalds1da177e2005-04-16 15:20:36 -07009805 ering->rx_max_pending = TG3_RX_RING_SIZE - 1;
9806 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009807 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9808 ering->rx_jumbo_max_pending = TG3_RX_JUMBO_RING_SIZE - 1;
9809 else
9810 ering->rx_jumbo_max_pending = 0;
9811
9812 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009813
9814 ering->rx_pending = tp->rx_pending;
9815 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009816 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9817 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
9818 else
9819 ering->rx_jumbo_pending = 0;
9820
Matt Carlsonf3f3f272009-08-28 14:03:21 +00009821 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009822}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009823
Linus Torvalds1da177e2005-04-16 15:20:36 -07009824static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9825{
9826 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +00009827 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009828
Linus Torvalds1da177e2005-04-16 15:20:36 -07009829 if ((ering->rx_pending > TG3_RX_RING_SIZE - 1) ||
9830 (ering->rx_jumbo_pending > TG3_RX_JUMBO_RING_SIZE - 1) ||
Michael Chanbc3a9252006-10-18 20:55:18 -07009831 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
9832 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -08009833 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -07009834 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009835 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009836
Michael Chanbbe832c2005-06-24 20:20:04 -07009837 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009838 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009839 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009840 irq_sync = 1;
9841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009842
Michael Chanbbe832c2005-06-24 20:20:04 -07009843 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009844
Linus Torvalds1da177e2005-04-16 15:20:36 -07009845 tp->rx_pending = ering->rx_pending;
9846
9847 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
9848 tp->rx_pending > 63)
9849 tp->rx_pending = 63;
9850 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +00009851
9852 for (i = 0; i < TG3_IRQ_MAX_VECS; i++)
9853 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009854
9855 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -07009856 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -07009857 err = tg3_restart_hw(tp, 1);
9858 if (!err)
9859 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009860 }
9861
David S. Millerf47c11e2005-06-24 20:18:35 -07009862 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009863
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009864 if (irq_sync && !err)
9865 tg3_phy_start(tp);
9866
Michael Chanb9ec6c12006-07-25 16:37:27 -07009867 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009868}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009869
Linus Torvalds1da177e2005-04-16 15:20:36 -07009870static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9871{
9872 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009873
Linus Torvalds1da177e2005-04-16 15:20:36 -07009874 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -08009875
Steve Glendinninge18ce342008-12-16 02:00:00 -08009876 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -08009877 epause->rx_pause = 1;
9878 else
9879 epause->rx_pause = 0;
9880
Steve Glendinninge18ce342008-12-16 02:00:00 -08009881 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -08009882 epause->tx_pause = 1;
9883 else
9884 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009885}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009886
Linus Torvalds1da177e2005-04-16 15:20:36 -07009887static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9888{
9889 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009890 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009891
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009892 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson27121682010-02-17 15:16:57 +00009893 u32 newadv;
9894 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009895
Matt Carlson27121682010-02-17 15:16:57 +00009896 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009897
Matt Carlson27121682010-02-17 15:16:57 +00009898 if (!(phydev->supported & SUPPORTED_Pause) ||
9899 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
9900 ((epause->rx_pause && !epause->tx_pause) ||
9901 (!epause->rx_pause && epause->tx_pause))))
9902 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009903
Matt Carlson27121682010-02-17 15:16:57 +00009904 tp->link_config.flowctrl = 0;
9905 if (epause->rx_pause) {
9906 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009907
Matt Carlson27121682010-02-17 15:16:57 +00009908 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -08009909 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +00009910 newadv = ADVERTISED_Pause;
9911 } else
9912 newadv = ADVERTISED_Pause |
9913 ADVERTISED_Asym_Pause;
9914 } else if (epause->tx_pause) {
9915 tp->link_config.flowctrl |= FLOW_CTRL_TX;
9916 newadv = ADVERTISED_Asym_Pause;
9917 } else
9918 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009919
Matt Carlson27121682010-02-17 15:16:57 +00009920 if (epause->autoneg)
9921 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9922 else
9923 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9924
9925 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
9926 u32 oldadv = phydev->advertising &
9927 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
9928 if (oldadv != newadv) {
9929 phydev->advertising &=
9930 ~(ADVERTISED_Pause |
9931 ADVERTISED_Asym_Pause);
9932 phydev->advertising |= newadv;
9933 if (phydev->autoneg) {
9934 /*
9935 * Always renegotiate the link to
9936 * inform our link partner of our
9937 * flow control settings, even if the
9938 * flow control is forced. Let
9939 * tg3_adjust_link() do the final
9940 * flow control setup.
9941 */
9942 return phy_start_aneg(phydev);
9943 }
9944 }
9945
9946 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009947 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +00009948 } else {
9949 tp->link_config.orig_advertising &=
9950 ~(ADVERTISED_Pause |
9951 ADVERTISED_Asym_Pause);
9952 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009953 }
9954 } else {
9955 int irq_sync = 0;
9956
9957 if (netif_running(dev)) {
9958 tg3_netif_stop(tp);
9959 irq_sync = 1;
9960 }
9961
9962 tg3_full_lock(tp, irq_sync);
9963
9964 if (epause->autoneg)
9965 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9966 else
9967 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9968 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009969 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009970 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009971 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009972 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009973 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009974 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009975 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009976
9977 if (netif_running(dev)) {
9978 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9979 err = tg3_restart_hw(tp, 1);
9980 if (!err)
9981 tg3_netif_start(tp);
9982 }
9983
9984 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009986
Michael Chanb9ec6c12006-07-25 16:37:27 -07009987 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009988}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009989
Linus Torvalds1da177e2005-04-16 15:20:36 -07009990static u32 tg3_get_rx_csum(struct net_device *dev)
9991{
9992 struct tg3 *tp = netdev_priv(dev);
9993 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
9994}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009995
Linus Torvalds1da177e2005-04-16 15:20:36 -07009996static int tg3_set_rx_csum(struct net_device *dev, u32 data)
9997{
9998 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009999
Linus Torvalds1da177e2005-04-16 15:20:36 -070010000 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10001 if (data != 0)
10002 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010003 return 0;
10004 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010005
David S. Millerf47c11e2005-06-24 20:18:35 -070010006 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010007 if (data)
10008 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
10009 else
10010 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -070010011 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010012
Linus Torvalds1da177e2005-04-16 15:20:36 -070010013 return 0;
10014}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010015
Linus Torvalds1da177e2005-04-16 15:20:36 -070010016static int tg3_set_tx_csum(struct net_device *dev, u32 data)
10017{
10018 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010019
Linus Torvalds1da177e2005-04-16 15:20:36 -070010020 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
10021 if (data != 0)
10022 return -EINVAL;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010023 return 0;
10024 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010025
Matt Carlson321d32a2008-11-21 17:22:19 -080010026 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -070010027 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010028 else
Michael Chan9c27dbd2006-03-20 22:28:27 -080010029 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010030
10031 return 0;
10032}
10033
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010034static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010035{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010036 switch (sset) {
10037 case ETH_SS_TEST:
10038 return TG3_NUM_TEST;
10039 case ETH_SS_STATS:
10040 return TG3_NUM_STATS;
10041 default:
10042 return -EOPNOTSUPP;
10043 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010044}
10045
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010046static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010047{
10048 switch (stringset) {
10049 case ETH_SS_STATS:
10050 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10051 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010052 case ETH_SS_TEST:
10053 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10054 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010055 default:
10056 WARN_ON(1); /* we need a WARN() */
10057 break;
10058 }
10059}
10060
Michael Chan4009a932005-09-05 17:52:54 -070010061static int tg3_phys_id(struct net_device *dev, u32 data)
10062{
10063 struct tg3 *tp = netdev_priv(dev);
10064 int i;
10065
10066 if (!netif_running(tp->dev))
10067 return -EAGAIN;
10068
10069 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -080010070 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -070010071
10072 for (i = 0; i < (data * 2); i++) {
10073 if ((i % 2) == 0)
10074 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10075 LED_CTRL_1000MBPS_ON |
10076 LED_CTRL_100MBPS_ON |
10077 LED_CTRL_10MBPS_ON |
10078 LED_CTRL_TRAFFIC_OVERRIDE |
10079 LED_CTRL_TRAFFIC_BLINK |
10080 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010081
Michael Chan4009a932005-09-05 17:52:54 -070010082 else
10083 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10084 LED_CTRL_TRAFFIC_OVERRIDE);
10085
10086 if (msleep_interruptible(500))
10087 break;
10088 }
10089 tw32(MAC_LED_CTRL, tp->led_ctrl);
10090 return 0;
10091}
10092
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010093static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010094 struct ethtool_stats *estats, u64 *tmp_stats)
10095{
10096 struct tg3 *tp = netdev_priv(dev);
10097 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10098}
10099
Michael Chan566f86a2005-05-29 14:56:58 -070010100#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010101#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10102#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10103#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010104#define NVRAM_SELFBOOT_HW_SIZE 0x20
10105#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010106
10107static int tg3_test_nvram(struct tg3 *tp)
10108{
Al Virob9fc7dc2007-12-17 22:59:57 -080010109 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010110 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010111 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010112
Matt Carlsondf259d82009-04-20 06:57:14 +000010113 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10114 return 0;
10115
Matt Carlsone4f34112009-02-25 14:25:00 +000010116 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010117 return -EIO;
10118
Michael Chan1b277772006-03-20 22:27:48 -080010119 if (magic == TG3_EEPROM_MAGIC)
10120 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010121 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010122 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10123 TG3_EEPROM_SB_FORMAT_1) {
10124 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10125 case TG3_EEPROM_SB_REVISION_0:
10126 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10127 break;
10128 case TG3_EEPROM_SB_REVISION_2:
10129 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10130 break;
10131 case TG3_EEPROM_SB_REVISION_3:
10132 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10133 break;
10134 default:
10135 return 0;
10136 }
10137 } else
Michael Chan1b277772006-03-20 22:27:48 -080010138 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010139 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10140 size = NVRAM_SELFBOOT_HW_SIZE;
10141 else
Michael Chan1b277772006-03-20 22:27:48 -080010142 return -EIO;
10143
10144 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010145 if (buf == NULL)
10146 return -ENOMEM;
10147
Michael Chan1b277772006-03-20 22:27:48 -080010148 err = -EIO;
10149 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010150 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10151 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010152 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010153 }
Michael Chan1b277772006-03-20 22:27:48 -080010154 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010155 goto out;
10156
Michael Chan1b277772006-03-20 22:27:48 -080010157 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010158 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010159 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010160 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010161 u8 *buf8 = (u8 *) buf, csum8 = 0;
10162
Al Virob9fc7dc2007-12-17 22:59:57 -080010163 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010164 TG3_EEPROM_SB_REVISION_2) {
10165 /* For rev 2, the csum doesn't include the MBA. */
10166 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10167 csum8 += buf8[i];
10168 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10169 csum8 += buf8[i];
10170 } else {
10171 for (i = 0; i < size; i++)
10172 csum8 += buf8[i];
10173 }
Michael Chan1b277772006-03-20 22:27:48 -080010174
Adrian Bunkad96b482006-04-05 22:21:04 -070010175 if (csum8 == 0) {
10176 err = 0;
10177 goto out;
10178 }
10179
10180 err = -EIO;
10181 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010182 }
Michael Chan566f86a2005-05-29 14:56:58 -070010183
Al Virob9fc7dc2007-12-17 22:59:57 -080010184 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010185 TG3_EEPROM_MAGIC_HW) {
10186 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010187 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010188 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010189
10190 /* Separate the parity bits and the data bytes. */
10191 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10192 if ((i == 0) || (i == 8)) {
10193 int l;
10194 u8 msk;
10195
10196 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10197 parity[k++] = buf8[i] & msk;
10198 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010199 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010200 int l;
10201 u8 msk;
10202
10203 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10204 parity[k++] = buf8[i] & msk;
10205 i++;
10206
10207 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10208 parity[k++] = buf8[i] & msk;
10209 i++;
10210 }
10211 data[j++] = buf8[i];
10212 }
10213
10214 err = -EIO;
10215 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10216 u8 hw8 = hweight8(data[i]);
10217
10218 if ((hw8 & 0x1) && parity[i])
10219 goto out;
10220 else if (!(hw8 & 0x1) && !parity[i])
10221 goto out;
10222 }
10223 err = 0;
10224 goto out;
10225 }
10226
Michael Chan566f86a2005-05-29 14:56:58 -070010227 /* Bootstrap checksum at offset 0x10 */
10228 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010229 if (csum != be32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010230 goto out;
10231
10232 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10233 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlsona9dc5292009-02-25 14:25:30 +000010234 if (csum != be32_to_cpu(buf[0xfc/4]))
10235 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010236
10237 err = 0;
10238
10239out:
10240 kfree(buf);
10241 return err;
10242}
10243
Michael Chanca430072005-05-29 14:57:23 -070010244#define TG3_SERDES_TIMEOUT_SEC 2
10245#define TG3_COPPER_TIMEOUT_SEC 6
10246
10247static int tg3_test_link(struct tg3 *tp)
10248{
10249 int i, max;
10250
10251 if (!netif_running(tp->dev))
10252 return -ENODEV;
10253
Michael Chan4c987482005-09-05 17:52:38 -070010254 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010255 max = TG3_SERDES_TIMEOUT_SEC;
10256 else
10257 max = TG3_COPPER_TIMEOUT_SEC;
10258
10259 for (i = 0; i < max; i++) {
10260 if (netif_carrier_ok(tp->dev))
10261 return 0;
10262
10263 if (msleep_interruptible(1000))
10264 break;
10265 }
10266
10267 return -EIO;
10268}
10269
Michael Chana71116d2005-05-29 14:58:11 -070010270/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010271static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010272{
Michael Chanb16250e2006-09-27 16:10:14 -070010273 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010274 u32 offset, read_mask, write_mask, val, save_val, read_val;
10275 static struct {
10276 u16 offset;
10277 u16 flags;
10278#define TG3_FL_5705 0x1
10279#define TG3_FL_NOT_5705 0x2
10280#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010281#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010282 u32 read_mask;
10283 u32 write_mask;
10284 } reg_tbl[] = {
10285 /* MAC Control Registers */
10286 { MAC_MODE, TG3_FL_NOT_5705,
10287 0x00000000, 0x00ef6f8c },
10288 { MAC_MODE, TG3_FL_5705,
10289 0x00000000, 0x01ef6b8c },
10290 { MAC_STATUS, TG3_FL_NOT_5705,
10291 0x03800107, 0x00000000 },
10292 { MAC_STATUS, TG3_FL_5705,
10293 0x03800100, 0x00000000 },
10294 { MAC_ADDR_0_HIGH, 0x0000,
10295 0x00000000, 0x0000ffff },
10296 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010297 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010298 { MAC_RX_MTU_SIZE, 0x0000,
10299 0x00000000, 0x0000ffff },
10300 { MAC_TX_MODE, 0x0000,
10301 0x00000000, 0x00000070 },
10302 { MAC_TX_LENGTHS, 0x0000,
10303 0x00000000, 0x00003fff },
10304 { MAC_RX_MODE, TG3_FL_NOT_5705,
10305 0x00000000, 0x000007fc },
10306 { MAC_RX_MODE, TG3_FL_5705,
10307 0x00000000, 0x000007dc },
10308 { MAC_HASH_REG_0, 0x0000,
10309 0x00000000, 0xffffffff },
10310 { MAC_HASH_REG_1, 0x0000,
10311 0x00000000, 0xffffffff },
10312 { MAC_HASH_REG_2, 0x0000,
10313 0x00000000, 0xffffffff },
10314 { MAC_HASH_REG_3, 0x0000,
10315 0x00000000, 0xffffffff },
10316
10317 /* Receive Data and Receive BD Initiator Control Registers. */
10318 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10319 0x00000000, 0xffffffff },
10320 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10321 0x00000000, 0xffffffff },
10322 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10323 0x00000000, 0x00000003 },
10324 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10325 0x00000000, 0xffffffff },
10326 { RCVDBDI_STD_BD+0, 0x0000,
10327 0x00000000, 0xffffffff },
10328 { RCVDBDI_STD_BD+4, 0x0000,
10329 0x00000000, 0xffffffff },
10330 { RCVDBDI_STD_BD+8, 0x0000,
10331 0x00000000, 0xffff0002 },
10332 { RCVDBDI_STD_BD+0xc, 0x0000,
10333 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010334
Michael Chana71116d2005-05-29 14:58:11 -070010335 /* Receive BD Initiator Control Registers. */
10336 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10337 0x00000000, 0xffffffff },
10338 { RCVBDI_STD_THRESH, TG3_FL_5705,
10339 0x00000000, 0x000003ff },
10340 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10341 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010342
Michael Chana71116d2005-05-29 14:58:11 -070010343 /* Host Coalescing Control Registers. */
10344 { HOSTCC_MODE, TG3_FL_NOT_5705,
10345 0x00000000, 0x00000004 },
10346 { HOSTCC_MODE, TG3_FL_5705,
10347 0x00000000, 0x000000f6 },
10348 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10349 0x00000000, 0xffffffff },
10350 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10351 0x00000000, 0x000003ff },
10352 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10353 0x00000000, 0xffffffff },
10354 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10355 0x00000000, 0x000003ff },
10356 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10357 0x00000000, 0xffffffff },
10358 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10359 0x00000000, 0x000000ff },
10360 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10361 0x00000000, 0xffffffff },
10362 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10363 0x00000000, 0x000000ff },
10364 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10365 0x00000000, 0xffffffff },
10366 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10367 0x00000000, 0xffffffff },
10368 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10369 0x00000000, 0xffffffff },
10370 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10371 0x00000000, 0x000000ff },
10372 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10373 0x00000000, 0xffffffff },
10374 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10375 0x00000000, 0x000000ff },
10376 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10377 0x00000000, 0xffffffff },
10378 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10379 0x00000000, 0xffffffff },
10380 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10381 0x00000000, 0xffffffff },
10382 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10383 0x00000000, 0xffffffff },
10384 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10385 0x00000000, 0xffffffff },
10386 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10387 0xffffffff, 0x00000000 },
10388 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10389 0xffffffff, 0x00000000 },
10390
10391 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010392 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010393 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010394 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010395 0x00000000, 0x007fffff },
10396 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10397 0x00000000, 0x0000003f },
10398 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10399 0x00000000, 0x000001ff },
10400 { BUFMGR_MB_HIGH_WATER, 0x0000,
10401 0x00000000, 0x000001ff },
10402 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10403 0xffffffff, 0x00000000 },
10404 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10405 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010406
Michael Chana71116d2005-05-29 14:58:11 -070010407 /* Mailbox Registers */
10408 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10409 0x00000000, 0x000001ff },
10410 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10411 0x00000000, 0x000001ff },
10412 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10413 0x00000000, 0x000007ff },
10414 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10415 0x00000000, 0x000001ff },
10416
10417 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10418 };
10419
Michael Chanb16250e2006-09-27 16:10:14 -070010420 is_5705 = is_5750 = 0;
10421 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010422 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010423 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10424 is_5750 = 1;
10425 }
Michael Chana71116d2005-05-29 14:58:11 -070010426
10427 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10428 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10429 continue;
10430
10431 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10432 continue;
10433
10434 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10435 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10436 continue;
10437
Michael Chanb16250e2006-09-27 16:10:14 -070010438 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10439 continue;
10440
Michael Chana71116d2005-05-29 14:58:11 -070010441 offset = (u32) reg_tbl[i].offset;
10442 read_mask = reg_tbl[i].read_mask;
10443 write_mask = reg_tbl[i].write_mask;
10444
10445 /* Save the original register content */
10446 save_val = tr32(offset);
10447
10448 /* Determine the read-only value. */
10449 read_val = save_val & read_mask;
10450
10451 /* Write zero to the register, then make sure the read-only bits
10452 * are not changed and the read/write bits are all zeros.
10453 */
10454 tw32(offset, 0);
10455
10456 val = tr32(offset);
10457
10458 /* Test the read-only and read/write bits. */
10459 if (((val & read_mask) != read_val) || (val & write_mask))
10460 goto out;
10461
10462 /* Write ones to all the bits defined by RdMask and WrMask, then
10463 * make sure the read-only bits are not changed and the
10464 * read/write bits are all ones.
10465 */
10466 tw32(offset, read_mask | write_mask);
10467
10468 val = tr32(offset);
10469
10470 /* Test the read-only bits. */
10471 if ((val & read_mask) != read_val)
10472 goto out;
10473
10474 /* Test the read/write bits. */
10475 if ((val & write_mask) != write_mask)
10476 goto out;
10477
10478 tw32(offset, save_val);
10479 }
10480
10481 return 0;
10482
10483out:
Michael Chan9f88f292006-12-07 00:22:54 -080010484 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010485 netdev_err(tp->dev,
10486 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010487 tw32(offset, save_val);
10488 return -EIO;
10489}
10490
Michael Chan7942e1d2005-05-29 14:58:36 -070010491static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10492{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010493 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010494 int i;
10495 u32 j;
10496
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010497 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010498 for (j = 0; j < len; j += 4) {
10499 u32 val;
10500
10501 tg3_write_mem(tp, offset + j, test_pattern[i]);
10502 tg3_read_mem(tp, offset + j, &val);
10503 if (val != test_pattern[i])
10504 return -EIO;
10505 }
10506 }
10507 return 0;
10508}
10509
10510static int tg3_test_memory(struct tg3 *tp)
10511{
10512 static struct mem_entry {
10513 u32 offset;
10514 u32 len;
10515 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010516 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010517 { 0x00002000, 0x1c000},
10518 { 0xffffffff, 0x00000}
10519 }, mem_tbl_5705[] = {
10520 { 0x00000100, 0x0000c},
10521 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010522 { 0x00004000, 0x00800},
10523 { 0x00006000, 0x01000},
10524 { 0x00008000, 0x02000},
10525 { 0x00010000, 0x0e000},
10526 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010527 }, mem_tbl_5755[] = {
10528 { 0x00000200, 0x00008},
10529 { 0x00004000, 0x00800},
10530 { 0x00006000, 0x00800},
10531 { 0x00008000, 0x02000},
10532 { 0x00010000, 0x0c000},
10533 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010534 }, mem_tbl_5906[] = {
10535 { 0x00000200, 0x00008},
10536 { 0x00004000, 0x00400},
10537 { 0x00006000, 0x00400},
10538 { 0x00008000, 0x01000},
10539 { 0x00010000, 0x01000},
10540 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010541 }, mem_tbl_5717[] = {
10542 { 0x00000200, 0x00008},
10543 { 0x00010000, 0x0a000},
10544 { 0x00020000, 0x13c00},
10545 { 0xffffffff, 0x00000}
10546 }, mem_tbl_57765[] = {
10547 { 0x00000200, 0x00008},
10548 { 0x00004000, 0x00800},
10549 { 0x00006000, 0x09800},
10550 { 0x00010000, 0x0a000},
10551 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010552 };
10553 struct mem_entry *mem_tbl;
10554 int err = 0;
10555 int i;
10556
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010557 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
10558 mem_tbl = mem_tbl_5717;
10559 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10560 mem_tbl = mem_tbl_57765;
10561 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlson321d32a2008-11-21 17:22:19 -080010562 mem_tbl = mem_tbl_5755;
10563 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10564 mem_tbl = mem_tbl_5906;
10565 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10566 mem_tbl = mem_tbl_5705;
10567 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010568 mem_tbl = mem_tbl_570x;
10569
10570 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
10571 if ((err = tg3_do_mem_test(tp, mem_tbl[i].offset,
10572 mem_tbl[i].len)) != 0)
10573 break;
10574 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010575
Michael Chan7942e1d2005-05-29 14:58:36 -070010576 return err;
10577}
10578
Michael Chan9f40dea2005-09-05 17:53:06 -070010579#define TG3_MAC_LOOPBACK 0
10580#define TG3_PHY_LOOPBACK 1
10581
10582static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010583{
Michael Chan9f40dea2005-09-05 17:53:06 -070010584 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010585 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010586 struct sk_buff *skb, *rx_skb;
10587 u8 *tx_data;
10588 dma_addr_t map;
10589 int num_pkts, tx_len, rx_len, i, err;
10590 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010591 struct tg3_napi *tnapi, *rnapi;
Matt Carlson21f581a2009-08-28 14:00:25 +000010592 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Michael Chanc76949a2005-05-29 14:58:59 -070010593
Matt Carlsonc8873402010-02-12 14:47:11 +000010594 tnapi = &tp->napi[0];
10595 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010596 if (tp->irq_cnt > 1) {
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010597 rnapi = &tp->napi[1];
Matt Carlsonc8873402010-02-12 14:47:11 +000010598 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
10599 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010600 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010601 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010602
Michael Chan9f40dea2005-09-05 17:53:06 -070010603 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010604 /* HW errata - mac loopback fails in some cases on 5780.
10605 * Normal traffic and PHY loopback are not affected by
10606 * errata.
10607 */
10608 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
10609 return 0;
10610
Michael Chan9f40dea2005-09-05 17:53:06 -070010611 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010612 MAC_MODE_PORT_INT_LPBACK;
10613 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10614 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chan3f7045c2006-09-27 16:02:29 -070010615 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
10616 mac_mode |= MAC_MODE_PORT_MODE_MII;
10617 else
10618 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010619 tw32(MAC_MODE, mac_mode);
10620 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010621 u32 val;
10622
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010623 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10624 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010625 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10626 } else
10627 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010628
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010629 tg3_phy_toggle_automdix(tp, 0);
10630
Michael Chan3f7045c2006-09-27 16:02:29 -070010631 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010632 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010633
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010634 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010635 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000010636 tg3_writephy(tp, MII_TG3_FET_PTEST,
10637 MII_TG3_FET_PTEST_FRC_TX_LINK |
10638 MII_TG3_FET_PTEST_FRC_TX_LOCK);
10639 /* The write needs to be flushed for the AC131 */
10640 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
10641 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080010642 mac_mode |= MAC_MODE_PORT_MODE_MII;
10643 } else
10644 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010645
Michael Chanc94e3942005-09-27 12:12:42 -070010646 /* reset to prevent losing 1st rx packet intermittently */
10647 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
10648 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10649 udelay(10);
10650 tw32_f(MAC_RX_MODE, tp->rx_mode);
10651 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010652 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000010653 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
10654 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010655 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000010656 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010657 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010658 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10659 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10660 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010661 tw32(MAC_MODE, mac_mode);
Matt Carlson859a588792010-04-05 10:19:28 +000010662 } else {
Michael Chan9f40dea2005-09-05 17:53:06 -070010663 return -EINVAL;
Matt Carlson859a588792010-04-05 10:19:28 +000010664 }
Michael Chanc76949a2005-05-29 14:58:59 -070010665
10666 err = -EIO;
10667
Michael Chanc76949a2005-05-29 14:58:59 -070010668 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010669 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010670 if (!skb)
10671 return -ENOMEM;
10672
Michael Chanc76949a2005-05-29 14:58:59 -070010673 tx_data = skb_put(skb, tx_len);
10674 memcpy(tx_data, tp->dev->dev_addr, 6);
10675 memset(tx_data + 6, 0x0, 8);
10676
10677 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10678
10679 for (i = 14; i < tx_len; i++)
10680 tx_data[i] = (u8) (i & 0xff);
10681
Alexander Duyckf4188d82009-12-02 16:48:38 +000010682 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
10683 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000010684 dev_kfree_skb(skb);
10685 return -EIO;
10686 }
Michael Chanc76949a2005-05-29 14:58:59 -070010687
10688 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010689 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010690
10691 udelay(10);
10692
Matt Carlson898a56f2009-08-28 14:02:40 +000010693 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070010694
Michael Chanc76949a2005-05-29 14:58:59 -070010695 num_pkts = 0;
10696
Alexander Duyckf4188d82009-12-02 16:48:38 +000010697 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070010698
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010699 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070010700 num_pkts++;
10701
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010702 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
10703 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070010704
10705 udelay(10);
10706
Matt Carlson303fc922009-11-02 14:27:34 +000010707 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
10708 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070010709 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010710 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010711
10712 udelay(10);
10713
Matt Carlson898a56f2009-08-28 14:02:40 +000010714 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
10715 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010716 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070010717 (rx_idx == (rx_start_idx + num_pkts)))
10718 break;
10719 }
10720
Alexander Duyckf4188d82009-12-02 16:48:38 +000010721 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070010722 dev_kfree_skb(skb);
10723
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010724 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070010725 goto out;
10726
10727 if (rx_idx != rx_start_idx + num_pkts)
10728 goto out;
10729
Matt Carlson72334482009-08-28 14:03:01 +000010730 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070010731 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
10732 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
10733 if (opaque_key != RXD_OPAQUE_RING_STD)
10734 goto out;
10735
10736 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
10737 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
10738 goto out;
10739
10740 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
10741 if (rx_len != tx_len)
10742 goto out;
10743
Matt Carlson21f581a2009-08-28 14:00:25 +000010744 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070010745
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +000010746 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070010747 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
10748
10749 for (i = 14; i < tx_len; i++) {
10750 if (*(rx_skb->data + i) != (u8) (i & 0xff))
10751 goto out;
10752 }
10753 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010754
Michael Chanc76949a2005-05-29 14:58:59 -070010755 /* tg3_free_rings will unmap and free the rx_skb */
10756out:
10757 return err;
10758}
10759
Michael Chan9f40dea2005-09-05 17:53:06 -070010760#define TG3_MAC_LOOPBACK_FAILED 1
10761#define TG3_PHY_LOOPBACK_FAILED 2
10762#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
10763 TG3_PHY_LOOPBACK_FAILED)
10764
10765static int tg3_test_loopback(struct tg3 *tp)
10766{
10767 int err = 0;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010768 u32 cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070010769
10770 if (!netif_running(tp->dev))
10771 return TG3_LOOPBACK_FAILED;
10772
Michael Chanb9ec6c12006-07-25 16:37:27 -070010773 err = tg3_reset_hw(tp, 1);
10774 if (err)
10775 return TG3_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070010776
Matt Carlson6833c042008-11-21 17:18:59 -080010777 /* Turn off gphy autopowerdown. */
10778 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10779 tg3_phy_toggle_apd(tp, false);
10780
Matt Carlson321d32a2008-11-21 17:22:19 -080010781 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010782 int i;
10783 u32 status;
10784
10785 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
10786
10787 /* Wait for up to 40 microseconds to acquire lock. */
10788 for (i = 0; i < 4; i++) {
10789 status = tr32(TG3_CPMU_MUTEX_GNT);
10790 if (status == CPMU_MUTEX_GNT_DRIVER)
10791 break;
10792 udelay(10);
10793 }
10794
10795 if (status != CPMU_MUTEX_GNT_DRIVER)
10796 return TG3_LOOPBACK_FAILED;
10797
Matt Carlsonb2a5c192008-04-03 21:44:44 -070010798 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080010799 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070010800 tw32(TG3_CPMU_CTRL,
10801 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
10802 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070010803 }
10804
Michael Chan9f40dea2005-09-05 17:53:06 -070010805 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
10806 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010807
Matt Carlson321d32a2008-11-21 17:22:19 -080010808 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010809 tw32(TG3_CPMU_CTRL, cpmuctrl);
10810
10811 /* Release the mutex */
10812 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
10813 }
10814
Matt Carlsondd477002008-05-25 23:45:58 -070010815 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
10816 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070010817 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
10818 err |= TG3_PHY_LOOPBACK_FAILED;
10819 }
10820
Matt Carlson6833c042008-11-21 17:18:59 -080010821 /* Re-enable gphy autopowerdown. */
10822 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10823 tg3_phy_toggle_apd(tp, true);
10824
Michael Chan9f40dea2005-09-05 17:53:06 -070010825 return err;
10826}
10827
Michael Chan4cafd3f2005-05-29 14:56:34 -070010828static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
10829 u64 *data)
10830{
Michael Chan566f86a2005-05-29 14:56:58 -070010831 struct tg3 *tp = netdev_priv(dev);
10832
Michael Chanbc1c7562006-03-20 17:48:03 -080010833 if (tp->link_config.phy_is_low_power)
10834 tg3_set_power_state(tp, PCI_D0);
10835
Michael Chan566f86a2005-05-29 14:56:58 -070010836 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
10837
10838 if (tg3_test_nvram(tp) != 0) {
10839 etest->flags |= ETH_TEST_FL_FAILED;
10840 data[0] = 1;
10841 }
Michael Chanca430072005-05-29 14:57:23 -070010842 if (tg3_test_link(tp) != 0) {
10843 etest->flags |= ETH_TEST_FL_FAILED;
10844 data[1] = 1;
10845 }
Michael Chana71116d2005-05-29 14:58:11 -070010846 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010847 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070010848
Michael Chanbbe832c2005-06-24 20:20:04 -070010849 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010850 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010851 tg3_netif_stop(tp);
10852 irq_sync = 1;
10853 }
10854
10855 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070010856
10857 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080010858 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010859 tg3_halt_cpu(tp, RX_CPU_BASE);
10860 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10861 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080010862 if (!err)
10863 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010864
Michael Chand9ab5ad2006-03-20 22:27:35 -080010865 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
10866 tg3_phy_reset(tp);
10867
Michael Chana71116d2005-05-29 14:58:11 -070010868 if (tg3_test_registers(tp) != 0) {
10869 etest->flags |= ETH_TEST_FL_FAILED;
10870 data[2] = 1;
10871 }
Michael Chan7942e1d2005-05-29 14:58:36 -070010872 if (tg3_test_memory(tp) != 0) {
10873 etest->flags |= ETH_TEST_FL_FAILED;
10874 data[3] = 1;
10875 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010876 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070010877 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070010878
David S. Millerf47c11e2005-06-24 20:18:35 -070010879 tg3_full_unlock(tp);
10880
Michael Chand4bc3922005-05-29 14:59:20 -070010881 if (tg3_test_interrupt(tp) != 0) {
10882 etest->flags |= ETH_TEST_FL_FAILED;
10883 data[5] = 1;
10884 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010885
10886 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070010887
Michael Chana71116d2005-05-29 14:58:11 -070010888 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10889 if (netif_running(dev)) {
10890 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010891 err2 = tg3_restart_hw(tp, 1);
10892 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070010893 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010894 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010895
10896 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010897
10898 if (irq_sync && !err2)
10899 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010900 }
Michael Chanbc1c7562006-03-20 17:48:03 -080010901 if (tp->link_config.phy_is_low_power)
10902 tg3_set_power_state(tp, PCI_D3hot);
10903
Michael Chan4cafd3f2005-05-29 14:56:34 -070010904}
10905
Linus Torvalds1da177e2005-04-16 15:20:36 -070010906static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10907{
10908 struct mii_ioctl_data *data = if_mii(ifr);
10909 struct tg3 *tp = netdev_priv(dev);
10910 int err;
10911
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010912 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010913 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010914 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
10915 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010916 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10917 return phy_mii_ioctl(phydev, data, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010918 }
10919
Matt Carlson33f401a2010-04-05 10:19:27 +000010920 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010921 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000010922 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010923
10924 /* fallthru */
10925 case SIOCGMIIREG: {
10926 u32 mii_regval;
10927
10928 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10929 break; /* We have no PHY */
10930
Michael Chanbc1c7562006-03-20 17:48:03 -080010931 if (tp->link_config.phy_is_low_power)
10932 return -EAGAIN;
10933
David S. Millerf47c11e2005-06-24 20:18:35 -070010934 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010935 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070010936 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010937
10938 data->val_out = mii_regval;
10939
10940 return err;
10941 }
10942
10943 case SIOCSMIIREG:
10944 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10945 break; /* We have no PHY */
10946
Michael Chanbc1c7562006-03-20 17:48:03 -080010947 if (tp->link_config.phy_is_low_power)
10948 return -EAGAIN;
10949
David S. Millerf47c11e2005-06-24 20:18:35 -070010950 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010951 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070010952 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010953
10954 return err;
10955
10956 default:
10957 /* do nothing */
10958 break;
10959 }
10960 return -EOPNOTSUPP;
10961}
10962
10963#if TG3_VLAN_TAG_USED
10964static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
10965{
10966 struct tg3 *tp = netdev_priv(dev);
10967
Matt Carlson844b3ee2009-02-25 14:23:56 +000010968 if (!netif_running(dev)) {
10969 tp->vlgrp = grp;
10970 return;
10971 }
10972
10973 tg3_netif_stop(tp);
Michael Chan29315e82006-06-29 20:12:30 -070010974
David S. Millerf47c11e2005-06-24 20:18:35 -070010975 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010976
10977 tp->vlgrp = grp;
10978
10979 /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
10980 __tg3_set_rx_mode(dev);
10981
Matt Carlson844b3ee2009-02-25 14:23:56 +000010982 tg3_netif_start(tp);
Michael Chan46966542007-07-11 19:47:19 -070010983
10984 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010985}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010986#endif
10987
David S. Miller15f98502005-05-18 22:49:26 -070010988static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
10989{
10990 struct tg3 *tp = netdev_priv(dev);
10991
10992 memcpy(ec, &tp->coal, sizeof(*ec));
10993 return 0;
10994}
10995
Michael Chand244c892005-07-05 14:42:33 -070010996static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
10997{
10998 struct tg3 *tp = netdev_priv(dev);
10999 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11000 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11001
11002 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11003 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11004 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11005 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11006 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11007 }
11008
11009 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11010 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11011 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11012 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11013 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11014 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11015 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11016 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11017 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11018 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11019 return -EINVAL;
11020
11021 /* No rx interrupts will be generated if both are zero */
11022 if ((ec->rx_coalesce_usecs == 0) &&
11023 (ec->rx_max_coalesced_frames == 0))
11024 return -EINVAL;
11025
11026 /* No tx interrupts will be generated if both are zero */
11027 if ((ec->tx_coalesce_usecs == 0) &&
11028 (ec->tx_max_coalesced_frames == 0))
11029 return -EINVAL;
11030
11031 /* Only copy relevant parameters, ignore all others. */
11032 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11033 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11034 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11035 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11036 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11037 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11038 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11039 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11040 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11041
11042 if (netif_running(dev)) {
11043 tg3_full_lock(tp, 0);
11044 __tg3_set_coalesce(tp, &tp->coal);
11045 tg3_full_unlock(tp);
11046 }
11047 return 0;
11048}
11049
Jeff Garzik7282d492006-09-13 14:30:00 -040011050static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011051 .get_settings = tg3_get_settings,
11052 .set_settings = tg3_set_settings,
11053 .get_drvinfo = tg3_get_drvinfo,
11054 .get_regs_len = tg3_get_regs_len,
11055 .get_regs = tg3_get_regs,
11056 .get_wol = tg3_get_wol,
11057 .set_wol = tg3_set_wol,
11058 .get_msglevel = tg3_get_msglevel,
11059 .set_msglevel = tg3_set_msglevel,
11060 .nway_reset = tg3_nway_reset,
11061 .get_link = ethtool_op_get_link,
11062 .get_eeprom_len = tg3_get_eeprom_len,
11063 .get_eeprom = tg3_get_eeprom,
11064 .set_eeprom = tg3_set_eeprom,
11065 .get_ringparam = tg3_get_ringparam,
11066 .set_ringparam = tg3_set_ringparam,
11067 .get_pauseparam = tg3_get_pauseparam,
11068 .set_pauseparam = tg3_set_pauseparam,
11069 .get_rx_csum = tg3_get_rx_csum,
11070 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011071 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011072 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011073 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011074 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011075 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070011076 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011077 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011078 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011079 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011080 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011081};
11082
11083static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11084{
Michael Chan1b277772006-03-20 22:27:48 -080011085 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011086
11087 tp->nvram_size = EEPROM_CHIP_SIZE;
11088
Matt Carlsone4f34112009-02-25 14:25:00 +000011089 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011090 return;
11091
Michael Chanb16250e2006-09-27 16:10:14 -070011092 if ((magic != TG3_EEPROM_MAGIC) &&
11093 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11094 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011095 return;
11096
11097 /*
11098 * Size the chip by reading offsets at increasing powers of two.
11099 * When we encounter our validation signature, we know the addressing
11100 * has wrapped around, and thus have our chip size.
11101 */
Michael Chan1b277772006-03-20 22:27:48 -080011102 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011103
11104 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011105 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011106 return;
11107
Michael Chan18201802006-03-20 22:29:15 -080011108 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011109 break;
11110
11111 cursize <<= 1;
11112 }
11113
11114 tp->nvram_size = cursize;
11115}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011116
Linus Torvalds1da177e2005-04-16 15:20:36 -070011117static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11118{
11119 u32 val;
11120
Matt Carlsondf259d82009-04-20 06:57:14 +000011121 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11122 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011123 return;
11124
11125 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011126 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011127 tg3_get_eeprom_size(tp);
11128 return;
11129 }
11130
Matt Carlson6d348f22009-02-25 14:25:52 +000011131 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011132 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011133 /* This is confusing. We want to operate on the
11134 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11135 * call will read from NVRAM and byteswap the data
11136 * according to the byteswapping settings for all
11137 * other register accesses. This ensures the data we
11138 * want will always reside in the lower 16-bits.
11139 * However, the data in NVRAM is in LE format, which
11140 * means the data from the NVRAM read will always be
11141 * opposite the endianness of the CPU. The 16-bit
11142 * byteswap then brings the data to CPU endianness.
11143 */
11144 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011145 return;
11146 }
11147 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011148 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011149}
11150
11151static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11152{
11153 u32 nvcfg1;
11154
11155 nvcfg1 = tr32(NVRAM_CFG1);
11156 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11157 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011158 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011159 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11160 tw32(NVRAM_CFG1, nvcfg1);
11161 }
11162
Michael Chan4c987482005-09-05 17:52:38 -070011163 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011164 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011165 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011166 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11167 tp->nvram_jedecnum = JEDEC_ATMEL;
11168 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11169 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11170 break;
11171 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11172 tp->nvram_jedecnum = JEDEC_ATMEL;
11173 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11174 break;
11175 case FLASH_VENDOR_ATMEL_EEPROM:
11176 tp->nvram_jedecnum = JEDEC_ATMEL;
11177 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11178 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11179 break;
11180 case FLASH_VENDOR_ST:
11181 tp->nvram_jedecnum = JEDEC_ST;
11182 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11183 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11184 break;
11185 case FLASH_VENDOR_SAIFUN:
11186 tp->nvram_jedecnum = JEDEC_SAIFUN;
11187 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11188 break;
11189 case FLASH_VENDOR_SST_SMALL:
11190 case FLASH_VENDOR_SST_LARGE:
11191 tp->nvram_jedecnum = JEDEC_SST;
11192 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11193 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011194 }
Matt Carlson8590a602009-08-28 12:29:16 +000011195 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011196 tp->nvram_jedecnum = JEDEC_ATMEL;
11197 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11198 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11199 }
11200}
11201
Matt Carlsona1b950d2009-09-01 13:20:17 +000011202static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11203{
11204 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11205 case FLASH_5752PAGE_SIZE_256:
11206 tp->nvram_pagesize = 256;
11207 break;
11208 case FLASH_5752PAGE_SIZE_512:
11209 tp->nvram_pagesize = 512;
11210 break;
11211 case FLASH_5752PAGE_SIZE_1K:
11212 tp->nvram_pagesize = 1024;
11213 break;
11214 case FLASH_5752PAGE_SIZE_2K:
11215 tp->nvram_pagesize = 2048;
11216 break;
11217 case FLASH_5752PAGE_SIZE_4K:
11218 tp->nvram_pagesize = 4096;
11219 break;
11220 case FLASH_5752PAGE_SIZE_264:
11221 tp->nvram_pagesize = 264;
11222 break;
11223 case FLASH_5752PAGE_SIZE_528:
11224 tp->nvram_pagesize = 528;
11225 break;
11226 }
11227}
11228
Michael Chan361b4ac2005-04-21 17:11:21 -070011229static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11230{
11231 u32 nvcfg1;
11232
11233 nvcfg1 = tr32(NVRAM_CFG1);
11234
Michael Chane6af3012005-04-21 17:12:05 -070011235 /* NVRAM protection for TPM */
11236 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011237 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011238
Michael Chan361b4ac2005-04-21 17:11:21 -070011239 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011240 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11241 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11242 tp->nvram_jedecnum = JEDEC_ATMEL;
11243 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11244 break;
11245 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11246 tp->nvram_jedecnum = JEDEC_ATMEL;
11247 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11248 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11249 break;
11250 case FLASH_5752VENDOR_ST_M45PE10:
11251 case FLASH_5752VENDOR_ST_M45PE20:
11252 case FLASH_5752VENDOR_ST_M45PE40:
11253 tp->nvram_jedecnum = JEDEC_ST;
11254 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11255 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11256 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011257 }
11258
11259 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011260 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011261 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011262 /* For eeprom, set pagesize to maximum eeprom size */
11263 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11264
11265 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11266 tw32(NVRAM_CFG1, nvcfg1);
11267 }
11268}
11269
Michael Chand3c7b882006-03-23 01:28:25 -080011270static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11271{
Matt Carlson989a9d22007-05-05 11:51:05 -070011272 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011273
11274 nvcfg1 = tr32(NVRAM_CFG1);
11275
11276 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011277 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011278 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011279 protect = 1;
11280 }
Michael Chand3c7b882006-03-23 01:28:25 -080011281
Matt Carlson989a9d22007-05-05 11:51:05 -070011282 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11283 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011284 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11285 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11286 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11287 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11288 tp->nvram_jedecnum = JEDEC_ATMEL;
11289 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11290 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11291 tp->nvram_pagesize = 264;
11292 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11293 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11294 tp->nvram_size = (protect ? 0x3e200 :
11295 TG3_NVRAM_SIZE_512KB);
11296 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11297 tp->nvram_size = (protect ? 0x1f200 :
11298 TG3_NVRAM_SIZE_256KB);
11299 else
11300 tp->nvram_size = (protect ? 0x1f200 :
11301 TG3_NVRAM_SIZE_128KB);
11302 break;
11303 case FLASH_5752VENDOR_ST_M45PE10:
11304 case FLASH_5752VENDOR_ST_M45PE20:
11305 case FLASH_5752VENDOR_ST_M45PE40:
11306 tp->nvram_jedecnum = JEDEC_ST;
11307 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11308 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11309 tp->nvram_pagesize = 256;
11310 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11311 tp->nvram_size = (protect ?
11312 TG3_NVRAM_SIZE_64KB :
11313 TG3_NVRAM_SIZE_128KB);
11314 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11315 tp->nvram_size = (protect ?
11316 TG3_NVRAM_SIZE_64KB :
11317 TG3_NVRAM_SIZE_256KB);
11318 else
11319 tp->nvram_size = (protect ?
11320 TG3_NVRAM_SIZE_128KB :
11321 TG3_NVRAM_SIZE_512KB);
11322 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011323 }
11324}
11325
Michael Chan1b277772006-03-20 22:27:48 -080011326static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11327{
11328 u32 nvcfg1;
11329
11330 nvcfg1 = tr32(NVRAM_CFG1);
11331
11332 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011333 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11334 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11335 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11336 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11337 tp->nvram_jedecnum = JEDEC_ATMEL;
11338 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11339 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011340
Matt Carlson8590a602009-08-28 12:29:16 +000011341 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11342 tw32(NVRAM_CFG1, nvcfg1);
11343 break;
11344 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11345 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11346 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11347 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11348 tp->nvram_jedecnum = JEDEC_ATMEL;
11349 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11350 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11351 tp->nvram_pagesize = 264;
11352 break;
11353 case FLASH_5752VENDOR_ST_M45PE10:
11354 case FLASH_5752VENDOR_ST_M45PE20:
11355 case FLASH_5752VENDOR_ST_M45PE40:
11356 tp->nvram_jedecnum = JEDEC_ST;
11357 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11358 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11359 tp->nvram_pagesize = 256;
11360 break;
Michael Chan1b277772006-03-20 22:27:48 -080011361 }
11362}
11363
Matt Carlson6b91fa02007-10-10 18:01:09 -070011364static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11365{
11366 u32 nvcfg1, protect = 0;
11367
11368 nvcfg1 = tr32(NVRAM_CFG1);
11369
11370 /* NVRAM protection for TPM */
11371 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011372 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011373 protect = 1;
11374 }
11375
11376 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11377 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011378 case FLASH_5761VENDOR_ATMEL_ADB021D:
11379 case FLASH_5761VENDOR_ATMEL_ADB041D:
11380 case FLASH_5761VENDOR_ATMEL_ADB081D:
11381 case FLASH_5761VENDOR_ATMEL_ADB161D:
11382 case FLASH_5761VENDOR_ATMEL_MDB021D:
11383 case FLASH_5761VENDOR_ATMEL_MDB041D:
11384 case FLASH_5761VENDOR_ATMEL_MDB081D:
11385 case FLASH_5761VENDOR_ATMEL_MDB161D:
11386 tp->nvram_jedecnum = JEDEC_ATMEL;
11387 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11388 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11389 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11390 tp->nvram_pagesize = 256;
11391 break;
11392 case FLASH_5761VENDOR_ST_A_M45PE20:
11393 case FLASH_5761VENDOR_ST_A_M45PE40:
11394 case FLASH_5761VENDOR_ST_A_M45PE80:
11395 case FLASH_5761VENDOR_ST_A_M45PE16:
11396 case FLASH_5761VENDOR_ST_M_M45PE20:
11397 case FLASH_5761VENDOR_ST_M_M45PE40:
11398 case FLASH_5761VENDOR_ST_M_M45PE80:
11399 case FLASH_5761VENDOR_ST_M_M45PE16:
11400 tp->nvram_jedecnum = JEDEC_ST;
11401 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11402 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11403 tp->nvram_pagesize = 256;
11404 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011405 }
11406
11407 if (protect) {
11408 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11409 } else {
11410 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011411 case FLASH_5761VENDOR_ATMEL_ADB161D:
11412 case FLASH_5761VENDOR_ATMEL_MDB161D:
11413 case FLASH_5761VENDOR_ST_A_M45PE16:
11414 case FLASH_5761VENDOR_ST_M_M45PE16:
11415 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11416 break;
11417 case FLASH_5761VENDOR_ATMEL_ADB081D:
11418 case FLASH_5761VENDOR_ATMEL_MDB081D:
11419 case FLASH_5761VENDOR_ST_A_M45PE80:
11420 case FLASH_5761VENDOR_ST_M_M45PE80:
11421 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11422 break;
11423 case FLASH_5761VENDOR_ATMEL_ADB041D:
11424 case FLASH_5761VENDOR_ATMEL_MDB041D:
11425 case FLASH_5761VENDOR_ST_A_M45PE40:
11426 case FLASH_5761VENDOR_ST_M_M45PE40:
11427 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11428 break;
11429 case FLASH_5761VENDOR_ATMEL_ADB021D:
11430 case FLASH_5761VENDOR_ATMEL_MDB021D:
11431 case FLASH_5761VENDOR_ST_A_M45PE20:
11432 case FLASH_5761VENDOR_ST_M_M45PE20:
11433 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11434 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011435 }
11436 }
11437}
11438
Michael Chanb5d37722006-09-27 16:06:21 -070011439static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11440{
11441 tp->nvram_jedecnum = JEDEC_ATMEL;
11442 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11443 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11444}
11445
Matt Carlson321d32a2008-11-21 17:22:19 -080011446static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11447{
11448 u32 nvcfg1;
11449
11450 nvcfg1 = tr32(NVRAM_CFG1);
11451
11452 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11453 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11454 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11455 tp->nvram_jedecnum = JEDEC_ATMEL;
11456 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11457 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11458
11459 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11460 tw32(NVRAM_CFG1, nvcfg1);
11461 return;
11462 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11463 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11464 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11465 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11466 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11467 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11468 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11469 tp->nvram_jedecnum = JEDEC_ATMEL;
11470 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11471 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11472
11473 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11474 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11475 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11476 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11477 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11478 break;
11479 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11480 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11481 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11482 break;
11483 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11484 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11485 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11486 break;
11487 }
11488 break;
11489 case FLASH_5752VENDOR_ST_M45PE10:
11490 case FLASH_5752VENDOR_ST_M45PE20:
11491 case FLASH_5752VENDOR_ST_M45PE40:
11492 tp->nvram_jedecnum = JEDEC_ST;
11493 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11494 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11495
11496 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11497 case FLASH_5752VENDOR_ST_M45PE10:
11498 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11499 break;
11500 case FLASH_5752VENDOR_ST_M45PE20:
11501 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11502 break;
11503 case FLASH_5752VENDOR_ST_M45PE40:
11504 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11505 break;
11506 }
11507 break;
11508 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011509 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011510 return;
11511 }
11512
Matt Carlsona1b950d2009-09-01 13:20:17 +000011513 tg3_nvram_get_pagesize(tp, nvcfg1);
11514 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011515 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011516}
11517
11518
11519static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11520{
11521 u32 nvcfg1;
11522
11523 nvcfg1 = tr32(NVRAM_CFG1);
11524
11525 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11526 case FLASH_5717VENDOR_ATMEL_EEPROM:
11527 case FLASH_5717VENDOR_MICRO_EEPROM:
11528 tp->nvram_jedecnum = JEDEC_ATMEL;
11529 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11530 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11531
11532 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11533 tw32(NVRAM_CFG1, nvcfg1);
11534 return;
11535 case FLASH_5717VENDOR_ATMEL_MDB011D:
11536 case FLASH_5717VENDOR_ATMEL_ADB011B:
11537 case FLASH_5717VENDOR_ATMEL_ADB011D:
11538 case FLASH_5717VENDOR_ATMEL_MDB021D:
11539 case FLASH_5717VENDOR_ATMEL_ADB021B:
11540 case FLASH_5717VENDOR_ATMEL_ADB021D:
11541 case FLASH_5717VENDOR_ATMEL_45USPT:
11542 tp->nvram_jedecnum = JEDEC_ATMEL;
11543 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11544 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11545
11546 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11547 case FLASH_5717VENDOR_ATMEL_MDB021D:
11548 case FLASH_5717VENDOR_ATMEL_ADB021B:
11549 case FLASH_5717VENDOR_ATMEL_ADB021D:
11550 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11551 break;
11552 default:
11553 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11554 break;
11555 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011556 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011557 case FLASH_5717VENDOR_ST_M_M25PE10:
11558 case FLASH_5717VENDOR_ST_A_M25PE10:
11559 case FLASH_5717VENDOR_ST_M_M45PE10:
11560 case FLASH_5717VENDOR_ST_A_M45PE10:
11561 case FLASH_5717VENDOR_ST_M_M25PE20:
11562 case FLASH_5717VENDOR_ST_A_M25PE20:
11563 case FLASH_5717VENDOR_ST_M_M45PE20:
11564 case FLASH_5717VENDOR_ST_A_M45PE20:
11565 case FLASH_5717VENDOR_ST_25USPT:
11566 case FLASH_5717VENDOR_ST_45USPT:
11567 tp->nvram_jedecnum = JEDEC_ST;
11568 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11569 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11570
11571 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11572 case FLASH_5717VENDOR_ST_M_M25PE20:
11573 case FLASH_5717VENDOR_ST_A_M25PE20:
11574 case FLASH_5717VENDOR_ST_M_M45PE20:
11575 case FLASH_5717VENDOR_ST_A_M45PE20:
11576 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11577 break;
11578 default:
11579 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11580 break;
11581 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011582 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011583 default:
11584 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11585 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011586 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011587
11588 tg3_nvram_get_pagesize(tp, nvcfg1);
11589 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11590 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011591}
11592
Linus Torvalds1da177e2005-04-16 15:20:36 -070011593/* Chips other than 5700/5701 use the NVRAM for fetching info. */
11594static void __devinit tg3_nvram_init(struct tg3 *tp)
11595{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011596 tw32_f(GRC_EEPROM_ADDR,
11597 (EEPROM_ADDR_FSM_RESET |
11598 (EEPROM_DEFAULT_CLOCK_PERIOD <<
11599 EEPROM_ADDR_CLKPERD_SHIFT)));
11600
Michael Chan9d57f012006-12-07 00:23:25 -080011601 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011602
11603 /* Enable seeprom accesses. */
11604 tw32_f(GRC_LOCAL_CTRL,
11605 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
11606 udelay(100);
11607
11608 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
11609 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
11610 tp->tg3_flags |= TG3_FLAG_NVRAM;
11611
Michael Chanec41c7d2006-01-17 02:40:55 -080011612 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000011613 netdev_warn(tp->dev,
11614 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000011615 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080011616 return;
11617 }
Michael Chane6af3012005-04-21 17:12:05 -070011618 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011619
Matt Carlson989a9d22007-05-05 11:51:05 -070011620 tp->nvram_size = 0;
11621
Michael Chan361b4ac2005-04-21 17:11:21 -070011622 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11623 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080011624 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
11625 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070011626 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011627 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
11628 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080011629 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011630 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
11631 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070011632 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11633 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000011634 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
11635 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080011636 tg3_get_57780_nvram_info(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000011637 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
11638 tg3_get_5717_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070011639 else
11640 tg3_get_nvram_info(tp);
11641
Matt Carlson989a9d22007-05-05 11:51:05 -070011642 if (tp->nvram_size == 0)
11643 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011644
Michael Chane6af3012005-04-21 17:12:05 -070011645 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080011646 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011647
11648 } else {
11649 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
11650
11651 tg3_get_eeprom_size(tp);
11652 }
11653}
11654
Linus Torvalds1da177e2005-04-16 15:20:36 -070011655static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
11656 u32 offset, u32 len, u8 *buf)
11657{
11658 int i, j, rc = 0;
11659 u32 val;
11660
11661 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011662 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011663 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011664
11665 addr = offset + i;
11666
11667 memcpy(&data, buf + i, 4);
11668
Matt Carlson62cedd12009-04-20 14:52:29 -070011669 /*
11670 * The SEEPROM interface expects the data to always be opposite
11671 * the native endian format. We accomplish this by reversing
11672 * all the operations that would have been performed on the
11673 * data from a call to tg3_nvram_read_be32().
11674 */
11675 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011676
11677 val = tr32(GRC_EEPROM_ADDR);
11678 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
11679
11680 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
11681 EEPROM_ADDR_READ);
11682 tw32(GRC_EEPROM_ADDR, val |
11683 (0 << EEPROM_ADDR_DEVID_SHIFT) |
11684 (addr & EEPROM_ADDR_ADDR_MASK) |
11685 EEPROM_ADDR_START |
11686 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011687
Michael Chan9d57f012006-12-07 00:23:25 -080011688 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011689 val = tr32(GRC_EEPROM_ADDR);
11690
11691 if (val & EEPROM_ADDR_COMPLETE)
11692 break;
Michael Chan9d57f012006-12-07 00:23:25 -080011693 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011694 }
11695 if (!(val & EEPROM_ADDR_COMPLETE)) {
11696 rc = -EBUSY;
11697 break;
11698 }
11699 }
11700
11701 return rc;
11702}
11703
11704/* offset and length are dword aligned */
11705static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
11706 u8 *buf)
11707{
11708 int ret = 0;
11709 u32 pagesize = tp->nvram_pagesize;
11710 u32 pagemask = pagesize - 1;
11711 u32 nvram_cmd;
11712 u8 *tmp;
11713
11714 tmp = kmalloc(pagesize, GFP_KERNEL);
11715 if (tmp == NULL)
11716 return -ENOMEM;
11717
11718 while (len) {
11719 int j;
Michael Chane6af3012005-04-21 17:12:05 -070011720 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011721
11722 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011723
Linus Torvalds1da177e2005-04-16 15:20:36 -070011724 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011725 ret = tg3_nvram_read_be32(tp, phy_addr + j,
11726 (__be32 *) (tmp + j));
11727 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011728 break;
11729 }
11730 if (ret)
11731 break;
11732
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011733 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011734 size = pagesize;
11735 if (len < size)
11736 size = len;
11737
11738 len -= size;
11739
11740 memcpy(tmp + page_off, buf, size);
11741
11742 offset = offset + (pagesize - page_off);
11743
Michael Chane6af3012005-04-21 17:12:05 -070011744 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011745
11746 /*
11747 * Before we can erase the flash page, we need
11748 * to issue a special "write enable" command.
11749 */
11750 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11751
11752 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11753 break;
11754
11755 /* Erase the target page */
11756 tw32(NVRAM_ADDR, phy_addr);
11757
11758 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
11759 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
11760
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011761 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011762 break;
11763
11764 /* Issue another write enable to start the write. */
11765 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11766
11767 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11768 break;
11769
11770 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011771 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011772
Al Virob9fc7dc2007-12-17 22:59:57 -080011773 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000011774
Al Virob9fc7dc2007-12-17 22:59:57 -080011775 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011776
11777 tw32(NVRAM_ADDR, phy_addr + j);
11778
11779 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
11780 NVRAM_CMD_WR;
11781
11782 if (j == 0)
11783 nvram_cmd |= NVRAM_CMD_FIRST;
11784 else if (j == (pagesize - 4))
11785 nvram_cmd |= NVRAM_CMD_LAST;
11786
11787 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11788 break;
11789 }
11790 if (ret)
11791 break;
11792 }
11793
11794 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11795 tg3_nvram_exec_cmd(tp, nvram_cmd);
11796
11797 kfree(tmp);
11798
11799 return ret;
11800}
11801
11802/* offset and length are dword aligned */
11803static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
11804 u8 *buf)
11805{
11806 int i, ret = 0;
11807
11808 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011809 u32 page_off, phy_addr, nvram_cmd;
11810 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011811
11812 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080011813 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011814
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011815 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011816
Michael Chan18201802006-03-20 22:29:15 -080011817 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011818
11819 tw32(NVRAM_ADDR, phy_addr);
11820
11821 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
11822
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011823 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011824 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070011825 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011826 nvram_cmd |= NVRAM_CMD_LAST;
11827
11828 if (i == (len - 4))
11829 nvram_cmd |= NVRAM_CMD_LAST;
11830
Matt Carlson321d32a2008-11-21 17:22:19 -080011831 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
11832 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070011833 (tp->nvram_jedecnum == JEDEC_ST) &&
11834 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011835
11836 if ((ret = tg3_nvram_exec_cmd(tp,
11837 NVRAM_CMD_WREN | NVRAM_CMD_GO |
11838 NVRAM_CMD_DONE)))
11839
11840 break;
11841 }
11842 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11843 /* We always do complete word writes to eeprom. */
11844 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
11845 }
11846
11847 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11848 break;
11849 }
11850 return ret;
11851}
11852
11853/* offset and length are dword aligned */
11854static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
11855{
11856 int ret;
11857
Linus Torvalds1da177e2005-04-16 15:20:36 -070011858 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011859 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
11860 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011861 udelay(40);
11862 }
11863
11864 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
11865 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000011866 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011867 u32 grc_mode;
11868
Michael Chanec41c7d2006-01-17 02:40:55 -080011869 ret = tg3_nvram_lock(tp);
11870 if (ret)
11871 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011872
Michael Chane6af3012005-04-21 17:12:05 -070011873 tg3_enable_nvram_access(tp);
11874 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011875 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011876 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011877
11878 grc_mode = tr32(GRC_MODE);
11879 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
11880
11881 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
11882 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11883
11884 ret = tg3_nvram_write_block_buffered(tp, offset, len,
11885 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000011886 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011887 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
11888 buf);
11889 }
11890
11891 grc_mode = tr32(GRC_MODE);
11892 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
11893
Michael Chane6af3012005-04-21 17:12:05 -070011894 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011895 tg3_nvram_unlock(tp);
11896 }
11897
11898 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011899 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011900 udelay(40);
11901 }
11902
11903 return ret;
11904}
11905
11906struct subsys_tbl_ent {
11907 u16 subsys_vendor, subsys_devid;
11908 u32 phy_id;
11909};
11910
Matt Carlson24daf2b2010-02-17 15:17:02 +000011911static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011912 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011913 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011914 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011915 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011916 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011917 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011918 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011919 { TG3PCI_SUBVENDOR_ID_BROADCOM,
11920 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
11921 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011922 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011923 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011924 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011925 { TG3PCI_SUBVENDOR_ID_BROADCOM,
11926 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
11927 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011928 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011929 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011930 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011931 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011932 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011933 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011934 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011935
11936 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011937 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011938 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011939 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011940 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011941 { TG3PCI_SUBVENDOR_ID_3COM,
11942 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
11943 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011944 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011945 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000011946 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011947
11948 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011949 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011950 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011951 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011952 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011953 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011954 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011955 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000011956 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011957
11958 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011959 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011960 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011961 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011962 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011963 { TG3PCI_SUBVENDOR_ID_COMPAQ,
11964 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
11965 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011966 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000011967 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000011968 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011969
11970 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000011971 { TG3PCI_SUBVENDOR_ID_IBM,
11972 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011973};
11974
Matt Carlson24daf2b2010-02-17 15:17:02 +000011975static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011976{
11977 int i;
11978
11979 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
11980 if ((subsys_id_to_phy_id[i].subsys_vendor ==
11981 tp->pdev->subsystem_vendor) &&
11982 (subsys_id_to_phy_id[i].subsys_devid ==
11983 tp->pdev->subsystem_device))
11984 return &subsys_id_to_phy_id[i];
11985 }
11986 return NULL;
11987}
11988
Michael Chan7d0c41e2005-04-21 17:06:20 -070011989static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011990{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011991 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080011992 u16 pmcsr;
11993
11994 /* On some early chips the SRAM cannot be accessed in D3hot state,
11995 * so need make sure we're in D0.
11996 */
11997 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
11998 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
11999 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12000 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012001
12002 /* Make sure register accesses (indirect or otherwise)
12003 * will function correctly.
12004 */
12005 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12006 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012007
David S. Millerf49639e2006-06-09 11:58:36 -070012008 /* The memory arbiter has to be enabled in order for SRAM accesses
12009 * to succeed. Normally on powerup the tg3 chip firmware will make
12010 * sure it is enabled, but other entities such as system netboot
12011 * code might disable it.
12012 */
12013 val = tr32(MEMARB_MODE);
12014 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12015
Matt Carlson79eb6902010-02-17 15:17:03 +000012016 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012017 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12018
Gary Zambranoa85feb82007-05-05 11:52:19 -070012019 /* Assume an onboard device and WOL capable by default. */
12020 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080012021
Michael Chanb5d37722006-09-27 16:06:21 -070012022 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012023 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070012024 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012025 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12026 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012027 val = tr32(VCPU_CFGSHDW);
12028 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070012029 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070012030 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080012031 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070012032 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012033 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012034 }
12035
Linus Torvalds1da177e2005-04-16 15:20:36 -070012036 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12037 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12038 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012039 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012040 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012041
12042 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12043 tp->nic_sram_data_cfg = nic_cfg;
12044
12045 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12046 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12047 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12048 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12049 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12050 (ver > 0) && (ver < 0x100))
12051 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12052
Matt Carlsona9daf362008-05-25 23:49:44 -070012053 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12054 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12055
Linus Torvalds1da177e2005-04-16 15:20:36 -070012056 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12057 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12058 eeprom_phy_serdes = 1;
12059
12060 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12061 if (nic_phy_id != 0) {
12062 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12063 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12064
12065 eeprom_phy_id = (id1 >> 16) << 10;
12066 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12067 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12068 } else
12069 eeprom_phy_id = 0;
12070
Michael Chan7d0c41e2005-04-21 17:06:20 -070012071 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012072 if (eeprom_phy_serdes) {
Matt Carlsond1ec96a2010-01-12 10:11:38 +000012073 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
12074 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan747e8f82005-07-25 12:33:22 -070012075 tp->tg3_flags2 |= TG3_FLG2_MII_SERDES;
12076 else
12077 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
12078 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012079
John W. Linvillecbf46852005-04-21 17:01:29 -070012080 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012081 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12082 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012083 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012084 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12085
12086 switch (led_cfg) {
12087 default:
12088 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12089 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12090 break;
12091
12092 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12093 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12094 break;
12095
12096 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12097 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012098
12099 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12100 * read on some older 5700/5701 bootcode.
12101 */
12102 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12103 ASIC_REV_5700 ||
12104 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12105 ASIC_REV_5701)
12106 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12107
Linus Torvalds1da177e2005-04-16 15:20:36 -070012108 break;
12109
12110 case SHASTA_EXT_LED_SHARED:
12111 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12112 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12113 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12114 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12115 LED_CTRL_MODE_PHY_2);
12116 break;
12117
12118 case SHASTA_EXT_LED_MAC:
12119 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12120 break;
12121
12122 case SHASTA_EXT_LED_COMBO:
12123 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12124 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12125 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12126 LED_CTRL_MODE_PHY_2);
12127 break;
12128
Stephen Hemminger855e1112008-04-16 16:37:28 -070012129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012130
12131 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12132 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12133 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12134 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12135
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012136 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12137 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012138
Michael Chan9d26e212006-12-07 00:21:14 -080012139 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012140 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012141 if ((tp->pdev->subsystem_vendor ==
12142 PCI_VENDOR_ID_ARIMA) &&
12143 (tp->pdev->subsystem_device == 0x205a ||
12144 tp->pdev->subsystem_device == 0x2063))
12145 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12146 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012147 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012148 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012150
12151 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12152 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012153 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012154 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12155 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012156
12157 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12158 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012159 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012160
Gary Zambranoa85feb82007-05-05 11:52:19 -070012161 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES &&
12162 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12163 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012164
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012165 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012166 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012167 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12168
Linus Torvalds1da177e2005-04-16 15:20:36 -070012169 if (cfg2 & (1 << 17))
12170 tp->tg3_flags2 |= TG3_FLG2_CAPACITIVE_COUPLING;
12171
12172 /* serdes signal pre-emphasis in register 0x590 set by */
12173 /* bootcode if bit 18 is set */
12174 if (cfg2 & (1 << 18))
12175 tp->tg3_flags2 |= TG3_FLG2_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012176
Matt Carlson321d32a2008-11-21 17:22:19 -080012177 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12178 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012179 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
12180 tp->tg3_flags3 |= TG3_FLG3_PHY_ENABLE_APD;
12181
Matt Carlson8ed5d972007-05-07 00:25:49 -070012182 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
12183 u32 cfg3;
12184
12185 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12186 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12187 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12188 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012189
Matt Carlson14417062010-02-17 15:16:59 +000012190 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
12191 tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
Matt Carlsona9daf362008-05-25 23:49:44 -070012192 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12193 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12194 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12195 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012196 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012197done:
12198 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
12199 device_set_wakeup_enable(&tp->pdev->dev,
12200 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012201}
12202
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012203static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12204{
12205 int i;
12206 u32 val;
12207
12208 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12209 tw32(OTP_CTRL, cmd);
12210
12211 /* Wait for up to 1 ms for command to execute. */
12212 for (i = 0; i < 100; i++) {
12213 val = tr32(OTP_STATUS);
12214 if (val & OTP_STATUS_CMD_DONE)
12215 break;
12216 udelay(10);
12217 }
12218
12219 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12220}
12221
12222/* Read the gphy configuration from the OTP region of the chip. The gphy
12223 * configuration is a 32-bit value that straddles the alignment boundary.
12224 * We do two 32-bit reads and then shift and merge the results.
12225 */
12226static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12227{
12228 u32 bhalf_otp, thalf_otp;
12229
12230 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12231
12232 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12233 return 0;
12234
12235 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12236
12237 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12238 return 0;
12239
12240 thalf_otp = tr32(OTP_READ_DATA);
12241
12242 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12243
12244 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12245 return 0;
12246
12247 bhalf_otp = tr32(OTP_READ_DATA);
12248
12249 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12250}
12251
Michael Chan7d0c41e2005-04-21 17:06:20 -070012252static int __devinit tg3_phy_probe(struct tg3 *tp)
12253{
12254 u32 hw_phy_id_1, hw_phy_id_2;
12255 u32 hw_phy_id, hw_phy_id_masked;
12256 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012257
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012258 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12259 return tg3_phy_init(tp);
12260
Linus Torvalds1da177e2005-04-16 15:20:36 -070012261 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012262 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012263 */
12264 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012265 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12266 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012267 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012268 } else {
12269 /* Now read the physical PHY_ID from the chip and verify
12270 * that it is sane. If it doesn't look good, we fall back
12271 * to either the hard-coded table based PHY_ID and failing
12272 * that the value found in the eeprom area.
12273 */
12274 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12275 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12276
12277 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12278 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12279 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12280
Matt Carlson79eb6902010-02-17 15:17:03 +000012281 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012282 }
12283
Matt Carlson79eb6902010-02-17 15:17:03 +000012284 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012285 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012286 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012287 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012288 else
12289 tp->tg3_flags2 &= ~TG3_FLG2_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012290 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012291 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012292 /* Do nothing, phy ID already set up in
12293 * tg3_get_eeprom_hw_cfg().
12294 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012295 } else {
12296 struct subsys_tbl_ent *p;
12297
12298 /* No eeprom signature? Try the hardcoded
12299 * subsys device table.
12300 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012301 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012302 if (!p)
12303 return -ENODEV;
12304
12305 tp->phy_id = p->phy_id;
12306 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012307 tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012308 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
12309 }
12310 }
12311
Michael Chan747e8f82005-07-25 12:33:22 -070012312 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012313 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012314 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012315 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012316
12317 tg3_readphy(tp, MII_BMSR, &bmsr);
12318 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12319 (bmsr & BMSR_LSTATUS))
12320 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012321
Linus Torvalds1da177e2005-04-16 15:20:36 -070012322 err = tg3_phy_reset(tp);
12323 if (err)
12324 return err;
12325
12326 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12327 ADVERTISE_100HALF | ADVERTISE_100FULL |
12328 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12329 tg3_ctrl = 0;
12330 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
12331 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12332 MII_TG3_CTRL_ADV_1000_FULL);
12333 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12334 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12335 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12336 MII_TG3_CTRL_ENABLE_AS_MASTER);
12337 }
12338
Michael Chan3600d912006-12-07 00:21:48 -080012339 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12340 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12341 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12342 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012343 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12344
12345 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12346 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12347
12348 tg3_writephy(tp, MII_BMCR,
12349 BMCR_ANENABLE | BMCR_ANRESTART);
12350 }
12351 tg3_phy_set_wirespeed(tp);
12352
12353 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12354 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12355 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12356 }
12357
12358skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012359 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012360 err = tg3_init_5401phy_dsp(tp);
12361 if (err)
12362 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012363
Linus Torvalds1da177e2005-04-16 15:20:36 -070012364 err = tg3_init_5401phy_dsp(tp);
12365 }
12366
Michael Chan747e8f82005-07-25 12:33:22 -070012367 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012368 tp->link_config.advertising =
12369 (ADVERTISED_1000baseT_Half |
12370 ADVERTISED_1000baseT_Full |
12371 ADVERTISED_Autoneg |
12372 ADVERTISED_FIBRE);
12373 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
12374 tp->link_config.advertising &=
12375 ~(ADVERTISED_1000baseT_Half |
12376 ADVERTISED_1000baseT_Full);
12377
12378 return err;
12379}
12380
Matt Carlson184b8902010-04-05 10:19:25 +000012381static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012382{
Matt Carlson184b8902010-04-05 10:19:25 +000012383 u8 vpd_data[TG3_NVM_VPD_LEN];
Matt Carlson4181b2c2010-02-26 14:04:45 +000012384 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000012385 int j, i = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012386 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012387
Matt Carlsondf259d82009-04-20 06:57:14 +000012388 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12389 tg3_nvram_read(tp, 0x0, &magic))
David S. Millerf49639e2006-06-09 11:58:36 -070012390 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012391
Michael Chan18201802006-03-20 22:29:15 -080012392 if (magic == TG3_EEPROM_MAGIC) {
Matt Carlson141518c2009-12-03 08:36:22 +000012393 for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
Michael Chan1b277772006-03-20 22:27:48 -080012394 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012395
Matt Carlson6d348f22009-02-25 14:25:52 +000012396 /* The data is in little-endian format in NVRAM.
12397 * Use the big-endian read routines to preserve
12398 * the byte order as it exists in NVRAM.
12399 */
Matt Carlson141518c2009-12-03 08:36:22 +000012400 if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012401 goto out_not_found;
12402
Matt Carlson6d348f22009-02-25 14:25:52 +000012403 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012404 }
12405 } else {
Matt Carlson94c982b2009-12-03 08:36:23 +000012406 ssize_t cnt;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012407 unsigned int pos = 0;
Michael Chan1b277772006-03-20 22:27:48 -080012408
Matt Carlson94c982b2009-12-03 08:36:23 +000012409 for (; pos < TG3_NVM_VPD_LEN && i < 3; i++, pos += cnt) {
12410 cnt = pci_read_vpd(tp->pdev, pos,
12411 TG3_NVM_VPD_LEN - pos,
12412 &vpd_data[pos]);
12413 if (cnt == -ETIMEDOUT || -EINTR)
12414 cnt = 0;
12415 else if (cnt < 0)
David S. Millerf49639e2006-06-09 11:58:36 -070012416 goto out_not_found;
Michael Chan1b277772006-03-20 22:27:48 -080012417 }
Matt Carlson94c982b2009-12-03 08:36:23 +000012418 if (pos != TG3_NVM_VPD_LEN)
12419 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012420 }
12421
Matt Carlson4181b2c2010-02-26 14:04:45 +000012422 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
12423 PCI_VPD_LRDT_RO_DATA);
12424 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012425 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012426
12427 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
12428 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
12429 i += PCI_VPD_LRDT_TAG_SIZE;
12430
12431 if (block_end > TG3_NVM_VPD_LEN)
12432 goto out_not_found;
12433
Matt Carlson184b8902010-04-05 10:19:25 +000012434 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12435 PCI_VPD_RO_KEYWORD_MFR_ID);
12436 if (j > 0) {
12437 len = pci_vpd_info_field_size(&vpd_data[j]);
12438
12439 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12440 if (j + len > block_end || len != 4 ||
12441 memcmp(&vpd_data[j], "1028", 4))
12442 goto partno;
12443
12444 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12445 PCI_VPD_RO_KEYWORD_VENDOR0);
12446 if (j < 0)
12447 goto partno;
12448
12449 len = pci_vpd_info_field_size(&vpd_data[j]);
12450
12451 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12452 if (j + len > block_end)
12453 goto partno;
12454
12455 memcpy(tp->fw_ver, &vpd_data[j], len);
12456 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
12457 }
12458
12459partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000012460 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
12461 PCI_VPD_RO_KEYWORD_PARTNO);
12462 if (i < 0)
12463 goto out_not_found;
12464
12465 len = pci_vpd_info_field_size(&vpd_data[i]);
12466
12467 i += PCI_VPD_INFO_FLD_HDR_SIZE;
12468 if (len > TG3_BPN_SIZE ||
12469 (len + i) > TG3_NVM_VPD_LEN)
12470 goto out_not_found;
12471
12472 memcpy(tp->board_part_number, &vpd_data[i], len);
12473
12474 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012475
12476out_not_found:
Michael Chanb5d37722006-09-27 16:06:21 -070012477 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12478 strcpy(tp->board_part_number, "BCM95906");
Matt Carlsondf259d82009-04-20 06:57:14 +000012479 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12480 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12481 strcpy(tp->board_part_number, "BCM57780");
12482 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12483 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12484 strcpy(tp->board_part_number, "BCM57760");
12485 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12486 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12487 strcpy(tp->board_part_number, "BCM57790");
Matt Carlson5e7ccf22009-08-25 10:08:42 +000012488 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12489 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12490 strcpy(tp->board_part_number, "BCM57788");
Matt Carlsonb474eca2010-02-17 15:16:58 +000012491 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12492 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
12493 strcpy(tp->board_part_number, "BCM57761");
12494 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12495 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
Matt Carlsonb703df62009-12-03 08:36:21 +000012496 strcpy(tp->board_part_number, "BCM57765");
Matt Carlsonb474eca2010-02-17 15:16:58 +000012497 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12498 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
12499 strcpy(tp->board_part_number, "BCM57781");
12500 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12501 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
12502 strcpy(tp->board_part_number, "BCM57785");
12503 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12504 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
12505 strcpy(tp->board_part_number, "BCM57791");
12506 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12507 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12508 strcpy(tp->board_part_number, "BCM57795");
Michael Chanb5d37722006-09-27 16:06:21 -070012509 else
12510 strcpy(tp->board_part_number, "none");
Linus Torvalds1da177e2005-04-16 15:20:36 -070012511}
12512
Matt Carlson9c8a6202007-10-21 16:16:08 -070012513static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12514{
12515 u32 val;
12516
Matt Carlsone4f34112009-02-25 14:25:00 +000012517 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012518 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012519 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012520 val != 0)
12521 return 0;
12522
12523 return 1;
12524}
12525
Matt Carlsonacd9c112009-02-25 14:26:33 +000012526static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12527{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012528 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000012529 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012530 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012531
12532 if (tg3_nvram_read(tp, 0xc, &offset) ||
12533 tg3_nvram_read(tp, 0x4, &start))
12534 return;
12535
12536 offset = tg3_nvram_logical_addr(tp, offset);
12537
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012538 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012539 return;
12540
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012541 if ((val & 0xfc000000) == 0x0c000000) {
12542 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012543 return;
12544
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012545 if (val == 0)
12546 newver = true;
12547 }
12548
Matt Carlson75f99362010-04-05 10:19:24 +000012549 dst_off = strlen(tp->fw_ver);
12550
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012551 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000012552 if (TG3_VER_SIZE - dst_off < 16 ||
12553 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012554 return;
12555
12556 offset = offset + ver_offset - start;
12557 for (i = 0; i < 16; i += 4) {
12558 __be32 v;
12559 if (tg3_nvram_read_be32(tp, offset + i, &v))
12560 return;
12561
Matt Carlson75f99362010-04-05 10:19:24 +000012562 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012563 }
12564 } else {
12565 u32 major, minor;
12566
12567 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
12568 return;
12569
12570 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
12571 TG3_NVM_BCVER_MAJSFT;
12572 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000012573 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
12574 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012575 }
12576}
12577
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012578static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
12579{
12580 u32 val, major, minor;
12581
12582 /* Use native endian representation */
12583 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
12584 return;
12585
12586 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
12587 TG3_NVM_HWSB_CFG1_MAJSFT;
12588 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
12589 TG3_NVM_HWSB_CFG1_MINSFT;
12590
12591 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
12592}
12593
Matt Carlsondfe00d72008-11-21 17:19:41 -080012594static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
12595{
12596 u32 offset, major, minor, build;
12597
Matt Carlson75f99362010-04-05 10:19:24 +000012598 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012599
12600 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
12601 return;
12602
12603 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
12604 case TG3_EEPROM_SB_REVISION_0:
12605 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
12606 break;
12607 case TG3_EEPROM_SB_REVISION_2:
12608 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
12609 break;
12610 case TG3_EEPROM_SB_REVISION_3:
12611 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
12612 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000012613 case TG3_EEPROM_SB_REVISION_4:
12614 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
12615 break;
12616 case TG3_EEPROM_SB_REVISION_5:
12617 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
12618 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012619 default:
12620 return;
12621 }
12622
Matt Carlsone4f34112009-02-25 14:25:00 +000012623 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080012624 return;
12625
12626 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
12627 TG3_EEPROM_SB_EDH_BLD_SHFT;
12628 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
12629 TG3_EEPROM_SB_EDH_MAJ_SHFT;
12630 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
12631
12632 if (minor > 99 || build > 26)
12633 return;
12634
Matt Carlson75f99362010-04-05 10:19:24 +000012635 offset = strlen(tp->fw_ver);
12636 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
12637 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080012638
12639 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000012640 offset = strlen(tp->fw_ver);
12641 if (offset < TG3_VER_SIZE - 1)
12642 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080012643 }
12644}
12645
Matt Carlsonacd9c112009-02-25 14:26:33 +000012646static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080012647{
12648 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012649 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070012650
12651 for (offset = TG3_NVM_DIR_START;
12652 offset < TG3_NVM_DIR_END;
12653 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012654 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012655 return;
12656
12657 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
12658 break;
12659 }
12660
12661 if (offset == TG3_NVM_DIR_END)
12662 return;
12663
12664 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12665 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000012666 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012667 return;
12668
Matt Carlsone4f34112009-02-25 14:25:00 +000012669 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012670 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012671 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012672 return;
12673
12674 offset += val - start;
12675
Matt Carlsonacd9c112009-02-25 14:26:33 +000012676 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012677
Matt Carlsonacd9c112009-02-25 14:26:33 +000012678 tp->fw_ver[vlen++] = ',';
12679 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070012680
12681 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012682 __be32 v;
12683 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012684 return;
12685
Al Virob9fc7dc2007-12-17 22:59:57 -080012686 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012687
Matt Carlsonacd9c112009-02-25 14:26:33 +000012688 if (vlen > TG3_VER_SIZE - sizeof(v)) {
12689 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012690 break;
12691 }
12692
Matt Carlsonacd9c112009-02-25 14:26:33 +000012693 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
12694 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012695 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000012696}
12697
Matt Carlson7fd76442009-02-25 14:27:20 +000012698static void __devinit tg3_read_dash_ver(struct tg3 *tp)
12699{
12700 int vlen;
12701 u32 apedata;
12702
12703 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
12704 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
12705 return;
12706
12707 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
12708 if (apedata != APE_SEG_SIG_MAGIC)
12709 return;
12710
12711 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
12712 if (!(apedata & APE_FW_STATUS_READY))
12713 return;
12714
12715 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
12716
12717 vlen = strlen(tp->fw_ver);
12718
12719 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " DASH v%d.%d.%d.%d",
12720 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
12721 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
12722 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
12723 (apedata & APE_FW_VERSION_BLDMSK));
12724}
12725
Matt Carlsonacd9c112009-02-25 14:26:33 +000012726static void __devinit tg3_read_fw_ver(struct tg3 *tp)
12727{
12728 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000012729 bool vpd_vers = false;
12730
12731 if (tp->fw_ver[0] != 0)
12732 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012733
Matt Carlsondf259d82009-04-20 06:57:14 +000012734 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
Matt Carlson75f99362010-04-05 10:19:24 +000012735 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000012736 return;
12737 }
12738
Matt Carlsonacd9c112009-02-25 14:26:33 +000012739 if (tg3_nvram_read(tp, 0, &val))
12740 return;
12741
12742 if (val == TG3_EEPROM_MAGIC)
12743 tg3_read_bc_ver(tp);
12744 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
12745 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012746 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12747 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012748 else
12749 return;
12750
12751 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson75f99362010-04-05 10:19:24 +000012752 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) || vpd_vers)
12753 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012754
12755 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012756
Matt Carlson75f99362010-04-05 10:19:24 +000012757done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070012758 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080012759}
12760
Michael Chan7544b092007-05-05 13:08:32 -070012761static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
12762
Linus Torvalds1da177e2005-04-16 15:20:36 -070012763static int __devinit tg3_get_invariants(struct tg3 *tp)
12764{
12765 static struct pci_device_id write_reorder_chipsets[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012766 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012767 PCI_DEVICE_ID_AMD_FE_GATE_700C) },
John W. Linvillec165b002006-07-08 13:28:53 -070012768 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012769 PCI_DEVICE_ID_AMD_8131_BRIDGE) },
Michael Chan399de502005-10-03 14:02:39 -070012770 { PCI_DEVICE(PCI_VENDOR_ID_VIA,
12771 PCI_DEVICE_ID_VIA_8385_0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012772 { },
12773 };
12774 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012775 u32 pci_state_reg, grc_misc_cfg;
12776 u32 val;
12777 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012778 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012779
Linus Torvalds1da177e2005-04-16 15:20:36 -070012780 /* Force memory write invalidate off. If we leave it on,
12781 * then on 5700_BX chips we have to enable a workaround.
12782 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
12783 * to match the cacheline size. The Broadcom driver have this
12784 * workaround but turns MWI off all the times so never uses
12785 * it. This seems to suggest that the workaround is insufficient.
12786 */
12787 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12788 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
12789 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12790
12791 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
12792 * has the register indirect write enable bit set before
12793 * we try to access any of the MMIO registers. It is also
12794 * critical that the PCI-X hw workaround situation is decided
12795 * before that as well.
12796 */
12797 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12798 &misc_ctrl_reg);
12799
12800 tp->pci_chip_rev_id = (misc_ctrl_reg >>
12801 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070012802 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
12803 u32 prod_id_asic_rev;
12804
Matt Carlson5001e2f2009-11-13 13:03:51 +000012805 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
12806 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
12807 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5724)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012808 pci_read_config_dword(tp->pdev,
12809 TG3PCI_GEN2_PRODID_ASICREV,
12810 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000012811 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
12812 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
12813 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
12814 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
12815 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
12816 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
12817 pci_read_config_dword(tp->pdev,
12818 TG3PCI_GEN15_PRODID_ASICREV,
12819 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012820 else
12821 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
12822 &prod_id_asic_rev);
12823
Matt Carlson321d32a2008-11-21 17:22:19 -080012824 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070012825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012826
Michael Chanff645be2005-04-21 17:09:53 -070012827 /* Wrong chip ID in 5752 A0. This code can be removed later
12828 * as A0 is not in production.
12829 */
12830 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
12831 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
12832
Michael Chan68929142005-08-09 20:17:14 -070012833 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
12834 * we need to disable memory and use config. cycles
12835 * only to access all registers. The 5702/03 chips
12836 * can mistakenly decode the special cycles from the
12837 * ICH chipsets as memory write cycles, causing corruption
12838 * of register and memory space. Only certain ICH bridges
12839 * will drive special cycles with non-zero data during the
12840 * address phase which can fall within the 5703's address
12841 * range. This is not an ICH bug as the PCI spec allows
12842 * non-zero address during special cycles. However, only
12843 * these ICH bridges are known to drive non-zero addresses
12844 * during special cycles.
12845 *
12846 * Since special cycles do not cross PCI bridges, we only
12847 * enable this workaround if the 5703 is on the secondary
12848 * bus of these ICH bridges.
12849 */
12850 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
12851 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
12852 static struct tg3_dev_id {
12853 u32 vendor;
12854 u32 device;
12855 u32 rev;
12856 } ich_chipsets[] = {
12857 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
12858 PCI_ANY_ID },
12859 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
12860 PCI_ANY_ID },
12861 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
12862 0xa },
12863 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
12864 PCI_ANY_ID },
12865 { },
12866 };
12867 struct tg3_dev_id *pci_id = &ich_chipsets[0];
12868 struct pci_dev *bridge = NULL;
12869
12870 while (pci_id->vendor != 0) {
12871 bridge = pci_get_device(pci_id->vendor, pci_id->device,
12872 bridge);
12873 if (!bridge) {
12874 pci_id++;
12875 continue;
12876 }
12877 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070012878 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070012879 continue;
12880 }
12881 if (bridge->subordinate &&
12882 (bridge->subordinate->number ==
12883 tp->pdev->bus->number)) {
12884
12885 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
12886 pci_dev_put(bridge);
12887 break;
12888 }
12889 }
12890 }
12891
Matt Carlson41588ba2008-04-19 18:12:33 -070012892 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
12893 static struct tg3_dev_id {
12894 u32 vendor;
12895 u32 device;
12896 } bridge_chipsets[] = {
12897 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
12898 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
12899 { },
12900 };
12901 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
12902 struct pci_dev *bridge = NULL;
12903
12904 while (pci_id->vendor != 0) {
12905 bridge = pci_get_device(pci_id->vendor,
12906 pci_id->device,
12907 bridge);
12908 if (!bridge) {
12909 pci_id++;
12910 continue;
12911 }
12912 if (bridge->subordinate &&
12913 (bridge->subordinate->number <=
12914 tp->pdev->bus->number) &&
12915 (bridge->subordinate->subordinate >=
12916 tp->pdev->bus->number)) {
12917 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
12918 pci_dev_put(bridge);
12919 break;
12920 }
12921 }
12922 }
12923
Michael Chan4a29cc22006-03-19 13:21:12 -080012924 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
12925 * DMA addresses > 40-bit. This bridge may have other additional
12926 * 57xx devices behind it in some 4-port NIC designs for example.
12927 * Any tg3 device found behind the bridge will also need the 40-bit
12928 * DMA workaround.
12929 */
Michael Chana4e2b342005-10-26 15:46:52 -070012930 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
12931 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
12932 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080012933 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070012934 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000012935 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080012936 struct pci_dev *bridge = NULL;
12937
12938 do {
12939 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
12940 PCI_DEVICE_ID_SERVERWORKS_EPB,
12941 bridge);
12942 if (bridge && bridge->subordinate &&
12943 (bridge->subordinate->number <=
12944 tp->pdev->bus->number) &&
12945 (bridge->subordinate->subordinate >=
12946 tp->pdev->bus->number)) {
12947 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
12948 pci_dev_put(bridge);
12949 break;
12950 }
12951 } while (bridge);
12952 }
Michael Chan4cf78e42005-07-25 12:29:19 -070012953
Linus Torvalds1da177e2005-04-16 15:20:36 -070012954 /* Initialize misc host control in PCI block. */
12955 tp->misc_host_ctrl |= (misc_ctrl_reg &
12956 MISC_HOST_CTRL_CHIPREV);
12957 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12958 tp->misc_host_ctrl);
12959
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012960 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
12961 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
12962 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan7544b092007-05-05 13:08:32 -070012963 tp->pdev_peer = tg3_find_peer(tp);
12964
Matt Carlson321d32a2008-11-21 17:22:19 -080012965 /* Intentionally exclude ASIC_REV_5906 */
12966 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080012967 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070012968 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070012969 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012970 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012971 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonb703df62009-12-03 08:36:21 +000012972 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12973 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012974 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
12975
12976 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
12977 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070012978 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012979 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070012980 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070012981 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
12982
John W. Linville1b440c562005-04-21 17:03:18 -070012983 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
12984 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
12985 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
12986
Matt Carlson027455a2008-12-21 20:19:30 -080012987 /* 5700 B0 chips do not support checksumming correctly due
12988 * to hardware bugs.
12989 */
12990 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
12991 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
12992 else {
12993 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
12994 tp->dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
12995 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
12996 tp->dev->features |= NETIF_F_IPV6_CSUM;
David S. Millercb903bf2010-04-20 18:49:45 -070012997 tp->dev->features |= NETIF_F_GRO;
Matt Carlson027455a2008-12-21 20:19:30 -080012998 }
12999
Matt Carlson507399f2009-11-13 13:03:37 +000013000 /* Determine TSO capabilities */
Matt Carlsonb703df62009-12-03 08:36:21 +000013001 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13002 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsone849cdc2009-11-13 13:03:38 +000013003 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13004 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13005 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000013006 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13007 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13008 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13009 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13010 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13011 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13012 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13013 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13014 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13015 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13016 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13017 tp->fw_needed = FIRMWARE_TG3TSO5;
13018 else
13019 tp->fw_needed = FIRMWARE_TG3TSO;
13020 }
13021
13022 tp->irq_max = 1;
13023
Michael Chan5a6f3072006-03-20 22:28:05 -080013024 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070013025 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13026 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13027 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13028 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13029 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13030 tp->pdev_peer == tp->pdev))
13031 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13032
Matt Carlson321d32a2008-11-21 17:22:19 -080013033 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013034 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080013035 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070013036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013037
Matt Carlsonb703df62009-12-03 08:36:21 +000013038 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13039 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlson507399f2009-11-13 13:03:37 +000013040 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13041 tp->irq_max = TG3_IRQ_MAX_VECS;
13042 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013043 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013044
Matt Carlson615774f2009-11-13 13:03:39 +000013045 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13046 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13047 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13048 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13049 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13050 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000013051 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013052
Matt Carlsonb703df62009-12-03 08:36:21 +000013053 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13054 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
13055 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13056
Matt Carlsonf51f3562008-05-25 23:45:08 -070013057 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013058 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
13059 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
Matt Carlson8f666b02009-08-28 13:58:24 +000013060 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070013061
Matt Carlson52f44902008-11-21 17:17:04 -080013062 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13063 &pci_state_reg);
13064
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013065 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13066 if (tp->pcie_cap != 0) {
13067 u16 lnkctl;
13068
Linus Torvalds1da177e2005-04-16 15:20:36 -070013069 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013070
13071 pcie_set_readrq(tp->pdev, 4096);
13072
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013073 pci_read_config_word(tp->pdev,
13074 tp->pcie_cap + PCI_EXP_LNKCTL,
13075 &lnkctl);
13076 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13077 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080013078 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013079 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013080 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013081 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13082 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013083 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Matt Carlson614b0592010-01-20 16:58:02 +000013084 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13085 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
Michael Chanc7835a72006-11-15 21:14:42 -080013086 }
Matt Carlson52f44902008-11-21 17:17:04 -080013087 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080013088 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080013089 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13090 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13091 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13092 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013093 dev_err(&tp->pdev->dev,
13094 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013095 return -EIO;
13096 }
13097
13098 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13099 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013101
Michael Chan399de502005-10-03 14:02:39 -070013102 /* If we have an AMD 762 or VIA K8T800 chipset, write
13103 * reordering to the mailbox registers done by the host
13104 * controller can cause major troubles. We read back from
13105 * every mailbox register write to force the writes to be
13106 * posted to the chip in order.
13107 */
13108 if (pci_dev_present(write_reorder_chipsets) &&
13109 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13110 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13111
Matt Carlson69fc4052008-12-21 20:19:57 -080013112 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13113 &tp->pci_cacheline_sz);
13114 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13115 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013116 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13117 tp->pci_lat_timer < 64) {
13118 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013119 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13120 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013121 }
13122
Matt Carlson52f44902008-11-21 17:17:04 -080013123 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13124 /* 5700 BX chips need to have their TX producer index
13125 * mailboxes written twice to workaround a bug.
13126 */
13127 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013128
Matt Carlson52f44902008-11-21 17:17:04 -080013129 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013130 *
13131 * The workaround is to use indirect register accesses
13132 * for all chip writes not to mailbox registers.
13133 */
Matt Carlson52f44902008-11-21 17:17:04 -080013134 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013135 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013136
13137 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13138
13139 /* The chip can have it's power management PCI config
13140 * space registers clobbered due to this bug.
13141 * So explicitly force the chip into D0 here.
13142 */
Matt Carlson9974a352007-10-07 23:27:28 -070013143 pci_read_config_dword(tp->pdev,
13144 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013145 &pm_reg);
13146 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13147 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013148 pci_write_config_dword(tp->pdev,
13149 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013150 pm_reg);
13151
13152 /* Also, force SERR#/PERR# in PCI command. */
13153 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13154 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13155 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13156 }
13157 }
13158
Linus Torvalds1da177e2005-04-16 15:20:36 -070013159 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13160 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13161 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13162 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13163
13164 /* Chip-specific fixup from Broadcom driver */
13165 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13166 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13167 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13168 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13169 }
13170
Michael Chan1ee582d2005-08-09 20:16:46 -070013171 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013172 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013173 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013174 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013175 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013176 tp->write32_tx_mbox = tg3_write32;
13177 tp->write32_rx_mbox = tg3_write32;
13178
13179 /* Various workaround register access methods */
13180 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13181 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013182 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13183 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13184 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13185 /*
13186 * Back to back register writes can cause problems on these
13187 * chips, the workaround is to read back all reg writes
13188 * except those to mailbox regs.
13189 *
13190 * See tg3_write_indirect_reg32().
13191 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013192 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013193 }
13194
Michael Chan1ee582d2005-08-09 20:16:46 -070013195 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13196 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13197 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13198 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13199 tp->write32_rx_mbox = tg3_write_flush_reg32;
13200 }
Michael Chan20094932005-08-09 20:16:32 -070013201
Michael Chan68929142005-08-09 20:17:14 -070013202 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13203 tp->read32 = tg3_read_indirect_reg32;
13204 tp->write32 = tg3_write_indirect_reg32;
13205 tp->read32_mbox = tg3_read_indirect_mbox;
13206 tp->write32_mbox = tg3_write_indirect_mbox;
13207 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13208 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13209
13210 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013211 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013212
13213 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13214 pci_cmd &= ~PCI_COMMAND_MEMORY;
13215 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13216 }
Michael Chanb5d37722006-09-27 16:06:21 -070013217 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13218 tp->read32_mbox = tg3_read32_mbox_5906;
13219 tp->write32_mbox = tg3_write32_mbox_5906;
13220 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13221 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13222 }
Michael Chan68929142005-08-09 20:17:14 -070013223
Michael Chanbbadf502006-04-06 21:46:34 -070013224 if (tp->write32 == tg3_write_indirect_reg32 ||
13225 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13226 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013227 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013228 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13229
Michael Chan7d0c41e2005-04-21 17:06:20 -070013230 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013231 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013232 * determined before calling tg3_set_power_state() so that
13233 * we know whether or not to switch out of Vaux power.
13234 * When the flag is set, it means that GPIO1 is used for eeprom
13235 * write protect and also implies that it is a LOM where GPIOs
13236 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013237 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013238 tg3_get_eeprom_hw_cfg(tp);
13239
Matt Carlson0d3031d2007-10-10 18:02:43 -070013240 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13241 /* Allow reads and writes to the
13242 * APE register and memory space.
13243 */
13244 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
13245 PCISTATE_ALLOW_APE_SHMEM_WR;
13246 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13247 pci_state_reg);
13248 }
13249
Matt Carlson9936bcf2007-10-10 18:03:07 -070013250 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013251 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013252 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013253 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlsonb703df62009-12-03 08:36:21 +000013254 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13255 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsond30cdd22007-10-07 23:28:35 -070013256 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13257
Michael Chan314fba32005-04-21 17:07:04 -070013258 /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
13259 * GPIO1 driven high will bring 5700's external PHY out of reset.
13260 * It is also used as eeprom write protect on LOMs.
13261 */
13262 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13263 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13264 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13265 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13266 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013267 /* Unused GPIO3 must be driven as output on 5752 because there
13268 * are no pull-up resistors on unused GPIO pins.
13269 */
13270 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13271 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013272
Matt Carlson321d32a2008-11-21 17:22:19 -080013273 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013274 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13275 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013276 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13277
Matt Carlson8d519ab2009-04-20 06:58:01 +000013278 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13279 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013280 /* Turn off the debug UART. */
13281 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13282 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13283 /* Keep VMain power. */
13284 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13285 GRC_LCLCTRL_GPIO_OUTPUT0;
13286 }
13287
Linus Torvalds1da177e2005-04-16 15:20:36 -070013288 /* Force the chip into D0. */
Michael Chanbc1c7562006-03-20 17:48:03 -080013289 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013290 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013291 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013292 return err;
13293 }
13294
Linus Torvalds1da177e2005-04-16 15:20:36 -070013295 /* Derive initial jumbo mode from MTU assigned in
13296 * ether_setup() via the alloc_etherdev() call
13297 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013298 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013299 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013300 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013301
13302 /* Determine WakeOnLan speed to use. */
13303 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13304 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13305 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13306 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13307 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13308 } else {
13309 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13310 }
13311
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013312 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13313 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
13314
Linus Torvalds1da177e2005-04-16 15:20:36 -070013315 /* A few boards don't want Ethernet@WireSpeed phy feature */
13316 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13317 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13318 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013319 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013320 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) ||
Michael Chan747e8f82005-07-25 12:33:22 -070013321 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013322 tp->tg3_flags2 |= TG3_FLG2_NO_ETH_WIRE_SPEED;
13323
13324 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13325 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
13326 tp->tg3_flags2 |= TG3_FLG2_PHY_ADC_BUG;
13327 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
13328 tp->tg3_flags2 |= TG3_FLG2_PHY_5704_A0_BUG;
13329
Matt Carlson321d32a2008-11-21 17:22:19 -080013330 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013331 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013332 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013333 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Matt Carlsonb703df62009-12-03 08:36:21 +000013334 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
13335 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765) {
Michael Chanc424cb22006-04-29 18:56:34 -070013336 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013337 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013338 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13339 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013340 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13341 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
13342 tp->tg3_flags2 |= TG3_FLG2_PHY_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013343 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
13344 tp->tg3_flags2 |= TG3_FLG2_PHY_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013345 } else
Michael Chanc424cb22006-04-29 18:56:34 -070013346 tp->tg3_flags2 |= TG3_FLG2_PHY_BER_BUG;
13347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013348
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013349 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13350 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13351 tp->phy_otp = tg3_read_otp_phycfg(tp);
13352 if (tp->phy_otp == 0)
13353 tp->phy_otp = TG3_OTP_DEFAULT;
13354 }
13355
Matt Carlsonf51f3562008-05-25 23:45:08 -070013356 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070013357 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13358 else
13359 tp->mi_mode = MAC_MI_MODE_BASE;
13360
Linus Torvalds1da177e2005-04-16 15:20:36 -070013361 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013362 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13363 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13364 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13365
Matt Carlson321d32a2008-11-21 17:22:19 -080013366 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13367 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070013368 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
13369
Matt Carlson158d7ab2008-05-29 01:37:54 -070013370 err = tg3_mdio_init(tp);
13371 if (err)
13372 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013373
Matt Carlson55dffe72010-01-12 10:11:39 +000013374 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
13375 (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0 ||
13376 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
13377 return -ENOTSUPP;
13378
Linus Torvalds1da177e2005-04-16 15:20:36 -070013379 /* Initialize data/descriptor byte/word swapping. */
13380 val = tr32(GRC_MODE);
13381 val &= GRC_MODE_HOST_STACKUP;
13382 tw32(GRC_MODE, val | tp->grc_mode);
13383
13384 tg3_switch_clocks(tp);
13385
13386 /* Clear this out for sanity. */
13387 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
13388
13389 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13390 &pci_state_reg);
13391 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
13392 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
13393 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
13394
13395 if (chiprevid == CHIPREV_ID_5701_A0 ||
13396 chiprevid == CHIPREV_ID_5701_B0 ||
13397 chiprevid == CHIPREV_ID_5701_B2 ||
13398 chiprevid == CHIPREV_ID_5701_B5) {
13399 void __iomem *sram_base;
13400
13401 /* Write some dummy words into the SRAM status block
13402 * area, see if it reads back correctly. If the return
13403 * value is bad, force enable the PCIX workaround.
13404 */
13405 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13406
13407 writel(0x00000000, sram_base);
13408 writel(0x00000000, sram_base + 4);
13409 writel(0xffffffff, sram_base + 4);
13410 if (readl(sram_base) != 0x00000000)
13411 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13412 }
13413 }
13414
13415 udelay(50);
13416 tg3_nvram_init(tp);
13417
13418 grc_misc_cfg = tr32(GRC_MISC_CFG);
13419 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13420
Linus Torvalds1da177e2005-04-16 15:20:36 -070013421 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13422 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13423 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13424 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13425
David S. Millerfac9b832005-05-18 22:46:34 -070013426 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13427 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13428 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13429 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13430 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13431 HOSTCC_MODE_CLRTICK_TXBD);
13432
13433 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13434 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13435 tp->misc_host_ctrl);
13436 }
13437
Matt Carlson3bda1252008-08-15 14:08:22 -070013438 /* Preserve the APE MAC_MODE bits */
13439 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
13440 tp->mac_mode = tr32(MAC_MODE) |
13441 MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
13442 else
13443 tp->mac_mode = TG3_DEF_MAC_MODE;
13444
Linus Torvalds1da177e2005-04-16 15:20:36 -070013445 /* these are limited to 10/100 only */
13446 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13447 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13448 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13449 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13450 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13451 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13452 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13453 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13454 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013455 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13456 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013457 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000013458 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13459 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013460 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013461 tp->tg3_flags |= TG3_FLAG_10_100_ONLY;
13462
13463 err = tg3_phy_probe(tp);
13464 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013465 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013466 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013467 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013468 }
13469
Matt Carlson184b8902010-04-05 10:19:25 +000013470 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013471 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013472
13473 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
13474 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13475 } else {
13476 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13477 tp->tg3_flags |= TG3_FLAG_USE_MI_INTERRUPT;
13478 else
13479 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13480 }
13481
13482 /* 5700 {AX,BX} chips have a broken status block link
13483 * change bit implementation, so we must use the
13484 * status register in those cases.
13485 */
13486 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13487 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13488 else
13489 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13490
13491 /* The led_ctrl is set during tg3_phy_probe, here we might
13492 * have to force the link status polling mechanism based
13493 * upon subsystem IDs.
13494 */
13495 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070013496 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013497 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
13498 tp->tg3_flags |= (TG3_FLAG_USE_MI_INTERRUPT |
13499 TG3_FLAG_USE_LINKCHG_REG);
13500 }
13501
13502 /* For all SERDES we poll the MAC status register. */
13503 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
13504 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
13505 else
13506 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
13507
Matt Carlson9dc7a112010-04-12 06:58:28 +000013508 tp->rx_offset = NET_IP_ALIGN + TG3_RX_HEADROOM;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013509 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013510 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsond2757fc2010-04-12 06:58:27 +000013511 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
Matt Carlson9dc7a112010-04-12 06:58:28 +000013512 tp->rx_offset -= NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013513#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000013514 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000013515#endif
13516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013517
Michael Chanf92905d2006-06-29 20:14:29 -070013518 tp->rx_std_max_post = TG3_RX_RING_SIZE;
13519
13520 /* Increment the rx prod index on the rx std ring by at most
13521 * 8 for these chips to workaround hw errata.
13522 */
13523 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13524 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13525 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13526 tp->rx_std_max_post = 8;
13527
Matt Carlson8ed5d972007-05-07 00:25:49 -070013528 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
13529 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
13530 PCIE_PWR_MGMT_L1_THRESH_MSK;
13531
Linus Torvalds1da177e2005-04-16 15:20:36 -070013532 return err;
13533}
13534
David S. Miller49b6e95f2007-03-29 01:38:42 -070013535#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013536static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
13537{
13538 struct net_device *dev = tp->dev;
13539 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013540 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070013541 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013542 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013543
David S. Miller49b6e95f2007-03-29 01:38:42 -070013544 addr = of_get_property(dp, "local-mac-address", &len);
13545 if (addr && len == 6) {
13546 memcpy(dev->dev_addr, addr, 6);
13547 memcpy(dev->perm_addr, dev->dev_addr, 6);
13548 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013549 }
13550 return -ENODEV;
13551}
13552
13553static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
13554{
13555 struct net_device *dev = tp->dev;
13556
13557 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070013558 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013559 return 0;
13560}
13561#endif
13562
13563static int __devinit tg3_get_device_address(struct tg3 *tp)
13564{
13565 struct net_device *dev = tp->dev;
13566 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080013567 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013568
David S. Miller49b6e95f2007-03-29 01:38:42 -070013569#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013570 if (!tg3_get_macaddr_sparc(tp))
13571 return 0;
13572#endif
13573
13574 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070013575 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013576 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013577 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
13578 mac_offset = 0xcc;
13579 if (tg3_nvram_lock(tp))
13580 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
13581 else
13582 tg3_nvram_unlock(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013583 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13584 if (tr32(TG3_CPMU_STATUS) & TG3_CPMU_STATUS_PCIE_FUNC)
13585 mac_offset = 0xcc;
13586 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070013587 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013588
13589 /* First try to get it from MAC address mailbox. */
13590 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
13591 if ((hi >> 16) == 0x484b) {
13592 dev->dev_addr[0] = (hi >> 8) & 0xff;
13593 dev->dev_addr[1] = (hi >> 0) & 0xff;
13594
13595 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
13596 dev->dev_addr[2] = (lo >> 24) & 0xff;
13597 dev->dev_addr[3] = (lo >> 16) & 0xff;
13598 dev->dev_addr[4] = (lo >> 8) & 0xff;
13599 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013600
Michael Chan008652b2006-03-27 23:14:53 -080013601 /* Some old bootcode may report a 0 MAC address in SRAM */
13602 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
13603 }
13604 if (!addr_ok) {
13605 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000013606 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
13607 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000013608 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070013609 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
13610 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080013611 }
13612 /* Finally just fetch it out of the MAC control regs. */
13613 else {
13614 hi = tr32(MAC_ADDR_0_HIGH);
13615 lo = tr32(MAC_ADDR_0_LOW);
13616
13617 dev->dev_addr[5] = lo & 0xff;
13618 dev->dev_addr[4] = (lo >> 8) & 0xff;
13619 dev->dev_addr[3] = (lo >> 16) & 0xff;
13620 dev->dev_addr[2] = (lo >> 24) & 0xff;
13621 dev->dev_addr[1] = hi & 0xff;
13622 dev->dev_addr[0] = (hi >> 8) & 0xff;
13623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013624 }
13625
13626 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070013627#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013628 if (!tg3_get_default_macaddr_sparc(tp))
13629 return 0;
13630#endif
13631 return -EINVAL;
13632 }
John W. Linville2ff43692005-09-12 14:44:20 -070013633 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013634 return 0;
13635}
13636
David S. Miller59e6b432005-05-18 22:50:10 -070013637#define BOUNDARY_SINGLE_CACHELINE 1
13638#define BOUNDARY_MULTI_CACHELINE 2
13639
13640static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
13641{
13642 int cacheline_size;
13643 u8 byte;
13644 int goal;
13645
13646 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
13647 if (byte == 0)
13648 cacheline_size = 1024;
13649 else
13650 cacheline_size = (int) byte * 4;
13651
13652 /* On 5703 and later chips, the boundary bits have no
13653 * effect.
13654 */
13655 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13656 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13657 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13658 goto out;
13659
13660#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
13661 goal = BOUNDARY_MULTI_CACHELINE;
13662#else
13663#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
13664 goal = BOUNDARY_SINGLE_CACHELINE;
13665#else
13666 goal = 0;
13667#endif
13668#endif
13669
Matt Carlsonb703df62009-12-03 08:36:21 +000013670 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13671 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013672 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
13673 goto out;
13674 }
13675
David S. Miller59e6b432005-05-18 22:50:10 -070013676 if (!goal)
13677 goto out;
13678
13679 /* PCI controllers on most RISC systems tend to disconnect
13680 * when a device tries to burst across a cache-line boundary.
13681 * Therefore, letting tg3 do so just wastes PCI bandwidth.
13682 *
13683 * Unfortunately, for PCI-E there are only limited
13684 * write-side controls for this, and thus for reads
13685 * we will still get the disconnects. We'll also waste
13686 * these PCI cycles for both read and write for chips
13687 * other than 5700 and 5701 which do not implement the
13688 * boundary bits.
13689 */
13690 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13691 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
13692 switch (cacheline_size) {
13693 case 16:
13694 case 32:
13695 case 64:
13696 case 128:
13697 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13698 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
13699 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
13700 } else {
13701 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13702 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13703 }
13704 break;
13705
13706 case 256:
13707 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
13708 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
13709 break;
13710
13711 default:
13712 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13713 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13714 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013715 }
David S. Miller59e6b432005-05-18 22:50:10 -070013716 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13717 switch (cacheline_size) {
13718 case 16:
13719 case 32:
13720 case 64:
13721 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13722 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13723 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
13724 break;
13725 }
13726 /* fallthrough */
13727 case 128:
13728 default:
13729 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13730 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
13731 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013732 }
David S. Miller59e6b432005-05-18 22:50:10 -070013733 } else {
13734 switch (cacheline_size) {
13735 case 16:
13736 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13737 val |= (DMA_RWCTRL_READ_BNDRY_16 |
13738 DMA_RWCTRL_WRITE_BNDRY_16);
13739 break;
13740 }
13741 /* fallthrough */
13742 case 32:
13743 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13744 val |= (DMA_RWCTRL_READ_BNDRY_32 |
13745 DMA_RWCTRL_WRITE_BNDRY_32);
13746 break;
13747 }
13748 /* fallthrough */
13749 case 64:
13750 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13751 val |= (DMA_RWCTRL_READ_BNDRY_64 |
13752 DMA_RWCTRL_WRITE_BNDRY_64);
13753 break;
13754 }
13755 /* fallthrough */
13756 case 128:
13757 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13758 val |= (DMA_RWCTRL_READ_BNDRY_128 |
13759 DMA_RWCTRL_WRITE_BNDRY_128);
13760 break;
13761 }
13762 /* fallthrough */
13763 case 256:
13764 val |= (DMA_RWCTRL_READ_BNDRY_256 |
13765 DMA_RWCTRL_WRITE_BNDRY_256);
13766 break;
13767 case 512:
13768 val |= (DMA_RWCTRL_READ_BNDRY_512 |
13769 DMA_RWCTRL_WRITE_BNDRY_512);
13770 break;
13771 case 1024:
13772 default:
13773 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
13774 DMA_RWCTRL_WRITE_BNDRY_1024);
13775 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013776 }
David S. Miller59e6b432005-05-18 22:50:10 -070013777 }
13778
13779out:
13780 return val;
13781}
13782
Linus Torvalds1da177e2005-04-16 15:20:36 -070013783static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
13784{
13785 struct tg3_internal_buffer_desc test_desc;
13786 u32 sram_dma_descs;
13787 int i, ret;
13788
13789 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
13790
13791 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
13792 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
13793 tw32(RDMAC_STATUS, 0);
13794 tw32(WDMAC_STATUS, 0);
13795
13796 tw32(BUFMGR_MODE, 0);
13797 tw32(FTQ_RESET, 0);
13798
13799 test_desc.addr_hi = ((u64) buf_dma) >> 32;
13800 test_desc.addr_lo = buf_dma & 0xffffffff;
13801 test_desc.nic_mbuf = 0x00002100;
13802 test_desc.len = size;
13803
13804 /*
13805 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
13806 * the *second* time the tg3 driver was getting loaded after an
13807 * initial scan.
13808 *
13809 * Broadcom tells me:
13810 * ...the DMA engine is connected to the GRC block and a DMA
13811 * reset may affect the GRC block in some unpredictable way...
13812 * The behavior of resets to individual blocks has not been tested.
13813 *
13814 * Broadcom noted the GRC reset will also reset all sub-components.
13815 */
13816 if (to_device) {
13817 test_desc.cqid_sqid = (13 << 8) | 2;
13818
13819 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
13820 udelay(40);
13821 } else {
13822 test_desc.cqid_sqid = (16 << 8) | 7;
13823
13824 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
13825 udelay(40);
13826 }
13827 test_desc.flags = 0x00000005;
13828
13829 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
13830 u32 val;
13831
13832 val = *(((u32 *)&test_desc) + i);
13833 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
13834 sram_dma_descs + (i * sizeof(u32)));
13835 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
13836 }
13837 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
13838
Matt Carlson859a588792010-04-05 10:19:28 +000013839 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013840 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000013841 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013842 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013843
13844 ret = -ENODEV;
13845 for (i = 0; i < 40; i++) {
13846 u32 val;
13847
13848 if (to_device)
13849 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
13850 else
13851 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
13852 if ((val & 0xffff) == sram_dma_descs) {
13853 ret = 0;
13854 break;
13855 }
13856
13857 udelay(100);
13858 }
13859
13860 return ret;
13861}
13862
David S. Millerded73402005-05-23 13:59:47 -070013863#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070013864
13865static int __devinit tg3_test_dma(struct tg3 *tp)
13866{
13867 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070013868 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013869 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013870
13871 buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
13872 if (!buf) {
13873 ret = -ENOMEM;
13874 goto out_nofree;
13875 }
13876
13877 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
13878 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
13879
David S. Miller59e6b432005-05-18 22:50:10 -070013880 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013881
Matt Carlsonb703df62009-12-03 08:36:21 +000013882 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13883 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000013884 goto out;
13885
Linus Torvalds1da177e2005-04-16 15:20:36 -070013886 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13887 /* DMA read watermark not used on PCIE */
13888 tp->dma_rwctrl |= 0x00180000;
13889 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070013890 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
13891 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013892 tp->dma_rwctrl |= 0x003f0000;
13893 else
13894 tp->dma_rwctrl |= 0x003f000f;
13895 } else {
13896 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13897 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
13898 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080013899 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013900
Michael Chan4a29cc22006-03-19 13:21:12 -080013901 /* If the 5704 is behind the EPB bridge, we can
13902 * do the less restrictive ONE_DMA workaround for
13903 * better performance.
13904 */
13905 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
13906 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13907 tp->dma_rwctrl |= 0x8000;
13908 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013909 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
13910
Michael Chan49afdeb2007-02-13 12:17:03 -080013911 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
13912 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070013913 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080013914 tp->dma_rwctrl |=
13915 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
13916 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
13917 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070013918 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
13919 /* 5780 always in PCIX mode */
13920 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070013921 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13922 /* 5714 always in PCIX mode */
13923 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013924 } else {
13925 tp->dma_rwctrl |= 0x001b000f;
13926 }
13927 }
13928
13929 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13930 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13931 tp->dma_rwctrl &= 0xfffffff0;
13932
13933 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13934 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
13935 /* Remove this if it causes problems for some boards. */
13936 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
13937
13938 /* On 5700/5701 chips, we need to set this bit.
13939 * Otherwise the chip will issue cacheline transactions
13940 * to streamable DMA memory with not all the byte
13941 * enables turned on. This is an error on several
13942 * RISC PCI controllers, in particular sparc64.
13943 *
13944 * On 5703/5704 chips, this bit has been reassigned
13945 * a different meaning. In particular, it is used
13946 * on those chips to enable a PCI-X workaround.
13947 */
13948 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
13949 }
13950
13951 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13952
13953#if 0
13954 /* Unneeded, already done by tg3_get_invariants. */
13955 tg3_switch_clocks(tp);
13956#endif
13957
Linus Torvalds1da177e2005-04-16 15:20:36 -070013958 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13959 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
13960 goto out;
13961
David S. Miller59e6b432005-05-18 22:50:10 -070013962 /* It is best to perform DMA test with maximum write burst size
13963 * to expose the 5700/5701 write DMA bug.
13964 */
13965 saved_dma_rwctrl = tp->dma_rwctrl;
13966 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
13967 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13968
Linus Torvalds1da177e2005-04-16 15:20:36 -070013969 while (1) {
13970 u32 *p = buf, i;
13971
13972 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
13973 p[i] = i;
13974
13975 /* Send the buffer to the chip. */
13976 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
13977 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000013978 dev_err(&tp->pdev->dev,
13979 "%s: Buffer write failed. err = %d\n",
13980 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013981 break;
13982 }
13983
13984#if 0
13985 /* validate data reached card RAM correctly. */
13986 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
13987 u32 val;
13988 tg3_read_mem(tp, 0x2100 + (i*4), &val);
13989 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000013990 dev_err(&tp->pdev->dev,
13991 "%s: Buffer corrupted on device! "
13992 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013993 /* ret = -ENODEV here? */
13994 }
13995 p[i] = 0;
13996 }
13997#endif
13998 /* Now read it back. */
13999 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14000 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014001 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14002 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014003 break;
14004 }
14005
14006 /* Verify it. */
14007 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14008 if (p[i] == i)
14009 continue;
14010
David S. Miller59e6b432005-05-18 22:50:10 -070014011 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14012 DMA_RWCTRL_WRITE_BNDRY_16) {
14013 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014014 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14015 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14016 break;
14017 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014018 dev_err(&tp->pdev->dev,
14019 "%s: Buffer corrupted on read back! "
14020 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014021 ret = -ENODEV;
14022 goto out;
14023 }
14024 }
14025
14026 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14027 /* Success. */
14028 ret = 0;
14029 break;
14030 }
14031 }
David S. Miller59e6b432005-05-18 22:50:10 -070014032 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14033 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014034 static struct pci_device_id dma_wait_state_chipsets[] = {
14035 { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
14036 PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14037 { },
14038 };
14039
David S. Miller59e6b432005-05-18 22:50:10 -070014040 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014041 * now look for chipsets that are known to expose the
14042 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014043 */
Michael Chan6d1cfba2005-06-08 14:13:14 -070014044 if (pci_dev_present(dma_wait_state_chipsets)) {
14045 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14046 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000014047 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014048 /* Safe to use the calculated DMA boundary. */
14049 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000014050 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014051
David S. Miller59e6b432005-05-18 22:50:10 -070014052 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014054
14055out:
14056 pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
14057out_nofree:
14058 return ret;
14059}
14060
14061static void __devinit tg3_init_link_config(struct tg3 *tp)
14062{
14063 tp->link_config.advertising =
14064 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
14065 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
14066 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
14067 ADVERTISED_Autoneg | ADVERTISED_MII);
14068 tp->link_config.speed = SPEED_INVALID;
14069 tp->link_config.duplex = DUPLEX_INVALID;
14070 tp->link_config.autoneg = AUTONEG_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014071 tp->link_config.active_speed = SPEED_INVALID;
14072 tp->link_config.active_duplex = DUPLEX_INVALID;
14073 tp->link_config.phy_is_low_power = 0;
14074 tp->link_config.orig_speed = SPEED_INVALID;
14075 tp->link_config.orig_duplex = DUPLEX_INVALID;
14076 tp->link_config.orig_autoneg = AUTONEG_INVALID;
14077}
14078
14079static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14080{
Matt Carlson666bc832010-01-20 16:58:03 +000014081 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14082 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
14083 tp->bufmgr_config.mbuf_read_dma_low_water =
14084 DEFAULT_MB_RDMA_LOW_WATER_5705;
14085 tp->bufmgr_config.mbuf_mac_rx_low_water =
14086 DEFAULT_MB_MACRX_LOW_WATER_57765;
14087 tp->bufmgr_config.mbuf_high_water =
14088 DEFAULT_MB_HIGH_WATER_57765;
14089
14090 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14091 DEFAULT_MB_RDMA_LOW_WATER_5705;
14092 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14093 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14094 tp->bufmgr_config.mbuf_high_water_jumbo =
14095 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14096 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014097 tp->bufmgr_config.mbuf_read_dma_low_water =
14098 DEFAULT_MB_RDMA_LOW_WATER_5705;
14099 tp->bufmgr_config.mbuf_mac_rx_low_water =
14100 DEFAULT_MB_MACRX_LOW_WATER_5705;
14101 tp->bufmgr_config.mbuf_high_water =
14102 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014103 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14104 tp->bufmgr_config.mbuf_mac_rx_low_water =
14105 DEFAULT_MB_MACRX_LOW_WATER_5906;
14106 tp->bufmgr_config.mbuf_high_water =
14107 DEFAULT_MB_HIGH_WATER_5906;
14108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014109
Michael Chanfdfec1722005-07-25 12:31:48 -070014110 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14111 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14112 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14113 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14114 tp->bufmgr_config.mbuf_high_water_jumbo =
14115 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14116 } else {
14117 tp->bufmgr_config.mbuf_read_dma_low_water =
14118 DEFAULT_MB_RDMA_LOW_WATER;
14119 tp->bufmgr_config.mbuf_mac_rx_low_water =
14120 DEFAULT_MB_MACRX_LOW_WATER;
14121 tp->bufmgr_config.mbuf_high_water =
14122 DEFAULT_MB_HIGH_WATER;
14123
14124 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14125 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14126 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14127 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14128 tp->bufmgr_config.mbuf_high_water_jumbo =
14129 DEFAULT_MB_HIGH_WATER_JUMBO;
14130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014131
14132 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14133 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14134}
14135
14136static char * __devinit tg3_phy_string(struct tg3 *tp)
14137{
Matt Carlson79eb6902010-02-17 15:17:03 +000014138 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14139 case TG3_PHY_ID_BCM5400: return "5400";
14140 case TG3_PHY_ID_BCM5401: return "5401";
14141 case TG3_PHY_ID_BCM5411: return "5411";
14142 case TG3_PHY_ID_BCM5701: return "5701";
14143 case TG3_PHY_ID_BCM5703: return "5703";
14144 case TG3_PHY_ID_BCM5704: return "5704";
14145 case TG3_PHY_ID_BCM5705: return "5705";
14146 case TG3_PHY_ID_BCM5750: return "5750";
14147 case TG3_PHY_ID_BCM5752: return "5752";
14148 case TG3_PHY_ID_BCM5714: return "5714";
14149 case TG3_PHY_ID_BCM5780: return "5780";
14150 case TG3_PHY_ID_BCM5755: return "5755";
14151 case TG3_PHY_ID_BCM5787: return "5787";
14152 case TG3_PHY_ID_BCM5784: return "5784";
14153 case TG3_PHY_ID_BCM5756: return "5722/5756";
14154 case TG3_PHY_ID_BCM5906: return "5906";
14155 case TG3_PHY_ID_BCM5761: return "5761";
14156 case TG3_PHY_ID_BCM5718C: return "5718C";
14157 case TG3_PHY_ID_BCM5718S: return "5718S";
14158 case TG3_PHY_ID_BCM57765: return "57765";
14159 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014160 case 0: return "serdes";
14161 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014163}
14164
Michael Chanf9804dd2005-09-27 12:13:10 -070014165static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14166{
14167 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14168 strcpy(str, "PCI Express");
14169 return str;
14170 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14171 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14172
14173 strcpy(str, "PCIX:");
14174
14175 if ((clock_ctrl == 7) ||
14176 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14177 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14178 strcat(str, "133MHz");
14179 else if (clock_ctrl == 0)
14180 strcat(str, "33MHz");
14181 else if (clock_ctrl == 2)
14182 strcat(str, "50MHz");
14183 else if (clock_ctrl == 4)
14184 strcat(str, "66MHz");
14185 else if (clock_ctrl == 6)
14186 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014187 } else {
14188 strcpy(str, "PCI:");
14189 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14190 strcat(str, "66MHz");
14191 else
14192 strcat(str, "33MHz");
14193 }
14194 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14195 strcat(str, ":32-bit");
14196 else
14197 strcat(str, ":64-bit");
14198 return str;
14199}
14200
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014201static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014202{
14203 struct pci_dev *peer;
14204 unsigned int func, devnr = tp->pdev->devfn & ~7;
14205
14206 for (func = 0; func < 8; func++) {
14207 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14208 if (peer && peer != tp->pdev)
14209 break;
14210 pci_dev_put(peer);
14211 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014212 /* 5704 can be configured in single-port mode, set peer to
14213 * tp->pdev in that case.
14214 */
14215 if (!peer) {
14216 peer = tp->pdev;
14217 return peer;
14218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014219
14220 /*
14221 * We don't need to keep the refcount elevated; there's no way
14222 * to remove one half of this device without removing the other
14223 */
14224 pci_dev_put(peer);
14225
14226 return peer;
14227}
14228
David S. Miller15f98502005-05-18 22:49:26 -070014229static void __devinit tg3_init_coal(struct tg3 *tp)
14230{
14231 struct ethtool_coalesce *ec = &tp->coal;
14232
14233 memset(ec, 0, sizeof(*ec));
14234 ec->cmd = ETHTOOL_GCOALESCE;
14235 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14236 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14237 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14238 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14239 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14240 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14241 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14242 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14243 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14244
14245 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14246 HOSTCC_MODE_CLRTICK_TXBD)) {
14247 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14248 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14249 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14250 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14251 }
Michael Chand244c892005-07-05 14:42:33 -070014252
14253 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14254 ec->rx_coalesce_usecs_irq = 0;
14255 ec->tx_coalesce_usecs_irq = 0;
14256 ec->stats_block_coalesce_usecs = 0;
14257 }
David S. Miller15f98502005-05-18 22:49:26 -070014258}
14259
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014260static const struct net_device_ops tg3_netdev_ops = {
14261 .ndo_open = tg3_open,
14262 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014263 .ndo_start_xmit = tg3_start_xmit,
14264 .ndo_get_stats = tg3_get_stats,
14265 .ndo_validate_addr = eth_validate_addr,
14266 .ndo_set_multicast_list = tg3_set_rx_mode,
14267 .ndo_set_mac_address = tg3_set_mac_addr,
14268 .ndo_do_ioctl = tg3_ioctl,
14269 .ndo_tx_timeout = tg3_tx_timeout,
14270 .ndo_change_mtu = tg3_change_mtu,
14271#if TG3_VLAN_TAG_USED
14272 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14273#endif
14274#ifdef CONFIG_NET_POLL_CONTROLLER
14275 .ndo_poll_controller = tg3_poll_controller,
14276#endif
14277};
14278
14279static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14280 .ndo_open = tg3_open,
14281 .ndo_stop = tg3_close,
14282 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014283 .ndo_get_stats = tg3_get_stats,
14284 .ndo_validate_addr = eth_validate_addr,
14285 .ndo_set_multicast_list = tg3_set_rx_mode,
14286 .ndo_set_mac_address = tg3_set_mac_addr,
14287 .ndo_do_ioctl = tg3_ioctl,
14288 .ndo_tx_timeout = tg3_tx_timeout,
14289 .ndo_change_mtu = tg3_change_mtu,
14290#if TG3_VLAN_TAG_USED
14291 .ndo_vlan_rx_register = tg3_vlan_rx_register,
14292#endif
14293#ifdef CONFIG_NET_POLL_CONTROLLER
14294 .ndo_poll_controller = tg3_poll_controller,
14295#endif
14296};
14297
Linus Torvalds1da177e2005-04-16 15:20:36 -070014298static int __devinit tg3_init_one(struct pci_dev *pdev,
14299 const struct pci_device_id *ent)
14300{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014301 struct net_device *dev;
14302 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014303 int i, err, pm_cap;
14304 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014305 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014306 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014307
Joe Perches05dbe002010-02-17 19:44:19 +000014308 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014309
14310 err = pci_enable_device(pdev);
14311 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014312 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014313 return err;
14314 }
14315
Linus Torvalds1da177e2005-04-16 15:20:36 -070014316 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14317 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014318 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014319 goto err_out_disable_pdev;
14320 }
14321
14322 pci_set_master(pdev);
14323
14324 /* Find power-management capability. */
14325 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14326 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014327 dev_err(&pdev->dev,
14328 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014329 err = -EIO;
14330 goto err_out_free_res;
14331 }
14332
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014333 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014334 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014335 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014336 err = -ENOMEM;
14337 goto err_out_free_res;
14338 }
14339
Linus Torvalds1da177e2005-04-16 15:20:36 -070014340 SET_NETDEV_DEV(dev, &pdev->dev);
14341
Linus Torvalds1da177e2005-04-16 15:20:36 -070014342#if TG3_VLAN_TAG_USED
14343 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014344#endif
14345
14346 tp = netdev_priv(dev);
14347 tp->pdev = pdev;
14348 tp->dev = dev;
14349 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014350 tp->rx_mode = TG3_DEF_RX_MODE;
14351 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014352
Linus Torvalds1da177e2005-04-16 15:20:36 -070014353 if (tg3_debug > 0)
14354 tp->msg_enable = tg3_debug;
14355 else
14356 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14357
14358 /* The word/byte swap controls here control register access byte
14359 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14360 * setting below.
14361 */
14362 tp->misc_host_ctrl =
14363 MISC_HOST_CTRL_MASK_PCI_INT |
14364 MISC_HOST_CTRL_WORD_SWAP |
14365 MISC_HOST_CTRL_INDIR_ACCESS |
14366 MISC_HOST_CTRL_PCISTATE_RW;
14367
14368 /* The NONFRM (non-frame) byte/word swap controls take effect
14369 * on descriptor entries, anything which isn't packet data.
14370 *
14371 * The StrongARM chips on the board (one for tx, one for rx)
14372 * are running in big-endian mode.
14373 */
14374 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14375 GRC_MODE_WSWAP_NONFRM_DATA);
14376#ifdef __BIG_ENDIAN
14377 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14378#endif
14379 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014380 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000014381 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014382
Matt Carlsond5fe4882008-11-21 17:20:32 -080014383 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010014384 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014385 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014386 err = -ENOMEM;
14387 goto err_out_free_dev;
14388 }
14389
14390 tg3_init_link_config(tp);
14391
Linus Torvalds1da177e2005-04-16 15:20:36 -070014392 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
14393 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014394
Linus Torvalds1da177e2005-04-16 15:20:36 -070014395 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014396 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014397 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014398
14399 err = tg3_get_invariants(tp);
14400 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014401 dev_err(&pdev->dev,
14402 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014403 goto err_out_iounmap;
14404 }
14405
Matt Carlson615774f2009-11-13 13:03:39 +000014406 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
14407 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
Stephen Hemminger00829822008-11-20 20:14:53 -080014408 dev->netdev_ops = &tg3_netdev_ops;
14409 else
14410 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14411
14412
Michael Chan4a29cc22006-03-19 13:21:12 -080014413 /* The EPB bridge inside 5714, 5715, and 5780 and any
14414 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014415 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14416 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14417 * do DMA address check in tg3_start_xmit().
14418 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014419 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070014420 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014421 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014422 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014423#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014424 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014425#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014426 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014427 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014428
14429 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014430 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014431 err = pci_set_dma_mask(pdev, dma_mask);
14432 if (!err) {
14433 dev->features |= NETIF_F_HIGHDMA;
14434 err = pci_set_consistent_dma_mask(pdev,
14435 persist_dma_mask);
14436 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014437 dev_err(&pdev->dev, "Unable to obtain 64 bit "
14438 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014439 goto err_out_iounmap;
14440 }
14441 }
14442 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014443 if (err || dma_mask == DMA_BIT_MASK(32)) {
14444 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014445 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014446 dev_err(&pdev->dev,
14447 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080014448 goto err_out_iounmap;
14449 }
14450 }
14451
Michael Chanfdfec1722005-07-25 12:31:48 -070014452 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014453
Matt Carlson507399f2009-11-13 13:03:37 +000014454 /* Selectively allow TSO based on operating conditions */
14455 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
14456 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
14457 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14458 else {
14459 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
14460 tp->fw_needed = NULL;
14461 }
14462
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014463 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014464 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014465
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014466 /* TSO is on by default on chips that support hardware TSO.
14467 * Firmware TSO on older chips gives lower performance, so it
14468 * is off by default, but can be enabled using ethtool.
14469 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000014470 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
14471 (dev->features & NETIF_F_IP_CSUM))
14472 dev->features |= NETIF_F_TSO;
14473
14474 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
14475 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
14476 if (dev->features & NETIF_F_IPV6_CSUM)
Michael Chanb0026622006-07-03 19:42:14 -070014477 dev->features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +000014478 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
14479 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014480 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14481 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014482 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000014483 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -070014484 dev->features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070014485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014486
Linus Torvalds1da177e2005-04-16 15:20:36 -070014487 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14488 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14489 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14490 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14491 tp->rx_pending = 63;
14492 }
14493
Linus Torvalds1da177e2005-04-16 15:20:36 -070014494 err = tg3_get_device_address(tp);
14495 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014496 dev_err(&pdev->dev,
14497 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000014498 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014499 }
14500
Matt Carlson0d3031d2007-10-10 18:02:43 -070014501 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080014502 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080014503 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014504 dev_err(&pdev->dev,
14505 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014506 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000014507 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014508 }
14509
14510 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000014511
14512 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14513 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014514 }
14515
Matt Carlsonc88864d2007-11-12 21:07:01 -080014516 /*
14517 * Reset chip in case UNDI or EFI driver did not shutdown
14518 * DMA self test will enable WDMAC and we'll see (spurious)
14519 * pending DMA on the PCI bus at that point.
14520 */
14521 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
14522 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
14523 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
14524 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14525 }
14526
14527 err = tg3_test_dma(tp);
14528 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014529 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080014530 goto err_out_apeunmap;
14531 }
14532
Matt Carlsonc88864d2007-11-12 21:07:01 -080014533 /* flow control autonegotiation is default behavior */
14534 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
Steve Glendinninge18ce342008-12-16 02:00:00 -080014535 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlsonc88864d2007-11-12 21:07:01 -080014536
Matt Carlson78f90dc2009-11-13 13:03:42 +000014537 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
14538 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
14539 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
14540 for (i = 0; i < TG3_IRQ_MAX_VECS; i++) {
14541 struct tg3_napi *tnapi = &tp->napi[i];
14542
14543 tnapi->tp = tp;
14544 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
14545
14546 tnapi->int_mbox = intmbx;
14547 if (i < 4)
14548 intmbx += 0x8;
14549 else
14550 intmbx += 0x4;
14551
14552 tnapi->consmbox = rcvmbx;
14553 tnapi->prodmbox = sndmbx;
14554
14555 if (i) {
14556 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
14557 netif_napi_add(dev, &tnapi->napi, tg3_poll_msix, 64);
14558 } else {
14559 tnapi->coal_now = HOSTCC_MODE_NOW;
14560 netif_napi_add(dev, &tnapi->napi, tg3_poll, 64);
14561 }
14562
14563 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
14564 break;
14565
14566 /*
14567 * If we support MSIX, we'll be using RSS. If we're using
14568 * RSS, the first vector only handles link interrupts and the
14569 * remaining vectors handle rx and tx interrupts. Reuse the
14570 * mailbox values for the next iteration. The values we setup
14571 * above are still useful for the single vectored mode.
14572 */
14573 if (!i)
14574 continue;
14575
14576 rcvmbx += 0x8;
14577
14578 if (sndmbx & 0x4)
14579 sndmbx -= 0x4;
14580 else
14581 sndmbx += 0xc;
14582 }
14583
Matt Carlsonc88864d2007-11-12 21:07:01 -080014584 tg3_init_coal(tp);
14585
Michael Chanc49a1562006-12-17 17:07:29 -080014586 pci_set_drvdata(pdev, dev);
14587
Linus Torvalds1da177e2005-04-16 15:20:36 -070014588 err = register_netdev(dev);
14589 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000014590 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014591 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014592 }
14593
Joe Perches05dbe002010-02-17 19:44:19 +000014594 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
14595 tp->board_part_number,
14596 tp->pci_chip_rev_id,
14597 tg3_bus_string(tp, str),
14598 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014599
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014600 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
14601 struct phy_device *phydev;
14602 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000014603 netdev_info(dev,
14604 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014605 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014606 } else
Matt Carlson5129c3a2010-04-05 10:19:23 +000014607 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
14608 "(WireSpeed[%d])\n", tg3_phy_string(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000014609 ((tp->tg3_flags & TG3_FLAG_10_100_ONLY) ? "10/100Base-TX" :
14610 ((tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) ? "1000Base-SX" :
14611 "10/100/1000Base-T")),
14612 (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED) == 0);
Matt Carlsondf59c942008-11-03 16:52:56 -080014613
Joe Perches05dbe002010-02-17 19:44:19 +000014614 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
14615 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
14616 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
14617 (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) != 0,
14618 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
14619 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
14620 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
14621 tp->dma_rwctrl,
14622 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
14623 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014624
14625 return 0;
14626
Matt Carlson0d3031d2007-10-10 18:02:43 -070014627err_out_apeunmap:
14628 if (tp->aperegs) {
14629 iounmap(tp->aperegs);
14630 tp->aperegs = NULL;
14631 }
14632
Linus Torvalds1da177e2005-04-16 15:20:36 -070014633err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070014634 if (tp->regs) {
14635 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014636 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014638
14639err_out_free_dev:
14640 free_netdev(dev);
14641
14642err_out_free_res:
14643 pci_release_regions(pdev);
14644
14645err_out_disable_pdev:
14646 pci_disable_device(pdev);
14647 pci_set_drvdata(pdev, NULL);
14648 return err;
14649}
14650
14651static void __devexit tg3_remove_one(struct pci_dev *pdev)
14652{
14653 struct net_device *dev = pci_get_drvdata(pdev);
14654
14655 if (dev) {
14656 struct tg3 *tp = netdev_priv(dev);
14657
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014658 if (tp->fw)
14659 release_firmware(tp->fw);
14660
Michael Chan7faa0062006-02-02 17:29:28 -080014661 flush_scheduled_work();
Matt Carlson158d7ab2008-05-29 01:37:54 -070014662
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014663 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
14664 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070014665 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014666 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070014667
Linus Torvalds1da177e2005-04-16 15:20:36 -070014668 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014669 if (tp->aperegs) {
14670 iounmap(tp->aperegs);
14671 tp->aperegs = NULL;
14672 }
Michael Chan68929142005-08-09 20:17:14 -070014673 if (tp->regs) {
14674 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014675 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014677 free_netdev(dev);
14678 pci_release_regions(pdev);
14679 pci_disable_device(pdev);
14680 pci_set_drvdata(pdev, NULL);
14681 }
14682}
14683
14684static int tg3_suspend(struct pci_dev *pdev, pm_message_t state)
14685{
14686 struct net_device *dev = pci_get_drvdata(pdev);
14687 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014688 pci_power_t target_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014689 int err;
14690
Michael Chan3e0c95f2007-08-03 20:56:54 -070014691 /* PCI register 4 needs to be saved whether netif_running() or not.
14692 * MSI address and data need to be saved if using MSI and
14693 * netif_running().
14694 */
14695 pci_save_state(pdev);
14696
Linus Torvalds1da177e2005-04-16 15:20:36 -070014697 if (!netif_running(dev))
14698 return 0;
14699
Michael Chan7faa0062006-02-02 17:29:28 -080014700 flush_scheduled_work();
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014701 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014702 tg3_netif_stop(tp);
14703
14704 del_timer_sync(&tp->timer);
14705
David S. Millerf47c11e2005-06-24 20:18:35 -070014706 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014707 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070014708 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014709
14710 netif_device_detach(dev);
14711
David S. Millerf47c11e2005-06-24 20:18:35 -070014712 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070014713 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080014714 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070014715 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014716
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014717 target_state = pdev->pm_cap ? pci_target_state(pdev) : PCI_D3hot;
14718
14719 err = tg3_set_power_state(tp, target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014720 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014721 int err2;
14722
David S. Millerf47c11e2005-06-24 20:18:35 -070014723 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014724
Michael Chan6a9eba12005-12-13 21:08:58 -080014725 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014726 err2 = tg3_restart_hw(tp, 1);
14727 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070014728 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014729
14730 tp->timer.expires = jiffies + tp->timer_offset;
14731 add_timer(&tp->timer);
14732
14733 netif_device_attach(dev);
14734 tg3_netif_start(tp);
14735
Michael Chanb9ec6c12006-07-25 16:37:27 -070014736out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014737 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014738
14739 if (!err2)
14740 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014741 }
14742
14743 return err;
14744}
14745
14746static int tg3_resume(struct pci_dev *pdev)
14747{
14748 struct net_device *dev = pci_get_drvdata(pdev);
14749 struct tg3 *tp = netdev_priv(dev);
14750 int err;
14751
Michael Chan3e0c95f2007-08-03 20:56:54 -070014752 pci_restore_state(tp->pdev);
14753
Linus Torvalds1da177e2005-04-16 15:20:36 -070014754 if (!netif_running(dev))
14755 return 0;
14756
Michael Chanbc1c7562006-03-20 17:48:03 -080014757 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014758 if (err)
14759 return err;
14760
14761 netif_device_attach(dev);
14762
David S. Millerf47c11e2005-06-24 20:18:35 -070014763 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014764
Michael Chan6a9eba12005-12-13 21:08:58 -080014765 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070014766 err = tg3_restart_hw(tp, 1);
14767 if (err)
14768 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014769
14770 tp->timer.expires = jiffies + tp->timer_offset;
14771 add_timer(&tp->timer);
14772
Linus Torvalds1da177e2005-04-16 15:20:36 -070014773 tg3_netif_start(tp);
14774
Michael Chanb9ec6c12006-07-25 16:37:27 -070014775out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014776 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014777
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014778 if (!err)
14779 tg3_phy_start(tp);
14780
Michael Chanb9ec6c12006-07-25 16:37:27 -070014781 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014782}
14783
14784static struct pci_driver tg3_driver = {
14785 .name = DRV_MODULE_NAME,
14786 .id_table = tg3_pci_tbl,
14787 .probe = tg3_init_one,
14788 .remove = __devexit_p(tg3_remove_one),
14789 .suspend = tg3_suspend,
14790 .resume = tg3_resume
14791};
14792
14793static int __init tg3_init(void)
14794{
Jeff Garzik29917622006-08-19 17:48:59 -040014795 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014796}
14797
14798static void __exit tg3_cleanup(void)
14799{
14800 pci_unregister_driver(&tg3_driver);
14801}
14802
14803module_init(tg3_init);
14804module_exit(tg3_cleanup);