blob: f09bc5dfe8b2c7d652f8d3848ee204d5c2630fba [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 Carlson0d2a5062009-02-25 14:40:42 +00007 * Copyright (C) 2005-2009 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"
70#define PFX DRV_MODULE_NAME ": "
Matt Carlsondaf09de2009-09-01 13:22:42 +000071#define DRV_MODULE_VERSION "3.102"
72#define DRV_MODULE_RELDATE "September 1, 2009"
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74#define TG3_DEF_MAC_MODE 0
75#define TG3_DEF_RX_MODE 0
76#define TG3_DEF_TX_MODE 0
77#define TG3_DEF_MSG_ENABLE \
78 (NETIF_MSG_DRV | \
79 NETIF_MSG_PROBE | \
80 NETIF_MSG_LINK | \
81 NETIF_MSG_TIMER | \
82 NETIF_MSG_IFDOWN | \
83 NETIF_MSG_IFUP | \
84 NETIF_MSG_RX_ERR | \
85 NETIF_MSG_TX_ERR)
86
87/* length of time before we decide the hardware is borked,
88 * and dev->tx_timeout() should be called to fix the problem
89 */
90#define TG3_TX_TIMEOUT (5 * HZ)
91
92/* hardware minimum and maximum for a single frame's data payload */
93#define TG3_MIN_MTU 60
94#define TG3_MAX_MTU(tp) \
Matt Carlson8f666b02009-08-28 13:58:24 +000095 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/* These numbers seem to be hard coded in the NIC firmware somehow.
98 * You can't change the ring sizes, but you can change where you place
99 * them in the NIC onboard memory.
100 */
101#define TG3_RX_RING_SIZE 512
102#define TG3_DEF_RX_RING_PENDING 200
103#define TG3_RX_JUMBO_RING_SIZE 256
104#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonbaf8a942009-09-01 13:13:00 +0000105#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/* Do not place this n-ring entries value into the tp struct itself,
108 * we really want to expose these constants to GCC so that modulo et
109 * al. operations are done with shifts and masks instead of with
110 * hw multiply/modulo instructions. Another solution would be to
111 * replace things like '% foo' with '& (foo - 1)'.
112 */
113#define TG3_RX_RCB_RING_SIZE(tp) \
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000114 (((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) && \
Matt Carlson5ea1c502009-09-11 16:50:16 -0700115 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) ? 1024 : 512)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117#define TG3_TX_RING_SIZE 512
118#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
119
120#define TG3_RX_RING_BYTES (sizeof(struct tg3_rx_buffer_desc) * \
121 TG3_RX_RING_SIZE)
Matt Carlson79ed5ac2009-08-28 14:00:55 +0000122#define TG3_RX_JUMBO_RING_BYTES (sizeof(struct tg3_ext_rx_buffer_desc) * \
123 TG3_RX_JUMBO_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define TG3_RX_RCB_RING_BYTES(tp) (sizeof(struct tg3_rx_buffer_desc) * \
Matt Carlson79ed5ac2009-08-28 14:00:55 +0000125 TG3_RX_RCB_RING_SIZE(tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
127 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
129
Matt Carlson287be122009-08-28 13:58:46 +0000130#define TG3_DMA_BYTE_ENAB 64
131
132#define TG3_RX_STD_DMA_SZ 1536
133#define TG3_RX_JMB_DMA_SZ 9046
134
135#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
136
137#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
138#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000141#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Matt Carlsonad829262008-11-21 17:16:16 -0800143#define TG3_RAW_IP_ALIGN 2
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/* number of ETHTOOL_GSTATS u64's */
146#define TG3_NUM_STATS (sizeof(struct tg3_ethtool_stats)/sizeof(u64))
147
Michael Chan4cafd3f2005-05-29 14:56:34 -0700148#define TG3_NUM_TEST 6
149
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800150#define FIRMWARE_TG3 "tigon/tg3.bin"
151#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
152#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static char version[] __devinitdata =
155 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
156
157MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
158MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
159MODULE_LICENSE("GPL");
160MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800161MODULE_FIRMWARE(FIRMWARE_TG3);
162MODULE_FIRMWARE(FIRMWARE_TG3TSO);
163MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
164
Matt Carlson679563f2009-09-01 12:55:46 +0000165#define TG3_RSS_MIN_NUM_MSIX_VECS 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
168module_param(tg3_debug, int, 0);
169MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
170
171static struct pci_device_id tg3_pci_tbl[] = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700172 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
173 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
174 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
175 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
176 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
177 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
178 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
179 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
180 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
181 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
182 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
183 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
184 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
185 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
186 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
187 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
188 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
189 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
190 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
191 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
192 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
193 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
194 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5720)},
195 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700196 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700197 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
198 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
199 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750M)},
200 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
201 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
202 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
203 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
204 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
205 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
206 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
207 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
208 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
209 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
210 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700211 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700212 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
213 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
214 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700238 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
239 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
240 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
241 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
242 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
243 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
244 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
245 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246};
247
248MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
249
Andreas Mohr50da8592006-08-14 23:54:30 -0700250static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 const char string[ETH_GSTRING_LEN];
252} ethtool_stats_keys[TG3_NUM_STATS] = {
253 { "rx_octets" },
254 { "rx_fragments" },
255 { "rx_ucast_packets" },
256 { "rx_mcast_packets" },
257 { "rx_bcast_packets" },
258 { "rx_fcs_errors" },
259 { "rx_align_errors" },
260 { "rx_xon_pause_rcvd" },
261 { "rx_xoff_pause_rcvd" },
262 { "rx_mac_ctrl_rcvd" },
263 { "rx_xoff_entered" },
264 { "rx_frame_too_long_errors" },
265 { "rx_jabbers" },
266 { "rx_undersize_packets" },
267 { "rx_in_length_errors" },
268 { "rx_out_length_errors" },
269 { "rx_64_or_less_octet_packets" },
270 { "rx_65_to_127_octet_packets" },
271 { "rx_128_to_255_octet_packets" },
272 { "rx_256_to_511_octet_packets" },
273 { "rx_512_to_1023_octet_packets" },
274 { "rx_1024_to_1522_octet_packets" },
275 { "rx_1523_to_2047_octet_packets" },
276 { "rx_2048_to_4095_octet_packets" },
277 { "rx_4096_to_8191_octet_packets" },
278 { "rx_8192_to_9022_octet_packets" },
279
280 { "tx_octets" },
281 { "tx_collisions" },
282
283 { "tx_xon_sent" },
284 { "tx_xoff_sent" },
285 { "tx_flow_control" },
286 { "tx_mac_errors" },
287 { "tx_single_collisions" },
288 { "tx_mult_collisions" },
289 { "tx_deferred" },
290 { "tx_excessive_collisions" },
291 { "tx_late_collisions" },
292 { "tx_collide_2times" },
293 { "tx_collide_3times" },
294 { "tx_collide_4times" },
295 { "tx_collide_5times" },
296 { "tx_collide_6times" },
297 { "tx_collide_7times" },
298 { "tx_collide_8times" },
299 { "tx_collide_9times" },
300 { "tx_collide_10times" },
301 { "tx_collide_11times" },
302 { "tx_collide_12times" },
303 { "tx_collide_13times" },
304 { "tx_collide_14times" },
305 { "tx_collide_15times" },
306 { "tx_ucast_packets" },
307 { "tx_mcast_packets" },
308 { "tx_bcast_packets" },
309 { "tx_carrier_sense_errors" },
310 { "tx_discards" },
311 { "tx_errors" },
312
313 { "dma_writeq_full" },
314 { "dma_write_prioq_full" },
315 { "rxbds_empty" },
316 { "rx_discards" },
317 { "rx_errors" },
318 { "rx_threshold_hit" },
319
320 { "dma_readq_full" },
321 { "dma_read_prioq_full" },
322 { "tx_comp_queue_full" },
323
324 { "ring_set_send_prod_index" },
325 { "ring_status_update" },
326 { "nic_irqs" },
327 { "nic_avoided_irqs" },
328 { "nic_tx_threshold_hit" }
329};
330
Andreas Mohr50da8592006-08-14 23:54:30 -0700331static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700332 const char string[ETH_GSTRING_LEN];
333} ethtool_test_keys[TG3_NUM_TEST] = {
334 { "nvram test (online) " },
335 { "link test (online) " },
336 { "register test (offline)" },
337 { "memory test (offline)" },
338 { "loopback test (offline)" },
339 { "interrupt test (offline)" },
340};
341
Michael Chanb401e9e2005-12-19 16:27:04 -0800342static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
343{
344 writel(val, tp->regs + off);
345}
346
347static u32 tg3_read32(struct tg3 *tp, u32 off)
348{
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400349 return (readl(tp->regs + off));
Michael Chanb401e9e2005-12-19 16:27:04 -0800350}
351
Matt Carlson0d3031d2007-10-10 18:02:43 -0700352static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
353{
354 writel(val, tp->aperegs + off);
355}
356
357static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
358{
359 return (readl(tp->aperegs + off));
360}
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
363{
Michael Chan68929142005-08-09 20:17:14 -0700364 unsigned long flags;
365
366 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700367 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
368 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700369 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700370}
371
372static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
373{
374 writel(val, tp->regs + off);
375 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Michael Chan68929142005-08-09 20:17:14 -0700378static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
379{
380 unsigned long flags;
381 u32 val;
382
383 spin_lock_irqsave(&tp->indirect_lock, flags);
384 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
385 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
386 spin_unlock_irqrestore(&tp->indirect_lock, flags);
387 return val;
388}
389
390static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
391{
392 unsigned long flags;
393
394 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
395 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
396 TG3_64BIT_REG_LOW, val);
397 return;
398 }
399 if (off == (MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW)) {
400 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
401 TG3_64BIT_REG_LOW, val);
402 return;
403 }
404
405 spin_lock_irqsave(&tp->indirect_lock, flags);
406 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
407 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
408 spin_unlock_irqrestore(&tp->indirect_lock, flags);
409
410 /* In indirect mode when disabling interrupts, we also need
411 * to clear the interrupt bit in the GRC local ctrl register.
412 */
413 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
414 (val == 0x1)) {
415 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
416 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
417 }
418}
419
420static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
421{
422 unsigned long flags;
423 u32 val;
424
425 spin_lock_irqsave(&tp->indirect_lock, flags);
426 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
427 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
428 spin_unlock_irqrestore(&tp->indirect_lock, flags);
429 return val;
430}
431
Michael Chanb401e9e2005-12-19 16:27:04 -0800432/* usec_wait specifies the wait time in usec when writing to certain registers
433 * where it is unsafe to read back the register without some delay.
434 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
435 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
436 */
437static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Michael Chanb401e9e2005-12-19 16:27:04 -0800439 if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
440 (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
441 /* Non-posted methods */
442 tp->write32(tp, off, val);
443 else {
444 /* Posted method */
445 tg3_write32(tp, off, val);
446 if (usec_wait)
447 udelay(usec_wait);
448 tp->read32(tp, off);
449 }
450 /* Wait again after the read for the posted method to guarantee that
451 * the wait time is met.
452 */
453 if (usec_wait)
454 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Michael Chan09ee9292005-08-09 20:17:00 -0700457static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
458{
459 tp->write32_mbox(tp, off, val);
Michael Chan68929142005-08-09 20:17:14 -0700460 if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
461 !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
462 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700463}
464
Michael Chan20094932005-08-09 20:16:32 -0700465static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 void __iomem *mbox = tp->regs + off;
468 writel(val, mbox);
469 if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
470 writel(val, mbox);
471 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
472 readl(mbox);
473}
474
Michael Chanb5d37722006-09-27 16:06:21 -0700475static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
476{
477 return (readl(tp->regs + off + GRCMBOX_BASE));
478}
479
480static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
481{
482 writel(val, tp->regs + off + GRCMBOX_BASE);
483}
484
Michael Chan20094932005-08-09 20:16:32 -0700485#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700486#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Michael Chan20094932005-08-09 20:16:32 -0700487#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
488#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700489#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700490
491#define tw32(reg,val) tp->write32(tp, reg, val)
Michael Chanb401e9e2005-12-19 16:27:04 -0800492#define tw32_f(reg,val) _tw32_flush(tp,(reg),(val), 0)
493#define tw32_wait_f(reg,val,us) _tw32_flush(tp,(reg),(val), (us))
Michael Chan20094932005-08-09 20:16:32 -0700494#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
497{
Michael Chan68929142005-08-09 20:17:14 -0700498 unsigned long flags;
499
Michael Chanb5d37722006-09-27 16:06:21 -0700500 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
501 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
502 return;
503
Michael Chan68929142005-08-09 20:17:14 -0700504 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700505 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
506 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
507 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Michael Chanbbadf502006-04-06 21:46:34 -0700509 /* Always leave this as zero. */
510 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
511 } else {
512 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
513 tw32_f(TG3PCI_MEM_WIN_DATA, val);
514
515 /* Always leave this as zero. */
516 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
517 }
Michael Chan68929142005-08-09 20:17:14 -0700518 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
521static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
522{
Michael Chan68929142005-08-09 20:17:14 -0700523 unsigned long flags;
524
Michael Chanb5d37722006-09-27 16:06:21 -0700525 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
526 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
527 *val = 0;
528 return;
529 }
530
Michael Chan68929142005-08-09 20:17:14 -0700531 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700532 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
533 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
534 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Michael Chanbbadf502006-04-06 21:46:34 -0700536 /* Always leave this as zero. */
537 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
538 } else {
539 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
540 *val = tr32(TG3PCI_MEM_WIN_DATA);
541
542 /* Always leave this as zero. */
543 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
544 }
Michael Chan68929142005-08-09 20:17:14 -0700545 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Matt Carlson0d3031d2007-10-10 18:02:43 -0700548static void tg3_ape_lock_init(struct tg3 *tp)
549{
550 int i;
551
552 /* Make sure the driver hasn't any stale locks. */
553 for (i = 0; i < 8; i++)
554 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + 4 * i,
555 APE_LOCK_GRANT_DRIVER);
556}
557
558static int tg3_ape_lock(struct tg3 *tp, int locknum)
559{
560 int i, off;
561 int ret = 0;
562 u32 status;
563
564 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
565 return 0;
566
567 switch (locknum) {
Matt Carlson77b483f2008-08-15 14:07:24 -0700568 case TG3_APE_LOCK_GRC:
Matt Carlson0d3031d2007-10-10 18:02:43 -0700569 case TG3_APE_LOCK_MEM:
570 break;
571 default:
572 return -EINVAL;
573 }
574
575 off = 4 * locknum;
576
577 tg3_ape_write32(tp, TG3_APE_LOCK_REQ + off, APE_LOCK_REQ_DRIVER);
578
579 /* Wait for up to 1 millisecond to acquire lock. */
580 for (i = 0; i < 100; i++) {
581 status = tg3_ape_read32(tp, TG3_APE_LOCK_GRANT + off);
582 if (status == APE_LOCK_GRANT_DRIVER)
583 break;
584 udelay(10);
585 }
586
587 if (status != APE_LOCK_GRANT_DRIVER) {
588 /* Revoke the lock request. */
589 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + off,
590 APE_LOCK_GRANT_DRIVER);
591
592 ret = -EBUSY;
593 }
594
595 return ret;
596}
597
598static void tg3_ape_unlock(struct tg3 *tp, int locknum)
599{
600 int off;
601
602 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
603 return;
604
605 switch (locknum) {
Matt Carlson77b483f2008-08-15 14:07:24 -0700606 case TG3_APE_LOCK_GRC:
Matt Carlson0d3031d2007-10-10 18:02:43 -0700607 case TG3_APE_LOCK_MEM:
608 break;
609 default:
610 return;
611 }
612
613 off = 4 * locknum;
614 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + off, APE_LOCK_GRANT_DRIVER);
615}
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617static void tg3_disable_ints(struct tg3 *tp)
618{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000619 int i;
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 tw32(TG3PCI_MISC_HOST_CTRL,
622 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000623 for (i = 0; i < tp->irq_max; i++)
624 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627static void tg3_enable_ints(struct tg3 *tp)
628{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000629 int i;
630 u32 coal_now = 0;
631
Michael Chanbbe832c2005-06-24 20:20:04 -0700632 tp->irq_sync = 0;
633 wmb();
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 tw32(TG3PCI_MISC_HOST_CTRL,
636 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000637
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000638 for (i = 0; i < tp->irq_cnt; i++) {
639 struct tg3_napi *tnapi = &tp->napi[i];
640 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
641 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
642 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
643
644 coal_now |= tnapi->coal_now;
645 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000646
647 /* Force an initial interrupt */
648 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
649 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
650 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
651 else
652 tw32(HOSTCC_MODE, tp->coalesce_mode |
653 HOSTCC_MODE_ENABLE | coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Matt Carlson17375d22009-08-28 14:02:18 +0000656static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700657{
Matt Carlson17375d22009-08-28 14:02:18 +0000658 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000659 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700660 unsigned int work_exists = 0;
661
662 /* check for phy events */
663 if (!(tp->tg3_flags &
664 (TG3_FLAG_USE_LINKCHG_REG |
665 TG3_FLAG_POLL_SERDES))) {
666 if (sblk->status & SD_STATUS_LINK_CHG)
667 work_exists = 1;
668 }
669 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000670 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000671 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700672 work_exists = 1;
673
674 return work_exists;
675}
676
Matt Carlson17375d22009-08-28 14:02:18 +0000677/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700678 * similar to tg3_enable_ints, but it accurately determines whether there
679 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400680 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 */
Matt Carlson17375d22009-08-28 14:02:18 +0000682static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Matt Carlson17375d22009-08-28 14:02:18 +0000684 struct tg3 *tp = tnapi->tp;
685
Matt Carlson898a56f2009-08-28 14:02:40 +0000686 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 mmiowb();
688
David S. Millerfac9b832005-05-18 22:46:34 -0700689 /* When doing tagged status, this work check is unnecessary.
690 * The last_tag we write above tells the chip which piece of
691 * work we've completed.
692 */
693 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
Matt Carlson17375d22009-08-28 14:02:18 +0000694 tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700695 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000696 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Matt Carlsonfed97812009-09-01 13:10:19 +0000699static void tg3_napi_disable(struct tg3 *tp)
700{
701 int i;
702
703 for (i = tp->irq_cnt - 1; i >= 0; i--)
704 napi_disable(&tp->napi[i].napi);
705}
706
707static void tg3_napi_enable(struct tg3 *tp)
708{
709 int i;
710
711 for (i = 0; i < tp->irq_cnt; i++)
712 napi_enable(&tp->napi[i].napi);
713}
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715static inline void tg3_netif_stop(struct tg3 *tp)
716{
Michael Chanbbe832c2005-06-24 20:20:04 -0700717 tp->dev->trans_start = jiffies; /* prevent tx timeout */
Matt Carlsonfed97812009-09-01 13:10:19 +0000718 tg3_napi_disable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 netif_tx_disable(tp->dev);
720}
721
722static inline void tg3_netif_start(struct tg3 *tp)
723{
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000724 /* NOTE: unconditional netif_tx_wake_all_queues is only
725 * appropriate so long as all callers are assured to
726 * have free tx slots (such as after tg3_init_hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 */
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000728 netif_tx_wake_all_queues(tp->dev);
729
Matt Carlsonfed97812009-09-01 13:10:19 +0000730 tg3_napi_enable(tp);
731 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
David S. Millerf47c11e2005-06-24 20:18:35 -0700732 tg3_enable_ints(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
735static void tg3_switch_clocks(struct tg3 *tp)
736{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000737 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 u32 orig_clock_ctrl;
739
Matt Carlson795d01c2007-10-07 23:28:17 -0700740 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
741 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700742 return;
743
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000744 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 orig_clock_ctrl = clock_ctrl;
747 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
748 CLOCK_CTRL_CLKRUN_OENABLE |
749 0x1f);
750 tp->pci_clock_ctrl = clock_ctrl;
751
752 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
753 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800754 tw32_wait_f(TG3PCI_CLOCK_CTRL,
755 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800758 tw32_wait_f(TG3PCI_CLOCK_CTRL,
759 clock_ctrl |
760 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
761 40);
762 tw32_wait_f(TG3PCI_CLOCK_CTRL,
763 clock_ctrl | (CLOCK_CTRL_ALTCLK),
764 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800766 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
769#define PHY_BUSY_LOOPS 5000
770
771static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
772{
773 u32 frame_val;
774 unsigned int loops;
775 int ret;
776
777 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
778 tw32_f(MAC_MI_MODE,
779 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
780 udelay(80);
781 }
782
783 *val = 0x0;
784
Matt Carlson882e9792009-09-01 13:21:36 +0000785 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 MI_COM_PHY_ADDR_MASK);
787 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
788 MI_COM_REG_ADDR_MASK);
789 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 tw32_f(MAC_MI_COM, frame_val);
792
793 loops = PHY_BUSY_LOOPS;
794 while (loops != 0) {
795 udelay(10);
796 frame_val = tr32(MAC_MI_COM);
797
798 if ((frame_val & MI_COM_BUSY) == 0) {
799 udelay(5);
800 frame_val = tr32(MAC_MI_COM);
801 break;
802 }
803 loops -= 1;
804 }
805
806 ret = -EBUSY;
807 if (loops != 0) {
808 *val = frame_val & MI_COM_DATA_MASK;
809 ret = 0;
810 }
811
812 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
813 tw32_f(MAC_MI_MODE, tp->mi_mode);
814 udelay(80);
815 }
816
817 return ret;
818}
819
820static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
821{
822 u32 frame_val;
823 unsigned int loops;
824 int ret;
825
Matt Carlson7f97a4b2009-08-25 10:10:03 +0000826 if ((tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700827 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
828 return 0;
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
831 tw32_f(MAC_MI_MODE,
832 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
833 udelay(80);
834 }
835
Matt Carlson882e9792009-09-01 13:21:36 +0000836 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 MI_COM_PHY_ADDR_MASK);
838 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
839 MI_COM_REG_ADDR_MASK);
840 frame_val |= (val & MI_COM_DATA_MASK);
841 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 tw32_f(MAC_MI_COM, frame_val);
844
845 loops = PHY_BUSY_LOOPS;
846 while (loops != 0) {
847 udelay(10);
848 frame_val = tr32(MAC_MI_COM);
849 if ((frame_val & MI_COM_BUSY) == 0) {
850 udelay(5);
851 frame_val = tr32(MAC_MI_COM);
852 break;
853 }
854 loops -= 1;
855 }
856
857 ret = -EBUSY;
858 if (loops != 0)
859 ret = 0;
860
861 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
862 tw32_f(MAC_MI_MODE, tp->mi_mode);
863 udelay(80);
864 }
865
866 return ret;
867}
868
Matt Carlson95e28692008-05-25 23:44:14 -0700869static int tg3_bmcr_reset(struct tg3 *tp)
870{
871 u32 phy_control;
872 int limit, err;
873
874 /* OK, reset it, and poll the BMCR_RESET bit until it
875 * clears or we time out.
876 */
877 phy_control = BMCR_RESET;
878 err = tg3_writephy(tp, MII_BMCR, phy_control);
879 if (err != 0)
880 return -EBUSY;
881
882 limit = 5000;
883 while (limit--) {
884 err = tg3_readphy(tp, MII_BMCR, &phy_control);
885 if (err != 0)
886 return -EBUSY;
887
888 if ((phy_control & BMCR_RESET) == 0) {
889 udelay(40);
890 break;
891 }
892 udelay(10);
893 }
Roel Kluind4675b52009-02-12 16:33:27 -0800894 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -0700895 return -EBUSY;
896
897 return 0;
898}
899
Matt Carlson158d7ab2008-05-29 01:37:54 -0700900static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
901{
Francois Romieu3d165432009-01-19 16:56:50 -0800902 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700903 u32 val;
904
905 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_PAUSED)
906 return -EAGAIN;
907
908 if (tg3_readphy(tp, reg, &val))
909 return -EIO;
910
911 return val;
912}
913
914static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
915{
Francois Romieu3d165432009-01-19 16:56:50 -0800916 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700917
918 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_PAUSED)
919 return -EAGAIN;
920
921 if (tg3_writephy(tp, reg, val))
922 return -EIO;
923
924 return 0;
925}
926
927static int tg3_mdio_reset(struct mii_bus *bp)
928{
929 return 0;
930}
931
Matt Carlson9c61d6b2008-11-03 16:54:56 -0800932static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -0700933{
934 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800935 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -0700936
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800937 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
938 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
939 case TG3_PHY_ID_BCM50610:
940 val = MAC_PHYCFG2_50610_LED_MODES;
941 break;
942 case TG3_PHY_ID_BCMAC131:
943 val = MAC_PHYCFG2_AC131_LED_MODES;
944 break;
945 case TG3_PHY_ID_RTL8211C:
946 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
947 break;
948 case TG3_PHY_ID_RTL8201E:
949 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
950 break;
951 default:
Matt Carlsona9daf362008-05-25 23:49:44 -0700952 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800953 }
954
955 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
956 tw32(MAC_PHYCFG2, val);
957
958 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000959 val &= ~(MAC_PHYCFG1_RGMII_INT |
960 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
961 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800962 tw32(MAC_PHYCFG1, val);
963
964 return;
965 }
966
967 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE))
968 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
969 MAC_PHYCFG2_FMODE_MASK_MASK |
970 MAC_PHYCFG2_GMODE_MASK_MASK |
971 MAC_PHYCFG2_ACT_MASK_MASK |
972 MAC_PHYCFG2_QUAL_MASK_MASK |
973 MAC_PHYCFG2_INBAND_ENABLE;
974
975 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -0700976
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000977 val = tr32(MAC_PHYCFG1);
978 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
979 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
980 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -0700981 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
982 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
983 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
984 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
985 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000986 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
987 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
988 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -0700989
Matt Carlsona9daf362008-05-25 23:49:44 -0700990 val = tr32(MAC_EXT_RGMII_MODE);
991 val &= ~(MAC_RGMII_MODE_RX_INT_B |
992 MAC_RGMII_MODE_RX_QUALITY |
993 MAC_RGMII_MODE_RX_ACTIVITY |
994 MAC_RGMII_MODE_RX_ENG_DET |
995 MAC_RGMII_MODE_TX_ENABLE |
996 MAC_RGMII_MODE_TX_LOWPWR |
997 MAC_RGMII_MODE_TX_RESET);
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800998 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -0700999 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1000 val |= MAC_RGMII_MODE_RX_INT_B |
1001 MAC_RGMII_MODE_RX_QUALITY |
1002 MAC_RGMII_MODE_RX_ACTIVITY |
1003 MAC_RGMII_MODE_RX_ENG_DET;
1004 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1005 val |= MAC_RGMII_MODE_TX_ENABLE |
1006 MAC_RGMII_MODE_TX_LOWPWR |
1007 MAC_RGMII_MODE_TX_RESET;
1008 }
1009 tw32(MAC_EXT_RGMII_MODE, val);
1010}
1011
Matt Carlson158d7ab2008-05-29 01:37:54 -07001012static void tg3_mdio_start(struct tg3 *tp)
1013{
1014 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001015 mutex_lock(&tp->mdio_bus->mdio_lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001016 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_PAUSED;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001017 mutex_unlock(&tp->mdio_bus->mdio_lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001018 }
1019
1020 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1021 tw32_f(MAC_MI_MODE, tp->mi_mode);
1022 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001023
Matt Carlson882e9792009-09-01 13:21:36 +00001024 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
1025 u32 funcnum, is_serdes;
1026
1027 funcnum = tr32(TG3_CPMU_STATUS) & TG3_CPMU_STATUS_PCIE_FUNC;
1028 if (funcnum)
1029 tp->phy_addr = 2;
1030 else
1031 tp->phy_addr = 1;
1032
1033 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1034 if (is_serdes)
1035 tp->phy_addr += 7;
1036 } else
1037 tp->phy_addr = PHY_ADDR;
1038
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001039 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1040 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1041 tg3_mdio_config_5785(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001042}
1043
1044static void tg3_mdio_stop(struct tg3 *tp)
1045{
1046 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001047 mutex_lock(&tp->mdio_bus->mdio_lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001048 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_PAUSED;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001049 mutex_unlock(&tp->mdio_bus->mdio_lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001050 }
1051}
1052
1053static int tg3_mdio_init(struct tg3 *tp)
1054{
1055 int i;
1056 u32 reg;
Matt Carlsona9daf362008-05-25 23:49:44 -07001057 struct phy_device *phydev;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001058
1059 tg3_mdio_start(tp);
1060
1061 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1062 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1063 return 0;
1064
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001065 tp->mdio_bus = mdiobus_alloc();
1066 if (tp->mdio_bus == NULL)
1067 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001068
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001069 tp->mdio_bus->name = "tg3 mdio bus";
1070 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001071 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001072 tp->mdio_bus->priv = tp;
1073 tp->mdio_bus->parent = &tp->pdev->dev;
1074 tp->mdio_bus->read = &tg3_mdio_read;
1075 tp->mdio_bus->write = &tg3_mdio_write;
1076 tp->mdio_bus->reset = &tg3_mdio_reset;
1077 tp->mdio_bus->phy_mask = ~(1 << PHY_ADDR);
1078 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001079
1080 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001081 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001082
1083 /* The bus registration will look for all the PHYs on the mdio bus.
1084 * Unfortunately, it does not ensure the PHY is powered up before
1085 * accessing the PHY ID registers. A chip reset is the
1086 * quickest way to bring the device back to an operational state..
1087 */
1088 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1089 tg3_bmcr_reset(tp);
1090
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001091 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001092 if (i) {
Matt Carlson158d7ab2008-05-29 01:37:54 -07001093 printk(KERN_WARNING "%s: mdiobus_reg failed (0x%x)\n",
1094 tp->dev->name, i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001095 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001096 return i;
1097 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001098
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001099 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001100
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001101 if (!phydev || !phydev->drv) {
1102 printk(KERN_WARNING "%s: No PHY devices\n", tp->dev->name);
1103 mdiobus_unregister(tp->mdio_bus);
1104 mdiobus_free(tp->mdio_bus);
1105 return -ENODEV;
1106 }
1107
1108 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001109 case TG3_PHY_ID_BCM57780:
1110 phydev->interface = PHY_INTERFACE_MODE_GMII;
1111 break;
Matt Carlsona9daf362008-05-25 23:49:44 -07001112 case TG3_PHY_ID_BCM50610:
Matt Carlsona9daf362008-05-25 23:49:44 -07001113 if (tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE)
1114 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1115 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1116 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1117 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1118 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001119 /* fallthru */
1120 case TG3_PHY_ID_RTL8211C:
1121 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001122 break;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001123 case TG3_PHY_ID_RTL8201E:
Matt Carlsona9daf362008-05-25 23:49:44 -07001124 case TG3_PHY_ID_BCMAC131:
1125 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001126 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001127 break;
1128 }
1129
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001130 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1131
1132 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1133 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001134
1135 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001136}
1137
1138static void tg3_mdio_fini(struct tg3 *tp)
1139{
1140 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1141 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001142 mdiobus_unregister(tp->mdio_bus);
1143 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001144 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_PAUSED;
1145 }
1146}
1147
Matt Carlson95e28692008-05-25 23:44:14 -07001148/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001149static inline void tg3_generate_fw_event(struct tg3 *tp)
1150{
1151 u32 val;
1152
1153 val = tr32(GRC_RX_CPU_EVENT);
1154 val |= GRC_RX_CPU_DRIVER_EVENT;
1155 tw32_f(GRC_RX_CPU_EVENT, val);
1156
1157 tp->last_event_jiffies = jiffies;
1158}
1159
1160#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1161
1162/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001163static void tg3_wait_for_event_ack(struct tg3 *tp)
1164{
1165 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001166 unsigned int delay_cnt;
1167 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001168
Matt Carlson4ba526c2008-08-15 14:10:04 -07001169 /* If enough time has passed, no wait is necessary. */
1170 time_remain = (long)(tp->last_event_jiffies + 1 +
1171 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1172 (long)jiffies;
1173 if (time_remain < 0)
1174 return;
1175
1176 /* Check if we can shorten the wait time. */
1177 delay_cnt = jiffies_to_usecs(time_remain);
1178 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1179 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1180 delay_cnt = (delay_cnt >> 3) + 1;
1181
1182 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001183 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1184 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001185 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001186 }
1187}
1188
1189/* tp->lock is held. */
1190static void tg3_ump_link_report(struct tg3 *tp)
1191{
1192 u32 reg;
1193 u32 val;
1194
1195 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1196 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1197 return;
1198
1199 tg3_wait_for_event_ack(tp);
1200
1201 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1202
1203 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1204
1205 val = 0;
1206 if (!tg3_readphy(tp, MII_BMCR, &reg))
1207 val = reg << 16;
1208 if (!tg3_readphy(tp, MII_BMSR, &reg))
1209 val |= (reg & 0xffff);
1210 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1211
1212 val = 0;
1213 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1214 val = reg << 16;
1215 if (!tg3_readphy(tp, MII_LPA, &reg))
1216 val |= (reg & 0xffff);
1217 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1218
1219 val = 0;
1220 if (!(tp->tg3_flags2 & TG3_FLG2_MII_SERDES)) {
1221 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1222 val = reg << 16;
1223 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1224 val |= (reg & 0xffff);
1225 }
1226 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1227
1228 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1229 val = reg << 16;
1230 else
1231 val = 0;
1232 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1233
Matt Carlson4ba526c2008-08-15 14:10:04 -07001234 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001235}
1236
1237static void tg3_link_report(struct tg3 *tp)
1238{
1239 if (!netif_carrier_ok(tp->dev)) {
1240 if (netif_msg_link(tp))
1241 printk(KERN_INFO PFX "%s: Link is down.\n",
1242 tp->dev->name);
1243 tg3_ump_link_report(tp);
1244 } else if (netif_msg_link(tp)) {
1245 printk(KERN_INFO PFX "%s: Link is up at %d Mbps, %s duplex.\n",
1246 tp->dev->name,
1247 (tp->link_config.active_speed == SPEED_1000 ?
1248 1000 :
1249 (tp->link_config.active_speed == SPEED_100 ?
1250 100 : 10)),
1251 (tp->link_config.active_duplex == DUPLEX_FULL ?
1252 "full" : "half"));
1253
1254 printk(KERN_INFO PFX
1255 "%s: Flow control is %s for TX and %s for RX.\n",
1256 tp->dev->name,
Steve Glendinninge18ce342008-12-16 02:00:00 -08001257 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
Matt Carlson95e28692008-05-25 23:44:14 -07001258 "on" : "off",
Steve Glendinninge18ce342008-12-16 02:00:00 -08001259 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
Matt Carlson95e28692008-05-25 23:44:14 -07001260 "on" : "off");
1261 tg3_ump_link_report(tp);
1262 }
1263}
1264
1265static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1266{
1267 u16 miireg;
1268
Steve Glendinninge18ce342008-12-16 02:00:00 -08001269 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001270 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001271 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001272 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001273 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001274 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1275 else
1276 miireg = 0;
1277
1278 return miireg;
1279}
1280
1281static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1282{
1283 u16 miireg;
1284
Steve Glendinninge18ce342008-12-16 02:00:00 -08001285 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001286 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001287 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001288 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001289 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001290 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1291 else
1292 miireg = 0;
1293
1294 return miireg;
1295}
1296
Matt Carlson95e28692008-05-25 23:44:14 -07001297static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1298{
1299 u8 cap = 0;
1300
1301 if (lcladv & ADVERTISE_1000XPAUSE) {
1302 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1303 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001304 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001305 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001306 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001307 } else {
1308 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001309 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001310 }
1311 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1312 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001313 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001314 }
1315
1316 return cap;
1317}
1318
Matt Carlsonf51f3562008-05-25 23:45:08 -07001319static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001320{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001321 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001322 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001323 u32 old_rx_mode = tp->rx_mode;
1324 u32 old_tx_mode = tp->tx_mode;
1325
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001326 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001327 autoneg = tp->mdio_bus->phy_map[PHY_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001328 else
1329 autoneg = tp->link_config.autoneg;
1330
1331 if (autoneg == AUTONEG_ENABLE &&
Matt Carlson95e28692008-05-25 23:44:14 -07001332 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
1333 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001334 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001335 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001336 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001337 } else
1338 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001339
Matt Carlsonf51f3562008-05-25 23:45:08 -07001340 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001341
Steve Glendinninge18ce342008-12-16 02:00:00 -08001342 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001343 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1344 else
1345 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1346
Matt Carlsonf51f3562008-05-25 23:45:08 -07001347 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001348 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001349
Steve Glendinninge18ce342008-12-16 02:00:00 -08001350 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001351 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1352 else
1353 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1354
Matt Carlsonf51f3562008-05-25 23:45:08 -07001355 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001356 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001357}
1358
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001359static void tg3_adjust_link(struct net_device *dev)
1360{
1361 u8 oldflowctrl, linkmesg = 0;
1362 u32 mac_mode, lcl_adv, rmt_adv;
1363 struct tg3 *tp = netdev_priv(dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001364 struct phy_device *phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001365
1366 spin_lock(&tp->lock);
1367
1368 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1369 MAC_MODE_HALF_DUPLEX);
1370
1371 oldflowctrl = tp->link_config.active_flowctrl;
1372
1373 if (phydev->link) {
1374 lcl_adv = 0;
1375 rmt_adv = 0;
1376
1377 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1378 mac_mode |= MAC_MODE_PORT_MODE_MII;
1379 else
1380 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1381
1382 if (phydev->duplex == DUPLEX_HALF)
1383 mac_mode |= MAC_MODE_HALF_DUPLEX;
1384 else {
1385 lcl_adv = tg3_advert_flowctrl_1000T(
1386 tp->link_config.flowctrl);
1387
1388 if (phydev->pause)
1389 rmt_adv = LPA_PAUSE_CAP;
1390 if (phydev->asym_pause)
1391 rmt_adv |= LPA_PAUSE_ASYM;
1392 }
1393
1394 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1395 } else
1396 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1397
1398 if (mac_mode != tp->mac_mode) {
1399 tp->mac_mode = mac_mode;
1400 tw32_f(MAC_MODE, tp->mac_mode);
1401 udelay(40);
1402 }
1403
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001404 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1405 if (phydev->speed == SPEED_10)
1406 tw32(MAC_MI_STAT,
1407 MAC_MI_STAT_10MBPS_MODE |
1408 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1409 else
1410 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1411 }
1412
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001413 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1414 tw32(MAC_TX_LENGTHS,
1415 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1416 (6 << TX_LENGTHS_IPG_SHIFT) |
1417 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1418 else
1419 tw32(MAC_TX_LENGTHS,
1420 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1421 (6 << TX_LENGTHS_IPG_SHIFT) |
1422 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1423
1424 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1425 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1426 phydev->speed != tp->link_config.active_speed ||
1427 phydev->duplex != tp->link_config.active_duplex ||
1428 oldflowctrl != tp->link_config.active_flowctrl)
1429 linkmesg = 1;
1430
1431 tp->link_config.active_speed = phydev->speed;
1432 tp->link_config.active_duplex = phydev->duplex;
1433
1434 spin_unlock(&tp->lock);
1435
1436 if (linkmesg)
1437 tg3_link_report(tp);
1438}
1439
1440static int tg3_phy_init(struct tg3 *tp)
1441{
1442 struct phy_device *phydev;
1443
1444 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED)
1445 return 0;
1446
1447 /* Bring the PHY back to a known state. */
1448 tg3_bmcr_reset(tp);
1449
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001450 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001451
1452 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001453 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001454 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001455 if (IS_ERR(phydev)) {
1456 printk(KERN_ERR "%s: Could not attach to PHY\n", tp->dev->name);
1457 return PTR_ERR(phydev);
1458 }
1459
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001460 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001461 switch (phydev->interface) {
1462 case PHY_INTERFACE_MODE_GMII:
1463 case PHY_INTERFACE_MODE_RGMII:
Matt Carlson321d32a2008-11-21 17:22:19 -08001464 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
1465 phydev->supported &= (PHY_GBIT_FEATURES |
1466 SUPPORTED_Pause |
1467 SUPPORTED_Asym_Pause);
1468 break;
1469 }
1470 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001471 case PHY_INTERFACE_MODE_MII:
1472 phydev->supported &= (PHY_BASIC_FEATURES |
1473 SUPPORTED_Pause |
1474 SUPPORTED_Asym_Pause);
1475 break;
1476 default:
1477 phy_disconnect(tp->mdio_bus->phy_map[PHY_ADDR]);
1478 return -EINVAL;
1479 }
1480
1481 tp->tg3_flags3 |= TG3_FLG3_PHY_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001482
1483 phydev->advertising = phydev->supported;
1484
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001485 return 0;
1486}
1487
1488static void tg3_phy_start(struct tg3 *tp)
1489{
1490 struct phy_device *phydev;
1491
1492 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1493 return;
1494
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001495 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001496
1497 if (tp->link_config.phy_is_low_power) {
1498 tp->link_config.phy_is_low_power = 0;
1499 phydev->speed = tp->link_config.orig_speed;
1500 phydev->duplex = tp->link_config.orig_duplex;
1501 phydev->autoneg = tp->link_config.orig_autoneg;
1502 phydev->advertising = tp->link_config.orig_advertising;
1503 }
1504
1505 phy_start(phydev);
1506
1507 phy_start_aneg(phydev);
1508}
1509
1510static void tg3_phy_stop(struct tg3 *tp)
1511{
1512 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1513 return;
1514
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001515 phy_stop(tp->mdio_bus->phy_map[PHY_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001516}
1517
1518static void tg3_phy_fini(struct tg3 *tp)
1519{
1520 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001521 phy_disconnect(tp->mdio_bus->phy_map[PHY_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001522 tp->tg3_flags3 &= ~TG3_FLG3_PHY_CONNECTED;
1523 }
1524}
1525
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001526static void tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1527{
1528 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1529 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1530}
1531
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001532static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1533{
1534 u32 phytest;
1535
1536 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1537 u32 phy;
1538
1539 tg3_writephy(tp, MII_TG3_FET_TEST,
1540 phytest | MII_TG3_FET_SHADOW_EN);
1541 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1542 if (enable)
1543 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1544 else
1545 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1546 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1547 }
1548 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1549 }
1550}
1551
Matt Carlson6833c042008-11-21 17:18:59 -08001552static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1553{
1554 u32 reg;
1555
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001556 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson6833c042008-11-21 17:18:59 -08001557 return;
1558
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001559 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
1560 tg3_phy_fet_toggle_apd(tp, enable);
1561 return;
1562 }
1563
Matt Carlson6833c042008-11-21 17:18:59 -08001564 reg = MII_TG3_MISC_SHDW_WREN |
1565 MII_TG3_MISC_SHDW_SCR5_SEL |
1566 MII_TG3_MISC_SHDW_SCR5_LPED |
1567 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1568 MII_TG3_MISC_SHDW_SCR5_SDTL |
1569 MII_TG3_MISC_SHDW_SCR5_C125OE;
1570 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1571 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1572
1573 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1574
1575
1576 reg = MII_TG3_MISC_SHDW_WREN |
1577 MII_TG3_MISC_SHDW_APD_SEL |
1578 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1579 if (enable)
1580 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1581
1582 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1583}
1584
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001585static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1586{
1587 u32 phy;
1588
1589 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1590 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
1591 return;
1592
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001593 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001594 u32 ephy;
1595
Matt Carlson535ef6e2009-08-25 10:09:36 +00001596 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1597 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1598
1599 tg3_writephy(tp, MII_TG3_FET_TEST,
1600 ephy | MII_TG3_FET_SHADOW_EN);
1601 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001602 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001603 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001604 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001605 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1606 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001607 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001608 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001609 }
1610 } else {
1611 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1612 MII_TG3_AUXCTL_SHDWSEL_MISC;
1613 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1614 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1615 if (enable)
1616 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1617 else
1618 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1619 phy |= MII_TG3_AUXCTL_MISC_WREN;
1620 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1621 }
1622 }
1623}
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625static void tg3_phy_set_wirespeed(struct tg3 *tp)
1626{
1627 u32 val;
1628
1629 if (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED)
1630 return;
1631
1632 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1633 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1634 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1635 (val | (1 << 15) | (1 << 4)));
1636}
1637
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001638static void tg3_phy_apply_otp(struct tg3 *tp)
1639{
1640 u32 otp, phy;
1641
1642 if (!tp->phy_otp)
1643 return;
1644
1645 otp = tp->phy_otp;
1646
1647 /* Enable SM_DSP clock and tx 6dB coding. */
1648 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1649 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1650 MII_TG3_AUXCTL_ACTL_TX_6DB;
1651 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1652
1653 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1654 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1655 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1656
1657 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1658 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1659 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1660
1661 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1662 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1663 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1664
1665 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1666 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1667
1668 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1669 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1670
1671 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1672 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1673 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1674
1675 /* Turn off SM_DSP clock. */
1676 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1677 MII_TG3_AUXCTL_ACTL_TX_6DB;
1678 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1679}
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681static int tg3_wait_macro_done(struct tg3 *tp)
1682{
1683 int limit = 100;
1684
1685 while (limit--) {
1686 u32 tmp32;
1687
1688 if (!tg3_readphy(tp, 0x16, &tmp32)) {
1689 if ((tmp32 & 0x1000) == 0)
1690 break;
1691 }
1692 }
Roel Kluind4675b52009-02-12 16:33:27 -08001693 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return -EBUSY;
1695
1696 return 0;
1697}
1698
1699static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1700{
1701 static const u32 test_pat[4][6] = {
1702 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1703 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1704 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1705 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1706 };
1707 int chan;
1708
1709 for (chan = 0; chan < 4; chan++) {
1710 int i;
1711
1712 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1713 (chan * 0x2000) | 0x0200);
1714 tg3_writephy(tp, 0x16, 0x0002);
1715
1716 for (i = 0; i < 6; i++)
1717 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1718 test_pat[chan][i]);
1719
1720 tg3_writephy(tp, 0x16, 0x0202);
1721 if (tg3_wait_macro_done(tp)) {
1722 *resetp = 1;
1723 return -EBUSY;
1724 }
1725
1726 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1727 (chan * 0x2000) | 0x0200);
1728 tg3_writephy(tp, 0x16, 0x0082);
1729 if (tg3_wait_macro_done(tp)) {
1730 *resetp = 1;
1731 return -EBUSY;
1732 }
1733
1734 tg3_writephy(tp, 0x16, 0x0802);
1735 if (tg3_wait_macro_done(tp)) {
1736 *resetp = 1;
1737 return -EBUSY;
1738 }
1739
1740 for (i = 0; i < 6; i += 2) {
1741 u32 low, high;
1742
1743 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1744 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1745 tg3_wait_macro_done(tp)) {
1746 *resetp = 1;
1747 return -EBUSY;
1748 }
1749 low &= 0x7fff;
1750 high &= 0x000f;
1751 if (low != test_pat[chan][i] ||
1752 high != test_pat[chan][i+1]) {
1753 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1754 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1755 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1756
1757 return -EBUSY;
1758 }
1759 }
1760 }
1761
1762 return 0;
1763}
1764
1765static int tg3_phy_reset_chanpat(struct tg3 *tp)
1766{
1767 int chan;
1768
1769 for (chan = 0; chan < 4; chan++) {
1770 int i;
1771
1772 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1773 (chan * 0x2000) | 0x0200);
1774 tg3_writephy(tp, 0x16, 0x0002);
1775 for (i = 0; i < 6; i++)
1776 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
1777 tg3_writephy(tp, 0x16, 0x0202);
1778 if (tg3_wait_macro_done(tp))
1779 return -EBUSY;
1780 }
1781
1782 return 0;
1783}
1784
1785static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1786{
1787 u32 reg32, phy9_orig;
1788 int retries, do_phy_reset, err;
1789
1790 retries = 10;
1791 do_phy_reset = 1;
1792 do {
1793 if (do_phy_reset) {
1794 err = tg3_bmcr_reset(tp);
1795 if (err)
1796 return err;
1797 do_phy_reset = 0;
1798 }
1799
1800 /* Disable transmitter and interrupt. */
1801 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1802 continue;
1803
1804 reg32 |= 0x3000;
1805 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1806
1807 /* Set full-duplex, 1000 mbps. */
1808 tg3_writephy(tp, MII_BMCR,
1809 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1810
1811 /* Set to master mode. */
1812 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1813 continue;
1814
1815 tg3_writephy(tp, MII_TG3_CTRL,
1816 (MII_TG3_CTRL_AS_MASTER |
1817 MII_TG3_CTRL_ENABLE_AS_MASTER));
1818
1819 /* Enable SM_DSP_CLOCK and 6dB. */
1820 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1821
1822 /* Block the PHY control access. */
1823 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1824 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0800);
1825
1826 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1827 if (!err)
1828 break;
1829 } while (--retries);
1830
1831 err = tg3_phy_reset_chanpat(tp);
1832 if (err)
1833 return err;
1834
1835 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1836 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0000);
1837
1838 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
1839 tg3_writephy(tp, 0x16, 0x0000);
1840
1841 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1842 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1843 /* Set Extended packet length bit for jumbo frames */
1844 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
1845 }
1846 else {
1847 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1848 }
1849
1850 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1851
1852 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1853 reg32 &= ~0x3000;
1854 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1855 } else if (!err)
1856 err = -EBUSY;
1857
1858 return err;
1859}
1860
1861/* This will reset the tigon3 PHY if there is no valid
1862 * link unless the FORCE argument is non-zero.
1863 */
1864static int tg3_phy_reset(struct tg3 *tp)
1865{
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001866 u32 cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 u32 phy_status;
1868 int err;
1869
Michael Chan60189dd2006-12-17 17:08:07 -08001870 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1871 u32 val;
1872
1873 val = tr32(GRC_MISC_CFG);
1874 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1875 udelay(40);
1876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 err = tg3_readphy(tp, MII_BMSR, &phy_status);
1878 err |= tg3_readphy(tp, MII_BMSR, &phy_status);
1879 if (err != 0)
1880 return -EBUSY;
1881
Michael Chanc8e1e822006-04-29 18:55:17 -07001882 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
1883 netif_carrier_off(tp->dev);
1884 tg3_link_report(tp);
1885 }
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1888 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
1889 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
1890 err = tg3_phy_reset_5703_4_5(tp);
1891 if (err)
1892 return err;
1893 goto out;
1894 }
1895
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001896 cpmuctrl = 0;
1897 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
1898 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
1899 cpmuctrl = tr32(TG3_CPMU_CTRL);
1900 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
1901 tw32(TG3_CPMU_CTRL,
1902 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
1903 }
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 err = tg3_bmcr_reset(tp);
1906 if (err)
1907 return err;
1908
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001909 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
1910 u32 phy;
1911
1912 phy = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
1913 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, phy);
1914
1915 tw32(TG3_CPMU_CTRL, cpmuctrl);
1916 }
1917
Matt Carlsonbcb37f62008-11-03 16:52:09 -08001918 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
1919 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08001920 u32 val;
1921
1922 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
1923 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
1924 CPMU_LSPD_1000MB_MACCLK_12_5) {
1925 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
1926 udelay(40);
1927 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
1928 }
1929 }
1930
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001931 tg3_phy_apply_otp(tp);
1932
Matt Carlson6833c042008-11-21 17:18:59 -08001933 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
1934 tg3_phy_toggle_apd(tp, true);
1935 else
1936 tg3_phy_toggle_apd(tp, false);
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938out:
1939 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADC_BUG) {
1940 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1941 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
1942 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x2aaa);
1943 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
1944 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0323);
1945 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1946 }
1947 if (tp->tg3_flags2 & TG3_FLG2_PHY_5704_A0_BUG) {
1948 tg3_writephy(tp, 0x1c, 0x8d68);
1949 tg3_writephy(tp, 0x1c, 0x8d68);
1950 }
1951 if (tp->tg3_flags2 & TG3_FLG2_PHY_BER_BUG) {
1952 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1953 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
1954 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x310b);
1955 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
1956 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x9506);
1957 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x401f);
1958 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x14e2);
1959 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1960 }
Michael Chanc424cb22006-04-29 18:56:34 -07001961 else if (tp->tg3_flags2 & TG3_FLG2_PHY_JITTER_BUG) {
1962 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1963 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Michael Chanc1d2a192007-01-08 19:57:20 -08001964 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADJUST_TRIM) {
1965 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
1966 tg3_writephy(tp, MII_TG3_TEST1,
1967 MII_TG3_TEST1_TRIM_EN | 0x4);
1968 } else
1969 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07001970 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 /* Set Extended packet length bit (bit 14) on all chips that */
1973 /* support jumbo frames */
1974 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
1975 /* Cannot do read-modify-write on 5401 */
1976 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00001977 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 u32 phy_reg;
1979
1980 /* Set bit 14 with read-modify-write to preserve other bits */
1981 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
1982 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy_reg))
1983 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy_reg | 0x4000);
1984 }
1985
1986 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
1987 * jumbo frames transmission.
1988 */
Matt Carlson8f666b02009-08-28 13:58:24 +00001989 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 u32 phy_reg;
1991
1992 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &phy_reg))
1993 tg3_writephy(tp, MII_TG3_EXT_CTRL,
1994 phy_reg | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
1995 }
1996
Michael Chan715116a2006-09-27 16:09:25 -07001997 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07001998 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00001999 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002000 }
2001
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002002 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 tg3_phy_set_wirespeed(tp);
2004 return 0;
2005}
2006
2007static void tg3_frob_aux_power(struct tg3 *tp)
2008{
2009 struct tg3 *tp_peer = tp;
2010
Michael Chan9d26e212006-12-07 00:21:14 -08002011 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return;
2013
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00002014 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2015 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
2016 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002017 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002019 dev_peer = pci_get_drvdata(tp->pdev_peer);
Michael Chanbc1c7562006-03-20 17:48:03 -08002020 /* remove_one() may have been run on the peer. */
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002021 if (!dev_peer)
Michael Chanbc1c7562006-03-20 17:48:03 -08002022 tp_peer = tp;
2023 else
2024 tp_peer = netdev_priv(dev_peer);
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
Michael Chan6921d202005-12-13 21:15:53 -08002028 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0 ||
2029 (tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
2030 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2032 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002033 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2034 (GRC_LCLCTRL_GPIO_OE0 |
2035 GRC_LCLCTRL_GPIO_OE1 |
2036 GRC_LCLCTRL_GPIO_OE2 |
2037 GRC_LCLCTRL_GPIO_OUTPUT0 |
2038 GRC_LCLCTRL_GPIO_OUTPUT1),
2039 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002040 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2041 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002042 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2043 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2044 GRC_LCLCTRL_GPIO_OE1 |
2045 GRC_LCLCTRL_GPIO_OE2 |
2046 GRC_LCLCTRL_GPIO_OUTPUT0 |
2047 GRC_LCLCTRL_GPIO_OUTPUT1 |
2048 tp->grc_local_ctrl;
2049 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2050
2051 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2052 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2053
2054 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2055 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 } else {
2057 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002058 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 if (tp_peer != tp &&
2061 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2062 return;
2063
Michael Chandc56b7d2005-12-19 16:26:28 -08002064 /* Workaround to prevent overdrawing Amps. */
2065 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2066 ASIC_REV_5714) {
2067 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002068 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2069 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002070 }
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 /* On 5753 and variants, GPIO2 cannot be used. */
2073 no_gpio2 = tp->nic_sram_data_cfg &
2074 NIC_SRAM_DATA_CFG_NO_GPIO2;
2075
Michael Chandc56b7d2005-12-19 16:26:28 -08002076 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 GRC_LCLCTRL_GPIO_OE1 |
2078 GRC_LCLCTRL_GPIO_OE2 |
2079 GRC_LCLCTRL_GPIO_OUTPUT1 |
2080 GRC_LCLCTRL_GPIO_OUTPUT2;
2081 if (no_gpio2) {
2082 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2083 GRC_LCLCTRL_GPIO_OUTPUT2);
2084 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002085 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2086 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2089
Michael Chanb401e9e2005-12-19 16:27:04 -08002090 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2091 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 if (!no_gpio2) {
2094 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002095 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2096 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
2098 }
2099 } else {
2100 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2101 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2102 if (tp_peer != tp &&
2103 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2104 return;
2105
Michael Chanb401e9e2005-12-19 16:27:04 -08002106 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2107 (GRC_LCLCTRL_GPIO_OE1 |
2108 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Michael Chanb401e9e2005-12-19 16:27:04 -08002110 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2111 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Michael Chanb401e9e2005-12-19 16:27:04 -08002113 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2114 (GRC_LCLCTRL_GPIO_OE1 |
2115 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
2117 }
2118}
2119
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002120static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2121{
2122 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2123 return 1;
2124 else if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411) {
2125 if (speed != SPEED_10)
2126 return 1;
2127 } else if (speed == SPEED_10)
2128 return 1;
2129
2130 return 0;
2131}
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133static int tg3_setup_phy(struct tg3 *, int);
2134
2135#define RESET_KIND_SHUTDOWN 0
2136#define RESET_KIND_INIT 1
2137#define RESET_KIND_SUSPEND 2
2138
2139static void tg3_write_sig_post_reset(struct tg3 *, int);
2140static int tg3_halt_cpu(struct tg3 *, u32);
2141
Matt Carlson0a459aa2008-11-03 16:54:15 -08002142static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002143{
Matt Carlsonce057f02007-11-12 21:08:03 -08002144 u32 val;
2145
Michael Chan51297242007-02-13 12:17:57 -08002146 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
2147 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2148 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2149 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2150
2151 sg_dig_ctrl |=
2152 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2153 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2154 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2155 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002156 return;
Michael Chan51297242007-02-13 12:17:57 -08002157 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002158
Michael Chan60189dd2006-12-17 17:08:07 -08002159 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002160 tg3_bmcr_reset(tp);
2161 val = tr32(GRC_MISC_CFG);
2162 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2163 udelay(40);
2164 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002165 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002166 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2167 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002168
2169 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2170 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2171 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2172 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2173 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002174 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002175
Michael Chan15c3b692006-03-22 01:06:52 -08002176 /* The PHY should not be powered down on some chips because
2177 * of bugs.
2178 */
2179 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2180 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2181 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
2182 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
2183 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002184
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002185 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2186 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002187 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2188 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2189 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2190 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2191 }
2192
Michael Chan15c3b692006-03-22 01:06:52 -08002193 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2194}
2195
Matt Carlson3f007892008-11-03 16:51:36 -08002196/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002197static int tg3_nvram_lock(struct tg3 *tp)
2198{
2199 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2200 int i;
2201
2202 if (tp->nvram_lock_cnt == 0) {
2203 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2204 for (i = 0; i < 8000; i++) {
2205 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2206 break;
2207 udelay(20);
2208 }
2209 if (i == 8000) {
2210 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2211 return -ENODEV;
2212 }
2213 }
2214 tp->nvram_lock_cnt++;
2215 }
2216 return 0;
2217}
2218
2219/* tp->lock is held. */
2220static void tg3_nvram_unlock(struct tg3 *tp)
2221{
2222 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2223 if (tp->nvram_lock_cnt > 0)
2224 tp->nvram_lock_cnt--;
2225 if (tp->nvram_lock_cnt == 0)
2226 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2227 }
2228}
2229
2230/* tp->lock is held. */
2231static void tg3_enable_nvram_access(struct tg3 *tp)
2232{
2233 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
2234 !(tp->tg3_flags2 & TG3_FLG2_PROTECTED_NVRAM)) {
2235 u32 nvaccess = tr32(NVRAM_ACCESS);
2236
2237 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2238 }
2239}
2240
2241/* tp->lock is held. */
2242static void tg3_disable_nvram_access(struct tg3 *tp)
2243{
2244 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
2245 !(tp->tg3_flags2 & TG3_FLG2_PROTECTED_NVRAM)) {
2246 u32 nvaccess = tr32(NVRAM_ACCESS);
2247
2248 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2249 }
2250}
2251
2252static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2253 u32 offset, u32 *val)
2254{
2255 u32 tmp;
2256 int i;
2257
2258 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2259 return -EINVAL;
2260
2261 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2262 EEPROM_ADDR_DEVID_MASK |
2263 EEPROM_ADDR_READ);
2264 tw32(GRC_EEPROM_ADDR,
2265 tmp |
2266 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2267 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2268 EEPROM_ADDR_ADDR_MASK) |
2269 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2270
2271 for (i = 0; i < 1000; i++) {
2272 tmp = tr32(GRC_EEPROM_ADDR);
2273
2274 if (tmp & EEPROM_ADDR_COMPLETE)
2275 break;
2276 msleep(1);
2277 }
2278 if (!(tmp & EEPROM_ADDR_COMPLETE))
2279 return -EBUSY;
2280
Matt Carlson62cedd12009-04-20 14:52:29 -07002281 tmp = tr32(GRC_EEPROM_DATA);
2282
2283 /*
2284 * The data will always be opposite the native endian
2285 * format. Perform a blind byteswap to compensate.
2286 */
2287 *val = swab32(tmp);
2288
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002289 return 0;
2290}
2291
2292#define NVRAM_CMD_TIMEOUT 10000
2293
2294static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2295{
2296 int i;
2297
2298 tw32(NVRAM_CMD, nvram_cmd);
2299 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2300 udelay(10);
2301 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2302 udelay(10);
2303 break;
2304 }
2305 }
2306
2307 if (i == NVRAM_CMD_TIMEOUT)
2308 return -EBUSY;
2309
2310 return 0;
2311}
2312
2313static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2314{
2315 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2316 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2317 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2318 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2319 (tp->nvram_jedecnum == JEDEC_ATMEL))
2320
2321 addr = ((addr / tp->nvram_pagesize) <<
2322 ATMEL_AT45DB0X1B_PAGE_POS) +
2323 (addr % tp->nvram_pagesize);
2324
2325 return addr;
2326}
2327
2328static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2329{
2330 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2331 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2332 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2333 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2334 (tp->nvram_jedecnum == JEDEC_ATMEL))
2335
2336 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2337 tp->nvram_pagesize) +
2338 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2339
2340 return addr;
2341}
2342
Matt Carlsone4f34112009-02-25 14:25:00 +00002343/* NOTE: Data read in from NVRAM is byteswapped according to
2344 * the byteswapping settings for all other register accesses.
2345 * tg3 devices are BE devices, so on a BE machine, the data
2346 * returned will be exactly as it is seen in NVRAM. On a LE
2347 * machine, the 32-bit value will be byteswapped.
2348 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002349static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2350{
2351 int ret;
2352
2353 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2354 return tg3_nvram_read_using_eeprom(tp, offset, val);
2355
2356 offset = tg3_nvram_phys_addr(tp, offset);
2357
2358 if (offset > NVRAM_ADDR_MSK)
2359 return -EINVAL;
2360
2361 ret = tg3_nvram_lock(tp);
2362 if (ret)
2363 return ret;
2364
2365 tg3_enable_nvram_access(tp);
2366
2367 tw32(NVRAM_ADDR, offset);
2368 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2369 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2370
2371 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002372 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002373
2374 tg3_disable_nvram_access(tp);
2375
2376 tg3_nvram_unlock(tp);
2377
2378 return ret;
2379}
2380
Matt Carlsona9dc5292009-02-25 14:25:30 +00002381/* Ensures NVRAM data is in bytestream format. */
2382static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002383{
2384 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002385 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002386 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002387 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002388 return res;
2389}
2390
2391/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002392static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2393{
2394 u32 addr_high, addr_low;
2395 int i;
2396
2397 addr_high = ((tp->dev->dev_addr[0] << 8) |
2398 tp->dev->dev_addr[1]);
2399 addr_low = ((tp->dev->dev_addr[2] << 24) |
2400 (tp->dev->dev_addr[3] << 16) |
2401 (tp->dev->dev_addr[4] << 8) |
2402 (tp->dev->dev_addr[5] << 0));
2403 for (i = 0; i < 4; i++) {
2404 if (i == 1 && skip_mac_1)
2405 continue;
2406 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2407 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2408 }
2409
2410 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2411 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2412 for (i = 0; i < 12; i++) {
2413 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2414 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2415 }
2416 }
2417
2418 addr_high = (tp->dev->dev_addr[0] +
2419 tp->dev->dev_addr[1] +
2420 tp->dev->dev_addr[2] +
2421 tp->dev->dev_addr[3] +
2422 tp->dev->dev_addr[4] +
2423 tp->dev->dev_addr[5]) &
2424 TX_BACKOFF_SEED_MASK;
2425 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2426}
2427
Michael Chanbc1c7562006-03-20 17:48:03 -08002428static int tg3_set_power_state(struct tg3 *tp, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
2430 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002431 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433 /* Make sure register accesses (indirect or otherwise)
2434 * will function correctly.
2435 */
2436 pci_write_config_dword(tp->pdev,
2437 TG3PCI_MISC_HOST_CTRL,
2438 tp->misc_host_ctrl);
2439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 switch (state) {
Michael Chanbc1c7562006-03-20 17:48:03 -08002441 case PCI_D0:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002442 pci_enable_wake(tp->pdev, state, false);
2443 pci_set_power_state(tp->pdev, PCI_D0);
Michael Chan8c6bda12005-04-21 17:09:08 -07002444
Michael Chan9d26e212006-12-07 00:21:14 -08002445 /* Switch out of Vaux if it is a NIC */
2446 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
Michael Chanb401e9e2005-12-19 16:27:04 -08002447 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
2449 return 0;
2450
Michael Chanbc1c7562006-03-20 17:48:03 -08002451 case PCI_D1:
Michael Chanbc1c7562006-03-20 17:48:03 -08002452 case PCI_D2:
Michael Chanbc1c7562006-03-20 17:48:03 -08002453 case PCI_D3hot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 break;
2455
2456 default:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002457 printk(KERN_ERR PFX "%s: Invalid power state (D%d) requested\n",
2458 tp->dev->name, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002460 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002461
2462 /* Restore the CLKREQ setting. */
2463 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2464 u16 lnkctl;
2465
2466 pci_read_config_word(tp->pdev,
2467 tp->pcie_cap + PCI_EXP_LNKCTL,
2468 &lnkctl);
2469 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2470 pci_write_config_word(tp->pdev,
2471 tp->pcie_cap + PCI_EXP_LNKCTL,
2472 lnkctl);
2473 }
2474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2476 tw32(TG3PCI_MISC_HOST_CTRL,
2477 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2478
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002479 device_should_wake = pci_pme_capable(tp->pdev, state) &&
2480 device_may_wakeup(&tp->pdev->dev) &&
2481 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2482
Matt Carlsondd477002008-05-25 23:45:58 -07002483 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002484 do_low_power = false;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002485 if ((tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) &&
2486 !tp->link_config.phy_is_low_power) {
2487 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002488 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002489
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002490 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002491
2492 tp->link_config.phy_is_low_power = 1;
2493
2494 tp->link_config.orig_speed = phydev->speed;
2495 tp->link_config.orig_duplex = phydev->duplex;
2496 tp->link_config.orig_autoneg = phydev->autoneg;
2497 tp->link_config.orig_advertising = phydev->advertising;
2498
2499 advertising = ADVERTISED_TP |
2500 ADVERTISED_Pause |
2501 ADVERTISED_Autoneg |
2502 ADVERTISED_10baseT_Half;
2503
2504 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002505 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002506 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2507 advertising |=
2508 ADVERTISED_100baseT_Half |
2509 ADVERTISED_100baseT_Full |
2510 ADVERTISED_10baseT_Full;
2511 else
2512 advertising |= ADVERTISED_10baseT_Full;
2513 }
2514
2515 phydev->advertising = advertising;
2516
2517 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002518
2519 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
2520 if (phyid != TG3_PHY_ID_BCMAC131) {
2521 phyid &= TG3_PHY_OUI_MASK;
Roel Kluinf72b5342009-02-18 17:42:42 -08002522 if (phyid == TG3_PHY_OUI_1 ||
2523 phyid == TG3_PHY_OUI_2 ||
Matt Carlson0a459aa2008-11-03 16:54:15 -08002524 phyid == TG3_PHY_OUI_3)
2525 do_low_power = true;
2526 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002527 }
Matt Carlsondd477002008-05-25 23:45:58 -07002528 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002529 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002530
Matt Carlsondd477002008-05-25 23:45:58 -07002531 if (tp->link_config.phy_is_low_power == 0) {
2532 tp->link_config.phy_is_low_power = 1;
2533 tp->link_config.orig_speed = tp->link_config.speed;
2534 tp->link_config.orig_duplex = tp->link_config.duplex;
2535 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Matt Carlsondd477002008-05-25 23:45:58 -07002538 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
2539 tp->link_config.speed = SPEED_10;
2540 tp->link_config.duplex = DUPLEX_HALF;
2541 tp->link_config.autoneg = AUTONEG_ENABLE;
2542 tg3_setup_phy(tp, 0);
2543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 }
2545
Michael Chanb5d37722006-09-27 16:06:21 -07002546 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2547 u32 val;
2548
2549 val = tr32(GRC_VCPU_EXT_CTRL);
2550 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2551 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002552 int i;
2553 u32 val;
2554
2555 for (i = 0; i < 200; i++) {
2556 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2557 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2558 break;
2559 msleep(1);
2560 }
2561 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002562 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2563 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2564 WOL_DRV_STATE_SHUTDOWN |
2565 WOL_DRV_WOL |
2566 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002567
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002568 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 u32 mac_mode;
2570
2571 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002572 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002573 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2574 udelay(40);
2575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Michael Chan3f7045c2006-09-27 16:02:29 -07002577 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
2578 mac_mode = MAC_MODE_PORT_MODE_GMII;
2579 else
2580 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002582 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2583 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2584 ASIC_REV_5700) {
2585 u32 speed = (tp->tg3_flags &
2586 TG3_FLAG_WOL_SPEED_100MB) ?
2587 SPEED_100 : SPEED_10;
2588 if (tg3_5700_link_polarity(tp, speed))
2589 mac_mode |= MAC_MODE_LINK_POLARITY;
2590 else
2591 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 } else {
2594 mac_mode = MAC_MODE_PORT_MODE_TBI;
2595 }
2596
John W. Linvillecbf46852005-04-21 17:01:29 -07002597 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 tw32(MAC_LED_CTRL, tp->led_ctrl);
2599
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002600 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2601 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2602 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2603 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2604 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2605 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Matt Carlson3bda1252008-08-15 14:08:22 -07002607 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
2608 mac_mode |= tp->mac_mode &
2609 (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
2610 if (mac_mode & MAC_MODE_APE_TX_EN)
2611 mac_mode |= MAC_MODE_TDE_ENABLE;
2612 }
2613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 tw32_f(MAC_MODE, mac_mode);
2615 udelay(100);
2616
2617 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2618 udelay(10);
2619 }
2620
2621 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2622 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2623 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2624 u32 base_val;
2625
2626 base_val = tp->pci_clock_ctrl;
2627 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2628 CLOCK_CTRL_TXCLK_DISABLE);
2629
Michael Chanb401e9e2005-12-19 16:27:04 -08002630 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2631 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002632 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002633 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002634 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002635 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002636 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2638 u32 newbits1, newbits2;
2639
2640 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2641 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2642 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2643 CLOCK_CTRL_TXCLK_DISABLE |
2644 CLOCK_CTRL_ALTCLK);
2645 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2646 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2647 newbits1 = CLOCK_CTRL_625_CORE;
2648 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2649 } else {
2650 newbits1 = CLOCK_CTRL_ALTCLK;
2651 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2652 }
2653
Michael Chanb401e9e2005-12-19 16:27:04 -08002654 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2655 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Michael Chanb401e9e2005-12-19 16:27:04 -08002657 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2658 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
2660 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2661 u32 newbits3;
2662
2663 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2664 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2665 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2666 CLOCK_CTRL_TXCLK_DISABLE |
2667 CLOCK_CTRL_44MHZ_CORE);
2668 } else {
2669 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2670 }
2671
Michael Chanb401e9e2005-12-19 16:27:04 -08002672 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2673 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 }
2675 }
2676
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002677 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002678 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002679 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 tg3_frob_aux_power(tp);
2682
2683 /* Workaround for unstable PLL clock */
2684 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2685 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2686 u32 val = tr32(0x7d00);
2687
2688 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2689 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002690 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002691 int err;
2692
2693 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002695 if (!err)
2696 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
2699
Michael Chanbbadf502006-04-06 21:46:34 -07002700 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2701
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002702 if (device_should_wake)
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002703 pci_enable_wake(tp->pdev, state, true);
2704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 /* Finally, set the new power state. */
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002706 pci_set_power_state(tp->pdev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 return 0;
2709}
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2712{
2713 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2714 case MII_TG3_AUX_STAT_10HALF:
2715 *speed = SPEED_10;
2716 *duplex = DUPLEX_HALF;
2717 break;
2718
2719 case MII_TG3_AUX_STAT_10FULL:
2720 *speed = SPEED_10;
2721 *duplex = DUPLEX_FULL;
2722 break;
2723
2724 case MII_TG3_AUX_STAT_100HALF:
2725 *speed = SPEED_100;
2726 *duplex = DUPLEX_HALF;
2727 break;
2728
2729 case MII_TG3_AUX_STAT_100FULL:
2730 *speed = SPEED_100;
2731 *duplex = DUPLEX_FULL;
2732 break;
2733
2734 case MII_TG3_AUX_STAT_1000HALF:
2735 *speed = SPEED_1000;
2736 *duplex = DUPLEX_HALF;
2737 break;
2738
2739 case MII_TG3_AUX_STAT_1000FULL:
2740 *speed = SPEED_1000;
2741 *duplex = DUPLEX_FULL;
2742 break;
2743
2744 default:
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002745 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002746 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2747 SPEED_10;
2748 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2749 DUPLEX_HALF;
2750 break;
2751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 *speed = SPEED_INVALID;
2753 *duplex = DUPLEX_INVALID;
2754 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756}
2757
2758static void tg3_phy_copper_begin(struct tg3 *tp)
2759{
2760 u32 new_adv;
2761 int i;
2762
2763 if (tp->link_config.phy_is_low_power) {
2764 /* Entering low power mode. Disable gigabit and
2765 * 100baseT advertisements.
2766 */
2767 tg3_writephy(tp, MII_TG3_CTRL, 0);
2768
2769 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2770 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2771 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2772 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2773
2774 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2775 } else if (tp->link_config.speed == SPEED_INVALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
2777 tp->link_config.advertising &=
2778 ~(ADVERTISED_1000baseT_Half |
2779 ADVERTISED_1000baseT_Full);
2780
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002781 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2783 new_adv |= ADVERTISE_10HALF;
2784 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2785 new_adv |= ADVERTISE_10FULL;
2786 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2787 new_adv |= ADVERTISE_100HALF;
2788 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2789 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002790
2791 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2794
2795 if (tp->link_config.advertising &
2796 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2797 new_adv = 0;
2798 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2799 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2800 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2801 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
2802 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY) &&
2803 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2804 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2805 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2806 MII_TG3_CTRL_ENABLE_AS_MASTER);
2807 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2808 } else {
2809 tg3_writephy(tp, MII_TG3_CTRL, 0);
2810 }
2811 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002812 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2813 new_adv |= ADVERTISE_CSMA;
2814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 /* Asking for a specific link mode. */
2816 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2818
2819 if (tp->link_config.duplex == DUPLEX_FULL)
2820 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2821 else
2822 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2823 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2824 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2825 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2826 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 if (tp->link_config.speed == SPEED_100) {
2829 if (tp->link_config.duplex == DUPLEX_FULL)
2830 new_adv |= ADVERTISE_100FULL;
2831 else
2832 new_adv |= ADVERTISE_100HALF;
2833 } else {
2834 if (tp->link_config.duplex == DUPLEX_FULL)
2835 new_adv |= ADVERTISE_10FULL;
2836 else
2837 new_adv |= ADVERTISE_10HALF;
2838 }
2839 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002840
2841 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002843
2844 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
2846
2847 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
2848 tp->link_config.speed != SPEED_INVALID) {
2849 u32 bmcr, orig_bmcr;
2850
2851 tp->link_config.active_speed = tp->link_config.speed;
2852 tp->link_config.active_duplex = tp->link_config.duplex;
2853
2854 bmcr = 0;
2855 switch (tp->link_config.speed) {
2856 default:
2857 case SPEED_10:
2858 break;
2859
2860 case SPEED_100:
2861 bmcr |= BMCR_SPEED100;
2862 break;
2863
2864 case SPEED_1000:
2865 bmcr |= TG3_BMCR_SPEED1000;
2866 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869 if (tp->link_config.duplex == DUPLEX_FULL)
2870 bmcr |= BMCR_FULLDPLX;
2871
2872 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
2873 (bmcr != orig_bmcr)) {
2874 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
2875 for (i = 0; i < 1500; i++) {
2876 u32 tmp;
2877
2878 udelay(10);
2879 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
2880 tg3_readphy(tp, MII_BMSR, &tmp))
2881 continue;
2882 if (!(tmp & BMSR_LSTATUS)) {
2883 udelay(40);
2884 break;
2885 }
2886 }
2887 tg3_writephy(tp, MII_BMCR, bmcr);
2888 udelay(40);
2889 }
2890 } else {
2891 tg3_writephy(tp, MII_BMCR,
2892 BMCR_ANENABLE | BMCR_ANRESTART);
2893 }
2894}
2895
2896static int tg3_init_5401phy_dsp(struct tg3 *tp)
2897{
2898 int err;
2899
2900 /* Turn off tap power management. */
2901 /* Set Extended packet length bit */
2902 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
2903
2904 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0012);
2905 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1804);
2906
2907 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0013);
2908 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1204);
2909
2910 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2911 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0132);
2912
2913 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2914 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0232);
2915
2916 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2917 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0a20);
2918
2919 udelay(40);
2920
2921 return err;
2922}
2923
Michael Chan3600d912006-12-07 00:21:48 -08002924static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925{
Michael Chan3600d912006-12-07 00:21:48 -08002926 u32 adv_reg, all_mask = 0;
2927
2928 if (mask & ADVERTISED_10baseT_Half)
2929 all_mask |= ADVERTISE_10HALF;
2930 if (mask & ADVERTISED_10baseT_Full)
2931 all_mask |= ADVERTISE_10FULL;
2932 if (mask & ADVERTISED_100baseT_Half)
2933 all_mask |= ADVERTISE_100HALF;
2934 if (mask & ADVERTISED_100baseT_Full)
2935 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
2937 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
2938 return 0;
2939
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 if ((adv_reg & all_mask) != all_mask)
2941 return 0;
2942 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
2943 u32 tg3_ctrl;
2944
Michael Chan3600d912006-12-07 00:21:48 -08002945 all_mask = 0;
2946 if (mask & ADVERTISED_1000baseT_Half)
2947 all_mask |= ADVERTISE_1000HALF;
2948 if (mask & ADVERTISED_1000baseT_Full)
2949 all_mask |= ADVERTISE_1000FULL;
2950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
2952 return 0;
2953
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 if ((tg3_ctrl & all_mask) != all_mask)
2955 return 0;
2956 }
2957 return 1;
2958}
2959
Matt Carlsonef167e22007-12-20 20:10:01 -08002960static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
2961{
2962 u32 curadv, reqadv;
2963
2964 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
2965 return 1;
2966
2967 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
2968 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2969
2970 if (tp->link_config.active_duplex == DUPLEX_FULL) {
2971 if (curadv != reqadv)
2972 return 0;
2973
2974 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
2975 tg3_readphy(tp, MII_LPA, rmtadv);
2976 } else {
2977 /* Reprogram the advertisement register, even if it
2978 * does not affect the current link. If the link
2979 * gets renegotiated in the future, we can save an
2980 * additional renegotiation cycle by advertising
2981 * it correctly in the first place.
2982 */
2983 if (curadv != reqadv) {
2984 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
2985 ADVERTISE_PAUSE_ASYM);
2986 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
2987 }
2988 }
2989
2990 return 1;
2991}
2992
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
2994{
2995 int current_link_up;
2996 u32 bmsr, dummy;
Matt Carlsonef167e22007-12-20 20:10:01 -08002997 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 u16 current_speed;
2999 u8 current_duplex;
3000 int i, err;
3001
3002 tw32(MAC_EVENT, 0);
3003
3004 tw32_f(MAC_STATUS,
3005 (MAC_STATUS_SYNC_CHANGED |
3006 MAC_STATUS_CFG_CHANGED |
3007 MAC_STATUS_MI_COMPLETION |
3008 MAC_STATUS_LNKSTATE_CHANGED));
3009 udelay(40);
3010
Matt Carlson8ef21422008-05-02 16:47:53 -07003011 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3012 tw32_f(MAC_MI_MODE,
3013 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3014 udelay(80);
3015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
3017 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3018
3019 /* Some third-party PHYs need to be reset on link going
3020 * down.
3021 */
3022 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3023 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3024 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3025 netif_carrier_ok(tp->dev)) {
3026 tg3_readphy(tp, MII_BMSR, &bmsr);
3027 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3028 !(bmsr & BMSR_LSTATUS))
3029 force_reset = 1;
3030 }
3031 if (force_reset)
3032 tg3_phy_reset(tp);
3033
3034 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
3035 tg3_readphy(tp, MII_BMSR, &bmsr);
3036 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3037 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3038 bmsr = 0;
3039
3040 if (!(bmsr & BMSR_LSTATUS)) {
3041 err = tg3_init_5401phy_dsp(tp);
3042 if (err)
3043 return err;
3044
3045 tg3_readphy(tp, MII_BMSR, &bmsr);
3046 for (i = 0; i < 1000; i++) {
3047 udelay(10);
3048 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3049 (bmsr & BMSR_LSTATUS)) {
3050 udelay(40);
3051 break;
3052 }
3053 }
3054
3055 if ((tp->phy_id & PHY_ID_REV_MASK) == PHY_REV_BCM5401_B0 &&
3056 !(bmsr & BMSR_LSTATUS) &&
3057 tp->link_config.active_speed == SPEED_1000) {
3058 err = tg3_phy_reset(tp);
3059 if (!err)
3060 err = tg3_init_5401phy_dsp(tp);
3061 if (err)
3062 return err;
3063 }
3064 }
3065 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3066 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3067 /* 5701 {A0,B0} CRC bug workaround */
3068 tg3_writephy(tp, 0x15, 0x0a75);
3069 tg3_writephy(tp, 0x1c, 0x8c68);
3070 tg3_writephy(tp, 0x1c, 0x8d68);
3071 tg3_writephy(tp, 0x1c, 0x8c68);
3072 }
3073
3074 /* Clear pending interrupts... */
3075 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3076 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3077
3078 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT)
3079 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003080 else if (!(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3082
3083 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3084 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3085 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3086 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3087 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3088 else
3089 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3090 }
3091
3092 current_link_up = 0;
3093 current_speed = SPEED_INVALID;
3094 current_duplex = DUPLEX_INVALID;
3095
3096 if (tp->tg3_flags2 & TG3_FLG2_CAPACITIVE_COUPLING) {
3097 u32 val;
3098
3099 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3100 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3101 if (!(val & (1 << 10))) {
3102 val |= (1 << 10);
3103 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3104 goto relink;
3105 }
3106 }
3107
3108 bmsr = 0;
3109 for (i = 0; i < 100; i++) {
3110 tg3_readphy(tp, MII_BMSR, &bmsr);
3111 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3112 (bmsr & BMSR_LSTATUS))
3113 break;
3114 udelay(40);
3115 }
3116
3117 if (bmsr & BMSR_LSTATUS) {
3118 u32 aux_stat, bmcr;
3119
3120 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3121 for (i = 0; i < 2000; i++) {
3122 udelay(10);
3123 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3124 aux_stat)
3125 break;
3126 }
3127
3128 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3129 &current_speed,
3130 &current_duplex);
3131
3132 bmcr = 0;
3133 for (i = 0; i < 200; i++) {
3134 tg3_readphy(tp, MII_BMCR, &bmcr);
3135 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3136 continue;
3137 if (bmcr && bmcr != 0x7fff)
3138 break;
3139 udelay(10);
3140 }
3141
Matt Carlsonef167e22007-12-20 20:10:01 -08003142 lcl_adv = 0;
3143 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Matt Carlsonef167e22007-12-20 20:10:01 -08003145 tp->link_config.active_speed = current_speed;
3146 tp->link_config.active_duplex = current_duplex;
3147
3148 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3149 if ((bmcr & BMCR_ANENABLE) &&
3150 tg3_copper_is_advertising_all(tp,
3151 tp->link_config.advertising)) {
3152 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3153 &rmt_adv))
3154 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 }
3156 } else {
3157 if (!(bmcr & BMCR_ANENABLE) &&
3158 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003159 tp->link_config.duplex == current_duplex &&
3160 tp->link_config.flowctrl ==
3161 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 }
3164 }
3165
Matt Carlsonef167e22007-12-20 20:10:01 -08003166 if (current_link_up == 1 &&
3167 tp->link_config.active_duplex == DUPLEX_FULL)
3168 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 }
3170
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171relink:
Michael Chan6921d202005-12-13 21:15:53 -08003172 if (current_link_up == 0 || tp->link_config.phy_is_low_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 u32 tmp;
3174
3175 tg3_phy_copper_begin(tp);
3176
3177 tg3_readphy(tp, MII_BMSR, &tmp);
3178 if (!tg3_readphy(tp, MII_BMSR, &tmp) &&
3179 (tmp & BMSR_LSTATUS))
3180 current_link_up = 1;
3181 }
3182
3183 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3184 if (current_link_up == 1) {
3185 if (tp->link_config.active_speed == SPEED_100 ||
3186 tp->link_config.active_speed == SPEED_10)
3187 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3188 else
3189 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003190 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)
3191 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3192 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3194
3195 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3196 if (tp->link_config.active_duplex == DUPLEX_HALF)
3197 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003200 if (current_link_up == 1 &&
3201 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003203 else
3204 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 }
3206
3207 /* ??? Without this setting Netgear GA302T PHY does not
3208 * ??? send/receive packets...
3209 */
3210 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411 &&
3211 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3212 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3213 tw32_f(MAC_MI_MODE, tp->mi_mode);
3214 udelay(80);
3215 }
3216
3217 tw32_f(MAC_MODE, tp->mac_mode);
3218 udelay(40);
3219
3220 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3221 /* Polled via timer. */
3222 tw32_f(MAC_EVENT, 0);
3223 } else {
3224 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3225 }
3226 udelay(40);
3227
3228 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3229 current_link_up == 1 &&
3230 tp->link_config.active_speed == SPEED_1000 &&
3231 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3232 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3233 udelay(120);
3234 tw32_f(MAC_STATUS,
3235 (MAC_STATUS_SYNC_CHANGED |
3236 MAC_STATUS_CFG_CHANGED));
3237 udelay(40);
3238 tg3_write_mem(tp,
3239 NIC_SRAM_FIRMWARE_MBOX,
3240 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3241 }
3242
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003243 /* Prevent send BD corruption. */
3244 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3245 u16 oldlnkctl, newlnkctl;
3246
3247 pci_read_config_word(tp->pdev,
3248 tp->pcie_cap + PCI_EXP_LNKCTL,
3249 &oldlnkctl);
3250 if (tp->link_config.active_speed == SPEED_100 ||
3251 tp->link_config.active_speed == SPEED_10)
3252 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3253 else
3254 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3255 if (newlnkctl != oldlnkctl)
3256 pci_write_config_word(tp->pdev,
3257 tp->pcie_cap + PCI_EXP_LNKCTL,
3258 newlnkctl);
Matt Carlson255ca312009-08-25 10:07:27 +00003259 } else if (tp->tg3_flags3 & TG3_FLG3_TOGGLE_10_100_L1PLLPD) {
3260 u32 newreg, oldreg = tr32(TG3_PCIE_LNKCTL);
3261 if (tp->link_config.active_speed == SPEED_100 ||
3262 tp->link_config.active_speed == SPEED_10)
3263 newreg = oldreg & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
3264 else
3265 newreg = oldreg | TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
3266 if (newreg != oldreg)
3267 tw32(TG3_PCIE_LNKCTL, newreg);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003268 }
3269
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 if (current_link_up != netif_carrier_ok(tp->dev)) {
3271 if (current_link_up)
3272 netif_carrier_on(tp->dev);
3273 else
3274 netif_carrier_off(tp->dev);
3275 tg3_link_report(tp);
3276 }
3277
3278 return 0;
3279}
3280
3281struct tg3_fiber_aneginfo {
3282 int state;
3283#define ANEG_STATE_UNKNOWN 0
3284#define ANEG_STATE_AN_ENABLE 1
3285#define ANEG_STATE_RESTART_INIT 2
3286#define ANEG_STATE_RESTART 3
3287#define ANEG_STATE_DISABLE_LINK_OK 4
3288#define ANEG_STATE_ABILITY_DETECT_INIT 5
3289#define ANEG_STATE_ABILITY_DETECT 6
3290#define ANEG_STATE_ACK_DETECT_INIT 7
3291#define ANEG_STATE_ACK_DETECT 8
3292#define ANEG_STATE_COMPLETE_ACK_INIT 9
3293#define ANEG_STATE_COMPLETE_ACK 10
3294#define ANEG_STATE_IDLE_DETECT_INIT 11
3295#define ANEG_STATE_IDLE_DETECT 12
3296#define ANEG_STATE_LINK_OK 13
3297#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3298#define ANEG_STATE_NEXT_PAGE_WAIT 15
3299
3300 u32 flags;
3301#define MR_AN_ENABLE 0x00000001
3302#define MR_RESTART_AN 0x00000002
3303#define MR_AN_COMPLETE 0x00000004
3304#define MR_PAGE_RX 0x00000008
3305#define MR_NP_LOADED 0x00000010
3306#define MR_TOGGLE_TX 0x00000020
3307#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3308#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3309#define MR_LP_ADV_SYM_PAUSE 0x00000100
3310#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3311#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3312#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3313#define MR_LP_ADV_NEXT_PAGE 0x00001000
3314#define MR_TOGGLE_RX 0x00002000
3315#define MR_NP_RX 0x00004000
3316
3317#define MR_LINK_OK 0x80000000
3318
3319 unsigned long link_time, cur_time;
3320
3321 u32 ability_match_cfg;
3322 int ability_match_count;
3323
3324 char ability_match, idle_match, ack_match;
3325
3326 u32 txconfig, rxconfig;
3327#define ANEG_CFG_NP 0x00000080
3328#define ANEG_CFG_ACK 0x00000040
3329#define ANEG_CFG_RF2 0x00000020
3330#define ANEG_CFG_RF1 0x00000010
3331#define ANEG_CFG_PS2 0x00000001
3332#define ANEG_CFG_PS1 0x00008000
3333#define ANEG_CFG_HD 0x00004000
3334#define ANEG_CFG_FD 0x00002000
3335#define ANEG_CFG_INVAL 0x00001f06
3336
3337};
3338#define ANEG_OK 0
3339#define ANEG_DONE 1
3340#define ANEG_TIMER_ENAB 2
3341#define ANEG_FAILED -1
3342
3343#define ANEG_STATE_SETTLE_TIME 10000
3344
3345static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3346 struct tg3_fiber_aneginfo *ap)
3347{
Matt Carlson5be73b42007-12-20 20:09:29 -08003348 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 unsigned long delta;
3350 u32 rx_cfg_reg;
3351 int ret;
3352
3353 if (ap->state == ANEG_STATE_UNKNOWN) {
3354 ap->rxconfig = 0;
3355 ap->link_time = 0;
3356 ap->cur_time = 0;
3357 ap->ability_match_cfg = 0;
3358 ap->ability_match_count = 0;
3359 ap->ability_match = 0;
3360 ap->idle_match = 0;
3361 ap->ack_match = 0;
3362 }
3363 ap->cur_time++;
3364
3365 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3366 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3367
3368 if (rx_cfg_reg != ap->ability_match_cfg) {
3369 ap->ability_match_cfg = rx_cfg_reg;
3370 ap->ability_match = 0;
3371 ap->ability_match_count = 0;
3372 } else {
3373 if (++ap->ability_match_count > 1) {
3374 ap->ability_match = 1;
3375 ap->ability_match_cfg = rx_cfg_reg;
3376 }
3377 }
3378 if (rx_cfg_reg & ANEG_CFG_ACK)
3379 ap->ack_match = 1;
3380 else
3381 ap->ack_match = 0;
3382
3383 ap->idle_match = 0;
3384 } else {
3385 ap->idle_match = 1;
3386 ap->ability_match_cfg = 0;
3387 ap->ability_match_count = 0;
3388 ap->ability_match = 0;
3389 ap->ack_match = 0;
3390
3391 rx_cfg_reg = 0;
3392 }
3393
3394 ap->rxconfig = rx_cfg_reg;
3395 ret = ANEG_OK;
3396
3397 switch(ap->state) {
3398 case ANEG_STATE_UNKNOWN:
3399 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3400 ap->state = ANEG_STATE_AN_ENABLE;
3401
3402 /* fallthru */
3403 case ANEG_STATE_AN_ENABLE:
3404 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3405 if (ap->flags & MR_AN_ENABLE) {
3406 ap->link_time = 0;
3407 ap->cur_time = 0;
3408 ap->ability_match_cfg = 0;
3409 ap->ability_match_count = 0;
3410 ap->ability_match = 0;
3411 ap->idle_match = 0;
3412 ap->ack_match = 0;
3413
3414 ap->state = ANEG_STATE_RESTART_INIT;
3415 } else {
3416 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3417 }
3418 break;
3419
3420 case ANEG_STATE_RESTART_INIT:
3421 ap->link_time = ap->cur_time;
3422 ap->flags &= ~(MR_NP_LOADED);
3423 ap->txconfig = 0;
3424 tw32(MAC_TX_AUTO_NEG, 0);
3425 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3426 tw32_f(MAC_MODE, tp->mac_mode);
3427 udelay(40);
3428
3429 ret = ANEG_TIMER_ENAB;
3430 ap->state = ANEG_STATE_RESTART;
3431
3432 /* fallthru */
3433 case ANEG_STATE_RESTART:
3434 delta = ap->cur_time - ap->link_time;
3435 if (delta > ANEG_STATE_SETTLE_TIME) {
3436 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
3437 } else {
3438 ret = ANEG_TIMER_ENAB;
3439 }
3440 break;
3441
3442 case ANEG_STATE_DISABLE_LINK_OK:
3443 ret = ANEG_DONE;
3444 break;
3445
3446 case ANEG_STATE_ABILITY_DETECT_INIT:
3447 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003448 ap->txconfig = ANEG_CFG_FD;
3449 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3450 if (flowctrl & ADVERTISE_1000XPAUSE)
3451 ap->txconfig |= ANEG_CFG_PS1;
3452 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3453 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3455 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3456 tw32_f(MAC_MODE, tp->mac_mode);
3457 udelay(40);
3458
3459 ap->state = ANEG_STATE_ABILITY_DETECT;
3460 break;
3461
3462 case ANEG_STATE_ABILITY_DETECT:
3463 if (ap->ability_match != 0 && ap->rxconfig != 0) {
3464 ap->state = ANEG_STATE_ACK_DETECT_INIT;
3465 }
3466 break;
3467
3468 case ANEG_STATE_ACK_DETECT_INIT:
3469 ap->txconfig |= ANEG_CFG_ACK;
3470 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3471 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3472 tw32_f(MAC_MODE, tp->mac_mode);
3473 udelay(40);
3474
3475 ap->state = ANEG_STATE_ACK_DETECT;
3476
3477 /* fallthru */
3478 case ANEG_STATE_ACK_DETECT:
3479 if (ap->ack_match != 0) {
3480 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3481 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3482 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3483 } else {
3484 ap->state = ANEG_STATE_AN_ENABLE;
3485 }
3486 } else if (ap->ability_match != 0 &&
3487 ap->rxconfig == 0) {
3488 ap->state = ANEG_STATE_AN_ENABLE;
3489 }
3490 break;
3491
3492 case ANEG_STATE_COMPLETE_ACK_INIT:
3493 if (ap->rxconfig & ANEG_CFG_INVAL) {
3494 ret = ANEG_FAILED;
3495 break;
3496 }
3497 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3498 MR_LP_ADV_HALF_DUPLEX |
3499 MR_LP_ADV_SYM_PAUSE |
3500 MR_LP_ADV_ASYM_PAUSE |
3501 MR_LP_ADV_REMOTE_FAULT1 |
3502 MR_LP_ADV_REMOTE_FAULT2 |
3503 MR_LP_ADV_NEXT_PAGE |
3504 MR_TOGGLE_RX |
3505 MR_NP_RX);
3506 if (ap->rxconfig & ANEG_CFG_FD)
3507 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3508 if (ap->rxconfig & ANEG_CFG_HD)
3509 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3510 if (ap->rxconfig & ANEG_CFG_PS1)
3511 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3512 if (ap->rxconfig & ANEG_CFG_PS2)
3513 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3514 if (ap->rxconfig & ANEG_CFG_RF1)
3515 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3516 if (ap->rxconfig & ANEG_CFG_RF2)
3517 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3518 if (ap->rxconfig & ANEG_CFG_NP)
3519 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3520
3521 ap->link_time = ap->cur_time;
3522
3523 ap->flags ^= (MR_TOGGLE_TX);
3524 if (ap->rxconfig & 0x0008)
3525 ap->flags |= MR_TOGGLE_RX;
3526 if (ap->rxconfig & ANEG_CFG_NP)
3527 ap->flags |= MR_NP_RX;
3528 ap->flags |= MR_PAGE_RX;
3529
3530 ap->state = ANEG_STATE_COMPLETE_ACK;
3531 ret = ANEG_TIMER_ENAB;
3532 break;
3533
3534 case ANEG_STATE_COMPLETE_ACK:
3535 if (ap->ability_match != 0 &&
3536 ap->rxconfig == 0) {
3537 ap->state = ANEG_STATE_AN_ENABLE;
3538 break;
3539 }
3540 delta = ap->cur_time - ap->link_time;
3541 if (delta > ANEG_STATE_SETTLE_TIME) {
3542 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3543 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3544 } else {
3545 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3546 !(ap->flags & MR_NP_RX)) {
3547 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3548 } else {
3549 ret = ANEG_FAILED;
3550 }
3551 }
3552 }
3553 break;
3554
3555 case ANEG_STATE_IDLE_DETECT_INIT:
3556 ap->link_time = ap->cur_time;
3557 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3558 tw32_f(MAC_MODE, tp->mac_mode);
3559 udelay(40);
3560
3561 ap->state = ANEG_STATE_IDLE_DETECT;
3562 ret = ANEG_TIMER_ENAB;
3563 break;
3564
3565 case ANEG_STATE_IDLE_DETECT:
3566 if (ap->ability_match != 0 &&
3567 ap->rxconfig == 0) {
3568 ap->state = ANEG_STATE_AN_ENABLE;
3569 break;
3570 }
3571 delta = ap->cur_time - ap->link_time;
3572 if (delta > ANEG_STATE_SETTLE_TIME) {
3573 /* XXX another gem from the Broadcom driver :( */
3574 ap->state = ANEG_STATE_LINK_OK;
3575 }
3576 break;
3577
3578 case ANEG_STATE_LINK_OK:
3579 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3580 ret = ANEG_DONE;
3581 break;
3582
3583 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3584 /* ??? unimplemented */
3585 break;
3586
3587 case ANEG_STATE_NEXT_PAGE_WAIT:
3588 /* ??? unimplemented */
3589 break;
3590
3591 default:
3592 ret = ANEG_FAILED;
3593 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
3596 return ret;
3597}
3598
Matt Carlson5be73b42007-12-20 20:09:29 -08003599static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600{
3601 int res = 0;
3602 struct tg3_fiber_aneginfo aninfo;
3603 int status = ANEG_FAILED;
3604 unsigned int tick;
3605 u32 tmp;
3606
3607 tw32_f(MAC_TX_AUTO_NEG, 0);
3608
3609 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3610 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3611 udelay(40);
3612
3613 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3614 udelay(40);
3615
3616 memset(&aninfo, 0, sizeof(aninfo));
3617 aninfo.flags |= MR_AN_ENABLE;
3618 aninfo.state = ANEG_STATE_UNKNOWN;
3619 aninfo.cur_time = 0;
3620 tick = 0;
3621 while (++tick < 195000) {
3622 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3623 if (status == ANEG_DONE || status == ANEG_FAILED)
3624 break;
3625
3626 udelay(1);
3627 }
3628
3629 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3630 tw32_f(MAC_MODE, tp->mac_mode);
3631 udelay(40);
3632
Matt Carlson5be73b42007-12-20 20:09:29 -08003633 *txflags = aninfo.txconfig;
3634 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
3636 if (status == ANEG_DONE &&
3637 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3638 MR_LP_ADV_FULL_DUPLEX)))
3639 res = 1;
3640
3641 return res;
3642}
3643
3644static void tg3_init_bcm8002(struct tg3 *tp)
3645{
3646 u32 mac_status = tr32(MAC_STATUS);
3647 int i;
3648
3649 /* Reset when initting first time or we have a link. */
3650 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3651 !(mac_status & MAC_STATUS_PCS_SYNCED))
3652 return;
3653
3654 /* Set PLL lock range. */
3655 tg3_writephy(tp, 0x16, 0x8007);
3656
3657 /* SW reset */
3658 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3659
3660 /* Wait for reset to complete. */
3661 /* XXX schedule_timeout() ... */
3662 for (i = 0; i < 500; i++)
3663 udelay(10);
3664
3665 /* Config mode; select PMA/Ch 1 regs. */
3666 tg3_writephy(tp, 0x10, 0x8411);
3667
3668 /* Enable auto-lock and comdet, select txclk for tx. */
3669 tg3_writephy(tp, 0x11, 0x0a10);
3670
3671 tg3_writephy(tp, 0x18, 0x00a0);
3672 tg3_writephy(tp, 0x16, 0x41ff);
3673
3674 /* Assert and deassert POR. */
3675 tg3_writephy(tp, 0x13, 0x0400);
3676 udelay(40);
3677 tg3_writephy(tp, 0x13, 0x0000);
3678
3679 tg3_writephy(tp, 0x11, 0x0a50);
3680 udelay(40);
3681 tg3_writephy(tp, 0x11, 0x0a10);
3682
3683 /* Wait for signal to stabilize */
3684 /* XXX schedule_timeout() ... */
3685 for (i = 0; i < 15000; i++)
3686 udelay(10);
3687
3688 /* Deselect the channel register so we can read the PHYID
3689 * later.
3690 */
3691 tg3_writephy(tp, 0x10, 0x8011);
3692}
3693
3694static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3695{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003696 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 u32 sg_dig_ctrl, sg_dig_status;
3698 u32 serdes_cfg, expected_sg_dig_ctrl;
3699 int workaround, port_a;
3700 int current_link_up;
3701
3702 serdes_cfg = 0;
3703 expected_sg_dig_ctrl = 0;
3704 workaround = 0;
3705 port_a = 1;
3706 current_link_up = 0;
3707
3708 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3709 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3710 workaround = 1;
3711 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3712 port_a = 0;
3713
3714 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3715 /* preserve bits 20-23 for voltage regulator */
3716 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3717 }
3718
3719 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3720
3721 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003722 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 if (workaround) {
3724 u32 val = serdes_cfg;
3725
3726 if (port_a)
3727 val |= 0xc010000;
3728 else
3729 val |= 0x4010000;
3730 tw32_f(MAC_SERDES_CFG, val);
3731 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003732
3733 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 }
3735 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3736 tg3_setup_flow_control(tp, 0, 0);
3737 current_link_up = 1;
3738 }
3739 goto out;
3740 }
3741
3742 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003743 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
Matt Carlson82cd3d12007-12-20 20:09:00 -08003745 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3746 if (flowctrl & ADVERTISE_1000XPAUSE)
3747 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3748 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3749 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
3751 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003752 if ((tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT) &&
3753 tp->serdes_counter &&
3754 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3755 MAC_STATUS_RCVD_CFG)) ==
3756 MAC_STATUS_PCS_SYNCED)) {
3757 tp->serdes_counter--;
3758 current_link_up = 1;
3759 goto out;
3760 }
3761restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 if (workaround)
3763 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003764 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 udelay(5);
3766 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3767
Michael Chan3d3ebe72006-09-27 15:59:15 -07003768 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3769 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3771 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003772 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 mac_status = tr32(MAC_STATUS);
3774
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003775 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003777 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
Matt Carlson82cd3d12007-12-20 20:09:00 -08003779 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3780 local_adv |= ADVERTISE_1000XPAUSE;
3781 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3782 local_adv |= ADVERTISE_1000XPSE_ASYM;
3783
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003784 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003785 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003786 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003787 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
3789 tg3_setup_flow_control(tp, local_adv, remote_adv);
3790 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003791 tp->serdes_counter = 0;
3792 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003793 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003794 if (tp->serdes_counter)
3795 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 else {
3797 if (workaround) {
3798 u32 val = serdes_cfg;
3799
3800 if (port_a)
3801 val |= 0xc010000;
3802 else
3803 val |= 0x4010000;
3804
3805 tw32_f(MAC_SERDES_CFG, val);
3806 }
3807
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003808 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 udelay(40);
3810
3811 /* Link parallel detection - link is up */
3812 /* only if we have PCS_SYNC and not */
3813 /* receiving config code words */
3814 mac_status = tr32(MAC_STATUS);
3815 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3816 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3817 tg3_setup_flow_control(tp, 0, 0);
3818 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003819 tp->tg3_flags2 |=
3820 TG3_FLG2_PARALLEL_DETECT;
3821 tp->serdes_counter =
3822 SERDES_PARALLEL_DET_TIMEOUT;
3823 } else
3824 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 }
3826 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003827 } else {
3828 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3829 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 }
3831
3832out:
3833 return current_link_up;
3834}
3835
3836static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3837{
3838 int current_link_up = 0;
3839
Michael Chan5cf64b8a2007-05-05 12:11:21 -07003840 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
3843 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003844 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003846
Matt Carlson5be73b42007-12-20 20:09:29 -08003847 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3848 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849
Matt Carlson5be73b42007-12-20 20:09:29 -08003850 if (txflags & ANEG_CFG_PS1)
3851 local_adv |= ADVERTISE_1000XPAUSE;
3852 if (txflags & ANEG_CFG_PS2)
3853 local_adv |= ADVERTISE_1000XPSE_ASYM;
3854
3855 if (rxflags & MR_LP_ADV_SYM_PAUSE)
3856 remote_adv |= LPA_1000XPAUSE;
3857 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3858 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859
3860 tg3_setup_flow_control(tp, local_adv, remote_adv);
3861
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 current_link_up = 1;
3863 }
3864 for (i = 0; i < 30; i++) {
3865 udelay(20);
3866 tw32_f(MAC_STATUS,
3867 (MAC_STATUS_SYNC_CHANGED |
3868 MAC_STATUS_CFG_CHANGED));
3869 udelay(40);
3870 if ((tr32(MAC_STATUS) &
3871 (MAC_STATUS_SYNC_CHANGED |
3872 MAC_STATUS_CFG_CHANGED)) == 0)
3873 break;
3874 }
3875
3876 mac_status = tr32(MAC_STATUS);
3877 if (current_link_up == 0 &&
3878 (mac_status & MAC_STATUS_PCS_SYNCED) &&
3879 !(mac_status & MAC_STATUS_RCVD_CFG))
3880 current_link_up = 1;
3881 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08003882 tg3_setup_flow_control(tp, 0, 0);
3883
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 /* Forcing 1000FD link up. */
3885 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
3887 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
3888 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003889
3890 tw32_f(MAC_MODE, tp->mac_mode);
3891 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 }
3893
3894out:
3895 return current_link_up;
3896}
3897
3898static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
3899{
3900 u32 orig_pause_cfg;
3901 u16 orig_active_speed;
3902 u8 orig_active_duplex;
3903 u32 mac_status;
3904 int current_link_up;
3905 int i;
3906
Matt Carlson8d018622007-12-20 20:05:44 -08003907 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 orig_active_speed = tp->link_config.active_speed;
3909 orig_active_duplex = tp->link_config.active_duplex;
3910
3911 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
3912 netif_carrier_ok(tp->dev) &&
3913 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
3914 mac_status = tr32(MAC_STATUS);
3915 mac_status &= (MAC_STATUS_PCS_SYNCED |
3916 MAC_STATUS_SIGNAL_DET |
3917 MAC_STATUS_CFG_CHANGED |
3918 MAC_STATUS_RCVD_CFG);
3919 if (mac_status == (MAC_STATUS_PCS_SYNCED |
3920 MAC_STATUS_SIGNAL_DET)) {
3921 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3922 MAC_STATUS_CFG_CHANGED));
3923 return 0;
3924 }
3925 }
3926
3927 tw32_f(MAC_TX_AUTO_NEG, 0);
3928
3929 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
3930 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
3931 tw32_f(MAC_MODE, tp->mac_mode);
3932 udelay(40);
3933
3934 if (tp->phy_id == PHY_ID_BCM8002)
3935 tg3_init_bcm8002(tp);
3936
3937 /* Enable link change event even when serdes polling. */
3938 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3939 udelay(40);
3940
3941 current_link_up = 0;
3942 mac_status = tr32(MAC_STATUS);
3943
3944 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
3945 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
3946 else
3947 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
3948
Matt Carlson898a56f2009-08-28 14:02:40 +00003949 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00003951 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952
3953 for (i = 0; i < 100; i++) {
3954 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3955 MAC_STATUS_CFG_CHANGED));
3956 udelay(5);
3957 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07003958 MAC_STATUS_CFG_CHANGED |
3959 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 break;
3961 }
3962
3963 mac_status = tr32(MAC_STATUS);
3964 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
3965 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003966 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
3967 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 tw32_f(MAC_MODE, (tp->mac_mode |
3969 MAC_MODE_SEND_CONFIGS));
3970 udelay(1);
3971 tw32_f(MAC_MODE, tp->mac_mode);
3972 }
3973 }
3974
3975 if (current_link_up == 1) {
3976 tp->link_config.active_speed = SPEED_1000;
3977 tp->link_config.active_duplex = DUPLEX_FULL;
3978 tw32(MAC_LED_CTRL, (tp->led_ctrl |
3979 LED_CTRL_LNKLED_OVERRIDE |
3980 LED_CTRL_1000MBPS_ON));
3981 } else {
3982 tp->link_config.active_speed = SPEED_INVALID;
3983 tp->link_config.active_duplex = DUPLEX_INVALID;
3984 tw32(MAC_LED_CTRL, (tp->led_ctrl |
3985 LED_CTRL_LNKLED_OVERRIDE |
3986 LED_CTRL_TRAFFIC_OVERRIDE));
3987 }
3988
3989 if (current_link_up != netif_carrier_ok(tp->dev)) {
3990 if (current_link_up)
3991 netif_carrier_on(tp->dev);
3992 else
3993 netif_carrier_off(tp->dev);
3994 tg3_link_report(tp);
3995 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08003996 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 if (orig_pause_cfg != now_pause_cfg ||
3998 orig_active_speed != tp->link_config.active_speed ||
3999 orig_active_duplex != tp->link_config.active_duplex)
4000 tg3_link_report(tp);
4001 }
4002
4003 return 0;
4004}
4005
Michael Chan747e8f82005-07-25 12:33:22 -07004006static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4007{
4008 int current_link_up, err = 0;
4009 u32 bmsr, bmcr;
4010 u16 current_speed;
4011 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004012 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004013
4014 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4015 tw32_f(MAC_MODE, tp->mac_mode);
4016 udelay(40);
4017
4018 tw32(MAC_EVENT, 0);
4019
4020 tw32_f(MAC_STATUS,
4021 (MAC_STATUS_SYNC_CHANGED |
4022 MAC_STATUS_CFG_CHANGED |
4023 MAC_STATUS_MI_COMPLETION |
4024 MAC_STATUS_LNKSTATE_CHANGED));
4025 udelay(40);
4026
4027 if (force_reset)
4028 tg3_phy_reset(tp);
4029
4030 current_link_up = 0;
4031 current_speed = SPEED_INVALID;
4032 current_duplex = DUPLEX_INVALID;
4033
4034 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4035 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004036 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4037 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4038 bmsr |= BMSR_LSTATUS;
4039 else
4040 bmsr &= ~BMSR_LSTATUS;
4041 }
Michael Chan747e8f82005-07-25 12:33:22 -07004042
4043 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4044
4045 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlson2bd3ed02008-06-09 15:39:55 -07004046 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004047 /* do nothing, just check for link up at the end */
4048 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4049 u32 adv, new_adv;
4050
4051 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4052 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4053 ADVERTISE_1000XPAUSE |
4054 ADVERTISE_1000XPSE_ASYM |
4055 ADVERTISE_SLCT);
4056
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004057 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004058
4059 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4060 new_adv |= ADVERTISE_1000XHALF;
4061 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4062 new_adv |= ADVERTISE_1000XFULL;
4063
4064 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4065 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4066 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4067 tg3_writephy(tp, MII_BMCR, bmcr);
4068
4069 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004070 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Michael Chan747e8f82005-07-25 12:33:22 -07004071 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4072
4073 return err;
4074 }
4075 } else {
4076 u32 new_bmcr;
4077
4078 bmcr &= ~BMCR_SPEED1000;
4079 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4080
4081 if (tp->link_config.duplex == DUPLEX_FULL)
4082 new_bmcr |= BMCR_FULLDPLX;
4083
4084 if (new_bmcr != bmcr) {
4085 /* BMCR_SPEED1000 is a reserved bit that needs
4086 * to be set on write.
4087 */
4088 new_bmcr |= BMCR_SPEED1000;
4089
4090 /* Force a linkdown */
4091 if (netif_carrier_ok(tp->dev)) {
4092 u32 adv;
4093
4094 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4095 adv &= ~(ADVERTISE_1000XFULL |
4096 ADVERTISE_1000XHALF |
4097 ADVERTISE_SLCT);
4098 tg3_writephy(tp, MII_ADVERTISE, adv);
4099 tg3_writephy(tp, MII_BMCR, bmcr |
4100 BMCR_ANRESTART |
4101 BMCR_ANENABLE);
4102 udelay(10);
4103 netif_carrier_off(tp->dev);
4104 }
4105 tg3_writephy(tp, MII_BMCR, new_bmcr);
4106 bmcr = new_bmcr;
4107 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4108 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004109 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4110 ASIC_REV_5714) {
4111 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4112 bmsr |= BMSR_LSTATUS;
4113 else
4114 bmsr &= ~BMSR_LSTATUS;
4115 }
Michael Chan747e8f82005-07-25 12:33:22 -07004116 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4117 }
4118 }
4119
4120 if (bmsr & BMSR_LSTATUS) {
4121 current_speed = SPEED_1000;
4122 current_link_up = 1;
4123 if (bmcr & BMCR_FULLDPLX)
4124 current_duplex = DUPLEX_FULL;
4125 else
4126 current_duplex = DUPLEX_HALF;
4127
Matt Carlsonef167e22007-12-20 20:10:01 -08004128 local_adv = 0;
4129 remote_adv = 0;
4130
Michael Chan747e8f82005-07-25 12:33:22 -07004131 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004132 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004133
4134 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4135 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4136 common = local_adv & remote_adv;
4137 if (common & (ADVERTISE_1000XHALF |
4138 ADVERTISE_1000XFULL)) {
4139 if (common & ADVERTISE_1000XFULL)
4140 current_duplex = DUPLEX_FULL;
4141 else
4142 current_duplex = DUPLEX_HALF;
Michael Chan747e8f82005-07-25 12:33:22 -07004143 }
4144 else
4145 current_link_up = 0;
4146 }
4147 }
4148
Matt Carlsonef167e22007-12-20 20:10:01 -08004149 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4150 tg3_setup_flow_control(tp, local_adv, remote_adv);
4151
Michael Chan747e8f82005-07-25 12:33:22 -07004152 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4153 if (tp->link_config.active_duplex == DUPLEX_HALF)
4154 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4155
4156 tw32_f(MAC_MODE, tp->mac_mode);
4157 udelay(40);
4158
4159 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4160
4161 tp->link_config.active_speed = current_speed;
4162 tp->link_config.active_duplex = current_duplex;
4163
4164 if (current_link_up != netif_carrier_ok(tp->dev)) {
4165 if (current_link_up)
4166 netif_carrier_on(tp->dev);
4167 else {
4168 netif_carrier_off(tp->dev);
4169 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4170 }
4171 tg3_link_report(tp);
4172 }
4173 return err;
4174}
4175
4176static void tg3_serdes_parallel_detect(struct tg3 *tp)
4177{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004178 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004179 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004180 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004181 return;
4182 }
4183 if (!netif_carrier_ok(tp->dev) &&
4184 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4185 u32 bmcr;
4186
4187 tg3_readphy(tp, MII_BMCR, &bmcr);
4188 if (bmcr & BMCR_ANENABLE) {
4189 u32 phy1, phy2;
4190
4191 /* Select shadow register 0x1f */
4192 tg3_writephy(tp, 0x1c, 0x7c00);
4193 tg3_readphy(tp, 0x1c, &phy1);
4194
4195 /* Select expansion interrupt status register */
4196 tg3_writephy(tp, 0x17, 0x0f01);
4197 tg3_readphy(tp, 0x15, &phy2);
4198 tg3_readphy(tp, 0x15, &phy2);
4199
4200 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4201 /* We have signal detect and not receiving
4202 * config code words, link is up by parallel
4203 * detection.
4204 */
4205
4206 bmcr &= ~BMCR_ANENABLE;
4207 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4208 tg3_writephy(tp, MII_BMCR, bmcr);
4209 tp->tg3_flags2 |= TG3_FLG2_PARALLEL_DETECT;
4210 }
4211 }
4212 }
4213 else if (netif_carrier_ok(tp->dev) &&
4214 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
4215 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
4216 u32 phy2;
4217
4218 /* Select expansion interrupt status register */
4219 tg3_writephy(tp, 0x17, 0x0f01);
4220 tg3_readphy(tp, 0x15, &phy2);
4221 if (phy2 & 0x20) {
4222 u32 bmcr;
4223
4224 /* Config code words received, turn on autoneg. */
4225 tg3_readphy(tp, MII_BMCR, &bmcr);
4226 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4227
4228 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4229
4230 }
4231 }
4232}
4233
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4235{
4236 int err;
4237
4238 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
4239 err = tg3_setup_fiber_phy(tp, force_reset);
Michael Chan747e8f82005-07-25 12:33:22 -07004240 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
4241 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 } else {
4243 err = tg3_setup_copper_phy(tp, force_reset);
4244 }
4245
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004246 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004247 u32 val, scale;
4248
4249 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4250 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4251 scale = 65;
4252 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4253 scale = 6;
4254 else
4255 scale = 12;
4256
4257 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4258 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4259 tw32(GRC_MISC_CFG, val);
4260 }
4261
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 if (tp->link_config.active_speed == SPEED_1000 &&
4263 tp->link_config.active_duplex == DUPLEX_HALF)
4264 tw32(MAC_TX_LENGTHS,
4265 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4266 (6 << TX_LENGTHS_IPG_SHIFT) |
4267 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4268 else
4269 tw32(MAC_TX_LENGTHS,
4270 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4271 (6 << TX_LENGTHS_IPG_SHIFT) |
4272 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4273
4274 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4275 if (netif_carrier_ok(tp->dev)) {
4276 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004277 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 } else {
4279 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4280 }
4281 }
4282
Matt Carlson8ed5d972007-05-07 00:25:49 -07004283 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4284 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4285 if (!netif_carrier_ok(tp->dev))
4286 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4287 tp->pwrmgmt_thresh;
4288 else
4289 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4290 tw32(PCIE_PWR_MGMT_THRESH, val);
4291 }
4292
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 return err;
4294}
4295
Michael Chandf3e6542006-05-26 17:48:07 -07004296/* This is called whenever we suspect that the system chipset is re-
4297 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4298 * is bogus tx completions. We try to recover by setting the
4299 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4300 * in the workqueue.
4301 */
4302static void tg3_tx_recover(struct tg3 *tp)
4303{
4304 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4305 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4306
4307 printk(KERN_WARNING PFX "%s: The system may be re-ordering memory-"
4308 "mapped I/O cycles to the network device, attempting to "
4309 "recover. Please report the problem to the driver maintainer "
4310 "and include system chipset information.\n", tp->dev->name);
4311
4312 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004313 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004314 spin_unlock(&tp->lock);
4315}
4316
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004317static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004318{
4319 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004320 return tnapi->tx_pending -
4321 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004322}
4323
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324/* Tigon3 never reports partial packet sends. So we do not
4325 * need special logic to handle SKBs that have not had all
4326 * of their frags sent yet, like SunGEM does.
4327 */
Matt Carlson17375d22009-08-28 14:02:18 +00004328static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{
Matt Carlson17375d22009-08-28 14:02:18 +00004330 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004331 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004332 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004333 struct netdev_queue *txq;
4334 int index = tnapi - tp->napi;
4335
4336 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
4337 index--;
4338
4339 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340
4341 while (sw_idx != hw_idx) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004342 struct tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004344 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
Michael Chandf3e6542006-05-26 17:48:07 -07004346 if (unlikely(skb == NULL)) {
4347 tg3_tx_recover(tp);
4348 return;
4349 }
4350
David S. Miller90079ce2008-09-11 04:52:51 -07004351 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
4353 ri->skb = NULL;
4354
4355 sw_idx = NEXT_TX(sw_idx);
4356
4357 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004358 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004359 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4360 tx_bug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 sw_idx = NEXT_TX(sw_idx);
4362 }
4363
David S. Millerf47c11e2005-06-24 20:18:35 -07004364 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004365
4366 if (unlikely(tx_bug)) {
4367 tg3_tx_recover(tp);
4368 return;
4369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 }
4371
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004372 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
Michael Chan1b2a7202006-08-07 21:46:02 -07004374 /* Need to make the tx_cons update visible to tg3_start_xmit()
4375 * before checking for netif_queue_stopped(). Without the
4376 * memory barrier, there is a small possibility that tg3_start_xmit()
4377 * will miss it and cause the queue to be stopped forever.
4378 */
4379 smp_mb();
4380
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004381 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004382 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004383 __netif_tx_lock(txq, smp_processor_id());
4384 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004385 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004386 netif_tx_wake_queue(txq);
4387 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389}
4390
4391/* Returns size of skb allocated or < 0 on error.
4392 *
4393 * We only need to fill in the address because the other members
4394 * of the RX descriptor are invariant, see tg3_init_rings.
4395 *
4396 * Note the purposeful assymetry of cpu vs. chip accesses. For
4397 * posting buffers we only dirty the first cache line of the RX
4398 * descriptor (containing the address). Whereas for the RX status
4399 * buffers the cpu only reads the last cacheline of the RX descriptor
4400 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4401 */
Matt Carlson17375d22009-08-28 14:02:18 +00004402static int tg3_alloc_rx_skb(struct tg3_napi *tnapi, u32 opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 int src_idx, u32 dest_idx_unmasked)
4404{
Matt Carlson17375d22009-08-28 14:02:18 +00004405 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 struct tg3_rx_buffer_desc *desc;
4407 struct ring_info *map, *src_map;
4408 struct sk_buff *skb;
4409 dma_addr_t mapping;
4410 int skb_size, dest_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004411 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
4413 src_map = NULL;
4414 switch (opaque_key) {
4415 case RXD_OPAQUE_RING_STD:
4416 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlson21f581a2009-08-28 14:00:25 +00004417 desc = &tpr->rx_std[dest_idx];
4418 map = &tpr->rx_std_buffers[dest_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 if (src_idx >= 0)
Matt Carlson21f581a2009-08-28 14:00:25 +00004420 src_map = &tpr->rx_std_buffers[src_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004421 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 break;
4423
4424 case RXD_OPAQUE_RING_JUMBO:
4425 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004426 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004427 map = &tpr->rx_jmb_buffers[dest_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 if (src_idx >= 0)
Matt Carlson21f581a2009-08-28 14:00:25 +00004429 src_map = &tpr->rx_jmb_buffers[src_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004430 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 break;
4432
4433 default:
4434 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
4437 /* Do not overwrite any of the map or rp information
4438 * until we are sure we can commit to a new buffer.
4439 *
4440 * Callers depend upon this behavior and assume that
4441 * we leave everything unchanged if we fail.
4442 */
Matt Carlson287be122009-08-28 13:58:46 +00004443 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 if (skb == NULL)
4445 return -ENOMEM;
4446
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 skb_reserve(skb, tp->rx_offset);
4448
Matt Carlson287be122009-08-28 13:58:46 +00004449 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 PCI_DMA_FROMDEVICE);
4451
4452 map->skb = skb;
4453 pci_unmap_addr_set(map, mapping, mapping);
4454
4455 if (src_map != NULL)
4456 src_map->skb = NULL;
4457
4458 desc->addr_hi = ((u64)mapping >> 32);
4459 desc->addr_lo = ((u64)mapping & 0xffffffff);
4460
4461 return skb_size;
4462}
4463
4464/* We only need to move over in the address because the other
4465 * members of the RX descriptor are invariant. See notes above
4466 * tg3_alloc_rx_skb for full details.
4467 */
Matt Carlson17375d22009-08-28 14:02:18 +00004468static void tg3_recycle_rx(struct tg3_napi *tnapi, u32 opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 int src_idx, u32 dest_idx_unmasked)
4470{
Matt Carlson17375d22009-08-28 14:02:18 +00004471 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4473 struct ring_info *src_map, *dest_map;
4474 int dest_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004475 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
4477 switch (opaque_key) {
4478 case RXD_OPAQUE_RING_STD:
4479 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlson21f581a2009-08-28 14:00:25 +00004480 dest_desc = &tpr->rx_std[dest_idx];
4481 dest_map = &tpr->rx_std_buffers[dest_idx];
4482 src_desc = &tpr->rx_std[src_idx];
4483 src_map = &tpr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 break;
4485
4486 case RXD_OPAQUE_RING_JUMBO:
4487 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004488 dest_desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004489 dest_map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004490 src_desc = &tpr->rx_jmb[src_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004491 src_map = &tpr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 break;
4493
4494 default:
4495 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
4498 dest_map->skb = src_map->skb;
4499 pci_unmap_addr_set(dest_map, mapping,
4500 pci_unmap_addr(src_map, mapping));
4501 dest_desc->addr_hi = src_desc->addr_hi;
4502 dest_desc->addr_lo = src_desc->addr_lo;
4503
4504 src_map->skb = NULL;
4505}
4506
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507/* The RX ring scheme is composed of multiple rings which post fresh
4508 * buffers to the chip, and one special ring the chip uses to report
4509 * status back to the host.
4510 *
4511 * The special ring reports the status of received packets to the
4512 * host. The chip does not write into the original descriptor the
4513 * RX buffer was obtained from. The chip simply takes the original
4514 * descriptor as provided by the host, updates the status and length
4515 * field, then writes this into the next status ring entry.
4516 *
4517 * Each ring the host uses to post buffers to the chip is described
4518 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4519 * it is first placed into the on-chip ram. When the packet's length
4520 * is known, it walks down the TG3_BDINFO entries to select the ring.
4521 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4522 * which is within the range of the new packet's length is chosen.
4523 *
4524 * The "separate ring for rx status" scheme may sound queer, but it makes
4525 * sense from a cache coherency perspective. If only the host writes
4526 * to the buffer post rings, and only the chip writes to the rx status
4527 * rings, then cache lines never move beyond shared-modified state.
4528 * If both the host and chip were to write into the same ring, cache line
4529 * eviction could occur since both entities want it in an exclusive state.
4530 */
Matt Carlson17375d22009-08-28 14:02:18 +00004531static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532{
Matt Carlson17375d22009-08-28 14:02:18 +00004533 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004534 u32 work_mask, rx_std_posted = 0;
Matt Carlson72334482009-08-28 14:03:01 +00004535 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004536 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 int received;
Matt Carlson21f581a2009-08-28 14:00:25 +00004538 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004540 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 /*
4542 * We need to order the read of hw_idx and the read of
4543 * the opaque cookie.
4544 */
4545 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 work_mask = 0;
4547 received = 0;
4548 while (sw_idx != hw_idx && budget > 0) {
Matt Carlson72334482009-08-28 14:03:01 +00004549 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 unsigned int len;
4551 struct sk_buff *skb;
4552 dma_addr_t dma_addr;
4553 u32 opaque_key, desc_idx, *post_ptr;
4554
4555 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4556 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4557 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson21f581a2009-08-28 14:00:25 +00004558 struct ring_info *ri = &tpr->rx_std_buffers[desc_idx];
4559 dma_addr = pci_unmap_addr(ri, mapping);
4560 skb = ri->skb;
4561 post_ptr = &tpr->rx_std_ptr;
Michael Chanf92905d2006-06-29 20:14:29 -07004562 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson21f581a2009-08-28 14:00:25 +00004564 struct ring_info *ri = &tpr->rx_jmb_buffers[desc_idx];
4565 dma_addr = pci_unmap_addr(ri, mapping);
4566 skb = ri->skb;
4567 post_ptr = &tpr->rx_jmb_ptr;
4568 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
4571 work_mask |= opaque_key;
4572
4573 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4574 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4575 drop_it:
Matt Carlson17375d22009-08-28 14:02:18 +00004576 tg3_recycle_rx(tnapi, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 desc_idx, *post_ptr);
4578 drop_it_no_recycle:
4579 /* Other statistics kept track of by card. */
4580 tp->net_stats.rx_dropped++;
4581 goto next_pkt;
4582 }
4583
Matt Carlsonad829262008-11-21 17:16:16 -08004584 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4585 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004587 if (len > RX_COPY_THRESHOLD
Matt Carlsonad829262008-11-21 17:16:16 -08004588 && tp->rx_offset == NET_IP_ALIGN
4589 /* rx_offset will likely not equal NET_IP_ALIGN
4590 * if this is a 5701 card running in PCI-X mode
4591 * [see tg3_get_invariants()]
4592 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 ) {
4594 int skb_size;
4595
Matt Carlson17375d22009-08-28 14:02:18 +00004596 skb_size = tg3_alloc_rx_skb(tnapi, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 desc_idx, *post_ptr);
4598 if (skb_size < 0)
4599 goto drop_it;
4600
Matt Carlson287be122009-08-28 13:58:46 +00004601 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 PCI_DMA_FROMDEVICE);
4603
4604 skb_put(skb, len);
4605 } else {
4606 struct sk_buff *copy_skb;
4607
Matt Carlson17375d22009-08-28 14:02:18 +00004608 tg3_recycle_rx(tnapi, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 desc_idx, *post_ptr);
4610
Matt Carlsonad829262008-11-21 17:16:16 -08004611 copy_skb = netdev_alloc_skb(tp->dev,
4612 len + TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 if (copy_skb == NULL)
4614 goto drop_it_no_recycle;
4615
Matt Carlsonad829262008-11-21 17:16:16 -08004616 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 skb_put(copy_skb, len);
4618 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004619 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4621
4622 /* We'll reuse the original ring buffer. */
4623 skb = copy_skb;
4624 }
4625
4626 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4627 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4628 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4629 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4630 skb->ip_summed = CHECKSUM_UNNECESSARY;
4631 else
4632 skb->ip_summed = CHECKSUM_NONE;
4633
4634 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004635
4636 if (len > (tp->dev->mtu + ETH_HLEN) &&
4637 skb->protocol != htons(ETH_P_8021Q)) {
4638 dev_kfree_skb(skb);
4639 goto next_pkt;
4640 }
4641
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642#if TG3_VLAN_TAG_USED
4643 if (tp->vlgrp != NULL &&
4644 desc->type_flags & RXD_FLAG_VLAN) {
Matt Carlson17375d22009-08-28 14:02:18 +00004645 vlan_gro_receive(&tnapi->napi, tp->vlgrp,
Matt Carlson8ef04422009-08-28 14:01:37 +00004646 desc->err_vlan & RXD_VLAN_MASK, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 } else
4648#endif
Matt Carlson17375d22009-08-28 14:02:18 +00004649 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 received++;
4652 budget--;
4653
4654next_pkt:
4655 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004656
4657 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
4658 u32 idx = *post_ptr % TG3_RX_RING_SIZE;
4659
4660 tw32_rx_mbox(MAILBOX_RCV_STD_PROD_IDX +
4661 TG3_64BIT_REG_LOW, idx);
4662 work_mask &= ~RXD_OPAQUE_RING_STD;
4663 rx_std_posted = 0;
4664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004666 sw_idx++;
Eric Dumazet6b31a512007-02-06 13:29:21 -08004667 sw_idx &= (TG3_RX_RCB_RING_SIZE(tp) - 1);
Michael Chan52f6d692005-04-25 15:14:32 -07004668
4669 /* Refresh hw_idx to see if there is new work */
4670 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004671 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004672 rmb();
4673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 }
4675
4676 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004677 tnapi->rx_rcb_ptr = sw_idx;
4678 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679
4680 /* Refill RX ring(s). */
4681 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson21f581a2009-08-28 14:00:25 +00004682 sw_idx = tpr->rx_std_ptr % TG3_RX_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 tw32_rx_mbox(MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW,
4684 sw_idx);
4685 }
4686 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson21f581a2009-08-28 14:00:25 +00004687 sw_idx = tpr->rx_jmb_ptr % TG3_RX_JUMBO_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 tw32_rx_mbox(MAILBOX_RCV_JUMBO_PROD_IDX + TG3_64BIT_REG_LOW,
4689 sw_idx);
4690 }
4691 mmiowb();
4692
4693 return received;
4694}
4695
Matt Carlson17375d22009-08-28 14:02:18 +00004696static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697{
Matt Carlson17375d22009-08-28 14:02:18 +00004698 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004699 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 /* handle link change and other phy events */
4702 if (!(tp->tg3_flags &
4703 (TG3_FLAG_USE_LINKCHG_REG |
4704 TG3_FLAG_POLL_SERDES))) {
4705 if (sblk->status & SD_STATUS_LINK_CHG) {
4706 sblk->status = SD_STATUS_UPDATED |
4707 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004708 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004709 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4710 tw32_f(MAC_STATUS,
4711 (MAC_STATUS_SYNC_CHANGED |
4712 MAC_STATUS_CFG_CHANGED |
4713 MAC_STATUS_MI_COMPLETION |
4714 MAC_STATUS_LNKSTATE_CHANGED));
4715 udelay(40);
4716 } else
4717 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004718 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 }
4720 }
4721
4722 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004723 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00004724 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004725 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07004726 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 }
4728
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 /* run RX thread, within the bounds set by NAPI.
4730 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004731 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004733 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00004734 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735
David S. Miller6f535762007-10-11 18:08:29 -07004736 return work_done;
4737}
David S. Millerf7383c22005-05-18 22:50:53 -07004738
David S. Miller6f535762007-10-11 18:08:29 -07004739static int tg3_poll(struct napi_struct *napi, int budget)
4740{
Matt Carlson8ef04422009-08-28 14:01:37 +00004741 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
4742 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07004743 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00004744 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07004745
4746 while (1) {
Matt Carlson17375d22009-08-28 14:02:18 +00004747 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07004748
4749 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
4750 goto tx_recovery;
4751
4752 if (unlikely(work_done >= budget))
4753 break;
4754
Michael Chan4fd7ab52007-10-12 01:39:50 -07004755 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00004756 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07004757 * to tell the hw how much work has been processed,
4758 * so we must read it before checking for more work.
4759 */
Matt Carlson898a56f2009-08-28 14:02:40 +00004760 tnapi->last_tag = sblk->status_tag;
4761 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07004762 rmb();
4763 } else
4764 sblk->status &= ~SD_STATUS_UPDATED;
4765
Matt Carlson17375d22009-08-28 14:02:18 +00004766 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08004767 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00004768 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004769 break;
4770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 }
4772
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004773 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07004774
4775tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07004776 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08004777 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07004778 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07004779 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780}
4781
David S. Millerf47c11e2005-06-24 20:18:35 -07004782static void tg3_irq_quiesce(struct tg3 *tp)
4783{
Matt Carlson4f125f42009-09-01 12:55:02 +00004784 int i;
4785
David S. Millerf47c11e2005-06-24 20:18:35 -07004786 BUG_ON(tp->irq_sync);
4787
4788 tp->irq_sync = 1;
4789 smp_mb();
4790
Matt Carlson4f125f42009-09-01 12:55:02 +00004791 for (i = 0; i < tp->irq_cnt; i++)
4792 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07004793}
4794
4795static inline int tg3_irq_sync(struct tg3 *tp)
4796{
4797 return tp->irq_sync;
4798}
4799
4800/* Fully shutdown all tg3 driver activity elsewhere in the system.
4801 * If irq_sync is non-zero, then the IRQ handler must be synchronized
4802 * with as well. Most of the time, this is not necessary except when
4803 * shutting down the device.
4804 */
4805static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
4806{
Michael Chan46966542007-07-11 19:47:19 -07004807 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07004808 if (irq_sync)
4809 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07004810}
4811
4812static inline void tg3_full_unlock(struct tg3 *tp)
4813{
David S. Millerf47c11e2005-06-24 20:18:35 -07004814 spin_unlock_bh(&tp->lock);
4815}
4816
Michael Chanfcfa0a32006-03-20 22:28:41 -08004817/* One-shot MSI handler - Chip automatically disables interrupt
4818 * after sending MSI so driver doesn't have to do it.
4819 */
David Howells7d12e782006-10-05 14:55:46 +01004820static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08004821{
Matt Carlson09943a12009-08-28 14:01:57 +00004822 struct tg3_napi *tnapi = dev_id;
4823 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08004824
Matt Carlson898a56f2009-08-28 14:02:40 +00004825 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00004826 if (tnapi->rx_rcb)
4827 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08004828
4829 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00004830 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08004831
4832 return IRQ_HANDLED;
4833}
4834
Michael Chan88b06bc22005-04-21 17:13:25 -07004835/* MSI ISR - No need to check for interrupt sharing and no need to
4836 * flush status block and interrupt mailbox. PCI ordering rules
4837 * guarantee that MSI will arrive after the status block.
4838 */
David Howells7d12e782006-10-05 14:55:46 +01004839static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07004840{
Matt Carlson09943a12009-08-28 14:01:57 +00004841 struct tg3_napi *tnapi = dev_id;
4842 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07004843
Matt Carlson898a56f2009-08-28 14:02:40 +00004844 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00004845 if (tnapi->rx_rcb)
4846 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07004847 /*
David S. Millerfac9b832005-05-18 22:46:34 -07004848 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07004849 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07004850 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07004851 * NIC to stop sending us irqs, engaging "in-intr-handler"
4852 * event coalescing.
4853 */
4854 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07004855 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00004856 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07004857
Michael Chan88b06bc22005-04-21 17:13:25 -07004858 return IRQ_RETVAL(1);
4859}
4860
David Howells7d12e782006-10-05 14:55:46 +01004861static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862{
Matt Carlson09943a12009-08-28 14:01:57 +00004863 struct tg3_napi *tnapi = dev_id;
4864 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004865 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 unsigned int handled = 1;
4867
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 /* In INTx mode, it is possible for the interrupt to arrive at
4869 * the CPU before the status block posted prior to the interrupt.
4870 * Reading the PCI State register will confirm whether the
4871 * interrupt is ours and will flush the status block.
4872 */
Michael Chand18edcb2007-03-24 20:57:11 -07004873 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
4874 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
4875 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
4876 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07004877 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07004878 }
Michael Chand18edcb2007-03-24 20:57:11 -07004879 }
4880
4881 /*
4882 * Writing any value to intr-mbox-0 clears PCI INTA# and
4883 * chip-internal interrupt pending events.
4884 * Writing non-zero to intr-mbox-0 additional tells the
4885 * NIC to stop sending us irqs, engaging "in-intr-handler"
4886 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07004887 *
4888 * Flush the mailbox to de-assert the IRQ immediately to prevent
4889 * spurious interrupts. The flush impacts performance but
4890 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07004891 */
Michael Chanc04cb342007-05-07 00:26:15 -07004892 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07004893 if (tg3_irq_sync(tp))
4894 goto out;
4895 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00004896 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00004897 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00004898 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07004899 } else {
4900 /* No work, shared interrupt perhaps? re-enable
4901 * interrupts, and flush that PCI write
4902 */
4903 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
4904 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07004905 }
David S. Millerf47c11e2005-06-24 20:18:35 -07004906out:
David S. Millerfac9b832005-05-18 22:46:34 -07004907 return IRQ_RETVAL(handled);
4908}
4909
David Howells7d12e782006-10-05 14:55:46 +01004910static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07004911{
Matt Carlson09943a12009-08-28 14:01:57 +00004912 struct tg3_napi *tnapi = dev_id;
4913 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004914 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07004915 unsigned int handled = 1;
4916
David S. Millerfac9b832005-05-18 22:46:34 -07004917 /* In INTx mode, it is possible for the interrupt to arrive at
4918 * the CPU before the status block posted prior to the interrupt.
4919 * Reading the PCI State register will confirm whether the
4920 * interrupt is ours and will flush the status block.
4921 */
Matt Carlson898a56f2009-08-28 14:02:40 +00004922 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07004923 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
4924 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
4925 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07004926 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 }
Michael Chand18edcb2007-03-24 20:57:11 -07004928 }
4929
4930 /*
4931 * writing any value to intr-mbox-0 clears PCI INTA# and
4932 * chip-internal interrupt pending events.
4933 * writing non-zero to intr-mbox-0 additional tells the
4934 * NIC to stop sending us irqs, engaging "in-intr-handler"
4935 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07004936 *
4937 * Flush the mailbox to de-assert the IRQ immediately to prevent
4938 * spurious interrupts. The flush impacts performance but
4939 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07004940 */
Michael Chanc04cb342007-05-07 00:26:15 -07004941 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00004942
4943 /*
4944 * In a shared interrupt configuration, sometimes other devices'
4945 * interrupts will scream. We record the current status tag here
4946 * so that the above check can report that the screaming interrupts
4947 * are unhandled. Eventually they will be silenced.
4948 */
Matt Carlson898a56f2009-08-28 14:02:40 +00004949 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00004950
Michael Chand18edcb2007-03-24 20:57:11 -07004951 if (tg3_irq_sync(tp))
4952 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00004953
Matt Carlson72334482009-08-28 14:03:01 +00004954 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00004955
Matt Carlson09943a12009-08-28 14:01:57 +00004956 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00004957
David S. Millerf47c11e2005-06-24 20:18:35 -07004958out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 return IRQ_RETVAL(handled);
4960}
4961
Michael Chan79381092005-04-21 17:13:59 -07004962/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01004963static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07004964{
Matt Carlson09943a12009-08-28 14:01:57 +00004965 struct tg3_napi *tnapi = dev_id;
4966 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004967 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07004968
Michael Chanf9804dd2005-09-27 12:13:10 -07004969 if ((sblk->status & SD_STATUS_UPDATED) ||
4970 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07004971 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07004972 return IRQ_RETVAL(1);
4973 }
4974 return IRQ_RETVAL(0);
4975}
4976
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07004977static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07004978static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979
Michael Chanb9ec6c12006-07-25 16:37:27 -07004980/* Restart hardware after configuration changes, self-test, etc.
4981 * Invoked with tp->lock held.
4982 */
4983static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07004984 __releases(tp->lock)
4985 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07004986{
4987 int err;
4988
4989 err = tg3_init_hw(tp, reset_phy);
4990 if (err) {
4991 printk(KERN_ERR PFX "%s: Failed to re-initialize device, "
4992 "aborting.\n", tp->dev->name);
4993 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
4994 tg3_full_unlock(tp);
4995 del_timer_sync(&tp->timer);
4996 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00004997 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07004998 dev_close(tp->dev);
4999 tg3_full_lock(tp, 0);
5000 }
5001 return err;
5002}
5003
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004#ifdef CONFIG_NET_POLL_CONTROLLER
5005static void tg3_poll_controller(struct net_device *dev)
5006{
Matt Carlson4f125f42009-09-01 12:55:02 +00005007 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005008 struct tg3 *tp = netdev_priv(dev);
5009
Matt Carlson4f125f42009-09-01 12:55:02 +00005010 for (i = 0; i < tp->irq_cnt; i++)
5011 tg3_interrupt(tp->napi[i].irq_vec, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012}
5013#endif
5014
David Howellsc4028952006-11-22 14:57:56 +00005015static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016{
David Howellsc4028952006-11-22 14:57:56 +00005017 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005018 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 unsigned int restart_timer;
5020
Michael Chan7faa0062006-02-02 17:29:28 -08005021 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005022
5023 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005024 tg3_full_unlock(tp);
5025 return;
5026 }
5027
5028 tg3_full_unlock(tp);
5029
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005030 tg3_phy_stop(tp);
5031
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 tg3_netif_stop(tp);
5033
David S. Millerf47c11e2005-06-24 20:18:35 -07005034 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035
5036 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5037 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5038
Michael Chandf3e6542006-05-26 17:48:07 -07005039 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5040 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5041 tp->write32_rx_mbox = tg3_write_flush_reg32;
5042 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5043 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5044 }
5045
Michael Chan944d9802005-05-29 14:57:48 -07005046 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005047 err = tg3_init_hw(tp, 1);
5048 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005049 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050
5051 tg3_netif_start(tp);
5052
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 if (restart_timer)
5054 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005055
Michael Chanb9ec6c12006-07-25 16:37:27 -07005056out:
Michael Chan7faa0062006-02-02 17:29:28 -08005057 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005058
5059 if (!err)
5060 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061}
5062
Michael Chanb0408752007-02-13 12:18:30 -08005063static void tg3_dump_short_state(struct tg3 *tp)
5064{
5065 printk(KERN_ERR PFX "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5066 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5067 printk(KERN_ERR PFX "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5068 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
5069}
5070
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071static void tg3_tx_timeout(struct net_device *dev)
5072{
5073 struct tg3 *tp = netdev_priv(dev);
5074
Michael Chanb0408752007-02-13 12:18:30 -08005075 if (netif_msg_tx_err(tp)) {
Michael Chan9f88f292006-12-07 00:22:54 -08005076 printk(KERN_ERR PFX "%s: transmit timed out, resetting\n",
5077 dev->name);
Michael Chanb0408752007-02-13 12:18:30 -08005078 tg3_dump_short_state(tp);
5079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080
5081 schedule_work(&tp->reset_task);
5082}
5083
Michael Chanc58ec932005-09-17 00:46:27 -07005084/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5085static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5086{
5087 u32 base = (u32) mapping & 0xffffffff;
5088
5089 return ((base > 0xffffdcc0) &&
5090 (base + len + 8 < base));
5091}
5092
Michael Chan72f2afb2006-03-06 19:28:35 -08005093/* Test for DMA addresses > 40-bit */
5094static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5095 int len)
5096{
5097#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005098 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Yang Hongyang50cf1562009-04-06 19:01:14 -07005099 return (((u64) mapping + len) > DMA_BIT_MASK(40));
Michael Chan72f2afb2006-03-06 19:28:35 -08005100 return 0;
5101#else
5102 return 0;
5103#endif
5104}
5105
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005106static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
Michael Chan72f2afb2006-03-06 19:28:35 -08005108/* Workaround 4GB and 40-bit hardware DMA bugs. */
5109static int tigon3_dma_hwbug_workaround(struct tg3 *tp, struct sk_buff *skb,
Michael Chanc58ec932005-09-17 00:46:27 -07005110 u32 last_plus_one, u32 *start,
5111 u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005113 struct tg3_napi *tnapi = &tp->napi[0];
Matt Carlson41588ba2008-04-19 18:12:33 -07005114 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005115 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005117 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118
Matt Carlson41588ba2008-04-19 18:12:33 -07005119 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5120 new_skb = skb_copy(skb, GFP_ATOMIC);
5121 else {
5122 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5123
5124 new_skb = skb_copy_expand(skb,
5125 skb_headroom(skb) + more_headroom,
5126 skb_tailroom(skb), GFP_ATOMIC);
5127 }
5128
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005130 ret = -1;
5131 } else {
5132 /* New SKB is guaranteed to be linear. */
5133 entry = *start;
David S. Miller90079ce2008-09-11 04:52:51 -07005134 ret = skb_dma_map(&tp->pdev->dev, new_skb, DMA_TO_DEVICE);
Eric Dumazet042a53a2009-06-05 04:04:16 +00005135 new_addr = skb_shinfo(new_skb)->dma_head;
David S. Miller90079ce2008-09-11 04:52:51 -07005136
Michael Chanc58ec932005-09-17 00:46:27 -07005137 /* Make sure new skb does not cross any 4G boundaries.
5138 * Drop the packet if it does.
5139 */
David S. Miller90079ce2008-09-11 04:52:51 -07005140 if (ret || tg3_4g_overflow_test(new_addr, new_skb->len)) {
David S. Miller638266f2008-09-11 15:45:19 -07005141 if (!ret)
5142 skb_dma_unmap(&tp->pdev->dev, new_skb,
5143 DMA_TO_DEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005144 ret = -1;
5145 dev_kfree_skb(new_skb);
5146 new_skb = NULL;
5147 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005148 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005149 base_flags, 1 | (mss << 1));
5150 *start = NEXT_TX(entry);
5151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 }
5153
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 /* Now clean up the sw ring entries. */
5155 i = 0;
5156 while (entry != last_plus_one) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005157 if (i == 0)
5158 tnapi->tx_buffers[entry].skb = new_skb;
5159 else
5160 tnapi->tx_buffers[entry].skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 entry = NEXT_TX(entry);
5162 i++;
5163 }
5164
David S. Miller90079ce2008-09-11 04:52:51 -07005165 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 dev_kfree_skb(skb);
5167
Michael Chanc58ec932005-09-17 00:46:27 -07005168 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169}
5170
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005171static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 dma_addr_t mapping, int len, u32 flags,
5173 u32 mss_and_is_end)
5174{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005175 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 int is_end = (mss_and_is_end & 0x1);
5177 u32 mss = (mss_and_is_end >> 1);
5178 u32 vlan_tag = 0;
5179
5180 if (is_end)
5181 flags |= TXD_FLAG_END;
5182 if (flags & TXD_FLAG_VLAN) {
5183 vlan_tag = flags >> 16;
5184 flags &= 0xffff;
5185 }
5186 vlan_tag |= (mss << TXD_MSS_SHIFT);
5187
5188 txd->addr_hi = ((u64) mapping >> 32);
5189 txd->addr_lo = ((u64) mapping & 0xffffffff);
5190 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5191 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5192}
5193
Michael Chan5a6f3072006-03-20 22:28:05 -08005194/* hard_start_xmit for devices that don't have any bugs and
5195 * support TG3_FLG2_HW_TSO_2 only.
5196 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005197static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5198 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199{
5200 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005202 struct skb_shared_info *sp;
5203 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005204 struct tg3_napi *tnapi;
5205 struct netdev_queue *txq;
Michael Chan5a6f3072006-03-20 22:28:05 -08005206
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005207 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5208 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
5209 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
5210 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005211
Michael Chan00b70502006-06-17 21:58:45 -07005212 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005213 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005214 * interrupt. Furthermore, IRQ processing runs lockless so we have
5215 * no IRQ context deadlocks to worry about either. Rejoice!
5216 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005217 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005218 if (!netif_tx_queue_stopped(txq)) {
5219 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005220
5221 /* This is a hard error, log it. */
5222 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
5223 "queue awake!\n", dev->name);
5224 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005225 return NETDEV_TX_BUSY;
5226 }
5227
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005228 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005229 base_flags = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005230 mss = 0;
Matt Carlsonc13e3712007-05-05 11:50:04 -07005231 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005232 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005233 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005234
5235 if (skb_header_cloned(skb) &&
5236 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5237 dev_kfree_skb(skb);
5238 goto out_unlock;
5239 }
5240
Michael Chanb0026622006-07-03 19:42:14 -07005241 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005242 hdrlen = skb_headlen(skb) - ETH_HLEN;
Michael Chanb0026622006-07-03 19:42:14 -07005243 else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005244 struct iphdr *iph = ip_hdr(skb);
5245
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005246 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005247 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005248
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005249 iph->check = 0;
5250 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005251 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005252 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005253
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005254 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
5255 mss |= (hdrlen & 0xc) << 12;
5256 if (hdrlen & 0x10)
5257 base_flags |= 0x00000010;
5258 base_flags |= (hdrlen & 0x3e0) << 5;
5259 } else
5260 mss |= hdrlen << 9;
5261
Michael Chan5a6f3072006-03-20 22:28:05 -08005262 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5263 TXD_FLAG_CPU_POST_DMA);
5264
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005265 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005266
Michael Chan5a6f3072006-03-20 22:28:05 -08005267 }
Patrick McHardy84fa7932006-08-29 16:44:56 -07005268 else if (skb->ip_summed == CHECKSUM_PARTIAL)
Michael Chan5a6f3072006-03-20 22:28:05 -08005269 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Michael Chan5a6f3072006-03-20 22:28:05 -08005270#if TG3_VLAN_TAG_USED
5271 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5272 base_flags |= (TXD_FLAG_VLAN |
5273 (vlan_tx_tag_get(skb) << 16));
5274#endif
5275
David S. Miller90079ce2008-09-11 04:52:51 -07005276 if (skb_dma_map(&tp->pdev->dev, skb, DMA_TO_DEVICE)) {
5277 dev_kfree_skb(skb);
5278 goto out_unlock;
5279 }
5280
5281 sp = skb_shinfo(skb);
5282
Eric Dumazet042a53a2009-06-05 04:04:16 +00005283 mapping = sp->dma_head;
Michael Chan5a6f3072006-03-20 22:28:05 -08005284
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005285 tnapi->tx_buffers[entry].skb = skb;
Michael Chan5a6f3072006-03-20 22:28:05 -08005286
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005287 len = skb_headlen(skb);
5288
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005289 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
5290 !mss && skb->len > ETH_DATA_LEN)
5291 base_flags |= TXD_FLAG_JMB_PKT;
5292
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005293 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005294 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5295
5296 entry = NEXT_TX(entry);
5297
5298 /* Now loop through additional data fragments, and queue them. */
5299 if (skb_shinfo(skb)->nr_frags > 0) {
5300 unsigned int i, last;
5301
5302 last = skb_shinfo(skb)->nr_frags - 1;
5303 for (i = 0; i <= last; i++) {
5304 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5305
5306 len = frag->size;
Eric Dumazet042a53a2009-06-05 04:04:16 +00005307 mapping = sp->dma_maps[i];
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005308 tnapi->tx_buffers[entry].skb = NULL;
Michael Chan5a6f3072006-03-20 22:28:05 -08005309
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005310 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005311 base_flags, (i == last) | (mss << 1));
5312
5313 entry = NEXT_TX(entry);
5314 }
5315 }
5316
5317 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005318 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005319
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005320 tnapi->tx_prod = entry;
5321 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005322 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005323 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005324 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005325 }
5326
5327out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005328 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005329
5330 return NETDEV_TX_OK;
5331}
5332
Stephen Hemminger613573252009-08-31 19:50:58 +00005333static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5334 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005335
5336/* Use GSO to workaround a rare TSO bug that may be triggered when the
5337 * TSO header is greater than 80 bytes.
5338 */
5339static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5340{
5341 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005342 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005343
5344 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005345 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005346 netif_stop_queue(tp->dev);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005347 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005348 return NETDEV_TX_BUSY;
5349
5350 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005351 }
5352
5353 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005354 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005355 goto tg3_tso_bug_end;
5356
5357 do {
5358 nskb = segs;
5359 segs = segs->next;
5360 nskb->next = NULL;
5361 tg3_start_xmit_dma_bug(nskb, tp->dev);
5362 } while (segs);
5363
5364tg3_tso_bug_end:
5365 dev_kfree_skb(skb);
5366
5367 return NETDEV_TX_OK;
5368}
Michael Chan52c0fd82006-06-29 20:15:54 -07005369
Michael Chan5a6f3072006-03-20 22:28:05 -08005370/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5371 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5372 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005373static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5374 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005375{
5376 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005377 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005378 struct skb_shared_info *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005380 dma_addr_t mapping;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005381 struct tg3_napi *tnapi = &tp->napi[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382
5383 len = skb_headlen(skb);
5384
Michael Chan00b70502006-06-17 21:58:45 -07005385 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005386 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005387 * interrupt. Furthermore, IRQ processing runs lockless so we have
5388 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005390 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005391 if (!netif_queue_stopped(dev)) {
5392 netif_stop_queue(dev);
5393
5394 /* This is a hard error, log it. */
5395 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
5396 "queue awake!\n", dev->name);
5397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 return NETDEV_TX_BUSY;
5399 }
5400
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005401 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005403 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 mss = 0;
Matt Carlsonc13e3712007-05-05 11:50:04 -07005406 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005407 struct iphdr *iph;
Michael Chan52c0fd82006-06-29 20:15:54 -07005408 int tcp_opt_len, ip_tcp_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409
5410 if (skb_header_cloned(skb) &&
5411 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5412 dev_kfree_skb(skb);
5413 goto out_unlock;
5414 }
5415
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005416 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005417 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418
Michael Chan52c0fd82006-06-29 20:15:54 -07005419 hdr_len = ip_tcp_len + tcp_opt_len;
5420 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005421 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Michael Chan52c0fd82006-06-29 20:15:54 -07005422 return (tg3_tso_bug(tp, skb));
5423
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5425 TXD_FLAG_CPU_POST_DMA);
5426
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005427 iph = ip_hdr(skb);
5428 iph->check = 0;
5429 iph->tot_len = htons(mss + hdr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005431 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005433 } else
5434 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5435 iph->daddr, 0,
5436 IPPROTO_TCP,
5437 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438
5439 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
5440 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005441 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 int tsflags;
5443
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005444 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 mss |= (tsflags << 11);
5446 }
5447 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005448 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 int tsflags;
5450
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005451 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 base_flags |= tsflags << 12;
5453 }
5454 }
5455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456#if TG3_VLAN_TAG_USED
5457 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5458 base_flags |= (TXD_FLAG_VLAN |
5459 (vlan_tx_tag_get(skb) << 16));
5460#endif
5461
David S. Miller90079ce2008-09-11 04:52:51 -07005462 if (skb_dma_map(&tp->pdev->dev, skb, DMA_TO_DEVICE)) {
5463 dev_kfree_skb(skb);
5464 goto out_unlock;
5465 }
5466
5467 sp = skb_shinfo(skb);
5468
Eric Dumazet042a53a2009-06-05 04:04:16 +00005469 mapping = sp->dma_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005471 tnapi->tx_buffers[entry].skb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472
5473 would_hit_hwbug = 0;
5474
Matt Carlson41588ba2008-04-19 18:12:33 -07005475 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
5476 would_hit_hwbug = 1;
5477 else if (tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07005478 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005480 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5482
5483 entry = NEXT_TX(entry);
5484
5485 /* Now loop through additional data fragments, and queue them. */
5486 if (skb_shinfo(skb)->nr_frags > 0) {
5487 unsigned int i, last;
5488
5489 last = skb_shinfo(skb)->nr_frags - 1;
5490 for (i = 0; i <= last; i++) {
5491 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5492
5493 len = frag->size;
Eric Dumazet042a53a2009-06-05 04:04:16 +00005494 mapping = sp->dma_maps[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005496 tnapi->tx_buffers[entry].skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497
Michael Chanc58ec932005-09-17 00:46:27 -07005498 if (tg3_4g_overflow_test(mapping, len))
5499 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500
Michael Chan72f2afb2006-03-06 19:28:35 -08005501 if (tg3_40bit_overflow_test(tp, mapping, len))
5502 would_hit_hwbug = 1;
5503
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005505 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 base_flags, (i == last)|(mss << 1));
5507 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005508 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 base_flags, (i == last));
5510
5511 entry = NEXT_TX(entry);
5512 }
5513 }
5514
5515 if (would_hit_hwbug) {
5516 u32 last_plus_one = entry;
5517 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518
Michael Chanc58ec932005-09-17 00:46:27 -07005519 start = entry - 1 - skb_shinfo(skb)->nr_frags;
5520 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521
5522 /* If the workaround fails due to memory/mapping
5523 * failure, silently drop this packet.
5524 */
Michael Chan72f2afb2006-03-06 19:28:35 -08005525 if (tigon3_dma_hwbug_workaround(tp, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07005526 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 goto out_unlock;
5528
5529 entry = start;
5530 }
5531
5532 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005533 tw32_tx_mbox(MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005535 tnapi->tx_prod = entry;
5536 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 netif_stop_queue(dev);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005538 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Michael Chan51b91462005-09-01 17:41:28 -07005539 netif_wake_queue(tp->dev);
5540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541
5542out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005543 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544
5545 return NETDEV_TX_OK;
5546}
5547
5548static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
5549 int new_mtu)
5550{
5551 dev->mtu = new_mtu;
5552
Michael Chanef7f5ec2005-07-25 12:32:25 -07005553 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07005554 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005555 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
5556 ethtool_op_set_tso(dev, 0);
5557 }
5558 else
5559 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
5560 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07005561 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07005562 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07005563 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07005564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565}
5566
5567static int tg3_change_mtu(struct net_device *dev, int new_mtu)
5568{
5569 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005570 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571
5572 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
5573 return -EINVAL;
5574
5575 if (!netif_running(dev)) {
5576 /* We'll just catch it later when the
5577 * device is up'd.
5578 */
5579 tg3_set_mtu(dev, tp, new_mtu);
5580 return 0;
5581 }
5582
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005583 tg3_phy_stop(tp);
5584
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005586
5587 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588
Michael Chan944d9802005-05-29 14:57:48 -07005589 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
5591 tg3_set_mtu(dev, tp, new_mtu);
5592
Michael Chanb9ec6c12006-07-25 16:37:27 -07005593 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594
Michael Chanb9ec6c12006-07-25 16:37:27 -07005595 if (!err)
5596 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
David S. Millerf47c11e2005-06-24 20:18:35 -07005598 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005600 if (!err)
5601 tg3_phy_start(tp);
5602
Michael Chanb9ec6c12006-07-25 16:37:27 -07005603 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604}
5605
Matt Carlson21f581a2009-08-28 14:00:25 +00005606static void tg3_rx_prodring_free(struct tg3 *tp,
5607 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 int i;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005610 struct ring_info *rxp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611
5612 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
Matt Carlson21f581a2009-08-28 14:00:25 +00005613 rxp = &tpr->rx_std_buffers[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
5615 if (rxp->skb == NULL)
5616 continue;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005617
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 pci_unmap_single(tp->pdev,
5619 pci_unmap_addr(rxp, mapping),
Matt Carlson287be122009-08-28 13:58:46 +00005620 tp->rx_pkt_map_sz,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 PCI_DMA_FROMDEVICE);
5622 dev_kfree_skb_any(rxp->skb);
5623 rxp->skb = NULL;
5624 }
5625
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005626 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
5627 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
Matt Carlson21f581a2009-08-28 14:00:25 +00005628 rxp = &tpr->rx_jmb_buffers[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005630 if (rxp->skb == NULL)
5631 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005633 pci_unmap_single(tp->pdev,
5634 pci_unmap_addr(rxp, mapping),
5635 TG3_RX_JMB_MAP_SZ,
5636 PCI_DMA_FROMDEVICE);
5637 dev_kfree_skb_any(rxp->skb);
5638 rxp->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 }
5641}
5642
5643/* Initialize tx/rx rings for packet processing.
5644 *
5645 * The chip has been shut down and the driver detached from
5646 * the networking, so no interrupts or new tx packets will
5647 * end up in the driver. tp->{tx,}lock are held and thus
5648 * we may not sleep.
5649 */
Matt Carlson21f581a2009-08-28 14:00:25 +00005650static int tg3_rx_prodring_alloc(struct tg3 *tp,
5651 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652{
Matt Carlson287be122009-08-28 13:58:46 +00005653 u32 i, rx_pkt_dma_sz;
Matt Carlson17375d22009-08-28 14:02:18 +00005654 struct tg3_napi *tnapi = &tp->napi[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 /* Zero out all descriptors. */
Matt Carlson21f581a2009-08-28 14:00:25 +00005657 memset(tpr->rx_std, 0, TG3_RX_RING_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658
Matt Carlson287be122009-08-28 13:58:46 +00005659 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07005660 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00005661 tp->dev->mtu > ETH_DATA_LEN)
5662 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
5663 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07005664
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 /* Initialize invariants of the rings, we only set this
5666 * stuff once. This works because the card does not
5667 * write into the rx buffer posting rings.
5668 */
5669 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
5670 struct tg3_rx_buffer_desc *rxd;
5671
Matt Carlson21f581a2009-08-28 14:00:25 +00005672 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00005673 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
5675 rxd->opaque = (RXD_OPAQUE_RING_STD |
5676 (i << RXD_OPAQUE_INDEX_SHIFT));
5677 }
5678
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005679 /* Now allocate fresh SKBs for each rx ring. */
5680 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson17375d22009-08-28 14:02:18 +00005681 if (tg3_alloc_rx_skb(tnapi, RXD_OPAQUE_RING_STD, -1, i) < 0) {
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005682 printk(KERN_WARNING PFX
5683 "%s: Using a smaller RX standard ring, "
5684 "only %d out of %d buffers were allocated "
5685 "successfully.\n",
5686 tp->dev->name, i, tp->rx_pending);
5687 if (i == 0)
5688 goto initfail;
5689 tp->rx_pending = i;
5690 break;
5691 }
5692 }
5693
5694 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE))
5695 goto done;
5696
Matt Carlson21f581a2009-08-28 14:00:25 +00005697 memset(tpr->rx_jmb, 0, TG3_RX_JUMBO_RING_BYTES);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005698
Michael Chan0f893dc2005-07-25 12:30:38 -07005699 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
5701 struct tg3_rx_buffer_desc *rxd;
5702
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005703 rxd = &tpr->rx_jmb[i].std;
Matt Carlson287be122009-08-28 13:58:46 +00005704 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
5706 RXD_FLAG_JUMBO;
5707 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
5708 (i << RXD_OPAQUE_INDEX_SHIFT));
5709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Matt Carlson17375d22009-08-28 14:02:18 +00005712 if (tg3_alloc_rx_skb(tnapi, RXD_OPAQUE_RING_JUMBO,
Michael Chan32d8c572006-07-25 16:38:29 -07005713 -1, i) < 0) {
5714 printk(KERN_WARNING PFX
5715 "%s: Using a smaller RX jumbo ring, "
5716 "only %d out of %d buffers were "
5717 "allocated successfully.\n",
5718 tp->dev->name, i, tp->rx_jumbo_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005719 if (i == 0)
5720 goto initfail;
Michael Chan32d8c572006-07-25 16:38:29 -07005721 tp->rx_jumbo_pending = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 break;
Michael Chan32d8c572006-07-25 16:38:29 -07005723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 }
5725 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005726
5727done:
Michael Chan32d8c572006-07-25 16:38:29 -07005728 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005729
5730initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00005731 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005732 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733}
5734
Matt Carlson21f581a2009-08-28 14:00:25 +00005735static void tg3_rx_prodring_fini(struct tg3 *tp,
5736 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737{
Matt Carlson21f581a2009-08-28 14:00:25 +00005738 kfree(tpr->rx_std_buffers);
5739 tpr->rx_std_buffers = NULL;
5740 kfree(tpr->rx_jmb_buffers);
5741 tpr->rx_jmb_buffers = NULL;
5742 if (tpr->rx_std) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 pci_free_consistent(tp->pdev, TG3_RX_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00005744 tpr->rx_std, tpr->rx_std_mapping);
5745 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 }
Matt Carlson21f581a2009-08-28 14:00:25 +00005747 if (tpr->rx_jmb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 pci_free_consistent(tp->pdev, TG3_RX_JUMBO_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00005749 tpr->rx_jmb, tpr->rx_jmb_mapping);
5750 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005752}
5753
Matt Carlson21f581a2009-08-28 14:00:25 +00005754static int tg3_rx_prodring_init(struct tg3 *tp,
5755 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005756{
Matt Carlson21f581a2009-08-28 14:00:25 +00005757 tpr->rx_std_buffers = kzalloc(sizeof(struct ring_info) *
5758 TG3_RX_RING_SIZE, GFP_KERNEL);
5759 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005760 return -ENOMEM;
5761
Matt Carlson21f581a2009-08-28 14:00:25 +00005762 tpr->rx_std = pci_alloc_consistent(tp->pdev, TG3_RX_RING_BYTES,
5763 &tpr->rx_std_mapping);
5764 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005765 goto err_out;
5766
5767 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson21f581a2009-08-28 14:00:25 +00005768 tpr->rx_jmb_buffers = kzalloc(sizeof(struct ring_info) *
5769 TG3_RX_JUMBO_RING_SIZE,
5770 GFP_KERNEL);
5771 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005772 goto err_out;
5773
Matt Carlson21f581a2009-08-28 14:00:25 +00005774 tpr->rx_jmb = pci_alloc_consistent(tp->pdev,
5775 TG3_RX_JUMBO_RING_BYTES,
5776 &tpr->rx_jmb_mapping);
5777 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005778 goto err_out;
5779 }
5780
5781 return 0;
5782
5783err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00005784 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005785 return -ENOMEM;
5786}
5787
5788/* Free up pending packets in all rx/tx rings.
5789 *
5790 * The chip has been shut down and the driver detached from
5791 * the networking, so no interrupts or new tx packets will
5792 * end up in the driver. tp->{tx,}lock is not held and we are not
5793 * in an interrupt context and thus may sleep.
5794 */
5795static void tg3_free_rings(struct tg3 *tp)
5796{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005797 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005798
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005799 for (j = 0; j < tp->irq_cnt; j++) {
5800 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005801
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005802 if (!tnapi->tx_buffers)
5803 continue;
5804
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005805 for (i = 0; i < TG3_TX_RING_SIZE; ) {
5806 struct tx_ring_info *txp;
5807 struct sk_buff *skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005808
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005809 txp = &tnapi->tx_buffers[i];
5810 skb = txp->skb;
5811
5812 if (skb == NULL) {
5813 i++;
5814 continue;
5815 }
5816
5817 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
5818
5819 txp->skb = NULL;
5820
5821 i += skb_shinfo(skb)->nr_frags + 1;
5822
5823 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005824 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005825 }
5826
Matt Carlson21f581a2009-08-28 14:00:25 +00005827 tg3_rx_prodring_free(tp, &tp->prodring[0]);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005828}
5829
5830/* Initialize tx/rx rings for packet processing.
5831 *
5832 * The chip has been shut down and the driver detached from
5833 * the networking, so no interrupts or new tx packets will
5834 * end up in the driver. tp->{tx,}lock are held and thus
5835 * we may not sleep.
5836 */
5837static int tg3_init_rings(struct tg3 *tp)
5838{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005839 int i;
Matt Carlson72334482009-08-28 14:03:01 +00005840
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005841 /* Free up all the SKBs. */
5842 tg3_free_rings(tp);
5843
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005844 for (i = 0; i < tp->irq_cnt; i++) {
5845 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005846
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005847 tnapi->last_tag = 0;
5848 tnapi->last_irq_tag = 0;
5849 tnapi->hw_status->status = 0;
5850 tnapi->hw_status->status_tag = 0;
5851 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
5852
5853 tnapi->tx_prod = 0;
5854 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005855 if (tnapi->tx_ring)
5856 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005857
5858 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005859 if (tnapi->rx_rcb)
5860 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005861 }
Matt Carlson72334482009-08-28 14:03:01 +00005862
Matt Carlson21f581a2009-08-28 14:00:25 +00005863 return tg3_rx_prodring_alloc(tp, &tp->prodring[0]);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005864}
5865
5866/*
5867 * Must not be invoked with interrupt sources disabled and
5868 * the hardware shutdown down.
5869 */
5870static void tg3_free_consistent(struct tg3 *tp)
5871{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005872 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00005873
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005874 for (i = 0; i < tp->irq_cnt; i++) {
5875 struct tg3_napi *tnapi = &tp->napi[i];
5876
5877 if (tnapi->tx_ring) {
5878 pci_free_consistent(tp->pdev, TG3_TX_RING_BYTES,
5879 tnapi->tx_ring, tnapi->tx_desc_mapping);
5880 tnapi->tx_ring = NULL;
5881 }
5882
5883 kfree(tnapi->tx_buffers);
5884 tnapi->tx_buffers = NULL;
5885
5886 if (tnapi->rx_rcb) {
5887 pci_free_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
5888 tnapi->rx_rcb,
5889 tnapi->rx_rcb_mapping);
5890 tnapi->rx_rcb = NULL;
5891 }
5892
5893 if (tnapi->hw_status) {
5894 pci_free_consistent(tp->pdev, TG3_HW_STATUS_SIZE,
5895 tnapi->hw_status,
5896 tnapi->status_mapping);
5897 tnapi->hw_status = NULL;
5898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005900
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 if (tp->hw_stats) {
5902 pci_free_consistent(tp->pdev, sizeof(struct tg3_hw_stats),
5903 tp->hw_stats, tp->stats_mapping);
5904 tp->hw_stats = NULL;
5905 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005906
Matt Carlson21f581a2009-08-28 14:00:25 +00005907 tg3_rx_prodring_fini(tp, &tp->prodring[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908}
5909
5910/*
5911 * Must not be invoked with interrupt sources disabled and
5912 * the hardware shutdown down. Can sleep.
5913 */
5914static int tg3_alloc_consistent(struct tg3 *tp)
5915{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005916 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00005917
Matt Carlson21f581a2009-08-28 14:00:25 +00005918 if (tg3_rx_prodring_init(tp, &tp->prodring[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 return -ENOMEM;
5920
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 tp->hw_stats = pci_alloc_consistent(tp->pdev,
5922 sizeof(struct tg3_hw_stats),
5923 &tp->stats_mapping);
5924 if (!tp->hw_stats)
5925 goto err_out;
5926
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
5928
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005929 for (i = 0; i < tp->irq_cnt; i++) {
5930 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005931 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005932
5933 tnapi->hw_status = pci_alloc_consistent(tp->pdev,
5934 TG3_HW_STATUS_SIZE,
5935 &tnapi->status_mapping);
5936 if (!tnapi->hw_status)
5937 goto err_out;
5938
5939 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005940 sblk = tnapi->hw_status;
5941
5942 /*
5943 * When RSS is enabled, the status block format changes
5944 * slightly. The "rx_jumbo_consumer", "reserved",
5945 * and "rx_mini_consumer" members get mapped to the
5946 * other three rx return ring producer indexes.
5947 */
5948 switch (i) {
5949 default:
5950 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
5951 break;
5952 case 2:
5953 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
5954 break;
5955 case 3:
5956 tnapi->rx_rcb_prod_idx = &sblk->reserved;
5957 break;
5958 case 4:
5959 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
5960 break;
5961 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005962
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005963 /*
5964 * If multivector RSS is enabled, vector 0 does not handle
5965 * rx or tx interrupts. Don't allocate any resources for it.
5966 */
5967 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
5968 continue;
5969
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005970 tnapi->rx_rcb = pci_alloc_consistent(tp->pdev,
5971 TG3_RX_RCB_RING_BYTES(tp),
5972 &tnapi->rx_rcb_mapping);
5973 if (!tnapi->rx_rcb)
5974 goto err_out;
5975
5976 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
5977
5978 tnapi->tx_buffers = kzalloc(sizeof(struct tx_ring_info) *
5979 TG3_TX_RING_SIZE, GFP_KERNEL);
5980 if (!tnapi->tx_buffers)
5981 goto err_out;
5982
5983 tnapi->tx_ring = pci_alloc_consistent(tp->pdev,
5984 TG3_TX_RING_BYTES,
5985 &tnapi->tx_desc_mapping);
5986 if (!tnapi->tx_ring)
5987 goto err_out;
5988 }
5989
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 return 0;
5991
5992err_out:
5993 tg3_free_consistent(tp);
5994 return -ENOMEM;
5995}
5996
5997#define MAX_WAIT_CNT 1000
5998
5999/* To stop a block, clear the enable bit and poll till it
6000 * clears. tp->lock is held.
6001 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006002static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003{
6004 unsigned int i;
6005 u32 val;
6006
6007 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6008 switch (ofs) {
6009 case RCVLSC_MODE:
6010 case DMAC_MODE:
6011 case MBFREE_MODE:
6012 case BUFMGR_MODE:
6013 case MEMARB_MODE:
6014 /* We can't enable/disable these bits of the
6015 * 5705/5750, just say success.
6016 */
6017 return 0;
6018
6019 default:
6020 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 }
6023
6024 val = tr32(ofs);
6025 val &= ~enable_bit;
6026 tw32_f(ofs, val);
6027
6028 for (i = 0; i < MAX_WAIT_CNT; i++) {
6029 udelay(100);
6030 val = tr32(ofs);
6031 if ((val & enable_bit) == 0)
6032 break;
6033 }
6034
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006035 if (i == MAX_WAIT_CNT && !silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 printk(KERN_ERR PFX "tg3_stop_block timed out, "
6037 "ofs=%lx enable_bit=%x\n",
6038 ofs, enable_bit);
6039 return -ENODEV;
6040 }
6041
6042 return 0;
6043}
6044
6045/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006046static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047{
6048 int i, err;
6049
6050 tg3_disable_ints(tp);
6051
6052 tp->rx_mode &= ~RX_MODE_ENABLE;
6053 tw32_f(MAC_RX_MODE, tp->rx_mode);
6054 udelay(10);
6055
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006056 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6057 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6058 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6059 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6060 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6061 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006063 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6064 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6065 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6066 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6067 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6068 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6069 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070
6071 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6072 tw32_f(MAC_MODE, tp->mac_mode);
6073 udelay(40);
6074
6075 tp->tx_mode &= ~TX_MODE_ENABLE;
6076 tw32_f(MAC_TX_MODE, tp->tx_mode);
6077
6078 for (i = 0; i < MAX_WAIT_CNT; i++) {
6079 udelay(100);
6080 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6081 break;
6082 }
6083 if (i >= MAX_WAIT_CNT) {
6084 printk(KERN_ERR PFX "tg3_abort_hw timed out for %s, "
6085 "TX_MODE_ENABLE will not clear MAC_TX_MODE=%08x\n",
6086 tp->dev->name, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006087 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 }
6089
Michael Chane6de8ad2005-05-05 14:42:41 -07006090 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006091 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6092 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
6094 tw32(FTQ_RESET, 0xffffffff);
6095 tw32(FTQ_RESET, 0x00000000);
6096
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006097 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6098 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006100 for (i = 0; i < tp->irq_cnt; i++) {
6101 struct tg3_napi *tnapi = &tp->napi[i];
6102 if (tnapi->hw_status)
6103 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 if (tp->hw_stats)
6106 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6107
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 return err;
6109}
6110
Matt Carlson0d3031d2007-10-10 18:02:43 -07006111static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6112{
6113 int i;
6114 u32 apedata;
6115
6116 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6117 if (apedata != APE_SEG_SIG_MAGIC)
6118 return;
6119
6120 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006121 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006122 return;
6123
6124 /* Wait for up to 1 millisecond for APE to service previous event. */
6125 for (i = 0; i < 10; i++) {
6126 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6127 return;
6128
6129 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6130
6131 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6132 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6133 event | APE_EVENT_STATUS_EVENT_PENDING);
6134
6135 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6136
6137 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6138 break;
6139
6140 udelay(100);
6141 }
6142
6143 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6144 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6145}
6146
6147static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6148{
6149 u32 event;
6150 u32 apedata;
6151
6152 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6153 return;
6154
6155 switch (kind) {
6156 case RESET_KIND_INIT:
6157 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6158 APE_HOST_SEG_SIG_MAGIC);
6159 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6160 APE_HOST_SEG_LEN_MAGIC);
6161 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6162 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6163 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
6164 APE_HOST_DRIVER_ID_MAGIC);
6165 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6166 APE_HOST_BEHAV_NO_PHYLOCK);
6167
6168 event = APE_EVENT_STATUS_STATE_START;
6169 break;
6170 case RESET_KIND_SHUTDOWN:
Matt Carlsonb2aee152008-11-03 16:51:11 -08006171 /* With the interface we are currently using,
6172 * APE does not track driver state. Wiping
6173 * out the HOST SEGMENT SIGNATURE forces
6174 * the APE to assume OS absent status.
6175 */
6176 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
6177
Matt Carlson0d3031d2007-10-10 18:02:43 -07006178 event = APE_EVENT_STATUS_STATE_UNLOAD;
6179 break;
6180 case RESET_KIND_SUSPEND:
6181 event = APE_EVENT_STATUS_STATE_SUSPEND;
6182 break;
6183 default:
6184 return;
6185 }
6186
6187 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6188
6189 tg3_ape_send_event(tp, event);
6190}
6191
Michael Chane6af3012005-04-21 17:12:05 -07006192/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6194{
David S. Millerf49639e2006-06-09 11:58:36 -07006195 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6196 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197
6198 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6199 switch (kind) {
6200 case RESET_KIND_INIT:
6201 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6202 DRV_STATE_START);
6203 break;
6204
6205 case RESET_KIND_SHUTDOWN:
6206 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6207 DRV_STATE_UNLOAD);
6208 break;
6209
6210 case RESET_KIND_SUSPEND:
6211 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6212 DRV_STATE_SUSPEND);
6213 break;
6214
6215 default:
6216 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006219
6220 if (kind == RESET_KIND_INIT ||
6221 kind == RESET_KIND_SUSPEND)
6222 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223}
6224
6225/* tp->lock is held. */
6226static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6227{
6228 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6229 switch (kind) {
6230 case RESET_KIND_INIT:
6231 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6232 DRV_STATE_START_DONE);
6233 break;
6234
6235 case RESET_KIND_SHUTDOWN:
6236 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6237 DRV_STATE_UNLOAD_DONE);
6238 break;
6239
6240 default:
6241 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006244
6245 if (kind == RESET_KIND_SHUTDOWN)
6246 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247}
6248
6249/* tp->lock is held. */
6250static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6251{
6252 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
6253 switch (kind) {
6254 case RESET_KIND_INIT:
6255 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6256 DRV_STATE_START);
6257 break;
6258
6259 case RESET_KIND_SHUTDOWN:
6260 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6261 DRV_STATE_UNLOAD);
6262 break;
6263
6264 case RESET_KIND_SUSPEND:
6265 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6266 DRV_STATE_SUSPEND);
6267 break;
6268
6269 default:
6270 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 }
6273}
6274
Michael Chan7a6f4362006-09-27 16:03:31 -07006275static int tg3_poll_fw(struct tg3 *tp)
6276{
6277 int i;
6278 u32 val;
6279
Michael Chanb5d37722006-09-27 16:06:21 -07006280 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006281 /* Wait up to 20ms for init done. */
6282 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006283 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6284 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006285 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006286 }
6287 return -ENODEV;
6288 }
6289
Michael Chan7a6f4362006-09-27 16:03:31 -07006290 /* Wait for firmware initialization to complete. */
6291 for (i = 0; i < 100000; i++) {
6292 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6293 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6294 break;
6295 udelay(10);
6296 }
6297
6298 /* Chip might not be fitted with firmware. Some Sun onboard
6299 * parts are configured like that. So don't signal the timeout
6300 * of the above loop as an error, but do report the lack of
6301 * running firmware once.
6302 */
6303 if (i >= 100000 &&
6304 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6305 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6306
6307 printk(KERN_INFO PFX "%s: No firmware running.\n",
6308 tp->dev->name);
6309 }
6310
6311 return 0;
6312}
6313
Michael Chanee6a99b2007-07-18 21:49:10 -07006314/* Save PCI command register before chip reset */
6315static void tg3_save_pci_state(struct tg3 *tp)
6316{
Matt Carlson8a6eac92007-10-21 16:17:55 -07006317 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006318}
6319
6320/* Restore PCI state after chip reset */
6321static void tg3_restore_pci_state(struct tg3 *tp)
6322{
6323 u32 val;
6324
6325 /* Re-enable indirect register accesses. */
6326 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6327 tp->misc_host_ctrl);
6328
6329 /* Set MAX PCI retry to zero. */
6330 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6331 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6332 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6333 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006334 /* Allow reads and writes to the APE register and memory space. */
6335 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6336 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
6337 PCISTATE_ALLOW_APE_SHMEM_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07006338 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6339
Matt Carlson8a6eac92007-10-21 16:17:55 -07006340 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006341
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006342 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6343 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
6344 pcie_set_readrq(tp->pdev, 4096);
6345 else {
6346 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6347 tp->pci_cacheline_sz);
6348 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6349 tp->pci_lat_timer);
6350 }
Michael Chan114342f2007-10-15 02:12:26 -07006351 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08006352
Michael Chanee6a99b2007-07-18 21:49:10 -07006353 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08006354 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07006355 u16 pcix_cmd;
6356
6357 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6358 &pcix_cmd);
6359 pcix_cmd &= ~PCI_X_CMD_ERO;
6360 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6361 pcix_cmd);
6362 }
Michael Chanee6a99b2007-07-18 21:49:10 -07006363
6364 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07006365
6366 /* Chip reset on 5780 will reset MSI enable bit,
6367 * so need to restore it.
6368 */
6369 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
6370 u16 ctrl;
6371
6372 pci_read_config_word(tp->pdev,
6373 tp->msi_cap + PCI_MSI_FLAGS,
6374 &ctrl);
6375 pci_write_config_word(tp->pdev,
6376 tp->msi_cap + PCI_MSI_FLAGS,
6377 ctrl | PCI_MSI_FLAGS_ENABLE);
6378 val = tr32(MSGINT_MODE);
6379 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
6380 }
6381 }
6382}
6383
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384static void tg3_stop_fw(struct tg3 *);
6385
6386/* tp->lock is held. */
6387static int tg3_chip_reset(struct tg3 *tp)
6388{
6389 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07006390 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00006391 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392
David S. Millerf49639e2006-06-09 11:58:36 -07006393 tg3_nvram_lock(tp);
6394
Matt Carlson158d7ab2008-05-29 01:37:54 -07006395 tg3_mdio_stop(tp);
6396
Matt Carlson77b483f2008-08-15 14:07:24 -07006397 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
6398
David S. Millerf49639e2006-06-09 11:58:36 -07006399 /* No matching tg3_nvram_unlock() after this because
6400 * chip reset below will undo the nvram lock.
6401 */
6402 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403
Michael Chanee6a99b2007-07-18 21:49:10 -07006404 /* GRC_MISC_CFG core clock reset will clear the memory
6405 * enable bit in PCI register 4 and the MSI enable bit
6406 * on some chips, so we save relevant registers here.
6407 */
6408 tg3_save_pci_state(tp);
6409
Michael Chand9ab5ad12006-03-20 22:27:35 -08006410 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08006411 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08006412 tw32(GRC_FASTBOOT_PC, 0);
6413
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 /*
6415 * We must avoid the readl() that normally takes place.
6416 * It locks machines, causes machine checks, and other
6417 * fun things. So, temporarily disable the 5701
6418 * hardware workaround, while we do the reset.
6419 */
Michael Chan1ee582d2005-08-09 20:16:46 -07006420 write_op = tp->write32;
6421 if (write_op == tg3_write_flush_reg32)
6422 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423
Michael Chand18edcb2007-03-24 20:57:11 -07006424 /* Prevent the irq handler from reading or writing PCI registers
6425 * during chip reset when the memory enable bit in the PCI command
6426 * register may be cleared. The chip does not generate interrupt
6427 * at this time, but the irq handler may still be called due to irq
6428 * sharing or irqpoll.
6429 */
6430 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006431 for (i = 0; i < tp->irq_cnt; i++) {
6432 struct tg3_napi *tnapi = &tp->napi[i];
6433 if (tnapi->hw_status) {
6434 tnapi->hw_status->status = 0;
6435 tnapi->hw_status->status_tag = 0;
6436 }
6437 tnapi->last_tag = 0;
6438 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07006439 }
Michael Chand18edcb2007-03-24 20:57:11 -07006440 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00006441
6442 for (i = 0; i < tp->irq_cnt; i++)
6443 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07006444
Matt Carlson255ca312009-08-25 10:07:27 +00006445 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6446 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
6447 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
6448 }
6449
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 /* do the reset */
6451 val = GRC_MISC_CFG_CORECLK_RESET;
6452
6453 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
6454 if (tr32(0x7e2c) == 0x60) {
6455 tw32(0x7e2c, 0x20);
6456 }
6457 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
6458 tw32(GRC_MISC_CFG, (1 << 29));
6459 val |= (1 << 29);
6460 }
6461 }
6462
Michael Chanb5d37722006-09-27 16:06:21 -07006463 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6464 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
6465 tw32(GRC_VCPU_EXT_CTRL,
6466 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
6467 }
6468
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6470 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
6471 tw32(GRC_MISC_CFG, val);
6472
Michael Chan1ee582d2005-08-09 20:16:46 -07006473 /* restore 5701 hardware bug workaround write method */
6474 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475
6476 /* Unfortunately, we have to delay before the PCI read back.
6477 * Some 575X chips even will not respond to a PCI cfg access
6478 * when the reset command is given to the chip.
6479 *
6480 * How do these hardware designers expect things to work
6481 * properly if the PCI write is posted for a long period
6482 * of time? It is always necessary to have some method by
6483 * which a register read back can occur to push the write
6484 * out which does the reset.
6485 *
6486 * For most tg3 variants the trick below was working.
6487 * Ho hum...
6488 */
6489 udelay(120);
6490
6491 /* Flush PCI posted writes. The normal MMIO registers
6492 * are inaccessible at this time so this is the only
6493 * way to make this reliably (actually, this is no longer
6494 * the case, see above). I tried to use indirect
6495 * register read/write but this upset some 5701 variants.
6496 */
6497 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
6498
6499 udelay(120);
6500
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006501 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00006502 u16 val16;
6503
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
6505 int i;
6506 u32 cfg_val;
6507
6508 /* Wait for link training to complete. */
6509 for (i = 0; i < 5000; i++)
6510 udelay(100);
6511
6512 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
6513 pci_write_config_dword(tp->pdev, 0xc4,
6514 cfg_val | (1 << 15));
6515 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006516
Matt Carlsone7126992009-08-25 10:08:16 +00006517 /* Clear the "no snoop" and "relaxed ordering" bits. */
6518 pci_read_config_word(tp->pdev,
6519 tp->pcie_cap + PCI_EXP_DEVCTL,
6520 &val16);
6521 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
6522 PCI_EXP_DEVCTL_NOSNOOP_EN);
6523 /*
6524 * Older PCIe devices only support the 128 byte
6525 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006526 */
Matt Carlsone7126992009-08-25 10:08:16 +00006527 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
6528 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784))
6529 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006530 pci_write_config_word(tp->pdev,
6531 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00006532 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006533
6534 pcie_set_readrq(tp->pdev, 4096);
6535
6536 /* Clear error status */
6537 pci_write_config_word(tp->pdev,
6538 tp->pcie_cap + PCI_EXP_DEVSTA,
6539 PCI_EXP_DEVSTA_CED |
6540 PCI_EXP_DEVSTA_NFED |
6541 PCI_EXP_DEVSTA_FED |
6542 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543 }
6544
Michael Chanee6a99b2007-07-18 21:49:10 -07006545 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546
Michael Chand18edcb2007-03-24 20:57:11 -07006547 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
6548
Michael Chanee6a99b2007-07-18 21:49:10 -07006549 val = 0;
6550 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07006551 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07006552 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553
6554 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
6555 tg3_stop_fw(tp);
6556 tw32(0x5000, 0x400);
6557 }
6558
6559 tw32(GRC_MODE, tp->grc_mode);
6560
6561 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01006562 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563
6564 tw32(0xc4, val | (1 << 15));
6565 }
6566
6567 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
6568 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
6569 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
6570 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
6571 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
6572 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
6573 }
6574
6575 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
6576 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
6577 tw32_f(MAC_MODE, tp->mac_mode);
Michael Chan747e8f82005-07-25 12:33:22 -07006578 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
6579 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
6580 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlson3bda1252008-08-15 14:08:22 -07006581 } else if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
6582 tp->mac_mode &= (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
6583 if (tp->mac_mode & MAC_MODE_APE_TX_EN)
6584 tp->mac_mode |= MAC_MODE_TDE_ENABLE;
6585 tw32_f(MAC_MODE, tp->mac_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586 } else
6587 tw32_f(MAC_MODE, 0);
6588 udelay(40);
6589
Matt Carlson77b483f2008-08-15 14:07:24 -07006590 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
6591
Michael Chan7a6f4362006-09-27 16:03:31 -07006592 err = tg3_poll_fw(tp);
6593 if (err)
6594 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595
Matt Carlson0a9140c2009-08-28 12:27:50 +00006596 tg3_mdio_start(tp);
6597
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00006599 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
6600 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
6601 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01006602 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603
6604 tw32(0x7c00, val | (1 << 25));
6605 }
6606
6607 /* Reprobe ASF enable state. */
6608 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
6609 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
6610 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
6611 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
6612 u32 nic_cfg;
6613
6614 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
6615 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
6616 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07006617 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07006618 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
6620 }
6621 }
6622
6623 return 0;
6624}
6625
6626/* tp->lock is held. */
6627static void tg3_stop_fw(struct tg3 *tp)
6628{
Matt Carlson0d3031d2007-10-10 18:02:43 -07006629 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
6630 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07006631 /* Wait for RX cpu to ACK the previous event. */
6632 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633
6634 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07006635
6636 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637
Matt Carlson7c5026a2008-05-02 16:49:29 -07006638 /* Wait for RX cpu to ACK this event. */
6639 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640 }
6641}
6642
6643/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07006644static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645{
6646 int err;
6647
6648 tg3_stop_fw(tp);
6649
Michael Chan944d9802005-05-29 14:57:48 -07006650 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006652 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 err = tg3_chip_reset(tp);
6654
Matt Carlsondaba2a62009-04-20 06:58:52 +00006655 __tg3_set_mac_addr(tp, 0);
6656
Michael Chan944d9802005-05-29 14:57:48 -07006657 tg3_write_sig_legacy(tp, kind);
6658 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659
6660 if (err)
6661 return err;
6662
6663 return 0;
6664}
6665
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666#define RX_CPU_SCRATCH_BASE 0x30000
6667#define RX_CPU_SCRATCH_SIZE 0x04000
6668#define TX_CPU_SCRATCH_BASE 0x34000
6669#define TX_CPU_SCRATCH_SIZE 0x04000
6670
6671/* tp->lock is held. */
6672static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
6673{
6674 int i;
6675
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02006676 BUG_ON(offset == TX_CPU_BASE &&
6677 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678
Michael Chanb5d37722006-09-27 16:06:21 -07006679 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6680 u32 val = tr32(GRC_VCPU_EXT_CTRL);
6681
6682 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
6683 return 0;
6684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 if (offset == RX_CPU_BASE) {
6686 for (i = 0; i < 10000; i++) {
6687 tw32(offset + CPU_STATE, 0xffffffff);
6688 tw32(offset + CPU_MODE, CPU_MODE_HALT);
6689 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
6690 break;
6691 }
6692
6693 tw32(offset + CPU_STATE, 0xffffffff);
6694 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
6695 udelay(10);
6696 } else {
6697 for (i = 0; i < 10000; i++) {
6698 tw32(offset + CPU_STATE, 0xffffffff);
6699 tw32(offset + CPU_MODE, CPU_MODE_HALT);
6700 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
6701 break;
6702 }
6703 }
6704
6705 if (i >= 10000) {
6706 printk(KERN_ERR PFX "tg3_reset_cpu timed out for %s, "
6707 "and %s CPU\n",
6708 tp->dev->name,
6709 (offset == RX_CPU_BASE ? "RX" : "TX"));
6710 return -ENODEV;
6711 }
Michael Chanec41c7d2006-01-17 02:40:55 -08006712
6713 /* Clear firmware's nvram arbitration. */
6714 if (tp->tg3_flags & TG3_FLAG_NVRAM)
6715 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 return 0;
6717}
6718
6719struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006720 unsigned int fw_base;
6721 unsigned int fw_len;
6722 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723};
6724
6725/* tp->lock is held. */
6726static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
6727 int cpu_scratch_size, struct fw_info *info)
6728{
Michael Chanec41c7d2006-01-17 02:40:55 -08006729 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730 void (*write_op)(struct tg3 *, u32, u32);
6731
6732 if (cpu_base == TX_CPU_BASE &&
6733 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
6734 printk(KERN_ERR PFX "tg3_load_firmware_cpu: Trying to load "
6735 "TX cpu firmware on %s which is 5705.\n",
6736 tp->dev->name);
6737 return -EINVAL;
6738 }
6739
6740 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6741 write_op = tg3_write_mem;
6742 else
6743 write_op = tg3_write_indirect_reg32;
6744
Michael Chan1b628152005-05-29 14:59:49 -07006745 /* It is possible that bootcode is still loading at this point.
6746 * Get the nvram lock first before halting the cpu.
6747 */
Michael Chanec41c7d2006-01-17 02:40:55 -08006748 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08006750 if (!lock_err)
6751 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 if (err)
6753 goto out;
6754
6755 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
6756 write_op(tp, cpu_scratch_base + i, 0);
6757 tw32(cpu_base + CPU_STATE, 0xffffffff);
6758 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006759 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006761 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006763 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764
6765 err = 0;
6766
6767out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 return err;
6769}
6770
6771/* tp->lock is held. */
6772static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
6773{
6774 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006775 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 int err, i;
6777
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006778 fw_data = (void *)tp->fw->data;
6779
6780 /* Firmware blob starts with version numbers, followed by
6781 start address and length. We are setting complete length.
6782 length = end_address_of_bss - start_address_of_text.
6783 Remainder is the blob to be loaded contiguously
6784 from start address. */
6785
6786 info.fw_base = be32_to_cpu(fw_data[1]);
6787 info.fw_len = tp->fw->size - 12;
6788 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789
6790 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
6791 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
6792 &info);
6793 if (err)
6794 return err;
6795
6796 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
6797 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
6798 &info);
6799 if (err)
6800 return err;
6801
6802 /* Now startup only the RX cpu. */
6803 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006804 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805
6806 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006807 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808 break;
6809 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
6810 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006811 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 udelay(1000);
6813 }
6814 if (i >= 5) {
6815 printk(KERN_ERR PFX "tg3_load_firmware fails for %s "
6816 "to set RX CPU PC, is %08x should be %08x\n",
6817 tp->dev->name, tr32(RX_CPU_BASE + CPU_PC),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006818 info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 return -ENODEV;
6820 }
6821 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
6822 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
6823
6824 return 0;
6825}
6826
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828
6829/* tp->lock is held. */
6830static int tg3_load_tso_firmware(struct tg3 *tp)
6831{
6832 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006833 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
6835 int err, i;
6836
6837 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
6838 return 0;
6839
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006840 fw_data = (void *)tp->fw->data;
6841
6842 /* Firmware blob starts with version numbers, followed by
6843 start address and length. We are setting complete length.
6844 length = end_address_of_bss - start_address_of_text.
6845 Remainder is the blob to be loaded contiguously
6846 from start address. */
6847
6848 info.fw_base = be32_to_cpu(fw_data[1]);
6849 cpu_scratch_size = tp->fw_len;
6850 info.fw_len = tp->fw->size - 12;
6851 info.fw_data = &fw_data[3];
6852
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854 cpu_base = RX_CPU_BASE;
6855 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857 cpu_base = TX_CPU_BASE;
6858 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
6859 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
6860 }
6861
6862 err = tg3_load_firmware_cpu(tp, cpu_base,
6863 cpu_scratch_base, cpu_scratch_size,
6864 &info);
6865 if (err)
6866 return err;
6867
6868 /* Now startup the cpu. */
6869 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006870 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871
6872 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006873 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874 break;
6875 tw32(cpu_base + CPU_STATE, 0xffffffff);
6876 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006877 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 udelay(1000);
6879 }
6880 if (i >= 5) {
6881 printk(KERN_ERR PFX "tg3_load_tso_firmware fails for %s "
6882 "to set CPU PC, is %08x should be %08x\n",
6883 tp->dev->name, tr32(cpu_base + CPU_PC),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006884 info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885 return -ENODEV;
6886 }
6887 tw32(cpu_base + CPU_STATE, 0xffffffff);
6888 tw32_f(cpu_base + CPU_MODE, 0x00000000);
6889 return 0;
6890}
6891
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893static int tg3_set_mac_addr(struct net_device *dev, void *p)
6894{
6895 struct tg3 *tp = netdev_priv(dev);
6896 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07006897 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898
Michael Chanf9804dd2005-09-27 12:13:10 -07006899 if (!is_valid_ether_addr(addr->sa_data))
6900 return -EINVAL;
6901
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
6903
Michael Chane75f7c92006-03-20 21:33:26 -08006904 if (!netif_running(dev))
6905 return 0;
6906
Michael Chan58712ef2006-04-29 18:58:01 -07006907 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07006908 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07006909
Michael Chan986e0ae2007-05-05 12:10:20 -07006910 addr0_high = tr32(MAC_ADDR_0_HIGH);
6911 addr0_low = tr32(MAC_ADDR_0_LOW);
6912 addr1_high = tr32(MAC_ADDR_1_HIGH);
6913 addr1_low = tr32(MAC_ADDR_1_LOW);
6914
6915 /* Skip MAC addr 1 if ASF is using it. */
6916 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
6917 !(addr1_high == 0 && addr1_low == 0))
6918 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07006919 }
Michael Chan986e0ae2007-05-05 12:10:20 -07006920 spin_lock_bh(&tp->lock);
6921 __tg3_set_mac_addr(tp, skip_mac_1);
6922 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923
Michael Chanb9ec6c12006-07-25 16:37:27 -07006924 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925}
6926
6927/* tp->lock is held. */
6928static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
6929 dma_addr_t mapping, u32 maxlen_flags,
6930 u32 nic_addr)
6931{
6932 tg3_write_mem(tp,
6933 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
6934 ((u64) mapping >> 32));
6935 tg3_write_mem(tp,
6936 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
6937 ((u64) mapping & 0xffffffff));
6938 tg3_write_mem(tp,
6939 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
6940 maxlen_flags);
6941
6942 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
6943 tg3_write_mem(tp,
6944 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
6945 nic_addr);
6946}
6947
6948static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07006949static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07006950{
Matt Carlsonb6080e12009-09-01 13:12:00 +00006951 int i;
6952
6953 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
6954 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
6955 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
6956 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
6957
6958 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
6959 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
6960 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
6961 } else {
6962 tw32(HOSTCC_TXCOL_TICKS, 0);
6963 tw32(HOSTCC_TXMAX_FRAMES, 0);
6964 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
6965
6966 tw32(HOSTCC_RXCOL_TICKS, 0);
6967 tw32(HOSTCC_RXMAX_FRAMES, 0);
6968 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07006969 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00006970
David S. Miller15f98502005-05-18 22:49:26 -07006971 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
6972 u32 val = ec->stats_block_coalesce_usecs;
6973
Matt Carlsonb6080e12009-09-01 13:12:00 +00006974 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
6975 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
6976
David S. Miller15f98502005-05-18 22:49:26 -07006977 if (!netif_carrier_ok(tp->dev))
6978 val = 0;
6979
6980 tw32(HOSTCC_STAT_COAL_TICKS, val);
6981 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00006982
6983 for (i = 0; i < tp->irq_cnt - 1; i++) {
6984 u32 reg;
6985
6986 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
6987 tw32(reg, ec->rx_coalesce_usecs);
6988 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
6989 tw32(reg, ec->tx_coalesce_usecs);
6990 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
6991 tw32(reg, ec->rx_max_coalesced_frames);
6992 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
6993 tw32(reg, ec->tx_max_coalesced_frames);
6994 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
6995 tw32(reg, ec->rx_max_coalesced_frames_irq);
6996 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
6997 tw32(reg, ec->tx_max_coalesced_frames_irq);
6998 }
6999
7000 for (; i < tp->irq_max - 1; i++) {
7001 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
7002 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7003 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
7004 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7005 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7006 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7007 }
David S. Miller15f98502005-05-18 22:49:26 -07007008}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009
7010/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007011static void tg3_rings_reset(struct tg3 *tp)
7012{
7013 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007014 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007015 struct tg3_napi *tnapi = &tp->napi[0];
7016
7017 /* Disable all transmit rings but the first. */
7018 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7019 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
7020 else
7021 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7022
7023 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7024 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7025 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7026 BDINFO_FLAGS_DISABLED);
7027
7028
7029 /* Disable all receive return rings but the first. */
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007030 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
7031 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7032 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007033 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
7034 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
7035 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7036 else
7037 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7038
7039 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7040 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7041 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7042 BDINFO_FLAGS_DISABLED);
7043
7044 /* Disable interrupts */
7045 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7046
7047 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007048 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
7049 for (i = 1; i < TG3_IRQ_MAX_VECS; i++) {
7050 tp->napi[i].tx_prod = 0;
7051 tp->napi[i].tx_cons = 0;
7052 tw32_mailbox(tp->napi[i].prodmbox, 0);
7053 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7054 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7055 }
7056 } else {
7057 tp->napi[0].tx_prod = 0;
7058 tp->napi[0].tx_cons = 0;
7059 tw32_mailbox(tp->napi[0].prodmbox, 0);
7060 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7061 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007062
7063 /* Make sure the NIC-based send BD rings are disabled. */
7064 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7065 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7066 for (i = 0; i < 16; i++)
7067 tw32_tx_mbox(mbox + i * 8, 0);
7068 }
7069
7070 txrcb = NIC_SRAM_SEND_RCB;
7071 rxrcb = NIC_SRAM_RCV_RET_RCB;
7072
7073 /* Clear status block in ram. */
7074 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7075
7076 /* Set status block DMA address */
7077 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7078 ((u64) tnapi->status_mapping >> 32));
7079 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7080 ((u64) tnapi->status_mapping & 0xffffffff));
7081
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007082 if (tnapi->tx_ring) {
7083 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7084 (TG3_TX_RING_SIZE <<
7085 BDINFO_FLAGS_MAXLEN_SHIFT),
7086 NIC_SRAM_TX_BUFFER_DESC);
7087 txrcb += TG3_BDINFO_SIZE;
7088 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007089
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007090 if (tnapi->rx_rcb) {
7091 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7092 (TG3_RX_RCB_RING_SIZE(tp) <<
7093 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7094 rxrcb += TG3_BDINFO_SIZE;
7095 }
7096
7097 stblk = HOSTCC_STATBLCK_RING1;
7098
7099 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7100 u64 mapping = (u64)tnapi->status_mapping;
7101 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7102 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7103
7104 /* Clear status block in ram. */
7105 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7106
7107 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7108 (TG3_TX_RING_SIZE <<
7109 BDINFO_FLAGS_MAXLEN_SHIFT),
7110 NIC_SRAM_TX_BUFFER_DESC);
7111
7112 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7113 (TG3_RX_RCB_RING_SIZE(tp) <<
7114 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7115
7116 stblk += 8;
7117 txrcb += TG3_BDINFO_SIZE;
7118 rxrcb += TG3_BDINFO_SIZE;
7119 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007120}
7121
7122/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007123static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124{
7125 u32 val, rdmac_mode;
7126 int i, err, limit;
Matt Carlson21f581a2009-08-28 14:00:25 +00007127 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128
7129 tg3_disable_ints(tp);
7130
7131 tg3_stop_fw(tp);
7132
7133 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7134
7135 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) {
Michael Chane6de8ad2005-05-05 14:42:41 -07007136 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137 }
7138
Matt Carlsondd477002008-05-25 23:45:58 -07007139 if (reset_phy &&
7140 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB))
Michael Chand4d2c552006-03-20 17:47:20 -08007141 tg3_phy_reset(tp);
7142
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143 err = tg3_chip_reset(tp);
7144 if (err)
7145 return err;
7146
7147 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7148
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007149 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007150 val = tr32(TG3_CPMU_CTRL);
7151 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7152 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007153
7154 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7155 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7156 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7157 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7158
7159 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7160 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7161 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7162 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7163
7164 val = tr32(TG3_CPMU_HST_ACC);
7165 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7166 val |= CPMU_HST_ACC_MACCLK_6_25;
7167 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007168 }
7169
Matt Carlson33466d92009-04-20 06:57:41 +00007170 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7171 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7172 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7173 PCIE_PWR_MGMT_L1_THRESH_4MS;
7174 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007175
7176 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7177 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7178
7179 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007180 }
7181
Matt Carlson255ca312009-08-25 10:07:27 +00007182 if (tp->tg3_flags3 & TG3_FLG3_TOGGLE_10_100_L1PLLPD) {
7183 val = tr32(TG3_PCIE_LNKCTL);
7184 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG)
7185 val |= TG3_PCIE_LNKCTL_L1_PLL_PD_DIS;
7186 else
7187 val &= ~TG3_PCIE_LNKCTL_L1_PLL_PD_DIS;
7188 tw32(TG3_PCIE_LNKCTL, val);
7189 }
7190
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191 /* This works around an issue with Athlon chipsets on
7192 * B3 tigon3 silicon. This bit has no effect on any
7193 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07007194 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195 */
Matt Carlson795d01c2007-10-07 23:28:17 -07007196 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7197 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7198 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7199 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201
7202 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7203 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7204 val = tr32(TG3PCI_PCISTATE);
7205 val |= PCISTATE_RETRY_SAME_DMA;
7206 tw32(TG3PCI_PCISTATE, val);
7207 }
7208
Matt Carlson0d3031d2007-10-10 18:02:43 -07007209 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7210 /* Allow reads and writes to the
7211 * APE register and memory space.
7212 */
7213 val = tr32(TG3PCI_PCISTATE);
7214 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
7215 PCISTATE_ALLOW_APE_SHMEM_WR;
7216 tw32(TG3PCI_PCISTATE, val);
7217 }
7218
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7220 /* Enable some hw fixes. */
7221 val = tr32(TG3PCI_MSI_DATA);
7222 val |= (1 << 26) | (1 << 28) | (1 << 29);
7223 tw32(TG3PCI_MSI_DATA, val);
7224 }
7225
7226 /* Descriptor ring init may make accesses to the
7227 * NIC SRAM area to setup the TX descriptors, so we
7228 * can only do this after the hardware has been
7229 * successfully reset.
7230 */
Michael Chan32d8c572006-07-25 16:38:29 -07007231 err = tg3_init_rings(tp);
7232 if (err)
7233 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234
Matt Carlson9936bcf2007-10-10 18:03:07 -07007235 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007236 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761 &&
7237 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007238 /* This value is determined during the probe time DMA
7239 * engine test, tg3_test_dma.
7240 */
7241 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243
7244 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7245 GRC_MODE_4X_NIC_SEND_RINGS |
7246 GRC_MODE_NO_TX_PHDR_CSUM |
7247 GRC_MODE_NO_RX_PHDR_CSUM);
7248 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07007249
7250 /* Pseudo-header checksum is done by hardware logic and not
7251 * the offload processers, so make the chip do the pseudo-
7252 * header checksums on receive. For transmit it is more
7253 * convenient to do the pseudo-header checksum in software
7254 * as Linux does that on transmit for us in all cases.
7255 */
7256 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257
7258 tw32(GRC_MODE,
7259 tp->grc_mode |
7260 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7261
7262 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7263 val = tr32(GRC_MISC_CFG);
7264 val &= ~0xff;
7265 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7266 tw32(GRC_MISC_CFG, val);
7267
7268 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07007269 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270 /* Do nothing. */
7271 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
7272 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
7273 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
7274 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
7275 else
7276 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
7277 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
7278 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
7279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280 else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
7281 int fw_len;
7282
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007283 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
7285 tw32(BUFMGR_MB_POOL_ADDR,
7286 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
7287 tw32(BUFMGR_MB_POOL_SIZE,
7288 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
7289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290
Michael Chan0f893dc2005-07-25 12:30:38 -07007291 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7293 tp->bufmgr_config.mbuf_read_dma_low_water);
7294 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7295 tp->bufmgr_config.mbuf_mac_rx_low_water);
7296 tw32(BUFMGR_MB_HIGH_WATER,
7297 tp->bufmgr_config.mbuf_high_water);
7298 } else {
7299 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7300 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
7301 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7302 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
7303 tw32(BUFMGR_MB_HIGH_WATER,
7304 tp->bufmgr_config.mbuf_high_water_jumbo);
7305 }
7306 tw32(BUFMGR_DMA_LOW_WATER,
7307 tp->bufmgr_config.dma_low_water);
7308 tw32(BUFMGR_DMA_HIGH_WATER,
7309 tp->bufmgr_config.dma_high_water);
7310
7311 tw32(BUFMGR_MODE, BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE);
7312 for (i = 0; i < 2000; i++) {
7313 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
7314 break;
7315 udelay(10);
7316 }
7317 if (i >= 2000) {
7318 printk(KERN_ERR PFX "tg3_reset_hw cannot enable BUFMGR for %s.\n",
7319 tp->dev->name);
7320 return -ENODEV;
7321 }
7322
7323 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07007324 val = tp->rx_pending / 8;
7325 if (val == 0)
7326 val = 1;
7327 else if (val > tp->rx_std_max_post)
7328 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07007329 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7330 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
7331 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
7332
7333 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
7334 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
7335 }
Michael Chanf92905d2006-06-29 20:14:29 -07007336
7337 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338
7339 /* Initialize TG3_BDINFO's at:
7340 * RCVDBDI_STD_BD: standard eth size rx ring
7341 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
7342 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
7343 *
7344 * like so:
7345 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
7346 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
7347 * ring attribute flags
7348 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
7349 *
7350 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
7351 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
7352 *
7353 * The size of each ring is fixed in the firmware, but the location is
7354 * configurable.
7355 */
7356 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007357 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007359 ((u64) tpr->rx_std_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
7361 NIC_SRAM_RX_BUFFER_DESC);
7362
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007363 /* Disable the mini ring */
7364 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
7366 BDINFO_FLAGS_DISABLED);
7367
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007368 /* Program the jumbo buffer descriptor ring control
7369 * blocks on those devices that have them.
7370 */
Matt Carlson8f666b02009-08-28 13:58:24 +00007371 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007372 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 /* Setup replenish threshold. */
7374 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
7375
Michael Chan0f893dc2005-07-25 12:30:38 -07007376 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007378 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007380 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlson79ed5ac2009-08-28 14:00:55 +00007382 (RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT) |
7383 BDINFO_FLAGS_USE_EXT_RECV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
7385 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
7386 } else {
7387 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
7388 BDINFO_FLAGS_DISABLED);
7389 }
7390
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007391 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
7392 val = (RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT) |
7393 (RX_STD_MAX_SIZE << 2);
7394 else
7395 val = RX_STD_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007396 } else
7397 val = RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT;
7398
7399 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400
Matt Carlson21f581a2009-08-28 14:00:25 +00007401 tpr->rx_std_ptr = tp->rx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402 tw32_rx_mbox(MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007403 tpr->rx_std_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404
Matt Carlson21f581a2009-08-28 14:00:25 +00007405 tpr->rx_jmb_ptr = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
7406 tp->rx_jumbo_pending : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407 tw32_rx_mbox(MAILBOX_RCV_JUMBO_PROD_IDX + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007408 tpr->rx_jmb_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007410 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
7411 tw32(STD_REPLENISH_LWM, 32);
7412 tw32(JMB_REPLENISH_LWM, 16);
7413 }
7414
Matt Carlson2d31eca2009-09-01 12:53:31 +00007415 tg3_rings_reset(tp);
7416
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07007418 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419
7420 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00007421 tw32(MAC_RX_MTU_SIZE,
7422 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423
7424 /* The slot time is changed by tg3_setup_phy if we
7425 * run at gigabit with half duplex.
7426 */
7427 tw32(MAC_TX_LENGTHS,
7428 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
7429 (6 << TX_LENGTHS_IPG_SHIFT) |
7430 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
7431
7432 /* Receive rules. */
7433 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
7434 tw32(RCVLPC_CONFIG, 0x0181);
7435
7436 /* Calculate RDMAC_MODE setting early, we need it to determine
7437 * the RCVLPC_STATE_ENABLE mask.
7438 */
7439 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
7440 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
7441 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
7442 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
7443 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07007444
Matt Carlson57e69832008-05-25 23:48:31 -07007445 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007446 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7447 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07007448 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
7449 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
7450 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
7451
Michael Chan85e94ce2005-04-21 17:05:28 -07007452 /* If statement applies to 5705 and 5750 PCI devices only */
7453 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7454 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7455 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07007457 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
7459 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7460 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
7461 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7462 }
7463 }
7464
Michael Chan85e94ce2005-04-21 17:05:28 -07007465 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
7466 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7467
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08007469 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
7470
7471 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7472 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
7473 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474
7475 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07007476 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
7477 val = tr32(RCVLPC_STATS_ENABLE);
7478 val &= ~RCVLPC_STATSENAB_DACK_FIX;
7479 tw32(RCVLPC_STATS_ENABLE, val);
7480 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
7481 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482 val = tr32(RCVLPC_STATS_ENABLE);
7483 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
7484 tw32(RCVLPC_STATS_ENABLE, val);
7485 } else {
7486 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
7487 }
7488 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
7489 tw32(SNDDATAI_STATSENAB, 0xffffff);
7490 tw32(SNDDATAI_STATSCTRL,
7491 (SNDDATAI_SCTRL_ENABLE |
7492 SNDDATAI_SCTRL_FASTUPD));
7493
7494 /* Setup host coalescing engine. */
7495 tw32(HOSTCC_MODE, 0);
7496 for (i = 0; i < 2000; i++) {
7497 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
7498 break;
7499 udelay(10);
7500 }
7501
Michael Chand244c892005-07-05 14:42:33 -07007502 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7505 /* Status/statistics block address. See tg3_timer,
7506 * the tg3_periodic_fetch_stats call there, and
7507 * tg3_get_stats to see how this works for 5705/5750 chips.
7508 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7510 ((u64) tp->stats_mapping >> 32));
7511 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7512 ((u64) tp->stats_mapping & 0xffffffff));
7513 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00007514
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00007516
7517 /* Clear statistics and status block memory areas */
7518 for (i = NIC_SRAM_STATS_BLK;
7519 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
7520 i += sizeof(u32)) {
7521 tg3_write_mem(tp, i, 0);
7522 udelay(40);
7523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524 }
7525
7526 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
7527
7528 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
7529 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
7530 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7531 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
7532
Michael Chanc94e3942005-09-27 12:12:42 -07007533 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
7534 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
7535 /* reset to prevent losing 1st rx packet intermittently */
7536 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7537 udelay(10);
7538 }
7539
Matt Carlson3bda1252008-08-15 14:08:22 -07007540 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7541 tp->mac_mode &= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
7542 else
7543 tp->mac_mode = 0;
7544 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07007546 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
7547 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
7548 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
7549 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
7551 udelay(40);
7552
Michael Chan314fba32005-04-21 17:07:04 -07007553 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08007554 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07007555 * register to preserve the GPIO settings for LOMs. The GPIOs,
7556 * whether used as inputs or outputs, are set by boot code after
7557 * reset.
7558 */
Michael Chan9d26e212006-12-07 00:21:14 -08007559 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07007560 u32 gpio_mask;
7561
Michael Chan9d26e212006-12-07 00:21:14 -08007562 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
7563 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
7564 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07007565
7566 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7567 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
7568 GRC_LCLCTRL_GPIO_OUTPUT3;
7569
Michael Chanaf36e6b2006-03-23 01:28:06 -08007570 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
7571 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
7572
Gary Zambranoaaf84462007-05-05 11:51:45 -07007573 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07007574 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
7575
7576 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08007577 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
7578 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
7579 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07007580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
7582 udelay(100);
7583
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007584 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX) {
7585 val = tr32(MSGINT_MODE);
7586 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
7587 tw32(MSGINT_MODE, val);
7588 }
7589
Linus Torvalds1da177e2005-04-16 15:20:36 -07007590 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7591 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
7592 udelay(40);
7593 }
7594
7595 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
7596 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
7597 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
7598 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
7599 WDMAC_MODE_LNGREAD_ENAB);
7600
Michael Chan85e94ce2005-04-21 17:05:28 -07007601 /* If statement applies to 5705 and 5750 PCI devices only */
7602 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7603 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7604 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
Matt Carlson29ea0952009-08-25 10:07:54 +00007605 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07007606 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
7607 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
7608 /* nothing */
7609 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7610 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
7611 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
7612 val |= WDMAC_MODE_RX_ACCEL;
7613 }
7614 }
7615
Michael Chand9ab5ad12006-03-20 22:27:35 -08007616 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08007617 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07007618 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08007619
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620 tw32_f(WDMAC_MODE, val);
7621 udelay(40);
7622
Matt Carlson9974a352007-10-07 23:27:28 -07007623 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
7624 u16 pcix_cmd;
7625
7626 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7627 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07007629 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
7630 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07007632 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
7633 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634 }
Matt Carlson9974a352007-10-07 23:27:28 -07007635 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7636 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007637 }
7638
7639 tw32_f(RDMAC_MODE, rdmac_mode);
7640 udelay(40);
7641
7642 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
7643 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7644 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07007645
7646 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
7647 tw32(SNDDATAC_MODE,
7648 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
7649 else
7650 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
7651
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
7653 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
7654 tw32(RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ);
7655 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7657 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007658 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
7659 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
7660 val |= SNDBDI_MODE_MULTI_TXQ_EN;
7661 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007662 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
7663
7664 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
7665 err = tg3_load_5701_a0_firmware_fix(tp);
7666 if (err)
7667 return err;
7668 }
7669
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
7671 err = tg3_load_tso_firmware(tp);
7672 if (err)
7673 return err;
7674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007675
7676 tp->tx_mode = TX_MODE_ENABLE;
7677 tw32_f(MAC_TX_MODE, tp->tx_mode);
7678 udelay(100);
7679
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007680 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
7681 u32 reg = MAC_RSS_INDIR_TBL_0;
7682 u8 *ent = (u8 *)&val;
7683
7684 /* Setup the indirection table */
7685 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
7686 int idx = i % sizeof(val);
7687
7688 ent[idx] = i % (tp->irq_cnt - 1);
7689 if (idx == sizeof(val) - 1) {
7690 tw32(reg, val);
7691 reg += 4;
7692 }
7693 }
7694
7695 /* Setup the "secret" hash key. */
7696 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
7697 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
7698 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
7699 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
7700 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
7701 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
7702 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
7703 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
7704 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
7705 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
7706 }
7707
Linus Torvalds1da177e2005-04-16 15:20:36 -07007708 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08007709 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08007710 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
7711
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007712 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
7713 tp->rx_mode |= RX_MODE_RSS_ENABLE |
7714 RX_MODE_RSS_ITBL_HASH_BITS_7 |
7715 RX_MODE_RSS_IPV6_HASH_EN |
7716 RX_MODE_RSS_TCP_IPV6_HASH_EN |
7717 RX_MODE_RSS_IPV4_HASH_EN |
7718 RX_MODE_RSS_TCP_IPV4_HASH_EN;
7719
Linus Torvalds1da177e2005-04-16 15:20:36 -07007720 tw32_f(MAC_RX_MODE, tp->rx_mode);
7721 udelay(10);
7722
Linus Torvalds1da177e2005-04-16 15:20:36 -07007723 tw32(MAC_LED_CTRL, tp->led_ctrl);
7724
7725 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Michael Chanc94e3942005-09-27 12:12:42 -07007726 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007727 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7728 udelay(10);
7729 }
7730 tw32_f(MAC_RX_MODE, tp->rx_mode);
7731 udelay(10);
7732
7733 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
7734 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
7735 !(tp->tg3_flags2 & TG3_FLG2_SERDES_PREEMPHASIS)) {
7736 /* Set drive transmission level to 1.2V */
7737 /* only if the signal pre-emphasis bit is not set */
7738 val = tr32(MAC_SERDES_CFG);
7739 val &= 0xfffff000;
7740 val |= 0x880;
7741 tw32(MAC_SERDES_CFG, val);
7742 }
7743 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
7744 tw32(MAC_SERDES_CFG, 0x616000);
7745 }
7746
7747 /* Prevent chip from dropping frames when flow control
7748 * is enabled.
7749 */
7750 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, 2);
7751
7752 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
7753 (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
7754 /* Use hardware link auto-negotiation */
7755 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
7756 }
7757
Michael Chand4d2c552006-03-20 17:47:20 -08007758 if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
7759 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
7760 u32 tmp;
7761
7762 tmp = tr32(SERDES_RX_CTRL);
7763 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
7764 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
7765 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
7766 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
7767 }
7768
Matt Carlsondd477002008-05-25 23:45:58 -07007769 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
7770 if (tp->link_config.phy_is_low_power) {
7771 tp->link_config.phy_is_low_power = 0;
7772 tp->link_config.speed = tp->link_config.orig_speed;
7773 tp->link_config.duplex = tp->link_config.orig_duplex;
7774 tp->link_config.autoneg = tp->link_config.orig_autoneg;
7775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776
Matt Carlsondd477002008-05-25 23:45:58 -07007777 err = tg3_setup_phy(tp, 0);
7778 if (err)
7779 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007780
Matt Carlsondd477002008-05-25 23:45:58 -07007781 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +00007782 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07007783 u32 tmp;
7784
7785 /* Clear CRC stats. */
7786 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
7787 tg3_writephy(tp, MII_TG3_TEST1,
7788 tmp | MII_TG3_TEST1_CRC_EN);
7789 tg3_readphy(tp, 0x14, &tmp);
7790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791 }
7792 }
7793
7794 __tg3_set_rx_mode(tp->dev);
7795
7796 /* Initialize receive rules. */
7797 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
7798 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
7799 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
7800 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
7801
Michael Chan4cf78e42005-07-25 12:29:19 -07007802 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07007803 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804 limit = 8;
7805 else
7806 limit = 16;
7807 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
7808 limit -= 4;
7809 switch (limit) {
7810 case 16:
7811 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
7812 case 15:
7813 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
7814 case 14:
7815 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
7816 case 13:
7817 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
7818 case 12:
7819 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
7820 case 11:
7821 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
7822 case 10:
7823 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
7824 case 9:
7825 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
7826 case 8:
7827 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
7828 case 7:
7829 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
7830 case 6:
7831 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
7832 case 5:
7833 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
7834 case 4:
7835 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
7836 case 3:
7837 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
7838 case 2:
7839 case 1:
7840
7841 default:
7842 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844
Matt Carlson9ce768e2007-10-11 19:49:11 -07007845 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7846 /* Write our heartbeat update interval to APE. */
7847 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
7848 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07007849
Linus Torvalds1da177e2005-04-16 15:20:36 -07007850 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
7851
Linus Torvalds1da177e2005-04-16 15:20:36 -07007852 return 0;
7853}
7854
7855/* Called at device open time to get the chip ready for
7856 * packet processing. Invoked with tp->lock held.
7857 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007858static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007859{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007860 tg3_switch_clocks(tp);
7861
7862 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
7863
Matt Carlson2f751b62008-08-04 23:17:34 -07007864 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865}
7866
7867#define TG3_STAT_ADD32(PSTAT, REG) \
7868do { u32 __val = tr32(REG); \
7869 (PSTAT)->low += __val; \
7870 if ((PSTAT)->low < __val) \
7871 (PSTAT)->high += 1; \
7872} while (0)
7873
7874static void tg3_periodic_fetch_stats(struct tg3 *tp)
7875{
7876 struct tg3_hw_stats *sp = tp->hw_stats;
7877
7878 if (!netif_carrier_ok(tp->dev))
7879 return;
7880
7881 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
7882 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
7883 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
7884 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
7885 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
7886 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
7887 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
7888 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
7889 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
7890 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
7891 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
7892 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
7893 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
7894
7895 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
7896 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
7897 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
7898 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
7899 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
7900 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
7901 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
7902 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
7903 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
7904 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
7905 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
7906 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
7907 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
7908 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07007909
7910 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
7911 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
7912 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913}
7914
7915static void tg3_timer(unsigned long __opaque)
7916{
7917 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007918
Michael Chanf475f162006-03-27 23:20:14 -08007919 if (tp->irq_sync)
7920 goto restart_timer;
7921
David S. Millerf47c11e2005-06-24 20:18:35 -07007922 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923
David S. Millerfac9b832005-05-18 22:46:34 -07007924 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
7925 /* All of this garbage is because when using non-tagged
7926 * IRQ status the mailbox/status_block protocol the chip
7927 * uses with the cpu is race prone.
7928 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007929 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07007930 tw32(GRC_LOCAL_CTRL,
7931 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
7932 } else {
7933 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00007934 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07007935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936
David S. Millerfac9b832005-05-18 22:46:34 -07007937 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
7938 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07007939 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07007940 schedule_work(&tp->reset_task);
7941 return;
7942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007943 }
7944
Linus Torvalds1da177e2005-04-16 15:20:36 -07007945 /* This part only runs once per second. */
7946 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07007947 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7948 tg3_periodic_fetch_stats(tp);
7949
Linus Torvalds1da177e2005-04-16 15:20:36 -07007950 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
7951 u32 mac_stat;
7952 int phy_event;
7953
7954 mac_stat = tr32(MAC_STATUS);
7955
7956 phy_event = 0;
7957 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) {
7958 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
7959 phy_event = 1;
7960 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
7961 phy_event = 1;
7962
7963 if (phy_event)
7964 tg3_setup_phy(tp, 0);
7965 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
7966 u32 mac_stat = tr32(MAC_STATUS);
7967 int need_setup = 0;
7968
7969 if (netif_carrier_ok(tp->dev) &&
7970 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
7971 need_setup = 1;
7972 }
7973 if (! netif_carrier_ok(tp->dev) &&
7974 (mac_stat & (MAC_STATUS_PCS_SYNCED |
7975 MAC_STATUS_SIGNAL_DET))) {
7976 need_setup = 1;
7977 }
7978 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07007979 if (!tp->serdes_counter) {
7980 tw32_f(MAC_MODE,
7981 (tp->mac_mode &
7982 ~MAC_MODE_PORT_MODE_MASK));
7983 udelay(40);
7984 tw32_f(MAC_MODE, tp->mac_mode);
7985 udelay(40);
7986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007987 tg3_setup_phy(tp, 0);
7988 }
Michael Chan747e8f82005-07-25 12:33:22 -07007989 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
7990 tg3_serdes_parallel_detect(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007991
7992 tp->timer_counter = tp->timer_multiplier;
7993 }
7994
Michael Chan130b8e42006-09-27 16:00:40 -07007995 /* Heartbeat is only sent once every 2 seconds.
7996 *
7997 * The heartbeat is to tell the ASF firmware that the host
7998 * driver is still alive. In the event that the OS crashes,
7999 * ASF needs to reset the hardware to free up the FIFO space
8000 * that may be filled with rx packets destined for the host.
8001 * If the FIFO is full, ASF will no longer function properly.
8002 *
8003 * Unintended resets have been reported on real time kernels
8004 * where the timer doesn't run on time. Netpoll will also have
8005 * same problem.
8006 *
8007 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8008 * to check the ring condition when the heartbeat is expiring
8009 * before doing the reset. This will prevent most unintended
8010 * resets.
8011 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008012 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008013 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8014 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008015 tg3_wait_for_event_ack(tp);
8016
Michael Chanbbadf502006-04-06 21:46:34 -07008017 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008018 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008019 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Michael Chan28fbef72005-10-26 15:48:35 -07008020 /* 5 seconds timeout */
Michael Chanbbadf502006-04-06 21:46:34 -07008021 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, 5);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008022
8023 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008024 }
8025 tp->asf_counter = tp->asf_multiplier;
8026 }
8027
David S. Millerf47c11e2005-06-24 20:18:35 -07008028 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029
Michael Chanf475f162006-03-27 23:20:14 -08008030restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008031 tp->timer.expires = jiffies + tp->timer_offset;
8032 add_timer(&tp->timer);
8033}
8034
Matt Carlson4f125f42009-09-01 12:55:02 +00008035static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008036{
David Howells7d12e782006-10-05 14:55:46 +01008037 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008038 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008039 char *name;
8040 struct tg3_napi *tnapi = &tp->napi[irq_num];
8041
8042 if (tp->irq_cnt == 1)
8043 name = tp->dev->name;
8044 else {
8045 name = &tnapi->irq_lbl[0];
8046 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8047 name[IFNAMSIZ-1] = 0;
8048 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008049
Matt Carlson679563f2009-09-01 12:55:46 +00008050 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008051 fn = tg3_msi;
8052 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8053 fn = tg3_msi_1shot;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008054 flags = IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008055 } else {
8056 fn = tg3_interrupt;
8057 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8058 fn = tg3_interrupt_tagged;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008059 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008060 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008061
8062 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008063}
8064
Michael Chan79381092005-04-21 17:13:59 -07008065static int tg3_test_interrupt(struct tg3 *tp)
8066{
Matt Carlson09943a12009-08-28 14:01:57 +00008067 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008068 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008069 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008070 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008071
Michael Chand4bc3922005-05-29 14:59:20 -07008072 if (!netif_running(dev))
8073 return -ENODEV;
8074
Michael Chan79381092005-04-21 17:13:59 -07008075 tg3_disable_ints(tp);
8076
Matt Carlson4f125f42009-09-01 12:55:02 +00008077 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008078
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008079 /*
8080 * Turn off MSI one shot mode. Otherwise this test has no
8081 * observable way to know whether the interrupt was delivered.
8082 */
8083 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
8084 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8085 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8086 tw32(MSGINT_MODE, val);
8087 }
8088
Matt Carlson4f125f42009-09-01 12:55:02 +00008089 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008090 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008091 if (err)
8092 return err;
8093
Matt Carlson898a56f2009-08-28 14:02:40 +00008094 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008095 tg3_enable_ints(tp);
8096
8097 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008098 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07008099
8100 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07008101 u32 int_mbox, misc_host_ctrl;
8102
Matt Carlson898a56f2009-08-28 14:02:40 +00008103 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07008104 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8105
8106 if ((int_mbox != 0) ||
8107 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8108 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07008109 break;
Michael Chanb16250e2006-09-27 16:10:14 -07008110 }
8111
Michael Chan79381092005-04-21 17:13:59 -07008112 msleep(10);
8113 }
8114
8115 tg3_disable_ints(tp);
8116
Matt Carlson4f125f42009-09-01 12:55:02 +00008117 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008118
Matt Carlson4f125f42009-09-01 12:55:02 +00008119 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008120
8121 if (err)
8122 return err;
8123
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008124 if (intr_ok) {
8125 /* Reenable MSI one shot mode. */
8126 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
8127 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8128 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8129 tw32(MSGINT_MODE, val);
8130 }
Michael Chan79381092005-04-21 17:13:59 -07008131 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008132 }
Michael Chan79381092005-04-21 17:13:59 -07008133
8134 return -EIO;
8135}
8136
8137/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8138 * successfully restored
8139 */
8140static int tg3_test_msi(struct tg3 *tp)
8141{
Michael Chan79381092005-04-21 17:13:59 -07008142 int err;
8143 u16 pci_cmd;
8144
8145 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8146 return 0;
8147
8148 /* Turn off SERR reporting in case MSI terminates with Master
8149 * Abort.
8150 */
8151 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8152 pci_write_config_word(tp->pdev, PCI_COMMAND,
8153 pci_cmd & ~PCI_COMMAND_SERR);
8154
8155 err = tg3_test_interrupt(tp);
8156
8157 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8158
8159 if (!err)
8160 return 0;
8161
8162 /* other failures */
8163 if (err != -EIO)
8164 return err;
8165
8166 /* MSI test failed, go back to INTx mode */
8167 printk(KERN_WARNING PFX "%s: No interrupt was generated using MSI, "
8168 "switching to INTx mode. Please report this failure to "
8169 "the PCI maintainer and include system chipset information.\n",
8170 tp->dev->name);
8171
Matt Carlson4f125f42009-09-01 12:55:02 +00008172 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00008173
Michael Chan79381092005-04-21 17:13:59 -07008174 pci_disable_msi(tp->pdev);
8175
8176 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
8177
Matt Carlson4f125f42009-09-01 12:55:02 +00008178 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008179 if (err)
8180 return err;
8181
8182 /* Need to reset the chip because the MSI cycle may have terminated
8183 * with Master Abort.
8184 */
David S. Millerf47c11e2005-06-24 20:18:35 -07008185 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008186
Michael Chan944d9802005-05-29 14:57:48 -07008187 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008188 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008189
David S. Millerf47c11e2005-06-24 20:18:35 -07008190 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008191
8192 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00008193 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07008194
8195 return err;
8196}
8197
Matt Carlson9e9fd122009-01-19 16:57:45 -08008198static int tg3_request_firmware(struct tg3 *tp)
8199{
8200 const __be32 *fw_data;
8201
8202 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
8203 printk(KERN_ERR "%s: Failed to load firmware \"%s\"\n",
8204 tp->dev->name, tp->fw_needed);
8205 return -ENOENT;
8206 }
8207
8208 fw_data = (void *)tp->fw->data;
8209
8210 /* Firmware blob starts with version numbers, followed by
8211 * start address and _full_ length including BSS sections
8212 * (which must be longer than the actual data, of course
8213 */
8214
8215 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
8216 if (tp->fw_len < (tp->fw->size - 12)) {
8217 printk(KERN_ERR "%s: bogus length %d in \"%s\"\n",
8218 tp->dev->name, tp->fw_len, tp->fw_needed);
8219 release_firmware(tp->fw);
8220 tp->fw = NULL;
8221 return -EINVAL;
8222 }
8223
8224 /* We no longer need firmware; we have it. */
8225 tp->fw_needed = NULL;
8226 return 0;
8227}
8228
Matt Carlson679563f2009-09-01 12:55:46 +00008229static bool tg3_enable_msix(struct tg3 *tp)
8230{
8231 int i, rc, cpus = num_online_cpus();
8232 struct msix_entry msix_ent[tp->irq_max];
8233
8234 if (cpus == 1)
8235 /* Just fallback to the simpler MSI mode. */
8236 return false;
8237
8238 /*
8239 * We want as many rx rings enabled as there are cpus.
8240 * The first MSIX vector only deals with link interrupts, etc,
8241 * so we add one to the number of vectors we are requesting.
8242 */
8243 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
8244
8245 for (i = 0; i < tp->irq_max; i++) {
8246 msix_ent[i].entry = i;
8247 msix_ent[i].vector = 0;
8248 }
8249
8250 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
8251 if (rc != 0) {
8252 if (rc < TG3_RSS_MIN_NUM_MSIX_VECS)
8253 return false;
8254 if (pci_enable_msix(tp->pdev, msix_ent, rc))
8255 return false;
8256 printk(KERN_NOTICE
8257 "%s: Requested %d MSI-X vectors, received %d\n",
8258 tp->dev->name, tp->irq_cnt, rc);
8259 tp->irq_cnt = rc;
8260 }
8261
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008262 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
8263
Matt Carlson679563f2009-09-01 12:55:46 +00008264 for (i = 0; i < tp->irq_max; i++)
8265 tp->napi[i].irq_vec = msix_ent[i].vector;
8266
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008267 tp->dev->real_num_tx_queues = tp->irq_cnt - 1;
8268
Matt Carlson679563f2009-09-01 12:55:46 +00008269 return true;
8270}
8271
Matt Carlson07b01732009-08-28 14:01:15 +00008272static void tg3_ints_init(struct tg3 *tp)
8273{
Matt Carlson679563f2009-09-01 12:55:46 +00008274 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
8275 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00008276 /* All MSI supporting chips should support tagged
8277 * status. Assert that this is the case.
8278 */
Matt Carlson679563f2009-09-01 12:55:46 +00008279 printk(KERN_WARNING PFX "%s: MSI without TAGGED? "
8280 "Not using MSI.\n", tp->dev->name);
8281 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00008282 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008283
Matt Carlson679563f2009-09-01 12:55:46 +00008284 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
8285 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
8286 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
8287 pci_enable_msi(tp->pdev) == 0)
8288 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
8289
8290 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
8291 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008292 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8293 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00008294 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
8295 }
8296defcfg:
8297 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
8298 tp->irq_cnt = 1;
8299 tp->napi[0].irq_vec = tp->pdev->irq;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008300 tp->dev->real_num_tx_queues = 1;
Matt Carlson679563f2009-09-01 12:55:46 +00008301 }
Matt Carlson07b01732009-08-28 14:01:15 +00008302}
8303
8304static void tg3_ints_fini(struct tg3 *tp)
8305{
Matt Carlson679563f2009-09-01 12:55:46 +00008306 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8307 pci_disable_msix(tp->pdev);
8308 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
8309 pci_disable_msi(tp->pdev);
8310 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008311 tp->tg3_flags3 &= ~TG3_FLG3_ENABLE_RSS;
Matt Carlson07b01732009-08-28 14:01:15 +00008312}
8313
Linus Torvalds1da177e2005-04-16 15:20:36 -07008314static int tg3_open(struct net_device *dev)
8315{
8316 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00008317 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008318
Matt Carlson9e9fd122009-01-19 16:57:45 -08008319 if (tp->fw_needed) {
8320 err = tg3_request_firmware(tp);
8321 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8322 if (err)
8323 return err;
8324 } else if (err) {
8325 printk(KERN_WARNING "%s: TSO capability disabled.\n",
8326 tp->dev->name);
8327 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
8328 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
8329 printk(KERN_NOTICE "%s: TSO capability restored.\n",
8330 tp->dev->name);
8331 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
8332 }
8333 }
8334
Michael Chanc49a1562006-12-17 17:07:29 -08008335 netif_carrier_off(tp->dev);
8336
Michael Chanbc1c7562006-03-20 17:48:03 -08008337 err = tg3_set_power_state(tp, PCI_D0);
Matt Carlson2f751b62008-08-04 23:17:34 -07008338 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08008339 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07008340
8341 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08008342
Linus Torvalds1da177e2005-04-16 15:20:36 -07008343 tg3_disable_ints(tp);
8344 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
8345
David S. Millerf47c11e2005-06-24 20:18:35 -07008346 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008347
Matt Carlson679563f2009-09-01 12:55:46 +00008348 /*
8349 * Setup interrupts first so we know how
8350 * many NAPI resources to allocate
8351 */
8352 tg3_ints_init(tp);
8353
Linus Torvalds1da177e2005-04-16 15:20:36 -07008354 /* The placement of this call is tied
8355 * to the setup and use of Host TX descriptors.
8356 */
8357 err = tg3_alloc_consistent(tp);
8358 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008359 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008360
Matt Carlsonfed97812009-09-01 13:10:19 +00008361 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07008362
Matt Carlson4f125f42009-09-01 12:55:02 +00008363 for (i = 0; i < tp->irq_cnt; i++) {
8364 struct tg3_napi *tnapi = &tp->napi[i];
8365 err = tg3_request_irq(tp, i);
8366 if (err) {
8367 for (i--; i >= 0; i--)
8368 free_irq(tnapi->irq_vec, tnapi);
8369 break;
8370 }
8371 }
Matt Carlson07b01732009-08-28 14:01:15 +00008372
8373 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008374 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00008375
David S. Millerf47c11e2005-06-24 20:18:35 -07008376 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008377
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008378 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008379 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07008380 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381 tg3_free_rings(tp);
8382 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07008383 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8384 tp->timer_offset = HZ;
8385 else
8386 tp->timer_offset = HZ / 10;
8387
8388 BUG_ON(tp->timer_offset > HZ);
8389 tp->timer_counter = tp->timer_multiplier =
8390 (HZ / tp->timer_offset);
8391 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07008392 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008393
8394 init_timer(&tp->timer);
8395 tp->timer.expires = jiffies + tp->timer_offset;
8396 tp->timer.data = (unsigned long) tp;
8397 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008398 }
8399
David S. Millerf47c11e2005-06-24 20:18:35 -07008400 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008401
Matt Carlson07b01732009-08-28 14:01:15 +00008402 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008403 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008404
Michael Chan79381092005-04-21 17:13:59 -07008405 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
8406 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07008407
Michael Chan79381092005-04-21 17:13:59 -07008408 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07008409 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07008410 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07008411 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07008412 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008413
Matt Carlson679563f2009-09-01 12:55:46 +00008414 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07008415 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008416
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008417 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
8418 (tp->tg3_flags2 & TG3_FLG2_USING_MSI) &&
8419 (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)) {
8420 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008421
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008422 tw32(PCIE_TRANSACTION_CFG,
8423 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008424 }
Michael Chan79381092005-04-21 17:13:59 -07008425 }
8426
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008427 tg3_phy_start(tp);
8428
David S. Millerf47c11e2005-06-24 20:18:35 -07008429 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008430
Michael Chan79381092005-04-21 17:13:59 -07008431 add_timer(&tp->timer);
8432 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008433 tg3_enable_ints(tp);
8434
David S. Millerf47c11e2005-06-24 20:18:35 -07008435 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008436
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008437 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008438
8439 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00008440
Matt Carlson679563f2009-09-01 12:55:46 +00008441err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00008442 for (i = tp->irq_cnt - 1; i >= 0; i--) {
8443 struct tg3_napi *tnapi = &tp->napi[i];
8444 free_irq(tnapi->irq_vec, tnapi);
8445 }
Matt Carlson07b01732009-08-28 14:01:15 +00008446
Matt Carlson679563f2009-09-01 12:55:46 +00008447err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00008448 tg3_napi_disable(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008449 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00008450
8451err_out1:
8452 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008453 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008454}
8455
8456#if 0
8457/*static*/ void tg3_dump_state(struct tg3 *tp)
8458{
8459 u32 val32, val32_2, val32_3, val32_4, val32_5;
8460 u16 val16;
8461 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008462 struct tg3_hw_status *sblk = tp->napi[0]->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463
8464 pci_read_config_word(tp->pdev, PCI_STATUS, &val16);
8465 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE, &val32);
8466 printk("DEBUG: PCI status [%04x] TG3PCI state[%08x]\n",
8467 val16, val32);
8468
8469 /* MAC block */
8470 printk("DEBUG: MAC_MODE[%08x] MAC_STATUS[%08x]\n",
8471 tr32(MAC_MODE), tr32(MAC_STATUS));
8472 printk(" MAC_EVENT[%08x] MAC_LED_CTRL[%08x]\n",
8473 tr32(MAC_EVENT), tr32(MAC_LED_CTRL));
8474 printk("DEBUG: MAC_TX_MODE[%08x] MAC_TX_STATUS[%08x]\n",
8475 tr32(MAC_TX_MODE), tr32(MAC_TX_STATUS));
8476 printk(" MAC_RX_MODE[%08x] MAC_RX_STATUS[%08x]\n",
8477 tr32(MAC_RX_MODE), tr32(MAC_RX_STATUS));
8478
8479 /* Send data initiator control block */
8480 printk("DEBUG: SNDDATAI_MODE[%08x] SNDDATAI_STATUS[%08x]\n",
8481 tr32(SNDDATAI_MODE), tr32(SNDDATAI_STATUS));
8482 printk(" SNDDATAI_STATSCTRL[%08x]\n",
8483 tr32(SNDDATAI_STATSCTRL));
8484
8485 /* Send data completion control block */
8486 printk("DEBUG: SNDDATAC_MODE[%08x]\n", tr32(SNDDATAC_MODE));
8487
8488 /* Send BD ring selector block */
8489 printk("DEBUG: SNDBDS_MODE[%08x] SNDBDS_STATUS[%08x]\n",
8490 tr32(SNDBDS_MODE), tr32(SNDBDS_STATUS));
8491
8492 /* Send BD initiator control block */
8493 printk("DEBUG: SNDBDI_MODE[%08x] SNDBDI_STATUS[%08x]\n",
8494 tr32(SNDBDI_MODE), tr32(SNDBDI_STATUS));
8495
8496 /* Send BD completion control block */
8497 printk("DEBUG: SNDBDC_MODE[%08x]\n", tr32(SNDBDC_MODE));
8498
8499 /* Receive list placement control block */
8500 printk("DEBUG: RCVLPC_MODE[%08x] RCVLPC_STATUS[%08x]\n",
8501 tr32(RCVLPC_MODE), tr32(RCVLPC_STATUS));
8502 printk(" RCVLPC_STATSCTRL[%08x]\n",
8503 tr32(RCVLPC_STATSCTRL));
8504
8505 /* Receive data and receive BD initiator control block */
8506 printk("DEBUG: RCVDBDI_MODE[%08x] RCVDBDI_STATUS[%08x]\n",
8507 tr32(RCVDBDI_MODE), tr32(RCVDBDI_STATUS));
8508
8509 /* Receive data completion control block */
8510 printk("DEBUG: RCVDCC_MODE[%08x]\n",
8511 tr32(RCVDCC_MODE));
8512
8513 /* Receive BD initiator control block */
8514 printk("DEBUG: RCVBDI_MODE[%08x] RCVBDI_STATUS[%08x]\n",
8515 tr32(RCVBDI_MODE), tr32(RCVBDI_STATUS));
8516
8517 /* Receive BD completion control block */
8518 printk("DEBUG: RCVCC_MODE[%08x] RCVCC_STATUS[%08x]\n",
8519 tr32(RCVCC_MODE), tr32(RCVCC_STATUS));
8520
8521 /* Receive list selector control block */
8522 printk("DEBUG: RCVLSC_MODE[%08x] RCVLSC_STATUS[%08x]\n",
8523 tr32(RCVLSC_MODE), tr32(RCVLSC_STATUS));
8524
8525 /* Mbuf cluster free block */
8526 printk("DEBUG: MBFREE_MODE[%08x] MBFREE_STATUS[%08x]\n",
8527 tr32(MBFREE_MODE), tr32(MBFREE_STATUS));
8528
8529 /* Host coalescing control block */
8530 printk("DEBUG: HOSTCC_MODE[%08x] HOSTCC_STATUS[%08x]\n",
8531 tr32(HOSTCC_MODE), tr32(HOSTCC_STATUS));
8532 printk("DEBUG: HOSTCC_STATS_BLK_HOST_ADDR[%08x%08x]\n",
8533 tr32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH),
8534 tr32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW));
8535 printk("DEBUG: HOSTCC_STATUS_BLK_HOST_ADDR[%08x%08x]\n",
8536 tr32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH),
8537 tr32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW));
8538 printk("DEBUG: HOSTCC_STATS_BLK_NIC_ADDR[%08x]\n",
8539 tr32(HOSTCC_STATS_BLK_NIC_ADDR));
8540 printk("DEBUG: HOSTCC_STATUS_BLK_NIC_ADDR[%08x]\n",
8541 tr32(HOSTCC_STATUS_BLK_NIC_ADDR));
8542
8543 /* Memory arbiter control block */
8544 printk("DEBUG: MEMARB_MODE[%08x] MEMARB_STATUS[%08x]\n",
8545 tr32(MEMARB_MODE), tr32(MEMARB_STATUS));
8546
8547 /* Buffer manager control block */
8548 printk("DEBUG: BUFMGR_MODE[%08x] BUFMGR_STATUS[%08x]\n",
8549 tr32(BUFMGR_MODE), tr32(BUFMGR_STATUS));
8550 printk("DEBUG: BUFMGR_MB_POOL_ADDR[%08x] BUFMGR_MB_POOL_SIZE[%08x]\n",
8551 tr32(BUFMGR_MB_POOL_ADDR), tr32(BUFMGR_MB_POOL_SIZE));
8552 printk("DEBUG: BUFMGR_DMA_DESC_POOL_ADDR[%08x] "
8553 "BUFMGR_DMA_DESC_POOL_SIZE[%08x]\n",
8554 tr32(BUFMGR_DMA_DESC_POOL_ADDR),
8555 tr32(BUFMGR_DMA_DESC_POOL_SIZE));
8556
8557 /* Read DMA control block */
8558 printk("DEBUG: RDMAC_MODE[%08x] RDMAC_STATUS[%08x]\n",
8559 tr32(RDMAC_MODE), tr32(RDMAC_STATUS));
8560
8561 /* Write DMA control block */
8562 printk("DEBUG: WDMAC_MODE[%08x] WDMAC_STATUS[%08x]\n",
8563 tr32(WDMAC_MODE), tr32(WDMAC_STATUS));
8564
8565 /* DMA completion block */
8566 printk("DEBUG: DMAC_MODE[%08x]\n",
8567 tr32(DMAC_MODE));
8568
8569 /* GRC block */
8570 printk("DEBUG: GRC_MODE[%08x] GRC_MISC_CFG[%08x]\n",
8571 tr32(GRC_MODE), tr32(GRC_MISC_CFG));
8572 printk("DEBUG: GRC_LOCAL_CTRL[%08x]\n",
8573 tr32(GRC_LOCAL_CTRL));
8574
8575 /* TG3_BDINFOs */
8576 printk("DEBUG: RCVDBDI_JUMBO_BD[%08x%08x:%08x:%08x]\n",
8577 tr32(RCVDBDI_JUMBO_BD + 0x0),
8578 tr32(RCVDBDI_JUMBO_BD + 0x4),
8579 tr32(RCVDBDI_JUMBO_BD + 0x8),
8580 tr32(RCVDBDI_JUMBO_BD + 0xc));
8581 printk("DEBUG: RCVDBDI_STD_BD[%08x%08x:%08x:%08x]\n",
8582 tr32(RCVDBDI_STD_BD + 0x0),
8583 tr32(RCVDBDI_STD_BD + 0x4),
8584 tr32(RCVDBDI_STD_BD + 0x8),
8585 tr32(RCVDBDI_STD_BD + 0xc));
8586 printk("DEBUG: RCVDBDI_MINI_BD[%08x%08x:%08x:%08x]\n",
8587 tr32(RCVDBDI_MINI_BD + 0x0),
8588 tr32(RCVDBDI_MINI_BD + 0x4),
8589 tr32(RCVDBDI_MINI_BD + 0x8),
8590 tr32(RCVDBDI_MINI_BD + 0xc));
8591
8592 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x0, &val32);
8593 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x4, &val32_2);
8594 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x8, &val32_3);
8595 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0xc, &val32_4);
8596 printk("DEBUG: SRAM_SEND_RCB_0[%08x%08x:%08x:%08x]\n",
8597 val32, val32_2, val32_3, val32_4);
8598
8599 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x0, &val32);
8600 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x4, &val32_2);
8601 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x8, &val32_3);
8602 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0xc, &val32_4);
8603 printk("DEBUG: SRAM_RCV_RET_RCB_0[%08x%08x:%08x:%08x]\n",
8604 val32, val32_2, val32_3, val32_4);
8605
8606 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x0, &val32);
8607 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x4, &val32_2);
8608 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x8, &val32_3);
8609 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0xc, &val32_4);
8610 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x10, &val32_5);
8611 printk("DEBUG: SRAM_STATUS_BLK[%08x:%08x:%08x:%08x:%08x]\n",
8612 val32, val32_2, val32_3, val32_4, val32_5);
8613
8614 /* SW status block */
Matt Carlson898a56f2009-08-28 14:02:40 +00008615 printk(KERN_DEBUG
8616 "Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
8617 sblk->status,
8618 sblk->status_tag,
8619 sblk->rx_jumbo_consumer,
8620 sblk->rx_consumer,
8621 sblk->rx_mini_consumer,
8622 sblk->idx[0].rx_producer,
8623 sblk->idx[0].tx_consumer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008624
8625 /* SW statistics block */
8626 printk("DEBUG: Host statistics block [%08x:%08x:%08x:%08x]\n",
8627 ((u32 *)tp->hw_stats)[0],
8628 ((u32 *)tp->hw_stats)[1],
8629 ((u32 *)tp->hw_stats)[2],
8630 ((u32 *)tp->hw_stats)[3]);
8631
8632 /* Mailboxes */
8633 printk("DEBUG: SNDHOST_PROD[%08x%08x] SNDNIC_PROD[%08x%08x]\n",
Michael Chan09ee9292005-08-09 20:17:00 -07008634 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + 0x0),
8635 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + 0x4),
8636 tr32_mailbox(MAILBOX_SNDNIC_PROD_IDX_0 + 0x0),
8637 tr32_mailbox(MAILBOX_SNDNIC_PROD_IDX_0 + 0x4));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008638
8639 /* NIC side send descriptors. */
8640 for (i = 0; i < 6; i++) {
8641 unsigned long txd;
8642
8643 txd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_TX_BUFFER_DESC
8644 + (i * sizeof(struct tg3_tx_buffer_desc));
8645 printk("DEBUG: NIC TXD(%d)[%08x:%08x:%08x:%08x]\n",
8646 i,
8647 readl(txd + 0x0), readl(txd + 0x4),
8648 readl(txd + 0x8), readl(txd + 0xc));
8649 }
8650
8651 /* NIC side RX descriptors. */
8652 for (i = 0; i < 6; i++) {
8653 unsigned long rxd;
8654
8655 rxd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_RX_BUFFER_DESC
8656 + (i * sizeof(struct tg3_rx_buffer_desc));
8657 printk("DEBUG: NIC RXD_STD(%d)[0][%08x:%08x:%08x:%08x]\n",
8658 i,
8659 readl(rxd + 0x0), readl(rxd + 0x4),
8660 readl(rxd + 0x8), readl(rxd + 0xc));
8661 rxd += (4 * sizeof(u32));
8662 printk("DEBUG: NIC RXD_STD(%d)[1][%08x:%08x:%08x:%08x]\n",
8663 i,
8664 readl(rxd + 0x0), readl(rxd + 0x4),
8665 readl(rxd + 0x8), readl(rxd + 0xc));
8666 }
8667
8668 for (i = 0; i < 6; i++) {
8669 unsigned long rxd;
8670
8671 rxd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_RX_JUMBO_BUFFER_DESC
8672 + (i * sizeof(struct tg3_rx_buffer_desc));
8673 printk("DEBUG: NIC RXD_JUMBO(%d)[0][%08x:%08x:%08x:%08x]\n",
8674 i,
8675 readl(rxd + 0x0), readl(rxd + 0x4),
8676 readl(rxd + 0x8), readl(rxd + 0xc));
8677 rxd += (4 * sizeof(u32));
8678 printk("DEBUG: NIC RXD_JUMBO(%d)[1][%08x:%08x:%08x:%08x]\n",
8679 i,
8680 readl(rxd + 0x0), readl(rxd + 0x4),
8681 readl(rxd + 0x8), readl(rxd + 0xc));
8682 }
8683}
8684#endif
8685
8686static struct net_device_stats *tg3_get_stats(struct net_device *);
8687static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
8688
8689static int tg3_close(struct net_device *dev)
8690{
Matt Carlson4f125f42009-09-01 12:55:02 +00008691 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008692 struct tg3 *tp = netdev_priv(dev);
8693
Matt Carlsonfed97812009-09-01 13:10:19 +00008694 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07008695 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08008696
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008697 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008698
8699 del_timer_sync(&tp->timer);
8700
David S. Millerf47c11e2005-06-24 20:18:35 -07008701 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008702#if 0
8703 tg3_dump_state(tp);
8704#endif
8705
8706 tg3_disable_ints(tp);
8707
Michael Chan944d9802005-05-29 14:57:48 -07008708 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008709 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07008710 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008711
David S. Millerf47c11e2005-06-24 20:18:35 -07008712 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008713
Matt Carlson4f125f42009-09-01 12:55:02 +00008714 for (i = tp->irq_cnt - 1; i >= 0; i--) {
8715 struct tg3_napi *tnapi = &tp->napi[i];
8716 free_irq(tnapi->irq_vec, tnapi);
8717 }
Matt Carlson07b01732009-08-28 14:01:15 +00008718
8719 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008720
8721 memcpy(&tp->net_stats_prev, tg3_get_stats(tp->dev),
8722 sizeof(tp->net_stats_prev));
8723 memcpy(&tp->estats_prev, tg3_get_estats(tp),
8724 sizeof(tp->estats_prev));
8725
8726 tg3_free_consistent(tp);
8727
Michael Chanbc1c7562006-03-20 17:48:03 -08008728 tg3_set_power_state(tp, PCI_D3hot);
8729
8730 netif_carrier_off(tp->dev);
8731
Linus Torvalds1da177e2005-04-16 15:20:36 -07008732 return 0;
8733}
8734
8735static inline unsigned long get_stat64(tg3_stat64_t *val)
8736{
8737 unsigned long ret;
8738
8739#if (BITS_PER_LONG == 32)
8740 ret = val->low;
8741#else
8742 ret = ((u64)val->high << 32) | ((u64)val->low);
8743#endif
8744 return ret;
8745}
8746
Stefan Buehler816f8b82008-08-15 14:10:54 -07008747static inline u64 get_estat64(tg3_stat64_t *val)
8748{
8749 return ((u64)val->high << 32) | ((u64)val->low);
8750}
8751
Linus Torvalds1da177e2005-04-16 15:20:36 -07008752static unsigned long calc_crc_errors(struct tg3 *tp)
8753{
8754 struct tg3_hw_stats *hw_stats = tp->hw_stats;
8755
8756 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
8757 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
8758 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008759 u32 val;
8760
David S. Millerf47c11e2005-06-24 20:18:35 -07008761 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08008762 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
8763 tg3_writephy(tp, MII_TG3_TEST1,
8764 val | MII_TG3_TEST1_CRC_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008765 tg3_readphy(tp, 0x14, &val);
8766 } else
8767 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07008768 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008769
8770 tp->phy_crc_errors += val;
8771
8772 return tp->phy_crc_errors;
8773 }
8774
8775 return get_stat64(&hw_stats->rx_fcs_errors);
8776}
8777
8778#define ESTAT_ADD(member) \
8779 estats->member = old_estats->member + \
Stefan Buehler816f8b82008-08-15 14:10:54 -07008780 get_estat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008781
8782static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
8783{
8784 struct tg3_ethtool_stats *estats = &tp->estats;
8785 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
8786 struct tg3_hw_stats *hw_stats = tp->hw_stats;
8787
8788 if (!hw_stats)
8789 return old_estats;
8790
8791 ESTAT_ADD(rx_octets);
8792 ESTAT_ADD(rx_fragments);
8793 ESTAT_ADD(rx_ucast_packets);
8794 ESTAT_ADD(rx_mcast_packets);
8795 ESTAT_ADD(rx_bcast_packets);
8796 ESTAT_ADD(rx_fcs_errors);
8797 ESTAT_ADD(rx_align_errors);
8798 ESTAT_ADD(rx_xon_pause_rcvd);
8799 ESTAT_ADD(rx_xoff_pause_rcvd);
8800 ESTAT_ADD(rx_mac_ctrl_rcvd);
8801 ESTAT_ADD(rx_xoff_entered);
8802 ESTAT_ADD(rx_frame_too_long_errors);
8803 ESTAT_ADD(rx_jabbers);
8804 ESTAT_ADD(rx_undersize_packets);
8805 ESTAT_ADD(rx_in_length_errors);
8806 ESTAT_ADD(rx_out_length_errors);
8807 ESTAT_ADD(rx_64_or_less_octet_packets);
8808 ESTAT_ADD(rx_65_to_127_octet_packets);
8809 ESTAT_ADD(rx_128_to_255_octet_packets);
8810 ESTAT_ADD(rx_256_to_511_octet_packets);
8811 ESTAT_ADD(rx_512_to_1023_octet_packets);
8812 ESTAT_ADD(rx_1024_to_1522_octet_packets);
8813 ESTAT_ADD(rx_1523_to_2047_octet_packets);
8814 ESTAT_ADD(rx_2048_to_4095_octet_packets);
8815 ESTAT_ADD(rx_4096_to_8191_octet_packets);
8816 ESTAT_ADD(rx_8192_to_9022_octet_packets);
8817
8818 ESTAT_ADD(tx_octets);
8819 ESTAT_ADD(tx_collisions);
8820 ESTAT_ADD(tx_xon_sent);
8821 ESTAT_ADD(tx_xoff_sent);
8822 ESTAT_ADD(tx_flow_control);
8823 ESTAT_ADD(tx_mac_errors);
8824 ESTAT_ADD(tx_single_collisions);
8825 ESTAT_ADD(tx_mult_collisions);
8826 ESTAT_ADD(tx_deferred);
8827 ESTAT_ADD(tx_excessive_collisions);
8828 ESTAT_ADD(tx_late_collisions);
8829 ESTAT_ADD(tx_collide_2times);
8830 ESTAT_ADD(tx_collide_3times);
8831 ESTAT_ADD(tx_collide_4times);
8832 ESTAT_ADD(tx_collide_5times);
8833 ESTAT_ADD(tx_collide_6times);
8834 ESTAT_ADD(tx_collide_7times);
8835 ESTAT_ADD(tx_collide_8times);
8836 ESTAT_ADD(tx_collide_9times);
8837 ESTAT_ADD(tx_collide_10times);
8838 ESTAT_ADD(tx_collide_11times);
8839 ESTAT_ADD(tx_collide_12times);
8840 ESTAT_ADD(tx_collide_13times);
8841 ESTAT_ADD(tx_collide_14times);
8842 ESTAT_ADD(tx_collide_15times);
8843 ESTAT_ADD(tx_ucast_packets);
8844 ESTAT_ADD(tx_mcast_packets);
8845 ESTAT_ADD(tx_bcast_packets);
8846 ESTAT_ADD(tx_carrier_sense_errors);
8847 ESTAT_ADD(tx_discards);
8848 ESTAT_ADD(tx_errors);
8849
8850 ESTAT_ADD(dma_writeq_full);
8851 ESTAT_ADD(dma_write_prioq_full);
8852 ESTAT_ADD(rxbds_empty);
8853 ESTAT_ADD(rx_discards);
8854 ESTAT_ADD(rx_errors);
8855 ESTAT_ADD(rx_threshold_hit);
8856
8857 ESTAT_ADD(dma_readq_full);
8858 ESTAT_ADD(dma_read_prioq_full);
8859 ESTAT_ADD(tx_comp_queue_full);
8860
8861 ESTAT_ADD(ring_set_send_prod_index);
8862 ESTAT_ADD(ring_status_update);
8863 ESTAT_ADD(nic_irqs);
8864 ESTAT_ADD(nic_avoided_irqs);
8865 ESTAT_ADD(nic_tx_threshold_hit);
8866
8867 return estats;
8868}
8869
8870static struct net_device_stats *tg3_get_stats(struct net_device *dev)
8871{
8872 struct tg3 *tp = netdev_priv(dev);
8873 struct net_device_stats *stats = &tp->net_stats;
8874 struct net_device_stats *old_stats = &tp->net_stats_prev;
8875 struct tg3_hw_stats *hw_stats = tp->hw_stats;
8876
8877 if (!hw_stats)
8878 return old_stats;
8879
8880 stats->rx_packets = old_stats->rx_packets +
8881 get_stat64(&hw_stats->rx_ucast_packets) +
8882 get_stat64(&hw_stats->rx_mcast_packets) +
8883 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008884
Linus Torvalds1da177e2005-04-16 15:20:36 -07008885 stats->tx_packets = old_stats->tx_packets +
8886 get_stat64(&hw_stats->tx_ucast_packets) +
8887 get_stat64(&hw_stats->tx_mcast_packets) +
8888 get_stat64(&hw_stats->tx_bcast_packets);
8889
8890 stats->rx_bytes = old_stats->rx_bytes +
8891 get_stat64(&hw_stats->rx_octets);
8892 stats->tx_bytes = old_stats->tx_bytes +
8893 get_stat64(&hw_stats->tx_octets);
8894
8895 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07008896 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008897 stats->tx_errors = old_stats->tx_errors +
8898 get_stat64(&hw_stats->tx_errors) +
8899 get_stat64(&hw_stats->tx_mac_errors) +
8900 get_stat64(&hw_stats->tx_carrier_sense_errors) +
8901 get_stat64(&hw_stats->tx_discards);
8902
8903 stats->multicast = old_stats->multicast +
8904 get_stat64(&hw_stats->rx_mcast_packets);
8905 stats->collisions = old_stats->collisions +
8906 get_stat64(&hw_stats->tx_collisions);
8907
8908 stats->rx_length_errors = old_stats->rx_length_errors +
8909 get_stat64(&hw_stats->rx_frame_too_long_errors) +
8910 get_stat64(&hw_stats->rx_undersize_packets);
8911
8912 stats->rx_over_errors = old_stats->rx_over_errors +
8913 get_stat64(&hw_stats->rxbds_empty);
8914 stats->rx_frame_errors = old_stats->rx_frame_errors +
8915 get_stat64(&hw_stats->rx_align_errors);
8916 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
8917 get_stat64(&hw_stats->tx_discards);
8918 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
8919 get_stat64(&hw_stats->tx_carrier_sense_errors);
8920
8921 stats->rx_crc_errors = old_stats->rx_crc_errors +
8922 calc_crc_errors(tp);
8923
John W. Linville4f63b872005-09-12 14:43:18 -07008924 stats->rx_missed_errors = old_stats->rx_missed_errors +
8925 get_stat64(&hw_stats->rx_discards);
8926
Linus Torvalds1da177e2005-04-16 15:20:36 -07008927 return stats;
8928}
8929
8930static inline u32 calc_crc(unsigned char *buf, int len)
8931{
8932 u32 reg;
8933 u32 tmp;
8934 int j, k;
8935
8936 reg = 0xffffffff;
8937
8938 for (j = 0; j < len; j++) {
8939 reg ^= buf[j];
8940
8941 for (k = 0; k < 8; k++) {
8942 tmp = reg & 0x01;
8943
8944 reg >>= 1;
8945
8946 if (tmp) {
8947 reg ^= 0xedb88320;
8948 }
8949 }
8950 }
8951
8952 return ~reg;
8953}
8954
8955static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8956{
8957 /* accept or reject all multicast frames */
8958 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8959 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8960 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8961 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8962}
8963
8964static void __tg3_set_rx_mode(struct net_device *dev)
8965{
8966 struct tg3 *tp = netdev_priv(dev);
8967 u32 rx_mode;
8968
8969 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8970 RX_MODE_KEEP_VLAN_TAG);
8971
8972 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8973 * flag clear.
8974 */
8975#if TG3_VLAN_TAG_USED
8976 if (!tp->vlgrp &&
8977 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
8978 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8979#else
8980 /* By definition, VLAN is disabled always in this
8981 * case.
8982 */
8983 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
8984 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8985#endif
8986
8987 if (dev->flags & IFF_PROMISC) {
8988 /* Promiscuous mode. */
8989 rx_mode |= RX_MODE_PROMISC;
8990 } else if (dev->flags & IFF_ALLMULTI) {
8991 /* Accept all multicast. */
8992 tg3_set_multi (tp, 1);
8993 } else if (dev->mc_count < 1) {
8994 /* Reject all multicast. */
8995 tg3_set_multi (tp, 0);
8996 } else {
8997 /* Accept one or more multicast(s). */
8998 struct dev_mc_list *mclist;
8999 unsigned int i;
9000 u32 mc_filter[4] = { 0, };
9001 u32 regidx;
9002 u32 bit;
9003 u32 crc;
9004
9005 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
9006 i++, mclist = mclist->next) {
9007
9008 crc = calc_crc (mclist->dmi_addr, ETH_ALEN);
9009 bit = ~crc & 0x7f;
9010 regidx = (bit & 0x60) >> 5;
9011 bit &= 0x1f;
9012 mc_filter[regidx] |= (1 << bit);
9013 }
9014
9015 tw32(MAC_HASH_REG_0, mc_filter[0]);
9016 tw32(MAC_HASH_REG_1, mc_filter[1]);
9017 tw32(MAC_HASH_REG_2, mc_filter[2]);
9018 tw32(MAC_HASH_REG_3, mc_filter[3]);
9019 }
9020
9021 if (rx_mode != tp->rx_mode) {
9022 tp->rx_mode = rx_mode;
9023 tw32_f(MAC_RX_MODE, rx_mode);
9024 udelay(10);
9025 }
9026}
9027
9028static void tg3_set_rx_mode(struct net_device *dev)
9029{
9030 struct tg3 *tp = netdev_priv(dev);
9031
Michael Chane75f7c92006-03-20 21:33:26 -08009032 if (!netif_running(dev))
9033 return;
9034
David S. Millerf47c11e2005-06-24 20:18:35 -07009035 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009036 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009037 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009038}
9039
9040#define TG3_REGDUMP_LEN (32 * 1024)
9041
9042static int tg3_get_regs_len(struct net_device *dev)
9043{
9044 return TG3_REGDUMP_LEN;
9045}
9046
9047static void tg3_get_regs(struct net_device *dev,
9048 struct ethtool_regs *regs, void *_p)
9049{
9050 u32 *p = _p;
9051 struct tg3 *tp = netdev_priv(dev);
9052 u8 *orig_p = _p;
9053 int i;
9054
9055 regs->version = 0;
9056
9057 memset(p, 0, TG3_REGDUMP_LEN);
9058
Michael Chanbc1c7562006-03-20 17:48:03 -08009059 if (tp->link_config.phy_is_low_power)
9060 return;
9061
David S. Millerf47c11e2005-06-24 20:18:35 -07009062 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009063
9064#define __GET_REG32(reg) (*(p)++ = tr32(reg))
9065#define GET_REG32_LOOP(base,len) \
9066do { p = (u32 *)(orig_p + (base)); \
9067 for (i = 0; i < len; i += 4) \
9068 __GET_REG32((base) + i); \
9069} while (0)
9070#define GET_REG32_1(reg) \
9071do { p = (u32 *)(orig_p + (reg)); \
9072 __GET_REG32((reg)); \
9073} while (0)
9074
9075 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9076 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9077 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9078 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9079 GET_REG32_1(SNDDATAC_MODE);
9080 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9081 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9082 GET_REG32_1(SNDBDC_MODE);
9083 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9084 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9085 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9086 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9087 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9088 GET_REG32_1(RCVDCC_MODE);
9089 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9090 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9091 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9092 GET_REG32_1(MBFREE_MODE);
9093 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9094 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9095 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9096 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9097 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08009098 GET_REG32_1(RX_CPU_MODE);
9099 GET_REG32_1(RX_CPU_STATE);
9100 GET_REG32_1(RX_CPU_PGMCTR);
9101 GET_REG32_1(RX_CPU_HWBKPT);
9102 GET_REG32_1(TX_CPU_MODE);
9103 GET_REG32_1(TX_CPU_STATE);
9104 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009105 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9106 GET_REG32_LOOP(FTQ_RESET, 0x120);
9107 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9108 GET_REG32_1(DMAC_MODE);
9109 GET_REG32_LOOP(GRC_MODE, 0x4c);
9110 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9111 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9112
9113#undef __GET_REG32
9114#undef GET_REG32_LOOP
9115#undef GET_REG32_1
9116
David S. Millerf47c11e2005-06-24 20:18:35 -07009117 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009118}
9119
9120static int tg3_get_eeprom_len(struct net_device *dev)
9121{
9122 struct tg3 *tp = netdev_priv(dev);
9123
9124 return tp->nvram_size;
9125}
9126
Linus Torvalds1da177e2005-04-16 15:20:36 -07009127static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9128{
9129 struct tg3 *tp = netdev_priv(dev);
9130 int ret;
9131 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009132 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009133 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009134
Matt Carlsondf259d82009-04-20 06:57:14 +00009135 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9136 return -EINVAL;
9137
Michael Chanbc1c7562006-03-20 17:48:03 -08009138 if (tp->link_config.phy_is_low_power)
9139 return -EAGAIN;
9140
Linus Torvalds1da177e2005-04-16 15:20:36 -07009141 offset = eeprom->offset;
9142 len = eeprom->len;
9143 eeprom->len = 0;
9144
9145 eeprom->magic = TG3_EEPROM_MAGIC;
9146
9147 if (offset & 3) {
9148 /* adjustments to start on required 4 byte boundary */
9149 b_offset = offset & 3;
9150 b_count = 4 - b_offset;
9151 if (b_count > len) {
9152 /* i.e. offset=1 len=2 */
9153 b_count = len;
9154 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009155 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009156 if (ret)
9157 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009158 memcpy(data, ((char*)&val) + b_offset, b_count);
9159 len -= b_count;
9160 offset += b_count;
9161 eeprom->len += b_count;
9162 }
9163
9164 /* read bytes upto the last 4 byte boundary */
9165 pd = &data[eeprom->len];
9166 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009167 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009168 if (ret) {
9169 eeprom->len += i;
9170 return ret;
9171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009172 memcpy(pd + i, &val, 4);
9173 }
9174 eeprom->len += i;
9175
9176 if (len & 3) {
9177 /* read last bytes not ending on 4 byte boundary */
9178 pd = &data[eeprom->len];
9179 b_count = len & 3;
9180 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009181 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009182 if (ret)
9183 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009184 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009185 eeprom->len += b_count;
9186 }
9187 return 0;
9188}
9189
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009190static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009191
9192static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9193{
9194 struct tg3 *tp = netdev_priv(dev);
9195 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009196 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009197 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009198 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009199
Michael Chanbc1c7562006-03-20 17:48:03 -08009200 if (tp->link_config.phy_is_low_power)
9201 return -EAGAIN;
9202
Matt Carlsondf259d82009-04-20 06:57:14 +00009203 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9204 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009205 return -EINVAL;
9206
9207 offset = eeprom->offset;
9208 len = eeprom->len;
9209
9210 if ((b_offset = (offset & 3))) {
9211 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009212 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009213 if (ret)
9214 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009215 len += b_offset;
9216 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009217 if (len < 4)
9218 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009219 }
9220
9221 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009222 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009223 /* adjustments to end on required 4 byte boundary */
9224 odd_len = 1;
9225 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009226 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009227 if (ret)
9228 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009229 }
9230
9231 buf = data;
9232 if (b_offset || odd_len) {
9233 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009234 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009235 return -ENOMEM;
9236 if (b_offset)
9237 memcpy(buf, &start, 4);
9238 if (odd_len)
9239 memcpy(buf+len-4, &end, 4);
9240 memcpy(buf + b_offset, data, eeprom->len);
9241 }
9242
9243 ret = tg3_nvram_write_block(tp, offset, len, buf);
9244
9245 if (buf != data)
9246 kfree(buf);
9247
9248 return ret;
9249}
9250
9251static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9252{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009253 struct tg3 *tp = netdev_priv(dev);
9254
9255 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9256 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9257 return -EAGAIN;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07009258 return phy_ethtool_gset(tp->mdio_bus->phy_map[PHY_ADDR], cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009259 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009260
Linus Torvalds1da177e2005-04-16 15:20:36 -07009261 cmd->supported = (SUPPORTED_Autoneg);
9262
9263 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
9264 cmd->supported |= (SUPPORTED_1000baseT_Half |
9265 SUPPORTED_1000baseT_Full);
9266
Karsten Keilef348142006-05-12 12:49:08 -07009267 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009268 cmd->supported |= (SUPPORTED_100baseT_Half |
9269 SUPPORTED_100baseT_Full |
9270 SUPPORTED_10baseT_Half |
9271 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009272 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009273 cmd->port = PORT_TP;
9274 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009275 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009276 cmd->port = PORT_FIBRE;
9277 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009278
Linus Torvalds1da177e2005-04-16 15:20:36 -07009279 cmd->advertising = tp->link_config.advertising;
9280 if (netif_running(dev)) {
9281 cmd->speed = tp->link_config.active_speed;
9282 cmd->duplex = tp->link_config.active_duplex;
9283 }
Matt Carlson882e9792009-09-01 13:21:36 +00009284 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009285 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009286 cmd->autoneg = tp->link_config.autoneg;
9287 cmd->maxtxpkt = 0;
9288 cmd->maxrxpkt = 0;
9289 return 0;
9290}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009291
Linus Torvalds1da177e2005-04-16 15:20:36 -07009292static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9293{
9294 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009295
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009296 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9297 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9298 return -EAGAIN;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07009299 return phy_ethtool_sset(tp->mdio_bus->phy_map[PHY_ADDR], cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009300 }
9301
Matt Carlson7e5856b2009-02-25 14:23:01 +00009302 if (cmd->autoneg != AUTONEG_ENABLE &&
9303 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009304 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009305
9306 if (cmd->autoneg == AUTONEG_DISABLE &&
9307 cmd->duplex != DUPLEX_FULL &&
9308 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009309 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009310
Matt Carlson7e5856b2009-02-25 14:23:01 +00009311 if (cmd->autoneg == AUTONEG_ENABLE) {
9312 u32 mask = ADVERTISED_Autoneg |
9313 ADVERTISED_Pause |
9314 ADVERTISED_Asym_Pause;
9315
9316 if (!(tp->tg3_flags2 & TG3_FLAG_10_100_ONLY))
9317 mask |= ADVERTISED_1000baseT_Half |
9318 ADVERTISED_1000baseT_Full;
9319
9320 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
9321 mask |= ADVERTISED_100baseT_Half |
9322 ADVERTISED_100baseT_Full |
9323 ADVERTISED_10baseT_Half |
9324 ADVERTISED_10baseT_Full |
9325 ADVERTISED_TP;
9326 else
9327 mask |= ADVERTISED_FIBRE;
9328
9329 if (cmd->advertising & ~mask)
9330 return -EINVAL;
9331
9332 mask &= (ADVERTISED_1000baseT_Half |
9333 ADVERTISED_1000baseT_Full |
9334 ADVERTISED_100baseT_Half |
9335 ADVERTISED_100baseT_Full |
9336 ADVERTISED_10baseT_Half |
9337 ADVERTISED_10baseT_Full);
9338
9339 cmd->advertising &= mask;
9340 } else {
9341 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) {
9342 if (cmd->speed != SPEED_1000)
9343 return -EINVAL;
9344
9345 if (cmd->duplex != DUPLEX_FULL)
9346 return -EINVAL;
9347 } else {
9348 if (cmd->speed != SPEED_100 &&
9349 cmd->speed != SPEED_10)
9350 return -EINVAL;
9351 }
9352 }
9353
David S. Millerf47c11e2005-06-24 20:18:35 -07009354 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009355
9356 tp->link_config.autoneg = cmd->autoneg;
9357 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009358 tp->link_config.advertising = (cmd->advertising |
9359 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009360 tp->link_config.speed = SPEED_INVALID;
9361 tp->link_config.duplex = DUPLEX_INVALID;
9362 } else {
9363 tp->link_config.advertising = 0;
9364 tp->link_config.speed = cmd->speed;
9365 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009366 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009367
Michael Chan24fcad62006-12-17 17:06:46 -08009368 tp->link_config.orig_speed = tp->link_config.speed;
9369 tp->link_config.orig_duplex = tp->link_config.duplex;
9370 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9371
Linus Torvalds1da177e2005-04-16 15:20:36 -07009372 if (netif_running(dev))
9373 tg3_setup_phy(tp, 1);
9374
David S. Millerf47c11e2005-06-24 20:18:35 -07009375 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009376
Linus Torvalds1da177e2005-04-16 15:20:36 -07009377 return 0;
9378}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009379
Linus Torvalds1da177e2005-04-16 15:20:36 -07009380static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9381{
9382 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009383
Linus Torvalds1da177e2005-04-16 15:20:36 -07009384 strcpy(info->driver, DRV_MODULE_NAME);
9385 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08009386 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387 strcpy(info->bus_info, pci_name(tp->pdev));
9388}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009389
Linus Torvalds1da177e2005-04-16 15:20:36 -07009390static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9391{
9392 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009393
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009394 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9395 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07009396 wol->supported = WAKE_MAGIC;
9397 else
9398 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009399 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08009400 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9401 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009402 wol->wolopts = WAKE_MAGIC;
9403 memset(&wol->sopass, 0, sizeof(wol->sopass));
9404}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009405
Linus Torvalds1da177e2005-04-16 15:20:36 -07009406static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9407{
9408 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009409 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009410
Linus Torvalds1da177e2005-04-16 15:20:36 -07009411 if (wol->wolopts & ~WAKE_MAGIC)
9412 return -EINVAL;
9413 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009414 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009415 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009416
David S. Millerf47c11e2005-06-24 20:18:35 -07009417 spin_lock_bh(&tp->lock);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009418 if (wol->wolopts & WAKE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009419 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009420 device_set_wakeup_enable(dp, true);
9421 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009422 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009423 device_set_wakeup_enable(dp, false);
9424 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009425 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009426
Linus Torvalds1da177e2005-04-16 15:20:36 -07009427 return 0;
9428}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009429
Linus Torvalds1da177e2005-04-16 15:20:36 -07009430static u32 tg3_get_msglevel(struct net_device *dev)
9431{
9432 struct tg3 *tp = netdev_priv(dev);
9433 return tp->msg_enable;
9434}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009435
Linus Torvalds1da177e2005-04-16 15:20:36 -07009436static void tg3_set_msglevel(struct net_device *dev, u32 value)
9437{
9438 struct tg3 *tp = netdev_priv(dev);
9439 tp->msg_enable = value;
9440}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009441
Linus Torvalds1da177e2005-04-16 15:20:36 -07009442static int tg3_set_tso(struct net_device *dev, u32 value)
9443{
9444 struct tg3 *tp = netdev_priv(dev);
9445
9446 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9447 if (value)
9448 return -EINVAL;
9449 return 0;
9450 }
Matt Carlson027455a2008-12-21 20:19:30 -08009451 if ((dev->features & NETIF_F_IPV6_CSUM) &&
9452 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009453 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -07009454 dev->features |= NETIF_F_TSO6;
Matt Carlson57e69832008-05-25 23:48:31 -07009455 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
9456 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
9457 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009458 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009459 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
9460 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009461 dev->features |= NETIF_F_TSO_ECN;
9462 } else
9463 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -07009464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009465 return ethtool_op_set_tso(dev, value);
9466}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009467
Linus Torvalds1da177e2005-04-16 15:20:36 -07009468static int tg3_nway_reset(struct net_device *dev)
9469{
9470 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009471 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009472
Linus Torvalds1da177e2005-04-16 15:20:36 -07009473 if (!netif_running(dev))
9474 return -EAGAIN;
9475
Michael Chanc94e3942005-09-27 12:12:42 -07009476 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
9477 return -EINVAL;
9478
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009479 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9480 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9481 return -EAGAIN;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07009482 r = phy_start_aneg(tp->mdio_bus->phy_map[PHY_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009483 } else {
9484 u32 bmcr;
9485
9486 spin_lock_bh(&tp->lock);
9487 r = -EINVAL;
9488 tg3_readphy(tp, MII_BMCR, &bmcr);
9489 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
9490 ((bmcr & BMCR_ANENABLE) ||
9491 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT))) {
9492 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
9493 BMCR_ANENABLE);
9494 r = 0;
9495 }
9496 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009497 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009498
Linus Torvalds1da177e2005-04-16 15:20:36 -07009499 return r;
9500}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009501
Linus Torvalds1da177e2005-04-16 15:20:36 -07009502static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9503{
9504 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009505
Linus Torvalds1da177e2005-04-16 15:20:36 -07009506 ering->rx_max_pending = TG3_RX_RING_SIZE - 1;
9507 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009508 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9509 ering->rx_jumbo_max_pending = TG3_RX_JUMBO_RING_SIZE - 1;
9510 else
9511 ering->rx_jumbo_max_pending = 0;
9512
9513 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009514
9515 ering->rx_pending = tp->rx_pending;
9516 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009517 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9518 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
9519 else
9520 ering->rx_jumbo_pending = 0;
9521
Matt Carlsonf3f3f272009-08-28 14:03:21 +00009522 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009523}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009524
Linus Torvalds1da177e2005-04-16 15:20:36 -07009525static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9526{
9527 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +00009528 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009529
Linus Torvalds1da177e2005-04-16 15:20:36 -07009530 if ((ering->rx_pending > TG3_RX_RING_SIZE - 1) ||
9531 (ering->rx_jumbo_pending > TG3_RX_JUMBO_RING_SIZE - 1) ||
Michael Chanbc3a9252006-10-18 20:55:18 -07009532 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
9533 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -08009534 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -07009535 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009536 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009537
Michael Chanbbe832c2005-06-24 20:20:04 -07009538 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009539 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009540 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009541 irq_sync = 1;
9542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009543
Michael Chanbbe832c2005-06-24 20:20:04 -07009544 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009545
Linus Torvalds1da177e2005-04-16 15:20:36 -07009546 tp->rx_pending = ering->rx_pending;
9547
9548 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
9549 tp->rx_pending > 63)
9550 tp->rx_pending = 63;
9551 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +00009552
9553 for (i = 0; i < TG3_IRQ_MAX_VECS; i++)
9554 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009555
9556 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -07009557 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -07009558 err = tg3_restart_hw(tp, 1);
9559 if (!err)
9560 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009561 }
9562
David S. Millerf47c11e2005-06-24 20:18:35 -07009563 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009564
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009565 if (irq_sync && !err)
9566 tg3_phy_start(tp);
9567
Michael Chanb9ec6c12006-07-25 16:37:27 -07009568 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009569}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009570
Linus Torvalds1da177e2005-04-16 15:20:36 -07009571static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9572{
9573 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009574
Linus Torvalds1da177e2005-04-16 15:20:36 -07009575 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -08009576
Steve Glendinninge18ce342008-12-16 02:00:00 -08009577 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -08009578 epause->rx_pause = 1;
9579 else
9580 epause->rx_pause = 0;
9581
Steve Glendinninge18ce342008-12-16 02:00:00 -08009582 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -08009583 epause->tx_pause = 1;
9584 else
9585 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009586}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009587
Linus Torvalds1da177e2005-04-16 15:20:36 -07009588static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9589{
9590 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009591 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009592
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009593 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9594 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9595 return -EAGAIN;
9596
9597 if (epause->autoneg) {
9598 u32 newadv;
9599 struct phy_device *phydev;
9600
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07009601 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009602
9603 if (epause->rx_pause) {
9604 if (epause->tx_pause)
9605 newadv = ADVERTISED_Pause;
9606 else
9607 newadv = ADVERTISED_Pause |
9608 ADVERTISED_Asym_Pause;
9609 } else if (epause->tx_pause) {
9610 newadv = ADVERTISED_Asym_Pause;
9611 } else
9612 newadv = 0;
9613
9614 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
9615 u32 oldadv = phydev->advertising &
9616 (ADVERTISED_Pause |
9617 ADVERTISED_Asym_Pause);
9618 if (oldadv != newadv) {
9619 phydev->advertising &=
9620 ~(ADVERTISED_Pause |
9621 ADVERTISED_Asym_Pause);
9622 phydev->advertising |= newadv;
9623 err = phy_start_aneg(phydev);
9624 }
9625 } else {
9626 tp->link_config.advertising &=
9627 ~(ADVERTISED_Pause |
9628 ADVERTISED_Asym_Pause);
9629 tp->link_config.advertising |= newadv;
9630 }
9631 } else {
9632 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009633 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009634 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009635 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009636
9637 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009638 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009639 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009640 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009641
9642 if (netif_running(dev))
9643 tg3_setup_flow_control(tp, 0, 0);
9644 }
9645 } else {
9646 int irq_sync = 0;
9647
9648 if (netif_running(dev)) {
9649 tg3_netif_stop(tp);
9650 irq_sync = 1;
9651 }
9652
9653 tg3_full_lock(tp, irq_sync);
9654
9655 if (epause->autoneg)
9656 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9657 else
9658 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9659 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009660 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009661 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009662 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009663 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009664 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009665 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009666 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009667
9668 if (netif_running(dev)) {
9669 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9670 err = tg3_restart_hw(tp, 1);
9671 if (!err)
9672 tg3_netif_start(tp);
9673 }
9674
9675 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009677
Michael Chanb9ec6c12006-07-25 16:37:27 -07009678 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009679}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009680
Linus Torvalds1da177e2005-04-16 15:20:36 -07009681static u32 tg3_get_rx_csum(struct net_device *dev)
9682{
9683 struct tg3 *tp = netdev_priv(dev);
9684 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
9685}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009686
Linus Torvalds1da177e2005-04-16 15:20:36 -07009687static int tg3_set_rx_csum(struct net_device *dev, u32 data)
9688{
9689 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009690
Linus Torvalds1da177e2005-04-16 15:20:36 -07009691 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
9692 if (data != 0)
9693 return -EINVAL;
9694 return 0;
9695 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009696
David S. Millerf47c11e2005-06-24 20:18:35 -07009697 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009698 if (data)
9699 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
9700 else
9701 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -07009702 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009703
Linus Torvalds1da177e2005-04-16 15:20:36 -07009704 return 0;
9705}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009706
Linus Torvalds1da177e2005-04-16 15:20:36 -07009707static int tg3_set_tx_csum(struct net_device *dev, u32 data)
9708{
9709 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009710
Linus Torvalds1da177e2005-04-16 15:20:36 -07009711 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
9712 if (data != 0)
9713 return -EINVAL;
9714 return 0;
9715 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009716
Matt Carlson321d32a2008-11-21 17:22:19 -08009717 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -07009718 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009719 else
Michael Chan9c27dbd2006-03-20 22:28:27 -08009720 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009721
9722 return 0;
9723}
9724
Jeff Garzikb9f2c042007-10-03 18:07:32 -07009725static int tg3_get_sset_count (struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009726{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07009727 switch (sset) {
9728 case ETH_SS_TEST:
9729 return TG3_NUM_TEST;
9730 case ETH_SS_STATS:
9731 return TG3_NUM_STATS;
9732 default:
9733 return -EOPNOTSUPP;
9734 }
Michael Chan4cafd3f2005-05-29 14:56:34 -07009735}
9736
Linus Torvalds1da177e2005-04-16 15:20:36 -07009737static void tg3_get_strings (struct net_device *dev, u32 stringset, u8 *buf)
9738{
9739 switch (stringset) {
9740 case ETH_SS_STATS:
9741 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
9742 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -07009743 case ETH_SS_TEST:
9744 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
9745 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009746 default:
9747 WARN_ON(1); /* we need a WARN() */
9748 break;
9749 }
9750}
9751
Michael Chan4009a932005-09-05 17:52:54 -07009752static int tg3_phys_id(struct net_device *dev, u32 data)
9753{
9754 struct tg3 *tp = netdev_priv(dev);
9755 int i;
9756
9757 if (!netif_running(tp->dev))
9758 return -EAGAIN;
9759
9760 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -08009761 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -07009762
9763 for (i = 0; i < (data * 2); i++) {
9764 if ((i % 2) == 0)
9765 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
9766 LED_CTRL_1000MBPS_ON |
9767 LED_CTRL_100MBPS_ON |
9768 LED_CTRL_10MBPS_ON |
9769 LED_CTRL_TRAFFIC_OVERRIDE |
9770 LED_CTRL_TRAFFIC_BLINK |
9771 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009772
Michael Chan4009a932005-09-05 17:52:54 -07009773 else
9774 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
9775 LED_CTRL_TRAFFIC_OVERRIDE);
9776
9777 if (msleep_interruptible(500))
9778 break;
9779 }
9780 tw32(MAC_LED_CTRL, tp->led_ctrl);
9781 return 0;
9782}
9783
Linus Torvalds1da177e2005-04-16 15:20:36 -07009784static void tg3_get_ethtool_stats (struct net_device *dev,
9785 struct ethtool_stats *estats, u64 *tmp_stats)
9786{
9787 struct tg3 *tp = netdev_priv(dev);
9788 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
9789}
9790
Michael Chan566f86a2005-05-29 14:56:58 -07009791#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -08009792#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
9793#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
9794#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -07009795#define NVRAM_SELFBOOT_HW_SIZE 0x20
9796#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -07009797
9798static int tg3_test_nvram(struct tg3 *tp)
9799{
Al Virob9fc7dc2007-12-17 22:59:57 -08009800 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009801 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009802 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -07009803
Matt Carlsondf259d82009-04-20 06:57:14 +00009804 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9805 return 0;
9806
Matt Carlsone4f34112009-02-25 14:25:00 +00009807 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -08009808 return -EIO;
9809
Michael Chan1b277772006-03-20 22:27:48 -08009810 if (magic == TG3_EEPROM_MAGIC)
9811 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -07009812 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -08009813 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
9814 TG3_EEPROM_SB_FORMAT_1) {
9815 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
9816 case TG3_EEPROM_SB_REVISION_0:
9817 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
9818 break;
9819 case TG3_EEPROM_SB_REVISION_2:
9820 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
9821 break;
9822 case TG3_EEPROM_SB_REVISION_3:
9823 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
9824 break;
9825 default:
9826 return 0;
9827 }
9828 } else
Michael Chan1b277772006-03-20 22:27:48 -08009829 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -07009830 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
9831 size = NVRAM_SELFBOOT_HW_SIZE;
9832 else
Michael Chan1b277772006-03-20 22:27:48 -08009833 return -EIO;
9834
9835 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -07009836 if (buf == NULL)
9837 return -ENOMEM;
9838
Michael Chan1b277772006-03-20 22:27:48 -08009839 err = -EIO;
9840 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009841 err = tg3_nvram_read_be32(tp, i, &buf[j]);
9842 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -07009843 break;
Michael Chan566f86a2005-05-29 14:56:58 -07009844 }
Michael Chan1b277772006-03-20 22:27:48 -08009845 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -07009846 goto out;
9847
Michael Chan1b277772006-03-20 22:27:48 -08009848 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009849 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -08009850 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -07009851 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -08009852 u8 *buf8 = (u8 *) buf, csum8 = 0;
9853
Al Virob9fc7dc2007-12-17 22:59:57 -08009854 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -08009855 TG3_EEPROM_SB_REVISION_2) {
9856 /* For rev 2, the csum doesn't include the MBA. */
9857 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
9858 csum8 += buf8[i];
9859 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
9860 csum8 += buf8[i];
9861 } else {
9862 for (i = 0; i < size; i++)
9863 csum8 += buf8[i];
9864 }
Michael Chan1b277772006-03-20 22:27:48 -08009865
Adrian Bunkad96b482006-04-05 22:21:04 -07009866 if (csum8 == 0) {
9867 err = 0;
9868 goto out;
9869 }
9870
9871 err = -EIO;
9872 goto out;
Michael Chan1b277772006-03-20 22:27:48 -08009873 }
Michael Chan566f86a2005-05-29 14:56:58 -07009874
Al Virob9fc7dc2007-12-17 22:59:57 -08009875 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -07009876 TG3_EEPROM_MAGIC_HW) {
9877 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +00009878 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -07009879 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -07009880
9881 /* Separate the parity bits and the data bytes. */
9882 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
9883 if ((i == 0) || (i == 8)) {
9884 int l;
9885 u8 msk;
9886
9887 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
9888 parity[k++] = buf8[i] & msk;
9889 i++;
9890 }
9891 else if (i == 16) {
9892 int l;
9893 u8 msk;
9894
9895 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
9896 parity[k++] = buf8[i] & msk;
9897 i++;
9898
9899 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
9900 parity[k++] = buf8[i] & msk;
9901 i++;
9902 }
9903 data[j++] = buf8[i];
9904 }
9905
9906 err = -EIO;
9907 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
9908 u8 hw8 = hweight8(data[i]);
9909
9910 if ((hw8 & 0x1) && parity[i])
9911 goto out;
9912 else if (!(hw8 & 0x1) && !parity[i])
9913 goto out;
9914 }
9915 err = 0;
9916 goto out;
9917 }
9918
Michael Chan566f86a2005-05-29 14:56:58 -07009919 /* Bootstrap checksum at offset 0x10 */
9920 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlsona9dc5292009-02-25 14:25:30 +00009921 if (csum != be32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -07009922 goto out;
9923
9924 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
9925 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlsona9dc5292009-02-25 14:25:30 +00009926 if (csum != be32_to_cpu(buf[0xfc/4]))
9927 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -07009928
9929 err = 0;
9930
9931out:
9932 kfree(buf);
9933 return err;
9934}
9935
Michael Chanca430072005-05-29 14:57:23 -07009936#define TG3_SERDES_TIMEOUT_SEC 2
9937#define TG3_COPPER_TIMEOUT_SEC 6
9938
9939static int tg3_test_link(struct tg3 *tp)
9940{
9941 int i, max;
9942
9943 if (!netif_running(tp->dev))
9944 return -ENODEV;
9945
Michael Chan4c987482005-09-05 17:52:38 -07009946 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -07009947 max = TG3_SERDES_TIMEOUT_SEC;
9948 else
9949 max = TG3_COPPER_TIMEOUT_SEC;
9950
9951 for (i = 0; i < max; i++) {
9952 if (netif_carrier_ok(tp->dev))
9953 return 0;
9954
9955 if (msleep_interruptible(1000))
9956 break;
9957 }
9958
9959 return -EIO;
9960}
9961
Michael Chana71116d2005-05-29 14:58:11 -07009962/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -08009963static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -07009964{
Michael Chanb16250e2006-09-27 16:10:14 -07009965 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -07009966 u32 offset, read_mask, write_mask, val, save_val, read_val;
9967 static struct {
9968 u16 offset;
9969 u16 flags;
9970#define TG3_FL_5705 0x1
9971#define TG3_FL_NOT_5705 0x2
9972#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -07009973#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -07009974 u32 read_mask;
9975 u32 write_mask;
9976 } reg_tbl[] = {
9977 /* MAC Control Registers */
9978 { MAC_MODE, TG3_FL_NOT_5705,
9979 0x00000000, 0x00ef6f8c },
9980 { MAC_MODE, TG3_FL_5705,
9981 0x00000000, 0x01ef6b8c },
9982 { MAC_STATUS, TG3_FL_NOT_5705,
9983 0x03800107, 0x00000000 },
9984 { MAC_STATUS, TG3_FL_5705,
9985 0x03800100, 0x00000000 },
9986 { MAC_ADDR_0_HIGH, 0x0000,
9987 0x00000000, 0x0000ffff },
9988 { MAC_ADDR_0_LOW, 0x0000,
9989 0x00000000, 0xffffffff },
9990 { MAC_RX_MTU_SIZE, 0x0000,
9991 0x00000000, 0x0000ffff },
9992 { MAC_TX_MODE, 0x0000,
9993 0x00000000, 0x00000070 },
9994 { MAC_TX_LENGTHS, 0x0000,
9995 0x00000000, 0x00003fff },
9996 { MAC_RX_MODE, TG3_FL_NOT_5705,
9997 0x00000000, 0x000007fc },
9998 { MAC_RX_MODE, TG3_FL_5705,
9999 0x00000000, 0x000007dc },
10000 { MAC_HASH_REG_0, 0x0000,
10001 0x00000000, 0xffffffff },
10002 { MAC_HASH_REG_1, 0x0000,
10003 0x00000000, 0xffffffff },
10004 { MAC_HASH_REG_2, 0x0000,
10005 0x00000000, 0xffffffff },
10006 { MAC_HASH_REG_3, 0x0000,
10007 0x00000000, 0xffffffff },
10008
10009 /* Receive Data and Receive BD Initiator Control Registers. */
10010 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10011 0x00000000, 0xffffffff },
10012 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10013 0x00000000, 0xffffffff },
10014 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10015 0x00000000, 0x00000003 },
10016 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10017 0x00000000, 0xffffffff },
10018 { RCVDBDI_STD_BD+0, 0x0000,
10019 0x00000000, 0xffffffff },
10020 { RCVDBDI_STD_BD+4, 0x0000,
10021 0x00000000, 0xffffffff },
10022 { RCVDBDI_STD_BD+8, 0x0000,
10023 0x00000000, 0xffff0002 },
10024 { RCVDBDI_STD_BD+0xc, 0x0000,
10025 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010026
Michael Chana71116d2005-05-29 14:58:11 -070010027 /* Receive BD Initiator Control Registers. */
10028 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10029 0x00000000, 0xffffffff },
10030 { RCVBDI_STD_THRESH, TG3_FL_5705,
10031 0x00000000, 0x000003ff },
10032 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10033 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010034
Michael Chana71116d2005-05-29 14:58:11 -070010035 /* Host Coalescing Control Registers. */
10036 { HOSTCC_MODE, TG3_FL_NOT_5705,
10037 0x00000000, 0x00000004 },
10038 { HOSTCC_MODE, TG3_FL_5705,
10039 0x00000000, 0x000000f6 },
10040 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10041 0x00000000, 0xffffffff },
10042 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10043 0x00000000, 0x000003ff },
10044 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10045 0x00000000, 0xffffffff },
10046 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10047 0x00000000, 0x000003ff },
10048 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10049 0x00000000, 0xffffffff },
10050 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10051 0x00000000, 0x000000ff },
10052 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10053 0x00000000, 0xffffffff },
10054 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10055 0x00000000, 0x000000ff },
10056 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10057 0x00000000, 0xffffffff },
10058 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10059 0x00000000, 0xffffffff },
10060 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10061 0x00000000, 0xffffffff },
10062 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10063 0x00000000, 0x000000ff },
10064 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10065 0x00000000, 0xffffffff },
10066 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10067 0x00000000, 0x000000ff },
10068 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10069 0x00000000, 0xffffffff },
10070 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10071 0x00000000, 0xffffffff },
10072 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10073 0x00000000, 0xffffffff },
10074 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10075 0x00000000, 0xffffffff },
10076 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10077 0x00000000, 0xffffffff },
10078 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10079 0xffffffff, 0x00000000 },
10080 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10081 0xffffffff, 0x00000000 },
10082
10083 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010084 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010085 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010086 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010087 0x00000000, 0x007fffff },
10088 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10089 0x00000000, 0x0000003f },
10090 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10091 0x00000000, 0x000001ff },
10092 { BUFMGR_MB_HIGH_WATER, 0x0000,
10093 0x00000000, 0x000001ff },
10094 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10095 0xffffffff, 0x00000000 },
10096 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10097 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010098
Michael Chana71116d2005-05-29 14:58:11 -070010099 /* Mailbox Registers */
10100 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10101 0x00000000, 0x000001ff },
10102 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10103 0x00000000, 0x000001ff },
10104 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10105 0x00000000, 0x000007ff },
10106 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10107 0x00000000, 0x000001ff },
10108
10109 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10110 };
10111
Michael Chanb16250e2006-09-27 16:10:14 -070010112 is_5705 = is_5750 = 0;
10113 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010114 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010115 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10116 is_5750 = 1;
10117 }
Michael Chana71116d2005-05-29 14:58:11 -070010118
10119 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10120 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10121 continue;
10122
10123 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10124 continue;
10125
10126 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10127 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10128 continue;
10129
Michael Chanb16250e2006-09-27 16:10:14 -070010130 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10131 continue;
10132
Michael Chana71116d2005-05-29 14:58:11 -070010133 offset = (u32) reg_tbl[i].offset;
10134 read_mask = reg_tbl[i].read_mask;
10135 write_mask = reg_tbl[i].write_mask;
10136
10137 /* Save the original register content */
10138 save_val = tr32(offset);
10139
10140 /* Determine the read-only value. */
10141 read_val = save_val & read_mask;
10142
10143 /* Write zero to the register, then make sure the read-only bits
10144 * are not changed and the read/write bits are all zeros.
10145 */
10146 tw32(offset, 0);
10147
10148 val = tr32(offset);
10149
10150 /* Test the read-only and read/write bits. */
10151 if (((val & read_mask) != read_val) || (val & write_mask))
10152 goto out;
10153
10154 /* Write ones to all the bits defined by RdMask and WrMask, then
10155 * make sure the read-only bits are not changed and the
10156 * read/write bits are all ones.
10157 */
10158 tw32(offset, read_mask | write_mask);
10159
10160 val = tr32(offset);
10161
10162 /* Test the read-only bits. */
10163 if ((val & read_mask) != read_val)
10164 goto out;
10165
10166 /* Test the read/write bits. */
10167 if ((val & write_mask) != write_mask)
10168 goto out;
10169
10170 tw32(offset, save_val);
10171 }
10172
10173 return 0;
10174
10175out:
Michael Chan9f88f292006-12-07 00:22:54 -080010176 if (netif_msg_hw(tp))
10177 printk(KERN_ERR PFX "Register test failed at offset %x\n",
10178 offset);
Michael Chana71116d2005-05-29 14:58:11 -070010179 tw32(offset, save_val);
10180 return -EIO;
10181}
10182
Michael Chan7942e1d2005-05-29 14:58:36 -070010183static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10184{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010185 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010186 int i;
10187 u32 j;
10188
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010189 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010190 for (j = 0; j < len; j += 4) {
10191 u32 val;
10192
10193 tg3_write_mem(tp, offset + j, test_pattern[i]);
10194 tg3_read_mem(tp, offset + j, &val);
10195 if (val != test_pattern[i])
10196 return -EIO;
10197 }
10198 }
10199 return 0;
10200}
10201
10202static int tg3_test_memory(struct tg3 *tp)
10203{
10204 static struct mem_entry {
10205 u32 offset;
10206 u32 len;
10207 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010208 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010209 { 0x00002000, 0x1c000},
10210 { 0xffffffff, 0x00000}
10211 }, mem_tbl_5705[] = {
10212 { 0x00000100, 0x0000c},
10213 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010214 { 0x00004000, 0x00800},
10215 { 0x00006000, 0x01000},
10216 { 0x00008000, 0x02000},
10217 { 0x00010000, 0x0e000},
10218 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010219 }, mem_tbl_5755[] = {
10220 { 0x00000200, 0x00008},
10221 { 0x00004000, 0x00800},
10222 { 0x00006000, 0x00800},
10223 { 0x00008000, 0x02000},
10224 { 0x00010000, 0x0c000},
10225 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010226 }, mem_tbl_5906[] = {
10227 { 0x00000200, 0x00008},
10228 { 0x00004000, 0x00400},
10229 { 0x00006000, 0x00400},
10230 { 0x00008000, 0x01000},
10231 { 0x00010000, 0x01000},
10232 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010233 };
10234 struct mem_entry *mem_tbl;
10235 int err = 0;
10236 int i;
10237
Matt Carlson321d32a2008-11-21 17:22:19 -080010238 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
10239 mem_tbl = mem_tbl_5755;
10240 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10241 mem_tbl = mem_tbl_5906;
10242 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10243 mem_tbl = mem_tbl_5705;
10244 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010245 mem_tbl = mem_tbl_570x;
10246
10247 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
10248 if ((err = tg3_do_mem_test(tp, mem_tbl[i].offset,
10249 mem_tbl[i].len)) != 0)
10250 break;
10251 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010252
Michael Chan7942e1d2005-05-29 14:58:36 -070010253 return err;
10254}
10255
Michael Chan9f40dea2005-09-05 17:53:06 -070010256#define TG3_MAC_LOOPBACK 0
10257#define TG3_PHY_LOOPBACK 1
10258
10259static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010260{
Michael Chan9f40dea2005-09-05 17:53:06 -070010261 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010262 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010263 struct sk_buff *skb, *rx_skb;
10264 u8 *tx_data;
10265 dma_addr_t map;
10266 int num_pkts, tx_len, rx_len, i, err;
10267 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010268 struct tg3_napi *tnapi, *rnapi;
Matt Carlson21f581a2009-08-28 14:00:25 +000010269 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Michael Chanc76949a2005-05-29 14:58:59 -070010270
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010271 if (tp->irq_cnt > 1) {
10272 tnapi = &tp->napi[1];
10273 rnapi = &tp->napi[1];
10274 } else {
10275 tnapi = &tp->napi[0];
10276 rnapi = &tp->napi[0];
10277 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010278 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010279
Michael Chan9f40dea2005-09-05 17:53:06 -070010280 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010281 /* HW errata - mac loopback fails in some cases on 5780.
10282 * Normal traffic and PHY loopback are not affected by
10283 * errata.
10284 */
10285 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
10286 return 0;
10287
Michael Chan9f40dea2005-09-05 17:53:06 -070010288 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010289 MAC_MODE_PORT_INT_LPBACK;
10290 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10291 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chan3f7045c2006-09-27 16:02:29 -070010292 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
10293 mac_mode |= MAC_MODE_PORT_MODE_MII;
10294 else
10295 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010296 tw32(MAC_MODE, mac_mode);
10297 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010298 u32 val;
10299
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010300 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10301 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010302 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10303 } else
10304 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010305
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010306 tg3_phy_toggle_automdix(tp, 0);
10307
Michael Chan3f7045c2006-09-27 16:02:29 -070010308 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010309 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010310
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010311 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010312 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10313 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10314 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x1800);
Michael Chan5d64ad32006-12-07 00:19:40 -080010315 mac_mode |= MAC_MODE_PORT_MODE_MII;
10316 } else
10317 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010318
Michael Chanc94e3942005-09-27 12:12:42 -070010319 /* reset to prevent losing 1st rx packet intermittently */
10320 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
10321 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10322 udelay(10);
10323 tw32_f(MAC_RX_MODE, tp->rx_mode);
10324 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010325 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
10326 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)
10327 mac_mode &= ~MAC_MODE_LINK_POLARITY;
10328 else if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411)
10329 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010330 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10331 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10332 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010333 tw32(MAC_MODE, mac_mode);
Michael Chan9f40dea2005-09-05 17:53:06 -070010334 }
10335 else
10336 return -EINVAL;
Michael Chanc76949a2005-05-29 14:58:59 -070010337
10338 err = -EIO;
10339
Michael Chanc76949a2005-05-29 14:58:59 -070010340 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010341 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010342 if (!skb)
10343 return -ENOMEM;
10344
Michael Chanc76949a2005-05-29 14:58:59 -070010345 tx_data = skb_put(skb, tx_len);
10346 memcpy(tx_data, tp->dev->dev_addr, 6);
10347 memset(tx_data + 6, 0x0, 8);
10348
10349 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10350
10351 for (i = 14; i < tx_len; i++)
10352 tx_data[i] = (u8) (i & 0xff);
10353
10354 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
10355
10356 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010357 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010358
10359 udelay(10);
10360
Matt Carlson898a56f2009-08-28 14:02:40 +000010361 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070010362
Michael Chanc76949a2005-05-29 14:58:59 -070010363 num_pkts = 0;
10364
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010365 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070010366
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010367 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070010368 num_pkts++;
10369
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010370 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
10371 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070010372
10373 udelay(10);
10374
Michael Chan3f7045c2006-09-27 16:02:29 -070010375 /* 250 usec to allow enough time on some 10/100 Mbps devices. */
10376 for (i = 0; i < 25; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070010377 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010378 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010379
10380 udelay(10);
10381
Matt Carlson898a56f2009-08-28 14:02:40 +000010382 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
10383 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010384 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070010385 (rx_idx == (rx_start_idx + num_pkts)))
10386 break;
10387 }
10388
10389 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
10390 dev_kfree_skb(skb);
10391
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010392 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070010393 goto out;
10394
10395 if (rx_idx != rx_start_idx + num_pkts)
10396 goto out;
10397
Matt Carlson72334482009-08-28 14:03:01 +000010398 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070010399 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
10400 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
10401 if (opaque_key != RXD_OPAQUE_RING_STD)
10402 goto out;
10403
10404 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
10405 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
10406 goto out;
10407
10408 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
10409 if (rx_len != tx_len)
10410 goto out;
10411
Matt Carlson21f581a2009-08-28 14:00:25 +000010412 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070010413
Matt Carlson21f581a2009-08-28 14:00:25 +000010414 map = pci_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070010415 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
10416
10417 for (i = 14; i < tx_len; i++) {
10418 if (*(rx_skb->data + i) != (u8) (i & 0xff))
10419 goto out;
10420 }
10421 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010422
Michael Chanc76949a2005-05-29 14:58:59 -070010423 /* tg3_free_rings will unmap and free the rx_skb */
10424out:
10425 return err;
10426}
10427
Michael Chan9f40dea2005-09-05 17:53:06 -070010428#define TG3_MAC_LOOPBACK_FAILED 1
10429#define TG3_PHY_LOOPBACK_FAILED 2
10430#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
10431 TG3_PHY_LOOPBACK_FAILED)
10432
10433static int tg3_test_loopback(struct tg3 *tp)
10434{
10435 int err = 0;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010436 u32 cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070010437
10438 if (!netif_running(tp->dev))
10439 return TG3_LOOPBACK_FAILED;
10440
Michael Chanb9ec6c12006-07-25 16:37:27 -070010441 err = tg3_reset_hw(tp, 1);
10442 if (err)
10443 return TG3_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070010444
Matt Carlson6833c042008-11-21 17:18:59 -080010445 /* Turn off gphy autopowerdown. */
10446 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10447 tg3_phy_toggle_apd(tp, false);
10448
Matt Carlson321d32a2008-11-21 17:22:19 -080010449 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010450 int i;
10451 u32 status;
10452
10453 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
10454
10455 /* Wait for up to 40 microseconds to acquire lock. */
10456 for (i = 0; i < 4; i++) {
10457 status = tr32(TG3_CPMU_MUTEX_GNT);
10458 if (status == CPMU_MUTEX_GNT_DRIVER)
10459 break;
10460 udelay(10);
10461 }
10462
10463 if (status != CPMU_MUTEX_GNT_DRIVER)
10464 return TG3_LOOPBACK_FAILED;
10465
Matt Carlsonb2a5c192008-04-03 21:44:44 -070010466 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080010467 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070010468 tw32(TG3_CPMU_CTRL,
10469 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
10470 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070010471 }
10472
Michael Chan9f40dea2005-09-05 17:53:06 -070010473 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
10474 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010475
Matt Carlson321d32a2008-11-21 17:22:19 -080010476 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010477 tw32(TG3_CPMU_CTRL, cpmuctrl);
10478
10479 /* Release the mutex */
10480 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
10481 }
10482
Matt Carlsondd477002008-05-25 23:45:58 -070010483 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
10484 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070010485 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
10486 err |= TG3_PHY_LOOPBACK_FAILED;
10487 }
10488
Matt Carlson6833c042008-11-21 17:18:59 -080010489 /* Re-enable gphy autopowerdown. */
10490 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10491 tg3_phy_toggle_apd(tp, true);
10492
Michael Chan9f40dea2005-09-05 17:53:06 -070010493 return err;
10494}
10495
Michael Chan4cafd3f2005-05-29 14:56:34 -070010496static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
10497 u64 *data)
10498{
Michael Chan566f86a2005-05-29 14:56:58 -070010499 struct tg3 *tp = netdev_priv(dev);
10500
Michael Chanbc1c7562006-03-20 17:48:03 -080010501 if (tp->link_config.phy_is_low_power)
10502 tg3_set_power_state(tp, PCI_D0);
10503
Michael Chan566f86a2005-05-29 14:56:58 -070010504 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
10505
10506 if (tg3_test_nvram(tp) != 0) {
10507 etest->flags |= ETH_TEST_FL_FAILED;
10508 data[0] = 1;
10509 }
Michael Chanca430072005-05-29 14:57:23 -070010510 if (tg3_test_link(tp) != 0) {
10511 etest->flags |= ETH_TEST_FL_FAILED;
10512 data[1] = 1;
10513 }
Michael Chana71116d2005-05-29 14:58:11 -070010514 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010515 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070010516
Michael Chanbbe832c2005-06-24 20:20:04 -070010517 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010518 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010519 tg3_netif_stop(tp);
10520 irq_sync = 1;
10521 }
10522
10523 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070010524
10525 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080010526 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010527 tg3_halt_cpu(tp, RX_CPU_BASE);
10528 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10529 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080010530 if (!err)
10531 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010532
Michael Chand9ab5ad12006-03-20 22:27:35 -080010533 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
10534 tg3_phy_reset(tp);
10535
Michael Chana71116d2005-05-29 14:58:11 -070010536 if (tg3_test_registers(tp) != 0) {
10537 etest->flags |= ETH_TEST_FL_FAILED;
10538 data[2] = 1;
10539 }
Michael Chan7942e1d2005-05-29 14:58:36 -070010540 if (tg3_test_memory(tp) != 0) {
10541 etest->flags |= ETH_TEST_FL_FAILED;
10542 data[3] = 1;
10543 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010544 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070010545 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070010546
David S. Millerf47c11e2005-06-24 20:18:35 -070010547 tg3_full_unlock(tp);
10548
Michael Chand4bc3922005-05-29 14:59:20 -070010549 if (tg3_test_interrupt(tp) != 0) {
10550 etest->flags |= ETH_TEST_FL_FAILED;
10551 data[5] = 1;
10552 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010553
10554 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070010555
Michael Chana71116d2005-05-29 14:58:11 -070010556 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10557 if (netif_running(dev)) {
10558 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010559 err2 = tg3_restart_hw(tp, 1);
10560 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070010561 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010562 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010563
10564 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010565
10566 if (irq_sync && !err2)
10567 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010568 }
Michael Chanbc1c7562006-03-20 17:48:03 -080010569 if (tp->link_config.phy_is_low_power)
10570 tg3_set_power_state(tp, PCI_D3hot);
10571
Michael Chan4cafd3f2005-05-29 14:56:34 -070010572}
10573
Linus Torvalds1da177e2005-04-16 15:20:36 -070010574static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10575{
10576 struct mii_ioctl_data *data = if_mii(ifr);
10577 struct tg3 *tp = netdev_priv(dev);
10578 int err;
10579
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010580 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
10581 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
10582 return -EAGAIN;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070010583 return phy_mii_ioctl(tp->mdio_bus->phy_map[PHY_ADDR], data, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010584 }
10585
Linus Torvalds1da177e2005-04-16 15:20:36 -070010586 switch(cmd) {
10587 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000010588 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010589
10590 /* fallthru */
10591 case SIOCGMIIREG: {
10592 u32 mii_regval;
10593
10594 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10595 break; /* We have no PHY */
10596
Michael Chanbc1c7562006-03-20 17:48:03 -080010597 if (tp->link_config.phy_is_low_power)
10598 return -EAGAIN;
10599
David S. Millerf47c11e2005-06-24 20:18:35 -070010600 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010601 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070010602 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010603
10604 data->val_out = mii_regval;
10605
10606 return err;
10607 }
10608
10609 case SIOCSMIIREG:
10610 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10611 break; /* We have no PHY */
10612
Michael Chanbc1c7562006-03-20 17:48:03 -080010613 if (tp->link_config.phy_is_low_power)
10614 return -EAGAIN;
10615
David S. Millerf47c11e2005-06-24 20:18:35 -070010616 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010617 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070010618 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010619
10620 return err;
10621
10622 default:
10623 /* do nothing */
10624 break;
10625 }
10626 return -EOPNOTSUPP;
10627}
10628
10629#if TG3_VLAN_TAG_USED
10630static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
10631{
10632 struct tg3 *tp = netdev_priv(dev);
10633
Matt Carlson844b3ee2009-02-25 14:23:56 +000010634 if (!netif_running(dev)) {
10635 tp->vlgrp = grp;
10636 return;
10637 }
10638
10639 tg3_netif_stop(tp);
Michael Chan29315e82006-06-29 20:12:30 -070010640
David S. Millerf47c11e2005-06-24 20:18:35 -070010641 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010642
10643 tp->vlgrp = grp;
10644
10645 /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
10646 __tg3_set_rx_mode(dev);
10647
Matt Carlson844b3ee2009-02-25 14:23:56 +000010648 tg3_netif_start(tp);
Michael Chan46966542007-07-11 19:47:19 -070010649
10650 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010651}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010652#endif
10653
David S. Miller15f98502005-05-18 22:49:26 -070010654static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
10655{
10656 struct tg3 *tp = netdev_priv(dev);
10657
10658 memcpy(ec, &tp->coal, sizeof(*ec));
10659 return 0;
10660}
10661
Michael Chand244c892005-07-05 14:42:33 -070010662static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
10663{
10664 struct tg3 *tp = netdev_priv(dev);
10665 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
10666 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
10667
10668 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
10669 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
10670 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
10671 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
10672 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
10673 }
10674
10675 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
10676 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
10677 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
10678 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
10679 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
10680 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
10681 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
10682 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
10683 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
10684 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
10685 return -EINVAL;
10686
10687 /* No rx interrupts will be generated if both are zero */
10688 if ((ec->rx_coalesce_usecs == 0) &&
10689 (ec->rx_max_coalesced_frames == 0))
10690 return -EINVAL;
10691
10692 /* No tx interrupts will be generated if both are zero */
10693 if ((ec->tx_coalesce_usecs == 0) &&
10694 (ec->tx_max_coalesced_frames == 0))
10695 return -EINVAL;
10696
10697 /* Only copy relevant parameters, ignore all others. */
10698 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
10699 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
10700 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
10701 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
10702 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
10703 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
10704 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
10705 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
10706 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
10707
10708 if (netif_running(dev)) {
10709 tg3_full_lock(tp, 0);
10710 __tg3_set_coalesce(tp, &tp->coal);
10711 tg3_full_unlock(tp);
10712 }
10713 return 0;
10714}
10715
Jeff Garzik7282d492006-09-13 14:30:00 -040010716static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010717 .get_settings = tg3_get_settings,
10718 .set_settings = tg3_set_settings,
10719 .get_drvinfo = tg3_get_drvinfo,
10720 .get_regs_len = tg3_get_regs_len,
10721 .get_regs = tg3_get_regs,
10722 .get_wol = tg3_get_wol,
10723 .set_wol = tg3_set_wol,
10724 .get_msglevel = tg3_get_msglevel,
10725 .set_msglevel = tg3_set_msglevel,
10726 .nway_reset = tg3_nway_reset,
10727 .get_link = ethtool_op_get_link,
10728 .get_eeprom_len = tg3_get_eeprom_len,
10729 .get_eeprom = tg3_get_eeprom,
10730 .set_eeprom = tg3_set_eeprom,
10731 .get_ringparam = tg3_get_ringparam,
10732 .set_ringparam = tg3_set_ringparam,
10733 .get_pauseparam = tg3_get_pauseparam,
10734 .set_pauseparam = tg3_set_pauseparam,
10735 .get_rx_csum = tg3_get_rx_csum,
10736 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010737 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010738 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010739 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070010740 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010741 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070010742 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010743 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070010744 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070010745 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010746 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010747};
10748
10749static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
10750{
Michael Chan1b277772006-03-20 22:27:48 -080010751 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010752
10753 tp->nvram_size = EEPROM_CHIP_SIZE;
10754
Matt Carlsone4f34112009-02-25 14:25:00 +000010755 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010756 return;
10757
Michael Chanb16250e2006-09-27 16:10:14 -070010758 if ((magic != TG3_EEPROM_MAGIC) &&
10759 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
10760 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010761 return;
10762
10763 /*
10764 * Size the chip by reading offsets at increasing powers of two.
10765 * When we encounter our validation signature, we know the addressing
10766 * has wrapped around, and thus have our chip size.
10767 */
Michael Chan1b277772006-03-20 22:27:48 -080010768 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010769
10770 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000010771 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010772 return;
10773
Michael Chan18201802006-03-20 22:29:15 -080010774 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010775 break;
10776
10777 cursize <<= 1;
10778 }
10779
10780 tp->nvram_size = cursize;
10781}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010782
Linus Torvalds1da177e2005-04-16 15:20:36 -070010783static void __devinit tg3_get_nvram_size(struct tg3 *tp)
10784{
10785 u32 val;
10786
Matt Carlsondf259d82009-04-20 06:57:14 +000010787 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
10788 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010789 return;
10790
10791 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080010792 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080010793 tg3_get_eeprom_size(tp);
10794 return;
10795 }
10796
Matt Carlson6d348f22009-02-25 14:25:52 +000010797 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010798 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000010799 /* This is confusing. We want to operate on the
10800 * 16-bit value at offset 0xf2. The tg3_nvram_read()
10801 * call will read from NVRAM and byteswap the data
10802 * according to the byteswapping settings for all
10803 * other register accesses. This ensures the data we
10804 * want will always reside in the lower 16-bits.
10805 * However, the data in NVRAM is in LE format, which
10806 * means the data from the NVRAM read will always be
10807 * opposite the endianness of the CPU. The 16-bit
10808 * byteswap then brings the data to CPU endianness.
10809 */
10810 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010811 return;
10812 }
10813 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010814 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010815}
10816
10817static void __devinit tg3_get_nvram_info(struct tg3 *tp)
10818{
10819 u32 nvcfg1;
10820
10821 nvcfg1 = tr32(NVRAM_CFG1);
10822 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
10823 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000010824 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010825 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
10826 tw32(NVRAM_CFG1, nvcfg1);
10827 }
10828
Michael Chan4c987482005-09-05 17:52:38 -070010829 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070010830 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010831 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000010832 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
10833 tp->nvram_jedecnum = JEDEC_ATMEL;
10834 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
10835 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10836 break;
10837 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
10838 tp->nvram_jedecnum = JEDEC_ATMEL;
10839 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
10840 break;
10841 case FLASH_VENDOR_ATMEL_EEPROM:
10842 tp->nvram_jedecnum = JEDEC_ATMEL;
10843 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
10844 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10845 break;
10846 case FLASH_VENDOR_ST:
10847 tp->nvram_jedecnum = JEDEC_ST;
10848 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
10849 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10850 break;
10851 case FLASH_VENDOR_SAIFUN:
10852 tp->nvram_jedecnum = JEDEC_SAIFUN;
10853 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
10854 break;
10855 case FLASH_VENDOR_SST_SMALL:
10856 case FLASH_VENDOR_SST_LARGE:
10857 tp->nvram_jedecnum = JEDEC_SST;
10858 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
10859 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010860 }
Matt Carlson8590a602009-08-28 12:29:16 +000010861 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010862 tp->nvram_jedecnum = JEDEC_ATMEL;
10863 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
10864 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10865 }
10866}
10867
Matt Carlsona1b950d2009-09-01 13:20:17 +000010868static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
10869{
10870 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
10871 case FLASH_5752PAGE_SIZE_256:
10872 tp->nvram_pagesize = 256;
10873 break;
10874 case FLASH_5752PAGE_SIZE_512:
10875 tp->nvram_pagesize = 512;
10876 break;
10877 case FLASH_5752PAGE_SIZE_1K:
10878 tp->nvram_pagesize = 1024;
10879 break;
10880 case FLASH_5752PAGE_SIZE_2K:
10881 tp->nvram_pagesize = 2048;
10882 break;
10883 case FLASH_5752PAGE_SIZE_4K:
10884 tp->nvram_pagesize = 4096;
10885 break;
10886 case FLASH_5752PAGE_SIZE_264:
10887 tp->nvram_pagesize = 264;
10888 break;
10889 case FLASH_5752PAGE_SIZE_528:
10890 tp->nvram_pagesize = 528;
10891 break;
10892 }
10893}
10894
Michael Chan361b4ac2005-04-21 17:11:21 -070010895static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
10896{
10897 u32 nvcfg1;
10898
10899 nvcfg1 = tr32(NVRAM_CFG1);
10900
Michael Chane6af3012005-04-21 17:12:05 -070010901 /* NVRAM protection for TPM */
10902 if (nvcfg1 & (1 << 27))
10903 tp->tg3_flags2 |= TG3_FLG2_PROTECTED_NVRAM;
10904
Michael Chan361b4ac2005-04-21 17:11:21 -070010905 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000010906 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
10907 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
10908 tp->nvram_jedecnum = JEDEC_ATMEL;
10909 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10910 break;
10911 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
10912 tp->nvram_jedecnum = JEDEC_ATMEL;
10913 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10914 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10915 break;
10916 case FLASH_5752VENDOR_ST_M45PE10:
10917 case FLASH_5752VENDOR_ST_M45PE20:
10918 case FLASH_5752VENDOR_ST_M45PE40:
10919 tp->nvram_jedecnum = JEDEC_ST;
10920 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10921 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10922 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070010923 }
10924
10925 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000010926 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000010927 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070010928 /* For eeprom, set pagesize to maximum eeprom size */
10929 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
10930
10931 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
10932 tw32(NVRAM_CFG1, nvcfg1);
10933 }
10934}
10935
Michael Chand3c7b882006-03-23 01:28:25 -080010936static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
10937{
Matt Carlson989a9d22007-05-05 11:51:05 -070010938 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080010939
10940 nvcfg1 = tr32(NVRAM_CFG1);
10941
10942 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070010943 if (nvcfg1 & (1 << 27)) {
Michael Chand3c7b882006-03-23 01:28:25 -080010944 tp->tg3_flags2 |= TG3_FLG2_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070010945 protect = 1;
10946 }
Michael Chand3c7b882006-03-23 01:28:25 -080010947
Matt Carlson989a9d22007-05-05 11:51:05 -070010948 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
10949 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000010950 case FLASH_5755VENDOR_ATMEL_FLASH_1:
10951 case FLASH_5755VENDOR_ATMEL_FLASH_2:
10952 case FLASH_5755VENDOR_ATMEL_FLASH_3:
10953 case FLASH_5755VENDOR_ATMEL_FLASH_5:
10954 tp->nvram_jedecnum = JEDEC_ATMEL;
10955 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10956 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10957 tp->nvram_pagesize = 264;
10958 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
10959 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
10960 tp->nvram_size = (protect ? 0x3e200 :
10961 TG3_NVRAM_SIZE_512KB);
10962 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
10963 tp->nvram_size = (protect ? 0x1f200 :
10964 TG3_NVRAM_SIZE_256KB);
10965 else
10966 tp->nvram_size = (protect ? 0x1f200 :
10967 TG3_NVRAM_SIZE_128KB);
10968 break;
10969 case FLASH_5752VENDOR_ST_M45PE10:
10970 case FLASH_5752VENDOR_ST_M45PE20:
10971 case FLASH_5752VENDOR_ST_M45PE40:
10972 tp->nvram_jedecnum = JEDEC_ST;
10973 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10974 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10975 tp->nvram_pagesize = 256;
10976 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
10977 tp->nvram_size = (protect ?
10978 TG3_NVRAM_SIZE_64KB :
10979 TG3_NVRAM_SIZE_128KB);
10980 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
10981 tp->nvram_size = (protect ?
10982 TG3_NVRAM_SIZE_64KB :
10983 TG3_NVRAM_SIZE_256KB);
10984 else
10985 tp->nvram_size = (protect ?
10986 TG3_NVRAM_SIZE_128KB :
10987 TG3_NVRAM_SIZE_512KB);
10988 break;
Michael Chand3c7b882006-03-23 01:28:25 -080010989 }
10990}
10991
Michael Chan1b277772006-03-20 22:27:48 -080010992static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
10993{
10994 u32 nvcfg1;
10995
10996 nvcfg1 = tr32(NVRAM_CFG1);
10997
10998 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000010999 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11000 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11001 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11002 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11003 tp->nvram_jedecnum = JEDEC_ATMEL;
11004 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11005 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011006
Matt Carlson8590a602009-08-28 12:29:16 +000011007 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11008 tw32(NVRAM_CFG1, nvcfg1);
11009 break;
11010 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11011 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11012 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11013 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11014 tp->nvram_jedecnum = JEDEC_ATMEL;
11015 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11016 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11017 tp->nvram_pagesize = 264;
11018 break;
11019 case FLASH_5752VENDOR_ST_M45PE10:
11020 case FLASH_5752VENDOR_ST_M45PE20:
11021 case FLASH_5752VENDOR_ST_M45PE40:
11022 tp->nvram_jedecnum = JEDEC_ST;
11023 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11024 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11025 tp->nvram_pagesize = 256;
11026 break;
Michael Chan1b277772006-03-20 22:27:48 -080011027 }
11028}
11029
Matt Carlson6b91fa02007-10-10 18:01:09 -070011030static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11031{
11032 u32 nvcfg1, protect = 0;
11033
11034 nvcfg1 = tr32(NVRAM_CFG1);
11035
11036 /* NVRAM protection for TPM */
11037 if (nvcfg1 & (1 << 27)) {
11038 tp->tg3_flags2 |= TG3_FLG2_PROTECTED_NVRAM;
11039 protect = 1;
11040 }
11041
11042 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11043 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011044 case FLASH_5761VENDOR_ATMEL_ADB021D:
11045 case FLASH_5761VENDOR_ATMEL_ADB041D:
11046 case FLASH_5761VENDOR_ATMEL_ADB081D:
11047 case FLASH_5761VENDOR_ATMEL_ADB161D:
11048 case FLASH_5761VENDOR_ATMEL_MDB021D:
11049 case FLASH_5761VENDOR_ATMEL_MDB041D:
11050 case FLASH_5761VENDOR_ATMEL_MDB081D:
11051 case FLASH_5761VENDOR_ATMEL_MDB161D:
11052 tp->nvram_jedecnum = JEDEC_ATMEL;
11053 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11054 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11055 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11056 tp->nvram_pagesize = 256;
11057 break;
11058 case FLASH_5761VENDOR_ST_A_M45PE20:
11059 case FLASH_5761VENDOR_ST_A_M45PE40:
11060 case FLASH_5761VENDOR_ST_A_M45PE80:
11061 case FLASH_5761VENDOR_ST_A_M45PE16:
11062 case FLASH_5761VENDOR_ST_M_M45PE20:
11063 case FLASH_5761VENDOR_ST_M_M45PE40:
11064 case FLASH_5761VENDOR_ST_M_M45PE80:
11065 case FLASH_5761VENDOR_ST_M_M45PE16:
11066 tp->nvram_jedecnum = JEDEC_ST;
11067 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11068 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11069 tp->nvram_pagesize = 256;
11070 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011071 }
11072
11073 if (protect) {
11074 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11075 } else {
11076 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011077 case FLASH_5761VENDOR_ATMEL_ADB161D:
11078 case FLASH_5761VENDOR_ATMEL_MDB161D:
11079 case FLASH_5761VENDOR_ST_A_M45PE16:
11080 case FLASH_5761VENDOR_ST_M_M45PE16:
11081 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11082 break;
11083 case FLASH_5761VENDOR_ATMEL_ADB081D:
11084 case FLASH_5761VENDOR_ATMEL_MDB081D:
11085 case FLASH_5761VENDOR_ST_A_M45PE80:
11086 case FLASH_5761VENDOR_ST_M_M45PE80:
11087 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11088 break;
11089 case FLASH_5761VENDOR_ATMEL_ADB041D:
11090 case FLASH_5761VENDOR_ATMEL_MDB041D:
11091 case FLASH_5761VENDOR_ST_A_M45PE40:
11092 case FLASH_5761VENDOR_ST_M_M45PE40:
11093 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11094 break;
11095 case FLASH_5761VENDOR_ATMEL_ADB021D:
11096 case FLASH_5761VENDOR_ATMEL_MDB021D:
11097 case FLASH_5761VENDOR_ST_A_M45PE20:
11098 case FLASH_5761VENDOR_ST_M_M45PE20:
11099 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11100 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011101 }
11102 }
11103}
11104
Michael Chanb5d37722006-09-27 16:06:21 -070011105static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11106{
11107 tp->nvram_jedecnum = JEDEC_ATMEL;
11108 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11109 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11110}
11111
Matt Carlson321d32a2008-11-21 17:22:19 -080011112static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11113{
11114 u32 nvcfg1;
11115
11116 nvcfg1 = tr32(NVRAM_CFG1);
11117
11118 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11119 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11120 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11121 tp->nvram_jedecnum = JEDEC_ATMEL;
11122 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11123 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11124
11125 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11126 tw32(NVRAM_CFG1, nvcfg1);
11127 return;
11128 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11129 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11130 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11131 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11132 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11133 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11134 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11135 tp->nvram_jedecnum = JEDEC_ATMEL;
11136 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11137 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11138
11139 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11140 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11141 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11142 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11143 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11144 break;
11145 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11146 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11147 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11148 break;
11149 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11150 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11151 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11152 break;
11153 }
11154 break;
11155 case FLASH_5752VENDOR_ST_M45PE10:
11156 case FLASH_5752VENDOR_ST_M45PE20:
11157 case FLASH_5752VENDOR_ST_M45PE40:
11158 tp->nvram_jedecnum = JEDEC_ST;
11159 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11160 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11161
11162 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11163 case FLASH_5752VENDOR_ST_M45PE10:
11164 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11165 break;
11166 case FLASH_5752VENDOR_ST_M45PE20:
11167 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11168 break;
11169 case FLASH_5752VENDOR_ST_M45PE40:
11170 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11171 break;
11172 }
11173 break;
11174 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011175 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011176 return;
11177 }
11178
Matt Carlsona1b950d2009-09-01 13:20:17 +000011179 tg3_nvram_get_pagesize(tp, nvcfg1);
11180 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011181 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011182}
11183
11184
11185static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11186{
11187 u32 nvcfg1;
11188
11189 nvcfg1 = tr32(NVRAM_CFG1);
11190
11191 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11192 case FLASH_5717VENDOR_ATMEL_EEPROM:
11193 case FLASH_5717VENDOR_MICRO_EEPROM:
11194 tp->nvram_jedecnum = JEDEC_ATMEL;
11195 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11196 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11197
11198 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11199 tw32(NVRAM_CFG1, nvcfg1);
11200 return;
11201 case FLASH_5717VENDOR_ATMEL_MDB011D:
11202 case FLASH_5717VENDOR_ATMEL_ADB011B:
11203 case FLASH_5717VENDOR_ATMEL_ADB011D:
11204 case FLASH_5717VENDOR_ATMEL_MDB021D:
11205 case FLASH_5717VENDOR_ATMEL_ADB021B:
11206 case FLASH_5717VENDOR_ATMEL_ADB021D:
11207 case FLASH_5717VENDOR_ATMEL_45USPT:
11208 tp->nvram_jedecnum = JEDEC_ATMEL;
11209 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11210 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11211
11212 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11213 case FLASH_5717VENDOR_ATMEL_MDB021D:
11214 case FLASH_5717VENDOR_ATMEL_ADB021B:
11215 case FLASH_5717VENDOR_ATMEL_ADB021D:
11216 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11217 break;
11218 default:
11219 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11220 break;
11221 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011222 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011223 case FLASH_5717VENDOR_ST_M_M25PE10:
11224 case FLASH_5717VENDOR_ST_A_M25PE10:
11225 case FLASH_5717VENDOR_ST_M_M45PE10:
11226 case FLASH_5717VENDOR_ST_A_M45PE10:
11227 case FLASH_5717VENDOR_ST_M_M25PE20:
11228 case FLASH_5717VENDOR_ST_A_M25PE20:
11229 case FLASH_5717VENDOR_ST_M_M45PE20:
11230 case FLASH_5717VENDOR_ST_A_M45PE20:
11231 case FLASH_5717VENDOR_ST_25USPT:
11232 case FLASH_5717VENDOR_ST_45USPT:
11233 tp->nvram_jedecnum = JEDEC_ST;
11234 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11235 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11236
11237 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11238 case FLASH_5717VENDOR_ST_M_M25PE20:
11239 case FLASH_5717VENDOR_ST_A_M25PE20:
11240 case FLASH_5717VENDOR_ST_M_M45PE20:
11241 case FLASH_5717VENDOR_ST_A_M45PE20:
11242 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11243 break;
11244 default:
11245 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11246 break;
11247 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011248 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011249 default:
11250 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11251 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011252 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011253
11254 tg3_nvram_get_pagesize(tp, nvcfg1);
11255 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11256 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011257}
11258
Linus Torvalds1da177e2005-04-16 15:20:36 -070011259/* Chips other than 5700/5701 use the NVRAM for fetching info. */
11260static void __devinit tg3_nvram_init(struct tg3 *tp)
11261{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011262 tw32_f(GRC_EEPROM_ADDR,
11263 (EEPROM_ADDR_FSM_RESET |
11264 (EEPROM_DEFAULT_CLOCK_PERIOD <<
11265 EEPROM_ADDR_CLKPERD_SHIFT)));
11266
Michael Chan9d57f012006-12-07 00:23:25 -080011267 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011268
11269 /* Enable seeprom accesses. */
11270 tw32_f(GRC_LOCAL_CTRL,
11271 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
11272 udelay(100);
11273
11274 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
11275 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
11276 tp->tg3_flags |= TG3_FLAG_NVRAM;
11277
Michael Chanec41c7d2006-01-17 02:40:55 -080011278 if (tg3_nvram_lock(tp)) {
11279 printk(KERN_WARNING PFX "%s: Cannot get nvarm lock, "
11280 "tg3_nvram_init failed.\n", tp->dev->name);
11281 return;
11282 }
Michael Chane6af3012005-04-21 17:12:05 -070011283 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011284
Matt Carlson989a9d22007-05-05 11:51:05 -070011285 tp->nvram_size = 0;
11286
Michael Chan361b4ac2005-04-21 17:11:21 -070011287 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11288 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080011289 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
11290 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070011291 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011292 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
11293 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080011294 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011295 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
11296 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070011297 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11298 tg3_get_5906_nvram_info(tp);
Matt Carlson321d32a2008-11-21 17:22:19 -080011299 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
11300 tg3_get_57780_nvram_info(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000011301 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
11302 tg3_get_5717_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070011303 else
11304 tg3_get_nvram_info(tp);
11305
Matt Carlson989a9d22007-05-05 11:51:05 -070011306 if (tp->nvram_size == 0)
11307 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011308
Michael Chane6af3012005-04-21 17:12:05 -070011309 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080011310 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011311
11312 } else {
11313 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
11314
11315 tg3_get_eeprom_size(tp);
11316 }
11317}
11318
Linus Torvalds1da177e2005-04-16 15:20:36 -070011319static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
11320 u32 offset, u32 len, u8 *buf)
11321{
11322 int i, j, rc = 0;
11323 u32 val;
11324
11325 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011326 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011327 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011328
11329 addr = offset + i;
11330
11331 memcpy(&data, buf + i, 4);
11332
Matt Carlson62cedd12009-04-20 14:52:29 -070011333 /*
11334 * The SEEPROM interface expects the data to always be opposite
11335 * the native endian format. We accomplish this by reversing
11336 * all the operations that would have been performed on the
11337 * data from a call to tg3_nvram_read_be32().
11338 */
11339 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011340
11341 val = tr32(GRC_EEPROM_ADDR);
11342 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
11343
11344 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
11345 EEPROM_ADDR_READ);
11346 tw32(GRC_EEPROM_ADDR, val |
11347 (0 << EEPROM_ADDR_DEVID_SHIFT) |
11348 (addr & EEPROM_ADDR_ADDR_MASK) |
11349 EEPROM_ADDR_START |
11350 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011351
Michael Chan9d57f012006-12-07 00:23:25 -080011352 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011353 val = tr32(GRC_EEPROM_ADDR);
11354
11355 if (val & EEPROM_ADDR_COMPLETE)
11356 break;
Michael Chan9d57f012006-12-07 00:23:25 -080011357 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011358 }
11359 if (!(val & EEPROM_ADDR_COMPLETE)) {
11360 rc = -EBUSY;
11361 break;
11362 }
11363 }
11364
11365 return rc;
11366}
11367
11368/* offset and length are dword aligned */
11369static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
11370 u8 *buf)
11371{
11372 int ret = 0;
11373 u32 pagesize = tp->nvram_pagesize;
11374 u32 pagemask = pagesize - 1;
11375 u32 nvram_cmd;
11376 u8 *tmp;
11377
11378 tmp = kmalloc(pagesize, GFP_KERNEL);
11379 if (tmp == NULL)
11380 return -ENOMEM;
11381
11382 while (len) {
11383 int j;
Michael Chane6af3012005-04-21 17:12:05 -070011384 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011385
11386 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011387
Linus Torvalds1da177e2005-04-16 15:20:36 -070011388 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011389 ret = tg3_nvram_read_be32(tp, phy_addr + j,
11390 (__be32 *) (tmp + j));
11391 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011392 break;
11393 }
11394 if (ret)
11395 break;
11396
11397 page_off = offset & pagemask;
11398 size = pagesize;
11399 if (len < size)
11400 size = len;
11401
11402 len -= size;
11403
11404 memcpy(tmp + page_off, buf, size);
11405
11406 offset = offset + (pagesize - page_off);
11407
Michael Chane6af3012005-04-21 17:12:05 -070011408 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011409
11410 /*
11411 * Before we can erase the flash page, we need
11412 * to issue a special "write enable" command.
11413 */
11414 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11415
11416 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11417 break;
11418
11419 /* Erase the target page */
11420 tw32(NVRAM_ADDR, phy_addr);
11421
11422 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
11423 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
11424
11425 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11426 break;
11427
11428 /* Issue another write enable to start the write. */
11429 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11430
11431 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11432 break;
11433
11434 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011435 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011436
Al Virob9fc7dc2007-12-17 22:59:57 -080011437 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000011438
Al Virob9fc7dc2007-12-17 22:59:57 -080011439 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011440
11441 tw32(NVRAM_ADDR, phy_addr + j);
11442
11443 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
11444 NVRAM_CMD_WR;
11445
11446 if (j == 0)
11447 nvram_cmd |= NVRAM_CMD_FIRST;
11448 else if (j == (pagesize - 4))
11449 nvram_cmd |= NVRAM_CMD_LAST;
11450
11451 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11452 break;
11453 }
11454 if (ret)
11455 break;
11456 }
11457
11458 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11459 tg3_nvram_exec_cmd(tp, nvram_cmd);
11460
11461 kfree(tmp);
11462
11463 return ret;
11464}
11465
11466/* offset and length are dword aligned */
11467static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
11468 u8 *buf)
11469{
11470 int i, ret = 0;
11471
11472 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011473 u32 page_off, phy_addr, nvram_cmd;
11474 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011475
11476 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080011477 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011478
11479 page_off = offset % tp->nvram_pagesize;
11480
Michael Chan18201802006-03-20 22:29:15 -080011481 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011482
11483 tw32(NVRAM_ADDR, phy_addr);
11484
11485 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
11486
11487 if ((page_off == 0) || (i == 0))
11488 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070011489 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011490 nvram_cmd |= NVRAM_CMD_LAST;
11491
11492 if (i == (len - 4))
11493 nvram_cmd |= NVRAM_CMD_LAST;
11494
Matt Carlson321d32a2008-11-21 17:22:19 -080011495 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
11496 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070011497 (tp->nvram_jedecnum == JEDEC_ST) &&
11498 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011499
11500 if ((ret = tg3_nvram_exec_cmd(tp,
11501 NVRAM_CMD_WREN | NVRAM_CMD_GO |
11502 NVRAM_CMD_DONE)))
11503
11504 break;
11505 }
11506 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11507 /* We always do complete word writes to eeprom. */
11508 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
11509 }
11510
11511 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11512 break;
11513 }
11514 return ret;
11515}
11516
11517/* offset and length are dword aligned */
11518static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
11519{
11520 int ret;
11521
Linus Torvalds1da177e2005-04-16 15:20:36 -070011522 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011523 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
11524 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011525 udelay(40);
11526 }
11527
11528 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
11529 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
11530 }
11531 else {
11532 u32 grc_mode;
11533
Michael Chanec41c7d2006-01-17 02:40:55 -080011534 ret = tg3_nvram_lock(tp);
11535 if (ret)
11536 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011537
Michael Chane6af3012005-04-21 17:12:05 -070011538 tg3_enable_nvram_access(tp);
11539 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
11540 !(tp->tg3_flags2 & TG3_FLG2_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011541 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011542
11543 grc_mode = tr32(GRC_MODE);
11544 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
11545
11546 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
11547 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11548
11549 ret = tg3_nvram_write_block_buffered(tp, offset, len,
11550 buf);
11551 }
11552 else {
11553 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
11554 buf);
11555 }
11556
11557 grc_mode = tr32(GRC_MODE);
11558 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
11559
Michael Chane6af3012005-04-21 17:12:05 -070011560 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011561 tg3_nvram_unlock(tp);
11562 }
11563
11564 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011565 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011566 udelay(40);
11567 }
11568
11569 return ret;
11570}
11571
11572struct subsys_tbl_ent {
11573 u16 subsys_vendor, subsys_devid;
11574 u32 phy_id;
11575};
11576
11577static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
11578 /* Broadcom boards. */
11579 { PCI_VENDOR_ID_BROADCOM, 0x1644, PHY_ID_BCM5401 }, /* BCM95700A6 */
11580 { PCI_VENDOR_ID_BROADCOM, 0x0001, PHY_ID_BCM5701 }, /* BCM95701A5 */
11581 { PCI_VENDOR_ID_BROADCOM, 0x0002, PHY_ID_BCM8002 }, /* BCM95700T6 */
11582 { PCI_VENDOR_ID_BROADCOM, 0x0003, 0 }, /* BCM95700A9 */
11583 { PCI_VENDOR_ID_BROADCOM, 0x0005, PHY_ID_BCM5701 }, /* BCM95701T1 */
11584 { PCI_VENDOR_ID_BROADCOM, 0x0006, PHY_ID_BCM5701 }, /* BCM95701T8 */
11585 { PCI_VENDOR_ID_BROADCOM, 0x0007, 0 }, /* BCM95701A7 */
11586 { PCI_VENDOR_ID_BROADCOM, 0x0008, PHY_ID_BCM5701 }, /* BCM95701A10 */
11587 { PCI_VENDOR_ID_BROADCOM, 0x8008, PHY_ID_BCM5701 }, /* BCM95701A12 */
11588 { PCI_VENDOR_ID_BROADCOM, 0x0009, PHY_ID_BCM5703 }, /* BCM95703Ax1 */
11589 { PCI_VENDOR_ID_BROADCOM, 0x8009, PHY_ID_BCM5703 }, /* BCM95703Ax2 */
11590
11591 /* 3com boards. */
11592 { PCI_VENDOR_ID_3COM, 0x1000, PHY_ID_BCM5401 }, /* 3C996T */
11593 { PCI_VENDOR_ID_3COM, 0x1006, PHY_ID_BCM5701 }, /* 3C996BT */
11594 { PCI_VENDOR_ID_3COM, 0x1004, 0 }, /* 3C996SX */
11595 { PCI_VENDOR_ID_3COM, 0x1007, PHY_ID_BCM5701 }, /* 3C1000T */
11596 { PCI_VENDOR_ID_3COM, 0x1008, PHY_ID_BCM5701 }, /* 3C940BR01 */
11597
11598 /* DELL boards. */
11599 { PCI_VENDOR_ID_DELL, 0x00d1, PHY_ID_BCM5401 }, /* VIPER */
11600 { PCI_VENDOR_ID_DELL, 0x0106, PHY_ID_BCM5401 }, /* JAGUAR */
11601 { PCI_VENDOR_ID_DELL, 0x0109, PHY_ID_BCM5411 }, /* MERLOT */
11602 { PCI_VENDOR_ID_DELL, 0x010a, PHY_ID_BCM5411 }, /* SLIM_MERLOT */
11603
11604 /* Compaq boards. */
11605 { PCI_VENDOR_ID_COMPAQ, 0x007c, PHY_ID_BCM5701 }, /* BANSHEE */
11606 { PCI_VENDOR_ID_COMPAQ, 0x009a, PHY_ID_BCM5701 }, /* BANSHEE_2 */
11607 { PCI_VENDOR_ID_COMPAQ, 0x007d, 0 }, /* CHANGELING */
11608 { PCI_VENDOR_ID_COMPAQ, 0x0085, PHY_ID_BCM5701 }, /* NC7780 */
11609 { PCI_VENDOR_ID_COMPAQ, 0x0099, PHY_ID_BCM5701 }, /* NC7780_2 */
11610
11611 /* IBM boards. */
11612 { PCI_VENDOR_ID_IBM, 0x0281, 0 } /* IBM??? */
11613};
11614
11615static inline struct subsys_tbl_ent *lookup_by_subsys(struct tg3 *tp)
11616{
11617 int i;
11618
11619 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
11620 if ((subsys_id_to_phy_id[i].subsys_vendor ==
11621 tp->pdev->subsystem_vendor) &&
11622 (subsys_id_to_phy_id[i].subsys_devid ==
11623 tp->pdev->subsystem_device))
11624 return &subsys_id_to_phy_id[i];
11625 }
11626 return NULL;
11627}
11628
Michael Chan7d0c41e2005-04-21 17:06:20 -070011629static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011630{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011631 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080011632 u16 pmcsr;
11633
11634 /* On some early chips the SRAM cannot be accessed in D3hot state,
11635 * so need make sure we're in D0.
11636 */
11637 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
11638 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
11639 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
11640 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070011641
11642 /* Make sure register accesses (indirect or otherwise)
11643 * will function correctly.
11644 */
11645 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
11646 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011647
David S. Millerf49639e2006-06-09 11:58:36 -070011648 /* The memory arbiter has to be enabled in order for SRAM accesses
11649 * to succeed. Normally on powerup the tg3 chip firmware will make
11650 * sure it is enabled, but other entities such as system netboot
11651 * code might disable it.
11652 */
11653 val = tr32(MEMARB_MODE);
11654 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
11655
Linus Torvalds1da177e2005-04-16 15:20:36 -070011656 tp->phy_id = PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070011657 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
11658
Gary Zambranoa85feb82007-05-05 11:52:19 -070011659 /* Assume an onboard device and WOL capable by default. */
11660 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080011661
Michael Chanb5d37722006-09-27 16:06:21 -070011662 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080011663 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070011664 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080011665 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
11666 }
Matt Carlson0527ba32007-10-10 18:03:30 -070011667 val = tr32(VCPU_CFGSHDW);
11668 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070011669 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070011670 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080011671 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070011672 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080011673 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070011674 }
11675
Linus Torvalds1da177e2005-04-16 15:20:36 -070011676 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
11677 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
11678 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070011679 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070011680 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011681
11682 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
11683 tp->nic_sram_data_cfg = nic_cfg;
11684
11685 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
11686 ver >>= NIC_SRAM_DATA_VER_SHIFT;
11687 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
11688 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
11689 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
11690 (ver > 0) && (ver < 0x100))
11691 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
11692
Matt Carlsona9daf362008-05-25 23:49:44 -070011693 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11694 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
11695
Linus Torvalds1da177e2005-04-16 15:20:36 -070011696 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
11697 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
11698 eeprom_phy_serdes = 1;
11699
11700 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
11701 if (nic_phy_id != 0) {
11702 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
11703 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
11704
11705 eeprom_phy_id = (id1 >> 16) << 10;
11706 eeprom_phy_id |= (id2 & 0xfc00) << 16;
11707 eeprom_phy_id |= (id2 & 0x03ff) << 0;
11708 } else
11709 eeprom_phy_id = 0;
11710
Michael Chan7d0c41e2005-04-21 17:06:20 -070011711 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070011712 if (eeprom_phy_serdes) {
Michael Chana4e2b342005-10-26 15:46:52 -070011713 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan747e8f82005-07-25 12:33:22 -070011714 tp->tg3_flags2 |= TG3_FLG2_MII_SERDES;
11715 else
11716 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
11717 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070011718
John W. Linvillecbf46852005-04-21 17:01:29 -070011719 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011720 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
11721 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070011722 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070011723 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
11724
11725 switch (led_cfg) {
11726 default:
11727 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
11728 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
11729 break;
11730
11731 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
11732 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
11733 break;
11734
11735 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
11736 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070011737
11738 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
11739 * read on some older 5700/5701 bootcode.
11740 */
11741 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
11742 ASIC_REV_5700 ||
11743 GET_ASIC_REV(tp->pci_chip_rev_id) ==
11744 ASIC_REV_5701)
11745 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
11746
Linus Torvalds1da177e2005-04-16 15:20:36 -070011747 break;
11748
11749 case SHASTA_EXT_LED_SHARED:
11750 tp->led_ctrl = LED_CTRL_MODE_SHARED;
11751 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
11752 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
11753 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
11754 LED_CTRL_MODE_PHY_2);
11755 break;
11756
11757 case SHASTA_EXT_LED_MAC:
11758 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
11759 break;
11760
11761 case SHASTA_EXT_LED_COMBO:
11762 tp->led_ctrl = LED_CTRL_MODE_COMBO;
11763 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
11764 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
11765 LED_CTRL_MODE_PHY_2);
11766 break;
11767
Stephen Hemminger855e1112008-04-16 16:37:28 -070011768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011769
11770 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
11771 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
11772 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
11773 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
11774
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011775 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
11776 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080011777
Michael Chan9d26e212006-12-07 00:21:14 -080011778 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011779 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080011780 if ((tp->pdev->subsystem_vendor ==
11781 PCI_VENDOR_ID_ARIMA) &&
11782 (tp->pdev->subsystem_device == 0x205a ||
11783 tp->pdev->subsystem_device == 0x2063))
11784 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
11785 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070011786 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080011787 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
11788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011789
11790 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
11791 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070011792 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011793 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
11794 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080011795
11796 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
11797 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070011798 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080011799
Gary Zambranoa85feb82007-05-05 11:52:19 -070011800 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES &&
11801 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
11802 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011803
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070011804 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080011805 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070011806 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
11807
Linus Torvalds1da177e2005-04-16 15:20:36 -070011808 if (cfg2 & (1 << 17))
11809 tp->tg3_flags2 |= TG3_FLG2_CAPACITIVE_COUPLING;
11810
11811 /* serdes signal pre-emphasis in register 0x590 set by */
11812 /* bootcode if bit 18 is set */
11813 if (cfg2 & (1 << 18))
11814 tp->tg3_flags2 |= TG3_FLG2_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070011815
Matt Carlson321d32a2008-11-21 17:22:19 -080011816 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
11817 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080011818 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
11819 tp->tg3_flags3 |= TG3_FLG3_PHY_ENABLE_APD;
11820
Matt Carlson8ed5d972007-05-07 00:25:49 -070011821 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
11822 u32 cfg3;
11823
11824 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
11825 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
11826 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
11827 }
Matt Carlsona9daf362008-05-25 23:49:44 -070011828
11829 if (cfg4 & NIC_SRAM_RGMII_STD_IBND_DISABLE)
11830 tp->tg3_flags3 |= TG3_FLG3_RGMII_STD_IBND_DISABLE;
11831 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
11832 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
11833 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
11834 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011835 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080011836done:
11837 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
11838 device_set_wakeup_enable(&tp->pdev->dev,
11839 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Michael Chan7d0c41e2005-04-21 17:06:20 -070011840}
11841
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011842static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
11843{
11844 int i;
11845 u32 val;
11846
11847 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
11848 tw32(OTP_CTRL, cmd);
11849
11850 /* Wait for up to 1 ms for command to execute. */
11851 for (i = 0; i < 100; i++) {
11852 val = tr32(OTP_STATUS);
11853 if (val & OTP_STATUS_CMD_DONE)
11854 break;
11855 udelay(10);
11856 }
11857
11858 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
11859}
11860
11861/* Read the gphy configuration from the OTP region of the chip. The gphy
11862 * configuration is a 32-bit value that straddles the alignment boundary.
11863 * We do two 32-bit reads and then shift and merge the results.
11864 */
11865static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
11866{
11867 u32 bhalf_otp, thalf_otp;
11868
11869 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
11870
11871 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
11872 return 0;
11873
11874 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
11875
11876 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
11877 return 0;
11878
11879 thalf_otp = tr32(OTP_READ_DATA);
11880
11881 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
11882
11883 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
11884 return 0;
11885
11886 bhalf_otp = tr32(OTP_READ_DATA);
11887
11888 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
11889}
11890
Michael Chan7d0c41e2005-04-21 17:06:20 -070011891static int __devinit tg3_phy_probe(struct tg3 *tp)
11892{
11893 u32 hw_phy_id_1, hw_phy_id_2;
11894 u32 hw_phy_id, hw_phy_id_masked;
11895 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011896
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011897 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
11898 return tg3_phy_init(tp);
11899
Linus Torvalds1da177e2005-04-16 15:20:36 -070011900 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010011901 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011902 */
11903 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070011904 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
11905 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011906 hw_phy_id = hw_phy_id_masked = PHY_ID_INVALID;
11907 } else {
11908 /* Now read the physical PHY_ID from the chip and verify
11909 * that it is sane. If it doesn't look good, we fall back
11910 * to either the hard-coded table based PHY_ID and failing
11911 * that the value found in the eeprom area.
11912 */
11913 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
11914 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
11915
11916 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
11917 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
11918 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
11919
11920 hw_phy_id_masked = hw_phy_id & PHY_ID_MASK;
11921 }
11922
11923 if (!err && KNOWN_PHY_ID(hw_phy_id_masked)) {
11924 tp->phy_id = hw_phy_id;
11925 if (hw_phy_id_masked == PHY_ID_BCM8002)
11926 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070011927 else
11928 tp->tg3_flags2 &= ~TG3_FLG2_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011929 } else {
Michael Chan7d0c41e2005-04-21 17:06:20 -070011930 if (tp->phy_id != PHY_ID_INVALID) {
11931 /* Do nothing, phy ID already set up in
11932 * tg3_get_eeprom_hw_cfg().
11933 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011934 } else {
11935 struct subsys_tbl_ent *p;
11936
11937 /* No eeprom signature? Try the hardcoded
11938 * subsys device table.
11939 */
11940 p = lookup_by_subsys(tp);
11941 if (!p)
11942 return -ENODEV;
11943
11944 tp->phy_id = p->phy_id;
11945 if (!tp->phy_id ||
11946 tp->phy_id == PHY_ID_BCM8002)
11947 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
11948 }
11949 }
11950
Michael Chan747e8f82005-07-25 12:33:22 -070011951 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070011952 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011953 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080011954 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011955
11956 tg3_readphy(tp, MII_BMSR, &bmsr);
11957 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
11958 (bmsr & BMSR_LSTATUS))
11959 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011960
Linus Torvalds1da177e2005-04-16 15:20:36 -070011961 err = tg3_phy_reset(tp);
11962 if (err)
11963 return err;
11964
11965 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
11966 ADVERTISE_100HALF | ADVERTISE_100FULL |
11967 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
11968 tg3_ctrl = 0;
11969 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
11970 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
11971 MII_TG3_CTRL_ADV_1000_FULL);
11972 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
11973 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
11974 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
11975 MII_TG3_CTRL_ENABLE_AS_MASTER);
11976 }
11977
Michael Chan3600d912006-12-07 00:21:48 -080011978 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
11979 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
11980 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
11981 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011982 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
11983
11984 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
11985 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
11986
11987 tg3_writephy(tp, MII_BMCR,
11988 BMCR_ANENABLE | BMCR_ANRESTART);
11989 }
11990 tg3_phy_set_wirespeed(tp);
11991
11992 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
11993 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
11994 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
11995 }
11996
11997skip_phy_reset:
11998 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
11999 err = tg3_init_5401phy_dsp(tp);
12000 if (err)
12001 return err;
12002 }
12003
12004 if (!err && ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)) {
12005 err = tg3_init_5401phy_dsp(tp);
12006 }
12007
Michael Chan747e8f82005-07-25 12:33:22 -070012008 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012009 tp->link_config.advertising =
12010 (ADVERTISED_1000baseT_Half |
12011 ADVERTISED_1000baseT_Full |
12012 ADVERTISED_Autoneg |
12013 ADVERTISED_FIBRE);
12014 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
12015 tp->link_config.advertising &=
12016 ~(ADVERTISED_1000baseT_Half |
12017 ADVERTISED_1000baseT_Full);
12018
12019 return err;
12020}
12021
12022static void __devinit tg3_read_partno(struct tg3 *tp)
12023{
Matt Carlson6d348f22009-02-25 14:25:52 +000012024 unsigned char vpd_data[256]; /* in little-endian format */
Michael Chanaf2c6a42006-11-07 14:57:51 -080012025 unsigned int i;
Michael Chan1b277772006-03-20 22:27:48 -080012026 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012027
Matt Carlsondf259d82009-04-20 06:57:14 +000012028 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12029 tg3_nvram_read(tp, 0x0, &magic))
David S. Millerf49639e2006-06-09 11:58:36 -070012030 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012031
Michael Chan18201802006-03-20 22:29:15 -080012032 if (magic == TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080012033 for (i = 0; i < 256; i += 4) {
12034 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012035
Matt Carlson6d348f22009-02-25 14:25:52 +000012036 /* The data is in little-endian format in NVRAM.
12037 * Use the big-endian read routines to preserve
12038 * the byte order as it exists in NVRAM.
12039 */
12040 if (tg3_nvram_read_be32(tp, 0x100 + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012041 goto out_not_found;
12042
Matt Carlson6d348f22009-02-25 14:25:52 +000012043 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012044 }
12045 } else {
12046 int vpd_cap;
12047
12048 vpd_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_VPD);
12049 for (i = 0; i < 256; i += 4) {
12050 u32 tmp, j = 0;
Al Virob9fc7dc2007-12-17 22:59:57 -080012051 __le32 v;
Michael Chan1b277772006-03-20 22:27:48 -080012052 u16 tmp16;
12053
12054 pci_write_config_word(tp->pdev, vpd_cap + PCI_VPD_ADDR,
12055 i);
12056 while (j++ < 100) {
12057 pci_read_config_word(tp->pdev, vpd_cap +
12058 PCI_VPD_ADDR, &tmp16);
12059 if (tmp16 & 0x8000)
12060 break;
12061 msleep(1);
12062 }
David S. Millerf49639e2006-06-09 11:58:36 -070012063 if (!(tmp16 & 0x8000))
12064 goto out_not_found;
12065
Michael Chan1b277772006-03-20 22:27:48 -080012066 pci_read_config_dword(tp->pdev, vpd_cap + PCI_VPD_DATA,
12067 &tmp);
Al Virob9fc7dc2007-12-17 22:59:57 -080012068 v = cpu_to_le32(tmp);
Matt Carlson6d348f22009-02-25 14:25:52 +000012069 memcpy(&vpd_data[i], &v, sizeof(v));
Michael Chan1b277772006-03-20 22:27:48 -080012070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012071 }
12072
12073 /* Now parse and find the part number. */
Michael Chanaf2c6a42006-11-07 14:57:51 -080012074 for (i = 0; i < 254; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012075 unsigned char val = vpd_data[i];
Michael Chanaf2c6a42006-11-07 14:57:51 -080012076 unsigned int block_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012077
12078 if (val == 0x82 || val == 0x91) {
12079 i = (i + 3 +
12080 (vpd_data[i + 1] +
12081 (vpd_data[i + 2] << 8)));
12082 continue;
12083 }
12084
12085 if (val != 0x90)
12086 goto out_not_found;
12087
12088 block_end = (i + 3 +
12089 (vpd_data[i + 1] +
12090 (vpd_data[i + 2] << 8)));
12091 i += 3;
Michael Chanaf2c6a42006-11-07 14:57:51 -080012092
12093 if (block_end > 256)
12094 goto out_not_found;
12095
12096 while (i < (block_end - 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012097 if (vpd_data[i + 0] == 'P' &&
12098 vpd_data[i + 1] == 'N') {
12099 int partno_len = vpd_data[i + 2];
12100
Michael Chanaf2c6a42006-11-07 14:57:51 -080012101 i += 3;
12102 if (partno_len > 24 || (partno_len + i) > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012103 goto out_not_found;
12104
12105 memcpy(tp->board_part_number,
Michael Chanaf2c6a42006-11-07 14:57:51 -080012106 &vpd_data[i], partno_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012107
12108 /* Success. */
12109 return;
12110 }
Michael Chanaf2c6a42006-11-07 14:57:51 -080012111 i += 3 + vpd_data[i + 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070012112 }
12113
12114 /* Part number not found. */
12115 goto out_not_found;
12116 }
12117
12118out_not_found:
Michael Chanb5d37722006-09-27 16:06:21 -070012119 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12120 strcpy(tp->board_part_number, "BCM95906");
Matt Carlsondf259d82009-04-20 06:57:14 +000012121 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12122 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12123 strcpy(tp->board_part_number, "BCM57780");
12124 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12125 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12126 strcpy(tp->board_part_number, "BCM57760");
12127 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12128 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12129 strcpy(tp->board_part_number, "BCM57790");
Matt Carlson5e7ccf22009-08-25 10:08:42 +000012130 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12131 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12132 strcpy(tp->board_part_number, "BCM57788");
Michael Chanb5d37722006-09-27 16:06:21 -070012133 else
12134 strcpy(tp->board_part_number, "none");
Linus Torvalds1da177e2005-04-16 15:20:36 -070012135}
12136
Matt Carlson9c8a6202007-10-21 16:16:08 -070012137static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12138{
12139 u32 val;
12140
Matt Carlsone4f34112009-02-25 14:25:00 +000012141 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012142 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012143 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012144 val != 0)
12145 return 0;
12146
12147 return 1;
12148}
12149
Matt Carlsonacd9c112009-02-25 14:26:33 +000012150static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12151{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012152 u32 val, offset, start, ver_offset;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012153 int i;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012154 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012155
12156 if (tg3_nvram_read(tp, 0xc, &offset) ||
12157 tg3_nvram_read(tp, 0x4, &start))
12158 return;
12159
12160 offset = tg3_nvram_logical_addr(tp, offset);
12161
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012162 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012163 return;
12164
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012165 if ((val & 0xfc000000) == 0x0c000000) {
12166 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012167 return;
12168
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012169 if (val == 0)
12170 newver = true;
12171 }
12172
12173 if (newver) {
12174 if (tg3_nvram_read(tp, offset + 8, &ver_offset))
12175 return;
12176
12177 offset = offset + ver_offset - start;
12178 for (i = 0; i < 16; i += 4) {
12179 __be32 v;
12180 if (tg3_nvram_read_be32(tp, offset + i, &v))
12181 return;
12182
12183 memcpy(tp->fw_ver + i, &v, sizeof(v));
12184 }
12185 } else {
12186 u32 major, minor;
12187
12188 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
12189 return;
12190
12191 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
12192 TG3_NVM_BCVER_MAJSFT;
12193 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
12194 snprintf(&tp->fw_ver[0], 32, "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012195 }
12196}
12197
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012198static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
12199{
12200 u32 val, major, minor;
12201
12202 /* Use native endian representation */
12203 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
12204 return;
12205
12206 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
12207 TG3_NVM_HWSB_CFG1_MAJSFT;
12208 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
12209 TG3_NVM_HWSB_CFG1_MINSFT;
12210
12211 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
12212}
12213
Matt Carlsondfe00d72008-11-21 17:19:41 -080012214static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
12215{
12216 u32 offset, major, minor, build;
12217
12218 tp->fw_ver[0] = 's';
12219 tp->fw_ver[1] = 'b';
12220 tp->fw_ver[2] = '\0';
12221
12222 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
12223 return;
12224
12225 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
12226 case TG3_EEPROM_SB_REVISION_0:
12227 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
12228 break;
12229 case TG3_EEPROM_SB_REVISION_2:
12230 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
12231 break;
12232 case TG3_EEPROM_SB_REVISION_3:
12233 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
12234 break;
12235 default:
12236 return;
12237 }
12238
Matt Carlsone4f34112009-02-25 14:25:00 +000012239 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080012240 return;
12241
12242 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
12243 TG3_EEPROM_SB_EDH_BLD_SHFT;
12244 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
12245 TG3_EEPROM_SB_EDH_MAJ_SHFT;
12246 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
12247
12248 if (minor > 99 || build > 26)
12249 return;
12250
12251 snprintf(&tp->fw_ver[2], 30, " v%d.%02d", major, minor);
12252
12253 if (build > 0) {
12254 tp->fw_ver[8] = 'a' + build - 1;
12255 tp->fw_ver[9] = '\0';
12256 }
12257}
12258
Matt Carlsonacd9c112009-02-25 14:26:33 +000012259static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080012260{
12261 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012262 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070012263
12264 for (offset = TG3_NVM_DIR_START;
12265 offset < TG3_NVM_DIR_END;
12266 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012267 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012268 return;
12269
12270 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
12271 break;
12272 }
12273
12274 if (offset == TG3_NVM_DIR_END)
12275 return;
12276
12277 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12278 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000012279 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012280 return;
12281
Matt Carlsone4f34112009-02-25 14:25:00 +000012282 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012283 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012284 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012285 return;
12286
12287 offset += val - start;
12288
Matt Carlsonacd9c112009-02-25 14:26:33 +000012289 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012290
Matt Carlsonacd9c112009-02-25 14:26:33 +000012291 tp->fw_ver[vlen++] = ',';
12292 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070012293
12294 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012295 __be32 v;
12296 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012297 return;
12298
Al Virob9fc7dc2007-12-17 22:59:57 -080012299 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012300
Matt Carlsonacd9c112009-02-25 14:26:33 +000012301 if (vlen > TG3_VER_SIZE - sizeof(v)) {
12302 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012303 break;
12304 }
12305
Matt Carlsonacd9c112009-02-25 14:26:33 +000012306 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
12307 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012308 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000012309}
12310
Matt Carlson7fd76442009-02-25 14:27:20 +000012311static void __devinit tg3_read_dash_ver(struct tg3 *tp)
12312{
12313 int vlen;
12314 u32 apedata;
12315
12316 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
12317 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
12318 return;
12319
12320 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
12321 if (apedata != APE_SEG_SIG_MAGIC)
12322 return;
12323
12324 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
12325 if (!(apedata & APE_FW_STATUS_READY))
12326 return;
12327
12328 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
12329
12330 vlen = strlen(tp->fw_ver);
12331
12332 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " DASH v%d.%d.%d.%d",
12333 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
12334 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
12335 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
12336 (apedata & APE_FW_VERSION_BLDMSK));
12337}
12338
Matt Carlsonacd9c112009-02-25 14:26:33 +000012339static void __devinit tg3_read_fw_ver(struct tg3 *tp)
12340{
12341 u32 val;
12342
Matt Carlsondf259d82009-04-20 06:57:14 +000012343 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
12344 tp->fw_ver[0] = 's';
12345 tp->fw_ver[1] = 'b';
12346 tp->fw_ver[2] = '\0';
12347
12348 return;
12349 }
12350
Matt Carlsonacd9c112009-02-25 14:26:33 +000012351 if (tg3_nvram_read(tp, 0, &val))
12352 return;
12353
12354 if (val == TG3_EEPROM_MAGIC)
12355 tg3_read_bc_ver(tp);
12356 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
12357 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012358 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12359 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012360 else
12361 return;
12362
12363 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12364 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
12365 return;
12366
12367 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012368
12369 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080012370}
12371
Michael Chan7544b092007-05-05 13:08:32 -070012372static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
12373
Linus Torvalds1da177e2005-04-16 15:20:36 -070012374static int __devinit tg3_get_invariants(struct tg3 *tp)
12375{
12376 static struct pci_device_id write_reorder_chipsets[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012377 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
12378 PCI_DEVICE_ID_AMD_FE_GATE_700C) },
John W. Linvillec165b002006-07-08 13:28:53 -070012379 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
12380 PCI_DEVICE_ID_AMD_8131_BRIDGE) },
Michael Chan399de502005-10-03 14:02:39 -070012381 { PCI_DEVICE(PCI_VENDOR_ID_VIA,
12382 PCI_DEVICE_ID_VIA_8385_0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012383 { },
12384 };
12385 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012386 u32 pci_state_reg, grc_misc_cfg;
12387 u32 val;
12388 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012389 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012390
Linus Torvalds1da177e2005-04-16 15:20:36 -070012391 /* Force memory write invalidate off. If we leave it on,
12392 * then on 5700_BX chips we have to enable a workaround.
12393 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
12394 * to match the cacheline size. The Broadcom driver have this
12395 * workaround but turns MWI off all the times so never uses
12396 * it. This seems to suggest that the workaround is insufficient.
12397 */
12398 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12399 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
12400 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12401
12402 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
12403 * has the register indirect write enable bit set before
12404 * we try to access any of the MMIO registers. It is also
12405 * critical that the PCI-X hw workaround situation is decided
12406 * before that as well.
12407 */
12408 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12409 &misc_ctrl_reg);
12410
12411 tp->pci_chip_rev_id = (misc_ctrl_reg >>
12412 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070012413 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
12414 u32 prod_id_asic_rev;
12415
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012416 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717C ||
12417 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717S ||
12418 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718C ||
12419 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718S)
12420 pci_read_config_dword(tp->pdev,
12421 TG3PCI_GEN2_PRODID_ASICREV,
12422 &prod_id_asic_rev);
12423 else
12424 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
12425 &prod_id_asic_rev);
12426
Matt Carlson321d32a2008-11-21 17:22:19 -080012427 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070012428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012429
Michael Chanff645be2005-04-21 17:09:53 -070012430 /* Wrong chip ID in 5752 A0. This code can be removed later
12431 * as A0 is not in production.
12432 */
12433 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
12434 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
12435
Michael Chan68929142005-08-09 20:17:14 -070012436 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
12437 * we need to disable memory and use config. cycles
12438 * only to access all registers. The 5702/03 chips
12439 * can mistakenly decode the special cycles from the
12440 * ICH chipsets as memory write cycles, causing corruption
12441 * of register and memory space. Only certain ICH bridges
12442 * will drive special cycles with non-zero data during the
12443 * address phase which can fall within the 5703's address
12444 * range. This is not an ICH bug as the PCI spec allows
12445 * non-zero address during special cycles. However, only
12446 * these ICH bridges are known to drive non-zero addresses
12447 * during special cycles.
12448 *
12449 * Since special cycles do not cross PCI bridges, we only
12450 * enable this workaround if the 5703 is on the secondary
12451 * bus of these ICH bridges.
12452 */
12453 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
12454 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
12455 static struct tg3_dev_id {
12456 u32 vendor;
12457 u32 device;
12458 u32 rev;
12459 } ich_chipsets[] = {
12460 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
12461 PCI_ANY_ID },
12462 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
12463 PCI_ANY_ID },
12464 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
12465 0xa },
12466 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
12467 PCI_ANY_ID },
12468 { },
12469 };
12470 struct tg3_dev_id *pci_id = &ich_chipsets[0];
12471 struct pci_dev *bridge = NULL;
12472
12473 while (pci_id->vendor != 0) {
12474 bridge = pci_get_device(pci_id->vendor, pci_id->device,
12475 bridge);
12476 if (!bridge) {
12477 pci_id++;
12478 continue;
12479 }
12480 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070012481 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070012482 continue;
12483 }
12484 if (bridge->subordinate &&
12485 (bridge->subordinate->number ==
12486 tp->pdev->bus->number)) {
12487
12488 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
12489 pci_dev_put(bridge);
12490 break;
12491 }
12492 }
12493 }
12494
Matt Carlson41588ba2008-04-19 18:12:33 -070012495 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
12496 static struct tg3_dev_id {
12497 u32 vendor;
12498 u32 device;
12499 } bridge_chipsets[] = {
12500 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
12501 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
12502 { },
12503 };
12504 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
12505 struct pci_dev *bridge = NULL;
12506
12507 while (pci_id->vendor != 0) {
12508 bridge = pci_get_device(pci_id->vendor,
12509 pci_id->device,
12510 bridge);
12511 if (!bridge) {
12512 pci_id++;
12513 continue;
12514 }
12515 if (bridge->subordinate &&
12516 (bridge->subordinate->number <=
12517 tp->pdev->bus->number) &&
12518 (bridge->subordinate->subordinate >=
12519 tp->pdev->bus->number)) {
12520 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
12521 pci_dev_put(bridge);
12522 break;
12523 }
12524 }
12525 }
12526
Michael Chan4a29cc22006-03-19 13:21:12 -080012527 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
12528 * DMA addresses > 40-bit. This bridge may have other additional
12529 * 57xx devices behind it in some 4-port NIC designs for example.
12530 * Any tg3 device found behind the bridge will also need the 40-bit
12531 * DMA workaround.
12532 */
Michael Chana4e2b342005-10-26 15:46:52 -070012533 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
12534 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
12535 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080012536 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070012537 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Michael Chana4e2b342005-10-26 15:46:52 -070012538 }
Michael Chan4a29cc22006-03-19 13:21:12 -080012539 else {
12540 struct pci_dev *bridge = NULL;
12541
12542 do {
12543 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
12544 PCI_DEVICE_ID_SERVERWORKS_EPB,
12545 bridge);
12546 if (bridge && bridge->subordinate &&
12547 (bridge->subordinate->number <=
12548 tp->pdev->bus->number) &&
12549 (bridge->subordinate->subordinate >=
12550 tp->pdev->bus->number)) {
12551 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
12552 pci_dev_put(bridge);
12553 break;
12554 }
12555 } while (bridge);
12556 }
Michael Chan4cf78e42005-07-25 12:29:19 -070012557
Linus Torvalds1da177e2005-04-16 15:20:36 -070012558 /* Initialize misc host control in PCI block. */
12559 tp->misc_host_ctrl |= (misc_ctrl_reg &
12560 MISC_HOST_CTRL_CHIPREV);
12561 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12562 tp->misc_host_ctrl);
12563
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012564 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
12565 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
12566 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan7544b092007-05-05 13:08:32 -070012567 tp->pdev_peer = tg3_find_peer(tp);
12568
Matt Carlson321d32a2008-11-21 17:22:19 -080012569 /* Intentionally exclude ASIC_REV_5906 */
12570 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad12006-03-20 22:27:35 -080012571 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070012572 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070012573 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012574 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012575 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12576 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson321d32a2008-11-21 17:22:19 -080012577 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
12578
12579 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
12580 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070012581 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012582 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070012583 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070012584 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
12585
John W. Linville1b440c562005-04-21 17:03:18 -070012586 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
12587 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
12588 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
12589
Matt Carlson027455a2008-12-21 20:19:30 -080012590 /* 5700 B0 chips do not support checksumming correctly due
12591 * to hardware bugs.
12592 */
12593 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
12594 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
12595 else {
12596 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
12597 tp->dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
12598 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
12599 tp->dev->features |= NETIF_F_IPV6_CSUM;
12600 }
12601
Michael Chan5a6f3072006-03-20 22:28:05 -080012602 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070012603 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
12604 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
12605 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
12606 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
12607 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
12608 tp->pdev_peer == tp->pdev))
12609 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
12610
Matt Carlson321d32a2008-11-21 17:22:19 -080012611 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070012612 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan5a6f3072006-03-20 22:28:05 -080012613 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
Michael Chanfcfa0a32006-03-20 22:28:41 -080012614 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070012615 } else {
Michael Chan7f62ad52007-02-20 23:25:40 -080012616 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
Michael Chan52c0fd82006-06-29 20:15:54 -070012617 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12618 ASIC_REV_5750 &&
12619 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Michael Chan7f62ad52007-02-20 23:25:40 -080012620 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
Michael Chan52c0fd82006-06-29 20:15:54 -070012621 }
Michael Chan5a6f3072006-03-20 22:28:05 -080012622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012623
Matt Carlson4f125f42009-09-01 12:55:02 +000012624 tp->irq_max = 1;
12625
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012626#ifdef TG3_NAPI
12627 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
12628 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
12629 tp->irq_max = TG3_IRQ_MAX_VECS;
12630 }
12631#endif
12632
Matt Carlsonf51f3562008-05-25 23:45:08 -070012633 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012634 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
12635 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson8f666b02009-08-28 13:58:24 +000012636 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070012637
Matt Carlson52f44902008-11-21 17:17:04 -080012638 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
12639 &pci_state_reg);
12640
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012641 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
12642 if (tp->pcie_cap != 0) {
12643 u16 lnkctl;
12644
Linus Torvalds1da177e2005-04-16 15:20:36 -070012645 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080012646
12647 pcie_set_readrq(tp->pdev, 4096);
12648
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012649 pci_read_config_word(tp->pdev,
12650 tp->pcie_cap + PCI_EXP_LNKCTL,
12651 &lnkctl);
12652 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
12653 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080012654 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012655 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012656 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000012657 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
12658 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012659 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Michael Chanc7835a72006-11-15 21:14:42 -080012660 }
Matt Carlson52f44902008-11-21 17:17:04 -080012661 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080012662 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080012663 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
12664 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
12665 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
12666 if (!tp->pcix_cap) {
12667 printk(KERN_ERR PFX "Cannot find PCI-X "
12668 "capability, aborting.\n");
12669 return -EIO;
12670 }
12671
12672 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
12673 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
12674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012675
Michael Chan399de502005-10-03 14:02:39 -070012676 /* If we have an AMD 762 or VIA K8T800 chipset, write
12677 * reordering to the mailbox registers done by the host
12678 * controller can cause major troubles. We read back from
12679 * every mailbox register write to force the writes to be
12680 * posted to the chip in order.
12681 */
12682 if (pci_dev_present(write_reorder_chipsets) &&
12683 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
12684 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
12685
Matt Carlson69fc4052008-12-21 20:19:57 -080012686 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
12687 &tp->pci_cacheline_sz);
12688 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
12689 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012690 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
12691 tp->pci_lat_timer < 64) {
12692 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080012693 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
12694 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012695 }
12696
Matt Carlson52f44902008-11-21 17:17:04 -080012697 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
12698 /* 5700 BX chips need to have their TX producer index
12699 * mailboxes written twice to workaround a bug.
12700 */
12701 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070012702
Matt Carlson52f44902008-11-21 17:17:04 -080012703 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012704 *
12705 * The workaround is to use indirect register accesses
12706 * for all chip writes not to mailbox registers.
12707 */
Matt Carlson52f44902008-11-21 17:17:04 -080012708 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012709 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012710
12711 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
12712
12713 /* The chip can have it's power management PCI config
12714 * space registers clobbered due to this bug.
12715 * So explicitly force the chip into D0 here.
12716 */
Matt Carlson9974a352007-10-07 23:27:28 -070012717 pci_read_config_dword(tp->pdev,
12718 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012719 &pm_reg);
12720 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
12721 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070012722 pci_write_config_dword(tp->pdev,
12723 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012724 pm_reg);
12725
12726 /* Also, force SERR#/PERR# in PCI command. */
12727 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12728 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
12729 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12730 }
12731 }
12732
Linus Torvalds1da177e2005-04-16 15:20:36 -070012733 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
12734 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
12735 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
12736 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
12737
12738 /* Chip-specific fixup from Broadcom driver */
12739 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
12740 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
12741 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
12742 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
12743 }
12744
Michael Chan1ee582d2005-08-09 20:16:46 -070012745 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070012746 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070012747 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070012748 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070012749 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070012750 tp->write32_tx_mbox = tg3_write32;
12751 tp->write32_rx_mbox = tg3_write32;
12752
12753 /* Various workaround register access methods */
12754 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
12755 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070012756 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
12757 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
12758 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
12759 /*
12760 * Back to back register writes can cause problems on these
12761 * chips, the workaround is to read back all reg writes
12762 * except those to mailbox regs.
12763 *
12764 * See tg3_write_indirect_reg32().
12765 */
Michael Chan1ee582d2005-08-09 20:16:46 -070012766 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070012767 }
12768
Michael Chan1ee582d2005-08-09 20:16:46 -070012769 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
12770 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
12771 tp->write32_tx_mbox = tg3_write32_tx_mbox;
12772 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
12773 tp->write32_rx_mbox = tg3_write_flush_reg32;
12774 }
Michael Chan20094932005-08-09 20:16:32 -070012775
Michael Chan68929142005-08-09 20:17:14 -070012776 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
12777 tp->read32 = tg3_read_indirect_reg32;
12778 tp->write32 = tg3_write_indirect_reg32;
12779 tp->read32_mbox = tg3_read_indirect_mbox;
12780 tp->write32_mbox = tg3_write_indirect_mbox;
12781 tp->write32_tx_mbox = tg3_write_indirect_mbox;
12782 tp->write32_rx_mbox = tg3_write_indirect_mbox;
12783
12784 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070012785 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070012786
12787 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12788 pci_cmd &= ~PCI_COMMAND_MEMORY;
12789 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12790 }
Michael Chanb5d37722006-09-27 16:06:21 -070012791 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
12792 tp->read32_mbox = tg3_read32_mbox_5906;
12793 tp->write32_mbox = tg3_write32_mbox_5906;
12794 tp->write32_tx_mbox = tg3_write32_mbox_5906;
12795 tp->write32_rx_mbox = tg3_write32_mbox_5906;
12796 }
Michael Chan68929142005-08-09 20:17:14 -070012797
Michael Chanbbadf502006-04-06 21:46:34 -070012798 if (tp->write32 == tg3_write_indirect_reg32 ||
12799 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
12800 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070012801 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070012802 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
12803
Michael Chan7d0c41e2005-04-21 17:06:20 -070012804 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080012805 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070012806 * determined before calling tg3_set_power_state() so that
12807 * we know whether or not to switch out of Vaux power.
12808 * When the flag is set, it means that GPIO1 is used for eeprom
12809 * write protect and also implies that it is a LOM where GPIOs
12810 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012811 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070012812 tg3_get_eeprom_hw_cfg(tp);
12813
Matt Carlson0d3031d2007-10-10 18:02:43 -070012814 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
12815 /* Allow reads and writes to the
12816 * APE register and memory space.
12817 */
12818 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
12819 PCISTATE_ALLOW_APE_SHMEM_WR;
12820 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
12821 pci_state_reg);
12822 }
12823
Matt Carlson9936bcf2007-10-10 18:03:07 -070012824 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012825 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012826 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012827 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12828 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlsond30cdd22007-10-07 23:28:35 -070012829 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
12830
Michael Chan314fba32005-04-21 17:07:04 -070012831 /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
12832 * GPIO1 driven high will bring 5700's external PHY out of reset.
12833 * It is also used as eeprom write protect on LOMs.
12834 */
12835 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
12836 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
12837 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
12838 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
12839 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070012840 /* Unused GPIO3 must be driven as output on 5752 because there
12841 * are no pull-up resistors on unused GPIO pins.
12842 */
12843 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12844 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070012845
Matt Carlson321d32a2008-11-21 17:22:19 -080012846 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
12847 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Michael Chanaf36e6b2006-03-23 01:28:06 -080012848 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
12849
Matt Carlson8d519ab2009-04-20 06:58:01 +000012850 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
12851 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070012852 /* Turn off the debug UART. */
12853 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
12854 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
12855 /* Keep VMain power. */
12856 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
12857 GRC_LCLCTRL_GPIO_OUTPUT0;
12858 }
12859
Linus Torvalds1da177e2005-04-16 15:20:36 -070012860 /* Force the chip into D0. */
Michael Chanbc1c7562006-03-20 17:48:03 -080012861 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012862 if (err) {
12863 printk(KERN_ERR PFX "(%s) transition to D0 failed\n",
12864 pci_name(tp->pdev));
12865 return err;
12866 }
12867
Linus Torvalds1da177e2005-04-16 15:20:36 -070012868 /* Derive initial jumbo mode from MTU assigned in
12869 * ether_setup() via the alloc_etherdev() call
12870 */
Michael Chan0f893dc2005-07-25 12:30:38 -070012871 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070012872 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070012873 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012874
12875 /* Determine WakeOnLan speed to use. */
12876 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12877 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12878 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
12879 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
12880 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
12881 } else {
12882 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
12883 }
12884
Matt Carlson7f97a4b2009-08-25 10:10:03 +000012885 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12886 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
12887
Linus Torvalds1da177e2005-04-16 15:20:36 -070012888 /* A few boards don't want Ethernet@WireSpeed phy feature */
12889 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
12890 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
12891 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070012892 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000012893 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) ||
Michael Chan747e8f82005-07-25 12:33:22 -070012894 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012895 tp->tg3_flags2 |= TG3_FLG2_NO_ETH_WIRE_SPEED;
12896
12897 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
12898 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
12899 tp->tg3_flags2 |= TG3_FLG2_PHY_ADC_BUG;
12900 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
12901 tp->tg3_flags2 |= TG3_FLG2_PHY_5704_A0_BUG;
12902
Matt Carlson321d32a2008-11-21 17:22:19 -080012903 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +000012904 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080012905 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012906 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
12907 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
Michael Chanc424cb22006-04-29 18:56:34 -070012908 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070012909 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070012910 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12911 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080012912 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
12913 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
12914 tp->tg3_flags2 |= TG3_FLG2_PHY_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080012915 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
12916 tp->tg3_flags2 |= TG3_FLG2_PHY_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080012917 } else
Michael Chanc424cb22006-04-29 18:56:34 -070012918 tp->tg3_flags2 |= TG3_FLG2_PHY_BER_BUG;
12919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012920
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012921 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12922 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
12923 tp->phy_otp = tg3_read_otp_phycfg(tp);
12924 if (tp->phy_otp == 0)
12925 tp->phy_otp = TG3_OTP_DEFAULT;
12926 }
12927
Matt Carlsonf51f3562008-05-25 23:45:08 -070012928 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070012929 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
12930 else
12931 tp->mi_mode = MAC_MI_MODE_BASE;
12932
Linus Torvalds1da177e2005-04-16 15:20:36 -070012933 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012934 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
12935 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
12936 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
12937
Matt Carlson321d32a2008-11-21 17:22:19 -080012938 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
12939 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070012940 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
12941
Matt Carlson255ca312009-08-25 10:07:27 +000012942 if ((tp->pci_chip_rev_id == CHIPREV_ID_57780_A1 &&
12943 tr32(RCVLPC_STATS_ENABLE) & RCVLPC_STATSENAB_ASF_FIX) ||
12944 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0)
12945 tp->tg3_flags3 |= TG3_FLG3_TOGGLE_10_100_L1PLLPD;
12946
Matt Carlson158d7ab2008-05-29 01:37:54 -070012947 err = tg3_mdio_init(tp);
12948 if (err)
12949 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012950
12951 /* Initialize data/descriptor byte/word swapping. */
12952 val = tr32(GRC_MODE);
12953 val &= GRC_MODE_HOST_STACKUP;
12954 tw32(GRC_MODE, val | tp->grc_mode);
12955
12956 tg3_switch_clocks(tp);
12957
12958 /* Clear this out for sanity. */
12959 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
12960
12961 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
12962 &pci_state_reg);
12963 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
12964 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
12965 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
12966
12967 if (chiprevid == CHIPREV_ID_5701_A0 ||
12968 chiprevid == CHIPREV_ID_5701_B0 ||
12969 chiprevid == CHIPREV_ID_5701_B2 ||
12970 chiprevid == CHIPREV_ID_5701_B5) {
12971 void __iomem *sram_base;
12972
12973 /* Write some dummy words into the SRAM status block
12974 * area, see if it reads back correctly. If the return
12975 * value is bad, force enable the PCIX workaround.
12976 */
12977 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
12978
12979 writel(0x00000000, sram_base);
12980 writel(0x00000000, sram_base + 4);
12981 writel(0xffffffff, sram_base + 4);
12982 if (readl(sram_base) != 0x00000000)
12983 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
12984 }
12985 }
12986
12987 udelay(50);
12988 tg3_nvram_init(tp);
12989
12990 grc_misc_cfg = tr32(GRC_MISC_CFG);
12991 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
12992
Linus Torvalds1da177e2005-04-16 15:20:36 -070012993 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
12994 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
12995 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
12996 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
12997
David S. Millerfac9b832005-05-18 22:46:34 -070012998 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
12999 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13000 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13001 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13002 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13003 HOSTCC_MODE_CLRTICK_TXBD);
13004
13005 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13006 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13007 tp->misc_host_ctrl);
13008 }
13009
Matt Carlson3bda1252008-08-15 14:08:22 -070013010 /* Preserve the APE MAC_MODE bits */
13011 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
13012 tp->mac_mode = tr32(MAC_MODE) |
13013 MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
13014 else
13015 tp->mac_mode = TG3_DEF_MAC_MODE;
13016
Linus Torvalds1da177e2005-04-16 15:20:36 -070013017 /* these are limited to 10/100 only */
13018 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13019 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13020 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13021 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13022 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13023 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13024 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13025 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13026 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013027 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13028 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013029 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013030 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013031 tp->tg3_flags |= TG3_FLAG_10_100_ONLY;
13032
13033 err = tg3_phy_probe(tp);
13034 if (err) {
13035 printk(KERN_ERR PFX "(%s) phy probe failed, err %d\n",
13036 pci_name(tp->pdev), err);
13037 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013038 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013039 }
13040
13041 tg3_read_partno(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013042 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013043
13044 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
13045 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13046 } else {
13047 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13048 tp->tg3_flags |= TG3_FLAG_USE_MI_INTERRUPT;
13049 else
13050 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13051 }
13052
13053 /* 5700 {AX,BX} chips have a broken status block link
13054 * change bit implementation, so we must use the
13055 * status register in those cases.
13056 */
13057 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13058 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13059 else
13060 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13061
13062 /* The led_ctrl is set during tg3_phy_probe, here we might
13063 * have to force the link status polling mechanism based
13064 * upon subsystem IDs.
13065 */
13066 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070013067 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013068 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
13069 tp->tg3_flags |= (TG3_FLAG_USE_MI_INTERRUPT |
13070 TG3_FLAG_USE_LINKCHG_REG);
13071 }
13072
13073 /* For all SERDES we poll the MAC status register. */
13074 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
13075 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
13076 else
13077 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
13078
Matt Carlsonad829262008-11-21 17:16:16 -080013079 tp->rx_offset = NET_IP_ALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013080 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
13081 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0)
13082 tp->rx_offset = 0;
13083
Michael Chanf92905d2006-06-29 20:14:29 -070013084 tp->rx_std_max_post = TG3_RX_RING_SIZE;
13085
13086 /* Increment the rx prod index on the rx std ring by at most
13087 * 8 for these chips to workaround hw errata.
13088 */
13089 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13090 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13091 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13092 tp->rx_std_max_post = 8;
13093
Matt Carlson8ed5d972007-05-07 00:25:49 -070013094 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
13095 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
13096 PCIE_PWR_MGMT_L1_THRESH_MSK;
13097
Linus Torvalds1da177e2005-04-16 15:20:36 -070013098 return err;
13099}
13100
David S. Miller49b6e95f2007-03-29 01:38:42 -070013101#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013102static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
13103{
13104 struct net_device *dev = tp->dev;
13105 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013106 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070013107 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013108 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013109
David S. Miller49b6e95f2007-03-29 01:38:42 -070013110 addr = of_get_property(dp, "local-mac-address", &len);
13111 if (addr && len == 6) {
13112 memcpy(dev->dev_addr, addr, 6);
13113 memcpy(dev->perm_addr, dev->dev_addr, 6);
13114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013115 }
13116 return -ENODEV;
13117}
13118
13119static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
13120{
13121 struct net_device *dev = tp->dev;
13122
13123 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070013124 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013125 return 0;
13126}
13127#endif
13128
13129static int __devinit tg3_get_device_address(struct tg3 *tp)
13130{
13131 struct net_device *dev = tp->dev;
13132 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080013133 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013134
David S. Miller49b6e95f2007-03-29 01:38:42 -070013135#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013136 if (!tg3_get_macaddr_sparc(tp))
13137 return 0;
13138#endif
13139
13140 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070013141 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013142 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013143 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
13144 mac_offset = 0xcc;
13145 if (tg3_nvram_lock(tp))
13146 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
13147 else
13148 tg3_nvram_unlock(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013149 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13150 if (tr32(TG3_CPMU_STATUS) & TG3_CPMU_STATUS_PCIE_FUNC)
13151 mac_offset = 0xcc;
13152 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070013153 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013154
13155 /* First try to get it from MAC address mailbox. */
13156 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
13157 if ((hi >> 16) == 0x484b) {
13158 dev->dev_addr[0] = (hi >> 8) & 0xff;
13159 dev->dev_addr[1] = (hi >> 0) & 0xff;
13160
13161 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
13162 dev->dev_addr[2] = (lo >> 24) & 0xff;
13163 dev->dev_addr[3] = (lo >> 16) & 0xff;
13164 dev->dev_addr[4] = (lo >> 8) & 0xff;
13165 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013166
Michael Chan008652b2006-03-27 23:14:53 -080013167 /* Some old bootcode may report a 0 MAC address in SRAM */
13168 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
13169 }
13170 if (!addr_ok) {
13171 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000013172 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
13173 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000013174 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070013175 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
13176 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080013177 }
13178 /* Finally just fetch it out of the MAC control regs. */
13179 else {
13180 hi = tr32(MAC_ADDR_0_HIGH);
13181 lo = tr32(MAC_ADDR_0_LOW);
13182
13183 dev->dev_addr[5] = lo & 0xff;
13184 dev->dev_addr[4] = (lo >> 8) & 0xff;
13185 dev->dev_addr[3] = (lo >> 16) & 0xff;
13186 dev->dev_addr[2] = (lo >> 24) & 0xff;
13187 dev->dev_addr[1] = hi & 0xff;
13188 dev->dev_addr[0] = (hi >> 8) & 0xff;
13189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013190 }
13191
13192 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070013193#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013194 if (!tg3_get_default_macaddr_sparc(tp))
13195 return 0;
13196#endif
13197 return -EINVAL;
13198 }
John W. Linville2ff43692005-09-12 14:44:20 -070013199 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013200 return 0;
13201}
13202
David S. Miller59e6b432005-05-18 22:50:10 -070013203#define BOUNDARY_SINGLE_CACHELINE 1
13204#define BOUNDARY_MULTI_CACHELINE 2
13205
13206static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
13207{
13208 int cacheline_size;
13209 u8 byte;
13210 int goal;
13211
13212 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
13213 if (byte == 0)
13214 cacheline_size = 1024;
13215 else
13216 cacheline_size = (int) byte * 4;
13217
13218 /* On 5703 and later chips, the boundary bits have no
13219 * effect.
13220 */
13221 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13222 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13223 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13224 goto out;
13225
13226#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
13227 goal = BOUNDARY_MULTI_CACHELINE;
13228#else
13229#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
13230 goal = BOUNDARY_SINGLE_CACHELINE;
13231#else
13232 goal = 0;
13233#endif
13234#endif
13235
13236 if (!goal)
13237 goto out;
13238
13239 /* PCI controllers on most RISC systems tend to disconnect
13240 * when a device tries to burst across a cache-line boundary.
13241 * Therefore, letting tg3 do so just wastes PCI bandwidth.
13242 *
13243 * Unfortunately, for PCI-E there are only limited
13244 * write-side controls for this, and thus for reads
13245 * we will still get the disconnects. We'll also waste
13246 * these PCI cycles for both read and write for chips
13247 * other than 5700 and 5701 which do not implement the
13248 * boundary bits.
13249 */
13250 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13251 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
13252 switch (cacheline_size) {
13253 case 16:
13254 case 32:
13255 case 64:
13256 case 128:
13257 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13258 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
13259 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
13260 } else {
13261 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13262 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13263 }
13264 break;
13265
13266 case 256:
13267 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
13268 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
13269 break;
13270
13271 default:
13272 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13273 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13274 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013275 }
David S. Miller59e6b432005-05-18 22:50:10 -070013276 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13277 switch (cacheline_size) {
13278 case 16:
13279 case 32:
13280 case 64:
13281 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13282 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13283 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
13284 break;
13285 }
13286 /* fallthrough */
13287 case 128:
13288 default:
13289 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13290 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
13291 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013292 }
David S. Miller59e6b432005-05-18 22:50:10 -070013293 } else {
13294 switch (cacheline_size) {
13295 case 16:
13296 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13297 val |= (DMA_RWCTRL_READ_BNDRY_16 |
13298 DMA_RWCTRL_WRITE_BNDRY_16);
13299 break;
13300 }
13301 /* fallthrough */
13302 case 32:
13303 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13304 val |= (DMA_RWCTRL_READ_BNDRY_32 |
13305 DMA_RWCTRL_WRITE_BNDRY_32);
13306 break;
13307 }
13308 /* fallthrough */
13309 case 64:
13310 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13311 val |= (DMA_RWCTRL_READ_BNDRY_64 |
13312 DMA_RWCTRL_WRITE_BNDRY_64);
13313 break;
13314 }
13315 /* fallthrough */
13316 case 128:
13317 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13318 val |= (DMA_RWCTRL_READ_BNDRY_128 |
13319 DMA_RWCTRL_WRITE_BNDRY_128);
13320 break;
13321 }
13322 /* fallthrough */
13323 case 256:
13324 val |= (DMA_RWCTRL_READ_BNDRY_256 |
13325 DMA_RWCTRL_WRITE_BNDRY_256);
13326 break;
13327 case 512:
13328 val |= (DMA_RWCTRL_READ_BNDRY_512 |
13329 DMA_RWCTRL_WRITE_BNDRY_512);
13330 break;
13331 case 1024:
13332 default:
13333 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
13334 DMA_RWCTRL_WRITE_BNDRY_1024);
13335 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013336 }
David S. Miller59e6b432005-05-18 22:50:10 -070013337 }
13338
13339out:
13340 return val;
13341}
13342
Linus Torvalds1da177e2005-04-16 15:20:36 -070013343static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
13344{
13345 struct tg3_internal_buffer_desc test_desc;
13346 u32 sram_dma_descs;
13347 int i, ret;
13348
13349 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
13350
13351 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
13352 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
13353 tw32(RDMAC_STATUS, 0);
13354 tw32(WDMAC_STATUS, 0);
13355
13356 tw32(BUFMGR_MODE, 0);
13357 tw32(FTQ_RESET, 0);
13358
13359 test_desc.addr_hi = ((u64) buf_dma) >> 32;
13360 test_desc.addr_lo = buf_dma & 0xffffffff;
13361 test_desc.nic_mbuf = 0x00002100;
13362 test_desc.len = size;
13363
13364 /*
13365 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
13366 * the *second* time the tg3 driver was getting loaded after an
13367 * initial scan.
13368 *
13369 * Broadcom tells me:
13370 * ...the DMA engine is connected to the GRC block and a DMA
13371 * reset may affect the GRC block in some unpredictable way...
13372 * The behavior of resets to individual blocks has not been tested.
13373 *
13374 * Broadcom noted the GRC reset will also reset all sub-components.
13375 */
13376 if (to_device) {
13377 test_desc.cqid_sqid = (13 << 8) | 2;
13378
13379 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
13380 udelay(40);
13381 } else {
13382 test_desc.cqid_sqid = (16 << 8) | 7;
13383
13384 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
13385 udelay(40);
13386 }
13387 test_desc.flags = 0x00000005;
13388
13389 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
13390 u32 val;
13391
13392 val = *(((u32 *)&test_desc) + i);
13393 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
13394 sram_dma_descs + (i * sizeof(u32)));
13395 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
13396 }
13397 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
13398
13399 if (to_device) {
13400 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
13401 } else {
13402 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
13403 }
13404
13405 ret = -ENODEV;
13406 for (i = 0; i < 40; i++) {
13407 u32 val;
13408
13409 if (to_device)
13410 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
13411 else
13412 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
13413 if ((val & 0xffff) == sram_dma_descs) {
13414 ret = 0;
13415 break;
13416 }
13417
13418 udelay(100);
13419 }
13420
13421 return ret;
13422}
13423
David S. Millerded73402005-05-23 13:59:47 -070013424#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070013425
13426static int __devinit tg3_test_dma(struct tg3 *tp)
13427{
13428 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070013429 u32 *buf, saved_dma_rwctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013430 int ret;
13431
13432 buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
13433 if (!buf) {
13434 ret = -ENOMEM;
13435 goto out_nofree;
13436 }
13437
13438 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
13439 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
13440
David S. Miller59e6b432005-05-18 22:50:10 -070013441 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013442
13443 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13444 /* DMA read watermark not used on PCIE */
13445 tp->dma_rwctrl |= 0x00180000;
13446 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070013447 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
13448 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013449 tp->dma_rwctrl |= 0x003f0000;
13450 else
13451 tp->dma_rwctrl |= 0x003f000f;
13452 } else {
13453 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13454 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
13455 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080013456 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013457
Michael Chan4a29cc22006-03-19 13:21:12 -080013458 /* If the 5704 is behind the EPB bridge, we can
13459 * do the less restrictive ONE_DMA workaround for
13460 * better performance.
13461 */
13462 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
13463 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13464 tp->dma_rwctrl |= 0x8000;
13465 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013466 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
13467
Michael Chan49afdeb2007-02-13 12:17:03 -080013468 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
13469 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070013470 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080013471 tp->dma_rwctrl |=
13472 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
13473 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
13474 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070013475 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
13476 /* 5780 always in PCIX mode */
13477 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070013478 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13479 /* 5714 always in PCIX mode */
13480 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013481 } else {
13482 tp->dma_rwctrl |= 0x001b000f;
13483 }
13484 }
13485
13486 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13487 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13488 tp->dma_rwctrl &= 0xfffffff0;
13489
13490 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13491 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
13492 /* Remove this if it causes problems for some boards. */
13493 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
13494
13495 /* On 5700/5701 chips, we need to set this bit.
13496 * Otherwise the chip will issue cacheline transactions
13497 * to streamable DMA memory with not all the byte
13498 * enables turned on. This is an error on several
13499 * RISC PCI controllers, in particular sparc64.
13500 *
13501 * On 5703/5704 chips, this bit has been reassigned
13502 * a different meaning. In particular, it is used
13503 * on those chips to enable a PCI-X workaround.
13504 */
13505 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
13506 }
13507
13508 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13509
13510#if 0
13511 /* Unneeded, already done by tg3_get_invariants. */
13512 tg3_switch_clocks(tp);
13513#endif
13514
13515 ret = 0;
13516 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13517 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
13518 goto out;
13519
David S. Miller59e6b432005-05-18 22:50:10 -070013520 /* It is best to perform DMA test with maximum write burst size
13521 * to expose the 5700/5701 write DMA bug.
13522 */
13523 saved_dma_rwctrl = tp->dma_rwctrl;
13524 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
13525 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13526
Linus Torvalds1da177e2005-04-16 15:20:36 -070013527 while (1) {
13528 u32 *p = buf, i;
13529
13530 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
13531 p[i] = i;
13532
13533 /* Send the buffer to the chip. */
13534 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
13535 if (ret) {
13536 printk(KERN_ERR "tg3_test_dma() Write the buffer failed %d\n", ret);
13537 break;
13538 }
13539
13540#if 0
13541 /* validate data reached card RAM correctly. */
13542 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
13543 u32 val;
13544 tg3_read_mem(tp, 0x2100 + (i*4), &val);
13545 if (le32_to_cpu(val) != p[i]) {
13546 printk(KERN_ERR " tg3_test_dma() Card buffer corrupted on write! (%d != %d)\n", val, i);
13547 /* ret = -ENODEV here? */
13548 }
13549 p[i] = 0;
13550 }
13551#endif
13552 /* Now read it back. */
13553 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
13554 if (ret) {
13555 printk(KERN_ERR "tg3_test_dma() Read the buffer failed %d\n", ret);
13556
13557 break;
13558 }
13559
13560 /* Verify it. */
13561 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
13562 if (p[i] == i)
13563 continue;
13564
David S. Miller59e6b432005-05-18 22:50:10 -070013565 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
13566 DMA_RWCTRL_WRITE_BNDRY_16) {
13567 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013568 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
13569 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13570 break;
13571 } else {
13572 printk(KERN_ERR "tg3_test_dma() buffer corrupted on read back! (%d != %d)\n", p[i], i);
13573 ret = -ENODEV;
13574 goto out;
13575 }
13576 }
13577
13578 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
13579 /* Success. */
13580 ret = 0;
13581 break;
13582 }
13583 }
David S. Miller59e6b432005-05-18 22:50:10 -070013584 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
13585 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070013586 static struct pci_device_id dma_wait_state_chipsets[] = {
13587 { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
13588 PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
13589 { },
13590 };
13591
David S. Miller59e6b432005-05-18 22:50:10 -070013592 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070013593 * now look for chipsets that are known to expose the
13594 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070013595 */
Michael Chan6d1cfba2005-06-08 14:13:14 -070013596 if (pci_dev_present(dma_wait_state_chipsets)) {
13597 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
13598 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
13599 }
13600 else
13601 /* Safe to use the calculated DMA boundary. */
13602 tp->dma_rwctrl = saved_dma_rwctrl;
13603
David S. Miller59e6b432005-05-18 22:50:10 -070013604 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013606
13607out:
13608 pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
13609out_nofree:
13610 return ret;
13611}
13612
13613static void __devinit tg3_init_link_config(struct tg3 *tp)
13614{
13615 tp->link_config.advertising =
13616 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13617 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13618 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
13619 ADVERTISED_Autoneg | ADVERTISED_MII);
13620 tp->link_config.speed = SPEED_INVALID;
13621 tp->link_config.duplex = DUPLEX_INVALID;
13622 tp->link_config.autoneg = AUTONEG_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013623 tp->link_config.active_speed = SPEED_INVALID;
13624 tp->link_config.active_duplex = DUPLEX_INVALID;
13625 tp->link_config.phy_is_low_power = 0;
13626 tp->link_config.orig_speed = SPEED_INVALID;
13627 tp->link_config.orig_duplex = DUPLEX_INVALID;
13628 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13629}
13630
13631static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
13632{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013633 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS &&
13634 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
Michael Chanfdfec1722005-07-25 12:31:48 -070013635 tp->bufmgr_config.mbuf_read_dma_low_water =
13636 DEFAULT_MB_RDMA_LOW_WATER_5705;
13637 tp->bufmgr_config.mbuf_mac_rx_low_water =
13638 DEFAULT_MB_MACRX_LOW_WATER_5705;
13639 tp->bufmgr_config.mbuf_high_water =
13640 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070013641 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13642 tp->bufmgr_config.mbuf_mac_rx_low_water =
13643 DEFAULT_MB_MACRX_LOW_WATER_5906;
13644 tp->bufmgr_config.mbuf_high_water =
13645 DEFAULT_MB_HIGH_WATER_5906;
13646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013647
Michael Chanfdfec1722005-07-25 12:31:48 -070013648 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
13649 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
13650 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
13651 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
13652 tp->bufmgr_config.mbuf_high_water_jumbo =
13653 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
13654 } else {
13655 tp->bufmgr_config.mbuf_read_dma_low_water =
13656 DEFAULT_MB_RDMA_LOW_WATER;
13657 tp->bufmgr_config.mbuf_mac_rx_low_water =
13658 DEFAULT_MB_MACRX_LOW_WATER;
13659 tp->bufmgr_config.mbuf_high_water =
13660 DEFAULT_MB_HIGH_WATER;
13661
13662 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
13663 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
13664 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
13665 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
13666 tp->bufmgr_config.mbuf_high_water_jumbo =
13667 DEFAULT_MB_HIGH_WATER_JUMBO;
13668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013669
13670 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
13671 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
13672}
13673
13674static char * __devinit tg3_phy_string(struct tg3 *tp)
13675{
13676 switch (tp->phy_id & PHY_ID_MASK) {
13677 case PHY_ID_BCM5400: return "5400";
13678 case PHY_ID_BCM5401: return "5401";
13679 case PHY_ID_BCM5411: return "5411";
13680 case PHY_ID_BCM5701: return "5701";
13681 case PHY_ID_BCM5703: return "5703";
13682 case PHY_ID_BCM5704: return "5704";
13683 case PHY_ID_BCM5705: return "5705";
13684 case PHY_ID_BCM5750: return "5750";
Michael Chan85e94ce2005-04-21 17:05:28 -070013685 case PHY_ID_BCM5752: return "5752";
Michael Chana4e2b342005-10-26 15:46:52 -070013686 case PHY_ID_BCM5714: return "5714";
Michael Chan4cf78e42005-07-25 12:29:19 -070013687 case PHY_ID_BCM5780: return "5780";
Michael Chanaf36e6b2006-03-23 01:28:06 -080013688 case PHY_ID_BCM5755: return "5755";
Michael Chand9ab5ad12006-03-20 22:27:35 -080013689 case PHY_ID_BCM5787: return "5787";
Matt Carlsond30cdd22007-10-07 23:28:35 -070013690 case PHY_ID_BCM5784: return "5784";
Michael Chan126a3362006-09-27 16:03:07 -070013691 case PHY_ID_BCM5756: return "5722/5756";
Michael Chanb5d37722006-09-27 16:06:21 -070013692 case PHY_ID_BCM5906: return "5906";
Matt Carlson9936bcf2007-10-10 18:03:07 -070013693 case PHY_ID_BCM5761: return "5761";
Linus Torvalds1da177e2005-04-16 15:20:36 -070013694 case PHY_ID_BCM8002: return "8002/serdes";
13695 case 0: return "serdes";
13696 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070013697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013698}
13699
Michael Chanf9804dd2005-09-27 12:13:10 -070013700static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
13701{
13702 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13703 strcpy(str, "PCI Express");
13704 return str;
13705 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
13706 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
13707
13708 strcpy(str, "PCIX:");
13709
13710 if ((clock_ctrl == 7) ||
13711 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
13712 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
13713 strcat(str, "133MHz");
13714 else if (clock_ctrl == 0)
13715 strcat(str, "33MHz");
13716 else if (clock_ctrl == 2)
13717 strcat(str, "50MHz");
13718 else if (clock_ctrl == 4)
13719 strcat(str, "66MHz");
13720 else if (clock_ctrl == 6)
13721 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070013722 } else {
13723 strcpy(str, "PCI:");
13724 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
13725 strcat(str, "66MHz");
13726 else
13727 strcat(str, "33MHz");
13728 }
13729 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
13730 strcat(str, ":32-bit");
13731 else
13732 strcat(str, ":64-bit");
13733 return str;
13734}
13735
Michael Chan8c2dc7e2005-12-19 16:26:02 -080013736static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013737{
13738 struct pci_dev *peer;
13739 unsigned int func, devnr = tp->pdev->devfn & ~7;
13740
13741 for (func = 0; func < 8; func++) {
13742 peer = pci_get_slot(tp->pdev->bus, devnr | func);
13743 if (peer && peer != tp->pdev)
13744 break;
13745 pci_dev_put(peer);
13746 }
Michael Chan16fe9d72005-12-13 21:09:54 -080013747 /* 5704 can be configured in single-port mode, set peer to
13748 * tp->pdev in that case.
13749 */
13750 if (!peer) {
13751 peer = tp->pdev;
13752 return peer;
13753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013754
13755 /*
13756 * We don't need to keep the refcount elevated; there's no way
13757 * to remove one half of this device without removing the other
13758 */
13759 pci_dev_put(peer);
13760
13761 return peer;
13762}
13763
David S. Miller15f98502005-05-18 22:49:26 -070013764static void __devinit tg3_init_coal(struct tg3 *tp)
13765{
13766 struct ethtool_coalesce *ec = &tp->coal;
13767
13768 memset(ec, 0, sizeof(*ec));
13769 ec->cmd = ETHTOOL_GCOALESCE;
13770 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
13771 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
13772 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
13773 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
13774 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
13775 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
13776 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
13777 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
13778 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
13779
13780 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
13781 HOSTCC_MODE_CLRTICK_TXBD)) {
13782 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
13783 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
13784 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
13785 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
13786 }
Michael Chand244c892005-07-05 14:42:33 -070013787
13788 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
13789 ec->rx_coalesce_usecs_irq = 0;
13790 ec->tx_coalesce_usecs_irq = 0;
13791 ec->stats_block_coalesce_usecs = 0;
13792 }
David S. Miller15f98502005-05-18 22:49:26 -070013793}
13794
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080013795static const struct net_device_ops tg3_netdev_ops = {
13796 .ndo_open = tg3_open,
13797 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080013798 .ndo_start_xmit = tg3_start_xmit,
13799 .ndo_get_stats = tg3_get_stats,
13800 .ndo_validate_addr = eth_validate_addr,
13801 .ndo_set_multicast_list = tg3_set_rx_mode,
13802 .ndo_set_mac_address = tg3_set_mac_addr,
13803 .ndo_do_ioctl = tg3_ioctl,
13804 .ndo_tx_timeout = tg3_tx_timeout,
13805 .ndo_change_mtu = tg3_change_mtu,
13806#if TG3_VLAN_TAG_USED
13807 .ndo_vlan_rx_register = tg3_vlan_rx_register,
13808#endif
13809#ifdef CONFIG_NET_POLL_CONTROLLER
13810 .ndo_poll_controller = tg3_poll_controller,
13811#endif
13812};
13813
13814static const struct net_device_ops tg3_netdev_ops_dma_bug = {
13815 .ndo_open = tg3_open,
13816 .ndo_stop = tg3_close,
13817 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080013818 .ndo_get_stats = tg3_get_stats,
13819 .ndo_validate_addr = eth_validate_addr,
13820 .ndo_set_multicast_list = tg3_set_rx_mode,
13821 .ndo_set_mac_address = tg3_set_mac_addr,
13822 .ndo_do_ioctl = tg3_ioctl,
13823 .ndo_tx_timeout = tg3_tx_timeout,
13824 .ndo_change_mtu = tg3_change_mtu,
13825#if TG3_VLAN_TAG_USED
13826 .ndo_vlan_rx_register = tg3_vlan_rx_register,
13827#endif
13828#ifdef CONFIG_NET_POLL_CONTROLLER
13829 .ndo_poll_controller = tg3_poll_controller,
13830#endif
13831};
13832
Linus Torvalds1da177e2005-04-16 15:20:36 -070013833static int __devinit tg3_init_one(struct pci_dev *pdev,
13834 const struct pci_device_id *ent)
13835{
13836 static int tg3_version_printed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013837 struct net_device *dev;
13838 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000013839 int i, err, pm_cap;
13840 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070013841 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080013842 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013843
13844 if (tg3_version_printed++ == 0)
13845 printk(KERN_INFO "%s", version);
13846
13847 err = pci_enable_device(pdev);
13848 if (err) {
13849 printk(KERN_ERR PFX "Cannot enable PCI device, "
13850 "aborting.\n");
13851 return err;
13852 }
13853
Linus Torvalds1da177e2005-04-16 15:20:36 -070013854 err = pci_request_regions(pdev, DRV_MODULE_NAME);
13855 if (err) {
13856 printk(KERN_ERR PFX "Cannot obtain PCI resources, "
13857 "aborting.\n");
13858 goto err_out_disable_pdev;
13859 }
13860
13861 pci_set_master(pdev);
13862
13863 /* Find power-management capability. */
13864 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
13865 if (pm_cap == 0) {
13866 printk(KERN_ERR PFX "Cannot find PowerManagement capability, "
13867 "aborting.\n");
13868 err = -EIO;
13869 goto err_out_free_res;
13870 }
13871
Matt Carlsonfe5f5782009-09-01 13:09:39 +000013872 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013873 if (!dev) {
13874 printk(KERN_ERR PFX "Etherdev alloc failed, aborting.\n");
13875 err = -ENOMEM;
13876 goto err_out_free_res;
13877 }
13878
Linus Torvalds1da177e2005-04-16 15:20:36 -070013879 SET_NETDEV_DEV(dev, &pdev->dev);
13880
Linus Torvalds1da177e2005-04-16 15:20:36 -070013881#if TG3_VLAN_TAG_USED
13882 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013883#endif
13884
13885 tp = netdev_priv(dev);
13886 tp->pdev = pdev;
13887 tp->dev = dev;
13888 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013889 tp->rx_mode = TG3_DEF_RX_MODE;
13890 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070013891
Linus Torvalds1da177e2005-04-16 15:20:36 -070013892 if (tg3_debug > 0)
13893 tp->msg_enable = tg3_debug;
13894 else
13895 tp->msg_enable = TG3_DEF_MSG_ENABLE;
13896
13897 /* The word/byte swap controls here control register access byte
13898 * swapping. DMA data byte swapping is controlled in the GRC_MODE
13899 * setting below.
13900 */
13901 tp->misc_host_ctrl =
13902 MISC_HOST_CTRL_MASK_PCI_INT |
13903 MISC_HOST_CTRL_WORD_SWAP |
13904 MISC_HOST_CTRL_INDIR_ACCESS |
13905 MISC_HOST_CTRL_PCISTATE_RW;
13906
13907 /* The NONFRM (non-frame) byte/word swap controls take effect
13908 * on descriptor entries, anything which isn't packet data.
13909 *
13910 * The StrongARM chips on the board (one for tx, one for rx)
13911 * are running in big-endian mode.
13912 */
13913 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
13914 GRC_MODE_WSWAP_NONFRM_DATA);
13915#ifdef __BIG_ENDIAN
13916 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
13917#endif
13918 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013919 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000013920 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013921
Matt Carlsond5fe4882008-11-21 17:20:32 -080013922 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010013923 if (!tp->regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013924 printk(KERN_ERR PFX "Cannot map device registers, "
13925 "aborting.\n");
13926 err = -ENOMEM;
13927 goto err_out_free_dev;
13928 }
13929
13930 tg3_init_link_config(tp);
13931
Linus Torvalds1da177e2005-04-16 15:20:36 -070013932 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
13933 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013934
Matt Carlson646c9ed2009-09-01 12:58:41 +000013935 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
13936 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
13937 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
13938 for (i = 0; i < TG3_IRQ_MAX_VECS; i++) {
13939 struct tg3_napi *tnapi = &tp->napi[i];
13940
13941 tnapi->tp = tp;
13942 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
13943
13944 tnapi->int_mbox = intmbx;
13945 if (i < 4)
13946 intmbx += 0x8;
13947 else
13948 intmbx += 0x4;
13949
13950 tnapi->consmbox = rcvmbx;
13951 tnapi->prodmbox = sndmbx;
13952
13953 if (i)
13954 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
13955 else
13956 tnapi->coal_now = HOSTCC_MODE_NOW;
13957
13958 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
13959 break;
13960
13961 /*
13962 * If we support MSIX, we'll be using RSS. If we're using
13963 * RSS, the first vector only handles link interrupts and the
13964 * remaining vectors handle rx and tx interrupts. Reuse the
13965 * mailbox values for the next iteration. The values we setup
13966 * above are still useful for the single vectored mode.
13967 */
13968 if (!i)
13969 continue;
13970
13971 rcvmbx += 0x8;
13972
13973 if (sndmbx & 0x4)
13974 sndmbx -= 0x4;
13975 else
13976 sndmbx += 0xc;
13977 }
13978
Matt Carlson8ef04422009-08-28 14:01:37 +000013979 netif_napi_add(dev, &tp->napi[0].napi, tg3_poll, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013980 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013981 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013982 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013983
13984 err = tg3_get_invariants(tp);
13985 if (err) {
13986 printk(KERN_ERR PFX "Problem fetching invariants of chip, "
13987 "aborting.\n");
13988 goto err_out_iounmap;
13989 }
13990
Matt Carlson321d32a2008-11-21 17:22:19 -080013991 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Stephen Hemminger00829822008-11-20 20:14:53 -080013992 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13993 dev->netdev_ops = &tg3_netdev_ops;
13994 else
13995 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
13996
13997
Michael Chan4a29cc22006-03-19 13:21:12 -080013998 /* The EPB bridge inside 5714, 5715, and 5780 and any
13999 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014000 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14001 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14002 * do DMA address check in tg3_start_xmit().
14003 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014004 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070014005 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014006 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014007 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014008#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014009 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014010#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014011 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014012 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014013
14014 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014015 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014016 err = pci_set_dma_mask(pdev, dma_mask);
14017 if (!err) {
14018 dev->features |= NETIF_F_HIGHDMA;
14019 err = pci_set_consistent_dma_mask(pdev,
14020 persist_dma_mask);
14021 if (err < 0) {
14022 printk(KERN_ERR PFX "Unable to obtain 64 bit "
14023 "DMA for consistent allocations\n");
14024 goto err_out_iounmap;
14025 }
14026 }
14027 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014028 if (err || dma_mask == DMA_BIT_MASK(32)) {
14029 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014030 if (err) {
14031 printk(KERN_ERR PFX "No usable DMA configuration, "
14032 "aborting.\n");
14033 goto err_out_iounmap;
14034 }
14035 }
14036
Michael Chanfdfec1722005-07-25 12:31:48 -070014037 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014038
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014039 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014040 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014041
Linus Torvalds1da177e2005-04-16 15:20:36 -070014042 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
14043 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14044 }
14045 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14046 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
14047 tp->pci_chip_rev_id == CHIPREV_ID_5705_A0 ||
Michael Chanc7835a72006-11-15 21:14:42 -080014048 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070014049 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
14050 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
14051 } else {
Michael Chan7f62ad52007-02-20 23:25:40 -080014052 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014053 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014054 tp->fw_needed = FIRMWARE_TG3TSO5;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014055 else
Matt Carlson9e9fd122009-01-19 16:57:45 -080014056 tp->fw_needed = FIRMWARE_TG3TSO;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014058
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014059 /* TSO is on by default on chips that support hardware TSO.
14060 * Firmware TSO on older chips gives lower performance, so it
14061 * is off by default, but can be enabled using ethtool.
14062 */
Michael Chanb0026622006-07-03 19:42:14 -070014063 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Matt Carlson027455a2008-12-21 20:19:30 -080014064 if (dev->features & NETIF_F_IP_CSUM)
14065 dev->features |= NETIF_F_TSO;
14066 if ((dev->features & NETIF_F_IPV6_CSUM) &&
14067 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2))
Michael Chanb0026622006-07-03 19:42:14 -070014068 dev->features |= NETIF_F_TSO6;
Matt Carlson57e69832008-05-25 23:48:31 -070014069 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
14070 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14071 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014072 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014073 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14074 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson9936bcf2007-10-10 18:03:07 -070014075 dev->features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070014076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014077
Linus Torvalds1da177e2005-04-16 15:20:36 -070014078
14079 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14080 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14081 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14082 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14083 tp->rx_pending = 63;
14084 }
14085
Linus Torvalds1da177e2005-04-16 15:20:36 -070014086 err = tg3_get_device_address(tp);
14087 if (err) {
14088 printk(KERN_ERR PFX "Could not obtain valid ethernet address, "
14089 "aborting.\n");
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014090 goto err_out_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014091 }
14092
Matt Carlson0d3031d2007-10-10 18:02:43 -070014093 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080014094 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080014095 if (!tp->aperegs) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014096 printk(KERN_ERR PFX "Cannot map APE registers, "
14097 "aborting.\n");
14098 err = -ENOMEM;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014099 goto err_out_fw;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014100 }
14101
14102 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000014103
14104 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14105 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014106 }
14107
Matt Carlsonc88864d2007-11-12 21:07:01 -080014108 /*
14109 * Reset chip in case UNDI or EFI driver did not shutdown
14110 * DMA self test will enable WDMAC and we'll see (spurious)
14111 * pending DMA on the PCI bus at that point.
14112 */
14113 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
14114 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
14115 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
14116 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14117 }
14118
14119 err = tg3_test_dma(tp);
14120 if (err) {
14121 printk(KERN_ERR PFX "DMA engine test failed, aborting.\n");
14122 goto err_out_apeunmap;
14123 }
14124
Matt Carlsonc88864d2007-11-12 21:07:01 -080014125 /* flow control autonegotiation is default behavior */
14126 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
Steve Glendinninge18ce342008-12-16 02:00:00 -080014127 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlsonc88864d2007-11-12 21:07:01 -080014128
14129 tg3_init_coal(tp);
14130
Michael Chanc49a1562006-12-17 17:07:29 -080014131 pci_set_drvdata(pdev, dev);
14132
Linus Torvalds1da177e2005-04-16 15:20:36 -070014133 err = register_netdev(dev);
14134 if (err) {
14135 printk(KERN_ERR PFX "Cannot register net device, "
14136 "aborting.\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014137 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014138 }
14139
Matt Carlsondf59c942008-11-03 16:52:56 -080014140 printk(KERN_INFO "%s: Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070014141 dev->name,
14142 tp->board_part_number,
14143 tp->pci_chip_rev_id,
Michael Chanf9804dd2005-09-27 12:13:10 -070014144 tg3_bus_string(tp, str),
Johannes Berge1749612008-10-27 15:59:26 -070014145 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014146
Matt Carlsondf59c942008-11-03 16:52:56 -080014147 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED)
14148 printk(KERN_INFO
14149 "%s: attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
14150 tp->dev->name,
14151 tp->mdio_bus->phy_map[PHY_ADDR]->drv->name,
Kay Sieversfb28ad32008-11-10 13:55:14 -080014152 dev_name(&tp->mdio_bus->phy_map[PHY_ADDR]->dev));
Matt Carlsondf59c942008-11-03 16:52:56 -080014153 else
14154 printk(KERN_INFO
14155 "%s: attached PHY is %s (%s Ethernet) (WireSpeed[%d])\n",
14156 tp->dev->name, tg3_phy_string(tp),
14157 ((tp->tg3_flags & TG3_FLAG_10_100_ONLY) ? "10/100Base-TX" :
14158 ((tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) ? "1000Base-SX" :
14159 "10/100/1000Base-T")),
14160 (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED) == 0);
14161
14162 printk(KERN_INFO "%s: RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070014163 dev->name,
14164 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
14165 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
14166 (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) != 0,
14167 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014168 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
Michael Chan4a29cc22006-03-19 13:21:12 -080014169 printk(KERN_INFO "%s: dma_rwctrl[%08x] dma_mask[%d-bit]\n",
14170 dev->name, tp->dma_rwctrl,
Yang Hongyang284901a2009-04-06 19:01:15 -070014171 (pdev->dma_mask == DMA_BIT_MASK(32)) ? 32 :
Yang Hongyang50cf1562009-04-06 19:01:14 -070014172 (((u64) pdev->dma_mask == DMA_BIT_MASK(40)) ? 40 : 64));
Linus Torvalds1da177e2005-04-16 15:20:36 -070014173
14174 return 0;
14175
Matt Carlson0d3031d2007-10-10 18:02:43 -070014176err_out_apeunmap:
14177 if (tp->aperegs) {
14178 iounmap(tp->aperegs);
14179 tp->aperegs = NULL;
14180 }
14181
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014182err_out_fw:
14183 if (tp->fw)
14184 release_firmware(tp->fw);
14185
Linus Torvalds1da177e2005-04-16 15:20:36 -070014186err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070014187 if (tp->regs) {
14188 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014189 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014191
14192err_out_free_dev:
14193 free_netdev(dev);
14194
14195err_out_free_res:
14196 pci_release_regions(pdev);
14197
14198err_out_disable_pdev:
14199 pci_disable_device(pdev);
14200 pci_set_drvdata(pdev, NULL);
14201 return err;
14202}
14203
14204static void __devexit tg3_remove_one(struct pci_dev *pdev)
14205{
14206 struct net_device *dev = pci_get_drvdata(pdev);
14207
14208 if (dev) {
14209 struct tg3 *tp = netdev_priv(dev);
14210
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014211 if (tp->fw)
14212 release_firmware(tp->fw);
14213
Michael Chan7faa0062006-02-02 17:29:28 -080014214 flush_scheduled_work();
Matt Carlson158d7ab2008-05-29 01:37:54 -070014215
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014216 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
14217 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070014218 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014219 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070014220
Linus Torvalds1da177e2005-04-16 15:20:36 -070014221 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014222 if (tp->aperegs) {
14223 iounmap(tp->aperegs);
14224 tp->aperegs = NULL;
14225 }
Michael Chan68929142005-08-09 20:17:14 -070014226 if (tp->regs) {
14227 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014228 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014230 free_netdev(dev);
14231 pci_release_regions(pdev);
14232 pci_disable_device(pdev);
14233 pci_set_drvdata(pdev, NULL);
14234 }
14235}
14236
14237static int tg3_suspend(struct pci_dev *pdev, pm_message_t state)
14238{
14239 struct net_device *dev = pci_get_drvdata(pdev);
14240 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014241 pci_power_t target_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014242 int err;
14243
Michael Chan3e0c95f2007-08-03 20:56:54 -070014244 /* PCI register 4 needs to be saved whether netif_running() or not.
14245 * MSI address and data need to be saved if using MSI and
14246 * netif_running().
14247 */
14248 pci_save_state(pdev);
14249
Linus Torvalds1da177e2005-04-16 15:20:36 -070014250 if (!netif_running(dev))
14251 return 0;
14252
Michael Chan7faa0062006-02-02 17:29:28 -080014253 flush_scheduled_work();
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014254 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014255 tg3_netif_stop(tp);
14256
14257 del_timer_sync(&tp->timer);
14258
David S. Millerf47c11e2005-06-24 20:18:35 -070014259 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014260 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070014261 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014262
14263 netif_device_detach(dev);
14264
David S. Millerf47c11e2005-06-24 20:18:35 -070014265 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070014266 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080014267 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070014268 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014269
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014270 target_state = pdev->pm_cap ? pci_target_state(pdev) : PCI_D3hot;
14271
14272 err = tg3_set_power_state(tp, target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014273 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014274 int err2;
14275
David S. Millerf47c11e2005-06-24 20:18:35 -070014276 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014277
Michael Chan6a9eba12005-12-13 21:08:58 -080014278 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014279 err2 = tg3_restart_hw(tp, 1);
14280 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070014281 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014282
14283 tp->timer.expires = jiffies + tp->timer_offset;
14284 add_timer(&tp->timer);
14285
14286 netif_device_attach(dev);
14287 tg3_netif_start(tp);
14288
Michael Chanb9ec6c12006-07-25 16:37:27 -070014289out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014290 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014291
14292 if (!err2)
14293 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014294 }
14295
14296 return err;
14297}
14298
14299static int tg3_resume(struct pci_dev *pdev)
14300{
14301 struct net_device *dev = pci_get_drvdata(pdev);
14302 struct tg3 *tp = netdev_priv(dev);
14303 int err;
14304
Michael Chan3e0c95f2007-08-03 20:56:54 -070014305 pci_restore_state(tp->pdev);
14306
Linus Torvalds1da177e2005-04-16 15:20:36 -070014307 if (!netif_running(dev))
14308 return 0;
14309
Michael Chanbc1c7562006-03-20 17:48:03 -080014310 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014311 if (err)
14312 return err;
14313
14314 netif_device_attach(dev);
14315
David S. Millerf47c11e2005-06-24 20:18:35 -070014316 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014317
Michael Chan6a9eba12005-12-13 21:08:58 -080014318 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070014319 err = tg3_restart_hw(tp, 1);
14320 if (err)
14321 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014322
14323 tp->timer.expires = jiffies + tp->timer_offset;
14324 add_timer(&tp->timer);
14325
Linus Torvalds1da177e2005-04-16 15:20:36 -070014326 tg3_netif_start(tp);
14327
Michael Chanb9ec6c12006-07-25 16:37:27 -070014328out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014329 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014330
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014331 if (!err)
14332 tg3_phy_start(tp);
14333
Michael Chanb9ec6c12006-07-25 16:37:27 -070014334 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014335}
14336
14337static struct pci_driver tg3_driver = {
14338 .name = DRV_MODULE_NAME,
14339 .id_table = tg3_pci_tbl,
14340 .probe = tg3_init_one,
14341 .remove = __devexit_p(tg3_remove_one),
14342 .suspend = tg3_suspend,
14343 .resume = tg3_resume
14344};
14345
14346static int __init tg3_init(void)
14347{
Jeff Garzik29917622006-08-19 17:48:59 -040014348 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014349}
14350
14351static void __exit tg3_cleanup(void)
14352{
14353 pci_unregister_driver(&tg3_driver);
14354}
14355
14356module_init(tg3_init);
14357module_exit(tg3_cleanup);