blob: 369ddba95821e82e2dbd4527392956e15fb7e3ee [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
3 *
4 * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5 * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6 * Copyright (C) 2004 Sun Microsystems Inc.
Matt Carlson0d2a5062009-02-25 14:40:42 +00007 * Copyright (C) 2005-2009 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
21#include <linux/kernel.h>
22#include <linux/types.h>
23#include <linux/compiler.h>
24#include <linux/slab.h>
25#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020026#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
28#include <linux/ioport.h>
29#include <linux/pci.h>
30#include <linux/netdevice.h>
31#include <linux/etherdevice.h>
32#include <linux/skbuff.h>
33#include <linux/ethtool.h>
34#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070035#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070036#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/if_vlan.h>
38#include <linux/ip.h>
39#include <linux/tcp.h>
40#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070041#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020042#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080043#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030046#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <asm/system.h>
49#include <asm/io.h>
50#include <asm/byteorder.h>
51#include <asm/uaccess.h>
52
David S. Miller49b6e95f2007-03-29 01:38:42 -070053#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070055#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#endif
57
Matt Carlson63532392008-11-03 16:49:57 -080058#define BAR_0 0
59#define BAR_2 2
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
62#define TG3_VLAN_TAG_USED 1
63#else
64#define TG3_VLAN_TAG_USED 0
65#endif
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include "tg3.h"
68
69#define DRV_MODULE_NAME "tg3"
70#define PFX DRV_MODULE_NAME ": "
Matt Carlsondaf09de2009-09-01 13:22:42 +000071#define DRV_MODULE_VERSION "3.102"
72#define DRV_MODULE_RELDATE "September 1, 2009"
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74#define TG3_DEF_MAC_MODE 0
75#define TG3_DEF_RX_MODE 0
76#define TG3_DEF_TX_MODE 0
77#define TG3_DEF_MSG_ENABLE \
78 (NETIF_MSG_DRV | \
79 NETIF_MSG_PROBE | \
80 NETIF_MSG_LINK | \
81 NETIF_MSG_TIMER | \
82 NETIF_MSG_IFDOWN | \
83 NETIF_MSG_IFUP | \
84 NETIF_MSG_RX_ERR | \
85 NETIF_MSG_TX_ERR)
86
87/* length of time before we decide the hardware is borked,
88 * and dev->tx_timeout() should be called to fix the problem
89 */
90#define TG3_TX_TIMEOUT (5 * HZ)
91
92/* hardware minimum and maximum for a single frame's data payload */
93#define TG3_MIN_MTU 60
94#define TG3_MAX_MTU(tp) \
Matt Carlson8f666b02009-08-28 13:58:24 +000095 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/* These numbers seem to be hard coded in the NIC firmware somehow.
98 * You can't change the ring sizes, but you can change where you place
99 * them in the NIC onboard memory.
100 */
101#define TG3_RX_RING_SIZE 512
102#define TG3_DEF_RX_RING_PENDING 200
103#define TG3_RX_JUMBO_RING_SIZE 256
104#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonbaf8a942009-09-01 13:13:00 +0000105#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/* Do not place this n-ring entries value into the tp struct itself,
108 * we really want to expose these constants to GCC so that modulo et
109 * al. operations are done with shifts and masks instead of with
110 * hw multiply/modulo instructions. Another solution would be to
111 * replace things like '% foo' with '& (foo - 1)'.
112 */
113#define TG3_RX_RCB_RING_SIZE(tp) \
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000114 (((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) && \
Matt Carlson5ea1c502009-09-11 16:50:16 -0700115 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) ? 1024 : 512)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117#define TG3_TX_RING_SIZE 512
118#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
119
120#define TG3_RX_RING_BYTES (sizeof(struct tg3_rx_buffer_desc) * \
121 TG3_RX_RING_SIZE)
Matt Carlson79ed5ac2009-08-28 14:00:55 +0000122#define TG3_RX_JUMBO_RING_BYTES (sizeof(struct tg3_ext_rx_buffer_desc) * \
123 TG3_RX_JUMBO_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define TG3_RX_RCB_RING_BYTES(tp) (sizeof(struct tg3_rx_buffer_desc) * \
Matt Carlson79ed5ac2009-08-28 14:00:55 +0000125 TG3_RX_RCB_RING_SIZE(tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
127 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
129
Matt Carlson287be122009-08-28 13:58:46 +0000130#define TG3_DMA_BYTE_ENAB 64
131
132#define TG3_RX_STD_DMA_SZ 1536
133#define TG3_RX_JMB_DMA_SZ 9046
134
135#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
136
137#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
138#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000141#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Matt Carlsonad829262008-11-21 17:16:16 -0800143#define TG3_RAW_IP_ALIGN 2
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/* number of ETHTOOL_GSTATS u64's */
146#define TG3_NUM_STATS (sizeof(struct tg3_ethtool_stats)/sizeof(u64))
147
Michael Chan4cafd3f2005-05-29 14:56:34 -0700148#define TG3_NUM_TEST 6
149
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800150#define FIRMWARE_TG3 "tigon/tg3.bin"
151#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
152#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static char version[] __devinitdata =
155 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
156
157MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
158MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
159MODULE_LICENSE("GPL");
160MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800161MODULE_FIRMWARE(FIRMWARE_TG3);
162MODULE_FIRMWARE(FIRMWARE_TG3TSO);
163MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
164
Matt Carlson679563f2009-09-01 12:55:46 +0000165#define TG3_RSS_MIN_NUM_MSIX_VECS 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
168module_param(tg3_debug, int, 0);
169MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
170
171static struct pci_device_id tg3_pci_tbl[] = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700172 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
173 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
174 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
175 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
176 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
177 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
178 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
179 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
180 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
181 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
182 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
183 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
184 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
185 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
186 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
187 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
188 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
189 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
190 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
191 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
192 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
193 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
194 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5720)},
195 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700196 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700197 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
198 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
199 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750M)},
200 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
201 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
202 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
203 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
204 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
205 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
206 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
207 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
208 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
209 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
210 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700211 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700212 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
213 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
214 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700238 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
239 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
240 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
241 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
242 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
243 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
244 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
245 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246};
247
248MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
249
Andreas Mohr50da8592006-08-14 23:54:30 -0700250static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 const char string[ETH_GSTRING_LEN];
252} ethtool_stats_keys[TG3_NUM_STATS] = {
253 { "rx_octets" },
254 { "rx_fragments" },
255 { "rx_ucast_packets" },
256 { "rx_mcast_packets" },
257 { "rx_bcast_packets" },
258 { "rx_fcs_errors" },
259 { "rx_align_errors" },
260 { "rx_xon_pause_rcvd" },
261 { "rx_xoff_pause_rcvd" },
262 { "rx_mac_ctrl_rcvd" },
263 { "rx_xoff_entered" },
264 { "rx_frame_too_long_errors" },
265 { "rx_jabbers" },
266 { "rx_undersize_packets" },
267 { "rx_in_length_errors" },
268 { "rx_out_length_errors" },
269 { "rx_64_or_less_octet_packets" },
270 { "rx_65_to_127_octet_packets" },
271 { "rx_128_to_255_octet_packets" },
272 { "rx_256_to_511_octet_packets" },
273 { "rx_512_to_1023_octet_packets" },
274 { "rx_1024_to_1522_octet_packets" },
275 { "rx_1523_to_2047_octet_packets" },
276 { "rx_2048_to_4095_octet_packets" },
277 { "rx_4096_to_8191_octet_packets" },
278 { "rx_8192_to_9022_octet_packets" },
279
280 { "tx_octets" },
281 { "tx_collisions" },
282
283 { "tx_xon_sent" },
284 { "tx_xoff_sent" },
285 { "tx_flow_control" },
286 { "tx_mac_errors" },
287 { "tx_single_collisions" },
288 { "tx_mult_collisions" },
289 { "tx_deferred" },
290 { "tx_excessive_collisions" },
291 { "tx_late_collisions" },
292 { "tx_collide_2times" },
293 { "tx_collide_3times" },
294 { "tx_collide_4times" },
295 { "tx_collide_5times" },
296 { "tx_collide_6times" },
297 { "tx_collide_7times" },
298 { "tx_collide_8times" },
299 { "tx_collide_9times" },
300 { "tx_collide_10times" },
301 { "tx_collide_11times" },
302 { "tx_collide_12times" },
303 { "tx_collide_13times" },
304 { "tx_collide_14times" },
305 { "tx_collide_15times" },
306 { "tx_ucast_packets" },
307 { "tx_mcast_packets" },
308 { "tx_bcast_packets" },
309 { "tx_carrier_sense_errors" },
310 { "tx_discards" },
311 { "tx_errors" },
312
313 { "dma_writeq_full" },
314 { "dma_write_prioq_full" },
315 { "rxbds_empty" },
316 { "rx_discards" },
317 { "rx_errors" },
318 { "rx_threshold_hit" },
319
320 { "dma_readq_full" },
321 { "dma_read_prioq_full" },
322 { "tx_comp_queue_full" },
323
324 { "ring_set_send_prod_index" },
325 { "ring_status_update" },
326 { "nic_irqs" },
327 { "nic_avoided_irqs" },
328 { "nic_tx_threshold_hit" }
329};
330
Andreas Mohr50da8592006-08-14 23:54:30 -0700331static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700332 const char string[ETH_GSTRING_LEN];
333} ethtool_test_keys[TG3_NUM_TEST] = {
334 { "nvram test (online) " },
335 { "link test (online) " },
336 { "register test (offline)" },
337 { "memory test (offline)" },
338 { "loopback test (offline)" },
339 { "interrupt test (offline)" },
340};
341
Michael Chanb401e9e2005-12-19 16:27:04 -0800342static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
343{
344 writel(val, tp->regs + off);
345}
346
347static u32 tg3_read32(struct tg3 *tp, u32 off)
348{
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400349 return (readl(tp->regs + off));
Michael Chanb401e9e2005-12-19 16:27:04 -0800350}
351
Matt Carlson0d3031d2007-10-10 18:02:43 -0700352static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
353{
354 writel(val, tp->aperegs + off);
355}
356
357static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
358{
359 return (readl(tp->aperegs + off));
360}
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
363{
Michael Chan68929142005-08-09 20:17:14 -0700364 unsigned long flags;
365
366 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700367 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
368 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700369 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700370}
371
372static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
373{
374 writel(val, tp->regs + off);
375 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Michael Chan68929142005-08-09 20:17:14 -0700378static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
379{
380 unsigned long flags;
381 u32 val;
382
383 spin_lock_irqsave(&tp->indirect_lock, flags);
384 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
385 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
386 spin_unlock_irqrestore(&tp->indirect_lock, flags);
387 return val;
388}
389
390static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
391{
392 unsigned long flags;
393
394 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
395 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
396 TG3_64BIT_REG_LOW, val);
397 return;
398 }
399 if (off == (MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW)) {
400 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
401 TG3_64BIT_REG_LOW, val);
402 return;
403 }
404
405 spin_lock_irqsave(&tp->indirect_lock, flags);
406 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
407 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
408 spin_unlock_irqrestore(&tp->indirect_lock, flags);
409
410 /* In indirect mode when disabling interrupts, we also need
411 * to clear the interrupt bit in the GRC local ctrl register.
412 */
413 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
414 (val == 0x1)) {
415 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
416 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
417 }
418}
419
420static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
421{
422 unsigned long flags;
423 u32 val;
424
425 spin_lock_irqsave(&tp->indirect_lock, flags);
426 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
427 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
428 spin_unlock_irqrestore(&tp->indirect_lock, flags);
429 return val;
430}
431
Michael Chanb401e9e2005-12-19 16:27:04 -0800432/* usec_wait specifies the wait time in usec when writing to certain registers
433 * where it is unsafe to read back the register without some delay.
434 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
435 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
436 */
437static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Michael Chanb401e9e2005-12-19 16:27:04 -0800439 if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
440 (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
441 /* Non-posted methods */
442 tp->write32(tp, off, val);
443 else {
444 /* Posted method */
445 tg3_write32(tp, off, val);
446 if (usec_wait)
447 udelay(usec_wait);
448 tp->read32(tp, off);
449 }
450 /* Wait again after the read for the posted method to guarantee that
451 * the wait time is met.
452 */
453 if (usec_wait)
454 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Michael Chan09ee9292005-08-09 20:17:00 -0700457static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
458{
459 tp->write32_mbox(tp, off, val);
Michael Chan68929142005-08-09 20:17:14 -0700460 if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
461 !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
462 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700463}
464
Michael Chan20094932005-08-09 20:16:32 -0700465static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 void __iomem *mbox = tp->regs + off;
468 writel(val, mbox);
469 if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
470 writel(val, mbox);
471 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
472 readl(mbox);
473}
474
Michael Chanb5d37722006-09-27 16:06:21 -0700475static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
476{
477 return (readl(tp->regs + off + GRCMBOX_BASE));
478}
479
480static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
481{
482 writel(val, tp->regs + off + GRCMBOX_BASE);
483}
484
Michael Chan20094932005-08-09 20:16:32 -0700485#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700486#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Michael Chan20094932005-08-09 20:16:32 -0700487#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
488#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700489#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700490
491#define tw32(reg,val) tp->write32(tp, reg, val)
Michael Chanb401e9e2005-12-19 16:27:04 -0800492#define tw32_f(reg,val) _tw32_flush(tp,(reg),(val), 0)
493#define tw32_wait_f(reg,val,us) _tw32_flush(tp,(reg),(val), (us))
Michael Chan20094932005-08-09 20:16:32 -0700494#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
497{
Michael Chan68929142005-08-09 20:17:14 -0700498 unsigned long flags;
499
Michael Chanb5d37722006-09-27 16:06:21 -0700500 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
501 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
502 return;
503
Michael Chan68929142005-08-09 20:17:14 -0700504 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700505 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
506 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
507 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Michael Chanbbadf502006-04-06 21:46:34 -0700509 /* Always leave this as zero. */
510 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
511 } else {
512 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
513 tw32_f(TG3PCI_MEM_WIN_DATA, val);
514
515 /* Always leave this as zero. */
516 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
517 }
Michael Chan68929142005-08-09 20:17:14 -0700518 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
521static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
522{
Michael Chan68929142005-08-09 20:17:14 -0700523 unsigned long flags;
524
Michael Chanb5d37722006-09-27 16:06:21 -0700525 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
526 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
527 *val = 0;
528 return;
529 }
530
Michael Chan68929142005-08-09 20:17:14 -0700531 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700532 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
533 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
534 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Michael Chanbbadf502006-04-06 21:46:34 -0700536 /* Always leave this as zero. */
537 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
538 } else {
539 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
540 *val = tr32(TG3PCI_MEM_WIN_DATA);
541
542 /* Always leave this as zero. */
543 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
544 }
Michael Chan68929142005-08-09 20:17:14 -0700545 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Matt Carlson0d3031d2007-10-10 18:02:43 -0700548static void tg3_ape_lock_init(struct tg3 *tp)
549{
550 int i;
551
552 /* Make sure the driver hasn't any stale locks. */
553 for (i = 0; i < 8; i++)
554 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + 4 * i,
555 APE_LOCK_GRANT_DRIVER);
556}
557
558static int tg3_ape_lock(struct tg3 *tp, int locknum)
559{
560 int i, off;
561 int ret = 0;
562 u32 status;
563
564 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
565 return 0;
566
567 switch (locknum) {
Matt Carlson77b483f2008-08-15 14:07:24 -0700568 case TG3_APE_LOCK_GRC:
Matt Carlson0d3031d2007-10-10 18:02:43 -0700569 case TG3_APE_LOCK_MEM:
570 break;
571 default:
572 return -EINVAL;
573 }
574
575 off = 4 * locknum;
576
577 tg3_ape_write32(tp, TG3_APE_LOCK_REQ + off, APE_LOCK_REQ_DRIVER);
578
579 /* Wait for up to 1 millisecond to acquire lock. */
580 for (i = 0; i < 100; i++) {
581 status = tg3_ape_read32(tp, TG3_APE_LOCK_GRANT + off);
582 if (status == APE_LOCK_GRANT_DRIVER)
583 break;
584 udelay(10);
585 }
586
587 if (status != APE_LOCK_GRANT_DRIVER) {
588 /* Revoke the lock request. */
589 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + off,
590 APE_LOCK_GRANT_DRIVER);
591
592 ret = -EBUSY;
593 }
594
595 return ret;
596}
597
598static void tg3_ape_unlock(struct tg3 *tp, int locknum)
599{
600 int off;
601
602 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
603 return;
604
605 switch (locknum) {
Matt Carlson77b483f2008-08-15 14:07:24 -0700606 case TG3_APE_LOCK_GRC:
Matt Carlson0d3031d2007-10-10 18:02:43 -0700607 case TG3_APE_LOCK_MEM:
608 break;
609 default:
610 return;
611 }
612
613 off = 4 * locknum;
614 tg3_ape_write32(tp, TG3_APE_LOCK_GRANT + off, APE_LOCK_GRANT_DRIVER);
615}
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617static void tg3_disable_ints(struct tg3 *tp)
618{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000619 int i;
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 tw32(TG3PCI_MISC_HOST_CTRL,
622 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000623 for (i = 0; i < tp->irq_max; i++)
624 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627static void tg3_enable_ints(struct tg3 *tp)
628{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000629 int i;
630 u32 coal_now = 0;
631
Michael Chanbbe832c2005-06-24 20:20:04 -0700632 tp->irq_sync = 0;
633 wmb();
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 tw32(TG3PCI_MISC_HOST_CTRL,
636 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000637
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000638 for (i = 0; i < tp->irq_cnt; i++) {
639 struct tg3_napi *tnapi = &tp->napi[i];
640 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
641 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
642 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
643
644 coal_now |= tnapi->coal_now;
645 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000646
647 /* Force an initial interrupt */
648 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
649 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
650 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
651 else
652 tw32(HOSTCC_MODE, tp->coalesce_mode |
653 HOSTCC_MODE_ENABLE | coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Matt Carlson17375d22009-08-28 14:02:18 +0000656static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700657{
Matt Carlson17375d22009-08-28 14:02:18 +0000658 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000659 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700660 unsigned int work_exists = 0;
661
662 /* check for phy events */
663 if (!(tp->tg3_flags &
664 (TG3_FLAG_USE_LINKCHG_REG |
665 TG3_FLAG_POLL_SERDES))) {
666 if (sblk->status & SD_STATUS_LINK_CHG)
667 work_exists = 1;
668 }
669 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000670 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000671 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700672 work_exists = 1;
673
674 return work_exists;
675}
676
Matt Carlson17375d22009-08-28 14:02:18 +0000677/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700678 * similar to tg3_enable_ints, but it accurately determines whether there
679 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400680 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 */
Matt Carlson17375d22009-08-28 14:02:18 +0000682static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Matt Carlson17375d22009-08-28 14:02:18 +0000684 struct tg3 *tp = tnapi->tp;
685
Matt Carlson898a56f2009-08-28 14:02:40 +0000686 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 mmiowb();
688
David S. Millerfac9b832005-05-18 22:46:34 -0700689 /* When doing tagged status, this work check is unnecessary.
690 * The last_tag we write above tells the chip which piece of
691 * work we've completed.
692 */
693 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
Matt Carlson17375d22009-08-28 14:02:18 +0000694 tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700695 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000696 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Matt Carlsonfed97812009-09-01 13:10:19 +0000699static void tg3_napi_disable(struct tg3 *tp)
700{
701 int i;
702
703 for (i = tp->irq_cnt - 1; i >= 0; i--)
704 napi_disable(&tp->napi[i].napi);
705}
706
707static void tg3_napi_enable(struct tg3 *tp)
708{
709 int i;
710
711 for (i = 0; i < tp->irq_cnt; i++)
712 napi_enable(&tp->napi[i].napi);
713}
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715static inline void tg3_netif_stop(struct tg3 *tp)
716{
Michael Chanbbe832c2005-06-24 20:20:04 -0700717 tp->dev->trans_start = jiffies; /* prevent tx timeout */
Matt Carlsonfed97812009-09-01 13:10:19 +0000718 tg3_napi_disable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 netif_tx_disable(tp->dev);
720}
721
722static inline void tg3_netif_start(struct tg3 *tp)
723{
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000724 /* NOTE: unconditional netif_tx_wake_all_queues is only
725 * appropriate so long as all callers are assured to
726 * have free tx slots (such as after tg3_init_hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 */
Matt Carlsonfe5f5782009-09-01 13:09:39 +0000728 netif_tx_wake_all_queues(tp->dev);
729
Matt Carlsonfed97812009-09-01 13:10:19 +0000730 tg3_napi_enable(tp);
731 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
David S. Millerf47c11e2005-06-24 20:18:35 -0700732 tg3_enable_ints(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
735static void tg3_switch_clocks(struct tg3 *tp)
736{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000737 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 u32 orig_clock_ctrl;
739
Matt Carlson795d01c2007-10-07 23:28:17 -0700740 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
741 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700742 return;
743
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000744 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 orig_clock_ctrl = clock_ctrl;
747 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
748 CLOCK_CTRL_CLKRUN_OENABLE |
749 0x1f);
750 tp->pci_clock_ctrl = clock_ctrl;
751
752 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
753 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800754 tw32_wait_f(TG3PCI_CLOCK_CTRL,
755 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800758 tw32_wait_f(TG3PCI_CLOCK_CTRL,
759 clock_ctrl |
760 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
761 40);
762 tw32_wait_f(TG3PCI_CLOCK_CTRL,
763 clock_ctrl | (CLOCK_CTRL_ALTCLK),
764 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800766 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
769#define PHY_BUSY_LOOPS 5000
770
771static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
772{
773 u32 frame_val;
774 unsigned int loops;
775 int ret;
776
777 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
778 tw32_f(MAC_MI_MODE,
779 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
780 udelay(80);
781 }
782
783 *val = 0x0;
784
Matt Carlson882e9792009-09-01 13:21:36 +0000785 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 MI_COM_PHY_ADDR_MASK);
787 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
788 MI_COM_REG_ADDR_MASK);
789 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 tw32_f(MAC_MI_COM, frame_val);
792
793 loops = PHY_BUSY_LOOPS;
794 while (loops != 0) {
795 udelay(10);
796 frame_val = tr32(MAC_MI_COM);
797
798 if ((frame_val & MI_COM_BUSY) == 0) {
799 udelay(5);
800 frame_val = tr32(MAC_MI_COM);
801 break;
802 }
803 loops -= 1;
804 }
805
806 ret = -EBUSY;
807 if (loops != 0) {
808 *val = frame_val & MI_COM_DATA_MASK;
809 ret = 0;
810 }
811
812 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
813 tw32_f(MAC_MI_MODE, tp->mi_mode);
814 udelay(80);
815 }
816
817 return ret;
818}
819
820static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
821{
822 u32 frame_val;
823 unsigned int loops;
824 int ret;
825
Matt Carlson7f97a4b2009-08-25 10:10:03 +0000826 if ((tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700827 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
828 return 0;
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
831 tw32_f(MAC_MI_MODE,
832 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
833 udelay(80);
834 }
835
Matt Carlson882e9792009-09-01 13:21:36 +0000836 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 MI_COM_PHY_ADDR_MASK);
838 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
839 MI_COM_REG_ADDR_MASK);
840 frame_val |= (val & MI_COM_DATA_MASK);
841 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 tw32_f(MAC_MI_COM, frame_val);
844
845 loops = PHY_BUSY_LOOPS;
846 while (loops != 0) {
847 udelay(10);
848 frame_val = tr32(MAC_MI_COM);
849 if ((frame_val & MI_COM_BUSY) == 0) {
850 udelay(5);
851 frame_val = tr32(MAC_MI_COM);
852 break;
853 }
854 loops -= 1;
855 }
856
857 ret = -EBUSY;
858 if (loops != 0)
859 ret = 0;
860
861 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
862 tw32_f(MAC_MI_MODE, tp->mi_mode);
863 udelay(80);
864 }
865
866 return ret;
867}
868
Matt Carlson95e28692008-05-25 23:44:14 -0700869static int tg3_bmcr_reset(struct tg3 *tp)
870{
871 u32 phy_control;
872 int limit, err;
873
874 /* OK, reset it, and poll the BMCR_RESET bit until it
875 * clears or we time out.
876 */
877 phy_control = BMCR_RESET;
878 err = tg3_writephy(tp, MII_BMCR, phy_control);
879 if (err != 0)
880 return -EBUSY;
881
882 limit = 5000;
883 while (limit--) {
884 err = tg3_readphy(tp, MII_BMCR, &phy_control);
885 if (err != 0)
886 return -EBUSY;
887
888 if ((phy_control & BMCR_RESET) == 0) {
889 udelay(40);
890 break;
891 }
892 udelay(10);
893 }
Roel Kluind4675b52009-02-12 16:33:27 -0800894 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -0700895 return -EBUSY;
896
897 return 0;
898}
899
Matt Carlson158d7ab2008-05-29 01:37:54 -0700900static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
901{
Francois Romieu3d165432009-01-19 16:56:50 -0800902 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700903 u32 val;
904
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000905 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700906
907 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000908 val = -EIO;
909
910 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700911
912 return val;
913}
914
915static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
916{
Francois Romieu3d165432009-01-19 16:56:50 -0800917 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000918 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700919
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000920 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700921
922 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000923 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700924
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000925 spin_unlock_bh(&tp->lock);
926
927 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700928}
929
930static int tg3_mdio_reset(struct mii_bus *bp)
931{
932 return 0;
933}
934
Matt Carlson9c61d6b2008-11-03 16:54:56 -0800935static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -0700936{
937 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800938 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -0700939
Matt Carlson3f0e3ad2009-11-02 14:24:36 +0000940 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800941 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
942 case TG3_PHY_ID_BCM50610:
Matt Carlsonc73430d2009-11-02 14:29:34 +0000943 case TG3_PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800944 val = MAC_PHYCFG2_50610_LED_MODES;
945 break;
946 case TG3_PHY_ID_BCMAC131:
947 val = MAC_PHYCFG2_AC131_LED_MODES;
948 break;
949 case TG3_PHY_ID_RTL8211C:
950 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
951 break;
952 case TG3_PHY_ID_RTL8201E:
953 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
954 break;
955 default:
Matt Carlsona9daf362008-05-25 23:49:44 -0700956 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800957 }
958
959 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
960 tw32(MAC_PHYCFG2, val);
961
962 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000963 val &= ~(MAC_PHYCFG1_RGMII_INT |
964 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
965 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800966 tw32(MAC_PHYCFG1, val);
967
968 return;
969 }
970
971 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE))
972 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
973 MAC_PHYCFG2_FMODE_MASK_MASK |
974 MAC_PHYCFG2_GMODE_MASK_MASK |
975 MAC_PHYCFG2_ACT_MASK_MASK |
976 MAC_PHYCFG2_QUAL_MASK_MASK |
977 MAC_PHYCFG2_INBAND_ENABLE;
978
979 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -0700980
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000981 val = tr32(MAC_PHYCFG1);
982 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
983 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
984 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -0700985 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
986 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
987 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
988 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
989 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000990 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
991 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
992 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -0700993
Matt Carlsona9daf362008-05-25 23:49:44 -0700994 val = tr32(MAC_EXT_RGMII_MODE);
995 val &= ~(MAC_RGMII_MODE_RX_INT_B |
996 MAC_RGMII_MODE_RX_QUALITY |
997 MAC_RGMII_MODE_RX_ACTIVITY |
998 MAC_RGMII_MODE_RX_ENG_DET |
999 MAC_RGMII_MODE_TX_ENABLE |
1000 MAC_RGMII_MODE_TX_LOWPWR |
1001 MAC_RGMII_MODE_TX_RESET);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001002 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001003 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1004 val |= MAC_RGMII_MODE_RX_INT_B |
1005 MAC_RGMII_MODE_RX_QUALITY |
1006 MAC_RGMII_MODE_RX_ACTIVITY |
1007 MAC_RGMII_MODE_RX_ENG_DET;
1008 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1009 val |= MAC_RGMII_MODE_TX_ENABLE |
1010 MAC_RGMII_MODE_TX_LOWPWR |
1011 MAC_RGMII_MODE_TX_RESET;
1012 }
1013 tw32(MAC_EXT_RGMII_MODE, val);
1014}
1015
Matt Carlson158d7ab2008-05-29 01:37:54 -07001016static void tg3_mdio_start(struct tg3 *tp)
1017{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001018 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1019 tw32_f(MAC_MI_MODE, tp->mi_mode);
1020 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001021
Matt Carlson882e9792009-09-01 13:21:36 +00001022 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
1023 u32 funcnum, is_serdes;
1024
1025 funcnum = tr32(TG3_CPMU_STATUS) & TG3_CPMU_STATUS_PCIE_FUNC;
1026 if (funcnum)
1027 tp->phy_addr = 2;
1028 else
1029 tp->phy_addr = 1;
1030
1031 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1032 if (is_serdes)
1033 tp->phy_addr += 7;
1034 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001035 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001036
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001037 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1038 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1039 tg3_mdio_config_5785(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001040}
1041
Matt Carlson158d7ab2008-05-29 01:37:54 -07001042static int tg3_mdio_init(struct tg3 *tp)
1043{
1044 int i;
1045 u32 reg;
Matt Carlsona9daf362008-05-25 23:49:44 -07001046 struct phy_device *phydev;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001047
1048 tg3_mdio_start(tp);
1049
1050 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1051 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1052 return 0;
1053
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001054 tp->mdio_bus = mdiobus_alloc();
1055 if (tp->mdio_bus == NULL)
1056 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001057
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001058 tp->mdio_bus->name = "tg3 mdio bus";
1059 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001060 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001061 tp->mdio_bus->priv = tp;
1062 tp->mdio_bus->parent = &tp->pdev->dev;
1063 tp->mdio_bus->read = &tg3_mdio_read;
1064 tp->mdio_bus->write = &tg3_mdio_write;
1065 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001066 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001067 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001068
1069 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001070 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001071
1072 /* The bus registration will look for all the PHYs on the mdio bus.
1073 * Unfortunately, it does not ensure the PHY is powered up before
1074 * accessing the PHY ID registers. A chip reset is the
1075 * quickest way to bring the device back to an operational state..
1076 */
1077 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1078 tg3_bmcr_reset(tp);
1079
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001080 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001081 if (i) {
Matt Carlson158d7ab2008-05-29 01:37:54 -07001082 printk(KERN_WARNING "%s: mdiobus_reg failed (0x%x)\n",
1083 tp->dev->name, i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001084 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001085 return i;
1086 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001087
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001088 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001089
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001090 if (!phydev || !phydev->drv) {
1091 printk(KERN_WARNING "%s: No PHY devices\n", tp->dev->name);
1092 mdiobus_unregister(tp->mdio_bus);
1093 mdiobus_free(tp->mdio_bus);
1094 return -ENODEV;
1095 }
1096
1097 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001098 case TG3_PHY_ID_BCM57780:
1099 phydev->interface = PHY_INTERFACE_MODE_GMII;
1100 break;
Matt Carlsona9daf362008-05-25 23:49:44 -07001101 case TG3_PHY_ID_BCM50610:
Matt Carlsonc73430d2009-11-02 14:29:34 +00001102 case TG3_PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001103 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
1104 PHY_BRCM_RX_REFCLK_UNUSED;
Matt Carlsona9daf362008-05-25 23:49:44 -07001105 if (tp->tg3_flags3 & TG3_FLG3_RGMII_STD_IBND_DISABLE)
1106 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1107 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1108 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1109 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1110 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001111 /* fallthru */
1112 case TG3_PHY_ID_RTL8211C:
1113 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001114 break;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001115 case TG3_PHY_ID_RTL8201E:
Matt Carlsona9daf362008-05-25 23:49:44 -07001116 case TG3_PHY_ID_BCMAC131:
1117 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001118 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001119 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001120 break;
1121 }
1122
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001123 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1124
1125 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1126 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001127
1128 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001129}
1130
1131static void tg3_mdio_fini(struct tg3 *tp)
1132{
1133 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1134 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001135 mdiobus_unregister(tp->mdio_bus);
1136 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001137 }
1138}
1139
Matt Carlson95e28692008-05-25 23:44:14 -07001140/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001141static inline void tg3_generate_fw_event(struct tg3 *tp)
1142{
1143 u32 val;
1144
1145 val = tr32(GRC_RX_CPU_EVENT);
1146 val |= GRC_RX_CPU_DRIVER_EVENT;
1147 tw32_f(GRC_RX_CPU_EVENT, val);
1148
1149 tp->last_event_jiffies = jiffies;
1150}
1151
1152#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1153
1154/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001155static void tg3_wait_for_event_ack(struct tg3 *tp)
1156{
1157 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001158 unsigned int delay_cnt;
1159 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001160
Matt Carlson4ba526c2008-08-15 14:10:04 -07001161 /* If enough time has passed, no wait is necessary. */
1162 time_remain = (long)(tp->last_event_jiffies + 1 +
1163 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1164 (long)jiffies;
1165 if (time_remain < 0)
1166 return;
1167
1168 /* Check if we can shorten the wait time. */
1169 delay_cnt = jiffies_to_usecs(time_remain);
1170 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1171 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1172 delay_cnt = (delay_cnt >> 3) + 1;
1173
1174 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001175 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1176 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001177 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001178 }
1179}
1180
1181/* tp->lock is held. */
1182static void tg3_ump_link_report(struct tg3 *tp)
1183{
1184 u32 reg;
1185 u32 val;
1186
1187 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1188 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1189 return;
1190
1191 tg3_wait_for_event_ack(tp);
1192
1193 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1194
1195 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1196
1197 val = 0;
1198 if (!tg3_readphy(tp, MII_BMCR, &reg))
1199 val = reg << 16;
1200 if (!tg3_readphy(tp, MII_BMSR, &reg))
1201 val |= (reg & 0xffff);
1202 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1203
1204 val = 0;
1205 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1206 val = reg << 16;
1207 if (!tg3_readphy(tp, MII_LPA, &reg))
1208 val |= (reg & 0xffff);
1209 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1210
1211 val = 0;
1212 if (!(tp->tg3_flags2 & TG3_FLG2_MII_SERDES)) {
1213 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1214 val = reg << 16;
1215 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1216 val |= (reg & 0xffff);
1217 }
1218 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1219
1220 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1221 val = reg << 16;
1222 else
1223 val = 0;
1224 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1225
Matt Carlson4ba526c2008-08-15 14:10:04 -07001226 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001227}
1228
1229static void tg3_link_report(struct tg3 *tp)
1230{
1231 if (!netif_carrier_ok(tp->dev)) {
1232 if (netif_msg_link(tp))
1233 printk(KERN_INFO PFX "%s: Link is down.\n",
1234 tp->dev->name);
1235 tg3_ump_link_report(tp);
1236 } else if (netif_msg_link(tp)) {
1237 printk(KERN_INFO PFX "%s: Link is up at %d Mbps, %s duplex.\n",
1238 tp->dev->name,
1239 (tp->link_config.active_speed == SPEED_1000 ?
1240 1000 :
1241 (tp->link_config.active_speed == SPEED_100 ?
1242 100 : 10)),
1243 (tp->link_config.active_duplex == DUPLEX_FULL ?
1244 "full" : "half"));
1245
1246 printk(KERN_INFO PFX
1247 "%s: Flow control is %s for TX and %s for RX.\n",
1248 tp->dev->name,
Steve Glendinninge18ce342008-12-16 02:00:00 -08001249 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
Matt Carlson95e28692008-05-25 23:44:14 -07001250 "on" : "off",
Steve Glendinninge18ce342008-12-16 02:00:00 -08001251 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
Matt Carlson95e28692008-05-25 23:44:14 -07001252 "on" : "off");
1253 tg3_ump_link_report(tp);
1254 }
1255}
1256
1257static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1258{
1259 u16 miireg;
1260
Steve Glendinninge18ce342008-12-16 02:00:00 -08001261 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001262 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001263 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001264 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001265 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001266 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1267 else
1268 miireg = 0;
1269
1270 return miireg;
1271}
1272
1273static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1274{
1275 u16 miireg;
1276
Steve Glendinninge18ce342008-12-16 02:00:00 -08001277 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001278 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001279 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001280 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001281 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001282 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1283 else
1284 miireg = 0;
1285
1286 return miireg;
1287}
1288
Matt Carlson95e28692008-05-25 23:44:14 -07001289static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1290{
1291 u8 cap = 0;
1292
1293 if (lcladv & ADVERTISE_1000XPAUSE) {
1294 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1295 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001296 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001297 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001298 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001299 } else {
1300 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001301 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001302 }
1303 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1304 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001305 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001306 }
1307
1308 return cap;
1309}
1310
Matt Carlsonf51f3562008-05-25 23:45:08 -07001311static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001312{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001313 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001314 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001315 u32 old_rx_mode = tp->rx_mode;
1316 u32 old_tx_mode = tp->tx_mode;
1317
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001318 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001319 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001320 else
1321 autoneg = tp->link_config.autoneg;
1322
1323 if (autoneg == AUTONEG_ENABLE &&
Matt Carlson95e28692008-05-25 23:44:14 -07001324 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
1325 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001326 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001327 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001328 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001329 } else
1330 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001331
Matt Carlsonf51f3562008-05-25 23:45:08 -07001332 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001333
Steve Glendinninge18ce342008-12-16 02:00:00 -08001334 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001335 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1336 else
1337 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1338
Matt Carlsonf51f3562008-05-25 23:45:08 -07001339 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001340 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001341
Steve Glendinninge18ce342008-12-16 02:00:00 -08001342 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001343 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1344 else
1345 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1346
Matt Carlsonf51f3562008-05-25 23:45:08 -07001347 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001348 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001349}
1350
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001351static void tg3_adjust_link(struct net_device *dev)
1352{
1353 u8 oldflowctrl, linkmesg = 0;
1354 u32 mac_mode, lcl_adv, rmt_adv;
1355 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001356 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001357
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001358 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001359
1360 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1361 MAC_MODE_HALF_DUPLEX);
1362
1363 oldflowctrl = tp->link_config.active_flowctrl;
1364
1365 if (phydev->link) {
1366 lcl_adv = 0;
1367 rmt_adv = 0;
1368
1369 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1370 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001371 else if (phydev->speed == SPEED_1000 ||
1372 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001373 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001374 else
1375 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001376
1377 if (phydev->duplex == DUPLEX_HALF)
1378 mac_mode |= MAC_MODE_HALF_DUPLEX;
1379 else {
1380 lcl_adv = tg3_advert_flowctrl_1000T(
1381 tp->link_config.flowctrl);
1382
1383 if (phydev->pause)
1384 rmt_adv = LPA_PAUSE_CAP;
1385 if (phydev->asym_pause)
1386 rmt_adv |= LPA_PAUSE_ASYM;
1387 }
1388
1389 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1390 } else
1391 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1392
1393 if (mac_mode != tp->mac_mode) {
1394 tp->mac_mode = mac_mode;
1395 tw32_f(MAC_MODE, tp->mac_mode);
1396 udelay(40);
1397 }
1398
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001399 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1400 if (phydev->speed == SPEED_10)
1401 tw32(MAC_MI_STAT,
1402 MAC_MI_STAT_10MBPS_MODE |
1403 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1404 else
1405 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1406 }
1407
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001408 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1409 tw32(MAC_TX_LENGTHS,
1410 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1411 (6 << TX_LENGTHS_IPG_SHIFT) |
1412 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1413 else
1414 tw32(MAC_TX_LENGTHS,
1415 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1416 (6 << TX_LENGTHS_IPG_SHIFT) |
1417 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1418
1419 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1420 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1421 phydev->speed != tp->link_config.active_speed ||
1422 phydev->duplex != tp->link_config.active_duplex ||
1423 oldflowctrl != tp->link_config.active_flowctrl)
1424 linkmesg = 1;
1425
1426 tp->link_config.active_speed = phydev->speed;
1427 tp->link_config.active_duplex = phydev->duplex;
1428
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001429 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001430
1431 if (linkmesg)
1432 tg3_link_report(tp);
1433}
1434
1435static int tg3_phy_init(struct tg3 *tp)
1436{
1437 struct phy_device *phydev;
1438
1439 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED)
1440 return 0;
1441
1442 /* Bring the PHY back to a known state. */
1443 tg3_bmcr_reset(tp);
1444
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001445 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001446
1447 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001448 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001449 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001450 if (IS_ERR(phydev)) {
1451 printk(KERN_ERR "%s: Could not attach to PHY\n", tp->dev->name);
1452 return PTR_ERR(phydev);
1453 }
1454
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001455 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001456 switch (phydev->interface) {
1457 case PHY_INTERFACE_MODE_GMII:
1458 case PHY_INTERFACE_MODE_RGMII:
Matt Carlson321d32a2008-11-21 17:22:19 -08001459 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
1460 phydev->supported &= (PHY_GBIT_FEATURES |
1461 SUPPORTED_Pause |
1462 SUPPORTED_Asym_Pause);
1463 break;
1464 }
1465 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001466 case PHY_INTERFACE_MODE_MII:
1467 phydev->supported &= (PHY_BASIC_FEATURES |
1468 SUPPORTED_Pause |
1469 SUPPORTED_Asym_Pause);
1470 break;
1471 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001472 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001473 return -EINVAL;
1474 }
1475
1476 tp->tg3_flags3 |= TG3_FLG3_PHY_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001477
1478 phydev->advertising = phydev->supported;
1479
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001480 return 0;
1481}
1482
1483static void tg3_phy_start(struct tg3 *tp)
1484{
1485 struct phy_device *phydev;
1486
1487 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1488 return;
1489
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001490 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001491
1492 if (tp->link_config.phy_is_low_power) {
1493 tp->link_config.phy_is_low_power = 0;
1494 phydev->speed = tp->link_config.orig_speed;
1495 phydev->duplex = tp->link_config.orig_duplex;
1496 phydev->autoneg = tp->link_config.orig_autoneg;
1497 phydev->advertising = tp->link_config.orig_advertising;
1498 }
1499
1500 phy_start(phydev);
1501
1502 phy_start_aneg(phydev);
1503}
1504
1505static void tg3_phy_stop(struct tg3 *tp)
1506{
1507 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
1508 return;
1509
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001510 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001511}
1512
1513static void tg3_phy_fini(struct tg3 *tp)
1514{
1515 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001516 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001517 tp->tg3_flags3 &= ~TG3_FLG3_PHY_CONNECTED;
1518 }
1519}
1520
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001521static void tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1522{
1523 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1524 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1525}
1526
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001527static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1528{
1529 u32 phytest;
1530
1531 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1532 u32 phy;
1533
1534 tg3_writephy(tp, MII_TG3_FET_TEST,
1535 phytest | MII_TG3_FET_SHADOW_EN);
1536 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1537 if (enable)
1538 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1539 else
1540 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1541 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1542 }
1543 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1544 }
1545}
1546
Matt Carlson6833c042008-11-21 17:18:59 -08001547static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1548{
1549 u32 reg;
1550
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001551 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson6833c042008-11-21 17:18:59 -08001552 return;
1553
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001554 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
1555 tg3_phy_fet_toggle_apd(tp, enable);
1556 return;
1557 }
1558
Matt Carlson6833c042008-11-21 17:18:59 -08001559 reg = MII_TG3_MISC_SHDW_WREN |
1560 MII_TG3_MISC_SHDW_SCR5_SEL |
1561 MII_TG3_MISC_SHDW_SCR5_LPED |
1562 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1563 MII_TG3_MISC_SHDW_SCR5_SDTL |
1564 MII_TG3_MISC_SHDW_SCR5_C125OE;
1565 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1566 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1567
1568 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1569
1570
1571 reg = MII_TG3_MISC_SHDW_WREN |
1572 MII_TG3_MISC_SHDW_APD_SEL |
1573 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1574 if (enable)
1575 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1576
1577 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1578}
1579
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001580static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1581{
1582 u32 phy;
1583
1584 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
1585 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
1586 return;
1587
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001588 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001589 u32 ephy;
1590
Matt Carlson535ef6e2009-08-25 10:09:36 +00001591 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1592 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1593
1594 tg3_writephy(tp, MII_TG3_FET_TEST,
1595 ephy | MII_TG3_FET_SHADOW_EN);
1596 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001597 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001598 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001599 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001600 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1601 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001602 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001603 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001604 }
1605 } else {
1606 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1607 MII_TG3_AUXCTL_SHDWSEL_MISC;
1608 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1609 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1610 if (enable)
1611 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1612 else
1613 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1614 phy |= MII_TG3_AUXCTL_MISC_WREN;
1615 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1616 }
1617 }
1618}
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620static void tg3_phy_set_wirespeed(struct tg3 *tp)
1621{
1622 u32 val;
1623
1624 if (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED)
1625 return;
1626
1627 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1628 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1629 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1630 (val | (1 << 15) | (1 << 4)));
1631}
1632
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001633static void tg3_phy_apply_otp(struct tg3 *tp)
1634{
1635 u32 otp, phy;
1636
1637 if (!tp->phy_otp)
1638 return;
1639
1640 otp = tp->phy_otp;
1641
1642 /* Enable SM_DSP clock and tx 6dB coding. */
1643 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1644 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1645 MII_TG3_AUXCTL_ACTL_TX_6DB;
1646 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1647
1648 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1649 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1650 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1651
1652 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1653 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1654 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1655
1656 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1657 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1658 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1659
1660 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1661 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1662
1663 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1664 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1665
1666 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1667 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1668 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1669
1670 /* Turn off SM_DSP clock. */
1671 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1672 MII_TG3_AUXCTL_ACTL_TX_6DB;
1673 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1674}
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676static int tg3_wait_macro_done(struct tg3 *tp)
1677{
1678 int limit = 100;
1679
1680 while (limit--) {
1681 u32 tmp32;
1682
1683 if (!tg3_readphy(tp, 0x16, &tmp32)) {
1684 if ((tmp32 & 0x1000) == 0)
1685 break;
1686 }
1687 }
Roel Kluind4675b52009-02-12 16:33:27 -08001688 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return -EBUSY;
1690
1691 return 0;
1692}
1693
1694static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1695{
1696 static const u32 test_pat[4][6] = {
1697 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1698 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1699 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1700 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1701 };
1702 int chan;
1703
1704 for (chan = 0; chan < 4; chan++) {
1705 int i;
1706
1707 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1708 (chan * 0x2000) | 0x0200);
1709 tg3_writephy(tp, 0x16, 0x0002);
1710
1711 for (i = 0; i < 6; i++)
1712 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1713 test_pat[chan][i]);
1714
1715 tg3_writephy(tp, 0x16, 0x0202);
1716 if (tg3_wait_macro_done(tp)) {
1717 *resetp = 1;
1718 return -EBUSY;
1719 }
1720
1721 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1722 (chan * 0x2000) | 0x0200);
1723 tg3_writephy(tp, 0x16, 0x0082);
1724 if (tg3_wait_macro_done(tp)) {
1725 *resetp = 1;
1726 return -EBUSY;
1727 }
1728
1729 tg3_writephy(tp, 0x16, 0x0802);
1730 if (tg3_wait_macro_done(tp)) {
1731 *resetp = 1;
1732 return -EBUSY;
1733 }
1734
1735 for (i = 0; i < 6; i += 2) {
1736 u32 low, high;
1737
1738 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1739 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1740 tg3_wait_macro_done(tp)) {
1741 *resetp = 1;
1742 return -EBUSY;
1743 }
1744 low &= 0x7fff;
1745 high &= 0x000f;
1746 if (low != test_pat[chan][i] ||
1747 high != test_pat[chan][i+1]) {
1748 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1749 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1750 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1751
1752 return -EBUSY;
1753 }
1754 }
1755 }
1756
1757 return 0;
1758}
1759
1760static int tg3_phy_reset_chanpat(struct tg3 *tp)
1761{
1762 int chan;
1763
1764 for (chan = 0; chan < 4; chan++) {
1765 int i;
1766
1767 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1768 (chan * 0x2000) | 0x0200);
1769 tg3_writephy(tp, 0x16, 0x0002);
1770 for (i = 0; i < 6; i++)
1771 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
1772 tg3_writephy(tp, 0x16, 0x0202);
1773 if (tg3_wait_macro_done(tp))
1774 return -EBUSY;
1775 }
1776
1777 return 0;
1778}
1779
1780static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1781{
1782 u32 reg32, phy9_orig;
1783 int retries, do_phy_reset, err;
1784
1785 retries = 10;
1786 do_phy_reset = 1;
1787 do {
1788 if (do_phy_reset) {
1789 err = tg3_bmcr_reset(tp);
1790 if (err)
1791 return err;
1792 do_phy_reset = 0;
1793 }
1794
1795 /* Disable transmitter and interrupt. */
1796 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1797 continue;
1798
1799 reg32 |= 0x3000;
1800 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1801
1802 /* Set full-duplex, 1000 mbps. */
1803 tg3_writephy(tp, MII_BMCR,
1804 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1805
1806 /* Set to master mode. */
1807 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1808 continue;
1809
1810 tg3_writephy(tp, MII_TG3_CTRL,
1811 (MII_TG3_CTRL_AS_MASTER |
1812 MII_TG3_CTRL_ENABLE_AS_MASTER));
1813
1814 /* Enable SM_DSP_CLOCK and 6dB. */
1815 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1816
1817 /* Block the PHY control access. */
1818 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1819 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0800);
1820
1821 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1822 if (!err)
1823 break;
1824 } while (--retries);
1825
1826 err = tg3_phy_reset_chanpat(tp);
1827 if (err)
1828 return err;
1829
1830 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
1831 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0000);
1832
1833 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
1834 tg3_writephy(tp, 0x16, 0x0000);
1835
1836 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1837 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1838 /* Set Extended packet length bit for jumbo frames */
1839 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
1840 }
1841 else {
1842 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1843 }
1844
1845 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1846
1847 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1848 reg32 &= ~0x3000;
1849 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1850 } else if (!err)
1851 err = -EBUSY;
1852
1853 return err;
1854}
1855
1856/* This will reset the tigon3 PHY if there is no valid
1857 * link unless the FORCE argument is non-zero.
1858 */
1859static int tg3_phy_reset(struct tg3 *tp)
1860{
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001861 u32 cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 u32 phy_status;
1863 int err;
1864
Michael Chan60189dd2006-12-17 17:08:07 -08001865 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1866 u32 val;
1867
1868 val = tr32(GRC_MISC_CFG);
1869 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1870 udelay(40);
1871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 err = tg3_readphy(tp, MII_BMSR, &phy_status);
1873 err |= tg3_readphy(tp, MII_BMSR, &phy_status);
1874 if (err != 0)
1875 return -EBUSY;
1876
Michael Chanc8e1e822006-04-29 18:55:17 -07001877 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
1878 netif_carrier_off(tp->dev);
1879 tg3_link_report(tp);
1880 }
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1883 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
1884 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
1885 err = tg3_phy_reset_5703_4_5(tp);
1886 if (err)
1887 return err;
1888 goto out;
1889 }
1890
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001891 cpmuctrl = 0;
1892 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
1893 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
1894 cpmuctrl = tr32(TG3_CPMU_CTRL);
1895 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
1896 tw32(TG3_CPMU_CTRL,
1897 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
1898 }
1899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 err = tg3_bmcr_reset(tp);
1901 if (err)
1902 return err;
1903
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001904 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
1905 u32 phy;
1906
1907 phy = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
1908 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, phy);
1909
1910 tw32(TG3_CPMU_CTRL, cpmuctrl);
1911 }
1912
Matt Carlsonbcb37f62008-11-03 16:52:09 -08001913 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
1914 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08001915 u32 val;
1916
1917 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
1918 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
1919 CPMU_LSPD_1000MB_MACCLK_12_5) {
1920 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
1921 udelay(40);
1922 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
1923 }
1924 }
1925
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001926 tg3_phy_apply_otp(tp);
1927
Matt Carlson6833c042008-11-21 17:18:59 -08001928 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
1929 tg3_phy_toggle_apd(tp, true);
1930 else
1931 tg3_phy_toggle_apd(tp, false);
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933out:
1934 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADC_BUG) {
1935 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1936 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
1937 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x2aaa);
1938 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
1939 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0323);
1940 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1941 }
1942 if (tp->tg3_flags2 & TG3_FLG2_PHY_5704_A0_BUG) {
1943 tg3_writephy(tp, 0x1c, 0x8d68);
1944 tg3_writephy(tp, 0x1c, 0x8d68);
1945 }
1946 if (tp->tg3_flags2 & TG3_FLG2_PHY_BER_BUG) {
1947 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1948 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
1949 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x310b);
1950 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
1951 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x9506);
1952 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x401f);
1953 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x14e2);
1954 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1955 }
Michael Chanc424cb22006-04-29 18:56:34 -07001956 else if (tp->tg3_flags2 & TG3_FLG2_PHY_JITTER_BUG) {
1957 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1958 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Michael Chanc1d2a192007-01-08 19:57:20 -08001959 if (tp->tg3_flags2 & TG3_FLG2_PHY_ADJUST_TRIM) {
1960 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
1961 tg3_writephy(tp, MII_TG3_TEST1,
1962 MII_TG3_TEST1_TRIM_EN | 0x4);
1963 } else
1964 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07001965 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 /* Set Extended packet length bit (bit 14) on all chips that */
1968 /* support jumbo frames */
1969 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
1970 /* Cannot do read-modify-write on 5401 */
1971 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00001972 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 u32 phy_reg;
1974
1975 /* Set bit 14 with read-modify-write to preserve other bits */
1976 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
1977 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy_reg))
1978 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy_reg | 0x4000);
1979 }
1980
1981 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
1982 * jumbo frames transmission.
1983 */
Matt Carlson8f666b02009-08-28 13:58:24 +00001984 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 u32 phy_reg;
1986
1987 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &phy_reg))
1988 tg3_writephy(tp, MII_TG3_EXT_CTRL,
1989 phy_reg | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
1990 }
1991
Michael Chan715116a2006-09-27 16:09:25 -07001992 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07001993 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00001994 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07001995 }
1996
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001997 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 tg3_phy_set_wirespeed(tp);
1999 return 0;
2000}
2001
2002static void tg3_frob_aux_power(struct tg3 *tp)
2003{
2004 struct tg3 *tp_peer = tp;
2005
Michael Chan9d26e212006-12-07 00:21:14 -08002006 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 return;
2008
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00002009 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2010 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
2011 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002012 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002014 dev_peer = pci_get_drvdata(tp->pdev_peer);
Michael Chanbc1c7562006-03-20 17:48:03 -08002015 /* remove_one() may have been run on the peer. */
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002016 if (!dev_peer)
Michael Chanbc1c7562006-03-20 17:48:03 -08002017 tp_peer = tp;
2018 else
2019 tp_peer = netdev_priv(dev_peer);
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
Michael Chan6921d202005-12-13 21:15:53 -08002023 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0 ||
2024 (tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
2025 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2027 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002028 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2029 (GRC_LCLCTRL_GPIO_OE0 |
2030 GRC_LCLCTRL_GPIO_OE1 |
2031 GRC_LCLCTRL_GPIO_OE2 |
2032 GRC_LCLCTRL_GPIO_OUTPUT0 |
2033 GRC_LCLCTRL_GPIO_OUTPUT1),
2034 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002035 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2036 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002037 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2038 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2039 GRC_LCLCTRL_GPIO_OE1 |
2040 GRC_LCLCTRL_GPIO_OE2 |
2041 GRC_LCLCTRL_GPIO_OUTPUT0 |
2042 GRC_LCLCTRL_GPIO_OUTPUT1 |
2043 tp->grc_local_ctrl;
2044 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2045
2046 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2047 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2048
2049 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2050 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 } else {
2052 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002053 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 if (tp_peer != tp &&
2056 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2057 return;
2058
Michael Chandc56b7d2005-12-19 16:26:28 -08002059 /* Workaround to prevent overdrawing Amps. */
2060 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2061 ASIC_REV_5714) {
2062 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002063 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2064 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002065 }
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 /* On 5753 and variants, GPIO2 cannot be used. */
2068 no_gpio2 = tp->nic_sram_data_cfg &
2069 NIC_SRAM_DATA_CFG_NO_GPIO2;
2070
Michael Chandc56b7d2005-12-19 16:26:28 -08002071 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 GRC_LCLCTRL_GPIO_OE1 |
2073 GRC_LCLCTRL_GPIO_OE2 |
2074 GRC_LCLCTRL_GPIO_OUTPUT1 |
2075 GRC_LCLCTRL_GPIO_OUTPUT2;
2076 if (no_gpio2) {
2077 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2078 GRC_LCLCTRL_GPIO_OUTPUT2);
2079 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002080 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2081 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2084
Michael Chanb401e9e2005-12-19 16:27:04 -08002085 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2086 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 if (!no_gpio2) {
2089 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002090 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2091 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
2093 }
2094 } else {
2095 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2096 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2097 if (tp_peer != tp &&
2098 (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
2099 return;
2100
Michael Chanb401e9e2005-12-19 16:27:04 -08002101 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2102 (GRC_LCLCTRL_GPIO_OE1 |
2103 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Michael Chanb401e9e2005-12-19 16:27:04 -08002105 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2106 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Michael Chanb401e9e2005-12-19 16:27:04 -08002108 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2109 (GRC_LCLCTRL_GPIO_OE1 |
2110 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
2112 }
2113}
2114
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002115static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2116{
2117 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2118 return 1;
2119 else if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411) {
2120 if (speed != SPEED_10)
2121 return 1;
2122 } else if (speed == SPEED_10)
2123 return 1;
2124
2125 return 0;
2126}
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128static int tg3_setup_phy(struct tg3 *, int);
2129
2130#define RESET_KIND_SHUTDOWN 0
2131#define RESET_KIND_INIT 1
2132#define RESET_KIND_SUSPEND 2
2133
2134static void tg3_write_sig_post_reset(struct tg3 *, int);
2135static int tg3_halt_cpu(struct tg3 *, u32);
2136
Matt Carlson0a459aa2008-11-03 16:54:15 -08002137static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002138{
Matt Carlsonce057f02007-11-12 21:08:03 -08002139 u32 val;
2140
Michael Chan51297242007-02-13 12:17:57 -08002141 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
2142 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2143 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2144 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2145
2146 sg_dig_ctrl |=
2147 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2148 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2149 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2150 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002151 return;
Michael Chan51297242007-02-13 12:17:57 -08002152 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002153
Michael Chan60189dd2006-12-17 17:08:07 -08002154 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002155 tg3_bmcr_reset(tp);
2156 val = tr32(GRC_MISC_CFG);
2157 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2158 udelay(40);
2159 return;
Matt Carlson0e5f7842009-11-02 14:26:38 +00002160 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
2161 u32 phytest;
2162 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2163 u32 phy;
2164
2165 tg3_writephy(tp, MII_ADVERTISE, 0);
2166 tg3_writephy(tp, MII_BMCR,
2167 BMCR_ANENABLE | BMCR_ANRESTART);
2168
2169 tg3_writephy(tp, MII_TG3_FET_TEST,
2170 phytest | MII_TG3_FET_SHADOW_EN);
2171 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2172 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2173 tg3_writephy(tp,
2174 MII_TG3_FET_SHDW_AUXMODE4,
2175 phy);
2176 }
2177 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2178 }
2179 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002180 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002181 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2182 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002183
2184 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2185 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2186 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2187 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2188 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002189 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002190
Michael Chan15c3b692006-03-22 01:06:52 -08002191 /* The PHY should not be powered down on some chips because
2192 * of bugs.
2193 */
2194 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2195 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2196 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
2197 (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)))
2198 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002199
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002200 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2201 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002202 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2203 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2204 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2205 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2206 }
2207
Michael Chan15c3b692006-03-22 01:06:52 -08002208 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2209}
2210
Matt Carlson3f007892008-11-03 16:51:36 -08002211/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002212static int tg3_nvram_lock(struct tg3 *tp)
2213{
2214 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2215 int i;
2216
2217 if (tp->nvram_lock_cnt == 0) {
2218 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2219 for (i = 0; i < 8000; i++) {
2220 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2221 break;
2222 udelay(20);
2223 }
2224 if (i == 8000) {
2225 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2226 return -ENODEV;
2227 }
2228 }
2229 tp->nvram_lock_cnt++;
2230 }
2231 return 0;
2232}
2233
2234/* tp->lock is held. */
2235static void tg3_nvram_unlock(struct tg3 *tp)
2236{
2237 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2238 if (tp->nvram_lock_cnt > 0)
2239 tp->nvram_lock_cnt--;
2240 if (tp->nvram_lock_cnt == 0)
2241 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2242 }
2243}
2244
2245/* tp->lock is held. */
2246static void tg3_enable_nvram_access(struct tg3 *tp)
2247{
2248 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
2249 !(tp->tg3_flags2 & TG3_FLG2_PROTECTED_NVRAM)) {
2250 u32 nvaccess = tr32(NVRAM_ACCESS);
2251
2252 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2253 }
2254}
2255
2256/* tp->lock is held. */
2257static void tg3_disable_nvram_access(struct tg3 *tp)
2258{
2259 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
2260 !(tp->tg3_flags2 & TG3_FLG2_PROTECTED_NVRAM)) {
2261 u32 nvaccess = tr32(NVRAM_ACCESS);
2262
2263 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2264 }
2265}
2266
2267static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2268 u32 offset, u32 *val)
2269{
2270 u32 tmp;
2271 int i;
2272
2273 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2274 return -EINVAL;
2275
2276 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2277 EEPROM_ADDR_DEVID_MASK |
2278 EEPROM_ADDR_READ);
2279 tw32(GRC_EEPROM_ADDR,
2280 tmp |
2281 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2282 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2283 EEPROM_ADDR_ADDR_MASK) |
2284 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2285
2286 for (i = 0; i < 1000; i++) {
2287 tmp = tr32(GRC_EEPROM_ADDR);
2288
2289 if (tmp & EEPROM_ADDR_COMPLETE)
2290 break;
2291 msleep(1);
2292 }
2293 if (!(tmp & EEPROM_ADDR_COMPLETE))
2294 return -EBUSY;
2295
Matt Carlson62cedd12009-04-20 14:52:29 -07002296 tmp = tr32(GRC_EEPROM_DATA);
2297
2298 /*
2299 * The data will always be opposite the native endian
2300 * format. Perform a blind byteswap to compensate.
2301 */
2302 *val = swab32(tmp);
2303
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002304 return 0;
2305}
2306
2307#define NVRAM_CMD_TIMEOUT 10000
2308
2309static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2310{
2311 int i;
2312
2313 tw32(NVRAM_CMD, nvram_cmd);
2314 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2315 udelay(10);
2316 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2317 udelay(10);
2318 break;
2319 }
2320 }
2321
2322 if (i == NVRAM_CMD_TIMEOUT)
2323 return -EBUSY;
2324
2325 return 0;
2326}
2327
2328static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2329{
2330 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2331 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2332 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2333 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2334 (tp->nvram_jedecnum == JEDEC_ATMEL))
2335
2336 addr = ((addr / tp->nvram_pagesize) <<
2337 ATMEL_AT45DB0X1B_PAGE_POS) +
2338 (addr % tp->nvram_pagesize);
2339
2340 return addr;
2341}
2342
2343static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2344{
2345 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2346 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2347 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2348 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2349 (tp->nvram_jedecnum == JEDEC_ATMEL))
2350
2351 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2352 tp->nvram_pagesize) +
2353 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2354
2355 return addr;
2356}
2357
Matt Carlsone4f34112009-02-25 14:25:00 +00002358/* NOTE: Data read in from NVRAM is byteswapped according to
2359 * the byteswapping settings for all other register accesses.
2360 * tg3 devices are BE devices, so on a BE machine, the data
2361 * returned will be exactly as it is seen in NVRAM. On a LE
2362 * machine, the 32-bit value will be byteswapped.
2363 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002364static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2365{
2366 int ret;
2367
2368 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2369 return tg3_nvram_read_using_eeprom(tp, offset, val);
2370
2371 offset = tg3_nvram_phys_addr(tp, offset);
2372
2373 if (offset > NVRAM_ADDR_MSK)
2374 return -EINVAL;
2375
2376 ret = tg3_nvram_lock(tp);
2377 if (ret)
2378 return ret;
2379
2380 tg3_enable_nvram_access(tp);
2381
2382 tw32(NVRAM_ADDR, offset);
2383 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2384 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2385
2386 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002387 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002388
2389 tg3_disable_nvram_access(tp);
2390
2391 tg3_nvram_unlock(tp);
2392
2393 return ret;
2394}
2395
Matt Carlsona9dc5292009-02-25 14:25:30 +00002396/* Ensures NVRAM data is in bytestream format. */
2397static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002398{
2399 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002400 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002401 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002402 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002403 return res;
2404}
2405
2406/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002407static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2408{
2409 u32 addr_high, addr_low;
2410 int i;
2411
2412 addr_high = ((tp->dev->dev_addr[0] << 8) |
2413 tp->dev->dev_addr[1]);
2414 addr_low = ((tp->dev->dev_addr[2] << 24) |
2415 (tp->dev->dev_addr[3] << 16) |
2416 (tp->dev->dev_addr[4] << 8) |
2417 (tp->dev->dev_addr[5] << 0));
2418 for (i = 0; i < 4; i++) {
2419 if (i == 1 && skip_mac_1)
2420 continue;
2421 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2422 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2423 }
2424
2425 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2426 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2427 for (i = 0; i < 12; i++) {
2428 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2429 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2430 }
2431 }
2432
2433 addr_high = (tp->dev->dev_addr[0] +
2434 tp->dev->dev_addr[1] +
2435 tp->dev->dev_addr[2] +
2436 tp->dev->dev_addr[3] +
2437 tp->dev->dev_addr[4] +
2438 tp->dev->dev_addr[5]) &
2439 TX_BACKOFF_SEED_MASK;
2440 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2441}
2442
Michael Chanbc1c7562006-03-20 17:48:03 -08002443static int tg3_set_power_state(struct tg3 *tp, pci_power_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
2445 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002446 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448 /* Make sure register accesses (indirect or otherwise)
2449 * will function correctly.
2450 */
2451 pci_write_config_dword(tp->pdev,
2452 TG3PCI_MISC_HOST_CTRL,
2453 tp->misc_host_ctrl);
2454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 switch (state) {
Michael Chanbc1c7562006-03-20 17:48:03 -08002456 case PCI_D0:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002457 pci_enable_wake(tp->pdev, state, false);
2458 pci_set_power_state(tp->pdev, PCI_D0);
Michael Chan8c6bda12005-04-21 17:09:08 -07002459
Michael Chan9d26e212006-12-07 00:21:14 -08002460 /* Switch out of Vaux if it is a NIC */
2461 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
Michael Chanb401e9e2005-12-19 16:27:04 -08002462 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 return 0;
2465
Michael Chanbc1c7562006-03-20 17:48:03 -08002466 case PCI_D1:
Michael Chanbc1c7562006-03-20 17:48:03 -08002467 case PCI_D2:
Michael Chanbc1c7562006-03-20 17:48:03 -08002468 case PCI_D3hot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 break;
2470
2471 default:
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002472 printk(KERN_ERR PFX "%s: Invalid power state (D%d) requested\n",
2473 tp->dev->name, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002475 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002476
2477 /* Restore the CLKREQ setting. */
2478 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2479 u16 lnkctl;
2480
2481 pci_read_config_word(tp->pdev,
2482 tp->pcie_cap + PCI_EXP_LNKCTL,
2483 &lnkctl);
2484 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2485 pci_write_config_word(tp->pdev,
2486 tp->pcie_cap + PCI_EXP_LNKCTL,
2487 lnkctl);
2488 }
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2491 tw32(TG3PCI_MISC_HOST_CTRL,
2492 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2493
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002494 device_should_wake = pci_pme_capable(tp->pdev, state) &&
2495 device_may_wakeup(&tp->pdev->dev) &&
2496 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2497
Matt Carlsondd477002008-05-25 23:45:58 -07002498 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002499 do_low_power = false;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002500 if ((tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) &&
2501 !tp->link_config.phy_is_low_power) {
2502 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002503 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002504
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002505 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002506
2507 tp->link_config.phy_is_low_power = 1;
2508
2509 tp->link_config.orig_speed = phydev->speed;
2510 tp->link_config.orig_duplex = phydev->duplex;
2511 tp->link_config.orig_autoneg = phydev->autoneg;
2512 tp->link_config.orig_advertising = phydev->advertising;
2513
2514 advertising = ADVERTISED_TP |
2515 ADVERTISED_Pause |
2516 ADVERTISED_Autoneg |
2517 ADVERTISED_10baseT_Half;
2518
2519 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002520 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002521 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2522 advertising |=
2523 ADVERTISED_100baseT_Half |
2524 ADVERTISED_100baseT_Full |
2525 ADVERTISED_10baseT_Full;
2526 else
2527 advertising |= ADVERTISED_10baseT_Full;
2528 }
2529
2530 phydev->advertising = advertising;
2531
2532 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002533
2534 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
2535 if (phyid != TG3_PHY_ID_BCMAC131) {
2536 phyid &= TG3_PHY_OUI_MASK;
Roel Kluinf72b5342009-02-18 17:42:42 -08002537 if (phyid == TG3_PHY_OUI_1 ||
2538 phyid == TG3_PHY_OUI_2 ||
Matt Carlson0a459aa2008-11-03 16:54:15 -08002539 phyid == TG3_PHY_OUI_3)
2540 do_low_power = true;
2541 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002542 }
Matt Carlsondd477002008-05-25 23:45:58 -07002543 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002544 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002545
Matt Carlsondd477002008-05-25 23:45:58 -07002546 if (tp->link_config.phy_is_low_power == 0) {
2547 tp->link_config.phy_is_low_power = 1;
2548 tp->link_config.orig_speed = tp->link_config.speed;
2549 tp->link_config.orig_duplex = tp->link_config.duplex;
2550 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Matt Carlsondd477002008-05-25 23:45:58 -07002553 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
2554 tp->link_config.speed = SPEED_10;
2555 tp->link_config.duplex = DUPLEX_HALF;
2556 tp->link_config.autoneg = AUTONEG_ENABLE;
2557 tg3_setup_phy(tp, 0);
2558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 }
2560
Michael Chanb5d37722006-09-27 16:06:21 -07002561 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2562 u32 val;
2563
2564 val = tr32(GRC_VCPU_EXT_CTRL);
2565 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2566 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002567 int i;
2568 u32 val;
2569
2570 for (i = 0; i < 200; i++) {
2571 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2572 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2573 break;
2574 msleep(1);
2575 }
2576 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002577 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2578 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2579 WOL_DRV_STATE_SHUTDOWN |
2580 WOL_DRV_WOL |
2581 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002582
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002583 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 u32 mac_mode;
2585
2586 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002587 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002588 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2589 udelay(40);
2590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Michael Chan3f7045c2006-09-27 16:02:29 -07002592 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
2593 mac_mode = MAC_MODE_PORT_MODE_GMII;
2594 else
2595 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002597 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2598 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2599 ASIC_REV_5700) {
2600 u32 speed = (tp->tg3_flags &
2601 TG3_FLAG_WOL_SPEED_100MB) ?
2602 SPEED_100 : SPEED_10;
2603 if (tg3_5700_link_polarity(tp, speed))
2604 mac_mode |= MAC_MODE_LINK_POLARITY;
2605 else
2606 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 } else {
2609 mac_mode = MAC_MODE_PORT_MODE_TBI;
2610 }
2611
John W. Linvillecbf46852005-04-21 17:01:29 -07002612 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 tw32(MAC_LED_CTRL, tp->led_ctrl);
2614
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002615 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2616 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2617 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2618 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2619 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2620 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Matt Carlson3bda1252008-08-15 14:08:22 -07002622 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
2623 mac_mode |= tp->mac_mode &
2624 (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
2625 if (mac_mode & MAC_MODE_APE_TX_EN)
2626 mac_mode |= MAC_MODE_TDE_ENABLE;
2627 }
2628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 tw32_f(MAC_MODE, mac_mode);
2630 udelay(100);
2631
2632 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2633 udelay(10);
2634 }
2635
2636 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2637 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2638 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2639 u32 base_val;
2640
2641 base_val = tp->pci_clock_ctrl;
2642 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2643 CLOCK_CTRL_TXCLK_DISABLE);
2644
Michael Chanb401e9e2005-12-19 16:27:04 -08002645 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2646 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002647 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002648 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002649 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002650 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002651 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2653 u32 newbits1, newbits2;
2654
2655 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2656 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2657 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2658 CLOCK_CTRL_TXCLK_DISABLE |
2659 CLOCK_CTRL_ALTCLK);
2660 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2661 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2662 newbits1 = CLOCK_CTRL_625_CORE;
2663 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2664 } else {
2665 newbits1 = CLOCK_CTRL_ALTCLK;
2666 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2667 }
2668
Michael Chanb401e9e2005-12-19 16:27:04 -08002669 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2670 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Michael Chanb401e9e2005-12-19 16:27:04 -08002672 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2673 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
2675 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2676 u32 newbits3;
2677
2678 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2679 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2680 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2681 CLOCK_CTRL_TXCLK_DISABLE |
2682 CLOCK_CTRL_44MHZ_CORE);
2683 } else {
2684 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2685 }
2686
Michael Chanb401e9e2005-12-19 16:27:04 -08002687 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2688 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 }
2690 }
2691
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002692 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002693 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002694 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 tg3_frob_aux_power(tp);
2697
2698 /* Workaround for unstable PLL clock */
2699 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2700 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2701 u32 val = tr32(0x7d00);
2702
2703 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2704 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002705 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002706 int err;
2707
2708 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002710 if (!err)
2711 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 }
2714
Michael Chanbbadf502006-04-06 21:46:34 -07002715 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2716
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002717 if (device_should_wake)
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002718 pci_enable_wake(tp->pdev, state, true);
2719
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 /* Finally, set the new power state. */
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07002721 pci_set_power_state(tp->pdev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 return 0;
2724}
2725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2727{
2728 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2729 case MII_TG3_AUX_STAT_10HALF:
2730 *speed = SPEED_10;
2731 *duplex = DUPLEX_HALF;
2732 break;
2733
2734 case MII_TG3_AUX_STAT_10FULL:
2735 *speed = SPEED_10;
2736 *duplex = DUPLEX_FULL;
2737 break;
2738
2739 case MII_TG3_AUX_STAT_100HALF:
2740 *speed = SPEED_100;
2741 *duplex = DUPLEX_HALF;
2742 break;
2743
2744 case MII_TG3_AUX_STAT_100FULL:
2745 *speed = SPEED_100;
2746 *duplex = DUPLEX_FULL;
2747 break;
2748
2749 case MII_TG3_AUX_STAT_1000HALF:
2750 *speed = SPEED_1000;
2751 *duplex = DUPLEX_HALF;
2752 break;
2753
2754 case MII_TG3_AUX_STAT_1000FULL:
2755 *speed = SPEED_1000;
2756 *duplex = DUPLEX_FULL;
2757 break;
2758
2759 default:
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002760 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002761 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2762 SPEED_10;
2763 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2764 DUPLEX_HALF;
2765 break;
2766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 *speed = SPEED_INVALID;
2768 *duplex = DUPLEX_INVALID;
2769 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771}
2772
2773static void tg3_phy_copper_begin(struct tg3 *tp)
2774{
2775 u32 new_adv;
2776 int i;
2777
2778 if (tp->link_config.phy_is_low_power) {
2779 /* Entering low power mode. Disable gigabit and
2780 * 100baseT advertisements.
2781 */
2782 tg3_writephy(tp, MII_TG3_CTRL, 0);
2783
2784 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2785 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2786 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2787 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2788
2789 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2790 } else if (tp->link_config.speed == SPEED_INVALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
2792 tp->link_config.advertising &=
2793 ~(ADVERTISED_1000baseT_Half |
2794 ADVERTISED_1000baseT_Full);
2795
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002796 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2798 new_adv |= ADVERTISE_10HALF;
2799 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2800 new_adv |= ADVERTISE_10FULL;
2801 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2802 new_adv |= ADVERTISE_100HALF;
2803 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2804 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002805
2806 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2809
2810 if (tp->link_config.advertising &
2811 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2812 new_adv = 0;
2813 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2814 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2815 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2816 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
2817 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY) &&
2818 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2819 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2820 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2821 MII_TG3_CTRL_ENABLE_AS_MASTER);
2822 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2823 } else {
2824 tg3_writephy(tp, MII_TG3_CTRL, 0);
2825 }
2826 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002827 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2828 new_adv |= ADVERTISE_CSMA;
2829
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 /* Asking for a specific link mode. */
2831 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2833
2834 if (tp->link_config.duplex == DUPLEX_FULL)
2835 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2836 else
2837 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2838 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2839 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2840 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2841 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 if (tp->link_config.speed == SPEED_100) {
2844 if (tp->link_config.duplex == DUPLEX_FULL)
2845 new_adv |= ADVERTISE_100FULL;
2846 else
2847 new_adv |= ADVERTISE_100HALF;
2848 } else {
2849 if (tp->link_config.duplex == DUPLEX_FULL)
2850 new_adv |= ADVERTISE_10FULL;
2851 else
2852 new_adv |= ADVERTISE_10HALF;
2853 }
2854 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002855
2856 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002858
2859 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 }
2861
2862 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
2863 tp->link_config.speed != SPEED_INVALID) {
2864 u32 bmcr, orig_bmcr;
2865
2866 tp->link_config.active_speed = tp->link_config.speed;
2867 tp->link_config.active_duplex = tp->link_config.duplex;
2868
2869 bmcr = 0;
2870 switch (tp->link_config.speed) {
2871 default:
2872 case SPEED_10:
2873 break;
2874
2875 case SPEED_100:
2876 bmcr |= BMCR_SPEED100;
2877 break;
2878
2879 case SPEED_1000:
2880 bmcr |= TG3_BMCR_SPEED1000;
2881 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
2884 if (tp->link_config.duplex == DUPLEX_FULL)
2885 bmcr |= BMCR_FULLDPLX;
2886
2887 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
2888 (bmcr != orig_bmcr)) {
2889 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
2890 for (i = 0; i < 1500; i++) {
2891 u32 tmp;
2892
2893 udelay(10);
2894 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
2895 tg3_readphy(tp, MII_BMSR, &tmp))
2896 continue;
2897 if (!(tmp & BMSR_LSTATUS)) {
2898 udelay(40);
2899 break;
2900 }
2901 }
2902 tg3_writephy(tp, MII_BMCR, bmcr);
2903 udelay(40);
2904 }
2905 } else {
2906 tg3_writephy(tp, MII_BMCR,
2907 BMCR_ANENABLE | BMCR_ANRESTART);
2908 }
2909}
2910
2911static int tg3_init_5401phy_dsp(struct tg3 *tp)
2912{
2913 int err;
2914
2915 /* Turn off tap power management. */
2916 /* Set Extended packet length bit */
2917 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
2918
2919 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0012);
2920 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1804);
2921
2922 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x0013);
2923 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x1204);
2924
2925 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2926 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0132);
2927
2928 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8006);
2929 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0232);
2930
2931 err |= tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2932 err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0a20);
2933
2934 udelay(40);
2935
2936 return err;
2937}
2938
Michael Chan3600d912006-12-07 00:21:48 -08002939static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
Michael Chan3600d912006-12-07 00:21:48 -08002941 u32 adv_reg, all_mask = 0;
2942
2943 if (mask & ADVERTISED_10baseT_Half)
2944 all_mask |= ADVERTISE_10HALF;
2945 if (mask & ADVERTISED_10baseT_Full)
2946 all_mask |= ADVERTISE_10FULL;
2947 if (mask & ADVERTISED_100baseT_Half)
2948 all_mask |= ADVERTISE_100HALF;
2949 if (mask & ADVERTISED_100baseT_Full)
2950 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
2952 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
2953 return 0;
2954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 if ((adv_reg & all_mask) != all_mask)
2956 return 0;
2957 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
2958 u32 tg3_ctrl;
2959
Michael Chan3600d912006-12-07 00:21:48 -08002960 all_mask = 0;
2961 if (mask & ADVERTISED_1000baseT_Half)
2962 all_mask |= ADVERTISE_1000HALF;
2963 if (mask & ADVERTISED_1000baseT_Full)
2964 all_mask |= ADVERTISE_1000FULL;
2965
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
2967 return 0;
2968
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 if ((tg3_ctrl & all_mask) != all_mask)
2970 return 0;
2971 }
2972 return 1;
2973}
2974
Matt Carlsonef167e22007-12-20 20:10:01 -08002975static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
2976{
2977 u32 curadv, reqadv;
2978
2979 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
2980 return 1;
2981
2982 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
2983 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2984
2985 if (tp->link_config.active_duplex == DUPLEX_FULL) {
2986 if (curadv != reqadv)
2987 return 0;
2988
2989 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
2990 tg3_readphy(tp, MII_LPA, rmtadv);
2991 } else {
2992 /* Reprogram the advertisement register, even if it
2993 * does not affect the current link. If the link
2994 * gets renegotiated in the future, we can save an
2995 * additional renegotiation cycle by advertising
2996 * it correctly in the first place.
2997 */
2998 if (curadv != reqadv) {
2999 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3000 ADVERTISE_PAUSE_ASYM);
3001 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3002 }
3003 }
3004
3005 return 1;
3006}
3007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3009{
3010 int current_link_up;
3011 u32 bmsr, dummy;
Matt Carlsonef167e22007-12-20 20:10:01 -08003012 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 u16 current_speed;
3014 u8 current_duplex;
3015 int i, err;
3016
3017 tw32(MAC_EVENT, 0);
3018
3019 tw32_f(MAC_STATUS,
3020 (MAC_STATUS_SYNC_CHANGED |
3021 MAC_STATUS_CFG_CHANGED |
3022 MAC_STATUS_MI_COMPLETION |
3023 MAC_STATUS_LNKSTATE_CHANGED));
3024 udelay(40);
3025
Matt Carlson8ef21422008-05-02 16:47:53 -07003026 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3027 tw32_f(MAC_MI_MODE,
3028 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3029 udelay(80);
3030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
3032 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3033
3034 /* Some third-party PHYs need to be reset on link going
3035 * down.
3036 */
3037 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3038 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3039 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3040 netif_carrier_ok(tp->dev)) {
3041 tg3_readphy(tp, MII_BMSR, &bmsr);
3042 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3043 !(bmsr & BMSR_LSTATUS))
3044 force_reset = 1;
3045 }
3046 if (force_reset)
3047 tg3_phy_reset(tp);
3048
3049 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
3050 tg3_readphy(tp, MII_BMSR, &bmsr);
3051 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3052 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3053 bmsr = 0;
3054
3055 if (!(bmsr & BMSR_LSTATUS)) {
3056 err = tg3_init_5401phy_dsp(tp);
3057 if (err)
3058 return err;
3059
3060 tg3_readphy(tp, MII_BMSR, &bmsr);
3061 for (i = 0; i < 1000; i++) {
3062 udelay(10);
3063 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3064 (bmsr & BMSR_LSTATUS)) {
3065 udelay(40);
3066 break;
3067 }
3068 }
3069
3070 if ((tp->phy_id & PHY_ID_REV_MASK) == PHY_REV_BCM5401_B0 &&
3071 !(bmsr & BMSR_LSTATUS) &&
3072 tp->link_config.active_speed == SPEED_1000) {
3073 err = tg3_phy_reset(tp);
3074 if (!err)
3075 err = tg3_init_5401phy_dsp(tp);
3076 if (err)
3077 return err;
3078 }
3079 }
3080 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3081 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3082 /* 5701 {A0,B0} CRC bug workaround */
3083 tg3_writephy(tp, 0x15, 0x0a75);
3084 tg3_writephy(tp, 0x1c, 0x8c68);
3085 tg3_writephy(tp, 0x1c, 0x8d68);
3086 tg3_writephy(tp, 0x1c, 0x8c68);
3087 }
3088
3089 /* Clear pending interrupts... */
3090 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3091 tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
3092
3093 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT)
3094 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003095 else if (!(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3097
3098 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3099 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3100 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3101 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3102 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3103 else
3104 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3105 }
3106
3107 current_link_up = 0;
3108 current_speed = SPEED_INVALID;
3109 current_duplex = DUPLEX_INVALID;
3110
3111 if (tp->tg3_flags2 & TG3_FLG2_CAPACITIVE_COUPLING) {
3112 u32 val;
3113
3114 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3115 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3116 if (!(val & (1 << 10))) {
3117 val |= (1 << 10);
3118 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3119 goto relink;
3120 }
3121 }
3122
3123 bmsr = 0;
3124 for (i = 0; i < 100; i++) {
3125 tg3_readphy(tp, MII_BMSR, &bmsr);
3126 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3127 (bmsr & BMSR_LSTATUS))
3128 break;
3129 udelay(40);
3130 }
3131
3132 if (bmsr & BMSR_LSTATUS) {
3133 u32 aux_stat, bmcr;
3134
3135 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3136 for (i = 0; i < 2000; i++) {
3137 udelay(10);
3138 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3139 aux_stat)
3140 break;
3141 }
3142
3143 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3144 &current_speed,
3145 &current_duplex);
3146
3147 bmcr = 0;
3148 for (i = 0; i < 200; i++) {
3149 tg3_readphy(tp, MII_BMCR, &bmcr);
3150 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3151 continue;
3152 if (bmcr && bmcr != 0x7fff)
3153 break;
3154 udelay(10);
3155 }
3156
Matt Carlsonef167e22007-12-20 20:10:01 -08003157 lcl_adv = 0;
3158 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Matt Carlsonef167e22007-12-20 20:10:01 -08003160 tp->link_config.active_speed = current_speed;
3161 tp->link_config.active_duplex = current_duplex;
3162
3163 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3164 if ((bmcr & BMCR_ANENABLE) &&
3165 tg3_copper_is_advertising_all(tp,
3166 tp->link_config.advertising)) {
3167 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3168 &rmt_adv))
3169 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 }
3171 } else {
3172 if (!(bmcr & BMCR_ANENABLE) &&
3173 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003174 tp->link_config.duplex == current_duplex &&
3175 tp->link_config.flowctrl ==
3176 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 }
3179 }
3180
Matt Carlsonef167e22007-12-20 20:10:01 -08003181 if (current_link_up == 1 &&
3182 tp->link_config.active_duplex == DUPLEX_FULL)
3183 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 }
3185
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186relink:
Michael Chan6921d202005-12-13 21:15:53 -08003187 if (current_link_up == 0 || tp->link_config.phy_is_low_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 u32 tmp;
3189
3190 tg3_phy_copper_begin(tp);
3191
3192 tg3_readphy(tp, MII_BMSR, &tmp);
3193 if (!tg3_readphy(tp, MII_BMSR, &tmp) &&
3194 (tmp & BMSR_LSTATUS))
3195 current_link_up = 1;
3196 }
3197
3198 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3199 if (current_link_up == 1) {
3200 if (tp->link_config.active_speed == SPEED_100 ||
3201 tp->link_config.active_speed == SPEED_10)
3202 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3203 else
3204 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003205 } else if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)
3206 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3207 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3209
3210 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3211 if (tp->link_config.active_duplex == DUPLEX_HALF)
3212 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003215 if (current_link_up == 1 &&
3216 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003218 else
3219 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 }
3221
3222 /* ??? Without this setting Netgear GA302T PHY does not
3223 * ??? send/receive packets...
3224 */
3225 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411 &&
3226 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3227 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3228 tw32_f(MAC_MI_MODE, tp->mi_mode);
3229 udelay(80);
3230 }
3231
3232 tw32_f(MAC_MODE, tp->mac_mode);
3233 udelay(40);
3234
3235 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3236 /* Polled via timer. */
3237 tw32_f(MAC_EVENT, 0);
3238 } else {
3239 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3240 }
3241 udelay(40);
3242
3243 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3244 current_link_up == 1 &&
3245 tp->link_config.active_speed == SPEED_1000 &&
3246 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3247 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3248 udelay(120);
3249 tw32_f(MAC_STATUS,
3250 (MAC_STATUS_SYNC_CHANGED |
3251 MAC_STATUS_CFG_CHANGED));
3252 udelay(40);
3253 tg3_write_mem(tp,
3254 NIC_SRAM_FIRMWARE_MBOX,
3255 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3256 }
3257
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003258 /* Prevent send BD corruption. */
3259 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3260 u16 oldlnkctl, newlnkctl;
3261
3262 pci_read_config_word(tp->pdev,
3263 tp->pcie_cap + PCI_EXP_LNKCTL,
3264 &oldlnkctl);
3265 if (tp->link_config.active_speed == SPEED_100 ||
3266 tp->link_config.active_speed == SPEED_10)
3267 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3268 else
3269 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3270 if (newlnkctl != oldlnkctl)
3271 pci_write_config_word(tp->pdev,
3272 tp->pcie_cap + PCI_EXP_LNKCTL,
3273 newlnkctl);
3274 }
3275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 if (current_link_up != netif_carrier_ok(tp->dev)) {
3277 if (current_link_up)
3278 netif_carrier_on(tp->dev);
3279 else
3280 netif_carrier_off(tp->dev);
3281 tg3_link_report(tp);
3282 }
3283
3284 return 0;
3285}
3286
3287struct tg3_fiber_aneginfo {
3288 int state;
3289#define ANEG_STATE_UNKNOWN 0
3290#define ANEG_STATE_AN_ENABLE 1
3291#define ANEG_STATE_RESTART_INIT 2
3292#define ANEG_STATE_RESTART 3
3293#define ANEG_STATE_DISABLE_LINK_OK 4
3294#define ANEG_STATE_ABILITY_DETECT_INIT 5
3295#define ANEG_STATE_ABILITY_DETECT 6
3296#define ANEG_STATE_ACK_DETECT_INIT 7
3297#define ANEG_STATE_ACK_DETECT 8
3298#define ANEG_STATE_COMPLETE_ACK_INIT 9
3299#define ANEG_STATE_COMPLETE_ACK 10
3300#define ANEG_STATE_IDLE_DETECT_INIT 11
3301#define ANEG_STATE_IDLE_DETECT 12
3302#define ANEG_STATE_LINK_OK 13
3303#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3304#define ANEG_STATE_NEXT_PAGE_WAIT 15
3305
3306 u32 flags;
3307#define MR_AN_ENABLE 0x00000001
3308#define MR_RESTART_AN 0x00000002
3309#define MR_AN_COMPLETE 0x00000004
3310#define MR_PAGE_RX 0x00000008
3311#define MR_NP_LOADED 0x00000010
3312#define MR_TOGGLE_TX 0x00000020
3313#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3314#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3315#define MR_LP_ADV_SYM_PAUSE 0x00000100
3316#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3317#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3318#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3319#define MR_LP_ADV_NEXT_PAGE 0x00001000
3320#define MR_TOGGLE_RX 0x00002000
3321#define MR_NP_RX 0x00004000
3322
3323#define MR_LINK_OK 0x80000000
3324
3325 unsigned long link_time, cur_time;
3326
3327 u32 ability_match_cfg;
3328 int ability_match_count;
3329
3330 char ability_match, idle_match, ack_match;
3331
3332 u32 txconfig, rxconfig;
3333#define ANEG_CFG_NP 0x00000080
3334#define ANEG_CFG_ACK 0x00000040
3335#define ANEG_CFG_RF2 0x00000020
3336#define ANEG_CFG_RF1 0x00000010
3337#define ANEG_CFG_PS2 0x00000001
3338#define ANEG_CFG_PS1 0x00008000
3339#define ANEG_CFG_HD 0x00004000
3340#define ANEG_CFG_FD 0x00002000
3341#define ANEG_CFG_INVAL 0x00001f06
3342
3343};
3344#define ANEG_OK 0
3345#define ANEG_DONE 1
3346#define ANEG_TIMER_ENAB 2
3347#define ANEG_FAILED -1
3348
3349#define ANEG_STATE_SETTLE_TIME 10000
3350
3351static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3352 struct tg3_fiber_aneginfo *ap)
3353{
Matt Carlson5be73b42007-12-20 20:09:29 -08003354 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 unsigned long delta;
3356 u32 rx_cfg_reg;
3357 int ret;
3358
3359 if (ap->state == ANEG_STATE_UNKNOWN) {
3360 ap->rxconfig = 0;
3361 ap->link_time = 0;
3362 ap->cur_time = 0;
3363 ap->ability_match_cfg = 0;
3364 ap->ability_match_count = 0;
3365 ap->ability_match = 0;
3366 ap->idle_match = 0;
3367 ap->ack_match = 0;
3368 }
3369 ap->cur_time++;
3370
3371 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3372 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3373
3374 if (rx_cfg_reg != ap->ability_match_cfg) {
3375 ap->ability_match_cfg = rx_cfg_reg;
3376 ap->ability_match = 0;
3377 ap->ability_match_count = 0;
3378 } else {
3379 if (++ap->ability_match_count > 1) {
3380 ap->ability_match = 1;
3381 ap->ability_match_cfg = rx_cfg_reg;
3382 }
3383 }
3384 if (rx_cfg_reg & ANEG_CFG_ACK)
3385 ap->ack_match = 1;
3386 else
3387 ap->ack_match = 0;
3388
3389 ap->idle_match = 0;
3390 } else {
3391 ap->idle_match = 1;
3392 ap->ability_match_cfg = 0;
3393 ap->ability_match_count = 0;
3394 ap->ability_match = 0;
3395 ap->ack_match = 0;
3396
3397 rx_cfg_reg = 0;
3398 }
3399
3400 ap->rxconfig = rx_cfg_reg;
3401 ret = ANEG_OK;
3402
3403 switch(ap->state) {
3404 case ANEG_STATE_UNKNOWN:
3405 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3406 ap->state = ANEG_STATE_AN_ENABLE;
3407
3408 /* fallthru */
3409 case ANEG_STATE_AN_ENABLE:
3410 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3411 if (ap->flags & MR_AN_ENABLE) {
3412 ap->link_time = 0;
3413 ap->cur_time = 0;
3414 ap->ability_match_cfg = 0;
3415 ap->ability_match_count = 0;
3416 ap->ability_match = 0;
3417 ap->idle_match = 0;
3418 ap->ack_match = 0;
3419
3420 ap->state = ANEG_STATE_RESTART_INIT;
3421 } else {
3422 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3423 }
3424 break;
3425
3426 case ANEG_STATE_RESTART_INIT:
3427 ap->link_time = ap->cur_time;
3428 ap->flags &= ~(MR_NP_LOADED);
3429 ap->txconfig = 0;
3430 tw32(MAC_TX_AUTO_NEG, 0);
3431 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3432 tw32_f(MAC_MODE, tp->mac_mode);
3433 udelay(40);
3434
3435 ret = ANEG_TIMER_ENAB;
3436 ap->state = ANEG_STATE_RESTART;
3437
3438 /* fallthru */
3439 case ANEG_STATE_RESTART:
3440 delta = ap->cur_time - ap->link_time;
3441 if (delta > ANEG_STATE_SETTLE_TIME) {
3442 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
3443 } else {
3444 ret = ANEG_TIMER_ENAB;
3445 }
3446 break;
3447
3448 case ANEG_STATE_DISABLE_LINK_OK:
3449 ret = ANEG_DONE;
3450 break;
3451
3452 case ANEG_STATE_ABILITY_DETECT_INIT:
3453 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003454 ap->txconfig = ANEG_CFG_FD;
3455 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3456 if (flowctrl & ADVERTISE_1000XPAUSE)
3457 ap->txconfig |= ANEG_CFG_PS1;
3458 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3459 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3461 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3462 tw32_f(MAC_MODE, tp->mac_mode);
3463 udelay(40);
3464
3465 ap->state = ANEG_STATE_ABILITY_DETECT;
3466 break;
3467
3468 case ANEG_STATE_ABILITY_DETECT:
3469 if (ap->ability_match != 0 && ap->rxconfig != 0) {
3470 ap->state = ANEG_STATE_ACK_DETECT_INIT;
3471 }
3472 break;
3473
3474 case ANEG_STATE_ACK_DETECT_INIT:
3475 ap->txconfig |= ANEG_CFG_ACK;
3476 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3477 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3478 tw32_f(MAC_MODE, tp->mac_mode);
3479 udelay(40);
3480
3481 ap->state = ANEG_STATE_ACK_DETECT;
3482
3483 /* fallthru */
3484 case ANEG_STATE_ACK_DETECT:
3485 if (ap->ack_match != 0) {
3486 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3487 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3488 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3489 } else {
3490 ap->state = ANEG_STATE_AN_ENABLE;
3491 }
3492 } else if (ap->ability_match != 0 &&
3493 ap->rxconfig == 0) {
3494 ap->state = ANEG_STATE_AN_ENABLE;
3495 }
3496 break;
3497
3498 case ANEG_STATE_COMPLETE_ACK_INIT:
3499 if (ap->rxconfig & ANEG_CFG_INVAL) {
3500 ret = ANEG_FAILED;
3501 break;
3502 }
3503 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3504 MR_LP_ADV_HALF_DUPLEX |
3505 MR_LP_ADV_SYM_PAUSE |
3506 MR_LP_ADV_ASYM_PAUSE |
3507 MR_LP_ADV_REMOTE_FAULT1 |
3508 MR_LP_ADV_REMOTE_FAULT2 |
3509 MR_LP_ADV_NEXT_PAGE |
3510 MR_TOGGLE_RX |
3511 MR_NP_RX);
3512 if (ap->rxconfig & ANEG_CFG_FD)
3513 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3514 if (ap->rxconfig & ANEG_CFG_HD)
3515 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3516 if (ap->rxconfig & ANEG_CFG_PS1)
3517 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3518 if (ap->rxconfig & ANEG_CFG_PS2)
3519 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3520 if (ap->rxconfig & ANEG_CFG_RF1)
3521 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3522 if (ap->rxconfig & ANEG_CFG_RF2)
3523 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3524 if (ap->rxconfig & ANEG_CFG_NP)
3525 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3526
3527 ap->link_time = ap->cur_time;
3528
3529 ap->flags ^= (MR_TOGGLE_TX);
3530 if (ap->rxconfig & 0x0008)
3531 ap->flags |= MR_TOGGLE_RX;
3532 if (ap->rxconfig & ANEG_CFG_NP)
3533 ap->flags |= MR_NP_RX;
3534 ap->flags |= MR_PAGE_RX;
3535
3536 ap->state = ANEG_STATE_COMPLETE_ACK;
3537 ret = ANEG_TIMER_ENAB;
3538 break;
3539
3540 case ANEG_STATE_COMPLETE_ACK:
3541 if (ap->ability_match != 0 &&
3542 ap->rxconfig == 0) {
3543 ap->state = ANEG_STATE_AN_ENABLE;
3544 break;
3545 }
3546 delta = ap->cur_time - ap->link_time;
3547 if (delta > ANEG_STATE_SETTLE_TIME) {
3548 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3549 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3550 } else {
3551 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3552 !(ap->flags & MR_NP_RX)) {
3553 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3554 } else {
3555 ret = ANEG_FAILED;
3556 }
3557 }
3558 }
3559 break;
3560
3561 case ANEG_STATE_IDLE_DETECT_INIT:
3562 ap->link_time = ap->cur_time;
3563 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3564 tw32_f(MAC_MODE, tp->mac_mode);
3565 udelay(40);
3566
3567 ap->state = ANEG_STATE_IDLE_DETECT;
3568 ret = ANEG_TIMER_ENAB;
3569 break;
3570
3571 case ANEG_STATE_IDLE_DETECT:
3572 if (ap->ability_match != 0 &&
3573 ap->rxconfig == 0) {
3574 ap->state = ANEG_STATE_AN_ENABLE;
3575 break;
3576 }
3577 delta = ap->cur_time - ap->link_time;
3578 if (delta > ANEG_STATE_SETTLE_TIME) {
3579 /* XXX another gem from the Broadcom driver :( */
3580 ap->state = ANEG_STATE_LINK_OK;
3581 }
3582 break;
3583
3584 case ANEG_STATE_LINK_OK:
3585 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3586 ret = ANEG_DONE;
3587 break;
3588
3589 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3590 /* ??? unimplemented */
3591 break;
3592
3593 case ANEG_STATE_NEXT_PAGE_WAIT:
3594 /* ??? unimplemented */
3595 break;
3596
3597 default:
3598 ret = ANEG_FAILED;
3599 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
3602 return ret;
3603}
3604
Matt Carlson5be73b42007-12-20 20:09:29 -08003605static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
3607 int res = 0;
3608 struct tg3_fiber_aneginfo aninfo;
3609 int status = ANEG_FAILED;
3610 unsigned int tick;
3611 u32 tmp;
3612
3613 tw32_f(MAC_TX_AUTO_NEG, 0);
3614
3615 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3616 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3617 udelay(40);
3618
3619 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3620 udelay(40);
3621
3622 memset(&aninfo, 0, sizeof(aninfo));
3623 aninfo.flags |= MR_AN_ENABLE;
3624 aninfo.state = ANEG_STATE_UNKNOWN;
3625 aninfo.cur_time = 0;
3626 tick = 0;
3627 while (++tick < 195000) {
3628 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3629 if (status == ANEG_DONE || status == ANEG_FAILED)
3630 break;
3631
3632 udelay(1);
3633 }
3634
3635 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3636 tw32_f(MAC_MODE, tp->mac_mode);
3637 udelay(40);
3638
Matt Carlson5be73b42007-12-20 20:09:29 -08003639 *txflags = aninfo.txconfig;
3640 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
3642 if (status == ANEG_DONE &&
3643 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3644 MR_LP_ADV_FULL_DUPLEX)))
3645 res = 1;
3646
3647 return res;
3648}
3649
3650static void tg3_init_bcm8002(struct tg3 *tp)
3651{
3652 u32 mac_status = tr32(MAC_STATUS);
3653 int i;
3654
3655 /* Reset when initting first time or we have a link. */
3656 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3657 !(mac_status & MAC_STATUS_PCS_SYNCED))
3658 return;
3659
3660 /* Set PLL lock range. */
3661 tg3_writephy(tp, 0x16, 0x8007);
3662
3663 /* SW reset */
3664 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3665
3666 /* Wait for reset to complete. */
3667 /* XXX schedule_timeout() ... */
3668 for (i = 0; i < 500; i++)
3669 udelay(10);
3670
3671 /* Config mode; select PMA/Ch 1 regs. */
3672 tg3_writephy(tp, 0x10, 0x8411);
3673
3674 /* Enable auto-lock and comdet, select txclk for tx. */
3675 tg3_writephy(tp, 0x11, 0x0a10);
3676
3677 tg3_writephy(tp, 0x18, 0x00a0);
3678 tg3_writephy(tp, 0x16, 0x41ff);
3679
3680 /* Assert and deassert POR. */
3681 tg3_writephy(tp, 0x13, 0x0400);
3682 udelay(40);
3683 tg3_writephy(tp, 0x13, 0x0000);
3684
3685 tg3_writephy(tp, 0x11, 0x0a50);
3686 udelay(40);
3687 tg3_writephy(tp, 0x11, 0x0a10);
3688
3689 /* Wait for signal to stabilize */
3690 /* XXX schedule_timeout() ... */
3691 for (i = 0; i < 15000; i++)
3692 udelay(10);
3693
3694 /* Deselect the channel register so we can read the PHYID
3695 * later.
3696 */
3697 tg3_writephy(tp, 0x10, 0x8011);
3698}
3699
3700static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3701{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003702 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 u32 sg_dig_ctrl, sg_dig_status;
3704 u32 serdes_cfg, expected_sg_dig_ctrl;
3705 int workaround, port_a;
3706 int current_link_up;
3707
3708 serdes_cfg = 0;
3709 expected_sg_dig_ctrl = 0;
3710 workaround = 0;
3711 port_a = 1;
3712 current_link_up = 0;
3713
3714 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3715 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3716 workaround = 1;
3717 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3718 port_a = 0;
3719
3720 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3721 /* preserve bits 20-23 for voltage regulator */
3722 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3723 }
3724
3725 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3726
3727 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003728 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 if (workaround) {
3730 u32 val = serdes_cfg;
3731
3732 if (port_a)
3733 val |= 0xc010000;
3734 else
3735 val |= 0x4010000;
3736 tw32_f(MAC_SERDES_CFG, val);
3737 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003738
3739 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 }
3741 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3742 tg3_setup_flow_control(tp, 0, 0);
3743 current_link_up = 1;
3744 }
3745 goto out;
3746 }
3747
3748 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003749 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
Matt Carlson82cd3d12007-12-20 20:09:00 -08003751 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3752 if (flowctrl & ADVERTISE_1000XPAUSE)
3753 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3754 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3755 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
3757 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003758 if ((tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT) &&
3759 tp->serdes_counter &&
3760 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3761 MAC_STATUS_RCVD_CFG)) ==
3762 MAC_STATUS_PCS_SYNCED)) {
3763 tp->serdes_counter--;
3764 current_link_up = 1;
3765 goto out;
3766 }
3767restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 if (workaround)
3769 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003770 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 udelay(5);
3772 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3773
Michael Chan3d3ebe72006-09-27 15:59:15 -07003774 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3775 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3777 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003778 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 mac_status = tr32(MAC_STATUS);
3780
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003781 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003783 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
Matt Carlson82cd3d12007-12-20 20:09:00 -08003785 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3786 local_adv |= ADVERTISE_1000XPAUSE;
3787 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3788 local_adv |= ADVERTISE_1000XPSE_ASYM;
3789
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003790 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003791 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003792 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003793 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
3795 tg3_setup_flow_control(tp, local_adv, remote_adv);
3796 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003797 tp->serdes_counter = 0;
3798 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003799 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003800 if (tp->serdes_counter)
3801 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 else {
3803 if (workaround) {
3804 u32 val = serdes_cfg;
3805
3806 if (port_a)
3807 val |= 0xc010000;
3808 else
3809 val |= 0x4010000;
3810
3811 tw32_f(MAC_SERDES_CFG, val);
3812 }
3813
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003814 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 udelay(40);
3816
3817 /* Link parallel detection - link is up */
3818 /* only if we have PCS_SYNC and not */
3819 /* receiving config code words */
3820 mac_status = tr32(MAC_STATUS);
3821 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3822 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3823 tg3_setup_flow_control(tp, 0, 0);
3824 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003825 tp->tg3_flags2 |=
3826 TG3_FLG2_PARALLEL_DETECT;
3827 tp->serdes_counter =
3828 SERDES_PARALLEL_DET_TIMEOUT;
3829 } else
3830 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 }
3832 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003833 } else {
3834 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
3835 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 }
3837
3838out:
3839 return current_link_up;
3840}
3841
3842static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3843{
3844 int current_link_up = 0;
3845
Michael Chan5cf64b8a2007-05-05 12:11:21 -07003846 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
3849 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08003850 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003852
Matt Carlson5be73b42007-12-20 20:09:29 -08003853 if (fiber_autoneg(tp, &txflags, &rxflags)) {
3854 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855
Matt Carlson5be73b42007-12-20 20:09:29 -08003856 if (txflags & ANEG_CFG_PS1)
3857 local_adv |= ADVERTISE_1000XPAUSE;
3858 if (txflags & ANEG_CFG_PS2)
3859 local_adv |= ADVERTISE_1000XPSE_ASYM;
3860
3861 if (rxflags & MR_LP_ADV_SYM_PAUSE)
3862 remote_adv |= LPA_1000XPAUSE;
3863 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
3864 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
3866 tg3_setup_flow_control(tp, local_adv, remote_adv);
3867
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 current_link_up = 1;
3869 }
3870 for (i = 0; i < 30; i++) {
3871 udelay(20);
3872 tw32_f(MAC_STATUS,
3873 (MAC_STATUS_SYNC_CHANGED |
3874 MAC_STATUS_CFG_CHANGED));
3875 udelay(40);
3876 if ((tr32(MAC_STATUS) &
3877 (MAC_STATUS_SYNC_CHANGED |
3878 MAC_STATUS_CFG_CHANGED)) == 0)
3879 break;
3880 }
3881
3882 mac_status = tr32(MAC_STATUS);
3883 if (current_link_up == 0 &&
3884 (mac_status & MAC_STATUS_PCS_SYNCED) &&
3885 !(mac_status & MAC_STATUS_RCVD_CFG))
3886 current_link_up = 1;
3887 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08003888 tg3_setup_flow_control(tp, 0, 0);
3889
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 /* Forcing 1000FD link up. */
3891 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
3893 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
3894 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003895
3896 tw32_f(MAC_MODE, tp->mac_mode);
3897 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 }
3899
3900out:
3901 return current_link_up;
3902}
3903
3904static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
3905{
3906 u32 orig_pause_cfg;
3907 u16 orig_active_speed;
3908 u8 orig_active_duplex;
3909 u32 mac_status;
3910 int current_link_up;
3911 int i;
3912
Matt Carlson8d018622007-12-20 20:05:44 -08003913 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 orig_active_speed = tp->link_config.active_speed;
3915 orig_active_duplex = tp->link_config.active_duplex;
3916
3917 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
3918 netif_carrier_ok(tp->dev) &&
3919 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
3920 mac_status = tr32(MAC_STATUS);
3921 mac_status &= (MAC_STATUS_PCS_SYNCED |
3922 MAC_STATUS_SIGNAL_DET |
3923 MAC_STATUS_CFG_CHANGED |
3924 MAC_STATUS_RCVD_CFG);
3925 if (mac_status == (MAC_STATUS_PCS_SYNCED |
3926 MAC_STATUS_SIGNAL_DET)) {
3927 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3928 MAC_STATUS_CFG_CHANGED));
3929 return 0;
3930 }
3931 }
3932
3933 tw32_f(MAC_TX_AUTO_NEG, 0);
3934
3935 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
3936 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
3937 tw32_f(MAC_MODE, tp->mac_mode);
3938 udelay(40);
3939
3940 if (tp->phy_id == PHY_ID_BCM8002)
3941 tg3_init_bcm8002(tp);
3942
3943 /* Enable link change event even when serdes polling. */
3944 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3945 udelay(40);
3946
3947 current_link_up = 0;
3948 mac_status = tr32(MAC_STATUS);
3949
3950 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
3951 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
3952 else
3953 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
3954
Matt Carlson898a56f2009-08-28 14:02:40 +00003955 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00003957 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958
3959 for (i = 0; i < 100; i++) {
3960 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
3961 MAC_STATUS_CFG_CHANGED));
3962 udelay(5);
3963 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07003964 MAC_STATUS_CFG_CHANGED |
3965 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 break;
3967 }
3968
3969 mac_status = tr32(MAC_STATUS);
3970 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
3971 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003972 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
3973 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 tw32_f(MAC_MODE, (tp->mac_mode |
3975 MAC_MODE_SEND_CONFIGS));
3976 udelay(1);
3977 tw32_f(MAC_MODE, tp->mac_mode);
3978 }
3979 }
3980
3981 if (current_link_up == 1) {
3982 tp->link_config.active_speed = SPEED_1000;
3983 tp->link_config.active_duplex = DUPLEX_FULL;
3984 tw32(MAC_LED_CTRL, (tp->led_ctrl |
3985 LED_CTRL_LNKLED_OVERRIDE |
3986 LED_CTRL_1000MBPS_ON));
3987 } else {
3988 tp->link_config.active_speed = SPEED_INVALID;
3989 tp->link_config.active_duplex = DUPLEX_INVALID;
3990 tw32(MAC_LED_CTRL, (tp->led_ctrl |
3991 LED_CTRL_LNKLED_OVERRIDE |
3992 LED_CTRL_TRAFFIC_OVERRIDE));
3993 }
3994
3995 if (current_link_up != netif_carrier_ok(tp->dev)) {
3996 if (current_link_up)
3997 netif_carrier_on(tp->dev);
3998 else
3999 netif_carrier_off(tp->dev);
4000 tg3_link_report(tp);
4001 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004002 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 if (orig_pause_cfg != now_pause_cfg ||
4004 orig_active_speed != tp->link_config.active_speed ||
4005 orig_active_duplex != tp->link_config.active_duplex)
4006 tg3_link_report(tp);
4007 }
4008
4009 return 0;
4010}
4011
Michael Chan747e8f82005-07-25 12:33:22 -07004012static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4013{
4014 int current_link_up, err = 0;
4015 u32 bmsr, bmcr;
4016 u16 current_speed;
4017 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004018 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004019
4020 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4021 tw32_f(MAC_MODE, tp->mac_mode);
4022 udelay(40);
4023
4024 tw32(MAC_EVENT, 0);
4025
4026 tw32_f(MAC_STATUS,
4027 (MAC_STATUS_SYNC_CHANGED |
4028 MAC_STATUS_CFG_CHANGED |
4029 MAC_STATUS_MI_COMPLETION |
4030 MAC_STATUS_LNKSTATE_CHANGED));
4031 udelay(40);
4032
4033 if (force_reset)
4034 tg3_phy_reset(tp);
4035
4036 current_link_up = 0;
4037 current_speed = SPEED_INVALID;
4038 current_duplex = DUPLEX_INVALID;
4039
4040 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4041 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004042 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4043 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4044 bmsr |= BMSR_LSTATUS;
4045 else
4046 bmsr &= ~BMSR_LSTATUS;
4047 }
Michael Chan747e8f82005-07-25 12:33:22 -07004048
4049 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4050
4051 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlson2bd3ed02008-06-09 15:39:55 -07004052 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004053 /* do nothing, just check for link up at the end */
4054 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4055 u32 adv, new_adv;
4056
4057 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4058 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4059 ADVERTISE_1000XPAUSE |
4060 ADVERTISE_1000XPSE_ASYM |
4061 ADVERTISE_SLCT);
4062
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004063 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004064
4065 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4066 new_adv |= ADVERTISE_1000XHALF;
4067 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4068 new_adv |= ADVERTISE_1000XFULL;
4069
4070 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4071 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4072 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4073 tg3_writephy(tp, MII_BMCR, bmcr);
4074
4075 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004076 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Michael Chan747e8f82005-07-25 12:33:22 -07004077 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4078
4079 return err;
4080 }
4081 } else {
4082 u32 new_bmcr;
4083
4084 bmcr &= ~BMCR_SPEED1000;
4085 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4086
4087 if (tp->link_config.duplex == DUPLEX_FULL)
4088 new_bmcr |= BMCR_FULLDPLX;
4089
4090 if (new_bmcr != bmcr) {
4091 /* BMCR_SPEED1000 is a reserved bit that needs
4092 * to be set on write.
4093 */
4094 new_bmcr |= BMCR_SPEED1000;
4095
4096 /* Force a linkdown */
4097 if (netif_carrier_ok(tp->dev)) {
4098 u32 adv;
4099
4100 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4101 adv &= ~(ADVERTISE_1000XFULL |
4102 ADVERTISE_1000XHALF |
4103 ADVERTISE_SLCT);
4104 tg3_writephy(tp, MII_ADVERTISE, adv);
4105 tg3_writephy(tp, MII_BMCR, bmcr |
4106 BMCR_ANRESTART |
4107 BMCR_ANENABLE);
4108 udelay(10);
4109 netif_carrier_off(tp->dev);
4110 }
4111 tg3_writephy(tp, MII_BMCR, new_bmcr);
4112 bmcr = new_bmcr;
4113 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4114 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004115 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4116 ASIC_REV_5714) {
4117 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4118 bmsr |= BMSR_LSTATUS;
4119 else
4120 bmsr &= ~BMSR_LSTATUS;
4121 }
Michael Chan747e8f82005-07-25 12:33:22 -07004122 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4123 }
4124 }
4125
4126 if (bmsr & BMSR_LSTATUS) {
4127 current_speed = SPEED_1000;
4128 current_link_up = 1;
4129 if (bmcr & BMCR_FULLDPLX)
4130 current_duplex = DUPLEX_FULL;
4131 else
4132 current_duplex = DUPLEX_HALF;
4133
Matt Carlsonef167e22007-12-20 20:10:01 -08004134 local_adv = 0;
4135 remote_adv = 0;
4136
Michael Chan747e8f82005-07-25 12:33:22 -07004137 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004138 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004139
4140 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4141 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4142 common = local_adv & remote_adv;
4143 if (common & (ADVERTISE_1000XHALF |
4144 ADVERTISE_1000XFULL)) {
4145 if (common & ADVERTISE_1000XFULL)
4146 current_duplex = DUPLEX_FULL;
4147 else
4148 current_duplex = DUPLEX_HALF;
Michael Chan747e8f82005-07-25 12:33:22 -07004149 }
4150 else
4151 current_link_up = 0;
4152 }
4153 }
4154
Matt Carlsonef167e22007-12-20 20:10:01 -08004155 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4156 tg3_setup_flow_control(tp, local_adv, remote_adv);
4157
Michael Chan747e8f82005-07-25 12:33:22 -07004158 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4159 if (tp->link_config.active_duplex == DUPLEX_HALF)
4160 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4161
4162 tw32_f(MAC_MODE, tp->mac_mode);
4163 udelay(40);
4164
4165 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4166
4167 tp->link_config.active_speed = current_speed;
4168 tp->link_config.active_duplex = current_duplex;
4169
4170 if (current_link_up != netif_carrier_ok(tp->dev)) {
4171 if (current_link_up)
4172 netif_carrier_on(tp->dev);
4173 else {
4174 netif_carrier_off(tp->dev);
4175 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4176 }
4177 tg3_link_report(tp);
4178 }
4179 return err;
4180}
4181
4182static void tg3_serdes_parallel_detect(struct tg3 *tp)
4183{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004184 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004185 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004186 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004187 return;
4188 }
4189 if (!netif_carrier_ok(tp->dev) &&
4190 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4191 u32 bmcr;
4192
4193 tg3_readphy(tp, MII_BMCR, &bmcr);
4194 if (bmcr & BMCR_ANENABLE) {
4195 u32 phy1, phy2;
4196
4197 /* Select shadow register 0x1f */
4198 tg3_writephy(tp, 0x1c, 0x7c00);
4199 tg3_readphy(tp, 0x1c, &phy1);
4200
4201 /* Select expansion interrupt status register */
4202 tg3_writephy(tp, 0x17, 0x0f01);
4203 tg3_readphy(tp, 0x15, &phy2);
4204 tg3_readphy(tp, 0x15, &phy2);
4205
4206 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4207 /* We have signal detect and not receiving
4208 * config code words, link is up by parallel
4209 * detection.
4210 */
4211
4212 bmcr &= ~BMCR_ANENABLE;
4213 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4214 tg3_writephy(tp, MII_BMCR, bmcr);
4215 tp->tg3_flags2 |= TG3_FLG2_PARALLEL_DETECT;
4216 }
4217 }
4218 }
4219 else if (netif_carrier_ok(tp->dev) &&
4220 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
4221 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT)) {
4222 u32 phy2;
4223
4224 /* Select expansion interrupt status register */
4225 tg3_writephy(tp, 0x17, 0x0f01);
4226 tg3_readphy(tp, 0x15, &phy2);
4227 if (phy2 & 0x20) {
4228 u32 bmcr;
4229
4230 /* Config code words received, turn on autoneg. */
4231 tg3_readphy(tp, MII_BMCR, &bmcr);
4232 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4233
4234 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
4235
4236 }
4237 }
4238}
4239
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4241{
4242 int err;
4243
4244 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
4245 err = tg3_setup_fiber_phy(tp, force_reset);
Michael Chan747e8f82005-07-25 12:33:22 -07004246 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
4247 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 } else {
4249 err = tg3_setup_copper_phy(tp, force_reset);
4250 }
4251
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004252 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004253 u32 val, scale;
4254
4255 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4256 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4257 scale = 65;
4258 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4259 scale = 6;
4260 else
4261 scale = 12;
4262
4263 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4264 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4265 tw32(GRC_MISC_CFG, val);
4266 }
4267
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 if (tp->link_config.active_speed == SPEED_1000 &&
4269 tp->link_config.active_duplex == DUPLEX_HALF)
4270 tw32(MAC_TX_LENGTHS,
4271 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4272 (6 << TX_LENGTHS_IPG_SHIFT) |
4273 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
4274 else
4275 tw32(MAC_TX_LENGTHS,
4276 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4277 (6 << TX_LENGTHS_IPG_SHIFT) |
4278 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
4279
4280 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4281 if (netif_carrier_ok(tp->dev)) {
4282 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004283 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 } else {
4285 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4286 }
4287 }
4288
Matt Carlson8ed5d972007-05-07 00:25:49 -07004289 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
4290 u32 val = tr32(PCIE_PWR_MGMT_THRESH);
4291 if (!netif_carrier_ok(tp->dev))
4292 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4293 tp->pwrmgmt_thresh;
4294 else
4295 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4296 tw32(PCIE_PWR_MGMT_THRESH, val);
4297 }
4298
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 return err;
4300}
4301
Michael Chandf3e6542006-05-26 17:48:07 -07004302/* This is called whenever we suspect that the system chipset is re-
4303 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4304 * is bogus tx completions. We try to recover by setting the
4305 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4306 * in the workqueue.
4307 */
4308static void tg3_tx_recover(struct tg3 *tp)
4309{
4310 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4311 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4312
4313 printk(KERN_WARNING PFX "%s: The system may be re-ordering memory-"
4314 "mapped I/O cycles to the network device, attempting to "
4315 "recover. Please report the problem to the driver maintainer "
4316 "and include system chipset information.\n", tp->dev->name);
4317
4318 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004319 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004320 spin_unlock(&tp->lock);
4321}
4322
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004323static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004324{
4325 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004326 return tnapi->tx_pending -
4327 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004328}
4329
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330/* Tigon3 never reports partial packet sends. So we do not
4331 * need special logic to handle SKBs that have not had all
4332 * of their frags sent yet, like SunGEM does.
4333 */
Matt Carlson17375d22009-08-28 14:02:18 +00004334static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335{
Matt Carlson17375d22009-08-28 14:02:18 +00004336 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004337 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004338 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004339 struct netdev_queue *txq;
4340 int index = tnapi - tp->napi;
4341
4342 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
4343 index--;
4344
4345 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
4347 while (sw_idx != hw_idx) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004348 struct tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004350 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
Michael Chandf3e6542006-05-26 17:48:07 -07004352 if (unlikely(skb == NULL)) {
4353 tg3_tx_recover(tp);
4354 return;
4355 }
4356
David S. Miller90079ce2008-09-11 04:52:51 -07004357 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358
4359 ri->skb = NULL;
4360
4361 sw_idx = NEXT_TX(sw_idx);
4362
4363 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004364 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004365 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4366 tx_bug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 sw_idx = NEXT_TX(sw_idx);
4368 }
4369
David S. Millerf47c11e2005-06-24 20:18:35 -07004370 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004371
4372 if (unlikely(tx_bug)) {
4373 tg3_tx_recover(tp);
4374 return;
4375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 }
4377
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004378 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
Michael Chan1b2a7202006-08-07 21:46:02 -07004380 /* Need to make the tx_cons update visible to tg3_start_xmit()
4381 * before checking for netif_queue_stopped(). Without the
4382 * memory barrier, there is a small possibility that tg3_start_xmit()
4383 * will miss it and cause the queue to be stopped forever.
4384 */
4385 smp_mb();
4386
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004387 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004388 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004389 __netif_tx_lock(txq, smp_processor_id());
4390 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004391 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004392 netif_tx_wake_queue(txq);
4393 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395}
4396
4397/* Returns size of skb allocated or < 0 on error.
4398 *
4399 * We only need to fill in the address because the other members
4400 * of the RX descriptor are invariant, see tg3_init_rings.
4401 *
4402 * Note the purposeful assymetry of cpu vs. chip accesses. For
4403 * posting buffers we only dirty the first cache line of the RX
4404 * descriptor (containing the address). Whereas for the RX status
4405 * buffers the cpu only reads the last cacheline of the RX descriptor
4406 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4407 */
Matt Carlson17375d22009-08-28 14:02:18 +00004408static int tg3_alloc_rx_skb(struct tg3_napi *tnapi, u32 opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 int src_idx, u32 dest_idx_unmasked)
4410{
Matt Carlson17375d22009-08-28 14:02:18 +00004411 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 struct tg3_rx_buffer_desc *desc;
4413 struct ring_info *map, *src_map;
4414 struct sk_buff *skb;
4415 dma_addr_t mapping;
4416 int skb_size, dest_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004417 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418
4419 src_map = NULL;
4420 switch (opaque_key) {
4421 case RXD_OPAQUE_RING_STD:
4422 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlson21f581a2009-08-28 14:00:25 +00004423 desc = &tpr->rx_std[dest_idx];
4424 map = &tpr->rx_std_buffers[dest_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 if (src_idx >= 0)
Matt Carlson21f581a2009-08-28 14:00:25 +00004426 src_map = &tpr->rx_std_buffers[src_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004427 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 break;
4429
4430 case RXD_OPAQUE_RING_JUMBO:
4431 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004432 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004433 map = &tpr->rx_jmb_buffers[dest_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 if (src_idx >= 0)
Matt Carlson21f581a2009-08-28 14:00:25 +00004435 src_map = &tpr->rx_jmb_buffers[src_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004436 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 break;
4438
4439 default:
4440 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
4443 /* Do not overwrite any of the map or rp information
4444 * until we are sure we can commit to a new buffer.
4445 *
4446 * Callers depend upon this behavior and assume that
4447 * we leave everything unchanged if we fail.
4448 */
Matt Carlson287be122009-08-28 13:58:46 +00004449 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 if (skb == NULL)
4451 return -ENOMEM;
4452
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 skb_reserve(skb, tp->rx_offset);
4454
Matt Carlson287be122009-08-28 13:58:46 +00004455 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004457 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4458 dev_kfree_skb(skb);
4459 return -EIO;
4460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
4462 map->skb = skb;
4463 pci_unmap_addr_set(map, mapping, mapping);
4464
4465 if (src_map != NULL)
4466 src_map->skb = NULL;
4467
4468 desc->addr_hi = ((u64)mapping >> 32);
4469 desc->addr_lo = ((u64)mapping & 0xffffffff);
4470
4471 return skb_size;
4472}
4473
4474/* We only need to move over in the address because the other
4475 * members of the RX descriptor are invariant. See notes above
4476 * tg3_alloc_rx_skb for full details.
4477 */
Matt Carlson17375d22009-08-28 14:02:18 +00004478static void tg3_recycle_rx(struct tg3_napi *tnapi, u32 opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 int src_idx, u32 dest_idx_unmasked)
4480{
Matt Carlson17375d22009-08-28 14:02:18 +00004481 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4483 struct ring_info *src_map, *dest_map;
4484 int dest_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004485 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
4487 switch (opaque_key) {
4488 case RXD_OPAQUE_RING_STD:
4489 dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
Matt Carlson21f581a2009-08-28 14:00:25 +00004490 dest_desc = &tpr->rx_std[dest_idx];
4491 dest_map = &tpr->rx_std_buffers[dest_idx];
4492 src_desc = &tpr->rx_std[src_idx];
4493 src_map = &tpr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 break;
4495
4496 case RXD_OPAQUE_RING_JUMBO:
4497 dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004498 dest_desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004499 dest_map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004500 src_desc = &tpr->rx_jmb[src_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004501 src_map = &tpr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 break;
4503
4504 default:
4505 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
4508 dest_map->skb = src_map->skb;
4509 pci_unmap_addr_set(dest_map, mapping,
4510 pci_unmap_addr(src_map, mapping));
4511 dest_desc->addr_hi = src_desc->addr_hi;
4512 dest_desc->addr_lo = src_desc->addr_lo;
4513
4514 src_map->skb = NULL;
4515}
4516
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517/* The RX ring scheme is composed of multiple rings which post fresh
4518 * buffers to the chip, and one special ring the chip uses to report
4519 * status back to the host.
4520 *
4521 * The special ring reports the status of received packets to the
4522 * host. The chip does not write into the original descriptor the
4523 * RX buffer was obtained from. The chip simply takes the original
4524 * descriptor as provided by the host, updates the status and length
4525 * field, then writes this into the next status ring entry.
4526 *
4527 * Each ring the host uses to post buffers to the chip is described
4528 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4529 * it is first placed into the on-chip ram. When the packet's length
4530 * is known, it walks down the TG3_BDINFO entries to select the ring.
4531 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4532 * which is within the range of the new packet's length is chosen.
4533 *
4534 * The "separate ring for rx status" scheme may sound queer, but it makes
4535 * sense from a cache coherency perspective. If only the host writes
4536 * to the buffer post rings, and only the chip writes to the rx status
4537 * rings, then cache lines never move beyond shared-modified state.
4538 * If both the host and chip were to write into the same ring, cache line
4539 * eviction could occur since both entities want it in an exclusive state.
4540 */
Matt Carlson17375d22009-08-28 14:02:18 +00004541static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542{
Matt Carlson17375d22009-08-28 14:02:18 +00004543 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004544 u32 work_mask, rx_std_posted = 0;
Matt Carlson72334482009-08-28 14:03:01 +00004545 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004546 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 int received;
Matt Carlson21f581a2009-08-28 14:00:25 +00004548 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004550 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 /*
4552 * We need to order the read of hw_idx and the read of
4553 * the opaque cookie.
4554 */
4555 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 work_mask = 0;
4557 received = 0;
4558 while (sw_idx != hw_idx && budget > 0) {
Matt Carlson72334482009-08-28 14:03:01 +00004559 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 unsigned int len;
4561 struct sk_buff *skb;
4562 dma_addr_t dma_addr;
4563 u32 opaque_key, desc_idx, *post_ptr;
4564
4565 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4566 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4567 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson21f581a2009-08-28 14:00:25 +00004568 struct ring_info *ri = &tpr->rx_std_buffers[desc_idx];
4569 dma_addr = pci_unmap_addr(ri, mapping);
4570 skb = ri->skb;
4571 post_ptr = &tpr->rx_std_ptr;
Michael Chanf92905d2006-06-29 20:14:29 -07004572 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson21f581a2009-08-28 14:00:25 +00004574 struct ring_info *ri = &tpr->rx_jmb_buffers[desc_idx];
4575 dma_addr = pci_unmap_addr(ri, mapping);
4576 skb = ri->skb;
4577 post_ptr = &tpr->rx_jmb_ptr;
4578 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
4581 work_mask |= opaque_key;
4582
4583 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4584 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4585 drop_it:
Matt Carlson17375d22009-08-28 14:02:18 +00004586 tg3_recycle_rx(tnapi, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 desc_idx, *post_ptr);
4588 drop_it_no_recycle:
4589 /* Other statistics kept track of by card. */
4590 tp->net_stats.rx_dropped++;
4591 goto next_pkt;
4592 }
4593
Matt Carlsonad829262008-11-21 17:16:16 -08004594 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4595 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004597 if (len > RX_COPY_THRESHOLD
Matt Carlsonad829262008-11-21 17:16:16 -08004598 && tp->rx_offset == NET_IP_ALIGN
4599 /* rx_offset will likely not equal NET_IP_ALIGN
4600 * if this is a 5701 card running in PCI-X mode
4601 * [see tg3_get_invariants()]
4602 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 ) {
4604 int skb_size;
4605
Matt Carlson17375d22009-08-28 14:02:18 +00004606 skb_size = tg3_alloc_rx_skb(tnapi, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 desc_idx, *post_ptr);
4608 if (skb_size < 0)
4609 goto drop_it;
4610
Matt Carlson287be122009-08-28 13:58:46 +00004611 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 PCI_DMA_FROMDEVICE);
4613
4614 skb_put(skb, len);
4615 } else {
4616 struct sk_buff *copy_skb;
4617
Matt Carlson17375d22009-08-28 14:02:18 +00004618 tg3_recycle_rx(tnapi, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 desc_idx, *post_ptr);
4620
Matt Carlsonad829262008-11-21 17:16:16 -08004621 copy_skb = netdev_alloc_skb(tp->dev,
4622 len + TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 if (copy_skb == NULL)
4624 goto drop_it_no_recycle;
4625
Matt Carlsonad829262008-11-21 17:16:16 -08004626 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 skb_put(copy_skb, len);
4628 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004629 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4631
4632 /* We'll reuse the original ring buffer. */
4633 skb = copy_skb;
4634 }
4635
4636 if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
4637 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4638 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4639 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4640 skb->ip_summed = CHECKSUM_UNNECESSARY;
4641 else
4642 skb->ip_summed = CHECKSUM_NONE;
4643
4644 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004645
4646 if (len > (tp->dev->mtu + ETH_HLEN) &&
4647 skb->protocol != htons(ETH_P_8021Q)) {
4648 dev_kfree_skb(skb);
4649 goto next_pkt;
4650 }
4651
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652#if TG3_VLAN_TAG_USED
4653 if (tp->vlgrp != NULL &&
4654 desc->type_flags & RXD_FLAG_VLAN) {
Matt Carlson17375d22009-08-28 14:02:18 +00004655 vlan_gro_receive(&tnapi->napi, tp->vlgrp,
Matt Carlson8ef04422009-08-28 14:01:37 +00004656 desc->err_vlan & RXD_VLAN_MASK, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 } else
4658#endif
Matt Carlson17375d22009-08-28 14:02:18 +00004659 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 received++;
4662 budget--;
4663
4664next_pkt:
4665 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004666
4667 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
4668 u32 idx = *post_ptr % TG3_RX_RING_SIZE;
4669
4670 tw32_rx_mbox(MAILBOX_RCV_STD_PROD_IDX +
4671 TG3_64BIT_REG_LOW, idx);
4672 work_mask &= ~RXD_OPAQUE_RING_STD;
4673 rx_std_posted = 0;
4674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004676 sw_idx++;
Eric Dumazet6b31a512007-02-06 13:29:21 -08004677 sw_idx &= (TG3_RX_RCB_RING_SIZE(tp) - 1);
Michael Chan52f6d692005-04-25 15:14:32 -07004678
4679 /* Refresh hw_idx to see if there is new work */
4680 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004681 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004682 rmb();
4683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 }
4685
4686 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004687 tnapi->rx_rcb_ptr = sw_idx;
4688 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
4690 /* Refill RX ring(s). */
4691 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson21f581a2009-08-28 14:00:25 +00004692 sw_idx = tpr->rx_std_ptr % TG3_RX_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 tw32_rx_mbox(MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW,
4694 sw_idx);
4695 }
4696 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson21f581a2009-08-28 14:00:25 +00004697 sw_idx = tpr->rx_jmb_ptr % TG3_RX_JUMBO_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 tw32_rx_mbox(MAILBOX_RCV_JUMBO_PROD_IDX + TG3_64BIT_REG_LOW,
4699 sw_idx);
4700 }
4701 mmiowb();
4702
4703 return received;
4704}
4705
Matt Carlson17375d22009-08-28 14:02:18 +00004706static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707{
Matt Carlson17375d22009-08-28 14:02:18 +00004708 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004709 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 /* handle link change and other phy events */
4712 if (!(tp->tg3_flags &
4713 (TG3_FLAG_USE_LINKCHG_REG |
4714 TG3_FLAG_POLL_SERDES))) {
4715 if (sblk->status & SD_STATUS_LINK_CHG) {
4716 sblk->status = SD_STATUS_UPDATED |
4717 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07004718 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07004719 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
4720 tw32_f(MAC_STATUS,
4721 (MAC_STATUS_SYNC_CHANGED |
4722 MAC_STATUS_CFG_CHANGED |
4723 MAC_STATUS_MI_COMPLETION |
4724 MAC_STATUS_LNKSTATE_CHANGED));
4725 udelay(40);
4726 } else
4727 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07004728 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 }
4730 }
4731
4732 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004733 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00004734 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004735 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07004736 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 }
4738
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 /* run RX thread, within the bounds set by NAPI.
4740 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004741 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004743 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00004744 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745
David S. Miller6f535762007-10-11 18:08:29 -07004746 return work_done;
4747}
David S. Millerf7383c22005-05-18 22:50:53 -07004748
David S. Miller6f535762007-10-11 18:08:29 -07004749static int tg3_poll(struct napi_struct *napi, int budget)
4750{
Matt Carlson8ef04422009-08-28 14:01:37 +00004751 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
4752 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07004753 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00004754 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07004755
4756 while (1) {
Matt Carlson17375d22009-08-28 14:02:18 +00004757 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07004758
4759 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
4760 goto tx_recovery;
4761
4762 if (unlikely(work_done >= budget))
4763 break;
4764
Michael Chan4fd7ab52007-10-12 01:39:50 -07004765 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00004766 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07004767 * to tell the hw how much work has been processed,
4768 * so we must read it before checking for more work.
4769 */
Matt Carlson898a56f2009-08-28 14:02:40 +00004770 tnapi->last_tag = sblk->status_tag;
4771 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07004772 rmb();
4773 } else
4774 sblk->status &= ~SD_STATUS_UPDATED;
4775
Matt Carlson17375d22009-08-28 14:02:18 +00004776 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08004777 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00004778 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07004779 break;
4780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 }
4782
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004783 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07004784
4785tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07004786 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08004787 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07004788 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07004789 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790}
4791
David S. Millerf47c11e2005-06-24 20:18:35 -07004792static void tg3_irq_quiesce(struct tg3 *tp)
4793{
Matt Carlson4f125f42009-09-01 12:55:02 +00004794 int i;
4795
David S. Millerf47c11e2005-06-24 20:18:35 -07004796 BUG_ON(tp->irq_sync);
4797
4798 tp->irq_sync = 1;
4799 smp_mb();
4800
Matt Carlson4f125f42009-09-01 12:55:02 +00004801 for (i = 0; i < tp->irq_cnt; i++)
4802 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07004803}
4804
4805static inline int tg3_irq_sync(struct tg3 *tp)
4806{
4807 return tp->irq_sync;
4808}
4809
4810/* Fully shutdown all tg3 driver activity elsewhere in the system.
4811 * If irq_sync is non-zero, then the IRQ handler must be synchronized
4812 * with as well. Most of the time, this is not necessary except when
4813 * shutting down the device.
4814 */
4815static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
4816{
Michael Chan46966542007-07-11 19:47:19 -07004817 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07004818 if (irq_sync)
4819 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07004820}
4821
4822static inline void tg3_full_unlock(struct tg3 *tp)
4823{
David S. Millerf47c11e2005-06-24 20:18:35 -07004824 spin_unlock_bh(&tp->lock);
4825}
4826
Michael Chanfcfa0a32006-03-20 22:28:41 -08004827/* One-shot MSI handler - Chip automatically disables interrupt
4828 * after sending MSI so driver doesn't have to do it.
4829 */
David Howells7d12e782006-10-05 14:55:46 +01004830static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08004831{
Matt Carlson09943a12009-08-28 14:01:57 +00004832 struct tg3_napi *tnapi = dev_id;
4833 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08004834
Matt Carlson898a56f2009-08-28 14:02:40 +00004835 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00004836 if (tnapi->rx_rcb)
4837 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08004838
4839 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00004840 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08004841
4842 return IRQ_HANDLED;
4843}
4844
Michael Chan88b06bc22005-04-21 17:13:25 -07004845/* MSI ISR - No need to check for interrupt sharing and no need to
4846 * flush status block and interrupt mailbox. PCI ordering rules
4847 * guarantee that MSI will arrive after the status block.
4848 */
David Howells7d12e782006-10-05 14:55:46 +01004849static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07004850{
Matt Carlson09943a12009-08-28 14:01:57 +00004851 struct tg3_napi *tnapi = dev_id;
4852 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07004853
Matt Carlson898a56f2009-08-28 14:02:40 +00004854 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00004855 if (tnapi->rx_rcb)
4856 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07004857 /*
David S. Millerfac9b832005-05-18 22:46:34 -07004858 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07004859 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07004860 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07004861 * NIC to stop sending us irqs, engaging "in-intr-handler"
4862 * event coalescing.
4863 */
4864 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07004865 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00004866 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07004867
Michael Chan88b06bc22005-04-21 17:13:25 -07004868 return IRQ_RETVAL(1);
4869}
4870
David Howells7d12e782006-10-05 14:55:46 +01004871static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872{
Matt Carlson09943a12009-08-28 14:01:57 +00004873 struct tg3_napi *tnapi = dev_id;
4874 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004875 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 unsigned int handled = 1;
4877
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 /* In INTx mode, it is possible for the interrupt to arrive at
4879 * the CPU before the status block posted prior to the interrupt.
4880 * Reading the PCI State register will confirm whether the
4881 * interrupt is ours and will flush the status block.
4882 */
Michael Chand18edcb2007-03-24 20:57:11 -07004883 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
4884 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
4885 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
4886 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07004887 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07004888 }
Michael Chand18edcb2007-03-24 20:57:11 -07004889 }
4890
4891 /*
4892 * Writing any value to intr-mbox-0 clears PCI INTA# and
4893 * chip-internal interrupt pending events.
4894 * Writing non-zero to intr-mbox-0 additional tells the
4895 * NIC to stop sending us irqs, engaging "in-intr-handler"
4896 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07004897 *
4898 * Flush the mailbox to de-assert the IRQ immediately to prevent
4899 * spurious interrupts. The flush impacts performance but
4900 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07004901 */
Michael Chanc04cb342007-05-07 00:26:15 -07004902 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07004903 if (tg3_irq_sync(tp))
4904 goto out;
4905 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00004906 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00004907 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00004908 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07004909 } else {
4910 /* No work, shared interrupt perhaps? re-enable
4911 * interrupts, and flush that PCI write
4912 */
4913 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
4914 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07004915 }
David S. Millerf47c11e2005-06-24 20:18:35 -07004916out:
David S. Millerfac9b832005-05-18 22:46:34 -07004917 return IRQ_RETVAL(handled);
4918}
4919
David Howells7d12e782006-10-05 14:55:46 +01004920static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07004921{
Matt Carlson09943a12009-08-28 14:01:57 +00004922 struct tg3_napi *tnapi = dev_id;
4923 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004924 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07004925 unsigned int handled = 1;
4926
David S. Millerfac9b832005-05-18 22:46:34 -07004927 /* In INTx mode, it is possible for the interrupt to arrive at
4928 * the CPU before the status block posted prior to the interrupt.
4929 * Reading the PCI State register will confirm whether the
4930 * interrupt is ours and will flush the status block.
4931 */
Matt Carlson898a56f2009-08-28 14:02:40 +00004932 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07004933 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
4934 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
4935 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07004936 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 }
Michael Chand18edcb2007-03-24 20:57:11 -07004938 }
4939
4940 /*
4941 * writing any value to intr-mbox-0 clears PCI INTA# and
4942 * chip-internal interrupt pending events.
4943 * writing non-zero to intr-mbox-0 additional tells the
4944 * NIC to stop sending us irqs, engaging "in-intr-handler"
4945 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07004946 *
4947 * Flush the mailbox to de-assert the IRQ immediately to prevent
4948 * spurious interrupts. The flush impacts performance but
4949 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07004950 */
Michael Chanc04cb342007-05-07 00:26:15 -07004951 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00004952
4953 /*
4954 * In a shared interrupt configuration, sometimes other devices'
4955 * interrupts will scream. We record the current status tag here
4956 * so that the above check can report that the screaming interrupts
4957 * are unhandled. Eventually they will be silenced.
4958 */
Matt Carlson898a56f2009-08-28 14:02:40 +00004959 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00004960
Michael Chand18edcb2007-03-24 20:57:11 -07004961 if (tg3_irq_sync(tp))
4962 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00004963
Matt Carlson72334482009-08-28 14:03:01 +00004964 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00004965
Matt Carlson09943a12009-08-28 14:01:57 +00004966 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00004967
David S. Millerf47c11e2005-06-24 20:18:35 -07004968out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 return IRQ_RETVAL(handled);
4970}
4971
Michael Chan79381092005-04-21 17:13:59 -07004972/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01004973static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07004974{
Matt Carlson09943a12009-08-28 14:01:57 +00004975 struct tg3_napi *tnapi = dev_id;
4976 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004977 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07004978
Michael Chanf9804dd2005-09-27 12:13:10 -07004979 if ((sblk->status & SD_STATUS_UPDATED) ||
4980 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07004981 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07004982 return IRQ_RETVAL(1);
4983 }
4984 return IRQ_RETVAL(0);
4985}
4986
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07004987static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07004988static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989
Michael Chanb9ec6c12006-07-25 16:37:27 -07004990/* Restart hardware after configuration changes, self-test, etc.
4991 * Invoked with tp->lock held.
4992 */
4993static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07004994 __releases(tp->lock)
4995 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07004996{
4997 int err;
4998
4999 err = tg3_init_hw(tp, reset_phy);
5000 if (err) {
5001 printk(KERN_ERR PFX "%s: Failed to re-initialize device, "
5002 "aborting.\n", tp->dev->name);
5003 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5004 tg3_full_unlock(tp);
5005 del_timer_sync(&tp->timer);
5006 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005007 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005008 dev_close(tp->dev);
5009 tg3_full_lock(tp, 0);
5010 }
5011 return err;
5012}
5013
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014#ifdef CONFIG_NET_POLL_CONTROLLER
5015static void tg3_poll_controller(struct net_device *dev)
5016{
Matt Carlson4f125f42009-09-01 12:55:02 +00005017 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005018 struct tg3 *tp = netdev_priv(dev);
5019
Matt Carlson4f125f42009-09-01 12:55:02 +00005020 for (i = 0; i < tp->irq_cnt; i++)
5021 tg3_interrupt(tp->napi[i].irq_vec, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022}
5023#endif
5024
David Howellsc4028952006-11-22 14:57:56 +00005025static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026{
David Howellsc4028952006-11-22 14:57:56 +00005027 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005028 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 unsigned int restart_timer;
5030
Michael Chan7faa0062006-02-02 17:29:28 -08005031 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005032
5033 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005034 tg3_full_unlock(tp);
5035 return;
5036 }
5037
5038 tg3_full_unlock(tp);
5039
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005040 tg3_phy_stop(tp);
5041
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 tg3_netif_stop(tp);
5043
David S. Millerf47c11e2005-06-24 20:18:35 -07005044 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
5046 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5047 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5048
Michael Chandf3e6542006-05-26 17:48:07 -07005049 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5050 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5051 tp->write32_rx_mbox = tg3_write_flush_reg32;
5052 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5053 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5054 }
5055
Michael Chan944d9802005-05-29 14:57:48 -07005056 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005057 err = tg3_init_hw(tp, 1);
5058 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005059 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
5061 tg3_netif_start(tp);
5062
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 if (restart_timer)
5064 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005065
Michael Chanb9ec6c12006-07-25 16:37:27 -07005066out:
Michael Chan7faa0062006-02-02 17:29:28 -08005067 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005068
5069 if (!err)
5070 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071}
5072
Michael Chanb0408752007-02-13 12:18:30 -08005073static void tg3_dump_short_state(struct tg3 *tp)
5074{
5075 printk(KERN_ERR PFX "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
5076 tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
5077 printk(KERN_ERR PFX "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
5078 tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
5079}
5080
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081static void tg3_tx_timeout(struct net_device *dev)
5082{
5083 struct tg3 *tp = netdev_priv(dev);
5084
Michael Chanb0408752007-02-13 12:18:30 -08005085 if (netif_msg_tx_err(tp)) {
Michael Chan9f88f292006-12-07 00:22:54 -08005086 printk(KERN_ERR PFX "%s: transmit timed out, resetting\n",
5087 dev->name);
Michael Chanb0408752007-02-13 12:18:30 -08005088 tg3_dump_short_state(tp);
5089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
5091 schedule_work(&tp->reset_task);
5092}
5093
Michael Chanc58ec932005-09-17 00:46:27 -07005094/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5095static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5096{
5097 u32 base = (u32) mapping & 0xffffffff;
5098
5099 return ((base > 0xffffdcc0) &&
5100 (base + len + 8 < base));
5101}
5102
Michael Chan72f2afb2006-03-06 19:28:35 -08005103/* Test for DMA addresses > 40-bit */
5104static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5105 int len)
5106{
5107#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005108 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Yang Hongyang50cf1562009-04-06 19:01:14 -07005109 return (((u64) mapping + len) > DMA_BIT_MASK(40));
Michael Chan72f2afb2006-03-06 19:28:35 -08005110 return 0;
5111#else
5112 return 0;
5113#endif
5114}
5115
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005116static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117
Michael Chan72f2afb2006-03-06 19:28:35 -08005118/* Workaround 4GB and 40-bit hardware DMA bugs. */
5119static int tigon3_dma_hwbug_workaround(struct tg3 *tp, struct sk_buff *skb,
Michael Chanc58ec932005-09-17 00:46:27 -07005120 u32 last_plus_one, u32 *start,
5121 u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005123 struct tg3_napi *tnapi = &tp->napi[0];
Matt Carlson41588ba2008-04-19 18:12:33 -07005124 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005125 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005127 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128
Matt Carlson41588ba2008-04-19 18:12:33 -07005129 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5130 new_skb = skb_copy(skb, GFP_ATOMIC);
5131 else {
5132 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5133
5134 new_skb = skb_copy_expand(skb,
5135 skb_headroom(skb) + more_headroom,
5136 skb_tailroom(skb), GFP_ATOMIC);
5137 }
5138
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005140 ret = -1;
5141 } else {
5142 /* New SKB is guaranteed to be linear. */
5143 entry = *start;
David S. Miller90079ce2008-09-11 04:52:51 -07005144 ret = skb_dma_map(&tp->pdev->dev, new_skb, DMA_TO_DEVICE);
Eric Dumazet042a53a2009-06-05 04:04:16 +00005145 new_addr = skb_shinfo(new_skb)->dma_head;
David S. Miller90079ce2008-09-11 04:52:51 -07005146
Michael Chanc58ec932005-09-17 00:46:27 -07005147 /* Make sure new skb does not cross any 4G boundaries.
5148 * Drop the packet if it does.
5149 */
Matt Carlson0e1406d2009-11-02 12:33:33 +00005150 if (ret || ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5151 tg3_4g_overflow_test(new_addr, new_skb->len))) {
David S. Miller638266f2008-09-11 15:45:19 -07005152 if (!ret)
5153 skb_dma_unmap(&tp->pdev->dev, new_skb,
5154 DMA_TO_DEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005155 ret = -1;
5156 dev_kfree_skb(new_skb);
5157 new_skb = NULL;
5158 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005159 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005160 base_flags, 1 | (mss << 1));
5161 *start = NEXT_TX(entry);
5162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 }
5164
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 /* Now clean up the sw ring entries. */
5166 i = 0;
5167 while (entry != last_plus_one) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005168 if (i == 0)
5169 tnapi->tx_buffers[entry].skb = new_skb;
5170 else
5171 tnapi->tx_buffers[entry].skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 entry = NEXT_TX(entry);
5173 i++;
5174 }
5175
David S. Miller90079ce2008-09-11 04:52:51 -07005176 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 dev_kfree_skb(skb);
5178
Michael Chanc58ec932005-09-17 00:46:27 -07005179 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180}
5181
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005182static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 dma_addr_t mapping, int len, u32 flags,
5184 u32 mss_and_is_end)
5185{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005186 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 int is_end = (mss_and_is_end & 0x1);
5188 u32 mss = (mss_and_is_end >> 1);
5189 u32 vlan_tag = 0;
5190
5191 if (is_end)
5192 flags |= TXD_FLAG_END;
5193 if (flags & TXD_FLAG_VLAN) {
5194 vlan_tag = flags >> 16;
5195 flags &= 0xffff;
5196 }
5197 vlan_tag |= (mss << TXD_MSS_SHIFT);
5198
5199 txd->addr_hi = ((u64) mapping >> 32);
5200 txd->addr_lo = ((u64) mapping & 0xffffffff);
5201 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5202 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5203}
5204
Michael Chan5a6f3072006-03-20 22:28:05 -08005205/* hard_start_xmit for devices that don't have any bugs and
5206 * support TG3_FLG2_HW_TSO_2 only.
5207 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005208static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5209 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210{
5211 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005213 struct skb_shared_info *sp;
5214 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005215 struct tg3_napi *tnapi;
5216 struct netdev_queue *txq;
Michael Chan5a6f3072006-03-20 22:28:05 -08005217
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005218 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5219 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
5220 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
5221 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005222
Michael Chan00b70502006-06-17 21:58:45 -07005223 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005224 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005225 * interrupt. Furthermore, IRQ processing runs lockless so we have
5226 * no IRQ context deadlocks to worry about either. Rejoice!
5227 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005228 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005229 if (!netif_tx_queue_stopped(txq)) {
5230 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005231
5232 /* This is a hard error, log it. */
5233 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
5234 "queue awake!\n", dev->name);
5235 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005236 return NETDEV_TX_BUSY;
5237 }
5238
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005239 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005240 base_flags = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005241 mss = 0;
Matt Carlsonc13e3712007-05-05 11:50:04 -07005242 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005243 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005244 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005245
5246 if (skb_header_cloned(skb) &&
5247 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5248 dev_kfree_skb(skb);
5249 goto out_unlock;
5250 }
5251
Michael Chanb0026622006-07-03 19:42:14 -07005252 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005253 hdrlen = skb_headlen(skb) - ETH_HLEN;
Michael Chanb0026622006-07-03 19:42:14 -07005254 else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005255 struct iphdr *iph = ip_hdr(skb);
5256
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005257 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005258 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005259
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005260 iph->check = 0;
5261 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005262 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005263 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005264
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005265 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
5266 mss |= (hdrlen & 0xc) << 12;
5267 if (hdrlen & 0x10)
5268 base_flags |= 0x00000010;
5269 base_flags |= (hdrlen & 0x3e0) << 5;
5270 } else
5271 mss |= hdrlen << 9;
5272
Michael Chan5a6f3072006-03-20 22:28:05 -08005273 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5274 TXD_FLAG_CPU_POST_DMA);
5275
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005276 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005277
Michael Chan5a6f3072006-03-20 22:28:05 -08005278 }
Patrick McHardy84fa7932006-08-29 16:44:56 -07005279 else if (skb->ip_summed == CHECKSUM_PARTIAL)
Michael Chan5a6f3072006-03-20 22:28:05 -08005280 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Michael Chan5a6f3072006-03-20 22:28:05 -08005281#if TG3_VLAN_TAG_USED
5282 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5283 base_flags |= (TXD_FLAG_VLAN |
5284 (vlan_tx_tag_get(skb) << 16));
5285#endif
5286
David S. Miller90079ce2008-09-11 04:52:51 -07005287 if (skb_dma_map(&tp->pdev->dev, skb, DMA_TO_DEVICE)) {
5288 dev_kfree_skb(skb);
5289 goto out_unlock;
5290 }
5291
5292 sp = skb_shinfo(skb);
5293
Eric Dumazet042a53a2009-06-05 04:04:16 +00005294 mapping = sp->dma_head;
Michael Chan5a6f3072006-03-20 22:28:05 -08005295
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005296 tnapi->tx_buffers[entry].skb = skb;
Michael Chan5a6f3072006-03-20 22:28:05 -08005297
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005298 len = skb_headlen(skb);
5299
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005300 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
5301 !mss && skb->len > ETH_DATA_LEN)
5302 base_flags |= TXD_FLAG_JMB_PKT;
5303
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005304 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005305 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5306
5307 entry = NEXT_TX(entry);
5308
5309 /* Now loop through additional data fragments, and queue them. */
5310 if (skb_shinfo(skb)->nr_frags > 0) {
5311 unsigned int i, last;
5312
5313 last = skb_shinfo(skb)->nr_frags - 1;
5314 for (i = 0; i <= last; i++) {
5315 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5316
5317 len = frag->size;
Eric Dumazet042a53a2009-06-05 04:04:16 +00005318 mapping = sp->dma_maps[i];
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005319 tnapi->tx_buffers[entry].skb = NULL;
Michael Chan5a6f3072006-03-20 22:28:05 -08005320
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005321 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005322 base_flags, (i == last) | (mss << 1));
5323
5324 entry = NEXT_TX(entry);
5325 }
5326 }
5327
5328 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005329 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005330
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005331 tnapi->tx_prod = entry;
5332 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005333 netif_tx_stop_queue(txq);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005334 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005335 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005336 }
5337
5338out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005339 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005340
5341 return NETDEV_TX_OK;
5342}
5343
Stephen Hemminger613573252009-08-31 19:50:58 +00005344static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5345 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005346
5347/* Use GSO to workaround a rare TSO bug that may be triggered when the
5348 * TSO header is greater than 80 bytes.
5349 */
5350static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5351{
5352 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005353 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005354
5355 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005356 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005357 netif_stop_queue(tp->dev);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005358 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005359 return NETDEV_TX_BUSY;
5360
5361 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005362 }
5363
5364 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005365 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005366 goto tg3_tso_bug_end;
5367
5368 do {
5369 nskb = segs;
5370 segs = segs->next;
5371 nskb->next = NULL;
5372 tg3_start_xmit_dma_bug(nskb, tp->dev);
5373 } while (segs);
5374
5375tg3_tso_bug_end:
5376 dev_kfree_skb(skb);
5377
5378 return NETDEV_TX_OK;
5379}
Michael Chan52c0fd82006-06-29 20:15:54 -07005380
Michael Chan5a6f3072006-03-20 22:28:05 -08005381/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
5382 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
5383 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005384static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
5385 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005386{
5387 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005388 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005389 struct skb_shared_info *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005391 dma_addr_t mapping;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005392 struct tg3_napi *tnapi = &tp->napi[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393
5394 len = skb_headlen(skb);
5395
Michael Chan00b70502006-06-17 21:58:45 -07005396 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005397 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005398 * interrupt. Furthermore, IRQ processing runs lockless so we have
5399 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005401 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005402 if (!netif_queue_stopped(dev)) {
5403 netif_stop_queue(dev);
5404
5405 /* This is a hard error, log it. */
5406 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
5407 "queue awake!\n", dev->name);
5408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 return NETDEV_TX_BUSY;
5410 }
5411
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005412 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005414 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 mss = 0;
Matt Carlsonc13e3712007-05-05 11:50:04 -07005417 if ((mss = skb_shinfo(skb)->gso_size) != 0) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005418 struct iphdr *iph;
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005419 u32 tcp_opt_len, ip_tcp_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420
5421 if (skb_header_cloned(skb) &&
5422 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5423 dev_kfree_skb(skb);
5424 goto out_unlock;
5425 }
5426
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005427 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005428 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429
Michael Chan52c0fd82006-06-29 20:15:54 -07005430 hdr_len = ip_tcp_len + tcp_opt_len;
5431 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08005432 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Michael Chan52c0fd82006-06-29 20:15:54 -07005433 return (tg3_tso_bug(tp, skb));
5434
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5436 TXD_FLAG_CPU_POST_DMA);
5437
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005438 iph = ip_hdr(skb);
5439 iph->check = 0;
5440 iph->tot_len = htons(mss + hdr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005442 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005444 } else
5445 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5446 iph->daddr, 0,
5447 IPPROTO_TCP,
5448 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005450 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
5451 mss |= hdr_len << 9;
5452 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
5453 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005454 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 int tsflags;
5456
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005457 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 mss |= (tsflags << 11);
5459 }
5460 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005461 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 int tsflags;
5463
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005464 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 base_flags |= tsflags << 12;
5466 }
5467 }
5468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469#if TG3_VLAN_TAG_USED
5470 if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
5471 base_flags |= (TXD_FLAG_VLAN |
5472 (vlan_tx_tag_get(skb) << 16));
5473#endif
5474
David S. Miller90079ce2008-09-11 04:52:51 -07005475 if (skb_dma_map(&tp->pdev->dev, skb, DMA_TO_DEVICE)) {
5476 dev_kfree_skb(skb);
5477 goto out_unlock;
5478 }
5479
5480 sp = skb_shinfo(skb);
5481
Eric Dumazet042a53a2009-06-05 04:04:16 +00005482 mapping = sp->dma_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005484 tnapi->tx_buffers[entry].skb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485
5486 would_hit_hwbug = 0;
5487
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005488 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
5489 would_hit_hwbug = 1;
5490
Matt Carlson0e1406d2009-11-02 12:33:33 +00005491 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5492 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07005493 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00005494
5495 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5496 tg3_40bit_overflow_test(tp, mapping, len))
5497 would_hit_hwbug = 1;
5498
5499 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07005500 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005502 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5504
5505 entry = NEXT_TX(entry);
5506
5507 /* Now loop through additional data fragments, and queue them. */
5508 if (skb_shinfo(skb)->nr_frags > 0) {
5509 unsigned int i, last;
5510
5511 last = skb_shinfo(skb)->nr_frags - 1;
5512 for (i = 0; i <= last; i++) {
5513 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5514
5515 len = frag->size;
Eric Dumazet042a53a2009-06-05 04:04:16 +00005516 mapping = sp->dma_maps[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005518 tnapi->tx_buffers[entry].skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005520 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
5521 len <= 8)
5522 would_hit_hwbug = 1;
5523
Matt Carlson0e1406d2009-11-02 12:33:33 +00005524 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5525 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07005526 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527
Matt Carlson0e1406d2009-11-02 12:33:33 +00005528 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
5529 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08005530 would_hit_hwbug = 1;
5531
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005533 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 base_flags, (i == last)|(mss << 1));
5535 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005536 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 base_flags, (i == last));
5538
5539 entry = NEXT_TX(entry);
5540 }
5541 }
5542
5543 if (would_hit_hwbug) {
5544 u32 last_plus_one = entry;
5545 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546
Michael Chanc58ec932005-09-17 00:46:27 -07005547 start = entry - 1 - skb_shinfo(skb)->nr_frags;
5548 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549
5550 /* If the workaround fails due to memory/mapping
5551 * failure, silently drop this packet.
5552 */
Michael Chan72f2afb2006-03-06 19:28:35 -08005553 if (tigon3_dma_hwbug_workaround(tp, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07005554 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 goto out_unlock;
5556
5557 entry = start;
5558 }
5559
5560 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005561 tw32_tx_mbox(MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005563 tnapi->tx_prod = entry;
5564 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 netif_stop_queue(dev);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005566 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Michael Chan51b91462005-09-01 17:41:28 -07005567 netif_wake_queue(tp->dev);
5568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569
5570out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005571 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572
5573 return NETDEV_TX_OK;
5574}
5575
5576static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
5577 int new_mtu)
5578{
5579 dev->mtu = new_mtu;
5580
Michael Chanef7f5ec2005-07-25 12:32:25 -07005581 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07005582 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07005583 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
5584 ethtool_op_set_tso(dev, 0);
5585 }
5586 else
5587 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
5588 } else {
Michael Chana4e2b342005-10-26 15:46:52 -07005589 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chanef7f5ec2005-07-25 12:32:25 -07005590 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -07005591 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07005592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593}
5594
5595static int tg3_change_mtu(struct net_device *dev, int new_mtu)
5596{
5597 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005598 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599
5600 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
5601 return -EINVAL;
5602
5603 if (!netif_running(dev)) {
5604 /* We'll just catch it later when the
5605 * device is up'd.
5606 */
5607 tg3_set_mtu(dev, tp, new_mtu);
5608 return 0;
5609 }
5610
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005611 tg3_phy_stop(tp);
5612
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005614
5615 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616
Michael Chan944d9802005-05-29 14:57:48 -07005617 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618
5619 tg3_set_mtu(dev, tp, new_mtu);
5620
Michael Chanb9ec6c12006-07-25 16:37:27 -07005621 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622
Michael Chanb9ec6c12006-07-25 16:37:27 -07005623 if (!err)
5624 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
David S. Millerf47c11e2005-06-24 20:18:35 -07005626 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005628 if (!err)
5629 tg3_phy_start(tp);
5630
Michael Chanb9ec6c12006-07-25 16:37:27 -07005631 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632}
5633
Matt Carlson21f581a2009-08-28 14:00:25 +00005634static void tg3_rx_prodring_free(struct tg3 *tp,
5635 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 int i;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005638 struct ring_info *rxp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639
5640 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
Matt Carlson21f581a2009-08-28 14:00:25 +00005641 rxp = &tpr->rx_std_buffers[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642
5643 if (rxp->skb == NULL)
5644 continue;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005645
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 pci_unmap_single(tp->pdev,
5647 pci_unmap_addr(rxp, mapping),
Matt Carlson287be122009-08-28 13:58:46 +00005648 tp->rx_pkt_map_sz,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 PCI_DMA_FROMDEVICE);
5650 dev_kfree_skb_any(rxp->skb);
5651 rxp->skb = NULL;
5652 }
5653
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005654 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
5655 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
Matt Carlson21f581a2009-08-28 14:00:25 +00005656 rxp = &tpr->rx_jmb_buffers[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005658 if (rxp->skb == NULL)
5659 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005661 pci_unmap_single(tp->pdev,
5662 pci_unmap_addr(rxp, mapping),
5663 TG3_RX_JMB_MAP_SZ,
5664 PCI_DMA_FROMDEVICE);
5665 dev_kfree_skb_any(rxp->skb);
5666 rxp->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 }
5669}
5670
5671/* Initialize tx/rx rings for packet processing.
5672 *
5673 * The chip has been shut down and the driver detached from
5674 * the networking, so no interrupts or new tx packets will
5675 * end up in the driver. tp->{tx,}lock are held and thus
5676 * we may not sleep.
5677 */
Matt Carlson21f581a2009-08-28 14:00:25 +00005678static int tg3_rx_prodring_alloc(struct tg3 *tp,
5679 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680{
Matt Carlson287be122009-08-28 13:58:46 +00005681 u32 i, rx_pkt_dma_sz;
Matt Carlson17375d22009-08-28 14:02:18 +00005682 struct tg3_napi *tnapi = &tp->napi[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 /* Zero out all descriptors. */
Matt Carlson21f581a2009-08-28 14:00:25 +00005685 memset(tpr->rx_std, 0, TG3_RX_RING_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
Matt Carlson287be122009-08-28 13:58:46 +00005687 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07005688 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00005689 tp->dev->mtu > ETH_DATA_LEN)
5690 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
5691 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07005692
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 /* Initialize invariants of the rings, we only set this
5694 * stuff once. This works because the card does not
5695 * write into the rx buffer posting rings.
5696 */
5697 for (i = 0; i < TG3_RX_RING_SIZE; i++) {
5698 struct tg3_rx_buffer_desc *rxd;
5699
Matt Carlson21f581a2009-08-28 14:00:25 +00005700 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00005701 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
5703 rxd->opaque = (RXD_OPAQUE_RING_STD |
5704 (i << RXD_OPAQUE_INDEX_SHIFT));
5705 }
5706
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005707 /* Now allocate fresh SKBs for each rx ring. */
5708 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson17375d22009-08-28 14:02:18 +00005709 if (tg3_alloc_rx_skb(tnapi, RXD_OPAQUE_RING_STD, -1, i) < 0) {
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005710 printk(KERN_WARNING PFX
5711 "%s: Using a smaller RX standard ring, "
5712 "only %d out of %d buffers were allocated "
5713 "successfully.\n",
5714 tp->dev->name, i, tp->rx_pending);
5715 if (i == 0)
5716 goto initfail;
5717 tp->rx_pending = i;
5718 break;
5719 }
5720 }
5721
5722 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE))
5723 goto done;
5724
Matt Carlson21f581a2009-08-28 14:00:25 +00005725 memset(tpr->rx_jmb, 0, TG3_RX_JUMBO_RING_BYTES);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005726
Michael Chan0f893dc2005-07-25 12:30:38 -07005727 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
5729 struct tg3_rx_buffer_desc *rxd;
5730
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005731 rxd = &tpr->rx_jmb[i].std;
Matt Carlson287be122009-08-28 13:58:46 +00005732 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
5734 RXD_FLAG_JUMBO;
5735 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
5736 (i << RXD_OPAQUE_INDEX_SHIFT));
5737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Matt Carlson17375d22009-08-28 14:02:18 +00005740 if (tg3_alloc_rx_skb(tnapi, RXD_OPAQUE_RING_JUMBO,
Michael Chan32d8c572006-07-25 16:38:29 -07005741 -1, i) < 0) {
5742 printk(KERN_WARNING PFX
5743 "%s: Using a smaller RX jumbo ring, "
5744 "only %d out of %d buffers were "
5745 "allocated successfully.\n",
5746 tp->dev->name, i, tp->rx_jumbo_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005747 if (i == 0)
5748 goto initfail;
Michael Chan32d8c572006-07-25 16:38:29 -07005749 tp->rx_jumbo_pending = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 break;
Michael Chan32d8c572006-07-25 16:38:29 -07005751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 }
5753 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005754
5755done:
Michael Chan32d8c572006-07-25 16:38:29 -07005756 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005757
5758initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00005759 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005760 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761}
5762
Matt Carlson21f581a2009-08-28 14:00:25 +00005763static void tg3_rx_prodring_fini(struct tg3 *tp,
5764 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765{
Matt Carlson21f581a2009-08-28 14:00:25 +00005766 kfree(tpr->rx_std_buffers);
5767 tpr->rx_std_buffers = NULL;
5768 kfree(tpr->rx_jmb_buffers);
5769 tpr->rx_jmb_buffers = NULL;
5770 if (tpr->rx_std) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 pci_free_consistent(tp->pdev, TG3_RX_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00005772 tpr->rx_std, tpr->rx_std_mapping);
5773 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 }
Matt Carlson21f581a2009-08-28 14:00:25 +00005775 if (tpr->rx_jmb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 pci_free_consistent(tp->pdev, TG3_RX_JUMBO_RING_BYTES,
Matt Carlson21f581a2009-08-28 14:00:25 +00005777 tpr->rx_jmb, tpr->rx_jmb_mapping);
5778 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005780}
5781
Matt Carlson21f581a2009-08-28 14:00:25 +00005782static int tg3_rx_prodring_init(struct tg3 *tp,
5783 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005784{
Matt Carlson21f581a2009-08-28 14:00:25 +00005785 tpr->rx_std_buffers = kzalloc(sizeof(struct ring_info) *
5786 TG3_RX_RING_SIZE, GFP_KERNEL);
5787 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005788 return -ENOMEM;
5789
Matt Carlson21f581a2009-08-28 14:00:25 +00005790 tpr->rx_std = pci_alloc_consistent(tp->pdev, TG3_RX_RING_BYTES,
5791 &tpr->rx_std_mapping);
5792 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005793 goto err_out;
5794
5795 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlson21f581a2009-08-28 14:00:25 +00005796 tpr->rx_jmb_buffers = kzalloc(sizeof(struct ring_info) *
5797 TG3_RX_JUMBO_RING_SIZE,
5798 GFP_KERNEL);
5799 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005800 goto err_out;
5801
Matt Carlson21f581a2009-08-28 14:00:25 +00005802 tpr->rx_jmb = pci_alloc_consistent(tp->pdev,
5803 TG3_RX_JUMBO_RING_BYTES,
5804 &tpr->rx_jmb_mapping);
5805 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005806 goto err_out;
5807 }
5808
5809 return 0;
5810
5811err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00005812 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005813 return -ENOMEM;
5814}
5815
5816/* Free up pending packets in all rx/tx rings.
5817 *
5818 * The chip has been shut down and the driver detached from
5819 * the networking, so no interrupts or new tx packets will
5820 * end up in the driver. tp->{tx,}lock is not held and we are not
5821 * in an interrupt context and thus may sleep.
5822 */
5823static void tg3_free_rings(struct tg3 *tp)
5824{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005825 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005826
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005827 for (j = 0; j < tp->irq_cnt; j++) {
5828 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005829
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005830 if (!tnapi->tx_buffers)
5831 continue;
5832
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005833 for (i = 0; i < TG3_TX_RING_SIZE; ) {
5834 struct tx_ring_info *txp;
5835 struct sk_buff *skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005836
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005837 txp = &tnapi->tx_buffers[i];
5838 skb = txp->skb;
5839
5840 if (skb == NULL) {
5841 i++;
5842 continue;
5843 }
5844
5845 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
5846
5847 txp->skb = NULL;
5848
5849 i += skb_shinfo(skb)->nr_frags + 1;
5850
5851 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005852 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005853 }
5854
Matt Carlson21f581a2009-08-28 14:00:25 +00005855 tg3_rx_prodring_free(tp, &tp->prodring[0]);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005856}
5857
5858/* Initialize tx/rx rings for packet processing.
5859 *
5860 * The chip has been shut down and the driver detached from
5861 * the networking, so no interrupts or new tx packets will
5862 * end up in the driver. tp->{tx,}lock are held and thus
5863 * we may not sleep.
5864 */
5865static int tg3_init_rings(struct tg3 *tp)
5866{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005867 int i;
Matt Carlson72334482009-08-28 14:03:01 +00005868
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005869 /* Free up all the SKBs. */
5870 tg3_free_rings(tp);
5871
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005872 for (i = 0; i < tp->irq_cnt; i++) {
5873 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005874
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005875 tnapi->last_tag = 0;
5876 tnapi->last_irq_tag = 0;
5877 tnapi->hw_status->status = 0;
5878 tnapi->hw_status->status_tag = 0;
5879 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
5880
5881 tnapi->tx_prod = 0;
5882 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005883 if (tnapi->tx_ring)
5884 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005885
5886 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005887 if (tnapi->rx_rcb)
5888 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005889 }
Matt Carlson72334482009-08-28 14:03:01 +00005890
Matt Carlson21f581a2009-08-28 14:00:25 +00005891 return tg3_rx_prodring_alloc(tp, &tp->prodring[0]);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00005892}
5893
5894/*
5895 * Must not be invoked with interrupt sources disabled and
5896 * the hardware shutdown down.
5897 */
5898static void tg3_free_consistent(struct tg3 *tp)
5899{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005900 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00005901
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005902 for (i = 0; i < tp->irq_cnt; i++) {
5903 struct tg3_napi *tnapi = &tp->napi[i];
5904
5905 if (tnapi->tx_ring) {
5906 pci_free_consistent(tp->pdev, TG3_TX_RING_BYTES,
5907 tnapi->tx_ring, tnapi->tx_desc_mapping);
5908 tnapi->tx_ring = NULL;
5909 }
5910
5911 kfree(tnapi->tx_buffers);
5912 tnapi->tx_buffers = NULL;
5913
5914 if (tnapi->rx_rcb) {
5915 pci_free_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
5916 tnapi->rx_rcb,
5917 tnapi->rx_rcb_mapping);
5918 tnapi->rx_rcb = NULL;
5919 }
5920
5921 if (tnapi->hw_status) {
5922 pci_free_consistent(tp->pdev, TG3_HW_STATUS_SIZE,
5923 tnapi->hw_status,
5924 tnapi->status_mapping);
5925 tnapi->hw_status = NULL;
5926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005928
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 if (tp->hw_stats) {
5930 pci_free_consistent(tp->pdev, sizeof(struct tg3_hw_stats),
5931 tp->hw_stats, tp->stats_mapping);
5932 tp->hw_stats = NULL;
5933 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005934
Matt Carlson21f581a2009-08-28 14:00:25 +00005935 tg3_rx_prodring_fini(tp, &tp->prodring[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936}
5937
5938/*
5939 * Must not be invoked with interrupt sources disabled and
5940 * the hardware shutdown down. Can sleep.
5941 */
5942static int tg3_alloc_consistent(struct tg3 *tp)
5943{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005944 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00005945
Matt Carlson21f581a2009-08-28 14:00:25 +00005946 if (tg3_rx_prodring_init(tp, &tp->prodring[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 return -ENOMEM;
5948
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 tp->hw_stats = pci_alloc_consistent(tp->pdev,
5950 sizeof(struct tg3_hw_stats),
5951 &tp->stats_mapping);
5952 if (!tp->hw_stats)
5953 goto err_out;
5954
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
5956
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005957 for (i = 0; i < tp->irq_cnt; i++) {
5958 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005959 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005960
5961 tnapi->hw_status = pci_alloc_consistent(tp->pdev,
5962 TG3_HW_STATUS_SIZE,
5963 &tnapi->status_mapping);
5964 if (!tnapi->hw_status)
5965 goto err_out;
5966
5967 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005968 sblk = tnapi->hw_status;
5969
5970 /*
5971 * When RSS is enabled, the status block format changes
5972 * slightly. The "rx_jumbo_consumer", "reserved",
5973 * and "rx_mini_consumer" members get mapped to the
5974 * other three rx return ring producer indexes.
5975 */
5976 switch (i) {
5977 default:
5978 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
5979 break;
5980 case 2:
5981 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
5982 break;
5983 case 3:
5984 tnapi->rx_rcb_prod_idx = &sblk->reserved;
5985 break;
5986 case 4:
5987 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
5988 break;
5989 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005990
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005991 /*
5992 * If multivector RSS is enabled, vector 0 does not handle
5993 * rx or tx interrupts. Don't allocate any resources for it.
5994 */
5995 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
5996 continue;
5997
Matt Carlsonf77a6a82009-09-01 13:04:37 +00005998 tnapi->rx_rcb = pci_alloc_consistent(tp->pdev,
5999 TG3_RX_RCB_RING_BYTES(tp),
6000 &tnapi->rx_rcb_mapping);
6001 if (!tnapi->rx_rcb)
6002 goto err_out;
6003
6004 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
6005
6006 tnapi->tx_buffers = kzalloc(sizeof(struct tx_ring_info) *
6007 TG3_TX_RING_SIZE, GFP_KERNEL);
6008 if (!tnapi->tx_buffers)
6009 goto err_out;
6010
6011 tnapi->tx_ring = pci_alloc_consistent(tp->pdev,
6012 TG3_TX_RING_BYTES,
6013 &tnapi->tx_desc_mapping);
6014 if (!tnapi->tx_ring)
6015 goto err_out;
6016 }
6017
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 return 0;
6019
6020err_out:
6021 tg3_free_consistent(tp);
6022 return -ENOMEM;
6023}
6024
6025#define MAX_WAIT_CNT 1000
6026
6027/* To stop a block, clear the enable bit and poll till it
6028 * clears. tp->lock is held.
6029 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006030static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031{
6032 unsigned int i;
6033 u32 val;
6034
6035 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6036 switch (ofs) {
6037 case RCVLSC_MODE:
6038 case DMAC_MODE:
6039 case MBFREE_MODE:
6040 case BUFMGR_MODE:
6041 case MEMARB_MODE:
6042 /* We can't enable/disable these bits of the
6043 * 5705/5750, just say success.
6044 */
6045 return 0;
6046
6047 default:
6048 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 }
6051
6052 val = tr32(ofs);
6053 val &= ~enable_bit;
6054 tw32_f(ofs, val);
6055
6056 for (i = 0; i < MAX_WAIT_CNT; i++) {
6057 udelay(100);
6058 val = tr32(ofs);
6059 if ((val & enable_bit) == 0)
6060 break;
6061 }
6062
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006063 if (i == MAX_WAIT_CNT && !silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 printk(KERN_ERR PFX "tg3_stop_block timed out, "
6065 "ofs=%lx enable_bit=%x\n",
6066 ofs, enable_bit);
6067 return -ENODEV;
6068 }
6069
6070 return 0;
6071}
6072
6073/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006074static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075{
6076 int i, err;
6077
6078 tg3_disable_ints(tp);
6079
6080 tp->rx_mode &= ~RX_MODE_ENABLE;
6081 tw32_f(MAC_RX_MODE, tp->rx_mode);
6082 udelay(10);
6083
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006084 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6085 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6086 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6087 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6088 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6089 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006091 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6092 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6093 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6094 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6095 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6096 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6097 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098
6099 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6100 tw32_f(MAC_MODE, tp->mac_mode);
6101 udelay(40);
6102
6103 tp->tx_mode &= ~TX_MODE_ENABLE;
6104 tw32_f(MAC_TX_MODE, tp->tx_mode);
6105
6106 for (i = 0; i < MAX_WAIT_CNT; i++) {
6107 udelay(100);
6108 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6109 break;
6110 }
6111 if (i >= MAX_WAIT_CNT) {
6112 printk(KERN_ERR PFX "tg3_abort_hw timed out for %s, "
6113 "TX_MODE_ENABLE will not clear MAC_TX_MODE=%08x\n",
6114 tp->dev->name, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006115 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 }
6117
Michael Chane6de8ad2005-05-05 14:42:41 -07006118 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006119 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6120 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121
6122 tw32(FTQ_RESET, 0xffffffff);
6123 tw32(FTQ_RESET, 0x00000000);
6124
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006125 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6126 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006128 for (i = 0; i < tp->irq_cnt; i++) {
6129 struct tg3_napi *tnapi = &tp->napi[i];
6130 if (tnapi->hw_status)
6131 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 if (tp->hw_stats)
6134 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6135
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 return err;
6137}
6138
Matt Carlson0d3031d2007-10-10 18:02:43 -07006139static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6140{
6141 int i;
6142 u32 apedata;
6143
6144 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6145 if (apedata != APE_SEG_SIG_MAGIC)
6146 return;
6147
6148 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006149 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006150 return;
6151
6152 /* Wait for up to 1 millisecond for APE to service previous event. */
6153 for (i = 0; i < 10; i++) {
6154 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6155 return;
6156
6157 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6158
6159 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6160 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6161 event | APE_EVENT_STATUS_EVENT_PENDING);
6162
6163 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6164
6165 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6166 break;
6167
6168 udelay(100);
6169 }
6170
6171 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6172 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6173}
6174
6175static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6176{
6177 u32 event;
6178 u32 apedata;
6179
6180 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6181 return;
6182
6183 switch (kind) {
6184 case RESET_KIND_INIT:
6185 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6186 APE_HOST_SEG_SIG_MAGIC);
6187 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6188 APE_HOST_SEG_LEN_MAGIC);
6189 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6190 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6191 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
6192 APE_HOST_DRIVER_ID_MAGIC);
6193 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6194 APE_HOST_BEHAV_NO_PHYLOCK);
6195
6196 event = APE_EVENT_STATUS_STATE_START;
6197 break;
6198 case RESET_KIND_SHUTDOWN:
Matt Carlsonb2aee152008-11-03 16:51:11 -08006199 /* With the interface we are currently using,
6200 * APE does not track driver state. Wiping
6201 * out the HOST SEGMENT SIGNATURE forces
6202 * the APE to assume OS absent status.
6203 */
6204 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
6205
Matt Carlson0d3031d2007-10-10 18:02:43 -07006206 event = APE_EVENT_STATUS_STATE_UNLOAD;
6207 break;
6208 case RESET_KIND_SUSPEND:
6209 event = APE_EVENT_STATUS_STATE_SUSPEND;
6210 break;
6211 default:
6212 return;
6213 }
6214
6215 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6216
6217 tg3_ape_send_event(tp, event);
6218}
6219
Michael Chane6af3012005-04-21 17:12:05 -07006220/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6222{
David S. Millerf49639e2006-06-09 11:58:36 -07006223 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6224 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225
6226 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6227 switch (kind) {
6228 case RESET_KIND_INIT:
6229 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6230 DRV_STATE_START);
6231 break;
6232
6233 case RESET_KIND_SHUTDOWN:
6234 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6235 DRV_STATE_UNLOAD);
6236 break;
6237
6238 case RESET_KIND_SUSPEND:
6239 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6240 DRV_STATE_SUSPEND);
6241 break;
6242
6243 default:
6244 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006247
6248 if (kind == RESET_KIND_INIT ||
6249 kind == RESET_KIND_SUSPEND)
6250 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251}
6252
6253/* tp->lock is held. */
6254static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6255{
6256 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6257 switch (kind) {
6258 case RESET_KIND_INIT:
6259 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6260 DRV_STATE_START_DONE);
6261 break;
6262
6263 case RESET_KIND_SHUTDOWN:
6264 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6265 DRV_STATE_UNLOAD_DONE);
6266 break;
6267
6268 default:
6269 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006272
6273 if (kind == RESET_KIND_SHUTDOWN)
6274 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275}
6276
6277/* tp->lock is held. */
6278static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6279{
6280 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
6281 switch (kind) {
6282 case RESET_KIND_INIT:
6283 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6284 DRV_STATE_START);
6285 break;
6286
6287 case RESET_KIND_SHUTDOWN:
6288 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6289 DRV_STATE_UNLOAD);
6290 break;
6291
6292 case RESET_KIND_SUSPEND:
6293 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6294 DRV_STATE_SUSPEND);
6295 break;
6296
6297 default:
6298 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 }
6301}
6302
Michael Chan7a6f4362006-09-27 16:03:31 -07006303static int tg3_poll_fw(struct tg3 *tp)
6304{
6305 int i;
6306 u32 val;
6307
Michael Chanb5d37722006-09-27 16:06:21 -07006308 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006309 /* Wait up to 20ms for init done. */
6310 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006311 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6312 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006313 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006314 }
6315 return -ENODEV;
6316 }
6317
Michael Chan7a6f4362006-09-27 16:03:31 -07006318 /* Wait for firmware initialization to complete. */
6319 for (i = 0; i < 100000; i++) {
6320 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6321 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6322 break;
6323 udelay(10);
6324 }
6325
6326 /* Chip might not be fitted with firmware. Some Sun onboard
6327 * parts are configured like that. So don't signal the timeout
6328 * of the above loop as an error, but do report the lack of
6329 * running firmware once.
6330 */
6331 if (i >= 100000 &&
6332 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
6333 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
6334
6335 printk(KERN_INFO PFX "%s: No firmware running.\n",
6336 tp->dev->name);
6337 }
6338
6339 return 0;
6340}
6341
Michael Chanee6a99b2007-07-18 21:49:10 -07006342/* Save PCI command register before chip reset */
6343static void tg3_save_pci_state(struct tg3 *tp)
6344{
Matt Carlson8a6eac92007-10-21 16:17:55 -07006345 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006346}
6347
6348/* Restore PCI state after chip reset */
6349static void tg3_restore_pci_state(struct tg3 *tp)
6350{
6351 u32 val;
6352
6353 /* Re-enable indirect register accesses. */
6354 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
6355 tp->misc_host_ctrl);
6356
6357 /* Set MAX PCI retry to zero. */
6358 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
6359 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
6360 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
6361 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006362 /* Allow reads and writes to the APE register and memory space. */
6363 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
6364 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
6365 PCISTATE_ALLOW_APE_SHMEM_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07006366 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
6367
Matt Carlson8a6eac92007-10-21 16:17:55 -07006368 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07006369
Matt Carlsonfcb389d2008-11-03 16:55:44 -08006370 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
6371 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
6372 pcie_set_readrq(tp->pdev, 4096);
6373 else {
6374 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
6375 tp->pci_cacheline_sz);
6376 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
6377 tp->pci_lat_timer);
6378 }
Michael Chan114342f2007-10-15 02:12:26 -07006379 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08006380
Michael Chanee6a99b2007-07-18 21:49:10 -07006381 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08006382 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07006383 u16 pcix_cmd;
6384
6385 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6386 &pcix_cmd);
6387 pcix_cmd &= ~PCI_X_CMD_ERO;
6388 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
6389 pcix_cmd);
6390 }
Michael Chanee6a99b2007-07-18 21:49:10 -07006391
6392 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07006393
6394 /* Chip reset on 5780 will reset MSI enable bit,
6395 * so need to restore it.
6396 */
6397 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
6398 u16 ctrl;
6399
6400 pci_read_config_word(tp->pdev,
6401 tp->msi_cap + PCI_MSI_FLAGS,
6402 &ctrl);
6403 pci_write_config_word(tp->pdev,
6404 tp->msi_cap + PCI_MSI_FLAGS,
6405 ctrl | PCI_MSI_FLAGS_ENABLE);
6406 val = tr32(MSGINT_MODE);
6407 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
6408 }
6409 }
6410}
6411
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412static void tg3_stop_fw(struct tg3 *);
6413
6414/* tp->lock is held. */
6415static int tg3_chip_reset(struct tg3 *tp)
6416{
6417 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07006418 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00006419 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420
David S. Millerf49639e2006-06-09 11:58:36 -07006421 tg3_nvram_lock(tp);
6422
Matt Carlson77b483f2008-08-15 14:07:24 -07006423 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
6424
David S. Millerf49639e2006-06-09 11:58:36 -07006425 /* No matching tg3_nvram_unlock() after this because
6426 * chip reset below will undo the nvram lock.
6427 */
6428 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429
Michael Chanee6a99b2007-07-18 21:49:10 -07006430 /* GRC_MISC_CFG core clock reset will clear the memory
6431 * enable bit in PCI register 4 and the MSI enable bit
6432 * on some chips, so we save relevant registers here.
6433 */
6434 tg3_save_pci_state(tp);
6435
Michael Chand9ab5ad12006-03-20 22:27:35 -08006436 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08006437 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08006438 tw32(GRC_FASTBOOT_PC, 0);
6439
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 /*
6441 * We must avoid the readl() that normally takes place.
6442 * It locks machines, causes machine checks, and other
6443 * fun things. So, temporarily disable the 5701
6444 * hardware workaround, while we do the reset.
6445 */
Michael Chan1ee582d2005-08-09 20:16:46 -07006446 write_op = tp->write32;
6447 if (write_op == tg3_write_flush_reg32)
6448 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449
Michael Chand18edcb2007-03-24 20:57:11 -07006450 /* Prevent the irq handler from reading or writing PCI registers
6451 * during chip reset when the memory enable bit in the PCI command
6452 * register may be cleared. The chip does not generate interrupt
6453 * at this time, but the irq handler may still be called due to irq
6454 * sharing or irqpoll.
6455 */
6456 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006457 for (i = 0; i < tp->irq_cnt; i++) {
6458 struct tg3_napi *tnapi = &tp->napi[i];
6459 if (tnapi->hw_status) {
6460 tnapi->hw_status->status = 0;
6461 tnapi->hw_status->status_tag = 0;
6462 }
6463 tnapi->last_tag = 0;
6464 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07006465 }
Michael Chand18edcb2007-03-24 20:57:11 -07006466 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00006467
6468 for (i = 0; i < tp->irq_cnt; i++)
6469 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07006470
Matt Carlson255ca312009-08-25 10:07:27 +00006471 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6472 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
6473 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
6474 }
6475
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 /* do the reset */
6477 val = GRC_MISC_CFG_CORECLK_RESET;
6478
6479 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
6480 if (tr32(0x7e2c) == 0x60) {
6481 tw32(0x7e2c, 0x20);
6482 }
6483 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
6484 tw32(GRC_MISC_CFG, (1 << 29));
6485 val |= (1 << 29);
6486 }
6487 }
6488
Michael Chanb5d37722006-09-27 16:06:21 -07006489 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6490 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
6491 tw32(GRC_VCPU_EXT_CTRL,
6492 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
6493 }
6494
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6496 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
6497 tw32(GRC_MISC_CFG, val);
6498
Michael Chan1ee582d2005-08-09 20:16:46 -07006499 /* restore 5701 hardware bug workaround write method */
6500 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501
6502 /* Unfortunately, we have to delay before the PCI read back.
6503 * Some 575X chips even will not respond to a PCI cfg access
6504 * when the reset command is given to the chip.
6505 *
6506 * How do these hardware designers expect things to work
6507 * properly if the PCI write is posted for a long period
6508 * of time? It is always necessary to have some method by
6509 * which a register read back can occur to push the write
6510 * out which does the reset.
6511 *
6512 * For most tg3 variants the trick below was working.
6513 * Ho hum...
6514 */
6515 udelay(120);
6516
6517 /* Flush PCI posted writes. The normal MMIO registers
6518 * are inaccessible at this time so this is the only
6519 * way to make this reliably (actually, this is no longer
6520 * the case, see above). I tried to use indirect
6521 * register read/write but this upset some 5701 variants.
6522 */
6523 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
6524
6525 udelay(120);
6526
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006527 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00006528 u16 val16;
6529
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
6531 int i;
6532 u32 cfg_val;
6533
6534 /* Wait for link training to complete. */
6535 for (i = 0; i < 5000; i++)
6536 udelay(100);
6537
6538 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
6539 pci_write_config_dword(tp->pdev, 0xc4,
6540 cfg_val | (1 << 15));
6541 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006542
Matt Carlsone7126992009-08-25 10:08:16 +00006543 /* Clear the "no snoop" and "relaxed ordering" bits. */
6544 pci_read_config_word(tp->pdev,
6545 tp->pcie_cap + PCI_EXP_DEVCTL,
6546 &val16);
6547 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
6548 PCI_EXP_DEVCTL_NOSNOOP_EN);
6549 /*
6550 * Older PCIe devices only support the 128 byte
6551 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006552 */
Matt Carlsone7126992009-08-25 10:08:16 +00006553 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
6554 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784))
6555 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006556 pci_write_config_word(tp->pdev,
6557 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00006558 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08006559
6560 pcie_set_readrq(tp->pdev, 4096);
6561
6562 /* Clear error status */
6563 pci_write_config_word(tp->pdev,
6564 tp->pcie_cap + PCI_EXP_DEVSTA,
6565 PCI_EXP_DEVSTA_CED |
6566 PCI_EXP_DEVSTA_NFED |
6567 PCI_EXP_DEVSTA_FED |
6568 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569 }
6570
Michael Chanee6a99b2007-07-18 21:49:10 -07006571 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572
Michael Chand18edcb2007-03-24 20:57:11 -07006573 tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
6574
Michael Chanee6a99b2007-07-18 21:49:10 -07006575 val = 0;
6576 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07006577 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07006578 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579
6580 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
6581 tg3_stop_fw(tp);
6582 tw32(0x5000, 0x400);
6583 }
6584
6585 tw32(GRC_MODE, tp->grc_mode);
6586
6587 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01006588 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589
6590 tw32(0xc4, val | (1 << 15));
6591 }
6592
6593 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
6594 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
6595 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
6596 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
6597 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
6598 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
6599 }
6600
6601 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
6602 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
6603 tw32_f(MAC_MODE, tp->mac_mode);
Michael Chan747e8f82005-07-25 12:33:22 -07006604 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
6605 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
6606 tw32_f(MAC_MODE, tp->mac_mode);
Matt Carlson3bda1252008-08-15 14:08:22 -07006607 } else if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
6608 tp->mac_mode &= (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
6609 if (tp->mac_mode & MAC_MODE_APE_TX_EN)
6610 tp->mac_mode |= MAC_MODE_TDE_ENABLE;
6611 tw32_f(MAC_MODE, tp->mac_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 } else
6613 tw32_f(MAC_MODE, 0);
6614 udelay(40);
6615
Matt Carlson77b483f2008-08-15 14:07:24 -07006616 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
6617
Michael Chan7a6f4362006-09-27 16:03:31 -07006618 err = tg3_poll_fw(tp);
6619 if (err)
6620 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621
Matt Carlson0a9140c2009-08-28 12:27:50 +00006622 tg3_mdio_start(tp);
6623
Matt Carlson52cdf852009-11-02 14:25:06 +00006624 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
6625 u8 phy_addr;
6626
6627 phy_addr = tp->phy_addr;
6628 tp->phy_addr = TG3_PHY_PCIE_ADDR;
6629
6630 tg3_writephy(tp, TG3_PCIEPHY_BLOCK_ADDR,
6631 TG3_PCIEPHY_TXB_BLK << TG3_PCIEPHY_BLOCK_SHIFT);
6632 val = TG3_PCIEPHY_TX0CTRL1_TXOCM | TG3_PCIEPHY_TX0CTRL1_RDCTL |
6633 TG3_PCIEPHY_TX0CTRL1_TXCMV | TG3_PCIEPHY_TX0CTRL1_TKSEL |
6634 TG3_PCIEPHY_TX0CTRL1_NB_EN;
6635 tg3_writephy(tp, TG3_PCIEPHY_TX0CTRL1, val);
6636 udelay(10);
6637
6638 tg3_writephy(tp, TG3_PCIEPHY_BLOCK_ADDR,
6639 TG3_PCIEPHY_XGXS_BLK1 << TG3_PCIEPHY_BLOCK_SHIFT);
6640 val = TG3_PCIEPHY_PWRMGMT4_LOWPWR_EN |
6641 TG3_PCIEPHY_PWRMGMT4_L1PLLPD_EN;
6642 tg3_writephy(tp, TG3_PCIEPHY_PWRMGMT4, val);
6643 udelay(10);
6644
6645 tp->phy_addr = phy_addr;
6646 }
6647
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00006649 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
6650 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
6651 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01006652 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653
6654 tw32(0x7c00, val | (1 << 25));
6655 }
6656
6657 /* Reprobe ASF enable state. */
6658 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
6659 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
6660 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
6661 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
6662 u32 nic_cfg;
6663
6664 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
6665 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
6666 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07006667 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07006668 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
6670 }
6671 }
6672
6673 return 0;
6674}
6675
6676/* tp->lock is held. */
6677static void tg3_stop_fw(struct tg3 *tp)
6678{
Matt Carlson0d3031d2007-10-10 18:02:43 -07006679 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
6680 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07006681 /* Wait for RX cpu to ACK the previous event. */
6682 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683
6684 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07006685
6686 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687
Matt Carlson7c5026a2008-05-02 16:49:29 -07006688 /* Wait for RX cpu to ACK this event. */
6689 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 }
6691}
6692
6693/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07006694static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695{
6696 int err;
6697
6698 tg3_stop_fw(tp);
6699
Michael Chan944d9802005-05-29 14:57:48 -07006700 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006702 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 err = tg3_chip_reset(tp);
6704
Matt Carlsondaba2a62009-04-20 06:58:52 +00006705 __tg3_set_mac_addr(tp, 0);
6706
Michael Chan944d9802005-05-29 14:57:48 -07006707 tg3_write_sig_legacy(tp, kind);
6708 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709
6710 if (err)
6711 return err;
6712
6713 return 0;
6714}
6715
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716#define RX_CPU_SCRATCH_BASE 0x30000
6717#define RX_CPU_SCRATCH_SIZE 0x04000
6718#define TX_CPU_SCRATCH_BASE 0x34000
6719#define TX_CPU_SCRATCH_SIZE 0x04000
6720
6721/* tp->lock is held. */
6722static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
6723{
6724 int i;
6725
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02006726 BUG_ON(offset == TX_CPU_BASE &&
6727 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728
Michael Chanb5d37722006-09-27 16:06:21 -07006729 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
6730 u32 val = tr32(GRC_VCPU_EXT_CTRL);
6731
6732 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
6733 return 0;
6734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735 if (offset == RX_CPU_BASE) {
6736 for (i = 0; i < 10000; i++) {
6737 tw32(offset + CPU_STATE, 0xffffffff);
6738 tw32(offset + CPU_MODE, CPU_MODE_HALT);
6739 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
6740 break;
6741 }
6742
6743 tw32(offset + CPU_STATE, 0xffffffff);
6744 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
6745 udelay(10);
6746 } else {
6747 for (i = 0; i < 10000; i++) {
6748 tw32(offset + CPU_STATE, 0xffffffff);
6749 tw32(offset + CPU_MODE, CPU_MODE_HALT);
6750 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
6751 break;
6752 }
6753 }
6754
6755 if (i >= 10000) {
6756 printk(KERN_ERR PFX "tg3_reset_cpu timed out for %s, "
6757 "and %s CPU\n",
6758 tp->dev->name,
6759 (offset == RX_CPU_BASE ? "RX" : "TX"));
6760 return -ENODEV;
6761 }
Michael Chanec41c7d2006-01-17 02:40:55 -08006762
6763 /* Clear firmware's nvram arbitration. */
6764 if (tp->tg3_flags & TG3_FLAG_NVRAM)
6765 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766 return 0;
6767}
6768
6769struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006770 unsigned int fw_base;
6771 unsigned int fw_len;
6772 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773};
6774
6775/* tp->lock is held. */
6776static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
6777 int cpu_scratch_size, struct fw_info *info)
6778{
Michael Chanec41c7d2006-01-17 02:40:55 -08006779 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780 void (*write_op)(struct tg3 *, u32, u32);
6781
6782 if (cpu_base == TX_CPU_BASE &&
6783 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
6784 printk(KERN_ERR PFX "tg3_load_firmware_cpu: Trying to load "
6785 "TX cpu firmware on %s which is 5705.\n",
6786 tp->dev->name);
6787 return -EINVAL;
6788 }
6789
6790 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
6791 write_op = tg3_write_mem;
6792 else
6793 write_op = tg3_write_indirect_reg32;
6794
Michael Chan1b628152005-05-29 14:59:49 -07006795 /* It is possible that bootcode is still loading at this point.
6796 * Get the nvram lock first before halting the cpu.
6797 */
Michael Chanec41c7d2006-01-17 02:40:55 -08006798 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08006800 if (!lock_err)
6801 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802 if (err)
6803 goto out;
6804
6805 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
6806 write_op(tp, cpu_scratch_base + i, 0);
6807 tw32(cpu_base + CPU_STATE, 0xffffffff);
6808 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006809 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006811 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006813 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814
6815 err = 0;
6816
6817out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818 return err;
6819}
6820
6821/* tp->lock is held. */
6822static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
6823{
6824 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006825 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 int err, i;
6827
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006828 fw_data = (void *)tp->fw->data;
6829
6830 /* Firmware blob starts with version numbers, followed by
6831 start address and length. We are setting complete length.
6832 length = end_address_of_bss - start_address_of_text.
6833 Remainder is the blob to be loaded contiguously
6834 from start address. */
6835
6836 info.fw_base = be32_to_cpu(fw_data[1]);
6837 info.fw_len = tp->fw->size - 12;
6838 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839
6840 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
6841 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
6842 &info);
6843 if (err)
6844 return err;
6845
6846 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
6847 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
6848 &info);
6849 if (err)
6850 return err;
6851
6852 /* Now startup only the RX cpu. */
6853 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006854 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855
6856 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006857 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858 break;
6859 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
6860 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006861 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 udelay(1000);
6863 }
6864 if (i >= 5) {
6865 printk(KERN_ERR PFX "tg3_load_firmware fails for %s "
6866 "to set RX CPU PC, is %08x should be %08x\n",
6867 tp->dev->name, tr32(RX_CPU_BASE + CPU_PC),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006868 info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869 return -ENODEV;
6870 }
6871 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
6872 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
6873
6874 return 0;
6875}
6876
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878
6879/* tp->lock is held. */
6880static int tg3_load_tso_firmware(struct tg3 *tp)
6881{
6882 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006883 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
6885 int err, i;
6886
6887 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
6888 return 0;
6889
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006890 fw_data = (void *)tp->fw->data;
6891
6892 /* Firmware blob starts with version numbers, followed by
6893 start address and length. We are setting complete length.
6894 length = end_address_of_bss - start_address_of_text.
6895 Remainder is the blob to be loaded contiguously
6896 from start address. */
6897
6898 info.fw_base = be32_to_cpu(fw_data[1]);
6899 cpu_scratch_size = tp->fw_len;
6900 info.fw_len = tp->fw->size - 12;
6901 info.fw_data = &fw_data[3];
6902
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904 cpu_base = RX_CPU_BASE;
6905 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907 cpu_base = TX_CPU_BASE;
6908 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
6909 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
6910 }
6911
6912 err = tg3_load_firmware_cpu(tp, cpu_base,
6913 cpu_scratch_base, cpu_scratch_size,
6914 &info);
6915 if (err)
6916 return err;
6917
6918 /* Now startup the cpu. */
6919 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006920 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921
6922 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006923 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924 break;
6925 tw32(cpu_base + CPU_STATE, 0xffffffff);
6926 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006927 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 udelay(1000);
6929 }
6930 if (i >= 5) {
6931 printk(KERN_ERR PFX "tg3_load_tso_firmware fails for %s "
6932 "to set CPU PC, is %08x should be %08x\n",
6933 tp->dev->name, tr32(cpu_base + CPU_PC),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08006934 info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935 return -ENODEV;
6936 }
6937 tw32(cpu_base + CPU_STATE, 0xffffffff);
6938 tw32_f(cpu_base + CPU_MODE, 0x00000000);
6939 return 0;
6940}
6941
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943static int tg3_set_mac_addr(struct net_device *dev, void *p)
6944{
6945 struct tg3 *tp = netdev_priv(dev);
6946 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07006947 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948
Michael Chanf9804dd2005-09-27 12:13:10 -07006949 if (!is_valid_ether_addr(addr->sa_data))
6950 return -EINVAL;
6951
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
6953
Michael Chane75f7c92006-03-20 21:33:26 -08006954 if (!netif_running(dev))
6955 return 0;
6956
Michael Chan58712ef2006-04-29 18:58:01 -07006957 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07006958 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07006959
Michael Chan986e0ae2007-05-05 12:10:20 -07006960 addr0_high = tr32(MAC_ADDR_0_HIGH);
6961 addr0_low = tr32(MAC_ADDR_0_LOW);
6962 addr1_high = tr32(MAC_ADDR_1_HIGH);
6963 addr1_low = tr32(MAC_ADDR_1_LOW);
6964
6965 /* Skip MAC addr 1 if ASF is using it. */
6966 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
6967 !(addr1_high == 0 && addr1_low == 0))
6968 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07006969 }
Michael Chan986e0ae2007-05-05 12:10:20 -07006970 spin_lock_bh(&tp->lock);
6971 __tg3_set_mac_addr(tp, skip_mac_1);
6972 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973
Michael Chanb9ec6c12006-07-25 16:37:27 -07006974 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975}
6976
6977/* tp->lock is held. */
6978static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
6979 dma_addr_t mapping, u32 maxlen_flags,
6980 u32 nic_addr)
6981{
6982 tg3_write_mem(tp,
6983 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
6984 ((u64) mapping >> 32));
6985 tg3_write_mem(tp,
6986 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
6987 ((u64) mapping & 0xffffffff));
6988 tg3_write_mem(tp,
6989 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
6990 maxlen_flags);
6991
6992 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
6993 tg3_write_mem(tp,
6994 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
6995 nic_addr);
6996}
6997
6998static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07006999static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007000{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007001 int i;
7002
7003 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
7004 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7005 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7006 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
7007
7008 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7009 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7010 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7011 } else {
7012 tw32(HOSTCC_TXCOL_TICKS, 0);
7013 tw32(HOSTCC_TXMAX_FRAMES, 0);
7014 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
7015
7016 tw32(HOSTCC_RXCOL_TICKS, 0);
7017 tw32(HOSTCC_RXMAX_FRAMES, 0);
7018 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007019 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007020
David S. Miller15f98502005-05-18 22:49:26 -07007021 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7022 u32 val = ec->stats_block_coalesce_usecs;
7023
Matt Carlsonb6080e12009-09-01 13:12:00 +00007024 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7025 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7026
David S. Miller15f98502005-05-18 22:49:26 -07007027 if (!netif_carrier_ok(tp->dev))
7028 val = 0;
7029
7030 tw32(HOSTCC_STAT_COAL_TICKS, val);
7031 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007032
7033 for (i = 0; i < tp->irq_cnt - 1; i++) {
7034 u32 reg;
7035
7036 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7037 tw32(reg, ec->rx_coalesce_usecs);
7038 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7039 tw32(reg, ec->tx_coalesce_usecs);
7040 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7041 tw32(reg, ec->rx_max_coalesced_frames);
7042 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7043 tw32(reg, ec->tx_max_coalesced_frames);
7044 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7045 tw32(reg, ec->rx_max_coalesced_frames_irq);
7046 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7047 tw32(reg, ec->tx_max_coalesced_frames_irq);
7048 }
7049
7050 for (; i < tp->irq_max - 1; i++) {
7051 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
7052 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7053 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
7054 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7055 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7056 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7057 }
David S. Miller15f98502005-05-18 22:49:26 -07007058}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059
7060/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007061static void tg3_rings_reset(struct tg3 *tp)
7062{
7063 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007064 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007065 struct tg3_napi *tnapi = &tp->napi[0];
7066
7067 /* Disable all transmit rings but the first. */
7068 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7069 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
7070 else
7071 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7072
7073 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7074 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7075 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7076 BDINFO_FLAGS_DISABLED);
7077
7078
7079 /* Disable all receive return rings but the first. */
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007080 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
7081 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7082 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007083 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
7084 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
7085 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7086 else
7087 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7088
7089 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7090 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7091 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7092 BDINFO_FLAGS_DISABLED);
7093
7094 /* Disable interrupts */
7095 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7096
7097 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007098 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
7099 for (i = 1; i < TG3_IRQ_MAX_VECS; i++) {
7100 tp->napi[i].tx_prod = 0;
7101 tp->napi[i].tx_cons = 0;
7102 tw32_mailbox(tp->napi[i].prodmbox, 0);
7103 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7104 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7105 }
7106 } else {
7107 tp->napi[0].tx_prod = 0;
7108 tp->napi[0].tx_cons = 0;
7109 tw32_mailbox(tp->napi[0].prodmbox, 0);
7110 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7111 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007112
7113 /* Make sure the NIC-based send BD rings are disabled. */
7114 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7115 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7116 for (i = 0; i < 16; i++)
7117 tw32_tx_mbox(mbox + i * 8, 0);
7118 }
7119
7120 txrcb = NIC_SRAM_SEND_RCB;
7121 rxrcb = NIC_SRAM_RCV_RET_RCB;
7122
7123 /* Clear status block in ram. */
7124 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7125
7126 /* Set status block DMA address */
7127 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7128 ((u64) tnapi->status_mapping >> 32));
7129 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7130 ((u64) tnapi->status_mapping & 0xffffffff));
7131
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007132 if (tnapi->tx_ring) {
7133 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7134 (TG3_TX_RING_SIZE <<
7135 BDINFO_FLAGS_MAXLEN_SHIFT),
7136 NIC_SRAM_TX_BUFFER_DESC);
7137 txrcb += TG3_BDINFO_SIZE;
7138 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007139
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007140 if (tnapi->rx_rcb) {
7141 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7142 (TG3_RX_RCB_RING_SIZE(tp) <<
7143 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7144 rxrcb += TG3_BDINFO_SIZE;
7145 }
7146
7147 stblk = HOSTCC_STATBLCK_RING1;
7148
7149 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7150 u64 mapping = (u64)tnapi->status_mapping;
7151 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7152 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7153
7154 /* Clear status block in ram. */
7155 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7156
7157 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7158 (TG3_TX_RING_SIZE <<
7159 BDINFO_FLAGS_MAXLEN_SHIFT),
7160 NIC_SRAM_TX_BUFFER_DESC);
7161
7162 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
7163 (TG3_RX_RCB_RING_SIZE(tp) <<
7164 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7165
7166 stblk += 8;
7167 txrcb += TG3_BDINFO_SIZE;
7168 rxrcb += TG3_BDINFO_SIZE;
7169 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007170}
7171
7172/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007173static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174{
7175 u32 val, rdmac_mode;
7176 int i, err, limit;
Matt Carlson21f581a2009-08-28 14:00:25 +00007177 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178
7179 tg3_disable_ints(tp);
7180
7181 tg3_stop_fw(tp);
7182
7183 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7184
7185 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) {
Michael Chane6de8ad2005-05-05 14:42:41 -07007186 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187 }
7188
Matt Carlsondd477002008-05-25 23:45:58 -07007189 if (reset_phy &&
7190 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB))
Michael Chand4d2c552006-03-20 17:47:20 -08007191 tg3_phy_reset(tp);
7192
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 err = tg3_chip_reset(tp);
7194 if (err)
7195 return err;
7196
7197 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7198
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007199 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007200 val = tr32(TG3_CPMU_CTRL);
7201 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7202 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007203
7204 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7205 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7206 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7207 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7208
7209 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7210 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7211 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7212 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7213
7214 val = tr32(TG3_CPMU_HST_ACC);
7215 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7216 val |= CPMU_HST_ACC_MACCLK_6_25;
7217 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007218 }
7219
Matt Carlson33466d932009-04-20 06:57:41 +00007220 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7221 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7222 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7223 PCIE_PWR_MGMT_L1_THRESH_4MS;
7224 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007225
7226 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7227 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7228
7229 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d932009-04-20 06:57:41 +00007230
Matt Carlsonf40386c2009-11-02 14:24:02 +00007231 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7232 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007233 }
7234
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235 /* This works around an issue with Athlon chipsets on
7236 * B3 tigon3 silicon. This bit has no effect on any
7237 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07007238 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239 */
Matt Carlson795d01c2007-10-07 23:28:17 -07007240 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
7241 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
7242 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
7243 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245
7246 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7247 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
7248 val = tr32(TG3PCI_PCISTATE);
7249 val |= PCISTATE_RETRY_SAME_DMA;
7250 tw32(TG3PCI_PCISTATE, val);
7251 }
7252
Matt Carlson0d3031d2007-10-10 18:02:43 -07007253 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
7254 /* Allow reads and writes to the
7255 * APE register and memory space.
7256 */
7257 val = tr32(TG3PCI_PCISTATE);
7258 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
7259 PCISTATE_ALLOW_APE_SHMEM_WR;
7260 tw32(TG3PCI_PCISTATE, val);
7261 }
7262
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
7264 /* Enable some hw fixes. */
7265 val = tr32(TG3PCI_MSI_DATA);
7266 val |= (1 << 26) | (1 << 28) | (1 << 29);
7267 tw32(TG3PCI_MSI_DATA, val);
7268 }
7269
7270 /* Descriptor ring init may make accesses to the
7271 * NIC SRAM area to setup the TX descriptors, so we
7272 * can only do this after the hardware has been
7273 * successfully reset.
7274 */
Michael Chan32d8c572006-07-25 16:38:29 -07007275 err = tg3_init_rings(tp);
7276 if (err)
7277 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278
Matt Carlson9936bcf2007-10-10 18:03:07 -07007279 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007280 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761 &&
7281 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007282 /* This value is determined during the probe time DMA
7283 * engine test, tg3_test_dma.
7284 */
7285 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
7286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287
7288 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
7289 GRC_MODE_4X_NIC_SEND_RINGS |
7290 GRC_MODE_NO_TX_PHDR_CSUM |
7291 GRC_MODE_NO_RX_PHDR_CSUM);
7292 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07007293
7294 /* Pseudo-header checksum is done by hardware logic and not
7295 * the offload processers, so make the chip do the pseudo-
7296 * header checksums on receive. For transmit it is more
7297 * convenient to do the pseudo-header checksum in software
7298 * as Linux does that on transmit for us in all cases.
7299 */
7300 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301
7302 tw32(GRC_MODE,
7303 tp->grc_mode |
7304 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
7305
7306 /* Setup the timer prescalar register. Clock is always 66Mhz. */
7307 val = tr32(GRC_MISC_CFG);
7308 val &= ~0xff;
7309 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
7310 tw32(GRC_MISC_CFG, val);
7311
7312 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07007313 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314 /* Do nothing. */
7315 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
7316 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
7317 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
7318 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
7319 else
7320 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
7321 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
7322 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
7323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324 else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
7325 int fw_len;
7326
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007327 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007328 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
7329 tw32(BUFMGR_MB_POOL_ADDR,
7330 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
7331 tw32(BUFMGR_MB_POOL_SIZE,
7332 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
7333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334
Michael Chan0f893dc2005-07-25 12:30:38 -07007335 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7337 tp->bufmgr_config.mbuf_read_dma_low_water);
7338 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7339 tp->bufmgr_config.mbuf_mac_rx_low_water);
7340 tw32(BUFMGR_MB_HIGH_WATER,
7341 tp->bufmgr_config.mbuf_high_water);
7342 } else {
7343 tw32(BUFMGR_MB_RDMA_LOW_WATER,
7344 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
7345 tw32(BUFMGR_MB_MACRX_LOW_WATER,
7346 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
7347 tw32(BUFMGR_MB_HIGH_WATER,
7348 tp->bufmgr_config.mbuf_high_water_jumbo);
7349 }
7350 tw32(BUFMGR_DMA_LOW_WATER,
7351 tp->bufmgr_config.dma_low_water);
7352 tw32(BUFMGR_DMA_HIGH_WATER,
7353 tp->bufmgr_config.dma_high_water);
7354
7355 tw32(BUFMGR_MODE, BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE);
7356 for (i = 0; i < 2000; i++) {
7357 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
7358 break;
7359 udelay(10);
7360 }
7361 if (i >= 2000) {
7362 printk(KERN_ERR PFX "tg3_reset_hw cannot enable BUFMGR for %s.\n",
7363 tp->dev->name);
7364 return -ENODEV;
7365 }
7366
7367 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07007368 val = tp->rx_pending / 8;
7369 if (val == 0)
7370 val = 1;
7371 else if (val > tp->rx_std_max_post)
7372 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07007373 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7374 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
7375 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
7376
7377 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
7378 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
7379 }
Michael Chanf92905d2006-06-29 20:14:29 -07007380
7381 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382
7383 /* Initialize TG3_BDINFO's at:
7384 * RCVDBDI_STD_BD: standard eth size rx ring
7385 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
7386 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
7387 *
7388 * like so:
7389 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
7390 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
7391 * ring attribute flags
7392 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
7393 *
7394 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
7395 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
7396 *
7397 * The size of each ring is fixed in the firmware, but the location is
7398 * configurable.
7399 */
7400 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007401 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007403 ((u64) tpr->rx_std_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
7405 NIC_SRAM_RX_BUFFER_DESC);
7406
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007407 /* Disable the mini ring */
7408 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
7410 BDINFO_FLAGS_DISABLED);
7411
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007412 /* Program the jumbo buffer descriptor ring control
7413 * blocks on those devices that have them.
7414 */
Matt Carlson8f666b02009-08-28 13:58:24 +00007415 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007416 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417 /* Setup replenish threshold. */
7418 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
7419
Michael Chan0f893dc2005-07-25 12:30:38 -07007420 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00007422 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007424 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlson79ed5ac2009-08-28 14:00:55 +00007426 (RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT) |
7427 BDINFO_FLAGS_USE_EXT_RECV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007428 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
7429 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
7430 } else {
7431 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
7432 BDINFO_FLAGS_DISABLED);
7433 }
7434
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007435 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
7436 val = (RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT) |
7437 (RX_STD_MAX_SIZE << 2);
7438 else
7439 val = RX_STD_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00007440 } else
7441 val = RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT;
7442
7443 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444
Matt Carlson21f581a2009-08-28 14:00:25 +00007445 tpr->rx_std_ptr = tp->rx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446 tw32_rx_mbox(MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007447 tpr->rx_std_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448
Matt Carlson21f581a2009-08-28 14:00:25 +00007449 tpr->rx_jmb_ptr = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
7450 tp->rx_jumbo_pending : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451 tw32_rx_mbox(MAILBOX_RCV_JUMBO_PROD_IDX + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00007452 tpr->rx_jmb_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007454 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
7455 tw32(STD_REPLENISH_LWM, 32);
7456 tw32(JMB_REPLENISH_LWM, 16);
7457 }
7458
Matt Carlson2d31eca2009-09-01 12:53:31 +00007459 tg3_rings_reset(tp);
7460
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07007462 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007463
7464 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00007465 tw32(MAC_RX_MTU_SIZE,
7466 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467
7468 /* The slot time is changed by tg3_setup_phy if we
7469 * run at gigabit with half duplex.
7470 */
7471 tw32(MAC_TX_LENGTHS,
7472 (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
7473 (6 << TX_LENGTHS_IPG_SHIFT) |
7474 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
7475
7476 /* Receive rules. */
7477 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
7478 tw32(RCVLPC_CONFIG, 0x0181);
7479
7480 /* Calculate RDMAC_MODE setting early, we need it to determine
7481 * the RCVLPC_STATE_ENABLE mask.
7482 */
7483 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
7484 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
7485 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
7486 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
7487 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07007488
Matt Carlson57e69832008-05-25 23:48:31 -07007489 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007490 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7491 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07007492 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
7493 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
7494 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
7495
Michael Chan85e94ce2005-04-21 17:05:28 -07007496 /* If statement applies to 5705 and 5750 PCI devices only */
7497 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7498 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7499 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07007501 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
7503 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7504 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
7505 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7506 }
7507 }
7508
Michael Chan85e94ce2005-04-21 17:05:28 -07007509 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
7510 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
7511
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08007513 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
7514
7515 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
7516 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
7517 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518
7519 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07007520 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
7521 val = tr32(RCVLPC_STATS_ENABLE);
7522 val &= ~RCVLPC_STATSENAB_DACK_FIX;
7523 tw32(RCVLPC_STATS_ENABLE, val);
7524 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
7525 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007526 val = tr32(RCVLPC_STATS_ENABLE);
7527 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
7528 tw32(RCVLPC_STATS_ENABLE, val);
7529 } else {
7530 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
7531 }
7532 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
7533 tw32(SNDDATAI_STATSENAB, 0xffffff);
7534 tw32(SNDDATAI_STATSCTRL,
7535 (SNDDATAI_SCTRL_ENABLE |
7536 SNDDATAI_SCTRL_FASTUPD));
7537
7538 /* Setup host coalescing engine. */
7539 tw32(HOSTCC_MODE, 0);
7540 for (i = 0; i < 2000; i++) {
7541 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
7542 break;
7543 udelay(10);
7544 }
7545
Michael Chand244c892005-07-05 14:42:33 -07007546 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7549 /* Status/statistics block address. See tg3_timer,
7550 * the tg3_periodic_fetch_stats call there, and
7551 * tg3_get_stats to see how this works for 5705/5750 chips.
7552 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7554 ((u64) tp->stats_mapping >> 32));
7555 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7556 ((u64) tp->stats_mapping & 0xffffffff));
7557 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00007558
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00007560
7561 /* Clear statistics and status block memory areas */
7562 for (i = NIC_SRAM_STATS_BLK;
7563 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
7564 i += sizeof(u32)) {
7565 tg3_write_mem(tp, i, 0);
7566 udelay(40);
7567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568 }
7569
7570 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
7571
7572 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
7573 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
7574 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7575 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
7576
Michael Chanc94e3942005-09-27 12:12:42 -07007577 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
7578 tp->tg3_flags2 &= ~TG3_FLG2_PARALLEL_DETECT;
7579 /* reset to prevent losing 1st rx packet intermittently */
7580 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7581 udelay(10);
7582 }
7583
Matt Carlson3bda1252008-08-15 14:08:22 -07007584 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7585 tp->mac_mode &= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
7586 else
7587 tp->mac_mode = 0;
7588 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07007590 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
7591 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
7592 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
7593 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
7595 udelay(40);
7596
Michael Chan314fba32005-04-21 17:07:04 -07007597 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08007598 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07007599 * register to preserve the GPIO settings for LOMs. The GPIOs,
7600 * whether used as inputs or outputs, are set by boot code after
7601 * reset.
7602 */
Michael Chan9d26e212006-12-07 00:21:14 -08007603 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07007604 u32 gpio_mask;
7605
Michael Chan9d26e212006-12-07 00:21:14 -08007606 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
7607 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
7608 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07007609
7610 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7611 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
7612 GRC_LCLCTRL_GPIO_OUTPUT3;
7613
Michael Chanaf36e6b2006-03-23 01:28:06 -08007614 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
7615 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
7616
Gary Zambranoaaf84462007-05-05 11:51:45 -07007617 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07007618 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
7619
7620 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08007621 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
7622 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
7623 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07007624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
7626 udelay(100);
7627
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007628 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX) {
7629 val = tr32(MSGINT_MODE);
7630 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
7631 tw32(MSGINT_MODE, val);
7632 }
7633
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7635 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
7636 udelay(40);
7637 }
7638
7639 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
7640 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
7641 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
7642 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
7643 WDMAC_MODE_LNGREAD_ENAB);
7644
Michael Chan85e94ce2005-04-21 17:05:28 -07007645 /* If statement applies to 5705 and 5750 PCI devices only */
7646 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
7647 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
7648 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
Matt Carlson29ea0952009-08-25 10:07:54 +00007649 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07007650 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
7651 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
7652 /* nothing */
7653 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
7654 !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
7655 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
7656 val |= WDMAC_MODE_RX_ACCEL;
7657 }
7658 }
7659
Michael Chand9ab5ad12006-03-20 22:27:35 -08007660 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08007661 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07007662 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08007663
Matt Carlson788a0352009-11-02 14:26:03 +00007664 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
7665 val |= WDMAC_MODE_BURST_ALL_DATA;
7666
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667 tw32_f(WDMAC_MODE, val);
7668 udelay(40);
7669
Matt Carlson9974a352007-10-07 23:27:28 -07007670 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
7671 u16 pcix_cmd;
7672
7673 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7674 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007675 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07007676 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
7677 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007678 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07007679 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
7680 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007681 }
Matt Carlson9974a352007-10-07 23:27:28 -07007682 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7683 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007684 }
7685
7686 tw32_f(RDMAC_MODE, rdmac_mode);
7687 udelay(40);
7688
7689 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
7690 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7691 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07007692
7693 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
7694 tw32(SNDDATAC_MODE,
7695 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
7696 else
7697 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
7698
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
7700 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
7701 tw32(RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ);
7702 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007703 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7704 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007705 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
7706 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
7707 val |= SNDBDI_MODE_MULTI_TXQ_EN;
7708 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007709 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
7710
7711 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
7712 err = tg3_load_5701_a0_firmware_fix(tp);
7713 if (err)
7714 return err;
7715 }
7716
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
7718 err = tg3_load_tso_firmware(tp);
7719 if (err)
7720 return err;
7721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007722
7723 tp->tx_mode = TX_MODE_ENABLE;
7724 tw32_f(MAC_TX_MODE, tp->tx_mode);
7725 udelay(100);
7726
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007727 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
7728 u32 reg = MAC_RSS_INDIR_TBL_0;
7729 u8 *ent = (u8 *)&val;
7730
7731 /* Setup the indirection table */
7732 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
7733 int idx = i % sizeof(val);
7734
7735 ent[idx] = i % (tp->irq_cnt - 1);
7736 if (idx == sizeof(val) - 1) {
7737 tw32(reg, val);
7738 reg += 4;
7739 }
7740 }
7741
7742 /* Setup the "secret" hash key. */
7743 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
7744 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
7745 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
7746 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
7747 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
7748 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
7749 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
7750 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
7751 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
7752 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
7753 }
7754
Linus Torvalds1da177e2005-04-16 15:20:36 -07007755 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08007756 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08007757 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
7758
Matt Carlsonbaf8a942009-09-01 13:13:00 +00007759 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
7760 tp->rx_mode |= RX_MODE_RSS_ENABLE |
7761 RX_MODE_RSS_ITBL_HASH_BITS_7 |
7762 RX_MODE_RSS_IPV6_HASH_EN |
7763 RX_MODE_RSS_TCP_IPV6_HASH_EN |
7764 RX_MODE_RSS_IPV4_HASH_EN |
7765 RX_MODE_RSS_TCP_IPV4_HASH_EN;
7766
Linus Torvalds1da177e2005-04-16 15:20:36 -07007767 tw32_f(MAC_RX_MODE, tp->rx_mode);
7768 udelay(10);
7769
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770 tw32(MAC_LED_CTRL, tp->led_ctrl);
7771
7772 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Michael Chanc94e3942005-09-27 12:12:42 -07007773 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7775 udelay(10);
7776 }
7777 tw32_f(MAC_RX_MODE, tp->rx_mode);
7778 udelay(10);
7779
7780 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
7781 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
7782 !(tp->tg3_flags2 & TG3_FLG2_SERDES_PREEMPHASIS)) {
7783 /* Set drive transmission level to 1.2V */
7784 /* only if the signal pre-emphasis bit is not set */
7785 val = tr32(MAC_SERDES_CFG);
7786 val &= 0xfffff000;
7787 val |= 0x880;
7788 tw32(MAC_SERDES_CFG, val);
7789 }
7790 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
7791 tw32(MAC_SERDES_CFG, 0x616000);
7792 }
7793
7794 /* Prevent chip from dropping frames when flow control
7795 * is enabled.
7796 */
7797 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, 2);
7798
7799 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
7800 (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
7801 /* Use hardware link auto-negotiation */
7802 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
7803 }
7804
Michael Chand4d2c552006-03-20 17:47:20 -08007805 if ((tp->tg3_flags2 & TG3_FLG2_MII_SERDES) &&
7806 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
7807 u32 tmp;
7808
7809 tmp = tr32(SERDES_RX_CTRL);
7810 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
7811 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
7812 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
7813 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
7814 }
7815
Matt Carlsondd477002008-05-25 23:45:58 -07007816 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
7817 if (tp->link_config.phy_is_low_power) {
7818 tp->link_config.phy_is_low_power = 0;
7819 tp->link_config.speed = tp->link_config.orig_speed;
7820 tp->link_config.duplex = tp->link_config.orig_duplex;
7821 tp->link_config.autoneg = tp->link_config.orig_autoneg;
7822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007823
Matt Carlsondd477002008-05-25 23:45:58 -07007824 err = tg3_setup_phy(tp, 0);
7825 if (err)
7826 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007827
Matt Carlsondd477002008-05-25 23:45:58 -07007828 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +00007829 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07007830 u32 tmp;
7831
7832 /* Clear CRC stats. */
7833 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
7834 tg3_writephy(tp, MII_TG3_TEST1,
7835 tmp | MII_TG3_TEST1_CRC_EN);
7836 tg3_readphy(tp, 0x14, &tmp);
7837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007838 }
7839 }
7840
7841 __tg3_set_rx_mode(tp->dev);
7842
7843 /* Initialize receive rules. */
7844 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
7845 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
7846 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
7847 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
7848
Michael Chan4cf78e42005-07-25 12:29:19 -07007849 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07007850 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007851 limit = 8;
7852 else
7853 limit = 16;
7854 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
7855 limit -= 4;
7856 switch (limit) {
7857 case 16:
7858 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
7859 case 15:
7860 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
7861 case 14:
7862 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
7863 case 13:
7864 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
7865 case 12:
7866 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
7867 case 11:
7868 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
7869 case 10:
7870 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
7871 case 9:
7872 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
7873 case 8:
7874 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
7875 case 7:
7876 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
7877 case 6:
7878 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
7879 case 5:
7880 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
7881 case 4:
7882 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
7883 case 3:
7884 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
7885 case 2:
7886 case 1:
7887
7888 default:
7889 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891
Matt Carlson9ce768e2007-10-11 19:49:11 -07007892 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7893 /* Write our heartbeat update interval to APE. */
7894 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
7895 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07007896
Linus Torvalds1da177e2005-04-16 15:20:36 -07007897 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
7898
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899 return 0;
7900}
7901
7902/* Called at device open time to get the chip ready for
7903 * packet processing. Invoked with tp->lock held.
7904 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007905static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007906{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007907 tg3_switch_clocks(tp);
7908
7909 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
7910
Matt Carlson2f751b62008-08-04 23:17:34 -07007911 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007912}
7913
7914#define TG3_STAT_ADD32(PSTAT, REG) \
7915do { u32 __val = tr32(REG); \
7916 (PSTAT)->low += __val; \
7917 if ((PSTAT)->low < __val) \
7918 (PSTAT)->high += 1; \
7919} while (0)
7920
7921static void tg3_periodic_fetch_stats(struct tg3 *tp)
7922{
7923 struct tg3_hw_stats *sp = tp->hw_stats;
7924
7925 if (!netif_carrier_ok(tp->dev))
7926 return;
7927
7928 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
7929 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
7930 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
7931 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
7932 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
7933 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
7934 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
7935 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
7936 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
7937 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
7938 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
7939 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
7940 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
7941
7942 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
7943 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
7944 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
7945 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
7946 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
7947 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
7948 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
7949 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
7950 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
7951 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
7952 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
7953 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
7954 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
7955 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07007956
7957 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
7958 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
7959 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007960}
7961
7962static void tg3_timer(unsigned long __opaque)
7963{
7964 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965
Michael Chanf475f162006-03-27 23:20:14 -08007966 if (tp->irq_sync)
7967 goto restart_timer;
7968
David S. Millerf47c11e2005-06-24 20:18:35 -07007969 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007970
David S. Millerfac9b832005-05-18 22:46:34 -07007971 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
7972 /* All of this garbage is because when using non-tagged
7973 * IRQ status the mailbox/status_block protocol the chip
7974 * uses with the cpu is race prone.
7975 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007976 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07007977 tw32(GRC_LOCAL_CTRL,
7978 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
7979 } else {
7980 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00007981 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07007982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007983
David S. Millerfac9b832005-05-18 22:46:34 -07007984 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
7985 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07007986 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07007987 schedule_work(&tp->reset_task);
7988 return;
7989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007990 }
7991
Linus Torvalds1da177e2005-04-16 15:20:36 -07007992 /* This part only runs once per second. */
7993 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07007994 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7995 tg3_periodic_fetch_stats(tp);
7996
Linus Torvalds1da177e2005-04-16 15:20:36 -07007997 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
7998 u32 mac_stat;
7999 int phy_event;
8000
8001 mac_stat = tr32(MAC_STATUS);
8002
8003 phy_event = 0;
8004 if (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) {
8005 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8006 phy_event = 1;
8007 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8008 phy_event = 1;
8009
8010 if (phy_event)
8011 tg3_setup_phy(tp, 0);
8012 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8013 u32 mac_stat = tr32(MAC_STATUS);
8014 int need_setup = 0;
8015
8016 if (netif_carrier_ok(tp->dev) &&
8017 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8018 need_setup = 1;
8019 }
8020 if (! netif_carrier_ok(tp->dev) &&
8021 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8022 MAC_STATUS_SIGNAL_DET))) {
8023 need_setup = 1;
8024 }
8025 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008026 if (!tp->serdes_counter) {
8027 tw32_f(MAC_MODE,
8028 (tp->mac_mode &
8029 ~MAC_MODE_PORT_MODE_MASK));
8030 udelay(40);
8031 tw32_f(MAC_MODE, tp->mac_mode);
8032 udelay(40);
8033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008034 tg3_setup_phy(tp, 0);
8035 }
Michael Chan747e8f82005-07-25 12:33:22 -07008036 } else if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
8037 tg3_serdes_parallel_detect(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008038
8039 tp->timer_counter = tp->timer_multiplier;
8040 }
8041
Michael Chan130b8e42006-09-27 16:00:40 -07008042 /* Heartbeat is only sent once every 2 seconds.
8043 *
8044 * The heartbeat is to tell the ASF firmware that the host
8045 * driver is still alive. In the event that the OS crashes,
8046 * ASF needs to reset the hardware to free up the FIFO space
8047 * that may be filled with rx packets destined for the host.
8048 * If the FIFO is full, ASF will no longer function properly.
8049 *
8050 * Unintended resets have been reported on real time kernels
8051 * where the timer doesn't run on time. Netpoll will also have
8052 * same problem.
8053 *
8054 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8055 * to check the ring condition when the heartbeat is expiring
8056 * before doing the reset. This will prevent most unintended
8057 * resets.
8058 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07008060 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
8061 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008062 tg3_wait_for_event_ack(tp);
8063
Michael Chanbbadf502006-04-06 21:46:34 -07008064 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008065 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008066 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Michael Chan28fbef72005-10-26 15:48:35 -07008067 /* 5 seconds timeout */
Michael Chanbbadf502006-04-06 21:46:34 -07008068 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, 5);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008069
8070 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008071 }
8072 tp->asf_counter = tp->asf_multiplier;
8073 }
8074
David S. Millerf47c11e2005-06-24 20:18:35 -07008075 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008076
Michael Chanf475f162006-03-27 23:20:14 -08008077restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008078 tp->timer.expires = jiffies + tp->timer_offset;
8079 add_timer(&tp->timer);
8080}
8081
Matt Carlson4f125f42009-09-01 12:55:02 +00008082static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008083{
David Howells7d12e782006-10-05 14:55:46 +01008084 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008085 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008086 char *name;
8087 struct tg3_napi *tnapi = &tp->napi[irq_num];
8088
8089 if (tp->irq_cnt == 1)
8090 name = tp->dev->name;
8091 else {
8092 name = &tnapi->irq_lbl[0];
8093 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8094 name[IFNAMSIZ-1] = 0;
8095 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008096
Matt Carlson679563f2009-09-01 12:55:46 +00008097 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008098 fn = tg3_msi;
8099 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
8100 fn = tg3_msi_1shot;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008101 flags = IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008102 } else {
8103 fn = tg3_interrupt;
8104 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8105 fn = tg3_interrupt_tagged;
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07008106 flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008107 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008108
8109 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008110}
8111
Michael Chan79381092005-04-21 17:13:59 -07008112static int tg3_test_interrupt(struct tg3 *tp)
8113{
Matt Carlson09943a12009-08-28 14:01:57 +00008114 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07008115 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07008116 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008117 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07008118
Michael Chand4bc3922005-05-29 14:59:20 -07008119 if (!netif_running(dev))
8120 return -ENODEV;
8121
Michael Chan79381092005-04-21 17:13:59 -07008122 tg3_disable_ints(tp);
8123
Matt Carlson4f125f42009-09-01 12:55:02 +00008124 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008125
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008126 /*
8127 * Turn off MSI one shot mode. Otherwise this test has no
8128 * observable way to know whether the interrupt was delivered.
8129 */
8130 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
8131 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8132 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
8133 tw32(MSGINT_MODE, val);
8134 }
8135
Matt Carlson4f125f42009-09-01 12:55:02 +00008136 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00008137 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07008138 if (err)
8139 return err;
8140
Matt Carlson898a56f2009-08-28 14:02:40 +00008141 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07008142 tg3_enable_ints(tp);
8143
8144 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008145 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07008146
8147 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07008148 u32 int_mbox, misc_host_ctrl;
8149
Matt Carlson898a56f2009-08-28 14:02:40 +00008150 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07008151 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
8152
8153 if ((int_mbox != 0) ||
8154 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
8155 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07008156 break;
Michael Chanb16250e2006-09-27 16:10:14 -07008157 }
8158
Michael Chan79381092005-04-21 17:13:59 -07008159 msleep(10);
8160 }
8161
8162 tg3_disable_ints(tp);
8163
Matt Carlson4f125f42009-09-01 12:55:02 +00008164 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008165
Matt Carlson4f125f42009-09-01 12:55:02 +00008166 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008167
8168 if (err)
8169 return err;
8170
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008171 if (intr_ok) {
8172 /* Reenable MSI one shot mode. */
8173 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
8174 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
8175 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
8176 tw32(MSGINT_MODE, val);
8177 }
Michael Chan79381092005-04-21 17:13:59 -07008178 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008179 }
Michael Chan79381092005-04-21 17:13:59 -07008180
8181 return -EIO;
8182}
8183
8184/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
8185 * successfully restored
8186 */
8187static int tg3_test_msi(struct tg3 *tp)
8188{
Michael Chan79381092005-04-21 17:13:59 -07008189 int err;
8190 u16 pci_cmd;
8191
8192 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
8193 return 0;
8194
8195 /* Turn off SERR reporting in case MSI terminates with Master
8196 * Abort.
8197 */
8198 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
8199 pci_write_config_word(tp->pdev, PCI_COMMAND,
8200 pci_cmd & ~PCI_COMMAND_SERR);
8201
8202 err = tg3_test_interrupt(tp);
8203
8204 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
8205
8206 if (!err)
8207 return 0;
8208
8209 /* other failures */
8210 if (err != -EIO)
8211 return err;
8212
8213 /* MSI test failed, go back to INTx mode */
8214 printk(KERN_WARNING PFX "%s: No interrupt was generated using MSI, "
8215 "switching to INTx mode. Please report this failure to "
8216 "the PCI maintainer and include system chipset information.\n",
8217 tp->dev->name);
8218
Matt Carlson4f125f42009-09-01 12:55:02 +00008219 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00008220
Michael Chan79381092005-04-21 17:13:59 -07008221 pci_disable_msi(tp->pdev);
8222
8223 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
8224
Matt Carlson4f125f42009-09-01 12:55:02 +00008225 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07008226 if (err)
8227 return err;
8228
8229 /* Need to reset the chip because the MSI cycle may have terminated
8230 * with Master Abort.
8231 */
David S. Millerf47c11e2005-06-24 20:18:35 -07008232 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008233
Michael Chan944d9802005-05-29 14:57:48 -07008234 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008235 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07008236
David S. Millerf47c11e2005-06-24 20:18:35 -07008237 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008238
8239 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00008240 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07008241
8242 return err;
8243}
8244
Matt Carlson9e9fd122009-01-19 16:57:45 -08008245static int tg3_request_firmware(struct tg3 *tp)
8246{
8247 const __be32 *fw_data;
8248
8249 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
8250 printk(KERN_ERR "%s: Failed to load firmware \"%s\"\n",
8251 tp->dev->name, tp->fw_needed);
8252 return -ENOENT;
8253 }
8254
8255 fw_data = (void *)tp->fw->data;
8256
8257 /* Firmware blob starts with version numbers, followed by
8258 * start address and _full_ length including BSS sections
8259 * (which must be longer than the actual data, of course
8260 */
8261
8262 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
8263 if (tp->fw_len < (tp->fw->size - 12)) {
8264 printk(KERN_ERR "%s: bogus length %d in \"%s\"\n",
8265 tp->dev->name, tp->fw_len, tp->fw_needed);
8266 release_firmware(tp->fw);
8267 tp->fw = NULL;
8268 return -EINVAL;
8269 }
8270
8271 /* We no longer need firmware; we have it. */
8272 tp->fw_needed = NULL;
8273 return 0;
8274}
8275
Matt Carlson679563f2009-09-01 12:55:46 +00008276static bool tg3_enable_msix(struct tg3 *tp)
8277{
8278 int i, rc, cpus = num_online_cpus();
8279 struct msix_entry msix_ent[tp->irq_max];
8280
8281 if (cpus == 1)
8282 /* Just fallback to the simpler MSI mode. */
8283 return false;
8284
8285 /*
8286 * We want as many rx rings enabled as there are cpus.
8287 * The first MSIX vector only deals with link interrupts, etc,
8288 * so we add one to the number of vectors we are requesting.
8289 */
8290 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
8291
8292 for (i = 0; i < tp->irq_max; i++) {
8293 msix_ent[i].entry = i;
8294 msix_ent[i].vector = 0;
8295 }
8296
8297 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
8298 if (rc != 0) {
8299 if (rc < TG3_RSS_MIN_NUM_MSIX_VECS)
8300 return false;
8301 if (pci_enable_msix(tp->pdev, msix_ent, rc))
8302 return false;
8303 printk(KERN_NOTICE
8304 "%s: Requested %d MSI-X vectors, received %d\n",
8305 tp->dev->name, tp->irq_cnt, rc);
8306 tp->irq_cnt = rc;
8307 }
8308
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008309 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
8310
Matt Carlson679563f2009-09-01 12:55:46 +00008311 for (i = 0; i < tp->irq_max; i++)
8312 tp->napi[i].irq_vec = msix_ent[i].vector;
8313
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008314 tp->dev->real_num_tx_queues = tp->irq_cnt - 1;
8315
Matt Carlson679563f2009-09-01 12:55:46 +00008316 return true;
8317}
8318
Matt Carlson07b01732009-08-28 14:01:15 +00008319static void tg3_ints_init(struct tg3 *tp)
8320{
Matt Carlson679563f2009-09-01 12:55:46 +00008321 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
8322 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00008323 /* All MSI supporting chips should support tagged
8324 * status. Assert that this is the case.
8325 */
Matt Carlson679563f2009-09-01 12:55:46 +00008326 printk(KERN_WARNING PFX "%s: MSI without TAGGED? "
8327 "Not using MSI.\n", tp->dev->name);
8328 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00008329 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008330
Matt Carlson679563f2009-09-01 12:55:46 +00008331 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
8332 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
8333 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
8334 pci_enable_msi(tp->pdev) == 0)
8335 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
8336
8337 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
8338 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008339 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8340 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00008341 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
8342 }
8343defcfg:
8344 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
8345 tp->irq_cnt = 1;
8346 tp->napi[0].irq_vec = tp->pdev->irq;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008347 tp->dev->real_num_tx_queues = 1;
Matt Carlson679563f2009-09-01 12:55:46 +00008348 }
Matt Carlson07b01732009-08-28 14:01:15 +00008349}
8350
8351static void tg3_ints_fini(struct tg3 *tp)
8352{
Matt Carlson679563f2009-09-01 12:55:46 +00008353 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
8354 pci_disable_msix(tp->pdev);
8355 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
8356 pci_disable_msi(tp->pdev);
8357 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008358 tp->tg3_flags3 &= ~TG3_FLG3_ENABLE_RSS;
Matt Carlson07b01732009-08-28 14:01:15 +00008359}
8360
Linus Torvalds1da177e2005-04-16 15:20:36 -07008361static int tg3_open(struct net_device *dev)
8362{
8363 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00008364 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008365
Matt Carlson9e9fd122009-01-19 16:57:45 -08008366 if (tp->fw_needed) {
8367 err = tg3_request_firmware(tp);
8368 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8369 if (err)
8370 return err;
8371 } else if (err) {
8372 printk(KERN_WARNING "%s: TSO capability disabled.\n",
8373 tp->dev->name);
8374 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
8375 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
8376 printk(KERN_NOTICE "%s: TSO capability restored.\n",
8377 tp->dev->name);
8378 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
8379 }
8380 }
8381
Michael Chanc49a1562006-12-17 17:07:29 -08008382 netif_carrier_off(tp->dev);
8383
Michael Chanbc1c7562006-03-20 17:48:03 -08008384 err = tg3_set_power_state(tp, PCI_D0);
Matt Carlson2f751b62008-08-04 23:17:34 -07008385 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08008386 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07008387
8388 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08008389
Linus Torvalds1da177e2005-04-16 15:20:36 -07008390 tg3_disable_ints(tp);
8391 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
8392
David S. Millerf47c11e2005-06-24 20:18:35 -07008393 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008394
Matt Carlson679563f2009-09-01 12:55:46 +00008395 /*
8396 * Setup interrupts first so we know how
8397 * many NAPI resources to allocate
8398 */
8399 tg3_ints_init(tp);
8400
Linus Torvalds1da177e2005-04-16 15:20:36 -07008401 /* The placement of this call is tied
8402 * to the setup and use of Host TX descriptors.
8403 */
8404 err = tg3_alloc_consistent(tp);
8405 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008406 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008407
Matt Carlsonfed97812009-09-01 13:10:19 +00008408 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07008409
Matt Carlson4f125f42009-09-01 12:55:02 +00008410 for (i = 0; i < tp->irq_cnt; i++) {
8411 struct tg3_napi *tnapi = &tp->napi[i];
8412 err = tg3_request_irq(tp, i);
8413 if (err) {
8414 for (i--; i >= 0; i--)
8415 free_irq(tnapi->irq_vec, tnapi);
8416 break;
8417 }
8418 }
Matt Carlson07b01732009-08-28 14:01:15 +00008419
8420 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008421 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00008422
David S. Millerf47c11e2005-06-24 20:18:35 -07008423 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008424
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008425 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008426 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07008427 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008428 tg3_free_rings(tp);
8429 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07008430 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
8431 tp->timer_offset = HZ;
8432 else
8433 tp->timer_offset = HZ / 10;
8434
8435 BUG_ON(tp->timer_offset > HZ);
8436 tp->timer_counter = tp->timer_multiplier =
8437 (HZ / tp->timer_offset);
8438 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07008439 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008440
8441 init_timer(&tp->timer);
8442 tp->timer.expires = jiffies + tp->timer_offset;
8443 tp->timer.data = (unsigned long) tp;
8444 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008445 }
8446
David S. Millerf47c11e2005-06-24 20:18:35 -07008447 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008448
Matt Carlson07b01732009-08-28 14:01:15 +00008449 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00008450 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451
Michael Chan79381092005-04-21 17:13:59 -07008452 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
8453 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07008454
Michael Chan79381092005-04-21 17:13:59 -07008455 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07008456 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07008457 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07008458 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07008459 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07008460
Matt Carlson679563f2009-09-01 12:55:46 +00008461 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07008462 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008463
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008464 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
8465 (tp->tg3_flags2 & TG3_FLG2_USING_MSI) &&
8466 (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)) {
8467 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008468
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008469 tw32(PCIE_TRANSACTION_CFG,
8470 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08008471 }
Michael Chan79381092005-04-21 17:13:59 -07008472 }
8473
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07008474 tg3_phy_start(tp);
8475
David S. Millerf47c11e2005-06-24 20:18:35 -07008476 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008477
Michael Chan79381092005-04-21 17:13:59 -07008478 add_timer(&tp->timer);
8479 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008480 tg3_enable_ints(tp);
8481
David S. Millerf47c11e2005-06-24 20:18:35 -07008482 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008483
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008484 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485
8486 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00008487
Matt Carlson679563f2009-09-01 12:55:46 +00008488err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00008489 for (i = tp->irq_cnt - 1; i >= 0; i--) {
8490 struct tg3_napi *tnapi = &tp->napi[i];
8491 free_irq(tnapi->irq_vec, tnapi);
8492 }
Matt Carlson07b01732009-08-28 14:01:15 +00008493
Matt Carlson679563f2009-09-01 12:55:46 +00008494err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00008495 tg3_napi_disable(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008496 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00008497
8498err_out1:
8499 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00008500 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501}
8502
8503#if 0
8504/*static*/ void tg3_dump_state(struct tg3 *tp)
8505{
8506 u32 val32, val32_2, val32_3, val32_4, val32_5;
8507 u16 val16;
8508 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008509 struct tg3_hw_status *sblk = tp->napi[0]->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510
8511 pci_read_config_word(tp->pdev, PCI_STATUS, &val16);
8512 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE, &val32);
8513 printk("DEBUG: PCI status [%04x] TG3PCI state[%08x]\n",
8514 val16, val32);
8515
8516 /* MAC block */
8517 printk("DEBUG: MAC_MODE[%08x] MAC_STATUS[%08x]\n",
8518 tr32(MAC_MODE), tr32(MAC_STATUS));
8519 printk(" MAC_EVENT[%08x] MAC_LED_CTRL[%08x]\n",
8520 tr32(MAC_EVENT), tr32(MAC_LED_CTRL));
8521 printk("DEBUG: MAC_TX_MODE[%08x] MAC_TX_STATUS[%08x]\n",
8522 tr32(MAC_TX_MODE), tr32(MAC_TX_STATUS));
8523 printk(" MAC_RX_MODE[%08x] MAC_RX_STATUS[%08x]\n",
8524 tr32(MAC_RX_MODE), tr32(MAC_RX_STATUS));
8525
8526 /* Send data initiator control block */
8527 printk("DEBUG: SNDDATAI_MODE[%08x] SNDDATAI_STATUS[%08x]\n",
8528 tr32(SNDDATAI_MODE), tr32(SNDDATAI_STATUS));
8529 printk(" SNDDATAI_STATSCTRL[%08x]\n",
8530 tr32(SNDDATAI_STATSCTRL));
8531
8532 /* Send data completion control block */
8533 printk("DEBUG: SNDDATAC_MODE[%08x]\n", tr32(SNDDATAC_MODE));
8534
8535 /* Send BD ring selector block */
8536 printk("DEBUG: SNDBDS_MODE[%08x] SNDBDS_STATUS[%08x]\n",
8537 tr32(SNDBDS_MODE), tr32(SNDBDS_STATUS));
8538
8539 /* Send BD initiator control block */
8540 printk("DEBUG: SNDBDI_MODE[%08x] SNDBDI_STATUS[%08x]\n",
8541 tr32(SNDBDI_MODE), tr32(SNDBDI_STATUS));
8542
8543 /* Send BD completion control block */
8544 printk("DEBUG: SNDBDC_MODE[%08x]\n", tr32(SNDBDC_MODE));
8545
8546 /* Receive list placement control block */
8547 printk("DEBUG: RCVLPC_MODE[%08x] RCVLPC_STATUS[%08x]\n",
8548 tr32(RCVLPC_MODE), tr32(RCVLPC_STATUS));
8549 printk(" RCVLPC_STATSCTRL[%08x]\n",
8550 tr32(RCVLPC_STATSCTRL));
8551
8552 /* Receive data and receive BD initiator control block */
8553 printk("DEBUG: RCVDBDI_MODE[%08x] RCVDBDI_STATUS[%08x]\n",
8554 tr32(RCVDBDI_MODE), tr32(RCVDBDI_STATUS));
8555
8556 /* Receive data completion control block */
8557 printk("DEBUG: RCVDCC_MODE[%08x]\n",
8558 tr32(RCVDCC_MODE));
8559
8560 /* Receive BD initiator control block */
8561 printk("DEBUG: RCVBDI_MODE[%08x] RCVBDI_STATUS[%08x]\n",
8562 tr32(RCVBDI_MODE), tr32(RCVBDI_STATUS));
8563
8564 /* Receive BD completion control block */
8565 printk("DEBUG: RCVCC_MODE[%08x] RCVCC_STATUS[%08x]\n",
8566 tr32(RCVCC_MODE), tr32(RCVCC_STATUS));
8567
8568 /* Receive list selector control block */
8569 printk("DEBUG: RCVLSC_MODE[%08x] RCVLSC_STATUS[%08x]\n",
8570 tr32(RCVLSC_MODE), tr32(RCVLSC_STATUS));
8571
8572 /* Mbuf cluster free block */
8573 printk("DEBUG: MBFREE_MODE[%08x] MBFREE_STATUS[%08x]\n",
8574 tr32(MBFREE_MODE), tr32(MBFREE_STATUS));
8575
8576 /* Host coalescing control block */
8577 printk("DEBUG: HOSTCC_MODE[%08x] HOSTCC_STATUS[%08x]\n",
8578 tr32(HOSTCC_MODE), tr32(HOSTCC_STATUS));
8579 printk("DEBUG: HOSTCC_STATS_BLK_HOST_ADDR[%08x%08x]\n",
8580 tr32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH),
8581 tr32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW));
8582 printk("DEBUG: HOSTCC_STATUS_BLK_HOST_ADDR[%08x%08x]\n",
8583 tr32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH),
8584 tr32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW));
8585 printk("DEBUG: HOSTCC_STATS_BLK_NIC_ADDR[%08x]\n",
8586 tr32(HOSTCC_STATS_BLK_NIC_ADDR));
8587 printk("DEBUG: HOSTCC_STATUS_BLK_NIC_ADDR[%08x]\n",
8588 tr32(HOSTCC_STATUS_BLK_NIC_ADDR));
8589
8590 /* Memory arbiter control block */
8591 printk("DEBUG: MEMARB_MODE[%08x] MEMARB_STATUS[%08x]\n",
8592 tr32(MEMARB_MODE), tr32(MEMARB_STATUS));
8593
8594 /* Buffer manager control block */
8595 printk("DEBUG: BUFMGR_MODE[%08x] BUFMGR_STATUS[%08x]\n",
8596 tr32(BUFMGR_MODE), tr32(BUFMGR_STATUS));
8597 printk("DEBUG: BUFMGR_MB_POOL_ADDR[%08x] BUFMGR_MB_POOL_SIZE[%08x]\n",
8598 tr32(BUFMGR_MB_POOL_ADDR), tr32(BUFMGR_MB_POOL_SIZE));
8599 printk("DEBUG: BUFMGR_DMA_DESC_POOL_ADDR[%08x] "
8600 "BUFMGR_DMA_DESC_POOL_SIZE[%08x]\n",
8601 tr32(BUFMGR_DMA_DESC_POOL_ADDR),
8602 tr32(BUFMGR_DMA_DESC_POOL_SIZE));
8603
8604 /* Read DMA control block */
8605 printk("DEBUG: RDMAC_MODE[%08x] RDMAC_STATUS[%08x]\n",
8606 tr32(RDMAC_MODE), tr32(RDMAC_STATUS));
8607
8608 /* Write DMA control block */
8609 printk("DEBUG: WDMAC_MODE[%08x] WDMAC_STATUS[%08x]\n",
8610 tr32(WDMAC_MODE), tr32(WDMAC_STATUS));
8611
8612 /* DMA completion block */
8613 printk("DEBUG: DMAC_MODE[%08x]\n",
8614 tr32(DMAC_MODE));
8615
8616 /* GRC block */
8617 printk("DEBUG: GRC_MODE[%08x] GRC_MISC_CFG[%08x]\n",
8618 tr32(GRC_MODE), tr32(GRC_MISC_CFG));
8619 printk("DEBUG: GRC_LOCAL_CTRL[%08x]\n",
8620 tr32(GRC_LOCAL_CTRL));
8621
8622 /* TG3_BDINFOs */
8623 printk("DEBUG: RCVDBDI_JUMBO_BD[%08x%08x:%08x:%08x]\n",
8624 tr32(RCVDBDI_JUMBO_BD + 0x0),
8625 tr32(RCVDBDI_JUMBO_BD + 0x4),
8626 tr32(RCVDBDI_JUMBO_BD + 0x8),
8627 tr32(RCVDBDI_JUMBO_BD + 0xc));
8628 printk("DEBUG: RCVDBDI_STD_BD[%08x%08x:%08x:%08x]\n",
8629 tr32(RCVDBDI_STD_BD + 0x0),
8630 tr32(RCVDBDI_STD_BD + 0x4),
8631 tr32(RCVDBDI_STD_BD + 0x8),
8632 tr32(RCVDBDI_STD_BD + 0xc));
8633 printk("DEBUG: RCVDBDI_MINI_BD[%08x%08x:%08x:%08x]\n",
8634 tr32(RCVDBDI_MINI_BD + 0x0),
8635 tr32(RCVDBDI_MINI_BD + 0x4),
8636 tr32(RCVDBDI_MINI_BD + 0x8),
8637 tr32(RCVDBDI_MINI_BD + 0xc));
8638
8639 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x0, &val32);
8640 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x4, &val32_2);
8641 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0x8, &val32_3);
8642 tg3_read_mem(tp, NIC_SRAM_SEND_RCB + 0xc, &val32_4);
8643 printk("DEBUG: SRAM_SEND_RCB_0[%08x%08x:%08x:%08x]\n",
8644 val32, val32_2, val32_3, val32_4);
8645
8646 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x0, &val32);
8647 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x4, &val32_2);
8648 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0x8, &val32_3);
8649 tg3_read_mem(tp, NIC_SRAM_RCV_RET_RCB + 0xc, &val32_4);
8650 printk("DEBUG: SRAM_RCV_RET_RCB_0[%08x%08x:%08x:%08x]\n",
8651 val32, val32_2, val32_3, val32_4);
8652
8653 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x0, &val32);
8654 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x4, &val32_2);
8655 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x8, &val32_3);
8656 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0xc, &val32_4);
8657 tg3_read_mem(tp, NIC_SRAM_STATUS_BLK + 0x10, &val32_5);
8658 printk("DEBUG: SRAM_STATUS_BLK[%08x:%08x:%08x:%08x:%08x]\n",
8659 val32, val32_2, val32_3, val32_4, val32_5);
8660
8661 /* SW status block */
Matt Carlson898a56f2009-08-28 14:02:40 +00008662 printk(KERN_DEBUG
8663 "Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
8664 sblk->status,
8665 sblk->status_tag,
8666 sblk->rx_jumbo_consumer,
8667 sblk->rx_consumer,
8668 sblk->rx_mini_consumer,
8669 sblk->idx[0].rx_producer,
8670 sblk->idx[0].tx_consumer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008671
8672 /* SW statistics block */
8673 printk("DEBUG: Host statistics block [%08x:%08x:%08x:%08x]\n",
8674 ((u32 *)tp->hw_stats)[0],
8675 ((u32 *)tp->hw_stats)[1],
8676 ((u32 *)tp->hw_stats)[2],
8677 ((u32 *)tp->hw_stats)[3]);
8678
8679 /* Mailboxes */
8680 printk("DEBUG: SNDHOST_PROD[%08x%08x] SNDNIC_PROD[%08x%08x]\n",
Michael Chan09ee9292005-08-09 20:17:00 -07008681 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + 0x0),
8682 tr32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + 0x4),
8683 tr32_mailbox(MAILBOX_SNDNIC_PROD_IDX_0 + 0x0),
8684 tr32_mailbox(MAILBOX_SNDNIC_PROD_IDX_0 + 0x4));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008685
8686 /* NIC side send descriptors. */
8687 for (i = 0; i < 6; i++) {
8688 unsigned long txd;
8689
8690 txd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_TX_BUFFER_DESC
8691 + (i * sizeof(struct tg3_tx_buffer_desc));
8692 printk("DEBUG: NIC TXD(%d)[%08x:%08x:%08x:%08x]\n",
8693 i,
8694 readl(txd + 0x0), readl(txd + 0x4),
8695 readl(txd + 0x8), readl(txd + 0xc));
8696 }
8697
8698 /* NIC side RX descriptors. */
8699 for (i = 0; i < 6; i++) {
8700 unsigned long rxd;
8701
8702 rxd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_RX_BUFFER_DESC
8703 + (i * sizeof(struct tg3_rx_buffer_desc));
8704 printk("DEBUG: NIC RXD_STD(%d)[0][%08x:%08x:%08x:%08x]\n",
8705 i,
8706 readl(rxd + 0x0), readl(rxd + 0x4),
8707 readl(rxd + 0x8), readl(rxd + 0xc));
8708 rxd += (4 * sizeof(u32));
8709 printk("DEBUG: NIC RXD_STD(%d)[1][%08x:%08x:%08x:%08x]\n",
8710 i,
8711 readl(rxd + 0x0), readl(rxd + 0x4),
8712 readl(rxd + 0x8), readl(rxd + 0xc));
8713 }
8714
8715 for (i = 0; i < 6; i++) {
8716 unsigned long rxd;
8717
8718 rxd = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_RX_JUMBO_BUFFER_DESC
8719 + (i * sizeof(struct tg3_rx_buffer_desc));
8720 printk("DEBUG: NIC RXD_JUMBO(%d)[0][%08x:%08x:%08x:%08x]\n",
8721 i,
8722 readl(rxd + 0x0), readl(rxd + 0x4),
8723 readl(rxd + 0x8), readl(rxd + 0xc));
8724 rxd += (4 * sizeof(u32));
8725 printk("DEBUG: NIC RXD_JUMBO(%d)[1][%08x:%08x:%08x:%08x]\n",
8726 i,
8727 readl(rxd + 0x0), readl(rxd + 0x4),
8728 readl(rxd + 0x8), readl(rxd + 0xc));
8729 }
8730}
8731#endif
8732
8733static struct net_device_stats *tg3_get_stats(struct net_device *);
8734static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
8735
8736static int tg3_close(struct net_device *dev)
8737{
Matt Carlson4f125f42009-09-01 12:55:02 +00008738 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008739 struct tg3 *tp = netdev_priv(dev);
8740
Matt Carlsonfed97812009-09-01 13:10:19 +00008741 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07008742 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08008743
Matt Carlsonfe5f5782009-09-01 13:09:39 +00008744 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008745
8746 del_timer_sync(&tp->timer);
8747
Matt Carlson24bb4fb2009-10-05 17:55:29 +00008748 tg3_phy_stop(tp);
8749
David S. Millerf47c11e2005-06-24 20:18:35 -07008750 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008751#if 0
8752 tg3_dump_state(tp);
8753#endif
8754
8755 tg3_disable_ints(tp);
8756
Michael Chan944d9802005-05-29 14:57:48 -07008757 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008758 tg3_free_rings(tp);
Michael Chan5cf64b8a2007-05-05 12:11:21 -07008759 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008760
David S. Millerf47c11e2005-06-24 20:18:35 -07008761 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008762
Matt Carlson4f125f42009-09-01 12:55:02 +00008763 for (i = tp->irq_cnt - 1; i >= 0; i--) {
8764 struct tg3_napi *tnapi = &tp->napi[i];
8765 free_irq(tnapi->irq_vec, tnapi);
8766 }
Matt Carlson07b01732009-08-28 14:01:15 +00008767
8768 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008769
8770 memcpy(&tp->net_stats_prev, tg3_get_stats(tp->dev),
8771 sizeof(tp->net_stats_prev));
8772 memcpy(&tp->estats_prev, tg3_get_estats(tp),
8773 sizeof(tp->estats_prev));
8774
8775 tg3_free_consistent(tp);
8776
Michael Chanbc1c7562006-03-20 17:48:03 -08008777 tg3_set_power_state(tp, PCI_D3hot);
8778
8779 netif_carrier_off(tp->dev);
8780
Linus Torvalds1da177e2005-04-16 15:20:36 -07008781 return 0;
8782}
8783
8784static inline unsigned long get_stat64(tg3_stat64_t *val)
8785{
8786 unsigned long ret;
8787
8788#if (BITS_PER_LONG == 32)
8789 ret = val->low;
8790#else
8791 ret = ((u64)val->high << 32) | ((u64)val->low);
8792#endif
8793 return ret;
8794}
8795
Stefan Buehler816f8b82008-08-15 14:10:54 -07008796static inline u64 get_estat64(tg3_stat64_t *val)
8797{
8798 return ((u64)val->high << 32) | ((u64)val->low);
8799}
8800
Linus Torvalds1da177e2005-04-16 15:20:36 -07008801static unsigned long calc_crc_errors(struct tg3 *tp)
8802{
8803 struct tg3_hw_stats *hw_stats = tp->hw_stats;
8804
8805 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
8806 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
8807 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008808 u32 val;
8809
David S. Millerf47c11e2005-06-24 20:18:35 -07008810 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08008811 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
8812 tg3_writephy(tp, MII_TG3_TEST1,
8813 val | MII_TG3_TEST1_CRC_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008814 tg3_readphy(tp, 0x14, &val);
8815 } else
8816 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07008817 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008818
8819 tp->phy_crc_errors += val;
8820
8821 return tp->phy_crc_errors;
8822 }
8823
8824 return get_stat64(&hw_stats->rx_fcs_errors);
8825}
8826
8827#define ESTAT_ADD(member) \
8828 estats->member = old_estats->member + \
Stefan Buehler816f8b82008-08-15 14:10:54 -07008829 get_estat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008830
8831static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
8832{
8833 struct tg3_ethtool_stats *estats = &tp->estats;
8834 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
8835 struct tg3_hw_stats *hw_stats = tp->hw_stats;
8836
8837 if (!hw_stats)
8838 return old_estats;
8839
8840 ESTAT_ADD(rx_octets);
8841 ESTAT_ADD(rx_fragments);
8842 ESTAT_ADD(rx_ucast_packets);
8843 ESTAT_ADD(rx_mcast_packets);
8844 ESTAT_ADD(rx_bcast_packets);
8845 ESTAT_ADD(rx_fcs_errors);
8846 ESTAT_ADD(rx_align_errors);
8847 ESTAT_ADD(rx_xon_pause_rcvd);
8848 ESTAT_ADD(rx_xoff_pause_rcvd);
8849 ESTAT_ADD(rx_mac_ctrl_rcvd);
8850 ESTAT_ADD(rx_xoff_entered);
8851 ESTAT_ADD(rx_frame_too_long_errors);
8852 ESTAT_ADD(rx_jabbers);
8853 ESTAT_ADD(rx_undersize_packets);
8854 ESTAT_ADD(rx_in_length_errors);
8855 ESTAT_ADD(rx_out_length_errors);
8856 ESTAT_ADD(rx_64_or_less_octet_packets);
8857 ESTAT_ADD(rx_65_to_127_octet_packets);
8858 ESTAT_ADD(rx_128_to_255_octet_packets);
8859 ESTAT_ADD(rx_256_to_511_octet_packets);
8860 ESTAT_ADD(rx_512_to_1023_octet_packets);
8861 ESTAT_ADD(rx_1024_to_1522_octet_packets);
8862 ESTAT_ADD(rx_1523_to_2047_octet_packets);
8863 ESTAT_ADD(rx_2048_to_4095_octet_packets);
8864 ESTAT_ADD(rx_4096_to_8191_octet_packets);
8865 ESTAT_ADD(rx_8192_to_9022_octet_packets);
8866
8867 ESTAT_ADD(tx_octets);
8868 ESTAT_ADD(tx_collisions);
8869 ESTAT_ADD(tx_xon_sent);
8870 ESTAT_ADD(tx_xoff_sent);
8871 ESTAT_ADD(tx_flow_control);
8872 ESTAT_ADD(tx_mac_errors);
8873 ESTAT_ADD(tx_single_collisions);
8874 ESTAT_ADD(tx_mult_collisions);
8875 ESTAT_ADD(tx_deferred);
8876 ESTAT_ADD(tx_excessive_collisions);
8877 ESTAT_ADD(tx_late_collisions);
8878 ESTAT_ADD(tx_collide_2times);
8879 ESTAT_ADD(tx_collide_3times);
8880 ESTAT_ADD(tx_collide_4times);
8881 ESTAT_ADD(tx_collide_5times);
8882 ESTAT_ADD(tx_collide_6times);
8883 ESTAT_ADD(tx_collide_7times);
8884 ESTAT_ADD(tx_collide_8times);
8885 ESTAT_ADD(tx_collide_9times);
8886 ESTAT_ADD(tx_collide_10times);
8887 ESTAT_ADD(tx_collide_11times);
8888 ESTAT_ADD(tx_collide_12times);
8889 ESTAT_ADD(tx_collide_13times);
8890 ESTAT_ADD(tx_collide_14times);
8891 ESTAT_ADD(tx_collide_15times);
8892 ESTAT_ADD(tx_ucast_packets);
8893 ESTAT_ADD(tx_mcast_packets);
8894 ESTAT_ADD(tx_bcast_packets);
8895 ESTAT_ADD(tx_carrier_sense_errors);
8896 ESTAT_ADD(tx_discards);
8897 ESTAT_ADD(tx_errors);
8898
8899 ESTAT_ADD(dma_writeq_full);
8900 ESTAT_ADD(dma_write_prioq_full);
8901 ESTAT_ADD(rxbds_empty);
8902 ESTAT_ADD(rx_discards);
8903 ESTAT_ADD(rx_errors);
8904 ESTAT_ADD(rx_threshold_hit);
8905
8906 ESTAT_ADD(dma_readq_full);
8907 ESTAT_ADD(dma_read_prioq_full);
8908 ESTAT_ADD(tx_comp_queue_full);
8909
8910 ESTAT_ADD(ring_set_send_prod_index);
8911 ESTAT_ADD(ring_status_update);
8912 ESTAT_ADD(nic_irqs);
8913 ESTAT_ADD(nic_avoided_irqs);
8914 ESTAT_ADD(nic_tx_threshold_hit);
8915
8916 return estats;
8917}
8918
8919static struct net_device_stats *tg3_get_stats(struct net_device *dev)
8920{
8921 struct tg3 *tp = netdev_priv(dev);
8922 struct net_device_stats *stats = &tp->net_stats;
8923 struct net_device_stats *old_stats = &tp->net_stats_prev;
8924 struct tg3_hw_stats *hw_stats = tp->hw_stats;
8925
8926 if (!hw_stats)
8927 return old_stats;
8928
8929 stats->rx_packets = old_stats->rx_packets +
8930 get_stat64(&hw_stats->rx_ucast_packets) +
8931 get_stat64(&hw_stats->rx_mcast_packets) +
8932 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04008933
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934 stats->tx_packets = old_stats->tx_packets +
8935 get_stat64(&hw_stats->tx_ucast_packets) +
8936 get_stat64(&hw_stats->tx_mcast_packets) +
8937 get_stat64(&hw_stats->tx_bcast_packets);
8938
8939 stats->rx_bytes = old_stats->rx_bytes +
8940 get_stat64(&hw_stats->rx_octets);
8941 stats->tx_bytes = old_stats->tx_bytes +
8942 get_stat64(&hw_stats->tx_octets);
8943
8944 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07008945 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008946 stats->tx_errors = old_stats->tx_errors +
8947 get_stat64(&hw_stats->tx_errors) +
8948 get_stat64(&hw_stats->tx_mac_errors) +
8949 get_stat64(&hw_stats->tx_carrier_sense_errors) +
8950 get_stat64(&hw_stats->tx_discards);
8951
8952 stats->multicast = old_stats->multicast +
8953 get_stat64(&hw_stats->rx_mcast_packets);
8954 stats->collisions = old_stats->collisions +
8955 get_stat64(&hw_stats->tx_collisions);
8956
8957 stats->rx_length_errors = old_stats->rx_length_errors +
8958 get_stat64(&hw_stats->rx_frame_too_long_errors) +
8959 get_stat64(&hw_stats->rx_undersize_packets);
8960
8961 stats->rx_over_errors = old_stats->rx_over_errors +
8962 get_stat64(&hw_stats->rxbds_empty);
8963 stats->rx_frame_errors = old_stats->rx_frame_errors +
8964 get_stat64(&hw_stats->rx_align_errors);
8965 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
8966 get_stat64(&hw_stats->tx_discards);
8967 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
8968 get_stat64(&hw_stats->tx_carrier_sense_errors);
8969
8970 stats->rx_crc_errors = old_stats->rx_crc_errors +
8971 calc_crc_errors(tp);
8972
John W. Linville4f63b872005-09-12 14:43:18 -07008973 stats->rx_missed_errors = old_stats->rx_missed_errors +
8974 get_stat64(&hw_stats->rx_discards);
8975
Linus Torvalds1da177e2005-04-16 15:20:36 -07008976 return stats;
8977}
8978
8979static inline u32 calc_crc(unsigned char *buf, int len)
8980{
8981 u32 reg;
8982 u32 tmp;
8983 int j, k;
8984
8985 reg = 0xffffffff;
8986
8987 for (j = 0; j < len; j++) {
8988 reg ^= buf[j];
8989
8990 for (k = 0; k < 8; k++) {
8991 tmp = reg & 0x01;
8992
8993 reg >>= 1;
8994
8995 if (tmp) {
8996 reg ^= 0xedb88320;
8997 }
8998 }
8999 }
9000
9001 return ~reg;
9002}
9003
9004static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9005{
9006 /* accept or reject all multicast frames */
9007 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9008 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9009 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9010 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9011}
9012
9013static void __tg3_set_rx_mode(struct net_device *dev)
9014{
9015 struct tg3 *tp = netdev_priv(dev);
9016 u32 rx_mode;
9017
9018 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9019 RX_MODE_KEEP_VLAN_TAG);
9020
9021 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9022 * flag clear.
9023 */
9024#if TG3_VLAN_TAG_USED
9025 if (!tp->vlgrp &&
9026 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9027 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9028#else
9029 /* By definition, VLAN is disabled always in this
9030 * case.
9031 */
9032 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9033 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9034#endif
9035
9036 if (dev->flags & IFF_PROMISC) {
9037 /* Promiscuous mode. */
9038 rx_mode |= RX_MODE_PROMISC;
9039 } else if (dev->flags & IFF_ALLMULTI) {
9040 /* Accept all multicast. */
9041 tg3_set_multi (tp, 1);
9042 } else if (dev->mc_count < 1) {
9043 /* Reject all multicast. */
9044 tg3_set_multi (tp, 0);
9045 } else {
9046 /* Accept one or more multicast(s). */
9047 struct dev_mc_list *mclist;
9048 unsigned int i;
9049 u32 mc_filter[4] = { 0, };
9050 u32 regidx;
9051 u32 bit;
9052 u32 crc;
9053
9054 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
9055 i++, mclist = mclist->next) {
9056
9057 crc = calc_crc (mclist->dmi_addr, ETH_ALEN);
9058 bit = ~crc & 0x7f;
9059 regidx = (bit & 0x60) >> 5;
9060 bit &= 0x1f;
9061 mc_filter[regidx] |= (1 << bit);
9062 }
9063
9064 tw32(MAC_HASH_REG_0, mc_filter[0]);
9065 tw32(MAC_HASH_REG_1, mc_filter[1]);
9066 tw32(MAC_HASH_REG_2, mc_filter[2]);
9067 tw32(MAC_HASH_REG_3, mc_filter[3]);
9068 }
9069
9070 if (rx_mode != tp->rx_mode) {
9071 tp->rx_mode = rx_mode;
9072 tw32_f(MAC_RX_MODE, rx_mode);
9073 udelay(10);
9074 }
9075}
9076
9077static void tg3_set_rx_mode(struct net_device *dev)
9078{
9079 struct tg3 *tp = netdev_priv(dev);
9080
Michael Chane75f7c92006-03-20 21:33:26 -08009081 if (!netif_running(dev))
9082 return;
9083
David S. Millerf47c11e2005-06-24 20:18:35 -07009084 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009085 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009086 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009087}
9088
9089#define TG3_REGDUMP_LEN (32 * 1024)
9090
9091static int tg3_get_regs_len(struct net_device *dev)
9092{
9093 return TG3_REGDUMP_LEN;
9094}
9095
9096static void tg3_get_regs(struct net_device *dev,
9097 struct ethtool_regs *regs, void *_p)
9098{
9099 u32 *p = _p;
9100 struct tg3 *tp = netdev_priv(dev);
9101 u8 *orig_p = _p;
9102 int i;
9103
9104 regs->version = 0;
9105
9106 memset(p, 0, TG3_REGDUMP_LEN);
9107
Michael Chanbc1c7562006-03-20 17:48:03 -08009108 if (tp->link_config.phy_is_low_power)
9109 return;
9110
David S. Millerf47c11e2005-06-24 20:18:35 -07009111 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009112
9113#define __GET_REG32(reg) (*(p)++ = tr32(reg))
9114#define GET_REG32_LOOP(base,len) \
9115do { p = (u32 *)(orig_p + (base)); \
9116 for (i = 0; i < len; i += 4) \
9117 __GET_REG32((base) + i); \
9118} while (0)
9119#define GET_REG32_1(reg) \
9120do { p = (u32 *)(orig_p + (reg)); \
9121 __GET_REG32((reg)); \
9122} while (0)
9123
9124 GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
9125 GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
9126 GET_REG32_LOOP(MAC_MODE, 0x4f0);
9127 GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
9128 GET_REG32_1(SNDDATAC_MODE);
9129 GET_REG32_LOOP(SNDBDS_MODE, 0x80);
9130 GET_REG32_LOOP(SNDBDI_MODE, 0x48);
9131 GET_REG32_1(SNDBDC_MODE);
9132 GET_REG32_LOOP(RCVLPC_MODE, 0x20);
9133 GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
9134 GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
9135 GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
9136 GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
9137 GET_REG32_1(RCVDCC_MODE);
9138 GET_REG32_LOOP(RCVBDI_MODE, 0x20);
9139 GET_REG32_LOOP(RCVCC_MODE, 0x14);
9140 GET_REG32_LOOP(RCVLSC_MODE, 0x08);
9141 GET_REG32_1(MBFREE_MODE);
9142 GET_REG32_LOOP(HOSTCC_MODE, 0x100);
9143 GET_REG32_LOOP(MEMARB_MODE, 0x10);
9144 GET_REG32_LOOP(BUFMGR_MODE, 0x58);
9145 GET_REG32_LOOP(RDMAC_MODE, 0x08);
9146 GET_REG32_LOOP(WDMAC_MODE, 0x08);
Chris Elmquist091465d2005-12-20 13:25:19 -08009147 GET_REG32_1(RX_CPU_MODE);
9148 GET_REG32_1(RX_CPU_STATE);
9149 GET_REG32_1(RX_CPU_PGMCTR);
9150 GET_REG32_1(RX_CPU_HWBKPT);
9151 GET_REG32_1(TX_CPU_MODE);
9152 GET_REG32_1(TX_CPU_STATE);
9153 GET_REG32_1(TX_CPU_PGMCTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009154 GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
9155 GET_REG32_LOOP(FTQ_RESET, 0x120);
9156 GET_REG32_LOOP(MSGINT_MODE, 0x0c);
9157 GET_REG32_1(DMAC_MODE);
9158 GET_REG32_LOOP(GRC_MODE, 0x4c);
9159 if (tp->tg3_flags & TG3_FLAG_NVRAM)
9160 GET_REG32_LOOP(NVRAM_CMD, 0x24);
9161
9162#undef __GET_REG32
9163#undef GET_REG32_LOOP
9164#undef GET_REG32_1
9165
David S. Millerf47c11e2005-06-24 20:18:35 -07009166 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009167}
9168
9169static int tg3_get_eeprom_len(struct net_device *dev)
9170{
9171 struct tg3 *tp = netdev_priv(dev);
9172
9173 return tp->nvram_size;
9174}
9175
Linus Torvalds1da177e2005-04-16 15:20:36 -07009176static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9177{
9178 struct tg3 *tp = netdev_priv(dev);
9179 int ret;
9180 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009181 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009182 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009183
Matt Carlsondf259d82009-04-20 06:57:14 +00009184 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9185 return -EINVAL;
9186
Michael Chanbc1c7562006-03-20 17:48:03 -08009187 if (tp->link_config.phy_is_low_power)
9188 return -EAGAIN;
9189
Linus Torvalds1da177e2005-04-16 15:20:36 -07009190 offset = eeprom->offset;
9191 len = eeprom->len;
9192 eeprom->len = 0;
9193
9194 eeprom->magic = TG3_EEPROM_MAGIC;
9195
9196 if (offset & 3) {
9197 /* adjustments to start on required 4 byte boundary */
9198 b_offset = offset & 3;
9199 b_count = 4 - b_offset;
9200 if (b_count > len) {
9201 /* i.e. offset=1 len=2 */
9202 b_count = len;
9203 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009204 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009205 if (ret)
9206 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009207 memcpy(data, ((char*)&val) + b_offset, b_count);
9208 len -= b_count;
9209 offset += b_count;
9210 eeprom->len += b_count;
9211 }
9212
9213 /* read bytes upto the last 4 byte boundary */
9214 pd = &data[eeprom->len];
9215 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009216 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009217 if (ret) {
9218 eeprom->len += i;
9219 return ret;
9220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009221 memcpy(pd + i, &val, 4);
9222 }
9223 eeprom->len += i;
9224
9225 if (len & 3) {
9226 /* read last bytes not ending on 4 byte boundary */
9227 pd = &data[eeprom->len];
9228 b_count = len & 3;
9229 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009230 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009231 if (ret)
9232 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009233 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009234 eeprom->len += b_count;
9235 }
9236 return 0;
9237}
9238
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009239static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009240
9241static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9242{
9243 struct tg3 *tp = netdev_priv(dev);
9244 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009245 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009246 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009247 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009248
Michael Chanbc1c7562006-03-20 17:48:03 -08009249 if (tp->link_config.phy_is_low_power)
9250 return -EAGAIN;
9251
Matt Carlsondf259d82009-04-20 06:57:14 +00009252 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9253 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009254 return -EINVAL;
9255
9256 offset = eeprom->offset;
9257 len = eeprom->len;
9258
9259 if ((b_offset = (offset & 3))) {
9260 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009261 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009262 if (ret)
9263 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009264 len += b_offset;
9265 offset &= ~3;
Michael Chan1c8594b42005-04-21 17:12:46 -07009266 if (len < 4)
9267 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009268 }
9269
9270 odd_len = 0;
Michael Chan1c8594b42005-04-21 17:12:46 -07009271 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009272 /* adjustments to end on required 4 byte boundary */
9273 odd_len = 1;
9274 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009275 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009276 if (ret)
9277 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009278 }
9279
9280 buf = data;
9281 if (b_offset || odd_len) {
9282 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009283 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009284 return -ENOMEM;
9285 if (b_offset)
9286 memcpy(buf, &start, 4);
9287 if (odd_len)
9288 memcpy(buf+len-4, &end, 4);
9289 memcpy(buf + b_offset, data, eeprom->len);
9290 }
9291
9292 ret = tg3_nvram_write_block(tp, offset, len, buf);
9293
9294 if (buf != data)
9295 kfree(buf);
9296
9297 return ret;
9298}
9299
9300static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9301{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009302 struct tg3 *tp = netdev_priv(dev);
9303
9304 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009305 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009306 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9307 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009308 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9309 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009310 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009311
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312 cmd->supported = (SUPPORTED_Autoneg);
9313
9314 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
9315 cmd->supported |= (SUPPORTED_1000baseT_Half |
9316 SUPPORTED_1000baseT_Full);
9317
Karsten Keilef348142006-05-12 12:49:08 -07009318 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009319 cmd->supported |= (SUPPORTED_100baseT_Half |
9320 SUPPORTED_100baseT_Full |
9321 SUPPORTED_10baseT_Half |
9322 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009323 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009324 cmd->port = PORT_TP;
9325 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009326 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009327 cmd->port = PORT_FIBRE;
9328 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009329
Linus Torvalds1da177e2005-04-16 15:20:36 -07009330 cmd->advertising = tp->link_config.advertising;
9331 if (netif_running(dev)) {
9332 cmd->speed = tp->link_config.active_speed;
9333 cmd->duplex = tp->link_config.active_duplex;
9334 }
Matt Carlson882e9792009-09-01 13:21:36 +00009335 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009336 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009337 cmd->autoneg = tp->link_config.autoneg;
9338 cmd->maxtxpkt = 0;
9339 cmd->maxrxpkt = 0;
9340 return 0;
9341}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009342
Linus Torvalds1da177e2005-04-16 15:20:36 -07009343static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9344{
9345 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009346
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009347 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009348 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009349 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9350 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009351 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9352 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009353 }
9354
Matt Carlson7e5856b2009-02-25 14:23:01 +00009355 if (cmd->autoneg != AUTONEG_ENABLE &&
9356 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009357 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009358
9359 if (cmd->autoneg == AUTONEG_DISABLE &&
9360 cmd->duplex != DUPLEX_FULL &&
9361 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009362 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009363
Matt Carlson7e5856b2009-02-25 14:23:01 +00009364 if (cmd->autoneg == AUTONEG_ENABLE) {
9365 u32 mask = ADVERTISED_Autoneg |
9366 ADVERTISED_Pause |
9367 ADVERTISED_Asym_Pause;
9368
9369 if (!(tp->tg3_flags2 & TG3_FLAG_10_100_ONLY))
9370 mask |= ADVERTISED_1000baseT_Half |
9371 ADVERTISED_1000baseT_Full;
9372
9373 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
9374 mask |= ADVERTISED_100baseT_Half |
9375 ADVERTISED_100baseT_Full |
9376 ADVERTISED_10baseT_Half |
9377 ADVERTISED_10baseT_Full |
9378 ADVERTISED_TP;
9379 else
9380 mask |= ADVERTISED_FIBRE;
9381
9382 if (cmd->advertising & ~mask)
9383 return -EINVAL;
9384
9385 mask &= (ADVERTISED_1000baseT_Half |
9386 ADVERTISED_1000baseT_Full |
9387 ADVERTISED_100baseT_Half |
9388 ADVERTISED_100baseT_Full |
9389 ADVERTISED_10baseT_Half |
9390 ADVERTISED_10baseT_Full);
9391
9392 cmd->advertising &= mask;
9393 } else {
9394 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) {
9395 if (cmd->speed != SPEED_1000)
9396 return -EINVAL;
9397
9398 if (cmd->duplex != DUPLEX_FULL)
9399 return -EINVAL;
9400 } else {
9401 if (cmd->speed != SPEED_100 &&
9402 cmd->speed != SPEED_10)
9403 return -EINVAL;
9404 }
9405 }
9406
David S. Millerf47c11e2005-06-24 20:18:35 -07009407 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009408
9409 tp->link_config.autoneg = cmd->autoneg;
9410 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -07009411 tp->link_config.advertising = (cmd->advertising |
9412 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009413 tp->link_config.speed = SPEED_INVALID;
9414 tp->link_config.duplex = DUPLEX_INVALID;
9415 } else {
9416 tp->link_config.advertising = 0;
9417 tp->link_config.speed = cmd->speed;
9418 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009419 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009420
Michael Chan24fcad62006-12-17 17:06:46 -08009421 tp->link_config.orig_speed = tp->link_config.speed;
9422 tp->link_config.orig_duplex = tp->link_config.duplex;
9423 tp->link_config.orig_autoneg = tp->link_config.autoneg;
9424
Linus Torvalds1da177e2005-04-16 15:20:36 -07009425 if (netif_running(dev))
9426 tg3_setup_phy(tp, 1);
9427
David S. Millerf47c11e2005-06-24 20:18:35 -07009428 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009429
Linus Torvalds1da177e2005-04-16 15:20:36 -07009430 return 0;
9431}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009432
Linus Torvalds1da177e2005-04-16 15:20:36 -07009433static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
9434{
9435 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009436
Linus Torvalds1da177e2005-04-16 15:20:36 -07009437 strcpy(info->driver, DRV_MODULE_NAME);
9438 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -08009439 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009440 strcpy(info->bus_info, pci_name(tp->pdev));
9441}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009442
Linus Torvalds1da177e2005-04-16 15:20:36 -07009443static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9444{
9445 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009446
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009447 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
9448 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -07009449 wol->supported = WAKE_MAGIC;
9450 else
9451 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009452 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -08009453 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
9454 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009455 wol->wolopts = WAKE_MAGIC;
9456 memset(&wol->sopass, 0, sizeof(wol->sopass));
9457}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009458
Linus Torvalds1da177e2005-04-16 15:20:36 -07009459static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9460{
9461 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009462 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009463
Linus Torvalds1da177e2005-04-16 15:20:36 -07009464 if (wol->wolopts & ~WAKE_MAGIC)
9465 return -EINVAL;
9466 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009467 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009468 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009469
David S. Millerf47c11e2005-06-24 20:18:35 -07009470 spin_lock_bh(&tp->lock);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009471 if (wol->wolopts & WAKE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009472 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009473 device_set_wakeup_enable(dp, true);
9474 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009475 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
Rafael J. Wysocki12dac072008-07-30 16:37:33 -07009476 device_set_wakeup_enable(dp, false);
9477 }
David S. Millerf47c11e2005-06-24 20:18:35 -07009478 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009479
Linus Torvalds1da177e2005-04-16 15:20:36 -07009480 return 0;
9481}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009482
Linus Torvalds1da177e2005-04-16 15:20:36 -07009483static u32 tg3_get_msglevel(struct net_device *dev)
9484{
9485 struct tg3 *tp = netdev_priv(dev);
9486 return tp->msg_enable;
9487}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009488
Linus Torvalds1da177e2005-04-16 15:20:36 -07009489static void tg3_set_msglevel(struct net_device *dev, u32 value)
9490{
9491 struct tg3 *tp = netdev_priv(dev);
9492 tp->msg_enable = value;
9493}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009494
Linus Torvalds1da177e2005-04-16 15:20:36 -07009495static int tg3_set_tso(struct net_device *dev, u32 value)
9496{
9497 struct tg3 *tp = netdev_priv(dev);
9498
9499 if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
9500 if (value)
9501 return -EINVAL;
9502 return 0;
9503 }
Matt Carlson027455a2008-12-21 20:19:30 -08009504 if ((dev->features & NETIF_F_IPV6_CSUM) &&
9505 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -07009506 if (value) {
Michael Chanb0026622006-07-03 19:42:14 -07009507 dev->features |= NETIF_F_TSO6;
Matt Carlson57e69832008-05-25 23:48:31 -07009508 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
9509 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
9510 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009511 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009512 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
9513 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009514 dev->features |= NETIF_F_TSO_ECN;
9515 } else
9516 dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
Michael Chanb0026622006-07-03 19:42:14 -07009517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009518 return ethtool_op_set_tso(dev, value);
9519}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009520
Linus Torvalds1da177e2005-04-16 15:20:36 -07009521static int tg3_nway_reset(struct net_device *dev)
9522{
9523 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009524 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009525
Linus Torvalds1da177e2005-04-16 15:20:36 -07009526 if (!netif_running(dev))
9527 return -EAGAIN;
9528
Michael Chanc94e3942005-09-27 12:12:42 -07009529 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
9530 return -EINVAL;
9531
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009532 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9533 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9534 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009535 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009536 } else {
9537 u32 bmcr;
9538
9539 spin_lock_bh(&tp->lock);
9540 r = -EINVAL;
9541 tg3_readphy(tp, MII_BMCR, &bmcr);
9542 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
9543 ((bmcr & BMCR_ANENABLE) ||
9544 (tp->tg3_flags2 & TG3_FLG2_PARALLEL_DETECT))) {
9545 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
9546 BMCR_ANENABLE);
9547 r = 0;
9548 }
9549 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009550 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009551
Linus Torvalds1da177e2005-04-16 15:20:36 -07009552 return r;
9553}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009554
Linus Torvalds1da177e2005-04-16 15:20:36 -07009555static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9556{
9557 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009558
Linus Torvalds1da177e2005-04-16 15:20:36 -07009559 ering->rx_max_pending = TG3_RX_RING_SIZE - 1;
9560 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009561 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9562 ering->rx_jumbo_max_pending = TG3_RX_JUMBO_RING_SIZE - 1;
9563 else
9564 ering->rx_jumbo_max_pending = 0;
9565
9566 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009567
9568 ering->rx_pending = tp->rx_pending;
9569 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -08009570 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
9571 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
9572 else
9573 ering->rx_jumbo_pending = 0;
9574
Matt Carlsonf3f3f272009-08-28 14:03:21 +00009575 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009576}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009577
Linus Torvalds1da177e2005-04-16 15:20:36 -07009578static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
9579{
9580 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +00009581 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009582
Linus Torvalds1da177e2005-04-16 15:20:36 -07009583 if ((ering->rx_pending > TG3_RX_RING_SIZE - 1) ||
9584 (ering->rx_jumbo_pending > TG3_RX_JUMBO_RING_SIZE - 1) ||
Michael Chanbc3a9252006-10-18 20:55:18 -07009585 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
9586 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -08009587 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -07009588 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009589 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009590
Michael Chanbbe832c2005-06-24 20:20:04 -07009591 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009592 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009593 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009594 irq_sync = 1;
9595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009596
Michael Chanbbe832c2005-06-24 20:20:04 -07009597 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009598
Linus Torvalds1da177e2005-04-16 15:20:36 -07009599 tp->rx_pending = ering->rx_pending;
9600
9601 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
9602 tp->rx_pending > 63)
9603 tp->rx_pending = 63;
9604 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +00009605
9606 for (i = 0; i < TG3_IRQ_MAX_VECS; i++)
9607 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009608
9609 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -07009610 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -07009611 err = tg3_restart_hw(tp, 1);
9612 if (!err)
9613 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009614 }
9615
David S. Millerf47c11e2005-06-24 20:18:35 -07009616 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009617
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009618 if (irq_sync && !err)
9619 tg3_phy_start(tp);
9620
Michael Chanb9ec6c12006-07-25 16:37:27 -07009621 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009622}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009623
Linus Torvalds1da177e2005-04-16 15:20:36 -07009624static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9625{
9626 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009627
Linus Torvalds1da177e2005-04-16 15:20:36 -07009628 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -08009629
Steve Glendinninge18ce342008-12-16 02:00:00 -08009630 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -08009631 epause->rx_pause = 1;
9632 else
9633 epause->rx_pause = 0;
9634
Steve Glendinninge18ce342008-12-16 02:00:00 -08009635 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -08009636 epause->tx_pause = 1;
9637 else
9638 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009639}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009640
Linus Torvalds1da177e2005-04-16 15:20:36 -07009641static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
9642{
9643 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009644 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009645
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009646 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
9647 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
9648 return -EAGAIN;
9649
9650 if (epause->autoneg) {
9651 u32 newadv;
9652 struct phy_device *phydev;
9653
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009654 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009655
9656 if (epause->rx_pause) {
9657 if (epause->tx_pause)
9658 newadv = ADVERTISED_Pause;
9659 else
9660 newadv = ADVERTISED_Pause |
9661 ADVERTISED_Asym_Pause;
9662 } else if (epause->tx_pause) {
9663 newadv = ADVERTISED_Asym_Pause;
9664 } else
9665 newadv = 0;
9666
9667 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
9668 u32 oldadv = phydev->advertising &
9669 (ADVERTISED_Pause |
9670 ADVERTISED_Asym_Pause);
9671 if (oldadv != newadv) {
9672 phydev->advertising &=
9673 ~(ADVERTISED_Pause |
9674 ADVERTISED_Asym_Pause);
9675 phydev->advertising |= newadv;
9676 err = phy_start_aneg(phydev);
9677 }
9678 } else {
9679 tp->link_config.advertising &=
9680 ~(ADVERTISED_Pause |
9681 ADVERTISED_Asym_Pause);
9682 tp->link_config.advertising |= newadv;
9683 }
9684 } else {
9685 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009686 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009687 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009688 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009689
9690 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009691 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009692 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009693 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009694
9695 if (netif_running(dev))
9696 tg3_setup_flow_control(tp, 0, 0);
9697 }
9698 } else {
9699 int irq_sync = 0;
9700
9701 if (netif_running(dev)) {
9702 tg3_netif_stop(tp);
9703 irq_sync = 1;
9704 }
9705
9706 tg3_full_lock(tp, irq_sync);
9707
9708 if (epause->autoneg)
9709 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
9710 else
9711 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
9712 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009713 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009714 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009715 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009716 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -08009717 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009718 else
Steve Glendinninge18ce342008-12-16 02:00:00 -08009719 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009720
9721 if (netif_running(dev)) {
9722 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9723 err = tg3_restart_hw(tp, 1);
9724 if (!err)
9725 tg3_netif_start(tp);
9726 }
9727
9728 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -07009729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009730
Michael Chanb9ec6c12006-07-25 16:37:27 -07009731 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009732}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009733
Linus Torvalds1da177e2005-04-16 15:20:36 -07009734static u32 tg3_get_rx_csum(struct net_device *dev)
9735{
9736 struct tg3 *tp = netdev_priv(dev);
9737 return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
9738}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009739
Linus Torvalds1da177e2005-04-16 15:20:36 -07009740static int tg3_set_rx_csum(struct net_device *dev, u32 data)
9741{
9742 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009743
Linus Torvalds1da177e2005-04-16 15:20:36 -07009744 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
9745 if (data != 0)
9746 return -EINVAL;
9747 return 0;
9748 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009749
David S. Millerf47c11e2005-06-24 20:18:35 -07009750 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009751 if (data)
9752 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
9753 else
9754 tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
David S. Millerf47c11e2005-06-24 20:18:35 -07009755 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009756
Linus Torvalds1da177e2005-04-16 15:20:36 -07009757 return 0;
9758}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009759
Linus Torvalds1da177e2005-04-16 15:20:36 -07009760static int tg3_set_tx_csum(struct net_device *dev, u32 data)
9761{
9762 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009763
Linus Torvalds1da177e2005-04-16 15:20:36 -07009764 if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
9765 if (data != 0)
9766 return -EINVAL;
9767 return 0;
9768 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009769
Matt Carlson321d32a2008-11-21 17:22:19 -08009770 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chan6460d942007-07-14 19:07:52 -07009771 ethtool_op_set_tx_ipv6_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009772 else
Michael Chan9c27dbd2006-03-20 22:28:27 -08009773 ethtool_op_set_tx_csum(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009774
9775 return 0;
9776}
9777
Jeff Garzikb9f2c042007-10-03 18:07:32 -07009778static int tg3_get_sset_count (struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009779{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07009780 switch (sset) {
9781 case ETH_SS_TEST:
9782 return TG3_NUM_TEST;
9783 case ETH_SS_STATS:
9784 return TG3_NUM_STATS;
9785 default:
9786 return -EOPNOTSUPP;
9787 }
Michael Chan4cafd3f2005-05-29 14:56:34 -07009788}
9789
Linus Torvalds1da177e2005-04-16 15:20:36 -07009790static void tg3_get_strings (struct net_device *dev, u32 stringset, u8 *buf)
9791{
9792 switch (stringset) {
9793 case ETH_SS_STATS:
9794 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
9795 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -07009796 case ETH_SS_TEST:
9797 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
9798 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009799 default:
9800 WARN_ON(1); /* we need a WARN() */
9801 break;
9802 }
9803}
9804
Michael Chan4009a932005-09-05 17:52:54 -07009805static int tg3_phys_id(struct net_device *dev, u32 data)
9806{
9807 struct tg3 *tp = netdev_priv(dev);
9808 int i;
9809
9810 if (!netif_running(tp->dev))
9811 return -EAGAIN;
9812
9813 if (data == 0)
Stephen Hemminger759afc32008-02-23 19:51:59 -08009814 data = UINT_MAX / 2;
Michael Chan4009a932005-09-05 17:52:54 -07009815
9816 for (i = 0; i < (data * 2); i++) {
9817 if ((i % 2) == 0)
9818 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
9819 LED_CTRL_1000MBPS_ON |
9820 LED_CTRL_100MBPS_ON |
9821 LED_CTRL_10MBPS_ON |
9822 LED_CTRL_TRAFFIC_OVERRIDE |
9823 LED_CTRL_TRAFFIC_BLINK |
9824 LED_CTRL_TRAFFIC_LED);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009825
Michael Chan4009a932005-09-05 17:52:54 -07009826 else
9827 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
9828 LED_CTRL_TRAFFIC_OVERRIDE);
9829
9830 if (msleep_interruptible(500))
9831 break;
9832 }
9833 tw32(MAC_LED_CTRL, tp->led_ctrl);
9834 return 0;
9835}
9836
Linus Torvalds1da177e2005-04-16 15:20:36 -07009837static void tg3_get_ethtool_stats (struct net_device *dev,
9838 struct ethtool_stats *estats, u64 *tmp_stats)
9839{
9840 struct tg3 *tp = netdev_priv(dev);
9841 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
9842}
9843
Michael Chan566f86a2005-05-29 14:56:58 -07009844#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -08009845#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
9846#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
9847#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -07009848#define NVRAM_SELFBOOT_HW_SIZE 0x20
9849#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -07009850
9851static int tg3_test_nvram(struct tg3 *tp)
9852{
Al Virob9fc7dc2007-12-17 22:59:57 -08009853 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009854 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009855 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -07009856
Matt Carlsondf259d82009-04-20 06:57:14 +00009857 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9858 return 0;
9859
Matt Carlsone4f34112009-02-25 14:25:00 +00009860 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -08009861 return -EIO;
9862
Michael Chan1b277772006-03-20 22:27:48 -08009863 if (magic == TG3_EEPROM_MAGIC)
9864 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -07009865 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -08009866 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
9867 TG3_EEPROM_SB_FORMAT_1) {
9868 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
9869 case TG3_EEPROM_SB_REVISION_0:
9870 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
9871 break;
9872 case TG3_EEPROM_SB_REVISION_2:
9873 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
9874 break;
9875 case TG3_EEPROM_SB_REVISION_3:
9876 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
9877 break;
9878 default:
9879 return 0;
9880 }
9881 } else
Michael Chan1b277772006-03-20 22:27:48 -08009882 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -07009883 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
9884 size = NVRAM_SELFBOOT_HW_SIZE;
9885 else
Michael Chan1b277772006-03-20 22:27:48 -08009886 return -EIO;
9887
9888 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -07009889 if (buf == NULL)
9890 return -ENOMEM;
9891
Michael Chan1b277772006-03-20 22:27:48 -08009892 err = -EIO;
9893 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009894 err = tg3_nvram_read_be32(tp, i, &buf[j]);
9895 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -07009896 break;
Michael Chan566f86a2005-05-29 14:56:58 -07009897 }
Michael Chan1b277772006-03-20 22:27:48 -08009898 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -07009899 goto out;
9900
Michael Chan1b277772006-03-20 22:27:48 -08009901 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009902 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -08009903 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -07009904 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -08009905 u8 *buf8 = (u8 *) buf, csum8 = 0;
9906
Al Virob9fc7dc2007-12-17 22:59:57 -08009907 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -08009908 TG3_EEPROM_SB_REVISION_2) {
9909 /* For rev 2, the csum doesn't include the MBA. */
9910 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
9911 csum8 += buf8[i];
9912 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
9913 csum8 += buf8[i];
9914 } else {
9915 for (i = 0; i < size; i++)
9916 csum8 += buf8[i];
9917 }
Michael Chan1b277772006-03-20 22:27:48 -08009918
Adrian Bunkad96b482006-04-05 22:21:04 -07009919 if (csum8 == 0) {
9920 err = 0;
9921 goto out;
9922 }
9923
9924 err = -EIO;
9925 goto out;
Michael Chan1b277772006-03-20 22:27:48 -08009926 }
Michael Chan566f86a2005-05-29 14:56:58 -07009927
Al Virob9fc7dc2007-12-17 22:59:57 -08009928 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -07009929 TG3_EEPROM_MAGIC_HW) {
9930 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +00009931 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -07009932 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -07009933
9934 /* Separate the parity bits and the data bytes. */
9935 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
9936 if ((i == 0) || (i == 8)) {
9937 int l;
9938 u8 msk;
9939
9940 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
9941 parity[k++] = buf8[i] & msk;
9942 i++;
9943 }
9944 else if (i == 16) {
9945 int l;
9946 u8 msk;
9947
9948 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
9949 parity[k++] = buf8[i] & msk;
9950 i++;
9951
9952 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
9953 parity[k++] = buf8[i] & msk;
9954 i++;
9955 }
9956 data[j++] = buf8[i];
9957 }
9958
9959 err = -EIO;
9960 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
9961 u8 hw8 = hweight8(data[i]);
9962
9963 if ((hw8 & 0x1) && parity[i])
9964 goto out;
9965 else if (!(hw8 & 0x1) && !parity[i])
9966 goto out;
9967 }
9968 err = 0;
9969 goto out;
9970 }
9971
Michael Chan566f86a2005-05-29 14:56:58 -07009972 /* Bootstrap checksum at offset 0x10 */
9973 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlsona9dc5292009-02-25 14:25:30 +00009974 if (csum != be32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -07009975 goto out;
9976
9977 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
9978 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlsona9dc5292009-02-25 14:25:30 +00009979 if (csum != be32_to_cpu(buf[0xfc/4]))
9980 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -07009981
9982 err = 0;
9983
9984out:
9985 kfree(buf);
9986 return err;
9987}
9988
Michael Chanca430072005-05-29 14:57:23 -07009989#define TG3_SERDES_TIMEOUT_SEC 2
9990#define TG3_COPPER_TIMEOUT_SEC 6
9991
9992static int tg3_test_link(struct tg3 *tp)
9993{
9994 int i, max;
9995
9996 if (!netif_running(tp->dev))
9997 return -ENODEV;
9998
Michael Chan4c987482005-09-05 17:52:38 -07009999 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010000 max = TG3_SERDES_TIMEOUT_SEC;
10001 else
10002 max = TG3_COPPER_TIMEOUT_SEC;
10003
10004 for (i = 0; i < max; i++) {
10005 if (netif_carrier_ok(tp->dev))
10006 return 0;
10007
10008 if (msleep_interruptible(1000))
10009 break;
10010 }
10011
10012 return -EIO;
10013}
10014
Michael Chana71116d2005-05-29 14:58:11 -070010015/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010016static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010017{
Michael Chanb16250e2006-09-27 16:10:14 -070010018 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010019 u32 offset, read_mask, write_mask, val, save_val, read_val;
10020 static struct {
10021 u16 offset;
10022 u16 flags;
10023#define TG3_FL_5705 0x1
10024#define TG3_FL_NOT_5705 0x2
10025#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010026#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010027 u32 read_mask;
10028 u32 write_mask;
10029 } reg_tbl[] = {
10030 /* MAC Control Registers */
10031 { MAC_MODE, TG3_FL_NOT_5705,
10032 0x00000000, 0x00ef6f8c },
10033 { MAC_MODE, TG3_FL_5705,
10034 0x00000000, 0x01ef6b8c },
10035 { MAC_STATUS, TG3_FL_NOT_5705,
10036 0x03800107, 0x00000000 },
10037 { MAC_STATUS, TG3_FL_5705,
10038 0x03800100, 0x00000000 },
10039 { MAC_ADDR_0_HIGH, 0x0000,
10040 0x00000000, 0x0000ffff },
10041 { MAC_ADDR_0_LOW, 0x0000,
10042 0x00000000, 0xffffffff },
10043 { MAC_RX_MTU_SIZE, 0x0000,
10044 0x00000000, 0x0000ffff },
10045 { MAC_TX_MODE, 0x0000,
10046 0x00000000, 0x00000070 },
10047 { MAC_TX_LENGTHS, 0x0000,
10048 0x00000000, 0x00003fff },
10049 { MAC_RX_MODE, TG3_FL_NOT_5705,
10050 0x00000000, 0x000007fc },
10051 { MAC_RX_MODE, TG3_FL_5705,
10052 0x00000000, 0x000007dc },
10053 { MAC_HASH_REG_0, 0x0000,
10054 0x00000000, 0xffffffff },
10055 { MAC_HASH_REG_1, 0x0000,
10056 0x00000000, 0xffffffff },
10057 { MAC_HASH_REG_2, 0x0000,
10058 0x00000000, 0xffffffff },
10059 { MAC_HASH_REG_3, 0x0000,
10060 0x00000000, 0xffffffff },
10061
10062 /* Receive Data and Receive BD Initiator Control Registers. */
10063 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10064 0x00000000, 0xffffffff },
10065 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10066 0x00000000, 0xffffffff },
10067 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10068 0x00000000, 0x00000003 },
10069 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10070 0x00000000, 0xffffffff },
10071 { RCVDBDI_STD_BD+0, 0x0000,
10072 0x00000000, 0xffffffff },
10073 { RCVDBDI_STD_BD+4, 0x0000,
10074 0x00000000, 0xffffffff },
10075 { RCVDBDI_STD_BD+8, 0x0000,
10076 0x00000000, 0xffff0002 },
10077 { RCVDBDI_STD_BD+0xc, 0x0000,
10078 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010079
Michael Chana71116d2005-05-29 14:58:11 -070010080 /* Receive BD Initiator Control Registers. */
10081 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10082 0x00000000, 0xffffffff },
10083 { RCVBDI_STD_THRESH, TG3_FL_5705,
10084 0x00000000, 0x000003ff },
10085 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10086 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010087
Michael Chana71116d2005-05-29 14:58:11 -070010088 /* Host Coalescing Control Registers. */
10089 { HOSTCC_MODE, TG3_FL_NOT_5705,
10090 0x00000000, 0x00000004 },
10091 { HOSTCC_MODE, TG3_FL_5705,
10092 0x00000000, 0x000000f6 },
10093 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10094 0x00000000, 0xffffffff },
10095 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10096 0x00000000, 0x000003ff },
10097 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10098 0x00000000, 0xffffffff },
10099 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10100 0x00000000, 0x000003ff },
10101 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10102 0x00000000, 0xffffffff },
10103 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10104 0x00000000, 0x000000ff },
10105 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10106 0x00000000, 0xffffffff },
10107 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10108 0x00000000, 0x000000ff },
10109 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10110 0x00000000, 0xffffffff },
10111 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10112 0x00000000, 0xffffffff },
10113 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10114 0x00000000, 0xffffffff },
10115 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10116 0x00000000, 0x000000ff },
10117 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10118 0x00000000, 0xffffffff },
10119 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10120 0x00000000, 0x000000ff },
10121 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10122 0x00000000, 0xffffffff },
10123 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10124 0x00000000, 0xffffffff },
10125 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10126 0x00000000, 0xffffffff },
10127 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10128 0x00000000, 0xffffffff },
10129 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10130 0x00000000, 0xffffffff },
10131 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10132 0xffffffff, 0x00000000 },
10133 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10134 0xffffffff, 0x00000000 },
10135
10136 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010137 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010138 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010139 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010140 0x00000000, 0x007fffff },
10141 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10142 0x00000000, 0x0000003f },
10143 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10144 0x00000000, 0x000001ff },
10145 { BUFMGR_MB_HIGH_WATER, 0x0000,
10146 0x00000000, 0x000001ff },
10147 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10148 0xffffffff, 0x00000000 },
10149 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10150 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010151
Michael Chana71116d2005-05-29 14:58:11 -070010152 /* Mailbox Registers */
10153 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10154 0x00000000, 0x000001ff },
10155 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10156 0x00000000, 0x000001ff },
10157 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10158 0x00000000, 0x000007ff },
10159 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10160 0x00000000, 0x000001ff },
10161
10162 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10163 };
10164
Michael Chanb16250e2006-09-27 16:10:14 -070010165 is_5705 = is_5750 = 0;
10166 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010167 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010168 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10169 is_5750 = 1;
10170 }
Michael Chana71116d2005-05-29 14:58:11 -070010171
10172 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10173 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10174 continue;
10175
10176 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10177 continue;
10178
10179 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10180 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10181 continue;
10182
Michael Chanb16250e2006-09-27 16:10:14 -070010183 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10184 continue;
10185
Michael Chana71116d2005-05-29 14:58:11 -070010186 offset = (u32) reg_tbl[i].offset;
10187 read_mask = reg_tbl[i].read_mask;
10188 write_mask = reg_tbl[i].write_mask;
10189
10190 /* Save the original register content */
10191 save_val = tr32(offset);
10192
10193 /* Determine the read-only value. */
10194 read_val = save_val & read_mask;
10195
10196 /* Write zero to the register, then make sure the read-only bits
10197 * are not changed and the read/write bits are all zeros.
10198 */
10199 tw32(offset, 0);
10200
10201 val = tr32(offset);
10202
10203 /* Test the read-only and read/write bits. */
10204 if (((val & read_mask) != read_val) || (val & write_mask))
10205 goto out;
10206
10207 /* Write ones to all the bits defined by RdMask and WrMask, then
10208 * make sure the read-only bits are not changed and the
10209 * read/write bits are all ones.
10210 */
10211 tw32(offset, read_mask | write_mask);
10212
10213 val = tr32(offset);
10214
10215 /* Test the read-only bits. */
10216 if ((val & read_mask) != read_val)
10217 goto out;
10218
10219 /* Test the read/write bits. */
10220 if ((val & write_mask) != write_mask)
10221 goto out;
10222
10223 tw32(offset, save_val);
10224 }
10225
10226 return 0;
10227
10228out:
Michael Chan9f88f292006-12-07 00:22:54 -080010229 if (netif_msg_hw(tp))
10230 printk(KERN_ERR PFX "Register test failed at offset %x\n",
10231 offset);
Michael Chana71116d2005-05-29 14:58:11 -070010232 tw32(offset, save_val);
10233 return -EIO;
10234}
10235
Michael Chan7942e1d2005-05-29 14:58:36 -070010236static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10237{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010238 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010239 int i;
10240 u32 j;
10241
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010242 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010243 for (j = 0; j < len; j += 4) {
10244 u32 val;
10245
10246 tg3_write_mem(tp, offset + j, test_pattern[i]);
10247 tg3_read_mem(tp, offset + j, &val);
10248 if (val != test_pattern[i])
10249 return -EIO;
10250 }
10251 }
10252 return 0;
10253}
10254
10255static int tg3_test_memory(struct tg3 *tp)
10256{
10257 static struct mem_entry {
10258 u32 offset;
10259 u32 len;
10260 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010261 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010262 { 0x00002000, 0x1c000},
10263 { 0xffffffff, 0x00000}
10264 }, mem_tbl_5705[] = {
10265 { 0x00000100, 0x0000c},
10266 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010267 { 0x00004000, 0x00800},
10268 { 0x00006000, 0x01000},
10269 { 0x00008000, 0x02000},
10270 { 0x00010000, 0x0e000},
10271 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010272 }, mem_tbl_5755[] = {
10273 { 0x00000200, 0x00008},
10274 { 0x00004000, 0x00800},
10275 { 0x00006000, 0x00800},
10276 { 0x00008000, 0x02000},
10277 { 0x00010000, 0x0c000},
10278 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010279 }, mem_tbl_5906[] = {
10280 { 0x00000200, 0x00008},
10281 { 0x00004000, 0x00400},
10282 { 0x00006000, 0x00400},
10283 { 0x00008000, 0x01000},
10284 { 0x00010000, 0x01000},
10285 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010286 };
10287 struct mem_entry *mem_tbl;
10288 int err = 0;
10289 int i;
10290
Matt Carlson321d32a2008-11-21 17:22:19 -080010291 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
10292 mem_tbl = mem_tbl_5755;
10293 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10294 mem_tbl = mem_tbl_5906;
10295 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
10296 mem_tbl = mem_tbl_5705;
10297 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010298 mem_tbl = mem_tbl_570x;
10299
10300 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
10301 if ((err = tg3_do_mem_test(tp, mem_tbl[i].offset,
10302 mem_tbl[i].len)) != 0)
10303 break;
10304 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010305
Michael Chan7942e1d2005-05-29 14:58:36 -070010306 return err;
10307}
10308
Michael Chan9f40dea2005-09-05 17:53:06 -070010309#define TG3_MAC_LOOPBACK 0
10310#define TG3_PHY_LOOPBACK 1
10311
10312static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070010313{
Michael Chan9f40dea2005-09-05 17:53:06 -070010314 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010315 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070010316 struct sk_buff *skb, *rx_skb;
10317 u8 *tx_data;
10318 dma_addr_t map;
10319 int num_pkts, tx_len, rx_len, i, err;
10320 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000010321 struct tg3_napi *tnapi, *rnapi;
Matt Carlson21f581a2009-08-28 14:00:25 +000010322 struct tg3_rx_prodring_set *tpr = &tp->prodring[0];
Michael Chanc76949a2005-05-29 14:58:59 -070010323
Matt Carlson0c1d0e22009-09-01 13:16:33 +000010324 if (tp->irq_cnt > 1) {
10325 tnapi = &tp->napi[1];
10326 rnapi = &tp->napi[1];
10327 } else {
10328 tnapi = &tp->napi[0];
10329 rnapi = &tp->napi[0];
10330 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010331 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000010332
Michael Chan9f40dea2005-09-05 17:53:06 -070010333 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070010334 /* HW errata - mac loopback fails in some cases on 5780.
10335 * Normal traffic and PHY loopback are not affected by
10336 * errata.
10337 */
10338 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
10339 return 0;
10340
Michael Chan9f40dea2005-09-05 17:53:06 -070010341 mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010342 MAC_MODE_PORT_INT_LPBACK;
10343 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10344 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chan3f7045c2006-09-27 16:02:29 -070010345 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
10346 mac_mode |= MAC_MODE_PORT_MODE_MII;
10347 else
10348 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070010349 tw32(MAC_MODE, mac_mode);
10350 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070010351 u32 val;
10352
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010353 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10354 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080010355 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
10356 } else
10357 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070010358
Matt Carlson9ef8ca92007-07-11 19:48:29 -070010359 tg3_phy_toggle_automdix(tp, 0);
10360
Michael Chan3f7045c2006-09-27 16:02:29 -070010361 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070010362 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080010363
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010364 mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000010365 if (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) {
10366 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10367 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x1800);
Michael Chan5d64ad32006-12-07 00:19:40 -080010368 mac_mode |= MAC_MODE_PORT_MODE_MII;
10369 } else
10370 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070010371
Michael Chanc94e3942005-09-27 12:12:42 -070010372 /* reset to prevent losing 1st rx packet intermittently */
10373 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES) {
10374 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10375 udelay(10);
10376 tw32_f(MAC_RX_MODE, tp->rx_mode);
10377 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010378 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
10379 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)
10380 mac_mode &= ~MAC_MODE_LINK_POLARITY;
10381 else if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411)
10382 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080010383 tg3_writephy(tp, MII_TG3_EXT_CTRL,
10384 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
10385 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010386 tw32(MAC_MODE, mac_mode);
Michael Chan9f40dea2005-09-05 17:53:06 -070010387 }
10388 else
10389 return -EINVAL;
Michael Chanc76949a2005-05-29 14:58:59 -070010390
10391 err = -EIO;
10392
Michael Chanc76949a2005-05-29 14:58:59 -070010393 tx_len = 1514;
David S. Millera20e9c62006-07-31 22:38:16 -070010394 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070010395 if (!skb)
10396 return -ENOMEM;
10397
Michael Chanc76949a2005-05-29 14:58:59 -070010398 tx_data = skb_put(skb, tx_len);
10399 memcpy(tx_data, tp->dev->dev_addr, 6);
10400 memset(tx_data + 6, 0x0, 8);
10401
10402 tw32(MAC_RX_MTU_SIZE, tx_len + 4);
10403
10404 for (i = 14; i < tx_len; i++)
10405 tx_data[i] = (u8) (i & 0xff);
10406
Matt Carlsona21771d2009-11-02 14:25:31 +000010407 if (skb_dma_map(&tp->pdev->dev, skb, DMA_TO_DEVICE)) {
10408 dev_kfree_skb(skb);
10409 return -EIO;
10410 }
Michael Chanc76949a2005-05-29 14:58:59 -070010411
10412 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010413 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010414
10415 udelay(10);
10416
Matt Carlson898a56f2009-08-28 14:02:40 +000010417 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070010418
Michael Chanc76949a2005-05-29 14:58:59 -070010419 num_pkts = 0;
10420
Matt Carlsona21771d2009-11-02 14:25:31 +000010421 tg3_set_txd(tnapi, tnapi->tx_prod,
10422 skb_shinfo(skb)->dma_head, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070010423
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010424 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070010425 num_pkts++;
10426
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010427 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
10428 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070010429
10430 udelay(10);
10431
Matt Carlson303fc922009-11-02 14:27:34 +000010432 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
10433 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070010434 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010435 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070010436
10437 udelay(10);
10438
Matt Carlson898a56f2009-08-28 14:02:40 +000010439 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
10440 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010441 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070010442 (rx_idx == (rx_start_idx + num_pkts)))
10443 break;
10444 }
10445
Matt Carlsona21771d2009-11-02 14:25:31 +000010446 skb_dma_unmap(&tp->pdev->dev, skb, DMA_TO_DEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070010447 dev_kfree_skb(skb);
10448
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010449 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070010450 goto out;
10451
10452 if (rx_idx != rx_start_idx + num_pkts)
10453 goto out;
10454
Matt Carlson72334482009-08-28 14:03:01 +000010455 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070010456 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
10457 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
10458 if (opaque_key != RXD_OPAQUE_RING_STD)
10459 goto out;
10460
10461 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
10462 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
10463 goto out;
10464
10465 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
10466 if (rx_len != tx_len)
10467 goto out;
10468
Matt Carlson21f581a2009-08-28 14:00:25 +000010469 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
Michael Chanc76949a2005-05-29 14:58:59 -070010470
Matt Carlson21f581a2009-08-28 14:00:25 +000010471 map = pci_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
Michael Chanc76949a2005-05-29 14:58:59 -070010472 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
10473
10474 for (i = 14; i < tx_len; i++) {
10475 if (*(rx_skb->data + i) != (u8) (i & 0xff))
10476 goto out;
10477 }
10478 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010479
Michael Chanc76949a2005-05-29 14:58:59 -070010480 /* tg3_free_rings will unmap and free the rx_skb */
10481out:
10482 return err;
10483}
10484
Michael Chan9f40dea2005-09-05 17:53:06 -070010485#define TG3_MAC_LOOPBACK_FAILED 1
10486#define TG3_PHY_LOOPBACK_FAILED 2
10487#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
10488 TG3_PHY_LOOPBACK_FAILED)
10489
10490static int tg3_test_loopback(struct tg3 *tp)
10491{
10492 int err = 0;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010493 u32 cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070010494
10495 if (!netif_running(tp->dev))
10496 return TG3_LOOPBACK_FAILED;
10497
Michael Chanb9ec6c12006-07-25 16:37:27 -070010498 err = tg3_reset_hw(tp, 1);
10499 if (err)
10500 return TG3_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070010501
Matt Carlson6833c042008-11-21 17:18:59 -080010502 /* Turn off gphy autopowerdown. */
10503 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10504 tg3_phy_toggle_apd(tp, false);
10505
Matt Carlson321d32a2008-11-21 17:22:19 -080010506 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010507 int i;
10508 u32 status;
10509
10510 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
10511
10512 /* Wait for up to 40 microseconds to acquire lock. */
10513 for (i = 0; i < 4; i++) {
10514 status = tr32(TG3_CPMU_MUTEX_GNT);
10515 if (status == CPMU_MUTEX_GNT_DRIVER)
10516 break;
10517 udelay(10);
10518 }
10519
10520 if (status != CPMU_MUTEX_GNT_DRIVER)
10521 return TG3_LOOPBACK_FAILED;
10522
Matt Carlsonb2a5c192008-04-03 21:44:44 -070010523 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080010524 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070010525 tw32(TG3_CPMU_CTRL,
10526 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
10527 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070010528 }
10529
Michael Chan9f40dea2005-09-05 17:53:06 -070010530 if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
10531 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070010532
Matt Carlson321d32a2008-11-21 17:22:19 -080010533 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070010534 tw32(TG3_CPMU_CTRL, cpmuctrl);
10535
10536 /* Release the mutex */
10537 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
10538 }
10539
Matt Carlsondd477002008-05-25 23:45:58 -070010540 if (!(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) &&
10541 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Michael Chan9f40dea2005-09-05 17:53:06 -070010542 if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
10543 err |= TG3_PHY_LOOPBACK_FAILED;
10544 }
10545
Matt Carlson6833c042008-11-21 17:18:59 -080010546 /* Re-enable gphy autopowerdown. */
10547 if (tp->tg3_flags3 & TG3_FLG3_PHY_ENABLE_APD)
10548 tg3_phy_toggle_apd(tp, true);
10549
Michael Chan9f40dea2005-09-05 17:53:06 -070010550 return err;
10551}
10552
Michael Chan4cafd3f2005-05-29 14:56:34 -070010553static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
10554 u64 *data)
10555{
Michael Chan566f86a2005-05-29 14:56:58 -070010556 struct tg3 *tp = netdev_priv(dev);
10557
Michael Chanbc1c7562006-03-20 17:48:03 -080010558 if (tp->link_config.phy_is_low_power)
10559 tg3_set_power_state(tp, PCI_D0);
10560
Michael Chan566f86a2005-05-29 14:56:58 -070010561 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
10562
10563 if (tg3_test_nvram(tp) != 0) {
10564 etest->flags |= ETH_TEST_FL_FAILED;
10565 data[0] = 1;
10566 }
Michael Chanca430072005-05-29 14:57:23 -070010567 if (tg3_test_link(tp) != 0) {
10568 etest->flags |= ETH_TEST_FL_FAILED;
10569 data[1] = 1;
10570 }
Michael Chana71116d2005-05-29 14:58:11 -070010571 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010572 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070010573
Michael Chanbbe832c2005-06-24 20:20:04 -070010574 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010575 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010576 tg3_netif_stop(tp);
10577 irq_sync = 1;
10578 }
10579
10580 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070010581
10582 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080010583 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010584 tg3_halt_cpu(tp, RX_CPU_BASE);
10585 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
10586 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080010587 if (!err)
10588 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010589
Michael Chand9ab5ad12006-03-20 22:27:35 -080010590 if (tp->tg3_flags2 & TG3_FLG2_MII_SERDES)
10591 tg3_phy_reset(tp);
10592
Michael Chana71116d2005-05-29 14:58:11 -070010593 if (tg3_test_registers(tp) != 0) {
10594 etest->flags |= ETH_TEST_FL_FAILED;
10595 data[2] = 1;
10596 }
Michael Chan7942e1d2005-05-29 14:58:36 -070010597 if (tg3_test_memory(tp) != 0) {
10598 etest->flags |= ETH_TEST_FL_FAILED;
10599 data[3] = 1;
10600 }
Michael Chan9f40dea2005-09-05 17:53:06 -070010601 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070010602 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070010603
David S. Millerf47c11e2005-06-24 20:18:35 -070010604 tg3_full_unlock(tp);
10605
Michael Chand4bc3922005-05-29 14:59:20 -070010606 if (tg3_test_interrupt(tp) != 0) {
10607 etest->flags |= ETH_TEST_FL_FAILED;
10608 data[5] = 1;
10609 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010610
10611 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070010612
Michael Chana71116d2005-05-29 14:58:11 -070010613 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10614 if (netif_running(dev)) {
10615 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010616 err2 = tg3_restart_hw(tp, 1);
10617 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070010618 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010619 }
David S. Millerf47c11e2005-06-24 20:18:35 -070010620
10621 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010622
10623 if (irq_sync && !err2)
10624 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070010625 }
Michael Chanbc1c7562006-03-20 17:48:03 -080010626 if (tp->link_config.phy_is_low_power)
10627 tg3_set_power_state(tp, PCI_D3hot);
10628
Michael Chan4cafd3f2005-05-29 14:56:34 -070010629}
10630
Linus Torvalds1da177e2005-04-16 15:20:36 -070010631static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10632{
10633 struct mii_ioctl_data *data = if_mii(ifr);
10634 struct tg3 *tp = netdev_priv(dev);
10635 int err;
10636
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010637 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010638 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010639 if (!(tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED))
10640 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010641 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10642 return phy_mii_ioctl(phydev, data, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010643 }
10644
Linus Torvalds1da177e2005-04-16 15:20:36 -070010645 switch(cmd) {
10646 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000010647 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010648
10649 /* fallthru */
10650 case SIOCGMIIREG: {
10651 u32 mii_regval;
10652
10653 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10654 break; /* We have no PHY */
10655
Michael Chanbc1c7562006-03-20 17:48:03 -080010656 if (tp->link_config.phy_is_low_power)
10657 return -EAGAIN;
10658
David S. Millerf47c11e2005-06-24 20:18:35 -070010659 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010660 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070010661 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010662
10663 data->val_out = mii_regval;
10664
10665 return err;
10666 }
10667
10668 case SIOCSMIIREG:
10669 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
10670 break; /* We have no PHY */
10671
Michael Chanbc1c7562006-03-20 17:48:03 -080010672 if (tp->link_config.phy_is_low_power)
10673 return -EAGAIN;
10674
David S. Millerf47c11e2005-06-24 20:18:35 -070010675 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010676 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070010677 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010678
10679 return err;
10680
10681 default:
10682 /* do nothing */
10683 break;
10684 }
10685 return -EOPNOTSUPP;
10686}
10687
10688#if TG3_VLAN_TAG_USED
10689static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
10690{
10691 struct tg3 *tp = netdev_priv(dev);
10692
Matt Carlson844b3ee2009-02-25 14:23:56 +000010693 if (!netif_running(dev)) {
10694 tp->vlgrp = grp;
10695 return;
10696 }
10697
10698 tg3_netif_stop(tp);
Michael Chan29315e82006-06-29 20:12:30 -070010699
David S. Millerf47c11e2005-06-24 20:18:35 -070010700 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010701
10702 tp->vlgrp = grp;
10703
10704 /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
10705 __tg3_set_rx_mode(dev);
10706
Matt Carlson844b3ee2009-02-25 14:23:56 +000010707 tg3_netif_start(tp);
Michael Chan46966542007-07-11 19:47:19 -070010708
10709 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010710}
Linus Torvalds1da177e2005-04-16 15:20:36 -070010711#endif
10712
David S. Miller15f98502005-05-18 22:49:26 -070010713static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
10714{
10715 struct tg3 *tp = netdev_priv(dev);
10716
10717 memcpy(ec, &tp->coal, sizeof(*ec));
10718 return 0;
10719}
10720
Michael Chand244c892005-07-05 14:42:33 -070010721static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
10722{
10723 struct tg3 *tp = netdev_priv(dev);
10724 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
10725 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
10726
10727 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
10728 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
10729 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
10730 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
10731 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
10732 }
10733
10734 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
10735 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
10736 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
10737 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
10738 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
10739 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
10740 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
10741 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
10742 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
10743 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
10744 return -EINVAL;
10745
10746 /* No rx interrupts will be generated if both are zero */
10747 if ((ec->rx_coalesce_usecs == 0) &&
10748 (ec->rx_max_coalesced_frames == 0))
10749 return -EINVAL;
10750
10751 /* No tx interrupts will be generated if both are zero */
10752 if ((ec->tx_coalesce_usecs == 0) &&
10753 (ec->tx_max_coalesced_frames == 0))
10754 return -EINVAL;
10755
10756 /* Only copy relevant parameters, ignore all others. */
10757 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
10758 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
10759 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
10760 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
10761 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
10762 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
10763 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
10764 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
10765 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
10766
10767 if (netif_running(dev)) {
10768 tg3_full_lock(tp, 0);
10769 __tg3_set_coalesce(tp, &tp->coal);
10770 tg3_full_unlock(tp);
10771 }
10772 return 0;
10773}
10774
Jeff Garzik7282d492006-09-13 14:30:00 -040010775static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010776 .get_settings = tg3_get_settings,
10777 .set_settings = tg3_set_settings,
10778 .get_drvinfo = tg3_get_drvinfo,
10779 .get_regs_len = tg3_get_regs_len,
10780 .get_regs = tg3_get_regs,
10781 .get_wol = tg3_get_wol,
10782 .set_wol = tg3_set_wol,
10783 .get_msglevel = tg3_get_msglevel,
10784 .set_msglevel = tg3_set_msglevel,
10785 .nway_reset = tg3_nway_reset,
10786 .get_link = ethtool_op_get_link,
10787 .get_eeprom_len = tg3_get_eeprom_len,
10788 .get_eeprom = tg3_get_eeprom,
10789 .set_eeprom = tg3_set_eeprom,
10790 .get_ringparam = tg3_get_ringparam,
10791 .set_ringparam = tg3_set_ringparam,
10792 .get_pauseparam = tg3_get_pauseparam,
10793 .set_pauseparam = tg3_set_pauseparam,
10794 .get_rx_csum = tg3_get_rx_csum,
10795 .set_rx_csum = tg3_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010796 .set_tx_csum = tg3_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010797 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010798 .set_tso = tg3_set_tso,
Michael Chan4cafd3f2005-05-29 14:56:34 -070010799 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010800 .get_strings = tg3_get_strings,
Michael Chan4009a932005-09-05 17:52:54 -070010801 .phys_id = tg3_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010802 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070010803 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070010804 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010805 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010806};
10807
10808static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
10809{
Michael Chan1b277772006-03-20 22:27:48 -080010810 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010811
10812 tp->nvram_size = EEPROM_CHIP_SIZE;
10813
Matt Carlsone4f34112009-02-25 14:25:00 +000010814 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010815 return;
10816
Michael Chanb16250e2006-09-27 16:10:14 -070010817 if ((magic != TG3_EEPROM_MAGIC) &&
10818 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
10819 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010820 return;
10821
10822 /*
10823 * Size the chip by reading offsets at increasing powers of two.
10824 * When we encounter our validation signature, we know the addressing
10825 * has wrapped around, and thus have our chip size.
10826 */
Michael Chan1b277772006-03-20 22:27:48 -080010827 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010828
10829 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000010830 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010831 return;
10832
Michael Chan18201802006-03-20 22:29:15 -080010833 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010834 break;
10835
10836 cursize <<= 1;
10837 }
10838
10839 tp->nvram_size = cursize;
10840}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010841
Linus Torvalds1da177e2005-04-16 15:20:36 -070010842static void __devinit tg3_get_nvram_size(struct tg3 *tp)
10843{
10844 u32 val;
10845
Matt Carlsondf259d82009-04-20 06:57:14 +000010846 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
10847 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010848 return;
10849
10850 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080010851 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080010852 tg3_get_eeprom_size(tp);
10853 return;
10854 }
10855
Matt Carlson6d348f22009-02-25 14:25:52 +000010856 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010857 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000010858 /* This is confusing. We want to operate on the
10859 * 16-bit value at offset 0xf2. The tg3_nvram_read()
10860 * call will read from NVRAM and byteswap the data
10861 * according to the byteswapping settings for all
10862 * other register accesses. This ensures the data we
10863 * want will always reside in the lower 16-bits.
10864 * However, the data in NVRAM is in LE format, which
10865 * means the data from the NVRAM read will always be
10866 * opposite the endianness of the CPU. The 16-bit
10867 * byteswap then brings the data to CPU endianness.
10868 */
10869 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010870 return;
10871 }
10872 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070010873 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010874}
10875
10876static void __devinit tg3_get_nvram_info(struct tg3 *tp)
10877{
10878 u32 nvcfg1;
10879
10880 nvcfg1 = tr32(NVRAM_CFG1);
10881 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
10882 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000010883 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010884 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
10885 tw32(NVRAM_CFG1, nvcfg1);
10886 }
10887
Michael Chan4c987482005-09-05 17:52:38 -070010888 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070010889 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010890 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000010891 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
10892 tp->nvram_jedecnum = JEDEC_ATMEL;
10893 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
10894 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10895 break;
10896 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
10897 tp->nvram_jedecnum = JEDEC_ATMEL;
10898 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
10899 break;
10900 case FLASH_VENDOR_ATMEL_EEPROM:
10901 tp->nvram_jedecnum = JEDEC_ATMEL;
10902 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
10903 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10904 break;
10905 case FLASH_VENDOR_ST:
10906 tp->nvram_jedecnum = JEDEC_ST;
10907 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
10908 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10909 break;
10910 case FLASH_VENDOR_SAIFUN:
10911 tp->nvram_jedecnum = JEDEC_SAIFUN;
10912 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
10913 break;
10914 case FLASH_VENDOR_SST_SMALL:
10915 case FLASH_VENDOR_SST_LARGE:
10916 tp->nvram_jedecnum = JEDEC_SST;
10917 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
10918 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010919 }
Matt Carlson8590a602009-08-28 12:29:16 +000010920 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010921 tp->nvram_jedecnum = JEDEC_ATMEL;
10922 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
10923 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10924 }
10925}
10926
Matt Carlsona1b950d2009-09-01 13:20:17 +000010927static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
10928{
10929 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
10930 case FLASH_5752PAGE_SIZE_256:
10931 tp->nvram_pagesize = 256;
10932 break;
10933 case FLASH_5752PAGE_SIZE_512:
10934 tp->nvram_pagesize = 512;
10935 break;
10936 case FLASH_5752PAGE_SIZE_1K:
10937 tp->nvram_pagesize = 1024;
10938 break;
10939 case FLASH_5752PAGE_SIZE_2K:
10940 tp->nvram_pagesize = 2048;
10941 break;
10942 case FLASH_5752PAGE_SIZE_4K:
10943 tp->nvram_pagesize = 4096;
10944 break;
10945 case FLASH_5752PAGE_SIZE_264:
10946 tp->nvram_pagesize = 264;
10947 break;
10948 case FLASH_5752PAGE_SIZE_528:
10949 tp->nvram_pagesize = 528;
10950 break;
10951 }
10952}
10953
Michael Chan361b4ac2005-04-21 17:11:21 -070010954static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
10955{
10956 u32 nvcfg1;
10957
10958 nvcfg1 = tr32(NVRAM_CFG1);
10959
Michael Chane6af3012005-04-21 17:12:05 -070010960 /* NVRAM protection for TPM */
10961 if (nvcfg1 & (1 << 27))
10962 tp->tg3_flags2 |= TG3_FLG2_PROTECTED_NVRAM;
10963
Michael Chan361b4ac2005-04-21 17:11:21 -070010964 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000010965 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
10966 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
10967 tp->nvram_jedecnum = JEDEC_ATMEL;
10968 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10969 break;
10970 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
10971 tp->nvram_jedecnum = JEDEC_ATMEL;
10972 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10973 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10974 break;
10975 case FLASH_5752VENDOR_ST_M45PE10:
10976 case FLASH_5752VENDOR_ST_M45PE20:
10977 case FLASH_5752VENDOR_ST_M45PE40:
10978 tp->nvram_jedecnum = JEDEC_ST;
10979 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
10980 tp->tg3_flags2 |= TG3_FLG2_FLASH;
10981 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070010982 }
10983
10984 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000010985 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000010986 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070010987 /* For eeprom, set pagesize to maximum eeprom size */
10988 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
10989
10990 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
10991 tw32(NVRAM_CFG1, nvcfg1);
10992 }
10993}
10994
Michael Chand3c7b882006-03-23 01:28:25 -080010995static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
10996{
Matt Carlson989a9d22007-05-05 11:51:05 -070010997 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080010998
10999 nvcfg1 = tr32(NVRAM_CFG1);
11000
11001 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011002 if (nvcfg1 & (1 << 27)) {
Michael Chand3c7b882006-03-23 01:28:25 -080011003 tp->tg3_flags2 |= TG3_FLG2_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011004 protect = 1;
11005 }
Michael Chand3c7b882006-03-23 01:28:25 -080011006
Matt Carlson989a9d22007-05-05 11:51:05 -070011007 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11008 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011009 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11010 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11011 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11012 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11013 tp->nvram_jedecnum = JEDEC_ATMEL;
11014 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11015 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11016 tp->nvram_pagesize = 264;
11017 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11018 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11019 tp->nvram_size = (protect ? 0x3e200 :
11020 TG3_NVRAM_SIZE_512KB);
11021 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11022 tp->nvram_size = (protect ? 0x1f200 :
11023 TG3_NVRAM_SIZE_256KB);
11024 else
11025 tp->nvram_size = (protect ? 0x1f200 :
11026 TG3_NVRAM_SIZE_128KB);
11027 break;
11028 case FLASH_5752VENDOR_ST_M45PE10:
11029 case FLASH_5752VENDOR_ST_M45PE20:
11030 case FLASH_5752VENDOR_ST_M45PE40:
11031 tp->nvram_jedecnum = JEDEC_ST;
11032 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11033 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11034 tp->nvram_pagesize = 256;
11035 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11036 tp->nvram_size = (protect ?
11037 TG3_NVRAM_SIZE_64KB :
11038 TG3_NVRAM_SIZE_128KB);
11039 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11040 tp->nvram_size = (protect ?
11041 TG3_NVRAM_SIZE_64KB :
11042 TG3_NVRAM_SIZE_256KB);
11043 else
11044 tp->nvram_size = (protect ?
11045 TG3_NVRAM_SIZE_128KB :
11046 TG3_NVRAM_SIZE_512KB);
11047 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011048 }
11049}
11050
Michael Chan1b277772006-03-20 22:27:48 -080011051static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11052{
11053 u32 nvcfg1;
11054
11055 nvcfg1 = tr32(NVRAM_CFG1);
11056
11057 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011058 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11059 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11060 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11061 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11062 tp->nvram_jedecnum = JEDEC_ATMEL;
11063 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11064 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011065
Matt Carlson8590a602009-08-28 12:29:16 +000011066 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11067 tw32(NVRAM_CFG1, nvcfg1);
11068 break;
11069 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11070 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11071 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11072 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11073 tp->nvram_jedecnum = JEDEC_ATMEL;
11074 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11075 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11076 tp->nvram_pagesize = 264;
11077 break;
11078 case FLASH_5752VENDOR_ST_M45PE10:
11079 case FLASH_5752VENDOR_ST_M45PE20:
11080 case FLASH_5752VENDOR_ST_M45PE40:
11081 tp->nvram_jedecnum = JEDEC_ST;
11082 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11083 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11084 tp->nvram_pagesize = 256;
11085 break;
Michael Chan1b277772006-03-20 22:27:48 -080011086 }
11087}
11088
Matt Carlson6b91fa02007-10-10 18:01:09 -070011089static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11090{
11091 u32 nvcfg1, protect = 0;
11092
11093 nvcfg1 = tr32(NVRAM_CFG1);
11094
11095 /* NVRAM protection for TPM */
11096 if (nvcfg1 & (1 << 27)) {
11097 tp->tg3_flags2 |= TG3_FLG2_PROTECTED_NVRAM;
11098 protect = 1;
11099 }
11100
11101 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11102 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011103 case FLASH_5761VENDOR_ATMEL_ADB021D:
11104 case FLASH_5761VENDOR_ATMEL_ADB041D:
11105 case FLASH_5761VENDOR_ATMEL_ADB081D:
11106 case FLASH_5761VENDOR_ATMEL_ADB161D:
11107 case FLASH_5761VENDOR_ATMEL_MDB021D:
11108 case FLASH_5761VENDOR_ATMEL_MDB041D:
11109 case FLASH_5761VENDOR_ATMEL_MDB081D:
11110 case FLASH_5761VENDOR_ATMEL_MDB161D:
11111 tp->nvram_jedecnum = JEDEC_ATMEL;
11112 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11113 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11114 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11115 tp->nvram_pagesize = 256;
11116 break;
11117 case FLASH_5761VENDOR_ST_A_M45PE20:
11118 case FLASH_5761VENDOR_ST_A_M45PE40:
11119 case FLASH_5761VENDOR_ST_A_M45PE80:
11120 case FLASH_5761VENDOR_ST_A_M45PE16:
11121 case FLASH_5761VENDOR_ST_M_M45PE20:
11122 case FLASH_5761VENDOR_ST_M_M45PE40:
11123 case FLASH_5761VENDOR_ST_M_M45PE80:
11124 case FLASH_5761VENDOR_ST_M_M45PE16:
11125 tp->nvram_jedecnum = JEDEC_ST;
11126 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11127 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11128 tp->nvram_pagesize = 256;
11129 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011130 }
11131
11132 if (protect) {
11133 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11134 } else {
11135 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011136 case FLASH_5761VENDOR_ATMEL_ADB161D:
11137 case FLASH_5761VENDOR_ATMEL_MDB161D:
11138 case FLASH_5761VENDOR_ST_A_M45PE16:
11139 case FLASH_5761VENDOR_ST_M_M45PE16:
11140 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11141 break;
11142 case FLASH_5761VENDOR_ATMEL_ADB081D:
11143 case FLASH_5761VENDOR_ATMEL_MDB081D:
11144 case FLASH_5761VENDOR_ST_A_M45PE80:
11145 case FLASH_5761VENDOR_ST_M_M45PE80:
11146 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11147 break;
11148 case FLASH_5761VENDOR_ATMEL_ADB041D:
11149 case FLASH_5761VENDOR_ATMEL_MDB041D:
11150 case FLASH_5761VENDOR_ST_A_M45PE40:
11151 case FLASH_5761VENDOR_ST_M_M45PE40:
11152 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11153 break;
11154 case FLASH_5761VENDOR_ATMEL_ADB021D:
11155 case FLASH_5761VENDOR_ATMEL_MDB021D:
11156 case FLASH_5761VENDOR_ST_A_M45PE20:
11157 case FLASH_5761VENDOR_ST_M_M45PE20:
11158 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11159 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011160 }
11161 }
11162}
11163
Michael Chanb5d37722006-09-27 16:06:21 -070011164static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11165{
11166 tp->nvram_jedecnum = JEDEC_ATMEL;
11167 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11168 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11169}
11170
Matt Carlson321d32a2008-11-21 17:22:19 -080011171static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11172{
11173 u32 nvcfg1;
11174
11175 nvcfg1 = tr32(NVRAM_CFG1);
11176
11177 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11178 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11179 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11180 tp->nvram_jedecnum = JEDEC_ATMEL;
11181 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11182 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11183
11184 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11185 tw32(NVRAM_CFG1, nvcfg1);
11186 return;
11187 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11188 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11189 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11190 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11191 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11192 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11193 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11194 tp->nvram_jedecnum = JEDEC_ATMEL;
11195 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11196 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11197
11198 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11199 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11200 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11201 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11202 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11203 break;
11204 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11205 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11206 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11207 break;
11208 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11209 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11210 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11211 break;
11212 }
11213 break;
11214 case FLASH_5752VENDOR_ST_M45PE10:
11215 case FLASH_5752VENDOR_ST_M45PE20:
11216 case FLASH_5752VENDOR_ST_M45PE40:
11217 tp->nvram_jedecnum = JEDEC_ST;
11218 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11219 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11220
11221 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11222 case FLASH_5752VENDOR_ST_M45PE10:
11223 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11224 break;
11225 case FLASH_5752VENDOR_ST_M45PE20:
11226 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11227 break;
11228 case FLASH_5752VENDOR_ST_M45PE40:
11229 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11230 break;
11231 }
11232 break;
11233 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011234 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011235 return;
11236 }
11237
Matt Carlsona1b950d2009-09-01 13:20:17 +000011238 tg3_nvram_get_pagesize(tp, nvcfg1);
11239 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011240 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011241}
11242
11243
11244static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11245{
11246 u32 nvcfg1;
11247
11248 nvcfg1 = tr32(NVRAM_CFG1);
11249
11250 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11251 case FLASH_5717VENDOR_ATMEL_EEPROM:
11252 case FLASH_5717VENDOR_MICRO_EEPROM:
11253 tp->nvram_jedecnum = JEDEC_ATMEL;
11254 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11255 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11256
11257 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11258 tw32(NVRAM_CFG1, nvcfg1);
11259 return;
11260 case FLASH_5717VENDOR_ATMEL_MDB011D:
11261 case FLASH_5717VENDOR_ATMEL_ADB011B:
11262 case FLASH_5717VENDOR_ATMEL_ADB011D:
11263 case FLASH_5717VENDOR_ATMEL_MDB021D:
11264 case FLASH_5717VENDOR_ATMEL_ADB021B:
11265 case FLASH_5717VENDOR_ATMEL_ADB021D:
11266 case FLASH_5717VENDOR_ATMEL_45USPT:
11267 tp->nvram_jedecnum = JEDEC_ATMEL;
11268 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11269 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11270
11271 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11272 case FLASH_5717VENDOR_ATMEL_MDB021D:
11273 case FLASH_5717VENDOR_ATMEL_ADB021B:
11274 case FLASH_5717VENDOR_ATMEL_ADB021D:
11275 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11276 break;
11277 default:
11278 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11279 break;
11280 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011281 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011282 case FLASH_5717VENDOR_ST_M_M25PE10:
11283 case FLASH_5717VENDOR_ST_A_M25PE10:
11284 case FLASH_5717VENDOR_ST_M_M45PE10:
11285 case FLASH_5717VENDOR_ST_A_M45PE10:
11286 case FLASH_5717VENDOR_ST_M_M25PE20:
11287 case FLASH_5717VENDOR_ST_A_M25PE20:
11288 case FLASH_5717VENDOR_ST_M_M45PE20:
11289 case FLASH_5717VENDOR_ST_A_M45PE20:
11290 case FLASH_5717VENDOR_ST_25USPT:
11291 case FLASH_5717VENDOR_ST_45USPT:
11292 tp->nvram_jedecnum = JEDEC_ST;
11293 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11294 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11295
11296 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11297 case FLASH_5717VENDOR_ST_M_M25PE20:
11298 case FLASH_5717VENDOR_ST_A_M25PE20:
11299 case FLASH_5717VENDOR_ST_M_M45PE20:
11300 case FLASH_5717VENDOR_ST_A_M45PE20:
11301 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11302 break;
11303 default:
11304 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11305 break;
11306 }
Matt Carlson321d32a2008-11-21 17:22:19 -080011307 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011308 default:
11309 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
11310 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080011311 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000011312
11313 tg3_nvram_get_pagesize(tp, nvcfg1);
11314 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
11315 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080011316}
11317
Linus Torvalds1da177e2005-04-16 15:20:36 -070011318/* Chips other than 5700/5701 use the NVRAM for fetching info. */
11319static void __devinit tg3_nvram_init(struct tg3 *tp)
11320{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011321 tw32_f(GRC_EEPROM_ADDR,
11322 (EEPROM_ADDR_FSM_RESET |
11323 (EEPROM_DEFAULT_CLOCK_PERIOD <<
11324 EEPROM_ADDR_CLKPERD_SHIFT)));
11325
Michael Chan9d57f012006-12-07 00:23:25 -080011326 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011327
11328 /* Enable seeprom accesses. */
11329 tw32_f(GRC_LOCAL_CTRL,
11330 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
11331 udelay(100);
11332
11333 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
11334 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
11335 tp->tg3_flags |= TG3_FLAG_NVRAM;
11336
Michael Chanec41c7d2006-01-17 02:40:55 -080011337 if (tg3_nvram_lock(tp)) {
11338 printk(KERN_WARNING PFX "%s: Cannot get nvarm lock, "
11339 "tg3_nvram_init failed.\n", tp->dev->name);
11340 return;
11341 }
Michael Chane6af3012005-04-21 17:12:05 -070011342 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011343
Matt Carlson989a9d22007-05-05 11:51:05 -070011344 tp->nvram_size = 0;
11345
Michael Chan361b4ac2005-04-21 17:11:21 -070011346 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
11347 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080011348 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
11349 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070011350 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070011351 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
11352 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080011353 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011354 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
11355 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070011356 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11357 tg3_get_5906_nvram_info(tp);
Matt Carlson321d32a2008-11-21 17:22:19 -080011358 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
11359 tg3_get_57780_nvram_info(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000011360 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
11361 tg3_get_5717_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070011362 else
11363 tg3_get_nvram_info(tp);
11364
Matt Carlson989a9d22007-05-05 11:51:05 -070011365 if (tp->nvram_size == 0)
11366 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011367
Michael Chane6af3012005-04-21 17:12:05 -070011368 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080011369 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011370
11371 } else {
11372 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
11373
11374 tg3_get_eeprom_size(tp);
11375 }
11376}
11377
Linus Torvalds1da177e2005-04-16 15:20:36 -070011378static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
11379 u32 offset, u32 len, u8 *buf)
11380{
11381 int i, j, rc = 0;
11382 u32 val;
11383
11384 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011385 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011386 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011387
11388 addr = offset + i;
11389
11390 memcpy(&data, buf + i, 4);
11391
Matt Carlson62cedd12009-04-20 14:52:29 -070011392 /*
11393 * The SEEPROM interface expects the data to always be opposite
11394 * the native endian format. We accomplish this by reversing
11395 * all the operations that would have been performed on the
11396 * data from a call to tg3_nvram_read_be32().
11397 */
11398 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011399
11400 val = tr32(GRC_EEPROM_ADDR);
11401 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
11402
11403 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
11404 EEPROM_ADDR_READ);
11405 tw32(GRC_EEPROM_ADDR, val |
11406 (0 << EEPROM_ADDR_DEVID_SHIFT) |
11407 (addr & EEPROM_ADDR_ADDR_MASK) |
11408 EEPROM_ADDR_START |
11409 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011410
Michael Chan9d57f012006-12-07 00:23:25 -080011411 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011412 val = tr32(GRC_EEPROM_ADDR);
11413
11414 if (val & EEPROM_ADDR_COMPLETE)
11415 break;
Michael Chan9d57f012006-12-07 00:23:25 -080011416 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011417 }
11418 if (!(val & EEPROM_ADDR_COMPLETE)) {
11419 rc = -EBUSY;
11420 break;
11421 }
11422 }
11423
11424 return rc;
11425}
11426
11427/* offset and length are dword aligned */
11428static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
11429 u8 *buf)
11430{
11431 int ret = 0;
11432 u32 pagesize = tp->nvram_pagesize;
11433 u32 pagemask = pagesize - 1;
11434 u32 nvram_cmd;
11435 u8 *tmp;
11436
11437 tmp = kmalloc(pagesize, GFP_KERNEL);
11438 if (tmp == NULL)
11439 return -ENOMEM;
11440
11441 while (len) {
11442 int j;
Michael Chane6af3012005-04-21 17:12:05 -070011443 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011444
11445 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011446
Linus Torvalds1da177e2005-04-16 15:20:36 -070011447 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011448 ret = tg3_nvram_read_be32(tp, phy_addr + j,
11449 (__be32 *) (tmp + j));
11450 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011451 break;
11452 }
11453 if (ret)
11454 break;
11455
11456 page_off = offset & pagemask;
11457 size = pagesize;
11458 if (len < size)
11459 size = len;
11460
11461 len -= size;
11462
11463 memcpy(tmp + page_off, buf, size);
11464
11465 offset = offset + (pagesize - page_off);
11466
Michael Chane6af3012005-04-21 17:12:05 -070011467 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011468
11469 /*
11470 * Before we can erase the flash page, we need
11471 * to issue a special "write enable" command.
11472 */
11473 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11474
11475 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11476 break;
11477
11478 /* Erase the target page */
11479 tw32(NVRAM_ADDR, phy_addr);
11480
11481 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
11482 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
11483
11484 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11485 break;
11486
11487 /* Issue another write enable to start the write. */
11488 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11489
11490 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
11491 break;
11492
11493 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011494 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011495
Al Virob9fc7dc2007-12-17 22:59:57 -080011496 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000011497
Al Virob9fc7dc2007-12-17 22:59:57 -080011498 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011499
11500 tw32(NVRAM_ADDR, phy_addr + j);
11501
11502 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
11503 NVRAM_CMD_WR;
11504
11505 if (j == 0)
11506 nvram_cmd |= NVRAM_CMD_FIRST;
11507 else if (j == (pagesize - 4))
11508 nvram_cmd |= NVRAM_CMD_LAST;
11509
11510 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11511 break;
11512 }
11513 if (ret)
11514 break;
11515 }
11516
11517 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
11518 tg3_nvram_exec_cmd(tp, nvram_cmd);
11519
11520 kfree(tmp);
11521
11522 return ret;
11523}
11524
11525/* offset and length are dword aligned */
11526static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
11527 u8 *buf)
11528{
11529 int i, ret = 0;
11530
11531 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080011532 u32 page_off, phy_addr, nvram_cmd;
11533 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011534
11535 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080011536 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011537
11538 page_off = offset % tp->nvram_pagesize;
11539
Michael Chan18201802006-03-20 22:29:15 -080011540 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011541
11542 tw32(NVRAM_ADDR, phy_addr);
11543
11544 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
11545
11546 if ((page_off == 0) || (i == 0))
11547 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070011548 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011549 nvram_cmd |= NVRAM_CMD_LAST;
11550
11551 if (i == (len - 4))
11552 nvram_cmd |= NVRAM_CMD_LAST;
11553
Matt Carlson321d32a2008-11-21 17:22:19 -080011554 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
11555 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070011556 (tp->nvram_jedecnum == JEDEC_ST) &&
11557 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011558
11559 if ((ret = tg3_nvram_exec_cmd(tp,
11560 NVRAM_CMD_WREN | NVRAM_CMD_GO |
11561 NVRAM_CMD_DONE)))
11562
11563 break;
11564 }
11565 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11566 /* We always do complete word writes to eeprom. */
11567 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
11568 }
11569
11570 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
11571 break;
11572 }
11573 return ret;
11574}
11575
11576/* offset and length are dword aligned */
11577static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
11578{
11579 int ret;
11580
Linus Torvalds1da177e2005-04-16 15:20:36 -070011581 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011582 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
11583 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011584 udelay(40);
11585 }
11586
11587 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
11588 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
11589 }
11590 else {
11591 u32 grc_mode;
11592
Michael Chanec41c7d2006-01-17 02:40:55 -080011593 ret = tg3_nvram_lock(tp);
11594 if (ret)
11595 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011596
Michael Chane6af3012005-04-21 17:12:05 -070011597 tg3_enable_nvram_access(tp);
11598 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
11599 !(tp->tg3_flags2 & TG3_FLG2_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011600 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011601
11602 grc_mode = tr32(GRC_MODE);
11603 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
11604
11605 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
11606 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
11607
11608 ret = tg3_nvram_write_block_buffered(tp, offset, len,
11609 buf);
11610 }
11611 else {
11612 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
11613 buf);
11614 }
11615
11616 grc_mode = tr32(GRC_MODE);
11617 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
11618
Michael Chane6af3012005-04-21 17:12:05 -070011619 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011620 tg3_nvram_unlock(tp);
11621 }
11622
11623 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070011624 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011625 udelay(40);
11626 }
11627
11628 return ret;
11629}
11630
11631struct subsys_tbl_ent {
11632 u16 subsys_vendor, subsys_devid;
11633 u32 phy_id;
11634};
11635
11636static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
11637 /* Broadcom boards. */
11638 { PCI_VENDOR_ID_BROADCOM, 0x1644, PHY_ID_BCM5401 }, /* BCM95700A6 */
11639 { PCI_VENDOR_ID_BROADCOM, 0x0001, PHY_ID_BCM5701 }, /* BCM95701A5 */
11640 { PCI_VENDOR_ID_BROADCOM, 0x0002, PHY_ID_BCM8002 }, /* BCM95700T6 */
11641 { PCI_VENDOR_ID_BROADCOM, 0x0003, 0 }, /* BCM95700A9 */
11642 { PCI_VENDOR_ID_BROADCOM, 0x0005, PHY_ID_BCM5701 }, /* BCM95701T1 */
11643 { PCI_VENDOR_ID_BROADCOM, 0x0006, PHY_ID_BCM5701 }, /* BCM95701T8 */
11644 { PCI_VENDOR_ID_BROADCOM, 0x0007, 0 }, /* BCM95701A7 */
11645 { PCI_VENDOR_ID_BROADCOM, 0x0008, PHY_ID_BCM5701 }, /* BCM95701A10 */
11646 { PCI_VENDOR_ID_BROADCOM, 0x8008, PHY_ID_BCM5701 }, /* BCM95701A12 */
11647 { PCI_VENDOR_ID_BROADCOM, 0x0009, PHY_ID_BCM5703 }, /* BCM95703Ax1 */
11648 { PCI_VENDOR_ID_BROADCOM, 0x8009, PHY_ID_BCM5703 }, /* BCM95703Ax2 */
11649
11650 /* 3com boards. */
11651 { PCI_VENDOR_ID_3COM, 0x1000, PHY_ID_BCM5401 }, /* 3C996T */
11652 { PCI_VENDOR_ID_3COM, 0x1006, PHY_ID_BCM5701 }, /* 3C996BT */
11653 { PCI_VENDOR_ID_3COM, 0x1004, 0 }, /* 3C996SX */
11654 { PCI_VENDOR_ID_3COM, 0x1007, PHY_ID_BCM5701 }, /* 3C1000T */
11655 { PCI_VENDOR_ID_3COM, 0x1008, PHY_ID_BCM5701 }, /* 3C940BR01 */
11656
11657 /* DELL boards. */
11658 { PCI_VENDOR_ID_DELL, 0x00d1, PHY_ID_BCM5401 }, /* VIPER */
11659 { PCI_VENDOR_ID_DELL, 0x0106, PHY_ID_BCM5401 }, /* JAGUAR */
11660 { PCI_VENDOR_ID_DELL, 0x0109, PHY_ID_BCM5411 }, /* MERLOT */
11661 { PCI_VENDOR_ID_DELL, 0x010a, PHY_ID_BCM5411 }, /* SLIM_MERLOT */
11662
11663 /* Compaq boards. */
11664 { PCI_VENDOR_ID_COMPAQ, 0x007c, PHY_ID_BCM5701 }, /* BANSHEE */
11665 { PCI_VENDOR_ID_COMPAQ, 0x009a, PHY_ID_BCM5701 }, /* BANSHEE_2 */
11666 { PCI_VENDOR_ID_COMPAQ, 0x007d, 0 }, /* CHANGELING */
11667 { PCI_VENDOR_ID_COMPAQ, 0x0085, PHY_ID_BCM5701 }, /* NC7780 */
11668 { PCI_VENDOR_ID_COMPAQ, 0x0099, PHY_ID_BCM5701 }, /* NC7780_2 */
11669
11670 /* IBM boards. */
11671 { PCI_VENDOR_ID_IBM, 0x0281, 0 } /* IBM??? */
11672};
11673
11674static inline struct subsys_tbl_ent *lookup_by_subsys(struct tg3 *tp)
11675{
11676 int i;
11677
11678 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
11679 if ((subsys_id_to_phy_id[i].subsys_vendor ==
11680 tp->pdev->subsystem_vendor) &&
11681 (subsys_id_to_phy_id[i].subsys_devid ==
11682 tp->pdev->subsystem_device))
11683 return &subsys_id_to_phy_id[i];
11684 }
11685 return NULL;
11686}
11687
Michael Chan7d0c41e2005-04-21 17:06:20 -070011688static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011689{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011690 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080011691 u16 pmcsr;
11692
11693 /* On some early chips the SRAM cannot be accessed in D3hot state,
11694 * so need make sure we're in D0.
11695 */
11696 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
11697 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
11698 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
11699 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070011700
11701 /* Make sure register accesses (indirect or otherwise)
11702 * will function correctly.
11703 */
11704 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
11705 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011706
David S. Millerf49639e2006-06-09 11:58:36 -070011707 /* The memory arbiter has to be enabled in order for SRAM accesses
11708 * to succeed. Normally on powerup the tg3 chip firmware will make
11709 * sure it is enabled, but other entities such as system netboot
11710 * code might disable it.
11711 */
11712 val = tr32(MEMARB_MODE);
11713 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
11714
Linus Torvalds1da177e2005-04-16 15:20:36 -070011715 tp->phy_id = PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070011716 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
11717
Gary Zambranoa85feb82007-05-05 11:52:19 -070011718 /* Assume an onboard device and WOL capable by default. */
11719 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080011720
Michael Chanb5d37722006-09-27 16:06:21 -070011721 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080011722 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070011723 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080011724 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
11725 }
Matt Carlson0527ba32007-10-10 18:03:30 -070011726 val = tr32(VCPU_CFGSHDW);
11727 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070011728 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070011729 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080011730 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070011731 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080011732 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070011733 }
11734
Linus Torvalds1da177e2005-04-16 15:20:36 -070011735 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
11736 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
11737 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070011738 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070011739 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011740
11741 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
11742 tp->nic_sram_data_cfg = nic_cfg;
11743
11744 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
11745 ver >>= NIC_SRAM_DATA_VER_SHIFT;
11746 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
11747 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
11748 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
11749 (ver > 0) && (ver < 0x100))
11750 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
11751
Matt Carlsona9daf362008-05-25 23:49:44 -070011752 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11753 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
11754
Linus Torvalds1da177e2005-04-16 15:20:36 -070011755 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
11756 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
11757 eeprom_phy_serdes = 1;
11758
11759 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
11760 if (nic_phy_id != 0) {
11761 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
11762 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
11763
11764 eeprom_phy_id = (id1 >> 16) << 10;
11765 eeprom_phy_id |= (id2 & 0xfc00) << 16;
11766 eeprom_phy_id |= (id2 & 0x03ff) << 0;
11767 } else
11768 eeprom_phy_id = 0;
11769
Michael Chan7d0c41e2005-04-21 17:06:20 -070011770 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070011771 if (eeprom_phy_serdes) {
Michael Chana4e2b342005-10-26 15:46:52 -070011772 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan747e8f82005-07-25 12:33:22 -070011773 tp->tg3_flags2 |= TG3_FLG2_MII_SERDES;
11774 else
11775 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
11776 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070011777
John W. Linvillecbf46852005-04-21 17:01:29 -070011778 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011779 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
11780 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070011781 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070011782 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
11783
11784 switch (led_cfg) {
11785 default:
11786 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
11787 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
11788 break;
11789
11790 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
11791 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
11792 break;
11793
11794 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
11795 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070011796
11797 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
11798 * read on some older 5700/5701 bootcode.
11799 */
11800 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
11801 ASIC_REV_5700 ||
11802 GET_ASIC_REV(tp->pci_chip_rev_id) ==
11803 ASIC_REV_5701)
11804 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
11805
Linus Torvalds1da177e2005-04-16 15:20:36 -070011806 break;
11807
11808 case SHASTA_EXT_LED_SHARED:
11809 tp->led_ctrl = LED_CTRL_MODE_SHARED;
11810 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
11811 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
11812 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
11813 LED_CTRL_MODE_PHY_2);
11814 break;
11815
11816 case SHASTA_EXT_LED_MAC:
11817 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
11818 break;
11819
11820 case SHASTA_EXT_LED_COMBO:
11821 tp->led_ctrl = LED_CTRL_MODE_COMBO;
11822 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
11823 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
11824 LED_CTRL_MODE_PHY_2);
11825 break;
11826
Stephen Hemminger855e1112008-04-16 16:37:28 -070011827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011828
11829 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
11830 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
11831 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
11832 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
11833
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011834 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
11835 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080011836
Michael Chan9d26e212006-12-07 00:21:14 -080011837 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011838 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080011839 if ((tp->pdev->subsystem_vendor ==
11840 PCI_VENDOR_ID_ARIMA) &&
11841 (tp->pdev->subsystem_device == 0x205a ||
11842 tp->pdev->subsystem_device == 0x2063))
11843 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
11844 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070011845 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080011846 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
11847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011848
11849 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
11850 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070011851 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011852 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
11853 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080011854
11855 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
11856 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070011857 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080011858
Gary Zambranoa85feb82007-05-05 11:52:19 -070011859 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES &&
11860 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
11861 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011862
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070011863 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080011864 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070011865 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
11866
Linus Torvalds1da177e2005-04-16 15:20:36 -070011867 if (cfg2 & (1 << 17))
11868 tp->tg3_flags2 |= TG3_FLG2_CAPACITIVE_COUPLING;
11869
11870 /* serdes signal pre-emphasis in register 0x590 set by */
11871 /* bootcode if bit 18 is set */
11872 if (cfg2 & (1 << 18))
11873 tp->tg3_flags2 |= TG3_FLG2_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070011874
Matt Carlson321d32a2008-11-21 17:22:19 -080011875 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
11876 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080011877 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
11878 tp->tg3_flags3 |= TG3_FLG3_PHY_ENABLE_APD;
11879
Matt Carlson8ed5d972007-05-07 00:25:49 -070011880 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
11881 u32 cfg3;
11882
11883 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
11884 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
11885 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
11886 }
Matt Carlsona9daf362008-05-25 23:49:44 -070011887
11888 if (cfg4 & NIC_SRAM_RGMII_STD_IBND_DISABLE)
11889 tp->tg3_flags3 |= TG3_FLG3_RGMII_STD_IBND_DISABLE;
11890 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
11891 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
11892 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
11893 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011894 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080011895done:
11896 device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
11897 device_set_wakeup_enable(&tp->pdev->dev,
11898 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Michael Chan7d0c41e2005-04-21 17:06:20 -070011899}
11900
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011901static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
11902{
11903 int i;
11904 u32 val;
11905
11906 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
11907 tw32(OTP_CTRL, cmd);
11908
11909 /* Wait for up to 1 ms for command to execute. */
11910 for (i = 0; i < 100; i++) {
11911 val = tr32(OTP_STATUS);
11912 if (val & OTP_STATUS_CMD_DONE)
11913 break;
11914 udelay(10);
11915 }
11916
11917 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
11918}
11919
11920/* Read the gphy configuration from the OTP region of the chip. The gphy
11921 * configuration is a 32-bit value that straddles the alignment boundary.
11922 * We do two 32-bit reads and then shift and merge the results.
11923 */
11924static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
11925{
11926 u32 bhalf_otp, thalf_otp;
11927
11928 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
11929
11930 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
11931 return 0;
11932
11933 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
11934
11935 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
11936 return 0;
11937
11938 thalf_otp = tr32(OTP_READ_DATA);
11939
11940 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
11941
11942 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
11943 return 0;
11944
11945 bhalf_otp = tr32(OTP_READ_DATA);
11946
11947 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
11948}
11949
Michael Chan7d0c41e2005-04-21 17:06:20 -070011950static int __devinit tg3_phy_probe(struct tg3 *tp)
11951{
11952 u32 hw_phy_id_1, hw_phy_id_2;
11953 u32 hw_phy_id, hw_phy_id_masked;
11954 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011955
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011956 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
11957 return tg3_phy_init(tp);
11958
Linus Torvalds1da177e2005-04-16 15:20:36 -070011959 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010011960 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011961 */
11962 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070011963 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
11964 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011965 hw_phy_id = hw_phy_id_masked = PHY_ID_INVALID;
11966 } else {
11967 /* Now read the physical PHY_ID from the chip and verify
11968 * that it is sane. If it doesn't look good, we fall back
11969 * to either the hard-coded table based PHY_ID and failing
11970 * that the value found in the eeprom area.
11971 */
11972 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
11973 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
11974
11975 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
11976 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
11977 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
11978
11979 hw_phy_id_masked = hw_phy_id & PHY_ID_MASK;
11980 }
11981
11982 if (!err && KNOWN_PHY_ID(hw_phy_id_masked)) {
11983 tp->phy_id = hw_phy_id;
11984 if (hw_phy_id_masked == PHY_ID_BCM8002)
11985 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070011986 else
11987 tp->tg3_flags2 &= ~TG3_FLG2_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011988 } else {
Michael Chan7d0c41e2005-04-21 17:06:20 -070011989 if (tp->phy_id != PHY_ID_INVALID) {
11990 /* Do nothing, phy ID already set up in
11991 * tg3_get_eeprom_hw_cfg().
11992 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011993 } else {
11994 struct subsys_tbl_ent *p;
11995
11996 /* No eeprom signature? Try the hardcoded
11997 * subsys device table.
11998 */
11999 p = lookup_by_subsys(tp);
12000 if (!p)
12001 return -ENODEV;
12002
12003 tp->phy_id = p->phy_id;
12004 if (!tp->phy_id ||
12005 tp->phy_id == PHY_ID_BCM8002)
12006 tp->tg3_flags2 |= TG3_FLG2_PHY_SERDES;
12007 }
12008 }
12009
Michael Chan747e8f82005-07-25 12:33:22 -070012010 if (!(tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012011 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012012 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012013 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012014
12015 tg3_readphy(tp, MII_BMSR, &bmsr);
12016 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12017 (bmsr & BMSR_LSTATUS))
12018 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012019
Linus Torvalds1da177e2005-04-16 15:20:36 -070012020 err = tg3_phy_reset(tp);
12021 if (err)
12022 return err;
12023
12024 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12025 ADVERTISE_100HALF | ADVERTISE_100FULL |
12026 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12027 tg3_ctrl = 0;
12028 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY)) {
12029 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12030 MII_TG3_CTRL_ADV_1000_FULL);
12031 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12032 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12033 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12034 MII_TG3_CTRL_ENABLE_AS_MASTER);
12035 }
12036
Michael Chan3600d912006-12-07 00:21:48 -080012037 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12038 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12039 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12040 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012041 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12042
12043 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12044 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12045
12046 tg3_writephy(tp, MII_BMCR,
12047 BMCR_ANENABLE | BMCR_ANRESTART);
12048 }
12049 tg3_phy_set_wirespeed(tp);
12050
12051 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12052 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY))
12053 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12054 }
12055
12056skip_phy_reset:
12057 if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
12058 err = tg3_init_5401phy_dsp(tp);
12059 if (err)
12060 return err;
12061 }
12062
12063 if (!err && ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)) {
12064 err = tg3_init_5401phy_dsp(tp);
12065 }
12066
Michael Chan747e8f82005-07-25 12:33:22 -070012067 if (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012068 tp->link_config.advertising =
12069 (ADVERTISED_1000baseT_Half |
12070 ADVERTISED_1000baseT_Full |
12071 ADVERTISED_Autoneg |
12072 ADVERTISED_FIBRE);
12073 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
12074 tp->link_config.advertising &=
12075 ~(ADVERTISED_1000baseT_Half |
12076 ADVERTISED_1000baseT_Full);
12077
12078 return err;
12079}
12080
12081static void __devinit tg3_read_partno(struct tg3 *tp)
12082{
Matt Carlson6d348f22009-02-25 14:25:52 +000012083 unsigned char vpd_data[256]; /* in little-endian format */
Michael Chanaf2c6a42006-11-07 14:57:51 -080012084 unsigned int i;
Michael Chan1b277772006-03-20 22:27:48 -080012085 u32 magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012086
Matt Carlsondf259d82009-04-20 06:57:14 +000012087 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
12088 tg3_nvram_read(tp, 0x0, &magic))
David S. Millerf49639e2006-06-09 11:58:36 -070012089 goto out_not_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012090
Michael Chan18201802006-03-20 22:29:15 -080012091 if (magic == TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080012092 for (i = 0; i < 256; i += 4) {
12093 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012094
Matt Carlson6d348f22009-02-25 14:25:52 +000012095 /* The data is in little-endian format in NVRAM.
12096 * Use the big-endian read routines to preserve
12097 * the byte order as it exists in NVRAM.
12098 */
12099 if (tg3_nvram_read_be32(tp, 0x100 + i, &tmp))
Michael Chan1b277772006-03-20 22:27:48 -080012100 goto out_not_found;
12101
Matt Carlson6d348f22009-02-25 14:25:52 +000012102 memcpy(&vpd_data[i], &tmp, sizeof(tmp));
Michael Chan1b277772006-03-20 22:27:48 -080012103 }
12104 } else {
12105 int vpd_cap;
12106
12107 vpd_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_VPD);
12108 for (i = 0; i < 256; i += 4) {
12109 u32 tmp, j = 0;
Al Virob9fc7dc2007-12-17 22:59:57 -080012110 __le32 v;
Michael Chan1b277772006-03-20 22:27:48 -080012111 u16 tmp16;
12112
12113 pci_write_config_word(tp->pdev, vpd_cap + PCI_VPD_ADDR,
12114 i);
12115 while (j++ < 100) {
12116 pci_read_config_word(tp->pdev, vpd_cap +
12117 PCI_VPD_ADDR, &tmp16);
12118 if (tmp16 & 0x8000)
12119 break;
12120 msleep(1);
12121 }
David S. Millerf49639e2006-06-09 11:58:36 -070012122 if (!(tmp16 & 0x8000))
12123 goto out_not_found;
12124
Michael Chan1b277772006-03-20 22:27:48 -080012125 pci_read_config_dword(tp->pdev, vpd_cap + PCI_VPD_DATA,
12126 &tmp);
Al Virob9fc7dc2007-12-17 22:59:57 -080012127 v = cpu_to_le32(tmp);
Matt Carlson6d348f22009-02-25 14:25:52 +000012128 memcpy(&vpd_data[i], &v, sizeof(v));
Michael Chan1b277772006-03-20 22:27:48 -080012129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012130 }
12131
12132 /* Now parse and find the part number. */
Michael Chanaf2c6a42006-11-07 14:57:51 -080012133 for (i = 0; i < 254; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012134 unsigned char val = vpd_data[i];
Michael Chanaf2c6a42006-11-07 14:57:51 -080012135 unsigned int block_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012136
12137 if (val == 0x82 || val == 0x91) {
12138 i = (i + 3 +
12139 (vpd_data[i + 1] +
12140 (vpd_data[i + 2] << 8)));
12141 continue;
12142 }
12143
12144 if (val != 0x90)
12145 goto out_not_found;
12146
12147 block_end = (i + 3 +
12148 (vpd_data[i + 1] +
12149 (vpd_data[i + 2] << 8)));
12150 i += 3;
Michael Chanaf2c6a42006-11-07 14:57:51 -080012151
12152 if (block_end > 256)
12153 goto out_not_found;
12154
12155 while (i < (block_end - 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012156 if (vpd_data[i + 0] == 'P' &&
12157 vpd_data[i + 1] == 'N') {
12158 int partno_len = vpd_data[i + 2];
12159
Michael Chanaf2c6a42006-11-07 14:57:51 -080012160 i += 3;
12161 if (partno_len > 24 || (partno_len + i) > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012162 goto out_not_found;
12163
12164 memcpy(tp->board_part_number,
Michael Chanaf2c6a42006-11-07 14:57:51 -080012165 &vpd_data[i], partno_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012166
12167 /* Success. */
12168 return;
12169 }
Michael Chanaf2c6a42006-11-07 14:57:51 -080012170 i += 3 + vpd_data[i + 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070012171 }
12172
12173 /* Part number not found. */
12174 goto out_not_found;
12175 }
12176
12177out_not_found:
Michael Chanb5d37722006-09-27 16:06:21 -070012178 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12179 strcpy(tp->board_part_number, "BCM95906");
Matt Carlsondf259d82009-04-20 06:57:14 +000012180 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12181 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
12182 strcpy(tp->board_part_number, "BCM57780");
12183 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12184 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
12185 strcpy(tp->board_part_number, "BCM57760");
12186 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12187 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
12188 strcpy(tp->board_part_number, "BCM57790");
Matt Carlson5e7ccf22009-08-25 10:08:42 +000012189 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
12190 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
12191 strcpy(tp->board_part_number, "BCM57788");
Michael Chanb5d37722006-09-27 16:06:21 -070012192 else
12193 strcpy(tp->board_part_number, "none");
Linus Torvalds1da177e2005-04-16 15:20:36 -070012194}
12195
Matt Carlson9c8a6202007-10-21 16:16:08 -070012196static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
12197{
12198 u32 val;
12199
Matt Carlsone4f34112009-02-25 14:25:00 +000012200 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012201 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012202 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012203 val != 0)
12204 return 0;
12205
12206 return 1;
12207}
12208
Matt Carlsonacd9c112009-02-25 14:26:33 +000012209static void __devinit tg3_read_bc_ver(struct tg3 *tp)
12210{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012211 u32 val, offset, start, ver_offset;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012212 int i;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012213 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012214
12215 if (tg3_nvram_read(tp, 0xc, &offset) ||
12216 tg3_nvram_read(tp, 0x4, &start))
12217 return;
12218
12219 offset = tg3_nvram_logical_addr(tp, offset);
12220
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012221 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012222 return;
12223
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012224 if ((val & 0xfc000000) == 0x0c000000) {
12225 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000012226 return;
12227
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000012228 if (val == 0)
12229 newver = true;
12230 }
12231
12232 if (newver) {
12233 if (tg3_nvram_read(tp, offset + 8, &ver_offset))
12234 return;
12235
12236 offset = offset + ver_offset - start;
12237 for (i = 0; i < 16; i += 4) {
12238 __be32 v;
12239 if (tg3_nvram_read_be32(tp, offset + i, &v))
12240 return;
12241
12242 memcpy(tp->fw_ver + i, &v, sizeof(v));
12243 }
12244 } else {
12245 u32 major, minor;
12246
12247 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
12248 return;
12249
12250 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
12251 TG3_NVM_BCVER_MAJSFT;
12252 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
12253 snprintf(&tp->fw_ver[0], 32, "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012254 }
12255}
12256
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012257static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
12258{
12259 u32 val, major, minor;
12260
12261 /* Use native endian representation */
12262 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
12263 return;
12264
12265 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
12266 TG3_NVM_HWSB_CFG1_MAJSFT;
12267 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
12268 TG3_NVM_HWSB_CFG1_MINSFT;
12269
12270 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
12271}
12272
Matt Carlsondfe00d72008-11-21 17:19:41 -080012273static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
12274{
12275 u32 offset, major, minor, build;
12276
12277 tp->fw_ver[0] = 's';
12278 tp->fw_ver[1] = 'b';
12279 tp->fw_ver[2] = '\0';
12280
12281 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
12282 return;
12283
12284 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
12285 case TG3_EEPROM_SB_REVISION_0:
12286 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
12287 break;
12288 case TG3_EEPROM_SB_REVISION_2:
12289 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
12290 break;
12291 case TG3_EEPROM_SB_REVISION_3:
12292 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
12293 break;
12294 default:
12295 return;
12296 }
12297
Matt Carlsone4f34112009-02-25 14:25:00 +000012298 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080012299 return;
12300
12301 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
12302 TG3_EEPROM_SB_EDH_BLD_SHFT;
12303 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
12304 TG3_EEPROM_SB_EDH_MAJ_SHFT;
12305 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
12306
12307 if (minor > 99 || build > 26)
12308 return;
12309
12310 snprintf(&tp->fw_ver[2], 30, " v%d.%02d", major, minor);
12311
12312 if (build > 0) {
12313 tp->fw_ver[8] = 'a' + build - 1;
12314 tp->fw_ver[9] = '\0';
12315 }
12316}
12317
Matt Carlsonacd9c112009-02-25 14:26:33 +000012318static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080012319{
12320 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000012321 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070012322
12323 for (offset = TG3_NVM_DIR_START;
12324 offset < TG3_NVM_DIR_END;
12325 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012326 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012327 return;
12328
12329 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
12330 break;
12331 }
12332
12333 if (offset == TG3_NVM_DIR_END)
12334 return;
12335
12336 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
12337 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000012338 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012339 return;
12340
Matt Carlsone4f34112009-02-25 14:25:00 +000012341 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070012342 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000012343 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012344 return;
12345
12346 offset += val - start;
12347
Matt Carlsonacd9c112009-02-25 14:26:33 +000012348 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012349
Matt Carlsonacd9c112009-02-25 14:26:33 +000012350 tp->fw_ver[vlen++] = ',';
12351 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070012352
12353 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012354 __be32 v;
12355 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070012356 return;
12357
Al Virob9fc7dc2007-12-17 22:59:57 -080012358 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012359
Matt Carlsonacd9c112009-02-25 14:26:33 +000012360 if (vlen > TG3_VER_SIZE - sizeof(v)) {
12361 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012362 break;
12363 }
12364
Matt Carlsonacd9c112009-02-25 14:26:33 +000012365 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
12366 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012367 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000012368}
12369
Matt Carlson7fd76442009-02-25 14:27:20 +000012370static void __devinit tg3_read_dash_ver(struct tg3 *tp)
12371{
12372 int vlen;
12373 u32 apedata;
12374
12375 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
12376 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
12377 return;
12378
12379 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
12380 if (apedata != APE_SEG_SIG_MAGIC)
12381 return;
12382
12383 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
12384 if (!(apedata & APE_FW_STATUS_READY))
12385 return;
12386
12387 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
12388
12389 vlen = strlen(tp->fw_ver);
12390
12391 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " DASH v%d.%d.%d.%d",
12392 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
12393 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
12394 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
12395 (apedata & APE_FW_VERSION_BLDMSK));
12396}
12397
Matt Carlsonacd9c112009-02-25 14:26:33 +000012398static void __devinit tg3_read_fw_ver(struct tg3 *tp)
12399{
12400 u32 val;
12401
Matt Carlsondf259d82009-04-20 06:57:14 +000012402 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
12403 tp->fw_ver[0] = 's';
12404 tp->fw_ver[1] = 'b';
12405 tp->fw_ver[2] = '\0';
12406
12407 return;
12408 }
12409
Matt Carlsonacd9c112009-02-25 14:26:33 +000012410 if (tg3_nvram_read(tp, 0, &val))
12411 return;
12412
12413 if (val == TG3_EEPROM_MAGIC)
12414 tg3_read_bc_ver(tp);
12415 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
12416 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000012417 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12418 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000012419 else
12420 return;
12421
12422 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12423 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
12424 return;
12425
12426 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070012427
12428 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080012429}
12430
Michael Chan7544b092007-05-05 13:08:32 -070012431static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
12432
Linus Torvalds1da177e2005-04-16 15:20:36 -070012433static int __devinit tg3_get_invariants(struct tg3 *tp)
12434{
12435 static struct pci_device_id write_reorder_chipsets[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012436 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
12437 PCI_DEVICE_ID_AMD_FE_GATE_700C) },
John W. Linvillec165b002006-07-08 13:28:53 -070012438 { PCI_DEVICE(PCI_VENDOR_ID_AMD,
12439 PCI_DEVICE_ID_AMD_8131_BRIDGE) },
Michael Chan399de502005-10-03 14:02:39 -070012440 { PCI_DEVICE(PCI_VENDOR_ID_VIA,
12441 PCI_DEVICE_ID_VIA_8385_0) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012442 { },
12443 };
12444 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012445 u32 pci_state_reg, grc_misc_cfg;
12446 u32 val;
12447 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012448 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012449
Linus Torvalds1da177e2005-04-16 15:20:36 -070012450 /* Force memory write invalidate off. If we leave it on,
12451 * then on 5700_BX chips we have to enable a workaround.
12452 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
12453 * to match the cacheline size. The Broadcom driver have this
12454 * workaround but turns MWI off all the times so never uses
12455 * it. This seems to suggest that the workaround is insufficient.
12456 */
12457 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12458 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
12459 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12460
12461 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
12462 * has the register indirect write enable bit set before
12463 * we try to access any of the MMIO registers. It is also
12464 * critical that the PCI-X hw workaround situation is decided
12465 * before that as well.
12466 */
12467 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12468 &misc_ctrl_reg);
12469
12470 tp->pci_chip_rev_id = (misc_ctrl_reg >>
12471 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070012472 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
12473 u32 prod_id_asic_rev;
12474
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012475 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717C ||
12476 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717S ||
12477 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718C ||
12478 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718S)
12479 pci_read_config_dword(tp->pdev,
12480 TG3PCI_GEN2_PRODID_ASICREV,
12481 &prod_id_asic_rev);
12482 else
12483 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
12484 &prod_id_asic_rev);
12485
Matt Carlson321d32a2008-11-21 17:22:19 -080012486 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070012487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012488
Michael Chanff645be2005-04-21 17:09:53 -070012489 /* Wrong chip ID in 5752 A0. This code can be removed later
12490 * as A0 is not in production.
12491 */
12492 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
12493 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
12494
Michael Chan68929142005-08-09 20:17:14 -070012495 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
12496 * we need to disable memory and use config. cycles
12497 * only to access all registers. The 5702/03 chips
12498 * can mistakenly decode the special cycles from the
12499 * ICH chipsets as memory write cycles, causing corruption
12500 * of register and memory space. Only certain ICH bridges
12501 * will drive special cycles with non-zero data during the
12502 * address phase which can fall within the 5703's address
12503 * range. This is not an ICH bug as the PCI spec allows
12504 * non-zero address during special cycles. However, only
12505 * these ICH bridges are known to drive non-zero addresses
12506 * during special cycles.
12507 *
12508 * Since special cycles do not cross PCI bridges, we only
12509 * enable this workaround if the 5703 is on the secondary
12510 * bus of these ICH bridges.
12511 */
12512 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
12513 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
12514 static struct tg3_dev_id {
12515 u32 vendor;
12516 u32 device;
12517 u32 rev;
12518 } ich_chipsets[] = {
12519 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
12520 PCI_ANY_ID },
12521 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
12522 PCI_ANY_ID },
12523 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
12524 0xa },
12525 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
12526 PCI_ANY_ID },
12527 { },
12528 };
12529 struct tg3_dev_id *pci_id = &ich_chipsets[0];
12530 struct pci_dev *bridge = NULL;
12531
12532 while (pci_id->vendor != 0) {
12533 bridge = pci_get_device(pci_id->vendor, pci_id->device,
12534 bridge);
12535 if (!bridge) {
12536 pci_id++;
12537 continue;
12538 }
12539 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070012540 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070012541 continue;
12542 }
12543 if (bridge->subordinate &&
12544 (bridge->subordinate->number ==
12545 tp->pdev->bus->number)) {
12546
12547 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
12548 pci_dev_put(bridge);
12549 break;
12550 }
12551 }
12552 }
12553
Matt Carlson41588ba2008-04-19 18:12:33 -070012554 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
12555 static struct tg3_dev_id {
12556 u32 vendor;
12557 u32 device;
12558 } bridge_chipsets[] = {
12559 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
12560 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
12561 { },
12562 };
12563 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
12564 struct pci_dev *bridge = NULL;
12565
12566 while (pci_id->vendor != 0) {
12567 bridge = pci_get_device(pci_id->vendor,
12568 pci_id->device,
12569 bridge);
12570 if (!bridge) {
12571 pci_id++;
12572 continue;
12573 }
12574 if (bridge->subordinate &&
12575 (bridge->subordinate->number <=
12576 tp->pdev->bus->number) &&
12577 (bridge->subordinate->subordinate >=
12578 tp->pdev->bus->number)) {
12579 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
12580 pci_dev_put(bridge);
12581 break;
12582 }
12583 }
12584 }
12585
Michael Chan4a29cc22006-03-19 13:21:12 -080012586 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
12587 * DMA addresses > 40-bit. This bridge may have other additional
12588 * 57xx devices behind it in some 4-port NIC designs for example.
12589 * Any tg3 device found behind the bridge will also need the 40-bit
12590 * DMA workaround.
12591 */
Michael Chana4e2b342005-10-26 15:46:52 -070012592 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
12593 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
12594 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080012595 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070012596 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Michael Chana4e2b342005-10-26 15:46:52 -070012597 }
Michael Chan4a29cc22006-03-19 13:21:12 -080012598 else {
12599 struct pci_dev *bridge = NULL;
12600
12601 do {
12602 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
12603 PCI_DEVICE_ID_SERVERWORKS_EPB,
12604 bridge);
12605 if (bridge && bridge->subordinate &&
12606 (bridge->subordinate->number <=
12607 tp->pdev->bus->number) &&
12608 (bridge->subordinate->subordinate >=
12609 tp->pdev->bus->number)) {
12610 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
12611 pci_dev_put(bridge);
12612 break;
12613 }
12614 } while (bridge);
12615 }
Michael Chan4cf78e42005-07-25 12:29:19 -070012616
Linus Torvalds1da177e2005-04-16 15:20:36 -070012617 /* Initialize misc host control in PCI block. */
12618 tp->misc_host_ctrl |= (misc_ctrl_reg &
12619 MISC_HOST_CTRL_CHIPREV);
12620 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12621 tp->misc_host_ctrl);
12622
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012623 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
12624 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
12625 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Michael Chan7544b092007-05-05 13:08:32 -070012626 tp->pdev_peer = tg3_find_peer(tp);
12627
Matt Carlson321d32a2008-11-21 17:22:19 -080012628 /* Intentionally exclude ASIC_REV_5906 */
12629 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad12006-03-20 22:27:35 -080012630 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070012631 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070012632 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012633 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012634 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12635 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson321d32a2008-11-21 17:22:19 -080012636 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
12637
12638 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
12639 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070012640 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012641 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070012642 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070012643 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
12644
John W. Linville1b440c562005-04-21 17:03:18 -070012645 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
12646 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
12647 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
12648
Matt Carlson027455a2008-12-21 20:19:30 -080012649 /* 5700 B0 chips do not support checksumming correctly due
12650 * to hardware bugs.
12651 */
12652 if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
12653 tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
12654 else {
12655 tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
12656 tp->dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
12657 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
12658 tp->dev->features |= NETIF_F_IPV6_CSUM;
12659 }
12660
Michael Chan5a6f3072006-03-20 22:28:05 -080012661 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070012662 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
12663 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
12664 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
12665 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
12666 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
12667 tp->pdev_peer == tp->pdev))
12668 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
12669
Matt Carlson321d32a2008-11-21 17:22:19 -080012670 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070012671 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan5a6f3072006-03-20 22:28:05 -080012672 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
Michael Chanfcfa0a32006-03-20 22:28:41 -080012673 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070012674 } else {
Michael Chan7f62ad52007-02-20 23:25:40 -080012675 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
Michael Chan52c0fd82006-06-29 20:15:54 -070012676 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12677 ASIC_REV_5750 &&
12678 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Michael Chan7f62ad52007-02-20 23:25:40 -080012679 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
Michael Chan52c0fd82006-06-29 20:15:54 -070012680 }
Michael Chan5a6f3072006-03-20 22:28:05 -080012681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012682
Matt Carlson4f125f42009-09-01 12:55:02 +000012683 tp->irq_max = 1;
12684
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012685 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
12686 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
12687 tp->irq_max = TG3_IRQ_MAX_VECS;
12688 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000012689
12690 if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +000012691 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12692 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
12693 else {
12694 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
12695 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
12696 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000012697 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012698
Matt Carlsonf51f3562008-05-25 23:45:08 -070012699 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012700 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
12701 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson8f666b02009-08-28 13:58:24 +000012702 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070012703
Matt Carlson52f44902008-11-21 17:17:04 -080012704 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
12705 &pci_state_reg);
12706
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012707 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
12708 if (tp->pcie_cap != 0) {
12709 u16 lnkctl;
12710
Linus Torvalds1da177e2005-04-16 15:20:36 -070012711 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080012712
12713 pcie_set_readrq(tp->pdev, 4096);
12714
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012715 pci_read_config_word(tp->pdev,
12716 tp->pcie_cap + PCI_EXP_LNKCTL,
12717 &lnkctl);
12718 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
12719 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080012720 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012721 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012722 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000012723 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
12724 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080012725 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Michael Chanc7835a72006-11-15 21:14:42 -080012726 }
Matt Carlson52f44902008-11-21 17:17:04 -080012727 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080012728 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080012729 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
12730 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
12731 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
12732 if (!tp->pcix_cap) {
12733 printk(KERN_ERR PFX "Cannot find PCI-X "
12734 "capability, aborting.\n");
12735 return -EIO;
12736 }
12737
12738 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
12739 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
12740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012741
Michael Chan399de502005-10-03 14:02:39 -070012742 /* If we have an AMD 762 or VIA K8T800 chipset, write
12743 * reordering to the mailbox registers done by the host
12744 * controller can cause major troubles. We read back from
12745 * every mailbox register write to force the writes to be
12746 * posted to the chip in order.
12747 */
12748 if (pci_dev_present(write_reorder_chipsets) &&
12749 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
12750 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
12751
Matt Carlson69fc4052008-12-21 20:19:57 -080012752 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
12753 &tp->pci_cacheline_sz);
12754 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
12755 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012756 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
12757 tp->pci_lat_timer < 64) {
12758 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080012759 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
12760 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012761 }
12762
Matt Carlson52f44902008-11-21 17:17:04 -080012763 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
12764 /* 5700 BX chips need to have their TX producer index
12765 * mailboxes written twice to workaround a bug.
12766 */
12767 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070012768
Matt Carlson52f44902008-11-21 17:17:04 -080012769 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012770 *
12771 * The workaround is to use indirect register accesses
12772 * for all chip writes not to mailbox registers.
12773 */
Matt Carlson52f44902008-11-21 17:17:04 -080012774 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012775 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012776
12777 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
12778
12779 /* The chip can have it's power management PCI config
12780 * space registers clobbered due to this bug.
12781 * So explicitly force the chip into D0 here.
12782 */
Matt Carlson9974a352007-10-07 23:27:28 -070012783 pci_read_config_dword(tp->pdev,
12784 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012785 &pm_reg);
12786 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
12787 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070012788 pci_write_config_dword(tp->pdev,
12789 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012790 pm_reg);
12791
12792 /* Also, force SERR#/PERR# in PCI command. */
12793 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12794 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
12795 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12796 }
12797 }
12798
Linus Torvalds1da177e2005-04-16 15:20:36 -070012799 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
12800 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
12801 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
12802 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
12803
12804 /* Chip-specific fixup from Broadcom driver */
12805 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
12806 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
12807 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
12808 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
12809 }
12810
Michael Chan1ee582d2005-08-09 20:16:46 -070012811 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070012812 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070012813 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070012814 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070012815 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070012816 tp->write32_tx_mbox = tg3_write32;
12817 tp->write32_rx_mbox = tg3_write32;
12818
12819 /* Various workaround register access methods */
12820 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
12821 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070012822 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
12823 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
12824 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
12825 /*
12826 * Back to back register writes can cause problems on these
12827 * chips, the workaround is to read back all reg writes
12828 * except those to mailbox regs.
12829 *
12830 * See tg3_write_indirect_reg32().
12831 */
Michael Chan1ee582d2005-08-09 20:16:46 -070012832 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070012833 }
12834
Michael Chan1ee582d2005-08-09 20:16:46 -070012835 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
12836 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
12837 tp->write32_tx_mbox = tg3_write32_tx_mbox;
12838 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
12839 tp->write32_rx_mbox = tg3_write_flush_reg32;
12840 }
Michael Chan20094932005-08-09 20:16:32 -070012841
Michael Chan68929142005-08-09 20:17:14 -070012842 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
12843 tp->read32 = tg3_read_indirect_reg32;
12844 tp->write32 = tg3_write_indirect_reg32;
12845 tp->read32_mbox = tg3_read_indirect_mbox;
12846 tp->write32_mbox = tg3_write_indirect_mbox;
12847 tp->write32_tx_mbox = tg3_write_indirect_mbox;
12848 tp->write32_rx_mbox = tg3_write_indirect_mbox;
12849
12850 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070012851 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070012852
12853 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
12854 pci_cmd &= ~PCI_COMMAND_MEMORY;
12855 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
12856 }
Michael Chanb5d37722006-09-27 16:06:21 -070012857 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
12858 tp->read32_mbox = tg3_read32_mbox_5906;
12859 tp->write32_mbox = tg3_write32_mbox_5906;
12860 tp->write32_tx_mbox = tg3_write32_mbox_5906;
12861 tp->write32_rx_mbox = tg3_write32_mbox_5906;
12862 }
Michael Chan68929142005-08-09 20:17:14 -070012863
Michael Chanbbadf502006-04-06 21:46:34 -070012864 if (tp->write32 == tg3_write_indirect_reg32 ||
12865 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
12866 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070012867 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070012868 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
12869
Michael Chan7d0c41e2005-04-21 17:06:20 -070012870 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080012871 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070012872 * determined before calling tg3_set_power_state() so that
12873 * we know whether or not to switch out of Vaux power.
12874 * When the flag is set, it means that GPIO1 is used for eeprom
12875 * write protect and also implies that it is a LOM where GPIOs
12876 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012877 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070012878 tg3_get_eeprom_hw_cfg(tp);
12879
Matt Carlson0d3031d2007-10-10 18:02:43 -070012880 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
12881 /* Allow reads and writes to the
12882 * APE register and memory space.
12883 */
12884 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
12885 PCISTATE_ALLOW_APE_SHMEM_WR;
12886 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
12887 pci_state_reg);
12888 }
12889
Matt Carlson9936bcf2007-10-10 18:03:07 -070012890 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012891 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080012892 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012893 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12894 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlsond30cdd22007-10-07 23:28:35 -070012895 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
12896
Michael Chan314fba32005-04-21 17:07:04 -070012897 /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
12898 * GPIO1 driven high will bring 5700's external PHY out of reset.
12899 * It is also used as eeprom write protect on LOMs.
12900 */
12901 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
12902 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
12903 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
12904 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
12905 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070012906 /* Unused GPIO3 must be driven as output on 5752 because there
12907 * are no pull-up resistors on unused GPIO pins.
12908 */
12909 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12910 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070012911
Matt Carlson321d32a2008-11-21 17:22:19 -080012912 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
12913 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Michael Chanaf36e6b2006-03-23 01:28:06 -080012914 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
12915
Matt Carlson8d519ab2009-04-20 06:58:01 +000012916 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
12917 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070012918 /* Turn off the debug UART. */
12919 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
12920 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
12921 /* Keep VMain power. */
12922 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
12923 GRC_LCLCTRL_GPIO_OUTPUT0;
12924 }
12925
Linus Torvalds1da177e2005-04-16 15:20:36 -070012926 /* Force the chip into D0. */
Michael Chanbc1c7562006-03-20 17:48:03 -080012927 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012928 if (err) {
12929 printk(KERN_ERR PFX "(%s) transition to D0 failed\n",
12930 pci_name(tp->pdev));
12931 return err;
12932 }
12933
Linus Torvalds1da177e2005-04-16 15:20:36 -070012934 /* Derive initial jumbo mode from MTU assigned in
12935 * ether_setup() via the alloc_etherdev() call
12936 */
Michael Chan0f893dc2005-07-25 12:30:38 -070012937 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070012938 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070012939 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012940
12941 /* Determine WakeOnLan speed to use. */
12942 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12943 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12944 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
12945 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
12946 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
12947 } else {
12948 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
12949 }
12950
Matt Carlson7f97a4b2009-08-25 10:10:03 +000012951 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12952 tp->tg3_flags3 |= TG3_FLG3_PHY_IS_FET;
12953
Linus Torvalds1da177e2005-04-16 15:20:36 -070012954 /* A few boards don't want Ethernet@WireSpeed phy feature */
12955 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
12956 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
12957 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070012958 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000012959 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) ||
Michael Chan747e8f82005-07-25 12:33:22 -070012960 (tp->tg3_flags2 & TG3_FLG2_ANY_SERDES))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012961 tp->tg3_flags2 |= TG3_FLG2_NO_ETH_WIRE_SPEED;
12962
12963 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
12964 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
12965 tp->tg3_flags2 |= TG3_FLG2_PHY_ADC_BUG;
12966 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
12967 tp->tg3_flags2 |= TG3_FLG2_PHY_5704_A0_BUG;
12968
Matt Carlson321d32a2008-11-21 17:22:19 -080012969 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlson7f97a4b2009-08-25 10:10:03 +000012970 !(tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080012971 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000012972 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
12973 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
Michael Chanc424cb22006-04-29 18:56:34 -070012974 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070012975 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070012976 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12977 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080012978 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
12979 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
12980 tp->tg3_flags2 |= TG3_FLG2_PHY_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080012981 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
12982 tp->tg3_flags2 |= TG3_FLG2_PHY_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080012983 } else
Michael Chanc424cb22006-04-29 18:56:34 -070012984 tp->tg3_flags2 |= TG3_FLG2_PHY_BER_BUG;
12985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012986
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012987 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12988 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
12989 tp->phy_otp = tg3_read_otp_phycfg(tp);
12990 if (tp->phy_otp == 0)
12991 tp->phy_otp = TG3_OTP_DEFAULT;
12992 }
12993
Matt Carlsonf51f3562008-05-25 23:45:08 -070012994 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070012995 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
12996 else
12997 tp->mi_mode = MAC_MI_MODE_BASE;
12998
Linus Torvalds1da177e2005-04-16 15:20:36 -070012999 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013000 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13001 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13002 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13003
Matt Carlson321d32a2008-11-21 17:22:19 -080013004 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
13005 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070013006 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
13007
Matt Carlson158d7ab2008-05-29 01:37:54 -070013008 err = tg3_mdio_init(tp);
13009 if (err)
13010 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013011
13012 /* Initialize data/descriptor byte/word swapping. */
13013 val = tr32(GRC_MODE);
13014 val &= GRC_MODE_HOST_STACKUP;
13015 tw32(GRC_MODE, val | tp->grc_mode);
13016
13017 tg3_switch_clocks(tp);
13018
13019 /* Clear this out for sanity. */
13020 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
13021
13022 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13023 &pci_state_reg);
13024 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
13025 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
13026 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
13027
13028 if (chiprevid == CHIPREV_ID_5701_A0 ||
13029 chiprevid == CHIPREV_ID_5701_B0 ||
13030 chiprevid == CHIPREV_ID_5701_B2 ||
13031 chiprevid == CHIPREV_ID_5701_B5) {
13032 void __iomem *sram_base;
13033
13034 /* Write some dummy words into the SRAM status block
13035 * area, see if it reads back correctly. If the return
13036 * value is bad, force enable the PCIX workaround.
13037 */
13038 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
13039
13040 writel(0x00000000, sram_base);
13041 writel(0x00000000, sram_base + 4);
13042 writel(0xffffffff, sram_base + 4);
13043 if (readl(sram_base) != 0x00000000)
13044 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13045 }
13046 }
13047
13048 udelay(50);
13049 tg3_nvram_init(tp);
13050
13051 grc_misc_cfg = tr32(GRC_MISC_CFG);
13052 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
13053
Linus Torvalds1da177e2005-04-16 15:20:36 -070013054 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13055 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
13056 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
13057 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
13058
David S. Millerfac9b832005-05-18 22:46:34 -070013059 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
13060 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
13061 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
13062 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
13063 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
13064 HOSTCC_MODE_CLRTICK_TXBD);
13065
13066 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
13067 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13068 tp->misc_host_ctrl);
13069 }
13070
Matt Carlson3bda1252008-08-15 14:08:22 -070013071 /* Preserve the APE MAC_MODE bits */
13072 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
13073 tp->mac_mode = tr32(MAC_MODE) |
13074 MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
13075 else
13076 tp->mac_mode = TG3_DEF_MAC_MODE;
13077
Linus Torvalds1da177e2005-04-16 15:20:36 -070013078 /* these are limited to 10/100 only */
13079 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13080 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
13081 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
13082 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13083 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
13084 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
13085 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
13086 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
13087 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080013088 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
13089 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013090 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013091 (tp->tg3_flags3 & TG3_FLG3_PHY_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013092 tp->tg3_flags |= TG3_FLAG_10_100_ONLY;
13093
13094 err = tg3_phy_probe(tp);
13095 if (err) {
13096 printk(KERN_ERR PFX "(%s) phy probe failed, err %d\n",
13097 pci_name(tp->pdev), err);
13098 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013099 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013100 }
13101
13102 tg3_read_partno(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080013103 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013104
13105 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES) {
13106 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13107 } else {
13108 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13109 tp->tg3_flags |= TG3_FLAG_USE_MI_INTERRUPT;
13110 else
13111 tp->tg3_flags &= ~TG3_FLAG_USE_MI_INTERRUPT;
13112 }
13113
13114 /* 5700 {AX,BX} chips have a broken status block link
13115 * change bit implementation, so we must use the
13116 * status register in those cases.
13117 */
13118 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
13119 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
13120 else
13121 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
13122
13123 /* The led_ctrl is set during tg3_phy_probe, here we might
13124 * have to force the link status polling mechanism based
13125 * upon subsystem IDs.
13126 */
13127 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070013128 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013129 !(tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)) {
13130 tp->tg3_flags |= (TG3_FLAG_USE_MI_INTERRUPT |
13131 TG3_FLAG_USE_LINKCHG_REG);
13132 }
13133
13134 /* For all SERDES we poll the MAC status register. */
13135 if (tp->tg3_flags2 & TG3_FLG2_PHY_SERDES)
13136 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
13137 else
13138 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
13139
Matt Carlsonad829262008-11-21 17:16:16 -080013140 tp->rx_offset = NET_IP_ALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013141 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
13142 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0)
13143 tp->rx_offset = 0;
13144
Michael Chanf92905d2006-06-29 20:14:29 -070013145 tp->rx_std_max_post = TG3_RX_RING_SIZE;
13146
13147 /* Increment the rx prod index on the rx std ring by at most
13148 * 8 for these chips to workaround hw errata.
13149 */
13150 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13151 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
13152 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13153 tp->rx_std_max_post = 8;
13154
Matt Carlson8ed5d972007-05-07 00:25:49 -070013155 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
13156 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
13157 PCIE_PWR_MGMT_L1_THRESH_MSK;
13158
Linus Torvalds1da177e2005-04-16 15:20:36 -070013159 return err;
13160}
13161
David S. Miller49b6e95f2007-03-29 01:38:42 -070013162#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013163static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
13164{
13165 struct net_device *dev = tp->dev;
13166 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013167 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070013168 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070013169 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013170
David S. Miller49b6e95f2007-03-29 01:38:42 -070013171 addr = of_get_property(dp, "local-mac-address", &len);
13172 if (addr && len == 6) {
13173 memcpy(dev->dev_addr, addr, 6);
13174 memcpy(dev->perm_addr, dev->dev_addr, 6);
13175 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013176 }
13177 return -ENODEV;
13178}
13179
13180static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
13181{
13182 struct net_device *dev = tp->dev;
13183
13184 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070013185 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013186 return 0;
13187}
13188#endif
13189
13190static int __devinit tg3_get_device_address(struct tg3 *tp)
13191{
13192 struct net_device *dev = tp->dev;
13193 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080013194 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013195
David S. Miller49b6e95f2007-03-29 01:38:42 -070013196#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013197 if (!tg3_get_macaddr_sparc(tp))
13198 return 0;
13199#endif
13200
13201 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070013202 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013203 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013204 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
13205 mac_offset = 0xcc;
13206 if (tg3_nvram_lock(tp))
13207 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
13208 else
13209 tg3_nvram_unlock(tp);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013210 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13211 if (tr32(TG3_CPMU_STATUS) & TG3_CPMU_STATUS_PCIE_FUNC)
13212 mac_offset = 0xcc;
13213 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070013214 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013215
13216 /* First try to get it from MAC address mailbox. */
13217 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
13218 if ((hi >> 16) == 0x484b) {
13219 dev->dev_addr[0] = (hi >> 8) & 0xff;
13220 dev->dev_addr[1] = (hi >> 0) & 0xff;
13221
13222 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
13223 dev->dev_addr[2] = (lo >> 24) & 0xff;
13224 dev->dev_addr[3] = (lo >> 16) & 0xff;
13225 dev->dev_addr[4] = (lo >> 8) & 0xff;
13226 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013227
Michael Chan008652b2006-03-27 23:14:53 -080013228 /* Some old bootcode may report a 0 MAC address in SRAM */
13229 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
13230 }
13231 if (!addr_ok) {
13232 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000013233 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
13234 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000013235 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070013236 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
13237 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080013238 }
13239 /* Finally just fetch it out of the MAC control regs. */
13240 else {
13241 hi = tr32(MAC_ADDR_0_HIGH);
13242 lo = tr32(MAC_ADDR_0_LOW);
13243
13244 dev->dev_addr[5] = lo & 0xff;
13245 dev->dev_addr[4] = (lo >> 8) & 0xff;
13246 dev->dev_addr[3] = (lo >> 16) & 0xff;
13247 dev->dev_addr[2] = (lo >> 24) & 0xff;
13248 dev->dev_addr[1] = hi & 0xff;
13249 dev->dev_addr[0] = (hi >> 8) & 0xff;
13250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013251 }
13252
13253 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070013254#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070013255 if (!tg3_get_default_macaddr_sparc(tp))
13256 return 0;
13257#endif
13258 return -EINVAL;
13259 }
John W. Linville2ff43692005-09-12 14:44:20 -070013260 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013261 return 0;
13262}
13263
David S. Miller59e6b432005-05-18 22:50:10 -070013264#define BOUNDARY_SINGLE_CACHELINE 1
13265#define BOUNDARY_MULTI_CACHELINE 2
13266
13267static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
13268{
13269 int cacheline_size;
13270 u8 byte;
13271 int goal;
13272
13273 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
13274 if (byte == 0)
13275 cacheline_size = 1024;
13276 else
13277 cacheline_size = (int) byte * 4;
13278
13279 /* On 5703 and later chips, the boundary bits have no
13280 * effect.
13281 */
13282 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13283 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13284 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13285 goto out;
13286
13287#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
13288 goal = BOUNDARY_MULTI_CACHELINE;
13289#else
13290#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
13291 goal = BOUNDARY_SINGLE_CACHELINE;
13292#else
13293 goal = 0;
13294#endif
13295#endif
13296
13297 if (!goal)
13298 goto out;
13299
13300 /* PCI controllers on most RISC systems tend to disconnect
13301 * when a device tries to burst across a cache-line boundary.
13302 * Therefore, letting tg3 do so just wastes PCI bandwidth.
13303 *
13304 * Unfortunately, for PCI-E there are only limited
13305 * write-side controls for this, and thus for reads
13306 * we will still get the disconnects. We'll also waste
13307 * these PCI cycles for both read and write for chips
13308 * other than 5700 and 5701 which do not implement the
13309 * boundary bits.
13310 */
13311 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13312 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
13313 switch (cacheline_size) {
13314 case 16:
13315 case 32:
13316 case 64:
13317 case 128:
13318 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13319 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
13320 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
13321 } else {
13322 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13323 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13324 }
13325 break;
13326
13327 case 256:
13328 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
13329 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
13330 break;
13331
13332 default:
13333 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
13334 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
13335 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013336 }
David S. Miller59e6b432005-05-18 22:50:10 -070013337 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13338 switch (cacheline_size) {
13339 case 16:
13340 case 32:
13341 case 64:
13342 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13343 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13344 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
13345 break;
13346 }
13347 /* fallthrough */
13348 case 128:
13349 default:
13350 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
13351 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
13352 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013353 }
David S. Miller59e6b432005-05-18 22:50:10 -070013354 } else {
13355 switch (cacheline_size) {
13356 case 16:
13357 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13358 val |= (DMA_RWCTRL_READ_BNDRY_16 |
13359 DMA_RWCTRL_WRITE_BNDRY_16);
13360 break;
13361 }
13362 /* fallthrough */
13363 case 32:
13364 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13365 val |= (DMA_RWCTRL_READ_BNDRY_32 |
13366 DMA_RWCTRL_WRITE_BNDRY_32);
13367 break;
13368 }
13369 /* fallthrough */
13370 case 64:
13371 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13372 val |= (DMA_RWCTRL_READ_BNDRY_64 |
13373 DMA_RWCTRL_WRITE_BNDRY_64);
13374 break;
13375 }
13376 /* fallthrough */
13377 case 128:
13378 if (goal == BOUNDARY_SINGLE_CACHELINE) {
13379 val |= (DMA_RWCTRL_READ_BNDRY_128 |
13380 DMA_RWCTRL_WRITE_BNDRY_128);
13381 break;
13382 }
13383 /* fallthrough */
13384 case 256:
13385 val |= (DMA_RWCTRL_READ_BNDRY_256 |
13386 DMA_RWCTRL_WRITE_BNDRY_256);
13387 break;
13388 case 512:
13389 val |= (DMA_RWCTRL_READ_BNDRY_512 |
13390 DMA_RWCTRL_WRITE_BNDRY_512);
13391 break;
13392 case 1024:
13393 default:
13394 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
13395 DMA_RWCTRL_WRITE_BNDRY_1024);
13396 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070013397 }
David S. Miller59e6b432005-05-18 22:50:10 -070013398 }
13399
13400out:
13401 return val;
13402}
13403
Linus Torvalds1da177e2005-04-16 15:20:36 -070013404static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
13405{
13406 struct tg3_internal_buffer_desc test_desc;
13407 u32 sram_dma_descs;
13408 int i, ret;
13409
13410 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
13411
13412 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
13413 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
13414 tw32(RDMAC_STATUS, 0);
13415 tw32(WDMAC_STATUS, 0);
13416
13417 tw32(BUFMGR_MODE, 0);
13418 tw32(FTQ_RESET, 0);
13419
13420 test_desc.addr_hi = ((u64) buf_dma) >> 32;
13421 test_desc.addr_lo = buf_dma & 0xffffffff;
13422 test_desc.nic_mbuf = 0x00002100;
13423 test_desc.len = size;
13424
13425 /*
13426 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
13427 * the *second* time the tg3 driver was getting loaded after an
13428 * initial scan.
13429 *
13430 * Broadcom tells me:
13431 * ...the DMA engine is connected to the GRC block and a DMA
13432 * reset may affect the GRC block in some unpredictable way...
13433 * The behavior of resets to individual blocks has not been tested.
13434 *
13435 * Broadcom noted the GRC reset will also reset all sub-components.
13436 */
13437 if (to_device) {
13438 test_desc.cqid_sqid = (13 << 8) | 2;
13439
13440 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
13441 udelay(40);
13442 } else {
13443 test_desc.cqid_sqid = (16 << 8) | 7;
13444
13445 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
13446 udelay(40);
13447 }
13448 test_desc.flags = 0x00000005;
13449
13450 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
13451 u32 val;
13452
13453 val = *(((u32 *)&test_desc) + i);
13454 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
13455 sram_dma_descs + (i * sizeof(u32)));
13456 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
13457 }
13458 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
13459
13460 if (to_device) {
13461 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
13462 } else {
13463 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
13464 }
13465
13466 ret = -ENODEV;
13467 for (i = 0; i < 40; i++) {
13468 u32 val;
13469
13470 if (to_device)
13471 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
13472 else
13473 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
13474 if ((val & 0xffff) == sram_dma_descs) {
13475 ret = 0;
13476 break;
13477 }
13478
13479 udelay(100);
13480 }
13481
13482 return ret;
13483}
13484
David S. Millerded73402005-05-23 13:59:47 -070013485#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070013486
13487static int __devinit tg3_test_dma(struct tg3 *tp)
13488{
13489 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070013490 u32 *buf, saved_dma_rwctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013491 int ret;
13492
13493 buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
13494 if (!buf) {
13495 ret = -ENOMEM;
13496 goto out_nofree;
13497 }
13498
13499 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
13500 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
13501
David S. Miller59e6b432005-05-18 22:50:10 -070013502 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013503
13504 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13505 /* DMA read watermark not used on PCIE */
13506 tp->dma_rwctrl |= 0x00180000;
13507 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070013508 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
13509 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013510 tp->dma_rwctrl |= 0x003f0000;
13511 else
13512 tp->dma_rwctrl |= 0x003f000f;
13513 } else {
13514 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13515 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
13516 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080013517 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013518
Michael Chan4a29cc22006-03-19 13:21:12 -080013519 /* If the 5704 is behind the EPB bridge, we can
13520 * do the less restrictive ONE_DMA workaround for
13521 * better performance.
13522 */
13523 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
13524 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13525 tp->dma_rwctrl |= 0x8000;
13526 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013527 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
13528
Michael Chan49afdeb2007-02-13 12:17:03 -080013529 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
13530 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070013531 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080013532 tp->dma_rwctrl |=
13533 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
13534 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
13535 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070013536 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
13537 /* 5780 always in PCIX mode */
13538 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070013539 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13540 /* 5714 always in PCIX mode */
13541 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013542 } else {
13543 tp->dma_rwctrl |= 0x001b000f;
13544 }
13545 }
13546
13547 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
13548 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
13549 tp->dma_rwctrl &= 0xfffffff0;
13550
13551 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13552 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
13553 /* Remove this if it causes problems for some boards. */
13554 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
13555
13556 /* On 5700/5701 chips, we need to set this bit.
13557 * Otherwise the chip will issue cacheline transactions
13558 * to streamable DMA memory with not all the byte
13559 * enables turned on. This is an error on several
13560 * RISC PCI controllers, in particular sparc64.
13561 *
13562 * On 5703/5704 chips, this bit has been reassigned
13563 * a different meaning. In particular, it is used
13564 * on those chips to enable a PCI-X workaround.
13565 */
13566 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
13567 }
13568
13569 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13570
13571#if 0
13572 /* Unneeded, already done by tg3_get_invariants. */
13573 tg3_switch_clocks(tp);
13574#endif
13575
13576 ret = 0;
13577 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13578 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
13579 goto out;
13580
David S. Miller59e6b432005-05-18 22:50:10 -070013581 /* It is best to perform DMA test with maximum write burst size
13582 * to expose the 5700/5701 write DMA bug.
13583 */
13584 saved_dma_rwctrl = tp->dma_rwctrl;
13585 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
13586 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13587
Linus Torvalds1da177e2005-04-16 15:20:36 -070013588 while (1) {
13589 u32 *p = buf, i;
13590
13591 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
13592 p[i] = i;
13593
13594 /* Send the buffer to the chip. */
13595 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
13596 if (ret) {
13597 printk(KERN_ERR "tg3_test_dma() Write the buffer failed %d\n", ret);
13598 break;
13599 }
13600
13601#if 0
13602 /* validate data reached card RAM correctly. */
13603 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
13604 u32 val;
13605 tg3_read_mem(tp, 0x2100 + (i*4), &val);
13606 if (le32_to_cpu(val) != p[i]) {
13607 printk(KERN_ERR " tg3_test_dma() Card buffer corrupted on write! (%d != %d)\n", val, i);
13608 /* ret = -ENODEV here? */
13609 }
13610 p[i] = 0;
13611 }
13612#endif
13613 /* Now read it back. */
13614 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
13615 if (ret) {
13616 printk(KERN_ERR "tg3_test_dma() Read the buffer failed %d\n", ret);
13617
13618 break;
13619 }
13620
13621 /* Verify it. */
13622 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
13623 if (p[i] == i)
13624 continue;
13625
David S. Miller59e6b432005-05-18 22:50:10 -070013626 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
13627 DMA_RWCTRL_WRITE_BNDRY_16) {
13628 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013629 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
13630 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13631 break;
13632 } else {
13633 printk(KERN_ERR "tg3_test_dma() buffer corrupted on read back! (%d != %d)\n", p[i], i);
13634 ret = -ENODEV;
13635 goto out;
13636 }
13637 }
13638
13639 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
13640 /* Success. */
13641 ret = 0;
13642 break;
13643 }
13644 }
David S. Miller59e6b432005-05-18 22:50:10 -070013645 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
13646 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070013647 static struct pci_device_id dma_wait_state_chipsets[] = {
13648 { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
13649 PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
13650 { },
13651 };
13652
David S. Miller59e6b432005-05-18 22:50:10 -070013653 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070013654 * now look for chipsets that are known to expose the
13655 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070013656 */
Michael Chan6d1cfba2005-06-08 14:13:14 -070013657 if (pci_dev_present(dma_wait_state_chipsets)) {
13658 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
13659 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
13660 }
13661 else
13662 /* Safe to use the calculated DMA boundary. */
13663 tp->dma_rwctrl = saved_dma_rwctrl;
13664
David S. Miller59e6b432005-05-18 22:50:10 -070013665 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
13666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013667
13668out:
13669 pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
13670out_nofree:
13671 return ret;
13672}
13673
13674static void __devinit tg3_init_link_config(struct tg3 *tp)
13675{
13676 tp->link_config.advertising =
13677 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13678 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13679 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
13680 ADVERTISED_Autoneg | ADVERTISED_MII);
13681 tp->link_config.speed = SPEED_INVALID;
13682 tp->link_config.duplex = DUPLEX_INVALID;
13683 tp->link_config.autoneg = AUTONEG_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013684 tp->link_config.active_speed = SPEED_INVALID;
13685 tp->link_config.active_duplex = DUPLEX_INVALID;
13686 tp->link_config.phy_is_low_power = 0;
13687 tp->link_config.orig_speed = SPEED_INVALID;
13688 tp->link_config.orig_duplex = DUPLEX_INVALID;
13689 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13690}
13691
13692static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
13693{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013694 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS &&
13695 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
Michael Chanfdfec1722005-07-25 12:31:48 -070013696 tp->bufmgr_config.mbuf_read_dma_low_water =
13697 DEFAULT_MB_RDMA_LOW_WATER_5705;
13698 tp->bufmgr_config.mbuf_mac_rx_low_water =
13699 DEFAULT_MB_MACRX_LOW_WATER_5705;
13700 tp->bufmgr_config.mbuf_high_water =
13701 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070013702 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13703 tp->bufmgr_config.mbuf_mac_rx_low_water =
13704 DEFAULT_MB_MACRX_LOW_WATER_5906;
13705 tp->bufmgr_config.mbuf_high_water =
13706 DEFAULT_MB_HIGH_WATER_5906;
13707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013708
Michael Chanfdfec1722005-07-25 12:31:48 -070013709 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
13710 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
13711 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
13712 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
13713 tp->bufmgr_config.mbuf_high_water_jumbo =
13714 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
13715 } else {
13716 tp->bufmgr_config.mbuf_read_dma_low_water =
13717 DEFAULT_MB_RDMA_LOW_WATER;
13718 tp->bufmgr_config.mbuf_mac_rx_low_water =
13719 DEFAULT_MB_MACRX_LOW_WATER;
13720 tp->bufmgr_config.mbuf_high_water =
13721 DEFAULT_MB_HIGH_WATER;
13722
13723 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
13724 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
13725 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
13726 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
13727 tp->bufmgr_config.mbuf_high_water_jumbo =
13728 DEFAULT_MB_HIGH_WATER_JUMBO;
13729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013730
13731 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
13732 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
13733}
13734
13735static char * __devinit tg3_phy_string(struct tg3 *tp)
13736{
13737 switch (tp->phy_id & PHY_ID_MASK) {
13738 case PHY_ID_BCM5400: return "5400";
13739 case PHY_ID_BCM5401: return "5401";
13740 case PHY_ID_BCM5411: return "5411";
13741 case PHY_ID_BCM5701: return "5701";
13742 case PHY_ID_BCM5703: return "5703";
13743 case PHY_ID_BCM5704: return "5704";
13744 case PHY_ID_BCM5705: return "5705";
13745 case PHY_ID_BCM5750: return "5750";
Michael Chan85e94ce2005-04-21 17:05:28 -070013746 case PHY_ID_BCM5752: return "5752";
Michael Chana4e2b342005-10-26 15:46:52 -070013747 case PHY_ID_BCM5714: return "5714";
Michael Chan4cf78e42005-07-25 12:29:19 -070013748 case PHY_ID_BCM5780: return "5780";
Michael Chanaf36e6b2006-03-23 01:28:06 -080013749 case PHY_ID_BCM5755: return "5755";
Michael Chand9ab5ad12006-03-20 22:27:35 -080013750 case PHY_ID_BCM5787: return "5787";
Matt Carlsond30cdd22007-10-07 23:28:35 -070013751 case PHY_ID_BCM5784: return "5784";
Michael Chan126a3362006-09-27 16:03:07 -070013752 case PHY_ID_BCM5756: return "5722/5756";
Michael Chanb5d37722006-09-27 16:06:21 -070013753 case PHY_ID_BCM5906: return "5906";
Matt Carlson9936bcf2007-10-10 18:03:07 -070013754 case PHY_ID_BCM5761: return "5761";
Linus Torvalds1da177e2005-04-16 15:20:36 -070013755 case PHY_ID_BCM8002: return "8002/serdes";
13756 case 0: return "serdes";
13757 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070013758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013759}
13760
Michael Chanf9804dd2005-09-27 12:13:10 -070013761static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
13762{
13763 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
13764 strcpy(str, "PCI Express");
13765 return str;
13766 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
13767 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
13768
13769 strcpy(str, "PCIX:");
13770
13771 if ((clock_ctrl == 7) ||
13772 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
13773 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
13774 strcat(str, "133MHz");
13775 else if (clock_ctrl == 0)
13776 strcat(str, "33MHz");
13777 else if (clock_ctrl == 2)
13778 strcat(str, "50MHz");
13779 else if (clock_ctrl == 4)
13780 strcat(str, "66MHz");
13781 else if (clock_ctrl == 6)
13782 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070013783 } else {
13784 strcpy(str, "PCI:");
13785 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
13786 strcat(str, "66MHz");
13787 else
13788 strcat(str, "33MHz");
13789 }
13790 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
13791 strcat(str, ":32-bit");
13792 else
13793 strcat(str, ":64-bit");
13794 return str;
13795}
13796
Michael Chan8c2dc7e2005-12-19 16:26:02 -080013797static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013798{
13799 struct pci_dev *peer;
13800 unsigned int func, devnr = tp->pdev->devfn & ~7;
13801
13802 for (func = 0; func < 8; func++) {
13803 peer = pci_get_slot(tp->pdev->bus, devnr | func);
13804 if (peer && peer != tp->pdev)
13805 break;
13806 pci_dev_put(peer);
13807 }
Michael Chan16fe9d72005-12-13 21:09:54 -080013808 /* 5704 can be configured in single-port mode, set peer to
13809 * tp->pdev in that case.
13810 */
13811 if (!peer) {
13812 peer = tp->pdev;
13813 return peer;
13814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013815
13816 /*
13817 * We don't need to keep the refcount elevated; there's no way
13818 * to remove one half of this device without removing the other
13819 */
13820 pci_dev_put(peer);
13821
13822 return peer;
13823}
13824
David S. Miller15f98502005-05-18 22:49:26 -070013825static void __devinit tg3_init_coal(struct tg3 *tp)
13826{
13827 struct ethtool_coalesce *ec = &tp->coal;
13828
13829 memset(ec, 0, sizeof(*ec));
13830 ec->cmd = ETHTOOL_GCOALESCE;
13831 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
13832 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
13833 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
13834 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
13835 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
13836 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
13837 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
13838 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
13839 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
13840
13841 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
13842 HOSTCC_MODE_CLRTICK_TXBD)) {
13843 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
13844 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
13845 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
13846 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
13847 }
Michael Chand244c892005-07-05 14:42:33 -070013848
13849 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
13850 ec->rx_coalesce_usecs_irq = 0;
13851 ec->tx_coalesce_usecs_irq = 0;
13852 ec->stats_block_coalesce_usecs = 0;
13853 }
David S. Miller15f98502005-05-18 22:49:26 -070013854}
13855
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080013856static const struct net_device_ops tg3_netdev_ops = {
13857 .ndo_open = tg3_open,
13858 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080013859 .ndo_start_xmit = tg3_start_xmit,
13860 .ndo_get_stats = tg3_get_stats,
13861 .ndo_validate_addr = eth_validate_addr,
13862 .ndo_set_multicast_list = tg3_set_rx_mode,
13863 .ndo_set_mac_address = tg3_set_mac_addr,
13864 .ndo_do_ioctl = tg3_ioctl,
13865 .ndo_tx_timeout = tg3_tx_timeout,
13866 .ndo_change_mtu = tg3_change_mtu,
13867#if TG3_VLAN_TAG_USED
13868 .ndo_vlan_rx_register = tg3_vlan_rx_register,
13869#endif
13870#ifdef CONFIG_NET_POLL_CONTROLLER
13871 .ndo_poll_controller = tg3_poll_controller,
13872#endif
13873};
13874
13875static const struct net_device_ops tg3_netdev_ops_dma_bug = {
13876 .ndo_open = tg3_open,
13877 .ndo_stop = tg3_close,
13878 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080013879 .ndo_get_stats = tg3_get_stats,
13880 .ndo_validate_addr = eth_validate_addr,
13881 .ndo_set_multicast_list = tg3_set_rx_mode,
13882 .ndo_set_mac_address = tg3_set_mac_addr,
13883 .ndo_do_ioctl = tg3_ioctl,
13884 .ndo_tx_timeout = tg3_tx_timeout,
13885 .ndo_change_mtu = tg3_change_mtu,
13886#if TG3_VLAN_TAG_USED
13887 .ndo_vlan_rx_register = tg3_vlan_rx_register,
13888#endif
13889#ifdef CONFIG_NET_POLL_CONTROLLER
13890 .ndo_poll_controller = tg3_poll_controller,
13891#endif
13892};
13893
Linus Torvalds1da177e2005-04-16 15:20:36 -070013894static int __devinit tg3_init_one(struct pci_dev *pdev,
13895 const struct pci_device_id *ent)
13896{
13897 static int tg3_version_printed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013898 struct net_device *dev;
13899 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000013900 int i, err, pm_cap;
13901 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070013902 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080013903 u64 dma_mask, persist_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013904
13905 if (tg3_version_printed++ == 0)
13906 printk(KERN_INFO "%s", version);
13907
13908 err = pci_enable_device(pdev);
13909 if (err) {
13910 printk(KERN_ERR PFX "Cannot enable PCI device, "
13911 "aborting.\n");
13912 return err;
13913 }
13914
Linus Torvalds1da177e2005-04-16 15:20:36 -070013915 err = pci_request_regions(pdev, DRV_MODULE_NAME);
13916 if (err) {
13917 printk(KERN_ERR PFX "Cannot obtain PCI resources, "
13918 "aborting.\n");
13919 goto err_out_disable_pdev;
13920 }
13921
13922 pci_set_master(pdev);
13923
13924 /* Find power-management capability. */
13925 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
13926 if (pm_cap == 0) {
13927 printk(KERN_ERR PFX "Cannot find PowerManagement capability, "
13928 "aborting.\n");
13929 err = -EIO;
13930 goto err_out_free_res;
13931 }
13932
Matt Carlsonfe5f5782009-09-01 13:09:39 +000013933 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013934 if (!dev) {
13935 printk(KERN_ERR PFX "Etherdev alloc failed, aborting.\n");
13936 err = -ENOMEM;
13937 goto err_out_free_res;
13938 }
13939
Linus Torvalds1da177e2005-04-16 15:20:36 -070013940 SET_NETDEV_DEV(dev, &pdev->dev);
13941
Linus Torvalds1da177e2005-04-16 15:20:36 -070013942#if TG3_VLAN_TAG_USED
13943 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013944#endif
13945
13946 tp = netdev_priv(dev);
13947 tp->pdev = pdev;
13948 tp->dev = dev;
13949 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013950 tp->rx_mode = TG3_DEF_RX_MODE;
13951 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070013952
Linus Torvalds1da177e2005-04-16 15:20:36 -070013953 if (tg3_debug > 0)
13954 tp->msg_enable = tg3_debug;
13955 else
13956 tp->msg_enable = TG3_DEF_MSG_ENABLE;
13957
13958 /* The word/byte swap controls here control register access byte
13959 * swapping. DMA data byte swapping is controlled in the GRC_MODE
13960 * setting below.
13961 */
13962 tp->misc_host_ctrl =
13963 MISC_HOST_CTRL_MASK_PCI_INT |
13964 MISC_HOST_CTRL_WORD_SWAP |
13965 MISC_HOST_CTRL_INDIR_ACCESS |
13966 MISC_HOST_CTRL_PCISTATE_RW;
13967
13968 /* The NONFRM (non-frame) byte/word swap controls take effect
13969 * on descriptor entries, anything which isn't packet data.
13970 *
13971 * The StrongARM chips on the board (one for tx, one for rx)
13972 * are running in big-endian mode.
13973 */
13974 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
13975 GRC_MODE_WSWAP_NONFRM_DATA);
13976#ifdef __BIG_ENDIAN
13977 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
13978#endif
13979 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013980 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000013981 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013982
Matt Carlsond5fe4882008-11-21 17:20:32 -080013983 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010013984 if (!tp->regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013985 printk(KERN_ERR PFX "Cannot map device registers, "
13986 "aborting.\n");
13987 err = -ENOMEM;
13988 goto err_out_free_dev;
13989 }
13990
13991 tg3_init_link_config(tp);
13992
Linus Torvalds1da177e2005-04-16 15:20:36 -070013993 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
13994 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013995
Matt Carlson646c9ed2009-09-01 12:58:41 +000013996 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
13997 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
13998 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
13999 for (i = 0; i < TG3_IRQ_MAX_VECS; i++) {
14000 struct tg3_napi *tnapi = &tp->napi[i];
14001
14002 tnapi->tp = tp;
14003 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
14004
14005 tnapi->int_mbox = intmbx;
14006 if (i < 4)
14007 intmbx += 0x8;
14008 else
14009 intmbx += 0x4;
14010
14011 tnapi->consmbox = rcvmbx;
14012 tnapi->prodmbox = sndmbx;
14013
14014 if (i)
14015 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
14016 else
14017 tnapi->coal_now = HOSTCC_MODE_NOW;
14018
14019 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
14020 break;
14021
14022 /*
14023 * If we support MSIX, we'll be using RSS. If we're using
14024 * RSS, the first vector only handles link interrupts and the
14025 * remaining vectors handle rx and tx interrupts. Reuse the
14026 * mailbox values for the next iteration. The values we setup
14027 * above are still useful for the single vectored mode.
14028 */
14029 if (!i)
14030 continue;
14031
14032 rcvmbx += 0x8;
14033
14034 if (sndmbx & 0x4)
14035 sndmbx -= 0x4;
14036 else
14037 sndmbx += 0xc;
14038 }
14039
Matt Carlson8ef04422009-08-28 14:01:37 +000014040 netif_napi_add(dev, &tp->napi[0].napi, tg3_poll, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014041 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014042 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014043 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014044
14045 err = tg3_get_invariants(tp);
14046 if (err) {
14047 printk(KERN_ERR PFX "Problem fetching invariants of chip, "
14048 "aborting.\n");
14049 goto err_out_iounmap;
14050 }
14051
Matt Carlson92c6b8d2009-11-02 14:23:27 +000014052 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Stephen Hemminger00829822008-11-20 20:14:53 -080014053 dev->netdev_ops = &tg3_netdev_ops;
14054 else
14055 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
14056
14057
Michael Chan4a29cc22006-03-19 13:21:12 -080014058 /* The EPB bridge inside 5714, 5715, and 5780 and any
14059 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080014060 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
14061 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
14062 * do DMA address check in tg3_start_xmit().
14063 */
Michael Chan4a29cc22006-03-19 13:21:12 -080014064 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070014065 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080014066 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070014067 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080014068#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070014069 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014070#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080014071 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070014072 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080014073
14074 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070014075 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080014076 err = pci_set_dma_mask(pdev, dma_mask);
14077 if (!err) {
14078 dev->features |= NETIF_F_HIGHDMA;
14079 err = pci_set_consistent_dma_mask(pdev,
14080 persist_dma_mask);
14081 if (err < 0) {
14082 printk(KERN_ERR PFX "Unable to obtain 64 bit "
14083 "DMA for consistent allocations\n");
14084 goto err_out_iounmap;
14085 }
14086 }
14087 }
Yang Hongyang284901a2009-04-06 19:01:15 -070014088 if (err || dma_mask == DMA_BIT_MASK(32)) {
14089 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080014090 if (err) {
14091 printk(KERN_ERR PFX "No usable DMA configuration, "
14092 "aborting.\n");
14093 goto err_out_iounmap;
14094 }
14095 }
14096
Michael Chanfdfec1722005-07-25 12:31:48 -070014097 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014098
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014099 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014100 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014101
Linus Torvalds1da177e2005-04-16 15:20:36 -070014102 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
14103 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
14104 }
14105 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14106 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
14107 tp->pci_chip_rev_id == CHIPREV_ID_5705_A0 ||
Michael Chanc7835a72006-11-15 21:14:42 -080014108 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070014109 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
14110 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
14111 } else {
Michael Chan7f62ad52007-02-20 23:25:40 -080014112 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014113 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
Matt Carlson9e9fd122009-01-19 16:57:45 -080014114 tp->fw_needed = FIRMWARE_TG3TSO5;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014115 else
Matt Carlson9e9fd122009-01-19 16:57:45 -080014116 tp->fw_needed = FIRMWARE_TG3TSO;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014118
Michael Chan4e3a7aa2006-03-20 17:47:44 -080014119 /* TSO is on by default on chips that support hardware TSO.
14120 * Firmware TSO on older chips gives lower performance, so it
14121 * is off by default, but can be enabled using ethtool.
14122 */
Michael Chanb0026622006-07-03 19:42:14 -070014123 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Matt Carlson027455a2008-12-21 20:19:30 -080014124 if (dev->features & NETIF_F_IP_CSUM)
14125 dev->features |= NETIF_F_TSO;
14126 if ((dev->features & NETIF_F_IPV6_CSUM) &&
14127 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2))
Michael Chanb0026622006-07-03 19:42:14 -070014128 dev->features |= NETIF_F_TSO6;
Matt Carlson57e69832008-05-25 23:48:31 -070014129 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
14130 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14131 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014132 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014133 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14134 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson9936bcf2007-10-10 18:03:07 -070014135 dev->features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070014136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014137
Linus Torvalds1da177e2005-04-16 15:20:36 -070014138
14139 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
14140 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
14141 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
14142 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
14143 tp->rx_pending = 63;
14144 }
14145
Linus Torvalds1da177e2005-04-16 15:20:36 -070014146 err = tg3_get_device_address(tp);
14147 if (err) {
14148 printk(KERN_ERR PFX "Could not obtain valid ethernet address, "
14149 "aborting.\n");
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014150 goto err_out_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014151 }
14152
Matt Carlson0d3031d2007-10-10 18:02:43 -070014153 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080014154 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080014155 if (!tp->aperegs) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014156 printk(KERN_ERR PFX "Cannot map APE registers, "
14157 "aborting.\n");
14158 err = -ENOMEM;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014159 goto err_out_fw;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014160 }
14161
14162 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000014163
14164 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
14165 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014166 }
14167
Matt Carlsonc88864d2007-11-12 21:07:01 -080014168 /*
14169 * Reset chip in case UNDI or EFI driver did not shutdown
14170 * DMA self test will enable WDMAC and we'll see (spurious)
14171 * pending DMA on the PCI bus at that point.
14172 */
14173 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
14174 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
14175 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
14176 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14177 }
14178
14179 err = tg3_test_dma(tp);
14180 if (err) {
14181 printk(KERN_ERR PFX "DMA engine test failed, aborting.\n");
14182 goto err_out_apeunmap;
14183 }
14184
Matt Carlsonc88864d2007-11-12 21:07:01 -080014185 /* flow control autonegotiation is default behavior */
14186 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
Steve Glendinninge18ce342008-12-16 02:00:00 -080014187 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlsonc88864d2007-11-12 21:07:01 -080014188
14189 tg3_init_coal(tp);
14190
Michael Chanc49a1562006-12-17 17:07:29 -080014191 pci_set_drvdata(pdev, dev);
14192
Linus Torvalds1da177e2005-04-16 15:20:36 -070014193 err = register_netdev(dev);
14194 if (err) {
14195 printk(KERN_ERR PFX "Cannot register net device, "
14196 "aborting.\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070014197 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014198 }
14199
Matt Carlsondf59c942008-11-03 16:52:56 -080014200 printk(KERN_INFO "%s: Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070014201 dev->name,
14202 tp->board_part_number,
14203 tp->pci_chip_rev_id,
Michael Chanf9804dd2005-09-27 12:13:10 -070014204 tg3_bus_string(tp, str),
Johannes Berge1749612008-10-27 15:59:26 -070014205 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014206
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014207 if (tp->tg3_flags3 & TG3_FLG3_PHY_CONNECTED) {
14208 struct phy_device *phydev;
14209 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsondf59c942008-11-03 16:52:56 -080014210 printk(KERN_INFO
14211 "%s: attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000014212 tp->dev->name, phydev->drv->name,
14213 dev_name(&phydev->dev));
14214 } else
Matt Carlsondf59c942008-11-03 16:52:56 -080014215 printk(KERN_INFO
14216 "%s: attached PHY is %s (%s Ethernet) (WireSpeed[%d])\n",
14217 tp->dev->name, tg3_phy_string(tp),
14218 ((tp->tg3_flags & TG3_FLAG_10_100_ONLY) ? "10/100Base-TX" :
14219 ((tp->tg3_flags2 & TG3_FLG2_ANY_SERDES) ? "1000Base-SX" :
14220 "10/100/1000Base-T")),
14221 (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED) == 0);
14222
14223 printk(KERN_INFO "%s: RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070014224 dev->name,
14225 (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
14226 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
14227 (tp->tg3_flags & TG3_FLAG_USE_MI_INTERRUPT) != 0,
14228 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014229 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
Michael Chan4a29cc22006-03-19 13:21:12 -080014230 printk(KERN_INFO "%s: dma_rwctrl[%08x] dma_mask[%d-bit]\n",
14231 dev->name, tp->dma_rwctrl,
Yang Hongyang284901a2009-04-06 19:01:15 -070014232 (pdev->dma_mask == DMA_BIT_MASK(32)) ? 32 :
Yang Hongyang50cf1562009-04-06 19:01:14 -070014233 (((u64) pdev->dma_mask == DMA_BIT_MASK(40)) ? 40 : 64));
Linus Torvalds1da177e2005-04-16 15:20:36 -070014234
14235 return 0;
14236
Matt Carlson0d3031d2007-10-10 18:02:43 -070014237err_out_apeunmap:
14238 if (tp->aperegs) {
14239 iounmap(tp->aperegs);
14240 tp->aperegs = NULL;
14241 }
14242
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014243err_out_fw:
14244 if (tp->fw)
14245 release_firmware(tp->fw);
14246
Linus Torvalds1da177e2005-04-16 15:20:36 -070014247err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070014248 if (tp->regs) {
14249 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014250 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014252
14253err_out_free_dev:
14254 free_netdev(dev);
14255
14256err_out_free_res:
14257 pci_release_regions(pdev);
14258
14259err_out_disable_pdev:
14260 pci_disable_device(pdev);
14261 pci_set_drvdata(pdev, NULL);
14262 return err;
14263}
14264
14265static void __devexit tg3_remove_one(struct pci_dev *pdev)
14266{
14267 struct net_device *dev = pci_get_drvdata(pdev);
14268
14269 if (dev) {
14270 struct tg3 *tp = netdev_priv(dev);
14271
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080014272 if (tp->fw)
14273 release_firmware(tp->fw);
14274
Michael Chan7faa0062006-02-02 17:29:28 -080014275 flush_scheduled_work();
Matt Carlson158d7ab2008-05-29 01:37:54 -070014276
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014277 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
14278 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070014279 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014280 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070014281
Linus Torvalds1da177e2005-04-16 15:20:36 -070014282 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014283 if (tp->aperegs) {
14284 iounmap(tp->aperegs);
14285 tp->aperegs = NULL;
14286 }
Michael Chan68929142005-08-09 20:17:14 -070014287 if (tp->regs) {
14288 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014289 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014291 free_netdev(dev);
14292 pci_release_regions(pdev);
14293 pci_disable_device(pdev);
14294 pci_set_drvdata(pdev, NULL);
14295 }
14296}
14297
14298static int tg3_suspend(struct pci_dev *pdev, pm_message_t state)
14299{
14300 struct net_device *dev = pci_get_drvdata(pdev);
14301 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014302 pci_power_t target_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014303 int err;
14304
Michael Chan3e0c95f2007-08-03 20:56:54 -070014305 /* PCI register 4 needs to be saved whether netif_running() or not.
14306 * MSI address and data need to be saved if using MSI and
14307 * netif_running().
14308 */
14309 pci_save_state(pdev);
14310
Linus Torvalds1da177e2005-04-16 15:20:36 -070014311 if (!netif_running(dev))
14312 return 0;
14313
Michael Chan7faa0062006-02-02 17:29:28 -080014314 flush_scheduled_work();
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014315 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014316 tg3_netif_stop(tp);
14317
14318 del_timer_sync(&tp->timer);
14319
David S. Millerf47c11e2005-06-24 20:18:35 -070014320 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014321 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070014322 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014323
14324 netif_device_detach(dev);
14325
David S. Millerf47c11e2005-06-24 20:18:35 -070014326 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070014327 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080014328 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070014329 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014330
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070014331 target_state = pdev->pm_cap ? pci_target_state(pdev) : PCI_D3hot;
14332
14333 err = tg3_set_power_state(tp, target_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014334 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014335 int err2;
14336
David S. Millerf47c11e2005-06-24 20:18:35 -070014337 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014338
Michael Chan6a9eba12005-12-13 21:08:58 -080014339 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014340 err2 = tg3_restart_hw(tp, 1);
14341 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070014342 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014343
14344 tp->timer.expires = jiffies + tp->timer_offset;
14345 add_timer(&tp->timer);
14346
14347 netif_device_attach(dev);
14348 tg3_netif_start(tp);
14349
Michael Chanb9ec6c12006-07-25 16:37:27 -070014350out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014351 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014352
14353 if (!err2)
14354 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014355 }
14356
14357 return err;
14358}
14359
14360static int tg3_resume(struct pci_dev *pdev)
14361{
14362 struct net_device *dev = pci_get_drvdata(pdev);
14363 struct tg3 *tp = netdev_priv(dev);
14364 int err;
14365
Michael Chan3e0c95f2007-08-03 20:56:54 -070014366 pci_restore_state(tp->pdev);
14367
Linus Torvalds1da177e2005-04-16 15:20:36 -070014368 if (!netif_running(dev))
14369 return 0;
14370
Michael Chanbc1c7562006-03-20 17:48:03 -080014371 err = tg3_set_power_state(tp, PCI_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014372 if (err)
14373 return err;
14374
14375 netif_device_attach(dev);
14376
David S. Millerf47c11e2005-06-24 20:18:35 -070014377 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014378
Michael Chan6a9eba12005-12-13 21:08:58 -080014379 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070014380 err = tg3_restart_hw(tp, 1);
14381 if (err)
14382 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014383
14384 tp->timer.expires = jiffies + tp->timer_offset;
14385 add_timer(&tp->timer);
14386
Linus Torvalds1da177e2005-04-16 15:20:36 -070014387 tg3_netif_start(tp);
14388
Michael Chanb9ec6c12006-07-25 16:37:27 -070014389out:
David S. Millerf47c11e2005-06-24 20:18:35 -070014390 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014391
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014392 if (!err)
14393 tg3_phy_start(tp);
14394
Michael Chanb9ec6c12006-07-25 16:37:27 -070014395 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014396}
14397
14398static struct pci_driver tg3_driver = {
14399 .name = DRV_MODULE_NAME,
14400 .id_table = tg3_pci_tbl,
14401 .probe = tg3_init_one,
14402 .remove = __devexit_p(tg3_remove_one),
14403 .suspend = tg3_suspend,
14404 .resume = tg3_resume
14405};
14406
14407static int __init tg3_init(void)
14408{
Jeff Garzik29917622006-08-19 17:48:59 -040014409 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014410}
14411
14412static void __exit tg3_cleanup(void)
14413{
14414 pci_unregister_driver(&tg3_driver);
14415}
14416
14417module_init(tg3_init);
14418module_exit(tg3_cleanup);