blob: 46a3f86125be7659e4f35700a6ee209914febcea [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 Carlsonbb9e63e2009-04-20 07:13:31 +000071#define DRV_MODULE_VERSION "3.99"
72#define DRV_MODULE_RELDATE "April 20, 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) \
Michael Chan0f893dc2005-07-25 12:30:38 -070095 ((tp->tg3_flags2 & TG3_FLG2_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
105
106/* Do not place this n-ring entries value into the tp struct itself,
107 * we really want to expose these constants to GCC so that modulo et
108 * al. operations are done with shifts and masks instead of with
109 * hw multiply/modulo instructions. Another solution would be to
110 * replace things like '% foo' with '& (foo - 1)'.
111 */
112#define TG3_RX_RCB_RING_SIZE(tp) \
113 ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ? 512 : 1024)
114
115#define TG3_TX_RING_SIZE 512
116#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
117
118#define TG3_RX_RING_BYTES (sizeof(struct tg3_rx_buffer_desc) * \
119 TG3_RX_RING_SIZE)
120#define TG3_RX_JUMBO_RING_BYTES (sizeof(struct tg3_rx_buffer_desc) * \
121 TG3_RX_JUMBO_RING_SIZE)
122#define TG3_RX_RCB_RING_BYTES(tp) (sizeof(struct tg3_rx_buffer_desc) * \
123 TG3_RX_RCB_RING_SIZE(tp))
124#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
125 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
127
128#define RX_PKT_BUF_SZ (1536 + tp->rx_offset + 64)
129#define RX_JUMBO_PKT_BUF_SZ (9046 + tp->rx_offset + 64)
130
131/* minimum number of free TX descriptors required to wake up TX process */
Ranjit Manomohan42952232006-10-18 20:54:26 -0700132#define TG3_TX_WAKEUP_THRESH(tp) ((tp)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Matt Carlsonad829262008-11-21 17:16:16 -0800134#define TG3_RAW_IP_ALIGN 2
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/* number of ETHTOOL_GSTATS u64's */
137#define TG3_NUM_STATS (sizeof(struct tg3_ethtool_stats)/sizeof(u64))
138
Michael Chan4cafd3f2005-05-29 14:56:34 -0700139#define TG3_NUM_TEST 6
140
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800141#define FIRMWARE_TG3 "tigon/tg3.bin"
142#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
143#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static char version[] __devinitdata =
146 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
147
148MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
149MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
150MODULE_LICENSE("GPL");
151MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800152MODULE_FIRMWARE(FIRMWARE_TG3);
153MODULE_FIRMWARE(FIRMWARE_TG3TSO);
154MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
158module_param(tg3_debug, int, 0);
159MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
160
161static struct pci_device_id tg3_pci_tbl[] = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700162 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
163 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
164 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
165 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
166 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
167 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
168 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
169 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
170 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
171 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
172 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
173 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
174 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
175 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
176 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
177 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
178 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
179 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
180 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
181 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
182 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
183 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
184 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5720)},
185 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700186 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700187 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
188 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
189 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750M)},
190 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
191 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
192 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
193 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
194 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
195 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
196 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
197 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
198 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
199 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
200 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700201 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700202 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
203 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
204 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800205 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700206 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
207 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
208 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
209 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
210 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
211 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
212 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700213 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
214 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson57e69832008-05-25 23:48:31 -0700222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5785)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700227 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
228 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
229 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
230 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
231 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
232 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
233 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
234 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235};
236
237MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
238
Andreas Mohr50da8592006-08-14 23:54:30 -0700239static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 const char string[ETH_GSTRING_LEN];
241} ethtool_stats_keys[TG3_NUM_STATS] = {
242 { "rx_octets" },
243 { "rx_fragments" },
244 { "rx_ucast_packets" },
245 { "rx_mcast_packets" },
246 { "rx_bcast_packets" },
247 { "rx_fcs_errors" },
248 { "rx_align_errors" },
249 { "rx_xon_pause_rcvd" },
250 { "rx_xoff_pause_rcvd" },
251 { "rx_mac_ctrl_rcvd" },
252 { "rx_xoff_entered" },
253 { "rx_frame_too_long_errors" },
254 { "rx_jabbers" },
255 { "rx_undersize_packets" },
256 { "rx_in_length_errors" },
257 { "rx_out_length_errors" },
258 { "rx_64_or_less_octet_packets" },
259 { "rx_65_to_127_octet_packets" },
260 { "rx_128_to_255_octet_packets" },
261 { "rx_256_to_511_octet_packets" },
262 { "rx_512_to_1023_octet_packets" },
263 { "rx_1024_to_1522_octet_packets" },
264 { "rx_1523_to_2047_octet_packets" },
265 { "rx_2048_to_4095_octet_packets" },
266 { "rx_4096_to_8191_octet_packets" },
267 { "rx_8192_to_9022_octet_packets" },
268
269 { "tx_octets" },
270 { "tx_collisions" },
271
272 { "tx_xon_sent" },
273 { "tx_xoff_sent" },
274 { "tx_flow_control" },
275 { "tx_mac_errors" },
276 { "tx_single_collisions" },
277 { "tx_mult_collisions" },
278 { "tx_deferred" },
279 { "tx_excessive_collisions" },
280 { "tx_late_collisions" },
281 { "tx_collide_2times" },
282 { "tx_collide_3times" },
283 { "tx_collide_4times" },
284 { "tx_collide_5times" },
285 { "tx_collide_6times" },
286 { "tx_collide_7times" },
287 { "tx_collide_8times" },
288 { "tx_collide_9times" },
289 { "tx_collide_10times" },
290 { "tx_collide_11times" },
291 { "tx_collide_12times" },
292 { "tx_collide_13times" },
293 { "tx_collide_14times" },
294 { "tx_collide_15times" },
295 { "tx_ucast_packets" },
296 { "tx_mcast_packets" },
297 { "tx_bcast_packets" },
298 { "tx_carrier_sense_errors" },
299 { "tx_discards" },
300 { "tx_errors" },
301
302 { "dma_writeq_full" },
303 { "dma_write_prioq_full" },
304 { "rxbds_empty" },
305 { "rx_discards" },
306 { "rx_errors" },
307 { "rx_threshold_hit" },
308
309 { "dma_readq_full" },
310 { "dma_read_prioq_full" },
311 { "tx_comp_queue_full" },
312
313 { "ring_set_send_prod_index" },
314 { "ring_status_update" },
315 { "nic_irqs" },
316 { "nic_avoided_irqs" },
317 { "nic_tx_threshold_hit" }
318};
319
Andreas Mohr50da8592006-08-14 23:54:30 -0700320static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700321 const char string[ETH_GSTRING_LEN];
322} ethtool_test_keys[TG3_NUM_TEST] = {
323 { "nvram test (online) " },
324 { "link test (online) " },
325 { "register test (offline)" },
326 { "memory test (offline)" },
327 { "loopback test (offline)" },
328 { "interrupt test (offline)" },
329};
330
Michael Chanb401e9e2005-12-19 16:27:04 -0800331static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
332{
333 writel(val, tp->regs + off);
334}
335
336static u32 tg3_read32(struct tg3 *tp, u32 off)
337{
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400338 return (readl(tp->regs + off));
Michael Chanb401e9e2005-12-19 16:27:04 -0800339}
340
Matt Carlson0d3031d2007-10-10 18:02:43 -0700341static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
342{
343 writel(val, tp->aperegs + off);
344}
345
346static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
347{
348 return (readl(tp->aperegs + off));
349}
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
352{
Michael Chan68929142005-08-09 20:17:14 -0700353 unsigned long flags;
354
355 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700356 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
357 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700358 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700359}
360
361static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
362{
363 writel(val, tp->regs + off);
364 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Michael Chan68929142005-08-09 20:17:14 -0700367static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
368{
369 unsigned long flags;
370 u32 val;
371
372 spin_lock_irqsave(&tp->indirect_lock, flags);
373 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
374 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
375 spin_unlock_irqrestore(&tp->indirect_lock, flags);
376 return val;
377}
378
379static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
380{
381 unsigned long flags;
382
383 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
384 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
385 TG3_64BIT_REG_LOW, val);
386 return;
387 }
388 if (off == (MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW)) {
389 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
390 TG3_64BIT_REG_LOW, val);
391 return;
392 }
393
394 spin_lock_irqsave(&tp->indirect_lock, flags);
395 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
396 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
397 spin_unlock_irqrestore(&tp->indirect_lock, flags);
398
399 /* In indirect mode when disabling interrupts, we also need
400 * to clear the interrupt bit in the GRC local ctrl register.
401 */
402 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
403 (val == 0x1)) {
404 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
405 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
406 }
407}
408
409static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
410{
411 unsigned long flags;
412 u32 val;
413
414 spin_lock_irqsave(&tp->indirect_lock, flags);
415 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
416 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
417 spin_unlock_irqrestore(&tp->indirect_lock, flags);
418 return val;
419}
420
Michael Chanb401e9e2005-12-19 16:27:04 -0800421/* usec_wait specifies the wait time in usec when writing to certain registers
422 * where it is unsafe to read back the register without some delay.
423 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
424 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
425 */
426static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Michael Chanb401e9e2005-12-19 16:27:04 -0800428 if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
429 (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
430 /* Non-posted methods */
431 tp->write32(tp, off, val);
432 else {
433 /* Posted method */
434 tg3_write32(tp, off, val);
435 if (usec_wait)
436 udelay(usec_wait);
437 tp->read32(tp, off);
438 }
439 /* Wait again after the read for the posted method to guarantee that
440 * the wait time is met.
441 */
442 if (usec_wait)
443 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Michael Chan09ee9292005-08-09 20:17:00 -0700446static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
447{
448 tp->write32_mbox(tp, off, val);
Michael Chan68929142005-08-09 20:17:14 -0700449 if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
450 !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
451 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700452}
453
Michael Chan20094932005-08-09 20:16:32 -0700454static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 void __iomem *mbox = tp->regs + off;
457 writel(val, mbox);
458 if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
459 writel(val, mbox);
460 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
461 readl(mbox);
462}
463
Michael Chanb5d37722006-09-27 16:06:21 -0700464static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
465{
466 return (readl(tp->regs + off + GRCMBOX_BASE));
467}
468
469static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
470{
471 writel(val, tp->regs + off + GRCMBOX_BASE);
472}
473
Michael Chan20094932005-08-09 20:16:32 -0700474#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700475#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Michael Chan20094932005-08-09 20:16:32 -0700476#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
477#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700478#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700479
480#define tw32(reg,val) tp->write32(tp, reg, val)
Michael Chanb401e9e2005-12-19 16:27:04 -0800481#define tw32_f(reg,val) _tw32_flush(tp,(reg),(val), 0)
482#define tw32_wait_f(reg,val,us) _tw32_flush(tp,(reg),(val), (us))
Michael Chan20094932005-08-09 20:16:32 -0700483#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
486{
Michael Chan68929142005-08-09 20:17:14 -0700487 unsigned long flags;
488
Michael Chanb5d37722006-09-27 16:06:21 -0700489 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
490 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
491 return;
492
Michael Chan68929142005-08-09 20:17:14 -0700493 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700494 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
495 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
496 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Michael Chanbbadf502006-04-06 21:46:34 -0700498 /* Always leave this as zero. */
499 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
500 } else {
501 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
502 tw32_f(TG3PCI_MEM_WIN_DATA, val);
503
504 /* Always leave this as zero. */
505 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
506 }
Michael Chan68929142005-08-09 20:17:14 -0700507 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
510static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
511{
Michael Chan68929142005-08-09 20:17:14 -0700512 unsigned long flags;
513
Michael Chanb5d37722006-09-27 16:06:21 -0700514 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
515 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
516 *val = 0;
517 return;
518 }
519
Michael Chan68929142005-08-09 20:17:14 -0700520 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700521 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
522 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
523 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Michael Chanbbadf502006-04-06 21:46:34 -0700525 /* Always leave this as zero. */
526 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
527 } else {
528 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
529 *val = tr32(TG3PCI_MEM_WIN_DATA);
530
531 /* Always leave this as zero. */
532 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
533 }
Michael Chan68929142005-08-09 20:17:14 -0700534 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Matt Carlson0d3031d2007-10-10 18:02:43 -0700537static void tg3_ape_lock_init(struct tg3 *tp)
538{
539 int i;
540
541 /* Make sure the driver hasn't any stale locks. */
542 for (i = 0; i < 8; i++)
543 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + 4 * i,
544 APE_LOCK_GRANT_DRIVER);
545}
546
547static int tg3_ape_lock(struct tg3 *tp, int locknum)
548{
549 int i, off;
550 int ret = 0;
551 u32 status;
552
553 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
554 return 0;
555
556 switch (locknum) {
Matt Carlson77b483f2008-08-15 14:07:24 -0700557 case TG3_APE_LOCK_GRC:
Matt Carlson0d3031d2007-10-10 18:02:43 -0700558 case TG3_APE_LOCK_MEM:
559 break;
560 default:
561 return -EINVAL;
562 }
563
564 off = 4 * locknum;
565
566 tg3_ape_write32(tp, TG3_APE_LOCK_REQ + off, APE_LOCK_REQ_DRIVER);
567
568 /* Wait for up to 1 millisecond to acquire lock. */
569 for (i = 0; i < 100; i++) {
570 status = tg3_ape_read32(tp, TG3_APE_LOCK_GRANT + off);
571 if (status == APE_LOCK_GRANT_DRIVER)
572 break;
573 udelay(10);
574 }
575
576 if (status != APE_LOCK_GRANT_DRIVER) {
577 /* Revoke the lock request. */
578 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + off,
579 APE_LOCK_GRANT_DRIVER);
580
581 ret = -EBUSY;
582 }
583
584 return ret;
585}
586
587static void tg3_ape_unlock(struct tg3 *tp, int locknum)
588{
589 int off;
590
591 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
592 return;
593
594 switch (locknum) {
Matt Carlson77b483f2008-08-15 14:07:24 -0700595 case TG3_APE_LOCK_GRC:
Matt Carlson0d3031d2007-10-10 18:02:43 -0700596 case TG3_APE_LOCK_MEM:
597 break;
598 default:
599 return;
600 }
601
602 off = 4 * locknum;
603 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + off, APE_LOCK_GRANT_DRIVER);
604}
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606static void tg3_disable_ints(struct tg3 *tp)
607{
608 tw32(TG3PCI_MISC_HOST_CTRL,
609 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Michael Chan09ee9292005-08-09 20:17:00 -0700610 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
613static inline void tg3_cond_int(struct tg3 *tp)
614{
Michael Chan38f38432005-09-05 17:53:32 -0700615 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
616 (tp->hw_status->status & SD_STATUS_UPDATED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
Michael Chanb5d37722006-09-27 16:06:21 -0700618 else
619 tw32(HOSTCC_MODE, tp->coalesce_mode |
620 (HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623static void tg3_enable_ints(struct tg3 *tp)
624{
Michael Chanbbe832c2005-06-24 20:20:04 -0700625 tp->irq_sync = 0;
626 wmb();
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 tw32(TG3PCI_MISC_HOST_CTRL,
629 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Michael Chan09ee9292005-08-09 20:17:00 -0700630 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
631 (tp->last_tag << 24));
Michael Chanfcfa0a32006-03-20 22:28:41 -0800632 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
633 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
634 (tp->last_tag << 24));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 tg3_cond_int(tp);
636}
637
Michael Chan04237dd2005-04-25 15:17:17 -0700638static inline unsigned int tg3_has_work(struct tg3 *tp)
639{
640 struct tg3_hw_status *sblk = tp->hw_status;
641 unsigned int work_exists = 0;
642
643 /* check for phy events */
644 if (!(tp->tg3_flags &
645 (TG3_FLAG_USE_LINKCHG_REG |
646 TG3_FLAG_POLL_SERDES))) {
647 if (sblk->status & SD_STATUS_LINK_CHG)
648 work_exists = 1;
649 }
650 /* check for RX/TX work to do */
651 if (sblk->idx[0].tx_consumer != tp->tx_cons ||
652 sblk->idx[0].rx_producer != tp->rx_rcb_ptr)
653 work_exists = 1;
654
655 return work_exists;
656}
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658/* tg3_restart_ints
Michael Chan04237dd2005-04-25 15:17:17 -0700659 * similar to tg3_enable_ints, but it accurately determines whether there
660 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400661 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
663static void tg3_restart_ints(struct tg3 *tp)
664{
David S. Millerfac9b832005-05-18 22:46:34 -0700665 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
666 tp->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 mmiowb();
668
David S. Millerfac9b832005-05-18 22:46:34 -0700669 /* When doing tagged status, this work check is unnecessary.
670 * The last_tag we write above tells the chip which piece of
671 * work we've completed.
672 */
673 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
674 tg3_has_work(tp))
Michael Chan04237dd2005-04-25 15:17:17 -0700675 tw32(HOSTCC_MODE, tp->coalesce_mode |
676 (HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679static inline void tg3_netif_stop(struct tg3 *tp)
680{
Michael Chanbbe832c2005-06-24 20:20:04 -0700681 tp->dev->trans_start = jiffies; /* prevent tx timeout */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700682 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 netif_tx_disable(tp->dev);
684}
685
686static inline void tg3_netif_start(struct tg3 *tp)
687{
688 netif_wake_queue(tp->dev);
689 /* NOTE: unconditional netif_wake_queue is only appropriate
690 * so long as all callers are assured to have free tx slots
691 * (such as after tg3_init_hw)
692 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700693 napi_enable(&tp->napi);
David S. Millerf47c11e2005-06-24 20:18:35 -0700694 tp->hw_status->status |= SD_STATUS_UPDATED;
695 tg3_enable_ints(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
698static void tg3_switch_clocks(struct tg3 *tp)
699{
700 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
701 u32 orig_clock_ctrl;
702
Matt Carlson795d01c2007-10-07 23:28:17 -0700703 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
704 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700705 return;
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 orig_clock_ctrl = clock_ctrl;
708 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
709 CLOCK_CTRL_CLKRUN_OENABLE |
710 0x1f);
711 tp->pci_clock_ctrl = clock_ctrl;
712
713 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
714 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800715 tw32_wait_f(TG3PCI_CLOCK_CTRL,
716 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800719 tw32_wait_f(TG3PCI_CLOCK_CTRL,
720 clock_ctrl |
721 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
722 40);
723 tw32_wait_f(TG3PCI_CLOCK_CTRL,
724 clock_ctrl | (CLOCK_CTRL_ALTCLK),
725 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800727 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
730#define PHY_BUSY_LOOPS 5000
731
732static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
733{
734 u32 frame_val;
735 unsigned int loops;
736 int ret;
737
738 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
739 tw32_f(MAC_MI_MODE,
740 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
741 udelay(80);
742 }
743
744 *val = 0x0;
745
746 frame_val = ((PHY_ADDR << MI_COM_PHY_ADDR_SHIFT) &
747 MI_COM_PHY_ADDR_MASK);
748 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
749 MI_COM_REG_ADDR_MASK);
750 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 tw32_f(MAC_MI_COM, frame_val);
753
754 loops = PHY_BUSY_LOOPS;
755 while (loops != 0) {
756 udelay(10);
757 frame_val = tr32(MAC_MI_COM);
758
759 if ((frame_val & MI_COM_BUSY) == 0) {
760 udelay(5);
761 frame_val = tr32(MAC_MI_COM);
762 break;
763 }
764 loops -= 1;
765 }
766
767 ret = -EBUSY;
768 if (loops != 0) {
769 *val = frame_val & MI_COM_DATA_MASK;
770 ret = 0;
771 }
772
773 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
774 tw32_f(MAC_MI_MODE, tp->mi_mode);
775 udelay(80);
776 }
777
778 return ret;
779}
780
781static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
782{
783 u32 frame_val;
784 unsigned int loops;
785 int ret;
786
Michael Chanb5d37722006-09-27 16:06:21 -0700787 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
788 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
789 return 0;
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
792 tw32_f(MAC_MI_MODE,
793 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
794 udelay(80);
795 }
796
797 frame_val = ((PHY_ADDR << MI_COM_PHY_ADDR_SHIFT) &
798 MI_COM_PHY_ADDR_MASK);
799 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
800 MI_COM_REG_ADDR_MASK);
801 frame_val |= (val & MI_COM_DATA_MASK);
802 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 tw32_f(MAC_MI_COM, frame_val);
805
806 loops = PHY_BUSY_LOOPS;
807 while (loops != 0) {
808 udelay(10);
809 frame_val = tr32(MAC_MI_COM);
810 if ((frame_val & MI_COM_BUSY) == 0) {
811 udelay(5);
812 frame_val = tr32(MAC_MI_COM);
813 break;
814 }
815 loops -= 1;
816 }
817
818 ret = -EBUSY;
819 if (loops != 0)
820 ret = 0;
821
822 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
823 tw32_f(MAC_MI_MODE, tp->mi_mode);
824 udelay(80);
825 }
826
827 return ret;
828}
829
Matt Carlson95e28692008-05-25 23:44:14 -0700830static int tg3_bmcr_reset(struct tg3 *tp)
831{
832 u32 phy_control;
833 int limit, err;
834
835 /* OK, reset it, and poll the BMCR_RESET bit until it
836 * clears or we time out.
837 */
838 phy_control = BMCR_RESET;
839 err = tg3_writephy(tp, MII_BMCR, phy_control);
840 if (err != 0)
841 return -EBUSY;
842
843 limit = 5000;
844 while (limit--) {
845 err = tg3_readphy(tp, MII_BMCR, &phy_control);
846 if (err != 0)
847 return -EBUSY;
848
849 if ((phy_control & BMCR_RESET) == 0) {
850 udelay(40);
851 break;
852 }
853 udelay(10);
854 }
Roel Kluind4675b52009-02-12 16:33:27 -0800855 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -0700856 return -EBUSY;
857
858 return 0;
859}
860
Matt Carlson158d7ab2008-05-29 01:37:54 -0700861static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
862{
Francois Romieu3d165432009-01-19 16:56:50 -0800863 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700864 u32 val;
865
866 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_PAUSED)
867 return -EAGAIN;
868
869 if (tg3_readphy(tp, reg, &val))
870 return -EIO;
871
872 return val;
873}
874
875static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
876{
Francois Romieu3d165432009-01-19 16:56:50 -0800877 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700878
879 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_PAUSED)
880 return -EAGAIN;
881
882 if (tg3_writephy(tp, reg, val))
883 return -EIO;
884
885 return 0;
886}
887
888static int tg3_mdio_reset(struct mii_bus *bp)
889{
890 return 0;
891}
892
Matt Carlson9c61d6b2008-11-03 16:54:56 -0800893static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -0700894{
895 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800896 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -0700897
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800898 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
899 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
900 case TG3_PHY_ID_BCM50610:
901 val = MAC_PHYCFG2_50610_LED_MODES;
902 break;
903 case TG3_PHY_ID_BCMAC131:
904 val = MAC_PHYCFG2_AC131_LED_MODES;
905 break;
906 case TG3_PHY_ID_RTL8211C:
907 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
908 break;
909 case TG3_PHY_ID_RTL8201E:
910 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
911 break;
912 default:
Matt Carlsona9daf362008-05-25 23:49:44 -0700913 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800914 }
915
916 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
917 tw32(MAC_PHYCFG2, val);
918
919 val = tr32(MAC_PHYCFG1);
920 val &= ~MAC_PHYCFG1_RGMII_INT;
921 tw32(MAC_PHYCFG1, val);
922
923 return;
924 }
925
926 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE))
927 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
928 MAC_PHYCFG2_FMODE_MASK_MASK |
929 MAC_PHYCFG2_GMODE_MASK_MASK |
930 MAC_PHYCFG2_ACT_MASK_MASK |
931 MAC_PHYCFG2_QUAL_MASK_MASK |
932 MAC_PHYCFG2_INBAND_ENABLE;
933
934 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -0700935
936 val = tr32(MAC_PHYCFG1) & ~(MAC_PHYCFG1_RGMII_EXT_RX_DEC |
937 MAC_PHYCFG1_RGMII_SND_STAT_EN);
938 if (tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE) {
939 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
940 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
941 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
942 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
943 }
944 tw32(MAC_PHYCFG1, val | MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV);
945
Matt Carlsona9daf362008-05-25 23:49:44 -0700946 val = tr32(MAC_EXT_RGMII_MODE);
947 val &= ~(MAC_RGMII_MODE_RX_INT_B |
948 MAC_RGMII_MODE_RX_QUALITY |
949 MAC_RGMII_MODE_RX_ACTIVITY |
950 MAC_RGMII_MODE_RX_ENG_DET |
951 MAC_RGMII_MODE_TX_ENABLE |
952 MAC_RGMII_MODE_TX_LOWPWR |
953 MAC_RGMII_MODE_TX_RESET);
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800954 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -0700955 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
956 val |= MAC_RGMII_MODE_RX_INT_B |
957 MAC_RGMII_MODE_RX_QUALITY |
958 MAC_RGMII_MODE_RX_ACTIVITY |
959 MAC_RGMII_MODE_RX_ENG_DET;
960 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
961 val |= MAC_RGMII_MODE_TX_ENABLE |
962 MAC_RGMII_MODE_TX_LOWPWR |
963 MAC_RGMII_MODE_TX_RESET;
964 }
965 tw32(MAC_EXT_RGMII_MODE, val);
966}
967
Matt Carlson158d7ab2008-05-29 01:37:54 -0700968static void tg3_mdio_start(struct tg3 *tp)
969{
970 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700971 mutex_lock(&tp->mdio_bus->mdio_lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700972 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_PAUSED;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700973 mutex_unlock(&tp->mdio_bus->mdio_lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700974 }
975
976 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
977 tw32_f(MAC_MI_MODE, tp->mi_mode);
978 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -0700979
Matt Carlson9c61d6b2008-11-03 16:54:56 -0800980 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
981 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
982 tg3_mdio_config_5785(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700983}
984
985static void tg3_mdio_stop(struct tg3 *tp)
986{
987 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700988 mutex_lock(&tp->mdio_bus->mdio_lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700989 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_PAUSED;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700990 mutex_unlock(&tp->mdio_bus->mdio_lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700991 }
992}
993
994static int tg3_mdio_init(struct tg3 *tp)
995{
996 int i;
997 u32 reg;
Matt Carlsona9daf362008-05-25 23:49:44 -0700998 struct phy_device *phydev;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700999
1000 tg3_mdio_start(tp);
1001
1002 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1003 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1004 return 0;
1005
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001006 tp->mdio_bus = mdiobus_alloc();
1007 if (tp->mdio_bus == NULL)
1008 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001009
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001010 tp->mdio_bus->name = "tg3 mdio bus";
1011 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001012 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001013 tp->mdio_bus->priv = tp;
1014 tp->mdio_bus->parent = &tp->pdev->dev;
1015 tp->mdio_bus->read = &tg3_mdio_read;
1016 tp->mdio_bus->write = &tg3_mdio_write;
1017 tp->mdio_bus->reset = &tg3_mdio_reset;
1018 tp->mdio_bus->phy_mask = ~(1 << PHY_ADDR);
1019 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001020
1021 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001022 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001023
1024 /* The bus registration will look for all the PHYs on the mdio bus.
1025 * Unfortunately, it does not ensure the PHY is powered up before
1026 * accessing the PHY ID registers. A chip reset is the
1027 * quickest way to bring the device back to an operational state..
1028 */
1029 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1030 tg3_bmcr_reset(tp);
1031
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001032 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001033 if (i) {
Matt Carlson158d7ab2008-05-29 01:37:54 -07001034 printk(KERN_WARNING "%s: mdiobus_reg failed (0x%x)\n",
1035 tp->dev->name, i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001036 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001037 return i;
1038 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001039
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001040 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001041
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001042 if (!phydev || !phydev->drv) {
1043 printk(KERN_WARNING "%s: No PHY devices\n", tp->dev->name);
1044 mdiobus_unregister(tp->mdio_bus);
1045 mdiobus_free(tp->mdio_bus);
1046 return -ENODEV;
1047 }
1048
1049 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001050 case TG3_PHY_ID_BCM57780:
1051 phydev->interface = PHY_INTERFACE_MODE_GMII;
1052 break;
Matt Carlsona9daf362008-05-25 23:49:44 -07001053 case TG3_PHY_ID_BCM50610:
Matt Carlsona9daf362008-05-25 23:49:44 -07001054 if (tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE)
1055 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1056 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1057 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1058 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1059 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001060 /* fallthru */
1061 case TG3_PHY_ID_RTL8211C:
1062 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001063 break;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001064 case TG3_PHY_ID_RTL8201E:
Matt Carlsona9daf362008-05-25 23:49:44 -07001065 case TG3_PHY_ID_BCMAC131:
1066 phydev->interface = PHY_INTERFACE_MODE_MII;
1067 break;
1068 }
1069
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001070 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1071
1072 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1073 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001074
1075 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001076}
1077
1078static void tg3_mdio_fini(struct tg3 *tp)
1079{
1080 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1081 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001082 mdiobus_unregister(tp->mdio_bus);
1083 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001084 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_PAUSED;
1085 }
1086}
1087
Matt Carlson95e28692008-05-25 23:44:14 -07001088/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001089static inline void tg3_generate_fw_event(struct tg3 *tp)
1090{
1091 u32 val;
1092
1093 val = tr32(GRC_RX_CPU_EVENT);
1094 val |= GRC_RX_CPU_DRIVER_EVENT;
1095 tw32_f(GRC_RX_CPU_EVENT, val);
1096
1097 tp->last_event_jiffies = jiffies;
1098}
1099
1100#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1101
1102/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001103static void tg3_wait_for_event_ack(struct tg3 *tp)
1104{
1105 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001106 unsigned int delay_cnt;
1107 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001108
Matt Carlson4ba526c2008-08-15 14:10:04 -07001109 /* If enough time has passed, no wait is necessary. */
1110 time_remain = (long)(tp->last_event_jiffies + 1 +
1111 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1112 (long)jiffies;
1113 if (time_remain < 0)
1114 return;
1115
1116 /* Check if we can shorten the wait time. */
1117 delay_cnt = jiffies_to_usecs(time_remain);
1118 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1119 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1120 delay_cnt = (delay_cnt >> 3) + 1;
1121
1122 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001123 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1124 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001125 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001126 }
1127}
1128
1129/* tp->lock is held. */
1130static void tg3_ump_link_report(struct tg3 *tp)
1131{
1132 u32 reg;
1133 u32 val;
1134
1135 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1136 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1137 return;
1138
1139 tg3_wait_for_event_ack(tp);
1140
1141 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1142
1143 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1144
1145 val = 0;
1146 if (!tg3_readphy(tp, MII_BMCR, &reg))
1147 val = reg << 16;
1148 if (!tg3_readphy(tp, MII_BMSR, &reg))
1149 val |= (reg & 0xffff);
1150 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1151
1152 val = 0;
1153 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1154 val = reg << 16;
1155 if (!tg3_readphy(tp, MII_LPA, &reg))
1156 val |= (reg & 0xffff);
1157 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1158
1159 val = 0;
1160 if (!(tp->tg3_flags2 & TG3_FLG2_MII_SERDES)) {
1161 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1162 val = reg << 16;
1163 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1164 val |= (reg & 0xffff);
1165 }
1166 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1167
1168 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1169 val = reg << 16;
1170 else
1171 val = 0;
1172 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1173
Matt Carlson4ba526c2008-08-15 14:10:04 -07001174 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001175}
1176
1177static void tg3_link_report(struct tg3 *tp)
1178{
1179 if (!netif_carrier_ok(tp->dev)) {
1180 if (netif_msg_link(tp))
1181 printk(KERN_INFO PFX "%s: Link is down.\n",
1182 tp->dev->name);
1183 tg3_ump_link_report(tp);
1184 } else if (netif_msg_link(tp)) {
1185 printk(KERN_INFO PFX "%s: Link is up at %d Mbps, %s duplex.\n",
1186 tp->dev->name,
1187 (tp->link_config.active_speed == SPEED_1000 ?
1188 1000 :
1189 (tp->link_config.active_speed == SPEED_100 ?
1190 100 : 10)),
1191 (tp->link_config.active_duplex == DUPLEX_FULL ?
1192 "full" : "half"));
1193
1194 printk(KERN_INFO PFX
1195 "%s: Flow control is %s for TX and %s for RX.\n",
1196 tp->dev->name,
Steve Glendinninge18ce342008-12-16 02:00:00 -08001197 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
Matt Carlson95e28692008-05-25 23:44:14 -07001198 "on" : "off",
Steve Glendinninge18ce342008-12-16 02:00:00 -08001199 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
Matt Carlson95e28692008-05-25 23:44:14 -07001200 "on" : "off");
1201 tg3_ump_link_report(tp);
1202 }
1203}
1204
1205static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1206{
1207 u16 miireg;
1208
Steve Glendinninge18ce342008-12-16 02:00:00 -08001209 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001210 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001211 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001212 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001213 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001214 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1215 else
1216 miireg = 0;
1217
1218 return miireg;
1219}
1220
1221static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1222{
1223 u16 miireg;
1224
Steve Glendinninge18ce342008-12-16 02:00:00 -08001225 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001226 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001227 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001228 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001229 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001230 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1231 else
1232 miireg = 0;
1233
1234 return miireg;
1235}
1236
Matt Carlson95e28692008-05-25 23:44:14 -07001237static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1238{
1239 u8 cap = 0;
1240
1241 if (lcladv & ADVERTISE_1000XPAUSE) {
1242 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1243 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001244 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001245 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001246 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001247 } else {
1248 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001249 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001250 }
1251 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1252 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001253 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001254 }
1255
1256 return cap;
1257}
1258
Matt Carlsonf51f3562008-05-25 23:45:08 -07001259static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001260{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001261 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001262 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001263 u32 old_rx_mode = tp->rx_mode;
1264 u32 old_tx_mode = tp->tx_mode;
1265
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001266 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001267 autoneg = tp->mdio_bus->phy_map[PHY_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001268 else
1269 autoneg = tp->link_config.autoneg;
1270
1271 if (autoneg == AUTONEG_ENABLE &&
Matt Carlson95e28692008-05-25 23:44:14 -07001272 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
1273 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001274 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001275 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001276 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001277 } else
1278 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001279
Matt Carlsonf51f3562008-05-25 23:45:08 -07001280 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001281
Steve Glendinninge18ce342008-12-16 02:00:00 -08001282 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001283 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1284 else
1285 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1286
Matt Carlsonf51f3562008-05-25 23:45:08 -07001287 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001288 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001289
Steve Glendinninge18ce342008-12-16 02:00:00 -08001290 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001291 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1292 else
1293 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1294
Matt Carlsonf51f3562008-05-25 23:45:08 -07001295 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001296 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001297}
1298
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001299static void tg3_adjust_link(struct net_device *dev)
1300{
1301 u8 oldflowctrl, linkmesg = 0;
1302 u32 mac_mode, lcl_adv, rmt_adv;
1303 struct tg3 *tp = netdev_priv(dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001304 struct phy_device *phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001305
1306 spin_lock(&tp->lock);
1307
1308 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1309 MAC_MODE_HALF_DUPLEX);
1310
1311 oldflowctrl = tp->link_config.active_flowctrl;
1312
1313 if (phydev->link) {
1314 lcl_adv = 0;
1315 rmt_adv = 0;
1316
1317 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1318 mac_mode |= MAC_MODE_PORT_MODE_MII;
1319 else
1320 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1321
1322 if (phydev->duplex == DUPLEX_HALF)
1323 mac_mode |= MAC_MODE_HALF_DUPLEX;
1324 else {
1325 lcl_adv = tg3_advert_flowctrl_1000T(
1326 tp->link_config.flowctrl);
1327
1328 if (phydev->pause)
1329 rmt_adv = LPA_PAUSE_CAP;
1330 if (phydev->asym_pause)
1331 rmt_adv |= LPA_PAUSE_ASYM;
1332 }
1333
1334 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1335 } else
1336 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1337
1338 if (mac_mode != tp->mac_mode) {
1339 tp->mac_mode = mac_mode;
1340 tw32_f(MAC_MODE, tp->mac_mode);
1341 udelay(40);
1342 }
1343
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001344 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1345 if (phydev->speed == SPEED_10)
1346 tw32(MAC_MI_STAT,
1347 MAC_MI_STAT_10MBPS_MODE |
1348 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1349 else
1350 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1351 }
1352
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001353 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1354 tw32(MAC_TX_LENGTHS,
1355 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1356 (6 << TX_LENGTHS_IPG_SHIFT) |
1357 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1358 else
1359 tw32(MAC_TX_LENGTHS,
1360 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1361 (6 << TX_LENGTHS_IPG_SHIFT) |
1362 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1363
1364 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1365 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1366 phydev->speed != tp->link_config.active_speed ||
1367 phydev->duplex != tp->link_config.active_duplex ||
1368 oldflowctrl != tp->link_config.active_flowctrl)
1369 linkmesg = 1;
1370
1371 tp->link_config.active_speed = phydev->speed;
1372 tp->link_config.active_duplex = phydev->duplex;
1373
1374 spin_unlock(&tp->lock);
1375
1376 if (linkmesg)
1377 tg3_link_report(tp);
1378}
1379
1380static int tg3_phy_init(struct tg3 *tp)
1381{
1382 struct phy_device *phydev;
1383
1384 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED)
1385 return 0;
1386
1387 /* Bring the PHY back to a known state. */
1388 tg3_bmcr_reset(tp);
1389
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001390 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001391
1392 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001393 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001394 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001395 if (IS_ERR(phydev)) {
1396 printk(KERN_ERR "%s: Could not attach to PHY\n", tp->dev->name);
1397 return PTR_ERR(phydev);
1398 }
1399
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001400 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001401 switch (phydev->interface) {
1402 case PHY_INTERFACE_MODE_GMII:
1403 case PHY_INTERFACE_MODE_RGMII:
Matt Carlson321d32a2008-11-21 17:22:19 -08001404 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
1405 phydev->supported &= (PHY_GBIT_FEATURES |
1406 SUPPORTED_Pause |
1407 SUPPORTED_Asym_Pause);
1408 break;
1409 }
1410 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001411 case PHY_INTERFACE_MODE_MII:
1412 phydev->supported &= (PHY_BASIC_FEATURES |
1413 SUPPORTED_Pause |
1414 SUPPORTED_Asym_Pause);
1415 break;
1416 default:
1417 phy_disconnect(tp->mdio_bus->phy_map[PHY_ADDR]);
1418 return -EINVAL;
1419 }
1420
1421 tp->tg3_flags3 |= TG3_FLG3_PHY_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001422
1423 phydev->advertising = phydev->supported;
1424
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001425 return 0;
1426}
1427
1428static void tg3_phy_start(struct tg3 *tp)
1429{
1430 struct phy_device *phydev;
1431
1432 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1433 return;
1434
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001435 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001436
1437 if (tp->link_config.phy_is_low_power) {
1438 tp->link_config.phy_is_low_power = 0;
1439 phydev->speed = tp->link_config.orig_speed;
1440 phydev->duplex = tp->link_config.orig_duplex;
1441 phydev->autoneg = tp->link_config.orig_autoneg;
1442 phydev->advertising = tp->link_config.orig_advertising;
1443 }
1444
1445 phy_start(phydev);
1446
1447 phy_start_aneg(phydev);
1448}
1449
1450static void tg3_phy_stop(struct tg3 *tp)
1451{
1452 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1453 return;
1454
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001455 phy_stop(tp->mdio_bus->phy_map[PHY_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001456}
1457
1458static void tg3_phy_fini(struct tg3 *tp)
1459{
1460 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001461 phy_disconnect(tp->mdio_bus->phy_map[PHY_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001462 tp->tg3_flags3 &= ~TG3_FLG3_PHY_CONNECTED;
1463 }
1464}
1465
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001466static void tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1467{
1468 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1469 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1470}
1471
Matt Carlson6833c042008-11-21 17:18:59 -08001472static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1473{
1474 u32 reg;
1475
Matt Carlsona6435f32009-02-25 14:21:20 +00001476 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1477 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson6833c042008-11-21 17:18:59 -08001478 return;
1479
1480 reg = MII_TG3_MISC_SHDW_WREN |
1481 MII_TG3_MISC_SHDW_SCR5_SEL |
1482 MII_TG3_MISC_SHDW_SCR5_LPED |
1483 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1484 MII_TG3_MISC_SHDW_SCR5_SDTL |
1485 MII_TG3_MISC_SHDW_SCR5_C125OE;
1486 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1487 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1488
1489 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1490
1491
1492 reg = MII_TG3_MISC_SHDW_WREN |
1493 MII_TG3_MISC_SHDW_APD_SEL |
1494 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1495 if (enable)
1496 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1497
1498 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1499}
1500
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001501static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1502{
1503 u32 phy;
1504
1505 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1506 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
1507 return;
1508
1509 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1510 u32 ephy;
1511
1512 if (!tg3_readphy(tp, MII_TG3_EPHY_TEST, &ephy)) {
1513 tg3_writephy(tp, MII_TG3_EPHY_TEST,
1514 ephy | MII_TG3_EPHY_SHADOW_EN);
1515 if (!tg3_readphy(tp, MII_TG3_EPHYTST_MISCCTRL, &phy)) {
1516 if (enable)
1517 phy |= MII_TG3_EPHYTST_MISCCTRL_MDIX;
1518 else
1519 phy &= ~MII_TG3_EPHYTST_MISCCTRL_MDIX;
1520 tg3_writephy(tp, MII_TG3_EPHYTST_MISCCTRL, phy);
1521 }
1522 tg3_writephy(tp, MII_TG3_EPHY_TEST, ephy);
1523 }
1524 } else {
1525 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1526 MII_TG3_AUXCTL_SHDWSEL_MISC;
1527 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1528 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1529 if (enable)
1530 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1531 else
1532 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1533 phy |= MII_TG3_AUXCTL_MISC_WREN;
1534 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1535 }
1536 }
1537}
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539static void tg3_phy_set_wirespeed(struct tg3 *tp)
1540{
1541 u32 val;
1542
1543 if (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED)
1544 return;
1545
1546 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1547 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1548 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1549 (val | (1 << 15) | (1 << 4)));
1550}
1551
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001552static void tg3_phy_apply_otp(struct tg3 *tp)
1553{
1554 u32 otp, phy;
1555
1556 if (!tp->phy_otp)
1557 return;
1558
1559 otp = tp->phy_otp;
1560
1561 /* Enable SM_DSP clock and tx 6dB coding. */
1562 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1563 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1564 MII_TG3_AUXCTL_ACTL_TX_6DB;
1565 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1566
1567 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1568 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1569 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1570
1571 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1572 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1573 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1574
1575 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1576 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1577 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1578
1579 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1580 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1581
1582 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1583 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1584
1585 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1586 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1587 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1588
1589 /* Turn off SM_DSP clock. */
1590 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1591 MII_TG3_AUXCTL_ACTL_TX_6DB;
1592 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1593}
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595static int tg3_wait_macro_done(struct tg3 *tp)
1596{
1597 int limit = 100;
1598
1599 while (limit--) {
1600 u32 tmp32;
1601
1602 if (!tg3_readphy(tp, 0x16, &tmp32)) {
1603 if ((tmp32 & 0x1000) == 0)
1604 break;
1605 }
1606 }
Roel Kluind4675b52009-02-12 16:33:27 -08001607 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return -EBUSY;
1609
1610 return 0;
1611}
1612
1613static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1614{
1615 static const u32 test_pat[4][6] = {
1616 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1617 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1618 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1619 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1620 };
1621 int chan;
1622
1623 for (chan = 0; chan < 4; chan++) {
1624 int i;
1625
1626 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1627 (chan * 0x2000) | 0x0200);
1628 tg3_writephy(tp, 0x16, 0x0002);
1629
1630 for (i = 0; i < 6; i++)
1631 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1632 test_pat[chan][i]);
1633
1634 tg3_writephy(tp, 0x16, 0x0202);
1635 if (tg3_wait_macro_done(tp)) {
1636 *resetp = 1;
1637 return -EBUSY;
1638 }
1639
1640 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1641 (chan * 0x2000) | 0x0200);
1642 tg3_writephy(tp, 0x16, 0x0082);
1643 if (tg3_wait_macro_done(tp)) {
1644 *resetp = 1;
1645 return -EBUSY;
1646 }
1647
1648 tg3_writephy(tp, 0x16, 0x0802);
1649 if (tg3_wait_macro_done(tp)) {
1650 *resetp = 1;
1651 return -EBUSY;
1652 }
1653
1654 for (i = 0; i < 6; i += 2) {
1655 u32 low, high;
1656
1657 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1658 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1659 tg3_wait_macro_done(tp)) {
1660 *resetp = 1;
1661 return -EBUSY;
1662 }
1663 low &= 0x7fff;
1664 high &= 0x000f;
1665 if (low != test_pat[chan][i] ||
1666 high != test_pat[chan][i+1]) {
1667 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1668 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1669 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1670
1671 return -EBUSY;
1672 }
1673 }
1674 }
1675
1676 return 0;
1677}
1678
1679static int tg3_phy_reset_chanpat(struct tg3 *tp)
1680{
1681 int chan;
1682
1683 for (chan = 0; chan < 4; chan++) {
1684 int i;
1685
1686 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1687 (chan * 0x2000) | 0x0200);
1688 tg3_writephy(tp, 0x16, 0x0002);
1689 for (i = 0; i < 6; i++)
1690 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
1691 tg3_writephy(tp, 0x16, 0x0202);
1692 if (tg3_wait_macro_done(tp))
1693 return -EBUSY;
1694 }
1695
1696 return 0;
1697}
1698
1699static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1700{
1701 u32 reg32, phy9_orig;
1702 int retries, do_phy_reset, err;
1703
1704 retries = 10;
1705 do_phy_reset = 1;
1706 do {
1707 if (do_phy_reset) {
1708 err = tg3_bmcr_reset(tp);
1709 if (err)
1710 return err;
1711 do_phy_reset = 0;
1712 }
1713
1714 /* Disable transmitter and interrupt. */
1715 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1716 continue;
1717
1718 reg32 |= 0x3000;
1719 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1720
1721 /* Set full-duplex, 1000 mbps. */
1722 tg3_writephy(tp, MII_BMCR,
1723 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1724
1725 /* Set to master mode. */
1726 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1727 continue;
1728
1729 tg3_writephy(tp, MII_TG3_CTRL,
1730 (MII_TG3_CTRL_AS_MASTER |
1731 MII_TG3_CTRL_ENABLE_AS_MASTER));
1732
1733 /* Enable SM_DSP_CLOCK and 6dB. */
1734 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1735
1736 /* Block the PHY control access. */
1737 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1738 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0800);
1739
1740 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1741 if (!err)
1742 break;
1743 } while (--retries);
1744
1745 err = tg3_phy_reset_chanpat(tp);
1746 if (err)
1747 return err;
1748
1749 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1750 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0000);
1751
1752 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
1753 tg3_writephy(tp, 0x16, 0x0000);
1754
1755 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1756 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1757 /* Set Extended packet length bit for jumbo frames */
1758 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
1759 }
1760 else {
1761 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1762 }
1763
1764 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1765
1766 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1767 reg32 &= ~0x3000;
1768 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1769 } else if (!err)
1770 err = -EBUSY;
1771
1772 return err;
1773}
1774
1775/* This will reset the tigon3 PHY if there is no valid
1776 * link unless the FORCE argument is non-zero.
1777 */
1778static int tg3_phy_reset(struct tg3 *tp)
1779{
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001780 u32 cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 u32 phy_status;
1782 int err;
1783
Michael Chan60189dd2006-12-17 17:08:07 -08001784 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1785 u32 val;
1786
1787 val = tr32(GRC_MISC_CFG);
1788 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1789 udelay(40);
1790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 err = tg3_readphy(tp, MII_BMSR, &phy_status);
1792 err |= tg3_readphy(tp, MII_BMSR, &phy_status);
1793 if (err != 0)
1794 return -EBUSY;
1795
Michael Chanc8e1e822006-04-29 18:55:17 -07001796 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
1797 netif_carrier_off(tp->dev);
1798 tg3_link_report(tp);
1799 }
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1802 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
1803 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
1804 err = tg3_phy_reset_5703_4_5(tp);
1805 if (err)
1806 return err;
1807 goto out;
1808 }
1809
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001810 cpmuctrl = 0;
1811 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
1812 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
1813 cpmuctrl = tr32(TG3_CPMU_CTRL);
1814 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
1815 tw32(TG3_CPMU_CTRL,
1816 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
1817 }
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 err = tg3_bmcr_reset(tp);
1820 if (err)
1821 return err;
1822
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001823 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
1824 u32 phy;
1825
1826 phy = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
1827 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, phy);
1828
1829 tw32(TG3_CPMU_CTRL, cpmuctrl);
1830 }
1831
Matt Carlsonbcb37f62008-11-03 16:52:09 -08001832 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
1833 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08001834 u32 val;
1835
1836 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
1837 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
1838 CPMU_LSPD_1000MB_MACCLK_12_5) {
1839 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
1840 udelay(40);
1841 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
1842 }
1843 }
1844
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001845 tg3_phy_apply_otp(tp);
1846
Matt Carlson6833c042008-11-21 17:18:59 -08001847 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
1848 tg3_phy_toggle_apd(tp, true);
1849 else
1850 tg3_phy_toggle_apd(tp, false);
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852out:
1853 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADC_BUG) {
1854 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1855 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
1856 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x2aaa);
1857 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
1858 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0323);
1859 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1860 }
1861 if (tp->tg3_flags2 & TG3_FLG2_PHY_5704_A0_BUG) {
1862 tg3_writephy(tp, 0x1c, 0x8d68);
1863 tg3_writephy(tp, 0x1c, 0x8d68);
1864 }
1865 if (tp->tg3_flags2 & TG3_FLG2_PHY_BER_BUG) {
1866 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1867 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
1868 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x310b);
1869 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
1870 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x9506);
1871 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x401f);
1872 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x14e2);
1873 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1874 }
Michael Chanc424cb22006-04-29 18:56:34 -07001875 else if (tp->tg3_flags2 & TG3_FLG2_PHY_JITTER_BUG) {
1876 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1877 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Michael Chanc1d2a192007-01-08 19:57:20 -08001878 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADJUST_TRIM) {
1879 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
1880 tg3_writephy(tp, MII_TG3_TEST1,
1881 MII_TG3_TEST1_TRIM_EN | 0x4);
1882 } else
1883 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07001884 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 /* Set Extended packet length bit (bit 14) on all chips that */
1887 /* support jumbo frames */
1888 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
1889 /* Cannot do read-modify-write on 5401 */
1890 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Michael Chan0f893dc2005-07-25 12:30:38 -07001891 } else if (tp->tg3_flags2 & TG3_FLG2_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 u32 phy_reg;
1893
1894 /* Set bit 14 with read-modify-write to preserve other bits */
1895 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
1896 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy_reg))
1897 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy_reg | 0x4000);
1898 }
1899
1900 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
1901 * jumbo frames transmission.
1902 */
Michael Chan0f893dc2005-07-25 12:30:38 -07001903 if (tp->tg3_flags2 & TG3_FLG2_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 u32 phy_reg;
1905
1906 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &phy_reg))
1907 tg3_writephy(tp, MII_TG3_EXT_CTRL,
1908 phy_reg | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
1909 }
1910
Michael Chan715116a2006-09-27 16:09:25 -07001911 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07001912 /* adjust output voltage */
1913 tg3_writephy(tp, MII_TG3_EPHY_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07001914 }
1915
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001916 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 tg3_phy_set_wirespeed(tp);
1918 return 0;
1919}
1920
1921static void tg3_frob_aux_power(struct tg3 *tp)
1922{
1923 struct tg3 *tp_peer = tp;
1924
Michael Chan9d26e212006-12-07 00:21:14 -08001925 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return;
1927
Michael Chan8c2dc7e2005-12-19 16:26:02 -08001928 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
1929 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
1930 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Michael Chan8c2dc7e2005-12-19 16:26:02 -08001932 dev_peer = pci_get_drvdata(tp->pdev_peer);
Michael Chanbc1c7562006-03-20 17:48:03 -08001933 /* remove_one() may have been run on the peer. */
Michael Chan8c2dc7e2005-12-19 16:26:02 -08001934 if (!dev_peer)
Michael Chanbc1c7562006-03-20 17:48:03 -08001935 tp_peer = tp;
1936 else
1937 tp_peer = netdev_priv(dev_peer);
Michael Chan8c2dc7e2005-12-19 16:26:02 -08001938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
Michael Chan6921d202005-12-13 21:15:53 -08001941 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0 ||
1942 (tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
1943 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
1945 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001946 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
1947 (GRC_LCLCTRL_GPIO_OE0 |
1948 GRC_LCLCTRL_GPIO_OE1 |
1949 GRC_LCLCTRL_GPIO_OE2 |
1950 GRC_LCLCTRL_GPIO_OUTPUT0 |
1951 GRC_LCLCTRL_GPIO_OUTPUT1),
1952 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00001953 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
1954 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07001955 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
1956 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
1957 GRC_LCLCTRL_GPIO_OE1 |
1958 GRC_LCLCTRL_GPIO_OE2 |
1959 GRC_LCLCTRL_GPIO_OUTPUT0 |
1960 GRC_LCLCTRL_GPIO_OUTPUT1 |
1961 tp->grc_local_ctrl;
1962 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
1963
1964 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
1965 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
1966
1967 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
1968 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 } else {
1970 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08001971 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 if (tp_peer != tp &&
1974 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
1975 return;
1976
Michael Chandc56b7d2005-12-19 16:26:28 -08001977 /* Workaround to prevent overdrawing Amps. */
1978 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
1979 ASIC_REV_5714) {
1980 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08001981 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
1982 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08001983 }
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 /* On 5753 and variants, GPIO2 cannot be used. */
1986 no_gpio2 = tp->nic_sram_data_cfg &
1987 NIC_SRAM_DATA_CFG_NO_GPIO2;
1988
Michael Chandc56b7d2005-12-19 16:26:28 -08001989 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 GRC_LCLCTRL_GPIO_OE1 |
1991 GRC_LCLCTRL_GPIO_OE2 |
1992 GRC_LCLCTRL_GPIO_OUTPUT1 |
1993 GRC_LCLCTRL_GPIO_OUTPUT2;
1994 if (no_gpio2) {
1995 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
1996 GRC_LCLCTRL_GPIO_OUTPUT2);
1997 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001998 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
1999 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2002
Michael Chanb401e9e2005-12-19 16:27:04 -08002003 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2004 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 if (!no_gpio2) {
2007 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002008 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2009 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
2011 }
2012 } else {
2013 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2014 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2015 if (tp_peer != tp &&
2016 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2017 return;
2018
Michael Chanb401e9e2005-12-19 16:27:04 -08002019 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2020 (GRC_LCLCTRL_GPIO_OE1 |
2021 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Michael Chanb401e9e2005-12-19 16:27:04 -08002023 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2024 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Michael Chanb401e9e2005-12-19 16:27:04 -08002026 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2027 (GRC_LCLCTRL_GPIO_OE1 |
2028 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
2030 }
2031}
2032
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002033static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2034{
2035 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2036 return 1;
2037 else if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411) {
2038 if (speed != SPEED_10)
2039 return 1;
2040 } else if (speed == SPEED_10)
2041 return 1;
2042
2043 return 0;
2044}
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046static int tg3_setup_phy(struct tg3 *, int);
2047
2048#define RESET_KIND_SHUTDOWN 0
2049#define RESET_KIND_INIT 1
2050#define RESET_KIND_SUSPEND 2
2051
2052static void tg3_write_sig_post_reset(struct tg3 *, int);
2053static int tg3_halt_cpu(struct tg3 *, u32);
2054
Matt Carlson0a459aa2008-11-03 16:54:15 -08002055static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002056{
Matt Carlsonce057f02007-11-12 21:08:03 -08002057 u32 val;
2058
Michael Chan51297242007-02-13 12:17:57 -08002059 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
2060 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2061 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2062 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2063
2064 sg_dig_ctrl |=
2065 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2066 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2067 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2068 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002069 return;
Michael Chan51297242007-02-13 12:17:57 -08002070 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002071
Michael Chan60189dd2006-12-17 17:08:07 -08002072 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002073 tg3_bmcr_reset(tp);
2074 val = tr32(GRC_MISC_CFG);
2075 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2076 udelay(40);
2077 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002078 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002079 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2080 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002081
2082 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2083 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2084 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2085 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2086 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002087 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002088
Michael Chan15c3b692006-03-22 01:06:52 -08002089 /* The PHY should not be powered down on some chips because
2090 * of bugs.
2091 */
2092 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2093 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2094 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
2095 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
2096 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002097
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002098 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2099 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002100 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2101 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2102 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2103 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2104 }
2105
Michael Chan15c3b692006-03-22 01:06:52 -08002106 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2107}
2108
Matt Carlson3f007892008-11-03 16:51:36 -08002109/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002110static int tg3_nvram_lock(struct tg3 *tp)
2111{
2112 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2113 int i;
2114
2115 if (tp->nvram_lock_cnt == 0) {
2116 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2117 for (i = 0; i < 8000; i++) {
2118 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2119 break;
2120 udelay(20);
2121 }
2122 if (i == 8000) {
2123 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2124 return -ENODEV;
2125 }
2126 }
2127 tp->nvram_lock_cnt++;
2128 }
2129 return 0;
2130}
2131
2132/* tp->lock is held. */
2133static void tg3_nvram_unlock(struct tg3 *tp)
2134{
2135 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2136 if (tp->nvram_lock_cnt > 0)
2137 tp->nvram_lock_cnt--;
2138 if (tp->nvram_lock_cnt == 0)
2139 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2140 }
2141}
2142
2143/* tp->lock is held. */
2144static void tg3_enable_nvram_access(struct tg3 *tp)
2145{
2146 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
2147 !(tp->tg3_flags2 & TG3_FLG2_PROTECTED_NVRAM)) {
2148 u32 nvaccess = tr32(NVRAM_ACCESS);
2149
2150 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2151 }
2152}
2153
2154/* tp->lock is held. */
2155static void tg3_disable_nvram_access(struct tg3 *tp)
2156{
2157 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
2158 !(tp->tg3_flags2 & TG3_FLG2_PROTECTED_NVRAM)) {
2159 u32 nvaccess = tr32(NVRAM_ACCESS);
2160
2161 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2162 }
2163}
2164
2165static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2166 u32 offset, u32 *val)
2167{
2168 u32 tmp;
2169 int i;
2170
2171 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2172 return -EINVAL;
2173
2174 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2175 EEPROM_ADDR_DEVID_MASK |
2176 EEPROM_ADDR_READ);
2177 tw32(GRC_EEPROM_ADDR,
2178 tmp |
2179 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2180 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2181 EEPROM_ADDR_ADDR_MASK) |
2182 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2183
2184 for (i = 0; i < 1000; i++) {
2185 tmp = tr32(GRC_EEPROM_ADDR);
2186
2187 if (tmp & EEPROM_ADDR_COMPLETE)
2188 break;
2189 msleep(1);
2190 }
2191 if (!(tmp & EEPROM_ADDR_COMPLETE))
2192 return -EBUSY;
2193
Matt Carlson62cedd12009-04-20 14:52:29 -07002194 tmp = tr32(GRC_EEPROM_DATA);
2195
2196 /*
2197 * The data will always be opposite the native endian
2198 * format. Perform a blind byteswap to compensate.
2199 */
2200 *val = swab32(tmp);
2201
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002202 return 0;
2203}
2204
2205#define NVRAM_CMD_TIMEOUT 10000
2206
2207static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2208{
2209 int i;
2210
2211 tw32(NVRAM_CMD, nvram_cmd);
2212 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2213 udelay(10);
2214 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2215 udelay(10);
2216 break;
2217 }
2218 }
2219
2220 if (i == NVRAM_CMD_TIMEOUT)
2221 return -EBUSY;
2222
2223 return 0;
2224}
2225
2226static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2227{
2228 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2229 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2230 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2231 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2232 (tp->nvram_jedecnum == JEDEC_ATMEL))
2233
2234 addr = ((addr / tp->nvram_pagesize) <<
2235 ATMEL_AT45DB0X1B_PAGE_POS) +
2236 (addr % tp->nvram_pagesize);
2237
2238 return addr;
2239}
2240
2241static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2242{
2243 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2244 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2245 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2246 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2247 (tp->nvram_jedecnum == JEDEC_ATMEL))
2248
2249 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2250 tp->nvram_pagesize) +
2251 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2252
2253 return addr;
2254}
2255
Matt Carlsone4f34112009-02-25 14:25:00 +00002256/* NOTE: Data read in from NVRAM is byteswapped according to
2257 * the byteswapping settings for all other register accesses.
2258 * tg3 devices are BE devices, so on a BE machine, the data
2259 * returned will be exactly as it is seen in NVRAM. On a LE
2260 * machine, the 32-bit value will be byteswapped.
2261 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002262static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2263{
2264 int ret;
2265
2266 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2267 return tg3_nvram_read_using_eeprom(tp, offset, val);
2268
2269 offset = tg3_nvram_phys_addr(tp, offset);
2270
2271 if (offset > NVRAM_ADDR_MSK)
2272 return -EINVAL;
2273
2274 ret = tg3_nvram_lock(tp);
2275 if (ret)
2276 return ret;
2277
2278 tg3_enable_nvram_access(tp);
2279
2280 tw32(NVRAM_ADDR, offset);
2281 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2282 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2283
2284 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002285 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002286
2287 tg3_disable_nvram_access(tp);
2288
2289 tg3_nvram_unlock(tp);
2290
2291 return ret;
2292}
2293
Matt Carlsona9dc5292009-02-25 14:25:30 +00002294/* Ensures NVRAM data is in bytestream format. */
2295static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002296{
2297 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002298 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002299 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002300 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002301 return res;
2302}
2303
2304/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002305static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2306{
2307 u32 addr_high, addr_low;
2308 int i;
2309
2310 addr_high = ((tp->dev->dev_addr[0] << 8) |
2311 tp->dev->dev_addr[1]);
2312 addr_low = ((tp->dev->dev_addr[2] << 24) |
2313 (tp->dev->dev_addr[3] << 16) |
2314 (tp->dev->dev_addr[4] << 8) |
2315 (tp->dev->dev_addr[5] << 0));
2316 for (i = 0; i < 4; i++) {
2317 if (i == 1 && skip_mac_1)
2318 continue;
2319 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2320 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2321 }
2322
2323 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2324 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2325 for (i = 0; i < 12; i++) {
2326 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2327 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2328 }
2329 }
2330
2331 addr_high = (tp->dev->dev_addr[0] +
2332 tp->dev->dev_addr[1] +
2333 tp->dev->dev_addr[2] +
2334 tp->dev->dev_addr[3] +
2335 tp->dev->dev_addr[4] +
2336 tp->dev->dev_addr[5]) &
2337 TX_BACKOFF_SEED_MASK;
2338 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2339}
2340
Michael Chanbc1c7562006-03-20 17:48:03 -08002341static int tg3_set_power_state(struct tg3 *tp, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
2343 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002344 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
2346 /* Make sure register accesses (indirect or otherwise)
2347 * will function correctly.
2348 */
2349 pci_write_config_dword(tp->pdev,
2350 TG3PCI_MISC_HOST_CTRL,
2351 tp->misc_host_ctrl);
2352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 switch (state) {
Michael Chanbc1c7562006-03-20 17:48:03 -08002354 case PCI_D0:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002355 pci_enable_wake(tp->pdev, state, false);
2356 pci_set_power_state(tp->pdev, PCI_D0);
Michael Chan8c6bda12005-04-21 17:09:08 -07002357
Michael Chan9d26e212006-12-07 00:21:14 -08002358 /* Switch out of Vaux if it is a NIC */
2359 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
Michael Chanb401e9e2005-12-19 16:27:04 -08002360 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
2362 return 0;
2363
Michael Chanbc1c7562006-03-20 17:48:03 -08002364 case PCI_D1:
Michael Chanbc1c7562006-03-20 17:48:03 -08002365 case PCI_D2:
Michael Chanbc1c7562006-03-20 17:48:03 -08002366 case PCI_D3hot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 break;
2368
2369 default:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002370 printk(KERN_ERR PFX "%s: Invalid power state (D%d) requested\n",
2371 tp->dev->name, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002373 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002374
2375 /* Restore the CLKREQ setting. */
2376 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2377 u16 lnkctl;
2378
2379 pci_read_config_word(tp->pdev,
2380 tp->pcie_cap + PCI_EXP_LNKCTL,
2381 &lnkctl);
2382 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2383 pci_write_config_word(tp->pdev,
2384 tp->pcie_cap + PCI_EXP_LNKCTL,
2385 lnkctl);
2386 }
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2389 tw32(TG3PCI_MISC_HOST_CTRL,
2390 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2391
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002392 device_should_wake = pci_pme_capable(tp->pdev, state) &&
2393 device_may_wakeup(&tp->pdev->dev) &&
2394 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2395
Matt Carlsondd477002008-05-25 23:45:58 -07002396 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002397 do_low_power = false;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002398 if ((tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) &&
2399 !tp->link_config.phy_is_low_power) {
2400 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002401 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002402
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002403 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002404
2405 tp->link_config.phy_is_low_power = 1;
2406
2407 tp->link_config.orig_speed = phydev->speed;
2408 tp->link_config.orig_duplex = phydev->duplex;
2409 tp->link_config.orig_autoneg = phydev->autoneg;
2410 tp->link_config.orig_advertising = phydev->advertising;
2411
2412 advertising = ADVERTISED_TP |
2413 ADVERTISED_Pause |
2414 ADVERTISED_Autoneg |
2415 ADVERTISED_10baseT_Half;
2416
2417 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002418 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002419 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2420 advertising |=
2421 ADVERTISED_100baseT_Half |
2422 ADVERTISED_100baseT_Full |
2423 ADVERTISED_10baseT_Full;
2424 else
2425 advertising |= ADVERTISED_10baseT_Full;
2426 }
2427
2428 phydev->advertising = advertising;
2429
2430 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002431
2432 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
2433 if (phyid != TG3_PHY_ID_BCMAC131) {
2434 phyid &= TG3_PHY_OUI_MASK;
Roel Kluinf72b5342009-02-18 17:42:42 -08002435 if (phyid == TG3_PHY_OUI_1 ||
2436 phyid == TG3_PHY_OUI_2 ||
Matt Carlson0a459aa2008-11-03 16:54:15 -08002437 phyid == TG3_PHY_OUI_3)
2438 do_low_power = true;
2439 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002440 }
Matt Carlsondd477002008-05-25 23:45:58 -07002441 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002442 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002443
Matt Carlsondd477002008-05-25 23:45:58 -07002444 if (tp->link_config.phy_is_low_power == 0) {
2445 tp->link_config.phy_is_low_power = 1;
2446 tp->link_config.orig_speed = tp->link_config.speed;
2447 tp->link_config.orig_duplex = tp->link_config.duplex;
2448 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Matt Carlsondd477002008-05-25 23:45:58 -07002451 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
2452 tp->link_config.speed = SPEED_10;
2453 tp->link_config.duplex = DUPLEX_HALF;
2454 tp->link_config.autoneg = AUTONEG_ENABLE;
2455 tg3_setup_phy(tp, 0);
2456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
2458
Michael Chanb5d37722006-09-27 16:06:21 -07002459 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2460 u32 val;
2461
2462 val = tr32(GRC_VCPU_EXT_CTRL);
2463 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2464 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002465 int i;
2466 u32 val;
2467
2468 for (i = 0; i < 200; i++) {
2469 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2470 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2471 break;
2472 msleep(1);
2473 }
2474 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002475 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2476 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2477 WOL_DRV_STATE_SHUTDOWN |
2478 WOL_DRV_WOL |
2479 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002480
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002481 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 u32 mac_mode;
2483
2484 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002485 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002486 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2487 udelay(40);
2488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Michael Chan3f7045c2006-09-27 16:02:29 -07002490 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
2491 mac_mode = MAC_MODE_PORT_MODE_GMII;
2492 else
2493 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002495 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2496 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2497 ASIC_REV_5700) {
2498 u32 speed = (tp->tg3_flags &
2499 TG3_FLAG_WOL_SPEED_100MB) ?
2500 SPEED_100 : SPEED_10;
2501 if (tg3_5700_link_polarity(tp, speed))
2502 mac_mode |= MAC_MODE_LINK_POLARITY;
2503 else
2504 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 } else {
2507 mac_mode = MAC_MODE_PORT_MODE_TBI;
2508 }
2509
John W. Linvillecbf46852005-04-21 17:01:29 -07002510 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 tw32(MAC_LED_CTRL, tp->led_ctrl);
2512
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002513 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2514 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2515 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2516 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2517 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2518 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Matt Carlson3bda1252008-08-15 14:08:22 -07002520 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
2521 mac_mode |= tp->mac_mode &
2522 (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
2523 if (mac_mode & MAC_MODE_APE_TX_EN)
2524 mac_mode |= MAC_MODE_TDE_ENABLE;
2525 }
2526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 tw32_f(MAC_MODE, mac_mode);
2528 udelay(100);
2529
2530 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2531 udelay(10);
2532 }
2533
2534 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2535 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2536 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2537 u32 base_val;
2538
2539 base_val = tp->pci_clock_ctrl;
2540 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2541 CLOCK_CTRL_TXCLK_DISABLE);
2542
Michael Chanb401e9e2005-12-19 16:27:04 -08002543 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2544 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002545 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002546 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002547 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002548 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002549 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2551 u32 newbits1, newbits2;
2552
2553 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2554 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2555 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2556 CLOCK_CTRL_TXCLK_DISABLE |
2557 CLOCK_CTRL_ALTCLK);
2558 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2559 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2560 newbits1 = CLOCK_CTRL_625_CORE;
2561 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2562 } else {
2563 newbits1 = CLOCK_CTRL_ALTCLK;
2564 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2565 }
2566
Michael Chanb401e9e2005-12-19 16:27:04 -08002567 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2568 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Michael Chanb401e9e2005-12-19 16:27:04 -08002570 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2571 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
2573 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2574 u32 newbits3;
2575
2576 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2577 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2578 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2579 CLOCK_CTRL_TXCLK_DISABLE |
2580 CLOCK_CTRL_44MHZ_CORE);
2581 } else {
2582 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2583 }
2584
Michael Chanb401e9e2005-12-19 16:27:04 -08002585 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2586 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 }
2588 }
2589
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002590 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002591 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002592 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 tg3_frob_aux_power(tp);
2595
2596 /* Workaround for unstable PLL clock */
2597 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2598 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2599 u32 val = tr32(0x7d00);
2600
2601 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2602 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002603 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002604 int err;
2605
2606 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002608 if (!err)
2609 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 }
2612
Michael Chanbbadf502006-04-06 21:46:34 -07002613 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2614
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002615 if (device_should_wake)
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002616 pci_enable_wake(tp->pdev, state, true);
2617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 /* Finally, set the new power state. */
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002619 pci_set_power_state(tp->pdev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 return 0;
2622}
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2625{
2626 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2627 case MII_TG3_AUX_STAT_10HALF:
2628 *speed = SPEED_10;
2629 *duplex = DUPLEX_HALF;
2630 break;
2631
2632 case MII_TG3_AUX_STAT_10FULL:
2633 *speed = SPEED_10;
2634 *duplex = DUPLEX_FULL;
2635 break;
2636
2637 case MII_TG3_AUX_STAT_100HALF:
2638 *speed = SPEED_100;
2639 *duplex = DUPLEX_HALF;
2640 break;
2641
2642 case MII_TG3_AUX_STAT_100FULL:
2643 *speed = SPEED_100;
2644 *duplex = DUPLEX_FULL;
2645 break;
2646
2647 case MII_TG3_AUX_STAT_1000HALF:
2648 *speed = SPEED_1000;
2649 *duplex = DUPLEX_HALF;
2650 break;
2651
2652 case MII_TG3_AUX_STAT_1000FULL:
2653 *speed = SPEED_1000;
2654 *duplex = DUPLEX_FULL;
2655 break;
2656
2657 default:
Michael Chan715116a2006-09-27 16:09:25 -07002658 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2659 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2660 SPEED_10;
2661 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2662 DUPLEX_HALF;
2663 break;
2664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 *speed = SPEED_INVALID;
2666 *duplex = DUPLEX_INVALID;
2667 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669}
2670
2671static void tg3_phy_copper_begin(struct tg3 *tp)
2672{
2673 u32 new_adv;
2674 int i;
2675
2676 if (tp->link_config.phy_is_low_power) {
2677 /* Entering low power mode. Disable gigabit and
2678 * 100baseT advertisements.
2679 */
2680 tg3_writephy(tp, MII_TG3_CTRL, 0);
2681
2682 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2683 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2684 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2685 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2686
2687 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2688 } else if (tp->link_config.speed == SPEED_INVALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
2690 tp->link_config.advertising &=
2691 ~(ADVERTISED_1000baseT_Half |
2692 ADVERTISED_1000baseT_Full);
2693
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002694 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2696 new_adv |= ADVERTISE_10HALF;
2697 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2698 new_adv |= ADVERTISE_10FULL;
2699 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2700 new_adv |= ADVERTISE_100HALF;
2701 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2702 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002703
2704 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2707
2708 if (tp->link_config.advertising &
2709 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2710 new_adv = 0;
2711 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2712 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2713 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2714 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
2715 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY) &&
2716 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2717 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2718 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2719 MII_TG3_CTRL_ENABLE_AS_MASTER);
2720 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2721 } else {
2722 tg3_writephy(tp, MII_TG3_CTRL, 0);
2723 }
2724 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002725 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2726 new_adv |= ADVERTISE_CSMA;
2727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 /* Asking for a specific link mode. */
2729 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2731
2732 if (tp->link_config.duplex == DUPLEX_FULL)
2733 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2734 else
2735 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2736 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2737 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2738 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2739 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 if (tp->link_config.speed == SPEED_100) {
2742 if (tp->link_config.duplex == DUPLEX_FULL)
2743 new_adv |= ADVERTISE_100FULL;
2744 else
2745 new_adv |= ADVERTISE_100HALF;
2746 } else {
2747 if (tp->link_config.duplex == DUPLEX_FULL)
2748 new_adv |= ADVERTISE_10FULL;
2749 else
2750 new_adv |= ADVERTISE_10HALF;
2751 }
2752 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002753
2754 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002756
2757 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 }
2759
2760 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
2761 tp->link_config.speed != SPEED_INVALID) {
2762 u32 bmcr, orig_bmcr;
2763
2764 tp->link_config.active_speed = tp->link_config.speed;
2765 tp->link_config.active_duplex = tp->link_config.duplex;
2766
2767 bmcr = 0;
2768 switch (tp->link_config.speed) {
2769 default:
2770 case SPEED_10:
2771 break;
2772
2773 case SPEED_100:
2774 bmcr |= BMCR_SPEED100;
2775 break;
2776
2777 case SPEED_1000:
2778 bmcr |= TG3_BMCR_SPEED1000;
2779 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
2782 if (tp->link_config.duplex == DUPLEX_FULL)
2783 bmcr |= BMCR_FULLDPLX;
2784
2785 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
2786 (bmcr != orig_bmcr)) {
2787 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
2788 for (i = 0; i < 1500; i++) {
2789 u32 tmp;
2790
2791 udelay(10);
2792 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
2793 tg3_readphy(tp, MII_BMSR, &tmp))
2794 continue;
2795 if (!(tmp & BMSR_LSTATUS)) {
2796 udelay(40);
2797 break;
2798 }
2799 }
2800 tg3_writephy(tp, MII_BMCR, bmcr);
2801 udelay(40);
2802 }
2803 } else {
2804 tg3_writephy(tp, MII_BMCR,
2805 BMCR_ANENABLE | BMCR_ANRESTART);
2806 }
2807}
2808
2809static int tg3_init_5401phy_dsp(struct tg3 *tp)
2810{
2811 int err;
2812
2813 /* Turn off tap power management. */
2814 /* Set Extended packet length bit */
2815 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
2816
2817 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0012);
2818 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1804);
2819
2820 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0013);
2821 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1204);
2822
2823 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2824 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0132);
2825
2826 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2827 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0232);
2828
2829 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2830 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0a20);
2831
2832 udelay(40);
2833
2834 return err;
2835}
2836
Michael Chan3600d912006-12-07 00:21:48 -08002837static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838{
Michael Chan3600d912006-12-07 00:21:48 -08002839 u32 adv_reg, all_mask = 0;
2840
2841 if (mask & ADVERTISED_10baseT_Half)
2842 all_mask |= ADVERTISE_10HALF;
2843 if (mask & ADVERTISED_10baseT_Full)
2844 all_mask |= ADVERTISE_10FULL;
2845 if (mask & ADVERTISED_100baseT_Half)
2846 all_mask |= ADVERTISE_100HALF;
2847 if (mask & ADVERTISED_100baseT_Full)
2848 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
2851 return 0;
2852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 if ((adv_reg & all_mask) != all_mask)
2854 return 0;
2855 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
2856 u32 tg3_ctrl;
2857
Michael Chan3600d912006-12-07 00:21:48 -08002858 all_mask = 0;
2859 if (mask & ADVERTISED_1000baseT_Half)
2860 all_mask |= ADVERTISE_1000HALF;
2861 if (mask & ADVERTISED_1000baseT_Full)
2862 all_mask |= ADVERTISE_1000FULL;
2863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
2865 return 0;
2866
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 if ((tg3_ctrl & all_mask) != all_mask)
2868 return 0;
2869 }
2870 return 1;
2871}
2872
Matt Carlsonef167e22007-12-20 20:10:01 -08002873static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
2874{
2875 u32 curadv, reqadv;
2876
2877 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
2878 return 1;
2879
2880 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
2881 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2882
2883 if (tp->link_config.active_duplex == DUPLEX_FULL) {
2884 if (curadv != reqadv)
2885 return 0;
2886
2887 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
2888 tg3_readphy(tp, MII_LPA, rmtadv);
2889 } else {
2890 /* Reprogram the advertisement register, even if it
2891 * does not affect the current link. If the link
2892 * gets renegotiated in the future, we can save an
2893 * additional renegotiation cycle by advertising
2894 * it correctly in the first place.
2895 */
2896 if (curadv != reqadv) {
2897 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
2898 ADVERTISE_PAUSE_ASYM);
2899 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
2900 }
2901 }
2902
2903 return 1;
2904}
2905
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
2907{
2908 int current_link_up;
2909 u32 bmsr, dummy;
Matt Carlsonef167e22007-12-20 20:10:01 -08002910 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 u16 current_speed;
2912 u8 current_duplex;
2913 int i, err;
2914
2915 tw32(MAC_EVENT, 0);
2916
2917 tw32_f(MAC_STATUS,
2918 (MAC_STATUS_SYNC_CHANGED |
2919 MAC_STATUS_CFG_CHANGED |
2920 MAC_STATUS_MI_COMPLETION |
2921 MAC_STATUS_LNKSTATE_CHANGED));
2922 udelay(40);
2923
Matt Carlson8ef21422008-05-02 16:47:53 -07002924 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
2925 tw32_f(MAC_MI_MODE,
2926 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
2927 udelay(80);
2928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
2930 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
2931
2932 /* Some third-party PHYs need to be reset on link going
2933 * down.
2934 */
2935 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2936 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2937 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
2938 netif_carrier_ok(tp->dev)) {
2939 tg3_readphy(tp, MII_BMSR, &bmsr);
2940 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
2941 !(bmsr & BMSR_LSTATUS))
2942 force_reset = 1;
2943 }
2944 if (force_reset)
2945 tg3_phy_reset(tp);
2946
2947 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
2948 tg3_readphy(tp, MII_BMSR, &bmsr);
2949 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
2950 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
2951 bmsr = 0;
2952
2953 if (!(bmsr & BMSR_LSTATUS)) {
2954 err = tg3_init_5401phy_dsp(tp);
2955 if (err)
2956 return err;
2957
2958 tg3_readphy(tp, MII_BMSR, &bmsr);
2959 for (i = 0; i < 1000; i++) {
2960 udelay(10);
2961 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
2962 (bmsr & BMSR_LSTATUS)) {
2963 udelay(40);
2964 break;
2965 }
2966 }
2967
2968 if ((tp->phy_id & PHY_ID_REV_MASK) == PHY_REV_BCM5401_B0 &&
2969 !(bmsr & BMSR_LSTATUS) &&
2970 tp->link_config.active_speed == SPEED_1000) {
2971 err = tg3_phy_reset(tp);
2972 if (!err)
2973 err = tg3_init_5401phy_dsp(tp);
2974 if (err)
2975 return err;
2976 }
2977 }
2978 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2979 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
2980 /* 5701 {A0,B0} CRC bug workaround */
2981 tg3_writephy(tp, 0x15, 0x0a75);
2982 tg3_writephy(tp, 0x1c, 0x8c68);
2983 tg3_writephy(tp, 0x1c, 0x8d68);
2984 tg3_writephy(tp, 0x1c, 0x8c68);
2985 }
2986
2987 /* Clear pending interrupts... */
2988 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
2989 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
2990
2991 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT)
2992 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Michael Chan715116a2006-09-27 16:09:25 -07002993 else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5906)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 tg3_writephy(tp, MII_TG3_IMASK, ~0);
2995
2996 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2997 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2998 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
2999 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3000 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3001 else
3002 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3003 }
3004
3005 current_link_up = 0;
3006 current_speed = SPEED_INVALID;
3007 current_duplex = DUPLEX_INVALID;
3008
3009 if (tp->tg3_flags2 & TG3_FLG2_CAPACITIVE_COUPLING) {
3010 u32 val;
3011
3012 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3013 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3014 if (!(val & (1 << 10))) {
3015 val |= (1 << 10);
3016 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3017 goto relink;
3018 }
3019 }
3020
3021 bmsr = 0;
3022 for (i = 0; i < 100; i++) {
3023 tg3_readphy(tp, MII_BMSR, &bmsr);
3024 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3025 (bmsr & BMSR_LSTATUS))
3026 break;
3027 udelay(40);
3028 }
3029
3030 if (bmsr & BMSR_LSTATUS) {
3031 u32 aux_stat, bmcr;
3032
3033 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3034 for (i = 0; i < 2000; i++) {
3035 udelay(10);
3036 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3037 aux_stat)
3038 break;
3039 }
3040
3041 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3042 &current_speed,
3043 &current_duplex);
3044
3045 bmcr = 0;
3046 for (i = 0; i < 200; i++) {
3047 tg3_readphy(tp, MII_BMCR, &bmcr);
3048 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3049 continue;
3050 if (bmcr && bmcr != 0x7fff)
3051 break;
3052 udelay(10);
3053 }
3054
Matt Carlsonef167e22007-12-20 20:10:01 -08003055 lcl_adv = 0;
3056 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Matt Carlsonef167e22007-12-20 20:10:01 -08003058 tp->link_config.active_speed = current_speed;
3059 tp->link_config.active_duplex = current_duplex;
3060
3061 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3062 if ((bmcr & BMCR_ANENABLE) &&
3063 tg3_copper_is_advertising_all(tp,
3064 tp->link_config.advertising)) {
3065 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3066 &rmt_adv))
3067 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 }
3069 } else {
3070 if (!(bmcr & BMCR_ANENABLE) &&
3071 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003072 tp->link_config.duplex == current_duplex &&
3073 tp->link_config.flowctrl ==
3074 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 }
3077 }
3078
Matt Carlsonef167e22007-12-20 20:10:01 -08003079 if (current_link_up == 1 &&
3080 tp->link_config.active_duplex == DUPLEX_FULL)
3081 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
3083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084relink:
Michael Chan6921d202005-12-13 21:15:53 -08003085 if (current_link_up == 0 || tp->link_config.phy_is_low_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 u32 tmp;
3087
3088 tg3_phy_copper_begin(tp);
3089
3090 tg3_readphy(tp, MII_BMSR, &tmp);
3091 if (!tg3_readphy(tp, MII_BMSR, &tmp) &&
3092 (tmp & BMSR_LSTATUS))
3093 current_link_up = 1;
3094 }
3095
3096 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3097 if (current_link_up == 1) {
3098 if (tp->link_config.active_speed == SPEED_100 ||
3099 tp->link_config.active_speed == SPEED_10)
3100 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3101 else
3102 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3103 } else
3104 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3105
3106 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3107 if (tp->link_config.active_duplex == DUPLEX_HALF)
3108 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003111 if (current_link_up == 1 &&
3112 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003114 else
3115 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 }
3117
3118 /* ??? Without this setting Netgear GA302T PHY does not
3119 * ??? send/receive packets...
3120 */
3121 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411 &&
3122 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3123 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3124 tw32_f(MAC_MI_MODE, tp->mi_mode);
3125 udelay(80);
3126 }
3127
3128 tw32_f(MAC_MODE, tp->mac_mode);
3129 udelay(40);
3130
3131 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3132 /* Polled via timer. */
3133 tw32_f(MAC_EVENT, 0);
3134 } else {
3135 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3136 }
3137 udelay(40);
3138
3139 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3140 current_link_up == 1 &&
3141 tp->link_config.active_speed == SPEED_1000 &&
3142 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3143 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3144 udelay(120);
3145 tw32_f(MAC_STATUS,
3146 (MAC_STATUS_SYNC_CHANGED |
3147 MAC_STATUS_CFG_CHANGED));
3148 udelay(40);
3149 tg3_write_mem(tp,
3150 NIC_SRAM_FIRMWARE_MBOX,
3151 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3152 }
3153
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003154 /* Prevent send BD corruption. */
3155 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3156 u16 oldlnkctl, newlnkctl;
3157
3158 pci_read_config_word(tp->pdev,
3159 tp->pcie_cap + PCI_EXP_LNKCTL,
3160 &oldlnkctl);
3161 if (tp->link_config.active_speed == SPEED_100 ||
3162 tp->link_config.active_speed == SPEED_10)
3163 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3164 else
3165 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3166 if (newlnkctl != oldlnkctl)
3167 pci_write_config_word(tp->pdev,
3168 tp->pcie_cap + PCI_EXP_LNKCTL,
3169 newlnkctl);
3170 }
3171
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 if (current_link_up != netif_carrier_ok(tp->dev)) {
3173 if (current_link_up)
3174 netif_carrier_on(tp->dev);
3175 else
3176 netif_carrier_off(tp->dev);
3177 tg3_link_report(tp);
3178 }
3179
3180 return 0;
3181}
3182
3183struct tg3_fiber_aneginfo {
3184 int state;
3185#define ANEG_STATE_UNKNOWN 0
3186#define ANEG_STATE_AN_ENABLE 1
3187#define ANEG_STATE_RESTART_INIT 2
3188#define ANEG_STATE_RESTART 3
3189#define ANEG_STATE_DISABLE_LINK_OK 4
3190#define ANEG_STATE_ABILITY_DETECT_INIT 5
3191#define ANEG_STATE_ABILITY_DETECT 6
3192#define ANEG_STATE_ACK_DETECT_INIT 7
3193#define ANEG_STATE_ACK_DETECT 8
3194#define ANEG_STATE_COMPLETE_ACK_INIT 9
3195#define ANEG_STATE_COMPLETE_ACK 10
3196#define ANEG_STATE_IDLE_DETECT_INIT 11
3197#define ANEG_STATE_IDLE_DETECT 12
3198#define ANEG_STATE_LINK_OK 13
3199#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3200#define ANEG_STATE_NEXT_PAGE_WAIT 15
3201
3202 u32 flags;
3203#define MR_AN_ENABLE 0x00000001
3204#define MR_RESTART_AN 0x00000002
3205#define MR_AN_COMPLETE 0x00000004
3206#define MR_PAGE_RX 0x00000008
3207#define MR_NP_LOADED 0x00000010
3208#define MR_TOGGLE_TX 0x00000020
3209#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3210#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3211#define MR_LP_ADV_SYM_PAUSE 0x00000100
3212#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3213#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3214#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3215#define MR_LP_ADV_NEXT_PAGE 0x00001000
3216#define MR_TOGGLE_RX 0x00002000
3217#define MR_NP_RX 0x00004000
3218
3219#define MR_LINK_OK 0x80000000
3220
3221 unsigned long link_time, cur_time;
3222
3223 u32 ability_match_cfg;
3224 int ability_match_count;
3225
3226 char ability_match, idle_match, ack_match;
3227
3228 u32 txconfig, rxconfig;
3229#define ANEG_CFG_NP 0x00000080
3230#define ANEG_CFG_ACK 0x00000040
3231#define ANEG_CFG_RF2 0x00000020
3232#define ANEG_CFG_RF1 0x00000010
3233#define ANEG_CFG_PS2 0x00000001
3234#define ANEG_CFG_PS1 0x00008000
3235#define ANEG_CFG_HD 0x00004000
3236#define ANEG_CFG_FD 0x00002000
3237#define ANEG_CFG_INVAL 0x00001f06
3238
3239};
3240#define ANEG_OK 0
3241#define ANEG_DONE 1
3242#define ANEG_TIMER_ENAB 2
3243#define ANEG_FAILED -1
3244
3245#define ANEG_STATE_SETTLE_TIME 10000
3246
3247static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3248 struct tg3_fiber_aneginfo *ap)
3249{
Matt Carlson5be73b42007-12-20 20:09:29 -08003250 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 unsigned long delta;
3252 u32 rx_cfg_reg;
3253 int ret;
3254
3255 if (ap->state == ANEG_STATE_UNKNOWN) {
3256 ap->rxconfig = 0;
3257 ap->link_time = 0;
3258 ap->cur_time = 0;
3259 ap->ability_match_cfg = 0;
3260 ap->ability_match_count = 0;
3261 ap->ability_match = 0;
3262 ap->idle_match = 0;
3263 ap->ack_match = 0;
3264 }
3265 ap->cur_time++;
3266
3267 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3268 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3269
3270 if (rx_cfg_reg != ap->ability_match_cfg) {
3271 ap->ability_match_cfg = rx_cfg_reg;
3272 ap->ability_match = 0;
3273 ap->ability_match_count = 0;
3274 } else {
3275 if (++ap->ability_match_count > 1) {
3276 ap->ability_match = 1;
3277 ap->ability_match_cfg = rx_cfg_reg;
3278 }
3279 }
3280 if (rx_cfg_reg & ANEG_CFG_ACK)
3281 ap->ack_match = 1;
3282 else
3283 ap->ack_match = 0;
3284
3285 ap->idle_match = 0;
3286 } else {
3287 ap->idle_match = 1;
3288 ap->ability_match_cfg = 0;
3289 ap->ability_match_count = 0;
3290 ap->ability_match = 0;
3291 ap->ack_match = 0;
3292
3293 rx_cfg_reg = 0;
3294 }
3295
3296 ap->rxconfig = rx_cfg_reg;
3297 ret = ANEG_OK;
3298
3299 switch(ap->state) {
3300 case ANEG_STATE_UNKNOWN:
3301 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3302 ap->state = ANEG_STATE_AN_ENABLE;
3303
3304 /* fallthru */
3305 case ANEG_STATE_AN_ENABLE:
3306 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3307 if (ap->flags & MR_AN_ENABLE) {
3308 ap->link_time = 0;
3309 ap->cur_time = 0;
3310 ap->ability_match_cfg = 0;
3311 ap->ability_match_count = 0;
3312 ap->ability_match = 0;
3313 ap->idle_match = 0;
3314 ap->ack_match = 0;
3315
3316 ap->state = ANEG_STATE_RESTART_INIT;
3317 } else {
3318 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3319 }
3320 break;
3321
3322 case ANEG_STATE_RESTART_INIT:
3323 ap->link_time = ap->cur_time;
3324 ap->flags &= ~(MR_NP_LOADED);
3325 ap->txconfig = 0;
3326 tw32(MAC_TX_AUTO_NEG, 0);
3327 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3328 tw32_f(MAC_MODE, tp->mac_mode);
3329 udelay(40);
3330
3331 ret = ANEG_TIMER_ENAB;
3332 ap->state = ANEG_STATE_RESTART;
3333
3334 /* fallthru */
3335 case ANEG_STATE_RESTART:
3336 delta = ap->cur_time - ap->link_time;
3337 if (delta > ANEG_STATE_SETTLE_TIME) {
3338 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
3339 } else {
3340 ret = ANEG_TIMER_ENAB;
3341 }
3342 break;
3343
3344 case ANEG_STATE_DISABLE_LINK_OK:
3345 ret = ANEG_DONE;
3346 break;
3347
3348 case ANEG_STATE_ABILITY_DETECT_INIT:
3349 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003350 ap->txconfig = ANEG_CFG_FD;
3351 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3352 if (flowctrl & ADVERTISE_1000XPAUSE)
3353 ap->txconfig |= ANEG_CFG_PS1;
3354 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3355 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3357 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3358 tw32_f(MAC_MODE, tp->mac_mode);
3359 udelay(40);
3360
3361 ap->state = ANEG_STATE_ABILITY_DETECT;
3362 break;
3363
3364 case ANEG_STATE_ABILITY_DETECT:
3365 if (ap->ability_match != 0 && ap->rxconfig != 0) {
3366 ap->state = ANEG_STATE_ACK_DETECT_INIT;
3367 }
3368 break;
3369
3370 case ANEG_STATE_ACK_DETECT_INIT:
3371 ap->txconfig |= ANEG_CFG_ACK;
3372 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3373 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3374 tw32_f(MAC_MODE, tp->mac_mode);
3375 udelay(40);
3376
3377 ap->state = ANEG_STATE_ACK_DETECT;
3378
3379 /* fallthru */
3380 case ANEG_STATE_ACK_DETECT:
3381 if (ap->ack_match != 0) {
3382 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3383 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3384 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3385 } else {
3386 ap->state = ANEG_STATE_AN_ENABLE;
3387 }
3388 } else if (ap->ability_match != 0 &&
3389 ap->rxconfig == 0) {
3390 ap->state = ANEG_STATE_AN_ENABLE;
3391 }
3392 break;
3393
3394 case ANEG_STATE_COMPLETE_ACK_INIT:
3395 if (ap->rxconfig & ANEG_CFG_INVAL) {
3396 ret = ANEG_FAILED;
3397 break;
3398 }
3399 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3400 MR_LP_ADV_HALF_DUPLEX |
3401 MR_LP_ADV_SYM_PAUSE |
3402 MR_LP_ADV_ASYM_PAUSE |
3403 MR_LP_ADV_REMOTE_FAULT1 |
3404 MR_LP_ADV_REMOTE_FAULT2 |
3405 MR_LP_ADV_NEXT_PAGE |
3406 MR_TOGGLE_RX |
3407 MR_NP_RX);
3408 if (ap->rxconfig & ANEG_CFG_FD)
3409 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3410 if (ap->rxconfig & ANEG_CFG_HD)
3411 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3412 if (ap->rxconfig & ANEG_CFG_PS1)
3413 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3414 if (ap->rxconfig & ANEG_CFG_PS2)
3415 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3416 if (ap->rxconfig & ANEG_CFG_RF1)
3417 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3418 if (ap->rxconfig & ANEG_CFG_RF2)
3419 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3420 if (ap->rxconfig & ANEG_CFG_NP)
3421 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3422
3423 ap->link_time = ap->cur_time;
3424
3425 ap->flags ^= (MR_TOGGLE_TX);
3426 if (ap->rxconfig & 0x0008)
3427 ap->flags |= MR_TOGGLE_RX;
3428 if (ap->rxconfig & ANEG_CFG_NP)
3429 ap->flags |= MR_NP_RX;
3430 ap->flags |= MR_PAGE_RX;
3431
3432 ap->state = ANEG_STATE_COMPLETE_ACK;
3433 ret = ANEG_TIMER_ENAB;
3434 break;
3435
3436 case ANEG_STATE_COMPLETE_ACK:
3437 if (ap->ability_match != 0 &&
3438 ap->rxconfig == 0) {
3439 ap->state = ANEG_STATE_AN_ENABLE;
3440 break;
3441 }
3442 delta = ap->cur_time - ap->link_time;
3443 if (delta > ANEG_STATE_SETTLE_TIME) {
3444 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3445 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3446 } else {
3447 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3448 !(ap->flags & MR_NP_RX)) {
3449 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3450 } else {
3451 ret = ANEG_FAILED;
3452 }
3453 }
3454 }
3455 break;
3456
3457 case ANEG_STATE_IDLE_DETECT_INIT:
3458 ap->link_time = ap->cur_time;
3459 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3460 tw32_f(MAC_MODE, tp->mac_mode);
3461 udelay(40);
3462
3463 ap->state = ANEG_STATE_IDLE_DETECT;
3464 ret = ANEG_TIMER_ENAB;
3465 break;
3466
3467 case ANEG_STATE_IDLE_DETECT:
3468 if (ap->ability_match != 0 &&
3469 ap->rxconfig == 0) {
3470 ap->state = ANEG_STATE_AN_ENABLE;
3471 break;
3472 }
3473 delta = ap->cur_time - ap->link_time;
3474 if (delta > ANEG_STATE_SETTLE_TIME) {
3475 /* XXX another gem from the Broadcom driver :( */
3476 ap->state = ANEG_STATE_LINK_OK;
3477 }
3478 break;
3479
3480 case ANEG_STATE_LINK_OK:
3481 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3482 ret = ANEG_DONE;
3483 break;
3484
3485 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3486 /* ??? unimplemented */
3487 break;
3488
3489 case ANEG_STATE_NEXT_PAGE_WAIT:
3490 /* ??? unimplemented */
3491 break;
3492
3493 default:
3494 ret = ANEG_FAILED;
3495 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
3498 return ret;
3499}
3500
Matt Carlson5be73b42007-12-20 20:09:29 -08003501static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502{
3503 int res = 0;
3504 struct tg3_fiber_aneginfo aninfo;
3505 int status = ANEG_FAILED;
3506 unsigned int tick;
3507 u32 tmp;
3508
3509 tw32_f(MAC_TX_AUTO_NEG, 0);
3510
3511 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3512 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3513 udelay(40);
3514
3515 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3516 udelay(40);
3517
3518 memset(&aninfo, 0, sizeof(aninfo));
3519 aninfo.flags |= MR_AN_ENABLE;
3520 aninfo.state = ANEG_STATE_UNKNOWN;
3521 aninfo.cur_time = 0;
3522 tick = 0;
3523 while (++tick < 195000) {
3524 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3525 if (status == ANEG_DONE || status == ANEG_FAILED)
3526 break;
3527
3528 udelay(1);
3529 }
3530
3531 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3532 tw32_f(MAC_MODE, tp->mac_mode);
3533 udelay(40);
3534
Matt Carlson5be73b42007-12-20 20:09:29 -08003535 *txflags = aninfo.txconfig;
3536 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
3538 if (status == ANEG_DONE &&
3539 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3540 MR_LP_ADV_FULL_DUPLEX)))
3541 res = 1;
3542
3543 return res;
3544}
3545
3546static void tg3_init_bcm8002(struct tg3 *tp)
3547{
3548 u32 mac_status = tr32(MAC_STATUS);
3549 int i;
3550
3551 /* Reset when initting first time or we have a link. */
3552 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3553 !(mac_status & MAC_STATUS_PCS_SYNCED))
3554 return;
3555
3556 /* Set PLL lock range. */
3557 tg3_writephy(tp, 0x16, 0x8007);
3558
3559 /* SW reset */
3560 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3561
3562 /* Wait for reset to complete. */
3563 /* XXX schedule_timeout() ... */
3564 for (i = 0; i < 500; i++)
3565 udelay(10);
3566
3567 /* Config mode; select PMA/Ch 1 regs. */
3568 tg3_writephy(tp, 0x10, 0x8411);
3569
3570 /* Enable auto-lock and comdet, select txclk for tx. */
3571 tg3_writephy(tp, 0x11, 0x0a10);
3572
3573 tg3_writephy(tp, 0x18, 0x00a0);
3574 tg3_writephy(tp, 0x16, 0x41ff);
3575
3576 /* Assert and deassert POR. */
3577 tg3_writephy(tp, 0x13, 0x0400);
3578 udelay(40);
3579 tg3_writephy(tp, 0x13, 0x0000);
3580
3581 tg3_writephy(tp, 0x11, 0x0a50);
3582 udelay(40);
3583 tg3_writephy(tp, 0x11, 0x0a10);
3584
3585 /* Wait for signal to stabilize */
3586 /* XXX schedule_timeout() ... */
3587 for (i = 0; i < 15000; i++)
3588 udelay(10);
3589
3590 /* Deselect the channel register so we can read the PHYID
3591 * later.
3592 */
3593 tg3_writephy(tp, 0x10, 0x8011);
3594}
3595
3596static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3597{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003598 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 u32 sg_dig_ctrl, sg_dig_status;
3600 u32 serdes_cfg, expected_sg_dig_ctrl;
3601 int workaround, port_a;
3602 int current_link_up;
3603
3604 serdes_cfg = 0;
3605 expected_sg_dig_ctrl = 0;
3606 workaround = 0;
3607 port_a = 1;
3608 current_link_up = 0;
3609
3610 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3611 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3612 workaround = 1;
3613 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3614 port_a = 0;
3615
3616 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3617 /* preserve bits 20-23 for voltage regulator */
3618 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3619 }
3620
3621 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3622
3623 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003624 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 if (workaround) {
3626 u32 val = serdes_cfg;
3627
3628 if (port_a)
3629 val |= 0xc010000;
3630 else
3631 val |= 0x4010000;
3632 tw32_f(MAC_SERDES_CFG, val);
3633 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003634
3635 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 }
3637 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3638 tg3_setup_flow_control(tp, 0, 0);
3639 current_link_up = 1;
3640 }
3641 goto out;
3642 }
3643
3644 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003645 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Matt Carlson82cd3d12007-12-20 20:09:00 -08003647 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3648 if (flowctrl & ADVERTISE_1000XPAUSE)
3649 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3650 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3651 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
3653 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003654 if ((tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT) &&
3655 tp->serdes_counter &&
3656 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3657 MAC_STATUS_RCVD_CFG)) ==
3658 MAC_STATUS_PCS_SYNCED)) {
3659 tp->serdes_counter--;
3660 current_link_up = 1;
3661 goto out;
3662 }
3663restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 if (workaround)
3665 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003666 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 udelay(5);
3668 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3669
Michael Chan3d3ebe72006-09-27 15:59:15 -07003670 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3671 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3673 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003674 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 mac_status = tr32(MAC_STATUS);
3676
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003677 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003679 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Matt Carlson82cd3d12007-12-20 20:09:00 -08003681 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3682 local_adv |= ADVERTISE_1000XPAUSE;
3683 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3684 local_adv |= ADVERTISE_1000XPSE_ASYM;
3685
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003686 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003687 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003688 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003689 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690
3691 tg3_setup_flow_control(tp, local_adv, remote_adv);
3692 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003693 tp->serdes_counter = 0;
3694 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003695 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003696 if (tp->serdes_counter)
3697 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 else {
3699 if (workaround) {
3700 u32 val = serdes_cfg;
3701
3702 if (port_a)
3703 val |= 0xc010000;
3704 else
3705 val |= 0x4010000;
3706
3707 tw32_f(MAC_SERDES_CFG, val);
3708 }
3709
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003710 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 udelay(40);
3712
3713 /* Link parallel detection - link is up */
3714 /* only if we have PCS_SYNC and not */
3715 /* receiving config code words */
3716 mac_status = tr32(MAC_STATUS);
3717 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3718 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3719 tg3_setup_flow_control(tp, 0, 0);
3720 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003721 tp->tg3_flags2 |=
3722 TG3_FLG2_PARALLEL_DETECT;
3723 tp->serdes_counter =
3724 SERDES_PARALLEL_DET_TIMEOUT;
3725 } else
3726 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 }
3728 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003729 } else {
3730 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3731 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 }
3733
3734out:
3735 return current_link_up;
3736}
3737
3738static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3739{
3740 int current_link_up = 0;
3741
Michael Chan5cf64b8a2007-05-05 12:11:21 -07003742 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
3745 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003746 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003748
Matt Carlson5be73b42007-12-20 20:09:29 -08003749 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3750 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
Matt Carlson5be73b42007-12-20 20:09:29 -08003752 if (txflags & ANEG_CFG_PS1)
3753 local_adv |= ADVERTISE_1000XPAUSE;
3754 if (txflags & ANEG_CFG_PS2)
3755 local_adv |= ADVERTISE_1000XPSE_ASYM;
3756
3757 if (rxflags & MR_LP_ADV_SYM_PAUSE)
3758 remote_adv |= LPA_1000XPAUSE;
3759 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3760 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
3762 tg3_setup_flow_control(tp, local_adv, remote_adv);
3763
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 current_link_up = 1;
3765 }
3766 for (i = 0; i < 30; i++) {
3767 udelay(20);
3768 tw32_f(MAC_STATUS,
3769 (MAC_STATUS_SYNC_CHANGED |
3770 MAC_STATUS_CFG_CHANGED));
3771 udelay(40);
3772 if ((tr32(MAC_STATUS) &
3773 (MAC_STATUS_SYNC_CHANGED |
3774 MAC_STATUS_CFG_CHANGED)) == 0)
3775 break;
3776 }
3777
3778 mac_status = tr32(MAC_STATUS);
3779 if (current_link_up == 0 &&
3780 (mac_status & MAC_STATUS_PCS_SYNCED) &&
3781 !(mac_status & MAC_STATUS_RCVD_CFG))
3782 current_link_up = 1;
3783 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08003784 tg3_setup_flow_control(tp, 0, 0);
3785
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 /* Forcing 1000FD link up. */
3787 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
3789 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
3790 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003791
3792 tw32_f(MAC_MODE, tp->mac_mode);
3793 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 }
3795
3796out:
3797 return current_link_up;
3798}
3799
3800static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
3801{
3802 u32 orig_pause_cfg;
3803 u16 orig_active_speed;
3804 u8 orig_active_duplex;
3805 u32 mac_status;
3806 int current_link_up;
3807 int i;
3808
Matt Carlson8d018622007-12-20 20:05:44 -08003809 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 orig_active_speed = tp->link_config.active_speed;
3811 orig_active_duplex = tp->link_config.active_duplex;
3812
3813 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
3814 netif_carrier_ok(tp->dev) &&
3815 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
3816 mac_status = tr32(MAC_STATUS);
3817 mac_status &= (MAC_STATUS_PCS_SYNCED |
3818 MAC_STATUS_SIGNAL_DET |
3819 MAC_STATUS_CFG_CHANGED |
3820 MAC_STATUS_RCVD_CFG);
3821 if (mac_status == (MAC_STATUS_PCS_SYNCED |
3822 MAC_STATUS_SIGNAL_DET)) {
3823 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3824 MAC_STATUS_CFG_CHANGED));
3825 return 0;
3826 }
3827 }
3828
3829 tw32_f(MAC_TX_AUTO_NEG, 0);
3830
3831 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
3832 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
3833 tw32_f(MAC_MODE, tp->mac_mode);
3834 udelay(40);
3835
3836 if (tp->phy_id == PHY_ID_BCM8002)
3837 tg3_init_bcm8002(tp);
3838
3839 /* Enable link change event even when serdes polling. */
3840 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3841 udelay(40);
3842
3843 current_link_up = 0;
3844 mac_status = tr32(MAC_STATUS);
3845
3846 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
3847 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
3848 else
3849 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
3850
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 tp->hw_status->status =
3852 (SD_STATUS_UPDATED |
3853 (tp->hw_status->status & ~SD_STATUS_LINK_CHG));
3854
3855 for (i = 0; i < 100; i++) {
3856 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3857 MAC_STATUS_CFG_CHANGED));
3858 udelay(5);
3859 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07003860 MAC_STATUS_CFG_CHANGED |
3861 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 break;
3863 }
3864
3865 mac_status = tr32(MAC_STATUS);
3866 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
3867 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003868 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
3869 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 tw32_f(MAC_MODE, (tp->mac_mode |
3871 MAC_MODE_SEND_CONFIGS));
3872 udelay(1);
3873 tw32_f(MAC_MODE, tp->mac_mode);
3874 }
3875 }
3876
3877 if (current_link_up == 1) {
3878 tp->link_config.active_speed = SPEED_1000;
3879 tp->link_config.active_duplex = DUPLEX_FULL;
3880 tw32(MAC_LED_CTRL, (tp->led_ctrl |
3881 LED_CTRL_LNKLED_OVERRIDE |
3882 LED_CTRL_1000MBPS_ON));
3883 } else {
3884 tp->link_config.active_speed = SPEED_INVALID;
3885 tp->link_config.active_duplex = DUPLEX_INVALID;
3886 tw32(MAC_LED_CTRL, (tp->led_ctrl |
3887 LED_CTRL_LNKLED_OVERRIDE |
3888 LED_CTRL_TRAFFIC_OVERRIDE));
3889 }
3890
3891 if (current_link_up != netif_carrier_ok(tp->dev)) {
3892 if (current_link_up)
3893 netif_carrier_on(tp->dev);
3894 else
3895 netif_carrier_off(tp->dev);
3896 tg3_link_report(tp);
3897 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08003898 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 if (orig_pause_cfg != now_pause_cfg ||
3900 orig_active_speed != tp->link_config.active_speed ||
3901 orig_active_duplex != tp->link_config.active_duplex)
3902 tg3_link_report(tp);
3903 }
3904
3905 return 0;
3906}
3907
Michael Chan747e8f82005-07-25 12:33:22 -07003908static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
3909{
3910 int current_link_up, err = 0;
3911 u32 bmsr, bmcr;
3912 u16 current_speed;
3913 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08003914 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07003915
3916 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3917 tw32_f(MAC_MODE, tp->mac_mode);
3918 udelay(40);
3919
3920 tw32(MAC_EVENT, 0);
3921
3922 tw32_f(MAC_STATUS,
3923 (MAC_STATUS_SYNC_CHANGED |
3924 MAC_STATUS_CFG_CHANGED |
3925 MAC_STATUS_MI_COMPLETION |
3926 MAC_STATUS_LNKSTATE_CHANGED));
3927 udelay(40);
3928
3929 if (force_reset)
3930 tg3_phy_reset(tp);
3931
3932 current_link_up = 0;
3933 current_speed = SPEED_INVALID;
3934 current_duplex = DUPLEX_INVALID;
3935
3936 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
3937 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08003938 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
3939 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
3940 bmsr |= BMSR_LSTATUS;
3941 else
3942 bmsr &= ~BMSR_LSTATUS;
3943 }
Michael Chan747e8f82005-07-25 12:33:22 -07003944
3945 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
3946
3947 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlson2bd3ed02008-06-09 15:39:55 -07003948 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07003949 /* do nothing, just check for link up at the end */
3950 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3951 u32 adv, new_adv;
3952
3953 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
3954 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
3955 ADVERTISE_1000XPAUSE |
3956 ADVERTISE_1000XPSE_ASYM |
3957 ADVERTISE_SLCT);
3958
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003959 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07003960
3961 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
3962 new_adv |= ADVERTISE_1000XHALF;
3963 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
3964 new_adv |= ADVERTISE_1000XFULL;
3965
3966 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
3967 tg3_writephy(tp, MII_ADVERTISE, new_adv);
3968 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
3969 tg3_writephy(tp, MII_BMCR, bmcr);
3970
3971 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07003972 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Michael Chan747e8f82005-07-25 12:33:22 -07003973 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
3974
3975 return err;
3976 }
3977 } else {
3978 u32 new_bmcr;
3979
3980 bmcr &= ~BMCR_SPEED1000;
3981 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
3982
3983 if (tp->link_config.duplex == DUPLEX_FULL)
3984 new_bmcr |= BMCR_FULLDPLX;
3985
3986 if (new_bmcr != bmcr) {
3987 /* BMCR_SPEED1000 is a reserved bit that needs
3988 * to be set on write.
3989 */
3990 new_bmcr |= BMCR_SPEED1000;
3991
3992 /* Force a linkdown */
3993 if (netif_carrier_ok(tp->dev)) {
3994 u32 adv;
3995
3996 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
3997 adv &= ~(ADVERTISE_1000XFULL |
3998 ADVERTISE_1000XHALF |
3999 ADVERTISE_SLCT);
4000 tg3_writephy(tp, MII_ADVERTISE, adv);
4001 tg3_writephy(tp, MII_BMCR, bmcr |
4002 BMCR_ANRESTART |
4003 BMCR_ANENABLE);
4004 udelay(10);
4005 netif_carrier_off(tp->dev);
4006 }
4007 tg3_writephy(tp, MII_BMCR, new_bmcr);
4008 bmcr = new_bmcr;
4009 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4010 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004011 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4012 ASIC_REV_5714) {
4013 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4014 bmsr |= BMSR_LSTATUS;
4015 else
4016 bmsr &= ~BMSR_LSTATUS;
4017 }
Michael Chan747e8f82005-07-25 12:33:22 -07004018 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4019 }
4020 }
4021
4022 if (bmsr & BMSR_LSTATUS) {
4023 current_speed = SPEED_1000;
4024 current_link_up = 1;
4025 if (bmcr & BMCR_FULLDPLX)
4026 current_duplex = DUPLEX_FULL;
4027 else
4028 current_duplex = DUPLEX_HALF;
4029
Matt Carlsonef167e22007-12-20 20:10:01 -08004030 local_adv = 0;
4031 remote_adv = 0;
4032
Michael Chan747e8f82005-07-25 12:33:22 -07004033 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004034 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004035
4036 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4037 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4038 common = local_adv & remote_adv;
4039 if (common & (ADVERTISE_1000XHALF |
4040 ADVERTISE_1000XFULL)) {
4041 if (common & ADVERTISE_1000XFULL)
4042 current_duplex = DUPLEX_FULL;
4043 else
4044 current_duplex = DUPLEX_HALF;
Michael Chan747e8f82005-07-25 12:33:22 -07004045 }
4046 else
4047 current_link_up = 0;
4048 }
4049 }
4050
Matt Carlsonef167e22007-12-20 20:10:01 -08004051 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4052 tg3_setup_flow_control(tp, local_adv, remote_adv);
4053
Michael Chan747e8f82005-07-25 12:33:22 -07004054 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4055 if (tp->link_config.active_duplex == DUPLEX_HALF)
4056 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4057
4058 tw32_f(MAC_MODE, tp->mac_mode);
4059 udelay(40);
4060
4061 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4062
4063 tp->link_config.active_speed = current_speed;
4064 tp->link_config.active_duplex = current_duplex;
4065
4066 if (current_link_up != netif_carrier_ok(tp->dev)) {
4067 if (current_link_up)
4068 netif_carrier_on(tp->dev);
4069 else {
4070 netif_carrier_off(tp->dev);
4071 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4072 }
4073 tg3_link_report(tp);
4074 }
4075 return err;
4076}
4077
4078static void tg3_serdes_parallel_detect(struct tg3 *tp)
4079{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004080 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004081 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004082 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004083 return;
4084 }
4085 if (!netif_carrier_ok(tp->dev) &&
4086 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4087 u32 bmcr;
4088
4089 tg3_readphy(tp, MII_BMCR, &bmcr);
4090 if (bmcr & BMCR_ANENABLE) {
4091 u32 phy1, phy2;
4092
4093 /* Select shadow register 0x1f */
4094 tg3_writephy(tp, 0x1c, 0x7c00);
4095 tg3_readphy(tp, 0x1c, &phy1);
4096
4097 /* Select expansion interrupt status register */
4098 tg3_writephy(tp, 0x17, 0x0f01);
4099 tg3_readphy(tp, 0x15, &phy2);
4100 tg3_readphy(tp, 0x15, &phy2);
4101
4102 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4103 /* We have signal detect and not receiving
4104 * config code words, link is up by parallel
4105 * detection.
4106 */
4107
4108 bmcr &= ~BMCR_ANENABLE;
4109 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4110 tg3_writephy(tp, MII_BMCR, bmcr);
4111 tp->tg3_flags2 |= TG3_FLG2_PARALLEL_DETECT;
4112 }
4113 }
4114 }
4115 else if (netif_carrier_ok(tp->dev) &&
4116 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
4117 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
4118 u32 phy2;
4119
4120 /* Select expansion interrupt status register */
4121 tg3_writephy(tp, 0x17, 0x0f01);
4122 tg3_readphy(tp, 0x15, &phy2);
4123 if (phy2 & 0x20) {
4124 u32 bmcr;
4125
4126 /* Config code words received, turn on autoneg. */
4127 tg3_readphy(tp, MII_BMCR, &bmcr);
4128 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4129
4130 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4131
4132 }
4133 }
4134}
4135
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4137{
4138 int err;
4139
4140 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
4141 err = tg3_setup_fiber_phy(tp, force_reset);
Michael Chan747e8f82005-07-25 12:33:22 -07004142 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
4143 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 } else {
4145 err = tg3_setup_copper_phy(tp, force_reset);
4146 }
4147
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004148 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004149 u32 val, scale;
4150
4151 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4152 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4153 scale = 65;
4154 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4155 scale = 6;
4156 else
4157 scale = 12;
4158
4159 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4160 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4161 tw32(GRC_MISC_CFG, val);
4162 }
4163
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 if (tp->link_config.active_speed == SPEED_1000 &&
4165 tp->link_config.active_duplex == DUPLEX_HALF)
4166 tw32(MAC_TX_LENGTHS,
4167 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4168 (6 << TX_LENGTHS_IPG_SHIFT) |
4169 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4170 else
4171 tw32(MAC_TX_LENGTHS,
4172 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4173 (6 << TX_LENGTHS_IPG_SHIFT) |
4174 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4175
4176 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4177 if (netif_carrier_ok(tp->dev)) {
4178 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004179 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 } else {
4181 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4182 }
4183 }
4184
Matt Carlson8ed5d972007-05-07 00:25:49 -07004185 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4186 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4187 if (!netif_carrier_ok(tp->dev))
4188 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4189 tp->pwrmgmt_thresh;
4190 else
4191 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4192 tw32(PCIE_PWR_MGMT_THRESH, val);
4193 }
4194
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 return err;
4196}
4197
Michael Chandf3e6542006-05-26 17:48:07 -07004198/* This is called whenever we suspect that the system chipset is re-
4199 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4200 * is bogus tx completions. We try to recover by setting the
4201 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4202 * in the workqueue.
4203 */
4204static void tg3_tx_recover(struct tg3 *tp)
4205{
4206 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4207 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4208
4209 printk(KERN_WARNING PFX "%s: The system may be re-ordering memory-"
4210 "mapped I/O cycles to the network device, attempting to "
4211 "recover. Please report the problem to the driver maintainer "
4212 "and include system chipset information.\n", tp->dev->name);
4213
4214 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004215 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004216 spin_unlock(&tp->lock);
4217}
4218
Michael Chan1b2a7202006-08-07 21:46:02 -07004219static inline u32 tg3_tx_avail(struct tg3 *tp)
4220{
4221 smp_mb();
4222 return (tp->tx_pending -
4223 ((tp->tx_prod - tp->tx_cons) & (TG3_TX_RING_SIZE - 1)));
4224}
4225
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226/* Tigon3 never reports partial packet sends. So we do not
4227 * need special logic to handle SKBs that have not had all
4228 * of their frags sent yet, like SunGEM does.
4229 */
4230static void tg3_tx(struct tg3 *tp)
4231{
4232 u32 hw_idx = tp->hw_status->idx[0].tx_consumer;
4233 u32 sw_idx = tp->tx_cons;
4234
4235 while (sw_idx != hw_idx) {
4236 struct tx_ring_info *ri = &tp->tx_buffers[sw_idx];
4237 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004238 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
Michael Chandf3e6542006-05-26 17:48:07 -07004240 if (unlikely(skb == NULL)) {
4241 tg3_tx_recover(tp);
4242 return;
4243 }
4244
David S. Miller90079ce2008-09-11 04:52:51 -07004245 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
4247 ri->skb = NULL;
4248
4249 sw_idx = NEXT_TX(sw_idx);
4250
4251 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 ri = &tp->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004253 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4254 tx_bug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 sw_idx = NEXT_TX(sw_idx);
4256 }
4257
David S. Millerf47c11e2005-06-24 20:18:35 -07004258 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004259
4260 if (unlikely(tx_bug)) {
4261 tg3_tx_recover(tp);
4262 return;
4263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 }
4265
4266 tp->tx_cons = sw_idx;
4267
Michael Chan1b2a7202006-08-07 21:46:02 -07004268 /* Need to make the tx_cons update visible to tg3_start_xmit()
4269 * before checking for netif_queue_stopped(). Without the
4270 * memory barrier, there is a small possibility that tg3_start_xmit()
4271 * will miss it and cause the queue to be stopped forever.
4272 */
4273 smp_mb();
4274
4275 if (unlikely(netif_queue_stopped(tp->dev) &&
Ranjit Manomohan42952232006-10-18 20:54:26 -07004276 (tg3_tx_avail(tp) > TG3_TX_WAKEUP_THRESH(tp)))) {
Michael Chan1b2a7202006-08-07 21:46:02 -07004277 netif_tx_lock(tp->dev);
Michael Chan51b91462005-09-01 17:41:28 -07004278 if (netif_queue_stopped(tp->dev) &&
Ranjit Manomohan42952232006-10-18 20:54:26 -07004279 (tg3_tx_avail(tp) > TG3_TX_WAKEUP_THRESH(tp)))
Michael Chan51b91462005-09-01 17:41:28 -07004280 netif_wake_queue(tp->dev);
Michael Chan1b2a7202006-08-07 21:46:02 -07004281 netif_tx_unlock(tp->dev);
Michael Chan51b91462005-09-01 17:41:28 -07004282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283}
4284
4285/* Returns size of skb allocated or < 0 on error.
4286 *
4287 * We only need to fill in the address because the other members
4288 * of the RX descriptor are invariant, see tg3_init_rings.
4289 *
4290 * Note the purposeful assymetry of cpu vs. chip accesses. For
4291 * posting buffers we only dirty the first cache line of the RX
4292 * descriptor (containing the address). Whereas for the RX status
4293 * buffers the cpu only reads the last cacheline of the RX descriptor
4294 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4295 */
4296static int tg3_alloc_rx_skb(struct tg3 *tp, u32 opaque_key,
4297 int src_idx, u32 dest_idx_unmasked)
4298{
4299 struct tg3_rx_buffer_desc *desc;
4300 struct ring_info *map, *src_map;
4301 struct sk_buff *skb;
4302 dma_addr_t mapping;
4303 int skb_size, dest_idx;
4304
4305 src_map = NULL;
4306 switch (opaque_key) {
4307 case RXD_OPAQUE_RING_STD:
4308 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
4309 desc = &tp->rx_std[dest_idx];
4310 map = &tp->rx_std_buffers[dest_idx];
4311 if (src_idx >= 0)
4312 src_map = &tp->rx_std_buffers[src_idx];
Michael Chan7e72aad2005-07-25 12:31:17 -07004313 skb_size = tp->rx_pkt_buf_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 break;
4315
4316 case RXD_OPAQUE_RING_JUMBO:
4317 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
4318 desc = &tp->rx_jumbo[dest_idx];
4319 map = &tp->rx_jumbo_buffers[dest_idx];
4320 if (src_idx >= 0)
4321 src_map = &tp->rx_jumbo_buffers[src_idx];
4322 skb_size = RX_JUMBO_PKT_BUF_SZ;
4323 break;
4324
4325 default:
4326 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
4329 /* Do not overwrite any of the map or rp information
4330 * until we are sure we can commit to a new buffer.
4331 *
4332 * Callers depend upon this behavior and assume that
4333 * we leave everything unchanged if we fail.
4334 */
David S. Millera20e9c62006-07-31 22:38:16 -07004335 skb = netdev_alloc_skb(tp->dev, skb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 if (skb == NULL)
4337 return -ENOMEM;
4338
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 skb_reserve(skb, tp->rx_offset);
4340
4341 mapping = pci_map_single(tp->pdev, skb->data,
4342 skb_size - tp->rx_offset,
4343 PCI_DMA_FROMDEVICE);
4344
4345 map->skb = skb;
4346 pci_unmap_addr_set(map, mapping, mapping);
4347
4348 if (src_map != NULL)
4349 src_map->skb = NULL;
4350
4351 desc->addr_hi = ((u64)mapping >> 32);
4352 desc->addr_lo = ((u64)mapping & 0xffffffff);
4353
4354 return skb_size;
4355}
4356
4357/* We only need to move over in the address because the other
4358 * members of the RX descriptor are invariant. See notes above
4359 * tg3_alloc_rx_skb for full details.
4360 */
4361static void tg3_recycle_rx(struct tg3 *tp, u32 opaque_key,
4362 int src_idx, u32 dest_idx_unmasked)
4363{
4364 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4365 struct ring_info *src_map, *dest_map;
4366 int dest_idx;
4367
4368 switch (opaque_key) {
4369 case RXD_OPAQUE_RING_STD:
4370 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
4371 dest_desc = &tp->rx_std[dest_idx];
4372 dest_map = &tp->rx_std_buffers[dest_idx];
4373 src_desc = &tp->rx_std[src_idx];
4374 src_map = &tp->rx_std_buffers[src_idx];
4375 break;
4376
4377 case RXD_OPAQUE_RING_JUMBO:
4378 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
4379 dest_desc = &tp->rx_jumbo[dest_idx];
4380 dest_map = &tp->rx_jumbo_buffers[dest_idx];
4381 src_desc = &tp->rx_jumbo[src_idx];
4382 src_map = &tp->rx_jumbo_buffers[src_idx];
4383 break;
4384
4385 default:
4386 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
4389 dest_map->skb = src_map->skb;
4390 pci_unmap_addr_set(dest_map, mapping,
4391 pci_unmap_addr(src_map, mapping));
4392 dest_desc->addr_hi = src_desc->addr_hi;
4393 dest_desc->addr_lo = src_desc->addr_lo;
4394
4395 src_map->skb = NULL;
4396}
4397
4398#if TG3_VLAN_TAG_USED
4399static int tg3_vlan_rx(struct tg3 *tp, struct sk_buff *skb, u16 vlan_tag)
4400{
David S. Miller1383bdb2009-03-29 01:39:49 -07004401 return vlan_gro_receive(&tp->napi, tp->vlgrp, vlan_tag, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402}
4403#endif
4404
4405/* The RX ring scheme is composed of multiple rings which post fresh
4406 * buffers to the chip, and one special ring the chip uses to report
4407 * status back to the host.
4408 *
4409 * The special ring reports the status of received packets to the
4410 * host. The chip does not write into the original descriptor the
4411 * RX buffer was obtained from. The chip simply takes the original
4412 * descriptor as provided by the host, updates the status and length
4413 * field, then writes this into the next status ring entry.
4414 *
4415 * Each ring the host uses to post buffers to the chip is described
4416 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4417 * it is first placed into the on-chip ram. When the packet's length
4418 * is known, it walks down the TG3_BDINFO entries to select the ring.
4419 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4420 * which is within the range of the new packet's length is chosen.
4421 *
4422 * The "separate ring for rx status" scheme may sound queer, but it makes
4423 * sense from a cache coherency perspective. If only the host writes
4424 * to the buffer post rings, and only the chip writes to the rx status
4425 * rings, then cache lines never move beyond shared-modified state.
4426 * If both the host and chip were to write into the same ring, cache line
4427 * eviction could occur since both entities want it in an exclusive state.
4428 */
4429static int tg3_rx(struct tg3 *tp, int budget)
4430{
Michael Chanf92905d2006-06-29 20:14:29 -07004431 u32 work_mask, rx_std_posted = 0;
Michael Chan483ba502005-04-25 15:14:03 -07004432 u32 sw_idx = tp->rx_rcb_ptr;
4433 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 int received;
4435
4436 hw_idx = tp->hw_status->idx[0].rx_producer;
4437 /*
4438 * We need to order the read of hw_idx and the read of
4439 * the opaque cookie.
4440 */
4441 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 work_mask = 0;
4443 received = 0;
4444 while (sw_idx != hw_idx && budget > 0) {
4445 struct tg3_rx_buffer_desc *desc = &tp->rx_rcb[sw_idx];
4446 unsigned int len;
4447 struct sk_buff *skb;
4448 dma_addr_t dma_addr;
4449 u32 opaque_key, desc_idx, *post_ptr;
4450
4451 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4452 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4453 if (opaque_key == RXD_OPAQUE_RING_STD) {
4454 dma_addr = pci_unmap_addr(&tp->rx_std_buffers[desc_idx],
4455 mapping);
4456 skb = tp->rx_std_buffers[desc_idx].skb;
4457 post_ptr = &tp->rx_std_ptr;
Michael Chanf92905d2006-06-29 20:14:29 -07004458 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
4460 dma_addr = pci_unmap_addr(&tp->rx_jumbo_buffers[desc_idx],
4461 mapping);
4462 skb = tp->rx_jumbo_buffers[desc_idx].skb;
4463 post_ptr = &tp->rx_jumbo_ptr;
4464 }
4465 else {
4466 goto next_pkt_nopost;
4467 }
4468
4469 work_mask |= opaque_key;
4470
4471 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4472 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4473 drop_it:
4474 tg3_recycle_rx(tp, opaque_key,
4475 desc_idx, *post_ptr);
4476 drop_it_no_recycle:
4477 /* Other statistics kept track of by card. */
4478 tp->net_stats.rx_dropped++;
4479 goto next_pkt;
4480 }
4481
Matt Carlsonad829262008-11-21 17:16:16 -08004482 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4483 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004485 if (len > RX_COPY_THRESHOLD
Matt Carlsonad829262008-11-21 17:16:16 -08004486 && tp->rx_offset == NET_IP_ALIGN
4487 /* rx_offset will likely not equal NET_IP_ALIGN
4488 * if this is a 5701 card running in PCI-X mode
4489 * [see tg3_get_invariants()]
4490 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 ) {
4492 int skb_size;
4493
4494 skb_size = tg3_alloc_rx_skb(tp, opaque_key,
4495 desc_idx, *post_ptr);
4496 if (skb_size < 0)
4497 goto drop_it;
4498
4499 pci_unmap_single(tp->pdev, dma_addr,
4500 skb_size - tp->rx_offset,
4501 PCI_DMA_FROMDEVICE);
4502
4503 skb_put(skb, len);
4504 } else {
4505 struct sk_buff *copy_skb;
4506
4507 tg3_recycle_rx(tp, opaque_key,
4508 desc_idx, *post_ptr);
4509
Matt Carlsonad829262008-11-21 17:16:16 -08004510 copy_skb = netdev_alloc_skb(tp->dev,
4511 len + TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 if (copy_skb == NULL)
4513 goto drop_it_no_recycle;
4514
Matt Carlsonad829262008-11-21 17:16:16 -08004515 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 skb_put(copy_skb, len);
4517 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004518 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4520
4521 /* We'll reuse the original ring buffer. */
4522 skb = copy_skb;
4523 }
4524
4525 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4526 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4527 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4528 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4529 skb->ip_summed = CHECKSUM_UNNECESSARY;
4530 else
4531 skb->ip_summed = CHECKSUM_NONE;
4532
4533 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004534
4535 if (len > (tp->dev->mtu + ETH_HLEN) &&
4536 skb->protocol != htons(ETH_P_8021Q)) {
4537 dev_kfree_skb(skb);
4538 goto next_pkt;
4539 }
4540
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541#if TG3_VLAN_TAG_USED
4542 if (tp->vlgrp != NULL &&
4543 desc->type_flags & RXD_FLAG_VLAN) {
4544 tg3_vlan_rx(tp, skb,
4545 desc->err_vlan & RXD_VLAN_MASK);
4546 } else
4547#endif
David S. Miller1383bdb2009-03-29 01:39:49 -07004548 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 received++;
4551 budget--;
4552
4553next_pkt:
4554 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004555
4556 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
4557 u32 idx = *post_ptr % TG3_RX_RING_SIZE;
4558
4559 tw32_rx_mbox(MAILBOX_RCV_STD_PROD_IDX +
4560 TG3_64BIT_REG_LOW, idx);
4561 work_mask &= ~RXD_OPAQUE_RING_STD;
4562 rx_std_posted = 0;
4563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004565 sw_idx++;
Eric Dumazet6b31a512007-02-06 13:29:21 -08004566 sw_idx &= (TG3_RX_RCB_RING_SIZE(tp) - 1);
Michael Chan52f6d692005-04-25 15:14:32 -07004567
4568 /* Refresh hw_idx to see if there is new work */
4569 if (sw_idx == hw_idx) {
4570 hw_idx = tp->hw_status->idx[0].rx_producer;
4571 rmb();
4572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 }
4574
4575 /* ACK the status ring. */
Michael Chan483ba502005-04-25 15:14:03 -07004576 tp->rx_rcb_ptr = sw_idx;
4577 tw32_rx_mbox(MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
4579 /* Refill RX ring(s). */
4580 if (work_mask & RXD_OPAQUE_RING_STD) {
4581 sw_idx = tp->rx_std_ptr % TG3_RX_RING_SIZE;
4582 tw32_rx_mbox(MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW,
4583 sw_idx);
4584 }
4585 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
4586 sw_idx = tp->rx_jumbo_ptr % TG3_RX_JUMBO_RING_SIZE;
4587 tw32_rx_mbox(MAILBOX_RCV_JUMBO_PROD_IDX + TG3_64BIT_REG_LOW,
4588 sw_idx);
4589 }
4590 mmiowb();
4591
4592 return received;
4593}
4594
David S. Miller6f535762007-10-11 18:08:29 -07004595static int tg3_poll_work(struct tg3 *tp, int work_done, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 struct tg3_hw_status *sblk = tp->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 /* handle link change and other phy events */
4600 if (!(tp->tg3_flags &
4601 (TG3_FLAG_USE_LINKCHG_REG |
4602 TG3_FLAG_POLL_SERDES))) {
4603 if (sblk->status & SD_STATUS_LINK_CHG) {
4604 sblk->status = SD_STATUS_UPDATED |
4605 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004606 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004607 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4608 tw32_f(MAC_STATUS,
4609 (MAC_STATUS_SYNC_CHANGED |
4610 MAC_STATUS_CFG_CHANGED |
4611 MAC_STATUS_MI_COMPLETION |
4612 MAC_STATUS_LNKSTATE_CHANGED));
4613 udelay(40);
4614 } else
4615 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004616 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 }
4618 }
4619
4620 /* run TX completion thread */
4621 if (sblk->idx[0].tx_consumer != tp->tx_cons) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 tg3_tx(tp);
David S. Miller6f535762007-10-11 18:08:29 -07004623 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07004624 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 }
4626
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 /* run RX thread, within the bounds set by NAPI.
4628 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004629 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004631 if (sblk->idx[0].rx_producer != tp->rx_rcb_ptr)
David S. Miller6f535762007-10-11 18:08:29 -07004632 work_done += tg3_rx(tp, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
David S. Miller6f535762007-10-11 18:08:29 -07004634 return work_done;
4635}
David S. Millerf7383c22005-05-18 22:50:53 -07004636
David S. Miller6f535762007-10-11 18:08:29 -07004637static int tg3_poll(struct napi_struct *napi, int budget)
4638{
4639 struct tg3 *tp = container_of(napi, struct tg3, napi);
4640 int work_done = 0;
Michael Chan4fd7ab52007-10-12 01:39:50 -07004641 struct tg3_hw_status *sblk = tp->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07004642
4643 while (1) {
4644 work_done = tg3_poll_work(tp, work_done, budget);
4645
4646 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
4647 goto tx_recovery;
4648
4649 if (unlikely(work_done >= budget))
4650 break;
4651
Michael Chan4fd7ab52007-10-12 01:39:50 -07004652 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
4653 /* tp->last_tag is used in tg3_restart_ints() below
4654 * to tell the hw how much work has been processed,
4655 * so we must read it before checking for more work.
4656 */
4657 tp->last_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00004658 tp->last_irq_tag = tp->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07004659 rmb();
4660 } else
4661 sblk->status &= ~SD_STATUS_UPDATED;
4662
David S. Miller6f535762007-10-11 18:08:29 -07004663 if (likely(!tg3_has_work(tp))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08004664 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07004665 tg3_restart_ints(tp);
4666 break;
4667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 }
4669
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004670 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07004671
4672tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07004673 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08004674 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07004675 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07004676 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677}
4678
David S. Millerf47c11e2005-06-24 20:18:35 -07004679static void tg3_irq_quiesce(struct tg3 *tp)
4680{
4681 BUG_ON(tp->irq_sync);
4682
4683 tp->irq_sync = 1;
4684 smp_mb();
4685
4686 synchronize_irq(tp->pdev->irq);
4687}
4688
4689static inline int tg3_irq_sync(struct tg3 *tp)
4690{
4691 return tp->irq_sync;
4692}
4693
4694/* Fully shutdown all tg3 driver activity elsewhere in the system.
4695 * If irq_sync is non-zero, then the IRQ handler must be synchronized
4696 * with as well. Most of the time, this is not necessary except when
4697 * shutting down the device.
4698 */
4699static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
4700{
Michael Chan46966542007-07-11 19:47:19 -07004701 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07004702 if (irq_sync)
4703 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07004704}
4705
4706static inline void tg3_full_unlock(struct tg3 *tp)
4707{
David S. Millerf47c11e2005-06-24 20:18:35 -07004708 spin_unlock_bh(&tp->lock);
4709}
4710
Michael Chanfcfa0a32006-03-20 22:28:41 -08004711/* One-shot MSI handler - Chip automatically disables interrupt
4712 * after sending MSI so driver doesn't have to do it.
4713 */
David Howells7d12e782006-10-05 14:55:46 +01004714static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08004715{
4716 struct net_device *dev = dev_id;
4717 struct tg3 *tp = netdev_priv(dev);
4718
4719 prefetch(tp->hw_status);
4720 prefetch(&tp->rx_rcb[tp->rx_rcb_ptr]);
4721
4722 if (likely(!tg3_irq_sync(tp)))
Ben Hutchings288379f2009-01-19 16:43:59 -08004723 napi_schedule(&tp->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08004724
4725 return IRQ_HANDLED;
4726}
4727
Michael Chan88b06bc22005-04-21 17:13:25 -07004728/* MSI ISR - No need to check for interrupt sharing and no need to
4729 * flush status block and interrupt mailbox. PCI ordering rules
4730 * guarantee that MSI will arrive after the status block.
4731 */
David Howells7d12e782006-10-05 14:55:46 +01004732static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07004733{
4734 struct net_device *dev = dev_id;
4735 struct tg3 *tp = netdev_priv(dev);
Michael Chan88b06bc22005-04-21 17:13:25 -07004736
Michael Chan61487482005-09-05 17:53:19 -07004737 prefetch(tp->hw_status);
4738 prefetch(&tp->rx_rcb[tp->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07004739 /*
David S. Millerfac9b832005-05-18 22:46:34 -07004740 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07004741 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07004742 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07004743 * NIC to stop sending us irqs, engaging "in-intr-handler"
4744 * event coalescing.
4745 */
4746 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07004747 if (likely(!tg3_irq_sync(tp)))
Ben Hutchings288379f2009-01-19 16:43:59 -08004748 napi_schedule(&tp->napi);
Michael Chan61487482005-09-05 17:53:19 -07004749
Michael Chan88b06bc22005-04-21 17:13:25 -07004750 return IRQ_RETVAL(1);
4751}
4752
David Howells7d12e782006-10-05 14:55:46 +01004753static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754{
4755 struct net_device *dev = dev_id;
4756 struct tg3 *tp = netdev_priv(dev);
4757 struct tg3_hw_status *sblk = tp->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 unsigned int handled = 1;
4759
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 /* In INTx mode, it is possible for the interrupt to arrive at
4761 * the CPU before the status block posted prior to the interrupt.
4762 * Reading the PCI State register will confirm whether the
4763 * interrupt is ours and will flush the status block.
4764 */
Michael Chand18edcb2007-03-24 20:57:11 -07004765 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
4766 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
4767 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
4768 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07004769 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07004770 }
Michael Chand18edcb2007-03-24 20:57:11 -07004771 }
4772
4773 /*
4774 * Writing any value to intr-mbox-0 clears PCI INTA# and
4775 * chip-internal interrupt pending events.
4776 * Writing non-zero to intr-mbox-0 additional tells the
4777 * NIC to stop sending us irqs, engaging "in-intr-handler"
4778 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07004779 *
4780 * Flush the mailbox to de-assert the IRQ immediately to prevent
4781 * spurious interrupts. The flush impacts performance but
4782 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07004783 */
Michael Chanc04cb342007-05-07 00:26:15 -07004784 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07004785 if (tg3_irq_sync(tp))
4786 goto out;
4787 sblk->status &= ~SD_STATUS_UPDATED;
4788 if (likely(tg3_has_work(tp))) {
4789 prefetch(&tp->rx_rcb[tp->rx_rcb_ptr]);
Ben Hutchings288379f2009-01-19 16:43:59 -08004790 napi_schedule(&tp->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07004791 } else {
4792 /* No work, shared interrupt perhaps? re-enable
4793 * interrupts, and flush that PCI write
4794 */
4795 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
4796 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07004797 }
David S. Millerf47c11e2005-06-24 20:18:35 -07004798out:
David S. Millerfac9b832005-05-18 22:46:34 -07004799 return IRQ_RETVAL(handled);
4800}
4801
David Howells7d12e782006-10-05 14:55:46 +01004802static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07004803{
4804 struct net_device *dev = dev_id;
4805 struct tg3 *tp = netdev_priv(dev);
4806 struct tg3_hw_status *sblk = tp->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07004807 unsigned int handled = 1;
4808
David S. Millerfac9b832005-05-18 22:46:34 -07004809 /* In INTx mode, it is possible for the interrupt to arrive at
4810 * the CPU before the status block posted prior to the interrupt.
4811 * Reading the PCI State register will confirm whether the
4812 * interrupt is ours and will flush the status block.
4813 */
Matt Carlson624f8e52009-04-20 06:55:01 +00004814 if (unlikely(sblk->status_tag == tp->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07004815 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
4816 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
4817 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07004818 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 }
Michael Chand18edcb2007-03-24 20:57:11 -07004820 }
4821
4822 /*
4823 * writing any value to intr-mbox-0 clears PCI INTA# and
4824 * chip-internal interrupt pending events.
4825 * writing non-zero to intr-mbox-0 additional tells the
4826 * NIC to stop sending us irqs, engaging "in-intr-handler"
4827 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07004828 *
4829 * Flush the mailbox to de-assert the IRQ immediately to prevent
4830 * spurious interrupts. The flush impacts performance but
4831 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07004832 */
Michael Chanc04cb342007-05-07 00:26:15 -07004833 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00004834
4835 /*
4836 * In a shared interrupt configuration, sometimes other devices'
4837 * interrupts will scream. We record the current status tag here
4838 * so that the above check can report that the screaming interrupts
4839 * are unhandled. Eventually they will be silenced.
4840 */
4841 tp->last_irq_tag = sblk->status_tag;
4842
Michael Chand18edcb2007-03-24 20:57:11 -07004843 if (tg3_irq_sync(tp))
4844 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00004845
4846 prefetch(&tp->rx_rcb[tp->rx_rcb_ptr]);
4847
4848 napi_schedule(&tp->napi);
4849
David S. Millerf47c11e2005-06-24 20:18:35 -07004850out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 return IRQ_RETVAL(handled);
4852}
4853
Michael Chan79381092005-04-21 17:13:59 -07004854/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01004855static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07004856{
4857 struct net_device *dev = dev_id;
4858 struct tg3 *tp = netdev_priv(dev);
4859 struct tg3_hw_status *sblk = tp->hw_status;
4860
Michael Chanf9804dd2005-09-27 12:13:10 -07004861 if ((sblk->status & SD_STATUS_UPDATED) ||
4862 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07004863 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07004864 return IRQ_RETVAL(1);
4865 }
4866 return IRQ_RETVAL(0);
4867}
4868
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07004869static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07004870static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
Michael Chanb9ec6c12006-07-25 16:37:27 -07004872/* Restart hardware after configuration changes, self-test, etc.
4873 * Invoked with tp->lock held.
4874 */
4875static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07004876 __releases(tp->lock)
4877 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07004878{
4879 int err;
4880
4881 err = tg3_init_hw(tp, reset_phy);
4882 if (err) {
4883 printk(KERN_ERR PFX "%s: Failed to re-initialize device, "
4884 "aborting.\n", tp->dev->name);
4885 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
4886 tg3_full_unlock(tp);
4887 del_timer_sync(&tp->timer);
4888 tp->irq_sync = 0;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004889 napi_enable(&tp->napi);
Michael Chanb9ec6c12006-07-25 16:37:27 -07004890 dev_close(tp->dev);
4891 tg3_full_lock(tp, 0);
4892 }
4893 return err;
4894}
4895
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896#ifdef CONFIG_NET_POLL_CONTROLLER
4897static void tg3_poll_controller(struct net_device *dev)
4898{
Michael Chan88b06bc22005-04-21 17:13:25 -07004899 struct tg3 *tp = netdev_priv(dev);
4900
David Howells7d12e782006-10-05 14:55:46 +01004901 tg3_interrupt(tp->pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902}
4903#endif
4904
David Howellsc4028952006-11-22 14:57:56 +00004905static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906{
David Howellsc4028952006-11-22 14:57:56 +00004907 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004908 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 unsigned int restart_timer;
4910
Michael Chan7faa0062006-02-02 17:29:28 -08004911 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08004912
4913 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08004914 tg3_full_unlock(tp);
4915 return;
4916 }
4917
4918 tg3_full_unlock(tp);
4919
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004920 tg3_phy_stop(tp);
4921
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 tg3_netif_stop(tp);
4923
David S. Millerf47c11e2005-06-24 20:18:35 -07004924 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925
4926 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
4927 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
4928
Michael Chandf3e6542006-05-26 17:48:07 -07004929 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
4930 tp->write32_tx_mbox = tg3_write32_tx_mbox;
4931 tp->write32_rx_mbox = tg3_write_flush_reg32;
4932 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
4933 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
4934 }
4935
Michael Chan944d9802005-05-29 14:57:48 -07004936 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004937 err = tg3_init_hw(tp, 1);
4938 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07004939 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940
4941 tg3_netif_start(tp);
4942
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 if (restart_timer)
4944 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08004945
Michael Chanb9ec6c12006-07-25 16:37:27 -07004946out:
Michael Chan7faa0062006-02-02 17:29:28 -08004947 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004948
4949 if (!err)
4950 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951}
4952
Michael Chanb0408752007-02-13 12:18:30 -08004953static void tg3_dump_short_state(struct tg3 *tp)
4954{
4955 printk(KERN_ERR PFX "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
4956 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
4957 printk(KERN_ERR PFX "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
4958 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
4959}
4960
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961static void tg3_tx_timeout(struct net_device *dev)
4962{
4963 struct tg3 *tp = netdev_priv(dev);
4964
Michael Chanb0408752007-02-13 12:18:30 -08004965 if (netif_msg_tx_err(tp)) {
Michael Chan9f88f292006-12-07 00:22:54 -08004966 printk(KERN_ERR PFX "%s: transmit timed out, resetting\n",
4967 dev->name);
Michael Chanb0408752007-02-13 12:18:30 -08004968 tg3_dump_short_state(tp);
4969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
4971 schedule_work(&tp->reset_task);
4972}
4973
Michael Chanc58ec932005-09-17 00:46:27 -07004974/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
4975static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
4976{
4977 u32 base = (u32) mapping & 0xffffffff;
4978
4979 return ((base > 0xffffdcc0) &&
4980 (base + len + 8 < base));
4981}
4982
Michael Chan72f2afb2006-03-06 19:28:35 -08004983/* Test for DMA addresses > 40-bit */
4984static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
4985 int len)
4986{
4987#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08004988 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Yang Hongyang50cf1562009-04-06 19:01:14 -07004989 return (((u64) mapping + len) > DMA_BIT_MASK(40));
Michael Chan72f2afb2006-03-06 19:28:35 -08004990 return 0;
4991#else
4992 return 0;
4993#endif
4994}
4995
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996static void tg3_set_txd(struct tg3 *, int, dma_addr_t, int, u32, u32);
4997
Michael Chan72f2afb2006-03-06 19:28:35 -08004998/* Workaround 4GB and 40-bit hardware DMA bugs. */
4999static int tigon3_dma_hwbug_workaround(struct tg3 *tp, struct sk_buff *skb,
Michael Chanc58ec932005-09-17 00:46:27 -07005000 u32 last_plus_one, u32 *start,
5001 u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002{
Matt Carlson41588ba2008-04-19 18:12:33 -07005003 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005004 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005006 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007
Matt Carlson41588ba2008-04-19 18:12:33 -07005008 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5009 new_skb = skb_copy(skb, GFP_ATOMIC);
5010 else {
5011 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5012
5013 new_skb = skb_copy_expand(skb,
5014 skb_headroom(skb) + more_headroom,
5015 skb_tailroom(skb), GFP_ATOMIC);
5016 }
5017
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005019 ret = -1;
5020 } else {
5021 /* New SKB is guaranteed to be linear. */
5022 entry = *start;
David S. Miller90079ce2008-09-11 04:52:51 -07005023 ret = skb_dma_map(&tp->pdev->dev, new_skb, DMA_TO_DEVICE);
Eric Dumazet042a53a2009-06-05 04:04:16 +00005024 new_addr = skb_shinfo(new_skb)->dma_head;
David S. Miller90079ce2008-09-11 04:52:51 -07005025
Michael Chanc58ec932005-09-17 00:46:27 -07005026 /* Make sure new skb does not cross any 4G boundaries.
5027 * Drop the packet if it does.
5028 */
David S. Miller90079ce2008-09-11 04:52:51 -07005029 if (ret || tg3_4g_overflow_test(new_addr, new_skb->len)) {
David S. Miller638266f2008-09-11 15:45:19 -07005030 if (!ret)
5031 skb_dma_unmap(&tp->pdev->dev, new_skb,
5032 DMA_TO_DEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005033 ret = -1;
5034 dev_kfree_skb(new_skb);
5035 new_skb = NULL;
5036 } else {
5037 tg3_set_txd(tp, entry, new_addr, new_skb->len,
5038 base_flags, 1 | (mss << 1));
5039 *start = NEXT_TX(entry);
5040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 }
5042
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 /* Now clean up the sw ring entries. */
5044 i = 0;
5045 while (entry != last_plus_one) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 if (i == 0) {
5047 tp->tx_buffers[entry].skb = new_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 } else {
5049 tp->tx_buffers[entry].skb = NULL;
5050 }
5051 entry = NEXT_TX(entry);
5052 i++;
5053 }
5054
David S. Miller90079ce2008-09-11 04:52:51 -07005055 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 dev_kfree_skb(skb);
5057
Michael Chanc58ec932005-09-17 00:46:27 -07005058 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059}
5060
5061static void tg3_set_txd(struct tg3 *tp, int entry,
5062 dma_addr_t mapping, int len, u32 flags,
5063 u32 mss_and_is_end)
5064{
5065 struct tg3_tx_buffer_desc *txd = &tp->tx_ring[entry];
5066 int is_end = (mss_and_is_end & 0x1);
5067 u32 mss = (mss_and_is_end >> 1);
5068 u32 vlan_tag = 0;
5069
5070 if (is_end)
5071 flags |= TXD_FLAG_END;
5072 if (flags & TXD_FLAG_VLAN) {
5073 vlan_tag = flags >> 16;
5074 flags &= 0xffff;
5075 }
5076 vlan_tag |= (mss << TXD_MSS_SHIFT);
5077
5078 txd->addr_hi = ((u64) mapping >> 32);
5079 txd->addr_lo = ((u64) mapping & 0xffffffff);
5080 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5081 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5082}
5083
Michael Chan5a6f3072006-03-20 22:28:05 -08005084/* hard_start_xmit for devices that don't have any bugs and
5085 * support TG3_FLG2_HW_TSO_2 only.
5086 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087static int tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
5088{
5089 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005091 struct skb_shared_info *sp;
5092 dma_addr_t mapping;
Michael Chan5a6f3072006-03-20 22:28:05 -08005093
5094 len = skb_headlen(skb);
5095
Michael Chan00b70502006-06-17 21:58:45 -07005096 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005097 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005098 * interrupt. Furthermore, IRQ processing runs lockless so we have
5099 * no IRQ context deadlocks to worry about either. Rejoice!
5100 */
Michael Chan1b2a7202006-08-07 21:46:02 -07005101 if (unlikely(tg3_tx_avail(tp) <= (skb_shinfo(skb)->nr_frags + 1))) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005102 if (!netif_queue_stopped(dev)) {
5103 netif_stop_queue(dev);
5104
5105 /* This is a hard error, log it. */
5106 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
5107 "queue awake!\n", dev->name);
5108 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005109 return NETDEV_TX_BUSY;
5110 }
5111
5112 entry = tp->tx_prod;
5113 base_flags = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005114 mss = 0;
Matt Carlsonc13e3712007-05-05 11:50:04 -07005115 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005116 int tcp_opt_len, ip_tcp_len;
5117
5118 if (skb_header_cloned(skb) &&
5119 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5120 dev_kfree_skb(skb);
5121 goto out_unlock;
5122 }
5123
Michael Chanb0026622006-07-03 19:42:14 -07005124 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
5125 mss |= (skb_headlen(skb) - ETH_HLEN) << 9;
5126 else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005127 struct iphdr *iph = ip_hdr(skb);
5128
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005129 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005130 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005131
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005132 iph->check = 0;
5133 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Michael Chanb0026622006-07-03 19:42:14 -07005134 mss |= (ip_tcp_len + tcp_opt_len) << 9;
5135 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005136
5137 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5138 TXD_FLAG_CPU_POST_DMA);
5139
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005140 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005141
Michael Chan5a6f3072006-03-20 22:28:05 -08005142 }
Patrick McHardy84fa7932006-08-29 16:44:56 -07005143 else if (skb->ip_summed == CHECKSUM_PARTIAL)
Michael Chan5a6f3072006-03-20 22:28:05 -08005144 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Michael Chan5a6f3072006-03-20 22:28:05 -08005145#if TG3_VLAN_TAG_USED
5146 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5147 base_flags |= (TXD_FLAG_VLAN |
5148 (vlan_tx_tag_get(skb) << 16));
5149#endif
5150
David S. Miller90079ce2008-09-11 04:52:51 -07005151 if (skb_dma_map(&tp->pdev->dev, skb, DMA_TO_DEVICE)) {
5152 dev_kfree_skb(skb);
5153 goto out_unlock;
5154 }
5155
5156 sp = skb_shinfo(skb);
5157
Eric Dumazet042a53a2009-06-05 04:04:16 +00005158 mapping = sp->dma_head;
Michael Chan5a6f3072006-03-20 22:28:05 -08005159
5160 tp->tx_buffers[entry].skb = skb;
Michael Chan5a6f3072006-03-20 22:28:05 -08005161
5162 tg3_set_txd(tp, entry, mapping, len, base_flags,
5163 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5164
5165 entry = NEXT_TX(entry);
5166
5167 /* Now loop through additional data fragments, and queue them. */
5168 if (skb_shinfo(skb)->nr_frags > 0) {
5169 unsigned int i, last;
5170
5171 last = skb_shinfo(skb)->nr_frags - 1;
5172 for (i = 0; i <= last; i++) {
5173 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5174
5175 len = frag->size;
Eric Dumazet042a53a2009-06-05 04:04:16 +00005176 mapping = sp->dma_maps[i];
Michael Chan5a6f3072006-03-20 22:28:05 -08005177 tp->tx_buffers[entry].skb = NULL;
Michael Chan5a6f3072006-03-20 22:28:05 -08005178
5179 tg3_set_txd(tp, entry, mapping, len,
5180 base_flags, (i == last) | (mss << 1));
5181
5182 entry = NEXT_TX(entry);
5183 }
5184 }
5185
5186 /* Packets are ready, update Tx producer idx local and on card. */
5187 tw32_tx_mbox((MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW), entry);
5188
5189 tp->tx_prod = entry;
Michael Chan1b2a7202006-08-07 21:46:02 -07005190 if (unlikely(tg3_tx_avail(tp) <= (MAX_SKB_FRAGS + 1))) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005191 netif_stop_queue(dev);
Ranjit Manomohan42952232006-10-18 20:54:26 -07005192 if (tg3_tx_avail(tp) > TG3_TX_WAKEUP_THRESH(tp))
Michael Chan5a6f3072006-03-20 22:28:05 -08005193 netif_wake_queue(tp->dev);
5194 }
5195
5196out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005197 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005198
5199 return NETDEV_TX_OK;
5200}
5201
Michael Chan52c0fd82006-06-29 20:15:54 -07005202static int tg3_start_xmit_dma_bug(struct sk_buff *, struct net_device *);
5203
5204/* Use GSO to workaround a rare TSO bug that may be triggered when the
5205 * TSO header is greater than 80 bytes.
5206 */
5207static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5208{
5209 struct sk_buff *segs, *nskb;
5210
5211 /* Estimate the number of fragments in the worst case */
Michael Chan1b2a7202006-08-07 21:46:02 -07005212 if (unlikely(tg3_tx_avail(tp) <= (skb_shinfo(skb)->gso_segs * 3))) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005213 netif_stop_queue(tp->dev);
Michael Chan7f62ad52007-02-20 23:25:40 -08005214 if (tg3_tx_avail(tp) <= (skb_shinfo(skb)->gso_segs * 3))
5215 return NETDEV_TX_BUSY;
5216
5217 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005218 }
5219
5220 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005221 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005222 goto tg3_tso_bug_end;
5223
5224 do {
5225 nskb = segs;
5226 segs = segs->next;
5227 nskb->next = NULL;
5228 tg3_start_xmit_dma_bug(nskb, tp->dev);
5229 } while (segs);
5230
5231tg3_tso_bug_end:
5232 dev_kfree_skb(skb);
5233
5234 return NETDEV_TX_OK;
5235}
Michael Chan52c0fd82006-06-29 20:15:54 -07005236
Michael Chan5a6f3072006-03-20 22:28:05 -08005237/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5238 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5239 */
5240static int tg3_start_xmit_dma_bug(struct sk_buff *skb, struct net_device *dev)
5241{
5242 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005243 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005244 struct skb_shared_info *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005246 dma_addr_t mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247
5248 len = skb_headlen(skb);
5249
Michael Chan00b70502006-06-17 21:58:45 -07005250 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005251 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005252 * interrupt. Furthermore, IRQ processing runs lockless so we have
5253 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 */
Michael Chan1b2a7202006-08-07 21:46:02 -07005255 if (unlikely(tg3_tx_avail(tp) <= (skb_shinfo(skb)->nr_frags + 1))) {
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005256 if (!netif_queue_stopped(dev)) {
5257 netif_stop_queue(dev);
5258
5259 /* This is a hard error, log it. */
5260 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
5261 "queue awake!\n", dev->name);
5262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 return NETDEV_TX_BUSY;
5264 }
5265
5266 entry = tp->tx_prod;
5267 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005268 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 mss = 0;
Matt Carlsonc13e3712007-05-05 11:50:04 -07005271 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005272 struct iphdr *iph;
Michael Chan52c0fd82006-06-29 20:15:54 -07005273 int tcp_opt_len, ip_tcp_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274
5275 if (skb_header_cloned(skb) &&
5276 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5277 dev_kfree_skb(skb);
5278 goto out_unlock;
5279 }
5280
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005281 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005282 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283
Michael Chan52c0fd82006-06-29 20:15:54 -07005284 hdr_len = ip_tcp_len + tcp_opt_len;
5285 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005286 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Michael Chan52c0fd82006-06-29 20:15:54 -07005287 return (tg3_tso_bug(tp, skb));
5288
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5290 TXD_FLAG_CPU_POST_DMA);
5291
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005292 iph = ip_hdr(skb);
5293 iph->check = 0;
5294 iph->tot_len = htons(mss + hdr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005296 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005298 } else
5299 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5300 iph->daddr, 0,
5301 IPPROTO_TCP,
5302 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303
5304 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
5305 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005306 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 int tsflags;
5308
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005309 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 mss |= (tsflags << 11);
5311 }
5312 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005313 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 int tsflags;
5315
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005316 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 base_flags |= tsflags << 12;
5318 }
5319 }
5320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321#if TG3_VLAN_TAG_USED
5322 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5323 base_flags |= (TXD_FLAG_VLAN |
5324 (vlan_tx_tag_get(skb) << 16));
5325#endif
5326
David S. Miller90079ce2008-09-11 04:52:51 -07005327 if (skb_dma_map(&tp->pdev->dev, skb, DMA_TO_DEVICE)) {
5328 dev_kfree_skb(skb);
5329 goto out_unlock;
5330 }
5331
5332 sp = skb_shinfo(skb);
5333
Eric Dumazet042a53a2009-06-05 04:04:16 +00005334 mapping = sp->dma_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335
5336 tp->tx_buffers[entry].skb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337
5338 would_hit_hwbug = 0;
5339
Matt Carlson41588ba2008-04-19 18:12:33 -07005340 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
5341 would_hit_hwbug = 1;
5342 else if (tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07005343 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344
5345 tg3_set_txd(tp, entry, mapping, len, base_flags,
5346 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5347
5348 entry = NEXT_TX(entry);
5349
5350 /* Now loop through additional data fragments, and queue them. */
5351 if (skb_shinfo(skb)->nr_frags > 0) {
5352 unsigned int i, last;
5353
5354 last = skb_shinfo(skb)->nr_frags - 1;
5355 for (i = 0; i <= last; i++) {
5356 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5357
5358 len = frag->size;
Eric Dumazet042a53a2009-06-05 04:04:16 +00005359 mapping = sp->dma_maps[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360
5361 tp->tx_buffers[entry].skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362
Michael Chanc58ec932005-09-17 00:46:27 -07005363 if (tg3_4g_overflow_test(mapping, len))
5364 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365
Michael Chan72f2afb2006-03-06 19:28:35 -08005366 if (tg3_40bit_overflow_test(tp, mapping, len))
5367 would_hit_hwbug = 1;
5368
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
5370 tg3_set_txd(tp, entry, mapping, len,
5371 base_flags, (i == last)|(mss << 1));
5372 else
5373 tg3_set_txd(tp, entry, mapping, len,
5374 base_flags, (i == last));
5375
5376 entry = NEXT_TX(entry);
5377 }
5378 }
5379
5380 if (would_hit_hwbug) {
5381 u32 last_plus_one = entry;
5382 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383
Michael Chanc58ec932005-09-17 00:46:27 -07005384 start = entry - 1 - skb_shinfo(skb)->nr_frags;
5385 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386
5387 /* If the workaround fails due to memory/mapping
5388 * failure, silently drop this packet.
5389 */
Michael Chan72f2afb2006-03-06 19:28:35 -08005390 if (tigon3_dma_hwbug_workaround(tp, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07005391 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 goto out_unlock;
5393
5394 entry = start;
5395 }
5396
5397 /* Packets are ready, update Tx producer idx local and on card. */
5398 tw32_tx_mbox((MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW), entry);
5399
5400 tp->tx_prod = entry;
Michael Chan1b2a7202006-08-07 21:46:02 -07005401 if (unlikely(tg3_tx_avail(tp) <= (MAX_SKB_FRAGS + 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 netif_stop_queue(dev);
Ranjit Manomohan42952232006-10-18 20:54:26 -07005403 if (tg3_tx_avail(tp) > TG3_TX_WAKEUP_THRESH(tp))
Michael Chan51b91462005-09-01 17:41:28 -07005404 netif_wake_queue(tp->dev);
5405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
5407out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005408 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409
5410 return NETDEV_TX_OK;
5411}
5412
5413static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
5414 int new_mtu)
5415{
5416 dev->mtu = new_mtu;
5417
Michael Chanef7f5ec2005-07-25 12:32:25 -07005418 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07005419 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005420 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
5421 ethtool_op_set_tso(dev, 0);
5422 }
5423 else
5424 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
5425 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07005426 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07005427 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07005428 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07005429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430}
5431
5432static int tg3_change_mtu(struct net_device *dev, int new_mtu)
5433{
5434 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005435 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436
5437 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
5438 return -EINVAL;
5439
5440 if (!netif_running(dev)) {
5441 /* We'll just catch it later when the
5442 * device is up'd.
5443 */
5444 tg3_set_mtu(dev, tp, new_mtu);
5445 return 0;
5446 }
5447
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005448 tg3_phy_stop(tp);
5449
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005451
5452 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
Michael Chan944d9802005-05-29 14:57:48 -07005454 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
5456 tg3_set_mtu(dev, tp, new_mtu);
5457
Michael Chanb9ec6c12006-07-25 16:37:27 -07005458 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459
Michael Chanb9ec6c12006-07-25 16:37:27 -07005460 if (!err)
5461 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
David S. Millerf47c11e2005-06-24 20:18:35 -07005463 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005465 if (!err)
5466 tg3_phy_start(tp);
5467
Michael Chanb9ec6c12006-07-25 16:37:27 -07005468 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469}
5470
5471/* Free up pending packets in all rx/tx rings.
5472 *
5473 * The chip has been shut down and the driver detached from
5474 * the networking, so no interrupts or new tx packets will
5475 * end up in the driver. tp->{tx,}lock is not held and we are not
5476 * in an interrupt context and thus may sleep.
5477 */
5478static void tg3_free_rings(struct tg3 *tp)
5479{
5480 struct ring_info *rxp;
5481 int i;
5482
5483 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
5484 rxp = &tp->rx_std_buffers[i];
5485
5486 if (rxp->skb == NULL)
5487 continue;
5488 pci_unmap_single(tp->pdev,
5489 pci_unmap_addr(rxp, mapping),
Michael Chan7e72aad2005-07-25 12:31:17 -07005490 tp->rx_pkt_buf_sz - tp->rx_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 PCI_DMA_FROMDEVICE);
5492 dev_kfree_skb_any(rxp->skb);
5493 rxp->skb = NULL;
5494 }
5495
5496 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
5497 rxp = &tp->rx_jumbo_buffers[i];
5498
5499 if (rxp->skb == NULL)
5500 continue;
5501 pci_unmap_single(tp->pdev,
5502 pci_unmap_addr(rxp, mapping),
5503 RX_JUMBO_PKT_BUF_SZ - tp->rx_offset,
5504 PCI_DMA_FROMDEVICE);
5505 dev_kfree_skb_any(rxp->skb);
5506 rxp->skb = NULL;
5507 }
5508
5509 for (i = 0; i < TG3_TX_RING_SIZE; ) {
5510 struct tx_ring_info *txp;
5511 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512
5513 txp = &tp->tx_buffers[i];
5514 skb = txp->skb;
5515
5516 if (skb == NULL) {
5517 i++;
5518 continue;
5519 }
5520
David S. Miller90079ce2008-09-11 04:52:51 -07005521 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
5522
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 txp->skb = NULL;
5524
David S. Miller90079ce2008-09-11 04:52:51 -07005525 i += skb_shinfo(skb)->nr_frags + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526
5527 dev_kfree_skb_any(skb);
5528 }
5529}
5530
5531/* Initialize tx/rx rings for packet processing.
5532 *
5533 * The chip has been shut down and the driver detached from
5534 * the networking, so no interrupts or new tx packets will
5535 * end up in the driver. tp->{tx,}lock are held and thus
5536 * we may not sleep.
5537 */
Michael Chan32d8c572006-07-25 16:38:29 -07005538static int tg3_init_rings(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539{
5540 u32 i;
5541
5542 /* Free up all the SKBs. */
5543 tg3_free_rings(tp);
5544
5545 /* Zero out all descriptors. */
5546 memset(tp->rx_std, 0, TG3_RX_RING_BYTES);
5547 memset(tp->rx_jumbo, 0, TG3_RX_JUMBO_RING_BYTES);
5548 memset(tp->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
5549 memset(tp->tx_ring, 0, TG3_TX_RING_BYTES);
5550
Michael Chan7e72aad2005-07-25 12:31:17 -07005551 tp->rx_pkt_buf_sz = RX_PKT_BUF_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07005552 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Michael Chan7e72aad2005-07-25 12:31:17 -07005553 (tp->dev->mtu > ETH_DATA_LEN))
5554 tp->rx_pkt_buf_sz = RX_JUMBO_PKT_BUF_SZ;
5555
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 /* Initialize invariants of the rings, we only set this
5557 * stuff once. This works because the card does not
5558 * write into the rx buffer posting rings.
5559 */
5560 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
5561 struct tg3_rx_buffer_desc *rxd;
5562
5563 rxd = &tp->rx_std[i];
Michael Chan7e72aad2005-07-25 12:31:17 -07005564 rxd->idx_len = (tp->rx_pkt_buf_sz - tp->rx_offset - 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 << RXD_LEN_SHIFT;
5566 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
5567 rxd->opaque = (RXD_OPAQUE_RING_STD |
5568 (i << RXD_OPAQUE_INDEX_SHIFT));
5569 }
5570
Michael Chan0f893dc2005-07-25 12:30:38 -07005571 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
5573 struct tg3_rx_buffer_desc *rxd;
5574
5575 rxd = &tp->rx_jumbo[i];
5576 rxd->idx_len = (RX_JUMBO_PKT_BUF_SZ - tp->rx_offset - 64)
5577 << RXD_LEN_SHIFT;
5578 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
5579 RXD_FLAG_JUMBO;
5580 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
5581 (i << RXD_OPAQUE_INDEX_SHIFT));
5582 }
5583 }
5584
5585 /* Now allocate fresh SKBs for each rx ring. */
5586 for (i = 0; i < tp->rx_pending; i++) {
Michael Chan32d8c572006-07-25 16:38:29 -07005587 if (tg3_alloc_rx_skb(tp, RXD_OPAQUE_RING_STD, -1, i) < 0) {
5588 printk(KERN_WARNING PFX
5589 "%s: Using a smaller RX standard ring, "
5590 "only %d out of %d buffers were allocated "
5591 "successfully.\n",
5592 tp->dev->name, i, tp->rx_pending);
5593 if (i == 0)
5594 return -ENOMEM;
5595 tp->rx_pending = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 break;
Michael Chan32d8c572006-07-25 16:38:29 -07005597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 }
5599
Michael Chan0f893dc2005-07-25 12:30:38 -07005600 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 for (i = 0; i < tp->rx_jumbo_pending; i++) {
5602 if (tg3_alloc_rx_skb(tp, RXD_OPAQUE_RING_JUMBO,
Michael Chan32d8c572006-07-25 16:38:29 -07005603 -1, i) < 0) {
5604 printk(KERN_WARNING PFX
5605 "%s: Using a smaller RX jumbo ring, "
5606 "only %d out of %d buffers were "
5607 "allocated successfully.\n",
5608 tp->dev->name, i, tp->rx_jumbo_pending);
5609 if (i == 0) {
5610 tg3_free_rings(tp);
5611 return -ENOMEM;
5612 }
5613 tp->rx_jumbo_pending = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 break;
Michael Chan32d8c572006-07-25 16:38:29 -07005615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 }
5617 }
Michael Chan32d8c572006-07-25 16:38:29 -07005618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619}
5620
5621/*
5622 * Must not be invoked with interrupt sources disabled and
5623 * the hardware shutdown down.
5624 */
5625static void tg3_free_consistent(struct tg3 *tp)
5626{
Jesper Juhlb4558ea2005-10-28 16:53:13 -04005627 kfree(tp->rx_std_buffers);
5628 tp->rx_std_buffers = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 if (tp->rx_std) {
5630 pci_free_consistent(tp->pdev, TG3_RX_RING_BYTES,
5631 tp->rx_std, tp->rx_std_mapping);
5632 tp->rx_std = NULL;
5633 }
5634 if (tp->rx_jumbo) {
5635 pci_free_consistent(tp->pdev, TG3_RX_JUMBO_RING_BYTES,
5636 tp->rx_jumbo, tp->rx_jumbo_mapping);
5637 tp->rx_jumbo = NULL;
5638 }
5639 if (tp->rx_rcb) {
5640 pci_free_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
5641 tp->rx_rcb, tp->rx_rcb_mapping);
5642 tp->rx_rcb = NULL;
5643 }
5644 if (tp->tx_ring) {
5645 pci_free_consistent(tp->pdev, TG3_TX_RING_BYTES,
5646 tp->tx_ring, tp->tx_desc_mapping);
5647 tp->tx_ring = NULL;
5648 }
5649 if (tp->hw_status) {
5650 pci_free_consistent(tp->pdev, TG3_HW_STATUS_SIZE,
5651 tp->hw_status, tp->status_mapping);
5652 tp->hw_status = NULL;
5653 }
5654 if (tp->hw_stats) {
5655 pci_free_consistent(tp->pdev, sizeof(struct tg3_hw_stats),
5656 tp->hw_stats, tp->stats_mapping);
5657 tp->hw_stats = NULL;
5658 }
5659}
5660
5661/*
5662 * Must not be invoked with interrupt sources disabled and
5663 * the hardware shutdown down. Can sleep.
5664 */
5665static int tg3_alloc_consistent(struct tg3 *tp)
5666{
Yan Burmanbd2b3342006-12-14 15:25:00 -08005667 tp->rx_std_buffers = kzalloc((sizeof(struct ring_info) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 (TG3_RX_RING_SIZE +
5669 TG3_RX_JUMBO_RING_SIZE)) +
5670 (sizeof(struct tx_ring_info) *
5671 TG3_TX_RING_SIZE),
5672 GFP_KERNEL);
5673 if (!tp->rx_std_buffers)
5674 return -ENOMEM;
5675
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 tp->rx_jumbo_buffers = &tp->rx_std_buffers[TG3_RX_RING_SIZE];
5677 tp->tx_buffers = (struct tx_ring_info *)
5678 &tp->rx_jumbo_buffers[TG3_RX_JUMBO_RING_SIZE];
5679
5680 tp->rx_std = pci_alloc_consistent(tp->pdev, TG3_RX_RING_BYTES,
5681 &tp->rx_std_mapping);
5682 if (!tp->rx_std)
5683 goto err_out;
5684
5685 tp->rx_jumbo = pci_alloc_consistent(tp->pdev, TG3_RX_JUMBO_RING_BYTES,
5686 &tp->rx_jumbo_mapping);
5687
5688 if (!tp->rx_jumbo)
5689 goto err_out;
5690
5691 tp->rx_rcb = pci_alloc_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
5692 &tp->rx_rcb_mapping);
5693 if (!tp->rx_rcb)
5694 goto err_out;
5695
5696 tp->tx_ring = pci_alloc_consistent(tp->pdev, TG3_TX_RING_BYTES,
5697 &tp->tx_desc_mapping);
5698 if (!tp->tx_ring)
5699 goto err_out;
5700
5701 tp->hw_status = pci_alloc_consistent(tp->pdev,
5702 TG3_HW_STATUS_SIZE,
5703 &tp->status_mapping);
5704 if (!tp->hw_status)
5705 goto err_out;
5706
5707 tp->hw_stats = pci_alloc_consistent(tp->pdev,
5708 sizeof(struct tg3_hw_stats),
5709 &tp->stats_mapping);
5710 if (!tp->hw_stats)
5711 goto err_out;
5712
5713 memset(tp->hw_status, 0, TG3_HW_STATUS_SIZE);
5714 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
5715
5716 return 0;
5717
5718err_out:
5719 tg3_free_consistent(tp);
5720 return -ENOMEM;
5721}
5722
5723#define MAX_WAIT_CNT 1000
5724
5725/* To stop a block, clear the enable bit and poll till it
5726 * clears. tp->lock is held.
5727 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07005728static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729{
5730 unsigned int i;
5731 u32 val;
5732
5733 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
5734 switch (ofs) {
5735 case RCVLSC_MODE:
5736 case DMAC_MODE:
5737 case MBFREE_MODE:
5738 case BUFMGR_MODE:
5739 case MEMARB_MODE:
5740 /* We can't enable/disable these bits of the
5741 * 5705/5750, just say success.
5742 */
5743 return 0;
5744
5745 default:
5746 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 }
5749
5750 val = tr32(ofs);
5751 val &= ~enable_bit;
5752 tw32_f(ofs, val);
5753
5754 for (i = 0; i < MAX_WAIT_CNT; i++) {
5755 udelay(100);
5756 val = tr32(ofs);
5757 if ((val & enable_bit) == 0)
5758 break;
5759 }
5760
David S. Millerb3b7d6b2005-05-05 14:40:20 -07005761 if (i == MAX_WAIT_CNT && !silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 printk(KERN_ERR PFX "tg3_stop_block timed out, "
5763 "ofs=%lx enable_bit=%x\n",
5764 ofs, enable_bit);
5765 return -ENODEV;
5766 }
5767
5768 return 0;
5769}
5770
5771/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07005772static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773{
5774 int i, err;
5775
5776 tg3_disable_ints(tp);
5777
5778 tp->rx_mode &= ~RX_MODE_ENABLE;
5779 tw32_f(MAC_RX_MODE, tp->rx_mode);
5780 udelay(10);
5781
David S. Millerb3b7d6b2005-05-05 14:40:20 -07005782 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
5783 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
5784 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
5785 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
5786 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
5787 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788
David S. Millerb3b7d6b2005-05-05 14:40:20 -07005789 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
5790 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
5791 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
5792 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
5793 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
5794 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
5795 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
5797 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
5798 tw32_f(MAC_MODE, tp->mac_mode);
5799 udelay(40);
5800
5801 tp->tx_mode &= ~TX_MODE_ENABLE;
5802 tw32_f(MAC_TX_MODE, tp->tx_mode);
5803
5804 for (i = 0; i < MAX_WAIT_CNT; i++) {
5805 udelay(100);
5806 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
5807 break;
5808 }
5809 if (i >= MAX_WAIT_CNT) {
5810 printk(KERN_ERR PFX "tg3_abort_hw timed out for %s, "
5811 "TX_MODE_ENABLE will not clear MAC_TX_MODE=%08x\n",
5812 tp->dev->name, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07005813 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 }
5815
Michael Chane6de8ad2005-05-05 14:42:41 -07005816 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07005817 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
5818 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819
5820 tw32(FTQ_RESET, 0xffffffff);
5821 tw32(FTQ_RESET, 0x00000000);
5822
David S. Millerb3b7d6b2005-05-05 14:40:20 -07005823 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
5824 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
5826 if (tp->hw_status)
5827 memset(tp->hw_status, 0, TG3_HW_STATUS_SIZE);
5828 if (tp->hw_stats)
5829 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
5830
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 return err;
5832}
5833
Matt Carlson0d3031d2007-10-10 18:02:43 -07005834static void tg3_ape_send_event(struct tg3 *tp, u32 event)
5835{
5836 int i;
5837 u32 apedata;
5838
5839 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
5840 if (apedata != APE_SEG_SIG_MAGIC)
5841 return;
5842
5843 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07005844 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07005845 return;
5846
5847 /* Wait for up to 1 millisecond for APE to service previous event. */
5848 for (i = 0; i < 10; i++) {
5849 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
5850 return;
5851
5852 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
5853
5854 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
5855 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
5856 event | APE_EVENT_STATUS_EVENT_PENDING);
5857
5858 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
5859
5860 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
5861 break;
5862
5863 udelay(100);
5864 }
5865
5866 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
5867 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
5868}
5869
5870static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
5871{
5872 u32 event;
5873 u32 apedata;
5874
5875 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
5876 return;
5877
5878 switch (kind) {
5879 case RESET_KIND_INIT:
5880 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
5881 APE_HOST_SEG_SIG_MAGIC);
5882 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
5883 APE_HOST_SEG_LEN_MAGIC);
5884 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
5885 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
5886 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
5887 APE_HOST_DRIVER_ID_MAGIC);
5888 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
5889 APE_HOST_BEHAV_NO_PHYLOCK);
5890
5891 event = APE_EVENT_STATUS_STATE_START;
5892 break;
5893 case RESET_KIND_SHUTDOWN:
Matt Carlsonb2aee152008-11-03 16:51:11 -08005894 /* With the interface we are currently using,
5895 * APE does not track driver state. Wiping
5896 * out the HOST SEGMENT SIGNATURE forces
5897 * the APE to assume OS absent status.
5898 */
5899 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
5900
Matt Carlson0d3031d2007-10-10 18:02:43 -07005901 event = APE_EVENT_STATUS_STATE_UNLOAD;
5902 break;
5903 case RESET_KIND_SUSPEND:
5904 event = APE_EVENT_STATUS_STATE_SUSPEND;
5905 break;
5906 default:
5907 return;
5908 }
5909
5910 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
5911
5912 tg3_ape_send_event(tp, event);
5913}
5914
Michael Chane6af3012005-04-21 17:12:05 -07005915/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
5917{
David S. Millerf49639e2006-06-09 11:58:36 -07005918 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
5919 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920
5921 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
5922 switch (kind) {
5923 case RESET_KIND_INIT:
5924 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
5925 DRV_STATE_START);
5926 break;
5927
5928 case RESET_KIND_SHUTDOWN:
5929 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
5930 DRV_STATE_UNLOAD);
5931 break;
5932
5933 case RESET_KIND_SUSPEND:
5934 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
5935 DRV_STATE_SUSPEND);
5936 break;
5937
5938 default:
5939 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07005942
5943 if (kind == RESET_KIND_INIT ||
5944 kind == RESET_KIND_SUSPEND)
5945 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946}
5947
5948/* tp->lock is held. */
5949static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
5950{
5951 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
5952 switch (kind) {
5953 case RESET_KIND_INIT:
5954 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
5955 DRV_STATE_START_DONE);
5956 break;
5957
5958 case RESET_KIND_SHUTDOWN:
5959 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
5960 DRV_STATE_UNLOAD_DONE);
5961 break;
5962
5963 default:
5964 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07005967
5968 if (kind == RESET_KIND_SHUTDOWN)
5969 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970}
5971
5972/* tp->lock is held. */
5973static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
5974{
5975 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
5976 switch (kind) {
5977 case RESET_KIND_INIT:
5978 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
5979 DRV_STATE_START);
5980 break;
5981
5982 case RESET_KIND_SHUTDOWN:
5983 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
5984 DRV_STATE_UNLOAD);
5985 break;
5986
5987 case RESET_KIND_SUSPEND:
5988 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
5989 DRV_STATE_SUSPEND);
5990 break;
5991
5992 default:
5993 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 }
5996}
5997
Michael Chan7a6f4362006-09-27 16:03:31 -07005998static int tg3_poll_fw(struct tg3 *tp)
5999{
6000 int i;
6001 u32 val;
6002
Michael Chanb5d37722006-09-27 16:06:21 -07006003 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006004 /* Wait up to 20ms for init done. */
6005 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006006 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6007 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006008 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006009 }
6010 return -ENODEV;
6011 }
6012
Michael Chan7a6f4362006-09-27 16:03:31 -07006013 /* Wait for firmware initialization to complete. */
6014 for (i = 0; i < 100000; i++) {
6015 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6016 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6017 break;
6018 udelay(10);
6019 }
6020
6021 /* Chip might not be fitted with firmware. Some Sun onboard
6022 * parts are configured like that. So don't signal the timeout
6023 * of the above loop as an error, but do report the lack of
6024 * running firmware once.
6025 */
6026 if (i >= 100000 &&
6027 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6028 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6029
6030 printk(KERN_INFO PFX "%s: No firmware running.\n",
6031 tp->dev->name);
6032 }
6033
6034 return 0;
6035}
6036
Michael Chanee6a99b2007-07-18 21:49:10 -07006037/* Save PCI command register before chip reset */
6038static void tg3_save_pci_state(struct tg3 *tp)
6039{
Matt Carlson8a6eac92007-10-21 16:17:55 -07006040 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006041}
6042
6043/* Restore PCI state after chip reset */
6044static void tg3_restore_pci_state(struct tg3 *tp)
6045{
6046 u32 val;
6047
6048 /* Re-enable indirect register accesses. */
6049 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6050 tp->misc_host_ctrl);
6051
6052 /* Set MAX PCI retry to zero. */
6053 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6054 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6055 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6056 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006057 /* Allow reads and writes to the APE register and memory space. */
6058 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6059 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
6060 PCISTATE_ALLOW_APE_SHMEM_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07006061 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6062
Matt Carlson8a6eac92007-10-21 16:17:55 -07006063 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006064
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006065 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6066 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
6067 pcie_set_readrq(tp->pdev, 4096);
6068 else {
6069 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6070 tp->pci_cacheline_sz);
6071 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6072 tp->pci_lat_timer);
6073 }
Michael Chan114342f2007-10-15 02:12:26 -07006074 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08006075
Michael Chanee6a99b2007-07-18 21:49:10 -07006076 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08006077 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07006078 u16 pcix_cmd;
6079
6080 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6081 &pcix_cmd);
6082 pcix_cmd &= ~PCI_X_CMD_ERO;
6083 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6084 pcix_cmd);
6085 }
Michael Chanee6a99b2007-07-18 21:49:10 -07006086
6087 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07006088
6089 /* Chip reset on 5780 will reset MSI enable bit,
6090 * so need to restore it.
6091 */
6092 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
6093 u16 ctrl;
6094
6095 pci_read_config_word(tp->pdev,
6096 tp->msi_cap + PCI_MSI_FLAGS,
6097 &ctrl);
6098 pci_write_config_word(tp->pdev,
6099 tp->msi_cap + PCI_MSI_FLAGS,
6100 ctrl | PCI_MSI_FLAGS_ENABLE);
6101 val = tr32(MSGINT_MODE);
6102 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
6103 }
6104 }
6105}
6106
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107static void tg3_stop_fw(struct tg3 *);
6108
6109/* tp->lock is held. */
6110static int tg3_chip_reset(struct tg3 *tp)
6111{
6112 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07006113 void (*write_op)(struct tg3 *, u32, u32);
Michael Chan7a6f4362006-09-27 16:03:31 -07006114 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115
David S. Millerf49639e2006-06-09 11:58:36 -07006116 tg3_nvram_lock(tp);
6117
Matt Carlson158d7ab2008-05-29 01:37:54 -07006118 tg3_mdio_stop(tp);
6119
Matt Carlson77b483f2008-08-15 14:07:24 -07006120 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
6121
David S. Millerf49639e2006-06-09 11:58:36 -07006122 /* No matching tg3_nvram_unlock() after this because
6123 * chip reset below will undo the nvram lock.
6124 */
6125 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126
Michael Chanee6a99b2007-07-18 21:49:10 -07006127 /* GRC_MISC_CFG core clock reset will clear the memory
6128 * enable bit in PCI register 4 and the MSI enable bit
6129 * on some chips, so we save relevant registers here.
6130 */
6131 tg3_save_pci_state(tp);
6132
Michael Chand9ab5ad12006-03-20 22:27:35 -08006133 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08006134 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08006135 tw32(GRC_FASTBOOT_PC, 0);
6136
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 /*
6138 * We must avoid the readl() that normally takes place.
6139 * It locks machines, causes machine checks, and other
6140 * fun things. So, temporarily disable the 5701
6141 * hardware workaround, while we do the reset.
6142 */
Michael Chan1ee582d2005-08-09 20:16:46 -07006143 write_op = tp->write32;
6144 if (write_op == tg3_write_flush_reg32)
6145 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146
Michael Chand18edcb2007-03-24 20:57:11 -07006147 /* Prevent the irq handler from reading or writing PCI registers
6148 * during chip reset when the memory enable bit in the PCI command
6149 * register may be cleared. The chip does not generate interrupt
6150 * at this time, but the irq handler may still be called due to irq
6151 * sharing or irqpoll.
6152 */
6153 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Michael Chanb8fa2f32007-04-06 17:35:37 -07006154 if (tp->hw_status) {
6155 tp->hw_status->status = 0;
6156 tp->hw_status->status_tag = 0;
6157 }
Michael Chand18edcb2007-03-24 20:57:11 -07006158 tp->last_tag = 0;
Matt Carlson624f8e52009-04-20 06:55:01 +00006159 tp->last_irq_tag = 0;
Michael Chand18edcb2007-03-24 20:57:11 -07006160 smp_mb();
6161 synchronize_irq(tp->pdev->irq);
6162
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 /* do the reset */
6164 val = GRC_MISC_CFG_CORECLK_RESET;
6165
6166 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
6167 if (tr32(0x7e2c) == 0x60) {
6168 tw32(0x7e2c, 0x20);
6169 }
6170 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
6171 tw32(GRC_MISC_CFG, (1 << 29));
6172 val |= (1 << 29);
6173 }
6174 }
6175
Michael Chanb5d37722006-09-27 16:06:21 -07006176 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6177 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
6178 tw32(GRC_VCPU_EXT_CTRL,
6179 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
6180 }
6181
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6183 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
6184 tw32(GRC_MISC_CFG, val);
6185
Michael Chan1ee582d2005-08-09 20:16:46 -07006186 /* restore 5701 hardware bug workaround write method */
6187 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188
6189 /* Unfortunately, we have to delay before the PCI read back.
6190 * Some 575X chips even will not respond to a PCI cfg access
6191 * when the reset command is given to the chip.
6192 *
6193 * How do these hardware designers expect things to work
6194 * properly if the PCI write is posted for a long period
6195 * of time? It is always necessary to have some method by
6196 * which a register read back can occur to push the write
6197 * out which does the reset.
6198 *
6199 * For most tg3 variants the trick below was working.
6200 * Ho hum...
6201 */
6202 udelay(120);
6203
6204 /* Flush PCI posted writes. The normal MMIO registers
6205 * are inaccessible at this time so this is the only
6206 * way to make this reliably (actually, this is no longer
6207 * the case, see above). I tried to use indirect
6208 * register read/write but this upset some 5701 variants.
6209 */
6210 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
6211
6212 udelay(120);
6213
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006214 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
6216 int i;
6217 u32 cfg_val;
6218
6219 /* Wait for link training to complete. */
6220 for (i = 0; i < 5000; i++)
6221 udelay(100);
6222
6223 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
6224 pci_write_config_dword(tp->pdev, 0xc4,
6225 cfg_val | (1 << 15));
6226 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006227
6228 /* Set PCIE max payload size to 128 bytes and
6229 * clear the "no snoop" and "relaxed ordering" bits.
6230 */
6231 pci_write_config_word(tp->pdev,
6232 tp->pcie_cap + PCI_EXP_DEVCTL,
6233 0);
6234
6235 pcie_set_readrq(tp->pdev, 4096);
6236
6237 /* Clear error status */
6238 pci_write_config_word(tp->pdev,
6239 tp->pcie_cap + PCI_EXP_DEVSTA,
6240 PCI_EXP_DEVSTA_CED |
6241 PCI_EXP_DEVSTA_NFED |
6242 PCI_EXP_DEVSTA_FED |
6243 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244 }
6245
Michael Chanee6a99b2007-07-18 21:49:10 -07006246 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247
Michael Chand18edcb2007-03-24 20:57:11 -07006248 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
6249
Michael Chanee6a99b2007-07-18 21:49:10 -07006250 val = 0;
6251 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07006252 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07006253 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254
6255 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
6256 tg3_stop_fw(tp);
6257 tw32(0x5000, 0x400);
6258 }
6259
6260 tw32(GRC_MODE, tp->grc_mode);
6261
6262 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01006263 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264
6265 tw32(0xc4, val | (1 << 15));
6266 }
6267
6268 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
6269 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
6270 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
6271 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
6272 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
6273 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
6274 }
6275
6276 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
6277 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
6278 tw32_f(MAC_MODE, tp->mac_mode);
Michael Chan747e8f82005-07-25 12:33:22 -07006279 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
6280 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
6281 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlson3bda1252008-08-15 14:08:22 -07006282 } else if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
6283 tp->mac_mode &= (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
6284 if (tp->mac_mode & MAC_MODE_APE_TX_EN)
6285 tp->mac_mode |= MAC_MODE_TDE_ENABLE;
6286 tw32_f(MAC_MODE, tp->mac_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287 } else
6288 tw32_f(MAC_MODE, 0);
6289 udelay(40);
6290
Matt Carlson158d7ab2008-05-29 01:37:54 -07006291 tg3_mdio_start(tp);
6292
Matt Carlson77b483f2008-08-15 14:07:24 -07006293 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
6294
Michael Chan7a6f4362006-09-27 16:03:31 -07006295 err = tg3_poll_fw(tp);
6296 if (err)
6297 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298
6299 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
6300 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01006301 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302
6303 tw32(0x7c00, val | (1 << 25));
6304 }
6305
6306 /* Reprobe ASF enable state. */
6307 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
6308 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
6309 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
6310 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
6311 u32 nic_cfg;
6312
6313 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
6314 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
6315 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07006316 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07006317 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
6319 }
6320 }
6321
6322 return 0;
6323}
6324
6325/* tp->lock is held. */
6326static void tg3_stop_fw(struct tg3 *tp)
6327{
Matt Carlson0d3031d2007-10-10 18:02:43 -07006328 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
6329 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07006330 /* Wait for RX cpu to ACK the previous event. */
6331 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332
6333 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07006334
6335 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336
Matt Carlson7c5026a2008-05-02 16:49:29 -07006337 /* Wait for RX cpu to ACK this event. */
6338 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 }
6340}
6341
6342/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07006343static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344{
6345 int err;
6346
6347 tg3_stop_fw(tp);
6348
Michael Chan944d9802005-05-29 14:57:48 -07006349 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006351 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 err = tg3_chip_reset(tp);
6353
Matt Carlsondaba2a62009-04-20 06:58:52 +00006354 __tg3_set_mac_addr(tp, 0);
6355
Michael Chan944d9802005-05-29 14:57:48 -07006356 tg3_write_sig_legacy(tp, kind);
6357 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358
6359 if (err)
6360 return err;
6361
6362 return 0;
6363}
6364
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365#define RX_CPU_SCRATCH_BASE 0x30000
6366#define RX_CPU_SCRATCH_SIZE 0x04000
6367#define TX_CPU_SCRATCH_BASE 0x34000
6368#define TX_CPU_SCRATCH_SIZE 0x04000
6369
6370/* tp->lock is held. */
6371static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
6372{
6373 int i;
6374
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02006375 BUG_ON(offset == TX_CPU_BASE &&
6376 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
Michael Chanb5d37722006-09-27 16:06:21 -07006378 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6379 u32 val = tr32(GRC_VCPU_EXT_CTRL);
6380
6381 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
6382 return 0;
6383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 if (offset == RX_CPU_BASE) {
6385 for (i = 0; i < 10000; i++) {
6386 tw32(offset + CPU_STATE, 0xffffffff);
6387 tw32(offset + CPU_MODE, CPU_MODE_HALT);
6388 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
6389 break;
6390 }
6391
6392 tw32(offset + CPU_STATE, 0xffffffff);
6393 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
6394 udelay(10);
6395 } else {
6396 for (i = 0; i < 10000; i++) {
6397 tw32(offset + CPU_STATE, 0xffffffff);
6398 tw32(offset + CPU_MODE, CPU_MODE_HALT);
6399 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
6400 break;
6401 }
6402 }
6403
6404 if (i >= 10000) {
6405 printk(KERN_ERR PFX "tg3_reset_cpu timed out for %s, "
6406 "and %s CPU\n",
6407 tp->dev->name,
6408 (offset == RX_CPU_BASE ? "RX" : "TX"));
6409 return -ENODEV;
6410 }
Michael Chanec41c7d2006-01-17 02:40:55 -08006411
6412 /* Clear firmware's nvram arbitration. */
6413 if (tp->tg3_flags & TG3_FLAG_NVRAM)
6414 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415 return 0;
6416}
6417
6418struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006419 unsigned int fw_base;
6420 unsigned int fw_len;
6421 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422};
6423
6424/* tp->lock is held. */
6425static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
6426 int cpu_scratch_size, struct fw_info *info)
6427{
Michael Chanec41c7d2006-01-17 02:40:55 -08006428 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 void (*write_op)(struct tg3 *, u32, u32);
6430
6431 if (cpu_base == TX_CPU_BASE &&
6432 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
6433 printk(KERN_ERR PFX "tg3_load_firmware_cpu: Trying to load "
6434 "TX cpu firmware on %s which is 5705.\n",
6435 tp->dev->name);
6436 return -EINVAL;
6437 }
6438
6439 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6440 write_op = tg3_write_mem;
6441 else
6442 write_op = tg3_write_indirect_reg32;
6443
Michael Chan1b628152005-05-29 14:59:49 -07006444 /* It is possible that bootcode is still loading at this point.
6445 * Get the nvram lock first before halting the cpu.
6446 */
Michael Chanec41c7d2006-01-17 02:40:55 -08006447 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08006449 if (!lock_err)
6450 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 if (err)
6452 goto out;
6453
6454 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
6455 write_op(tp, cpu_scratch_base + i, 0);
6456 tw32(cpu_base + CPU_STATE, 0xffffffff);
6457 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006458 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006460 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006462 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463
6464 err = 0;
6465
6466out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 return err;
6468}
6469
6470/* tp->lock is held. */
6471static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
6472{
6473 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006474 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 int err, i;
6476
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006477 fw_data = (void *)tp->fw->data;
6478
6479 /* Firmware blob starts with version numbers, followed by
6480 start address and length. We are setting complete length.
6481 length = end_address_of_bss - start_address_of_text.
6482 Remainder is the blob to be loaded contiguously
6483 from start address. */
6484
6485 info.fw_base = be32_to_cpu(fw_data[1]);
6486 info.fw_len = tp->fw->size - 12;
6487 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488
6489 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
6490 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
6491 &info);
6492 if (err)
6493 return err;
6494
6495 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
6496 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
6497 &info);
6498 if (err)
6499 return err;
6500
6501 /* Now startup only the RX cpu. */
6502 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006503 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504
6505 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006506 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 break;
6508 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
6509 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006510 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511 udelay(1000);
6512 }
6513 if (i >= 5) {
6514 printk(KERN_ERR PFX "tg3_load_firmware fails for %s "
6515 "to set RX CPU PC, is %08x should be %08x\n",
6516 tp->dev->name, tr32(RX_CPU_BASE + CPU_PC),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006517 info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 return -ENODEV;
6519 }
6520 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
6521 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
6522
6523 return 0;
6524}
6525
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527
6528/* tp->lock is held. */
6529static int tg3_load_tso_firmware(struct tg3 *tp)
6530{
6531 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006532 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
6534 int err, i;
6535
6536 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
6537 return 0;
6538
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006539 fw_data = (void *)tp->fw->data;
6540
6541 /* Firmware blob starts with version numbers, followed by
6542 start address and length. We are setting complete length.
6543 length = end_address_of_bss - start_address_of_text.
6544 Remainder is the blob to be loaded contiguously
6545 from start address. */
6546
6547 info.fw_base = be32_to_cpu(fw_data[1]);
6548 cpu_scratch_size = tp->fw_len;
6549 info.fw_len = tp->fw->size - 12;
6550 info.fw_data = &fw_data[3];
6551
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553 cpu_base = RX_CPU_BASE;
6554 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 cpu_base = TX_CPU_BASE;
6557 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
6558 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
6559 }
6560
6561 err = tg3_load_firmware_cpu(tp, cpu_base,
6562 cpu_scratch_base, cpu_scratch_size,
6563 &info);
6564 if (err)
6565 return err;
6566
6567 /* Now startup the cpu. */
6568 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006569 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570
6571 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006572 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 break;
6574 tw32(cpu_base + CPU_STATE, 0xffffffff);
6575 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006576 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 udelay(1000);
6578 }
6579 if (i >= 5) {
6580 printk(KERN_ERR PFX "tg3_load_tso_firmware fails for %s "
6581 "to set CPU PC, is %08x should be %08x\n",
6582 tp->dev->name, tr32(cpu_base + CPU_PC),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006583 info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584 return -ENODEV;
6585 }
6586 tw32(cpu_base + CPU_STATE, 0xffffffff);
6587 tw32_f(cpu_base + CPU_MODE, 0x00000000);
6588 return 0;
6589}
6590
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592static int tg3_set_mac_addr(struct net_device *dev, void *p)
6593{
6594 struct tg3 *tp = netdev_priv(dev);
6595 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07006596 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597
Michael Chanf9804dd2005-09-27 12:13:10 -07006598 if (!is_valid_ether_addr(addr->sa_data))
6599 return -EINVAL;
6600
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
6602
Michael Chane75f7c92006-03-20 21:33:26 -08006603 if (!netif_running(dev))
6604 return 0;
6605
Michael Chan58712ef2006-04-29 18:58:01 -07006606 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07006607 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07006608
Michael Chan986e0ae2007-05-05 12:10:20 -07006609 addr0_high = tr32(MAC_ADDR_0_HIGH);
6610 addr0_low = tr32(MAC_ADDR_0_LOW);
6611 addr1_high = tr32(MAC_ADDR_1_HIGH);
6612 addr1_low = tr32(MAC_ADDR_1_LOW);
6613
6614 /* Skip MAC addr 1 if ASF is using it. */
6615 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
6616 !(addr1_high == 0 && addr1_low == 0))
6617 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07006618 }
Michael Chan986e0ae2007-05-05 12:10:20 -07006619 spin_lock_bh(&tp->lock);
6620 __tg3_set_mac_addr(tp, skip_mac_1);
6621 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622
Michael Chanb9ec6c12006-07-25 16:37:27 -07006623 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624}
6625
6626/* tp->lock is held. */
6627static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
6628 dma_addr_t mapping, u32 maxlen_flags,
6629 u32 nic_addr)
6630{
6631 tg3_write_mem(tp,
6632 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
6633 ((u64) mapping >> 32));
6634 tg3_write_mem(tp,
6635 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
6636 ((u64) mapping & 0xffffffff));
6637 tg3_write_mem(tp,
6638 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
6639 maxlen_flags);
6640
6641 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
6642 tg3_write_mem(tp,
6643 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
6644 nic_addr);
6645}
6646
6647static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07006648static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07006649{
6650 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
6651 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
6652 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
6653 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
6654 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
6655 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
6656 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
6657 }
6658 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
6659 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
6660 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
6661 u32 val = ec->stats_block_coalesce_usecs;
6662
6663 if (!netif_carrier_ok(tp->dev))
6664 val = 0;
6665
6666 tw32(HOSTCC_STAT_COAL_TICKS, val);
6667 }
6668}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669
6670/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07006671static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672{
6673 u32 val, rdmac_mode;
6674 int i, err, limit;
6675
6676 tg3_disable_ints(tp);
6677
6678 tg3_stop_fw(tp);
6679
6680 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
6681
6682 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) {
Michael Chane6de8ad2005-05-05 14:42:41 -07006683 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 }
6685
Matt Carlsondd477002008-05-25 23:45:58 -07006686 if (reset_phy &&
6687 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB))
Michael Chand4d2c552006-03-20 17:47:20 -08006688 tg3_phy_reset(tp);
6689
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 err = tg3_chip_reset(tp);
6691 if (err)
6692 return err;
6693
6694 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
6695
Matt Carlsonbcb37f62008-11-03 16:52:09 -08006696 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07006697 val = tr32(TG3_CPMU_CTRL);
6698 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
6699 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08006700
6701 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
6702 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
6703 val |= CPMU_LSPD_10MB_MACCLK_6_25;
6704 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
6705
6706 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
6707 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
6708 val |= CPMU_LNK_AWARE_MACCLK_6_25;
6709 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
6710
6711 val = tr32(TG3_CPMU_HST_ACC);
6712 val &= ~CPMU_HST_ACC_MACCLK_MASK;
6713 val |= CPMU_HST_ACC_MACCLK_6_25;
6714 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07006715 }
6716
Matt Carlson33466d92009-04-20 06:57:41 +00006717 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6718 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
6719 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
6720 PCIE_PWR_MGMT_L1_THRESH_4MS;
6721 tw32(PCIE_PWR_MGMT_THRESH, val);
6722 }
6723
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724 /* This works around an issue with Athlon chipsets on
6725 * B3 tigon3 silicon. This bit has no effect on any
6726 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07006727 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 */
Matt Carlson795d01c2007-10-07 23:28:17 -07006729 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
6730 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
6731 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
6732 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
6733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734
6735 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6736 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
6737 val = tr32(TG3PCI_PCISTATE);
6738 val |= PCISTATE_RETRY_SAME_DMA;
6739 tw32(TG3PCI_PCISTATE, val);
6740 }
6741
Matt Carlson0d3031d2007-10-10 18:02:43 -07006742 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
6743 /* Allow reads and writes to the
6744 * APE register and memory space.
6745 */
6746 val = tr32(TG3PCI_PCISTATE);
6747 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
6748 PCISTATE_ALLOW_APE_SHMEM_WR;
6749 tw32(TG3PCI_PCISTATE, val);
6750 }
6751
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
6753 /* Enable some hw fixes. */
6754 val = tr32(TG3PCI_MSI_DATA);
6755 val |= (1 << 26) | (1 << 28) | (1 << 29);
6756 tw32(TG3PCI_MSI_DATA, val);
6757 }
6758
6759 /* Descriptor ring init may make accesses to the
6760 * NIC SRAM area to setup the TX descriptors, so we
6761 * can only do this after the hardware has been
6762 * successfully reset.
6763 */
Michael Chan32d8c572006-07-25 16:38:29 -07006764 err = tg3_init_rings(tp);
6765 if (err)
6766 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767
Matt Carlson9936bcf2007-10-10 18:03:07 -07006768 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006769 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07006770 /* This value is determined during the probe time DMA
6771 * engine test, tg3_test_dma.
6772 */
6773 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
6774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775
6776 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
6777 GRC_MODE_4X_NIC_SEND_RINGS |
6778 GRC_MODE_NO_TX_PHDR_CSUM |
6779 GRC_MODE_NO_RX_PHDR_CSUM);
6780 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07006781
6782 /* Pseudo-header checksum is done by hardware logic and not
6783 * the offload processers, so make the chip do the pseudo-
6784 * header checksums on receive. For transmit it is more
6785 * convenient to do the pseudo-header checksum in software
6786 * as Linux does that on transmit for us in all cases.
6787 */
6788 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789
6790 tw32(GRC_MODE,
6791 tp->grc_mode |
6792 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
6793
6794 /* Setup the timer prescalar register. Clock is always 66Mhz. */
6795 val = tr32(GRC_MISC_CFG);
6796 val &= ~0xff;
6797 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
6798 tw32(GRC_MISC_CFG, val);
6799
6800 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07006801 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802 /* Do nothing. */
6803 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
6804 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
6805 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
6806 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
6807 else
6808 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
6809 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
6810 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
6811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
6813 int fw_len;
6814
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006815 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
6817 tw32(BUFMGR_MB_POOL_ADDR,
6818 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
6819 tw32(BUFMGR_MB_POOL_SIZE,
6820 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
6821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822
Michael Chan0f893dc2005-07-25 12:30:38 -07006823 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824 tw32(BUFMGR_MB_RDMA_LOW_WATER,
6825 tp->bufmgr_config.mbuf_read_dma_low_water);
6826 tw32(BUFMGR_MB_MACRX_LOW_WATER,
6827 tp->bufmgr_config.mbuf_mac_rx_low_water);
6828 tw32(BUFMGR_MB_HIGH_WATER,
6829 tp->bufmgr_config.mbuf_high_water);
6830 } else {
6831 tw32(BUFMGR_MB_RDMA_LOW_WATER,
6832 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
6833 tw32(BUFMGR_MB_MACRX_LOW_WATER,
6834 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
6835 tw32(BUFMGR_MB_HIGH_WATER,
6836 tp->bufmgr_config.mbuf_high_water_jumbo);
6837 }
6838 tw32(BUFMGR_DMA_LOW_WATER,
6839 tp->bufmgr_config.dma_low_water);
6840 tw32(BUFMGR_DMA_HIGH_WATER,
6841 tp->bufmgr_config.dma_high_water);
6842
6843 tw32(BUFMGR_MODE, BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE);
6844 for (i = 0; i < 2000; i++) {
6845 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
6846 break;
6847 udelay(10);
6848 }
6849 if (i >= 2000) {
6850 printk(KERN_ERR PFX "tg3_reset_hw cannot enable BUFMGR for %s.\n",
6851 tp->dev->name);
6852 return -ENODEV;
6853 }
6854
6855 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07006856 val = tp->rx_pending / 8;
6857 if (val == 0)
6858 val = 1;
6859 else if (val > tp->rx_std_max_post)
6860 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07006861 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6862 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
6863 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
6864
6865 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
6866 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
6867 }
Michael Chanf92905d2006-06-29 20:14:29 -07006868
6869 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870
6871 /* Initialize TG3_BDINFO's at:
6872 * RCVDBDI_STD_BD: standard eth size rx ring
6873 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
6874 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
6875 *
6876 * like so:
6877 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
6878 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
6879 * ring attribute flags
6880 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
6881 *
6882 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
6883 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
6884 *
6885 * The size of each ring is fixed in the firmware, but the location is
6886 * configurable.
6887 */
6888 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
6889 ((u64) tp->rx_std_mapping >> 32));
6890 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
6891 ((u64) tp->rx_std_mapping & 0xffffffff));
6892 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
6893 NIC_SRAM_RX_BUFFER_DESC);
6894
6895 /* Don't even try to program the JUMBO/MINI buffer descriptor
6896 * configs on 5705.
6897 */
6898 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6899 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS,
6900 RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT);
6901 } else {
6902 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS,
6903 RX_STD_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT);
6904
6905 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
6906 BDINFO_FLAGS_DISABLED);
6907
6908 /* Setup replenish threshold. */
6909 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
6910
Michael Chan0f893dc2005-07-25 12:30:38 -07006911 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
6913 ((u64) tp->rx_jumbo_mapping >> 32));
6914 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
6915 ((u64) tp->rx_jumbo_mapping & 0xffffffff));
6916 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
6917 RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT);
6918 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
6919 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
6920 } else {
6921 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
6922 BDINFO_FLAGS_DISABLED);
6923 }
6924
6925 }
6926
6927 /* There is only one send ring on 5705/5750, no need to explicitly
6928 * disable the others.
6929 */
6930 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
6931 /* Clear out send RCB ring in SRAM. */
6932 for (i = NIC_SRAM_SEND_RCB; i < NIC_SRAM_RCV_RET_RCB; i += TG3_BDINFO_SIZE)
6933 tg3_write_mem(tp, i + TG3_BDINFO_MAXLEN_FLAGS,
6934 BDINFO_FLAGS_DISABLED);
6935 }
6936
6937 tp->tx_prod = 0;
6938 tp->tx_cons = 0;
6939 tw32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW, 0);
6940 tw32_tx_mbox(MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW, 0);
6941
6942 tg3_set_bdinfo(tp, NIC_SRAM_SEND_RCB,
6943 tp->tx_desc_mapping,
6944 (TG3_TX_RING_SIZE <<
6945 BDINFO_FLAGS_MAXLEN_SHIFT),
6946 NIC_SRAM_TX_BUFFER_DESC);
6947
6948 /* There is only one receive return ring on 5705/5750, no need
6949 * to explicitly disable the others.
6950 */
6951 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
6952 for (i = NIC_SRAM_RCV_RET_RCB; i < NIC_SRAM_STATS_BLK;
6953 i += TG3_BDINFO_SIZE) {
6954 tg3_write_mem(tp, i + TG3_BDINFO_MAXLEN_FLAGS,
6955 BDINFO_FLAGS_DISABLED);
6956 }
6957 }
6958
6959 tp->rx_rcb_ptr = 0;
6960 tw32_rx_mbox(MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW, 0);
6961
6962 tg3_set_bdinfo(tp, NIC_SRAM_RCV_RET_RCB,
6963 tp->rx_rcb_mapping,
6964 (TG3_RX_RCB_RING_SIZE(tp) <<
6965 BDINFO_FLAGS_MAXLEN_SHIFT),
6966 0);
6967
6968 tp->rx_std_ptr = tp->rx_pending;
6969 tw32_rx_mbox(MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW,
6970 tp->rx_std_ptr);
6971
Michael Chan0f893dc2005-07-25 12:30:38 -07006972 tp->rx_jumbo_ptr = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 tp->rx_jumbo_pending : 0;
6974 tw32_rx_mbox(MAILBOX_RCV_JUMBO_PROD_IDX + TG3_64BIT_REG_LOW,
6975 tp->rx_jumbo_ptr);
6976
6977 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07006978 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979
6980 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006981 tw32(MAC_RX_MTU_SIZE,
6982 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983
6984 /* The slot time is changed by tg3_setup_phy if we
6985 * run at gigabit with half duplex.
6986 */
6987 tw32(MAC_TX_LENGTHS,
6988 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
6989 (6 << TX_LENGTHS_IPG_SHIFT) |
6990 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
6991
6992 /* Receive rules. */
6993 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
6994 tw32(RCVLPC_CONFIG, 0x0181);
6995
6996 /* Calculate RDMAC_MODE setting early, we need it to determine
6997 * the RCVLPC_STATE_ENABLE mask.
6998 */
6999 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
7000 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
7001 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
7002 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
7003 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07007004
Matt Carlson57e69832008-05-25 23:48:31 -07007005 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007006 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7007 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07007008 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
7009 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
7010 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
7011
Michael Chan85e94ce2005-04-21 17:05:28 -07007012 /* If statement applies to 5705 and 5750 PCI devices only */
7013 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7014 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7015 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07007017 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
7019 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7020 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
7021 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7022 }
7023 }
7024
Michael Chan85e94ce2005-04-21 17:05:28 -07007025 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
7026 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7027
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08007029 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
7030
7031 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7032 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
7033 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034
7035 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07007036 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
7037 val = tr32(RCVLPC_STATS_ENABLE);
7038 val &= ~RCVLPC_STATSENAB_DACK_FIX;
7039 tw32(RCVLPC_STATS_ENABLE, val);
7040 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
7041 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042 val = tr32(RCVLPC_STATS_ENABLE);
7043 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
7044 tw32(RCVLPC_STATS_ENABLE, val);
7045 } else {
7046 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
7047 }
7048 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
7049 tw32(SNDDATAI_STATSENAB, 0xffffff);
7050 tw32(SNDDATAI_STATSCTRL,
7051 (SNDDATAI_SCTRL_ENABLE |
7052 SNDDATAI_SCTRL_FASTUPD));
7053
7054 /* Setup host coalescing engine. */
7055 tw32(HOSTCC_MODE, 0);
7056 for (i = 0; i < 2000; i++) {
7057 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
7058 break;
7059 udelay(10);
7060 }
7061
Michael Chand244c892005-07-05 14:42:33 -07007062 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063
7064 /* set status block DMA address */
7065 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7066 ((u64) tp->status_mapping >> 32));
7067 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7068 ((u64) tp->status_mapping & 0xffffffff));
7069
7070 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7071 /* Status/statistics block address. See tg3_timer,
7072 * the tg3_periodic_fetch_stats call there, and
7073 * tg3_get_stats to see how this works for 5705/5750 chips.
7074 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7076 ((u64) tp->stats_mapping >> 32));
7077 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7078 ((u64) tp->stats_mapping & 0xffffffff));
7079 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
7080 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
7081 }
7082
7083 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
7084
7085 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
7086 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
7087 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7088 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
7089
7090 /* Clear statistics/status block in chip, and status block in ram. */
7091 for (i = NIC_SRAM_STATS_BLK;
7092 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
7093 i += sizeof(u32)) {
7094 tg3_write_mem(tp, i, 0);
7095 udelay(40);
7096 }
7097 memset(tp->hw_status, 0, TG3_HW_STATUS_SIZE);
7098
Michael Chanc94e3942005-09-27 12:12:42 -07007099 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
7100 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
7101 /* reset to prevent losing 1st rx packet intermittently */
7102 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7103 udelay(10);
7104 }
7105
Matt Carlson3bda1252008-08-15 14:08:22 -07007106 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7107 tp->mac_mode &= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
7108 else
7109 tp->mac_mode = 0;
7110 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07007112 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
7113 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
7114 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
7115 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
7117 udelay(40);
7118
Michael Chan314fba32005-04-21 17:07:04 -07007119 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08007120 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07007121 * register to preserve the GPIO settings for LOMs. The GPIOs,
7122 * whether used as inputs or outputs, are set by boot code after
7123 * reset.
7124 */
Michael Chan9d26e212006-12-07 00:21:14 -08007125 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07007126 u32 gpio_mask;
7127
Michael Chan9d26e212006-12-07 00:21:14 -08007128 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
7129 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
7130 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07007131
7132 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7133 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
7134 GRC_LCLCTRL_GPIO_OUTPUT3;
7135
Michael Chanaf36e6b2006-03-23 01:28:06 -08007136 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
7137 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
7138
Gary Zambranoaaf84462007-05-05 11:51:45 -07007139 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07007140 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
7141
7142 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08007143 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
7144 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
7145 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07007146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
7148 udelay(100);
7149
Michael Chan09ee9292005-08-09 20:17:00 -07007150 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151
7152 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7153 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
7154 udelay(40);
7155 }
7156
7157 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
7158 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
7159 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
7160 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
7161 WDMAC_MODE_LNGREAD_ENAB);
7162
Michael Chan85e94ce2005-04-21 17:05:28 -07007163 /* If statement applies to 5705 and 5750 PCI devices only */
7164 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7165 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7166 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167 if ((tp->tg3_flags & TG3_FLG2_TSO_CAPABLE) &&
7168 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
7169 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
7170 /* nothing */
7171 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7172 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
7173 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
7174 val |= WDMAC_MODE_RX_ACCEL;
7175 }
7176 }
7177
Michael Chand9ab5ad12006-03-20 22:27:35 -08007178 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08007179 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07007180 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08007181
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182 tw32_f(WDMAC_MODE, val);
7183 udelay(40);
7184
Matt Carlson9974a352007-10-07 23:27:28 -07007185 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
7186 u16 pcix_cmd;
7187
7188 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7189 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07007191 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
7192 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07007194 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
7195 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196 }
Matt Carlson9974a352007-10-07 23:27:28 -07007197 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7198 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 }
7200
7201 tw32_f(RDMAC_MODE, rdmac_mode);
7202 udelay(40);
7203
7204 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
7205 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7206 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07007207
7208 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
7209 tw32(SNDDATAC_MODE,
7210 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
7211 else
7212 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
7213
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
7215 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
7216 tw32(RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ);
7217 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7219 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220 tw32(SNDBDI_MODE, SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE);
7221 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
7222
7223 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
7224 err = tg3_load_5701_a0_firmware_fix(tp);
7225 if (err)
7226 return err;
7227 }
7228
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
7230 err = tg3_load_tso_firmware(tp);
7231 if (err)
7232 return err;
7233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234
7235 tp->tx_mode = TX_MODE_ENABLE;
7236 tw32_f(MAC_TX_MODE, tp->tx_mode);
7237 udelay(100);
7238
7239 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08007240 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08007241 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
7242
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243 tw32_f(MAC_RX_MODE, tp->rx_mode);
7244 udelay(10);
7245
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246 tw32(MAC_LED_CTRL, tp->led_ctrl);
7247
7248 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Michael Chanc94e3942005-09-27 12:12:42 -07007249 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7251 udelay(10);
7252 }
7253 tw32_f(MAC_RX_MODE, tp->rx_mode);
7254 udelay(10);
7255
7256 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
7257 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
7258 !(tp->tg3_flags2 & TG3_FLG2_SERDES_PREEMPHASIS)) {
7259 /* Set drive transmission level to 1.2V */
7260 /* only if the signal pre-emphasis bit is not set */
7261 val = tr32(MAC_SERDES_CFG);
7262 val &= 0xfffff000;
7263 val |= 0x880;
7264 tw32(MAC_SERDES_CFG, val);
7265 }
7266 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
7267 tw32(MAC_SERDES_CFG, 0x616000);
7268 }
7269
7270 /* Prevent chip from dropping frames when flow control
7271 * is enabled.
7272 */
7273 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, 2);
7274
7275 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
7276 (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
7277 /* Use hardware link auto-negotiation */
7278 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
7279 }
7280
Michael Chand4d2c552006-03-20 17:47:20 -08007281 if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
7282 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
7283 u32 tmp;
7284
7285 tmp = tr32(SERDES_RX_CTRL);
7286 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
7287 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
7288 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
7289 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
7290 }
7291
Matt Carlsondd477002008-05-25 23:45:58 -07007292 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
7293 if (tp->link_config.phy_is_low_power) {
7294 tp->link_config.phy_is_low_power = 0;
7295 tp->link_config.speed = tp->link_config.orig_speed;
7296 tp->link_config.duplex = tp->link_config.orig_duplex;
7297 tp->link_config.autoneg = tp->link_config.orig_autoneg;
7298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007299
Matt Carlsondd477002008-05-25 23:45:58 -07007300 err = tg3_setup_phy(tp, 0);
7301 if (err)
7302 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303
Matt Carlsondd477002008-05-25 23:45:58 -07007304 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
7305 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5906) {
7306 u32 tmp;
7307
7308 /* Clear CRC stats. */
7309 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
7310 tg3_writephy(tp, MII_TG3_TEST1,
7311 tmp | MII_TG3_TEST1_CRC_EN);
7312 tg3_readphy(tp, 0x14, &tmp);
7313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314 }
7315 }
7316
7317 __tg3_set_rx_mode(tp->dev);
7318
7319 /* Initialize receive rules. */
7320 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
7321 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
7322 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
7323 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
7324
Michael Chan4cf78e42005-07-25 12:29:19 -07007325 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07007326 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327 limit = 8;
7328 else
7329 limit = 16;
7330 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
7331 limit -= 4;
7332 switch (limit) {
7333 case 16:
7334 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
7335 case 15:
7336 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
7337 case 14:
7338 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
7339 case 13:
7340 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
7341 case 12:
7342 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
7343 case 11:
7344 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
7345 case 10:
7346 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
7347 case 9:
7348 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
7349 case 8:
7350 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
7351 case 7:
7352 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
7353 case 6:
7354 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
7355 case 5:
7356 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
7357 case 4:
7358 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
7359 case 3:
7360 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
7361 case 2:
7362 case 1:
7363
7364 default:
7365 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367
Matt Carlson9ce768e2007-10-11 19:49:11 -07007368 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7369 /* Write our heartbeat update interval to APE. */
7370 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
7371 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07007372
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
7374
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 return 0;
7376}
7377
7378/* Called at device open time to get the chip ready for
7379 * packet processing. Invoked with tp->lock held.
7380 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007381static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383 tg3_switch_clocks(tp);
7384
7385 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
7386
Matt Carlson2f751b62008-08-04 23:17:34 -07007387 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388}
7389
7390#define TG3_STAT_ADD32(PSTAT, REG) \
7391do { u32 __val = tr32(REG); \
7392 (PSTAT)->low += __val; \
7393 if ((PSTAT)->low < __val) \
7394 (PSTAT)->high += 1; \
7395} while (0)
7396
7397static void tg3_periodic_fetch_stats(struct tg3 *tp)
7398{
7399 struct tg3_hw_stats *sp = tp->hw_stats;
7400
7401 if (!netif_carrier_ok(tp->dev))
7402 return;
7403
7404 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
7405 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
7406 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
7407 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
7408 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
7409 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
7410 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
7411 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
7412 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
7413 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
7414 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
7415 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
7416 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
7417
7418 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
7419 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
7420 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
7421 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
7422 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
7423 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
7424 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
7425 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
7426 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
7427 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
7428 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
7429 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
7430 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
7431 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07007432
7433 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
7434 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
7435 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436}
7437
7438static void tg3_timer(unsigned long __opaque)
7439{
7440 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441
Michael Chanf475f162006-03-27 23:20:14 -08007442 if (tp->irq_sync)
7443 goto restart_timer;
7444
David S. Millerf47c11e2005-06-24 20:18:35 -07007445 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446
David S. Millerfac9b832005-05-18 22:46:34 -07007447 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
7448 /* All of this garbage is because when using non-tagged
7449 * IRQ status the mailbox/status_block protocol the chip
7450 * uses with the cpu is race prone.
7451 */
7452 if (tp->hw_status->status & SD_STATUS_UPDATED) {
7453 tw32(GRC_LOCAL_CTRL,
7454 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
7455 } else {
7456 tw32(HOSTCC_MODE, tp->coalesce_mode |
7457 (HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW));
7458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459
David S. Millerfac9b832005-05-18 22:46:34 -07007460 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
7461 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07007462 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07007463 schedule_work(&tp->reset_task);
7464 return;
7465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466 }
7467
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468 /* This part only runs once per second. */
7469 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07007470 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7471 tg3_periodic_fetch_stats(tp);
7472
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
7474 u32 mac_stat;
7475 int phy_event;
7476
7477 mac_stat = tr32(MAC_STATUS);
7478
7479 phy_event = 0;
7480 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) {
7481 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
7482 phy_event = 1;
7483 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
7484 phy_event = 1;
7485
7486 if (phy_event)
7487 tg3_setup_phy(tp, 0);
7488 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
7489 u32 mac_stat = tr32(MAC_STATUS);
7490 int need_setup = 0;
7491
7492 if (netif_carrier_ok(tp->dev) &&
7493 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
7494 need_setup = 1;
7495 }
7496 if (! netif_carrier_ok(tp->dev) &&
7497 (mac_stat & (MAC_STATUS_PCS_SYNCED |
7498 MAC_STATUS_SIGNAL_DET))) {
7499 need_setup = 1;
7500 }
7501 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07007502 if (!tp->serdes_counter) {
7503 tw32_f(MAC_MODE,
7504 (tp->mac_mode &
7505 ~MAC_MODE_PORT_MODE_MASK));
7506 udelay(40);
7507 tw32_f(MAC_MODE, tp->mac_mode);
7508 udelay(40);
7509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510 tg3_setup_phy(tp, 0);
7511 }
Michael Chan747e8f82005-07-25 12:33:22 -07007512 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
7513 tg3_serdes_parallel_detect(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514
7515 tp->timer_counter = tp->timer_multiplier;
7516 }
7517
Michael Chan130b8e42006-09-27 16:00:40 -07007518 /* Heartbeat is only sent once every 2 seconds.
7519 *
7520 * The heartbeat is to tell the ASF firmware that the host
7521 * driver is still alive. In the event that the OS crashes,
7522 * ASF needs to reset the hardware to free up the FIFO space
7523 * that may be filled with rx packets destined for the host.
7524 * If the FIFO is full, ASF will no longer function properly.
7525 *
7526 * Unintended resets have been reported on real time kernels
7527 * where the timer doesn't run on time. Netpoll will also have
7528 * same problem.
7529 *
7530 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
7531 * to check the ring condition when the heartbeat is expiring
7532 * before doing the reset. This will prevent most unintended
7533 * resets.
7534 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007535 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07007536 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7537 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007538 tg3_wait_for_event_ack(tp);
7539
Michael Chanbbadf502006-04-06 21:46:34 -07007540 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07007541 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07007542 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Michael Chan28fbef72005-10-26 15:48:35 -07007543 /* 5 seconds timeout */
Michael Chanbbadf502006-04-06 21:46:34 -07007544 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, 5);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007545
7546 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 }
7548 tp->asf_counter = tp->asf_multiplier;
7549 }
7550
David S. Millerf47c11e2005-06-24 20:18:35 -07007551 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007552
Michael Chanf475f162006-03-27 23:20:14 -08007553restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007554 tp->timer.expires = jiffies + tp->timer_offset;
7555 add_timer(&tp->timer);
7556}
7557
Adrian Bunk81789ef2006-03-20 23:00:14 -08007558static int tg3_request_irq(struct tg3 *tp)
Michael Chanfcfa0a32006-03-20 22:28:41 -08007559{
David Howells7d12e782006-10-05 14:55:46 +01007560 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007561 unsigned long flags;
7562 struct net_device *dev = tp->dev;
7563
7564 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7565 fn = tg3_msi;
7566 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
7567 fn = tg3_msi_1shot;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07007568 flags = IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007569 } else {
7570 fn = tg3_interrupt;
7571 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
7572 fn = tg3_interrupt_tagged;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07007573 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007574 }
7575 return (request_irq(tp->pdev->irq, fn, flags, dev->name, dev));
7576}
7577
Michael Chan79381092005-04-21 17:13:59 -07007578static int tg3_test_interrupt(struct tg3 *tp)
7579{
7580 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07007581 int err, i, intr_ok = 0;
Michael Chan79381092005-04-21 17:13:59 -07007582
Michael Chand4bc3922005-05-29 14:59:20 -07007583 if (!netif_running(dev))
7584 return -ENODEV;
7585
Michael Chan79381092005-04-21 17:13:59 -07007586 tg3_disable_ints(tp);
7587
7588 free_irq(tp->pdev->irq, dev);
7589
7590 err = request_irq(tp->pdev->irq, tg3_test_isr,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07007591 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
Michael Chan79381092005-04-21 17:13:59 -07007592 if (err)
7593 return err;
7594
Michael Chan38f38432005-09-05 17:53:32 -07007595 tp->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07007596 tg3_enable_ints(tp);
7597
7598 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
7599 HOSTCC_MODE_NOW);
7600
7601 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07007602 u32 int_mbox, misc_host_ctrl;
7603
Michael Chan09ee9292005-08-09 20:17:00 -07007604 int_mbox = tr32_mailbox(MAILBOX_INTERRUPT_0 +
7605 TG3_64BIT_REG_LOW);
Michael Chanb16250e2006-09-27 16:10:14 -07007606 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
7607
7608 if ((int_mbox != 0) ||
7609 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
7610 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07007611 break;
Michael Chanb16250e2006-09-27 16:10:14 -07007612 }
7613
Michael Chan79381092005-04-21 17:13:59 -07007614 msleep(10);
7615 }
7616
7617 tg3_disable_ints(tp);
7618
7619 free_irq(tp->pdev->irq, dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04007620
Michael Chanfcfa0a32006-03-20 22:28:41 -08007621 err = tg3_request_irq(tp);
Michael Chan79381092005-04-21 17:13:59 -07007622
7623 if (err)
7624 return err;
7625
Michael Chanb16250e2006-09-27 16:10:14 -07007626 if (intr_ok)
Michael Chan79381092005-04-21 17:13:59 -07007627 return 0;
7628
7629 return -EIO;
7630}
7631
7632/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
7633 * successfully restored
7634 */
7635static int tg3_test_msi(struct tg3 *tp)
7636{
7637 struct net_device *dev = tp->dev;
7638 int err;
7639 u16 pci_cmd;
7640
7641 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
7642 return 0;
7643
7644 /* Turn off SERR reporting in case MSI terminates with Master
7645 * Abort.
7646 */
7647 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
7648 pci_write_config_word(tp->pdev, PCI_COMMAND,
7649 pci_cmd & ~PCI_COMMAND_SERR);
7650
7651 err = tg3_test_interrupt(tp);
7652
7653 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
7654
7655 if (!err)
7656 return 0;
7657
7658 /* other failures */
7659 if (err != -EIO)
7660 return err;
7661
7662 /* MSI test failed, go back to INTx mode */
7663 printk(KERN_WARNING PFX "%s: No interrupt was generated using MSI, "
7664 "switching to INTx mode. Please report this failure to "
7665 "the PCI maintainer and include system chipset information.\n",
7666 tp->dev->name);
7667
7668 free_irq(tp->pdev->irq, dev);
7669 pci_disable_msi(tp->pdev);
7670
7671 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
7672
Michael Chanfcfa0a32006-03-20 22:28:41 -08007673 err = tg3_request_irq(tp);
Michael Chan79381092005-04-21 17:13:59 -07007674 if (err)
7675 return err;
7676
7677 /* Need to reset the chip because the MSI cycle may have terminated
7678 * with Master Abort.
7679 */
David S. Millerf47c11e2005-06-24 20:18:35 -07007680 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07007681
Michael Chan944d9802005-05-29 14:57:48 -07007682 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007683 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07007684
David S. Millerf47c11e2005-06-24 20:18:35 -07007685 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07007686
7687 if (err)
7688 free_irq(tp->pdev->irq, dev);
7689
7690 return err;
7691}
7692
Matt Carlson9e9fd122009-01-19 16:57:45 -08007693static int tg3_request_firmware(struct tg3 *tp)
7694{
7695 const __be32 *fw_data;
7696
7697 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
7698 printk(KERN_ERR "%s: Failed to load firmware \"%s\"\n",
7699 tp->dev->name, tp->fw_needed);
7700 return -ENOENT;
7701 }
7702
7703 fw_data = (void *)tp->fw->data;
7704
7705 /* Firmware blob starts with version numbers, followed by
7706 * start address and _full_ length including BSS sections
7707 * (which must be longer than the actual data, of course
7708 */
7709
7710 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
7711 if (tp->fw_len < (tp->fw->size - 12)) {
7712 printk(KERN_ERR "%s: bogus length %d in \"%s\"\n",
7713 tp->dev->name, tp->fw_len, tp->fw_needed);
7714 release_firmware(tp->fw);
7715 tp->fw = NULL;
7716 return -EINVAL;
7717 }
7718
7719 /* We no longer need firmware; we have it. */
7720 tp->fw_needed = NULL;
7721 return 0;
7722}
7723
Linus Torvalds1da177e2005-04-16 15:20:36 -07007724static int tg3_open(struct net_device *dev)
7725{
7726 struct tg3 *tp = netdev_priv(dev);
7727 int err;
7728
Matt Carlson9e9fd122009-01-19 16:57:45 -08007729 if (tp->fw_needed) {
7730 err = tg3_request_firmware(tp);
7731 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
7732 if (err)
7733 return err;
7734 } else if (err) {
7735 printk(KERN_WARNING "%s: TSO capability disabled.\n",
7736 tp->dev->name);
7737 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
7738 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
7739 printk(KERN_NOTICE "%s: TSO capability restored.\n",
7740 tp->dev->name);
7741 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
7742 }
7743 }
7744
Michael Chanc49a1562006-12-17 17:07:29 -08007745 netif_carrier_off(tp->dev);
7746
Michael Chanbc1c7562006-03-20 17:48:03 -08007747 err = tg3_set_power_state(tp, PCI_D0);
Matt Carlson2f751b62008-08-04 23:17:34 -07007748 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08007749 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07007750
7751 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08007752
Linus Torvalds1da177e2005-04-16 15:20:36 -07007753 tg3_disable_ints(tp);
7754 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
7755
David S. Millerf47c11e2005-06-24 20:18:35 -07007756 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757
7758 /* The placement of this call is tied
7759 * to the setup and use of Host TX descriptors.
7760 */
7761 err = tg3_alloc_consistent(tp);
7762 if (err)
7763 return err;
7764
Michael Chan7544b092007-05-05 13:08:32 -07007765 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) {
David S. Millerfac9b832005-05-18 22:46:34 -07007766 /* All MSI supporting chips should support tagged
7767 * status. Assert that this is the case.
7768 */
7769 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
7770 printk(KERN_WARNING PFX "%s: MSI without TAGGED? "
7771 "Not using MSI.\n", tp->dev->name);
7772 } else if (pci_enable_msi(tp->pdev) == 0) {
Michael Chan88b06bc22005-04-21 17:13:25 -07007773 u32 msi_mode;
7774
7775 msi_mode = tr32(MSGINT_MODE);
7776 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
7777 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
7778 }
7779 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08007780 err = tg3_request_irq(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781
7782 if (err) {
Michael Chan88b06bc22005-04-21 17:13:25 -07007783 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7784 pci_disable_msi(tp->pdev);
7785 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
7786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007787 tg3_free_consistent(tp);
7788 return err;
7789 }
7790
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007791 napi_enable(&tp->napi);
7792
David S. Millerf47c11e2005-06-24 20:18:35 -07007793 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007794
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007795 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007796 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07007797 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007798 tg3_free_rings(tp);
7799 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07007800 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
7801 tp->timer_offset = HZ;
7802 else
7803 tp->timer_offset = HZ / 10;
7804
7805 BUG_ON(tp->timer_offset > HZ);
7806 tp->timer_counter = tp->timer_multiplier =
7807 (HZ / tp->timer_offset);
7808 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07007809 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007810
7811 init_timer(&tp->timer);
7812 tp->timer.expires = jiffies + tp->timer_offset;
7813 tp->timer.data = (unsigned long) tp;
7814 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007815 }
7816
David S. Millerf47c11e2005-06-24 20:18:35 -07007817 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007818
7819 if (err) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007820 napi_disable(&tp->napi);
Michael Chan88b06bc22005-04-21 17:13:25 -07007821 free_irq(tp->pdev->irq, dev);
7822 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7823 pci_disable_msi(tp->pdev);
7824 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
7825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007826 tg3_free_consistent(tp);
7827 return err;
7828 }
7829
Michael Chan79381092005-04-21 17:13:59 -07007830 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7831 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07007832
Michael Chan79381092005-04-21 17:13:59 -07007833 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07007834 tg3_full_lock(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07007835
7836 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7837 pci_disable_msi(tp->pdev);
7838 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
7839 }
Michael Chan944d9802005-05-29 14:57:48 -07007840 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07007841 tg3_free_rings(tp);
7842 tg3_free_consistent(tp);
7843
David S. Millerf47c11e2005-06-24 20:18:35 -07007844 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07007845
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007846 napi_disable(&tp->napi);
7847
Michael Chan79381092005-04-21 17:13:59 -07007848 return err;
7849 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08007850
7851 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7852 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI) {
Michael Chanb5d37722006-09-27 16:06:21 -07007853 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007854
Michael Chanb5d37722006-09-27 16:06:21 -07007855 tw32(PCIE_TRANSACTION_CFG,
7856 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007857 }
7858 }
Michael Chan79381092005-04-21 17:13:59 -07007859 }
7860
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007861 tg3_phy_start(tp);
7862
David S. Millerf47c11e2005-06-24 20:18:35 -07007863 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007864
Michael Chan79381092005-04-21 17:13:59 -07007865 add_timer(&tp->timer);
7866 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007867 tg3_enable_ints(tp);
7868
David S. Millerf47c11e2005-06-24 20:18:35 -07007869 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870
7871 netif_start_queue(dev);
7872
7873 return 0;
7874}
7875
7876#if 0
7877/*static*/ void tg3_dump_state(struct tg3 *tp)
7878{
7879 u32 val32, val32_2, val32_3, val32_4, val32_5;
7880 u16 val16;
7881 int i;
7882
7883 pci_read_config_word(tp->pdev, PCI_STATUS, &val16);
7884 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE, &val32);
7885 printk("DEBUG: PCI status [%04x] TG3PCI state[%08x]\n",
7886 val16, val32);
7887
7888 /* MAC block */
7889 printk("DEBUG: MAC_MODE[%08x] MAC_STATUS[%08x]\n",
7890 tr32(MAC_MODE), tr32(MAC_STATUS));
7891 printk(" MAC_EVENT[%08x] MAC_LED_CTRL[%08x]\n",
7892 tr32(MAC_EVENT), tr32(MAC_LED_CTRL));
7893 printk("DEBUG: MAC_TX_MODE[%08x] MAC_TX_STATUS[%08x]\n",
7894 tr32(MAC_TX_MODE), tr32(MAC_TX_STATUS));
7895 printk(" MAC_RX_MODE[%08x] MAC_RX_STATUS[%08x]\n",
7896 tr32(MAC_RX_MODE), tr32(MAC_RX_STATUS));
7897
7898 /* Send data initiator control block */
7899 printk("DEBUG: SNDDATAI_MODE[%08x] SNDDATAI_STATUS[%08x]\n",
7900 tr32(SNDDATAI_MODE), tr32(SNDDATAI_STATUS));
7901 printk(" SNDDATAI_STATSCTRL[%08x]\n",
7902 tr32(SNDDATAI_STATSCTRL));
7903
7904 /* Send data completion control block */
7905 printk("DEBUG: SNDDATAC_MODE[%08x]\n", tr32(SNDDATAC_MODE));
7906
7907 /* Send BD ring selector block */
7908 printk("DEBUG: SNDBDS_MODE[%08x] SNDBDS_STATUS[%08x]\n",
7909 tr32(SNDBDS_MODE), tr32(SNDBDS_STATUS));
7910
7911 /* Send BD initiator control block */
7912 printk("DEBUG: SNDBDI_MODE[%08x] SNDBDI_STATUS[%08x]\n",
7913 tr32(SNDBDI_MODE), tr32(SNDBDI_STATUS));
7914
7915 /* Send BD completion control block */
7916 printk("DEBUG: SNDBDC_MODE[%08x]\n", tr32(SNDBDC_MODE));
7917
7918 /* Receive list placement control block */
7919 printk("DEBUG: RCVLPC_MODE[%08x] RCVLPC_STATUS[%08x]\n",
7920 tr32(RCVLPC_MODE), tr32(RCVLPC_STATUS));
7921 printk(" RCVLPC_STATSCTRL[%08x]\n",
7922 tr32(RCVLPC_STATSCTRL));
7923
7924 /* Receive data and receive BD initiator control block */
7925 printk("DEBUG: RCVDBDI_MODE[%08x] RCVDBDI_STATUS[%08x]\n",
7926 tr32(RCVDBDI_MODE), tr32(RCVDBDI_STATUS));
7927
7928 /* Receive data completion control block */
7929 printk("DEBUG: RCVDCC_MODE[%08x]\n",
7930 tr32(RCVDCC_MODE));
7931
7932 /* Receive BD initiator control block */
7933 printk("DEBUG: RCVBDI_MODE[%08x] RCVBDI_STATUS[%08x]\n",
7934 tr32(RCVBDI_MODE), tr32(RCVBDI_STATUS));
7935
7936 /* Receive BD completion control block */
7937 printk("DEBUG: RCVCC_MODE[%08x] RCVCC_STATUS[%08x]\n",
7938 tr32(RCVCC_MODE), tr32(RCVCC_STATUS));
7939
7940 /* Receive list selector control block */
7941 printk("DEBUG: RCVLSC_MODE[%08x] RCVLSC_STATUS[%08x]\n",
7942 tr32(RCVLSC_MODE), tr32(RCVLSC_STATUS));
7943
7944 /* Mbuf cluster free block */
7945 printk("DEBUG: MBFREE_MODE[%08x] MBFREE_STATUS[%08x]\n",
7946 tr32(MBFREE_MODE), tr32(MBFREE_STATUS));
7947
7948 /* Host coalescing control block */
7949 printk("DEBUG: HOSTCC_MODE[%08x] HOSTCC_STATUS[%08x]\n",
7950 tr32(HOSTCC_MODE), tr32(HOSTCC_STATUS));
7951 printk("DEBUG: HOSTCC_STATS_BLK_HOST_ADDR[%08x%08x]\n",
7952 tr32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH),
7953 tr32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW));
7954 printk("DEBUG: HOSTCC_STATUS_BLK_HOST_ADDR[%08x%08x]\n",
7955 tr32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH),
7956 tr32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW));
7957 printk("DEBUG: HOSTCC_STATS_BLK_NIC_ADDR[%08x]\n",
7958 tr32(HOSTCC_STATS_BLK_NIC_ADDR));
7959 printk("DEBUG: HOSTCC_STATUS_BLK_NIC_ADDR[%08x]\n",
7960 tr32(HOSTCC_STATUS_BLK_NIC_ADDR));
7961
7962 /* Memory arbiter control block */
7963 printk("DEBUG: MEMARB_MODE[%08x] MEMARB_STATUS[%08x]\n",
7964 tr32(MEMARB_MODE), tr32(MEMARB_STATUS));
7965
7966 /* Buffer manager control block */
7967 printk("DEBUG: BUFMGR_MODE[%08x] BUFMGR_STATUS[%08x]\n",
7968 tr32(BUFMGR_MODE), tr32(BUFMGR_STATUS));
7969 printk("DEBUG: BUFMGR_MB_POOL_ADDR[%08x] BUFMGR_MB_POOL_SIZE[%08x]\n",
7970 tr32(BUFMGR_MB_POOL_ADDR), tr32(BUFMGR_MB_POOL_SIZE));
7971 printk("DEBUG: BUFMGR_DMA_DESC_POOL_ADDR[%08x] "
7972 "BUFMGR_DMA_DESC_POOL_SIZE[%08x]\n",
7973 tr32(BUFMGR_DMA_DESC_POOL_ADDR),
7974 tr32(BUFMGR_DMA_DESC_POOL_SIZE));
7975
7976 /* Read DMA control block */
7977 printk("DEBUG: RDMAC_MODE[%08x] RDMAC_STATUS[%08x]\n",
7978 tr32(RDMAC_MODE), tr32(RDMAC_STATUS));
7979
7980 /* Write DMA control block */
7981 printk("DEBUG: WDMAC_MODE[%08x] WDMAC_STATUS[%08x]\n",
7982 tr32(WDMAC_MODE), tr32(WDMAC_STATUS));
7983
7984 /* DMA completion block */
7985 printk("DEBUG: DMAC_MODE[%08x]\n",
7986 tr32(DMAC_MODE));
7987
7988 /* GRC block */
7989 printk("DEBUG: GRC_MODE[%08x] GRC_MISC_CFG[%08x]\n",
7990 tr32(GRC_MODE), tr32(GRC_MISC_CFG));
7991 printk("DEBUG: GRC_LOCAL_CTRL[%08x]\n",
7992 tr32(GRC_LOCAL_CTRL));
7993
7994 /* TG3_BDINFOs */
7995 printk("DEBUG: RCVDBDI_JUMBO_BD[%08x%08x:%08x:%08x]\n",
7996 tr32(RCVDBDI_JUMBO_BD + 0x0),
7997 tr32(RCVDBDI_JUMBO_BD + 0x4),
7998 tr32(RCVDBDI_JUMBO_BD + 0x8),
7999 tr32(RCVDBDI_JUMBO_BD + 0xc));
8000 printk("DEBUG: RCVDBDI_STD_BD[%08x%08x:%08x:%08x]\n",
8001 tr32(RCVDBDI_STD_BD + 0x0),
8002 tr32(RCVDBDI_STD_BD + 0x4),
8003 tr32(RCVDBDI_STD_BD + 0x8),
8004 tr32(RCVDBDI_STD_BD + 0xc));
8005 printk("DEBUG: RCVDBDI_MINI_BD[%08x%08x:%08x:%08x]\n",
8006 tr32(RCVDBDI_MINI_BD + 0x0),
8007 tr32(RCVDBDI_MINI_BD + 0x4),
8008 tr32(RCVDBDI_MINI_BD + 0x8),
8009 tr32(RCVDBDI_MINI_BD + 0xc));
8010
8011 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x0, &val32);
8012 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x4, &val32_2);
8013 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x8, &val32_3);
8014 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0xc, &val32_4);
8015 printk("DEBUG: SRAM_SEND_RCB_0[%08x%08x:%08x:%08x]\n",
8016 val32, val32_2, val32_3, val32_4);
8017
8018 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x0, &val32);
8019 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x4, &val32_2);
8020 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x8, &val32_3);
8021 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0xc, &val32_4);
8022 printk("DEBUG: SRAM_RCV_RET_RCB_0[%08x%08x:%08x:%08x]\n",
8023 val32, val32_2, val32_3, val32_4);
8024
8025 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x0, &val32);
8026 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x4, &val32_2);
8027 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x8, &val32_3);
8028 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0xc, &val32_4);
8029 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x10, &val32_5);
8030 printk("DEBUG: SRAM_STATUS_BLK[%08x:%08x:%08x:%08x:%08x]\n",
8031 val32, val32_2, val32_3, val32_4, val32_5);
8032
8033 /* SW status block */
8034 printk("DEBUG: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
8035 tp->hw_status->status,
8036 tp->hw_status->status_tag,
8037 tp->hw_status->rx_jumbo_consumer,
8038 tp->hw_status->rx_consumer,
8039 tp->hw_status->rx_mini_consumer,
8040 tp->hw_status->idx[0].rx_producer,
8041 tp->hw_status->idx[0].tx_consumer);
8042
8043 /* SW statistics block */
8044 printk("DEBUG: Host statistics block [%08x:%08x:%08x:%08x]\n",
8045 ((u32 *)tp->hw_stats)[0],
8046 ((u32 *)tp->hw_stats)[1],
8047 ((u32 *)tp->hw_stats)[2],
8048 ((u32 *)tp->hw_stats)[3]);
8049
8050 /* Mailboxes */
8051 printk("DEBUG: SNDHOST_PROD[%08x%08x] SNDNIC_PROD[%08x%08x]\n",
Michael Chan09ee9292005-08-09 20:17:00 -07008052 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + 0x0),
8053 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + 0x4),
8054 tr32_mailbox(MAILBOX_SNDNIC_PROD_IDX_0 + 0x0),
8055 tr32_mailbox(MAILBOX_SNDNIC_PROD_IDX_0 + 0x4));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008056
8057 /* NIC side send descriptors. */
8058 for (i = 0; i < 6; i++) {
8059 unsigned long txd;
8060
8061 txd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_TX_BUFFER_DESC
8062 + (i * sizeof(struct tg3_tx_buffer_desc));
8063 printk("DEBUG: NIC TXD(%d)[%08x:%08x:%08x:%08x]\n",
8064 i,
8065 readl(txd + 0x0), readl(txd + 0x4),
8066 readl(txd + 0x8), readl(txd + 0xc));
8067 }
8068
8069 /* NIC side RX descriptors. */
8070 for (i = 0; i < 6; i++) {
8071 unsigned long rxd;
8072
8073 rxd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_RX_BUFFER_DESC
8074 + (i * sizeof(struct tg3_rx_buffer_desc));
8075 printk("DEBUG: NIC RXD_STD(%d)[0][%08x:%08x:%08x:%08x]\n",
8076 i,
8077 readl(rxd + 0x0), readl(rxd + 0x4),
8078 readl(rxd + 0x8), readl(rxd + 0xc));
8079 rxd += (4 * sizeof(u32));
8080 printk("DEBUG: NIC RXD_STD(%d)[1][%08x:%08x:%08x:%08x]\n",
8081 i,
8082 readl(rxd + 0x0), readl(rxd + 0x4),
8083 readl(rxd + 0x8), readl(rxd + 0xc));
8084 }
8085
8086 for (i = 0; i < 6; i++) {
8087 unsigned long rxd;
8088
8089 rxd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_RX_JUMBO_BUFFER_DESC
8090 + (i * sizeof(struct tg3_rx_buffer_desc));
8091 printk("DEBUG: NIC RXD_JUMBO(%d)[0][%08x:%08x:%08x:%08x]\n",
8092 i,
8093 readl(rxd + 0x0), readl(rxd + 0x4),
8094 readl(rxd + 0x8), readl(rxd + 0xc));
8095 rxd += (4 * sizeof(u32));
8096 printk("DEBUG: NIC RXD_JUMBO(%d)[1][%08x:%08x:%08x:%08x]\n",
8097 i,
8098 readl(rxd + 0x0), readl(rxd + 0x4),
8099 readl(rxd + 0x8), readl(rxd + 0xc));
8100 }
8101}
8102#endif
8103
8104static struct net_device_stats *tg3_get_stats(struct net_device *);
8105static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
8106
8107static int tg3_close(struct net_device *dev)
8108{
8109 struct tg3 *tp = netdev_priv(dev);
8110
Stephen Hemmingerbea33482007-10-03 16:41:36 -07008111 napi_disable(&tp->napi);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07008112 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08008113
Linus Torvalds1da177e2005-04-16 15:20:36 -07008114 netif_stop_queue(dev);
8115
8116 del_timer_sync(&tp->timer);
8117
David S. Millerf47c11e2005-06-24 20:18:35 -07008118 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008119#if 0
8120 tg3_dump_state(tp);
8121#endif
8122
8123 tg3_disable_ints(tp);
8124
Michael Chan944d9802005-05-29 14:57:48 -07008125 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008126 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07008127 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008128
David S. Millerf47c11e2005-06-24 20:18:35 -07008129 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008130
Michael Chan88b06bc22005-04-21 17:13:25 -07008131 free_irq(tp->pdev->irq, dev);
8132 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
8133 pci_disable_msi(tp->pdev);
8134 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
8135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008136
8137 memcpy(&tp->net_stats_prev, tg3_get_stats(tp->dev),
8138 sizeof(tp->net_stats_prev));
8139 memcpy(&tp->estats_prev, tg3_get_estats(tp),
8140 sizeof(tp->estats_prev));
8141
8142 tg3_free_consistent(tp);
8143
Michael Chanbc1c7562006-03-20 17:48:03 -08008144 tg3_set_power_state(tp, PCI_D3hot);
8145
8146 netif_carrier_off(tp->dev);
8147
Linus Torvalds1da177e2005-04-16 15:20:36 -07008148 return 0;
8149}
8150
8151static inline unsigned long get_stat64(tg3_stat64_t *val)
8152{
8153 unsigned long ret;
8154
8155#if (BITS_PER_LONG == 32)
8156 ret = val->low;
8157#else
8158 ret = ((u64)val->high << 32) | ((u64)val->low);
8159#endif
8160 return ret;
8161}
8162
Stefan Buehler816f8b82008-08-15 14:10:54 -07008163static inline u64 get_estat64(tg3_stat64_t *val)
8164{
8165 return ((u64)val->high << 32) | ((u64)val->low);
8166}
8167
Linus Torvalds1da177e2005-04-16 15:20:36 -07008168static unsigned long calc_crc_errors(struct tg3 *tp)
8169{
8170 struct tg3_hw_stats *hw_stats = tp->hw_stats;
8171
8172 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
8173 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
8174 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008175 u32 val;
8176
David S. Millerf47c11e2005-06-24 20:18:35 -07008177 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08008178 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
8179 tg3_writephy(tp, MII_TG3_TEST1,
8180 val | MII_TG3_TEST1_CRC_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008181 tg3_readphy(tp, 0x14, &val);
8182 } else
8183 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07008184 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008185
8186 tp->phy_crc_errors += val;
8187
8188 return tp->phy_crc_errors;
8189 }
8190
8191 return get_stat64(&hw_stats->rx_fcs_errors);
8192}
8193
8194#define ESTAT_ADD(member) \
8195 estats->member = old_estats->member + \
Stefan Buehler816f8b82008-08-15 14:10:54 -07008196 get_estat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008197
8198static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
8199{
8200 struct tg3_ethtool_stats *estats = &tp->estats;
8201 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
8202 struct tg3_hw_stats *hw_stats = tp->hw_stats;
8203
8204 if (!hw_stats)
8205 return old_estats;
8206
8207 ESTAT_ADD(rx_octets);
8208 ESTAT_ADD(rx_fragments);
8209 ESTAT_ADD(rx_ucast_packets);
8210 ESTAT_ADD(rx_mcast_packets);
8211 ESTAT_ADD(rx_bcast_packets);
8212 ESTAT_ADD(rx_fcs_errors);
8213 ESTAT_ADD(rx_align_errors);
8214 ESTAT_ADD(rx_xon_pause_rcvd);
8215 ESTAT_ADD(rx_xoff_pause_rcvd);
8216 ESTAT_ADD(rx_mac_ctrl_rcvd);
8217 ESTAT_ADD(rx_xoff_entered);
8218 ESTAT_ADD(rx_frame_too_long_errors);
8219 ESTAT_ADD(rx_jabbers);
8220 ESTAT_ADD(rx_undersize_packets);
8221 ESTAT_ADD(rx_in_length_errors);
8222 ESTAT_ADD(rx_out_length_errors);
8223 ESTAT_ADD(rx_64_or_less_octet_packets);
8224 ESTAT_ADD(rx_65_to_127_octet_packets);
8225 ESTAT_ADD(rx_128_to_255_octet_packets);
8226 ESTAT_ADD(rx_256_to_511_octet_packets);
8227 ESTAT_ADD(rx_512_to_1023_octet_packets);
8228 ESTAT_ADD(rx_1024_to_1522_octet_packets);
8229 ESTAT_ADD(rx_1523_to_2047_octet_packets);
8230 ESTAT_ADD(rx_2048_to_4095_octet_packets);
8231 ESTAT_ADD(rx_4096_to_8191_octet_packets);
8232 ESTAT_ADD(rx_8192_to_9022_octet_packets);
8233
8234 ESTAT_ADD(tx_octets);
8235 ESTAT_ADD(tx_collisions);
8236 ESTAT_ADD(tx_xon_sent);
8237 ESTAT_ADD(tx_xoff_sent);
8238 ESTAT_ADD(tx_flow_control);
8239 ESTAT_ADD(tx_mac_errors);
8240 ESTAT_ADD(tx_single_collisions);
8241 ESTAT_ADD(tx_mult_collisions);
8242 ESTAT_ADD(tx_deferred);
8243 ESTAT_ADD(tx_excessive_collisions);
8244 ESTAT_ADD(tx_late_collisions);
8245 ESTAT_ADD(tx_collide_2times);
8246 ESTAT_ADD(tx_collide_3times);
8247 ESTAT_ADD(tx_collide_4times);
8248 ESTAT_ADD(tx_collide_5times);
8249 ESTAT_ADD(tx_collide_6times);
8250 ESTAT_ADD(tx_collide_7times);
8251 ESTAT_ADD(tx_collide_8times);
8252 ESTAT_ADD(tx_collide_9times);
8253 ESTAT_ADD(tx_collide_10times);
8254 ESTAT_ADD(tx_collide_11times);
8255 ESTAT_ADD(tx_collide_12times);
8256 ESTAT_ADD(tx_collide_13times);
8257 ESTAT_ADD(tx_collide_14times);
8258 ESTAT_ADD(tx_collide_15times);
8259 ESTAT_ADD(tx_ucast_packets);
8260 ESTAT_ADD(tx_mcast_packets);
8261 ESTAT_ADD(tx_bcast_packets);
8262 ESTAT_ADD(tx_carrier_sense_errors);
8263 ESTAT_ADD(tx_discards);
8264 ESTAT_ADD(tx_errors);
8265
8266 ESTAT_ADD(dma_writeq_full);
8267 ESTAT_ADD(dma_write_prioq_full);
8268 ESTAT_ADD(rxbds_empty);
8269 ESTAT_ADD(rx_discards);
8270 ESTAT_ADD(rx_errors);
8271 ESTAT_ADD(rx_threshold_hit);
8272
8273 ESTAT_ADD(dma_readq_full);
8274 ESTAT_ADD(dma_read_prioq_full);
8275 ESTAT_ADD(tx_comp_queue_full);
8276
8277 ESTAT_ADD(ring_set_send_prod_index);
8278 ESTAT_ADD(ring_status_update);
8279 ESTAT_ADD(nic_irqs);
8280 ESTAT_ADD(nic_avoided_irqs);
8281 ESTAT_ADD(nic_tx_threshold_hit);
8282
8283 return estats;
8284}
8285
8286static struct net_device_stats *tg3_get_stats(struct net_device *dev)
8287{
8288 struct tg3 *tp = netdev_priv(dev);
8289 struct net_device_stats *stats = &tp->net_stats;
8290 struct net_device_stats *old_stats = &tp->net_stats_prev;
8291 struct tg3_hw_stats *hw_stats = tp->hw_stats;
8292
8293 if (!hw_stats)
8294 return old_stats;
8295
8296 stats->rx_packets = old_stats->rx_packets +
8297 get_stat64(&hw_stats->rx_ucast_packets) +
8298 get_stat64(&hw_stats->rx_mcast_packets) +
8299 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008300
Linus Torvalds1da177e2005-04-16 15:20:36 -07008301 stats->tx_packets = old_stats->tx_packets +
8302 get_stat64(&hw_stats->tx_ucast_packets) +
8303 get_stat64(&hw_stats->tx_mcast_packets) +
8304 get_stat64(&hw_stats->tx_bcast_packets);
8305
8306 stats->rx_bytes = old_stats->rx_bytes +
8307 get_stat64(&hw_stats->rx_octets);
8308 stats->tx_bytes = old_stats->tx_bytes +
8309 get_stat64(&hw_stats->tx_octets);
8310
8311 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07008312 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008313 stats->tx_errors = old_stats->tx_errors +
8314 get_stat64(&hw_stats->tx_errors) +
8315 get_stat64(&hw_stats->tx_mac_errors) +
8316 get_stat64(&hw_stats->tx_carrier_sense_errors) +
8317 get_stat64(&hw_stats->tx_discards);
8318
8319 stats->multicast = old_stats->multicast +
8320 get_stat64(&hw_stats->rx_mcast_packets);
8321 stats->collisions = old_stats->collisions +
8322 get_stat64(&hw_stats->tx_collisions);
8323
8324 stats->rx_length_errors = old_stats->rx_length_errors +
8325 get_stat64(&hw_stats->rx_frame_too_long_errors) +
8326 get_stat64(&hw_stats->rx_undersize_packets);
8327
8328 stats->rx_over_errors = old_stats->rx_over_errors +
8329 get_stat64(&hw_stats->rxbds_empty);
8330 stats->rx_frame_errors = old_stats->rx_frame_errors +
8331 get_stat64(&hw_stats->rx_align_errors);
8332 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
8333 get_stat64(&hw_stats->tx_discards);
8334 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
8335 get_stat64(&hw_stats->tx_carrier_sense_errors);
8336
8337 stats->rx_crc_errors = old_stats->rx_crc_errors +
8338 calc_crc_errors(tp);
8339
John W. Linville4f63b872005-09-12 14:43:18 -07008340 stats->rx_missed_errors = old_stats->rx_missed_errors +
8341 get_stat64(&hw_stats->rx_discards);
8342
Linus Torvalds1da177e2005-04-16 15:20:36 -07008343 return stats;
8344}
8345
8346static inline u32 calc_crc(unsigned char *buf, int len)
8347{
8348 u32 reg;
8349 u32 tmp;
8350 int j, k;
8351
8352 reg = 0xffffffff;
8353
8354 for (j = 0; j < len; j++) {
8355 reg ^= buf[j];
8356
8357 for (k = 0; k < 8; k++) {
8358 tmp = reg & 0x01;
8359
8360 reg >>= 1;
8361
8362 if (tmp) {
8363 reg ^= 0xedb88320;
8364 }
8365 }
8366 }
8367
8368 return ~reg;
8369}
8370
8371static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8372{
8373 /* accept or reject all multicast frames */
8374 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8375 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8376 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8377 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8378}
8379
8380static void __tg3_set_rx_mode(struct net_device *dev)
8381{
8382 struct tg3 *tp = netdev_priv(dev);
8383 u32 rx_mode;
8384
8385 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8386 RX_MODE_KEEP_VLAN_TAG);
8387
8388 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8389 * flag clear.
8390 */
8391#if TG3_VLAN_TAG_USED
8392 if (!tp->vlgrp &&
8393 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
8394 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8395#else
8396 /* By definition, VLAN is disabled always in this
8397 * case.
8398 */
8399 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
8400 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8401#endif
8402
8403 if (dev->flags & IFF_PROMISC) {
8404 /* Promiscuous mode. */
8405 rx_mode |= RX_MODE_PROMISC;
8406 } else if (dev->flags & IFF_ALLMULTI) {
8407 /* Accept all multicast. */
8408 tg3_set_multi (tp, 1);
8409 } else if (dev->mc_count < 1) {
8410 /* Reject all multicast. */
8411 tg3_set_multi (tp, 0);
8412 } else {
8413 /* Accept one or more multicast(s). */
8414 struct dev_mc_list *mclist;
8415 unsigned int i;
8416 u32 mc_filter[4] = { 0, };
8417 u32 regidx;
8418 u32 bit;
8419 u32 crc;
8420
8421 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
8422 i++, mclist = mclist->next) {
8423
8424 crc = calc_crc (mclist->dmi_addr, ETH_ALEN);
8425 bit = ~crc & 0x7f;
8426 regidx = (bit & 0x60) >> 5;
8427 bit &= 0x1f;
8428 mc_filter[regidx] |= (1 << bit);
8429 }
8430
8431 tw32(MAC_HASH_REG_0, mc_filter[0]);
8432 tw32(MAC_HASH_REG_1, mc_filter[1]);
8433 tw32(MAC_HASH_REG_2, mc_filter[2]);
8434 tw32(MAC_HASH_REG_3, mc_filter[3]);
8435 }
8436
8437 if (rx_mode != tp->rx_mode) {
8438 tp->rx_mode = rx_mode;
8439 tw32_f(MAC_RX_MODE, rx_mode);
8440 udelay(10);
8441 }
8442}
8443
8444static void tg3_set_rx_mode(struct net_device *dev)
8445{
8446 struct tg3 *tp = netdev_priv(dev);
8447
Michael Chane75f7c92006-03-20 21:33:26 -08008448 if (!netif_running(dev))
8449 return;
8450
David S. Millerf47c11e2005-06-24 20:18:35 -07008451 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008452 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07008453 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008454}
8455
8456#define TG3_REGDUMP_LEN (32 * 1024)
8457
8458static int tg3_get_regs_len(struct net_device *dev)
8459{
8460 return TG3_REGDUMP_LEN;
8461}
8462
8463static void tg3_get_regs(struct net_device *dev,
8464 struct ethtool_regs *regs, void *_p)
8465{
8466 u32 *p = _p;
8467 struct tg3 *tp = netdev_priv(dev);
8468 u8 *orig_p = _p;
8469 int i;
8470
8471 regs->version = 0;
8472
8473 memset(p, 0, TG3_REGDUMP_LEN);
8474
Michael Chanbc1c7562006-03-20 17:48:03 -08008475 if (tp->link_config.phy_is_low_power)
8476 return;
8477
David S. Millerf47c11e2005-06-24 20:18:35 -07008478 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008479
8480#define __GET_REG32(reg) (*(p)++ = tr32(reg))
8481#define GET_REG32_LOOP(base,len) \
8482do { p = (u32 *)(orig_p + (base)); \
8483 for (i = 0; i < len; i += 4) \
8484 __GET_REG32((base) + i); \
8485} while (0)
8486#define GET_REG32_1(reg) \
8487do { p = (u32 *)(orig_p + (reg)); \
8488 __GET_REG32((reg)); \
8489} while (0)
8490
8491 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
8492 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
8493 GET_REG32_LOOP(MAC_MODE, 0x4f0);
8494 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
8495 GET_REG32_1(SNDDATAC_MODE);
8496 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
8497 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
8498 GET_REG32_1(SNDBDC_MODE);
8499 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
8500 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
8501 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
8502 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
8503 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
8504 GET_REG32_1(RCVDCC_MODE);
8505 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
8506 GET_REG32_LOOP(RCVCC_MODE, 0x14);
8507 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
8508 GET_REG32_1(MBFREE_MODE);
8509 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
8510 GET_REG32_LOOP(MEMARB_MODE, 0x10);
8511 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
8512 GET_REG32_LOOP(RDMAC_MODE, 0x08);
8513 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08008514 GET_REG32_1(RX_CPU_MODE);
8515 GET_REG32_1(RX_CPU_STATE);
8516 GET_REG32_1(RX_CPU_PGMCTR);
8517 GET_REG32_1(RX_CPU_HWBKPT);
8518 GET_REG32_1(TX_CPU_MODE);
8519 GET_REG32_1(TX_CPU_STATE);
8520 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008521 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
8522 GET_REG32_LOOP(FTQ_RESET, 0x120);
8523 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
8524 GET_REG32_1(DMAC_MODE);
8525 GET_REG32_LOOP(GRC_MODE, 0x4c);
8526 if (tp->tg3_flags & TG3_FLAG_NVRAM)
8527 GET_REG32_LOOP(NVRAM_CMD, 0x24);
8528
8529#undef __GET_REG32
8530#undef GET_REG32_LOOP
8531#undef GET_REG32_1
8532
David S. Millerf47c11e2005-06-24 20:18:35 -07008533 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008534}
8535
8536static int tg3_get_eeprom_len(struct net_device *dev)
8537{
8538 struct tg3 *tp = netdev_priv(dev);
8539
8540 return tp->nvram_size;
8541}
8542
Linus Torvalds1da177e2005-04-16 15:20:36 -07008543static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
8544{
8545 struct tg3 *tp = netdev_priv(dev);
8546 int ret;
8547 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08008548 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00008549 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008550
Matt Carlsondf259d82009-04-20 06:57:14 +00008551 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
8552 return -EINVAL;
8553
Michael Chanbc1c7562006-03-20 17:48:03 -08008554 if (tp->link_config.phy_is_low_power)
8555 return -EAGAIN;
8556
Linus Torvalds1da177e2005-04-16 15:20:36 -07008557 offset = eeprom->offset;
8558 len = eeprom->len;
8559 eeprom->len = 0;
8560
8561 eeprom->magic = TG3_EEPROM_MAGIC;
8562
8563 if (offset & 3) {
8564 /* adjustments to start on required 4 byte boundary */
8565 b_offset = offset & 3;
8566 b_count = 4 - b_offset;
8567 if (b_count > len) {
8568 /* i.e. offset=1 len=2 */
8569 b_count = len;
8570 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00008571 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008572 if (ret)
8573 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008574 memcpy(data, ((char*)&val) + b_offset, b_count);
8575 len -= b_count;
8576 offset += b_count;
8577 eeprom->len += b_count;
8578 }
8579
8580 /* read bytes upto the last 4 byte boundary */
8581 pd = &data[eeprom->len];
8582 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00008583 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008584 if (ret) {
8585 eeprom->len += i;
8586 return ret;
8587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008588 memcpy(pd + i, &val, 4);
8589 }
8590 eeprom->len += i;
8591
8592 if (len & 3) {
8593 /* read last bytes not ending on 4 byte boundary */
8594 pd = &data[eeprom->len];
8595 b_count = len & 3;
8596 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00008597 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008598 if (ret)
8599 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08008600 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008601 eeprom->len += b_count;
8602 }
8603 return 0;
8604}
8605
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008606static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008607
8608static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
8609{
8610 struct tg3 *tp = netdev_priv(dev);
8611 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08008612 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00008614 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008615
Michael Chanbc1c7562006-03-20 17:48:03 -08008616 if (tp->link_config.phy_is_low_power)
8617 return -EAGAIN;
8618
Matt Carlsondf259d82009-04-20 06:57:14 +00008619 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
8620 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008621 return -EINVAL;
8622
8623 offset = eeprom->offset;
8624 len = eeprom->len;
8625
8626 if ((b_offset = (offset & 3))) {
8627 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00008628 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008629 if (ret)
8630 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008631 len += b_offset;
8632 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07008633 if (len < 4)
8634 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008635 }
8636
8637 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07008638 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008639 /* adjustments to end on required 4 byte boundary */
8640 odd_len = 1;
8641 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00008642 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008643 if (ret)
8644 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008645 }
8646
8647 buf = data;
8648 if (b_offset || odd_len) {
8649 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008650 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008651 return -ENOMEM;
8652 if (b_offset)
8653 memcpy(buf, &start, 4);
8654 if (odd_len)
8655 memcpy(buf+len-4, &end, 4);
8656 memcpy(buf + b_offset, data, eeprom->len);
8657 }
8658
8659 ret = tg3_nvram_write_block(tp, offset, len, buf);
8660
8661 if (buf != data)
8662 kfree(buf);
8663
8664 return ret;
8665}
8666
8667static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
8668{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008669 struct tg3 *tp = netdev_priv(dev);
8670
8671 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
8672 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
8673 return -EAGAIN;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07008674 return phy_ethtool_gset(tp->mdio_bus->phy_map[PHY_ADDR], cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008675 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008676
Linus Torvalds1da177e2005-04-16 15:20:36 -07008677 cmd->supported = (SUPPORTED_Autoneg);
8678
8679 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
8680 cmd->supported |= (SUPPORTED_1000baseT_Half |
8681 SUPPORTED_1000baseT_Full);
8682
Karsten Keilef348142006-05-12 12:49:08 -07008683 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008684 cmd->supported |= (SUPPORTED_100baseT_Half |
8685 SUPPORTED_100baseT_Full |
8686 SUPPORTED_10baseT_Half |
8687 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08008688 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07008689 cmd->port = PORT_TP;
8690 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008691 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07008692 cmd->port = PORT_FIBRE;
8693 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008694
Linus Torvalds1da177e2005-04-16 15:20:36 -07008695 cmd->advertising = tp->link_config.advertising;
8696 if (netif_running(dev)) {
8697 cmd->speed = tp->link_config.active_speed;
8698 cmd->duplex = tp->link_config.active_duplex;
8699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008700 cmd->phy_address = PHY_ADDR;
Matt Carlson7e5856b2009-02-25 14:23:01 +00008701 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008702 cmd->autoneg = tp->link_config.autoneg;
8703 cmd->maxtxpkt = 0;
8704 cmd->maxrxpkt = 0;
8705 return 0;
8706}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008707
Linus Torvalds1da177e2005-04-16 15:20:36 -07008708static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
8709{
8710 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008711
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008712 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
8713 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
8714 return -EAGAIN;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07008715 return phy_ethtool_sset(tp->mdio_bus->phy_map[PHY_ADDR], cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008716 }
8717
Matt Carlson7e5856b2009-02-25 14:23:01 +00008718 if (cmd->autoneg != AUTONEG_ENABLE &&
8719 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07008720 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00008721
8722 if (cmd->autoneg == AUTONEG_DISABLE &&
8723 cmd->duplex != DUPLEX_FULL &&
8724 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07008725 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008726
Matt Carlson7e5856b2009-02-25 14:23:01 +00008727 if (cmd->autoneg == AUTONEG_ENABLE) {
8728 u32 mask = ADVERTISED_Autoneg |
8729 ADVERTISED_Pause |
8730 ADVERTISED_Asym_Pause;
8731
8732 if (!(tp->tg3_flags2 & TG3_FLAG_10_100_ONLY))
8733 mask |= ADVERTISED_1000baseT_Half |
8734 ADVERTISED_1000baseT_Full;
8735
8736 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
8737 mask |= ADVERTISED_100baseT_Half |
8738 ADVERTISED_100baseT_Full |
8739 ADVERTISED_10baseT_Half |
8740 ADVERTISED_10baseT_Full |
8741 ADVERTISED_TP;
8742 else
8743 mask |= ADVERTISED_FIBRE;
8744
8745 if (cmd->advertising & ~mask)
8746 return -EINVAL;
8747
8748 mask &= (ADVERTISED_1000baseT_Half |
8749 ADVERTISED_1000baseT_Full |
8750 ADVERTISED_100baseT_Half |
8751 ADVERTISED_100baseT_Full |
8752 ADVERTISED_10baseT_Half |
8753 ADVERTISED_10baseT_Full);
8754
8755 cmd->advertising &= mask;
8756 } else {
8757 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) {
8758 if (cmd->speed != SPEED_1000)
8759 return -EINVAL;
8760
8761 if (cmd->duplex != DUPLEX_FULL)
8762 return -EINVAL;
8763 } else {
8764 if (cmd->speed != SPEED_100 &&
8765 cmd->speed != SPEED_10)
8766 return -EINVAL;
8767 }
8768 }
8769
David S. Millerf47c11e2005-06-24 20:18:35 -07008770 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008771
8772 tp->link_config.autoneg = cmd->autoneg;
8773 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07008774 tp->link_config.advertising = (cmd->advertising |
8775 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008776 tp->link_config.speed = SPEED_INVALID;
8777 tp->link_config.duplex = DUPLEX_INVALID;
8778 } else {
8779 tp->link_config.advertising = 0;
8780 tp->link_config.speed = cmd->speed;
8781 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008782 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008783
Michael Chan24fcad62006-12-17 17:06:46 -08008784 tp->link_config.orig_speed = tp->link_config.speed;
8785 tp->link_config.orig_duplex = tp->link_config.duplex;
8786 tp->link_config.orig_autoneg = tp->link_config.autoneg;
8787
Linus Torvalds1da177e2005-04-16 15:20:36 -07008788 if (netif_running(dev))
8789 tg3_setup_phy(tp, 1);
8790
David S. Millerf47c11e2005-06-24 20:18:35 -07008791 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008792
Linus Torvalds1da177e2005-04-16 15:20:36 -07008793 return 0;
8794}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008795
Linus Torvalds1da177e2005-04-16 15:20:36 -07008796static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
8797{
8798 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008799
Linus Torvalds1da177e2005-04-16 15:20:36 -07008800 strcpy(info->driver, DRV_MODULE_NAME);
8801 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08008802 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008803 strcpy(info->bus_info, pci_name(tp->pdev));
8804}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008805
Linus Torvalds1da177e2005-04-16 15:20:36 -07008806static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
8807{
8808 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008809
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07008810 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
8811 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07008812 wol->supported = WAKE_MAGIC;
8813 else
8814 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008815 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08008816 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
8817 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008818 wol->wolopts = WAKE_MAGIC;
8819 memset(&wol->sopass, 0, sizeof(wol->sopass));
8820}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008821
Linus Torvalds1da177e2005-04-16 15:20:36 -07008822static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
8823{
8824 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07008825 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008826
Linus Torvalds1da177e2005-04-16 15:20:36 -07008827 if (wol->wolopts & ~WAKE_MAGIC)
8828 return -EINVAL;
8829 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07008830 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008831 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008832
David S. Millerf47c11e2005-06-24 20:18:35 -07008833 spin_lock_bh(&tp->lock);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07008834 if (wol->wolopts & WAKE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008835 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07008836 device_set_wakeup_enable(dp, true);
8837 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008838 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07008839 device_set_wakeup_enable(dp, false);
8840 }
David S. Millerf47c11e2005-06-24 20:18:35 -07008841 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008842
Linus Torvalds1da177e2005-04-16 15:20:36 -07008843 return 0;
8844}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008845
Linus Torvalds1da177e2005-04-16 15:20:36 -07008846static u32 tg3_get_msglevel(struct net_device *dev)
8847{
8848 struct tg3 *tp = netdev_priv(dev);
8849 return tp->msg_enable;
8850}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008851
Linus Torvalds1da177e2005-04-16 15:20:36 -07008852static void tg3_set_msglevel(struct net_device *dev, u32 value)
8853{
8854 struct tg3 *tp = netdev_priv(dev);
8855 tp->msg_enable = value;
8856}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008857
Linus Torvalds1da177e2005-04-16 15:20:36 -07008858static int tg3_set_tso(struct net_device *dev, u32 value)
8859{
8860 struct tg3 *tp = netdev_priv(dev);
8861
8862 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
8863 if (value)
8864 return -EINVAL;
8865 return 0;
8866 }
Matt Carlson027455a2008-12-21 20:19:30 -08008867 if ((dev->features & NETIF_F_IPV6_CSUM) &&
8868 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07008869 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -07008870 dev->features |= NETIF_F_TSO6;
Matt Carlson57e69832008-05-25 23:48:31 -07008871 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8872 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
8873 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008874 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8875 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -07008876 dev->features |= NETIF_F_TSO_ECN;
8877 } else
8878 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -07008879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008880 return ethtool_op_set_tso(dev, value);
8881}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008882
Linus Torvalds1da177e2005-04-16 15:20:36 -07008883static int tg3_nway_reset(struct net_device *dev)
8884{
8885 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008886 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008887
Linus Torvalds1da177e2005-04-16 15:20:36 -07008888 if (!netif_running(dev))
8889 return -EAGAIN;
8890
Michael Chanc94e3942005-09-27 12:12:42 -07008891 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
8892 return -EINVAL;
8893
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008894 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
8895 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
8896 return -EAGAIN;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07008897 r = phy_start_aneg(tp->mdio_bus->phy_map[PHY_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008898 } else {
8899 u32 bmcr;
8900
8901 spin_lock_bh(&tp->lock);
8902 r = -EINVAL;
8903 tg3_readphy(tp, MII_BMCR, &bmcr);
8904 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
8905 ((bmcr & BMCR_ANENABLE) ||
8906 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT))) {
8907 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
8908 BMCR_ANENABLE);
8909 r = 0;
8910 }
8911 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008912 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008913
Linus Torvalds1da177e2005-04-16 15:20:36 -07008914 return r;
8915}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008916
Linus Torvalds1da177e2005-04-16 15:20:36 -07008917static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
8918{
8919 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008920
Linus Torvalds1da177e2005-04-16 15:20:36 -07008921 ering->rx_max_pending = TG3_RX_RING_SIZE - 1;
8922 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08008923 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
8924 ering->rx_jumbo_max_pending = TG3_RX_JUMBO_RING_SIZE - 1;
8925 else
8926 ering->rx_jumbo_max_pending = 0;
8927
8928 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008929
8930 ering->rx_pending = tp->rx_pending;
8931 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08008932 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
8933 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
8934 else
8935 ering->rx_jumbo_pending = 0;
8936
Linus Torvalds1da177e2005-04-16 15:20:36 -07008937 ering->tx_pending = tp->tx_pending;
8938}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008939
Linus Torvalds1da177e2005-04-16 15:20:36 -07008940static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
8941{
8942 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07008943 int irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008944
Linus Torvalds1da177e2005-04-16 15:20:36 -07008945 if ((ering->rx_pending > TG3_RX_RING_SIZE - 1) ||
8946 (ering->rx_jumbo_pending > TG3_RX_JUMBO_RING_SIZE - 1) ||
Michael Chanbc3a9252006-10-18 20:55:18 -07008947 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
8948 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -08008949 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -07008950 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008951 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008952
Michael Chanbbe832c2005-06-24 20:20:04 -07008953 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008954 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008955 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07008956 irq_sync = 1;
8957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008958
Michael Chanbbe832c2005-06-24 20:20:04 -07008959 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008960
Linus Torvalds1da177e2005-04-16 15:20:36 -07008961 tp->rx_pending = ering->rx_pending;
8962
8963 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
8964 tp->rx_pending > 63)
8965 tp->rx_pending = 63;
8966 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
8967 tp->tx_pending = ering->tx_pending;
8968
8969 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -07008970 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -07008971 err = tg3_restart_hw(tp, 1);
8972 if (!err)
8973 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008974 }
8975
David S. Millerf47c11e2005-06-24 20:18:35 -07008976 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008977
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008978 if (irq_sync && !err)
8979 tg3_phy_start(tp);
8980
Michael Chanb9ec6c12006-07-25 16:37:27 -07008981 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008982}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008983
Linus Torvalds1da177e2005-04-16 15:20:36 -07008984static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
8985{
8986 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008987
Linus Torvalds1da177e2005-04-16 15:20:36 -07008988 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -08008989
Steve Glendinninge18ce342008-12-16 02:00:00 -08008990 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -08008991 epause->rx_pause = 1;
8992 else
8993 epause->rx_pause = 0;
8994
Steve Glendinninge18ce342008-12-16 02:00:00 -08008995 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -08008996 epause->tx_pause = 1;
8997 else
8998 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008999}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009000
Linus Torvalds1da177e2005-04-16 15:20:36 -07009001static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9002{
9003 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009004 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009005
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009006 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9007 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9008 return -EAGAIN;
9009
9010 if (epause->autoneg) {
9011 u32 newadv;
9012 struct phy_device *phydev;
9013
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07009014 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009015
9016 if (epause->rx_pause) {
9017 if (epause->tx_pause)
9018 newadv = ADVERTISED_Pause;
9019 else
9020 newadv = ADVERTISED_Pause |
9021 ADVERTISED_Asym_Pause;
9022 } else if (epause->tx_pause) {
9023 newadv = ADVERTISED_Asym_Pause;
9024 } else
9025 newadv = 0;
9026
9027 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
9028 u32 oldadv = phydev->advertising &
9029 (ADVERTISED_Pause |
9030 ADVERTISED_Asym_Pause);
9031 if (oldadv != newadv) {
9032 phydev->advertising &=
9033 ~(ADVERTISED_Pause |
9034 ADVERTISED_Asym_Pause);
9035 phydev->advertising |= newadv;
9036 err = phy_start_aneg(phydev);
9037 }
9038 } else {
9039 tp->link_config.advertising &=
9040 ~(ADVERTISED_Pause |
9041 ADVERTISED_Asym_Pause);
9042 tp->link_config.advertising |= newadv;
9043 }
9044 } else {
9045 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009046 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009047 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009048 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009049
9050 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009051 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009052 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009053 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009054
9055 if (netif_running(dev))
9056 tg3_setup_flow_control(tp, 0, 0);
9057 }
9058 } else {
9059 int irq_sync = 0;
9060
9061 if (netif_running(dev)) {
9062 tg3_netif_stop(tp);
9063 irq_sync = 1;
9064 }
9065
9066 tg3_full_lock(tp, irq_sync);
9067
9068 if (epause->autoneg)
9069 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9070 else
9071 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9072 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009073 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009074 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009075 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009076 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009077 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009078 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009079 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009080
9081 if (netif_running(dev)) {
9082 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9083 err = tg3_restart_hw(tp, 1);
9084 if (!err)
9085 tg3_netif_start(tp);
9086 }
9087
9088 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009090
Michael Chanb9ec6c12006-07-25 16:37:27 -07009091 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009092}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009093
Linus Torvalds1da177e2005-04-16 15:20:36 -07009094static u32 tg3_get_rx_csum(struct net_device *dev)
9095{
9096 struct tg3 *tp = netdev_priv(dev);
9097 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
9098}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009099
Linus Torvalds1da177e2005-04-16 15:20:36 -07009100static int tg3_set_rx_csum(struct net_device *dev, u32 data)
9101{
9102 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009103
Linus Torvalds1da177e2005-04-16 15:20:36 -07009104 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
9105 if (data != 0)
9106 return -EINVAL;
9107 return 0;
9108 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009109
David S. Millerf47c11e2005-06-24 20:18:35 -07009110 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009111 if (data)
9112 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
9113 else
9114 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -07009115 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009116
Linus Torvalds1da177e2005-04-16 15:20:36 -07009117 return 0;
9118}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009119
Linus Torvalds1da177e2005-04-16 15:20:36 -07009120static int tg3_set_tx_csum(struct net_device *dev, u32 data)
9121{
9122 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009123
Linus Torvalds1da177e2005-04-16 15:20:36 -07009124 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
9125 if (data != 0)
9126 return -EINVAL;
9127 return 0;
9128 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009129
Matt Carlson321d32a2008-11-21 17:22:19 -08009130 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -07009131 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009132 else
Michael Chan9c27dbd2006-03-20 22:28:27 -08009133 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009134
9135 return 0;
9136}
9137
Jeff Garzikb9f2c042007-10-03 18:07:32 -07009138static int tg3_get_sset_count (struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009139{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07009140 switch (sset) {
9141 case ETH_SS_TEST:
9142 return TG3_NUM_TEST;
9143 case ETH_SS_STATS:
9144 return TG3_NUM_STATS;
9145 default:
9146 return -EOPNOTSUPP;
9147 }
Michael Chan4cafd3f2005-05-29 14:56:34 -07009148}
9149
Linus Torvalds1da177e2005-04-16 15:20:36 -07009150static void tg3_get_strings (struct net_device *dev, u32 stringset, u8 *buf)
9151{
9152 switch (stringset) {
9153 case ETH_SS_STATS:
9154 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
9155 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -07009156 case ETH_SS_TEST:
9157 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
9158 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009159 default:
9160 WARN_ON(1); /* we need a WARN() */
9161 break;
9162 }
9163}
9164
Michael Chan4009a932005-09-05 17:52:54 -07009165static int tg3_phys_id(struct net_device *dev, u32 data)
9166{
9167 struct tg3 *tp = netdev_priv(dev);
9168 int i;
9169
9170 if (!netif_running(tp->dev))
9171 return -EAGAIN;
9172
9173 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -08009174 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -07009175
9176 for (i = 0; i < (data * 2); i++) {
9177 if ((i % 2) == 0)
9178 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
9179 LED_CTRL_1000MBPS_ON |
9180 LED_CTRL_100MBPS_ON |
9181 LED_CTRL_10MBPS_ON |
9182 LED_CTRL_TRAFFIC_OVERRIDE |
9183 LED_CTRL_TRAFFIC_BLINK |
9184 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009185
Michael Chan4009a932005-09-05 17:52:54 -07009186 else
9187 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
9188 LED_CTRL_TRAFFIC_OVERRIDE);
9189
9190 if (msleep_interruptible(500))
9191 break;
9192 }
9193 tw32(MAC_LED_CTRL, tp->led_ctrl);
9194 return 0;
9195}
9196
Linus Torvalds1da177e2005-04-16 15:20:36 -07009197static void tg3_get_ethtool_stats (struct net_device *dev,
9198 struct ethtool_stats *estats, u64 *tmp_stats)
9199{
9200 struct tg3 *tp = netdev_priv(dev);
9201 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
9202}
9203
Michael Chan566f86a2005-05-29 14:56:58 -07009204#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -08009205#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
9206#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
9207#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -07009208#define NVRAM_SELFBOOT_HW_SIZE 0x20
9209#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -07009210
9211static int tg3_test_nvram(struct tg3 *tp)
9212{
Al Virob9fc7dc2007-12-17 22:59:57 -08009213 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009214 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009215 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -07009216
Matt Carlsondf259d82009-04-20 06:57:14 +00009217 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9218 return 0;
9219
Matt Carlsone4f34112009-02-25 14:25:00 +00009220 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -08009221 return -EIO;
9222
Michael Chan1b277772006-03-20 22:27:48 -08009223 if (magic == TG3_EEPROM_MAGIC)
9224 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -07009225 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -08009226 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
9227 TG3_EEPROM_SB_FORMAT_1) {
9228 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
9229 case TG3_EEPROM_SB_REVISION_0:
9230 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
9231 break;
9232 case TG3_EEPROM_SB_REVISION_2:
9233 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
9234 break;
9235 case TG3_EEPROM_SB_REVISION_3:
9236 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
9237 break;
9238 default:
9239 return 0;
9240 }
9241 } else
Michael Chan1b277772006-03-20 22:27:48 -08009242 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -07009243 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
9244 size = NVRAM_SELFBOOT_HW_SIZE;
9245 else
Michael Chan1b277772006-03-20 22:27:48 -08009246 return -EIO;
9247
9248 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -07009249 if (buf == NULL)
9250 return -ENOMEM;
9251
Michael Chan1b277772006-03-20 22:27:48 -08009252 err = -EIO;
9253 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009254 err = tg3_nvram_read_be32(tp, i, &buf[j]);
9255 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -07009256 break;
Michael Chan566f86a2005-05-29 14:56:58 -07009257 }
Michael Chan1b277772006-03-20 22:27:48 -08009258 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -07009259 goto out;
9260
Michael Chan1b277772006-03-20 22:27:48 -08009261 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009262 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -08009263 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -07009264 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -08009265 u8 *buf8 = (u8 *) buf, csum8 = 0;
9266
Al Virob9fc7dc2007-12-17 22:59:57 -08009267 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -08009268 TG3_EEPROM_SB_REVISION_2) {
9269 /* For rev 2, the csum doesn't include the MBA. */
9270 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
9271 csum8 += buf8[i];
9272 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
9273 csum8 += buf8[i];
9274 } else {
9275 for (i = 0; i < size; i++)
9276 csum8 += buf8[i];
9277 }
Michael Chan1b277772006-03-20 22:27:48 -08009278
Adrian Bunkad96b482006-04-05 22:21:04 -07009279 if (csum8 == 0) {
9280 err = 0;
9281 goto out;
9282 }
9283
9284 err = -EIO;
9285 goto out;
Michael Chan1b277772006-03-20 22:27:48 -08009286 }
Michael Chan566f86a2005-05-29 14:56:58 -07009287
Al Virob9fc7dc2007-12-17 22:59:57 -08009288 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -07009289 TG3_EEPROM_MAGIC_HW) {
9290 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +00009291 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -07009292 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -07009293
9294 /* Separate the parity bits and the data bytes. */
9295 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
9296 if ((i == 0) || (i == 8)) {
9297 int l;
9298 u8 msk;
9299
9300 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
9301 parity[k++] = buf8[i] & msk;
9302 i++;
9303 }
9304 else if (i == 16) {
9305 int l;
9306 u8 msk;
9307
9308 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
9309 parity[k++] = buf8[i] & msk;
9310 i++;
9311
9312 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
9313 parity[k++] = buf8[i] & msk;
9314 i++;
9315 }
9316 data[j++] = buf8[i];
9317 }
9318
9319 err = -EIO;
9320 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
9321 u8 hw8 = hweight8(data[i]);
9322
9323 if ((hw8 & 0x1) && parity[i])
9324 goto out;
9325 else if (!(hw8 & 0x1) && !parity[i])
9326 goto out;
9327 }
9328 err = 0;
9329 goto out;
9330 }
9331
Michael Chan566f86a2005-05-29 14:56:58 -07009332 /* Bootstrap checksum at offset 0x10 */
9333 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlsona9dc5292009-02-25 14:25:30 +00009334 if (csum != be32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -07009335 goto out;
9336
9337 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
9338 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlsona9dc5292009-02-25 14:25:30 +00009339 if (csum != be32_to_cpu(buf[0xfc/4]))
9340 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -07009341
9342 err = 0;
9343
9344out:
9345 kfree(buf);
9346 return err;
9347}
9348
Michael Chanca430072005-05-29 14:57:23 -07009349#define TG3_SERDES_TIMEOUT_SEC 2
9350#define TG3_COPPER_TIMEOUT_SEC 6
9351
9352static int tg3_test_link(struct tg3 *tp)
9353{
9354 int i, max;
9355
9356 if (!netif_running(tp->dev))
9357 return -ENODEV;
9358
Michael Chan4c987482005-09-05 17:52:38 -07009359 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -07009360 max = TG3_SERDES_TIMEOUT_SEC;
9361 else
9362 max = TG3_COPPER_TIMEOUT_SEC;
9363
9364 for (i = 0; i < max; i++) {
9365 if (netif_carrier_ok(tp->dev))
9366 return 0;
9367
9368 if (msleep_interruptible(1000))
9369 break;
9370 }
9371
9372 return -EIO;
9373}
9374
Michael Chana71116d2005-05-29 14:58:11 -07009375/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -08009376static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -07009377{
Michael Chanb16250e2006-09-27 16:10:14 -07009378 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -07009379 u32 offset, read_mask, write_mask, val, save_val, read_val;
9380 static struct {
9381 u16 offset;
9382 u16 flags;
9383#define TG3_FL_5705 0x1
9384#define TG3_FL_NOT_5705 0x2
9385#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -07009386#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -07009387 u32 read_mask;
9388 u32 write_mask;
9389 } reg_tbl[] = {
9390 /* MAC Control Registers */
9391 { MAC_MODE, TG3_FL_NOT_5705,
9392 0x00000000, 0x00ef6f8c },
9393 { MAC_MODE, TG3_FL_5705,
9394 0x00000000, 0x01ef6b8c },
9395 { MAC_STATUS, TG3_FL_NOT_5705,
9396 0x03800107, 0x00000000 },
9397 { MAC_STATUS, TG3_FL_5705,
9398 0x03800100, 0x00000000 },
9399 { MAC_ADDR_0_HIGH, 0x0000,
9400 0x00000000, 0x0000ffff },
9401 { MAC_ADDR_0_LOW, 0x0000,
9402 0x00000000, 0xffffffff },
9403 { MAC_RX_MTU_SIZE, 0x0000,
9404 0x00000000, 0x0000ffff },
9405 { MAC_TX_MODE, 0x0000,
9406 0x00000000, 0x00000070 },
9407 { MAC_TX_LENGTHS, 0x0000,
9408 0x00000000, 0x00003fff },
9409 { MAC_RX_MODE, TG3_FL_NOT_5705,
9410 0x00000000, 0x000007fc },
9411 { MAC_RX_MODE, TG3_FL_5705,
9412 0x00000000, 0x000007dc },
9413 { MAC_HASH_REG_0, 0x0000,
9414 0x00000000, 0xffffffff },
9415 { MAC_HASH_REG_1, 0x0000,
9416 0x00000000, 0xffffffff },
9417 { MAC_HASH_REG_2, 0x0000,
9418 0x00000000, 0xffffffff },
9419 { MAC_HASH_REG_3, 0x0000,
9420 0x00000000, 0xffffffff },
9421
9422 /* Receive Data and Receive BD Initiator Control Registers. */
9423 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
9424 0x00000000, 0xffffffff },
9425 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
9426 0x00000000, 0xffffffff },
9427 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
9428 0x00000000, 0x00000003 },
9429 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
9430 0x00000000, 0xffffffff },
9431 { RCVDBDI_STD_BD+0, 0x0000,
9432 0x00000000, 0xffffffff },
9433 { RCVDBDI_STD_BD+4, 0x0000,
9434 0x00000000, 0xffffffff },
9435 { RCVDBDI_STD_BD+8, 0x0000,
9436 0x00000000, 0xffff0002 },
9437 { RCVDBDI_STD_BD+0xc, 0x0000,
9438 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009439
Michael Chana71116d2005-05-29 14:58:11 -07009440 /* Receive BD Initiator Control Registers. */
9441 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
9442 0x00000000, 0xffffffff },
9443 { RCVBDI_STD_THRESH, TG3_FL_5705,
9444 0x00000000, 0x000003ff },
9445 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
9446 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009447
Michael Chana71116d2005-05-29 14:58:11 -07009448 /* Host Coalescing Control Registers. */
9449 { HOSTCC_MODE, TG3_FL_NOT_5705,
9450 0x00000000, 0x00000004 },
9451 { HOSTCC_MODE, TG3_FL_5705,
9452 0x00000000, 0x000000f6 },
9453 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
9454 0x00000000, 0xffffffff },
9455 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
9456 0x00000000, 0x000003ff },
9457 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
9458 0x00000000, 0xffffffff },
9459 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
9460 0x00000000, 0x000003ff },
9461 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
9462 0x00000000, 0xffffffff },
9463 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
9464 0x00000000, 0x000000ff },
9465 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
9466 0x00000000, 0xffffffff },
9467 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
9468 0x00000000, 0x000000ff },
9469 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
9470 0x00000000, 0xffffffff },
9471 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
9472 0x00000000, 0xffffffff },
9473 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
9474 0x00000000, 0xffffffff },
9475 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
9476 0x00000000, 0x000000ff },
9477 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
9478 0x00000000, 0xffffffff },
9479 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
9480 0x00000000, 0x000000ff },
9481 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
9482 0x00000000, 0xffffffff },
9483 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
9484 0x00000000, 0xffffffff },
9485 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
9486 0x00000000, 0xffffffff },
9487 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
9488 0x00000000, 0xffffffff },
9489 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
9490 0x00000000, 0xffffffff },
9491 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
9492 0xffffffff, 0x00000000 },
9493 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
9494 0xffffffff, 0x00000000 },
9495
9496 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -07009497 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -07009498 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -07009499 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -07009500 0x00000000, 0x007fffff },
9501 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
9502 0x00000000, 0x0000003f },
9503 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
9504 0x00000000, 0x000001ff },
9505 { BUFMGR_MB_HIGH_WATER, 0x0000,
9506 0x00000000, 0x000001ff },
9507 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
9508 0xffffffff, 0x00000000 },
9509 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
9510 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009511
Michael Chana71116d2005-05-29 14:58:11 -07009512 /* Mailbox Registers */
9513 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
9514 0x00000000, 0x000001ff },
9515 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
9516 0x00000000, 0x000001ff },
9517 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
9518 0x00000000, 0x000007ff },
9519 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
9520 0x00000000, 0x000001ff },
9521
9522 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
9523 };
9524
Michael Chanb16250e2006-09-27 16:10:14 -07009525 is_5705 = is_5750 = 0;
9526 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -07009527 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -07009528 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
9529 is_5750 = 1;
9530 }
Michael Chana71116d2005-05-29 14:58:11 -07009531
9532 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
9533 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
9534 continue;
9535
9536 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
9537 continue;
9538
9539 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
9540 (reg_tbl[i].flags & TG3_FL_NOT_5788))
9541 continue;
9542
Michael Chanb16250e2006-09-27 16:10:14 -07009543 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
9544 continue;
9545
Michael Chana71116d2005-05-29 14:58:11 -07009546 offset = (u32) reg_tbl[i].offset;
9547 read_mask = reg_tbl[i].read_mask;
9548 write_mask = reg_tbl[i].write_mask;
9549
9550 /* Save the original register content */
9551 save_val = tr32(offset);
9552
9553 /* Determine the read-only value. */
9554 read_val = save_val & read_mask;
9555
9556 /* Write zero to the register, then make sure the read-only bits
9557 * are not changed and the read/write bits are all zeros.
9558 */
9559 tw32(offset, 0);
9560
9561 val = tr32(offset);
9562
9563 /* Test the read-only and read/write bits. */
9564 if (((val & read_mask) != read_val) || (val & write_mask))
9565 goto out;
9566
9567 /* Write ones to all the bits defined by RdMask and WrMask, then
9568 * make sure the read-only bits are not changed and the
9569 * read/write bits are all ones.
9570 */
9571 tw32(offset, read_mask | write_mask);
9572
9573 val = tr32(offset);
9574
9575 /* Test the read-only bits. */
9576 if ((val & read_mask) != read_val)
9577 goto out;
9578
9579 /* Test the read/write bits. */
9580 if ((val & write_mask) != write_mask)
9581 goto out;
9582
9583 tw32(offset, save_val);
9584 }
9585
9586 return 0;
9587
9588out:
Michael Chan9f88f292006-12-07 00:22:54 -08009589 if (netif_msg_hw(tp))
9590 printk(KERN_ERR PFX "Register test failed at offset %x\n",
9591 offset);
Michael Chana71116d2005-05-29 14:58:11 -07009592 tw32(offset, save_val);
9593 return -EIO;
9594}
9595
Michael Chan7942e1d2005-05-29 14:58:36 -07009596static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
9597{
Arjan van de Venf71e1302006-03-03 21:33:57 -05009598 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -07009599 int i;
9600 u32 j;
9601
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +02009602 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -07009603 for (j = 0; j < len; j += 4) {
9604 u32 val;
9605
9606 tg3_write_mem(tp, offset + j, test_pattern[i]);
9607 tg3_read_mem(tp, offset + j, &val);
9608 if (val != test_pattern[i])
9609 return -EIO;
9610 }
9611 }
9612 return 0;
9613}
9614
9615static int tg3_test_memory(struct tg3 *tp)
9616{
9617 static struct mem_entry {
9618 u32 offset;
9619 u32 len;
9620 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -08009621 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -07009622 { 0x00002000, 0x1c000},
9623 { 0xffffffff, 0x00000}
9624 }, mem_tbl_5705[] = {
9625 { 0x00000100, 0x0000c},
9626 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -07009627 { 0x00004000, 0x00800},
9628 { 0x00006000, 0x01000},
9629 { 0x00008000, 0x02000},
9630 { 0x00010000, 0x0e000},
9631 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -08009632 }, mem_tbl_5755[] = {
9633 { 0x00000200, 0x00008},
9634 { 0x00004000, 0x00800},
9635 { 0x00006000, 0x00800},
9636 { 0x00008000, 0x02000},
9637 { 0x00010000, 0x0c000},
9638 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -07009639 }, mem_tbl_5906[] = {
9640 { 0x00000200, 0x00008},
9641 { 0x00004000, 0x00400},
9642 { 0x00006000, 0x00400},
9643 { 0x00008000, 0x01000},
9644 { 0x00010000, 0x01000},
9645 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -07009646 };
9647 struct mem_entry *mem_tbl;
9648 int err = 0;
9649 int i;
9650
Matt Carlson321d32a2008-11-21 17:22:19 -08009651 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
9652 mem_tbl = mem_tbl_5755;
9653 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
9654 mem_tbl = mem_tbl_5906;
9655 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
9656 mem_tbl = mem_tbl_5705;
9657 else
Michael Chan7942e1d2005-05-29 14:58:36 -07009658 mem_tbl = mem_tbl_570x;
9659
9660 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
9661 if ((err = tg3_do_mem_test(tp, mem_tbl[i].offset,
9662 mem_tbl[i].len)) != 0)
9663 break;
9664 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009665
Michael Chan7942e1d2005-05-29 14:58:36 -07009666 return err;
9667}
9668
Michael Chan9f40dea2005-09-05 17:53:06 -07009669#define TG3_MAC_LOOPBACK 0
9670#define TG3_PHY_LOOPBACK 1
9671
9672static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -07009673{
Michael Chan9f40dea2005-09-05 17:53:06 -07009674 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Michael Chanc76949a2005-05-29 14:58:59 -07009675 u32 desc_idx;
9676 struct sk_buff *skb, *rx_skb;
9677 u8 *tx_data;
9678 dma_addr_t map;
9679 int num_pkts, tx_len, rx_len, i, err;
9680 struct tg3_rx_buffer_desc *desc;
9681
Michael Chan9f40dea2005-09-05 17:53:06 -07009682 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -07009683 /* HW errata - mac loopback fails in some cases on 5780.
9684 * Normal traffic and PHY loopback are not affected by
9685 * errata.
9686 */
9687 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
9688 return 0;
9689
Michael Chan9f40dea2005-09-05 17:53:06 -07009690 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009691 MAC_MODE_PORT_INT_LPBACK;
9692 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
9693 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chan3f7045c2006-09-27 16:02:29 -07009694 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
9695 mac_mode |= MAC_MODE_PORT_MODE_MII;
9696 else
9697 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -07009698 tw32(MAC_MODE, mac_mode);
9699 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -07009700 u32 val;
9701
Michael Chanb16250e2006-09-27 16:10:14 -07009702 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
9703 u32 phytest;
9704
9705 if (!tg3_readphy(tp, MII_TG3_EPHY_TEST, &phytest)) {
9706 u32 phy;
9707
9708 tg3_writephy(tp, MII_TG3_EPHY_TEST,
9709 phytest | MII_TG3_EPHY_SHADOW_EN);
9710 if (!tg3_readphy(tp, 0x1b, &phy))
9711 tg3_writephy(tp, 0x1b, phy & ~0x20);
Michael Chanb16250e2006-09-27 16:10:14 -07009712 tg3_writephy(tp, MII_TG3_EPHY_TEST, phytest);
9713 }
Michael Chan5d64ad32006-12-07 00:19:40 -08009714 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
9715 } else
9716 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -07009717
Matt Carlson9ef8ca92007-07-11 19:48:29 -07009718 tg3_phy_toggle_automdix(tp, 0);
9719
Michael Chan3f7045c2006-09-27 16:02:29 -07009720 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -07009721 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -08009722
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009723 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
Michael Chan5d64ad32006-12-07 00:19:40 -08009724 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb16250e2006-09-27 16:10:14 -07009725 tg3_writephy(tp, MII_TG3_EPHY_PTEST, 0x1800);
Michael Chan5d64ad32006-12-07 00:19:40 -08009726 mac_mode |= MAC_MODE_PORT_MODE_MII;
9727 } else
9728 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -07009729
Michael Chanc94e3942005-09-27 12:12:42 -07009730 /* reset to prevent losing 1st rx packet intermittently */
9731 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
9732 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9733 udelay(10);
9734 tw32_f(MAC_RX_MODE, tp->rx_mode);
9735 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009736 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
9737 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)
9738 mac_mode &= ~MAC_MODE_LINK_POLARITY;
9739 else if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411)
9740 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -08009741 tg3_writephy(tp, MII_TG3_EXT_CTRL,
9742 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
9743 }
Michael Chan9f40dea2005-09-05 17:53:06 -07009744 tw32(MAC_MODE, mac_mode);
Michael Chan9f40dea2005-09-05 17:53:06 -07009745 }
9746 else
9747 return -EINVAL;
Michael Chanc76949a2005-05-29 14:58:59 -07009748
9749 err = -EIO;
9750
Michael Chanc76949a2005-05-29 14:58:59 -07009751 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -07009752 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -07009753 if (!skb)
9754 return -ENOMEM;
9755
Michael Chanc76949a2005-05-29 14:58:59 -07009756 tx_data = skb_put(skb, tx_len);
9757 memcpy(tx_data, tp->dev->dev_addr, 6);
9758 memset(tx_data + 6, 0x0, 8);
9759
9760 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
9761
9762 for (i = 14; i < tx_len; i++)
9763 tx_data[i] = (u8) (i & 0xff);
9764
9765 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
9766
9767 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
9768 HOSTCC_MODE_NOW);
9769
9770 udelay(10);
9771
9772 rx_start_idx = tp->hw_status->idx[0].rx_producer;
9773
Michael Chanc76949a2005-05-29 14:58:59 -07009774 num_pkts = 0;
9775
Michael Chan9f40dea2005-09-05 17:53:06 -07009776 tg3_set_txd(tp, tp->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -07009777
Michael Chan9f40dea2005-09-05 17:53:06 -07009778 tp->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -07009779 num_pkts++;
9780
Michael Chan9f40dea2005-09-05 17:53:06 -07009781 tw32_tx_mbox(MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW,
9782 tp->tx_prod);
Michael Chan09ee9292005-08-09 20:17:00 -07009783 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW);
Michael Chanc76949a2005-05-29 14:58:59 -07009784
9785 udelay(10);
9786
Michael Chan3f7045c2006-09-27 16:02:29 -07009787 /* 250 usec to allow enough time on some 10/100 Mbps devices. */
9788 for (i = 0; i < 25; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -07009789 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
9790 HOSTCC_MODE_NOW);
9791
9792 udelay(10);
9793
9794 tx_idx = tp->hw_status->idx[0].tx_consumer;
9795 rx_idx = tp->hw_status->idx[0].rx_producer;
Michael Chan9f40dea2005-09-05 17:53:06 -07009796 if ((tx_idx == tp->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -07009797 (rx_idx == (rx_start_idx + num_pkts)))
9798 break;
9799 }
9800
9801 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
9802 dev_kfree_skb(skb);
9803
Michael Chan9f40dea2005-09-05 17:53:06 -07009804 if (tx_idx != tp->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -07009805 goto out;
9806
9807 if (rx_idx != rx_start_idx + num_pkts)
9808 goto out;
9809
9810 desc = &tp->rx_rcb[rx_start_idx];
9811 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
9812 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
9813 if (opaque_key != RXD_OPAQUE_RING_STD)
9814 goto out;
9815
9816 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
9817 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
9818 goto out;
9819
9820 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
9821 if (rx_len != tx_len)
9822 goto out;
9823
9824 rx_skb = tp->rx_std_buffers[desc_idx].skb;
9825
9826 map = pci_unmap_addr(&tp->rx_std_buffers[desc_idx], mapping);
9827 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
9828
9829 for (i = 14; i < tx_len; i++) {
9830 if (*(rx_skb->data + i) != (u8) (i & 0xff))
9831 goto out;
9832 }
9833 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009834
Michael Chanc76949a2005-05-29 14:58:59 -07009835 /* tg3_free_rings will unmap and free the rx_skb */
9836out:
9837 return err;
9838}
9839
Michael Chan9f40dea2005-09-05 17:53:06 -07009840#define TG3_MAC_LOOPBACK_FAILED 1
9841#define TG3_PHY_LOOPBACK_FAILED 2
9842#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
9843 TG3_PHY_LOOPBACK_FAILED)
9844
9845static int tg3_test_loopback(struct tg3 *tp)
9846{
9847 int err = 0;
Matt Carlson9936bcf2007-10-10 18:03:07 -07009848 u32 cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -07009849
9850 if (!netif_running(tp->dev))
9851 return TG3_LOOPBACK_FAILED;
9852
Michael Chanb9ec6c12006-07-25 16:37:27 -07009853 err = tg3_reset_hw(tp, 1);
9854 if (err)
9855 return TG3_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -07009856
Matt Carlson6833c042008-11-21 17:18:59 -08009857 /* Turn off gphy autopowerdown. */
9858 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
9859 tg3_phy_toggle_apd(tp, false);
9860
Matt Carlson321d32a2008-11-21 17:22:19 -08009861 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009862 int i;
9863 u32 status;
9864
9865 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
9866
9867 /* Wait for up to 40 microseconds to acquire lock. */
9868 for (i = 0; i < 4; i++) {
9869 status = tr32(TG3_CPMU_MUTEX_GNT);
9870 if (status == CPMU_MUTEX_GNT_DRIVER)
9871 break;
9872 udelay(10);
9873 }
9874
9875 if (status != CPMU_MUTEX_GNT_DRIVER)
9876 return TG3_LOOPBACK_FAILED;
9877
Matt Carlsonb2a5c192008-04-03 21:44:44 -07009878 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -08009879 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -07009880 tw32(TG3_CPMU_CTRL,
9881 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
9882 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -07009883 }
9884
Michael Chan9f40dea2005-09-05 17:53:06 -07009885 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
9886 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -07009887
Matt Carlson321d32a2008-11-21 17:22:19 -08009888 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009889 tw32(TG3_CPMU_CTRL, cpmuctrl);
9890
9891 /* Release the mutex */
9892 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
9893 }
9894
Matt Carlsondd477002008-05-25 23:45:58 -07009895 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
9896 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -07009897 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
9898 err |= TG3_PHY_LOOPBACK_FAILED;
9899 }
9900
Matt Carlson6833c042008-11-21 17:18:59 -08009901 /* Re-enable gphy autopowerdown. */
9902 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
9903 tg3_phy_toggle_apd(tp, true);
9904
Michael Chan9f40dea2005-09-05 17:53:06 -07009905 return err;
9906}
9907
Michael Chan4cafd3f2005-05-29 14:56:34 -07009908static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
9909 u64 *data)
9910{
Michael Chan566f86a2005-05-29 14:56:58 -07009911 struct tg3 *tp = netdev_priv(dev);
9912
Michael Chanbc1c7562006-03-20 17:48:03 -08009913 if (tp->link_config.phy_is_low_power)
9914 tg3_set_power_state(tp, PCI_D0);
9915
Michael Chan566f86a2005-05-29 14:56:58 -07009916 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
9917
9918 if (tg3_test_nvram(tp) != 0) {
9919 etest->flags |= ETH_TEST_FL_FAILED;
9920 data[0] = 1;
9921 }
Michael Chanca430072005-05-29 14:57:23 -07009922 if (tg3_test_link(tp) != 0) {
9923 etest->flags |= ETH_TEST_FL_FAILED;
9924 data[1] = 1;
9925 }
Michael Chana71116d2005-05-29 14:58:11 -07009926 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009927 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -07009928
Michael Chanbbe832c2005-06-24 20:20:04 -07009929 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009930 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009931 tg3_netif_stop(tp);
9932 irq_sync = 1;
9933 }
9934
9935 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -07009936
9937 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -08009938 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -07009939 tg3_halt_cpu(tp, RX_CPU_BASE);
9940 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
9941 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08009942 if (!err)
9943 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -07009944
Michael Chand9ab5ad12006-03-20 22:27:35 -08009945 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
9946 tg3_phy_reset(tp);
9947
Michael Chana71116d2005-05-29 14:58:11 -07009948 if (tg3_test_registers(tp) != 0) {
9949 etest->flags |= ETH_TEST_FL_FAILED;
9950 data[2] = 1;
9951 }
Michael Chan7942e1d2005-05-29 14:58:36 -07009952 if (tg3_test_memory(tp) != 0) {
9953 etest->flags |= ETH_TEST_FL_FAILED;
9954 data[3] = 1;
9955 }
Michael Chan9f40dea2005-09-05 17:53:06 -07009956 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -07009957 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -07009958
David S. Millerf47c11e2005-06-24 20:18:35 -07009959 tg3_full_unlock(tp);
9960
Michael Chand4bc3922005-05-29 14:59:20 -07009961 if (tg3_test_interrupt(tp) != 0) {
9962 etest->flags |= ETH_TEST_FL_FAILED;
9963 data[5] = 1;
9964 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009965
9966 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -07009967
Michael Chana71116d2005-05-29 14:58:11 -07009968 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9969 if (netif_running(dev)) {
9970 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009971 err2 = tg3_restart_hw(tp, 1);
9972 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -07009973 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -07009974 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009975
9976 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009977
9978 if (irq_sync && !err2)
9979 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -07009980 }
Michael Chanbc1c7562006-03-20 17:48:03 -08009981 if (tp->link_config.phy_is_low_power)
9982 tg3_set_power_state(tp, PCI_D3hot);
9983
Michael Chan4cafd3f2005-05-29 14:56:34 -07009984}
9985
Linus Torvalds1da177e2005-04-16 15:20:36 -07009986static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
9987{
9988 struct mii_ioctl_data *data = if_mii(ifr);
9989 struct tg3 *tp = netdev_priv(dev);
9990 int err;
9991
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009992 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9993 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9994 return -EAGAIN;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07009995 return phy_mii_ioctl(tp->mdio_bus->phy_map[PHY_ADDR], data, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009996 }
9997
Linus Torvalds1da177e2005-04-16 15:20:36 -07009998 switch(cmd) {
9999 case SIOCGMIIPHY:
10000 data->phy_id = PHY_ADDR;
10001
10002 /* fallthru */
10003 case SIOCGMIIREG: {
10004 u32 mii_regval;
10005
10006 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10007 break; /* We have no PHY */
10008
Michael Chanbc1c7562006-03-20 17:48:03 -080010009 if (tp->link_config.phy_is_low_power)
10010 return -EAGAIN;
10011
David S. Millerf47c11e2005-06-24 20:18:35 -070010012 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010013 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070010014 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010015
10016 data->val_out = mii_regval;
10017
10018 return err;
10019 }
10020
10021 case SIOCSMIIREG:
10022 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10023 break; /* We have no PHY */
10024
10025 if (!capable(CAP_NET_ADMIN))
10026 return -EPERM;
10027
Michael Chanbc1c7562006-03-20 17:48:03 -080010028 if (tp->link_config.phy_is_low_power)
10029 return -EAGAIN;
10030
David S. Millerf47c11e2005-06-24 20:18:35 -070010031 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010032 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070010033 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010034
10035 return err;
10036
10037 default:
10038 /* do nothing */
10039 break;
10040 }
10041 return -EOPNOTSUPP;
10042}
10043
10044#if TG3_VLAN_TAG_USED
10045static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
10046{
10047 struct tg3 *tp = netdev_priv(dev);
10048
Matt Carlson844b3ee2009-02-25 14:23:56 +000010049 if (!netif_running(dev)) {
10050 tp->vlgrp = grp;
10051 return;
10052 }
10053
10054 tg3_netif_stop(tp);
Michael Chan29315e82006-06-29 20:12:30 -070010055
David S. Millerf47c11e2005-06-24 20:18:35 -070010056 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010057
10058 tp->vlgrp = grp;
10059
10060 /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
10061 __tg3_set_rx_mode(dev);
10062
Matt Carlson844b3ee2009-02-25 14:23:56 +000010063 tg3_netif_start(tp);
Michael Chan46966542007-07-11 19:47:19 -070010064
10065 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010066}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010067#endif
10068
David S. Miller15f98502005-05-18 22:49:26 -070010069static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
10070{
10071 struct tg3 *tp = netdev_priv(dev);
10072
10073 memcpy(ec, &tp->coal, sizeof(*ec));
10074 return 0;
10075}
10076
Michael Chand244c892005-07-05 14:42:33 -070010077static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
10078{
10079 struct tg3 *tp = netdev_priv(dev);
10080 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
10081 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
10082
10083 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
10084 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
10085 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
10086 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
10087 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
10088 }
10089
10090 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
10091 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
10092 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
10093 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
10094 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
10095 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
10096 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
10097 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
10098 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
10099 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
10100 return -EINVAL;
10101
10102 /* No rx interrupts will be generated if both are zero */
10103 if ((ec->rx_coalesce_usecs == 0) &&
10104 (ec->rx_max_coalesced_frames == 0))
10105 return -EINVAL;
10106
10107 /* No tx interrupts will be generated if both are zero */
10108 if ((ec->tx_coalesce_usecs == 0) &&
10109 (ec->tx_max_coalesced_frames == 0))
10110 return -EINVAL;
10111
10112 /* Only copy relevant parameters, ignore all others. */
10113 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
10114 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
10115 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
10116 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
10117 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
10118 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
10119 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
10120 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
10121 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
10122
10123 if (netif_running(dev)) {
10124 tg3_full_lock(tp, 0);
10125 __tg3_set_coalesce(tp, &tp->coal);
10126 tg3_full_unlock(tp);
10127 }
10128 return 0;
10129}
10130
Jeff Garzik7282d492006-09-13 14:30:00 -040010131static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010132 .get_settings = tg3_get_settings,
10133 .set_settings = tg3_set_settings,
10134 .get_drvinfo = tg3_get_drvinfo,
10135 .get_regs_len = tg3_get_regs_len,
10136 .get_regs = tg3_get_regs,
10137 .get_wol = tg3_get_wol,
10138 .set_wol = tg3_set_wol,
10139 .get_msglevel = tg3_get_msglevel,
10140 .set_msglevel = tg3_set_msglevel,
10141 .nway_reset = tg3_nway_reset,
10142 .get_link = ethtool_op_get_link,
10143 .get_eeprom_len = tg3_get_eeprom_len,
10144 .get_eeprom = tg3_get_eeprom,
10145 .set_eeprom = tg3_set_eeprom,
10146 .get_ringparam = tg3_get_ringparam,
10147 .set_ringparam = tg3_set_ringparam,
10148 .get_pauseparam = tg3_get_pauseparam,
10149 .set_pauseparam = tg3_set_pauseparam,
10150 .get_rx_csum = tg3_get_rx_csum,
10151 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010152 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010153 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010154 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070010155 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010156 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070010157 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010158 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070010159 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070010160 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010161 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010162};
10163
10164static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
10165{
Michael Chan1b277772006-03-20 22:27:48 -080010166 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010167
10168 tp->nvram_size = EEPROM_CHIP_SIZE;
10169
Matt Carlsone4f34112009-02-25 14:25:00 +000010170 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010171 return;
10172
Michael Chanb16250e2006-09-27 16:10:14 -070010173 if ((magic != TG3_EEPROM_MAGIC) &&
10174 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
10175 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010176 return;
10177
10178 /*
10179 * Size the chip by reading offsets at increasing powers of two.
10180 * When we encounter our validation signature, we know the addressing
10181 * has wrapped around, and thus have our chip size.
10182 */
Michael Chan1b277772006-03-20 22:27:48 -080010183 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010184
10185 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000010186 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010187 return;
10188
Michael Chan18201802006-03-20 22:29:15 -080010189 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010190 break;
10191
10192 cursize <<= 1;
10193 }
10194
10195 tp->nvram_size = cursize;
10196}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010197
Linus Torvalds1da177e2005-04-16 15:20:36 -070010198static void __devinit tg3_get_nvram_size(struct tg3 *tp)
10199{
10200 u32 val;
10201
Matt Carlsondf259d82009-04-20 06:57:14 +000010202 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
10203 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010204 return;
10205
10206 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080010207 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080010208 tg3_get_eeprom_size(tp);
10209 return;
10210 }
10211
Matt Carlson6d348f22009-02-25 14:25:52 +000010212 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010213 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000010214 /* This is confusing. We want to operate on the
10215 * 16-bit value at offset 0xf2. The tg3_nvram_read()
10216 * call will read from NVRAM and byteswap the data
10217 * according to the byteswapping settings for all
10218 * other register accesses. This ensures the data we
10219 * want will always reside in the lower 16-bits.
10220 * However, the data in NVRAM is in LE format, which
10221 * means the data from the NVRAM read will always be
10222 * opposite the endianness of the CPU. The 16-bit
10223 * byteswap then brings the data to CPU endianness.
10224 */
10225 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010226 return;
10227 }
10228 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010229 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010230}
10231
10232static void __devinit tg3_get_nvram_info(struct tg3 *tp)
10233{
10234 u32 nvcfg1;
10235
10236 nvcfg1 = tr32(NVRAM_CFG1);
10237 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
10238 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10239 }
10240 else {
10241 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
10242 tw32(NVRAM_CFG1, nvcfg1);
10243 }
10244
Michael Chan4c987482005-09-05 17:52:38 -070010245 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070010246 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010247 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
10248 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
10249 tp->nvram_jedecnum = JEDEC_ATMEL;
10250 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
10251 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10252 break;
10253 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
10254 tp->nvram_jedecnum = JEDEC_ATMEL;
10255 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
10256 break;
10257 case FLASH_VENDOR_ATMEL_EEPROM:
10258 tp->nvram_jedecnum = JEDEC_ATMEL;
10259 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
10260 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10261 break;
10262 case FLASH_VENDOR_ST:
10263 tp->nvram_jedecnum = JEDEC_ST;
10264 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
10265 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10266 break;
10267 case FLASH_VENDOR_SAIFUN:
10268 tp->nvram_jedecnum = JEDEC_SAIFUN;
10269 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
10270 break;
10271 case FLASH_VENDOR_SST_SMALL:
10272 case FLASH_VENDOR_SST_LARGE:
10273 tp->nvram_jedecnum = JEDEC_SST;
10274 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
10275 break;
10276 }
10277 }
10278 else {
10279 tp->nvram_jedecnum = JEDEC_ATMEL;
10280 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
10281 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10282 }
10283}
10284
Michael Chan361b4ac2005-04-21 17:11:21 -070010285static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
10286{
10287 u32 nvcfg1;
10288
10289 nvcfg1 = tr32(NVRAM_CFG1);
10290
Michael Chane6af3012005-04-21 17:12:05 -070010291 /* NVRAM protection for TPM */
10292 if (nvcfg1 & (1 << 27))
10293 tp->tg3_flags2 |= TG3_FLG2_PROTECTED_NVRAM;
10294
Michael Chan361b4ac2005-04-21 17:11:21 -070010295 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
10296 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
10297 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
10298 tp->nvram_jedecnum = JEDEC_ATMEL;
10299 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10300 break;
10301 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
10302 tp->nvram_jedecnum = JEDEC_ATMEL;
10303 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10304 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10305 break;
10306 case FLASH_5752VENDOR_ST_M45PE10:
10307 case FLASH_5752VENDOR_ST_M45PE20:
10308 case FLASH_5752VENDOR_ST_M45PE40:
10309 tp->nvram_jedecnum = JEDEC_ST;
10310 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10311 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10312 break;
10313 }
10314
10315 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
10316 switch (nvcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
10317 case FLASH_5752PAGE_SIZE_256:
10318 tp->nvram_pagesize = 256;
10319 break;
10320 case FLASH_5752PAGE_SIZE_512:
10321 tp->nvram_pagesize = 512;
10322 break;
10323 case FLASH_5752PAGE_SIZE_1K:
10324 tp->nvram_pagesize = 1024;
10325 break;
10326 case FLASH_5752PAGE_SIZE_2K:
10327 tp->nvram_pagesize = 2048;
10328 break;
10329 case FLASH_5752PAGE_SIZE_4K:
10330 tp->nvram_pagesize = 4096;
10331 break;
10332 case FLASH_5752PAGE_SIZE_264:
10333 tp->nvram_pagesize = 264;
10334 break;
10335 }
10336 }
10337 else {
10338 /* For eeprom, set pagesize to maximum eeprom size */
10339 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
10340
10341 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
10342 tw32(NVRAM_CFG1, nvcfg1);
10343 }
10344}
10345
Michael Chand3c7b882006-03-23 01:28:25 -080010346static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
10347{
Matt Carlson989a9d22007-05-05 11:51:05 -070010348 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080010349
10350 nvcfg1 = tr32(NVRAM_CFG1);
10351
10352 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070010353 if (nvcfg1 & (1 << 27)) {
Michael Chand3c7b882006-03-23 01:28:25 -080010354 tp->tg3_flags2 |= TG3_FLG2_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070010355 protect = 1;
10356 }
Michael Chand3c7b882006-03-23 01:28:25 -080010357
Matt Carlson989a9d22007-05-05 11:51:05 -070010358 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
10359 switch (nvcfg1) {
Michael Chand3c7b882006-03-23 01:28:25 -080010360 case FLASH_5755VENDOR_ATMEL_FLASH_1:
10361 case FLASH_5755VENDOR_ATMEL_FLASH_2:
10362 case FLASH_5755VENDOR_ATMEL_FLASH_3:
Matt Carlson70b65a22007-07-11 19:48:50 -070010363 case FLASH_5755VENDOR_ATMEL_FLASH_5:
Michael Chand3c7b882006-03-23 01:28:25 -080010364 tp->nvram_jedecnum = JEDEC_ATMEL;
10365 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10366 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10367 tp->nvram_pagesize = 264;
Matt Carlson70b65a22007-07-11 19:48:50 -070010368 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
10369 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010370 tp->nvram_size = (protect ? 0x3e200 :
10371 TG3_NVRAM_SIZE_512KB);
Matt Carlson989a9d22007-05-05 11:51:05 -070010372 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010373 tp->nvram_size = (protect ? 0x1f200 :
10374 TG3_NVRAM_SIZE_256KB);
Matt Carlson989a9d22007-05-05 11:51:05 -070010375 else
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010376 tp->nvram_size = (protect ? 0x1f200 :
10377 TG3_NVRAM_SIZE_128KB);
Michael Chand3c7b882006-03-23 01:28:25 -080010378 break;
10379 case FLASH_5752VENDOR_ST_M45PE10:
10380 case FLASH_5752VENDOR_ST_M45PE20:
10381 case FLASH_5752VENDOR_ST_M45PE40:
10382 tp->nvram_jedecnum = JEDEC_ST;
10383 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10384 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10385 tp->nvram_pagesize = 256;
Matt Carlson989a9d22007-05-05 11:51:05 -070010386 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010387 tp->nvram_size = (protect ?
10388 TG3_NVRAM_SIZE_64KB :
10389 TG3_NVRAM_SIZE_128KB);
Matt Carlson989a9d22007-05-05 11:51:05 -070010390 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010391 tp->nvram_size = (protect ?
10392 TG3_NVRAM_SIZE_64KB :
10393 TG3_NVRAM_SIZE_256KB);
Matt Carlson989a9d22007-05-05 11:51:05 -070010394 else
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010395 tp->nvram_size = (protect ?
10396 TG3_NVRAM_SIZE_128KB :
10397 TG3_NVRAM_SIZE_512KB);
Michael Chand3c7b882006-03-23 01:28:25 -080010398 break;
10399 }
10400}
10401
Michael Chan1b277772006-03-20 22:27:48 -080010402static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
10403{
10404 u32 nvcfg1;
10405
10406 nvcfg1 = tr32(NVRAM_CFG1);
10407
10408 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
10409 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
10410 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
10411 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
10412 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
10413 tp->nvram_jedecnum = JEDEC_ATMEL;
10414 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10415 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
10416
10417 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
10418 tw32(NVRAM_CFG1, nvcfg1);
10419 break;
10420 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
10421 case FLASH_5755VENDOR_ATMEL_FLASH_1:
10422 case FLASH_5755VENDOR_ATMEL_FLASH_2:
10423 case FLASH_5755VENDOR_ATMEL_FLASH_3:
10424 tp->nvram_jedecnum = JEDEC_ATMEL;
10425 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10426 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10427 tp->nvram_pagesize = 264;
10428 break;
10429 case FLASH_5752VENDOR_ST_M45PE10:
10430 case FLASH_5752VENDOR_ST_M45PE20:
10431 case FLASH_5752VENDOR_ST_M45PE40:
10432 tp->nvram_jedecnum = JEDEC_ST;
10433 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10434 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10435 tp->nvram_pagesize = 256;
10436 break;
10437 }
10438}
10439
Matt Carlson6b91fa02007-10-10 18:01:09 -070010440static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
10441{
10442 u32 nvcfg1, protect = 0;
10443
10444 nvcfg1 = tr32(NVRAM_CFG1);
10445
10446 /* NVRAM protection for TPM */
10447 if (nvcfg1 & (1 << 27)) {
10448 tp->tg3_flags2 |= TG3_FLG2_PROTECTED_NVRAM;
10449 protect = 1;
10450 }
10451
10452 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
10453 switch (nvcfg1) {
10454 case FLASH_5761VENDOR_ATMEL_ADB021D:
10455 case FLASH_5761VENDOR_ATMEL_ADB041D:
10456 case FLASH_5761VENDOR_ATMEL_ADB081D:
10457 case FLASH_5761VENDOR_ATMEL_ADB161D:
10458 case FLASH_5761VENDOR_ATMEL_MDB021D:
10459 case FLASH_5761VENDOR_ATMEL_MDB041D:
10460 case FLASH_5761VENDOR_ATMEL_MDB081D:
10461 case FLASH_5761VENDOR_ATMEL_MDB161D:
10462 tp->nvram_jedecnum = JEDEC_ATMEL;
10463 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10464 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10465 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
10466 tp->nvram_pagesize = 256;
10467 break;
10468 case FLASH_5761VENDOR_ST_A_M45PE20:
10469 case FLASH_5761VENDOR_ST_A_M45PE40:
10470 case FLASH_5761VENDOR_ST_A_M45PE80:
10471 case FLASH_5761VENDOR_ST_A_M45PE16:
10472 case FLASH_5761VENDOR_ST_M_M45PE20:
10473 case FLASH_5761VENDOR_ST_M_M45PE40:
10474 case FLASH_5761VENDOR_ST_M_M45PE80:
10475 case FLASH_5761VENDOR_ST_M_M45PE16:
10476 tp->nvram_jedecnum = JEDEC_ST;
10477 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10478 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10479 tp->nvram_pagesize = 256;
10480 break;
10481 }
10482
10483 if (protect) {
10484 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
10485 } else {
10486 switch (nvcfg1) {
10487 case FLASH_5761VENDOR_ATMEL_ADB161D:
10488 case FLASH_5761VENDOR_ATMEL_MDB161D:
10489 case FLASH_5761VENDOR_ST_A_M45PE16:
10490 case FLASH_5761VENDOR_ST_M_M45PE16:
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010491 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
Matt Carlson6b91fa02007-10-10 18:01:09 -070010492 break;
10493 case FLASH_5761VENDOR_ATMEL_ADB081D:
10494 case FLASH_5761VENDOR_ATMEL_MDB081D:
10495 case FLASH_5761VENDOR_ST_A_M45PE80:
10496 case FLASH_5761VENDOR_ST_M_M45PE80:
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010497 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
Matt Carlson6b91fa02007-10-10 18:01:09 -070010498 break;
10499 case FLASH_5761VENDOR_ATMEL_ADB041D:
10500 case FLASH_5761VENDOR_ATMEL_MDB041D:
10501 case FLASH_5761VENDOR_ST_A_M45PE40:
10502 case FLASH_5761VENDOR_ST_M_M45PE40:
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010503 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Matt Carlson6b91fa02007-10-10 18:01:09 -070010504 break;
10505 case FLASH_5761VENDOR_ATMEL_ADB021D:
10506 case FLASH_5761VENDOR_ATMEL_MDB021D:
10507 case FLASH_5761VENDOR_ST_A_M45PE20:
10508 case FLASH_5761VENDOR_ST_M_M45PE20:
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010509 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
Matt Carlson6b91fa02007-10-10 18:01:09 -070010510 break;
10511 }
10512 }
10513}
10514
Michael Chanb5d37722006-09-27 16:06:21 -070010515static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
10516{
10517 tp->nvram_jedecnum = JEDEC_ATMEL;
10518 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10519 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
10520}
10521
Matt Carlson321d32a2008-11-21 17:22:19 -080010522static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
10523{
10524 u32 nvcfg1;
10525
10526 nvcfg1 = tr32(NVRAM_CFG1);
10527
10528 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
10529 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
10530 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
10531 tp->nvram_jedecnum = JEDEC_ATMEL;
10532 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10533 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
10534
10535 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
10536 tw32(NVRAM_CFG1, nvcfg1);
10537 return;
10538 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
10539 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
10540 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
10541 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
10542 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
10543 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
10544 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
10545 tp->nvram_jedecnum = JEDEC_ATMEL;
10546 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10547 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10548
10549 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
10550 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
10551 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
10552 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
10553 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
10554 break;
10555 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
10556 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
10557 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
10558 break;
10559 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
10560 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
10561 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
10562 break;
10563 }
10564 break;
10565 case FLASH_5752VENDOR_ST_M45PE10:
10566 case FLASH_5752VENDOR_ST_M45PE20:
10567 case FLASH_5752VENDOR_ST_M45PE40:
10568 tp->nvram_jedecnum = JEDEC_ST;
10569 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10570 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10571
10572 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
10573 case FLASH_5752VENDOR_ST_M45PE10:
10574 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
10575 break;
10576 case FLASH_5752VENDOR_ST_M45PE20:
10577 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
10578 break;
10579 case FLASH_5752VENDOR_ST_M45PE40:
10580 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
10581 break;
10582 }
10583 break;
10584 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000010585 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080010586 return;
10587 }
10588
10589 switch (nvcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
10590 case FLASH_5752PAGE_SIZE_256:
10591 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
10592 tp->nvram_pagesize = 256;
10593 break;
10594 case FLASH_5752PAGE_SIZE_512:
10595 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
10596 tp->nvram_pagesize = 512;
10597 break;
10598 case FLASH_5752PAGE_SIZE_1K:
10599 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
10600 tp->nvram_pagesize = 1024;
10601 break;
10602 case FLASH_5752PAGE_SIZE_2K:
10603 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
10604 tp->nvram_pagesize = 2048;
10605 break;
10606 case FLASH_5752PAGE_SIZE_4K:
10607 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
10608 tp->nvram_pagesize = 4096;
10609 break;
10610 case FLASH_5752PAGE_SIZE_264:
10611 tp->nvram_pagesize = 264;
10612 break;
10613 case FLASH_5752PAGE_SIZE_528:
10614 tp->nvram_pagesize = 528;
10615 break;
10616 }
10617}
10618
Linus Torvalds1da177e2005-04-16 15:20:36 -070010619/* Chips other than 5700/5701 use the NVRAM for fetching info. */
10620static void __devinit tg3_nvram_init(struct tg3 *tp)
10621{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010622 tw32_f(GRC_EEPROM_ADDR,
10623 (EEPROM_ADDR_FSM_RESET |
10624 (EEPROM_DEFAULT_CLOCK_PERIOD <<
10625 EEPROM_ADDR_CLKPERD_SHIFT)));
10626
Michael Chan9d57f012006-12-07 00:23:25 -080010627 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010628
10629 /* Enable seeprom accesses. */
10630 tw32_f(GRC_LOCAL_CTRL,
10631 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
10632 udelay(100);
10633
10634 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
10635 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
10636 tp->tg3_flags |= TG3_FLAG_NVRAM;
10637
Michael Chanec41c7d2006-01-17 02:40:55 -080010638 if (tg3_nvram_lock(tp)) {
10639 printk(KERN_WARNING PFX "%s: Cannot get nvarm lock, "
10640 "tg3_nvram_init failed.\n", tp->dev->name);
10641 return;
10642 }
Michael Chane6af3012005-04-21 17:12:05 -070010643 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010644
Matt Carlson989a9d22007-05-05 11:51:05 -070010645 tp->nvram_size = 0;
10646
Michael Chan361b4ac2005-04-21 17:11:21 -070010647 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
10648 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080010649 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
10650 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070010651 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070010652 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
10653 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080010654 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070010655 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
10656 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070010657 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10658 tg3_get_5906_nvram_info(tp);
Matt Carlson321d32a2008-11-21 17:22:19 -080010659 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
10660 tg3_get_57780_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070010661 else
10662 tg3_get_nvram_info(tp);
10663
Matt Carlson989a9d22007-05-05 11:51:05 -070010664 if (tp->nvram_size == 0)
10665 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010666
Michael Chane6af3012005-04-21 17:12:05 -070010667 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080010668 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010669
10670 } else {
10671 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
10672
10673 tg3_get_eeprom_size(tp);
10674 }
10675}
10676
Linus Torvalds1da177e2005-04-16 15:20:36 -070010677static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
10678 u32 offset, u32 len, u8 *buf)
10679{
10680 int i, j, rc = 0;
10681 u32 val;
10682
10683 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080010684 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010685 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010686
10687 addr = offset + i;
10688
10689 memcpy(&data, buf + i, 4);
10690
Matt Carlson62cedd12009-04-20 14:52:29 -070010691 /*
10692 * The SEEPROM interface expects the data to always be opposite
10693 * the native endian format. We accomplish this by reversing
10694 * all the operations that would have been performed on the
10695 * data from a call to tg3_nvram_read_be32().
10696 */
10697 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010698
10699 val = tr32(GRC_EEPROM_ADDR);
10700 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
10701
10702 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
10703 EEPROM_ADDR_READ);
10704 tw32(GRC_EEPROM_ADDR, val |
10705 (0 << EEPROM_ADDR_DEVID_SHIFT) |
10706 (addr & EEPROM_ADDR_ADDR_MASK) |
10707 EEPROM_ADDR_START |
10708 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010709
Michael Chan9d57f012006-12-07 00:23:25 -080010710 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010711 val = tr32(GRC_EEPROM_ADDR);
10712
10713 if (val & EEPROM_ADDR_COMPLETE)
10714 break;
Michael Chan9d57f012006-12-07 00:23:25 -080010715 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010716 }
10717 if (!(val & EEPROM_ADDR_COMPLETE)) {
10718 rc = -EBUSY;
10719 break;
10720 }
10721 }
10722
10723 return rc;
10724}
10725
10726/* offset and length are dword aligned */
10727static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
10728 u8 *buf)
10729{
10730 int ret = 0;
10731 u32 pagesize = tp->nvram_pagesize;
10732 u32 pagemask = pagesize - 1;
10733 u32 nvram_cmd;
10734 u8 *tmp;
10735
10736 tmp = kmalloc(pagesize, GFP_KERNEL);
10737 if (tmp == NULL)
10738 return -ENOMEM;
10739
10740 while (len) {
10741 int j;
Michael Chane6af3012005-04-21 17:12:05 -070010742 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010743
10744 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010745
Linus Torvalds1da177e2005-04-16 15:20:36 -070010746 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010747 ret = tg3_nvram_read_be32(tp, phy_addr + j,
10748 (__be32 *) (tmp + j));
10749 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010750 break;
10751 }
10752 if (ret)
10753 break;
10754
10755 page_off = offset & pagemask;
10756 size = pagesize;
10757 if (len < size)
10758 size = len;
10759
10760 len -= size;
10761
10762 memcpy(tmp + page_off, buf, size);
10763
10764 offset = offset + (pagesize - page_off);
10765
Michael Chane6af3012005-04-21 17:12:05 -070010766 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010767
10768 /*
10769 * Before we can erase the flash page, we need
10770 * to issue a special "write enable" command.
10771 */
10772 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
10773
10774 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
10775 break;
10776
10777 /* Erase the target page */
10778 tw32(NVRAM_ADDR, phy_addr);
10779
10780 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
10781 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
10782
10783 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
10784 break;
10785
10786 /* Issue another write enable to start the write. */
10787 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
10788
10789 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
10790 break;
10791
10792 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080010793 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010794
Al Virob9fc7dc2007-12-17 22:59:57 -080010795 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000010796
Al Virob9fc7dc2007-12-17 22:59:57 -080010797 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010798
10799 tw32(NVRAM_ADDR, phy_addr + j);
10800
10801 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
10802 NVRAM_CMD_WR;
10803
10804 if (j == 0)
10805 nvram_cmd |= NVRAM_CMD_FIRST;
10806 else if (j == (pagesize - 4))
10807 nvram_cmd |= NVRAM_CMD_LAST;
10808
10809 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
10810 break;
10811 }
10812 if (ret)
10813 break;
10814 }
10815
10816 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
10817 tg3_nvram_exec_cmd(tp, nvram_cmd);
10818
10819 kfree(tmp);
10820
10821 return ret;
10822}
10823
10824/* offset and length are dword aligned */
10825static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
10826 u8 *buf)
10827{
10828 int i, ret = 0;
10829
10830 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080010831 u32 page_off, phy_addr, nvram_cmd;
10832 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010833
10834 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080010835 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010836
10837 page_off = offset % tp->nvram_pagesize;
10838
Michael Chan18201802006-03-20 22:29:15 -080010839 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010840
10841 tw32(NVRAM_ADDR, phy_addr);
10842
10843 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
10844
10845 if ((page_off == 0) || (i == 0))
10846 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070010847 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010848 nvram_cmd |= NVRAM_CMD_LAST;
10849
10850 if (i == (len - 4))
10851 nvram_cmd |= NVRAM_CMD_LAST;
10852
Matt Carlson321d32a2008-11-21 17:22:19 -080010853 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
10854 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070010855 (tp->nvram_jedecnum == JEDEC_ST) &&
10856 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010857
10858 if ((ret = tg3_nvram_exec_cmd(tp,
10859 NVRAM_CMD_WREN | NVRAM_CMD_GO |
10860 NVRAM_CMD_DONE)))
10861
10862 break;
10863 }
10864 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
10865 /* We always do complete word writes to eeprom. */
10866 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
10867 }
10868
10869 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
10870 break;
10871 }
10872 return ret;
10873}
10874
10875/* offset and length are dword aligned */
10876static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
10877{
10878 int ret;
10879
Linus Torvalds1da177e2005-04-16 15:20:36 -070010880 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070010881 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
10882 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010883 udelay(40);
10884 }
10885
10886 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
10887 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
10888 }
10889 else {
10890 u32 grc_mode;
10891
Michael Chanec41c7d2006-01-17 02:40:55 -080010892 ret = tg3_nvram_lock(tp);
10893 if (ret)
10894 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010895
Michael Chane6af3012005-04-21 17:12:05 -070010896 tg3_enable_nvram_access(tp);
10897 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
10898 !(tp->tg3_flags2 & TG3_FLG2_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010899 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010900
10901 grc_mode = tr32(GRC_MODE);
10902 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
10903
10904 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
10905 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
10906
10907 ret = tg3_nvram_write_block_buffered(tp, offset, len,
10908 buf);
10909 }
10910 else {
10911 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
10912 buf);
10913 }
10914
10915 grc_mode = tr32(GRC_MODE);
10916 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
10917
Michael Chane6af3012005-04-21 17:12:05 -070010918 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010919 tg3_nvram_unlock(tp);
10920 }
10921
10922 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070010923 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010924 udelay(40);
10925 }
10926
10927 return ret;
10928}
10929
10930struct subsys_tbl_ent {
10931 u16 subsys_vendor, subsys_devid;
10932 u32 phy_id;
10933};
10934
10935static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
10936 /* Broadcom boards. */
10937 { PCI_VENDOR_ID_BROADCOM, 0x1644, PHY_ID_BCM5401 }, /* BCM95700A6 */
10938 { PCI_VENDOR_ID_BROADCOM, 0x0001, PHY_ID_BCM5701 }, /* BCM95701A5 */
10939 { PCI_VENDOR_ID_BROADCOM, 0x0002, PHY_ID_BCM8002 }, /* BCM95700T6 */
10940 { PCI_VENDOR_ID_BROADCOM, 0x0003, 0 }, /* BCM95700A9 */
10941 { PCI_VENDOR_ID_BROADCOM, 0x0005, PHY_ID_BCM5701 }, /* BCM95701T1 */
10942 { PCI_VENDOR_ID_BROADCOM, 0x0006, PHY_ID_BCM5701 }, /* BCM95701T8 */
10943 { PCI_VENDOR_ID_BROADCOM, 0x0007, 0 }, /* BCM95701A7 */
10944 { PCI_VENDOR_ID_BROADCOM, 0x0008, PHY_ID_BCM5701 }, /* BCM95701A10 */
10945 { PCI_VENDOR_ID_BROADCOM, 0x8008, PHY_ID_BCM5701 }, /* BCM95701A12 */
10946 { PCI_VENDOR_ID_BROADCOM, 0x0009, PHY_ID_BCM5703 }, /* BCM95703Ax1 */
10947 { PCI_VENDOR_ID_BROADCOM, 0x8009, PHY_ID_BCM5703 }, /* BCM95703Ax2 */
10948
10949 /* 3com boards. */
10950 { PCI_VENDOR_ID_3COM, 0x1000, PHY_ID_BCM5401 }, /* 3C996T */
10951 { PCI_VENDOR_ID_3COM, 0x1006, PHY_ID_BCM5701 }, /* 3C996BT */
10952 { PCI_VENDOR_ID_3COM, 0x1004, 0 }, /* 3C996SX */
10953 { PCI_VENDOR_ID_3COM, 0x1007, PHY_ID_BCM5701 }, /* 3C1000T */
10954 { PCI_VENDOR_ID_3COM, 0x1008, PHY_ID_BCM5701 }, /* 3C940BR01 */
10955
10956 /* DELL boards. */
10957 { PCI_VENDOR_ID_DELL, 0x00d1, PHY_ID_BCM5401 }, /* VIPER */
10958 { PCI_VENDOR_ID_DELL, 0x0106, PHY_ID_BCM5401 }, /* JAGUAR */
10959 { PCI_VENDOR_ID_DELL, 0x0109, PHY_ID_BCM5411 }, /* MERLOT */
10960 { PCI_VENDOR_ID_DELL, 0x010a, PHY_ID_BCM5411 }, /* SLIM_MERLOT */
10961
10962 /* Compaq boards. */
10963 { PCI_VENDOR_ID_COMPAQ, 0x007c, PHY_ID_BCM5701 }, /* BANSHEE */
10964 { PCI_VENDOR_ID_COMPAQ, 0x009a, PHY_ID_BCM5701 }, /* BANSHEE_2 */
10965 { PCI_VENDOR_ID_COMPAQ, 0x007d, 0 }, /* CHANGELING */
10966 { PCI_VENDOR_ID_COMPAQ, 0x0085, PHY_ID_BCM5701 }, /* NC7780 */
10967 { PCI_VENDOR_ID_COMPAQ, 0x0099, PHY_ID_BCM5701 }, /* NC7780_2 */
10968
10969 /* IBM boards. */
10970 { PCI_VENDOR_ID_IBM, 0x0281, 0 } /* IBM??? */
10971};
10972
10973static inline struct subsys_tbl_ent *lookup_by_subsys(struct tg3 *tp)
10974{
10975 int i;
10976
10977 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
10978 if ((subsys_id_to_phy_id[i].subsys_vendor ==
10979 tp->pdev->subsystem_vendor) &&
10980 (subsys_id_to_phy_id[i].subsys_devid ==
10981 tp->pdev->subsystem_device))
10982 return &subsys_id_to_phy_id[i];
10983 }
10984 return NULL;
10985}
10986
Michael Chan7d0c41e2005-04-21 17:06:20 -070010987static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010988{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010989 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080010990 u16 pmcsr;
10991
10992 /* On some early chips the SRAM cannot be accessed in D3hot state,
10993 * so need make sure we're in D0.
10994 */
10995 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
10996 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
10997 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
10998 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070010999
11000 /* Make sure register accesses (indirect or otherwise)
11001 * will function correctly.
11002 */
11003 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
11004 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011005
David S. Millerf49639e2006-06-09 11:58:36 -070011006 /* The memory arbiter has to be enabled in order for SRAM accesses
11007 * to succeed. Normally on powerup the tg3 chip firmware will make
11008 * sure it is enabled, but other entities such as system netboot
11009 * code might disable it.
11010 */
11011 val = tr32(MEMARB_MODE);
11012 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
11013
Linus Torvalds1da177e2005-04-16 15:20:36 -070011014 tp->phy_id = PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070011015 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
11016
Gary Zambranoa85feb82007-05-05 11:52:19 -070011017 /* Assume an onboard device and WOL capable by default. */
11018 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080011019
Michael Chanb5d37722006-09-27 16:06:21 -070011020 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080011021 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070011022 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080011023 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
11024 }
Matt Carlson0527ba32007-10-10 18:03:30 -070011025 val = tr32(VCPU_CFGSHDW);
11026 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070011027 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070011028 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080011029 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070011030 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080011031 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070011032 }
11033
Linus Torvalds1da177e2005-04-16 15:20:36 -070011034 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
11035 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
11036 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070011037 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070011038 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011039
11040 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
11041 tp->nic_sram_data_cfg = nic_cfg;
11042
11043 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
11044 ver >>= NIC_SRAM_DATA_VER_SHIFT;
11045 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
11046 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
11047 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
11048 (ver > 0) && (ver < 0x100))
11049 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
11050
Matt Carlsona9daf362008-05-25 23:49:44 -070011051 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11052 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
11053
Linus Torvalds1da177e2005-04-16 15:20:36 -070011054 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
11055 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
11056 eeprom_phy_serdes = 1;
11057
11058 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
11059 if (nic_phy_id != 0) {
11060 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
11061 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
11062
11063 eeprom_phy_id = (id1 >> 16) << 10;
11064 eeprom_phy_id |= (id2 & 0xfc00) << 16;
11065 eeprom_phy_id |= (id2 & 0x03ff) << 0;
11066 } else
11067 eeprom_phy_id = 0;
11068
Michael Chan7d0c41e2005-04-21 17:06:20 -070011069 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070011070 if (eeprom_phy_serdes) {
Michael Chana4e2b342005-10-26 15:46:52 -070011071 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan747e8f82005-07-25 12:33:22 -070011072 tp->tg3_flags2 |= TG3_FLG2_MII_SERDES;
11073 else
11074 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
11075 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070011076
John W. Linvillecbf46852005-04-21 17:01:29 -070011077 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011078 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
11079 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070011080 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070011081 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
11082
11083 switch (led_cfg) {
11084 default:
11085 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
11086 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
11087 break;
11088
11089 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
11090 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
11091 break;
11092
11093 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
11094 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070011095
11096 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
11097 * read on some older 5700/5701 bootcode.
11098 */
11099 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
11100 ASIC_REV_5700 ||
11101 GET_ASIC_REV(tp->pci_chip_rev_id) ==
11102 ASIC_REV_5701)
11103 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
11104
Linus Torvalds1da177e2005-04-16 15:20:36 -070011105 break;
11106
11107 case SHASTA_EXT_LED_SHARED:
11108 tp->led_ctrl = LED_CTRL_MODE_SHARED;
11109 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
11110 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
11111 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
11112 LED_CTRL_MODE_PHY_2);
11113 break;
11114
11115 case SHASTA_EXT_LED_MAC:
11116 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
11117 break;
11118
11119 case SHASTA_EXT_LED_COMBO:
11120 tp->led_ctrl = LED_CTRL_MODE_COMBO;
11121 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
11122 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
11123 LED_CTRL_MODE_PHY_2);
11124 break;
11125
Stephen Hemminger855e1112008-04-16 16:37:28 -070011126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011127
11128 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
11129 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
11130 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
11131 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
11132
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011133 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
11134 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080011135
Michael Chan9d26e212006-12-07 00:21:14 -080011136 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011137 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080011138 if ((tp->pdev->subsystem_vendor ==
11139 PCI_VENDOR_ID_ARIMA) &&
11140 (tp->pdev->subsystem_device == 0x205a ||
11141 tp->pdev->subsystem_device == 0x2063))
11142 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
11143 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070011144 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080011145 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
11146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011147
11148 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
11149 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070011150 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011151 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
11152 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080011153
11154 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
11155 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070011156 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080011157
Gary Zambranoa85feb82007-05-05 11:52:19 -070011158 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES &&
11159 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
11160 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011161
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070011162 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080011163 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070011164 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
11165
Linus Torvalds1da177e2005-04-16 15:20:36 -070011166 if (cfg2 & (1 << 17))
11167 tp->tg3_flags2 |= TG3_FLG2_CAPACITIVE_COUPLING;
11168
11169 /* serdes signal pre-emphasis in register 0x590 set by */
11170 /* bootcode if bit 18 is set */
11171 if (cfg2 & (1 << 18))
11172 tp->tg3_flags2 |= TG3_FLG2_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070011173
Matt Carlson321d32a2008-11-21 17:22:19 -080011174 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
11175 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080011176 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
11177 tp->tg3_flags3 |= TG3_FLG3_PHY_ENABLE_APD;
11178
Matt Carlson8ed5d972007-05-07 00:25:49 -070011179 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
11180 u32 cfg3;
11181
11182 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
11183 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
11184 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
11185 }
Matt Carlsona9daf362008-05-25 23:49:44 -070011186
11187 if (cfg4 & NIC_SRAM_RGMII_STD_IBND_DISABLE)
11188 tp->tg3_flags3 |= TG3_FLG3_RGMII_STD_IBND_DISABLE;
11189 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
11190 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
11191 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
11192 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011193 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080011194done:
11195 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
11196 device_set_wakeup_enable(&tp->pdev->dev,
11197 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Michael Chan7d0c41e2005-04-21 17:06:20 -070011198}
11199
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011200static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
11201{
11202 int i;
11203 u32 val;
11204
11205 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
11206 tw32(OTP_CTRL, cmd);
11207
11208 /* Wait for up to 1 ms for command to execute. */
11209 for (i = 0; i < 100; i++) {
11210 val = tr32(OTP_STATUS);
11211 if (val & OTP_STATUS_CMD_DONE)
11212 break;
11213 udelay(10);
11214 }
11215
11216 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
11217}
11218
11219/* Read the gphy configuration from the OTP region of the chip. The gphy
11220 * configuration is a 32-bit value that straddles the alignment boundary.
11221 * We do two 32-bit reads and then shift and merge the results.
11222 */
11223static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
11224{
11225 u32 bhalf_otp, thalf_otp;
11226
11227 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
11228
11229 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
11230 return 0;
11231
11232 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
11233
11234 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
11235 return 0;
11236
11237 thalf_otp = tr32(OTP_READ_DATA);
11238
11239 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
11240
11241 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
11242 return 0;
11243
11244 bhalf_otp = tr32(OTP_READ_DATA);
11245
11246 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
11247}
11248
Michael Chan7d0c41e2005-04-21 17:06:20 -070011249static int __devinit tg3_phy_probe(struct tg3 *tp)
11250{
11251 u32 hw_phy_id_1, hw_phy_id_2;
11252 u32 hw_phy_id, hw_phy_id_masked;
11253 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011254
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011255 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
11256 return tg3_phy_init(tp);
11257
Linus Torvalds1da177e2005-04-16 15:20:36 -070011258 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010011259 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011260 */
11261 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070011262 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
11263 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011264 hw_phy_id = hw_phy_id_masked = PHY_ID_INVALID;
11265 } else {
11266 /* Now read the physical PHY_ID from the chip and verify
11267 * that it is sane. If it doesn't look good, we fall back
11268 * to either the hard-coded table based PHY_ID and failing
11269 * that the value found in the eeprom area.
11270 */
11271 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
11272 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
11273
11274 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
11275 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
11276 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
11277
11278 hw_phy_id_masked = hw_phy_id & PHY_ID_MASK;
11279 }
11280
11281 if (!err && KNOWN_PHY_ID(hw_phy_id_masked)) {
11282 tp->phy_id = hw_phy_id;
11283 if (hw_phy_id_masked == PHY_ID_BCM8002)
11284 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070011285 else
11286 tp->tg3_flags2 &= ~TG3_FLG2_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011287 } else {
Michael Chan7d0c41e2005-04-21 17:06:20 -070011288 if (tp->phy_id != PHY_ID_INVALID) {
11289 /* Do nothing, phy ID already set up in
11290 * tg3_get_eeprom_hw_cfg().
11291 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011292 } else {
11293 struct subsys_tbl_ent *p;
11294
11295 /* No eeprom signature? Try the hardcoded
11296 * subsys device table.
11297 */
11298 p = lookup_by_subsys(tp);
11299 if (!p)
11300 return -ENODEV;
11301
11302 tp->phy_id = p->phy_id;
11303 if (!tp->phy_id ||
11304 tp->phy_id == PHY_ID_BCM8002)
11305 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
11306 }
11307 }
11308
Michael Chan747e8f82005-07-25 12:33:22 -070011309 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070011310 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011311 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080011312 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011313
11314 tg3_readphy(tp, MII_BMSR, &bmsr);
11315 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
11316 (bmsr & BMSR_LSTATUS))
11317 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011318
Linus Torvalds1da177e2005-04-16 15:20:36 -070011319 err = tg3_phy_reset(tp);
11320 if (err)
11321 return err;
11322
11323 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
11324 ADVERTISE_100HALF | ADVERTISE_100FULL |
11325 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
11326 tg3_ctrl = 0;
11327 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
11328 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
11329 MII_TG3_CTRL_ADV_1000_FULL);
11330 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
11331 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
11332 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
11333 MII_TG3_CTRL_ENABLE_AS_MASTER);
11334 }
11335
Michael Chan3600d912006-12-07 00:21:48 -080011336 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
11337 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
11338 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
11339 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011340 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
11341
11342 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
11343 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
11344
11345 tg3_writephy(tp, MII_BMCR,
11346 BMCR_ANENABLE | BMCR_ANRESTART);
11347 }
11348 tg3_phy_set_wirespeed(tp);
11349
11350 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
11351 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
11352 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
11353 }
11354
11355skip_phy_reset:
11356 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
11357 err = tg3_init_5401phy_dsp(tp);
11358 if (err)
11359 return err;
11360 }
11361
11362 if (!err && ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)) {
11363 err = tg3_init_5401phy_dsp(tp);
11364 }
11365
Michael Chan747e8f82005-07-25 12:33:22 -070011366 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011367 tp->link_config.advertising =
11368 (ADVERTISED_1000baseT_Half |
11369 ADVERTISED_1000baseT_Full |
11370 ADVERTISED_Autoneg |
11371 ADVERTISED_FIBRE);
11372 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
11373 tp->link_config.advertising &=
11374 ~(ADVERTISED_1000baseT_Half |
11375 ADVERTISED_1000baseT_Full);
11376
11377 return err;
11378}
11379
11380static void __devinit tg3_read_partno(struct tg3 *tp)
11381{
Matt Carlson6d348f22009-02-25 14:25:52 +000011382 unsigned char vpd_data[256]; /* in little-endian format */
Michael Chanaf2c6a42006-11-07 14:57:51 -080011383 unsigned int i;
Michael Chan1b277772006-03-20 22:27:48 -080011384 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011385
Matt Carlsondf259d82009-04-20 06:57:14 +000011386 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11387 tg3_nvram_read(tp, 0x0, &magic))
David S. Millerf49639e2006-06-09 11:58:36 -070011388 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011389
Michael Chan18201802006-03-20 22:29:15 -080011390 if (magic == TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011391 for (i = 0; i < 256; i += 4) {
11392 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011393
Matt Carlson6d348f22009-02-25 14:25:52 +000011394 /* The data is in little-endian format in NVRAM.
11395 * Use the big-endian read routines to preserve
11396 * the byte order as it exists in NVRAM.
11397 */
11398 if (tg3_nvram_read_be32(tp, 0x100 + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080011399 goto out_not_found;
11400
Matt Carlson6d348f22009-02-25 14:25:52 +000011401 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080011402 }
11403 } else {
11404 int vpd_cap;
11405
11406 vpd_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_VPD);
11407 for (i = 0; i < 256; i += 4) {
11408 u32 tmp, j = 0;
Al Virob9fc7dc2007-12-17 22:59:57 -080011409 __le32 v;
Michael Chan1b277772006-03-20 22:27:48 -080011410 u16 tmp16;
11411
11412 pci_write_config_word(tp->pdev, vpd_cap + PCI_VPD_ADDR,
11413 i);
11414 while (j++ < 100) {
11415 pci_read_config_word(tp->pdev, vpd_cap +
11416 PCI_VPD_ADDR, &tmp16);
11417 if (tmp16 & 0x8000)
11418 break;
11419 msleep(1);
11420 }
David S. Millerf49639e2006-06-09 11:58:36 -070011421 if (!(tmp16 & 0x8000))
11422 goto out_not_found;
11423
Michael Chan1b277772006-03-20 22:27:48 -080011424 pci_read_config_dword(tp->pdev, vpd_cap + PCI_VPD_DATA,
11425 &tmp);
Al Virob9fc7dc2007-12-17 22:59:57 -080011426 v = cpu_to_le32(tmp);
Matt Carlson6d348f22009-02-25 14:25:52 +000011427 memcpy(&vpd_data[i], &v, sizeof(v));
Michael Chan1b277772006-03-20 22:27:48 -080011428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011429 }
11430
11431 /* Now parse and find the part number. */
Michael Chanaf2c6a42006-11-07 14:57:51 -080011432 for (i = 0; i < 254; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011433 unsigned char val = vpd_data[i];
Michael Chanaf2c6a42006-11-07 14:57:51 -080011434 unsigned int block_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011435
11436 if (val == 0x82 || val == 0x91) {
11437 i = (i + 3 +
11438 (vpd_data[i + 1] +
11439 (vpd_data[i + 2] << 8)));
11440 continue;
11441 }
11442
11443 if (val != 0x90)
11444 goto out_not_found;
11445
11446 block_end = (i + 3 +
11447 (vpd_data[i + 1] +
11448 (vpd_data[i + 2] << 8)));
11449 i += 3;
Michael Chanaf2c6a42006-11-07 14:57:51 -080011450
11451 if (block_end > 256)
11452 goto out_not_found;
11453
11454 while (i < (block_end - 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011455 if (vpd_data[i + 0] == 'P' &&
11456 vpd_data[i + 1] == 'N') {
11457 int partno_len = vpd_data[i + 2];
11458
Michael Chanaf2c6a42006-11-07 14:57:51 -080011459 i += 3;
11460 if (partno_len > 24 || (partno_len + i) > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011461 goto out_not_found;
11462
11463 memcpy(tp->board_part_number,
Michael Chanaf2c6a42006-11-07 14:57:51 -080011464 &vpd_data[i], partno_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011465
11466 /* Success. */
11467 return;
11468 }
Michael Chanaf2c6a42006-11-07 14:57:51 -080011469 i += 3 + vpd_data[i + 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070011470 }
11471
11472 /* Part number not found. */
11473 goto out_not_found;
11474 }
11475
11476out_not_found:
Michael Chanb5d37722006-09-27 16:06:21 -070011477 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11478 strcpy(tp->board_part_number, "BCM95906");
Matt Carlsondf259d82009-04-20 06:57:14 +000011479 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
11480 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
11481 strcpy(tp->board_part_number, "BCM57780");
11482 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
11483 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
11484 strcpy(tp->board_part_number, "BCM57760");
11485 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
11486 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
11487 strcpy(tp->board_part_number, "BCM57790");
Michael Chanb5d37722006-09-27 16:06:21 -070011488 else
11489 strcpy(tp->board_part_number, "none");
Linus Torvalds1da177e2005-04-16 15:20:36 -070011490}
11491
Matt Carlson9c8a6202007-10-21 16:16:08 -070011492static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
11493{
11494 u32 val;
11495
Matt Carlsone4f34112009-02-25 14:25:00 +000011496 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070011497 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000011498 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070011499 val != 0)
11500 return 0;
11501
11502 return 1;
11503}
11504
Matt Carlsonacd9c112009-02-25 14:26:33 +000011505static void __devinit tg3_read_bc_ver(struct tg3 *tp)
11506{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000011507 u32 val, offset, start, ver_offset;
Matt Carlsonacd9c112009-02-25 14:26:33 +000011508 int i;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000011509 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000011510
11511 if (tg3_nvram_read(tp, 0xc, &offset) ||
11512 tg3_nvram_read(tp, 0x4, &start))
11513 return;
11514
11515 offset = tg3_nvram_logical_addr(tp, offset);
11516
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000011517 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000011518 return;
11519
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000011520 if ((val & 0xfc000000) == 0x0c000000) {
11521 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000011522 return;
11523
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000011524 if (val == 0)
11525 newver = true;
11526 }
11527
11528 if (newver) {
11529 if (tg3_nvram_read(tp, offset + 8, &ver_offset))
11530 return;
11531
11532 offset = offset + ver_offset - start;
11533 for (i = 0; i < 16; i += 4) {
11534 __be32 v;
11535 if (tg3_nvram_read_be32(tp, offset + i, &v))
11536 return;
11537
11538 memcpy(tp->fw_ver + i, &v, sizeof(v));
11539 }
11540 } else {
11541 u32 major, minor;
11542
11543 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
11544 return;
11545
11546 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
11547 TG3_NVM_BCVER_MAJSFT;
11548 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
11549 snprintf(&tp->fw_ver[0], 32, "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000011550 }
11551}
11552
Matt Carlsona6f6cb12009-02-25 14:27:43 +000011553static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
11554{
11555 u32 val, major, minor;
11556
11557 /* Use native endian representation */
11558 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
11559 return;
11560
11561 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
11562 TG3_NVM_HWSB_CFG1_MAJSFT;
11563 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
11564 TG3_NVM_HWSB_CFG1_MINSFT;
11565
11566 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
11567}
11568
Matt Carlsondfe00d72008-11-21 17:19:41 -080011569static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
11570{
11571 u32 offset, major, minor, build;
11572
11573 tp->fw_ver[0] = 's';
11574 tp->fw_ver[1] = 'b';
11575 tp->fw_ver[2] = '\0';
11576
11577 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
11578 return;
11579
11580 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
11581 case TG3_EEPROM_SB_REVISION_0:
11582 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
11583 break;
11584 case TG3_EEPROM_SB_REVISION_2:
11585 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
11586 break;
11587 case TG3_EEPROM_SB_REVISION_3:
11588 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
11589 break;
11590 default:
11591 return;
11592 }
11593
Matt Carlsone4f34112009-02-25 14:25:00 +000011594 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080011595 return;
11596
11597 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
11598 TG3_EEPROM_SB_EDH_BLD_SHFT;
11599 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
11600 TG3_EEPROM_SB_EDH_MAJ_SHFT;
11601 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
11602
11603 if (minor > 99 || build > 26)
11604 return;
11605
11606 snprintf(&tp->fw_ver[2], 30, " v%d.%02d", major, minor);
11607
11608 if (build > 0) {
11609 tp->fw_ver[8] = 'a' + build - 1;
11610 tp->fw_ver[9] = '\0';
11611 }
11612}
11613
Matt Carlsonacd9c112009-02-25 14:26:33 +000011614static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080011615{
11616 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000011617 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070011618
11619 for (offset = TG3_NVM_DIR_START;
11620 offset < TG3_NVM_DIR_END;
11621 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011622 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070011623 return;
11624
11625 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
11626 break;
11627 }
11628
11629 if (offset == TG3_NVM_DIR_END)
11630 return;
11631
11632 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
11633 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000011634 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070011635 return;
11636
Matt Carlsone4f34112009-02-25 14:25:00 +000011637 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070011638 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000011639 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070011640 return;
11641
11642 offset += val - start;
11643
Matt Carlsonacd9c112009-02-25 14:26:33 +000011644 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070011645
Matt Carlsonacd9c112009-02-25 14:26:33 +000011646 tp->fw_ver[vlen++] = ',';
11647 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070011648
11649 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011650 __be32 v;
11651 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070011652 return;
11653
Al Virob9fc7dc2007-12-17 22:59:57 -080011654 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070011655
Matt Carlsonacd9c112009-02-25 14:26:33 +000011656 if (vlen > TG3_VER_SIZE - sizeof(v)) {
11657 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070011658 break;
11659 }
11660
Matt Carlsonacd9c112009-02-25 14:26:33 +000011661 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
11662 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070011663 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000011664}
11665
Matt Carlson7fd76442009-02-25 14:27:20 +000011666static void __devinit tg3_read_dash_ver(struct tg3 *tp)
11667{
11668 int vlen;
11669 u32 apedata;
11670
11671 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
11672 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
11673 return;
11674
11675 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
11676 if (apedata != APE_SEG_SIG_MAGIC)
11677 return;
11678
11679 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
11680 if (!(apedata & APE_FW_STATUS_READY))
11681 return;
11682
11683 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
11684
11685 vlen = strlen(tp->fw_ver);
11686
11687 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " DASH v%d.%d.%d.%d",
11688 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
11689 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
11690 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
11691 (apedata & APE_FW_VERSION_BLDMSK));
11692}
11693
Matt Carlsonacd9c112009-02-25 14:26:33 +000011694static void __devinit tg3_read_fw_ver(struct tg3 *tp)
11695{
11696 u32 val;
11697
Matt Carlsondf259d82009-04-20 06:57:14 +000011698 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
11699 tp->fw_ver[0] = 's';
11700 tp->fw_ver[1] = 'b';
11701 tp->fw_ver[2] = '\0';
11702
11703 return;
11704 }
11705
Matt Carlsonacd9c112009-02-25 14:26:33 +000011706 if (tg3_nvram_read(tp, 0, &val))
11707 return;
11708
11709 if (val == TG3_EEPROM_MAGIC)
11710 tg3_read_bc_ver(tp);
11711 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
11712 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000011713 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
11714 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000011715 else
11716 return;
11717
11718 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
11719 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
11720 return;
11721
11722 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070011723
11724 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080011725}
11726
Michael Chan7544b092007-05-05 13:08:32 -070011727static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
11728
Linus Torvalds1da177e2005-04-16 15:20:36 -070011729static int __devinit tg3_get_invariants(struct tg3 *tp)
11730{
11731 static struct pci_device_id write_reorder_chipsets[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011732 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
11733 PCI_DEVICE_ID_AMD_FE_GATE_700C) },
John W. Linvillec165b002006-07-08 13:28:53 -070011734 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
11735 PCI_DEVICE_ID_AMD_8131_BRIDGE) },
Michael Chan399de502005-10-03 14:02:39 -070011736 { PCI_DEVICE(PCI_VENDOR_ID_VIA,
11737 PCI_DEVICE_ID_VIA_8385_0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011738 { },
11739 };
11740 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011741 u32 pci_state_reg, grc_misc_cfg;
11742 u32 val;
11743 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080011744 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011745
Linus Torvalds1da177e2005-04-16 15:20:36 -070011746 /* Force memory write invalidate off. If we leave it on,
11747 * then on 5700_BX chips we have to enable a workaround.
11748 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
11749 * to match the cacheline size. The Broadcom driver have this
11750 * workaround but turns MWI off all the times so never uses
11751 * it. This seems to suggest that the workaround is insufficient.
11752 */
11753 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
11754 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
11755 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
11756
11757 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
11758 * has the register indirect write enable bit set before
11759 * we try to access any of the MMIO registers. It is also
11760 * critical that the PCI-X hw workaround situation is decided
11761 * before that as well.
11762 */
11763 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
11764 &misc_ctrl_reg);
11765
11766 tp->pci_chip_rev_id = (misc_ctrl_reg >>
11767 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070011768 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
11769 u32 prod_id_asic_rev;
11770
11771 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
11772 &prod_id_asic_rev);
Matt Carlson321d32a2008-11-21 17:22:19 -080011773 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070011774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011775
Michael Chanff645be2005-04-21 17:09:53 -070011776 /* Wrong chip ID in 5752 A0. This code can be removed later
11777 * as A0 is not in production.
11778 */
11779 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
11780 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
11781
Michael Chan68929142005-08-09 20:17:14 -070011782 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
11783 * we need to disable memory and use config. cycles
11784 * only to access all registers. The 5702/03 chips
11785 * can mistakenly decode the special cycles from the
11786 * ICH chipsets as memory write cycles, causing corruption
11787 * of register and memory space. Only certain ICH bridges
11788 * will drive special cycles with non-zero data during the
11789 * address phase which can fall within the 5703's address
11790 * range. This is not an ICH bug as the PCI spec allows
11791 * non-zero address during special cycles. However, only
11792 * these ICH bridges are known to drive non-zero addresses
11793 * during special cycles.
11794 *
11795 * Since special cycles do not cross PCI bridges, we only
11796 * enable this workaround if the 5703 is on the secondary
11797 * bus of these ICH bridges.
11798 */
11799 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
11800 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
11801 static struct tg3_dev_id {
11802 u32 vendor;
11803 u32 device;
11804 u32 rev;
11805 } ich_chipsets[] = {
11806 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
11807 PCI_ANY_ID },
11808 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
11809 PCI_ANY_ID },
11810 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
11811 0xa },
11812 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
11813 PCI_ANY_ID },
11814 { },
11815 };
11816 struct tg3_dev_id *pci_id = &ich_chipsets[0];
11817 struct pci_dev *bridge = NULL;
11818
11819 while (pci_id->vendor != 0) {
11820 bridge = pci_get_device(pci_id->vendor, pci_id->device,
11821 bridge);
11822 if (!bridge) {
11823 pci_id++;
11824 continue;
11825 }
11826 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070011827 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070011828 continue;
11829 }
11830 if (bridge->subordinate &&
11831 (bridge->subordinate->number ==
11832 tp->pdev->bus->number)) {
11833
11834 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
11835 pci_dev_put(bridge);
11836 break;
11837 }
11838 }
11839 }
11840
Matt Carlson41588ba2008-04-19 18:12:33 -070011841 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
11842 static struct tg3_dev_id {
11843 u32 vendor;
11844 u32 device;
11845 } bridge_chipsets[] = {
11846 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
11847 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
11848 { },
11849 };
11850 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
11851 struct pci_dev *bridge = NULL;
11852
11853 while (pci_id->vendor != 0) {
11854 bridge = pci_get_device(pci_id->vendor,
11855 pci_id->device,
11856 bridge);
11857 if (!bridge) {
11858 pci_id++;
11859 continue;
11860 }
11861 if (bridge->subordinate &&
11862 (bridge->subordinate->number <=
11863 tp->pdev->bus->number) &&
11864 (bridge->subordinate->subordinate >=
11865 tp->pdev->bus->number)) {
11866 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
11867 pci_dev_put(bridge);
11868 break;
11869 }
11870 }
11871 }
11872
Michael Chan4a29cc22006-03-19 13:21:12 -080011873 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
11874 * DMA addresses > 40-bit. This bridge may have other additional
11875 * 57xx devices behind it in some 4-port NIC designs for example.
11876 * Any tg3 device found behind the bridge will also need the 40-bit
11877 * DMA workaround.
11878 */
Michael Chana4e2b342005-10-26 15:46:52 -070011879 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
11880 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
11881 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080011882 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070011883 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Michael Chana4e2b342005-10-26 15:46:52 -070011884 }
Michael Chan4a29cc22006-03-19 13:21:12 -080011885 else {
11886 struct pci_dev *bridge = NULL;
11887
11888 do {
11889 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
11890 PCI_DEVICE_ID_SERVERWORKS_EPB,
11891 bridge);
11892 if (bridge && bridge->subordinate &&
11893 (bridge->subordinate->number <=
11894 tp->pdev->bus->number) &&
11895 (bridge->subordinate->subordinate >=
11896 tp->pdev->bus->number)) {
11897 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
11898 pci_dev_put(bridge);
11899 break;
11900 }
11901 } while (bridge);
11902 }
Michael Chan4cf78e42005-07-25 12:29:19 -070011903
Linus Torvalds1da177e2005-04-16 15:20:36 -070011904 /* Initialize misc host control in PCI block. */
11905 tp->misc_host_ctrl |= (misc_ctrl_reg &
11906 MISC_HOST_CTRL_CHIPREV);
11907 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
11908 tp->misc_host_ctrl);
11909
Michael Chan7544b092007-05-05 13:08:32 -070011910 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
11911 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714))
11912 tp->pdev_peer = tg3_find_peer(tp);
11913
Matt Carlson321d32a2008-11-21 17:22:19 -080011914 /* Intentionally exclude ASIC_REV_5906 */
11915 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad12006-03-20 22:27:35 -080011916 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070011917 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070011918 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011919 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080011920 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
11921 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
11922
11923 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
11924 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070011925 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080011926 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011927 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070011928 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
11929
John W. Linville1b440c562005-04-21 17:03:18 -070011930 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
11931 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
11932 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
11933
Matt Carlson027455a2008-12-21 20:19:30 -080011934 /* 5700 B0 chips do not support checksumming correctly due
11935 * to hardware bugs.
11936 */
11937 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
11938 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
11939 else {
11940 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
11941 tp->dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
11942 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
11943 tp->dev->features |= NETIF_F_IPV6_CSUM;
11944 }
11945
Michael Chan5a6f3072006-03-20 22:28:05 -080011946 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070011947 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
11948 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
11949 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
11950 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
11951 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
11952 tp->pdev_peer == tp->pdev))
11953 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
11954
Matt Carlson321d32a2008-11-21 17:22:19 -080011955 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070011956 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan5a6f3072006-03-20 22:28:05 -080011957 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011958 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070011959 } else {
Michael Chan7f62ad52007-02-20 23:25:40 -080011960 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
Michael Chan52c0fd82006-06-29 20:15:54 -070011961 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
11962 ASIC_REV_5750 &&
11963 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Michael Chan7f62ad52007-02-20 23:25:40 -080011964 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
Michael Chan52c0fd82006-06-29 20:15:54 -070011965 }
Michael Chan5a6f3072006-03-20 22:28:05 -080011966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011967
Matt Carlsonf51f3562008-05-25 23:45:08 -070011968 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
11969 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070011970 tp->tg3_flags2 |= TG3_FLG2_JUMBO_CAPABLE;
11971
Matt Carlson52f44902008-11-21 17:17:04 -080011972 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
11973 &pci_state_reg);
11974
Matt Carlson5e7dfd02008-11-21 17:18:16 -080011975 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
11976 if (tp->pcie_cap != 0) {
11977 u16 lnkctl;
11978
Linus Torvalds1da177e2005-04-16 15:20:36 -070011979 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080011980
11981 pcie_set_readrq(tp->pdev, 4096);
11982
Matt Carlson5e7dfd02008-11-21 17:18:16 -080011983 pci_read_config_word(tp->pdev,
11984 tp->pcie_cap + PCI_EXP_LNKCTL,
11985 &lnkctl);
11986 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
11987 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080011988 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080011989 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080011990 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000011991 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
11992 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080011993 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Michael Chanc7835a72006-11-15 21:14:42 -080011994 }
Matt Carlson52f44902008-11-21 17:17:04 -080011995 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080011996 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080011997 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
11998 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
11999 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
12000 if (!tp->pcix_cap) {
12001 printk(KERN_ERR PFX "Cannot find PCI-X "
12002 "capability, aborting.\n");
12003 return -EIO;
12004 }
12005
12006 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
12007 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
12008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012009
Michael Chan399de502005-10-03 14:02:39 -070012010 /* If we have an AMD 762 or VIA K8T800 chipset, write
12011 * reordering to the mailbox registers done by the host
12012 * controller can cause major troubles. We read back from
12013 * every mailbox register write to force the writes to be
12014 * posted to the chip in order.
12015 */
12016 if (pci_dev_present(write_reorder_chipsets) &&
12017 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
12018 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
12019
Matt Carlson69fc4052008-12-21 20:19:57 -080012020 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
12021 &tp->pci_cacheline_sz);
12022 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
12023 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012024 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
12025 tp->pci_lat_timer < 64) {
12026 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080012027 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
12028 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012029 }
12030
Matt Carlson52f44902008-11-21 17:17:04 -080012031 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
12032 /* 5700 BX chips need to have their TX producer index
12033 * mailboxes written twice to workaround a bug.
12034 */
12035 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070012036
Matt Carlson52f44902008-11-21 17:17:04 -080012037 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012038 *
12039 * The workaround is to use indirect register accesses
12040 * for all chip writes not to mailbox registers.
12041 */
Matt Carlson52f44902008-11-21 17:17:04 -080012042 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012043 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012044
12045 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
12046
12047 /* The chip can have it's power management PCI config
12048 * space registers clobbered due to this bug.
12049 * So explicitly force the chip into D0 here.
12050 */
Matt Carlson9974a352007-10-07 23:27:28 -070012051 pci_read_config_dword(tp->pdev,
12052 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012053 &pm_reg);
12054 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
12055 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070012056 pci_write_config_dword(tp->pdev,
12057 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012058 pm_reg);
12059
12060 /* Also, force SERR#/PERR# in PCI command. */
12061 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12062 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
12063 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12064 }
12065 }
12066
Linus Torvalds1da177e2005-04-16 15:20:36 -070012067 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
12068 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
12069 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
12070 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
12071
12072 /* Chip-specific fixup from Broadcom driver */
12073 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
12074 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
12075 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
12076 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
12077 }
12078
Michael Chan1ee582d2005-08-09 20:16:46 -070012079 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070012080 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070012081 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070012082 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070012083 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070012084 tp->write32_tx_mbox = tg3_write32;
12085 tp->write32_rx_mbox = tg3_write32;
12086
12087 /* Various workaround register access methods */
12088 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
12089 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070012090 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
12091 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
12092 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
12093 /*
12094 * Back to back register writes can cause problems on these
12095 * chips, the workaround is to read back all reg writes
12096 * except those to mailbox regs.
12097 *
12098 * See tg3_write_indirect_reg32().
12099 */
Michael Chan1ee582d2005-08-09 20:16:46 -070012100 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070012101 }
12102
Michael Chan1ee582d2005-08-09 20:16:46 -070012103
12104 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
12105 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
12106 tp->write32_tx_mbox = tg3_write32_tx_mbox;
12107 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
12108 tp->write32_rx_mbox = tg3_write_flush_reg32;
12109 }
Michael Chan20094932005-08-09 20:16:32 -070012110
Michael Chan68929142005-08-09 20:17:14 -070012111 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
12112 tp->read32 = tg3_read_indirect_reg32;
12113 tp->write32 = tg3_write_indirect_reg32;
12114 tp->read32_mbox = tg3_read_indirect_mbox;
12115 tp->write32_mbox = tg3_write_indirect_mbox;
12116 tp->write32_tx_mbox = tg3_write_indirect_mbox;
12117 tp->write32_rx_mbox = tg3_write_indirect_mbox;
12118
12119 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070012120 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070012121
12122 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12123 pci_cmd &= ~PCI_COMMAND_MEMORY;
12124 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12125 }
Michael Chanb5d37722006-09-27 16:06:21 -070012126 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
12127 tp->read32_mbox = tg3_read32_mbox_5906;
12128 tp->write32_mbox = tg3_write32_mbox_5906;
12129 tp->write32_tx_mbox = tg3_write32_mbox_5906;
12130 tp->write32_rx_mbox = tg3_write32_mbox_5906;
12131 }
Michael Chan68929142005-08-09 20:17:14 -070012132
Michael Chanbbadf502006-04-06 21:46:34 -070012133 if (tp->write32 == tg3_write_indirect_reg32 ||
12134 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
12135 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070012136 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070012137 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
12138
Michael Chan7d0c41e2005-04-21 17:06:20 -070012139 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080012140 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070012141 * determined before calling tg3_set_power_state() so that
12142 * we know whether or not to switch out of Vaux power.
12143 * When the flag is set, it means that GPIO1 is used for eeprom
12144 * write protect and also implies that it is a LOM where GPIOs
12145 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012146 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070012147 tg3_get_eeprom_hw_cfg(tp);
12148
Matt Carlson0d3031d2007-10-10 18:02:43 -070012149 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
12150 /* Allow reads and writes to the
12151 * APE register and memory space.
12152 */
12153 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
12154 PCISTATE_ALLOW_APE_SHMEM_WR;
12155 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
12156 pci_state_reg);
12157 }
12158
Matt Carlson9936bcf2007-10-10 18:03:07 -070012159 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012160 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012161 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
12162 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -070012163 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
12164
Michael Chan314fba32005-04-21 17:07:04 -070012165 /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
12166 * GPIO1 driven high will bring 5700's external PHY out of reset.
12167 * It is also used as eeprom write protect on LOMs.
12168 */
12169 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
12170 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
12171 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
12172 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
12173 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070012174 /* Unused GPIO3 must be driven as output on 5752 because there
12175 * are no pull-up resistors on unused GPIO pins.
12176 */
12177 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12178 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070012179
Matt Carlson321d32a2008-11-21 17:22:19 -080012180 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
12181 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Michael Chanaf36e6b2006-03-23 01:28:06 -080012182 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
12183
Matt Carlson8d519ab2009-04-20 06:58:01 +000012184 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
12185 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070012186 /* Turn off the debug UART. */
12187 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
12188 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
12189 /* Keep VMain power. */
12190 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
12191 GRC_LCLCTRL_GPIO_OUTPUT0;
12192 }
12193
Linus Torvalds1da177e2005-04-16 15:20:36 -070012194 /* Force the chip into D0. */
Michael Chanbc1c7562006-03-20 17:48:03 -080012195 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012196 if (err) {
12197 printk(KERN_ERR PFX "(%s) transition to D0 failed\n",
12198 pci_name(tp->pdev));
12199 return err;
12200 }
12201
Linus Torvalds1da177e2005-04-16 15:20:36 -070012202 /* Derive initial jumbo mode from MTU assigned in
12203 * ether_setup() via the alloc_etherdev() call
12204 */
Michael Chan0f893dc2005-07-25 12:30:38 -070012205 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070012206 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070012207 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012208
12209 /* Determine WakeOnLan speed to use. */
12210 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12211 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12212 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
12213 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
12214 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
12215 } else {
12216 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
12217 }
12218
12219 /* A few boards don't want Ethernet@WireSpeed phy feature */
12220 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
12221 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
12222 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070012223 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Michael Chanb5d37722006-09-27 16:06:21 -070012224 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) ||
Michael Chan747e8f82005-07-25 12:33:22 -070012225 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012226 tp->tg3_flags2 |= TG3_FLG2_NO_ETH_WIRE_SPEED;
12227
12228 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
12229 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
12230 tp->tg3_flags2 |= TG3_FLG2_PHY_ADC_BUG;
12231 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
12232 tp->tg3_flags2 |= TG3_FLG2_PHY_5704_A0_BUG;
12233
Matt Carlson321d32a2008-11-21 17:22:19 -080012234 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
12235 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5906 &&
12236 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
12237 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780) {
Michael Chanc424cb22006-04-29 18:56:34 -070012238 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070012239 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070012240 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12241 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080012242 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
12243 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
12244 tp->tg3_flags2 |= TG3_FLG2_PHY_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080012245 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
12246 tp->tg3_flags2 |= TG3_FLG2_PHY_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080012247 } else
Michael Chanc424cb22006-04-29 18:56:34 -070012248 tp->tg3_flags2 |= TG3_FLG2_PHY_BER_BUG;
12249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012250
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012251 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12252 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
12253 tp->phy_otp = tg3_read_otp_phycfg(tp);
12254 if (tp->phy_otp == 0)
12255 tp->phy_otp = TG3_OTP_DEFAULT;
12256 }
12257
Matt Carlsonf51f3562008-05-25 23:45:08 -070012258 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070012259 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
12260 else
12261 tp->mi_mode = MAC_MI_MODE_BASE;
12262
Linus Torvalds1da177e2005-04-16 15:20:36 -070012263 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012264 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
12265 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
12266 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
12267
Matt Carlson321d32a2008-11-21 17:22:19 -080012268 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
12269 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070012270 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
12271
Matt Carlson158d7ab2008-05-29 01:37:54 -070012272 err = tg3_mdio_init(tp);
12273 if (err)
12274 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012275
12276 /* Initialize data/descriptor byte/word swapping. */
12277 val = tr32(GRC_MODE);
12278 val &= GRC_MODE_HOST_STACKUP;
12279 tw32(GRC_MODE, val | tp->grc_mode);
12280
12281 tg3_switch_clocks(tp);
12282
12283 /* Clear this out for sanity. */
12284 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
12285
12286 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
12287 &pci_state_reg);
12288 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
12289 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
12290 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
12291
12292 if (chiprevid == CHIPREV_ID_5701_A0 ||
12293 chiprevid == CHIPREV_ID_5701_B0 ||
12294 chiprevid == CHIPREV_ID_5701_B2 ||
12295 chiprevid == CHIPREV_ID_5701_B5) {
12296 void __iomem *sram_base;
12297
12298 /* Write some dummy words into the SRAM status block
12299 * area, see if it reads back correctly. If the return
12300 * value is bad, force enable the PCIX workaround.
12301 */
12302 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
12303
12304 writel(0x00000000, sram_base);
12305 writel(0x00000000, sram_base + 4);
12306 writel(0xffffffff, sram_base + 4);
12307 if (readl(sram_base) != 0x00000000)
12308 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
12309 }
12310 }
12311
12312 udelay(50);
12313 tg3_nvram_init(tp);
12314
12315 grc_misc_cfg = tr32(GRC_MISC_CFG);
12316 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
12317
Linus Torvalds1da177e2005-04-16 15:20:36 -070012318 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
12319 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
12320 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
12321 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
12322
David S. Millerfac9b832005-05-18 22:46:34 -070012323 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
12324 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
12325 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
12326 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
12327 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
12328 HOSTCC_MODE_CLRTICK_TXBD);
12329
12330 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
12331 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12332 tp->misc_host_ctrl);
12333 }
12334
Matt Carlson3bda1252008-08-15 14:08:22 -070012335 /* Preserve the APE MAC_MODE bits */
12336 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
12337 tp->mac_mode = tr32(MAC_MODE) |
12338 MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
12339 else
12340 tp->mac_mode = TG3_DEF_MAC_MODE;
12341
Linus Torvalds1da177e2005-04-16 15:20:36 -070012342 /* these are limited to 10/100 only */
12343 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
12344 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
12345 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
12346 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
12347 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
12348 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
12349 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
12350 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
12351 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080012352 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
12353 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012354 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Michael Chanb5d37722006-09-27 16:06:21 -070012355 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012356 tp->tg3_flags |= TG3_FLAG_10_100_ONLY;
12357
12358 err = tg3_phy_probe(tp);
12359 if (err) {
12360 printk(KERN_ERR PFX "(%s) phy probe failed, err %d\n",
12361 pci_name(tp->pdev), err);
12362 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012363 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012364 }
12365
12366 tg3_read_partno(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080012367 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012368
12369 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
12370 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
12371 } else {
12372 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
12373 tp->tg3_flags |= TG3_FLAG_USE_MI_INTERRUPT;
12374 else
12375 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
12376 }
12377
12378 /* 5700 {AX,BX} chips have a broken status block link
12379 * change bit implementation, so we must use the
12380 * status register in those cases.
12381 */
12382 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
12383 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
12384 else
12385 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
12386
12387 /* The led_ctrl is set during tg3_phy_probe, here we might
12388 * have to force the link status polling mechanism based
12389 * upon subsystem IDs.
12390 */
12391 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070012392 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012393 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
12394 tp->tg3_flags |= (TG3_FLAG_USE_MI_INTERRUPT |
12395 TG3_FLAG_USE_LINKCHG_REG);
12396 }
12397
12398 /* For all SERDES we poll the MAC status register. */
12399 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
12400 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
12401 else
12402 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
12403
Matt Carlsonad829262008-11-21 17:16:16 -080012404 tp->rx_offset = NET_IP_ALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012405 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
12406 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0)
12407 tp->rx_offset = 0;
12408
Michael Chanf92905d2006-06-29 20:14:29 -070012409 tp->rx_std_max_post = TG3_RX_RING_SIZE;
12410
12411 /* Increment the rx prod index on the rx std ring by at most
12412 * 8 for these chips to workaround hw errata.
12413 */
12414 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
12415 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
12416 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12417 tp->rx_std_max_post = 8;
12418
Matt Carlson8ed5d972007-05-07 00:25:49 -070012419 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
12420 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
12421 PCIE_PWR_MGMT_L1_THRESH_MSK;
12422
Linus Torvalds1da177e2005-04-16 15:20:36 -070012423 return err;
12424}
12425
David S. Miller49b6e95f2007-03-29 01:38:42 -070012426#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070012427static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
12428{
12429 struct net_device *dev = tp->dev;
12430 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070012431 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070012432 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070012433 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012434
David S. Miller49b6e95f2007-03-29 01:38:42 -070012435 addr = of_get_property(dp, "local-mac-address", &len);
12436 if (addr && len == 6) {
12437 memcpy(dev->dev_addr, addr, 6);
12438 memcpy(dev->perm_addr, dev->dev_addr, 6);
12439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012440 }
12441 return -ENODEV;
12442}
12443
12444static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
12445{
12446 struct net_device *dev = tp->dev;
12447
12448 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070012449 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012450 return 0;
12451}
12452#endif
12453
12454static int __devinit tg3_get_device_address(struct tg3 *tp)
12455{
12456 struct net_device *dev = tp->dev;
12457 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080012458 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012459
David S. Miller49b6e95f2007-03-29 01:38:42 -070012460#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070012461 if (!tg3_get_macaddr_sparc(tp))
12462 return 0;
12463#endif
12464
12465 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070012466 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070012467 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012468 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
12469 mac_offset = 0xcc;
12470 if (tg3_nvram_lock(tp))
12471 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
12472 else
12473 tg3_nvram_unlock(tp);
12474 }
Michael Chanb5d37722006-09-27 16:06:21 -070012475 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12476 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012477
12478 /* First try to get it from MAC address mailbox. */
12479 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
12480 if ((hi >> 16) == 0x484b) {
12481 dev->dev_addr[0] = (hi >> 8) & 0xff;
12482 dev->dev_addr[1] = (hi >> 0) & 0xff;
12483
12484 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
12485 dev->dev_addr[2] = (lo >> 24) & 0xff;
12486 dev->dev_addr[3] = (lo >> 16) & 0xff;
12487 dev->dev_addr[4] = (lo >> 8) & 0xff;
12488 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012489
Michael Chan008652b2006-03-27 23:14:53 -080012490 /* Some old bootcode may report a 0 MAC address in SRAM */
12491 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
12492 }
12493 if (!addr_ok) {
12494 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000012495 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
12496 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000012497 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070012498 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
12499 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080012500 }
12501 /* Finally just fetch it out of the MAC control regs. */
12502 else {
12503 hi = tr32(MAC_ADDR_0_HIGH);
12504 lo = tr32(MAC_ADDR_0_LOW);
12505
12506 dev->dev_addr[5] = lo & 0xff;
12507 dev->dev_addr[4] = (lo >> 8) & 0xff;
12508 dev->dev_addr[3] = (lo >> 16) & 0xff;
12509 dev->dev_addr[2] = (lo >> 24) & 0xff;
12510 dev->dev_addr[1] = hi & 0xff;
12511 dev->dev_addr[0] = (hi >> 8) & 0xff;
12512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012513 }
12514
12515 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070012516#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070012517 if (!tg3_get_default_macaddr_sparc(tp))
12518 return 0;
12519#endif
12520 return -EINVAL;
12521 }
John W. Linville2ff43692005-09-12 14:44:20 -070012522 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012523 return 0;
12524}
12525
David S. Miller59e6b432005-05-18 22:50:10 -070012526#define BOUNDARY_SINGLE_CACHELINE 1
12527#define BOUNDARY_MULTI_CACHELINE 2
12528
12529static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
12530{
12531 int cacheline_size;
12532 u8 byte;
12533 int goal;
12534
12535 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
12536 if (byte == 0)
12537 cacheline_size = 1024;
12538 else
12539 cacheline_size = (int) byte * 4;
12540
12541 /* On 5703 and later chips, the boundary bits have no
12542 * effect.
12543 */
12544 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12545 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
12546 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
12547 goto out;
12548
12549#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
12550 goal = BOUNDARY_MULTI_CACHELINE;
12551#else
12552#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
12553 goal = BOUNDARY_SINGLE_CACHELINE;
12554#else
12555 goal = 0;
12556#endif
12557#endif
12558
12559 if (!goal)
12560 goto out;
12561
12562 /* PCI controllers on most RISC systems tend to disconnect
12563 * when a device tries to burst across a cache-line boundary.
12564 * Therefore, letting tg3 do so just wastes PCI bandwidth.
12565 *
12566 * Unfortunately, for PCI-E there are only limited
12567 * write-side controls for this, and thus for reads
12568 * we will still get the disconnects. We'll also waste
12569 * these PCI cycles for both read and write for chips
12570 * other than 5700 and 5701 which do not implement the
12571 * boundary bits.
12572 */
12573 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
12574 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
12575 switch (cacheline_size) {
12576 case 16:
12577 case 32:
12578 case 64:
12579 case 128:
12580 if (goal == BOUNDARY_SINGLE_CACHELINE) {
12581 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
12582 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
12583 } else {
12584 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
12585 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
12586 }
12587 break;
12588
12589 case 256:
12590 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
12591 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
12592 break;
12593
12594 default:
12595 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
12596 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
12597 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070012598 }
David S. Miller59e6b432005-05-18 22:50:10 -070012599 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
12600 switch (cacheline_size) {
12601 case 16:
12602 case 32:
12603 case 64:
12604 if (goal == BOUNDARY_SINGLE_CACHELINE) {
12605 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
12606 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
12607 break;
12608 }
12609 /* fallthrough */
12610 case 128:
12611 default:
12612 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
12613 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
12614 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070012615 }
David S. Miller59e6b432005-05-18 22:50:10 -070012616 } else {
12617 switch (cacheline_size) {
12618 case 16:
12619 if (goal == BOUNDARY_SINGLE_CACHELINE) {
12620 val |= (DMA_RWCTRL_READ_BNDRY_16 |
12621 DMA_RWCTRL_WRITE_BNDRY_16);
12622 break;
12623 }
12624 /* fallthrough */
12625 case 32:
12626 if (goal == BOUNDARY_SINGLE_CACHELINE) {
12627 val |= (DMA_RWCTRL_READ_BNDRY_32 |
12628 DMA_RWCTRL_WRITE_BNDRY_32);
12629 break;
12630 }
12631 /* fallthrough */
12632 case 64:
12633 if (goal == BOUNDARY_SINGLE_CACHELINE) {
12634 val |= (DMA_RWCTRL_READ_BNDRY_64 |
12635 DMA_RWCTRL_WRITE_BNDRY_64);
12636 break;
12637 }
12638 /* fallthrough */
12639 case 128:
12640 if (goal == BOUNDARY_SINGLE_CACHELINE) {
12641 val |= (DMA_RWCTRL_READ_BNDRY_128 |
12642 DMA_RWCTRL_WRITE_BNDRY_128);
12643 break;
12644 }
12645 /* fallthrough */
12646 case 256:
12647 val |= (DMA_RWCTRL_READ_BNDRY_256 |
12648 DMA_RWCTRL_WRITE_BNDRY_256);
12649 break;
12650 case 512:
12651 val |= (DMA_RWCTRL_READ_BNDRY_512 |
12652 DMA_RWCTRL_WRITE_BNDRY_512);
12653 break;
12654 case 1024:
12655 default:
12656 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
12657 DMA_RWCTRL_WRITE_BNDRY_1024);
12658 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070012659 }
David S. Miller59e6b432005-05-18 22:50:10 -070012660 }
12661
12662out:
12663 return val;
12664}
12665
Linus Torvalds1da177e2005-04-16 15:20:36 -070012666static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
12667{
12668 struct tg3_internal_buffer_desc test_desc;
12669 u32 sram_dma_descs;
12670 int i, ret;
12671
12672 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
12673
12674 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
12675 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
12676 tw32(RDMAC_STATUS, 0);
12677 tw32(WDMAC_STATUS, 0);
12678
12679 tw32(BUFMGR_MODE, 0);
12680 tw32(FTQ_RESET, 0);
12681
12682 test_desc.addr_hi = ((u64) buf_dma) >> 32;
12683 test_desc.addr_lo = buf_dma & 0xffffffff;
12684 test_desc.nic_mbuf = 0x00002100;
12685 test_desc.len = size;
12686
12687 /*
12688 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
12689 * the *second* time the tg3 driver was getting loaded after an
12690 * initial scan.
12691 *
12692 * Broadcom tells me:
12693 * ...the DMA engine is connected to the GRC block and a DMA
12694 * reset may affect the GRC block in some unpredictable way...
12695 * The behavior of resets to individual blocks has not been tested.
12696 *
12697 * Broadcom noted the GRC reset will also reset all sub-components.
12698 */
12699 if (to_device) {
12700 test_desc.cqid_sqid = (13 << 8) | 2;
12701
12702 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
12703 udelay(40);
12704 } else {
12705 test_desc.cqid_sqid = (16 << 8) | 7;
12706
12707 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
12708 udelay(40);
12709 }
12710 test_desc.flags = 0x00000005;
12711
12712 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
12713 u32 val;
12714
12715 val = *(((u32 *)&test_desc) + i);
12716 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
12717 sram_dma_descs + (i * sizeof(u32)));
12718 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
12719 }
12720 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
12721
12722 if (to_device) {
12723 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
12724 } else {
12725 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
12726 }
12727
12728 ret = -ENODEV;
12729 for (i = 0; i < 40; i++) {
12730 u32 val;
12731
12732 if (to_device)
12733 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
12734 else
12735 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
12736 if ((val & 0xffff) == sram_dma_descs) {
12737 ret = 0;
12738 break;
12739 }
12740
12741 udelay(100);
12742 }
12743
12744 return ret;
12745}
12746
David S. Millerded73402005-05-23 13:59:47 -070012747#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070012748
12749static int __devinit tg3_test_dma(struct tg3 *tp)
12750{
12751 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070012752 u32 *buf, saved_dma_rwctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012753 int ret;
12754
12755 buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
12756 if (!buf) {
12757 ret = -ENOMEM;
12758 goto out_nofree;
12759 }
12760
12761 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
12762 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
12763
David S. Miller59e6b432005-05-18 22:50:10 -070012764 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012765
12766 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
12767 /* DMA read watermark not used on PCIE */
12768 tp->dma_rwctrl |= 0x00180000;
12769 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070012770 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
12771 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012772 tp->dma_rwctrl |= 0x003f0000;
12773 else
12774 tp->dma_rwctrl |= 0x003f000f;
12775 } else {
12776 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
12777 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
12778 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080012779 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012780
Michael Chan4a29cc22006-03-19 13:21:12 -080012781 /* If the 5704 is behind the EPB bridge, we can
12782 * do the less restrictive ONE_DMA workaround for
12783 * better performance.
12784 */
12785 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
12786 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
12787 tp->dma_rwctrl |= 0x8000;
12788 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012789 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
12790
Michael Chan49afdeb2007-02-13 12:17:03 -080012791 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
12792 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070012793 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080012794 tp->dma_rwctrl |=
12795 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
12796 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
12797 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070012798 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
12799 /* 5780 always in PCIX mode */
12800 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070012801 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
12802 /* 5714 always in PCIX mode */
12803 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012804 } else {
12805 tp->dma_rwctrl |= 0x001b000f;
12806 }
12807 }
12808
12809 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
12810 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
12811 tp->dma_rwctrl &= 0xfffffff0;
12812
12813 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12814 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
12815 /* Remove this if it causes problems for some boards. */
12816 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
12817
12818 /* On 5700/5701 chips, we need to set this bit.
12819 * Otherwise the chip will issue cacheline transactions
12820 * to streamable DMA memory with not all the byte
12821 * enables turned on. This is an error on several
12822 * RISC PCI controllers, in particular sparc64.
12823 *
12824 * On 5703/5704 chips, this bit has been reassigned
12825 * a different meaning. In particular, it is used
12826 * on those chips to enable a PCI-X workaround.
12827 */
12828 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
12829 }
12830
12831 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
12832
12833#if 0
12834 /* Unneeded, already done by tg3_get_invariants. */
12835 tg3_switch_clocks(tp);
12836#endif
12837
12838 ret = 0;
12839 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12840 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
12841 goto out;
12842
David S. Miller59e6b432005-05-18 22:50:10 -070012843 /* It is best to perform DMA test with maximum write burst size
12844 * to expose the 5700/5701 write DMA bug.
12845 */
12846 saved_dma_rwctrl = tp->dma_rwctrl;
12847 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
12848 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
12849
Linus Torvalds1da177e2005-04-16 15:20:36 -070012850 while (1) {
12851 u32 *p = buf, i;
12852
12853 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
12854 p[i] = i;
12855
12856 /* Send the buffer to the chip. */
12857 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
12858 if (ret) {
12859 printk(KERN_ERR "tg3_test_dma() Write the buffer failed %d\n", ret);
12860 break;
12861 }
12862
12863#if 0
12864 /* validate data reached card RAM correctly. */
12865 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
12866 u32 val;
12867 tg3_read_mem(tp, 0x2100 + (i*4), &val);
12868 if (le32_to_cpu(val) != p[i]) {
12869 printk(KERN_ERR " tg3_test_dma() Card buffer corrupted on write! (%d != %d)\n", val, i);
12870 /* ret = -ENODEV here? */
12871 }
12872 p[i] = 0;
12873 }
12874#endif
12875 /* Now read it back. */
12876 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
12877 if (ret) {
12878 printk(KERN_ERR "tg3_test_dma() Read the buffer failed %d\n", ret);
12879
12880 break;
12881 }
12882
12883 /* Verify it. */
12884 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
12885 if (p[i] == i)
12886 continue;
12887
David S. Miller59e6b432005-05-18 22:50:10 -070012888 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
12889 DMA_RWCTRL_WRITE_BNDRY_16) {
12890 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012891 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
12892 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
12893 break;
12894 } else {
12895 printk(KERN_ERR "tg3_test_dma() buffer corrupted on read back! (%d != %d)\n", p[i], i);
12896 ret = -ENODEV;
12897 goto out;
12898 }
12899 }
12900
12901 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
12902 /* Success. */
12903 ret = 0;
12904 break;
12905 }
12906 }
David S. Miller59e6b432005-05-18 22:50:10 -070012907 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
12908 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070012909 static struct pci_device_id dma_wait_state_chipsets[] = {
12910 { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
12911 PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
12912 { },
12913 };
12914
David S. Miller59e6b432005-05-18 22:50:10 -070012915 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070012916 * now look for chipsets that are known to expose the
12917 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070012918 */
Michael Chan6d1cfba2005-06-08 14:13:14 -070012919 if (pci_dev_present(dma_wait_state_chipsets)) {
12920 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
12921 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
12922 }
12923 else
12924 /* Safe to use the calculated DMA boundary. */
12925 tp->dma_rwctrl = saved_dma_rwctrl;
12926
David S. Miller59e6b432005-05-18 22:50:10 -070012927 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
12928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012929
12930out:
12931 pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
12932out_nofree:
12933 return ret;
12934}
12935
12936static void __devinit tg3_init_link_config(struct tg3 *tp)
12937{
12938 tp->link_config.advertising =
12939 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12940 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12941 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
12942 ADVERTISED_Autoneg | ADVERTISED_MII);
12943 tp->link_config.speed = SPEED_INVALID;
12944 tp->link_config.duplex = DUPLEX_INVALID;
12945 tp->link_config.autoneg = AUTONEG_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012946 tp->link_config.active_speed = SPEED_INVALID;
12947 tp->link_config.active_duplex = DUPLEX_INVALID;
12948 tp->link_config.phy_is_low_power = 0;
12949 tp->link_config.orig_speed = SPEED_INVALID;
12950 tp->link_config.orig_duplex = DUPLEX_INVALID;
12951 tp->link_config.orig_autoneg = AUTONEG_INVALID;
12952}
12953
12954static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
12955{
Michael Chanfdfec1722005-07-25 12:31:48 -070012956 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
12957 tp->bufmgr_config.mbuf_read_dma_low_water =
12958 DEFAULT_MB_RDMA_LOW_WATER_5705;
12959 tp->bufmgr_config.mbuf_mac_rx_low_water =
12960 DEFAULT_MB_MACRX_LOW_WATER_5705;
12961 tp->bufmgr_config.mbuf_high_water =
12962 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070012963 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
12964 tp->bufmgr_config.mbuf_mac_rx_low_water =
12965 DEFAULT_MB_MACRX_LOW_WATER_5906;
12966 tp->bufmgr_config.mbuf_high_water =
12967 DEFAULT_MB_HIGH_WATER_5906;
12968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012969
Michael Chanfdfec1722005-07-25 12:31:48 -070012970 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
12971 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
12972 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
12973 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
12974 tp->bufmgr_config.mbuf_high_water_jumbo =
12975 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
12976 } else {
12977 tp->bufmgr_config.mbuf_read_dma_low_water =
12978 DEFAULT_MB_RDMA_LOW_WATER;
12979 tp->bufmgr_config.mbuf_mac_rx_low_water =
12980 DEFAULT_MB_MACRX_LOW_WATER;
12981 tp->bufmgr_config.mbuf_high_water =
12982 DEFAULT_MB_HIGH_WATER;
12983
12984 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
12985 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
12986 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
12987 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
12988 tp->bufmgr_config.mbuf_high_water_jumbo =
12989 DEFAULT_MB_HIGH_WATER_JUMBO;
12990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012991
12992 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
12993 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
12994}
12995
12996static char * __devinit tg3_phy_string(struct tg3 *tp)
12997{
12998 switch (tp->phy_id & PHY_ID_MASK) {
12999 case PHY_ID_BCM5400: return "5400";
13000 case PHY_ID_BCM5401: return "5401";
13001 case PHY_ID_BCM5411: return "5411";
13002 case PHY_ID_BCM5701: return "5701";
13003 case PHY_ID_BCM5703: return "5703";
13004 case PHY_ID_BCM5704: return "5704";
13005 case PHY_ID_BCM5705: return "5705";
13006 case PHY_ID_BCM5750: return "5750";
Michael Chan85e94ce2005-04-21 17:05:28 -070013007 case PHY_ID_BCM5752: return "5752";
Michael Chana4e2b342005-10-26 15:46:52 -070013008 case PHY_ID_BCM5714: return "5714";
Michael Chan4cf78e42005-07-25 12:29:19 -070013009 case PHY_ID_BCM5780: return "5780";
Michael Chanaf36e6b2006-03-23 01:28:06 -080013010 case PHY_ID_BCM5755: return "5755";
Michael Chand9ab5ad12006-03-20 22:27:35 -080013011 case PHY_ID_BCM5787: return "5787";
Matt Carlsond30cdd22007-10-07 23:28:35 -070013012 case PHY_ID_BCM5784: return "5784";
Michael Chan126a3362006-09-27 16:03:07 -070013013 case PHY_ID_BCM5756: return "5722/5756";
Michael Chanb5d37722006-09-27 16:06:21 -070013014 case PHY_ID_BCM5906: return "5906";
Matt Carlson9936bcf2007-10-10 18:03:07 -070013015 case PHY_ID_BCM5761: return "5761";
Linus Torvalds1da177e2005-04-16 15:20:36 -070013016 case PHY_ID_BCM8002: return "8002/serdes";
13017 case 0: return "serdes";
13018 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070013019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013020}
13021
Michael Chanf9804dd2005-09-27 12:13:10 -070013022static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
13023{
13024 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13025 strcpy(str, "PCI Express");
13026 return str;
13027 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
13028 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
13029
13030 strcpy(str, "PCIX:");
13031
13032 if ((clock_ctrl == 7) ||
13033 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
13034 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
13035 strcat(str, "133MHz");
13036 else if (clock_ctrl == 0)
13037 strcat(str, "33MHz");
13038 else if (clock_ctrl == 2)
13039 strcat(str, "50MHz");
13040 else if (clock_ctrl == 4)
13041 strcat(str, "66MHz");
13042 else if (clock_ctrl == 6)
13043 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070013044 } else {
13045 strcpy(str, "PCI:");
13046 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
13047 strcat(str, "66MHz");
13048 else
13049 strcat(str, "33MHz");
13050 }
13051 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
13052 strcat(str, ":32-bit");
13053 else
13054 strcat(str, ":64-bit");
13055 return str;
13056}
13057
Michael Chan8c2dc7e2005-12-19 16:26:02 -080013058static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013059{
13060 struct pci_dev *peer;
13061 unsigned int func, devnr = tp->pdev->devfn & ~7;
13062
13063 for (func = 0; func < 8; func++) {
13064 peer = pci_get_slot(tp->pdev->bus, devnr | func);
13065 if (peer && peer != tp->pdev)
13066 break;
13067 pci_dev_put(peer);
13068 }
Michael Chan16fe9d72005-12-13 21:09:54 -080013069 /* 5704 can be configured in single-port mode, set peer to
13070 * tp->pdev in that case.
13071 */
13072 if (!peer) {
13073 peer = tp->pdev;
13074 return peer;
13075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013076
13077 /*
13078 * We don't need to keep the refcount elevated; there's no way
13079 * to remove one half of this device without removing the other
13080 */
13081 pci_dev_put(peer);
13082
13083 return peer;
13084}
13085
David S. Miller15f98502005-05-18 22:49:26 -070013086static void __devinit tg3_init_coal(struct tg3 *tp)
13087{
13088 struct ethtool_coalesce *ec = &tp->coal;
13089
13090 memset(ec, 0, sizeof(*ec));
13091 ec->cmd = ETHTOOL_GCOALESCE;
13092 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
13093 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
13094 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
13095 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
13096 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
13097 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
13098 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
13099 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
13100 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
13101
13102 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
13103 HOSTCC_MODE_CLRTICK_TXBD)) {
13104 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
13105 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
13106 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
13107 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
13108 }
Michael Chand244c892005-07-05 14:42:33 -070013109
13110 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
13111 ec->rx_coalesce_usecs_irq = 0;
13112 ec->tx_coalesce_usecs_irq = 0;
13113 ec->stats_block_coalesce_usecs = 0;
13114 }
David S. Miller15f98502005-05-18 22:49:26 -070013115}
13116
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080013117static const struct net_device_ops tg3_netdev_ops = {
13118 .ndo_open = tg3_open,
13119 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080013120 .ndo_start_xmit = tg3_start_xmit,
13121 .ndo_get_stats = tg3_get_stats,
13122 .ndo_validate_addr = eth_validate_addr,
13123 .ndo_set_multicast_list = tg3_set_rx_mode,
13124 .ndo_set_mac_address = tg3_set_mac_addr,
13125 .ndo_do_ioctl = tg3_ioctl,
13126 .ndo_tx_timeout = tg3_tx_timeout,
13127 .ndo_change_mtu = tg3_change_mtu,
13128#if TG3_VLAN_TAG_USED
13129 .ndo_vlan_rx_register = tg3_vlan_rx_register,
13130#endif
13131#ifdef CONFIG_NET_POLL_CONTROLLER
13132 .ndo_poll_controller = tg3_poll_controller,
13133#endif
13134};
13135
13136static const struct net_device_ops tg3_netdev_ops_dma_bug = {
13137 .ndo_open = tg3_open,
13138 .ndo_stop = tg3_close,
13139 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080013140 .ndo_get_stats = tg3_get_stats,
13141 .ndo_validate_addr = eth_validate_addr,
13142 .ndo_set_multicast_list = tg3_set_rx_mode,
13143 .ndo_set_mac_address = tg3_set_mac_addr,
13144 .ndo_do_ioctl = tg3_ioctl,
13145 .ndo_tx_timeout = tg3_tx_timeout,
13146 .ndo_change_mtu = tg3_change_mtu,
13147#if TG3_VLAN_TAG_USED
13148 .ndo_vlan_rx_register = tg3_vlan_rx_register,
13149#endif
13150#ifdef CONFIG_NET_POLL_CONTROLLER
13151 .ndo_poll_controller = tg3_poll_controller,
13152#endif
13153};
13154
Linus Torvalds1da177e2005-04-16 15:20:36 -070013155static int __devinit tg3_init_one(struct pci_dev *pdev,
13156 const struct pci_device_id *ent)
13157{
13158 static int tg3_version_printed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013159 struct net_device *dev;
13160 struct tg3 *tp;
Joe Perchesd6645372007-12-20 04:06:59 -080013161 int err, pm_cap;
Michael Chanf9804dd2005-09-27 12:13:10 -070013162 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080013163 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013164
13165 if (tg3_version_printed++ == 0)
13166 printk(KERN_INFO "%s", version);
13167
13168 err = pci_enable_device(pdev);
13169 if (err) {
13170 printk(KERN_ERR PFX "Cannot enable PCI device, "
13171 "aborting.\n");
13172 return err;
13173 }
13174
Linus Torvalds1da177e2005-04-16 15:20:36 -070013175 err = pci_request_regions(pdev, DRV_MODULE_NAME);
13176 if (err) {
13177 printk(KERN_ERR PFX "Cannot obtain PCI resources, "
13178 "aborting.\n");
13179 goto err_out_disable_pdev;
13180 }
13181
13182 pci_set_master(pdev);
13183
13184 /* Find power-management capability. */
13185 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
13186 if (pm_cap == 0) {
13187 printk(KERN_ERR PFX "Cannot find PowerManagement capability, "
13188 "aborting.\n");
13189 err = -EIO;
13190 goto err_out_free_res;
13191 }
13192
Linus Torvalds1da177e2005-04-16 15:20:36 -070013193 dev = alloc_etherdev(sizeof(*tp));
13194 if (!dev) {
13195 printk(KERN_ERR PFX "Etherdev alloc failed, aborting.\n");
13196 err = -ENOMEM;
13197 goto err_out_free_res;
13198 }
13199
Linus Torvalds1da177e2005-04-16 15:20:36 -070013200 SET_NETDEV_DEV(dev, &pdev->dev);
13201
Linus Torvalds1da177e2005-04-16 15:20:36 -070013202#if TG3_VLAN_TAG_USED
13203 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013204#endif
13205
13206 tp = netdev_priv(dev);
13207 tp->pdev = pdev;
13208 tp->dev = dev;
13209 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013210 tp->rx_mode = TG3_DEF_RX_MODE;
13211 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070013212
Linus Torvalds1da177e2005-04-16 15:20:36 -070013213 if (tg3_debug > 0)
13214 tp->msg_enable = tg3_debug;
13215 else
13216 tp->msg_enable = TG3_DEF_MSG_ENABLE;
13217
13218 /* The word/byte swap controls here control register access byte
13219 * swapping. DMA data byte swapping is controlled in the GRC_MODE
13220 * setting below.
13221 */
13222 tp->misc_host_ctrl =
13223 MISC_HOST_CTRL_MASK_PCI_INT |
13224 MISC_HOST_CTRL_WORD_SWAP |
13225 MISC_HOST_CTRL_INDIR_ACCESS |
13226 MISC_HOST_CTRL_PCISTATE_RW;
13227
13228 /* The NONFRM (non-frame) byte/word swap controls take effect
13229 * on descriptor entries, anything which isn't packet data.
13230 *
13231 * The StrongARM chips on the board (one for tx, one for rx)
13232 * are running in big-endian mode.
13233 */
13234 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
13235 GRC_MODE_WSWAP_NONFRM_DATA);
13236#ifdef __BIG_ENDIAN
13237 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
13238#endif
13239 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013240 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000013241 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013242
Matt Carlsond5fe4882008-11-21 17:20:32 -080013243 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010013244 if (!tp->regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013245 printk(KERN_ERR PFX "Cannot map device registers, "
13246 "aborting.\n");
13247 err = -ENOMEM;
13248 goto err_out_free_dev;
13249 }
13250
13251 tg3_init_link_config(tp);
13252
Linus Torvalds1da177e2005-04-16 15:20:36 -070013253 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
13254 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
13255 tp->tx_pending = TG3_DEF_TX_RING_PENDING;
13256
Stephen Hemmingerbea33482007-10-03 16:41:36 -070013257 netif_napi_add(dev, &tp->napi, tg3_poll, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013258 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013259 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013260 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013261
13262 err = tg3_get_invariants(tp);
13263 if (err) {
13264 printk(KERN_ERR PFX "Problem fetching invariants of chip, "
13265 "aborting.\n");
13266 goto err_out_iounmap;
13267 }
13268
Matt Carlson321d32a2008-11-21 17:22:19 -080013269 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Stephen Hemminger00829822008-11-20 20:14:53 -080013270 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13271 dev->netdev_ops = &tg3_netdev_ops;
13272 else
13273 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
13274
13275
Michael Chan4a29cc22006-03-19 13:21:12 -080013276 /* The EPB bridge inside 5714, 5715, and 5780 and any
13277 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080013278 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
13279 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
13280 * do DMA address check in tg3_start_xmit().
13281 */
Michael Chan4a29cc22006-03-19 13:21:12 -080013282 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070013283 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080013284 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070013285 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080013286#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070013287 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080013288#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080013289 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070013290 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080013291
13292 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070013293 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080013294 err = pci_set_dma_mask(pdev, dma_mask);
13295 if (!err) {
13296 dev->features |= NETIF_F_HIGHDMA;
13297 err = pci_set_consistent_dma_mask(pdev,
13298 persist_dma_mask);
13299 if (err < 0) {
13300 printk(KERN_ERR PFX "Unable to obtain 64 bit "
13301 "DMA for consistent allocations\n");
13302 goto err_out_iounmap;
13303 }
13304 }
13305 }
Yang Hongyang284901a2009-04-06 19:01:15 -070013306 if (err || dma_mask == DMA_BIT_MASK(32)) {
13307 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080013308 if (err) {
13309 printk(KERN_ERR PFX "No usable DMA configuration, "
13310 "aborting.\n");
13311 goto err_out_iounmap;
13312 }
13313 }
13314
Michael Chanfdfec1722005-07-25 12:31:48 -070013315 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013316
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080013317 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080013318 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080013319
Linus Torvalds1da177e2005-04-16 15:20:36 -070013320 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
13321 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
13322 }
13323 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13324 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13325 tp->pci_chip_rev_id == CHIPREV_ID_5705_A0 ||
Michael Chanc7835a72006-11-15 21:14:42 -080013326 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070013327 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
13328 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
13329 } else {
Michael Chan7f62ad52007-02-20 23:25:40 -080013330 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080013331 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
Matt Carlson9e9fd122009-01-19 16:57:45 -080013332 tp->fw_needed = FIRMWARE_TG3TSO5;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080013333 else
Matt Carlson9e9fd122009-01-19 16:57:45 -080013334 tp->fw_needed = FIRMWARE_TG3TSO;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080013335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013336
Michael Chan4e3a7aa2006-03-20 17:47:44 -080013337 /* TSO is on by default on chips that support hardware TSO.
13338 * Firmware TSO on older chips gives lower performance, so it
13339 * is off by default, but can be enabled using ethtool.
13340 */
Michael Chanb0026622006-07-03 19:42:14 -070013341 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Matt Carlson027455a2008-12-21 20:19:30 -080013342 if (dev->features & NETIF_F_IP_CSUM)
13343 dev->features |= NETIF_F_TSO;
13344 if ((dev->features & NETIF_F_IPV6_CSUM) &&
13345 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2))
Michael Chanb0026622006-07-03 19:42:14 -070013346 dev->features |= NETIF_F_TSO6;
Matt Carlson57e69832008-05-25 23:48:31 -070013347 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
13348 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13349 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013350 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13351 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -070013352 dev->features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070013353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013354
Linus Torvalds1da177e2005-04-16 15:20:36 -070013355
13356 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
13357 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
13358 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
13359 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
13360 tp->rx_pending = 63;
13361 }
13362
Linus Torvalds1da177e2005-04-16 15:20:36 -070013363 err = tg3_get_device_address(tp);
13364 if (err) {
13365 printk(KERN_ERR PFX "Could not obtain valid ethernet address, "
13366 "aborting.\n");
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080013367 goto err_out_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013368 }
13369
Matt Carlson0d3031d2007-10-10 18:02:43 -070013370 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080013371 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080013372 if (!tp->aperegs) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070013373 printk(KERN_ERR PFX "Cannot map APE registers, "
13374 "aborting.\n");
13375 err = -ENOMEM;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080013376 goto err_out_fw;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013377 }
13378
13379 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000013380
13381 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
13382 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070013383 }
13384
Matt Carlsonc88864d2007-11-12 21:07:01 -080013385 /*
13386 * Reset chip in case UNDI or EFI driver did not shutdown
13387 * DMA self test will enable WDMAC and we'll see (spurious)
13388 * pending DMA on the PCI bus at that point.
13389 */
13390 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
13391 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
13392 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
13393 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13394 }
13395
13396 err = tg3_test_dma(tp);
13397 if (err) {
13398 printk(KERN_ERR PFX "DMA engine test failed, aborting.\n");
13399 goto err_out_apeunmap;
13400 }
13401
Matt Carlsonc88864d2007-11-12 21:07:01 -080013402 /* flow control autonegotiation is default behavior */
13403 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
Steve Glendinninge18ce342008-12-16 02:00:00 -080013404 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlsonc88864d2007-11-12 21:07:01 -080013405
13406 tg3_init_coal(tp);
13407
Michael Chanc49a1562006-12-17 17:07:29 -080013408 pci_set_drvdata(pdev, dev);
13409
Linus Torvalds1da177e2005-04-16 15:20:36 -070013410 err = register_netdev(dev);
13411 if (err) {
13412 printk(KERN_ERR PFX "Cannot register net device, "
13413 "aborting.\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070013414 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013415 }
13416
Matt Carlsondf59c942008-11-03 16:52:56 -080013417 printk(KERN_INFO "%s: Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070013418 dev->name,
13419 tp->board_part_number,
13420 tp->pci_chip_rev_id,
Michael Chanf9804dd2005-09-27 12:13:10 -070013421 tg3_bus_string(tp, str),
Johannes Berge1749612008-10-27 15:59:26 -070013422 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013423
Matt Carlsondf59c942008-11-03 16:52:56 -080013424 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED)
13425 printk(KERN_INFO
13426 "%s: attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
13427 tp->dev->name,
13428 tp->mdio_bus->phy_map[PHY_ADDR]->drv->name,
Kay Sieversfb28ad32008-11-10 13:55:14 -080013429 dev_name(&tp->mdio_bus->phy_map[PHY_ADDR]->dev));
Matt Carlsondf59c942008-11-03 16:52:56 -080013430 else
13431 printk(KERN_INFO
13432 "%s: attached PHY is %s (%s Ethernet) (WireSpeed[%d])\n",
13433 tp->dev->name, tg3_phy_string(tp),
13434 ((tp->tg3_flags & TG3_FLAG_10_100_ONLY) ? "10/100Base-TX" :
13435 ((tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) ? "1000Base-SX" :
13436 "10/100/1000Base-T")),
13437 (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED) == 0);
13438
13439 printk(KERN_INFO "%s: RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070013440 dev->name,
13441 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
13442 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
13443 (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) != 0,
13444 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013445 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
Michael Chan4a29cc22006-03-19 13:21:12 -080013446 printk(KERN_INFO "%s: dma_rwctrl[%08x] dma_mask[%d-bit]\n",
13447 dev->name, tp->dma_rwctrl,
Yang Hongyang284901a2009-04-06 19:01:15 -070013448 (pdev->dma_mask == DMA_BIT_MASK(32)) ? 32 :
Yang Hongyang50cf1562009-04-06 19:01:14 -070013449 (((u64) pdev->dma_mask == DMA_BIT_MASK(40)) ? 40 : 64));
Linus Torvalds1da177e2005-04-16 15:20:36 -070013450
13451 return 0;
13452
Matt Carlson0d3031d2007-10-10 18:02:43 -070013453err_out_apeunmap:
13454 if (tp->aperegs) {
13455 iounmap(tp->aperegs);
13456 tp->aperegs = NULL;
13457 }
13458
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080013459err_out_fw:
13460 if (tp->fw)
13461 release_firmware(tp->fw);
13462
Linus Torvalds1da177e2005-04-16 15:20:36 -070013463err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070013464 if (tp->regs) {
13465 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013466 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013468
13469err_out_free_dev:
13470 free_netdev(dev);
13471
13472err_out_free_res:
13473 pci_release_regions(pdev);
13474
13475err_out_disable_pdev:
13476 pci_disable_device(pdev);
13477 pci_set_drvdata(pdev, NULL);
13478 return err;
13479}
13480
13481static void __devexit tg3_remove_one(struct pci_dev *pdev)
13482{
13483 struct net_device *dev = pci_get_drvdata(pdev);
13484
13485 if (dev) {
13486 struct tg3 *tp = netdev_priv(dev);
13487
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080013488 if (tp->fw)
13489 release_firmware(tp->fw);
13490
Michael Chan7faa0062006-02-02 17:29:28 -080013491 flush_scheduled_work();
Matt Carlson158d7ab2008-05-29 01:37:54 -070013492
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013493 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
13494 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070013495 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013496 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070013497
Linus Torvalds1da177e2005-04-16 15:20:36 -070013498 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070013499 if (tp->aperegs) {
13500 iounmap(tp->aperegs);
13501 tp->aperegs = NULL;
13502 }
Michael Chan68929142005-08-09 20:17:14 -070013503 if (tp->regs) {
13504 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013505 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013507 free_netdev(dev);
13508 pci_release_regions(pdev);
13509 pci_disable_device(pdev);
13510 pci_set_drvdata(pdev, NULL);
13511 }
13512}
13513
13514static int tg3_suspend(struct pci_dev *pdev, pm_message_t state)
13515{
13516 struct net_device *dev = pci_get_drvdata(pdev);
13517 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070013518 pci_power_t target_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013519 int err;
13520
Michael Chan3e0c95f2007-08-03 20:56:54 -070013521 /* PCI register 4 needs to be saved whether netif_running() or not.
13522 * MSI address and data need to be saved if using MSI and
13523 * netif_running().
13524 */
13525 pci_save_state(pdev);
13526
Linus Torvalds1da177e2005-04-16 15:20:36 -070013527 if (!netif_running(dev))
13528 return 0;
13529
Michael Chan7faa0062006-02-02 17:29:28 -080013530 flush_scheduled_work();
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013531 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013532 tg3_netif_stop(tp);
13533
13534 del_timer_sync(&tp->timer);
13535
David S. Millerf47c11e2005-06-24 20:18:35 -070013536 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013537 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070013538 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013539
13540 netif_device_detach(dev);
13541
David S. Millerf47c11e2005-06-24 20:18:35 -070013542 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070013543 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080013544 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070013545 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013546
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070013547 target_state = pdev->pm_cap ? pci_target_state(pdev) : PCI_D3hot;
13548
13549 err = tg3_set_power_state(tp, target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013550 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013551 int err2;
13552
David S. Millerf47c11e2005-06-24 20:18:35 -070013553 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013554
Michael Chan6a9eba12005-12-13 21:08:58 -080013555 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013556 err2 = tg3_restart_hw(tp, 1);
13557 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070013558 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013559
13560 tp->timer.expires = jiffies + tp->timer_offset;
13561 add_timer(&tp->timer);
13562
13563 netif_device_attach(dev);
13564 tg3_netif_start(tp);
13565
Michael Chanb9ec6c12006-07-25 16:37:27 -070013566out:
David S. Millerf47c11e2005-06-24 20:18:35 -070013567 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013568
13569 if (!err2)
13570 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013571 }
13572
13573 return err;
13574}
13575
13576static int tg3_resume(struct pci_dev *pdev)
13577{
13578 struct net_device *dev = pci_get_drvdata(pdev);
13579 struct tg3 *tp = netdev_priv(dev);
13580 int err;
13581
Michael Chan3e0c95f2007-08-03 20:56:54 -070013582 pci_restore_state(tp->pdev);
13583
Linus Torvalds1da177e2005-04-16 15:20:36 -070013584 if (!netif_running(dev))
13585 return 0;
13586
Michael Chanbc1c7562006-03-20 17:48:03 -080013587 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013588 if (err)
13589 return err;
13590
13591 netif_device_attach(dev);
13592
David S. Millerf47c11e2005-06-24 20:18:35 -070013593 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013594
Michael Chan6a9eba12005-12-13 21:08:58 -080013595 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070013596 err = tg3_restart_hw(tp, 1);
13597 if (err)
13598 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013599
13600 tp->timer.expires = jiffies + tp->timer_offset;
13601 add_timer(&tp->timer);
13602
Linus Torvalds1da177e2005-04-16 15:20:36 -070013603 tg3_netif_start(tp);
13604
Michael Chanb9ec6c12006-07-25 16:37:27 -070013605out:
David S. Millerf47c11e2005-06-24 20:18:35 -070013606 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013607
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013608 if (!err)
13609 tg3_phy_start(tp);
13610
Michael Chanb9ec6c12006-07-25 16:37:27 -070013611 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013612}
13613
13614static struct pci_driver tg3_driver = {
13615 .name = DRV_MODULE_NAME,
13616 .id_table = tg3_pci_tbl,
13617 .probe = tg3_init_one,
13618 .remove = __devexit_p(tg3_remove_one),
13619 .suspend = tg3_suspend,
13620 .resume = tg3_resume
13621};
13622
13623static int __init tg3_init(void)
13624{
Jeff Garzik29917622006-08-19 17:48:59 -040013625 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013626}
13627
13628static void __exit tg3_cleanup(void)
13629{
13630 pci_unregister_driver(&tg3_driver);
13631}
13632
13633module_init(tg3_init);
13634module_exit(tg3_cleanup);