blob: dabdf59f80163bc3a8d3ca13f8f7fcc3acfc7b8c [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.
Michael Chan65610fb2007-02-13 12:18:46 -08007 * Copyright (C) 2005-2007 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 Carlsond3d31702008-12-21 20:21:52 -080071#define DRV_MODULE_VERSION "3.97"
72#define DRV_MODULE_RELDATE "December 10, 2008"
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{
863 struct tg3 *tp = (struct tg3 *)bp->priv;
864 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{
877 struct tg3 *tp = (struct tg3 *)bp->priv;
878
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 Carlson9f8ac0b72009-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 Carlson5f0c4a32008-06-09 15:41:12 -07001953 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761) {
1954 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
1955 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
1956 GRC_LCLCTRL_GPIO_OE1 |
1957 GRC_LCLCTRL_GPIO_OE2 |
1958 GRC_LCLCTRL_GPIO_OUTPUT0 |
1959 GRC_LCLCTRL_GPIO_OUTPUT1 |
1960 tp->grc_local_ctrl;
1961 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
1962
1963 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
1964 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
1965
1966 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
1967 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 } else {
1969 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08001970 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 if (tp_peer != tp &&
1973 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
1974 return;
1975
Michael Chandc56b7d2005-12-19 16:26:28 -08001976 /* Workaround to prevent overdrawing Amps. */
1977 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
1978 ASIC_REV_5714) {
1979 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08001980 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
1981 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08001982 }
1983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 /* On 5753 and variants, GPIO2 cannot be used. */
1985 no_gpio2 = tp->nic_sram_data_cfg &
1986 NIC_SRAM_DATA_CFG_NO_GPIO2;
1987
Michael Chandc56b7d2005-12-19 16:26:28 -08001988 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 GRC_LCLCTRL_GPIO_OE1 |
1990 GRC_LCLCTRL_GPIO_OE2 |
1991 GRC_LCLCTRL_GPIO_OUTPUT1 |
1992 GRC_LCLCTRL_GPIO_OUTPUT2;
1993 if (no_gpio2) {
1994 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
1995 GRC_LCLCTRL_GPIO_OUTPUT2);
1996 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001997 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
1998 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2001
Michael Chanb401e9e2005-12-19 16:27:04 -08002002 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2003 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 if (!no_gpio2) {
2006 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002007 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2008 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
2010 }
2011 } else {
2012 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2013 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2014 if (tp_peer != tp &&
2015 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2016 return;
2017
Michael Chanb401e9e2005-12-19 16:27:04 -08002018 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2019 (GRC_LCLCTRL_GPIO_OE1 |
2020 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Michael Chanb401e9e2005-12-19 16:27:04 -08002022 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2023 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Michael Chanb401e9e2005-12-19 16:27:04 -08002025 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2026 (GRC_LCLCTRL_GPIO_OE1 |
2027 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 }
2029 }
2030}
2031
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002032static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2033{
2034 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2035 return 1;
2036 else if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411) {
2037 if (speed != SPEED_10)
2038 return 1;
2039 } else if (speed == SPEED_10)
2040 return 1;
2041
2042 return 0;
2043}
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045static int tg3_setup_phy(struct tg3 *, int);
2046
2047#define RESET_KIND_SHUTDOWN 0
2048#define RESET_KIND_INIT 1
2049#define RESET_KIND_SUSPEND 2
2050
2051static void tg3_write_sig_post_reset(struct tg3 *, int);
2052static int tg3_halt_cpu(struct tg3 *, u32);
Michael Chan6921d202005-12-13 21:15:53 -08002053static int tg3_nvram_lock(struct tg3 *);
2054static void tg3_nvram_unlock(struct tg3 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Matt Carlson0a459aa2008-11-03 16:54:15 -08002056static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002057{
Matt Carlsonce057f02007-11-12 21:08:03 -08002058 u32 val;
2059
Michael Chan51297242007-02-13 12:17:57 -08002060 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
2061 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2062 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2063 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2064
2065 sg_dig_ctrl |=
2066 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2067 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2068 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2069 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002070 return;
Michael Chan51297242007-02-13 12:17:57 -08002071 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002072
Michael Chan60189dd2006-12-17 17:08:07 -08002073 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002074 tg3_bmcr_reset(tp);
2075 val = tr32(GRC_MISC_CFG);
2076 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2077 udelay(40);
2078 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002079 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002080 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2081 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002082
2083 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2084 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2085 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2086 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2087 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002088 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002089
Michael Chan15c3b692006-03-22 01:06:52 -08002090 /* The PHY should not be powered down on some chips because
2091 * of bugs.
2092 */
2093 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2094 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2095 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
2096 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
2097 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002098
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002099 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2100 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002101 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2102 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2103 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2104 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2105 }
2106
Michael Chan15c3b692006-03-22 01:06:52 -08002107 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2108}
2109
Matt Carlson3f007892008-11-03 16:51:36 -08002110/* tp->lock is held. */
2111static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2112{
2113 u32 addr_high, addr_low;
2114 int i;
2115
2116 addr_high = ((tp->dev->dev_addr[0] << 8) |
2117 tp->dev->dev_addr[1]);
2118 addr_low = ((tp->dev->dev_addr[2] << 24) |
2119 (tp->dev->dev_addr[3] << 16) |
2120 (tp->dev->dev_addr[4] << 8) |
2121 (tp->dev->dev_addr[5] << 0));
2122 for (i = 0; i < 4; i++) {
2123 if (i == 1 && skip_mac_1)
2124 continue;
2125 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2126 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2127 }
2128
2129 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2130 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2131 for (i = 0; i < 12; i++) {
2132 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2133 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2134 }
2135 }
2136
2137 addr_high = (tp->dev->dev_addr[0] +
2138 tp->dev->dev_addr[1] +
2139 tp->dev->dev_addr[2] +
2140 tp->dev->dev_addr[3] +
2141 tp->dev->dev_addr[4] +
2142 tp->dev->dev_addr[5]) &
2143 TX_BACKOFF_SEED_MASK;
2144 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2145}
2146
Michael Chanbc1c7562006-03-20 17:48:03 -08002147static int tg3_set_power_state(struct tg3 *tp, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
2149 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002150 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 /* Make sure register accesses (indirect or otherwise)
2153 * will function correctly.
2154 */
2155 pci_write_config_dword(tp->pdev,
2156 TG3PCI_MISC_HOST_CTRL,
2157 tp->misc_host_ctrl);
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 switch (state) {
Michael Chanbc1c7562006-03-20 17:48:03 -08002160 case PCI_D0:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002161 pci_enable_wake(tp->pdev, state, false);
2162 pci_set_power_state(tp->pdev, PCI_D0);
Michael Chan8c6bda12005-04-21 17:09:08 -07002163
Michael Chan9d26e212006-12-07 00:21:14 -08002164 /* Switch out of Vaux if it is a NIC */
2165 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
Michael Chanb401e9e2005-12-19 16:27:04 -08002166 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 return 0;
2169
Michael Chanbc1c7562006-03-20 17:48:03 -08002170 case PCI_D1:
Michael Chanbc1c7562006-03-20 17:48:03 -08002171 case PCI_D2:
Michael Chanbc1c7562006-03-20 17:48:03 -08002172 case PCI_D3hot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 break;
2174
2175 default:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002176 printk(KERN_ERR PFX "%s: Invalid power state (D%d) requested\n",
2177 tp->dev->name, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002179 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002180
2181 /* Restore the CLKREQ setting. */
2182 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2183 u16 lnkctl;
2184
2185 pci_read_config_word(tp->pdev,
2186 tp->pcie_cap + PCI_EXP_LNKCTL,
2187 &lnkctl);
2188 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2189 pci_write_config_word(tp->pdev,
2190 tp->pcie_cap + PCI_EXP_LNKCTL,
2191 lnkctl);
2192 }
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2195 tw32(TG3PCI_MISC_HOST_CTRL,
2196 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2197
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002198 device_should_wake = pci_pme_capable(tp->pdev, state) &&
2199 device_may_wakeup(&tp->pdev->dev) &&
2200 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2201
Matt Carlsondd477002008-05-25 23:45:58 -07002202 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002203 do_low_power = false;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002204 if ((tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) &&
2205 !tp->link_config.phy_is_low_power) {
2206 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002207 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002208
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002209 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002210
2211 tp->link_config.phy_is_low_power = 1;
2212
2213 tp->link_config.orig_speed = phydev->speed;
2214 tp->link_config.orig_duplex = phydev->duplex;
2215 tp->link_config.orig_autoneg = phydev->autoneg;
2216 tp->link_config.orig_advertising = phydev->advertising;
2217
2218 advertising = ADVERTISED_TP |
2219 ADVERTISED_Pause |
2220 ADVERTISED_Autoneg |
2221 ADVERTISED_10baseT_Half;
2222
2223 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002224 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002225 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2226 advertising |=
2227 ADVERTISED_100baseT_Half |
2228 ADVERTISED_100baseT_Full |
2229 ADVERTISED_10baseT_Full;
2230 else
2231 advertising |= ADVERTISED_10baseT_Full;
2232 }
2233
2234 phydev->advertising = advertising;
2235
2236 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002237
2238 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
2239 if (phyid != TG3_PHY_ID_BCMAC131) {
2240 phyid &= TG3_PHY_OUI_MASK;
Roel Kluinf72b5342009-02-18 17:42:42 -08002241 if (phyid == TG3_PHY_OUI_1 ||
2242 phyid == TG3_PHY_OUI_2 ||
Matt Carlson0a459aa2008-11-03 16:54:15 -08002243 phyid == TG3_PHY_OUI_3)
2244 do_low_power = true;
2245 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002246 }
Matt Carlsondd477002008-05-25 23:45:58 -07002247 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002248 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002249
Matt Carlsondd477002008-05-25 23:45:58 -07002250 if (tp->link_config.phy_is_low_power == 0) {
2251 tp->link_config.phy_is_low_power = 1;
2252 tp->link_config.orig_speed = tp->link_config.speed;
2253 tp->link_config.orig_duplex = tp->link_config.duplex;
2254 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Matt Carlsondd477002008-05-25 23:45:58 -07002257 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
2258 tp->link_config.speed = SPEED_10;
2259 tp->link_config.duplex = DUPLEX_HALF;
2260 tp->link_config.autoneg = AUTONEG_ENABLE;
2261 tg3_setup_phy(tp, 0);
2262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
2264
Matt Carlson3f007892008-11-03 16:51:36 -08002265 __tg3_set_mac_addr(tp, 0);
2266
Michael Chanb5d37722006-09-27 16:06:21 -07002267 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2268 u32 val;
2269
2270 val = tr32(GRC_VCPU_EXT_CTRL);
2271 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2272 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002273 int i;
2274 u32 val;
2275
2276 for (i = 0; i < 200; i++) {
2277 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2278 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2279 break;
2280 msleep(1);
2281 }
2282 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002283 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2284 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2285 WOL_DRV_STATE_SHUTDOWN |
2286 WOL_DRV_WOL |
2287 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002288
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002289 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 u32 mac_mode;
2291
2292 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002293 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002294 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2295 udelay(40);
2296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Michael Chan3f7045c2006-09-27 16:02:29 -07002298 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
2299 mac_mode = MAC_MODE_PORT_MODE_GMII;
2300 else
2301 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002303 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2304 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2305 ASIC_REV_5700) {
2306 u32 speed = (tp->tg3_flags &
2307 TG3_FLAG_WOL_SPEED_100MB) ?
2308 SPEED_100 : SPEED_10;
2309 if (tg3_5700_link_polarity(tp, speed))
2310 mac_mode |= MAC_MODE_LINK_POLARITY;
2311 else
2312 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 } else {
2315 mac_mode = MAC_MODE_PORT_MODE_TBI;
2316 }
2317
John W. Linvillecbf46852005-04-21 17:01:29 -07002318 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 tw32(MAC_LED_CTRL, tp->led_ctrl);
2320
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002321 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2322 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2323 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2324 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2325 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2326 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Matt Carlson3bda1252008-08-15 14:08:22 -07002328 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
2329 mac_mode |= tp->mac_mode &
2330 (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
2331 if (mac_mode & MAC_MODE_APE_TX_EN)
2332 mac_mode |= MAC_MODE_TDE_ENABLE;
2333 }
2334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 tw32_f(MAC_MODE, mac_mode);
2336 udelay(100);
2337
2338 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2339 udelay(10);
2340 }
2341
2342 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2343 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2344 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2345 u32 base_val;
2346
2347 base_val = tp->pci_clock_ctrl;
2348 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2349 CLOCK_CTRL_TXCLK_DISABLE);
2350
Michael Chanb401e9e2005-12-19 16:27:04 -08002351 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2352 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002353 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002354 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002355 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002356 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002357 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2359 u32 newbits1, newbits2;
2360
2361 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2362 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2363 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2364 CLOCK_CTRL_TXCLK_DISABLE |
2365 CLOCK_CTRL_ALTCLK);
2366 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2367 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2368 newbits1 = CLOCK_CTRL_625_CORE;
2369 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2370 } else {
2371 newbits1 = CLOCK_CTRL_ALTCLK;
2372 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2373 }
2374
Michael Chanb401e9e2005-12-19 16:27:04 -08002375 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2376 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Michael Chanb401e9e2005-12-19 16:27:04 -08002378 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2379 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2382 u32 newbits3;
2383
2384 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2385 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2386 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2387 CLOCK_CTRL_TXCLK_DISABLE |
2388 CLOCK_CTRL_44MHZ_CORE);
2389 } else {
2390 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2391 }
2392
Michael Chanb401e9e2005-12-19 16:27:04 -08002393 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2394 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 }
2396 }
2397
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002398 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002399 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002400 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 tg3_frob_aux_power(tp);
2403
2404 /* Workaround for unstable PLL clock */
2405 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2406 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2407 u32 val = tr32(0x7d00);
2408
2409 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2410 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002411 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002412 int err;
2413
2414 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002416 if (!err)
2417 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
2420
Michael Chanbbadf502006-04-06 21:46:34 -07002421 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2422
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002423 if (device_should_wake)
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002424 pci_enable_wake(tp->pdev, state, true);
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 /* Finally, set the new power state. */
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002427 pci_set_power_state(tp->pdev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 return 0;
2430}
2431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2433{
2434 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2435 case MII_TG3_AUX_STAT_10HALF:
2436 *speed = SPEED_10;
2437 *duplex = DUPLEX_HALF;
2438 break;
2439
2440 case MII_TG3_AUX_STAT_10FULL:
2441 *speed = SPEED_10;
2442 *duplex = DUPLEX_FULL;
2443 break;
2444
2445 case MII_TG3_AUX_STAT_100HALF:
2446 *speed = SPEED_100;
2447 *duplex = DUPLEX_HALF;
2448 break;
2449
2450 case MII_TG3_AUX_STAT_100FULL:
2451 *speed = SPEED_100;
2452 *duplex = DUPLEX_FULL;
2453 break;
2454
2455 case MII_TG3_AUX_STAT_1000HALF:
2456 *speed = SPEED_1000;
2457 *duplex = DUPLEX_HALF;
2458 break;
2459
2460 case MII_TG3_AUX_STAT_1000FULL:
2461 *speed = SPEED_1000;
2462 *duplex = DUPLEX_FULL;
2463 break;
2464
2465 default:
Michael Chan715116a2006-09-27 16:09:25 -07002466 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2467 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2468 SPEED_10;
2469 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2470 DUPLEX_HALF;
2471 break;
2472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 *speed = SPEED_INVALID;
2474 *duplex = DUPLEX_INVALID;
2475 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
2478
2479static void tg3_phy_copper_begin(struct tg3 *tp)
2480{
2481 u32 new_adv;
2482 int i;
2483
2484 if (tp->link_config.phy_is_low_power) {
2485 /* Entering low power mode. Disable gigabit and
2486 * 100baseT advertisements.
2487 */
2488 tg3_writephy(tp, MII_TG3_CTRL, 0);
2489
2490 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2491 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2492 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2493 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2494
2495 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2496 } else if (tp->link_config.speed == SPEED_INVALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
2498 tp->link_config.advertising &=
2499 ~(ADVERTISED_1000baseT_Half |
2500 ADVERTISED_1000baseT_Full);
2501
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002502 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2504 new_adv |= ADVERTISE_10HALF;
2505 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2506 new_adv |= ADVERTISE_10FULL;
2507 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2508 new_adv |= ADVERTISE_100HALF;
2509 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2510 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002511
2512 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2515
2516 if (tp->link_config.advertising &
2517 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2518 new_adv = 0;
2519 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2520 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2521 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2522 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
2523 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY) &&
2524 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2525 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2526 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2527 MII_TG3_CTRL_ENABLE_AS_MASTER);
2528 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2529 } else {
2530 tg3_writephy(tp, MII_TG3_CTRL, 0);
2531 }
2532 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002533 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2534 new_adv |= ADVERTISE_CSMA;
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 /* Asking for a specific link mode. */
2537 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2539
2540 if (tp->link_config.duplex == DUPLEX_FULL)
2541 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2542 else
2543 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2544 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2545 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2546 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2547 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 if (tp->link_config.speed == SPEED_100) {
2550 if (tp->link_config.duplex == DUPLEX_FULL)
2551 new_adv |= ADVERTISE_100FULL;
2552 else
2553 new_adv |= ADVERTISE_100HALF;
2554 } else {
2555 if (tp->link_config.duplex == DUPLEX_FULL)
2556 new_adv |= ADVERTISE_10FULL;
2557 else
2558 new_adv |= ADVERTISE_10HALF;
2559 }
2560 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002561
2562 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002564
2565 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 }
2567
2568 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
2569 tp->link_config.speed != SPEED_INVALID) {
2570 u32 bmcr, orig_bmcr;
2571
2572 tp->link_config.active_speed = tp->link_config.speed;
2573 tp->link_config.active_duplex = tp->link_config.duplex;
2574
2575 bmcr = 0;
2576 switch (tp->link_config.speed) {
2577 default:
2578 case SPEED_10:
2579 break;
2580
2581 case SPEED_100:
2582 bmcr |= BMCR_SPEED100;
2583 break;
2584
2585 case SPEED_1000:
2586 bmcr |= TG3_BMCR_SPEED1000;
2587 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
2590 if (tp->link_config.duplex == DUPLEX_FULL)
2591 bmcr |= BMCR_FULLDPLX;
2592
2593 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
2594 (bmcr != orig_bmcr)) {
2595 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
2596 for (i = 0; i < 1500; i++) {
2597 u32 tmp;
2598
2599 udelay(10);
2600 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
2601 tg3_readphy(tp, MII_BMSR, &tmp))
2602 continue;
2603 if (!(tmp & BMSR_LSTATUS)) {
2604 udelay(40);
2605 break;
2606 }
2607 }
2608 tg3_writephy(tp, MII_BMCR, bmcr);
2609 udelay(40);
2610 }
2611 } else {
2612 tg3_writephy(tp, MII_BMCR,
2613 BMCR_ANENABLE | BMCR_ANRESTART);
2614 }
2615}
2616
2617static int tg3_init_5401phy_dsp(struct tg3 *tp)
2618{
2619 int err;
2620
2621 /* Turn off tap power management. */
2622 /* Set Extended packet length bit */
2623 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
2624
2625 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0012);
2626 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1804);
2627
2628 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0013);
2629 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1204);
2630
2631 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2632 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0132);
2633
2634 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2635 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0232);
2636
2637 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2638 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0a20);
2639
2640 udelay(40);
2641
2642 return err;
2643}
2644
Michael Chan3600d912006-12-07 00:21:48 -08002645static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
Michael Chan3600d912006-12-07 00:21:48 -08002647 u32 adv_reg, all_mask = 0;
2648
2649 if (mask & ADVERTISED_10baseT_Half)
2650 all_mask |= ADVERTISE_10HALF;
2651 if (mask & ADVERTISED_10baseT_Full)
2652 all_mask |= ADVERTISE_10FULL;
2653 if (mask & ADVERTISED_100baseT_Half)
2654 all_mask |= ADVERTISE_100HALF;
2655 if (mask & ADVERTISED_100baseT_Full)
2656 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
2658 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
2659 return 0;
2660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 if ((adv_reg & all_mask) != all_mask)
2662 return 0;
2663 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
2664 u32 tg3_ctrl;
2665
Michael Chan3600d912006-12-07 00:21:48 -08002666 all_mask = 0;
2667 if (mask & ADVERTISED_1000baseT_Half)
2668 all_mask |= ADVERTISE_1000HALF;
2669 if (mask & ADVERTISED_1000baseT_Full)
2670 all_mask |= ADVERTISE_1000FULL;
2671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
2673 return 0;
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 if ((tg3_ctrl & all_mask) != all_mask)
2676 return 0;
2677 }
2678 return 1;
2679}
2680
Matt Carlsonef167e22007-12-20 20:10:01 -08002681static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
2682{
2683 u32 curadv, reqadv;
2684
2685 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
2686 return 1;
2687
2688 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
2689 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2690
2691 if (tp->link_config.active_duplex == DUPLEX_FULL) {
2692 if (curadv != reqadv)
2693 return 0;
2694
2695 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
2696 tg3_readphy(tp, MII_LPA, rmtadv);
2697 } else {
2698 /* Reprogram the advertisement register, even if it
2699 * does not affect the current link. If the link
2700 * gets renegotiated in the future, we can save an
2701 * additional renegotiation cycle by advertising
2702 * it correctly in the first place.
2703 */
2704 if (curadv != reqadv) {
2705 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
2706 ADVERTISE_PAUSE_ASYM);
2707 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
2708 }
2709 }
2710
2711 return 1;
2712}
2713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
2715{
2716 int current_link_up;
2717 u32 bmsr, dummy;
Matt Carlsonef167e22007-12-20 20:10:01 -08002718 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 u16 current_speed;
2720 u8 current_duplex;
2721 int i, err;
2722
2723 tw32(MAC_EVENT, 0);
2724
2725 tw32_f(MAC_STATUS,
2726 (MAC_STATUS_SYNC_CHANGED |
2727 MAC_STATUS_CFG_CHANGED |
2728 MAC_STATUS_MI_COMPLETION |
2729 MAC_STATUS_LNKSTATE_CHANGED));
2730 udelay(40);
2731
Matt Carlson8ef21422008-05-02 16:47:53 -07002732 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
2733 tw32_f(MAC_MI_MODE,
2734 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
2735 udelay(80);
2736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
2739
2740 /* Some third-party PHYs need to be reset on link going
2741 * down.
2742 */
2743 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2744 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2745 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
2746 netif_carrier_ok(tp->dev)) {
2747 tg3_readphy(tp, MII_BMSR, &bmsr);
2748 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
2749 !(bmsr & BMSR_LSTATUS))
2750 force_reset = 1;
2751 }
2752 if (force_reset)
2753 tg3_phy_reset(tp);
2754
2755 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
2756 tg3_readphy(tp, MII_BMSR, &bmsr);
2757 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
2758 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
2759 bmsr = 0;
2760
2761 if (!(bmsr & BMSR_LSTATUS)) {
2762 err = tg3_init_5401phy_dsp(tp);
2763 if (err)
2764 return err;
2765
2766 tg3_readphy(tp, MII_BMSR, &bmsr);
2767 for (i = 0; i < 1000; i++) {
2768 udelay(10);
2769 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
2770 (bmsr & BMSR_LSTATUS)) {
2771 udelay(40);
2772 break;
2773 }
2774 }
2775
2776 if ((tp->phy_id & PHY_ID_REV_MASK) == PHY_REV_BCM5401_B0 &&
2777 !(bmsr & BMSR_LSTATUS) &&
2778 tp->link_config.active_speed == SPEED_1000) {
2779 err = tg3_phy_reset(tp);
2780 if (!err)
2781 err = tg3_init_5401phy_dsp(tp);
2782 if (err)
2783 return err;
2784 }
2785 }
2786 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2787 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
2788 /* 5701 {A0,B0} CRC bug workaround */
2789 tg3_writephy(tp, 0x15, 0x0a75);
2790 tg3_writephy(tp, 0x1c, 0x8c68);
2791 tg3_writephy(tp, 0x1c, 0x8d68);
2792 tg3_writephy(tp, 0x1c, 0x8c68);
2793 }
2794
2795 /* Clear pending interrupts... */
2796 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
2797 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
2798
2799 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT)
2800 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Michael Chan715116a2006-09-27 16:09:25 -07002801 else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5906)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 tg3_writephy(tp, MII_TG3_IMASK, ~0);
2803
2804 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2805 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2806 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
2807 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2808 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
2809 else
2810 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
2811 }
2812
2813 current_link_up = 0;
2814 current_speed = SPEED_INVALID;
2815 current_duplex = DUPLEX_INVALID;
2816
2817 if (tp->tg3_flags2 & TG3_FLG2_CAPACITIVE_COUPLING) {
2818 u32 val;
2819
2820 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
2821 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
2822 if (!(val & (1 << 10))) {
2823 val |= (1 << 10);
2824 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
2825 goto relink;
2826 }
2827 }
2828
2829 bmsr = 0;
2830 for (i = 0; i < 100; i++) {
2831 tg3_readphy(tp, MII_BMSR, &bmsr);
2832 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
2833 (bmsr & BMSR_LSTATUS))
2834 break;
2835 udelay(40);
2836 }
2837
2838 if (bmsr & BMSR_LSTATUS) {
2839 u32 aux_stat, bmcr;
2840
2841 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
2842 for (i = 0; i < 2000; i++) {
2843 udelay(10);
2844 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
2845 aux_stat)
2846 break;
2847 }
2848
2849 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
2850 &current_speed,
2851 &current_duplex);
2852
2853 bmcr = 0;
2854 for (i = 0; i < 200; i++) {
2855 tg3_readphy(tp, MII_BMCR, &bmcr);
2856 if (tg3_readphy(tp, MII_BMCR, &bmcr))
2857 continue;
2858 if (bmcr && bmcr != 0x7fff)
2859 break;
2860 udelay(10);
2861 }
2862
Matt Carlsonef167e22007-12-20 20:10:01 -08002863 lcl_adv = 0;
2864 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Matt Carlsonef167e22007-12-20 20:10:01 -08002866 tp->link_config.active_speed = current_speed;
2867 tp->link_config.active_duplex = current_duplex;
2868
2869 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
2870 if ((bmcr & BMCR_ANENABLE) &&
2871 tg3_copper_is_advertising_all(tp,
2872 tp->link_config.advertising)) {
2873 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
2874 &rmt_adv))
2875 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 }
2877 } else {
2878 if (!(bmcr & BMCR_ANENABLE) &&
2879 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08002880 tp->link_config.duplex == current_duplex &&
2881 tp->link_config.flowctrl ==
2882 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 }
2885 }
2886
Matt Carlsonef167e22007-12-20 20:10:01 -08002887 if (current_link_up == 1 &&
2888 tp->link_config.active_duplex == DUPLEX_FULL)
2889 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 }
2891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892relink:
Michael Chan6921d202005-12-13 21:15:53 -08002893 if (current_link_up == 0 || tp->link_config.phy_is_low_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 u32 tmp;
2895
2896 tg3_phy_copper_begin(tp);
2897
2898 tg3_readphy(tp, MII_BMSR, &tmp);
2899 if (!tg3_readphy(tp, MII_BMSR, &tmp) &&
2900 (tmp & BMSR_LSTATUS))
2901 current_link_up = 1;
2902 }
2903
2904 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
2905 if (current_link_up == 1) {
2906 if (tp->link_config.active_speed == SPEED_100 ||
2907 tp->link_config.active_speed == SPEED_10)
2908 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
2909 else
2910 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
2911 } else
2912 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
2913
2914 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
2915 if (tp->link_config.active_duplex == DUPLEX_HALF)
2916 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
2917
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002919 if (current_link_up == 1 &&
2920 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002922 else
2923 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 }
2925
2926 /* ??? Without this setting Netgear GA302T PHY does not
2927 * ??? send/receive packets...
2928 */
2929 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411 &&
2930 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
2931 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
2932 tw32_f(MAC_MI_MODE, tp->mi_mode);
2933 udelay(80);
2934 }
2935
2936 tw32_f(MAC_MODE, tp->mac_mode);
2937 udelay(40);
2938
2939 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
2940 /* Polled via timer. */
2941 tw32_f(MAC_EVENT, 0);
2942 } else {
2943 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
2944 }
2945 udelay(40);
2946
2947 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
2948 current_link_up == 1 &&
2949 tp->link_config.active_speed == SPEED_1000 &&
2950 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
2951 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
2952 udelay(120);
2953 tw32_f(MAC_STATUS,
2954 (MAC_STATUS_SYNC_CHANGED |
2955 MAC_STATUS_CFG_CHANGED));
2956 udelay(40);
2957 tg3_write_mem(tp,
2958 NIC_SRAM_FIRMWARE_MBOX,
2959 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
2960 }
2961
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002962 /* Prevent send BD corruption. */
2963 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2964 u16 oldlnkctl, newlnkctl;
2965
2966 pci_read_config_word(tp->pdev,
2967 tp->pcie_cap + PCI_EXP_LNKCTL,
2968 &oldlnkctl);
2969 if (tp->link_config.active_speed == SPEED_100 ||
2970 tp->link_config.active_speed == SPEED_10)
2971 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
2972 else
2973 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
2974 if (newlnkctl != oldlnkctl)
2975 pci_write_config_word(tp->pdev,
2976 tp->pcie_cap + PCI_EXP_LNKCTL,
2977 newlnkctl);
2978 }
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 if (current_link_up != netif_carrier_ok(tp->dev)) {
2981 if (current_link_up)
2982 netif_carrier_on(tp->dev);
2983 else
2984 netif_carrier_off(tp->dev);
2985 tg3_link_report(tp);
2986 }
2987
2988 return 0;
2989}
2990
2991struct tg3_fiber_aneginfo {
2992 int state;
2993#define ANEG_STATE_UNKNOWN 0
2994#define ANEG_STATE_AN_ENABLE 1
2995#define ANEG_STATE_RESTART_INIT 2
2996#define ANEG_STATE_RESTART 3
2997#define ANEG_STATE_DISABLE_LINK_OK 4
2998#define ANEG_STATE_ABILITY_DETECT_INIT 5
2999#define ANEG_STATE_ABILITY_DETECT 6
3000#define ANEG_STATE_ACK_DETECT_INIT 7
3001#define ANEG_STATE_ACK_DETECT 8
3002#define ANEG_STATE_COMPLETE_ACK_INIT 9
3003#define ANEG_STATE_COMPLETE_ACK 10
3004#define ANEG_STATE_IDLE_DETECT_INIT 11
3005#define ANEG_STATE_IDLE_DETECT 12
3006#define ANEG_STATE_LINK_OK 13
3007#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3008#define ANEG_STATE_NEXT_PAGE_WAIT 15
3009
3010 u32 flags;
3011#define MR_AN_ENABLE 0x00000001
3012#define MR_RESTART_AN 0x00000002
3013#define MR_AN_COMPLETE 0x00000004
3014#define MR_PAGE_RX 0x00000008
3015#define MR_NP_LOADED 0x00000010
3016#define MR_TOGGLE_TX 0x00000020
3017#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3018#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3019#define MR_LP_ADV_SYM_PAUSE 0x00000100
3020#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3021#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3022#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3023#define MR_LP_ADV_NEXT_PAGE 0x00001000
3024#define MR_TOGGLE_RX 0x00002000
3025#define MR_NP_RX 0x00004000
3026
3027#define MR_LINK_OK 0x80000000
3028
3029 unsigned long link_time, cur_time;
3030
3031 u32 ability_match_cfg;
3032 int ability_match_count;
3033
3034 char ability_match, idle_match, ack_match;
3035
3036 u32 txconfig, rxconfig;
3037#define ANEG_CFG_NP 0x00000080
3038#define ANEG_CFG_ACK 0x00000040
3039#define ANEG_CFG_RF2 0x00000020
3040#define ANEG_CFG_RF1 0x00000010
3041#define ANEG_CFG_PS2 0x00000001
3042#define ANEG_CFG_PS1 0x00008000
3043#define ANEG_CFG_HD 0x00004000
3044#define ANEG_CFG_FD 0x00002000
3045#define ANEG_CFG_INVAL 0x00001f06
3046
3047};
3048#define ANEG_OK 0
3049#define ANEG_DONE 1
3050#define ANEG_TIMER_ENAB 2
3051#define ANEG_FAILED -1
3052
3053#define ANEG_STATE_SETTLE_TIME 10000
3054
3055static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3056 struct tg3_fiber_aneginfo *ap)
3057{
Matt Carlson5be73b42007-12-20 20:09:29 -08003058 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 unsigned long delta;
3060 u32 rx_cfg_reg;
3061 int ret;
3062
3063 if (ap->state == ANEG_STATE_UNKNOWN) {
3064 ap->rxconfig = 0;
3065 ap->link_time = 0;
3066 ap->cur_time = 0;
3067 ap->ability_match_cfg = 0;
3068 ap->ability_match_count = 0;
3069 ap->ability_match = 0;
3070 ap->idle_match = 0;
3071 ap->ack_match = 0;
3072 }
3073 ap->cur_time++;
3074
3075 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3076 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3077
3078 if (rx_cfg_reg != ap->ability_match_cfg) {
3079 ap->ability_match_cfg = rx_cfg_reg;
3080 ap->ability_match = 0;
3081 ap->ability_match_count = 0;
3082 } else {
3083 if (++ap->ability_match_count > 1) {
3084 ap->ability_match = 1;
3085 ap->ability_match_cfg = rx_cfg_reg;
3086 }
3087 }
3088 if (rx_cfg_reg & ANEG_CFG_ACK)
3089 ap->ack_match = 1;
3090 else
3091 ap->ack_match = 0;
3092
3093 ap->idle_match = 0;
3094 } else {
3095 ap->idle_match = 1;
3096 ap->ability_match_cfg = 0;
3097 ap->ability_match_count = 0;
3098 ap->ability_match = 0;
3099 ap->ack_match = 0;
3100
3101 rx_cfg_reg = 0;
3102 }
3103
3104 ap->rxconfig = rx_cfg_reg;
3105 ret = ANEG_OK;
3106
3107 switch(ap->state) {
3108 case ANEG_STATE_UNKNOWN:
3109 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3110 ap->state = ANEG_STATE_AN_ENABLE;
3111
3112 /* fallthru */
3113 case ANEG_STATE_AN_ENABLE:
3114 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3115 if (ap->flags & MR_AN_ENABLE) {
3116 ap->link_time = 0;
3117 ap->cur_time = 0;
3118 ap->ability_match_cfg = 0;
3119 ap->ability_match_count = 0;
3120 ap->ability_match = 0;
3121 ap->idle_match = 0;
3122 ap->ack_match = 0;
3123
3124 ap->state = ANEG_STATE_RESTART_INIT;
3125 } else {
3126 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3127 }
3128 break;
3129
3130 case ANEG_STATE_RESTART_INIT:
3131 ap->link_time = ap->cur_time;
3132 ap->flags &= ~(MR_NP_LOADED);
3133 ap->txconfig = 0;
3134 tw32(MAC_TX_AUTO_NEG, 0);
3135 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3136 tw32_f(MAC_MODE, tp->mac_mode);
3137 udelay(40);
3138
3139 ret = ANEG_TIMER_ENAB;
3140 ap->state = ANEG_STATE_RESTART;
3141
3142 /* fallthru */
3143 case ANEG_STATE_RESTART:
3144 delta = ap->cur_time - ap->link_time;
3145 if (delta > ANEG_STATE_SETTLE_TIME) {
3146 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
3147 } else {
3148 ret = ANEG_TIMER_ENAB;
3149 }
3150 break;
3151
3152 case ANEG_STATE_DISABLE_LINK_OK:
3153 ret = ANEG_DONE;
3154 break;
3155
3156 case ANEG_STATE_ABILITY_DETECT_INIT:
3157 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003158 ap->txconfig = ANEG_CFG_FD;
3159 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3160 if (flowctrl & ADVERTISE_1000XPAUSE)
3161 ap->txconfig |= ANEG_CFG_PS1;
3162 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3163 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3165 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3166 tw32_f(MAC_MODE, tp->mac_mode);
3167 udelay(40);
3168
3169 ap->state = ANEG_STATE_ABILITY_DETECT;
3170 break;
3171
3172 case ANEG_STATE_ABILITY_DETECT:
3173 if (ap->ability_match != 0 && ap->rxconfig != 0) {
3174 ap->state = ANEG_STATE_ACK_DETECT_INIT;
3175 }
3176 break;
3177
3178 case ANEG_STATE_ACK_DETECT_INIT:
3179 ap->txconfig |= ANEG_CFG_ACK;
3180 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3181 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3182 tw32_f(MAC_MODE, tp->mac_mode);
3183 udelay(40);
3184
3185 ap->state = ANEG_STATE_ACK_DETECT;
3186
3187 /* fallthru */
3188 case ANEG_STATE_ACK_DETECT:
3189 if (ap->ack_match != 0) {
3190 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3191 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3192 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3193 } else {
3194 ap->state = ANEG_STATE_AN_ENABLE;
3195 }
3196 } else if (ap->ability_match != 0 &&
3197 ap->rxconfig == 0) {
3198 ap->state = ANEG_STATE_AN_ENABLE;
3199 }
3200 break;
3201
3202 case ANEG_STATE_COMPLETE_ACK_INIT:
3203 if (ap->rxconfig & ANEG_CFG_INVAL) {
3204 ret = ANEG_FAILED;
3205 break;
3206 }
3207 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3208 MR_LP_ADV_HALF_DUPLEX |
3209 MR_LP_ADV_SYM_PAUSE |
3210 MR_LP_ADV_ASYM_PAUSE |
3211 MR_LP_ADV_REMOTE_FAULT1 |
3212 MR_LP_ADV_REMOTE_FAULT2 |
3213 MR_LP_ADV_NEXT_PAGE |
3214 MR_TOGGLE_RX |
3215 MR_NP_RX);
3216 if (ap->rxconfig & ANEG_CFG_FD)
3217 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3218 if (ap->rxconfig & ANEG_CFG_HD)
3219 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3220 if (ap->rxconfig & ANEG_CFG_PS1)
3221 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3222 if (ap->rxconfig & ANEG_CFG_PS2)
3223 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3224 if (ap->rxconfig & ANEG_CFG_RF1)
3225 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3226 if (ap->rxconfig & ANEG_CFG_RF2)
3227 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3228 if (ap->rxconfig & ANEG_CFG_NP)
3229 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3230
3231 ap->link_time = ap->cur_time;
3232
3233 ap->flags ^= (MR_TOGGLE_TX);
3234 if (ap->rxconfig & 0x0008)
3235 ap->flags |= MR_TOGGLE_RX;
3236 if (ap->rxconfig & ANEG_CFG_NP)
3237 ap->flags |= MR_NP_RX;
3238 ap->flags |= MR_PAGE_RX;
3239
3240 ap->state = ANEG_STATE_COMPLETE_ACK;
3241 ret = ANEG_TIMER_ENAB;
3242 break;
3243
3244 case ANEG_STATE_COMPLETE_ACK:
3245 if (ap->ability_match != 0 &&
3246 ap->rxconfig == 0) {
3247 ap->state = ANEG_STATE_AN_ENABLE;
3248 break;
3249 }
3250 delta = ap->cur_time - ap->link_time;
3251 if (delta > ANEG_STATE_SETTLE_TIME) {
3252 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3253 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3254 } else {
3255 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3256 !(ap->flags & MR_NP_RX)) {
3257 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3258 } else {
3259 ret = ANEG_FAILED;
3260 }
3261 }
3262 }
3263 break;
3264
3265 case ANEG_STATE_IDLE_DETECT_INIT:
3266 ap->link_time = ap->cur_time;
3267 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3268 tw32_f(MAC_MODE, tp->mac_mode);
3269 udelay(40);
3270
3271 ap->state = ANEG_STATE_IDLE_DETECT;
3272 ret = ANEG_TIMER_ENAB;
3273 break;
3274
3275 case ANEG_STATE_IDLE_DETECT:
3276 if (ap->ability_match != 0 &&
3277 ap->rxconfig == 0) {
3278 ap->state = ANEG_STATE_AN_ENABLE;
3279 break;
3280 }
3281 delta = ap->cur_time - ap->link_time;
3282 if (delta > ANEG_STATE_SETTLE_TIME) {
3283 /* XXX another gem from the Broadcom driver :( */
3284 ap->state = ANEG_STATE_LINK_OK;
3285 }
3286 break;
3287
3288 case ANEG_STATE_LINK_OK:
3289 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3290 ret = ANEG_DONE;
3291 break;
3292
3293 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3294 /* ??? unimplemented */
3295 break;
3296
3297 case ANEG_STATE_NEXT_PAGE_WAIT:
3298 /* ??? unimplemented */
3299 break;
3300
3301 default:
3302 ret = ANEG_FAILED;
3303 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
3306 return ret;
3307}
3308
Matt Carlson5be73b42007-12-20 20:09:29 -08003309static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310{
3311 int res = 0;
3312 struct tg3_fiber_aneginfo aninfo;
3313 int status = ANEG_FAILED;
3314 unsigned int tick;
3315 u32 tmp;
3316
3317 tw32_f(MAC_TX_AUTO_NEG, 0);
3318
3319 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3320 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3321 udelay(40);
3322
3323 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3324 udelay(40);
3325
3326 memset(&aninfo, 0, sizeof(aninfo));
3327 aninfo.flags |= MR_AN_ENABLE;
3328 aninfo.state = ANEG_STATE_UNKNOWN;
3329 aninfo.cur_time = 0;
3330 tick = 0;
3331 while (++tick < 195000) {
3332 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3333 if (status == ANEG_DONE || status == ANEG_FAILED)
3334 break;
3335
3336 udelay(1);
3337 }
3338
3339 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3340 tw32_f(MAC_MODE, tp->mac_mode);
3341 udelay(40);
3342
Matt Carlson5be73b42007-12-20 20:09:29 -08003343 *txflags = aninfo.txconfig;
3344 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
3346 if (status == ANEG_DONE &&
3347 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3348 MR_LP_ADV_FULL_DUPLEX)))
3349 res = 1;
3350
3351 return res;
3352}
3353
3354static void tg3_init_bcm8002(struct tg3 *tp)
3355{
3356 u32 mac_status = tr32(MAC_STATUS);
3357 int i;
3358
3359 /* Reset when initting first time or we have a link. */
3360 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3361 !(mac_status & MAC_STATUS_PCS_SYNCED))
3362 return;
3363
3364 /* Set PLL lock range. */
3365 tg3_writephy(tp, 0x16, 0x8007);
3366
3367 /* SW reset */
3368 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3369
3370 /* Wait for reset to complete. */
3371 /* XXX schedule_timeout() ... */
3372 for (i = 0; i < 500; i++)
3373 udelay(10);
3374
3375 /* Config mode; select PMA/Ch 1 regs. */
3376 tg3_writephy(tp, 0x10, 0x8411);
3377
3378 /* Enable auto-lock and comdet, select txclk for tx. */
3379 tg3_writephy(tp, 0x11, 0x0a10);
3380
3381 tg3_writephy(tp, 0x18, 0x00a0);
3382 tg3_writephy(tp, 0x16, 0x41ff);
3383
3384 /* Assert and deassert POR. */
3385 tg3_writephy(tp, 0x13, 0x0400);
3386 udelay(40);
3387 tg3_writephy(tp, 0x13, 0x0000);
3388
3389 tg3_writephy(tp, 0x11, 0x0a50);
3390 udelay(40);
3391 tg3_writephy(tp, 0x11, 0x0a10);
3392
3393 /* Wait for signal to stabilize */
3394 /* XXX schedule_timeout() ... */
3395 for (i = 0; i < 15000; i++)
3396 udelay(10);
3397
3398 /* Deselect the channel register so we can read the PHYID
3399 * later.
3400 */
3401 tg3_writephy(tp, 0x10, 0x8011);
3402}
3403
3404static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3405{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003406 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 u32 sg_dig_ctrl, sg_dig_status;
3408 u32 serdes_cfg, expected_sg_dig_ctrl;
3409 int workaround, port_a;
3410 int current_link_up;
3411
3412 serdes_cfg = 0;
3413 expected_sg_dig_ctrl = 0;
3414 workaround = 0;
3415 port_a = 1;
3416 current_link_up = 0;
3417
3418 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3419 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3420 workaround = 1;
3421 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3422 port_a = 0;
3423
3424 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3425 /* preserve bits 20-23 for voltage regulator */
3426 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3427 }
3428
3429 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3430
3431 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003432 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 if (workaround) {
3434 u32 val = serdes_cfg;
3435
3436 if (port_a)
3437 val |= 0xc010000;
3438 else
3439 val |= 0x4010000;
3440 tw32_f(MAC_SERDES_CFG, val);
3441 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003442
3443 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 }
3445 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3446 tg3_setup_flow_control(tp, 0, 0);
3447 current_link_up = 1;
3448 }
3449 goto out;
3450 }
3451
3452 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003453 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
Matt Carlson82cd3d12007-12-20 20:09:00 -08003455 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3456 if (flowctrl & ADVERTISE_1000XPAUSE)
3457 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3458 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3459 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
3461 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003462 if ((tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT) &&
3463 tp->serdes_counter &&
3464 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3465 MAC_STATUS_RCVD_CFG)) ==
3466 MAC_STATUS_PCS_SYNCED)) {
3467 tp->serdes_counter--;
3468 current_link_up = 1;
3469 goto out;
3470 }
3471restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 if (workaround)
3473 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003474 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 udelay(5);
3476 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3477
Michael Chan3d3ebe72006-09-27 15:59:15 -07003478 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3479 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3481 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003482 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 mac_status = tr32(MAC_STATUS);
3484
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003485 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003487 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
Matt Carlson82cd3d12007-12-20 20:09:00 -08003489 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3490 local_adv |= ADVERTISE_1000XPAUSE;
3491 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3492 local_adv |= ADVERTISE_1000XPSE_ASYM;
3493
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003494 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003495 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003496 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003497 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
3499 tg3_setup_flow_control(tp, local_adv, remote_adv);
3500 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003501 tp->serdes_counter = 0;
3502 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003503 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003504 if (tp->serdes_counter)
3505 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 else {
3507 if (workaround) {
3508 u32 val = serdes_cfg;
3509
3510 if (port_a)
3511 val |= 0xc010000;
3512 else
3513 val |= 0x4010000;
3514
3515 tw32_f(MAC_SERDES_CFG, val);
3516 }
3517
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003518 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 udelay(40);
3520
3521 /* Link parallel detection - link is up */
3522 /* only if we have PCS_SYNC and not */
3523 /* receiving config code words */
3524 mac_status = tr32(MAC_STATUS);
3525 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3526 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3527 tg3_setup_flow_control(tp, 0, 0);
3528 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003529 tp->tg3_flags2 |=
3530 TG3_FLG2_PARALLEL_DETECT;
3531 tp->serdes_counter =
3532 SERDES_PARALLEL_DET_TIMEOUT;
3533 } else
3534 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 }
3536 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003537 } else {
3538 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3539 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 }
3541
3542out:
3543 return current_link_up;
3544}
3545
3546static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3547{
3548 int current_link_up = 0;
3549
Michael Chan5cf64b8a2007-05-05 12:11:21 -07003550 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
3553 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003554 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003556
Matt Carlson5be73b42007-12-20 20:09:29 -08003557 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3558 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Matt Carlson5be73b42007-12-20 20:09:29 -08003560 if (txflags & ANEG_CFG_PS1)
3561 local_adv |= ADVERTISE_1000XPAUSE;
3562 if (txflags & ANEG_CFG_PS2)
3563 local_adv |= ADVERTISE_1000XPSE_ASYM;
3564
3565 if (rxflags & MR_LP_ADV_SYM_PAUSE)
3566 remote_adv |= LPA_1000XPAUSE;
3567 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3568 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
3570 tg3_setup_flow_control(tp, local_adv, remote_adv);
3571
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 current_link_up = 1;
3573 }
3574 for (i = 0; i < 30; i++) {
3575 udelay(20);
3576 tw32_f(MAC_STATUS,
3577 (MAC_STATUS_SYNC_CHANGED |
3578 MAC_STATUS_CFG_CHANGED));
3579 udelay(40);
3580 if ((tr32(MAC_STATUS) &
3581 (MAC_STATUS_SYNC_CHANGED |
3582 MAC_STATUS_CFG_CHANGED)) == 0)
3583 break;
3584 }
3585
3586 mac_status = tr32(MAC_STATUS);
3587 if (current_link_up == 0 &&
3588 (mac_status & MAC_STATUS_PCS_SYNCED) &&
3589 !(mac_status & MAC_STATUS_RCVD_CFG))
3590 current_link_up = 1;
3591 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08003592 tg3_setup_flow_control(tp, 0, 0);
3593
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 /* Forcing 1000FD link up. */
3595 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
3597 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
3598 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003599
3600 tw32_f(MAC_MODE, tp->mac_mode);
3601 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 }
3603
3604out:
3605 return current_link_up;
3606}
3607
3608static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
3609{
3610 u32 orig_pause_cfg;
3611 u16 orig_active_speed;
3612 u8 orig_active_duplex;
3613 u32 mac_status;
3614 int current_link_up;
3615 int i;
3616
Matt Carlson8d018622007-12-20 20:05:44 -08003617 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 orig_active_speed = tp->link_config.active_speed;
3619 orig_active_duplex = tp->link_config.active_duplex;
3620
3621 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
3622 netif_carrier_ok(tp->dev) &&
3623 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
3624 mac_status = tr32(MAC_STATUS);
3625 mac_status &= (MAC_STATUS_PCS_SYNCED |
3626 MAC_STATUS_SIGNAL_DET |
3627 MAC_STATUS_CFG_CHANGED |
3628 MAC_STATUS_RCVD_CFG);
3629 if (mac_status == (MAC_STATUS_PCS_SYNCED |
3630 MAC_STATUS_SIGNAL_DET)) {
3631 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3632 MAC_STATUS_CFG_CHANGED));
3633 return 0;
3634 }
3635 }
3636
3637 tw32_f(MAC_TX_AUTO_NEG, 0);
3638
3639 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
3640 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
3641 tw32_f(MAC_MODE, tp->mac_mode);
3642 udelay(40);
3643
3644 if (tp->phy_id == PHY_ID_BCM8002)
3645 tg3_init_bcm8002(tp);
3646
3647 /* Enable link change event even when serdes polling. */
3648 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3649 udelay(40);
3650
3651 current_link_up = 0;
3652 mac_status = tr32(MAC_STATUS);
3653
3654 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
3655 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
3656 else
3657 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
3658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 tp->hw_status->status =
3660 (SD_STATUS_UPDATED |
3661 (tp->hw_status->status & ~SD_STATUS_LINK_CHG));
3662
3663 for (i = 0; i < 100; i++) {
3664 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3665 MAC_STATUS_CFG_CHANGED));
3666 udelay(5);
3667 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07003668 MAC_STATUS_CFG_CHANGED |
3669 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 break;
3671 }
3672
3673 mac_status = tr32(MAC_STATUS);
3674 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
3675 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003676 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
3677 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 tw32_f(MAC_MODE, (tp->mac_mode |
3679 MAC_MODE_SEND_CONFIGS));
3680 udelay(1);
3681 tw32_f(MAC_MODE, tp->mac_mode);
3682 }
3683 }
3684
3685 if (current_link_up == 1) {
3686 tp->link_config.active_speed = SPEED_1000;
3687 tp->link_config.active_duplex = DUPLEX_FULL;
3688 tw32(MAC_LED_CTRL, (tp->led_ctrl |
3689 LED_CTRL_LNKLED_OVERRIDE |
3690 LED_CTRL_1000MBPS_ON));
3691 } else {
3692 tp->link_config.active_speed = SPEED_INVALID;
3693 tp->link_config.active_duplex = DUPLEX_INVALID;
3694 tw32(MAC_LED_CTRL, (tp->led_ctrl |
3695 LED_CTRL_LNKLED_OVERRIDE |
3696 LED_CTRL_TRAFFIC_OVERRIDE));
3697 }
3698
3699 if (current_link_up != netif_carrier_ok(tp->dev)) {
3700 if (current_link_up)
3701 netif_carrier_on(tp->dev);
3702 else
3703 netif_carrier_off(tp->dev);
3704 tg3_link_report(tp);
3705 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08003706 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 if (orig_pause_cfg != now_pause_cfg ||
3708 orig_active_speed != tp->link_config.active_speed ||
3709 orig_active_duplex != tp->link_config.active_duplex)
3710 tg3_link_report(tp);
3711 }
3712
3713 return 0;
3714}
3715
Michael Chan747e8f82005-07-25 12:33:22 -07003716static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
3717{
3718 int current_link_up, err = 0;
3719 u32 bmsr, bmcr;
3720 u16 current_speed;
3721 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08003722 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07003723
3724 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3725 tw32_f(MAC_MODE, tp->mac_mode);
3726 udelay(40);
3727
3728 tw32(MAC_EVENT, 0);
3729
3730 tw32_f(MAC_STATUS,
3731 (MAC_STATUS_SYNC_CHANGED |
3732 MAC_STATUS_CFG_CHANGED |
3733 MAC_STATUS_MI_COMPLETION |
3734 MAC_STATUS_LNKSTATE_CHANGED));
3735 udelay(40);
3736
3737 if (force_reset)
3738 tg3_phy_reset(tp);
3739
3740 current_link_up = 0;
3741 current_speed = SPEED_INVALID;
3742 current_duplex = DUPLEX_INVALID;
3743
3744 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
3745 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08003746 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
3747 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
3748 bmsr |= BMSR_LSTATUS;
3749 else
3750 bmsr &= ~BMSR_LSTATUS;
3751 }
Michael Chan747e8f82005-07-25 12:33:22 -07003752
3753 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
3754
3755 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlson2bd3ed02008-06-09 15:39:55 -07003756 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07003757 /* do nothing, just check for link up at the end */
3758 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3759 u32 adv, new_adv;
3760
3761 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
3762 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
3763 ADVERTISE_1000XPAUSE |
3764 ADVERTISE_1000XPSE_ASYM |
3765 ADVERTISE_SLCT);
3766
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003767 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07003768
3769 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
3770 new_adv |= ADVERTISE_1000XHALF;
3771 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
3772 new_adv |= ADVERTISE_1000XFULL;
3773
3774 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
3775 tg3_writephy(tp, MII_ADVERTISE, new_adv);
3776 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
3777 tg3_writephy(tp, MII_BMCR, bmcr);
3778
3779 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07003780 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Michael Chan747e8f82005-07-25 12:33:22 -07003781 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
3782
3783 return err;
3784 }
3785 } else {
3786 u32 new_bmcr;
3787
3788 bmcr &= ~BMCR_SPEED1000;
3789 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
3790
3791 if (tp->link_config.duplex == DUPLEX_FULL)
3792 new_bmcr |= BMCR_FULLDPLX;
3793
3794 if (new_bmcr != bmcr) {
3795 /* BMCR_SPEED1000 is a reserved bit that needs
3796 * to be set on write.
3797 */
3798 new_bmcr |= BMCR_SPEED1000;
3799
3800 /* Force a linkdown */
3801 if (netif_carrier_ok(tp->dev)) {
3802 u32 adv;
3803
3804 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
3805 adv &= ~(ADVERTISE_1000XFULL |
3806 ADVERTISE_1000XHALF |
3807 ADVERTISE_SLCT);
3808 tg3_writephy(tp, MII_ADVERTISE, adv);
3809 tg3_writephy(tp, MII_BMCR, bmcr |
3810 BMCR_ANRESTART |
3811 BMCR_ANENABLE);
3812 udelay(10);
3813 netif_carrier_off(tp->dev);
3814 }
3815 tg3_writephy(tp, MII_BMCR, new_bmcr);
3816 bmcr = new_bmcr;
3817 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
3818 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08003819 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3820 ASIC_REV_5714) {
3821 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
3822 bmsr |= BMSR_LSTATUS;
3823 else
3824 bmsr &= ~BMSR_LSTATUS;
3825 }
Michael Chan747e8f82005-07-25 12:33:22 -07003826 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
3827 }
3828 }
3829
3830 if (bmsr & BMSR_LSTATUS) {
3831 current_speed = SPEED_1000;
3832 current_link_up = 1;
3833 if (bmcr & BMCR_FULLDPLX)
3834 current_duplex = DUPLEX_FULL;
3835 else
3836 current_duplex = DUPLEX_HALF;
3837
Matt Carlsonef167e22007-12-20 20:10:01 -08003838 local_adv = 0;
3839 remote_adv = 0;
3840
Michael Chan747e8f82005-07-25 12:33:22 -07003841 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08003842 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07003843
3844 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
3845 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
3846 common = local_adv & remote_adv;
3847 if (common & (ADVERTISE_1000XHALF |
3848 ADVERTISE_1000XFULL)) {
3849 if (common & ADVERTISE_1000XFULL)
3850 current_duplex = DUPLEX_FULL;
3851 else
3852 current_duplex = DUPLEX_HALF;
Michael Chan747e8f82005-07-25 12:33:22 -07003853 }
3854 else
3855 current_link_up = 0;
3856 }
3857 }
3858
Matt Carlsonef167e22007-12-20 20:10:01 -08003859 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
3860 tg3_setup_flow_control(tp, local_adv, remote_adv);
3861
Michael Chan747e8f82005-07-25 12:33:22 -07003862 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3863 if (tp->link_config.active_duplex == DUPLEX_HALF)
3864 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3865
3866 tw32_f(MAC_MODE, tp->mac_mode);
3867 udelay(40);
3868
3869 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3870
3871 tp->link_config.active_speed = current_speed;
3872 tp->link_config.active_duplex = current_duplex;
3873
3874 if (current_link_up != netif_carrier_ok(tp->dev)) {
3875 if (current_link_up)
3876 netif_carrier_on(tp->dev);
3877 else {
3878 netif_carrier_off(tp->dev);
3879 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
3880 }
3881 tg3_link_report(tp);
3882 }
3883 return err;
3884}
3885
3886static void tg3_serdes_parallel_detect(struct tg3 *tp)
3887{
Michael Chan3d3ebe72006-09-27 15:59:15 -07003888 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07003889 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07003890 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07003891 return;
3892 }
3893 if (!netif_carrier_ok(tp->dev) &&
3894 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
3895 u32 bmcr;
3896
3897 tg3_readphy(tp, MII_BMCR, &bmcr);
3898 if (bmcr & BMCR_ANENABLE) {
3899 u32 phy1, phy2;
3900
3901 /* Select shadow register 0x1f */
3902 tg3_writephy(tp, 0x1c, 0x7c00);
3903 tg3_readphy(tp, 0x1c, &phy1);
3904
3905 /* Select expansion interrupt status register */
3906 tg3_writephy(tp, 0x17, 0x0f01);
3907 tg3_readphy(tp, 0x15, &phy2);
3908 tg3_readphy(tp, 0x15, &phy2);
3909
3910 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
3911 /* We have signal detect and not receiving
3912 * config code words, link is up by parallel
3913 * detection.
3914 */
3915
3916 bmcr &= ~BMCR_ANENABLE;
3917 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
3918 tg3_writephy(tp, MII_BMCR, bmcr);
3919 tp->tg3_flags2 |= TG3_FLG2_PARALLEL_DETECT;
3920 }
3921 }
3922 }
3923 else if (netif_carrier_ok(tp->dev) &&
3924 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
3925 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
3926 u32 phy2;
3927
3928 /* Select expansion interrupt status register */
3929 tg3_writephy(tp, 0x17, 0x0f01);
3930 tg3_readphy(tp, 0x15, &phy2);
3931 if (phy2 & 0x20) {
3932 u32 bmcr;
3933
3934 /* Config code words received, turn on autoneg. */
3935 tg3_readphy(tp, MII_BMCR, &bmcr);
3936 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
3937
3938 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
3939
3940 }
3941 }
3942}
3943
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944static int tg3_setup_phy(struct tg3 *tp, int force_reset)
3945{
3946 int err;
3947
3948 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
3949 err = tg3_setup_fiber_phy(tp, force_reset);
Michael Chan747e8f82005-07-25 12:33:22 -07003950 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
3951 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 } else {
3953 err = tg3_setup_copper_phy(tp, force_reset);
3954 }
3955
Matt Carlsonbcb37f62008-11-03 16:52:09 -08003956 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08003957 u32 val, scale;
3958
3959 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
3960 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
3961 scale = 65;
3962 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
3963 scale = 6;
3964 else
3965 scale = 12;
3966
3967 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
3968 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
3969 tw32(GRC_MISC_CFG, val);
3970 }
3971
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 if (tp->link_config.active_speed == SPEED_1000 &&
3973 tp->link_config.active_duplex == DUPLEX_HALF)
3974 tw32(MAC_TX_LENGTHS,
3975 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
3976 (6 << TX_LENGTHS_IPG_SHIFT) |
3977 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
3978 else
3979 tw32(MAC_TX_LENGTHS,
3980 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
3981 (6 << TX_LENGTHS_IPG_SHIFT) |
3982 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
3983
3984 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
3985 if (netif_carrier_ok(tp->dev)) {
3986 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07003987 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 } else {
3989 tw32(HOSTCC_STAT_COAL_TICKS, 0);
3990 }
3991 }
3992
Matt Carlson8ed5d972007-05-07 00:25:49 -07003993 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
3994 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
3995 if (!netif_carrier_ok(tp->dev))
3996 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
3997 tp->pwrmgmt_thresh;
3998 else
3999 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4000 tw32(PCIE_PWR_MGMT_THRESH, val);
4001 }
4002
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 return err;
4004}
4005
Michael Chandf3e6542006-05-26 17:48:07 -07004006/* This is called whenever we suspect that the system chipset is re-
4007 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4008 * is bogus tx completions. We try to recover by setting the
4009 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4010 * in the workqueue.
4011 */
4012static void tg3_tx_recover(struct tg3 *tp)
4013{
4014 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4015 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4016
4017 printk(KERN_WARNING PFX "%s: The system may be re-ordering memory-"
4018 "mapped I/O cycles to the network device, attempting to "
4019 "recover. Please report the problem to the driver maintainer "
4020 "and include system chipset information.\n", tp->dev->name);
4021
4022 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004023 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004024 spin_unlock(&tp->lock);
4025}
4026
Michael Chan1b2a7202006-08-07 21:46:02 -07004027static inline u32 tg3_tx_avail(struct tg3 *tp)
4028{
4029 smp_mb();
4030 return (tp->tx_pending -
4031 ((tp->tx_prod - tp->tx_cons) & (TG3_TX_RING_SIZE - 1)));
4032}
4033
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034/* Tigon3 never reports partial packet sends. So we do not
4035 * need special logic to handle SKBs that have not had all
4036 * of their frags sent yet, like SunGEM does.
4037 */
4038static void tg3_tx(struct tg3 *tp)
4039{
4040 u32 hw_idx = tp->hw_status->idx[0].tx_consumer;
4041 u32 sw_idx = tp->tx_cons;
4042
4043 while (sw_idx != hw_idx) {
4044 struct tx_ring_info *ri = &tp->tx_buffers[sw_idx];
4045 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004046 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
Michael Chandf3e6542006-05-26 17:48:07 -07004048 if (unlikely(skb == NULL)) {
4049 tg3_tx_recover(tp);
4050 return;
4051 }
4052
David S. Miller90079ce2008-09-11 04:52:51 -07004053 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
4055 ri->skb = NULL;
4056
4057 sw_idx = NEXT_TX(sw_idx);
4058
4059 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 ri = &tp->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004061 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4062 tx_bug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 sw_idx = NEXT_TX(sw_idx);
4064 }
4065
David S. Millerf47c11e2005-06-24 20:18:35 -07004066 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004067
4068 if (unlikely(tx_bug)) {
4069 tg3_tx_recover(tp);
4070 return;
4071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 }
4073
4074 tp->tx_cons = sw_idx;
4075
Michael Chan1b2a7202006-08-07 21:46:02 -07004076 /* Need to make the tx_cons update visible to tg3_start_xmit()
4077 * before checking for netif_queue_stopped(). Without the
4078 * memory barrier, there is a small possibility that tg3_start_xmit()
4079 * will miss it and cause the queue to be stopped forever.
4080 */
4081 smp_mb();
4082
4083 if (unlikely(netif_queue_stopped(tp->dev) &&
Ranjit Manomohan42952232006-10-18 20:54:26 -07004084 (tg3_tx_avail(tp) > TG3_TX_WAKEUP_THRESH(tp)))) {
Michael Chan1b2a7202006-08-07 21:46:02 -07004085 netif_tx_lock(tp->dev);
Michael Chan51b91462005-09-01 17:41:28 -07004086 if (netif_queue_stopped(tp->dev) &&
Ranjit Manomohan42952232006-10-18 20:54:26 -07004087 (tg3_tx_avail(tp) > TG3_TX_WAKEUP_THRESH(tp)))
Michael Chan51b91462005-09-01 17:41:28 -07004088 netif_wake_queue(tp->dev);
Michael Chan1b2a7202006-08-07 21:46:02 -07004089 netif_tx_unlock(tp->dev);
Michael Chan51b91462005-09-01 17:41:28 -07004090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091}
4092
4093/* Returns size of skb allocated or < 0 on error.
4094 *
4095 * We only need to fill in the address because the other members
4096 * of the RX descriptor are invariant, see tg3_init_rings.
4097 *
4098 * Note the purposeful assymetry of cpu vs. chip accesses. For
4099 * posting buffers we only dirty the first cache line of the RX
4100 * descriptor (containing the address). Whereas for the RX status
4101 * buffers the cpu only reads the last cacheline of the RX descriptor
4102 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4103 */
4104static int tg3_alloc_rx_skb(struct tg3 *tp, u32 opaque_key,
4105 int src_idx, u32 dest_idx_unmasked)
4106{
4107 struct tg3_rx_buffer_desc *desc;
4108 struct ring_info *map, *src_map;
4109 struct sk_buff *skb;
4110 dma_addr_t mapping;
4111 int skb_size, dest_idx;
4112
4113 src_map = NULL;
4114 switch (opaque_key) {
4115 case RXD_OPAQUE_RING_STD:
4116 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
4117 desc = &tp->rx_std[dest_idx];
4118 map = &tp->rx_std_buffers[dest_idx];
4119 if (src_idx >= 0)
4120 src_map = &tp->rx_std_buffers[src_idx];
Michael Chan7e72aad2005-07-25 12:31:17 -07004121 skb_size = tp->rx_pkt_buf_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 break;
4123
4124 case RXD_OPAQUE_RING_JUMBO:
4125 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
4126 desc = &tp->rx_jumbo[dest_idx];
4127 map = &tp->rx_jumbo_buffers[dest_idx];
4128 if (src_idx >= 0)
4129 src_map = &tp->rx_jumbo_buffers[src_idx];
4130 skb_size = RX_JUMBO_PKT_BUF_SZ;
4131 break;
4132
4133 default:
4134 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
4137 /* Do not overwrite any of the map or rp information
4138 * until we are sure we can commit to a new buffer.
4139 *
4140 * Callers depend upon this behavior and assume that
4141 * we leave everything unchanged if we fail.
4142 */
David S. Millera20e9c62006-07-31 22:38:16 -07004143 skb = netdev_alloc_skb(tp->dev, skb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 if (skb == NULL)
4145 return -ENOMEM;
4146
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 skb_reserve(skb, tp->rx_offset);
4148
4149 mapping = pci_map_single(tp->pdev, skb->data,
4150 skb_size - tp->rx_offset,
4151 PCI_DMA_FROMDEVICE);
4152
4153 map->skb = skb;
4154 pci_unmap_addr_set(map, mapping, mapping);
4155
4156 if (src_map != NULL)
4157 src_map->skb = NULL;
4158
4159 desc->addr_hi = ((u64)mapping >> 32);
4160 desc->addr_lo = ((u64)mapping & 0xffffffff);
4161
4162 return skb_size;
4163}
4164
4165/* We only need to move over in the address because the other
4166 * members of the RX descriptor are invariant. See notes above
4167 * tg3_alloc_rx_skb for full details.
4168 */
4169static void tg3_recycle_rx(struct tg3 *tp, u32 opaque_key,
4170 int src_idx, u32 dest_idx_unmasked)
4171{
4172 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4173 struct ring_info *src_map, *dest_map;
4174 int dest_idx;
4175
4176 switch (opaque_key) {
4177 case RXD_OPAQUE_RING_STD:
4178 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
4179 dest_desc = &tp->rx_std[dest_idx];
4180 dest_map = &tp->rx_std_buffers[dest_idx];
4181 src_desc = &tp->rx_std[src_idx];
4182 src_map = &tp->rx_std_buffers[src_idx];
4183 break;
4184
4185 case RXD_OPAQUE_RING_JUMBO:
4186 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
4187 dest_desc = &tp->rx_jumbo[dest_idx];
4188 dest_map = &tp->rx_jumbo_buffers[dest_idx];
4189 src_desc = &tp->rx_jumbo[src_idx];
4190 src_map = &tp->rx_jumbo_buffers[src_idx];
4191 break;
4192
4193 default:
4194 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
4197 dest_map->skb = src_map->skb;
4198 pci_unmap_addr_set(dest_map, mapping,
4199 pci_unmap_addr(src_map, mapping));
4200 dest_desc->addr_hi = src_desc->addr_hi;
4201 dest_desc->addr_lo = src_desc->addr_lo;
4202
4203 src_map->skb = NULL;
4204}
4205
4206#if TG3_VLAN_TAG_USED
4207static int tg3_vlan_rx(struct tg3 *tp, struct sk_buff *skb, u16 vlan_tag)
4208{
4209 return vlan_hwaccel_receive_skb(skb, tp->vlgrp, vlan_tag);
4210}
4211#endif
4212
4213/* The RX ring scheme is composed of multiple rings which post fresh
4214 * buffers to the chip, and one special ring the chip uses to report
4215 * status back to the host.
4216 *
4217 * The special ring reports the status of received packets to the
4218 * host. The chip does not write into the original descriptor the
4219 * RX buffer was obtained from. The chip simply takes the original
4220 * descriptor as provided by the host, updates the status and length
4221 * field, then writes this into the next status ring entry.
4222 *
4223 * Each ring the host uses to post buffers to the chip is described
4224 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4225 * it is first placed into the on-chip ram. When the packet's length
4226 * is known, it walks down the TG3_BDINFO entries to select the ring.
4227 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4228 * which is within the range of the new packet's length is chosen.
4229 *
4230 * The "separate ring for rx status" scheme may sound queer, but it makes
4231 * sense from a cache coherency perspective. If only the host writes
4232 * to the buffer post rings, and only the chip writes to the rx status
4233 * rings, then cache lines never move beyond shared-modified state.
4234 * If both the host and chip were to write into the same ring, cache line
4235 * eviction could occur since both entities want it in an exclusive state.
4236 */
4237static int tg3_rx(struct tg3 *tp, int budget)
4238{
Michael Chanf92905d2006-06-29 20:14:29 -07004239 u32 work_mask, rx_std_posted = 0;
Michael Chan483ba502005-04-25 15:14:03 -07004240 u32 sw_idx = tp->rx_rcb_ptr;
4241 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 int received;
4243
4244 hw_idx = tp->hw_status->idx[0].rx_producer;
4245 /*
4246 * We need to order the read of hw_idx and the read of
4247 * the opaque cookie.
4248 */
4249 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 work_mask = 0;
4251 received = 0;
4252 while (sw_idx != hw_idx && budget > 0) {
4253 struct tg3_rx_buffer_desc *desc = &tp->rx_rcb[sw_idx];
4254 unsigned int len;
4255 struct sk_buff *skb;
4256 dma_addr_t dma_addr;
4257 u32 opaque_key, desc_idx, *post_ptr;
4258
4259 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4260 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4261 if (opaque_key == RXD_OPAQUE_RING_STD) {
4262 dma_addr = pci_unmap_addr(&tp->rx_std_buffers[desc_idx],
4263 mapping);
4264 skb = tp->rx_std_buffers[desc_idx].skb;
4265 post_ptr = &tp->rx_std_ptr;
Michael Chanf92905d2006-06-29 20:14:29 -07004266 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
4268 dma_addr = pci_unmap_addr(&tp->rx_jumbo_buffers[desc_idx],
4269 mapping);
4270 skb = tp->rx_jumbo_buffers[desc_idx].skb;
4271 post_ptr = &tp->rx_jumbo_ptr;
4272 }
4273 else {
4274 goto next_pkt_nopost;
4275 }
4276
4277 work_mask |= opaque_key;
4278
4279 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4280 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4281 drop_it:
4282 tg3_recycle_rx(tp, opaque_key,
4283 desc_idx, *post_ptr);
4284 drop_it_no_recycle:
4285 /* Other statistics kept track of by card. */
4286 tp->net_stats.rx_dropped++;
4287 goto next_pkt;
4288 }
4289
Matt Carlsonad829262008-11-21 17:16:16 -08004290 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4291 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004293 if (len > RX_COPY_THRESHOLD
Matt Carlsonad829262008-11-21 17:16:16 -08004294 && tp->rx_offset == NET_IP_ALIGN
4295 /* rx_offset will likely not equal NET_IP_ALIGN
4296 * if this is a 5701 card running in PCI-X mode
4297 * [see tg3_get_invariants()]
4298 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 ) {
4300 int skb_size;
4301
4302 skb_size = tg3_alloc_rx_skb(tp, opaque_key,
4303 desc_idx, *post_ptr);
4304 if (skb_size < 0)
4305 goto drop_it;
4306
4307 pci_unmap_single(tp->pdev, dma_addr,
4308 skb_size - tp->rx_offset,
4309 PCI_DMA_FROMDEVICE);
4310
4311 skb_put(skb, len);
4312 } else {
4313 struct sk_buff *copy_skb;
4314
4315 tg3_recycle_rx(tp, opaque_key,
4316 desc_idx, *post_ptr);
4317
Matt Carlsonad829262008-11-21 17:16:16 -08004318 copy_skb = netdev_alloc_skb(tp->dev,
4319 len + TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 if (copy_skb == NULL)
4321 goto drop_it_no_recycle;
4322
Matt Carlsonad829262008-11-21 17:16:16 -08004323 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 skb_put(copy_skb, len);
4325 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004326 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4328
4329 /* We'll reuse the original ring buffer. */
4330 skb = copy_skb;
4331 }
4332
4333 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4334 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4335 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4336 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4337 skb->ip_summed = CHECKSUM_UNNECESSARY;
4338 else
4339 skb->ip_summed = CHECKSUM_NONE;
4340
4341 skb->protocol = eth_type_trans(skb, tp->dev);
4342#if TG3_VLAN_TAG_USED
4343 if (tp->vlgrp != NULL &&
4344 desc->type_flags & RXD_FLAG_VLAN) {
4345 tg3_vlan_rx(tp, skb,
4346 desc->err_vlan & RXD_VLAN_MASK);
4347 } else
4348#endif
4349 netif_receive_skb(skb);
4350
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 received++;
4352 budget--;
4353
4354next_pkt:
4355 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004356
4357 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
4358 u32 idx = *post_ptr % TG3_RX_RING_SIZE;
4359
4360 tw32_rx_mbox(MAILBOX_RCV_STD_PROD_IDX +
4361 TG3_64BIT_REG_LOW, idx);
4362 work_mask &= ~RXD_OPAQUE_RING_STD;
4363 rx_std_posted = 0;
4364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004366 sw_idx++;
Eric Dumazet6b31a512007-02-06 13:29:21 -08004367 sw_idx &= (TG3_RX_RCB_RING_SIZE(tp) - 1);
Michael Chan52f6d692005-04-25 15:14:32 -07004368
4369 /* Refresh hw_idx to see if there is new work */
4370 if (sw_idx == hw_idx) {
4371 hw_idx = tp->hw_status->idx[0].rx_producer;
4372 rmb();
4373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 }
4375
4376 /* ACK the status ring. */
Michael Chan483ba502005-04-25 15:14:03 -07004377 tp->rx_rcb_ptr = sw_idx;
4378 tw32_rx_mbox(MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
4380 /* Refill RX ring(s). */
4381 if (work_mask & RXD_OPAQUE_RING_STD) {
4382 sw_idx = tp->rx_std_ptr % TG3_RX_RING_SIZE;
4383 tw32_rx_mbox(MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW,
4384 sw_idx);
4385 }
4386 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
4387 sw_idx = tp->rx_jumbo_ptr % TG3_RX_JUMBO_RING_SIZE;
4388 tw32_rx_mbox(MAILBOX_RCV_JUMBO_PROD_IDX + TG3_64BIT_REG_LOW,
4389 sw_idx);
4390 }
4391 mmiowb();
4392
4393 return received;
4394}
4395
David S. Miller6f535762007-10-11 18:08:29 -07004396static int tg3_poll_work(struct tg3 *tp, int work_done, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 struct tg3_hw_status *sblk = tp->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 /* handle link change and other phy events */
4401 if (!(tp->tg3_flags &
4402 (TG3_FLAG_USE_LINKCHG_REG |
4403 TG3_FLAG_POLL_SERDES))) {
4404 if (sblk->status & SD_STATUS_LINK_CHG) {
4405 sblk->status = SD_STATUS_UPDATED |
4406 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004407 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004408 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4409 tw32_f(MAC_STATUS,
4410 (MAC_STATUS_SYNC_CHANGED |
4411 MAC_STATUS_CFG_CHANGED |
4412 MAC_STATUS_MI_COMPLETION |
4413 MAC_STATUS_LNKSTATE_CHANGED));
4414 udelay(40);
4415 } else
4416 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004417 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 }
4419 }
4420
4421 /* run TX completion thread */
4422 if (sblk->idx[0].tx_consumer != tp->tx_cons) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 tg3_tx(tp);
David S. Miller6f535762007-10-11 18:08:29 -07004424 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07004425 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 }
4427
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 /* run RX thread, within the bounds set by NAPI.
4429 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004430 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004432 if (sblk->idx[0].rx_producer != tp->rx_rcb_ptr)
David S. Miller6f535762007-10-11 18:08:29 -07004433 work_done += tg3_rx(tp, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
David S. Miller6f535762007-10-11 18:08:29 -07004435 return work_done;
4436}
David S. Millerf7383c22005-05-18 22:50:53 -07004437
David S. Miller6f535762007-10-11 18:08:29 -07004438static int tg3_poll(struct napi_struct *napi, int budget)
4439{
4440 struct tg3 *tp = container_of(napi, struct tg3, napi);
4441 int work_done = 0;
Michael Chan4fd7ab52007-10-12 01:39:50 -07004442 struct tg3_hw_status *sblk = tp->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07004443
4444 while (1) {
4445 work_done = tg3_poll_work(tp, work_done, budget);
4446
4447 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
4448 goto tx_recovery;
4449
4450 if (unlikely(work_done >= budget))
4451 break;
4452
Michael Chan4fd7ab52007-10-12 01:39:50 -07004453 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
4454 /* tp->last_tag is used in tg3_restart_ints() below
4455 * to tell the hw how much work has been processed,
4456 * so we must read it before checking for more work.
4457 */
4458 tp->last_tag = sblk->status_tag;
4459 rmb();
4460 } else
4461 sblk->status &= ~SD_STATUS_UPDATED;
4462
David S. Miller6f535762007-10-11 18:08:29 -07004463 if (likely(!tg3_has_work(tp))) {
Neil Horman908a7a12008-12-22 20:43:12 -08004464 netif_rx_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07004465 tg3_restart_ints(tp);
4466 break;
4467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 }
4469
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004470 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07004471
4472tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07004473 /* work_done is guaranteed to be less than budget. */
Neil Horman908a7a12008-12-22 20:43:12 -08004474 netif_rx_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07004475 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07004476 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477}
4478
David S. Millerf47c11e2005-06-24 20:18:35 -07004479static void tg3_irq_quiesce(struct tg3 *tp)
4480{
4481 BUG_ON(tp->irq_sync);
4482
4483 tp->irq_sync = 1;
4484 smp_mb();
4485
4486 synchronize_irq(tp->pdev->irq);
4487}
4488
4489static inline int tg3_irq_sync(struct tg3 *tp)
4490{
4491 return tp->irq_sync;
4492}
4493
4494/* Fully shutdown all tg3 driver activity elsewhere in the system.
4495 * If irq_sync is non-zero, then the IRQ handler must be synchronized
4496 * with as well. Most of the time, this is not necessary except when
4497 * shutting down the device.
4498 */
4499static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
4500{
Michael Chan46966542007-07-11 19:47:19 -07004501 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07004502 if (irq_sync)
4503 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07004504}
4505
4506static inline void tg3_full_unlock(struct tg3 *tp)
4507{
David S. Millerf47c11e2005-06-24 20:18:35 -07004508 spin_unlock_bh(&tp->lock);
4509}
4510
Michael Chanfcfa0a32006-03-20 22:28:41 -08004511/* One-shot MSI handler - Chip automatically disables interrupt
4512 * after sending MSI so driver doesn't have to do it.
4513 */
David Howells7d12e782006-10-05 14:55:46 +01004514static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08004515{
4516 struct net_device *dev = dev_id;
4517 struct tg3 *tp = netdev_priv(dev);
4518
4519 prefetch(tp->hw_status);
4520 prefetch(&tp->rx_rcb[tp->rx_rcb_ptr]);
4521
4522 if (likely(!tg3_irq_sync(tp)))
Neil Horman908a7a12008-12-22 20:43:12 -08004523 netif_rx_schedule(&tp->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08004524
4525 return IRQ_HANDLED;
4526}
4527
Michael Chan88b06bc22005-04-21 17:13:25 -07004528/* MSI ISR - No need to check for interrupt sharing and no need to
4529 * flush status block and interrupt mailbox. PCI ordering rules
4530 * guarantee that MSI will arrive after the status block.
4531 */
David Howells7d12e782006-10-05 14:55:46 +01004532static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07004533{
4534 struct net_device *dev = dev_id;
4535 struct tg3 *tp = netdev_priv(dev);
Michael Chan88b06bc22005-04-21 17:13:25 -07004536
Michael Chan61487482005-09-05 17:53:19 -07004537 prefetch(tp->hw_status);
4538 prefetch(&tp->rx_rcb[tp->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07004539 /*
David S. Millerfac9b832005-05-18 22:46:34 -07004540 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07004541 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07004542 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07004543 * NIC to stop sending us irqs, engaging "in-intr-handler"
4544 * event coalescing.
4545 */
4546 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07004547 if (likely(!tg3_irq_sync(tp)))
Neil Horman908a7a12008-12-22 20:43:12 -08004548 netif_rx_schedule(&tp->napi);
Michael Chan61487482005-09-05 17:53:19 -07004549
Michael Chan88b06bc22005-04-21 17:13:25 -07004550 return IRQ_RETVAL(1);
4551}
4552
David Howells7d12e782006-10-05 14:55:46 +01004553static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554{
4555 struct net_device *dev = dev_id;
4556 struct tg3 *tp = netdev_priv(dev);
4557 struct tg3_hw_status *sblk = tp->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 unsigned int handled = 1;
4559
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 /* In INTx mode, it is possible for the interrupt to arrive at
4561 * the CPU before the status block posted prior to the interrupt.
4562 * Reading the PCI State register will confirm whether the
4563 * interrupt is ours and will flush the status block.
4564 */
Michael Chand18edcb2007-03-24 20:57:11 -07004565 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
4566 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
4567 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
4568 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07004569 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07004570 }
Michael Chand18edcb2007-03-24 20:57:11 -07004571 }
4572
4573 /*
4574 * Writing any value to intr-mbox-0 clears PCI INTA# and
4575 * chip-internal interrupt pending events.
4576 * Writing non-zero to intr-mbox-0 additional tells the
4577 * NIC to stop sending us irqs, engaging "in-intr-handler"
4578 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07004579 *
4580 * Flush the mailbox to de-assert the IRQ immediately to prevent
4581 * spurious interrupts. The flush impacts performance but
4582 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07004583 */
Michael Chanc04cb342007-05-07 00:26:15 -07004584 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07004585 if (tg3_irq_sync(tp))
4586 goto out;
4587 sblk->status &= ~SD_STATUS_UPDATED;
4588 if (likely(tg3_has_work(tp))) {
4589 prefetch(&tp->rx_rcb[tp->rx_rcb_ptr]);
Neil Horman908a7a12008-12-22 20:43:12 -08004590 netif_rx_schedule(&tp->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07004591 } else {
4592 /* No work, shared interrupt perhaps? re-enable
4593 * interrupts, and flush that PCI write
4594 */
4595 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
4596 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07004597 }
David S. Millerf47c11e2005-06-24 20:18:35 -07004598out:
David S. Millerfac9b832005-05-18 22:46:34 -07004599 return IRQ_RETVAL(handled);
4600}
4601
David Howells7d12e782006-10-05 14:55:46 +01004602static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07004603{
4604 struct net_device *dev = dev_id;
4605 struct tg3 *tp = netdev_priv(dev);
4606 struct tg3_hw_status *sblk = tp->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07004607 unsigned int handled = 1;
4608
David S. Millerfac9b832005-05-18 22:46:34 -07004609 /* In INTx mode, it is possible for the interrupt to arrive at
4610 * the CPU before the status block posted prior to the interrupt.
4611 * Reading the PCI State register will confirm whether the
4612 * interrupt is ours and will flush the status block.
4613 */
Michael Chand18edcb2007-03-24 20:57:11 -07004614 if (unlikely(sblk->status_tag == tp->last_tag)) {
4615 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
4616 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
4617 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07004618 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 }
Michael Chand18edcb2007-03-24 20:57:11 -07004620 }
4621
4622 /*
4623 * writing any value to intr-mbox-0 clears PCI INTA# and
4624 * chip-internal interrupt pending events.
4625 * writing non-zero to intr-mbox-0 additional tells the
4626 * NIC to stop sending us irqs, engaging "in-intr-handler"
4627 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07004628 *
4629 * Flush the mailbox to de-assert the IRQ immediately to prevent
4630 * spurious interrupts. The flush impacts performance but
4631 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07004632 */
Michael Chanc04cb342007-05-07 00:26:15 -07004633 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07004634 if (tg3_irq_sync(tp))
4635 goto out;
Neil Horman908a7a12008-12-22 20:43:12 -08004636 if (netif_rx_schedule_prep(&tp->napi)) {
Michael Chand18edcb2007-03-24 20:57:11 -07004637 prefetch(&tp->rx_rcb[tp->rx_rcb_ptr]);
4638 /* Update last_tag to mark that this status has been
4639 * seen. Because interrupt may be shared, we may be
4640 * racing with tg3_poll(), so only update last_tag
4641 * if tg3_poll() is not scheduled.
4642 */
4643 tp->last_tag = sblk->status_tag;
Neil Horman908a7a12008-12-22 20:43:12 -08004644 __netif_rx_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 }
David S. Millerf47c11e2005-06-24 20:18:35 -07004646out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 return IRQ_RETVAL(handled);
4648}
4649
Michael Chan79381092005-04-21 17:13:59 -07004650/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01004651static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07004652{
4653 struct net_device *dev = dev_id;
4654 struct tg3 *tp = netdev_priv(dev);
4655 struct tg3_hw_status *sblk = tp->hw_status;
4656
Michael Chanf9804dd2005-09-27 12:13:10 -07004657 if ((sblk->status & SD_STATUS_UPDATED) ||
4658 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07004659 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07004660 return IRQ_RETVAL(1);
4661 }
4662 return IRQ_RETVAL(0);
4663}
4664
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07004665static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07004666static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Michael Chanb9ec6c12006-07-25 16:37:27 -07004668/* Restart hardware after configuration changes, self-test, etc.
4669 * Invoked with tp->lock held.
4670 */
4671static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07004672 __releases(tp->lock)
4673 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07004674{
4675 int err;
4676
4677 err = tg3_init_hw(tp, reset_phy);
4678 if (err) {
4679 printk(KERN_ERR PFX "%s: Failed to re-initialize device, "
4680 "aborting.\n", tp->dev->name);
4681 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
4682 tg3_full_unlock(tp);
4683 del_timer_sync(&tp->timer);
4684 tp->irq_sync = 0;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004685 napi_enable(&tp->napi);
Michael Chanb9ec6c12006-07-25 16:37:27 -07004686 dev_close(tp->dev);
4687 tg3_full_lock(tp, 0);
4688 }
4689 return err;
4690}
4691
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692#ifdef CONFIG_NET_POLL_CONTROLLER
4693static void tg3_poll_controller(struct net_device *dev)
4694{
Michael Chan88b06bc22005-04-21 17:13:25 -07004695 struct tg3 *tp = netdev_priv(dev);
4696
David Howells7d12e782006-10-05 14:55:46 +01004697 tg3_interrupt(tp->pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698}
4699#endif
4700
David Howellsc4028952006-11-22 14:57:56 +00004701static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702{
David Howellsc4028952006-11-22 14:57:56 +00004703 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004704 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 unsigned int restart_timer;
4706
Michael Chan7faa0062006-02-02 17:29:28 -08004707 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08004708
4709 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08004710 tg3_full_unlock(tp);
4711 return;
4712 }
4713
4714 tg3_full_unlock(tp);
4715
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004716 tg3_phy_stop(tp);
4717
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 tg3_netif_stop(tp);
4719
David S. Millerf47c11e2005-06-24 20:18:35 -07004720 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721
4722 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
4723 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
4724
Michael Chandf3e6542006-05-26 17:48:07 -07004725 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
4726 tp->write32_tx_mbox = tg3_write32_tx_mbox;
4727 tp->write32_rx_mbox = tg3_write_flush_reg32;
4728 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
4729 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
4730 }
4731
Michael Chan944d9802005-05-29 14:57:48 -07004732 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004733 err = tg3_init_hw(tp, 1);
4734 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07004735 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
4737 tg3_netif_start(tp);
4738
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 if (restart_timer)
4740 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08004741
Michael Chanb9ec6c12006-07-25 16:37:27 -07004742out:
Michael Chan7faa0062006-02-02 17:29:28 -08004743 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004744
4745 if (!err)
4746 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747}
4748
Michael Chanb0408752007-02-13 12:18:30 -08004749static void tg3_dump_short_state(struct tg3 *tp)
4750{
4751 printk(KERN_ERR PFX "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
4752 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
4753 printk(KERN_ERR PFX "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
4754 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
4755}
4756
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757static void tg3_tx_timeout(struct net_device *dev)
4758{
4759 struct tg3 *tp = netdev_priv(dev);
4760
Michael Chanb0408752007-02-13 12:18:30 -08004761 if (netif_msg_tx_err(tp)) {
Michael Chan9f88f292006-12-07 00:22:54 -08004762 printk(KERN_ERR PFX "%s: transmit timed out, resetting\n",
4763 dev->name);
Michael Chanb0408752007-02-13 12:18:30 -08004764 tg3_dump_short_state(tp);
4765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
4767 schedule_work(&tp->reset_task);
4768}
4769
Michael Chanc58ec932005-09-17 00:46:27 -07004770/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
4771static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
4772{
4773 u32 base = (u32) mapping & 0xffffffff;
4774
4775 return ((base > 0xffffdcc0) &&
4776 (base + len + 8 < base));
4777}
4778
Michael Chan72f2afb2006-03-06 19:28:35 -08004779/* Test for DMA addresses > 40-bit */
4780static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
4781 int len)
4782{
4783#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08004784 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Michael Chan72f2afb2006-03-06 19:28:35 -08004785 return (((u64) mapping + len) > DMA_40BIT_MASK);
4786 return 0;
4787#else
4788 return 0;
4789#endif
4790}
4791
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792static void tg3_set_txd(struct tg3 *, int, dma_addr_t, int, u32, u32);
4793
Michael Chan72f2afb2006-03-06 19:28:35 -08004794/* Workaround 4GB and 40-bit hardware DMA bugs. */
4795static int tigon3_dma_hwbug_workaround(struct tg3 *tp, struct sk_buff *skb,
Michael Chanc58ec932005-09-17 00:46:27 -07004796 u32 last_plus_one, u32 *start,
4797 u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798{
Matt Carlson41588ba2008-04-19 18:12:33 -07004799 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07004800 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07004802 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
Matt Carlson41588ba2008-04-19 18:12:33 -07004804 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
4805 new_skb = skb_copy(skb, GFP_ATOMIC);
4806 else {
4807 int more_headroom = 4 - ((unsigned long)skb->data & 3);
4808
4809 new_skb = skb_copy_expand(skb,
4810 skb_headroom(skb) + more_headroom,
4811 skb_tailroom(skb), GFP_ATOMIC);
4812 }
4813
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07004815 ret = -1;
4816 } else {
4817 /* New SKB is guaranteed to be linear. */
4818 entry = *start;
David S. Miller90079ce2008-09-11 04:52:51 -07004819 ret = skb_dma_map(&tp->pdev->dev, new_skb, DMA_TO_DEVICE);
4820 new_addr = skb_shinfo(new_skb)->dma_maps[0];
4821
Michael Chanc58ec932005-09-17 00:46:27 -07004822 /* Make sure new skb does not cross any 4G boundaries.
4823 * Drop the packet if it does.
4824 */
David S. Miller90079ce2008-09-11 04:52:51 -07004825 if (ret || tg3_4g_overflow_test(new_addr, new_skb->len)) {
David S. Miller638266f2008-09-11 15:45:19 -07004826 if (!ret)
4827 skb_dma_unmap(&tp->pdev->dev, new_skb,
4828 DMA_TO_DEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07004829 ret = -1;
4830 dev_kfree_skb(new_skb);
4831 new_skb = NULL;
4832 } else {
4833 tg3_set_txd(tp, entry, new_addr, new_skb->len,
4834 base_flags, 1 | (mss << 1));
4835 *start = NEXT_TX(entry);
4836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 }
4838
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 /* Now clean up the sw ring entries. */
4840 i = 0;
4841 while (entry != last_plus_one) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 if (i == 0) {
4843 tp->tx_buffers[entry].skb = new_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 } else {
4845 tp->tx_buffers[entry].skb = NULL;
4846 }
4847 entry = NEXT_TX(entry);
4848 i++;
4849 }
4850
David S. Miller90079ce2008-09-11 04:52:51 -07004851 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 dev_kfree_skb(skb);
4853
Michael Chanc58ec932005-09-17 00:46:27 -07004854 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855}
4856
4857static void tg3_set_txd(struct tg3 *tp, int entry,
4858 dma_addr_t mapping, int len, u32 flags,
4859 u32 mss_and_is_end)
4860{
4861 struct tg3_tx_buffer_desc *txd = &tp->tx_ring[entry];
4862 int is_end = (mss_and_is_end & 0x1);
4863 u32 mss = (mss_and_is_end >> 1);
4864 u32 vlan_tag = 0;
4865
4866 if (is_end)
4867 flags |= TXD_FLAG_END;
4868 if (flags & TXD_FLAG_VLAN) {
4869 vlan_tag = flags >> 16;
4870 flags &= 0xffff;
4871 }
4872 vlan_tag |= (mss << TXD_MSS_SHIFT);
4873
4874 txd->addr_hi = ((u64) mapping >> 32);
4875 txd->addr_lo = ((u64) mapping & 0xffffffff);
4876 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
4877 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
4878}
4879
Michael Chan5a6f3072006-03-20 22:28:05 -08004880/* hard_start_xmit for devices that don't have any bugs and
4881 * support TG3_FLG2_HW_TSO_2 only.
4882 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883static int tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
4884{
4885 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07004887 struct skb_shared_info *sp;
4888 dma_addr_t mapping;
Michael Chan5a6f3072006-03-20 22:28:05 -08004889
4890 len = skb_headlen(skb);
4891
Michael Chan00b70502006-06-17 21:58:45 -07004892 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004893 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08004894 * interrupt. Furthermore, IRQ processing runs lockless so we have
4895 * no IRQ context deadlocks to worry about either. Rejoice!
4896 */
Michael Chan1b2a7202006-08-07 21:46:02 -07004897 if (unlikely(tg3_tx_avail(tp) <= (skb_shinfo(skb)->nr_frags + 1))) {
Michael Chan5a6f3072006-03-20 22:28:05 -08004898 if (!netif_queue_stopped(dev)) {
4899 netif_stop_queue(dev);
4900
4901 /* This is a hard error, log it. */
4902 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
4903 "queue awake!\n", dev->name);
4904 }
Michael Chan5a6f3072006-03-20 22:28:05 -08004905 return NETDEV_TX_BUSY;
4906 }
4907
4908 entry = tp->tx_prod;
4909 base_flags = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08004910 mss = 0;
Matt Carlsonc13e3712007-05-05 11:50:04 -07004911 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08004912 int tcp_opt_len, ip_tcp_len;
4913
4914 if (skb_header_cloned(skb) &&
4915 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
4916 dev_kfree_skb(skb);
4917 goto out_unlock;
4918 }
4919
Michael Chanb0026622006-07-03 19:42:14 -07004920 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
4921 mss |= (skb_headlen(skb) - ETH_HLEN) << 9;
4922 else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07004923 struct iphdr *iph = ip_hdr(skb);
4924
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07004925 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03004926 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07004927
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07004928 iph->check = 0;
4929 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Michael Chanb0026622006-07-03 19:42:14 -07004930 mss |= (ip_tcp_len + tcp_opt_len) << 9;
4931 }
Michael Chan5a6f3072006-03-20 22:28:05 -08004932
4933 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
4934 TXD_FLAG_CPU_POST_DMA);
4935
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07004936 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08004937
Michael Chan5a6f3072006-03-20 22:28:05 -08004938 }
Patrick McHardy84fa7932006-08-29 16:44:56 -07004939 else if (skb->ip_summed == CHECKSUM_PARTIAL)
Michael Chan5a6f3072006-03-20 22:28:05 -08004940 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Michael Chan5a6f3072006-03-20 22:28:05 -08004941#if TG3_VLAN_TAG_USED
4942 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
4943 base_flags |= (TXD_FLAG_VLAN |
4944 (vlan_tx_tag_get(skb) << 16));
4945#endif
4946
David S. Miller90079ce2008-09-11 04:52:51 -07004947 if (skb_dma_map(&tp->pdev->dev, skb, DMA_TO_DEVICE)) {
4948 dev_kfree_skb(skb);
4949 goto out_unlock;
4950 }
4951
4952 sp = skb_shinfo(skb);
4953
4954 mapping = sp->dma_maps[0];
Michael Chan5a6f3072006-03-20 22:28:05 -08004955
4956 tp->tx_buffers[entry].skb = skb;
Michael Chan5a6f3072006-03-20 22:28:05 -08004957
4958 tg3_set_txd(tp, entry, mapping, len, base_flags,
4959 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
4960
4961 entry = NEXT_TX(entry);
4962
4963 /* Now loop through additional data fragments, and queue them. */
4964 if (skb_shinfo(skb)->nr_frags > 0) {
4965 unsigned int i, last;
4966
4967 last = skb_shinfo(skb)->nr_frags - 1;
4968 for (i = 0; i <= last; i++) {
4969 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4970
4971 len = frag->size;
David S. Miller90079ce2008-09-11 04:52:51 -07004972 mapping = sp->dma_maps[i + 1];
Michael Chan5a6f3072006-03-20 22:28:05 -08004973 tp->tx_buffers[entry].skb = NULL;
Michael Chan5a6f3072006-03-20 22:28:05 -08004974
4975 tg3_set_txd(tp, entry, mapping, len,
4976 base_flags, (i == last) | (mss << 1));
4977
4978 entry = NEXT_TX(entry);
4979 }
4980 }
4981
4982 /* Packets are ready, update Tx producer idx local and on card. */
4983 tw32_tx_mbox((MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW), entry);
4984
4985 tp->tx_prod = entry;
Michael Chan1b2a7202006-08-07 21:46:02 -07004986 if (unlikely(tg3_tx_avail(tp) <= (MAX_SKB_FRAGS + 1))) {
Michael Chan5a6f3072006-03-20 22:28:05 -08004987 netif_stop_queue(dev);
Ranjit Manomohan42952232006-10-18 20:54:26 -07004988 if (tg3_tx_avail(tp) > TG3_TX_WAKEUP_THRESH(tp))
Michael Chan5a6f3072006-03-20 22:28:05 -08004989 netif_wake_queue(tp->dev);
4990 }
4991
4992out_unlock:
4993 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08004994
4995 dev->trans_start = jiffies;
4996
4997 return NETDEV_TX_OK;
4998}
4999
Michael Chan52c0fd82006-06-29 20:15:54 -07005000static int tg3_start_xmit_dma_bug(struct sk_buff *, struct net_device *);
5001
5002/* Use GSO to workaround a rare TSO bug that may be triggered when the
5003 * TSO header is greater than 80 bytes.
5004 */
5005static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5006{
5007 struct sk_buff *segs, *nskb;
5008
5009 /* Estimate the number of fragments in the worst case */
Michael Chan1b2a7202006-08-07 21:46:02 -07005010 if (unlikely(tg3_tx_avail(tp) <= (skb_shinfo(skb)->gso_segs * 3))) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005011 netif_stop_queue(tp->dev);
Michael Chan7f62ad52007-02-20 23:25:40 -08005012 if (tg3_tx_avail(tp) <= (skb_shinfo(skb)->gso_segs * 3))
5013 return NETDEV_TX_BUSY;
5014
5015 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005016 }
5017
5018 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005019 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005020 goto tg3_tso_bug_end;
5021
5022 do {
5023 nskb = segs;
5024 segs = segs->next;
5025 nskb->next = NULL;
5026 tg3_start_xmit_dma_bug(nskb, tp->dev);
5027 } while (segs);
5028
5029tg3_tso_bug_end:
5030 dev_kfree_skb(skb);
5031
5032 return NETDEV_TX_OK;
5033}
Michael Chan52c0fd82006-06-29 20:15:54 -07005034
Michael Chan5a6f3072006-03-20 22:28:05 -08005035/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5036 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5037 */
5038static int tg3_start_xmit_dma_bug(struct sk_buff *skb, struct net_device *dev)
5039{
5040 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005041 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005042 struct skb_shared_info *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005044 dma_addr_t mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
5046 len = skb_headlen(skb);
5047
Michael Chan00b70502006-06-17 21:58:45 -07005048 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005049 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005050 * interrupt. Furthermore, IRQ processing runs lockless so we have
5051 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 */
Michael Chan1b2a7202006-08-07 21:46:02 -07005053 if (unlikely(tg3_tx_avail(tp) <= (skb_shinfo(skb)->nr_frags + 1))) {
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005054 if (!netif_queue_stopped(dev)) {
5055 netif_stop_queue(dev);
5056
5057 /* This is a hard error, log it. */
5058 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
5059 "queue awake!\n", dev->name);
5060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 return NETDEV_TX_BUSY;
5062 }
5063
5064 entry = tp->tx_prod;
5065 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005066 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 mss = 0;
Matt Carlsonc13e3712007-05-05 11:50:04 -07005069 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005070 struct iphdr *iph;
Michael Chan52c0fd82006-06-29 20:15:54 -07005071 int tcp_opt_len, ip_tcp_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
5073 if (skb_header_cloned(skb) &&
5074 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5075 dev_kfree_skb(skb);
5076 goto out_unlock;
5077 }
5078
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005079 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005080 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081
Michael Chan52c0fd82006-06-29 20:15:54 -07005082 hdr_len = ip_tcp_len + tcp_opt_len;
5083 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005084 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Michael Chan52c0fd82006-06-29 20:15:54 -07005085 return (tg3_tso_bug(tp, skb));
5086
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5088 TXD_FLAG_CPU_POST_DMA);
5089
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005090 iph = ip_hdr(skb);
5091 iph->check = 0;
5092 iph->tot_len = htons(mss + hdr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005094 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005096 } else
5097 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5098 iph->daddr, 0,
5099 IPPROTO_TCP,
5100 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
5102 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
5103 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005104 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 int tsflags;
5106
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005107 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 mss |= (tsflags << 11);
5109 }
5110 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005111 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 int tsflags;
5113
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005114 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 base_flags |= tsflags << 12;
5116 }
5117 }
5118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119#if TG3_VLAN_TAG_USED
5120 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5121 base_flags |= (TXD_FLAG_VLAN |
5122 (vlan_tx_tag_get(skb) << 16));
5123#endif
5124
David S. Miller90079ce2008-09-11 04:52:51 -07005125 if (skb_dma_map(&tp->pdev->dev, skb, DMA_TO_DEVICE)) {
5126 dev_kfree_skb(skb);
5127 goto out_unlock;
5128 }
5129
5130 sp = skb_shinfo(skb);
5131
5132 mapping = sp->dma_maps[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133
5134 tp->tx_buffers[entry].skb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135
5136 would_hit_hwbug = 0;
5137
Matt Carlson41588ba2008-04-19 18:12:33 -07005138 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
5139 would_hit_hwbug = 1;
5140 else if (tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07005141 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142
5143 tg3_set_txd(tp, entry, mapping, len, base_flags,
5144 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5145
5146 entry = NEXT_TX(entry);
5147
5148 /* Now loop through additional data fragments, and queue them. */
5149 if (skb_shinfo(skb)->nr_frags > 0) {
5150 unsigned int i, last;
5151
5152 last = skb_shinfo(skb)->nr_frags - 1;
5153 for (i = 0; i <= last; i++) {
5154 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5155
5156 len = frag->size;
David S. Miller90079ce2008-09-11 04:52:51 -07005157 mapping = sp->dma_maps[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158
5159 tp->tx_buffers[entry].skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160
Michael Chanc58ec932005-09-17 00:46:27 -07005161 if (tg3_4g_overflow_test(mapping, len))
5162 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163
Michael Chan72f2afb2006-03-06 19:28:35 -08005164 if (tg3_40bit_overflow_test(tp, mapping, len))
5165 would_hit_hwbug = 1;
5166
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
5168 tg3_set_txd(tp, entry, mapping, len,
5169 base_flags, (i == last)|(mss << 1));
5170 else
5171 tg3_set_txd(tp, entry, mapping, len,
5172 base_flags, (i == last));
5173
5174 entry = NEXT_TX(entry);
5175 }
5176 }
5177
5178 if (would_hit_hwbug) {
5179 u32 last_plus_one = entry;
5180 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181
Michael Chanc58ec932005-09-17 00:46:27 -07005182 start = entry - 1 - skb_shinfo(skb)->nr_frags;
5183 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184
5185 /* If the workaround fails due to memory/mapping
5186 * failure, silently drop this packet.
5187 */
Michael Chan72f2afb2006-03-06 19:28:35 -08005188 if (tigon3_dma_hwbug_workaround(tp, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07005189 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 goto out_unlock;
5191
5192 entry = start;
5193 }
5194
5195 /* Packets are ready, update Tx producer idx local and on card. */
5196 tw32_tx_mbox((MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW), entry);
5197
5198 tp->tx_prod = entry;
Michael Chan1b2a7202006-08-07 21:46:02 -07005199 if (unlikely(tg3_tx_avail(tp) <= (MAX_SKB_FRAGS + 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 netif_stop_queue(dev);
Ranjit Manomohan42952232006-10-18 20:54:26 -07005201 if (tg3_tx_avail(tp) > TG3_TX_WAKEUP_THRESH(tp))
Michael Chan51b91462005-09-01 17:41:28 -07005202 netif_wake_queue(tp->dev);
5203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204
5205out_unlock:
5206 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207
5208 dev->trans_start = jiffies;
5209
5210 return NETDEV_TX_OK;
5211}
5212
5213static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
5214 int new_mtu)
5215{
5216 dev->mtu = new_mtu;
5217
Michael Chanef7f5ec2005-07-25 12:32:25 -07005218 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07005219 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005220 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
5221 ethtool_op_set_tso(dev, 0);
5222 }
5223 else
5224 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
5225 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07005226 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07005227 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07005228 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07005229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230}
5231
5232static int tg3_change_mtu(struct net_device *dev, int new_mtu)
5233{
5234 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005235 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236
5237 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
5238 return -EINVAL;
5239
5240 if (!netif_running(dev)) {
5241 /* We'll just catch it later when the
5242 * device is up'd.
5243 */
5244 tg3_set_mtu(dev, tp, new_mtu);
5245 return 0;
5246 }
5247
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005248 tg3_phy_stop(tp);
5249
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005251
5252 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253
Michael Chan944d9802005-05-29 14:57:48 -07005254 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
5256 tg3_set_mtu(dev, tp, new_mtu);
5257
Michael Chanb9ec6c12006-07-25 16:37:27 -07005258 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259
Michael Chanb9ec6c12006-07-25 16:37:27 -07005260 if (!err)
5261 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262
David S. Millerf47c11e2005-06-24 20:18:35 -07005263 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005265 if (!err)
5266 tg3_phy_start(tp);
5267
Michael Chanb9ec6c12006-07-25 16:37:27 -07005268 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269}
5270
5271/* Free up pending packets in all rx/tx rings.
5272 *
5273 * The chip has been shut down and the driver detached from
5274 * the networking, so no interrupts or new tx packets will
5275 * end up in the driver. tp->{tx,}lock is not held and we are not
5276 * in an interrupt context and thus may sleep.
5277 */
5278static void tg3_free_rings(struct tg3 *tp)
5279{
5280 struct ring_info *rxp;
5281 int i;
5282
5283 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
5284 rxp = &tp->rx_std_buffers[i];
5285
5286 if (rxp->skb == NULL)
5287 continue;
5288 pci_unmap_single(tp->pdev,
5289 pci_unmap_addr(rxp, mapping),
Michael Chan7e72aad2005-07-25 12:31:17 -07005290 tp->rx_pkt_buf_sz - tp->rx_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 PCI_DMA_FROMDEVICE);
5292 dev_kfree_skb_any(rxp->skb);
5293 rxp->skb = NULL;
5294 }
5295
5296 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
5297 rxp = &tp->rx_jumbo_buffers[i];
5298
5299 if (rxp->skb == NULL)
5300 continue;
5301 pci_unmap_single(tp->pdev,
5302 pci_unmap_addr(rxp, mapping),
5303 RX_JUMBO_PKT_BUF_SZ - tp->rx_offset,
5304 PCI_DMA_FROMDEVICE);
5305 dev_kfree_skb_any(rxp->skb);
5306 rxp->skb = NULL;
5307 }
5308
5309 for (i = 0; i < TG3_TX_RING_SIZE; ) {
5310 struct tx_ring_info *txp;
5311 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312
5313 txp = &tp->tx_buffers[i];
5314 skb = txp->skb;
5315
5316 if (skb == NULL) {
5317 i++;
5318 continue;
5319 }
5320
David S. Miller90079ce2008-09-11 04:52:51 -07005321 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
5322
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 txp->skb = NULL;
5324
David S. Miller90079ce2008-09-11 04:52:51 -07005325 i += skb_shinfo(skb)->nr_frags + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326
5327 dev_kfree_skb_any(skb);
5328 }
5329}
5330
5331/* Initialize tx/rx rings for packet processing.
5332 *
5333 * The chip has been shut down and the driver detached from
5334 * the networking, so no interrupts or new tx packets will
5335 * end up in the driver. tp->{tx,}lock are held and thus
5336 * we may not sleep.
5337 */
Michael Chan32d8c572006-07-25 16:38:29 -07005338static int tg3_init_rings(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339{
5340 u32 i;
5341
5342 /* Free up all the SKBs. */
5343 tg3_free_rings(tp);
5344
5345 /* Zero out all descriptors. */
5346 memset(tp->rx_std, 0, TG3_RX_RING_BYTES);
5347 memset(tp->rx_jumbo, 0, TG3_RX_JUMBO_RING_BYTES);
5348 memset(tp->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
5349 memset(tp->tx_ring, 0, TG3_TX_RING_BYTES);
5350
Michael Chan7e72aad2005-07-25 12:31:17 -07005351 tp->rx_pkt_buf_sz = RX_PKT_BUF_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07005352 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Michael Chan7e72aad2005-07-25 12:31:17 -07005353 (tp->dev->mtu > ETH_DATA_LEN))
5354 tp->rx_pkt_buf_sz = RX_JUMBO_PKT_BUF_SZ;
5355
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 /* Initialize invariants of the rings, we only set this
5357 * stuff once. This works because the card does not
5358 * write into the rx buffer posting rings.
5359 */
5360 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
5361 struct tg3_rx_buffer_desc *rxd;
5362
5363 rxd = &tp->rx_std[i];
Michael Chan7e72aad2005-07-25 12:31:17 -07005364 rxd->idx_len = (tp->rx_pkt_buf_sz - tp->rx_offset - 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 << RXD_LEN_SHIFT;
5366 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
5367 rxd->opaque = (RXD_OPAQUE_RING_STD |
5368 (i << RXD_OPAQUE_INDEX_SHIFT));
5369 }
5370
Michael Chan0f893dc2005-07-25 12:30:38 -07005371 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
5373 struct tg3_rx_buffer_desc *rxd;
5374
5375 rxd = &tp->rx_jumbo[i];
5376 rxd->idx_len = (RX_JUMBO_PKT_BUF_SZ - tp->rx_offset - 64)
5377 << RXD_LEN_SHIFT;
5378 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
5379 RXD_FLAG_JUMBO;
5380 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
5381 (i << RXD_OPAQUE_INDEX_SHIFT));
5382 }
5383 }
5384
5385 /* Now allocate fresh SKBs for each rx ring. */
5386 for (i = 0; i < tp->rx_pending; i++) {
Michael Chan32d8c572006-07-25 16:38:29 -07005387 if (tg3_alloc_rx_skb(tp, RXD_OPAQUE_RING_STD, -1, i) < 0) {
5388 printk(KERN_WARNING PFX
5389 "%s: Using a smaller RX standard ring, "
5390 "only %d out of %d buffers were allocated "
5391 "successfully.\n",
5392 tp->dev->name, i, tp->rx_pending);
5393 if (i == 0)
5394 return -ENOMEM;
5395 tp->rx_pending = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 break;
Michael Chan32d8c572006-07-25 16:38:29 -07005397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 }
5399
Michael Chan0f893dc2005-07-25 12:30:38 -07005400 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 for (i = 0; i < tp->rx_jumbo_pending; i++) {
5402 if (tg3_alloc_rx_skb(tp, RXD_OPAQUE_RING_JUMBO,
Michael Chan32d8c572006-07-25 16:38:29 -07005403 -1, i) < 0) {
5404 printk(KERN_WARNING PFX
5405 "%s: Using a smaller RX jumbo ring, "
5406 "only %d out of %d buffers were "
5407 "allocated successfully.\n",
5408 tp->dev->name, i, tp->rx_jumbo_pending);
5409 if (i == 0) {
5410 tg3_free_rings(tp);
5411 return -ENOMEM;
5412 }
5413 tp->rx_jumbo_pending = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 break;
Michael Chan32d8c572006-07-25 16:38:29 -07005415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 }
5417 }
Michael Chan32d8c572006-07-25 16:38:29 -07005418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419}
5420
5421/*
5422 * Must not be invoked with interrupt sources disabled and
5423 * the hardware shutdown down.
5424 */
5425static void tg3_free_consistent(struct tg3 *tp)
5426{
Jesper Juhlb4558ea2005-10-28 16:53:13 -04005427 kfree(tp->rx_std_buffers);
5428 tp->rx_std_buffers = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 if (tp->rx_std) {
5430 pci_free_consistent(tp->pdev, TG3_RX_RING_BYTES,
5431 tp->rx_std, tp->rx_std_mapping);
5432 tp->rx_std = NULL;
5433 }
5434 if (tp->rx_jumbo) {
5435 pci_free_consistent(tp->pdev, TG3_RX_JUMBO_RING_BYTES,
5436 tp->rx_jumbo, tp->rx_jumbo_mapping);
5437 tp->rx_jumbo = NULL;
5438 }
5439 if (tp->rx_rcb) {
5440 pci_free_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
5441 tp->rx_rcb, tp->rx_rcb_mapping);
5442 tp->rx_rcb = NULL;
5443 }
5444 if (tp->tx_ring) {
5445 pci_free_consistent(tp->pdev, TG3_TX_RING_BYTES,
5446 tp->tx_ring, tp->tx_desc_mapping);
5447 tp->tx_ring = NULL;
5448 }
5449 if (tp->hw_status) {
5450 pci_free_consistent(tp->pdev, TG3_HW_STATUS_SIZE,
5451 tp->hw_status, tp->status_mapping);
5452 tp->hw_status = NULL;
5453 }
5454 if (tp->hw_stats) {
5455 pci_free_consistent(tp->pdev, sizeof(struct tg3_hw_stats),
5456 tp->hw_stats, tp->stats_mapping);
5457 tp->hw_stats = NULL;
5458 }
5459}
5460
5461/*
5462 * Must not be invoked with interrupt sources disabled and
5463 * the hardware shutdown down. Can sleep.
5464 */
5465static int tg3_alloc_consistent(struct tg3 *tp)
5466{
Yan Burmanbd2b3342006-12-14 15:25:00 -08005467 tp->rx_std_buffers = kzalloc((sizeof(struct ring_info) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 (TG3_RX_RING_SIZE +
5469 TG3_RX_JUMBO_RING_SIZE)) +
5470 (sizeof(struct tx_ring_info) *
5471 TG3_TX_RING_SIZE),
5472 GFP_KERNEL);
5473 if (!tp->rx_std_buffers)
5474 return -ENOMEM;
5475
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 tp->rx_jumbo_buffers = &tp->rx_std_buffers[TG3_RX_RING_SIZE];
5477 tp->tx_buffers = (struct tx_ring_info *)
5478 &tp->rx_jumbo_buffers[TG3_RX_JUMBO_RING_SIZE];
5479
5480 tp->rx_std = pci_alloc_consistent(tp->pdev, TG3_RX_RING_BYTES,
5481 &tp->rx_std_mapping);
5482 if (!tp->rx_std)
5483 goto err_out;
5484
5485 tp->rx_jumbo = pci_alloc_consistent(tp->pdev, TG3_RX_JUMBO_RING_BYTES,
5486 &tp->rx_jumbo_mapping);
5487
5488 if (!tp->rx_jumbo)
5489 goto err_out;
5490
5491 tp->rx_rcb = pci_alloc_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
5492 &tp->rx_rcb_mapping);
5493 if (!tp->rx_rcb)
5494 goto err_out;
5495
5496 tp->tx_ring = pci_alloc_consistent(tp->pdev, TG3_TX_RING_BYTES,
5497 &tp->tx_desc_mapping);
5498 if (!tp->tx_ring)
5499 goto err_out;
5500
5501 tp->hw_status = pci_alloc_consistent(tp->pdev,
5502 TG3_HW_STATUS_SIZE,
5503 &tp->status_mapping);
5504 if (!tp->hw_status)
5505 goto err_out;
5506
5507 tp->hw_stats = pci_alloc_consistent(tp->pdev,
5508 sizeof(struct tg3_hw_stats),
5509 &tp->stats_mapping);
5510 if (!tp->hw_stats)
5511 goto err_out;
5512
5513 memset(tp->hw_status, 0, TG3_HW_STATUS_SIZE);
5514 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
5515
5516 return 0;
5517
5518err_out:
5519 tg3_free_consistent(tp);
5520 return -ENOMEM;
5521}
5522
5523#define MAX_WAIT_CNT 1000
5524
5525/* To stop a block, clear the enable bit and poll till it
5526 * clears. tp->lock is held.
5527 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07005528static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529{
5530 unsigned int i;
5531 u32 val;
5532
5533 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
5534 switch (ofs) {
5535 case RCVLSC_MODE:
5536 case DMAC_MODE:
5537 case MBFREE_MODE:
5538 case BUFMGR_MODE:
5539 case MEMARB_MODE:
5540 /* We can't enable/disable these bits of the
5541 * 5705/5750, just say success.
5542 */
5543 return 0;
5544
5545 default:
5546 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 }
5549
5550 val = tr32(ofs);
5551 val &= ~enable_bit;
5552 tw32_f(ofs, val);
5553
5554 for (i = 0; i < MAX_WAIT_CNT; i++) {
5555 udelay(100);
5556 val = tr32(ofs);
5557 if ((val & enable_bit) == 0)
5558 break;
5559 }
5560
David S. Millerb3b7d6b2005-05-05 14:40:20 -07005561 if (i == MAX_WAIT_CNT && !silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 printk(KERN_ERR PFX "tg3_stop_block timed out, "
5563 "ofs=%lx enable_bit=%x\n",
5564 ofs, enable_bit);
5565 return -ENODEV;
5566 }
5567
5568 return 0;
5569}
5570
5571/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07005572static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573{
5574 int i, err;
5575
5576 tg3_disable_ints(tp);
5577
5578 tp->rx_mode &= ~RX_MODE_ENABLE;
5579 tw32_f(MAC_RX_MODE, tp->rx_mode);
5580 udelay(10);
5581
David S. Millerb3b7d6b2005-05-05 14:40:20 -07005582 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
5583 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
5584 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
5585 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
5586 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
5587 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588
David S. Millerb3b7d6b2005-05-05 14:40:20 -07005589 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
5590 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
5591 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
5592 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
5593 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
5594 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
5595 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596
5597 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
5598 tw32_f(MAC_MODE, tp->mac_mode);
5599 udelay(40);
5600
5601 tp->tx_mode &= ~TX_MODE_ENABLE;
5602 tw32_f(MAC_TX_MODE, tp->tx_mode);
5603
5604 for (i = 0; i < MAX_WAIT_CNT; i++) {
5605 udelay(100);
5606 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
5607 break;
5608 }
5609 if (i >= MAX_WAIT_CNT) {
5610 printk(KERN_ERR PFX "tg3_abort_hw timed out for %s, "
5611 "TX_MODE_ENABLE will not clear MAC_TX_MODE=%08x\n",
5612 tp->dev->name, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07005613 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 }
5615
Michael Chane6de8ad2005-05-05 14:42:41 -07005616 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07005617 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
5618 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
5620 tw32(FTQ_RESET, 0xffffffff);
5621 tw32(FTQ_RESET, 0x00000000);
5622
David S. Millerb3b7d6b2005-05-05 14:40:20 -07005623 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
5624 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
5626 if (tp->hw_status)
5627 memset(tp->hw_status, 0, TG3_HW_STATUS_SIZE);
5628 if (tp->hw_stats)
5629 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
5630
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 return err;
5632}
5633
5634/* tp->lock is held. */
5635static int tg3_nvram_lock(struct tg3 *tp)
5636{
5637 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
5638 int i;
5639
Michael Chanec41c7d2006-01-17 02:40:55 -08005640 if (tp->nvram_lock_cnt == 0) {
5641 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
5642 for (i = 0; i < 8000; i++) {
5643 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
5644 break;
5645 udelay(20);
5646 }
5647 if (i == 8000) {
5648 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
5649 return -ENODEV;
5650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 }
Michael Chanec41c7d2006-01-17 02:40:55 -08005652 tp->nvram_lock_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 }
5654 return 0;
5655}
5656
5657/* tp->lock is held. */
5658static void tg3_nvram_unlock(struct tg3 *tp)
5659{
Michael Chanec41c7d2006-01-17 02:40:55 -08005660 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
5661 if (tp->nvram_lock_cnt > 0)
5662 tp->nvram_lock_cnt--;
5663 if (tp->nvram_lock_cnt == 0)
5664 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
5665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666}
5667
5668/* tp->lock is held. */
Michael Chane6af3012005-04-21 17:12:05 -07005669static void tg3_enable_nvram_access(struct tg3 *tp)
5670{
5671 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
5672 !(tp->tg3_flags2 & TG3_FLG2_PROTECTED_NVRAM)) {
5673 u32 nvaccess = tr32(NVRAM_ACCESS);
5674
5675 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
5676 }
5677}
5678
5679/* tp->lock is held. */
5680static void tg3_disable_nvram_access(struct tg3 *tp)
5681{
5682 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
5683 !(tp->tg3_flags2 & TG3_FLG2_PROTECTED_NVRAM)) {
5684 u32 nvaccess = tr32(NVRAM_ACCESS);
5685
5686 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
5687 }
5688}
5689
Matt Carlson0d3031d2007-10-10 18:02:43 -07005690static void tg3_ape_send_event(struct tg3 *tp, u32 event)
5691{
5692 int i;
5693 u32 apedata;
5694
5695 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
5696 if (apedata != APE_SEG_SIG_MAGIC)
5697 return;
5698
5699 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07005700 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07005701 return;
5702
5703 /* Wait for up to 1 millisecond for APE to service previous event. */
5704 for (i = 0; i < 10; i++) {
5705 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
5706 return;
5707
5708 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
5709
5710 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
5711 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
5712 event | APE_EVENT_STATUS_EVENT_PENDING);
5713
5714 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
5715
5716 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
5717 break;
5718
5719 udelay(100);
5720 }
5721
5722 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
5723 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
5724}
5725
5726static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
5727{
5728 u32 event;
5729 u32 apedata;
5730
5731 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
5732 return;
5733
5734 switch (kind) {
5735 case RESET_KIND_INIT:
5736 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
5737 APE_HOST_SEG_SIG_MAGIC);
5738 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
5739 APE_HOST_SEG_LEN_MAGIC);
5740 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
5741 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
5742 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
5743 APE_HOST_DRIVER_ID_MAGIC);
5744 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
5745 APE_HOST_BEHAV_NO_PHYLOCK);
5746
5747 event = APE_EVENT_STATUS_STATE_START;
5748 break;
5749 case RESET_KIND_SHUTDOWN:
Matt Carlsonb2aee152008-11-03 16:51:11 -08005750 /* With the interface we are currently using,
5751 * APE does not track driver state. Wiping
5752 * out the HOST SEGMENT SIGNATURE forces
5753 * the APE to assume OS absent status.
5754 */
5755 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
5756
Matt Carlson0d3031d2007-10-10 18:02:43 -07005757 event = APE_EVENT_STATUS_STATE_UNLOAD;
5758 break;
5759 case RESET_KIND_SUSPEND:
5760 event = APE_EVENT_STATUS_STATE_SUSPEND;
5761 break;
5762 default:
5763 return;
5764 }
5765
5766 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
5767
5768 tg3_ape_send_event(tp, event);
5769}
5770
Michael Chane6af3012005-04-21 17:12:05 -07005771/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
5773{
David S. Millerf49639e2006-06-09 11:58:36 -07005774 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
5775 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776
5777 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
5778 switch (kind) {
5779 case RESET_KIND_INIT:
5780 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
5781 DRV_STATE_START);
5782 break;
5783
5784 case RESET_KIND_SHUTDOWN:
5785 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
5786 DRV_STATE_UNLOAD);
5787 break;
5788
5789 case RESET_KIND_SUSPEND:
5790 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
5791 DRV_STATE_SUSPEND);
5792 break;
5793
5794 default:
5795 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07005798
5799 if (kind == RESET_KIND_INIT ||
5800 kind == RESET_KIND_SUSPEND)
5801 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802}
5803
5804/* tp->lock is held. */
5805static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
5806{
5807 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
5808 switch (kind) {
5809 case RESET_KIND_INIT:
5810 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
5811 DRV_STATE_START_DONE);
5812 break;
5813
5814 case RESET_KIND_SHUTDOWN:
5815 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
5816 DRV_STATE_UNLOAD_DONE);
5817 break;
5818
5819 default:
5820 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07005823
5824 if (kind == RESET_KIND_SHUTDOWN)
5825 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826}
5827
5828/* tp->lock is held. */
5829static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
5830{
5831 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
5832 switch (kind) {
5833 case RESET_KIND_INIT:
5834 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
5835 DRV_STATE_START);
5836 break;
5837
5838 case RESET_KIND_SHUTDOWN:
5839 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
5840 DRV_STATE_UNLOAD);
5841 break;
5842
5843 case RESET_KIND_SUSPEND:
5844 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
5845 DRV_STATE_SUSPEND);
5846 break;
5847
5848 default:
5849 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 }
5852}
5853
Michael Chan7a6f4362006-09-27 16:03:31 -07005854static int tg3_poll_fw(struct tg3 *tp)
5855{
5856 int i;
5857 u32 val;
5858
Michael Chanb5d37722006-09-27 16:06:21 -07005859 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08005860 /* Wait up to 20ms for init done. */
5861 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07005862 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
5863 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08005864 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07005865 }
5866 return -ENODEV;
5867 }
5868
Michael Chan7a6f4362006-09-27 16:03:31 -07005869 /* Wait for firmware initialization to complete. */
5870 for (i = 0; i < 100000; i++) {
5871 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
5872 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
5873 break;
5874 udelay(10);
5875 }
5876
5877 /* Chip might not be fitted with firmware. Some Sun onboard
5878 * parts are configured like that. So don't signal the timeout
5879 * of the above loop as an error, but do report the lack of
5880 * running firmware once.
5881 */
5882 if (i >= 100000 &&
5883 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
5884 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
5885
5886 printk(KERN_INFO PFX "%s: No firmware running.\n",
5887 tp->dev->name);
5888 }
5889
5890 return 0;
5891}
5892
Michael Chanee6a99b2007-07-18 21:49:10 -07005893/* Save PCI command register before chip reset */
5894static void tg3_save_pci_state(struct tg3 *tp)
5895{
Matt Carlson8a6eac92007-10-21 16:17:55 -07005896 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07005897}
5898
5899/* Restore PCI state after chip reset */
5900static void tg3_restore_pci_state(struct tg3 *tp)
5901{
5902 u32 val;
5903
5904 /* Re-enable indirect register accesses. */
5905 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
5906 tp->misc_host_ctrl);
5907
5908 /* Set MAX PCI retry to zero. */
5909 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
5910 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
5911 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
5912 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07005913 /* Allow reads and writes to the APE register and memory space. */
5914 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
5915 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
5916 PCISTATE_ALLOW_APE_SHMEM_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07005917 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
5918
Matt Carlson8a6eac92007-10-21 16:17:55 -07005919 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07005920
Matt Carlsonfcb389d2008-11-03 16:55:44 -08005921 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
5922 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
5923 pcie_set_readrq(tp->pdev, 4096);
5924 else {
5925 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
5926 tp->pci_cacheline_sz);
5927 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
5928 tp->pci_lat_timer);
5929 }
Michael Chan114342f2007-10-15 02:12:26 -07005930 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08005931
Michael Chanee6a99b2007-07-18 21:49:10 -07005932 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08005933 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07005934 u16 pcix_cmd;
5935
5936 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
5937 &pcix_cmd);
5938 pcix_cmd &= ~PCI_X_CMD_ERO;
5939 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
5940 pcix_cmd);
5941 }
Michael Chanee6a99b2007-07-18 21:49:10 -07005942
5943 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07005944
5945 /* Chip reset on 5780 will reset MSI enable bit,
5946 * so need to restore it.
5947 */
5948 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
5949 u16 ctrl;
5950
5951 pci_read_config_word(tp->pdev,
5952 tp->msi_cap + PCI_MSI_FLAGS,
5953 &ctrl);
5954 pci_write_config_word(tp->pdev,
5955 tp->msi_cap + PCI_MSI_FLAGS,
5956 ctrl | PCI_MSI_FLAGS_ENABLE);
5957 val = tr32(MSGINT_MODE);
5958 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
5959 }
5960 }
5961}
5962
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963static void tg3_stop_fw(struct tg3 *);
5964
5965/* tp->lock is held. */
5966static int tg3_chip_reset(struct tg3 *tp)
5967{
5968 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07005969 void (*write_op)(struct tg3 *, u32, u32);
Michael Chan7a6f4362006-09-27 16:03:31 -07005970 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971
David S. Millerf49639e2006-06-09 11:58:36 -07005972 tg3_nvram_lock(tp);
5973
Matt Carlson158d7ab2008-05-29 01:37:54 -07005974 tg3_mdio_stop(tp);
5975
Matt Carlson77b483f2008-08-15 14:07:24 -07005976 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
5977
David S. Millerf49639e2006-06-09 11:58:36 -07005978 /* No matching tg3_nvram_unlock() after this because
5979 * chip reset below will undo the nvram lock.
5980 */
5981 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
Michael Chanee6a99b2007-07-18 21:49:10 -07005983 /* GRC_MISC_CFG core clock reset will clear the memory
5984 * enable bit in PCI register 4 and the MSI enable bit
5985 * on some chips, so we save relevant registers here.
5986 */
5987 tg3_save_pci_state(tp);
5988
Michael Chand9ab5ad12006-03-20 22:27:35 -08005989 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08005990 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08005991 tw32(GRC_FASTBOOT_PC, 0);
5992
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 /*
5994 * We must avoid the readl() that normally takes place.
5995 * It locks machines, causes machine checks, and other
5996 * fun things. So, temporarily disable the 5701
5997 * hardware workaround, while we do the reset.
5998 */
Michael Chan1ee582d2005-08-09 20:16:46 -07005999 write_op = tp->write32;
6000 if (write_op == tg3_write_flush_reg32)
6001 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002
Michael Chand18edcb2007-03-24 20:57:11 -07006003 /* Prevent the irq handler from reading or writing PCI registers
6004 * during chip reset when the memory enable bit in the PCI command
6005 * register may be cleared. The chip does not generate interrupt
6006 * at this time, but the irq handler may still be called due to irq
6007 * sharing or irqpoll.
6008 */
6009 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Michael Chanb8fa2f32007-04-06 17:35:37 -07006010 if (tp->hw_status) {
6011 tp->hw_status->status = 0;
6012 tp->hw_status->status_tag = 0;
6013 }
Michael Chand18edcb2007-03-24 20:57:11 -07006014 tp->last_tag = 0;
6015 smp_mb();
6016 synchronize_irq(tp->pdev->irq);
6017
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 /* do the reset */
6019 val = GRC_MISC_CFG_CORECLK_RESET;
6020
6021 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
6022 if (tr32(0x7e2c) == 0x60) {
6023 tw32(0x7e2c, 0x20);
6024 }
6025 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
6026 tw32(GRC_MISC_CFG, (1 << 29));
6027 val |= (1 << 29);
6028 }
6029 }
6030
Michael Chanb5d37722006-09-27 16:06:21 -07006031 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6032 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
6033 tw32(GRC_VCPU_EXT_CTRL,
6034 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
6035 }
6036
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6038 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
6039 tw32(GRC_MISC_CFG, val);
6040
Michael Chan1ee582d2005-08-09 20:16:46 -07006041 /* restore 5701 hardware bug workaround write method */
6042 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043
6044 /* Unfortunately, we have to delay before the PCI read back.
6045 * Some 575X chips even will not respond to a PCI cfg access
6046 * when the reset command is given to the chip.
6047 *
6048 * How do these hardware designers expect things to work
6049 * properly if the PCI write is posted for a long period
6050 * of time? It is always necessary to have some method by
6051 * which a register read back can occur to push the write
6052 * out which does the reset.
6053 *
6054 * For most tg3 variants the trick below was working.
6055 * Ho hum...
6056 */
6057 udelay(120);
6058
6059 /* Flush PCI posted writes. The normal MMIO registers
6060 * are inaccessible at this time so this is the only
6061 * way to make this reliably (actually, this is no longer
6062 * the case, see above). I tried to use indirect
6063 * register read/write but this upset some 5701 variants.
6064 */
6065 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
6066
6067 udelay(120);
6068
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006069 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
6071 int i;
6072 u32 cfg_val;
6073
6074 /* Wait for link training to complete. */
6075 for (i = 0; i < 5000; i++)
6076 udelay(100);
6077
6078 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
6079 pci_write_config_dword(tp->pdev, 0xc4,
6080 cfg_val | (1 << 15));
6081 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006082
6083 /* Set PCIE max payload size to 128 bytes and
6084 * clear the "no snoop" and "relaxed ordering" bits.
6085 */
6086 pci_write_config_word(tp->pdev,
6087 tp->pcie_cap + PCI_EXP_DEVCTL,
6088 0);
6089
6090 pcie_set_readrq(tp->pdev, 4096);
6091
6092 /* Clear error status */
6093 pci_write_config_word(tp->pdev,
6094 tp->pcie_cap + PCI_EXP_DEVSTA,
6095 PCI_EXP_DEVSTA_CED |
6096 PCI_EXP_DEVSTA_NFED |
6097 PCI_EXP_DEVSTA_FED |
6098 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 }
6100
Michael Chanee6a99b2007-07-18 21:49:10 -07006101 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102
Michael Chand18edcb2007-03-24 20:57:11 -07006103 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
6104
Michael Chanee6a99b2007-07-18 21:49:10 -07006105 val = 0;
6106 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07006107 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07006108 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109
6110 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
6111 tg3_stop_fw(tp);
6112 tw32(0x5000, 0x400);
6113 }
6114
6115 tw32(GRC_MODE, tp->grc_mode);
6116
6117 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01006118 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119
6120 tw32(0xc4, val | (1 << 15));
6121 }
6122
6123 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
6124 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
6125 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
6126 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
6127 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
6128 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
6129 }
6130
6131 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
6132 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
6133 tw32_f(MAC_MODE, tp->mac_mode);
Michael Chan747e8f82005-07-25 12:33:22 -07006134 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
6135 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
6136 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlson3bda1252008-08-15 14:08:22 -07006137 } else if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
6138 tp->mac_mode &= (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
6139 if (tp->mac_mode & MAC_MODE_APE_TX_EN)
6140 tp->mac_mode |= MAC_MODE_TDE_ENABLE;
6141 tw32_f(MAC_MODE, tp->mac_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 } else
6143 tw32_f(MAC_MODE, 0);
6144 udelay(40);
6145
Matt Carlson158d7ab2008-05-29 01:37:54 -07006146 tg3_mdio_start(tp);
6147
Matt Carlson77b483f2008-08-15 14:07:24 -07006148 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
6149
Michael Chan7a6f4362006-09-27 16:03:31 -07006150 err = tg3_poll_fw(tp);
6151 if (err)
6152 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153
6154 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
6155 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01006156 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157
6158 tw32(0x7c00, val | (1 << 25));
6159 }
6160
6161 /* Reprobe ASF enable state. */
6162 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
6163 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
6164 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
6165 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
6166 u32 nic_cfg;
6167
6168 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
6169 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
6170 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07006171 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07006172 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
6174 }
6175 }
6176
6177 return 0;
6178}
6179
6180/* tp->lock is held. */
6181static void tg3_stop_fw(struct tg3 *tp)
6182{
Matt Carlson0d3031d2007-10-10 18:02:43 -07006183 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
6184 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07006185 /* Wait for RX cpu to ACK the previous event. */
6186 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187
6188 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07006189
6190 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191
Matt Carlson7c5026a2008-05-02 16:49:29 -07006192 /* Wait for RX cpu to ACK this event. */
6193 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 }
6195}
6196
6197/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07006198static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199{
6200 int err;
6201
6202 tg3_stop_fw(tp);
6203
Michael Chan944d9802005-05-29 14:57:48 -07006204 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006206 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 err = tg3_chip_reset(tp);
6208
Michael Chan944d9802005-05-29 14:57:48 -07006209 tg3_write_sig_legacy(tp, kind);
6210 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211
6212 if (err)
6213 return err;
6214
6215 return 0;
6216}
6217
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218#define RX_CPU_SCRATCH_BASE 0x30000
6219#define RX_CPU_SCRATCH_SIZE 0x04000
6220#define TX_CPU_SCRATCH_BASE 0x34000
6221#define TX_CPU_SCRATCH_SIZE 0x04000
6222
6223/* tp->lock is held. */
6224static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
6225{
6226 int i;
6227
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02006228 BUG_ON(offset == TX_CPU_BASE &&
6229 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230
Michael Chanb5d37722006-09-27 16:06:21 -07006231 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6232 u32 val = tr32(GRC_VCPU_EXT_CTRL);
6233
6234 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
6235 return 0;
6236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 if (offset == RX_CPU_BASE) {
6238 for (i = 0; i < 10000; i++) {
6239 tw32(offset + CPU_STATE, 0xffffffff);
6240 tw32(offset + CPU_MODE, CPU_MODE_HALT);
6241 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
6242 break;
6243 }
6244
6245 tw32(offset + CPU_STATE, 0xffffffff);
6246 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
6247 udelay(10);
6248 } else {
6249 for (i = 0; i < 10000; i++) {
6250 tw32(offset + CPU_STATE, 0xffffffff);
6251 tw32(offset + CPU_MODE, CPU_MODE_HALT);
6252 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
6253 break;
6254 }
6255 }
6256
6257 if (i >= 10000) {
6258 printk(KERN_ERR PFX "tg3_reset_cpu timed out for %s, "
6259 "and %s CPU\n",
6260 tp->dev->name,
6261 (offset == RX_CPU_BASE ? "RX" : "TX"));
6262 return -ENODEV;
6263 }
Michael Chanec41c7d2006-01-17 02:40:55 -08006264
6265 /* Clear firmware's nvram arbitration. */
6266 if (tp->tg3_flags & TG3_FLAG_NVRAM)
6267 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 return 0;
6269}
6270
6271struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006272 unsigned int fw_base;
6273 unsigned int fw_len;
6274 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275};
6276
6277/* tp->lock is held. */
6278static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
6279 int cpu_scratch_size, struct fw_info *info)
6280{
Michael Chanec41c7d2006-01-17 02:40:55 -08006281 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282 void (*write_op)(struct tg3 *, u32, u32);
6283
6284 if (cpu_base == TX_CPU_BASE &&
6285 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
6286 printk(KERN_ERR PFX "tg3_load_firmware_cpu: Trying to load "
6287 "TX cpu firmware on %s which is 5705.\n",
6288 tp->dev->name);
6289 return -EINVAL;
6290 }
6291
6292 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6293 write_op = tg3_write_mem;
6294 else
6295 write_op = tg3_write_indirect_reg32;
6296
Michael Chan1b628152005-05-29 14:59:49 -07006297 /* It is possible that bootcode is still loading at this point.
6298 * Get the nvram lock first before halting the cpu.
6299 */
Michael Chanec41c7d2006-01-17 02:40:55 -08006300 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08006302 if (!lock_err)
6303 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 if (err)
6305 goto out;
6306
6307 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
6308 write_op(tp, cpu_scratch_base + i, 0);
6309 tw32(cpu_base + CPU_STATE, 0xffffffff);
6310 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006311 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006313 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006315 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316
6317 err = 0;
6318
6319out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320 return err;
6321}
6322
6323/* tp->lock is held. */
6324static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
6325{
6326 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006327 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 int err, i;
6329
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006330 fw_data = (void *)tp->fw->data;
6331
6332 /* Firmware blob starts with version numbers, followed by
6333 start address and length. We are setting complete length.
6334 length = end_address_of_bss - start_address_of_text.
6335 Remainder is the blob to be loaded contiguously
6336 from start address. */
6337
6338 info.fw_base = be32_to_cpu(fw_data[1]);
6339 info.fw_len = tp->fw->size - 12;
6340 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341
6342 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
6343 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
6344 &info);
6345 if (err)
6346 return err;
6347
6348 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
6349 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
6350 &info);
6351 if (err)
6352 return err;
6353
6354 /* Now startup only the RX cpu. */
6355 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006356 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357
6358 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006359 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 break;
6361 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
6362 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006363 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364 udelay(1000);
6365 }
6366 if (i >= 5) {
6367 printk(KERN_ERR PFX "tg3_load_firmware fails for %s "
6368 "to set RX CPU PC, is %08x should be %08x\n",
6369 tp->dev->name, tr32(RX_CPU_BASE + CPU_PC),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006370 info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 return -ENODEV;
6372 }
6373 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
6374 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
6375
6376 return 0;
6377}
6378
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380
6381/* tp->lock is held. */
6382static int tg3_load_tso_firmware(struct tg3 *tp)
6383{
6384 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006385 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
6387 int err, i;
6388
6389 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
6390 return 0;
6391
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006392 fw_data = (void *)tp->fw->data;
6393
6394 /* Firmware blob starts with version numbers, followed by
6395 start address and length. We are setting complete length.
6396 length = end_address_of_bss - start_address_of_text.
6397 Remainder is the blob to be loaded contiguously
6398 from start address. */
6399
6400 info.fw_base = be32_to_cpu(fw_data[1]);
6401 cpu_scratch_size = tp->fw_len;
6402 info.fw_len = tp->fw->size - 12;
6403 info.fw_data = &fw_data[3];
6404
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 cpu_base = RX_CPU_BASE;
6407 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 cpu_base = TX_CPU_BASE;
6410 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
6411 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
6412 }
6413
6414 err = tg3_load_firmware_cpu(tp, cpu_base,
6415 cpu_scratch_base, cpu_scratch_size,
6416 &info);
6417 if (err)
6418 return err;
6419
6420 /* Now startup the cpu. */
6421 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006422 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423
6424 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006425 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 break;
6427 tw32(cpu_base + CPU_STATE, 0xffffffff);
6428 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006429 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430 udelay(1000);
6431 }
6432 if (i >= 5) {
6433 printk(KERN_ERR PFX "tg3_load_tso_firmware fails for %s "
6434 "to set CPU PC, is %08x should be %08x\n",
6435 tp->dev->name, tr32(cpu_base + CPU_PC),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006436 info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 return -ENODEV;
6438 }
6439 tw32(cpu_base + CPU_STATE, 0xffffffff);
6440 tw32_f(cpu_base + CPU_MODE, 0x00000000);
6441 return 0;
6442}
6443
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445static int tg3_set_mac_addr(struct net_device *dev, void *p)
6446{
6447 struct tg3 *tp = netdev_priv(dev);
6448 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07006449 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450
Michael Chanf9804dd2005-09-27 12:13:10 -07006451 if (!is_valid_ether_addr(addr->sa_data))
6452 return -EINVAL;
6453
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
6455
Michael Chane75f7c92006-03-20 21:33:26 -08006456 if (!netif_running(dev))
6457 return 0;
6458
Michael Chan58712ef2006-04-29 18:58:01 -07006459 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07006460 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07006461
Michael Chan986e0ae2007-05-05 12:10:20 -07006462 addr0_high = tr32(MAC_ADDR_0_HIGH);
6463 addr0_low = tr32(MAC_ADDR_0_LOW);
6464 addr1_high = tr32(MAC_ADDR_1_HIGH);
6465 addr1_low = tr32(MAC_ADDR_1_LOW);
6466
6467 /* Skip MAC addr 1 if ASF is using it. */
6468 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
6469 !(addr1_high == 0 && addr1_low == 0))
6470 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07006471 }
Michael Chan986e0ae2007-05-05 12:10:20 -07006472 spin_lock_bh(&tp->lock);
6473 __tg3_set_mac_addr(tp, skip_mac_1);
6474 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475
Michael Chanb9ec6c12006-07-25 16:37:27 -07006476 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477}
6478
6479/* tp->lock is held. */
6480static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
6481 dma_addr_t mapping, u32 maxlen_flags,
6482 u32 nic_addr)
6483{
6484 tg3_write_mem(tp,
6485 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
6486 ((u64) mapping >> 32));
6487 tg3_write_mem(tp,
6488 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
6489 ((u64) mapping & 0xffffffff));
6490 tg3_write_mem(tp,
6491 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
6492 maxlen_flags);
6493
6494 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
6495 tg3_write_mem(tp,
6496 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
6497 nic_addr);
6498}
6499
6500static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07006501static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07006502{
6503 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
6504 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
6505 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
6506 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
6507 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
6508 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
6509 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
6510 }
6511 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
6512 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
6513 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
6514 u32 val = ec->stats_block_coalesce_usecs;
6515
6516 if (!netif_carrier_ok(tp->dev))
6517 val = 0;
6518
6519 tw32(HOSTCC_STAT_COAL_TICKS, val);
6520 }
6521}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522
6523/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07006524static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525{
6526 u32 val, rdmac_mode;
6527 int i, err, limit;
6528
6529 tg3_disable_ints(tp);
6530
6531 tg3_stop_fw(tp);
6532
6533 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
6534
6535 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) {
Michael Chane6de8ad2005-05-05 14:42:41 -07006536 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 }
6538
Matt Carlsondd477002008-05-25 23:45:58 -07006539 if (reset_phy &&
6540 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB))
Michael Chand4d2c552006-03-20 17:47:20 -08006541 tg3_phy_reset(tp);
6542
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543 err = tg3_chip_reset(tp);
6544 if (err)
6545 return err;
6546
6547 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
6548
Matt Carlsonbcb37f62008-11-03 16:52:09 -08006549 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07006550 val = tr32(TG3_CPMU_CTRL);
6551 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
6552 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08006553
6554 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
6555 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
6556 val |= CPMU_LSPD_10MB_MACCLK_6_25;
6557 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
6558
6559 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
6560 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
6561 val |= CPMU_LNK_AWARE_MACCLK_6_25;
6562 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
6563
6564 val = tr32(TG3_CPMU_HST_ACC);
6565 val &= ~CPMU_HST_ACC_MACCLK_MASK;
6566 val |= CPMU_HST_ACC_MACCLK_6_25;
6567 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07006568 }
6569
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570 /* This works around an issue with Athlon chipsets on
6571 * B3 tigon3 silicon. This bit has no effect on any
6572 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07006573 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574 */
Matt Carlson795d01c2007-10-07 23:28:17 -07006575 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
6576 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
6577 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
6578 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
6579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580
6581 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6582 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
6583 val = tr32(TG3PCI_PCISTATE);
6584 val |= PCISTATE_RETRY_SAME_DMA;
6585 tw32(TG3PCI_PCISTATE, val);
6586 }
6587
Matt Carlson0d3031d2007-10-10 18:02:43 -07006588 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
6589 /* Allow reads and writes to the
6590 * APE register and memory space.
6591 */
6592 val = tr32(TG3PCI_PCISTATE);
6593 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
6594 PCISTATE_ALLOW_APE_SHMEM_WR;
6595 tw32(TG3PCI_PCISTATE, val);
6596 }
6597
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
6599 /* Enable some hw fixes. */
6600 val = tr32(TG3PCI_MSI_DATA);
6601 val |= (1 << 26) | (1 << 28) | (1 << 29);
6602 tw32(TG3PCI_MSI_DATA, val);
6603 }
6604
6605 /* Descriptor ring init may make accesses to the
6606 * NIC SRAM area to setup the TX descriptors, so we
6607 * can only do this after the hardware has been
6608 * successfully reset.
6609 */
Michael Chan32d8c572006-07-25 16:38:29 -07006610 err = tg3_init_rings(tp);
6611 if (err)
6612 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613
Matt Carlson9936bcf2007-10-10 18:03:07 -07006614 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006615 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07006616 /* This value is determined during the probe time DMA
6617 * engine test, tg3_test_dma.
6618 */
6619 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
6620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621
6622 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
6623 GRC_MODE_4X_NIC_SEND_RINGS |
6624 GRC_MODE_NO_TX_PHDR_CSUM |
6625 GRC_MODE_NO_RX_PHDR_CSUM);
6626 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07006627
6628 /* Pseudo-header checksum is done by hardware logic and not
6629 * the offload processers, so make the chip do the pseudo-
6630 * header checksums on receive. For transmit it is more
6631 * convenient to do the pseudo-header checksum in software
6632 * as Linux does that on transmit for us in all cases.
6633 */
6634 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635
6636 tw32(GRC_MODE,
6637 tp->grc_mode |
6638 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
6639
6640 /* Setup the timer prescalar register. Clock is always 66Mhz. */
6641 val = tr32(GRC_MISC_CFG);
6642 val &= ~0xff;
6643 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
6644 tw32(GRC_MISC_CFG, val);
6645
6646 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07006647 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 /* Do nothing. */
6649 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
6650 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
6651 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
6652 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
6653 else
6654 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
6655 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
6656 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
6657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
6659 int fw_len;
6660
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006661 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
6663 tw32(BUFMGR_MB_POOL_ADDR,
6664 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
6665 tw32(BUFMGR_MB_POOL_SIZE,
6666 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
6667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668
Michael Chan0f893dc2005-07-25 12:30:38 -07006669 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670 tw32(BUFMGR_MB_RDMA_LOW_WATER,
6671 tp->bufmgr_config.mbuf_read_dma_low_water);
6672 tw32(BUFMGR_MB_MACRX_LOW_WATER,
6673 tp->bufmgr_config.mbuf_mac_rx_low_water);
6674 tw32(BUFMGR_MB_HIGH_WATER,
6675 tp->bufmgr_config.mbuf_high_water);
6676 } else {
6677 tw32(BUFMGR_MB_RDMA_LOW_WATER,
6678 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
6679 tw32(BUFMGR_MB_MACRX_LOW_WATER,
6680 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
6681 tw32(BUFMGR_MB_HIGH_WATER,
6682 tp->bufmgr_config.mbuf_high_water_jumbo);
6683 }
6684 tw32(BUFMGR_DMA_LOW_WATER,
6685 tp->bufmgr_config.dma_low_water);
6686 tw32(BUFMGR_DMA_HIGH_WATER,
6687 tp->bufmgr_config.dma_high_water);
6688
6689 tw32(BUFMGR_MODE, BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE);
6690 for (i = 0; i < 2000; i++) {
6691 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
6692 break;
6693 udelay(10);
6694 }
6695 if (i >= 2000) {
6696 printk(KERN_ERR PFX "tg3_reset_hw cannot enable BUFMGR for %s.\n",
6697 tp->dev->name);
6698 return -ENODEV;
6699 }
6700
6701 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07006702 val = tp->rx_pending / 8;
6703 if (val == 0)
6704 val = 1;
6705 else if (val > tp->rx_std_max_post)
6706 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07006707 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6708 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
6709 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
6710
6711 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
6712 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
6713 }
Michael Chanf92905d2006-06-29 20:14:29 -07006714
6715 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716
6717 /* Initialize TG3_BDINFO's at:
6718 * RCVDBDI_STD_BD: standard eth size rx ring
6719 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
6720 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
6721 *
6722 * like so:
6723 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
6724 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
6725 * ring attribute flags
6726 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
6727 *
6728 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
6729 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
6730 *
6731 * The size of each ring is fixed in the firmware, but the location is
6732 * configurable.
6733 */
6734 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
6735 ((u64) tp->rx_std_mapping >> 32));
6736 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
6737 ((u64) tp->rx_std_mapping & 0xffffffff));
6738 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
6739 NIC_SRAM_RX_BUFFER_DESC);
6740
6741 /* Don't even try to program the JUMBO/MINI buffer descriptor
6742 * configs on 5705.
6743 */
6744 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6745 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS,
6746 RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT);
6747 } else {
6748 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS,
6749 RX_STD_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT);
6750
6751 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
6752 BDINFO_FLAGS_DISABLED);
6753
6754 /* Setup replenish threshold. */
6755 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
6756
Michael Chan0f893dc2005-07-25 12:30:38 -07006757 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
6759 ((u64) tp->rx_jumbo_mapping >> 32));
6760 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
6761 ((u64) tp->rx_jumbo_mapping & 0xffffffff));
6762 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
6763 RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT);
6764 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
6765 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
6766 } else {
6767 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
6768 BDINFO_FLAGS_DISABLED);
6769 }
6770
6771 }
6772
6773 /* There is only one send ring on 5705/5750, no need to explicitly
6774 * disable the others.
6775 */
6776 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
6777 /* Clear out send RCB ring in SRAM. */
6778 for (i = NIC_SRAM_SEND_RCB; i < NIC_SRAM_RCV_RET_RCB; i += TG3_BDINFO_SIZE)
6779 tg3_write_mem(tp, i + TG3_BDINFO_MAXLEN_FLAGS,
6780 BDINFO_FLAGS_DISABLED);
6781 }
6782
6783 tp->tx_prod = 0;
6784 tp->tx_cons = 0;
6785 tw32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW, 0);
6786 tw32_tx_mbox(MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW, 0);
6787
6788 tg3_set_bdinfo(tp, NIC_SRAM_SEND_RCB,
6789 tp->tx_desc_mapping,
6790 (TG3_TX_RING_SIZE <<
6791 BDINFO_FLAGS_MAXLEN_SHIFT),
6792 NIC_SRAM_TX_BUFFER_DESC);
6793
6794 /* There is only one receive return ring on 5705/5750, no need
6795 * to explicitly disable the others.
6796 */
6797 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
6798 for (i = NIC_SRAM_RCV_RET_RCB; i < NIC_SRAM_STATS_BLK;
6799 i += TG3_BDINFO_SIZE) {
6800 tg3_write_mem(tp, i + TG3_BDINFO_MAXLEN_FLAGS,
6801 BDINFO_FLAGS_DISABLED);
6802 }
6803 }
6804
6805 tp->rx_rcb_ptr = 0;
6806 tw32_rx_mbox(MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW, 0);
6807
6808 tg3_set_bdinfo(tp, NIC_SRAM_RCV_RET_RCB,
6809 tp->rx_rcb_mapping,
6810 (TG3_RX_RCB_RING_SIZE(tp) <<
6811 BDINFO_FLAGS_MAXLEN_SHIFT),
6812 0);
6813
6814 tp->rx_std_ptr = tp->rx_pending;
6815 tw32_rx_mbox(MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW,
6816 tp->rx_std_ptr);
6817
Michael Chan0f893dc2005-07-25 12:30:38 -07006818 tp->rx_jumbo_ptr = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 tp->rx_jumbo_pending : 0;
6820 tw32_rx_mbox(MAILBOX_RCV_JUMBO_PROD_IDX + TG3_64BIT_REG_LOW,
6821 tp->rx_jumbo_ptr);
6822
6823 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07006824 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825
6826 /* MTU + ethernet header + FCS + optional VLAN tag */
6827 tw32(MAC_RX_MTU_SIZE, tp->dev->mtu + ETH_HLEN + 8);
6828
6829 /* The slot time is changed by tg3_setup_phy if we
6830 * run at gigabit with half duplex.
6831 */
6832 tw32(MAC_TX_LENGTHS,
6833 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
6834 (6 << TX_LENGTHS_IPG_SHIFT) |
6835 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
6836
6837 /* Receive rules. */
6838 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
6839 tw32(RCVLPC_CONFIG, 0x0181);
6840
6841 /* Calculate RDMAC_MODE setting early, we need it to determine
6842 * the RCVLPC_STATE_ENABLE mask.
6843 */
6844 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
6845 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
6846 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
6847 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
6848 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07006849
Matt Carlson57e69832008-05-25 23:48:31 -07006850 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08006851 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
6852 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07006853 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
6854 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
6855 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
6856
Michael Chan85e94ce2005-04-21 17:05:28 -07006857 /* If statement applies to 5705 and 5750 PCI devices only */
6858 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
6859 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
6860 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07006862 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
6864 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
6865 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
6866 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
6867 }
6868 }
6869
Michael Chan85e94ce2005-04-21 17:05:28 -07006870 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
6871 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
6872
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08006874 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
6875
6876 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
6877 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
6878 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879
6880 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07006881 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
6882 val = tr32(RCVLPC_STATS_ENABLE);
6883 val &= ~RCVLPC_STATSENAB_DACK_FIX;
6884 tw32(RCVLPC_STATS_ENABLE, val);
6885 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
6886 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887 val = tr32(RCVLPC_STATS_ENABLE);
6888 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
6889 tw32(RCVLPC_STATS_ENABLE, val);
6890 } else {
6891 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
6892 }
6893 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
6894 tw32(SNDDATAI_STATSENAB, 0xffffff);
6895 tw32(SNDDATAI_STATSCTRL,
6896 (SNDDATAI_SCTRL_ENABLE |
6897 SNDDATAI_SCTRL_FASTUPD));
6898
6899 /* Setup host coalescing engine. */
6900 tw32(HOSTCC_MODE, 0);
6901 for (i = 0; i < 2000; i++) {
6902 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
6903 break;
6904 udelay(10);
6905 }
6906
Michael Chand244c892005-07-05 14:42:33 -07006907 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908
6909 /* set status block DMA address */
6910 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
6911 ((u64) tp->status_mapping >> 32));
6912 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
6913 ((u64) tp->status_mapping & 0xffffffff));
6914
6915 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
6916 /* Status/statistics block address. See tg3_timer,
6917 * the tg3_periodic_fetch_stats call there, and
6918 * tg3_get_stats to see how this works for 5705/5750 chips.
6919 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
6921 ((u64) tp->stats_mapping >> 32));
6922 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
6923 ((u64) tp->stats_mapping & 0xffffffff));
6924 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
6925 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
6926 }
6927
6928 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
6929
6930 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
6931 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
6932 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
6933 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
6934
6935 /* Clear statistics/status block in chip, and status block in ram. */
6936 for (i = NIC_SRAM_STATS_BLK;
6937 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
6938 i += sizeof(u32)) {
6939 tg3_write_mem(tp, i, 0);
6940 udelay(40);
6941 }
6942 memset(tp->hw_status, 0, TG3_HW_STATUS_SIZE);
6943
Michael Chanc94e3942005-09-27 12:12:42 -07006944 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
6945 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
6946 /* reset to prevent losing 1st rx packet intermittently */
6947 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
6948 udelay(10);
6949 }
6950
Matt Carlson3bda1252008-08-15 14:08:22 -07006951 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6952 tp->mac_mode &= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
6953 else
6954 tp->mac_mode = 0;
6955 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07006957 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
6958 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
6959 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
6960 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
6962 udelay(40);
6963
Michael Chan314fba32005-04-21 17:07:04 -07006964 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08006965 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07006966 * register to preserve the GPIO settings for LOMs. The GPIOs,
6967 * whether used as inputs or outputs, are set by boot code after
6968 * reset.
6969 */
Michael Chan9d26e212006-12-07 00:21:14 -08006970 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07006971 u32 gpio_mask;
6972
Michael Chan9d26e212006-12-07 00:21:14 -08006973 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
6974 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
6975 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07006976
6977 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
6978 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
6979 GRC_LCLCTRL_GPIO_OUTPUT3;
6980
Michael Chanaf36e6b2006-03-23 01:28:06 -08006981 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
6982 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
6983
Gary Zambranoaaf84462007-05-05 11:51:45 -07006984 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07006985 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
6986
6987 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08006988 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
6989 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
6990 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07006991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
6993 udelay(100);
6994
Michael Chan09ee9292005-08-09 20:17:00 -07006995 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0);
David S. Millerfac9b832005-05-18 22:46:34 -07006996 tp->last_tag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997
6998 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
6999 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
7000 udelay(40);
7001 }
7002
7003 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
7004 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
7005 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
7006 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
7007 WDMAC_MODE_LNGREAD_ENAB);
7008
Michael Chan85e94ce2005-04-21 17:05:28 -07007009 /* If statement applies to 5705 and 5750 PCI devices only */
7010 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7011 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7012 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013 if ((tp->tg3_flags & TG3_FLG2_TSO_CAPABLE) &&
7014 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
7015 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
7016 /* nothing */
7017 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7018 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
7019 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
7020 val |= WDMAC_MODE_RX_ACCEL;
7021 }
7022 }
7023
Michael Chand9ab5ad12006-03-20 22:27:35 -08007024 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08007025 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07007026 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08007027
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028 tw32_f(WDMAC_MODE, val);
7029 udelay(40);
7030
Matt Carlson9974a352007-10-07 23:27:28 -07007031 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
7032 u16 pcix_cmd;
7033
7034 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7035 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07007037 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
7038 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07007040 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
7041 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042 }
Matt Carlson9974a352007-10-07 23:27:28 -07007043 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7044 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045 }
7046
7047 tw32_f(RDMAC_MODE, rdmac_mode);
7048 udelay(40);
7049
7050 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
7051 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7052 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07007053
7054 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
7055 tw32(SNDDATAC_MODE,
7056 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
7057 else
7058 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
7059
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
7061 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
7062 tw32(RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ);
7063 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7065 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066 tw32(SNDBDI_MODE, SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE);
7067 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
7068
7069 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
7070 err = tg3_load_5701_a0_firmware_fix(tp);
7071 if (err)
7072 return err;
7073 }
7074
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
7076 err = tg3_load_tso_firmware(tp);
7077 if (err)
7078 return err;
7079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080
7081 tp->tx_mode = TX_MODE_ENABLE;
7082 tw32_f(MAC_TX_MODE, tp->tx_mode);
7083 udelay(100);
7084
7085 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08007086 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08007087 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
7088
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089 tw32_f(MAC_RX_MODE, tp->rx_mode);
7090 udelay(10);
7091
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092 tw32(MAC_LED_CTRL, tp->led_ctrl);
7093
7094 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Michael Chanc94e3942005-09-27 12:12:42 -07007095 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7097 udelay(10);
7098 }
7099 tw32_f(MAC_RX_MODE, tp->rx_mode);
7100 udelay(10);
7101
7102 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
7103 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
7104 !(tp->tg3_flags2 & TG3_FLG2_SERDES_PREEMPHASIS)) {
7105 /* Set drive transmission level to 1.2V */
7106 /* only if the signal pre-emphasis bit is not set */
7107 val = tr32(MAC_SERDES_CFG);
7108 val &= 0xfffff000;
7109 val |= 0x880;
7110 tw32(MAC_SERDES_CFG, val);
7111 }
7112 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
7113 tw32(MAC_SERDES_CFG, 0x616000);
7114 }
7115
7116 /* Prevent chip from dropping frames when flow control
7117 * is enabled.
7118 */
7119 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, 2);
7120
7121 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
7122 (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
7123 /* Use hardware link auto-negotiation */
7124 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
7125 }
7126
Michael Chand4d2c552006-03-20 17:47:20 -08007127 if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
7128 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
7129 u32 tmp;
7130
7131 tmp = tr32(SERDES_RX_CTRL);
7132 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
7133 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
7134 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
7135 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
7136 }
7137
Matt Carlsondd477002008-05-25 23:45:58 -07007138 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
7139 if (tp->link_config.phy_is_low_power) {
7140 tp->link_config.phy_is_low_power = 0;
7141 tp->link_config.speed = tp->link_config.orig_speed;
7142 tp->link_config.duplex = tp->link_config.orig_duplex;
7143 tp->link_config.autoneg = tp->link_config.orig_autoneg;
7144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145
Matt Carlsondd477002008-05-25 23:45:58 -07007146 err = tg3_setup_phy(tp, 0);
7147 if (err)
7148 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149
Matt Carlsondd477002008-05-25 23:45:58 -07007150 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
7151 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5906) {
7152 u32 tmp;
7153
7154 /* Clear CRC stats. */
7155 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
7156 tg3_writephy(tp, MII_TG3_TEST1,
7157 tmp | MII_TG3_TEST1_CRC_EN);
7158 tg3_readphy(tp, 0x14, &tmp);
7159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160 }
7161 }
7162
7163 __tg3_set_rx_mode(tp->dev);
7164
7165 /* Initialize receive rules. */
7166 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
7167 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
7168 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
7169 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
7170
Michael Chan4cf78e42005-07-25 12:29:19 -07007171 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07007172 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173 limit = 8;
7174 else
7175 limit = 16;
7176 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
7177 limit -= 4;
7178 switch (limit) {
7179 case 16:
7180 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
7181 case 15:
7182 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
7183 case 14:
7184 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
7185 case 13:
7186 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
7187 case 12:
7188 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
7189 case 11:
7190 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
7191 case 10:
7192 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
7193 case 9:
7194 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
7195 case 8:
7196 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
7197 case 7:
7198 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
7199 case 6:
7200 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
7201 case 5:
7202 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
7203 case 4:
7204 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
7205 case 3:
7206 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
7207 case 2:
7208 case 1:
7209
7210 default:
7211 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213
Matt Carlson9ce768e2007-10-11 19:49:11 -07007214 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7215 /* Write our heartbeat update interval to APE. */
7216 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
7217 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07007218
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
7220
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 return 0;
7222}
7223
7224/* Called at device open time to get the chip ready for
7225 * packet processing. Invoked with tp->lock held.
7226 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007227static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229 tg3_switch_clocks(tp);
7230
7231 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
7232
Matt Carlson2f751b62008-08-04 23:17:34 -07007233 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234}
7235
7236#define TG3_STAT_ADD32(PSTAT, REG) \
7237do { u32 __val = tr32(REG); \
7238 (PSTAT)->low += __val; \
7239 if ((PSTAT)->low < __val) \
7240 (PSTAT)->high += 1; \
7241} while (0)
7242
7243static void tg3_periodic_fetch_stats(struct tg3 *tp)
7244{
7245 struct tg3_hw_stats *sp = tp->hw_stats;
7246
7247 if (!netif_carrier_ok(tp->dev))
7248 return;
7249
7250 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
7251 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
7252 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
7253 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
7254 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
7255 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
7256 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
7257 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
7258 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
7259 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
7260 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
7261 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
7262 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
7263
7264 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
7265 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
7266 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
7267 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
7268 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
7269 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
7270 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
7271 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
7272 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
7273 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
7274 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
7275 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
7276 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
7277 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07007278
7279 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
7280 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
7281 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282}
7283
7284static void tg3_timer(unsigned long __opaque)
7285{
7286 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287
Michael Chanf475f162006-03-27 23:20:14 -08007288 if (tp->irq_sync)
7289 goto restart_timer;
7290
David S. Millerf47c11e2005-06-24 20:18:35 -07007291 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292
David S. Millerfac9b832005-05-18 22:46:34 -07007293 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
7294 /* All of this garbage is because when using non-tagged
7295 * IRQ status the mailbox/status_block protocol the chip
7296 * uses with the cpu is race prone.
7297 */
7298 if (tp->hw_status->status & SD_STATUS_UPDATED) {
7299 tw32(GRC_LOCAL_CTRL,
7300 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
7301 } else {
7302 tw32(HOSTCC_MODE, tp->coalesce_mode |
7303 (HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW));
7304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305
David S. Millerfac9b832005-05-18 22:46:34 -07007306 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
7307 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07007308 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07007309 schedule_work(&tp->reset_task);
7310 return;
7311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312 }
7313
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314 /* This part only runs once per second. */
7315 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07007316 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7317 tg3_periodic_fetch_stats(tp);
7318
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
7320 u32 mac_stat;
7321 int phy_event;
7322
7323 mac_stat = tr32(MAC_STATUS);
7324
7325 phy_event = 0;
7326 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) {
7327 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
7328 phy_event = 1;
7329 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
7330 phy_event = 1;
7331
7332 if (phy_event)
7333 tg3_setup_phy(tp, 0);
7334 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
7335 u32 mac_stat = tr32(MAC_STATUS);
7336 int need_setup = 0;
7337
7338 if (netif_carrier_ok(tp->dev) &&
7339 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
7340 need_setup = 1;
7341 }
7342 if (! netif_carrier_ok(tp->dev) &&
7343 (mac_stat & (MAC_STATUS_PCS_SYNCED |
7344 MAC_STATUS_SIGNAL_DET))) {
7345 need_setup = 1;
7346 }
7347 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07007348 if (!tp->serdes_counter) {
7349 tw32_f(MAC_MODE,
7350 (tp->mac_mode &
7351 ~MAC_MODE_PORT_MODE_MASK));
7352 udelay(40);
7353 tw32_f(MAC_MODE, tp->mac_mode);
7354 udelay(40);
7355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356 tg3_setup_phy(tp, 0);
7357 }
Michael Chan747e8f82005-07-25 12:33:22 -07007358 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
7359 tg3_serdes_parallel_detect(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360
7361 tp->timer_counter = tp->timer_multiplier;
7362 }
7363
Michael Chan130b8e42006-09-27 16:00:40 -07007364 /* Heartbeat is only sent once every 2 seconds.
7365 *
7366 * The heartbeat is to tell the ASF firmware that the host
7367 * driver is still alive. In the event that the OS crashes,
7368 * ASF needs to reset the hardware to free up the FIFO space
7369 * that may be filled with rx packets destined for the host.
7370 * If the FIFO is full, ASF will no longer function properly.
7371 *
7372 * Unintended resets have been reported on real time kernels
7373 * where the timer doesn't run on time. Netpoll will also have
7374 * same problem.
7375 *
7376 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
7377 * to check the ring condition when the heartbeat is expiring
7378 * before doing the reset. This will prevent most unintended
7379 * resets.
7380 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07007382 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7383 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007384 tg3_wait_for_event_ack(tp);
7385
Michael Chanbbadf502006-04-06 21:46:34 -07007386 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07007387 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07007388 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Michael Chan28fbef72005-10-26 15:48:35 -07007389 /* 5 seconds timeout */
Michael Chanbbadf502006-04-06 21:46:34 -07007390 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, 5);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007391
7392 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393 }
7394 tp->asf_counter = tp->asf_multiplier;
7395 }
7396
David S. Millerf47c11e2005-06-24 20:18:35 -07007397 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398
Michael Chanf475f162006-03-27 23:20:14 -08007399restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400 tp->timer.expires = jiffies + tp->timer_offset;
7401 add_timer(&tp->timer);
7402}
7403
Adrian Bunk81789ef2006-03-20 23:00:14 -08007404static int tg3_request_irq(struct tg3 *tp)
Michael Chanfcfa0a32006-03-20 22:28:41 -08007405{
David Howells7d12e782006-10-05 14:55:46 +01007406 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007407 unsigned long flags;
7408 struct net_device *dev = tp->dev;
7409
7410 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7411 fn = tg3_msi;
7412 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
7413 fn = tg3_msi_1shot;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07007414 flags = IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007415 } else {
7416 fn = tg3_interrupt;
7417 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
7418 fn = tg3_interrupt_tagged;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07007419 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007420 }
7421 return (request_irq(tp->pdev->irq, fn, flags, dev->name, dev));
7422}
7423
Michael Chan79381092005-04-21 17:13:59 -07007424static int tg3_test_interrupt(struct tg3 *tp)
7425{
7426 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07007427 int err, i, intr_ok = 0;
Michael Chan79381092005-04-21 17:13:59 -07007428
Michael Chand4bc3922005-05-29 14:59:20 -07007429 if (!netif_running(dev))
7430 return -ENODEV;
7431
Michael Chan79381092005-04-21 17:13:59 -07007432 tg3_disable_ints(tp);
7433
7434 free_irq(tp->pdev->irq, dev);
7435
7436 err = request_irq(tp->pdev->irq, tg3_test_isr,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07007437 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
Michael Chan79381092005-04-21 17:13:59 -07007438 if (err)
7439 return err;
7440
Michael Chan38f38432005-09-05 17:53:32 -07007441 tp->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07007442 tg3_enable_ints(tp);
7443
7444 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
7445 HOSTCC_MODE_NOW);
7446
7447 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07007448 u32 int_mbox, misc_host_ctrl;
7449
Michael Chan09ee9292005-08-09 20:17:00 -07007450 int_mbox = tr32_mailbox(MAILBOX_INTERRUPT_0 +
7451 TG3_64BIT_REG_LOW);
Michael Chanb16250e2006-09-27 16:10:14 -07007452 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
7453
7454 if ((int_mbox != 0) ||
7455 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
7456 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07007457 break;
Michael Chanb16250e2006-09-27 16:10:14 -07007458 }
7459
Michael Chan79381092005-04-21 17:13:59 -07007460 msleep(10);
7461 }
7462
7463 tg3_disable_ints(tp);
7464
7465 free_irq(tp->pdev->irq, dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04007466
Michael Chanfcfa0a32006-03-20 22:28:41 -08007467 err = tg3_request_irq(tp);
Michael Chan79381092005-04-21 17:13:59 -07007468
7469 if (err)
7470 return err;
7471
Michael Chanb16250e2006-09-27 16:10:14 -07007472 if (intr_ok)
Michael Chan79381092005-04-21 17:13:59 -07007473 return 0;
7474
7475 return -EIO;
7476}
7477
7478/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
7479 * successfully restored
7480 */
7481static int tg3_test_msi(struct tg3 *tp)
7482{
7483 struct net_device *dev = tp->dev;
7484 int err;
7485 u16 pci_cmd;
7486
7487 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
7488 return 0;
7489
7490 /* Turn off SERR reporting in case MSI terminates with Master
7491 * Abort.
7492 */
7493 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
7494 pci_write_config_word(tp->pdev, PCI_COMMAND,
7495 pci_cmd & ~PCI_COMMAND_SERR);
7496
7497 err = tg3_test_interrupt(tp);
7498
7499 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
7500
7501 if (!err)
7502 return 0;
7503
7504 /* other failures */
7505 if (err != -EIO)
7506 return err;
7507
7508 /* MSI test failed, go back to INTx mode */
7509 printk(KERN_WARNING PFX "%s: No interrupt was generated using MSI, "
7510 "switching to INTx mode. Please report this failure to "
7511 "the PCI maintainer and include system chipset information.\n",
7512 tp->dev->name);
7513
7514 free_irq(tp->pdev->irq, dev);
7515 pci_disable_msi(tp->pdev);
7516
7517 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
7518
Michael Chanfcfa0a32006-03-20 22:28:41 -08007519 err = tg3_request_irq(tp);
Michael Chan79381092005-04-21 17:13:59 -07007520 if (err)
7521 return err;
7522
7523 /* Need to reset the chip because the MSI cycle may have terminated
7524 * with Master Abort.
7525 */
David S. Millerf47c11e2005-06-24 20:18:35 -07007526 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07007527
Michael Chan944d9802005-05-29 14:57:48 -07007528 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007529 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07007530
David S. Millerf47c11e2005-06-24 20:18:35 -07007531 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07007532
7533 if (err)
7534 free_irq(tp->pdev->irq, dev);
7535
7536 return err;
7537}
7538
Matt Carlson9e9fd122009-01-19 16:57:45 -08007539static int tg3_request_firmware(struct tg3 *tp)
7540{
7541 const __be32 *fw_data;
7542
7543 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
7544 printk(KERN_ERR "%s: Failed to load firmware \"%s\"\n",
7545 tp->dev->name, tp->fw_needed);
7546 return -ENOENT;
7547 }
7548
7549 fw_data = (void *)tp->fw->data;
7550
7551 /* Firmware blob starts with version numbers, followed by
7552 * start address and _full_ length including BSS sections
7553 * (which must be longer than the actual data, of course
7554 */
7555
7556 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
7557 if (tp->fw_len < (tp->fw->size - 12)) {
7558 printk(KERN_ERR "%s: bogus length %d in \"%s\"\n",
7559 tp->dev->name, tp->fw_len, tp->fw_needed);
7560 release_firmware(tp->fw);
7561 tp->fw = NULL;
7562 return -EINVAL;
7563 }
7564
7565 /* We no longer need firmware; we have it. */
7566 tp->fw_needed = NULL;
7567 return 0;
7568}
7569
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570static int tg3_open(struct net_device *dev)
7571{
7572 struct tg3 *tp = netdev_priv(dev);
7573 int err;
7574
Matt Carlson9e9fd122009-01-19 16:57:45 -08007575 if (tp->fw_needed) {
7576 err = tg3_request_firmware(tp);
7577 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
7578 if (err)
7579 return err;
7580 } else if (err) {
7581 printk(KERN_WARNING "%s: TSO capability disabled.\n",
7582 tp->dev->name);
7583 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
7584 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
7585 printk(KERN_NOTICE "%s: TSO capability restored.\n",
7586 tp->dev->name);
7587 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
7588 }
7589 }
7590
Michael Chanc49a1562006-12-17 17:07:29 -08007591 netif_carrier_off(tp->dev);
7592
Michael Chanbc1c7562006-03-20 17:48:03 -08007593 err = tg3_set_power_state(tp, PCI_D0);
Matt Carlson2f751b62008-08-04 23:17:34 -07007594 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08007595 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07007596
7597 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08007598
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599 tg3_disable_ints(tp);
7600 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
7601
David S. Millerf47c11e2005-06-24 20:18:35 -07007602 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007603
7604 /* The placement of this call is tied
7605 * to the setup and use of Host TX descriptors.
7606 */
7607 err = tg3_alloc_consistent(tp);
7608 if (err)
7609 return err;
7610
Michael Chan7544b092007-05-05 13:08:32 -07007611 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) {
David S. Millerfac9b832005-05-18 22:46:34 -07007612 /* All MSI supporting chips should support tagged
7613 * status. Assert that this is the case.
7614 */
7615 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
7616 printk(KERN_WARNING PFX "%s: MSI without TAGGED? "
7617 "Not using MSI.\n", tp->dev->name);
7618 } else if (pci_enable_msi(tp->pdev) == 0) {
Michael Chan88b06bc22005-04-21 17:13:25 -07007619 u32 msi_mode;
7620
7621 msi_mode = tr32(MSGINT_MODE);
7622 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
7623 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
7624 }
7625 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08007626 err = tg3_request_irq(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627
7628 if (err) {
Michael Chan88b06bc22005-04-21 17:13:25 -07007629 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7630 pci_disable_msi(tp->pdev);
7631 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
7632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633 tg3_free_consistent(tp);
7634 return err;
7635 }
7636
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007637 napi_enable(&tp->napi);
7638
David S. Millerf47c11e2005-06-24 20:18:35 -07007639 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007641 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007642 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07007643 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644 tg3_free_rings(tp);
7645 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07007646 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
7647 tp->timer_offset = HZ;
7648 else
7649 tp->timer_offset = HZ / 10;
7650
7651 BUG_ON(tp->timer_offset > HZ);
7652 tp->timer_counter = tp->timer_multiplier =
7653 (HZ / tp->timer_offset);
7654 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07007655 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656
7657 init_timer(&tp->timer);
7658 tp->timer.expires = jiffies + tp->timer_offset;
7659 tp->timer.data = (unsigned long) tp;
7660 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661 }
7662
David S. Millerf47c11e2005-06-24 20:18:35 -07007663 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664
7665 if (err) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007666 napi_disable(&tp->napi);
Michael Chan88b06bc22005-04-21 17:13:25 -07007667 free_irq(tp->pdev->irq, dev);
7668 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7669 pci_disable_msi(tp->pdev);
7670 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
7671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672 tg3_free_consistent(tp);
7673 return err;
7674 }
7675
Michael Chan79381092005-04-21 17:13:59 -07007676 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7677 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07007678
Michael Chan79381092005-04-21 17:13:59 -07007679 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07007680 tg3_full_lock(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07007681
7682 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7683 pci_disable_msi(tp->pdev);
7684 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
7685 }
Michael Chan944d9802005-05-29 14:57:48 -07007686 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07007687 tg3_free_rings(tp);
7688 tg3_free_consistent(tp);
7689
David S. Millerf47c11e2005-06-24 20:18:35 -07007690 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07007691
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007692 napi_disable(&tp->napi);
7693
Michael Chan79381092005-04-21 17:13:59 -07007694 return err;
7695 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08007696
7697 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7698 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI) {
Michael Chanb5d37722006-09-27 16:06:21 -07007699 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007700
Michael Chanb5d37722006-09-27 16:06:21 -07007701 tw32(PCIE_TRANSACTION_CFG,
7702 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007703 }
7704 }
Michael Chan79381092005-04-21 17:13:59 -07007705 }
7706
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007707 tg3_phy_start(tp);
7708
David S. Millerf47c11e2005-06-24 20:18:35 -07007709 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007710
Michael Chan79381092005-04-21 17:13:59 -07007711 add_timer(&tp->timer);
7712 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007713 tg3_enable_ints(tp);
7714
David S. Millerf47c11e2005-06-24 20:18:35 -07007715 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007716
7717 netif_start_queue(dev);
7718
7719 return 0;
7720}
7721
7722#if 0
7723/*static*/ void tg3_dump_state(struct tg3 *tp)
7724{
7725 u32 val32, val32_2, val32_3, val32_4, val32_5;
7726 u16 val16;
7727 int i;
7728
7729 pci_read_config_word(tp->pdev, PCI_STATUS, &val16);
7730 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE, &val32);
7731 printk("DEBUG: PCI status [%04x] TG3PCI state[%08x]\n",
7732 val16, val32);
7733
7734 /* MAC block */
7735 printk("DEBUG: MAC_MODE[%08x] MAC_STATUS[%08x]\n",
7736 tr32(MAC_MODE), tr32(MAC_STATUS));
7737 printk(" MAC_EVENT[%08x] MAC_LED_CTRL[%08x]\n",
7738 tr32(MAC_EVENT), tr32(MAC_LED_CTRL));
7739 printk("DEBUG: MAC_TX_MODE[%08x] MAC_TX_STATUS[%08x]\n",
7740 tr32(MAC_TX_MODE), tr32(MAC_TX_STATUS));
7741 printk(" MAC_RX_MODE[%08x] MAC_RX_STATUS[%08x]\n",
7742 tr32(MAC_RX_MODE), tr32(MAC_RX_STATUS));
7743
7744 /* Send data initiator control block */
7745 printk("DEBUG: SNDDATAI_MODE[%08x] SNDDATAI_STATUS[%08x]\n",
7746 tr32(SNDDATAI_MODE), tr32(SNDDATAI_STATUS));
7747 printk(" SNDDATAI_STATSCTRL[%08x]\n",
7748 tr32(SNDDATAI_STATSCTRL));
7749
7750 /* Send data completion control block */
7751 printk("DEBUG: SNDDATAC_MODE[%08x]\n", tr32(SNDDATAC_MODE));
7752
7753 /* Send BD ring selector block */
7754 printk("DEBUG: SNDBDS_MODE[%08x] SNDBDS_STATUS[%08x]\n",
7755 tr32(SNDBDS_MODE), tr32(SNDBDS_STATUS));
7756
7757 /* Send BD initiator control block */
7758 printk("DEBUG: SNDBDI_MODE[%08x] SNDBDI_STATUS[%08x]\n",
7759 tr32(SNDBDI_MODE), tr32(SNDBDI_STATUS));
7760
7761 /* Send BD completion control block */
7762 printk("DEBUG: SNDBDC_MODE[%08x]\n", tr32(SNDBDC_MODE));
7763
7764 /* Receive list placement control block */
7765 printk("DEBUG: RCVLPC_MODE[%08x] RCVLPC_STATUS[%08x]\n",
7766 tr32(RCVLPC_MODE), tr32(RCVLPC_STATUS));
7767 printk(" RCVLPC_STATSCTRL[%08x]\n",
7768 tr32(RCVLPC_STATSCTRL));
7769
7770 /* Receive data and receive BD initiator control block */
7771 printk("DEBUG: RCVDBDI_MODE[%08x] RCVDBDI_STATUS[%08x]\n",
7772 tr32(RCVDBDI_MODE), tr32(RCVDBDI_STATUS));
7773
7774 /* Receive data completion control block */
7775 printk("DEBUG: RCVDCC_MODE[%08x]\n",
7776 tr32(RCVDCC_MODE));
7777
7778 /* Receive BD initiator control block */
7779 printk("DEBUG: RCVBDI_MODE[%08x] RCVBDI_STATUS[%08x]\n",
7780 tr32(RCVBDI_MODE), tr32(RCVBDI_STATUS));
7781
7782 /* Receive BD completion control block */
7783 printk("DEBUG: RCVCC_MODE[%08x] RCVCC_STATUS[%08x]\n",
7784 tr32(RCVCC_MODE), tr32(RCVCC_STATUS));
7785
7786 /* Receive list selector control block */
7787 printk("DEBUG: RCVLSC_MODE[%08x] RCVLSC_STATUS[%08x]\n",
7788 tr32(RCVLSC_MODE), tr32(RCVLSC_STATUS));
7789
7790 /* Mbuf cluster free block */
7791 printk("DEBUG: MBFREE_MODE[%08x] MBFREE_STATUS[%08x]\n",
7792 tr32(MBFREE_MODE), tr32(MBFREE_STATUS));
7793
7794 /* Host coalescing control block */
7795 printk("DEBUG: HOSTCC_MODE[%08x] HOSTCC_STATUS[%08x]\n",
7796 tr32(HOSTCC_MODE), tr32(HOSTCC_STATUS));
7797 printk("DEBUG: HOSTCC_STATS_BLK_HOST_ADDR[%08x%08x]\n",
7798 tr32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH),
7799 tr32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW));
7800 printk("DEBUG: HOSTCC_STATUS_BLK_HOST_ADDR[%08x%08x]\n",
7801 tr32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH),
7802 tr32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW));
7803 printk("DEBUG: HOSTCC_STATS_BLK_NIC_ADDR[%08x]\n",
7804 tr32(HOSTCC_STATS_BLK_NIC_ADDR));
7805 printk("DEBUG: HOSTCC_STATUS_BLK_NIC_ADDR[%08x]\n",
7806 tr32(HOSTCC_STATUS_BLK_NIC_ADDR));
7807
7808 /* Memory arbiter control block */
7809 printk("DEBUG: MEMARB_MODE[%08x] MEMARB_STATUS[%08x]\n",
7810 tr32(MEMARB_MODE), tr32(MEMARB_STATUS));
7811
7812 /* Buffer manager control block */
7813 printk("DEBUG: BUFMGR_MODE[%08x] BUFMGR_STATUS[%08x]\n",
7814 tr32(BUFMGR_MODE), tr32(BUFMGR_STATUS));
7815 printk("DEBUG: BUFMGR_MB_POOL_ADDR[%08x] BUFMGR_MB_POOL_SIZE[%08x]\n",
7816 tr32(BUFMGR_MB_POOL_ADDR), tr32(BUFMGR_MB_POOL_SIZE));
7817 printk("DEBUG: BUFMGR_DMA_DESC_POOL_ADDR[%08x] "
7818 "BUFMGR_DMA_DESC_POOL_SIZE[%08x]\n",
7819 tr32(BUFMGR_DMA_DESC_POOL_ADDR),
7820 tr32(BUFMGR_DMA_DESC_POOL_SIZE));
7821
7822 /* Read DMA control block */
7823 printk("DEBUG: RDMAC_MODE[%08x] RDMAC_STATUS[%08x]\n",
7824 tr32(RDMAC_MODE), tr32(RDMAC_STATUS));
7825
7826 /* Write DMA control block */
7827 printk("DEBUG: WDMAC_MODE[%08x] WDMAC_STATUS[%08x]\n",
7828 tr32(WDMAC_MODE), tr32(WDMAC_STATUS));
7829
7830 /* DMA completion block */
7831 printk("DEBUG: DMAC_MODE[%08x]\n",
7832 tr32(DMAC_MODE));
7833
7834 /* GRC block */
7835 printk("DEBUG: GRC_MODE[%08x] GRC_MISC_CFG[%08x]\n",
7836 tr32(GRC_MODE), tr32(GRC_MISC_CFG));
7837 printk("DEBUG: GRC_LOCAL_CTRL[%08x]\n",
7838 tr32(GRC_LOCAL_CTRL));
7839
7840 /* TG3_BDINFOs */
7841 printk("DEBUG: RCVDBDI_JUMBO_BD[%08x%08x:%08x:%08x]\n",
7842 tr32(RCVDBDI_JUMBO_BD + 0x0),
7843 tr32(RCVDBDI_JUMBO_BD + 0x4),
7844 tr32(RCVDBDI_JUMBO_BD + 0x8),
7845 tr32(RCVDBDI_JUMBO_BD + 0xc));
7846 printk("DEBUG: RCVDBDI_STD_BD[%08x%08x:%08x:%08x]\n",
7847 tr32(RCVDBDI_STD_BD + 0x0),
7848 tr32(RCVDBDI_STD_BD + 0x4),
7849 tr32(RCVDBDI_STD_BD + 0x8),
7850 tr32(RCVDBDI_STD_BD + 0xc));
7851 printk("DEBUG: RCVDBDI_MINI_BD[%08x%08x:%08x:%08x]\n",
7852 tr32(RCVDBDI_MINI_BD + 0x0),
7853 tr32(RCVDBDI_MINI_BD + 0x4),
7854 tr32(RCVDBDI_MINI_BD + 0x8),
7855 tr32(RCVDBDI_MINI_BD + 0xc));
7856
7857 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x0, &val32);
7858 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x4, &val32_2);
7859 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x8, &val32_3);
7860 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0xc, &val32_4);
7861 printk("DEBUG: SRAM_SEND_RCB_0[%08x%08x:%08x:%08x]\n",
7862 val32, val32_2, val32_3, val32_4);
7863
7864 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x0, &val32);
7865 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x4, &val32_2);
7866 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x8, &val32_3);
7867 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0xc, &val32_4);
7868 printk("DEBUG: SRAM_RCV_RET_RCB_0[%08x%08x:%08x:%08x]\n",
7869 val32, val32_2, val32_3, val32_4);
7870
7871 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x0, &val32);
7872 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x4, &val32_2);
7873 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x8, &val32_3);
7874 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0xc, &val32_4);
7875 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x10, &val32_5);
7876 printk("DEBUG: SRAM_STATUS_BLK[%08x:%08x:%08x:%08x:%08x]\n",
7877 val32, val32_2, val32_3, val32_4, val32_5);
7878
7879 /* SW status block */
7880 printk("DEBUG: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
7881 tp->hw_status->status,
7882 tp->hw_status->status_tag,
7883 tp->hw_status->rx_jumbo_consumer,
7884 tp->hw_status->rx_consumer,
7885 tp->hw_status->rx_mini_consumer,
7886 tp->hw_status->idx[0].rx_producer,
7887 tp->hw_status->idx[0].tx_consumer);
7888
7889 /* SW statistics block */
7890 printk("DEBUG: Host statistics block [%08x:%08x:%08x:%08x]\n",
7891 ((u32 *)tp->hw_stats)[0],
7892 ((u32 *)tp->hw_stats)[1],
7893 ((u32 *)tp->hw_stats)[2],
7894 ((u32 *)tp->hw_stats)[3]);
7895
7896 /* Mailboxes */
7897 printk("DEBUG: SNDHOST_PROD[%08x%08x] SNDNIC_PROD[%08x%08x]\n",
Michael Chan09ee9292005-08-09 20:17:00 -07007898 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + 0x0),
7899 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + 0x4),
7900 tr32_mailbox(MAILBOX_SNDNIC_PROD_IDX_0 + 0x0),
7901 tr32_mailbox(MAILBOX_SNDNIC_PROD_IDX_0 + 0x4));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007902
7903 /* NIC side send descriptors. */
7904 for (i = 0; i < 6; i++) {
7905 unsigned long txd;
7906
7907 txd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_TX_BUFFER_DESC
7908 + (i * sizeof(struct tg3_tx_buffer_desc));
7909 printk("DEBUG: NIC TXD(%d)[%08x:%08x:%08x:%08x]\n",
7910 i,
7911 readl(txd + 0x0), readl(txd + 0x4),
7912 readl(txd + 0x8), readl(txd + 0xc));
7913 }
7914
7915 /* NIC side RX descriptors. */
7916 for (i = 0; i < 6; i++) {
7917 unsigned long rxd;
7918
7919 rxd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_RX_BUFFER_DESC
7920 + (i * sizeof(struct tg3_rx_buffer_desc));
7921 printk("DEBUG: NIC RXD_STD(%d)[0][%08x:%08x:%08x:%08x]\n",
7922 i,
7923 readl(rxd + 0x0), readl(rxd + 0x4),
7924 readl(rxd + 0x8), readl(rxd + 0xc));
7925 rxd += (4 * sizeof(u32));
7926 printk("DEBUG: NIC RXD_STD(%d)[1][%08x:%08x:%08x:%08x]\n",
7927 i,
7928 readl(rxd + 0x0), readl(rxd + 0x4),
7929 readl(rxd + 0x8), readl(rxd + 0xc));
7930 }
7931
7932 for (i = 0; i < 6; i++) {
7933 unsigned long rxd;
7934
7935 rxd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_RX_JUMBO_BUFFER_DESC
7936 + (i * sizeof(struct tg3_rx_buffer_desc));
7937 printk("DEBUG: NIC RXD_JUMBO(%d)[0][%08x:%08x:%08x:%08x]\n",
7938 i,
7939 readl(rxd + 0x0), readl(rxd + 0x4),
7940 readl(rxd + 0x8), readl(rxd + 0xc));
7941 rxd += (4 * sizeof(u32));
7942 printk("DEBUG: NIC RXD_JUMBO(%d)[1][%08x:%08x:%08x:%08x]\n",
7943 i,
7944 readl(rxd + 0x0), readl(rxd + 0x4),
7945 readl(rxd + 0x8), readl(rxd + 0xc));
7946 }
7947}
7948#endif
7949
7950static struct net_device_stats *tg3_get_stats(struct net_device *);
7951static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
7952
7953static int tg3_close(struct net_device *dev)
7954{
7955 struct tg3 *tp = netdev_priv(dev);
7956
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007957 napi_disable(&tp->napi);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07007958 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08007959
Linus Torvalds1da177e2005-04-16 15:20:36 -07007960 netif_stop_queue(dev);
7961
7962 del_timer_sync(&tp->timer);
7963
David S. Millerf47c11e2005-06-24 20:18:35 -07007964 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965#if 0
7966 tg3_dump_state(tp);
7967#endif
7968
7969 tg3_disable_ints(tp);
7970
Michael Chan944d9802005-05-29 14:57:48 -07007971 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07007973 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007974
David S. Millerf47c11e2005-06-24 20:18:35 -07007975 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007976
Michael Chan88b06bc22005-04-21 17:13:25 -07007977 free_irq(tp->pdev->irq, dev);
7978 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7979 pci_disable_msi(tp->pdev);
7980 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
7981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982
7983 memcpy(&tp->net_stats_prev, tg3_get_stats(tp->dev),
7984 sizeof(tp->net_stats_prev));
7985 memcpy(&tp->estats_prev, tg3_get_estats(tp),
7986 sizeof(tp->estats_prev));
7987
7988 tg3_free_consistent(tp);
7989
Michael Chanbc1c7562006-03-20 17:48:03 -08007990 tg3_set_power_state(tp, PCI_D3hot);
7991
7992 netif_carrier_off(tp->dev);
7993
Linus Torvalds1da177e2005-04-16 15:20:36 -07007994 return 0;
7995}
7996
7997static inline unsigned long get_stat64(tg3_stat64_t *val)
7998{
7999 unsigned long ret;
8000
8001#if (BITS_PER_LONG == 32)
8002 ret = val->low;
8003#else
8004 ret = ((u64)val->high << 32) | ((u64)val->low);
8005#endif
8006 return ret;
8007}
8008
Stefan Buehler816f8b82008-08-15 14:10:54 -07008009static inline u64 get_estat64(tg3_stat64_t *val)
8010{
8011 return ((u64)val->high << 32) | ((u64)val->low);
8012}
8013
Linus Torvalds1da177e2005-04-16 15:20:36 -07008014static unsigned long calc_crc_errors(struct tg3 *tp)
8015{
8016 struct tg3_hw_stats *hw_stats = tp->hw_stats;
8017
8018 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
8019 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
8020 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008021 u32 val;
8022
David S. Millerf47c11e2005-06-24 20:18:35 -07008023 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08008024 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
8025 tg3_writephy(tp, MII_TG3_TEST1,
8026 val | MII_TG3_TEST1_CRC_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008027 tg3_readphy(tp, 0x14, &val);
8028 } else
8029 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07008030 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008031
8032 tp->phy_crc_errors += val;
8033
8034 return tp->phy_crc_errors;
8035 }
8036
8037 return get_stat64(&hw_stats->rx_fcs_errors);
8038}
8039
8040#define ESTAT_ADD(member) \
8041 estats->member = old_estats->member + \
Stefan Buehler816f8b82008-08-15 14:10:54 -07008042 get_estat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008043
8044static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
8045{
8046 struct tg3_ethtool_stats *estats = &tp->estats;
8047 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
8048 struct tg3_hw_stats *hw_stats = tp->hw_stats;
8049
8050 if (!hw_stats)
8051 return old_estats;
8052
8053 ESTAT_ADD(rx_octets);
8054 ESTAT_ADD(rx_fragments);
8055 ESTAT_ADD(rx_ucast_packets);
8056 ESTAT_ADD(rx_mcast_packets);
8057 ESTAT_ADD(rx_bcast_packets);
8058 ESTAT_ADD(rx_fcs_errors);
8059 ESTAT_ADD(rx_align_errors);
8060 ESTAT_ADD(rx_xon_pause_rcvd);
8061 ESTAT_ADD(rx_xoff_pause_rcvd);
8062 ESTAT_ADD(rx_mac_ctrl_rcvd);
8063 ESTAT_ADD(rx_xoff_entered);
8064 ESTAT_ADD(rx_frame_too_long_errors);
8065 ESTAT_ADD(rx_jabbers);
8066 ESTAT_ADD(rx_undersize_packets);
8067 ESTAT_ADD(rx_in_length_errors);
8068 ESTAT_ADD(rx_out_length_errors);
8069 ESTAT_ADD(rx_64_or_less_octet_packets);
8070 ESTAT_ADD(rx_65_to_127_octet_packets);
8071 ESTAT_ADD(rx_128_to_255_octet_packets);
8072 ESTAT_ADD(rx_256_to_511_octet_packets);
8073 ESTAT_ADD(rx_512_to_1023_octet_packets);
8074 ESTAT_ADD(rx_1024_to_1522_octet_packets);
8075 ESTAT_ADD(rx_1523_to_2047_octet_packets);
8076 ESTAT_ADD(rx_2048_to_4095_octet_packets);
8077 ESTAT_ADD(rx_4096_to_8191_octet_packets);
8078 ESTAT_ADD(rx_8192_to_9022_octet_packets);
8079
8080 ESTAT_ADD(tx_octets);
8081 ESTAT_ADD(tx_collisions);
8082 ESTAT_ADD(tx_xon_sent);
8083 ESTAT_ADD(tx_xoff_sent);
8084 ESTAT_ADD(tx_flow_control);
8085 ESTAT_ADD(tx_mac_errors);
8086 ESTAT_ADD(tx_single_collisions);
8087 ESTAT_ADD(tx_mult_collisions);
8088 ESTAT_ADD(tx_deferred);
8089 ESTAT_ADD(tx_excessive_collisions);
8090 ESTAT_ADD(tx_late_collisions);
8091 ESTAT_ADD(tx_collide_2times);
8092 ESTAT_ADD(tx_collide_3times);
8093 ESTAT_ADD(tx_collide_4times);
8094 ESTAT_ADD(tx_collide_5times);
8095 ESTAT_ADD(tx_collide_6times);
8096 ESTAT_ADD(tx_collide_7times);
8097 ESTAT_ADD(tx_collide_8times);
8098 ESTAT_ADD(tx_collide_9times);
8099 ESTAT_ADD(tx_collide_10times);
8100 ESTAT_ADD(tx_collide_11times);
8101 ESTAT_ADD(tx_collide_12times);
8102 ESTAT_ADD(tx_collide_13times);
8103 ESTAT_ADD(tx_collide_14times);
8104 ESTAT_ADD(tx_collide_15times);
8105 ESTAT_ADD(tx_ucast_packets);
8106 ESTAT_ADD(tx_mcast_packets);
8107 ESTAT_ADD(tx_bcast_packets);
8108 ESTAT_ADD(tx_carrier_sense_errors);
8109 ESTAT_ADD(tx_discards);
8110 ESTAT_ADD(tx_errors);
8111
8112 ESTAT_ADD(dma_writeq_full);
8113 ESTAT_ADD(dma_write_prioq_full);
8114 ESTAT_ADD(rxbds_empty);
8115 ESTAT_ADD(rx_discards);
8116 ESTAT_ADD(rx_errors);
8117 ESTAT_ADD(rx_threshold_hit);
8118
8119 ESTAT_ADD(dma_readq_full);
8120 ESTAT_ADD(dma_read_prioq_full);
8121 ESTAT_ADD(tx_comp_queue_full);
8122
8123 ESTAT_ADD(ring_set_send_prod_index);
8124 ESTAT_ADD(ring_status_update);
8125 ESTAT_ADD(nic_irqs);
8126 ESTAT_ADD(nic_avoided_irqs);
8127 ESTAT_ADD(nic_tx_threshold_hit);
8128
8129 return estats;
8130}
8131
8132static struct net_device_stats *tg3_get_stats(struct net_device *dev)
8133{
8134 struct tg3 *tp = netdev_priv(dev);
8135 struct net_device_stats *stats = &tp->net_stats;
8136 struct net_device_stats *old_stats = &tp->net_stats_prev;
8137 struct tg3_hw_stats *hw_stats = tp->hw_stats;
8138
8139 if (!hw_stats)
8140 return old_stats;
8141
8142 stats->rx_packets = old_stats->rx_packets +
8143 get_stat64(&hw_stats->rx_ucast_packets) +
8144 get_stat64(&hw_stats->rx_mcast_packets) +
8145 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008146
Linus Torvalds1da177e2005-04-16 15:20:36 -07008147 stats->tx_packets = old_stats->tx_packets +
8148 get_stat64(&hw_stats->tx_ucast_packets) +
8149 get_stat64(&hw_stats->tx_mcast_packets) +
8150 get_stat64(&hw_stats->tx_bcast_packets);
8151
8152 stats->rx_bytes = old_stats->rx_bytes +
8153 get_stat64(&hw_stats->rx_octets);
8154 stats->tx_bytes = old_stats->tx_bytes +
8155 get_stat64(&hw_stats->tx_octets);
8156
8157 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07008158 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008159 stats->tx_errors = old_stats->tx_errors +
8160 get_stat64(&hw_stats->tx_errors) +
8161 get_stat64(&hw_stats->tx_mac_errors) +
8162 get_stat64(&hw_stats->tx_carrier_sense_errors) +
8163 get_stat64(&hw_stats->tx_discards);
8164
8165 stats->multicast = old_stats->multicast +
8166 get_stat64(&hw_stats->rx_mcast_packets);
8167 stats->collisions = old_stats->collisions +
8168 get_stat64(&hw_stats->tx_collisions);
8169
8170 stats->rx_length_errors = old_stats->rx_length_errors +
8171 get_stat64(&hw_stats->rx_frame_too_long_errors) +
8172 get_stat64(&hw_stats->rx_undersize_packets);
8173
8174 stats->rx_over_errors = old_stats->rx_over_errors +
8175 get_stat64(&hw_stats->rxbds_empty);
8176 stats->rx_frame_errors = old_stats->rx_frame_errors +
8177 get_stat64(&hw_stats->rx_align_errors);
8178 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
8179 get_stat64(&hw_stats->tx_discards);
8180 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
8181 get_stat64(&hw_stats->tx_carrier_sense_errors);
8182
8183 stats->rx_crc_errors = old_stats->rx_crc_errors +
8184 calc_crc_errors(tp);
8185
John W. Linville4f63b872005-09-12 14:43:18 -07008186 stats->rx_missed_errors = old_stats->rx_missed_errors +
8187 get_stat64(&hw_stats->rx_discards);
8188
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189 return stats;
8190}
8191
8192static inline u32 calc_crc(unsigned char *buf, int len)
8193{
8194 u32 reg;
8195 u32 tmp;
8196 int j, k;
8197
8198 reg = 0xffffffff;
8199
8200 for (j = 0; j < len; j++) {
8201 reg ^= buf[j];
8202
8203 for (k = 0; k < 8; k++) {
8204 tmp = reg & 0x01;
8205
8206 reg >>= 1;
8207
8208 if (tmp) {
8209 reg ^= 0xedb88320;
8210 }
8211 }
8212 }
8213
8214 return ~reg;
8215}
8216
8217static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8218{
8219 /* accept or reject all multicast frames */
8220 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8221 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8222 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8223 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8224}
8225
8226static void __tg3_set_rx_mode(struct net_device *dev)
8227{
8228 struct tg3 *tp = netdev_priv(dev);
8229 u32 rx_mode;
8230
8231 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8232 RX_MODE_KEEP_VLAN_TAG);
8233
8234 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8235 * flag clear.
8236 */
8237#if TG3_VLAN_TAG_USED
8238 if (!tp->vlgrp &&
8239 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
8240 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8241#else
8242 /* By definition, VLAN is disabled always in this
8243 * case.
8244 */
8245 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
8246 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8247#endif
8248
8249 if (dev->flags & IFF_PROMISC) {
8250 /* Promiscuous mode. */
8251 rx_mode |= RX_MODE_PROMISC;
8252 } else if (dev->flags & IFF_ALLMULTI) {
8253 /* Accept all multicast. */
8254 tg3_set_multi (tp, 1);
8255 } else if (dev->mc_count < 1) {
8256 /* Reject all multicast. */
8257 tg3_set_multi (tp, 0);
8258 } else {
8259 /* Accept one or more multicast(s). */
8260 struct dev_mc_list *mclist;
8261 unsigned int i;
8262 u32 mc_filter[4] = { 0, };
8263 u32 regidx;
8264 u32 bit;
8265 u32 crc;
8266
8267 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
8268 i++, mclist = mclist->next) {
8269
8270 crc = calc_crc (mclist->dmi_addr, ETH_ALEN);
8271 bit = ~crc & 0x7f;
8272 regidx = (bit & 0x60) >> 5;
8273 bit &= 0x1f;
8274 mc_filter[regidx] |= (1 << bit);
8275 }
8276
8277 tw32(MAC_HASH_REG_0, mc_filter[0]);
8278 tw32(MAC_HASH_REG_1, mc_filter[1]);
8279 tw32(MAC_HASH_REG_2, mc_filter[2]);
8280 tw32(MAC_HASH_REG_3, mc_filter[3]);
8281 }
8282
8283 if (rx_mode != tp->rx_mode) {
8284 tp->rx_mode = rx_mode;
8285 tw32_f(MAC_RX_MODE, rx_mode);
8286 udelay(10);
8287 }
8288}
8289
8290static void tg3_set_rx_mode(struct net_device *dev)
8291{
8292 struct tg3 *tp = netdev_priv(dev);
8293
Michael Chane75f7c92006-03-20 21:33:26 -08008294 if (!netif_running(dev))
8295 return;
8296
David S. Millerf47c11e2005-06-24 20:18:35 -07008297 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008298 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07008299 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008300}
8301
8302#define TG3_REGDUMP_LEN (32 * 1024)
8303
8304static int tg3_get_regs_len(struct net_device *dev)
8305{
8306 return TG3_REGDUMP_LEN;
8307}
8308
8309static void tg3_get_regs(struct net_device *dev,
8310 struct ethtool_regs *regs, void *_p)
8311{
8312 u32 *p = _p;
8313 struct tg3 *tp = netdev_priv(dev);
8314 u8 *orig_p = _p;
8315 int i;
8316
8317 regs->version = 0;
8318
8319 memset(p, 0, TG3_REGDUMP_LEN);
8320
Michael Chanbc1c7562006-03-20 17:48:03 -08008321 if (tp->link_config.phy_is_low_power)
8322 return;
8323
David S. Millerf47c11e2005-06-24 20:18:35 -07008324 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008325
8326#define __GET_REG32(reg) (*(p)++ = tr32(reg))
8327#define GET_REG32_LOOP(base,len) \
8328do { p = (u32 *)(orig_p + (base)); \
8329 for (i = 0; i < len; i += 4) \
8330 __GET_REG32((base) + i); \
8331} while (0)
8332#define GET_REG32_1(reg) \
8333do { p = (u32 *)(orig_p + (reg)); \
8334 __GET_REG32((reg)); \
8335} while (0)
8336
8337 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
8338 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
8339 GET_REG32_LOOP(MAC_MODE, 0x4f0);
8340 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
8341 GET_REG32_1(SNDDATAC_MODE);
8342 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
8343 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
8344 GET_REG32_1(SNDBDC_MODE);
8345 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
8346 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
8347 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
8348 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
8349 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
8350 GET_REG32_1(RCVDCC_MODE);
8351 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
8352 GET_REG32_LOOP(RCVCC_MODE, 0x14);
8353 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
8354 GET_REG32_1(MBFREE_MODE);
8355 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
8356 GET_REG32_LOOP(MEMARB_MODE, 0x10);
8357 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
8358 GET_REG32_LOOP(RDMAC_MODE, 0x08);
8359 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08008360 GET_REG32_1(RX_CPU_MODE);
8361 GET_REG32_1(RX_CPU_STATE);
8362 GET_REG32_1(RX_CPU_PGMCTR);
8363 GET_REG32_1(RX_CPU_HWBKPT);
8364 GET_REG32_1(TX_CPU_MODE);
8365 GET_REG32_1(TX_CPU_STATE);
8366 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008367 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
8368 GET_REG32_LOOP(FTQ_RESET, 0x120);
8369 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
8370 GET_REG32_1(DMAC_MODE);
8371 GET_REG32_LOOP(GRC_MODE, 0x4c);
8372 if (tp->tg3_flags & TG3_FLAG_NVRAM)
8373 GET_REG32_LOOP(NVRAM_CMD, 0x24);
8374
8375#undef __GET_REG32
8376#undef GET_REG32_LOOP
8377#undef GET_REG32_1
8378
David S. Millerf47c11e2005-06-24 20:18:35 -07008379 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008380}
8381
8382static int tg3_get_eeprom_len(struct net_device *dev)
8383{
8384 struct tg3 *tp = netdev_priv(dev);
8385
8386 return tp->nvram_size;
8387}
8388
8389static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val);
Al Virob9fc7dc2007-12-17 22:59:57 -08008390static int tg3_nvram_read_le(struct tg3 *tp, u32 offset, __le32 *val);
Michael Chan18201802006-03-20 22:29:15 -08008391static int tg3_nvram_read_swab(struct tg3 *tp, u32 offset, u32 *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008392
8393static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
8394{
8395 struct tg3 *tp = netdev_priv(dev);
8396 int ret;
8397 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08008398 u32 i, offset, len, b_offset, b_count;
8399 __le32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008400
Michael Chanbc1c7562006-03-20 17:48:03 -08008401 if (tp->link_config.phy_is_low_power)
8402 return -EAGAIN;
8403
Linus Torvalds1da177e2005-04-16 15:20:36 -07008404 offset = eeprom->offset;
8405 len = eeprom->len;
8406 eeprom->len = 0;
8407
8408 eeprom->magic = TG3_EEPROM_MAGIC;
8409
8410 if (offset & 3) {
8411 /* adjustments to start on required 4 byte boundary */
8412 b_offset = offset & 3;
8413 b_count = 4 - b_offset;
8414 if (b_count > len) {
8415 /* i.e. offset=1 len=2 */
8416 b_count = len;
8417 }
Al Virob9fc7dc2007-12-17 22:59:57 -08008418 ret = tg3_nvram_read_le(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008419 if (ret)
8420 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008421 memcpy(data, ((char*)&val) + b_offset, b_count);
8422 len -= b_count;
8423 offset += b_count;
8424 eeprom->len += b_count;
8425 }
8426
8427 /* read bytes upto the last 4 byte boundary */
8428 pd = &data[eeprom->len];
8429 for (i = 0; i < (len - (len & 3)); i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -08008430 ret = tg3_nvram_read_le(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008431 if (ret) {
8432 eeprom->len += i;
8433 return ret;
8434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008435 memcpy(pd + i, &val, 4);
8436 }
8437 eeprom->len += i;
8438
8439 if (len & 3) {
8440 /* read last bytes not ending on 4 byte boundary */
8441 pd = &data[eeprom->len];
8442 b_count = len & 3;
8443 b_offset = offset + len - b_count;
Al Virob9fc7dc2007-12-17 22:59:57 -08008444 ret = tg3_nvram_read_le(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008445 if (ret)
8446 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08008447 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008448 eeprom->len += b_count;
8449 }
8450 return 0;
8451}
8452
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008453static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008454
8455static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
8456{
8457 struct tg3 *tp = netdev_priv(dev);
8458 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08008459 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008460 u8 *buf;
Al Virob9fc7dc2007-12-17 22:59:57 -08008461 __le32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008462
Michael Chanbc1c7562006-03-20 17:48:03 -08008463 if (tp->link_config.phy_is_low_power)
8464 return -EAGAIN;
8465
Linus Torvalds1da177e2005-04-16 15:20:36 -07008466 if (eeprom->magic != TG3_EEPROM_MAGIC)
8467 return -EINVAL;
8468
8469 offset = eeprom->offset;
8470 len = eeprom->len;
8471
8472 if ((b_offset = (offset & 3))) {
8473 /* adjustments to start on required 4 byte boundary */
Al Virob9fc7dc2007-12-17 22:59:57 -08008474 ret = tg3_nvram_read_le(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008475 if (ret)
8476 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008477 len += b_offset;
8478 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07008479 if (len < 4)
8480 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008481 }
8482
8483 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07008484 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485 /* adjustments to end on required 4 byte boundary */
8486 odd_len = 1;
8487 len = (len + 3) & ~3;
Al Virob9fc7dc2007-12-17 22:59:57 -08008488 ret = tg3_nvram_read_le(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008489 if (ret)
8490 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008491 }
8492
8493 buf = data;
8494 if (b_offset || odd_len) {
8495 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008496 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008497 return -ENOMEM;
8498 if (b_offset)
8499 memcpy(buf, &start, 4);
8500 if (odd_len)
8501 memcpy(buf+len-4, &end, 4);
8502 memcpy(buf + b_offset, data, eeprom->len);
8503 }
8504
8505 ret = tg3_nvram_write_block(tp, offset, len, buf);
8506
8507 if (buf != data)
8508 kfree(buf);
8509
8510 return ret;
8511}
8512
8513static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
8514{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008515 struct tg3 *tp = netdev_priv(dev);
8516
8517 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
8518 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
8519 return -EAGAIN;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07008520 return phy_ethtool_gset(tp->mdio_bus->phy_map[PHY_ADDR], cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008521 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008522
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523 cmd->supported = (SUPPORTED_Autoneg);
8524
8525 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
8526 cmd->supported |= (SUPPORTED_1000baseT_Half |
8527 SUPPORTED_1000baseT_Full);
8528
Karsten Keilef348142006-05-12 12:49:08 -07008529 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008530 cmd->supported |= (SUPPORTED_100baseT_Half |
8531 SUPPORTED_100baseT_Full |
8532 SUPPORTED_10baseT_Half |
8533 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08008534 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07008535 cmd->port = PORT_TP;
8536 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008537 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07008538 cmd->port = PORT_FIBRE;
8539 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008540
Linus Torvalds1da177e2005-04-16 15:20:36 -07008541 cmd->advertising = tp->link_config.advertising;
8542 if (netif_running(dev)) {
8543 cmd->speed = tp->link_config.active_speed;
8544 cmd->duplex = tp->link_config.active_duplex;
8545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546 cmd->phy_address = PHY_ADDR;
8547 cmd->transceiver = 0;
8548 cmd->autoneg = tp->link_config.autoneg;
8549 cmd->maxtxpkt = 0;
8550 cmd->maxrxpkt = 0;
8551 return 0;
8552}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008553
Linus Torvalds1da177e2005-04-16 15:20:36 -07008554static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
8555{
8556 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008557
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008558 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
8559 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
8560 return -EAGAIN;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07008561 return phy_ethtool_sset(tp->mdio_bus->phy_map[PHY_ADDR], cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008562 }
8563
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008564 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008565 /* These are the only valid advertisement bits allowed. */
8566 if (cmd->autoneg == AUTONEG_ENABLE &&
8567 (cmd->advertising & ~(ADVERTISED_1000baseT_Half |
8568 ADVERTISED_1000baseT_Full |
8569 ADVERTISED_Autoneg |
8570 ADVERTISED_FIBRE)))
8571 return -EINVAL;
Michael Chan37ff2382005-10-26 15:49:51 -07008572 /* Fiber can only do SPEED_1000. */
8573 else if ((cmd->autoneg != AUTONEG_ENABLE) &&
8574 (cmd->speed != SPEED_1000))
8575 return -EINVAL;
8576 /* Copper cannot force SPEED_1000. */
8577 } else if ((cmd->autoneg != AUTONEG_ENABLE) &&
8578 (cmd->speed == SPEED_1000))
8579 return -EINVAL;
8580 else if ((cmd->speed == SPEED_1000) &&
Matt Carlson0ba11fb2008-06-09 15:40:26 -07008581 (tp->tg3_flags & TG3_FLAG_10_100_ONLY))
Michael Chan37ff2382005-10-26 15:49:51 -07008582 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008583
David S. Millerf47c11e2005-06-24 20:18:35 -07008584 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008585
8586 tp->link_config.autoneg = cmd->autoneg;
8587 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07008588 tp->link_config.advertising = (cmd->advertising |
8589 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008590 tp->link_config.speed = SPEED_INVALID;
8591 tp->link_config.duplex = DUPLEX_INVALID;
8592 } else {
8593 tp->link_config.advertising = 0;
8594 tp->link_config.speed = cmd->speed;
8595 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008596 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008597
Michael Chan24fcad62006-12-17 17:06:46 -08008598 tp->link_config.orig_speed = tp->link_config.speed;
8599 tp->link_config.orig_duplex = tp->link_config.duplex;
8600 tp->link_config.orig_autoneg = tp->link_config.autoneg;
8601
Linus Torvalds1da177e2005-04-16 15:20:36 -07008602 if (netif_running(dev))
8603 tg3_setup_phy(tp, 1);
8604
David S. Millerf47c11e2005-06-24 20:18:35 -07008605 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008606
Linus Torvalds1da177e2005-04-16 15:20:36 -07008607 return 0;
8608}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008609
Linus Torvalds1da177e2005-04-16 15:20:36 -07008610static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
8611{
8612 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008613
Linus Torvalds1da177e2005-04-16 15:20:36 -07008614 strcpy(info->driver, DRV_MODULE_NAME);
8615 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08008616 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008617 strcpy(info->bus_info, pci_name(tp->pdev));
8618}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008619
Linus Torvalds1da177e2005-04-16 15:20:36 -07008620static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
8621{
8622 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008623
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07008624 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
8625 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07008626 wol->supported = WAKE_MAGIC;
8627 else
8628 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008629 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08008630 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
8631 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008632 wol->wolopts = WAKE_MAGIC;
8633 memset(&wol->sopass, 0, sizeof(wol->sopass));
8634}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008635
Linus Torvalds1da177e2005-04-16 15:20:36 -07008636static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
8637{
8638 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07008639 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008640
Linus Torvalds1da177e2005-04-16 15:20:36 -07008641 if (wol->wolopts & ~WAKE_MAGIC)
8642 return -EINVAL;
8643 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07008644 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008645 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008646
David S. Millerf47c11e2005-06-24 20:18:35 -07008647 spin_lock_bh(&tp->lock);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07008648 if (wol->wolopts & WAKE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008649 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07008650 device_set_wakeup_enable(dp, true);
8651 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008652 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07008653 device_set_wakeup_enable(dp, false);
8654 }
David S. Millerf47c11e2005-06-24 20:18:35 -07008655 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008656
Linus Torvalds1da177e2005-04-16 15:20:36 -07008657 return 0;
8658}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008659
Linus Torvalds1da177e2005-04-16 15:20:36 -07008660static u32 tg3_get_msglevel(struct net_device *dev)
8661{
8662 struct tg3 *tp = netdev_priv(dev);
8663 return tp->msg_enable;
8664}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008665
Linus Torvalds1da177e2005-04-16 15:20:36 -07008666static void tg3_set_msglevel(struct net_device *dev, u32 value)
8667{
8668 struct tg3 *tp = netdev_priv(dev);
8669 tp->msg_enable = value;
8670}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008671
Linus Torvalds1da177e2005-04-16 15:20:36 -07008672static int tg3_set_tso(struct net_device *dev, u32 value)
8673{
8674 struct tg3 *tp = netdev_priv(dev);
8675
8676 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
8677 if (value)
8678 return -EINVAL;
8679 return 0;
8680 }
Matt Carlson027455a2008-12-21 20:19:30 -08008681 if ((dev->features & NETIF_F_IPV6_CSUM) &&
8682 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07008683 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -07008684 dev->features |= NETIF_F_TSO6;
Matt Carlson57e69832008-05-25 23:48:31 -07008685 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8686 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
8687 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008688 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8689 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -07008690 dev->features |= NETIF_F_TSO_ECN;
8691 } else
8692 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -07008693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008694 return ethtool_op_set_tso(dev, value);
8695}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008696
Linus Torvalds1da177e2005-04-16 15:20:36 -07008697static int tg3_nway_reset(struct net_device *dev)
8698{
8699 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008700 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008701
Linus Torvalds1da177e2005-04-16 15:20:36 -07008702 if (!netif_running(dev))
8703 return -EAGAIN;
8704
Michael Chanc94e3942005-09-27 12:12:42 -07008705 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
8706 return -EINVAL;
8707
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008708 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
8709 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
8710 return -EAGAIN;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07008711 r = phy_start_aneg(tp->mdio_bus->phy_map[PHY_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008712 } else {
8713 u32 bmcr;
8714
8715 spin_lock_bh(&tp->lock);
8716 r = -EINVAL;
8717 tg3_readphy(tp, MII_BMCR, &bmcr);
8718 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
8719 ((bmcr & BMCR_ANENABLE) ||
8720 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT))) {
8721 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
8722 BMCR_ANENABLE);
8723 r = 0;
8724 }
8725 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008726 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008727
Linus Torvalds1da177e2005-04-16 15:20:36 -07008728 return r;
8729}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008730
Linus Torvalds1da177e2005-04-16 15:20:36 -07008731static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
8732{
8733 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008734
Linus Torvalds1da177e2005-04-16 15:20:36 -07008735 ering->rx_max_pending = TG3_RX_RING_SIZE - 1;
8736 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08008737 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
8738 ering->rx_jumbo_max_pending = TG3_RX_JUMBO_RING_SIZE - 1;
8739 else
8740 ering->rx_jumbo_max_pending = 0;
8741
8742 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008743
8744 ering->rx_pending = tp->rx_pending;
8745 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08008746 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
8747 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
8748 else
8749 ering->rx_jumbo_pending = 0;
8750
Linus Torvalds1da177e2005-04-16 15:20:36 -07008751 ering->tx_pending = tp->tx_pending;
8752}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008753
Linus Torvalds1da177e2005-04-16 15:20:36 -07008754static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
8755{
8756 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07008757 int irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008758
Linus Torvalds1da177e2005-04-16 15:20:36 -07008759 if ((ering->rx_pending > TG3_RX_RING_SIZE - 1) ||
8760 (ering->rx_jumbo_pending > TG3_RX_JUMBO_RING_SIZE - 1) ||
Michael Chanbc3a9252006-10-18 20:55:18 -07008761 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
8762 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -08008763 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -07008764 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008765 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008766
Michael Chanbbe832c2005-06-24 20:20:04 -07008767 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008768 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008769 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07008770 irq_sync = 1;
8771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008772
Michael Chanbbe832c2005-06-24 20:20:04 -07008773 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008774
Linus Torvalds1da177e2005-04-16 15:20:36 -07008775 tp->rx_pending = ering->rx_pending;
8776
8777 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
8778 tp->rx_pending > 63)
8779 tp->rx_pending = 63;
8780 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
8781 tp->tx_pending = ering->tx_pending;
8782
8783 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -07008784 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -07008785 err = tg3_restart_hw(tp, 1);
8786 if (!err)
8787 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008788 }
8789
David S. Millerf47c11e2005-06-24 20:18:35 -07008790 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008791
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008792 if (irq_sync && !err)
8793 tg3_phy_start(tp);
8794
Michael Chanb9ec6c12006-07-25 16:37:27 -07008795 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008796}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008797
Linus Torvalds1da177e2005-04-16 15:20:36 -07008798static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
8799{
8800 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008801
Linus Torvalds1da177e2005-04-16 15:20:36 -07008802 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -08008803
Steve Glendinninge18ce342008-12-16 02:00:00 -08008804 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -08008805 epause->rx_pause = 1;
8806 else
8807 epause->rx_pause = 0;
8808
Steve Glendinninge18ce342008-12-16 02:00:00 -08008809 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -08008810 epause->tx_pause = 1;
8811 else
8812 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008813}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008814
Linus Torvalds1da177e2005-04-16 15:20:36 -07008815static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
8816{
8817 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008818 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008819
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008820 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
8821 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
8822 return -EAGAIN;
8823
8824 if (epause->autoneg) {
8825 u32 newadv;
8826 struct phy_device *phydev;
8827
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07008828 phydev = tp->mdio_bus->phy_map[PHY_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008829
8830 if (epause->rx_pause) {
8831 if (epause->tx_pause)
8832 newadv = ADVERTISED_Pause;
8833 else
8834 newadv = ADVERTISED_Pause |
8835 ADVERTISED_Asym_Pause;
8836 } else if (epause->tx_pause) {
8837 newadv = ADVERTISED_Asym_Pause;
8838 } else
8839 newadv = 0;
8840
8841 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
8842 u32 oldadv = phydev->advertising &
8843 (ADVERTISED_Pause |
8844 ADVERTISED_Asym_Pause);
8845 if (oldadv != newadv) {
8846 phydev->advertising &=
8847 ~(ADVERTISED_Pause |
8848 ADVERTISED_Asym_Pause);
8849 phydev->advertising |= newadv;
8850 err = phy_start_aneg(phydev);
8851 }
8852 } else {
8853 tp->link_config.advertising &=
8854 ~(ADVERTISED_Pause |
8855 ADVERTISED_Asym_Pause);
8856 tp->link_config.advertising |= newadv;
8857 }
8858 } else {
8859 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08008860 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008861 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08008862 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008863
8864 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08008865 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008866 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08008867 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008868
8869 if (netif_running(dev))
8870 tg3_setup_flow_control(tp, 0, 0);
8871 }
8872 } else {
8873 int irq_sync = 0;
8874
8875 if (netif_running(dev)) {
8876 tg3_netif_stop(tp);
8877 irq_sync = 1;
8878 }
8879
8880 tg3_full_lock(tp, irq_sync);
8881
8882 if (epause->autoneg)
8883 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
8884 else
8885 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
8886 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08008887 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008888 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08008889 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008890 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08008891 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008892 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08008893 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008894
8895 if (netif_running(dev)) {
8896 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
8897 err = tg3_restart_hw(tp, 1);
8898 if (!err)
8899 tg3_netif_start(tp);
8900 }
8901
8902 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07008903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008904
Michael Chanb9ec6c12006-07-25 16:37:27 -07008905 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008906}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008907
Linus Torvalds1da177e2005-04-16 15:20:36 -07008908static u32 tg3_get_rx_csum(struct net_device *dev)
8909{
8910 struct tg3 *tp = netdev_priv(dev);
8911 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
8912}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008913
Linus Torvalds1da177e2005-04-16 15:20:36 -07008914static int tg3_set_rx_csum(struct net_device *dev, u32 data)
8915{
8916 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008917
Linus Torvalds1da177e2005-04-16 15:20:36 -07008918 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
8919 if (data != 0)
8920 return -EINVAL;
8921 return 0;
8922 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008923
David S. Millerf47c11e2005-06-24 20:18:35 -07008924 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008925 if (data)
8926 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
8927 else
8928 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -07008929 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008930
Linus Torvalds1da177e2005-04-16 15:20:36 -07008931 return 0;
8932}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008933
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934static int tg3_set_tx_csum(struct net_device *dev, u32 data)
8935{
8936 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008937
Linus Torvalds1da177e2005-04-16 15:20:36 -07008938 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
8939 if (data != 0)
8940 return -EINVAL;
8941 return 0;
8942 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008943
Matt Carlson321d32a2008-11-21 17:22:19 -08008944 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -07008945 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008946 else
Michael Chan9c27dbd2006-03-20 22:28:27 -08008947 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008948
8949 return 0;
8950}
8951
Jeff Garzikb9f2c042007-10-03 18:07:32 -07008952static int tg3_get_sset_count (struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008953{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07008954 switch (sset) {
8955 case ETH_SS_TEST:
8956 return TG3_NUM_TEST;
8957 case ETH_SS_STATS:
8958 return TG3_NUM_STATS;
8959 default:
8960 return -EOPNOTSUPP;
8961 }
Michael Chan4cafd3f2005-05-29 14:56:34 -07008962}
8963
Linus Torvalds1da177e2005-04-16 15:20:36 -07008964static void tg3_get_strings (struct net_device *dev, u32 stringset, u8 *buf)
8965{
8966 switch (stringset) {
8967 case ETH_SS_STATS:
8968 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
8969 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -07008970 case ETH_SS_TEST:
8971 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
8972 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008973 default:
8974 WARN_ON(1); /* we need a WARN() */
8975 break;
8976 }
8977}
8978
Michael Chan4009a932005-09-05 17:52:54 -07008979static int tg3_phys_id(struct net_device *dev, u32 data)
8980{
8981 struct tg3 *tp = netdev_priv(dev);
8982 int i;
8983
8984 if (!netif_running(tp->dev))
8985 return -EAGAIN;
8986
8987 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -08008988 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -07008989
8990 for (i = 0; i < (data * 2); i++) {
8991 if ((i % 2) == 0)
8992 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
8993 LED_CTRL_1000MBPS_ON |
8994 LED_CTRL_100MBPS_ON |
8995 LED_CTRL_10MBPS_ON |
8996 LED_CTRL_TRAFFIC_OVERRIDE |
8997 LED_CTRL_TRAFFIC_BLINK |
8998 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008999
Michael Chan4009a932005-09-05 17:52:54 -07009000 else
9001 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
9002 LED_CTRL_TRAFFIC_OVERRIDE);
9003
9004 if (msleep_interruptible(500))
9005 break;
9006 }
9007 tw32(MAC_LED_CTRL, tp->led_ctrl);
9008 return 0;
9009}
9010
Linus Torvalds1da177e2005-04-16 15:20:36 -07009011static void tg3_get_ethtool_stats (struct net_device *dev,
9012 struct ethtool_stats *estats, u64 *tmp_stats)
9013{
9014 struct tg3 *tp = netdev_priv(dev);
9015 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
9016}
9017
Michael Chan566f86a2005-05-29 14:56:58 -07009018#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -08009019#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
9020#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
9021#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -07009022#define NVRAM_SELFBOOT_HW_SIZE 0x20
9023#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -07009024
9025static int tg3_test_nvram(struct tg3 *tp)
9026{
Al Virob9fc7dc2007-12-17 22:59:57 -08009027 u32 csum, magic;
9028 __le32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009029 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -07009030
Michael Chan18201802006-03-20 22:29:15 -08009031 if (tg3_nvram_read_swab(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -08009032 return -EIO;
9033
Michael Chan1b277772006-03-20 22:27:48 -08009034 if (magic == TG3_EEPROM_MAGIC)
9035 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -07009036 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -08009037 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
9038 TG3_EEPROM_SB_FORMAT_1) {
9039 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
9040 case TG3_EEPROM_SB_REVISION_0:
9041 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
9042 break;
9043 case TG3_EEPROM_SB_REVISION_2:
9044 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
9045 break;
9046 case TG3_EEPROM_SB_REVISION_3:
9047 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
9048 break;
9049 default:
9050 return 0;
9051 }
9052 } else
Michael Chan1b277772006-03-20 22:27:48 -08009053 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -07009054 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
9055 size = NVRAM_SELFBOOT_HW_SIZE;
9056 else
Michael Chan1b277772006-03-20 22:27:48 -08009057 return -EIO;
9058
9059 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -07009060 if (buf == NULL)
9061 return -ENOMEM;
9062
Michael Chan1b277772006-03-20 22:27:48 -08009063 err = -EIO;
9064 for (i = 0, j = 0; i < size; i += 4, j++) {
Al Virob9fc7dc2007-12-17 22:59:57 -08009065 if ((err = tg3_nvram_read_le(tp, i, &buf[j])) != 0)
Michael Chan566f86a2005-05-29 14:56:58 -07009066 break;
Michael Chan566f86a2005-05-29 14:56:58 -07009067 }
Michael Chan1b277772006-03-20 22:27:48 -08009068 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -07009069 goto out;
9070
Michael Chan1b277772006-03-20 22:27:48 -08009071 /* Selfboot format */
Al Virob9fc7dc2007-12-17 22:59:57 -08009072 magic = swab32(le32_to_cpu(buf[0]));
9073 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -07009074 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -08009075 u8 *buf8 = (u8 *) buf, csum8 = 0;
9076
Al Virob9fc7dc2007-12-17 22:59:57 -08009077 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -08009078 TG3_EEPROM_SB_REVISION_2) {
9079 /* For rev 2, the csum doesn't include the MBA. */
9080 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
9081 csum8 += buf8[i];
9082 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
9083 csum8 += buf8[i];
9084 } else {
9085 for (i = 0; i < size; i++)
9086 csum8 += buf8[i];
9087 }
Michael Chan1b277772006-03-20 22:27:48 -08009088
Adrian Bunkad96b482006-04-05 22:21:04 -07009089 if (csum8 == 0) {
9090 err = 0;
9091 goto out;
9092 }
9093
9094 err = -EIO;
9095 goto out;
Michael Chan1b277772006-03-20 22:27:48 -08009096 }
Michael Chan566f86a2005-05-29 14:56:58 -07009097
Al Virob9fc7dc2007-12-17 22:59:57 -08009098 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -07009099 TG3_EEPROM_MAGIC_HW) {
9100 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
9101 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
9102 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -07009103
9104 /* Separate the parity bits and the data bytes. */
9105 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
9106 if ((i == 0) || (i == 8)) {
9107 int l;
9108 u8 msk;
9109
9110 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
9111 parity[k++] = buf8[i] & msk;
9112 i++;
9113 }
9114 else if (i == 16) {
9115 int l;
9116 u8 msk;
9117
9118 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
9119 parity[k++] = buf8[i] & msk;
9120 i++;
9121
9122 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
9123 parity[k++] = buf8[i] & msk;
9124 i++;
9125 }
9126 data[j++] = buf8[i];
9127 }
9128
9129 err = -EIO;
9130 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
9131 u8 hw8 = hweight8(data[i]);
9132
9133 if ((hw8 & 0x1) && parity[i])
9134 goto out;
9135 else if (!(hw8 & 0x1) && !parity[i])
9136 goto out;
9137 }
9138 err = 0;
9139 goto out;
9140 }
9141
Michael Chan566f86a2005-05-29 14:56:58 -07009142 /* Bootstrap checksum at offset 0x10 */
9143 csum = calc_crc((unsigned char *) buf, 0x10);
Al Virob9fc7dc2007-12-17 22:59:57 -08009144 if(csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -07009145 goto out;
9146
9147 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
9148 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Al Virob9fc7dc2007-12-17 22:59:57 -08009149 if (csum != le32_to_cpu(buf[0xfc/4]))
Michael Chan566f86a2005-05-29 14:56:58 -07009150 goto out;
9151
9152 err = 0;
9153
9154out:
9155 kfree(buf);
9156 return err;
9157}
9158
Michael Chanca430072005-05-29 14:57:23 -07009159#define TG3_SERDES_TIMEOUT_SEC 2
9160#define TG3_COPPER_TIMEOUT_SEC 6
9161
9162static int tg3_test_link(struct tg3 *tp)
9163{
9164 int i, max;
9165
9166 if (!netif_running(tp->dev))
9167 return -ENODEV;
9168
Michael Chan4c987482005-09-05 17:52:38 -07009169 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -07009170 max = TG3_SERDES_TIMEOUT_SEC;
9171 else
9172 max = TG3_COPPER_TIMEOUT_SEC;
9173
9174 for (i = 0; i < max; i++) {
9175 if (netif_carrier_ok(tp->dev))
9176 return 0;
9177
9178 if (msleep_interruptible(1000))
9179 break;
9180 }
9181
9182 return -EIO;
9183}
9184
Michael Chana71116d2005-05-29 14:58:11 -07009185/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -08009186static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -07009187{
Michael Chanb16250e2006-09-27 16:10:14 -07009188 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -07009189 u32 offset, read_mask, write_mask, val, save_val, read_val;
9190 static struct {
9191 u16 offset;
9192 u16 flags;
9193#define TG3_FL_5705 0x1
9194#define TG3_FL_NOT_5705 0x2
9195#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -07009196#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -07009197 u32 read_mask;
9198 u32 write_mask;
9199 } reg_tbl[] = {
9200 /* MAC Control Registers */
9201 { MAC_MODE, TG3_FL_NOT_5705,
9202 0x00000000, 0x00ef6f8c },
9203 { MAC_MODE, TG3_FL_5705,
9204 0x00000000, 0x01ef6b8c },
9205 { MAC_STATUS, TG3_FL_NOT_5705,
9206 0x03800107, 0x00000000 },
9207 { MAC_STATUS, TG3_FL_5705,
9208 0x03800100, 0x00000000 },
9209 { MAC_ADDR_0_HIGH, 0x0000,
9210 0x00000000, 0x0000ffff },
9211 { MAC_ADDR_0_LOW, 0x0000,
9212 0x00000000, 0xffffffff },
9213 { MAC_RX_MTU_SIZE, 0x0000,
9214 0x00000000, 0x0000ffff },
9215 { MAC_TX_MODE, 0x0000,
9216 0x00000000, 0x00000070 },
9217 { MAC_TX_LENGTHS, 0x0000,
9218 0x00000000, 0x00003fff },
9219 { MAC_RX_MODE, TG3_FL_NOT_5705,
9220 0x00000000, 0x000007fc },
9221 { MAC_RX_MODE, TG3_FL_5705,
9222 0x00000000, 0x000007dc },
9223 { MAC_HASH_REG_0, 0x0000,
9224 0x00000000, 0xffffffff },
9225 { MAC_HASH_REG_1, 0x0000,
9226 0x00000000, 0xffffffff },
9227 { MAC_HASH_REG_2, 0x0000,
9228 0x00000000, 0xffffffff },
9229 { MAC_HASH_REG_3, 0x0000,
9230 0x00000000, 0xffffffff },
9231
9232 /* Receive Data and Receive BD Initiator Control Registers. */
9233 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
9234 0x00000000, 0xffffffff },
9235 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
9236 0x00000000, 0xffffffff },
9237 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
9238 0x00000000, 0x00000003 },
9239 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
9240 0x00000000, 0xffffffff },
9241 { RCVDBDI_STD_BD+0, 0x0000,
9242 0x00000000, 0xffffffff },
9243 { RCVDBDI_STD_BD+4, 0x0000,
9244 0x00000000, 0xffffffff },
9245 { RCVDBDI_STD_BD+8, 0x0000,
9246 0x00000000, 0xffff0002 },
9247 { RCVDBDI_STD_BD+0xc, 0x0000,
9248 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009249
Michael Chana71116d2005-05-29 14:58:11 -07009250 /* Receive BD Initiator Control Registers. */
9251 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
9252 0x00000000, 0xffffffff },
9253 { RCVBDI_STD_THRESH, TG3_FL_5705,
9254 0x00000000, 0x000003ff },
9255 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
9256 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009257
Michael Chana71116d2005-05-29 14:58:11 -07009258 /* Host Coalescing Control Registers. */
9259 { HOSTCC_MODE, TG3_FL_NOT_5705,
9260 0x00000000, 0x00000004 },
9261 { HOSTCC_MODE, TG3_FL_5705,
9262 0x00000000, 0x000000f6 },
9263 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
9264 0x00000000, 0xffffffff },
9265 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
9266 0x00000000, 0x000003ff },
9267 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
9268 0x00000000, 0xffffffff },
9269 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
9270 0x00000000, 0x000003ff },
9271 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
9272 0x00000000, 0xffffffff },
9273 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
9274 0x00000000, 0x000000ff },
9275 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
9276 0x00000000, 0xffffffff },
9277 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
9278 0x00000000, 0x000000ff },
9279 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
9280 0x00000000, 0xffffffff },
9281 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
9282 0x00000000, 0xffffffff },
9283 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
9284 0x00000000, 0xffffffff },
9285 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
9286 0x00000000, 0x000000ff },
9287 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
9288 0x00000000, 0xffffffff },
9289 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
9290 0x00000000, 0x000000ff },
9291 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
9292 0x00000000, 0xffffffff },
9293 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
9294 0x00000000, 0xffffffff },
9295 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
9296 0x00000000, 0xffffffff },
9297 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
9298 0x00000000, 0xffffffff },
9299 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
9300 0x00000000, 0xffffffff },
9301 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
9302 0xffffffff, 0x00000000 },
9303 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
9304 0xffffffff, 0x00000000 },
9305
9306 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -07009307 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -07009308 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -07009309 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -07009310 0x00000000, 0x007fffff },
9311 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
9312 0x00000000, 0x0000003f },
9313 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
9314 0x00000000, 0x000001ff },
9315 { BUFMGR_MB_HIGH_WATER, 0x0000,
9316 0x00000000, 0x000001ff },
9317 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
9318 0xffffffff, 0x00000000 },
9319 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
9320 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009321
Michael Chana71116d2005-05-29 14:58:11 -07009322 /* Mailbox Registers */
9323 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
9324 0x00000000, 0x000001ff },
9325 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
9326 0x00000000, 0x000001ff },
9327 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
9328 0x00000000, 0x000007ff },
9329 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
9330 0x00000000, 0x000001ff },
9331
9332 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
9333 };
9334
Michael Chanb16250e2006-09-27 16:10:14 -07009335 is_5705 = is_5750 = 0;
9336 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -07009337 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -07009338 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
9339 is_5750 = 1;
9340 }
Michael Chana71116d2005-05-29 14:58:11 -07009341
9342 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
9343 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
9344 continue;
9345
9346 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
9347 continue;
9348
9349 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
9350 (reg_tbl[i].flags & TG3_FL_NOT_5788))
9351 continue;
9352
Michael Chanb16250e2006-09-27 16:10:14 -07009353 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
9354 continue;
9355
Michael Chana71116d2005-05-29 14:58:11 -07009356 offset = (u32) reg_tbl[i].offset;
9357 read_mask = reg_tbl[i].read_mask;
9358 write_mask = reg_tbl[i].write_mask;
9359
9360 /* Save the original register content */
9361 save_val = tr32(offset);
9362
9363 /* Determine the read-only value. */
9364 read_val = save_val & read_mask;
9365
9366 /* Write zero to the register, then make sure the read-only bits
9367 * are not changed and the read/write bits are all zeros.
9368 */
9369 tw32(offset, 0);
9370
9371 val = tr32(offset);
9372
9373 /* Test the read-only and read/write bits. */
9374 if (((val & read_mask) != read_val) || (val & write_mask))
9375 goto out;
9376
9377 /* Write ones to all the bits defined by RdMask and WrMask, then
9378 * make sure the read-only bits are not changed and the
9379 * read/write bits are all ones.
9380 */
9381 tw32(offset, read_mask | write_mask);
9382
9383 val = tr32(offset);
9384
9385 /* Test the read-only bits. */
9386 if ((val & read_mask) != read_val)
9387 goto out;
9388
9389 /* Test the read/write bits. */
9390 if ((val & write_mask) != write_mask)
9391 goto out;
9392
9393 tw32(offset, save_val);
9394 }
9395
9396 return 0;
9397
9398out:
Michael Chan9f88f292006-12-07 00:22:54 -08009399 if (netif_msg_hw(tp))
9400 printk(KERN_ERR PFX "Register test failed at offset %x\n",
9401 offset);
Michael Chana71116d2005-05-29 14:58:11 -07009402 tw32(offset, save_val);
9403 return -EIO;
9404}
9405
Michael Chan7942e1d2005-05-29 14:58:36 -07009406static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
9407{
Arjan van de Venf71e1302006-03-03 21:33:57 -05009408 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -07009409 int i;
9410 u32 j;
9411
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +02009412 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -07009413 for (j = 0; j < len; j += 4) {
9414 u32 val;
9415
9416 tg3_write_mem(tp, offset + j, test_pattern[i]);
9417 tg3_read_mem(tp, offset + j, &val);
9418 if (val != test_pattern[i])
9419 return -EIO;
9420 }
9421 }
9422 return 0;
9423}
9424
9425static int tg3_test_memory(struct tg3 *tp)
9426{
9427 static struct mem_entry {
9428 u32 offset;
9429 u32 len;
9430 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -08009431 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -07009432 { 0x00002000, 0x1c000},
9433 { 0xffffffff, 0x00000}
9434 }, mem_tbl_5705[] = {
9435 { 0x00000100, 0x0000c},
9436 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -07009437 { 0x00004000, 0x00800},
9438 { 0x00006000, 0x01000},
9439 { 0x00008000, 0x02000},
9440 { 0x00010000, 0x0e000},
9441 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -08009442 }, mem_tbl_5755[] = {
9443 { 0x00000200, 0x00008},
9444 { 0x00004000, 0x00800},
9445 { 0x00006000, 0x00800},
9446 { 0x00008000, 0x02000},
9447 { 0x00010000, 0x0c000},
9448 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -07009449 }, mem_tbl_5906[] = {
9450 { 0x00000200, 0x00008},
9451 { 0x00004000, 0x00400},
9452 { 0x00006000, 0x00400},
9453 { 0x00008000, 0x01000},
9454 { 0x00010000, 0x01000},
9455 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -07009456 };
9457 struct mem_entry *mem_tbl;
9458 int err = 0;
9459 int i;
9460
Matt Carlson321d32a2008-11-21 17:22:19 -08009461 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
9462 mem_tbl = mem_tbl_5755;
9463 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
9464 mem_tbl = mem_tbl_5906;
9465 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
9466 mem_tbl = mem_tbl_5705;
9467 else
Michael Chan7942e1d2005-05-29 14:58:36 -07009468 mem_tbl = mem_tbl_570x;
9469
9470 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
9471 if ((err = tg3_do_mem_test(tp, mem_tbl[i].offset,
9472 mem_tbl[i].len)) != 0)
9473 break;
9474 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009475
Michael Chan7942e1d2005-05-29 14:58:36 -07009476 return err;
9477}
9478
Michael Chan9f40dea2005-09-05 17:53:06 -07009479#define TG3_MAC_LOOPBACK 0
9480#define TG3_PHY_LOOPBACK 1
9481
9482static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -07009483{
Michael Chan9f40dea2005-09-05 17:53:06 -07009484 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Michael Chanc76949a2005-05-29 14:58:59 -07009485 u32 desc_idx;
9486 struct sk_buff *skb, *rx_skb;
9487 u8 *tx_data;
9488 dma_addr_t map;
9489 int num_pkts, tx_len, rx_len, i, err;
9490 struct tg3_rx_buffer_desc *desc;
9491
Michael Chan9f40dea2005-09-05 17:53:06 -07009492 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -07009493 /* HW errata - mac loopback fails in some cases on 5780.
9494 * Normal traffic and PHY loopback are not affected by
9495 * errata.
9496 */
9497 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
9498 return 0;
9499
Michael Chan9f40dea2005-09-05 17:53:06 -07009500 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009501 MAC_MODE_PORT_INT_LPBACK;
9502 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
9503 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chan3f7045c2006-09-27 16:02:29 -07009504 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
9505 mac_mode |= MAC_MODE_PORT_MODE_MII;
9506 else
9507 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -07009508 tw32(MAC_MODE, mac_mode);
9509 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -07009510 u32 val;
9511
Michael Chanb16250e2006-09-27 16:10:14 -07009512 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
9513 u32 phytest;
9514
9515 if (!tg3_readphy(tp, MII_TG3_EPHY_TEST, &phytest)) {
9516 u32 phy;
9517
9518 tg3_writephy(tp, MII_TG3_EPHY_TEST,
9519 phytest | MII_TG3_EPHY_SHADOW_EN);
9520 if (!tg3_readphy(tp, 0x1b, &phy))
9521 tg3_writephy(tp, 0x1b, phy & ~0x20);
Michael Chanb16250e2006-09-27 16:10:14 -07009522 tg3_writephy(tp, MII_TG3_EPHY_TEST, phytest);
9523 }
Michael Chan5d64ad32006-12-07 00:19:40 -08009524 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
9525 } else
9526 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -07009527
Matt Carlson9ef8ca92007-07-11 19:48:29 -07009528 tg3_phy_toggle_automdix(tp, 0);
9529
Michael Chan3f7045c2006-09-27 16:02:29 -07009530 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -07009531 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -08009532
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009533 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
Michael Chan5d64ad32006-12-07 00:19:40 -08009534 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb16250e2006-09-27 16:10:14 -07009535 tg3_writephy(tp, MII_TG3_EPHY_PTEST, 0x1800);
Michael Chan5d64ad32006-12-07 00:19:40 -08009536 mac_mode |= MAC_MODE_PORT_MODE_MII;
9537 } else
9538 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -07009539
Michael Chanc94e3942005-09-27 12:12:42 -07009540 /* reset to prevent losing 1st rx packet intermittently */
9541 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
9542 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9543 udelay(10);
9544 tw32_f(MAC_RX_MODE, tp->rx_mode);
9545 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009546 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
9547 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)
9548 mac_mode &= ~MAC_MODE_LINK_POLARITY;
9549 else if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411)
9550 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -08009551 tg3_writephy(tp, MII_TG3_EXT_CTRL,
9552 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
9553 }
Michael Chan9f40dea2005-09-05 17:53:06 -07009554 tw32(MAC_MODE, mac_mode);
Michael Chan9f40dea2005-09-05 17:53:06 -07009555 }
9556 else
9557 return -EINVAL;
Michael Chanc76949a2005-05-29 14:58:59 -07009558
9559 err = -EIO;
9560
Michael Chanc76949a2005-05-29 14:58:59 -07009561 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -07009562 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -07009563 if (!skb)
9564 return -ENOMEM;
9565
Michael Chanc76949a2005-05-29 14:58:59 -07009566 tx_data = skb_put(skb, tx_len);
9567 memcpy(tx_data, tp->dev->dev_addr, 6);
9568 memset(tx_data + 6, 0x0, 8);
9569
9570 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
9571
9572 for (i = 14; i < tx_len; i++)
9573 tx_data[i] = (u8) (i & 0xff);
9574
9575 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
9576
9577 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
9578 HOSTCC_MODE_NOW);
9579
9580 udelay(10);
9581
9582 rx_start_idx = tp->hw_status->idx[0].rx_producer;
9583
Michael Chanc76949a2005-05-29 14:58:59 -07009584 num_pkts = 0;
9585
Michael Chan9f40dea2005-09-05 17:53:06 -07009586 tg3_set_txd(tp, tp->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -07009587
Michael Chan9f40dea2005-09-05 17:53:06 -07009588 tp->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -07009589 num_pkts++;
9590
Michael Chan9f40dea2005-09-05 17:53:06 -07009591 tw32_tx_mbox(MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW,
9592 tp->tx_prod);
Michael Chan09ee9292005-08-09 20:17:00 -07009593 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW);
Michael Chanc76949a2005-05-29 14:58:59 -07009594
9595 udelay(10);
9596
Michael Chan3f7045c2006-09-27 16:02:29 -07009597 /* 250 usec to allow enough time on some 10/100 Mbps devices. */
9598 for (i = 0; i < 25; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -07009599 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
9600 HOSTCC_MODE_NOW);
9601
9602 udelay(10);
9603
9604 tx_idx = tp->hw_status->idx[0].tx_consumer;
9605 rx_idx = tp->hw_status->idx[0].rx_producer;
Michael Chan9f40dea2005-09-05 17:53:06 -07009606 if ((tx_idx == tp->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -07009607 (rx_idx == (rx_start_idx + num_pkts)))
9608 break;
9609 }
9610
9611 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
9612 dev_kfree_skb(skb);
9613
Michael Chan9f40dea2005-09-05 17:53:06 -07009614 if (tx_idx != tp->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -07009615 goto out;
9616
9617 if (rx_idx != rx_start_idx + num_pkts)
9618 goto out;
9619
9620 desc = &tp->rx_rcb[rx_start_idx];
9621 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
9622 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
9623 if (opaque_key != RXD_OPAQUE_RING_STD)
9624 goto out;
9625
9626 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
9627 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
9628 goto out;
9629
9630 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
9631 if (rx_len != tx_len)
9632 goto out;
9633
9634 rx_skb = tp->rx_std_buffers[desc_idx].skb;
9635
9636 map = pci_unmap_addr(&tp->rx_std_buffers[desc_idx], mapping);
9637 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
9638
9639 for (i = 14; i < tx_len; i++) {
9640 if (*(rx_skb->data + i) != (u8) (i & 0xff))
9641 goto out;
9642 }
9643 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009644
Michael Chanc76949a2005-05-29 14:58:59 -07009645 /* tg3_free_rings will unmap and free the rx_skb */
9646out:
9647 return err;
9648}
9649
Michael Chan9f40dea2005-09-05 17:53:06 -07009650#define TG3_MAC_LOOPBACK_FAILED 1
9651#define TG3_PHY_LOOPBACK_FAILED 2
9652#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
9653 TG3_PHY_LOOPBACK_FAILED)
9654
9655static int tg3_test_loopback(struct tg3 *tp)
9656{
9657 int err = 0;
Matt Carlson9936bcf2007-10-10 18:03:07 -07009658 u32 cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -07009659
9660 if (!netif_running(tp->dev))
9661 return TG3_LOOPBACK_FAILED;
9662
Michael Chanb9ec6c12006-07-25 16:37:27 -07009663 err = tg3_reset_hw(tp, 1);
9664 if (err)
9665 return TG3_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -07009666
Matt Carlson6833c042008-11-21 17:18:59 -08009667 /* Turn off gphy autopowerdown. */
9668 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
9669 tg3_phy_toggle_apd(tp, false);
9670
Matt Carlson321d32a2008-11-21 17:22:19 -08009671 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009672 int i;
9673 u32 status;
9674
9675 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
9676
9677 /* Wait for up to 40 microseconds to acquire lock. */
9678 for (i = 0; i < 4; i++) {
9679 status = tr32(TG3_CPMU_MUTEX_GNT);
9680 if (status == CPMU_MUTEX_GNT_DRIVER)
9681 break;
9682 udelay(10);
9683 }
9684
9685 if (status != CPMU_MUTEX_GNT_DRIVER)
9686 return TG3_LOOPBACK_FAILED;
9687
Matt Carlsonb2a5c192008-04-03 21:44:44 -07009688 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -08009689 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -07009690 tw32(TG3_CPMU_CTRL,
9691 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
9692 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -07009693 }
9694
Michael Chan9f40dea2005-09-05 17:53:06 -07009695 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
9696 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -07009697
Matt Carlson321d32a2008-11-21 17:22:19 -08009698 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009699 tw32(TG3_CPMU_CTRL, cpmuctrl);
9700
9701 /* Release the mutex */
9702 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
9703 }
9704
Matt Carlsondd477002008-05-25 23:45:58 -07009705 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
9706 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -07009707 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
9708 err |= TG3_PHY_LOOPBACK_FAILED;
9709 }
9710
Matt Carlson6833c042008-11-21 17:18:59 -08009711 /* Re-enable gphy autopowerdown. */
9712 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
9713 tg3_phy_toggle_apd(tp, true);
9714
Michael Chan9f40dea2005-09-05 17:53:06 -07009715 return err;
9716}
9717
Michael Chan4cafd3f2005-05-29 14:56:34 -07009718static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
9719 u64 *data)
9720{
Michael Chan566f86a2005-05-29 14:56:58 -07009721 struct tg3 *tp = netdev_priv(dev);
9722
Michael Chanbc1c7562006-03-20 17:48:03 -08009723 if (tp->link_config.phy_is_low_power)
9724 tg3_set_power_state(tp, PCI_D0);
9725
Michael Chan566f86a2005-05-29 14:56:58 -07009726 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
9727
9728 if (tg3_test_nvram(tp) != 0) {
9729 etest->flags |= ETH_TEST_FL_FAILED;
9730 data[0] = 1;
9731 }
Michael Chanca430072005-05-29 14:57:23 -07009732 if (tg3_test_link(tp) != 0) {
9733 etest->flags |= ETH_TEST_FL_FAILED;
9734 data[1] = 1;
9735 }
Michael Chana71116d2005-05-29 14:58:11 -07009736 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009737 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -07009738
Michael Chanbbe832c2005-06-24 20:20:04 -07009739 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009740 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009741 tg3_netif_stop(tp);
9742 irq_sync = 1;
9743 }
9744
9745 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -07009746
9747 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -08009748 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -07009749 tg3_halt_cpu(tp, RX_CPU_BASE);
9750 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
9751 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08009752 if (!err)
9753 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -07009754
Michael Chand9ab5ad12006-03-20 22:27:35 -08009755 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
9756 tg3_phy_reset(tp);
9757
Michael Chana71116d2005-05-29 14:58:11 -07009758 if (tg3_test_registers(tp) != 0) {
9759 etest->flags |= ETH_TEST_FL_FAILED;
9760 data[2] = 1;
9761 }
Michael Chan7942e1d2005-05-29 14:58:36 -07009762 if (tg3_test_memory(tp) != 0) {
9763 etest->flags |= ETH_TEST_FL_FAILED;
9764 data[3] = 1;
9765 }
Michael Chan9f40dea2005-09-05 17:53:06 -07009766 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -07009767 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -07009768
David S. Millerf47c11e2005-06-24 20:18:35 -07009769 tg3_full_unlock(tp);
9770
Michael Chand4bc3922005-05-29 14:59:20 -07009771 if (tg3_test_interrupt(tp) != 0) {
9772 etest->flags |= ETH_TEST_FL_FAILED;
9773 data[5] = 1;
9774 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009775
9776 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -07009777
Michael Chana71116d2005-05-29 14:58:11 -07009778 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9779 if (netif_running(dev)) {
9780 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009781 err2 = tg3_restart_hw(tp, 1);
9782 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -07009783 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -07009784 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009785
9786 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009787
9788 if (irq_sync && !err2)
9789 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -07009790 }
Michael Chanbc1c7562006-03-20 17:48:03 -08009791 if (tp->link_config.phy_is_low_power)
9792 tg3_set_power_state(tp, PCI_D3hot);
9793
Michael Chan4cafd3f2005-05-29 14:56:34 -07009794}
9795
Linus Torvalds1da177e2005-04-16 15:20:36 -07009796static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
9797{
9798 struct mii_ioctl_data *data = if_mii(ifr);
9799 struct tg3 *tp = netdev_priv(dev);
9800 int err;
9801
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009802 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9803 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9804 return -EAGAIN;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07009805 return phy_mii_ioctl(tp->mdio_bus->phy_map[PHY_ADDR], data, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009806 }
9807
Linus Torvalds1da177e2005-04-16 15:20:36 -07009808 switch(cmd) {
9809 case SIOCGMIIPHY:
9810 data->phy_id = PHY_ADDR;
9811
9812 /* fallthru */
9813 case SIOCGMIIREG: {
9814 u32 mii_regval;
9815
9816 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
9817 break; /* We have no PHY */
9818
Michael Chanbc1c7562006-03-20 17:48:03 -08009819 if (tp->link_config.phy_is_low_power)
9820 return -EAGAIN;
9821
David S. Millerf47c11e2005-06-24 20:18:35 -07009822 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009823 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -07009824 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009825
9826 data->val_out = mii_regval;
9827
9828 return err;
9829 }
9830
9831 case SIOCSMIIREG:
9832 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
9833 break; /* We have no PHY */
9834
9835 if (!capable(CAP_NET_ADMIN))
9836 return -EPERM;
9837
Michael Chanbc1c7562006-03-20 17:48:03 -08009838 if (tp->link_config.phy_is_low_power)
9839 return -EAGAIN;
9840
David S. Millerf47c11e2005-06-24 20:18:35 -07009841 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009842 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -07009843 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009844
9845 return err;
9846
9847 default:
9848 /* do nothing */
9849 break;
9850 }
9851 return -EOPNOTSUPP;
9852}
9853
9854#if TG3_VLAN_TAG_USED
9855static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
9856{
9857 struct tg3 *tp = netdev_priv(dev);
9858
Michael Chan29315e82006-06-29 20:12:30 -07009859 if (netif_running(dev))
9860 tg3_netif_stop(tp);
9861
David S. Millerf47c11e2005-06-24 20:18:35 -07009862 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009863
9864 tp->vlgrp = grp;
9865
9866 /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
9867 __tg3_set_rx_mode(dev);
9868
Michael Chan29315e82006-06-29 20:12:30 -07009869 if (netif_running(dev))
9870 tg3_netif_start(tp);
Michael Chan46966542007-07-11 19:47:19 -07009871
9872 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009873}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009874#endif
9875
David S. Miller15f98502005-05-18 22:49:26 -07009876static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
9877{
9878 struct tg3 *tp = netdev_priv(dev);
9879
9880 memcpy(ec, &tp->coal, sizeof(*ec));
9881 return 0;
9882}
9883
Michael Chand244c892005-07-05 14:42:33 -07009884static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
9885{
9886 struct tg3 *tp = netdev_priv(dev);
9887 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
9888 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
9889
9890 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
9891 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
9892 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
9893 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
9894 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
9895 }
9896
9897 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
9898 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
9899 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
9900 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
9901 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
9902 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
9903 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
9904 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
9905 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
9906 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
9907 return -EINVAL;
9908
9909 /* No rx interrupts will be generated if both are zero */
9910 if ((ec->rx_coalesce_usecs == 0) &&
9911 (ec->rx_max_coalesced_frames == 0))
9912 return -EINVAL;
9913
9914 /* No tx interrupts will be generated if both are zero */
9915 if ((ec->tx_coalesce_usecs == 0) &&
9916 (ec->tx_max_coalesced_frames == 0))
9917 return -EINVAL;
9918
9919 /* Only copy relevant parameters, ignore all others. */
9920 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
9921 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
9922 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
9923 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
9924 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
9925 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
9926 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
9927 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
9928 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
9929
9930 if (netif_running(dev)) {
9931 tg3_full_lock(tp, 0);
9932 __tg3_set_coalesce(tp, &tp->coal);
9933 tg3_full_unlock(tp);
9934 }
9935 return 0;
9936}
9937
Jeff Garzik7282d492006-09-13 14:30:00 -04009938static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009939 .get_settings = tg3_get_settings,
9940 .set_settings = tg3_set_settings,
9941 .get_drvinfo = tg3_get_drvinfo,
9942 .get_regs_len = tg3_get_regs_len,
9943 .get_regs = tg3_get_regs,
9944 .get_wol = tg3_get_wol,
9945 .set_wol = tg3_set_wol,
9946 .get_msglevel = tg3_get_msglevel,
9947 .set_msglevel = tg3_set_msglevel,
9948 .nway_reset = tg3_nway_reset,
9949 .get_link = ethtool_op_get_link,
9950 .get_eeprom_len = tg3_get_eeprom_len,
9951 .get_eeprom = tg3_get_eeprom,
9952 .set_eeprom = tg3_set_eeprom,
9953 .get_ringparam = tg3_get_ringparam,
9954 .set_ringparam = tg3_set_ringparam,
9955 .get_pauseparam = tg3_get_pauseparam,
9956 .set_pauseparam = tg3_set_pauseparam,
9957 .get_rx_csum = tg3_get_rx_csum,
9958 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009959 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009960 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009961 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -07009962 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009963 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -07009964 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009965 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -07009966 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -07009967 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07009968 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009969};
9970
9971static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
9972{
Michael Chan1b277772006-03-20 22:27:48 -08009973 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009974
9975 tp->nvram_size = EEPROM_CHIP_SIZE;
9976
Michael Chan18201802006-03-20 22:29:15 -08009977 if (tg3_nvram_read_swab(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009978 return;
9979
Michael Chanb16250e2006-09-27 16:10:14 -07009980 if ((magic != TG3_EEPROM_MAGIC) &&
9981 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
9982 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009983 return;
9984
9985 /*
9986 * Size the chip by reading offsets at increasing powers of two.
9987 * When we encounter our validation signature, we know the addressing
9988 * has wrapped around, and thus have our chip size.
9989 */
Michael Chan1b277772006-03-20 22:27:48 -08009990 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009991
9992 while (cursize < tp->nvram_size) {
Michael Chan18201802006-03-20 22:29:15 -08009993 if (tg3_nvram_read_swab(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009994 return;
9995
Michael Chan18201802006-03-20 22:29:15 -08009996 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009997 break;
9998
9999 cursize <<= 1;
10000 }
10001
10002 tp->nvram_size = cursize;
10003}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010004
Linus Torvalds1da177e2005-04-16 15:20:36 -070010005static void __devinit tg3_get_nvram_size(struct tg3 *tp)
10006{
10007 u32 val;
10008
Michael Chan18201802006-03-20 22:29:15 -080010009 if (tg3_nvram_read_swab(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010010 return;
10011
10012 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080010013 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080010014 tg3_get_eeprom_size(tp);
10015 return;
10016 }
10017
Linus Torvalds1da177e2005-04-16 15:20:36 -070010018 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
10019 if (val != 0) {
10020 tp->nvram_size = (val >> 16) * 1024;
10021 return;
10022 }
10023 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010024 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010025}
10026
10027static void __devinit tg3_get_nvram_info(struct tg3 *tp)
10028{
10029 u32 nvcfg1;
10030
10031 nvcfg1 = tr32(NVRAM_CFG1);
10032 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
10033 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10034 }
10035 else {
10036 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
10037 tw32(NVRAM_CFG1, nvcfg1);
10038 }
10039
Michael Chan4c987482005-09-05 17:52:38 -070010040 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070010041 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010042 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
10043 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
10044 tp->nvram_jedecnum = JEDEC_ATMEL;
10045 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
10046 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10047 break;
10048 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
10049 tp->nvram_jedecnum = JEDEC_ATMEL;
10050 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
10051 break;
10052 case FLASH_VENDOR_ATMEL_EEPROM:
10053 tp->nvram_jedecnum = JEDEC_ATMEL;
10054 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
10055 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10056 break;
10057 case FLASH_VENDOR_ST:
10058 tp->nvram_jedecnum = JEDEC_ST;
10059 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
10060 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10061 break;
10062 case FLASH_VENDOR_SAIFUN:
10063 tp->nvram_jedecnum = JEDEC_SAIFUN;
10064 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
10065 break;
10066 case FLASH_VENDOR_SST_SMALL:
10067 case FLASH_VENDOR_SST_LARGE:
10068 tp->nvram_jedecnum = JEDEC_SST;
10069 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
10070 break;
10071 }
10072 }
10073 else {
10074 tp->nvram_jedecnum = JEDEC_ATMEL;
10075 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
10076 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10077 }
10078}
10079
Michael Chan361b4ac2005-04-21 17:11:21 -070010080static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
10081{
10082 u32 nvcfg1;
10083
10084 nvcfg1 = tr32(NVRAM_CFG1);
10085
Michael Chane6af3012005-04-21 17:12:05 -070010086 /* NVRAM protection for TPM */
10087 if (nvcfg1 & (1 << 27))
10088 tp->tg3_flags2 |= TG3_FLG2_PROTECTED_NVRAM;
10089
Michael Chan361b4ac2005-04-21 17:11:21 -070010090 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
10091 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
10092 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
10093 tp->nvram_jedecnum = JEDEC_ATMEL;
10094 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10095 break;
10096 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
10097 tp->nvram_jedecnum = JEDEC_ATMEL;
10098 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10099 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10100 break;
10101 case FLASH_5752VENDOR_ST_M45PE10:
10102 case FLASH_5752VENDOR_ST_M45PE20:
10103 case FLASH_5752VENDOR_ST_M45PE40:
10104 tp->nvram_jedecnum = JEDEC_ST;
10105 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10106 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10107 break;
10108 }
10109
10110 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
10111 switch (nvcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
10112 case FLASH_5752PAGE_SIZE_256:
10113 tp->nvram_pagesize = 256;
10114 break;
10115 case FLASH_5752PAGE_SIZE_512:
10116 tp->nvram_pagesize = 512;
10117 break;
10118 case FLASH_5752PAGE_SIZE_1K:
10119 tp->nvram_pagesize = 1024;
10120 break;
10121 case FLASH_5752PAGE_SIZE_2K:
10122 tp->nvram_pagesize = 2048;
10123 break;
10124 case FLASH_5752PAGE_SIZE_4K:
10125 tp->nvram_pagesize = 4096;
10126 break;
10127 case FLASH_5752PAGE_SIZE_264:
10128 tp->nvram_pagesize = 264;
10129 break;
10130 }
10131 }
10132 else {
10133 /* For eeprom, set pagesize to maximum eeprom size */
10134 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
10135
10136 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
10137 tw32(NVRAM_CFG1, nvcfg1);
10138 }
10139}
10140
Michael Chand3c7b882006-03-23 01:28:25 -080010141static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
10142{
Matt Carlson989a9d22007-05-05 11:51:05 -070010143 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080010144
10145 nvcfg1 = tr32(NVRAM_CFG1);
10146
10147 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070010148 if (nvcfg1 & (1 << 27)) {
Michael Chand3c7b882006-03-23 01:28:25 -080010149 tp->tg3_flags2 |= TG3_FLG2_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070010150 protect = 1;
10151 }
Michael Chand3c7b882006-03-23 01:28:25 -080010152
Matt Carlson989a9d22007-05-05 11:51:05 -070010153 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
10154 switch (nvcfg1) {
Michael Chand3c7b882006-03-23 01:28:25 -080010155 case FLASH_5755VENDOR_ATMEL_FLASH_1:
10156 case FLASH_5755VENDOR_ATMEL_FLASH_2:
10157 case FLASH_5755VENDOR_ATMEL_FLASH_3:
Matt Carlson70b65a22007-07-11 19:48:50 -070010158 case FLASH_5755VENDOR_ATMEL_FLASH_5:
Michael Chand3c7b882006-03-23 01:28:25 -080010159 tp->nvram_jedecnum = JEDEC_ATMEL;
10160 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10161 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10162 tp->nvram_pagesize = 264;
Matt Carlson70b65a22007-07-11 19:48:50 -070010163 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
10164 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010165 tp->nvram_size = (protect ? 0x3e200 :
10166 TG3_NVRAM_SIZE_512KB);
Matt Carlson989a9d22007-05-05 11:51:05 -070010167 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010168 tp->nvram_size = (protect ? 0x1f200 :
10169 TG3_NVRAM_SIZE_256KB);
Matt Carlson989a9d22007-05-05 11:51:05 -070010170 else
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010171 tp->nvram_size = (protect ? 0x1f200 :
10172 TG3_NVRAM_SIZE_128KB);
Michael Chand3c7b882006-03-23 01:28:25 -080010173 break;
10174 case FLASH_5752VENDOR_ST_M45PE10:
10175 case FLASH_5752VENDOR_ST_M45PE20:
10176 case FLASH_5752VENDOR_ST_M45PE40:
10177 tp->nvram_jedecnum = JEDEC_ST;
10178 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10179 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10180 tp->nvram_pagesize = 256;
Matt Carlson989a9d22007-05-05 11:51:05 -070010181 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010182 tp->nvram_size = (protect ?
10183 TG3_NVRAM_SIZE_64KB :
10184 TG3_NVRAM_SIZE_128KB);
Matt Carlson989a9d22007-05-05 11:51:05 -070010185 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010186 tp->nvram_size = (protect ?
10187 TG3_NVRAM_SIZE_64KB :
10188 TG3_NVRAM_SIZE_256KB);
Matt Carlson989a9d22007-05-05 11:51:05 -070010189 else
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010190 tp->nvram_size = (protect ?
10191 TG3_NVRAM_SIZE_128KB :
10192 TG3_NVRAM_SIZE_512KB);
Michael Chand3c7b882006-03-23 01:28:25 -080010193 break;
10194 }
10195}
10196
Michael Chan1b277772006-03-20 22:27:48 -080010197static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
10198{
10199 u32 nvcfg1;
10200
10201 nvcfg1 = tr32(NVRAM_CFG1);
10202
10203 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
10204 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
10205 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
10206 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
10207 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
10208 tp->nvram_jedecnum = JEDEC_ATMEL;
10209 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10210 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
10211
10212 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
10213 tw32(NVRAM_CFG1, nvcfg1);
10214 break;
10215 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
10216 case FLASH_5755VENDOR_ATMEL_FLASH_1:
10217 case FLASH_5755VENDOR_ATMEL_FLASH_2:
10218 case FLASH_5755VENDOR_ATMEL_FLASH_3:
10219 tp->nvram_jedecnum = JEDEC_ATMEL;
10220 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10221 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10222 tp->nvram_pagesize = 264;
10223 break;
10224 case FLASH_5752VENDOR_ST_M45PE10:
10225 case FLASH_5752VENDOR_ST_M45PE20:
10226 case FLASH_5752VENDOR_ST_M45PE40:
10227 tp->nvram_jedecnum = JEDEC_ST;
10228 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10229 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10230 tp->nvram_pagesize = 256;
10231 break;
10232 }
10233}
10234
Matt Carlson6b91fa02007-10-10 18:01:09 -070010235static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
10236{
10237 u32 nvcfg1, protect = 0;
10238
10239 nvcfg1 = tr32(NVRAM_CFG1);
10240
10241 /* NVRAM protection for TPM */
10242 if (nvcfg1 & (1 << 27)) {
10243 tp->tg3_flags2 |= TG3_FLG2_PROTECTED_NVRAM;
10244 protect = 1;
10245 }
10246
10247 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
10248 switch (nvcfg1) {
10249 case FLASH_5761VENDOR_ATMEL_ADB021D:
10250 case FLASH_5761VENDOR_ATMEL_ADB041D:
10251 case FLASH_5761VENDOR_ATMEL_ADB081D:
10252 case FLASH_5761VENDOR_ATMEL_ADB161D:
10253 case FLASH_5761VENDOR_ATMEL_MDB021D:
10254 case FLASH_5761VENDOR_ATMEL_MDB041D:
10255 case FLASH_5761VENDOR_ATMEL_MDB081D:
10256 case FLASH_5761VENDOR_ATMEL_MDB161D:
10257 tp->nvram_jedecnum = JEDEC_ATMEL;
10258 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10259 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10260 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
10261 tp->nvram_pagesize = 256;
10262 break;
10263 case FLASH_5761VENDOR_ST_A_M45PE20:
10264 case FLASH_5761VENDOR_ST_A_M45PE40:
10265 case FLASH_5761VENDOR_ST_A_M45PE80:
10266 case FLASH_5761VENDOR_ST_A_M45PE16:
10267 case FLASH_5761VENDOR_ST_M_M45PE20:
10268 case FLASH_5761VENDOR_ST_M_M45PE40:
10269 case FLASH_5761VENDOR_ST_M_M45PE80:
10270 case FLASH_5761VENDOR_ST_M_M45PE16:
10271 tp->nvram_jedecnum = JEDEC_ST;
10272 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10273 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10274 tp->nvram_pagesize = 256;
10275 break;
10276 }
10277
10278 if (protect) {
10279 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
10280 } else {
10281 switch (nvcfg1) {
10282 case FLASH_5761VENDOR_ATMEL_ADB161D:
10283 case FLASH_5761VENDOR_ATMEL_MDB161D:
10284 case FLASH_5761VENDOR_ST_A_M45PE16:
10285 case FLASH_5761VENDOR_ST_M_M45PE16:
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010286 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
Matt Carlson6b91fa02007-10-10 18:01:09 -070010287 break;
10288 case FLASH_5761VENDOR_ATMEL_ADB081D:
10289 case FLASH_5761VENDOR_ATMEL_MDB081D:
10290 case FLASH_5761VENDOR_ST_A_M45PE80:
10291 case FLASH_5761VENDOR_ST_M_M45PE80:
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010292 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
Matt Carlson6b91fa02007-10-10 18:01:09 -070010293 break;
10294 case FLASH_5761VENDOR_ATMEL_ADB041D:
10295 case FLASH_5761VENDOR_ATMEL_MDB041D:
10296 case FLASH_5761VENDOR_ST_A_M45PE40:
10297 case FLASH_5761VENDOR_ST_M_M45PE40:
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010298 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Matt Carlson6b91fa02007-10-10 18:01:09 -070010299 break;
10300 case FLASH_5761VENDOR_ATMEL_ADB021D:
10301 case FLASH_5761VENDOR_ATMEL_MDB021D:
10302 case FLASH_5761VENDOR_ST_A_M45PE20:
10303 case FLASH_5761VENDOR_ST_M_M45PE20:
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010304 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
Matt Carlson6b91fa02007-10-10 18:01:09 -070010305 break;
10306 }
10307 }
10308}
10309
Michael Chanb5d37722006-09-27 16:06:21 -070010310static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
10311{
10312 tp->nvram_jedecnum = JEDEC_ATMEL;
10313 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10314 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
10315}
10316
Matt Carlson321d32a2008-11-21 17:22:19 -080010317static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
10318{
10319 u32 nvcfg1;
10320
10321 nvcfg1 = tr32(NVRAM_CFG1);
10322
10323 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
10324 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
10325 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
10326 tp->nvram_jedecnum = JEDEC_ATMEL;
10327 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10328 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
10329
10330 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
10331 tw32(NVRAM_CFG1, nvcfg1);
10332 return;
10333 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
10334 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
10335 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
10336 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
10337 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
10338 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
10339 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
10340 tp->nvram_jedecnum = JEDEC_ATMEL;
10341 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10342 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10343
10344 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
10345 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
10346 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
10347 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
10348 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
10349 break;
10350 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
10351 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
10352 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
10353 break;
10354 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
10355 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
10356 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
10357 break;
10358 }
10359 break;
10360 case FLASH_5752VENDOR_ST_M45PE10:
10361 case FLASH_5752VENDOR_ST_M45PE20:
10362 case FLASH_5752VENDOR_ST_M45PE40:
10363 tp->nvram_jedecnum = JEDEC_ST;
10364 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10365 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10366
10367 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
10368 case FLASH_5752VENDOR_ST_M45PE10:
10369 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
10370 break;
10371 case FLASH_5752VENDOR_ST_M45PE20:
10372 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
10373 break;
10374 case FLASH_5752VENDOR_ST_M45PE40:
10375 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
10376 break;
10377 }
10378 break;
10379 default:
10380 return;
10381 }
10382
10383 switch (nvcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
10384 case FLASH_5752PAGE_SIZE_256:
10385 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
10386 tp->nvram_pagesize = 256;
10387 break;
10388 case FLASH_5752PAGE_SIZE_512:
10389 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
10390 tp->nvram_pagesize = 512;
10391 break;
10392 case FLASH_5752PAGE_SIZE_1K:
10393 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
10394 tp->nvram_pagesize = 1024;
10395 break;
10396 case FLASH_5752PAGE_SIZE_2K:
10397 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
10398 tp->nvram_pagesize = 2048;
10399 break;
10400 case FLASH_5752PAGE_SIZE_4K:
10401 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
10402 tp->nvram_pagesize = 4096;
10403 break;
10404 case FLASH_5752PAGE_SIZE_264:
10405 tp->nvram_pagesize = 264;
10406 break;
10407 case FLASH_5752PAGE_SIZE_528:
10408 tp->nvram_pagesize = 528;
10409 break;
10410 }
10411}
10412
Linus Torvalds1da177e2005-04-16 15:20:36 -070010413/* Chips other than 5700/5701 use the NVRAM for fetching info. */
10414static void __devinit tg3_nvram_init(struct tg3 *tp)
10415{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010416 tw32_f(GRC_EEPROM_ADDR,
10417 (EEPROM_ADDR_FSM_RESET |
10418 (EEPROM_DEFAULT_CLOCK_PERIOD <<
10419 EEPROM_ADDR_CLKPERD_SHIFT)));
10420
Michael Chan9d57f012006-12-07 00:23:25 -080010421 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010422
10423 /* Enable seeprom accesses. */
10424 tw32_f(GRC_LOCAL_CTRL,
10425 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
10426 udelay(100);
10427
10428 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
10429 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
10430 tp->tg3_flags |= TG3_FLAG_NVRAM;
10431
Michael Chanec41c7d2006-01-17 02:40:55 -080010432 if (tg3_nvram_lock(tp)) {
10433 printk(KERN_WARNING PFX "%s: Cannot get nvarm lock, "
10434 "tg3_nvram_init failed.\n", tp->dev->name);
10435 return;
10436 }
Michael Chane6af3012005-04-21 17:12:05 -070010437 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010438
Matt Carlson989a9d22007-05-05 11:51:05 -070010439 tp->nvram_size = 0;
10440
Michael Chan361b4ac2005-04-21 17:11:21 -070010441 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
10442 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080010443 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
10444 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070010445 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070010446 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
10447 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080010448 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070010449 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
10450 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070010451 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10452 tg3_get_5906_nvram_info(tp);
Matt Carlson321d32a2008-11-21 17:22:19 -080010453 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
10454 tg3_get_57780_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070010455 else
10456 tg3_get_nvram_info(tp);
10457
Matt Carlson989a9d22007-05-05 11:51:05 -070010458 if (tp->nvram_size == 0)
10459 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010460
Michael Chane6af3012005-04-21 17:12:05 -070010461 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080010462 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010463
10464 } else {
10465 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
10466
10467 tg3_get_eeprom_size(tp);
10468 }
10469}
10470
10471static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
10472 u32 offset, u32 *val)
10473{
10474 u32 tmp;
10475 int i;
10476
10477 if (offset > EEPROM_ADDR_ADDR_MASK ||
10478 (offset % 4) != 0)
10479 return -EINVAL;
10480
10481 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
10482 EEPROM_ADDR_DEVID_MASK |
10483 EEPROM_ADDR_READ);
10484 tw32(GRC_EEPROM_ADDR,
10485 tmp |
10486 (0 << EEPROM_ADDR_DEVID_SHIFT) |
10487 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
10488 EEPROM_ADDR_ADDR_MASK) |
10489 EEPROM_ADDR_READ | EEPROM_ADDR_START);
10490
Michael Chan9d57f012006-12-07 00:23:25 -080010491 for (i = 0; i < 1000; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010492 tmp = tr32(GRC_EEPROM_ADDR);
10493
10494 if (tmp & EEPROM_ADDR_COMPLETE)
10495 break;
Michael Chan9d57f012006-12-07 00:23:25 -080010496 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010497 }
10498 if (!(tmp & EEPROM_ADDR_COMPLETE))
10499 return -EBUSY;
10500
10501 *val = tr32(GRC_EEPROM_DATA);
10502 return 0;
10503}
10504
10505#define NVRAM_CMD_TIMEOUT 10000
10506
10507static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
10508{
10509 int i;
10510
10511 tw32(NVRAM_CMD, nvram_cmd);
10512 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
10513 udelay(10);
10514 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
10515 udelay(10);
10516 break;
10517 }
10518 }
10519 if (i == NVRAM_CMD_TIMEOUT) {
10520 return -EBUSY;
10521 }
10522 return 0;
10523}
10524
Michael Chan18201802006-03-20 22:29:15 -080010525static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
10526{
10527 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
10528 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
10529 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
Matt Carlson6b91fa02007-10-10 18:01:09 -070010530 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
Michael Chan18201802006-03-20 22:29:15 -080010531 (tp->nvram_jedecnum == JEDEC_ATMEL))
10532
10533 addr = ((addr / tp->nvram_pagesize) <<
10534 ATMEL_AT45DB0X1B_PAGE_POS) +
10535 (addr % tp->nvram_pagesize);
10536
10537 return addr;
10538}
10539
Michael Chanc4e65752006-03-20 22:29:32 -080010540static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
10541{
10542 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
10543 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
10544 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
Matt Carlson6b91fa02007-10-10 18:01:09 -070010545 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
Michael Chanc4e65752006-03-20 22:29:32 -080010546 (tp->nvram_jedecnum == JEDEC_ATMEL))
10547
10548 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
10549 tp->nvram_pagesize) +
10550 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
10551
10552 return addr;
10553}
10554
Linus Torvalds1da177e2005-04-16 15:20:36 -070010555static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
10556{
10557 int ret;
10558
Linus Torvalds1da177e2005-04-16 15:20:36 -070010559 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
10560 return tg3_nvram_read_using_eeprom(tp, offset, val);
10561
Michael Chan18201802006-03-20 22:29:15 -080010562 offset = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010563
10564 if (offset > NVRAM_ADDR_MSK)
10565 return -EINVAL;
10566
Michael Chanec41c7d2006-01-17 02:40:55 -080010567 ret = tg3_nvram_lock(tp);
10568 if (ret)
10569 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010570
Michael Chane6af3012005-04-21 17:12:05 -070010571 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010572
10573 tw32(NVRAM_ADDR, offset);
10574 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
10575 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
10576
10577 if (ret == 0)
10578 *val = swab32(tr32(NVRAM_RDDATA));
10579
Michael Chane6af3012005-04-21 17:12:05 -070010580 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010581
Michael Chan381291b2005-12-13 21:08:21 -080010582 tg3_nvram_unlock(tp);
10583
Linus Torvalds1da177e2005-04-16 15:20:36 -070010584 return ret;
10585}
10586
Al Virob9fc7dc2007-12-17 22:59:57 -080010587static int tg3_nvram_read_le(struct tg3 *tp, u32 offset, __le32 *val)
10588{
10589 u32 v;
10590 int res = tg3_nvram_read(tp, offset, &v);
10591 if (!res)
10592 *val = cpu_to_le32(v);
10593 return res;
10594}
10595
Michael Chan18201802006-03-20 22:29:15 -080010596static int tg3_nvram_read_swab(struct tg3 *tp, u32 offset, u32 *val)
10597{
10598 int err;
10599 u32 tmp;
10600
10601 err = tg3_nvram_read(tp, offset, &tmp);
10602 *val = swab32(tmp);
10603 return err;
10604}
10605
Linus Torvalds1da177e2005-04-16 15:20:36 -070010606static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
10607 u32 offset, u32 len, u8 *buf)
10608{
10609 int i, j, rc = 0;
10610 u32 val;
10611
10612 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080010613 u32 addr;
10614 __le32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010615
10616 addr = offset + i;
10617
10618 memcpy(&data, buf + i, 4);
10619
Al Virob9fc7dc2007-12-17 22:59:57 -080010620 tw32(GRC_EEPROM_DATA, le32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010621
10622 val = tr32(GRC_EEPROM_ADDR);
10623 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
10624
10625 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
10626 EEPROM_ADDR_READ);
10627 tw32(GRC_EEPROM_ADDR, val |
10628 (0 << EEPROM_ADDR_DEVID_SHIFT) |
10629 (addr & EEPROM_ADDR_ADDR_MASK) |
10630 EEPROM_ADDR_START |
10631 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010632
Michael Chan9d57f012006-12-07 00:23:25 -080010633 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010634 val = tr32(GRC_EEPROM_ADDR);
10635
10636 if (val & EEPROM_ADDR_COMPLETE)
10637 break;
Michael Chan9d57f012006-12-07 00:23:25 -080010638 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010639 }
10640 if (!(val & EEPROM_ADDR_COMPLETE)) {
10641 rc = -EBUSY;
10642 break;
10643 }
10644 }
10645
10646 return rc;
10647}
10648
10649/* offset and length are dword aligned */
10650static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
10651 u8 *buf)
10652{
10653 int ret = 0;
10654 u32 pagesize = tp->nvram_pagesize;
10655 u32 pagemask = pagesize - 1;
10656 u32 nvram_cmd;
10657 u8 *tmp;
10658
10659 tmp = kmalloc(pagesize, GFP_KERNEL);
10660 if (tmp == NULL)
10661 return -ENOMEM;
10662
10663 while (len) {
10664 int j;
Michael Chane6af3012005-04-21 17:12:05 -070010665 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010666
10667 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010668
Linus Torvalds1da177e2005-04-16 15:20:36 -070010669 for (j = 0; j < pagesize; j += 4) {
Al Viro286e3102007-12-17 23:00:31 -080010670 if ((ret = tg3_nvram_read_le(tp, phy_addr + j,
Al Virob9fc7dc2007-12-17 22:59:57 -080010671 (__le32 *) (tmp + j))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010672 break;
10673 }
10674 if (ret)
10675 break;
10676
10677 page_off = offset & pagemask;
10678 size = pagesize;
10679 if (len < size)
10680 size = len;
10681
10682 len -= size;
10683
10684 memcpy(tmp + page_off, buf, size);
10685
10686 offset = offset + (pagesize - page_off);
10687
Michael Chane6af3012005-04-21 17:12:05 -070010688 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010689
10690 /*
10691 * Before we can erase the flash page, we need
10692 * to issue a special "write enable" command.
10693 */
10694 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
10695
10696 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
10697 break;
10698
10699 /* Erase the target page */
10700 tw32(NVRAM_ADDR, phy_addr);
10701
10702 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
10703 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
10704
10705 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
10706 break;
10707
10708 /* Issue another write enable to start the write. */
10709 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
10710
10711 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
10712 break;
10713
10714 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080010715 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010716
Al Virob9fc7dc2007-12-17 22:59:57 -080010717 data = *((__be32 *) (tmp + j));
10718 /* swab32(le32_to_cpu(data)), actually */
10719 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010720
10721 tw32(NVRAM_ADDR, phy_addr + j);
10722
10723 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
10724 NVRAM_CMD_WR;
10725
10726 if (j == 0)
10727 nvram_cmd |= NVRAM_CMD_FIRST;
10728 else if (j == (pagesize - 4))
10729 nvram_cmd |= NVRAM_CMD_LAST;
10730
10731 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
10732 break;
10733 }
10734 if (ret)
10735 break;
10736 }
10737
10738 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
10739 tg3_nvram_exec_cmd(tp, nvram_cmd);
10740
10741 kfree(tmp);
10742
10743 return ret;
10744}
10745
10746/* offset and length are dword aligned */
10747static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
10748 u8 *buf)
10749{
10750 int i, ret = 0;
10751
10752 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080010753 u32 page_off, phy_addr, nvram_cmd;
10754 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010755
10756 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080010757 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010758
10759 page_off = offset % tp->nvram_pagesize;
10760
Michael Chan18201802006-03-20 22:29:15 -080010761 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010762
10763 tw32(NVRAM_ADDR, phy_addr);
10764
10765 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
10766
10767 if ((page_off == 0) || (i == 0))
10768 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070010769 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010770 nvram_cmd |= NVRAM_CMD_LAST;
10771
10772 if (i == (len - 4))
10773 nvram_cmd |= NVRAM_CMD_LAST;
10774
Matt Carlson321d32a2008-11-21 17:22:19 -080010775 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
10776 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070010777 (tp->nvram_jedecnum == JEDEC_ST) &&
10778 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010779
10780 if ((ret = tg3_nvram_exec_cmd(tp,
10781 NVRAM_CMD_WREN | NVRAM_CMD_GO |
10782 NVRAM_CMD_DONE)))
10783
10784 break;
10785 }
10786 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
10787 /* We always do complete word writes to eeprom. */
10788 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
10789 }
10790
10791 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
10792 break;
10793 }
10794 return ret;
10795}
10796
10797/* offset and length are dword aligned */
10798static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
10799{
10800 int ret;
10801
Linus Torvalds1da177e2005-04-16 15:20:36 -070010802 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070010803 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
10804 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010805 udelay(40);
10806 }
10807
10808 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
10809 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
10810 }
10811 else {
10812 u32 grc_mode;
10813
Michael Chanec41c7d2006-01-17 02:40:55 -080010814 ret = tg3_nvram_lock(tp);
10815 if (ret)
10816 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010817
Michael Chane6af3012005-04-21 17:12:05 -070010818 tg3_enable_nvram_access(tp);
10819 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
10820 !(tp->tg3_flags2 & TG3_FLG2_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010821 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010822
10823 grc_mode = tr32(GRC_MODE);
10824 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
10825
10826 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
10827 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
10828
10829 ret = tg3_nvram_write_block_buffered(tp, offset, len,
10830 buf);
10831 }
10832 else {
10833 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
10834 buf);
10835 }
10836
10837 grc_mode = tr32(GRC_MODE);
10838 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
10839
Michael Chane6af3012005-04-21 17:12:05 -070010840 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010841 tg3_nvram_unlock(tp);
10842 }
10843
10844 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070010845 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010846 udelay(40);
10847 }
10848
10849 return ret;
10850}
10851
10852struct subsys_tbl_ent {
10853 u16 subsys_vendor, subsys_devid;
10854 u32 phy_id;
10855};
10856
10857static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
10858 /* Broadcom boards. */
10859 { PCI_VENDOR_ID_BROADCOM, 0x1644, PHY_ID_BCM5401 }, /* BCM95700A6 */
10860 { PCI_VENDOR_ID_BROADCOM, 0x0001, PHY_ID_BCM5701 }, /* BCM95701A5 */
10861 { PCI_VENDOR_ID_BROADCOM, 0x0002, PHY_ID_BCM8002 }, /* BCM95700T6 */
10862 { PCI_VENDOR_ID_BROADCOM, 0x0003, 0 }, /* BCM95700A9 */
10863 { PCI_VENDOR_ID_BROADCOM, 0x0005, PHY_ID_BCM5701 }, /* BCM95701T1 */
10864 { PCI_VENDOR_ID_BROADCOM, 0x0006, PHY_ID_BCM5701 }, /* BCM95701T8 */
10865 { PCI_VENDOR_ID_BROADCOM, 0x0007, 0 }, /* BCM95701A7 */
10866 { PCI_VENDOR_ID_BROADCOM, 0x0008, PHY_ID_BCM5701 }, /* BCM95701A10 */
10867 { PCI_VENDOR_ID_BROADCOM, 0x8008, PHY_ID_BCM5701 }, /* BCM95701A12 */
10868 { PCI_VENDOR_ID_BROADCOM, 0x0009, PHY_ID_BCM5703 }, /* BCM95703Ax1 */
10869 { PCI_VENDOR_ID_BROADCOM, 0x8009, PHY_ID_BCM5703 }, /* BCM95703Ax2 */
10870
10871 /* 3com boards. */
10872 { PCI_VENDOR_ID_3COM, 0x1000, PHY_ID_BCM5401 }, /* 3C996T */
10873 { PCI_VENDOR_ID_3COM, 0x1006, PHY_ID_BCM5701 }, /* 3C996BT */
10874 { PCI_VENDOR_ID_3COM, 0x1004, 0 }, /* 3C996SX */
10875 { PCI_VENDOR_ID_3COM, 0x1007, PHY_ID_BCM5701 }, /* 3C1000T */
10876 { PCI_VENDOR_ID_3COM, 0x1008, PHY_ID_BCM5701 }, /* 3C940BR01 */
10877
10878 /* DELL boards. */
10879 { PCI_VENDOR_ID_DELL, 0x00d1, PHY_ID_BCM5401 }, /* VIPER */
10880 { PCI_VENDOR_ID_DELL, 0x0106, PHY_ID_BCM5401 }, /* JAGUAR */
10881 { PCI_VENDOR_ID_DELL, 0x0109, PHY_ID_BCM5411 }, /* MERLOT */
10882 { PCI_VENDOR_ID_DELL, 0x010a, PHY_ID_BCM5411 }, /* SLIM_MERLOT */
10883
10884 /* Compaq boards. */
10885 { PCI_VENDOR_ID_COMPAQ, 0x007c, PHY_ID_BCM5701 }, /* BANSHEE */
10886 { PCI_VENDOR_ID_COMPAQ, 0x009a, PHY_ID_BCM5701 }, /* BANSHEE_2 */
10887 { PCI_VENDOR_ID_COMPAQ, 0x007d, 0 }, /* CHANGELING */
10888 { PCI_VENDOR_ID_COMPAQ, 0x0085, PHY_ID_BCM5701 }, /* NC7780 */
10889 { PCI_VENDOR_ID_COMPAQ, 0x0099, PHY_ID_BCM5701 }, /* NC7780_2 */
10890
10891 /* IBM boards. */
10892 { PCI_VENDOR_ID_IBM, 0x0281, 0 } /* IBM??? */
10893};
10894
10895static inline struct subsys_tbl_ent *lookup_by_subsys(struct tg3 *tp)
10896{
10897 int i;
10898
10899 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
10900 if ((subsys_id_to_phy_id[i].subsys_vendor ==
10901 tp->pdev->subsystem_vendor) &&
10902 (subsys_id_to_phy_id[i].subsys_devid ==
10903 tp->pdev->subsystem_device))
10904 return &subsys_id_to_phy_id[i];
10905 }
10906 return NULL;
10907}
10908
Michael Chan7d0c41e2005-04-21 17:06:20 -070010909static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010910{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010911 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080010912 u16 pmcsr;
10913
10914 /* On some early chips the SRAM cannot be accessed in D3hot state,
10915 * so need make sure we're in D0.
10916 */
10917 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
10918 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
10919 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
10920 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070010921
10922 /* Make sure register accesses (indirect or otherwise)
10923 * will function correctly.
10924 */
10925 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
10926 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010927
David S. Millerf49639e2006-06-09 11:58:36 -070010928 /* The memory arbiter has to be enabled in order for SRAM accesses
10929 * to succeed. Normally on powerup the tg3 chip firmware will make
10930 * sure it is enabled, but other entities such as system netboot
10931 * code might disable it.
10932 */
10933 val = tr32(MEMARB_MODE);
10934 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
10935
Linus Torvalds1da177e2005-04-16 15:20:36 -070010936 tp->phy_id = PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070010937 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
10938
Gary Zambranoa85feb82007-05-05 11:52:19 -070010939 /* Assume an onboard device and WOL capable by default. */
10940 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080010941
Michael Chanb5d37722006-09-27 16:06:21 -070010942 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080010943 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070010944 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080010945 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
10946 }
Matt Carlson0527ba32007-10-10 18:03:30 -070010947 val = tr32(VCPU_CFGSHDW);
10948 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070010949 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070010950 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080010951 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070010952 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080010953 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070010954 }
10955
Linus Torvalds1da177e2005-04-16 15:20:36 -070010956 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
10957 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
10958 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070010959 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070010960 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010961
10962 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
10963 tp->nic_sram_data_cfg = nic_cfg;
10964
10965 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
10966 ver >>= NIC_SRAM_DATA_VER_SHIFT;
10967 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
10968 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
10969 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
10970 (ver > 0) && (ver < 0x100))
10971 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
10972
Matt Carlsona9daf362008-05-25 23:49:44 -070010973 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
10974 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
10975
Linus Torvalds1da177e2005-04-16 15:20:36 -070010976 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
10977 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
10978 eeprom_phy_serdes = 1;
10979
10980 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
10981 if (nic_phy_id != 0) {
10982 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
10983 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
10984
10985 eeprom_phy_id = (id1 >> 16) << 10;
10986 eeprom_phy_id |= (id2 & 0xfc00) << 16;
10987 eeprom_phy_id |= (id2 & 0x03ff) << 0;
10988 } else
10989 eeprom_phy_id = 0;
10990
Michael Chan7d0c41e2005-04-21 17:06:20 -070010991 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070010992 if (eeprom_phy_serdes) {
Michael Chana4e2b342005-10-26 15:46:52 -070010993 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan747e8f82005-07-25 12:33:22 -070010994 tp->tg3_flags2 |= TG3_FLG2_MII_SERDES;
10995 else
10996 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
10997 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070010998
John W. Linvillecbf46852005-04-21 17:01:29 -070010999 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011000 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
11001 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070011002 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070011003 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
11004
11005 switch (led_cfg) {
11006 default:
11007 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
11008 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
11009 break;
11010
11011 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
11012 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
11013 break;
11014
11015 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
11016 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070011017
11018 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
11019 * read on some older 5700/5701 bootcode.
11020 */
11021 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
11022 ASIC_REV_5700 ||
11023 GET_ASIC_REV(tp->pci_chip_rev_id) ==
11024 ASIC_REV_5701)
11025 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
11026
Linus Torvalds1da177e2005-04-16 15:20:36 -070011027 break;
11028
11029 case SHASTA_EXT_LED_SHARED:
11030 tp->led_ctrl = LED_CTRL_MODE_SHARED;
11031 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
11032 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
11033 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
11034 LED_CTRL_MODE_PHY_2);
11035 break;
11036
11037 case SHASTA_EXT_LED_MAC:
11038 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
11039 break;
11040
11041 case SHASTA_EXT_LED_COMBO:
11042 tp->led_ctrl = LED_CTRL_MODE_COMBO;
11043 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
11044 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
11045 LED_CTRL_MODE_PHY_2);
11046 break;
11047
Stephen Hemminger855e1112008-04-16 16:37:28 -070011048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011049
11050 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
11051 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
11052 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
11053 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
11054
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011055 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
11056 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080011057
Michael Chan9d26e212006-12-07 00:21:14 -080011058 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011059 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080011060 if ((tp->pdev->subsystem_vendor ==
11061 PCI_VENDOR_ID_ARIMA) &&
11062 (tp->pdev->subsystem_device == 0x205a ||
11063 tp->pdev->subsystem_device == 0x2063))
11064 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
11065 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070011066 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080011067 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
11068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011069
11070 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
11071 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070011072 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011073 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
11074 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080011075
11076 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
11077 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070011078 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080011079
Gary Zambranoa85feb82007-05-05 11:52:19 -070011080 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES &&
11081 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
11082 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011083
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070011084 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080011085 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070011086 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
11087
Linus Torvalds1da177e2005-04-16 15:20:36 -070011088 if (cfg2 & (1 << 17))
11089 tp->tg3_flags2 |= TG3_FLG2_CAPACITIVE_COUPLING;
11090
11091 /* serdes signal pre-emphasis in register 0x590 set by */
11092 /* bootcode if bit 18 is set */
11093 if (cfg2 & (1 << 18))
11094 tp->tg3_flags2 |= TG3_FLG2_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070011095
Matt Carlson321d32a2008-11-21 17:22:19 -080011096 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
11097 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080011098 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
11099 tp->tg3_flags3 |= TG3_FLG3_PHY_ENABLE_APD;
11100
Matt Carlson8ed5d972007-05-07 00:25:49 -070011101 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
11102 u32 cfg3;
11103
11104 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
11105 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
11106 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
11107 }
Matt Carlsona9daf362008-05-25 23:49:44 -070011108
11109 if (cfg4 & NIC_SRAM_RGMII_STD_IBND_DISABLE)
11110 tp->tg3_flags3 |= TG3_FLG3_RGMII_STD_IBND_DISABLE;
11111 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
11112 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
11113 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
11114 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011115 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080011116done:
11117 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
11118 device_set_wakeup_enable(&tp->pdev->dev,
11119 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Michael Chan7d0c41e2005-04-21 17:06:20 -070011120}
11121
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011122static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
11123{
11124 int i;
11125 u32 val;
11126
11127 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
11128 tw32(OTP_CTRL, cmd);
11129
11130 /* Wait for up to 1 ms for command to execute. */
11131 for (i = 0; i < 100; i++) {
11132 val = tr32(OTP_STATUS);
11133 if (val & OTP_STATUS_CMD_DONE)
11134 break;
11135 udelay(10);
11136 }
11137
11138 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
11139}
11140
11141/* Read the gphy configuration from the OTP region of the chip. The gphy
11142 * configuration is a 32-bit value that straddles the alignment boundary.
11143 * We do two 32-bit reads and then shift and merge the results.
11144 */
11145static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
11146{
11147 u32 bhalf_otp, thalf_otp;
11148
11149 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
11150
11151 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
11152 return 0;
11153
11154 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
11155
11156 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
11157 return 0;
11158
11159 thalf_otp = tr32(OTP_READ_DATA);
11160
11161 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
11162
11163 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
11164 return 0;
11165
11166 bhalf_otp = tr32(OTP_READ_DATA);
11167
11168 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
11169}
11170
Michael Chan7d0c41e2005-04-21 17:06:20 -070011171static int __devinit tg3_phy_probe(struct tg3 *tp)
11172{
11173 u32 hw_phy_id_1, hw_phy_id_2;
11174 u32 hw_phy_id, hw_phy_id_masked;
11175 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011176
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011177 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
11178 return tg3_phy_init(tp);
11179
Linus Torvalds1da177e2005-04-16 15:20:36 -070011180 /* Reading the PHY ID register can conflict with ASF
11181 * firwmare access to the PHY hardware.
11182 */
11183 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070011184 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
11185 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011186 hw_phy_id = hw_phy_id_masked = PHY_ID_INVALID;
11187 } else {
11188 /* Now read the physical PHY_ID from the chip and verify
11189 * that it is sane. If it doesn't look good, we fall back
11190 * to either the hard-coded table based PHY_ID and failing
11191 * that the value found in the eeprom area.
11192 */
11193 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
11194 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
11195
11196 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
11197 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
11198 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
11199
11200 hw_phy_id_masked = hw_phy_id & PHY_ID_MASK;
11201 }
11202
11203 if (!err && KNOWN_PHY_ID(hw_phy_id_masked)) {
11204 tp->phy_id = hw_phy_id;
11205 if (hw_phy_id_masked == PHY_ID_BCM8002)
11206 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070011207 else
11208 tp->tg3_flags2 &= ~TG3_FLG2_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011209 } else {
Michael Chan7d0c41e2005-04-21 17:06:20 -070011210 if (tp->phy_id != PHY_ID_INVALID) {
11211 /* Do nothing, phy ID already set up in
11212 * tg3_get_eeprom_hw_cfg().
11213 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011214 } else {
11215 struct subsys_tbl_ent *p;
11216
11217 /* No eeprom signature? Try the hardcoded
11218 * subsys device table.
11219 */
11220 p = lookup_by_subsys(tp);
11221 if (!p)
11222 return -ENODEV;
11223
11224 tp->phy_id = p->phy_id;
11225 if (!tp->phy_id ||
11226 tp->phy_id == PHY_ID_BCM8002)
11227 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
11228 }
11229 }
11230
Michael Chan747e8f82005-07-25 12:33:22 -070011231 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070011232 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011233 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080011234 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011235
11236 tg3_readphy(tp, MII_BMSR, &bmsr);
11237 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
11238 (bmsr & BMSR_LSTATUS))
11239 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011240
Linus Torvalds1da177e2005-04-16 15:20:36 -070011241 err = tg3_phy_reset(tp);
11242 if (err)
11243 return err;
11244
11245 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
11246 ADVERTISE_100HALF | ADVERTISE_100FULL |
11247 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
11248 tg3_ctrl = 0;
11249 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
11250 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
11251 MII_TG3_CTRL_ADV_1000_FULL);
11252 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
11253 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
11254 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
11255 MII_TG3_CTRL_ENABLE_AS_MASTER);
11256 }
11257
Michael Chan3600d912006-12-07 00:21:48 -080011258 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
11259 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
11260 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
11261 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011262 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
11263
11264 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
11265 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
11266
11267 tg3_writephy(tp, MII_BMCR,
11268 BMCR_ANENABLE | BMCR_ANRESTART);
11269 }
11270 tg3_phy_set_wirespeed(tp);
11271
11272 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
11273 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
11274 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
11275 }
11276
11277skip_phy_reset:
11278 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
11279 err = tg3_init_5401phy_dsp(tp);
11280 if (err)
11281 return err;
11282 }
11283
11284 if (!err && ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)) {
11285 err = tg3_init_5401phy_dsp(tp);
11286 }
11287
Michael Chan747e8f82005-07-25 12:33:22 -070011288 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011289 tp->link_config.advertising =
11290 (ADVERTISED_1000baseT_Half |
11291 ADVERTISED_1000baseT_Full |
11292 ADVERTISED_Autoneg |
11293 ADVERTISED_FIBRE);
11294 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
11295 tp->link_config.advertising &=
11296 ~(ADVERTISED_1000baseT_Half |
11297 ADVERTISED_1000baseT_Full);
11298
11299 return err;
11300}
11301
11302static void __devinit tg3_read_partno(struct tg3 *tp)
11303{
11304 unsigned char vpd_data[256];
Michael Chanaf2c6a42006-11-07 14:57:51 -080011305 unsigned int i;
Michael Chan1b277772006-03-20 22:27:48 -080011306 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011307
Michael Chan18201802006-03-20 22:29:15 -080011308 if (tg3_nvram_read_swab(tp, 0x0, &magic))
David S. Millerf49639e2006-06-09 11:58:36 -070011309 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011310
Michael Chan18201802006-03-20 22:29:15 -080011311 if (magic == TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011312 for (i = 0; i < 256; i += 4) {
11313 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011314
Michael Chan1b277772006-03-20 22:27:48 -080011315 if (tg3_nvram_read(tp, 0x100 + i, &tmp))
11316 goto out_not_found;
11317
11318 vpd_data[i + 0] = ((tmp >> 0) & 0xff);
11319 vpd_data[i + 1] = ((tmp >> 8) & 0xff);
11320 vpd_data[i + 2] = ((tmp >> 16) & 0xff);
11321 vpd_data[i + 3] = ((tmp >> 24) & 0xff);
11322 }
11323 } else {
11324 int vpd_cap;
11325
11326 vpd_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_VPD);
11327 for (i = 0; i < 256; i += 4) {
11328 u32 tmp, j = 0;
Al Virob9fc7dc2007-12-17 22:59:57 -080011329 __le32 v;
Michael Chan1b277772006-03-20 22:27:48 -080011330 u16 tmp16;
11331
11332 pci_write_config_word(tp->pdev, vpd_cap + PCI_VPD_ADDR,
11333 i);
11334 while (j++ < 100) {
11335 pci_read_config_word(tp->pdev, vpd_cap +
11336 PCI_VPD_ADDR, &tmp16);
11337 if (tmp16 & 0x8000)
11338 break;
11339 msleep(1);
11340 }
David S. Millerf49639e2006-06-09 11:58:36 -070011341 if (!(tmp16 & 0x8000))
11342 goto out_not_found;
11343
Michael Chan1b277772006-03-20 22:27:48 -080011344 pci_read_config_dword(tp->pdev, vpd_cap + PCI_VPD_DATA,
11345 &tmp);
Al Virob9fc7dc2007-12-17 22:59:57 -080011346 v = cpu_to_le32(tmp);
11347 memcpy(&vpd_data[i], &v, 4);
Michael Chan1b277772006-03-20 22:27:48 -080011348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011349 }
11350
11351 /* Now parse and find the part number. */
Michael Chanaf2c6a42006-11-07 14:57:51 -080011352 for (i = 0; i < 254; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011353 unsigned char val = vpd_data[i];
Michael Chanaf2c6a42006-11-07 14:57:51 -080011354 unsigned int block_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011355
11356 if (val == 0x82 || val == 0x91) {
11357 i = (i + 3 +
11358 (vpd_data[i + 1] +
11359 (vpd_data[i + 2] << 8)));
11360 continue;
11361 }
11362
11363 if (val != 0x90)
11364 goto out_not_found;
11365
11366 block_end = (i + 3 +
11367 (vpd_data[i + 1] +
11368 (vpd_data[i + 2] << 8)));
11369 i += 3;
Michael Chanaf2c6a42006-11-07 14:57:51 -080011370
11371 if (block_end > 256)
11372 goto out_not_found;
11373
11374 while (i < (block_end - 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011375 if (vpd_data[i + 0] == 'P' &&
11376 vpd_data[i + 1] == 'N') {
11377 int partno_len = vpd_data[i + 2];
11378
Michael Chanaf2c6a42006-11-07 14:57:51 -080011379 i += 3;
11380 if (partno_len > 24 || (partno_len + i) > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011381 goto out_not_found;
11382
11383 memcpy(tp->board_part_number,
Michael Chanaf2c6a42006-11-07 14:57:51 -080011384 &vpd_data[i], partno_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011385
11386 /* Success. */
11387 return;
11388 }
Michael Chanaf2c6a42006-11-07 14:57:51 -080011389 i += 3 + vpd_data[i + 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070011390 }
11391
11392 /* Part number not found. */
11393 goto out_not_found;
11394 }
11395
11396out_not_found:
Michael Chanb5d37722006-09-27 16:06:21 -070011397 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11398 strcpy(tp->board_part_number, "BCM95906");
11399 else
11400 strcpy(tp->board_part_number, "none");
Linus Torvalds1da177e2005-04-16 15:20:36 -070011401}
11402
Matt Carlson9c8a6202007-10-21 16:16:08 -070011403static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
11404{
11405 u32 val;
11406
11407 if (tg3_nvram_read_swab(tp, offset, &val) ||
11408 (val & 0xfc000000) != 0x0c000000 ||
11409 tg3_nvram_read_swab(tp, offset + 4, &val) ||
11410 val != 0)
11411 return 0;
11412
11413 return 1;
11414}
11415
Matt Carlsondfe00d72008-11-21 17:19:41 -080011416static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
11417{
11418 u32 offset, major, minor, build;
11419
11420 tp->fw_ver[0] = 's';
11421 tp->fw_ver[1] = 'b';
11422 tp->fw_ver[2] = '\0';
11423
11424 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
11425 return;
11426
11427 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
11428 case TG3_EEPROM_SB_REVISION_0:
11429 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
11430 break;
11431 case TG3_EEPROM_SB_REVISION_2:
11432 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
11433 break;
11434 case TG3_EEPROM_SB_REVISION_3:
11435 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
11436 break;
11437 default:
11438 return;
11439 }
11440
11441 if (tg3_nvram_read_swab(tp, offset, &val))
11442 return;
11443
11444 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
11445 TG3_EEPROM_SB_EDH_BLD_SHFT;
11446 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
11447 TG3_EEPROM_SB_EDH_MAJ_SHFT;
11448 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
11449
11450 if (minor > 99 || build > 26)
11451 return;
11452
11453 snprintf(&tp->fw_ver[2], 30, " v%d.%02d", major, minor);
11454
11455 if (build > 0) {
11456 tp->fw_ver[8] = 'a' + build - 1;
11457 tp->fw_ver[9] = '\0';
11458 }
11459}
11460
Michael Chanc4e65752006-03-20 22:29:32 -080011461static void __devinit tg3_read_fw_ver(struct tg3 *tp)
11462{
11463 u32 val, offset, start;
Matt Carlson9c8a6202007-10-21 16:16:08 -070011464 u32 ver_offset;
11465 int i, bcnt;
Michael Chanc4e65752006-03-20 22:29:32 -080011466
11467 if (tg3_nvram_read_swab(tp, 0, &val))
11468 return;
11469
Matt Carlsondfe00d72008-11-21 17:19:41 -080011470 if (val != TG3_EEPROM_MAGIC) {
11471 if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
11472 tg3_read_sb_ver(tp, val);
11473
Michael Chanc4e65752006-03-20 22:29:32 -080011474 return;
Matt Carlsondfe00d72008-11-21 17:19:41 -080011475 }
Michael Chanc4e65752006-03-20 22:29:32 -080011476
11477 if (tg3_nvram_read_swab(tp, 0xc, &offset) ||
11478 tg3_nvram_read_swab(tp, 0x4, &start))
11479 return;
11480
11481 offset = tg3_nvram_logical_addr(tp, offset);
Matt Carlson9c8a6202007-10-21 16:16:08 -070011482
11483 if (!tg3_fw_img_is_valid(tp, offset) ||
11484 tg3_nvram_read_swab(tp, offset + 8, &ver_offset))
Michael Chanc4e65752006-03-20 22:29:32 -080011485 return;
11486
Matt Carlson9c8a6202007-10-21 16:16:08 -070011487 offset = offset + ver_offset - start;
11488 for (i = 0; i < 16; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011489 __le32 v;
11490 if (tg3_nvram_read_le(tp, offset + i, &v))
Michael Chanc4e65752006-03-20 22:29:32 -080011491 return;
11492
Al Virob9fc7dc2007-12-17 22:59:57 -080011493 memcpy(tp->fw_ver + i, &v, 4);
Michael Chanc4e65752006-03-20 22:29:32 -080011494 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070011495
11496 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson84af67f2007-11-12 21:08:59 -080011497 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
Matt Carlson9c8a6202007-10-21 16:16:08 -070011498 return;
11499
11500 for (offset = TG3_NVM_DIR_START;
11501 offset < TG3_NVM_DIR_END;
11502 offset += TG3_NVM_DIRENT_SIZE) {
11503 if (tg3_nvram_read_swab(tp, offset, &val))
11504 return;
11505
11506 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
11507 break;
11508 }
11509
11510 if (offset == TG3_NVM_DIR_END)
11511 return;
11512
11513 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
11514 start = 0x08000000;
11515 else if (tg3_nvram_read_swab(tp, offset - 4, &start))
11516 return;
11517
11518 if (tg3_nvram_read_swab(tp, offset + 4, &offset) ||
11519 !tg3_fw_img_is_valid(tp, offset) ||
11520 tg3_nvram_read_swab(tp, offset + 8, &val))
11521 return;
11522
11523 offset += val - start;
11524
11525 bcnt = strlen(tp->fw_ver);
11526
11527 tp->fw_ver[bcnt++] = ',';
11528 tp->fw_ver[bcnt++] = ' ';
11529
11530 for (i = 0; i < 4; i++) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011531 __le32 v;
11532 if (tg3_nvram_read_le(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070011533 return;
11534
Al Virob9fc7dc2007-12-17 22:59:57 -080011535 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070011536
Al Virob9fc7dc2007-12-17 22:59:57 -080011537 if (bcnt > TG3_VER_SIZE - sizeof(v)) {
11538 memcpy(&tp->fw_ver[bcnt], &v, TG3_VER_SIZE - bcnt);
Matt Carlson9c8a6202007-10-21 16:16:08 -070011539 break;
11540 }
11541
Al Virob9fc7dc2007-12-17 22:59:57 -080011542 memcpy(&tp->fw_ver[bcnt], &v, sizeof(v));
11543 bcnt += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070011544 }
11545
11546 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080011547}
11548
Michael Chan7544b092007-05-05 13:08:32 -070011549static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
11550
Linus Torvalds1da177e2005-04-16 15:20:36 -070011551static int __devinit tg3_get_invariants(struct tg3 *tp)
11552{
11553 static struct pci_device_id write_reorder_chipsets[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011554 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
11555 PCI_DEVICE_ID_AMD_FE_GATE_700C) },
John W. Linvillec165b002006-07-08 13:28:53 -070011556 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
11557 PCI_DEVICE_ID_AMD_8131_BRIDGE) },
Michael Chan399de502005-10-03 14:02:39 -070011558 { PCI_DEVICE(PCI_VENDOR_ID_VIA,
11559 PCI_DEVICE_ID_VIA_8385_0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070011560 { },
11561 };
11562 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011563 u32 pci_state_reg, grc_misc_cfg;
11564 u32 val;
11565 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080011566 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011567
Linus Torvalds1da177e2005-04-16 15:20:36 -070011568 /* Force memory write invalidate off. If we leave it on,
11569 * then on 5700_BX chips we have to enable a workaround.
11570 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
11571 * to match the cacheline size. The Broadcom driver have this
11572 * workaround but turns MWI off all the times so never uses
11573 * it. This seems to suggest that the workaround is insufficient.
11574 */
11575 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
11576 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
11577 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
11578
11579 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
11580 * has the register indirect write enable bit set before
11581 * we try to access any of the MMIO registers. It is also
11582 * critical that the PCI-X hw workaround situation is decided
11583 * before that as well.
11584 */
11585 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
11586 &misc_ctrl_reg);
11587
11588 tp->pci_chip_rev_id = (misc_ctrl_reg >>
11589 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070011590 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
11591 u32 prod_id_asic_rev;
11592
11593 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
11594 &prod_id_asic_rev);
Matt Carlson321d32a2008-11-21 17:22:19 -080011595 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070011596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011597
Michael Chanff645be2005-04-21 17:09:53 -070011598 /* Wrong chip ID in 5752 A0. This code can be removed later
11599 * as A0 is not in production.
11600 */
11601 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
11602 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
11603
Michael Chan68929142005-08-09 20:17:14 -070011604 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
11605 * we need to disable memory and use config. cycles
11606 * only to access all registers. The 5702/03 chips
11607 * can mistakenly decode the special cycles from the
11608 * ICH chipsets as memory write cycles, causing corruption
11609 * of register and memory space. Only certain ICH bridges
11610 * will drive special cycles with non-zero data during the
11611 * address phase which can fall within the 5703's address
11612 * range. This is not an ICH bug as the PCI spec allows
11613 * non-zero address during special cycles. However, only
11614 * these ICH bridges are known to drive non-zero addresses
11615 * during special cycles.
11616 *
11617 * Since special cycles do not cross PCI bridges, we only
11618 * enable this workaround if the 5703 is on the secondary
11619 * bus of these ICH bridges.
11620 */
11621 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
11622 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
11623 static struct tg3_dev_id {
11624 u32 vendor;
11625 u32 device;
11626 u32 rev;
11627 } ich_chipsets[] = {
11628 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
11629 PCI_ANY_ID },
11630 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
11631 PCI_ANY_ID },
11632 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
11633 0xa },
11634 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
11635 PCI_ANY_ID },
11636 { },
11637 };
11638 struct tg3_dev_id *pci_id = &ich_chipsets[0];
11639 struct pci_dev *bridge = NULL;
11640
11641 while (pci_id->vendor != 0) {
11642 bridge = pci_get_device(pci_id->vendor, pci_id->device,
11643 bridge);
11644 if (!bridge) {
11645 pci_id++;
11646 continue;
11647 }
11648 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070011649 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070011650 continue;
11651 }
11652 if (bridge->subordinate &&
11653 (bridge->subordinate->number ==
11654 tp->pdev->bus->number)) {
11655
11656 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
11657 pci_dev_put(bridge);
11658 break;
11659 }
11660 }
11661 }
11662
Matt Carlson41588ba2008-04-19 18:12:33 -070011663 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
11664 static struct tg3_dev_id {
11665 u32 vendor;
11666 u32 device;
11667 } bridge_chipsets[] = {
11668 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
11669 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
11670 { },
11671 };
11672 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
11673 struct pci_dev *bridge = NULL;
11674
11675 while (pci_id->vendor != 0) {
11676 bridge = pci_get_device(pci_id->vendor,
11677 pci_id->device,
11678 bridge);
11679 if (!bridge) {
11680 pci_id++;
11681 continue;
11682 }
11683 if (bridge->subordinate &&
11684 (bridge->subordinate->number <=
11685 tp->pdev->bus->number) &&
11686 (bridge->subordinate->subordinate >=
11687 tp->pdev->bus->number)) {
11688 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
11689 pci_dev_put(bridge);
11690 break;
11691 }
11692 }
11693 }
11694
Michael Chan4a29cc22006-03-19 13:21:12 -080011695 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
11696 * DMA addresses > 40-bit. This bridge may have other additional
11697 * 57xx devices behind it in some 4-port NIC designs for example.
11698 * Any tg3 device found behind the bridge will also need the 40-bit
11699 * DMA workaround.
11700 */
Michael Chana4e2b342005-10-26 15:46:52 -070011701 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
11702 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
11703 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080011704 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070011705 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Michael Chana4e2b342005-10-26 15:46:52 -070011706 }
Michael Chan4a29cc22006-03-19 13:21:12 -080011707 else {
11708 struct pci_dev *bridge = NULL;
11709
11710 do {
11711 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
11712 PCI_DEVICE_ID_SERVERWORKS_EPB,
11713 bridge);
11714 if (bridge && bridge->subordinate &&
11715 (bridge->subordinate->number <=
11716 tp->pdev->bus->number) &&
11717 (bridge->subordinate->subordinate >=
11718 tp->pdev->bus->number)) {
11719 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
11720 pci_dev_put(bridge);
11721 break;
11722 }
11723 } while (bridge);
11724 }
Michael Chan4cf78e42005-07-25 12:29:19 -070011725
Linus Torvalds1da177e2005-04-16 15:20:36 -070011726 /* Initialize misc host control in PCI block. */
11727 tp->misc_host_ctrl |= (misc_ctrl_reg &
11728 MISC_HOST_CTRL_CHIPREV);
11729 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
11730 tp->misc_host_ctrl);
11731
Michael Chan7544b092007-05-05 13:08:32 -070011732 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
11733 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714))
11734 tp->pdev_peer = tg3_find_peer(tp);
11735
Matt Carlson321d32a2008-11-21 17:22:19 -080011736 /* Intentionally exclude ASIC_REV_5906 */
11737 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad12006-03-20 22:27:35 -080011738 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070011739 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070011740 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011741 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080011742 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
11743 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
11744
11745 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
11746 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070011747 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080011748 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011749 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070011750 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
11751
John W. Linville1b440c562005-04-21 17:03:18 -070011752 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
11753 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
11754 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
11755
Matt Carlson027455a2008-12-21 20:19:30 -080011756 /* 5700 B0 chips do not support checksumming correctly due
11757 * to hardware bugs.
11758 */
11759 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
11760 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
11761 else {
11762 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
11763 tp->dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
11764 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
11765 tp->dev->features |= NETIF_F_IPV6_CSUM;
11766 }
11767
Michael Chan5a6f3072006-03-20 22:28:05 -080011768 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070011769 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
11770 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
11771 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
11772 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
11773 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
11774 tp->pdev_peer == tp->pdev))
11775 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
11776
Matt Carlson321d32a2008-11-21 17:22:19 -080011777 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070011778 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan5a6f3072006-03-20 22:28:05 -080011779 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011780 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070011781 } else {
Michael Chan7f62ad52007-02-20 23:25:40 -080011782 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
Michael Chan52c0fd82006-06-29 20:15:54 -070011783 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
11784 ASIC_REV_5750 &&
11785 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Michael Chan7f62ad52007-02-20 23:25:40 -080011786 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
Michael Chan52c0fd82006-06-29 20:15:54 -070011787 }
Michael Chan5a6f3072006-03-20 22:28:05 -080011788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011789
Matt Carlsonf51f3562008-05-25 23:45:08 -070011790 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
11791 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070011792 tp->tg3_flags2 |= TG3_FLG2_JUMBO_CAPABLE;
11793
Matt Carlson52f44902008-11-21 17:17:04 -080011794 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
11795 &pci_state_reg);
11796
Matt Carlson5e7dfd02008-11-21 17:18:16 -080011797 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
11798 if (tp->pcie_cap != 0) {
11799 u16 lnkctl;
11800
Linus Torvalds1da177e2005-04-16 15:20:36 -070011801 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080011802
11803 pcie_set_readrq(tp->pdev, 4096);
11804
Matt Carlson5e7dfd02008-11-21 17:18:16 -080011805 pci_read_config_word(tp->pdev,
11806 tp->pcie_cap + PCI_EXP_LNKCTL,
11807 &lnkctl);
11808 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
11809 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080011810 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080011811 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080011812 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
11813 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080011814 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Michael Chanc7835a72006-11-15 21:14:42 -080011815 }
Matt Carlson52f44902008-11-21 17:17:04 -080011816 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080011817 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080011818 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
11819 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
11820 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
11821 if (!tp->pcix_cap) {
11822 printk(KERN_ERR PFX "Cannot find PCI-X "
11823 "capability, aborting.\n");
11824 return -EIO;
11825 }
11826
11827 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
11828 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
11829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011830
Michael Chan399de502005-10-03 14:02:39 -070011831 /* If we have an AMD 762 or VIA K8T800 chipset, write
11832 * reordering to the mailbox registers done by the host
11833 * controller can cause major troubles. We read back from
11834 * every mailbox register write to force the writes to be
11835 * posted to the chip in order.
11836 */
11837 if (pci_dev_present(write_reorder_chipsets) &&
11838 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
11839 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
11840
Matt Carlson69fc4052008-12-21 20:19:57 -080011841 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
11842 &tp->pci_cacheline_sz);
11843 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
11844 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011845 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
11846 tp->pci_lat_timer < 64) {
11847 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080011848 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
11849 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011850 }
11851
Matt Carlson52f44902008-11-21 17:17:04 -080011852 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
11853 /* 5700 BX chips need to have their TX producer index
11854 * mailboxes written twice to workaround a bug.
11855 */
11856 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070011857
Matt Carlson52f44902008-11-21 17:17:04 -080011858 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011859 *
11860 * The workaround is to use indirect register accesses
11861 * for all chip writes not to mailbox registers.
11862 */
Matt Carlson52f44902008-11-21 17:17:04 -080011863 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011864 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011865
11866 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
11867
11868 /* The chip can have it's power management PCI config
11869 * space registers clobbered due to this bug.
11870 * So explicitly force the chip into D0 here.
11871 */
Matt Carlson9974a352007-10-07 23:27:28 -070011872 pci_read_config_dword(tp->pdev,
11873 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011874 &pm_reg);
11875 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
11876 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070011877 pci_write_config_dword(tp->pdev,
11878 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011879 pm_reg);
11880
11881 /* Also, force SERR#/PERR# in PCI command. */
11882 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
11883 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
11884 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
11885 }
11886 }
11887
Linus Torvalds1da177e2005-04-16 15:20:36 -070011888 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
11889 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
11890 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
11891 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
11892
11893 /* Chip-specific fixup from Broadcom driver */
11894 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
11895 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
11896 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
11897 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
11898 }
11899
Michael Chan1ee582d2005-08-09 20:16:46 -070011900 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070011901 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070011902 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070011903 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070011904 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070011905 tp->write32_tx_mbox = tg3_write32;
11906 tp->write32_rx_mbox = tg3_write32;
11907
11908 /* Various workaround register access methods */
11909 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
11910 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070011911 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
11912 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
11913 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
11914 /*
11915 * Back to back register writes can cause problems on these
11916 * chips, the workaround is to read back all reg writes
11917 * except those to mailbox regs.
11918 *
11919 * See tg3_write_indirect_reg32().
11920 */
Michael Chan1ee582d2005-08-09 20:16:46 -070011921 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070011922 }
11923
Michael Chan1ee582d2005-08-09 20:16:46 -070011924
11925 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
11926 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
11927 tp->write32_tx_mbox = tg3_write32_tx_mbox;
11928 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
11929 tp->write32_rx_mbox = tg3_write_flush_reg32;
11930 }
Michael Chan20094932005-08-09 20:16:32 -070011931
Michael Chan68929142005-08-09 20:17:14 -070011932 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
11933 tp->read32 = tg3_read_indirect_reg32;
11934 tp->write32 = tg3_write_indirect_reg32;
11935 tp->read32_mbox = tg3_read_indirect_mbox;
11936 tp->write32_mbox = tg3_write_indirect_mbox;
11937 tp->write32_tx_mbox = tg3_write_indirect_mbox;
11938 tp->write32_rx_mbox = tg3_write_indirect_mbox;
11939
11940 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070011941 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070011942
11943 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
11944 pci_cmd &= ~PCI_COMMAND_MEMORY;
11945 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
11946 }
Michael Chanb5d37722006-09-27 16:06:21 -070011947 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
11948 tp->read32_mbox = tg3_read32_mbox_5906;
11949 tp->write32_mbox = tg3_write32_mbox_5906;
11950 tp->write32_tx_mbox = tg3_write32_mbox_5906;
11951 tp->write32_rx_mbox = tg3_write32_mbox_5906;
11952 }
Michael Chan68929142005-08-09 20:17:14 -070011953
Michael Chanbbadf502006-04-06 21:46:34 -070011954 if (tp->write32 == tg3_write_indirect_reg32 ||
11955 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
11956 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070011957 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070011958 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
11959
Michael Chan7d0c41e2005-04-21 17:06:20 -070011960 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080011961 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070011962 * determined before calling tg3_set_power_state() so that
11963 * we know whether or not to switch out of Vaux power.
11964 * When the flag is set, it means that GPIO1 is used for eeprom
11965 * write protect and also implies that it is a LOM where GPIOs
11966 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011967 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070011968 tg3_get_eeprom_hw_cfg(tp);
11969
Matt Carlson0d3031d2007-10-10 18:02:43 -070011970 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
11971 /* Allow reads and writes to the
11972 * APE register and memory space.
11973 */
11974 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
11975 PCISTATE_ALLOW_APE_SHMEM_WR;
11976 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
11977 pci_state_reg);
11978 }
11979
Matt Carlson9936bcf2007-10-10 18:03:07 -070011980 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011981 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080011982 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
11983 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -070011984 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
11985
Michael Chan314fba32005-04-21 17:07:04 -070011986 /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
11987 * GPIO1 driven high will bring 5700's external PHY out of reset.
11988 * It is also used as eeprom write protect on LOMs.
11989 */
11990 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
11991 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
11992 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
11993 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
11994 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070011995 /* Unused GPIO3 must be driven as output on 5752 because there
11996 * are no pull-up resistors on unused GPIO pins.
11997 */
11998 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11999 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070012000
Matt Carlson321d32a2008-11-21 17:22:19 -080012001 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
12002 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Michael Chanaf36e6b2006-03-23 01:28:06 -080012003 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
12004
Matt Carlson5f0c4a32008-06-09 15:41:12 -070012005 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761) {
12006 /* Turn off the debug UART. */
12007 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
12008 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
12009 /* Keep VMain power. */
12010 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
12011 GRC_LCLCTRL_GPIO_OUTPUT0;
12012 }
12013
Linus Torvalds1da177e2005-04-16 15:20:36 -070012014 /* Force the chip into D0. */
Michael Chanbc1c7562006-03-20 17:48:03 -080012015 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012016 if (err) {
12017 printk(KERN_ERR PFX "(%s) transition to D0 failed\n",
12018 pci_name(tp->pdev));
12019 return err;
12020 }
12021
Linus Torvalds1da177e2005-04-16 15:20:36 -070012022 /* Derive initial jumbo mode from MTU assigned in
12023 * ether_setup() via the alloc_etherdev() call
12024 */
Michael Chan0f893dc2005-07-25 12:30:38 -070012025 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070012026 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070012027 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012028
12029 /* Determine WakeOnLan speed to use. */
12030 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12031 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12032 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
12033 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
12034 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
12035 } else {
12036 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
12037 }
12038
12039 /* A few boards don't want Ethernet@WireSpeed phy feature */
12040 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
12041 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
12042 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070012043 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Michael Chanb5d37722006-09-27 16:06:21 -070012044 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) ||
Michael Chan747e8f82005-07-25 12:33:22 -070012045 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012046 tp->tg3_flags2 |= TG3_FLG2_NO_ETH_WIRE_SPEED;
12047
12048 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
12049 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
12050 tp->tg3_flags2 |= TG3_FLG2_PHY_ADC_BUG;
12051 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
12052 tp->tg3_flags2 |= TG3_FLG2_PHY_5704_A0_BUG;
12053
Matt Carlson321d32a2008-11-21 17:22:19 -080012054 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
12055 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5906 &&
12056 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
12057 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780) {
Michael Chanc424cb22006-04-29 18:56:34 -070012058 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070012059 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070012060 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12061 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080012062 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
12063 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
12064 tp->tg3_flags2 |= TG3_FLG2_PHY_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080012065 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
12066 tp->tg3_flags2 |= TG3_FLG2_PHY_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080012067 } else
Michael Chanc424cb22006-04-29 18:56:34 -070012068 tp->tg3_flags2 |= TG3_FLG2_PHY_BER_BUG;
12069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012070
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012071 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12072 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
12073 tp->phy_otp = tg3_read_otp_phycfg(tp);
12074 if (tp->phy_otp == 0)
12075 tp->phy_otp = TG3_OTP_DEFAULT;
12076 }
12077
Matt Carlsonf51f3562008-05-25 23:45:08 -070012078 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070012079 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
12080 else
12081 tp->mi_mode = MAC_MI_MODE_BASE;
12082
Linus Torvalds1da177e2005-04-16 15:20:36 -070012083 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012084 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
12085 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
12086 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
12087
Matt Carlson321d32a2008-11-21 17:22:19 -080012088 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
12089 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070012090 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
12091
Matt Carlson158d7ab2008-05-29 01:37:54 -070012092 err = tg3_mdio_init(tp);
12093 if (err)
12094 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012095
12096 /* Initialize data/descriptor byte/word swapping. */
12097 val = tr32(GRC_MODE);
12098 val &= GRC_MODE_HOST_STACKUP;
12099 tw32(GRC_MODE, val | tp->grc_mode);
12100
12101 tg3_switch_clocks(tp);
12102
12103 /* Clear this out for sanity. */
12104 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
12105
12106 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
12107 &pci_state_reg);
12108 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
12109 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
12110 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
12111
12112 if (chiprevid == CHIPREV_ID_5701_A0 ||
12113 chiprevid == CHIPREV_ID_5701_B0 ||
12114 chiprevid == CHIPREV_ID_5701_B2 ||
12115 chiprevid == CHIPREV_ID_5701_B5) {
12116 void __iomem *sram_base;
12117
12118 /* Write some dummy words into the SRAM status block
12119 * area, see if it reads back correctly. If the return
12120 * value is bad, force enable the PCIX workaround.
12121 */
12122 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
12123
12124 writel(0x00000000, sram_base);
12125 writel(0x00000000, sram_base + 4);
12126 writel(0xffffffff, sram_base + 4);
12127 if (readl(sram_base) != 0x00000000)
12128 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
12129 }
12130 }
12131
12132 udelay(50);
12133 tg3_nvram_init(tp);
12134
12135 grc_misc_cfg = tr32(GRC_MISC_CFG);
12136 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
12137
Linus Torvalds1da177e2005-04-16 15:20:36 -070012138 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
12139 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
12140 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
12141 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
12142
David S. Millerfac9b832005-05-18 22:46:34 -070012143 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
12144 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
12145 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
12146 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
12147 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
12148 HOSTCC_MODE_CLRTICK_TXBD);
12149
12150 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
12151 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12152 tp->misc_host_ctrl);
12153 }
12154
Matt Carlson3bda1252008-08-15 14:08:22 -070012155 /* Preserve the APE MAC_MODE bits */
12156 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
12157 tp->mac_mode = tr32(MAC_MODE) |
12158 MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
12159 else
12160 tp->mac_mode = TG3_DEF_MAC_MODE;
12161
Linus Torvalds1da177e2005-04-16 15:20:36 -070012162 /* these are limited to 10/100 only */
12163 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
12164 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
12165 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
12166 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
12167 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
12168 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
12169 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
12170 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
12171 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080012172 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
12173 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012174 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Michael Chanb5d37722006-09-27 16:06:21 -070012175 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012176 tp->tg3_flags |= TG3_FLAG_10_100_ONLY;
12177
12178 err = tg3_phy_probe(tp);
12179 if (err) {
12180 printk(KERN_ERR PFX "(%s) phy probe failed, err %d\n",
12181 pci_name(tp->pdev), err);
12182 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012183 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012184 }
12185
12186 tg3_read_partno(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080012187 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012188
12189 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
12190 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
12191 } else {
12192 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
12193 tp->tg3_flags |= TG3_FLAG_USE_MI_INTERRUPT;
12194 else
12195 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
12196 }
12197
12198 /* 5700 {AX,BX} chips have a broken status block link
12199 * change bit implementation, so we must use the
12200 * status register in those cases.
12201 */
12202 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
12203 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
12204 else
12205 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
12206
12207 /* The led_ctrl is set during tg3_phy_probe, here we might
12208 * have to force the link status polling mechanism based
12209 * upon subsystem IDs.
12210 */
12211 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070012212 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012213 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
12214 tp->tg3_flags |= (TG3_FLAG_USE_MI_INTERRUPT |
12215 TG3_FLAG_USE_LINKCHG_REG);
12216 }
12217
12218 /* For all SERDES we poll the MAC status register. */
12219 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
12220 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
12221 else
12222 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
12223
Matt Carlsonad829262008-11-21 17:16:16 -080012224 tp->rx_offset = NET_IP_ALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012225 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
12226 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0)
12227 tp->rx_offset = 0;
12228
Michael Chanf92905d2006-06-29 20:14:29 -070012229 tp->rx_std_max_post = TG3_RX_RING_SIZE;
12230
12231 /* Increment the rx prod index on the rx std ring by at most
12232 * 8 for these chips to workaround hw errata.
12233 */
12234 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
12235 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
12236 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12237 tp->rx_std_max_post = 8;
12238
Matt Carlson8ed5d972007-05-07 00:25:49 -070012239 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
12240 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
12241 PCIE_PWR_MGMT_L1_THRESH_MSK;
12242
Linus Torvalds1da177e2005-04-16 15:20:36 -070012243 return err;
12244}
12245
David S. Miller49b6e95f2007-03-29 01:38:42 -070012246#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070012247static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
12248{
12249 struct net_device *dev = tp->dev;
12250 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070012251 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070012252 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070012253 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012254
David S. Miller49b6e95f2007-03-29 01:38:42 -070012255 addr = of_get_property(dp, "local-mac-address", &len);
12256 if (addr && len == 6) {
12257 memcpy(dev->dev_addr, addr, 6);
12258 memcpy(dev->perm_addr, dev->dev_addr, 6);
12259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012260 }
12261 return -ENODEV;
12262}
12263
12264static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
12265{
12266 struct net_device *dev = tp->dev;
12267
12268 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070012269 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012270 return 0;
12271}
12272#endif
12273
12274static int __devinit tg3_get_device_address(struct tg3 *tp)
12275{
12276 struct net_device *dev = tp->dev;
12277 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080012278 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012279
David S. Miller49b6e95f2007-03-29 01:38:42 -070012280#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070012281 if (!tg3_get_macaddr_sparc(tp))
12282 return 0;
12283#endif
12284
12285 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070012286 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070012287 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012288 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
12289 mac_offset = 0xcc;
12290 if (tg3_nvram_lock(tp))
12291 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
12292 else
12293 tg3_nvram_unlock(tp);
12294 }
Michael Chanb5d37722006-09-27 16:06:21 -070012295 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12296 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012297
12298 /* First try to get it from MAC address mailbox. */
12299 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
12300 if ((hi >> 16) == 0x484b) {
12301 dev->dev_addr[0] = (hi >> 8) & 0xff;
12302 dev->dev_addr[1] = (hi >> 0) & 0xff;
12303
12304 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
12305 dev->dev_addr[2] = (lo >> 24) & 0xff;
12306 dev->dev_addr[3] = (lo >> 16) & 0xff;
12307 dev->dev_addr[4] = (lo >> 8) & 0xff;
12308 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012309
Michael Chan008652b2006-03-27 23:14:53 -080012310 /* Some old bootcode may report a 0 MAC address in SRAM */
12311 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
12312 }
12313 if (!addr_ok) {
12314 /* Next, try NVRAM. */
David S. Millerf49639e2006-06-09 11:58:36 -070012315 if (!tg3_nvram_read(tp, mac_offset + 0, &hi) &&
Michael Chan008652b2006-03-27 23:14:53 -080012316 !tg3_nvram_read(tp, mac_offset + 4, &lo)) {
12317 dev->dev_addr[0] = ((hi >> 16) & 0xff);
12318 dev->dev_addr[1] = ((hi >> 24) & 0xff);
12319 dev->dev_addr[2] = ((lo >> 0) & 0xff);
12320 dev->dev_addr[3] = ((lo >> 8) & 0xff);
12321 dev->dev_addr[4] = ((lo >> 16) & 0xff);
12322 dev->dev_addr[5] = ((lo >> 24) & 0xff);
12323 }
12324 /* Finally just fetch it out of the MAC control regs. */
12325 else {
12326 hi = tr32(MAC_ADDR_0_HIGH);
12327 lo = tr32(MAC_ADDR_0_LOW);
12328
12329 dev->dev_addr[5] = lo & 0xff;
12330 dev->dev_addr[4] = (lo >> 8) & 0xff;
12331 dev->dev_addr[3] = (lo >> 16) & 0xff;
12332 dev->dev_addr[2] = (lo >> 24) & 0xff;
12333 dev->dev_addr[1] = hi & 0xff;
12334 dev->dev_addr[0] = (hi >> 8) & 0xff;
12335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012336 }
12337
12338 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070012339#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070012340 if (!tg3_get_default_macaddr_sparc(tp))
12341 return 0;
12342#endif
12343 return -EINVAL;
12344 }
John W. Linville2ff43692005-09-12 14:44:20 -070012345 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012346 return 0;
12347}
12348
David S. Miller59e6b432005-05-18 22:50:10 -070012349#define BOUNDARY_SINGLE_CACHELINE 1
12350#define BOUNDARY_MULTI_CACHELINE 2
12351
12352static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
12353{
12354 int cacheline_size;
12355 u8 byte;
12356 int goal;
12357
12358 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
12359 if (byte == 0)
12360 cacheline_size = 1024;
12361 else
12362 cacheline_size = (int) byte * 4;
12363
12364 /* On 5703 and later chips, the boundary bits have no
12365 * effect.
12366 */
12367 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12368 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
12369 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
12370 goto out;
12371
12372#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
12373 goal = BOUNDARY_MULTI_CACHELINE;
12374#else
12375#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
12376 goal = BOUNDARY_SINGLE_CACHELINE;
12377#else
12378 goal = 0;
12379#endif
12380#endif
12381
12382 if (!goal)
12383 goto out;
12384
12385 /* PCI controllers on most RISC systems tend to disconnect
12386 * when a device tries to burst across a cache-line boundary.
12387 * Therefore, letting tg3 do so just wastes PCI bandwidth.
12388 *
12389 * Unfortunately, for PCI-E there are only limited
12390 * write-side controls for this, and thus for reads
12391 * we will still get the disconnects. We'll also waste
12392 * these PCI cycles for both read and write for chips
12393 * other than 5700 and 5701 which do not implement the
12394 * boundary bits.
12395 */
12396 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
12397 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
12398 switch (cacheline_size) {
12399 case 16:
12400 case 32:
12401 case 64:
12402 case 128:
12403 if (goal == BOUNDARY_SINGLE_CACHELINE) {
12404 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
12405 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
12406 } else {
12407 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
12408 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
12409 }
12410 break;
12411
12412 case 256:
12413 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
12414 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
12415 break;
12416
12417 default:
12418 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
12419 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
12420 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070012421 }
David S. Miller59e6b432005-05-18 22:50:10 -070012422 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
12423 switch (cacheline_size) {
12424 case 16:
12425 case 32:
12426 case 64:
12427 if (goal == BOUNDARY_SINGLE_CACHELINE) {
12428 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
12429 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
12430 break;
12431 }
12432 /* fallthrough */
12433 case 128:
12434 default:
12435 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
12436 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
12437 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070012438 }
David S. Miller59e6b432005-05-18 22:50:10 -070012439 } else {
12440 switch (cacheline_size) {
12441 case 16:
12442 if (goal == BOUNDARY_SINGLE_CACHELINE) {
12443 val |= (DMA_RWCTRL_READ_BNDRY_16 |
12444 DMA_RWCTRL_WRITE_BNDRY_16);
12445 break;
12446 }
12447 /* fallthrough */
12448 case 32:
12449 if (goal == BOUNDARY_SINGLE_CACHELINE) {
12450 val |= (DMA_RWCTRL_READ_BNDRY_32 |
12451 DMA_RWCTRL_WRITE_BNDRY_32);
12452 break;
12453 }
12454 /* fallthrough */
12455 case 64:
12456 if (goal == BOUNDARY_SINGLE_CACHELINE) {
12457 val |= (DMA_RWCTRL_READ_BNDRY_64 |
12458 DMA_RWCTRL_WRITE_BNDRY_64);
12459 break;
12460 }
12461 /* fallthrough */
12462 case 128:
12463 if (goal == BOUNDARY_SINGLE_CACHELINE) {
12464 val |= (DMA_RWCTRL_READ_BNDRY_128 |
12465 DMA_RWCTRL_WRITE_BNDRY_128);
12466 break;
12467 }
12468 /* fallthrough */
12469 case 256:
12470 val |= (DMA_RWCTRL_READ_BNDRY_256 |
12471 DMA_RWCTRL_WRITE_BNDRY_256);
12472 break;
12473 case 512:
12474 val |= (DMA_RWCTRL_READ_BNDRY_512 |
12475 DMA_RWCTRL_WRITE_BNDRY_512);
12476 break;
12477 case 1024:
12478 default:
12479 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
12480 DMA_RWCTRL_WRITE_BNDRY_1024);
12481 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070012482 }
David S. Miller59e6b432005-05-18 22:50:10 -070012483 }
12484
12485out:
12486 return val;
12487}
12488
Linus Torvalds1da177e2005-04-16 15:20:36 -070012489static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
12490{
12491 struct tg3_internal_buffer_desc test_desc;
12492 u32 sram_dma_descs;
12493 int i, ret;
12494
12495 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
12496
12497 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
12498 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
12499 tw32(RDMAC_STATUS, 0);
12500 tw32(WDMAC_STATUS, 0);
12501
12502 tw32(BUFMGR_MODE, 0);
12503 tw32(FTQ_RESET, 0);
12504
12505 test_desc.addr_hi = ((u64) buf_dma) >> 32;
12506 test_desc.addr_lo = buf_dma & 0xffffffff;
12507 test_desc.nic_mbuf = 0x00002100;
12508 test_desc.len = size;
12509
12510 /*
12511 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
12512 * the *second* time the tg3 driver was getting loaded after an
12513 * initial scan.
12514 *
12515 * Broadcom tells me:
12516 * ...the DMA engine is connected to the GRC block and a DMA
12517 * reset may affect the GRC block in some unpredictable way...
12518 * The behavior of resets to individual blocks has not been tested.
12519 *
12520 * Broadcom noted the GRC reset will also reset all sub-components.
12521 */
12522 if (to_device) {
12523 test_desc.cqid_sqid = (13 << 8) | 2;
12524
12525 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
12526 udelay(40);
12527 } else {
12528 test_desc.cqid_sqid = (16 << 8) | 7;
12529
12530 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
12531 udelay(40);
12532 }
12533 test_desc.flags = 0x00000005;
12534
12535 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
12536 u32 val;
12537
12538 val = *(((u32 *)&test_desc) + i);
12539 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
12540 sram_dma_descs + (i * sizeof(u32)));
12541 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
12542 }
12543 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
12544
12545 if (to_device) {
12546 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
12547 } else {
12548 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
12549 }
12550
12551 ret = -ENODEV;
12552 for (i = 0; i < 40; i++) {
12553 u32 val;
12554
12555 if (to_device)
12556 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
12557 else
12558 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
12559 if ((val & 0xffff) == sram_dma_descs) {
12560 ret = 0;
12561 break;
12562 }
12563
12564 udelay(100);
12565 }
12566
12567 return ret;
12568}
12569
David S. Millerded73402005-05-23 13:59:47 -070012570#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070012571
12572static int __devinit tg3_test_dma(struct tg3 *tp)
12573{
12574 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070012575 u32 *buf, saved_dma_rwctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012576 int ret;
12577
12578 buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
12579 if (!buf) {
12580 ret = -ENOMEM;
12581 goto out_nofree;
12582 }
12583
12584 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
12585 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
12586
David S. Miller59e6b432005-05-18 22:50:10 -070012587 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012588
12589 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
12590 /* DMA read watermark not used on PCIE */
12591 tp->dma_rwctrl |= 0x00180000;
12592 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070012593 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
12594 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012595 tp->dma_rwctrl |= 0x003f0000;
12596 else
12597 tp->dma_rwctrl |= 0x003f000f;
12598 } else {
12599 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
12600 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
12601 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080012602 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012603
Michael Chan4a29cc22006-03-19 13:21:12 -080012604 /* If the 5704 is behind the EPB bridge, we can
12605 * do the less restrictive ONE_DMA workaround for
12606 * better performance.
12607 */
12608 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
12609 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
12610 tp->dma_rwctrl |= 0x8000;
12611 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012612 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
12613
Michael Chan49afdeb2007-02-13 12:17:03 -080012614 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
12615 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070012616 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080012617 tp->dma_rwctrl |=
12618 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
12619 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
12620 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070012621 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
12622 /* 5780 always in PCIX mode */
12623 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070012624 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
12625 /* 5714 always in PCIX mode */
12626 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012627 } else {
12628 tp->dma_rwctrl |= 0x001b000f;
12629 }
12630 }
12631
12632 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
12633 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
12634 tp->dma_rwctrl &= 0xfffffff0;
12635
12636 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12637 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
12638 /* Remove this if it causes problems for some boards. */
12639 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
12640
12641 /* On 5700/5701 chips, we need to set this bit.
12642 * Otherwise the chip will issue cacheline transactions
12643 * to streamable DMA memory with not all the byte
12644 * enables turned on. This is an error on several
12645 * RISC PCI controllers, in particular sparc64.
12646 *
12647 * On 5703/5704 chips, this bit has been reassigned
12648 * a different meaning. In particular, it is used
12649 * on those chips to enable a PCI-X workaround.
12650 */
12651 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
12652 }
12653
12654 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
12655
12656#if 0
12657 /* Unneeded, already done by tg3_get_invariants. */
12658 tg3_switch_clocks(tp);
12659#endif
12660
12661 ret = 0;
12662 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12663 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
12664 goto out;
12665
David S. Miller59e6b432005-05-18 22:50:10 -070012666 /* It is best to perform DMA test with maximum write burst size
12667 * to expose the 5700/5701 write DMA bug.
12668 */
12669 saved_dma_rwctrl = tp->dma_rwctrl;
12670 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
12671 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
12672
Linus Torvalds1da177e2005-04-16 15:20:36 -070012673 while (1) {
12674 u32 *p = buf, i;
12675
12676 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
12677 p[i] = i;
12678
12679 /* Send the buffer to the chip. */
12680 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
12681 if (ret) {
12682 printk(KERN_ERR "tg3_test_dma() Write the buffer failed %d\n", ret);
12683 break;
12684 }
12685
12686#if 0
12687 /* validate data reached card RAM correctly. */
12688 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
12689 u32 val;
12690 tg3_read_mem(tp, 0x2100 + (i*4), &val);
12691 if (le32_to_cpu(val) != p[i]) {
12692 printk(KERN_ERR " tg3_test_dma() Card buffer corrupted on write! (%d != %d)\n", val, i);
12693 /* ret = -ENODEV here? */
12694 }
12695 p[i] = 0;
12696 }
12697#endif
12698 /* Now read it back. */
12699 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
12700 if (ret) {
12701 printk(KERN_ERR "tg3_test_dma() Read the buffer failed %d\n", ret);
12702
12703 break;
12704 }
12705
12706 /* Verify it. */
12707 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
12708 if (p[i] == i)
12709 continue;
12710
David S. Miller59e6b432005-05-18 22:50:10 -070012711 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
12712 DMA_RWCTRL_WRITE_BNDRY_16) {
12713 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012714 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
12715 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
12716 break;
12717 } else {
12718 printk(KERN_ERR "tg3_test_dma() buffer corrupted on read back! (%d != %d)\n", p[i], i);
12719 ret = -ENODEV;
12720 goto out;
12721 }
12722 }
12723
12724 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
12725 /* Success. */
12726 ret = 0;
12727 break;
12728 }
12729 }
David S. Miller59e6b432005-05-18 22:50:10 -070012730 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
12731 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070012732 static struct pci_device_id dma_wait_state_chipsets[] = {
12733 { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
12734 PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
12735 { },
12736 };
12737
David S. Miller59e6b432005-05-18 22:50:10 -070012738 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070012739 * now look for chipsets that are known to expose the
12740 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070012741 */
Michael Chan6d1cfba2005-06-08 14:13:14 -070012742 if (pci_dev_present(dma_wait_state_chipsets)) {
12743 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
12744 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
12745 }
12746 else
12747 /* Safe to use the calculated DMA boundary. */
12748 tp->dma_rwctrl = saved_dma_rwctrl;
12749
David S. Miller59e6b432005-05-18 22:50:10 -070012750 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
12751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012752
12753out:
12754 pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
12755out_nofree:
12756 return ret;
12757}
12758
12759static void __devinit tg3_init_link_config(struct tg3 *tp)
12760{
12761 tp->link_config.advertising =
12762 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12763 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12764 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
12765 ADVERTISED_Autoneg | ADVERTISED_MII);
12766 tp->link_config.speed = SPEED_INVALID;
12767 tp->link_config.duplex = DUPLEX_INVALID;
12768 tp->link_config.autoneg = AUTONEG_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012769 tp->link_config.active_speed = SPEED_INVALID;
12770 tp->link_config.active_duplex = DUPLEX_INVALID;
12771 tp->link_config.phy_is_low_power = 0;
12772 tp->link_config.orig_speed = SPEED_INVALID;
12773 tp->link_config.orig_duplex = DUPLEX_INVALID;
12774 tp->link_config.orig_autoneg = AUTONEG_INVALID;
12775}
12776
12777static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
12778{
Michael Chanfdfec1722005-07-25 12:31:48 -070012779 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
12780 tp->bufmgr_config.mbuf_read_dma_low_water =
12781 DEFAULT_MB_RDMA_LOW_WATER_5705;
12782 tp->bufmgr_config.mbuf_mac_rx_low_water =
12783 DEFAULT_MB_MACRX_LOW_WATER_5705;
12784 tp->bufmgr_config.mbuf_high_water =
12785 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070012786 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
12787 tp->bufmgr_config.mbuf_mac_rx_low_water =
12788 DEFAULT_MB_MACRX_LOW_WATER_5906;
12789 tp->bufmgr_config.mbuf_high_water =
12790 DEFAULT_MB_HIGH_WATER_5906;
12791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012792
Michael Chanfdfec1722005-07-25 12:31:48 -070012793 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
12794 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
12795 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
12796 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
12797 tp->bufmgr_config.mbuf_high_water_jumbo =
12798 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
12799 } else {
12800 tp->bufmgr_config.mbuf_read_dma_low_water =
12801 DEFAULT_MB_RDMA_LOW_WATER;
12802 tp->bufmgr_config.mbuf_mac_rx_low_water =
12803 DEFAULT_MB_MACRX_LOW_WATER;
12804 tp->bufmgr_config.mbuf_high_water =
12805 DEFAULT_MB_HIGH_WATER;
12806
12807 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
12808 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
12809 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
12810 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
12811 tp->bufmgr_config.mbuf_high_water_jumbo =
12812 DEFAULT_MB_HIGH_WATER_JUMBO;
12813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012814
12815 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
12816 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
12817}
12818
12819static char * __devinit tg3_phy_string(struct tg3 *tp)
12820{
12821 switch (tp->phy_id & PHY_ID_MASK) {
12822 case PHY_ID_BCM5400: return "5400";
12823 case PHY_ID_BCM5401: return "5401";
12824 case PHY_ID_BCM5411: return "5411";
12825 case PHY_ID_BCM5701: return "5701";
12826 case PHY_ID_BCM5703: return "5703";
12827 case PHY_ID_BCM5704: return "5704";
12828 case PHY_ID_BCM5705: return "5705";
12829 case PHY_ID_BCM5750: return "5750";
Michael Chan85e94ce2005-04-21 17:05:28 -070012830 case PHY_ID_BCM5752: return "5752";
Michael Chana4e2b342005-10-26 15:46:52 -070012831 case PHY_ID_BCM5714: return "5714";
Michael Chan4cf78e42005-07-25 12:29:19 -070012832 case PHY_ID_BCM5780: return "5780";
Michael Chanaf36e6b2006-03-23 01:28:06 -080012833 case PHY_ID_BCM5755: return "5755";
Michael Chand9ab5ad12006-03-20 22:27:35 -080012834 case PHY_ID_BCM5787: return "5787";
Matt Carlsond30cdd22007-10-07 23:28:35 -070012835 case PHY_ID_BCM5784: return "5784";
Michael Chan126a3362006-09-27 16:03:07 -070012836 case PHY_ID_BCM5756: return "5722/5756";
Michael Chanb5d37722006-09-27 16:06:21 -070012837 case PHY_ID_BCM5906: return "5906";
Matt Carlson9936bcf2007-10-10 18:03:07 -070012838 case PHY_ID_BCM5761: return "5761";
Linus Torvalds1da177e2005-04-16 15:20:36 -070012839 case PHY_ID_BCM8002: return "8002/serdes";
12840 case 0: return "serdes";
12841 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070012842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012843}
12844
Michael Chanf9804dd2005-09-27 12:13:10 -070012845static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
12846{
12847 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
12848 strcpy(str, "PCI Express");
12849 return str;
12850 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
12851 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
12852
12853 strcpy(str, "PCIX:");
12854
12855 if ((clock_ctrl == 7) ||
12856 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
12857 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
12858 strcat(str, "133MHz");
12859 else if (clock_ctrl == 0)
12860 strcat(str, "33MHz");
12861 else if (clock_ctrl == 2)
12862 strcat(str, "50MHz");
12863 else if (clock_ctrl == 4)
12864 strcat(str, "66MHz");
12865 else if (clock_ctrl == 6)
12866 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070012867 } else {
12868 strcpy(str, "PCI:");
12869 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
12870 strcat(str, "66MHz");
12871 else
12872 strcat(str, "33MHz");
12873 }
12874 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
12875 strcat(str, ":32-bit");
12876 else
12877 strcat(str, ":64-bit");
12878 return str;
12879}
12880
Michael Chan8c2dc7e2005-12-19 16:26:02 -080012881static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012882{
12883 struct pci_dev *peer;
12884 unsigned int func, devnr = tp->pdev->devfn & ~7;
12885
12886 for (func = 0; func < 8; func++) {
12887 peer = pci_get_slot(tp->pdev->bus, devnr | func);
12888 if (peer && peer != tp->pdev)
12889 break;
12890 pci_dev_put(peer);
12891 }
Michael Chan16fe9d72005-12-13 21:09:54 -080012892 /* 5704 can be configured in single-port mode, set peer to
12893 * tp->pdev in that case.
12894 */
12895 if (!peer) {
12896 peer = tp->pdev;
12897 return peer;
12898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012899
12900 /*
12901 * We don't need to keep the refcount elevated; there's no way
12902 * to remove one half of this device without removing the other
12903 */
12904 pci_dev_put(peer);
12905
12906 return peer;
12907}
12908
David S. Miller15f98502005-05-18 22:49:26 -070012909static void __devinit tg3_init_coal(struct tg3 *tp)
12910{
12911 struct ethtool_coalesce *ec = &tp->coal;
12912
12913 memset(ec, 0, sizeof(*ec));
12914 ec->cmd = ETHTOOL_GCOALESCE;
12915 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
12916 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
12917 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
12918 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
12919 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
12920 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
12921 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
12922 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
12923 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
12924
12925 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
12926 HOSTCC_MODE_CLRTICK_TXBD)) {
12927 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
12928 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
12929 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
12930 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
12931 }
Michael Chand244c892005-07-05 14:42:33 -070012932
12933 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
12934 ec->rx_coalesce_usecs_irq = 0;
12935 ec->tx_coalesce_usecs_irq = 0;
12936 ec->stats_block_coalesce_usecs = 0;
12937 }
David S. Miller15f98502005-05-18 22:49:26 -070012938}
12939
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080012940static const struct net_device_ops tg3_netdev_ops = {
12941 .ndo_open = tg3_open,
12942 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080012943 .ndo_start_xmit = tg3_start_xmit,
12944 .ndo_get_stats = tg3_get_stats,
12945 .ndo_validate_addr = eth_validate_addr,
12946 .ndo_set_multicast_list = tg3_set_rx_mode,
12947 .ndo_set_mac_address = tg3_set_mac_addr,
12948 .ndo_do_ioctl = tg3_ioctl,
12949 .ndo_tx_timeout = tg3_tx_timeout,
12950 .ndo_change_mtu = tg3_change_mtu,
12951#if TG3_VLAN_TAG_USED
12952 .ndo_vlan_rx_register = tg3_vlan_rx_register,
12953#endif
12954#ifdef CONFIG_NET_POLL_CONTROLLER
12955 .ndo_poll_controller = tg3_poll_controller,
12956#endif
12957};
12958
12959static const struct net_device_ops tg3_netdev_ops_dma_bug = {
12960 .ndo_open = tg3_open,
12961 .ndo_stop = tg3_close,
12962 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080012963 .ndo_get_stats = tg3_get_stats,
12964 .ndo_validate_addr = eth_validate_addr,
12965 .ndo_set_multicast_list = tg3_set_rx_mode,
12966 .ndo_set_mac_address = tg3_set_mac_addr,
12967 .ndo_do_ioctl = tg3_ioctl,
12968 .ndo_tx_timeout = tg3_tx_timeout,
12969 .ndo_change_mtu = tg3_change_mtu,
12970#if TG3_VLAN_TAG_USED
12971 .ndo_vlan_rx_register = tg3_vlan_rx_register,
12972#endif
12973#ifdef CONFIG_NET_POLL_CONTROLLER
12974 .ndo_poll_controller = tg3_poll_controller,
12975#endif
12976};
12977
Linus Torvalds1da177e2005-04-16 15:20:36 -070012978static int __devinit tg3_init_one(struct pci_dev *pdev,
12979 const struct pci_device_id *ent)
12980{
12981 static int tg3_version_printed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012982 struct net_device *dev;
12983 struct tg3 *tp;
Joe Perchesd6645372007-12-20 04:06:59 -080012984 int err, pm_cap;
Michael Chanf9804dd2005-09-27 12:13:10 -070012985 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080012986 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012987
12988 if (tg3_version_printed++ == 0)
12989 printk(KERN_INFO "%s", version);
12990
12991 err = pci_enable_device(pdev);
12992 if (err) {
12993 printk(KERN_ERR PFX "Cannot enable PCI device, "
12994 "aborting.\n");
12995 return err;
12996 }
12997
Linus Torvalds1da177e2005-04-16 15:20:36 -070012998 err = pci_request_regions(pdev, DRV_MODULE_NAME);
12999 if (err) {
13000 printk(KERN_ERR PFX "Cannot obtain PCI resources, "
13001 "aborting.\n");
13002 goto err_out_disable_pdev;
13003 }
13004
13005 pci_set_master(pdev);
13006
13007 /* Find power-management capability. */
13008 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
13009 if (pm_cap == 0) {
13010 printk(KERN_ERR PFX "Cannot find PowerManagement capability, "
13011 "aborting.\n");
13012 err = -EIO;
13013 goto err_out_free_res;
13014 }
13015
Linus Torvalds1da177e2005-04-16 15:20:36 -070013016 dev = alloc_etherdev(sizeof(*tp));
13017 if (!dev) {
13018 printk(KERN_ERR PFX "Etherdev alloc failed, aborting.\n");
13019 err = -ENOMEM;
13020 goto err_out_free_res;
13021 }
13022
Linus Torvalds1da177e2005-04-16 15:20:36 -070013023 SET_NETDEV_DEV(dev, &pdev->dev);
13024
Linus Torvalds1da177e2005-04-16 15:20:36 -070013025#if TG3_VLAN_TAG_USED
13026 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013027#endif
13028
13029 tp = netdev_priv(dev);
13030 tp->pdev = pdev;
13031 tp->dev = dev;
13032 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013033 tp->rx_mode = TG3_DEF_RX_MODE;
13034 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070013035
Linus Torvalds1da177e2005-04-16 15:20:36 -070013036 if (tg3_debug > 0)
13037 tp->msg_enable = tg3_debug;
13038 else
13039 tp->msg_enable = TG3_DEF_MSG_ENABLE;
13040
13041 /* The word/byte swap controls here control register access byte
13042 * swapping. DMA data byte swapping is controlled in the GRC_MODE
13043 * setting below.
13044 */
13045 tp->misc_host_ctrl =
13046 MISC_HOST_CTRL_MASK_PCI_INT |
13047 MISC_HOST_CTRL_WORD_SWAP |
13048 MISC_HOST_CTRL_INDIR_ACCESS |
13049 MISC_HOST_CTRL_PCISTATE_RW;
13050
13051 /* The NONFRM (non-frame) byte/word swap controls take effect
13052 * on descriptor entries, anything which isn't packet data.
13053 *
13054 * The StrongARM chips on the board (one for tx, one for rx)
13055 * are running in big-endian mode.
13056 */
13057 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
13058 GRC_MODE_WSWAP_NONFRM_DATA);
13059#ifdef __BIG_ENDIAN
13060 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
13061#endif
13062 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013063 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000013064 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013065
Matt Carlsond5fe4882008-11-21 17:20:32 -080013066 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010013067 if (!tp->regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013068 printk(KERN_ERR PFX "Cannot map device registers, "
13069 "aborting.\n");
13070 err = -ENOMEM;
13071 goto err_out_free_dev;
13072 }
13073
13074 tg3_init_link_config(tp);
13075
Linus Torvalds1da177e2005-04-16 15:20:36 -070013076 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
13077 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
13078 tp->tx_pending = TG3_DEF_TX_RING_PENDING;
13079
Stephen Hemmingerbea33482007-10-03 16:41:36 -070013080 netif_napi_add(dev, &tp->napi, tg3_poll, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013081 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013082 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013083 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013084
13085 err = tg3_get_invariants(tp);
13086 if (err) {
13087 printk(KERN_ERR PFX "Problem fetching invariants of chip, "
13088 "aborting.\n");
13089 goto err_out_iounmap;
13090 }
13091
Matt Carlson321d32a2008-11-21 17:22:19 -080013092 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Stephen Hemminger00829822008-11-20 20:14:53 -080013093 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13094 dev->netdev_ops = &tg3_netdev_ops;
13095 else
13096 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
13097
13098
Michael Chan4a29cc22006-03-19 13:21:12 -080013099 /* The EPB bridge inside 5714, 5715, and 5780 and any
13100 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080013101 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
13102 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
13103 * do DMA address check in tg3_start_xmit().
13104 */
Michael Chan4a29cc22006-03-19 13:21:12 -080013105 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
13106 persist_dma_mask = dma_mask = DMA_32BIT_MASK;
13107 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Michael Chan72f2afb2006-03-06 19:28:35 -080013108 persist_dma_mask = dma_mask = DMA_40BIT_MASK;
13109#ifdef CONFIG_HIGHMEM
13110 dma_mask = DMA_64BIT_MASK;
13111#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080013112 } else
Michael Chan72f2afb2006-03-06 19:28:35 -080013113 persist_dma_mask = dma_mask = DMA_64BIT_MASK;
13114
13115 /* Configure DMA attributes. */
13116 if (dma_mask > DMA_32BIT_MASK) {
13117 err = pci_set_dma_mask(pdev, dma_mask);
13118 if (!err) {
13119 dev->features |= NETIF_F_HIGHDMA;
13120 err = pci_set_consistent_dma_mask(pdev,
13121 persist_dma_mask);
13122 if (err < 0) {
13123 printk(KERN_ERR PFX "Unable to obtain 64 bit "
13124 "DMA for consistent allocations\n");
13125 goto err_out_iounmap;
13126 }
13127 }
13128 }
13129 if (err || dma_mask == DMA_32BIT_MASK) {
13130 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
13131 if (err) {
13132 printk(KERN_ERR PFX "No usable DMA configuration, "
13133 "aborting.\n");
13134 goto err_out_iounmap;
13135 }
13136 }
13137
Michael Chanfdfec1722005-07-25 12:31:48 -070013138 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013139
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080013140 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080013141 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080013142
Linus Torvalds1da177e2005-04-16 15:20:36 -070013143 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
13144 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
13145 }
13146 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13147 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13148 tp->pci_chip_rev_id == CHIPREV_ID_5705_A0 ||
Michael Chanc7835a72006-11-15 21:14:42 -080013149 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070013150 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
13151 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
13152 } else {
Michael Chan7f62ad52007-02-20 23:25:40 -080013153 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080013154 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
Matt Carlson9e9fd122009-01-19 16:57:45 -080013155 tp->fw_needed = FIRMWARE_TG3TSO5;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080013156 else
Matt Carlson9e9fd122009-01-19 16:57:45 -080013157 tp->fw_needed = FIRMWARE_TG3TSO;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080013158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013159
Michael Chan4e3a7aa2006-03-20 17:47:44 -080013160 /* TSO is on by default on chips that support hardware TSO.
13161 * Firmware TSO on older chips gives lower performance, so it
13162 * is off by default, but can be enabled using ethtool.
13163 */
Michael Chanb0026622006-07-03 19:42:14 -070013164 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Matt Carlson027455a2008-12-21 20:19:30 -080013165 if (dev->features & NETIF_F_IP_CSUM)
13166 dev->features |= NETIF_F_TSO;
13167 if ((dev->features & NETIF_F_IPV6_CSUM) &&
13168 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2))
Michael Chanb0026622006-07-03 19:42:14 -070013169 dev->features |= NETIF_F_TSO6;
Matt Carlson57e69832008-05-25 23:48:31 -070013170 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
13171 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13172 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013173 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13174 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson9936bcf2007-10-10 18:03:07 -070013175 dev->features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070013176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013177
Linus Torvalds1da177e2005-04-16 15:20:36 -070013178
13179 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
13180 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
13181 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
13182 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
13183 tp->rx_pending = 63;
13184 }
13185
Linus Torvalds1da177e2005-04-16 15:20:36 -070013186 err = tg3_get_device_address(tp);
13187 if (err) {
13188 printk(KERN_ERR PFX "Could not obtain valid ethernet address, "
13189 "aborting.\n");
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080013190 goto err_out_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013191 }
13192
Matt Carlson0d3031d2007-10-10 18:02:43 -070013193 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080013194 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080013195 if (!tp->aperegs) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070013196 printk(KERN_ERR PFX "Cannot map APE registers, "
13197 "aborting.\n");
13198 err = -ENOMEM;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080013199 goto err_out_fw;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013200 }
13201
13202 tg3_ape_lock_init(tp);
13203 }
13204
Matt Carlsonc88864d2007-11-12 21:07:01 -080013205 /*
13206 * Reset chip in case UNDI or EFI driver did not shutdown
13207 * DMA self test will enable WDMAC and we'll see (spurious)
13208 * pending DMA on the PCI bus at that point.
13209 */
13210 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
13211 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
13212 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
13213 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13214 }
13215
13216 err = tg3_test_dma(tp);
13217 if (err) {
13218 printk(KERN_ERR PFX "DMA engine test failed, aborting.\n");
13219 goto err_out_apeunmap;
13220 }
13221
Matt Carlsonc88864d2007-11-12 21:07:01 -080013222 /* flow control autonegotiation is default behavior */
13223 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
Steve Glendinninge18ce342008-12-16 02:00:00 -080013224 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlsonc88864d2007-11-12 21:07:01 -080013225
13226 tg3_init_coal(tp);
13227
Michael Chanc49a1562006-12-17 17:07:29 -080013228 pci_set_drvdata(pdev, dev);
13229
Linus Torvalds1da177e2005-04-16 15:20:36 -070013230 err = register_netdev(dev);
13231 if (err) {
13232 printk(KERN_ERR PFX "Cannot register net device, "
13233 "aborting.\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070013234 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013235 }
13236
Matt Carlsondf59c942008-11-03 16:52:56 -080013237 printk(KERN_INFO "%s: Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070013238 dev->name,
13239 tp->board_part_number,
13240 tp->pci_chip_rev_id,
Michael Chanf9804dd2005-09-27 12:13:10 -070013241 tg3_bus_string(tp, str),
Johannes Berge1749612008-10-27 15:59:26 -070013242 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013243
Matt Carlsondf59c942008-11-03 16:52:56 -080013244 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED)
13245 printk(KERN_INFO
13246 "%s: attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
13247 tp->dev->name,
13248 tp->mdio_bus->phy_map[PHY_ADDR]->drv->name,
Kay Sieversfb28ad32008-11-10 13:55:14 -080013249 dev_name(&tp->mdio_bus->phy_map[PHY_ADDR]->dev));
Matt Carlsondf59c942008-11-03 16:52:56 -080013250 else
13251 printk(KERN_INFO
13252 "%s: attached PHY is %s (%s Ethernet) (WireSpeed[%d])\n",
13253 tp->dev->name, tg3_phy_string(tp),
13254 ((tp->tg3_flags & TG3_FLAG_10_100_ONLY) ? "10/100Base-TX" :
13255 ((tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) ? "1000Base-SX" :
13256 "10/100/1000Base-T")),
13257 (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED) == 0);
13258
13259 printk(KERN_INFO "%s: RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070013260 dev->name,
13261 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
13262 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
13263 (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) != 0,
13264 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013265 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
Michael Chan4a29cc22006-03-19 13:21:12 -080013266 printk(KERN_INFO "%s: dma_rwctrl[%08x] dma_mask[%d-bit]\n",
13267 dev->name, tp->dma_rwctrl,
13268 (pdev->dma_mask == DMA_32BIT_MASK) ? 32 :
13269 (((u64) pdev->dma_mask == DMA_40BIT_MASK) ? 40 : 64));
Linus Torvalds1da177e2005-04-16 15:20:36 -070013270
13271 return 0;
13272
Matt Carlson0d3031d2007-10-10 18:02:43 -070013273err_out_apeunmap:
13274 if (tp->aperegs) {
13275 iounmap(tp->aperegs);
13276 tp->aperegs = NULL;
13277 }
13278
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080013279err_out_fw:
13280 if (tp->fw)
13281 release_firmware(tp->fw);
13282
Linus Torvalds1da177e2005-04-16 15:20:36 -070013283err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070013284 if (tp->regs) {
13285 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013286 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013288
13289err_out_free_dev:
13290 free_netdev(dev);
13291
13292err_out_free_res:
13293 pci_release_regions(pdev);
13294
13295err_out_disable_pdev:
13296 pci_disable_device(pdev);
13297 pci_set_drvdata(pdev, NULL);
13298 return err;
13299}
13300
13301static void __devexit tg3_remove_one(struct pci_dev *pdev)
13302{
13303 struct net_device *dev = pci_get_drvdata(pdev);
13304
13305 if (dev) {
13306 struct tg3 *tp = netdev_priv(dev);
13307
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080013308 if (tp->fw)
13309 release_firmware(tp->fw);
13310
Michael Chan7faa0062006-02-02 17:29:28 -080013311 flush_scheduled_work();
Matt Carlson158d7ab2008-05-29 01:37:54 -070013312
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013313 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
13314 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070013315 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013316 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070013317
Linus Torvalds1da177e2005-04-16 15:20:36 -070013318 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070013319 if (tp->aperegs) {
13320 iounmap(tp->aperegs);
13321 tp->aperegs = NULL;
13322 }
Michael Chan68929142005-08-09 20:17:14 -070013323 if (tp->regs) {
13324 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013325 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013327 free_netdev(dev);
13328 pci_release_regions(pdev);
13329 pci_disable_device(pdev);
13330 pci_set_drvdata(pdev, NULL);
13331 }
13332}
13333
13334static int tg3_suspend(struct pci_dev *pdev, pm_message_t state)
13335{
13336 struct net_device *dev = pci_get_drvdata(pdev);
13337 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070013338 pci_power_t target_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013339 int err;
13340
Michael Chan3e0c95f2007-08-03 20:56:54 -070013341 /* PCI register 4 needs to be saved whether netif_running() or not.
13342 * MSI address and data need to be saved if using MSI and
13343 * netif_running().
13344 */
13345 pci_save_state(pdev);
13346
Linus Torvalds1da177e2005-04-16 15:20:36 -070013347 if (!netif_running(dev))
13348 return 0;
13349
Michael Chan7faa0062006-02-02 17:29:28 -080013350 flush_scheduled_work();
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013351 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013352 tg3_netif_stop(tp);
13353
13354 del_timer_sync(&tp->timer);
13355
David S. Millerf47c11e2005-06-24 20:18:35 -070013356 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013357 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070013358 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013359
13360 netif_device_detach(dev);
13361
David S. Millerf47c11e2005-06-24 20:18:35 -070013362 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070013363 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080013364 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070013365 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013366
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070013367 target_state = pdev->pm_cap ? pci_target_state(pdev) : PCI_D3hot;
13368
13369 err = tg3_set_power_state(tp, target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013370 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013371 int err2;
13372
David S. Millerf47c11e2005-06-24 20:18:35 -070013373 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013374
Michael Chan6a9eba12005-12-13 21:08:58 -080013375 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013376 err2 = tg3_restart_hw(tp, 1);
13377 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070013378 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013379
13380 tp->timer.expires = jiffies + tp->timer_offset;
13381 add_timer(&tp->timer);
13382
13383 netif_device_attach(dev);
13384 tg3_netif_start(tp);
13385
Michael Chanb9ec6c12006-07-25 16:37:27 -070013386out:
David S. Millerf47c11e2005-06-24 20:18:35 -070013387 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013388
13389 if (!err2)
13390 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013391 }
13392
13393 return err;
13394}
13395
13396static int tg3_resume(struct pci_dev *pdev)
13397{
13398 struct net_device *dev = pci_get_drvdata(pdev);
13399 struct tg3 *tp = netdev_priv(dev);
13400 int err;
13401
Michael Chan3e0c95f2007-08-03 20:56:54 -070013402 pci_restore_state(tp->pdev);
13403
Linus Torvalds1da177e2005-04-16 15:20:36 -070013404 if (!netif_running(dev))
13405 return 0;
13406
Michael Chanbc1c7562006-03-20 17:48:03 -080013407 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013408 if (err)
13409 return err;
13410
13411 netif_device_attach(dev);
13412
David S. Millerf47c11e2005-06-24 20:18:35 -070013413 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013414
Michael Chan6a9eba12005-12-13 21:08:58 -080013415 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070013416 err = tg3_restart_hw(tp, 1);
13417 if (err)
13418 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013419
13420 tp->timer.expires = jiffies + tp->timer_offset;
13421 add_timer(&tp->timer);
13422
Linus Torvalds1da177e2005-04-16 15:20:36 -070013423 tg3_netif_start(tp);
13424
Michael Chanb9ec6c12006-07-25 16:37:27 -070013425out:
David S. Millerf47c11e2005-06-24 20:18:35 -070013426 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013427
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013428 if (!err)
13429 tg3_phy_start(tp);
13430
Michael Chanb9ec6c12006-07-25 16:37:27 -070013431 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013432}
13433
13434static struct pci_driver tg3_driver = {
13435 .name = DRV_MODULE_NAME,
13436 .id_table = tg3_pci_tbl,
13437 .probe = tg3_init_one,
13438 .remove = __devexit_p(tg3_remove_one),
13439 .suspend = tg3_suspend,
13440 .resume = tg3_resume
13441};
13442
13443static int __init tg3_init(void)
13444{
Jeff Garzik29917622006-08-19 17:48:59 -040013445 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013446}
13447
13448static void __exit tg3_cleanup(void)
13449{
13450 pci_unregister_driver(&tg3_driver);
13451}
13452
13453module_init(tg3_init);
13454module_exit(tg3_cleanup);